Changeset 430


Ignore:
Timestamp:
May 3, 2006 6:16:06 PM (18 years ago)
Author:
bennylp
Message:

Added maximum duration and auto-quit option in siprtp sample.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip-apps/src/samples/siprtp.c

    r412 r430  
    3535" Program options:\n" 
    3636"   --count=N,        -c    Set number of calls to create (default:1) \n" 
     37"   --duration=SEC,   -d    Set maximum call duration (default:unlimited) \n" 
     38"   --auto-quit,      -q    Quit when calls have been completed (default:no)\n" 
    3739"\n" 
    3840" Address and ports options:\n" 
     
    129131    pj_time_val          response_time; 
    130132    pj_time_val          connect_time; 
     133 
     134    pj_timer_entry       d_timer;           /**< Disconnect timer.      */ 
    131135}; 
    132136 
     
    135139{ 
    136140    unsigned             max_calls; 
     141    unsigned             uac_calls; 
     142    unsigned             duration; 
     143    pj_bool_t            auto_quit; 
    137144    unsigned             thread_count; 
    138145    int                  sip_port; 
     
    188195                               struct call *call, 
    189196                               pjmedia_sdp_session **p_sdp); 
     197 
     198/* Hangup call */ 
     199static void hangup_call(unsigned index); 
    190200 
    191201/* Destroy the call's media */ 
     
    524534                                   dst_uri,             /* remote target    */ 
    525535                                   &dlg);               /* dialog           */ 
    526     if (status != PJ_SUCCESS) 
     536    if (status != PJ_SUCCESS) { 
     537        ++app.uac_calls; 
    527538        return status; 
     539    } 
    528540 
    529541    /* Create SDP */ 
     
    534546    if (status != PJ_SUCCESS) { 
    535547        pjsip_dlg_terminate(dlg); 
     548        ++app.uac_calls; 
    536549        return status; 
    537550    } 
     
    682695 
    683696 
     697/* Callback timer to disconnect call (limiting call duration) */ 
     698static void timer_disconnect_call( pj_timer_heap_t *timer_heap, 
     699                                   struct pj_timer_entry *entry) 
     700{ 
     701    struct call *call = entry->user_data; 
     702 
     703    PJ_UNUSED_ARG(timer_heap); 
     704 
     705    entry->id = 0; 
     706    hangup_call(call->index); 
     707} 
     708 
     709 
    684710/* Callback to be called when invite session's state has changed: */ 
    685711static void call_on_state_changed( pjsip_inv_session *inv,  
     
    696722         
    697723        pj_time_val null_time = {0, 0}; 
     724 
     725        if (call->d_timer.id != 0) { 
     726            pjsip_endpt_cancel_timer(app.sip_endpt, &call->d_timer); 
     727            call->d_timer.id = 0; 
     728        } 
    698729 
    699730        PJ_LOG(3,(THIS_FILE, "Call #%d disconnected. Reason=%s", 
     
    713744        call->connect_time = null_time; 
    714745 
     746        ++app.uac_calls; 
    715747 
    716748    } else if (inv->state == PJSIP_INV_STATE_CONFIRMED) { 
     
    728760                  PJ_TIME_VAL_MSEC(t))); 
    729761 
     762        if (app.duration != 0) { 
     763            call->d_timer.id = 1; 
     764            call->d_timer.user_data = call; 
     765            call->d_timer.cb = &timer_disconnect_call; 
     766 
     767            t.sec = app.duration; 
     768            t.msec = 0; 
     769 
     770            pjsip_endpt_schedule_timer(app.sip_endpt, &call->d_timer, &t); 
     771        } 
     772 
    730773    } else if ( inv->state == PJSIP_INV_STATE_EARLY || 
    731774                inv->state == PJSIP_INV_STATE_CONNECTING) { 
     
    775818    struct pj_getopt_option long_options[] = { 
    776819        { "count",          1, 0, 'c' }, 
     820        { "duration",       1, 0, 'd' }, 
     821        { "auto-quit",      0, 0, 'q' }, 
    777822        { "local-port",     1, 0, 'p' }, 
    778823        { "rtp-port",       1, 0, 'r' }, 
     
    820865    /* Parse options */ 
    821866    pj_optind = 0; 
    822     while((c=pj_getopt_long(argc,argv, "c:p:r:i:l:",  
     867    while((c=pj_getopt_long(argc,argv, "c:d:p:r:i:l:q",  
    823868                            long_options, &option_index))!=-1)  
    824869    { 
     
    831876            } 
    832877            break; 
     878        case 'd': 
     879            app.duration = atoi(pj_optarg); 
     880            break; 
     881        case 'q': 
     882            app.auto_quit = 1; 
     883            break; 
     884 
    833885        case 'p': 
    834886            app.sip_port = atoi(pj_optarg); 
     
    17601812        return 1; 
    17611813 
     1814    /* Verify options: */ 
     1815 
     1816    /* Auto-quit can not be specified for UAS */ 
     1817    if (app.auto_quit && app.uri_to_call.slen == 0) { 
     1818        printf("Error: --auto-quit option only valid for outgoing " 
     1819               "mode (UAC) only\n"); 
     1820        return 1; 
     1821    } 
     1822 
    17621823    /* Init logging */ 
    17631824    status = app_logging_init(); 
     
    18051866        } 
    18061867 
     1868        if (app.auto_quit) { 
     1869            /* Wait for calls to complete */ 
     1870            while (app.uac_calls < app.max_calls) 
     1871                pj_thread_sleep(100); 
     1872            pj_thread_sleep(200); 
     1873        } else { 
     1874            /* Start user interface loop */ 
     1875            console_main(); 
     1876        } 
     1877 
    18071878    } else { 
    18081879 
    18091880        PJ_LOG(3,(THIS_FILE, "Ready for incoming calls (max=%d)",  
    18101881                  app.max_calls)); 
    1811     } 
    1812  
    1813     /* Start user interface loop */ 
    1814     console_main(); 
     1882 
     1883        /* Start user interface loop */ 
     1884        console_main(); 
     1885 
     1886    } 
    18151887 
    18161888     
Note: See TracChangeset for help on using the changeset viewer.