Ignore:
Timestamp:
Feb 26, 2006 9:23:45 PM (18 years ago)
Author:
bennylp
Message:

Major redesign in pjsua: call is indexed by number, multiple accounts, configurable max-calls, more auto-xxx features, fixed bugs in save_settings(), etc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip/src/pjsua/pjsua_opt.c

    r223 r236  
    4444    puts("  pjsua [options]"); 
    4545    puts(""); 
    46     puts("  [sip-url]   Default URL to invite."); 
    47     puts(""); 
    4846    puts("General options:"); 
    4947    puts("  --help              Display this help screen"); 
     
    5654    puts("  --app-log-level=N   Set log max level for stdout display to N"); 
    5755    puts(""); 
     56    puts("SIP Account options:"); 
     57    puts("  --id=url            Set the URL of local ID (used in From header)"); 
     58    puts("  --contact=url       Override the Contact information"); 
     59    puts("  --proxy=url         Set the URL of proxy server"); 
     60    puts(""); 
     61    puts("SIP Account Registration Options:"); 
     62    puts("  --registrar=url     Set the URL of registrar server"); 
     63    puts("  --reg-timeout=secs  Set registration interval to secs (default 3600)"); 
     64    puts(""); 
     65    puts("SIP Account Control:"); 
     66    puts("  --next-account      Add more account"); 
     67    puts(""); 
    5868    puts("Authentication options:"); 
    5969    puts("  --realm=string      Set realm"); 
    6070    puts("  --username=string   Set authentication username"); 
    6171    puts("  --password=string   Set authentication password"); 
    62     puts(""); 
    63     puts("SIP options:"); 
    64     puts("  --id=url            Set the URL of local ID (used in From header)"); 
    65     puts("  --contact=url       Override the Contact information"); 
    66     puts("  --proxy=url         Set the URL of proxy server"); 
    67     //puts("  --outbound=url      Set the URL of outbound proxy server"); 
    68     puts(""); 
    69     puts("Registration Options:"); 
    70     puts("  --registrar=url     Set the URL of registrar server"); 
    71     puts("  --reg-timeout=secs  Set registration interval to secs (default 3600)"); 
     72    puts("  --next-cred         Add more credential"); 
    7273    puts(""); 
    7374    puts("Transport Options:"); 
    74     puts("  --local-port=port   Set TCP/UDP port"); 
     75    puts("  --local-port=port        Set TCP/UDP port"); 
     76    puts("  --outbound=url           Set the URL of outbound proxy server"); 
    7577    puts("  --use-stun1=host[:port]"); 
    76     puts("  --use-stun2=host[:port]  Use STUN and set host name and port of STUN servers"); 
     78    puts("  --use-stun2=host[:port]  Resolve local IP with the specified STUN servers"); 
    7779    puts(""); 
    7880    puts("Media Options:"); 
    7981    puts("  --null-audio        Use NULL audio device"); 
    80     //puts("  --wav-file=file     Play WAV file in conference bridge"); 
     82    puts("  --play-file=file    Play WAV file in conference bridge"); 
     83    puts("  --auto-play         Automatically play the file (to incoming calls only)"); 
     84    puts("  --auto-loop         Automatically loop incoming RTP to outgoing RTP"); 
     85    puts("  --auto-conf         Automatically put incoming calls to conference"); 
    8186    puts(""); 
    8287    puts("Buddy List (can be more than one):"); 
     
    8590    puts("User Agent options:"); 
    8691    puts("  --auto-answer=code  Automatically answer incoming calls with code (e.g. 200)"); 
    87     puts("  --auto-play=file    Automatically play WAVE file to incoming calls"); 
     92    puts("  --max-calls=N       Maximum number of concurrent calls (default:4, max:255)"); 
    8893    puts(""); 
    8994    fflush(stdout); 
     
    200205           OPT_USE_STUN1, OPT_USE_STUN2,  
    201206           OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE, 
    202            OPT_AUTO_ANSWER, OPT_AUTO_HANGUP, OPT_AUTO_PLAY}; 
     207           OPT_AUTO_ANSWER, OPT_AUTO_HANGUP, OPT_AUTO_PLAY, OPT_AUTO_LOOP, 
     208           OPT_AUTO_CONF, 
     209           OPT_PLAY_FILE, 
     210           OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS, 
     211    }; 
    203212    struct option long_options[] = { 
    204213        { "config-file",1, 0, OPT_CONFIG_FILE}, 
     
    226235        { "auto-answer",1, 0, OPT_AUTO_ANSWER}, 
    227236        { "auto-hangup",1, 0, OPT_AUTO_HANGUP}, 
    228         { "auto-play",  1, 0, OPT_AUTO_PLAY}, 
     237        { "auto-play",  0, 0, OPT_AUTO_PLAY}, 
     238        { "auto-loop",  0, 0, OPT_AUTO_LOOP}, 
     239        { "auto-conf",  0, 0, OPT_AUTO_CONF}, 
     240        { "play-file",  1, 0, OPT_PLAY_FILE}, 
     241        { "next-account",0,0, OPT_NEXT_ACCOUNT}, 
     242        { "next-cred",  0, 0, OPT_NEXT_CRED}, 
     243        { "max-calls",  1, 0, OPT_MAX_CALLS}, 
    229244        { NULL, 0, 0, 0} 
    230245    }; 
    231246    pj_status_t status; 
     247    pjsua_acc *cur_acc; 
     248    pjsip_cred_info *cur_cred; 
    232249    char *config_file = NULL; 
    233250 
     
    248265            return status; 
    249266    } 
     267 
     268 
     269    cur_acc = &pjsua.acc[0]; 
     270    cur_cred = &pjsua.cred_info[0]; 
    250271 
    251272 
     
    308329                return PJ_EINVAL; 
    309330            } 
    310             pjsua.proxy = pj_str(optarg); 
     331            cur_acc->proxy = pj_str(optarg); 
    311332            break; 
    312333 
     
    324345                return PJ_EINVAL; 
    325346            } 
    326             pjsua.registrar_uri = pj_str(optarg); 
     347            cur_acc->reg_uri = pj_str(optarg); 
    327348            break; 
    328349 
    329350        case OPT_REG_TIMEOUT:   /* reg-timeout */ 
    330             pjsua.reg_timeout = pj_strtoul(pj_cstr(&tmp,optarg)); 
    331             if (pjsua.reg_timeout < 1 || pjsua.reg_timeout > 3600) { 
     351            cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,optarg)); 
     352            if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) { 
    332353                printf("Error: invalid value for --reg-timeout (expecting 1-3600)\n"); 
    333354                return PJ_EINVAL; 
     
    340361                return PJ_EINVAL; 
    341362            } 
    342             pjsua.local_uri = pj_str(optarg); 
     363            cur_acc->local_uri = pj_str(optarg); 
    343364            break; 
    344365 
     
    348369                return PJ_EINVAL; 
    349370            } 
    350             pjsua.contact_uri = pj_str(optarg); 
     371            cur_acc->contact_uri = pj_str(optarg); 
     372            break; 
     373 
     374        case OPT_NEXT_ACCOUNT: /* Add more account. */ 
     375            pjsua.acc_cnt++; 
     376            cur_acc = &pjsua.acc[pjsua.acc_cnt - 1]; 
    351377            break; 
    352378 
    353379        case OPT_USERNAME:   /* Default authentication user */ 
    354             if (!pjsua.cred_count) pjsua.cred_count = 1; 
    355             pjsua.cred_info[0].username = pj_str(optarg); 
     380            if (pjsua.cred_count==0) pjsua.cred_count=1; 
     381            cur_cred->username = pj_str(optarg); 
    356382            break; 
    357383 
    358384        case OPT_REALM:     /* Default authentication realm. */ 
    359             if (!pjsua.cred_count) pjsua.cred_count = 1; 
    360             pjsua.cred_info[0].realm = pj_str(optarg); 
     385            if (pjsua.cred_count==0) pjsua.cred_count=1; 
     386            cur_cred->realm = pj_str(optarg); 
    361387            break; 
    362388 
    363389        case OPT_PASSWORD:   /* authentication password */ 
    364             if (!pjsua.cred_count) pjsua.cred_count = 1; 
    365             pjsua.cred_info[0].data_type = 0; 
    366             pjsua.cred_info[0].data = pj_str(optarg); 
     390            if (pjsua.cred_count==0) pjsua.cred_count=1; 
     391            cur_cred->data_type = 0; 
     392            cur_cred->data = pj_str(optarg); 
     393            break; 
     394 
     395        case OPT_NEXT_CRED: /* Next credential */ 
     396            pjsua.cred_count++; 
     397            cur_cred = &pjsua.cred_info[pjsua.cred_count - 1]; 
    367398            break; 
    368399 
     
    412443 
    413444        case OPT_AUTO_PLAY: 
     445            pjsua.auto_play = 1; 
     446            break; 
     447 
     448        case OPT_AUTO_LOOP: 
     449            pjsua.auto_loop = 1; 
     450            break; 
     451 
     452        case OPT_AUTO_CONF: 
     453            pjsua.auto_conf = 1; 
     454            break; 
     455 
     456        case OPT_PLAY_FILE: 
    414457            pjsua.wav_file = optarg; 
    415458            break; 
     
    422465            } 
    423466            break; 
     467 
     468        case OPT_MAX_CALLS: 
     469            pjsua.max_calls = atoi(optarg); 
     470            if (pjsua.max_calls < 1 || pjsua.max_calls > 255) { 
     471                puts("Too many calls for max-calls (1-255)"); 
     472                return -1; 
     473            } 
     474            break; 
    424475        } 
    425476    } 
     
    430481    } 
    431482 
    432     if (pjsua.reg_timeout == 0) 
    433         pjsua.reg_timeout = 3600; 
    434  
    435  
    436483    return PJ_SUCCESS; 
    437484} 
     
    439486 
    440487 
    441 static void print_invite_session(const char *title, 
    442                                  struct pjsua_inv_data *inv_data,  
    443                                 char *buf, pj_size_t size) 
     488static void print_call(const char *title, 
     489                       int call_index,  
     490                      char *buf, pj_size_t size) 
    444491{ 
    445492    int len; 
    446     pjsip_inv_session *inv = inv_data->inv; 
     493    pjsip_inv_session *inv = pjsua.calls[call_index].inv; 
    447494    pjsip_dialog *dlg = inv->dlg; 
    448495    char userinfo[128]; 
     
    516563void pjsua_dump(void) 
    517564{ 
    518     struct pjsua_inv_data *inv_data; 
    519565    char buf[128]; 
    520566    unsigned old_decor; 
     
    534580    PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:")); 
    535581 
    536     if (pj_list_empty(&pjsua.inv_list)) { 
     582    if (pjsua.call_cnt == 0) { 
    537583 
    538584        PJ_LOG(3,(THIS_FILE, "  - no sessions -")); 
    539585 
    540586    } else { 
    541  
    542         inv_data = pjsua.inv_list.next; 
    543  
    544         while (inv_data != &pjsua.inv_list) { 
    545  
    546             print_invite_session("  ", inv_data, buf, sizeof(buf)); 
     587        int i; 
     588 
     589        for (i=0; i<pjsua.max_calls; ++i) { 
     590 
     591            if (pjsua.calls[i].inv == NULL) 
     592                continue; 
     593 
     594            print_call("  ", i, buf, sizeof(buf)); 
    547595            PJ_LOG(3,(THIS_FILE, "%s", buf)); 
    548596 
    549             if (inv_data->session) 
    550                 dump_media_session(inv_data->session); 
    551  
    552             inv_data = inv_data->next; 
     597            if (pjsua.calls[i].session) 
     598                dump_media_session(pjsua.calls[i].session); 
    553599        } 
    554600    } 
     
    576622 
    577623/* 
    578  * Save settings. 
     624 * Save account settings 
    579625 */ 
    580 pj_status_t pjsua_save_settings(const char *filename) 
    581 { 
    582     unsigned i; 
     626static void save_account_settings(int acc_index, pj_str_t *result) 
     627{ 
     628    char line[128]; 
     629    pjsua_acc *acc = &pjsua.acc[acc_index]; 
     630 
     631     
     632    pj_ansi_sprintf(line, "#\n# Account %d:\n#\n", acc_index); 
     633    pj_strcat2(result, line); 
     634 
     635 
     636    /* Identity */ 
     637    if (acc->local_uri.slen) { 
     638        pj_ansi_sprintf(line, "--id %.*s\n",  
     639                        (int)acc->local_uri.slen,  
     640                        acc->local_uri.ptr); 
     641        pj_strcat2(result, line); 
     642    } 
     643 
     644    /* Registrar server */ 
     645    if (acc->reg_uri.slen) { 
     646        pj_ansi_sprintf(line, "--registrar %.*s\n", 
     647                              (int)acc->reg_uri.slen, 
     648                              acc->reg_uri.ptr); 
     649        pj_strcat2(result, line); 
     650 
     651        pj_ansi_sprintf(line, "--reg-timeout %u\n", 
     652                              acc->reg_timeout); 
     653        pj_strcat2(result, line); 
     654    } 
     655 
     656 
     657    /* Proxy */ 
     658    if (acc->proxy.slen) { 
     659        pj_ansi_sprintf(line, "--proxy %.*s\n", 
     660                              (int)acc->proxy.slen, 
     661                              acc->proxy.ptr); 
     662        pj_strcat2(result, line); 
     663    } 
     664} 
     665 
     666 
     667 
     668/* 
     669 * Dump settings. 
     670 */ 
     671int pjsua_dump_settings(char *buf, pj_size_t max) 
     672{ 
     673    int acc_index; 
     674    int i; 
    583675    pj_str_t cfg; 
    584676    char line[128]; 
    585     pj_pool_t *pool; 
    586     FILE *fhnd; 
    587  
    588     /* Create pool for temporary buffer. */ 
    589     pool = pj_pool_create(&pjsua.cp.factory, "settings", 4000, 0, NULL); 
    590     if (!pool) 
    591         return PJ_ENOMEM; 
    592  
    593  
    594     cfg.ptr = pj_pool_alloc(pool, 3800); 
    595     if (!cfg.ptr) { 
    596         pj_pool_release(pool); 
    597         return PJ_EBUG; 
    598     } 
     677 
     678    cfg.ptr = buf; 
    599679    cfg.slen = 0; 
    600680 
    601681 
    602     /* Identity */ 
    603     if (pjsua.local_uri.slen) { 
    604         pj_ansi_sprintf(line, "--id %.*s\n",  
    605                         (int)pjsua.local_uri.slen,  
    606                         pjsua.local_uri.ptr); 
    607         pj_strcat2(&cfg, line); 
     682    /* Logging. */ 
     683    pj_strcat2(&cfg, "#\n# Logging options:\n#\n"); 
     684    pj_ansi_sprintf(line, "--log-level %d\n", 
     685                    pjsua.log_level); 
     686    pj_strcat2(&cfg, line); 
     687 
     688    pj_ansi_sprintf(line, "--app-log-level %d\n", 
     689                    pjsua.app_log_level); 
     690    pj_strcat2(&cfg, line); 
     691 
     692    if (pjsua.log_filename) { 
     693        pj_ansi_sprintf(line, "--log-file %s\n", 
     694                        pjsua.log_filename); 
     695        pj_strcat2(&cfg, line); 
     696    } 
     697 
     698 
     699    /* Save account settings. */ 
     700    for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) { 
     701         
     702        save_account_settings(acc_index, &cfg); 
     703 
     704        if (acc_index < pjsua.acc_cnt-1) 
     705            pj_strcat2(&cfg, "--next-account\n"); 
    608706    } 
    609707 
    610708    /* Credentials. */ 
    611709    for (i=0; i<pjsua.cred_count; ++i) { 
     710 
     711        pj_ansi_sprintf(line, "#\n# Credential %d:\n#\n", i); 
     712        pj_strcat2(&cfg, line); 
     713 
    612714        if (pjsua.cred_info[i].realm.slen) { 
    613715            pj_ansi_sprintf(line, "--realm %.*s\n", 
     
    626728                              pjsua.cred_info[i].data.ptr); 
    627729        pj_strcat2(&cfg, line); 
    628     } 
    629  
    630     /* Registrar server */ 
    631     if (pjsua.registrar_uri.slen) { 
    632         pj_ansi_sprintf(line, "--registrar %.*s\n", 
    633                               (int)pjsua.registrar_uri.slen, 
    634                               pjsua.registrar_uri.ptr); 
    635         pj_strcat2(&cfg, line); 
    636     } 
    637  
     730 
     731        if (i < pjsua.cred_count-1) 
     732            pj_strcat2(&cfg, "--next-cred\n"); 
     733    } 
     734 
     735 
     736    pj_strcat2(&cfg, "#\n# Network settings:\n#\n"); 
    638737 
    639738    /* Outbound proxy */ 
     
    644743        pj_strcat2(&cfg, line); 
    645744    } 
    646  
    647     /* Media */ 
    648     if (pjsua.null_audio) 
    649         pj_strcat2(&cfg, "--null-audio\n"); 
    650745 
    651746 
     
    673768 
    674769 
     770    pj_strcat2(&cfg, "#\n# Media settings:\n#\n"); 
     771 
     772 
     773    /* Media */ 
     774    if (pjsua.null_audio) 
     775        pj_strcat2(&cfg, "--null-audio\n"); 
     776    if (pjsua.auto_play) 
     777        pj_strcat2(&cfg, "--auto-play\n"); 
     778    if (pjsua.auto_loop) 
     779        pj_strcat2(&cfg, "--auto-loop\n"); 
     780    if (pjsua.auto_conf) 
     781        pj_strcat2(&cfg, "--auto-conf\n"); 
     782    if (pjsua.wav_file) { 
     783        pj_ansi_sprintf(line, "--play-file %s\n", 
     784                        pjsua.wav_file); 
     785        pj_strcat2(&cfg, line); 
     786    } 
     787 
     788 
     789    pj_strcat2(&cfg, "#\n# User agent:\n#\n"); 
     790 
     791    /* Auto-answer. */ 
     792    if (pjsua.auto_answer != 0) { 
     793        pj_ansi_sprintf(line, "--auto-answer %d\n", 
     794                        pjsua.auto_answer); 
     795        pj_strcat2(&cfg, line); 
     796    } 
     797 
     798    /* Max calls. */ 
     799    pj_ansi_sprintf(line, "--max-calls %d\n", 
     800                    pjsua.max_calls); 
     801    pj_strcat2(&cfg, line); 
     802 
     803 
     804    pj_strcat2(&cfg, "#\n# Buddies:\n#\n"); 
     805 
    675806    /* Add buddies. */ 
    676807    for (i=0; i<pjsua.buddy_cnt; ++i) { 
     
    682813 
    683814 
     815    *(cfg.ptr + cfg.slen) = '\0'; 
     816    return cfg.slen; 
     817} 
     818 
     819/* 
     820 * Save settings. 
     821 */ 
     822pj_status_t pjsua_save_settings(const char *filename) 
     823{ 
     824    pj_str_t cfg; 
     825    pj_pool_t *pool; 
     826    FILE *fhnd; 
     827 
     828    /* Create pool for temporary buffer. */ 
     829    pool = pj_pool_create(&pjsua.cp.factory, "settings", 4000, 0, NULL); 
     830    if (!pool) 
     831        return PJ_ENOMEM; 
     832 
     833 
     834    cfg.ptr = pj_pool_alloc(pool, 3800); 
     835    if (!cfg.ptr) { 
     836        pj_pool_release(pool); 
     837        return PJ_EBUG; 
     838    } 
     839 
     840 
     841    cfg.slen = pjsua_dump_settings(cfg.ptr, 3800); 
     842    if (cfg.slen < 1) { 
     843        pj_pool_release(pool); 
     844        return PJ_ENOMEM; 
     845    } 
     846 
     847 
    684848    /* Write to file. */ 
    685849    fhnd = fopen(filename, "wt"); 
Note: See TracChangeset for help on using the changeset viewer.