PJSIP学习笔记——PJSUA层发起呼叫的主要流程

时间:2025-01-29 21:46:31
  • /* 
  •  * main() 
  •  * 
  •  * argv[1] may contain URL to call. 
  •  */  
  • int main(int argc, char *argv[])  
  • {  
  •     pjsua_acc_id acc_id;  
  •     pj_status_t status;  
  •   
  • //  创建PJSIP  
  •     /* Create pjsua first! */  
  •     status = pjsua_create();  
  •     if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status);  
  •   
  • //  校验被叫SIP地址是否正确  
  •     /* If argument is specified, it's got to be a valid SIP URL */  
  •     if (argc > 1) {  
  •     status = pjsua_verify_url(argv[1]);  
  •     if (status != PJ_SUCCESS) error_exit("Invalid URL in argv", status);  
  •     }  
  •   
  • //    初始化PJSUA,设置回调函数  
  •     /* Init pjsua */  
  •     {  
  •     pjsua_config cfg;  
  •     pjsua_logging_config log_cfg;  
  •   
  •     pjsua_config_default(&cfg);  
  •     .on_incoming_call = &on_incoming_call;  
  •     .on_call_media_state = &on_call_media_state;  
  •     .on_call_state = &on_call_state;  
  •   
  •     pjsua_logging_config_default(&log_cfg);  
  •     log_cfg.console_level = 4;  
  •   
  •     status = pjsua_init(&cfg, &log_cfg, NULL);  
  •     if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status);  
  •     }  
  •   
  • //    创建PJSIP的传输端口  
  •     /* Add UDP transport. */  
  •     {  
  •     pjsua_transport_config cfg;  
  •   
  •     pjsua_transport_config_default(&cfg);  
  •      = 5060;  
  •     status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, NULL);  
  •     if (status != PJ_SUCCESS) error_exit("Error creating transport", status);  
  •     }  
  •   
  • //    启动PJSIP  
  •     /* Initialization is done, now start pjsua */  
  •     status = pjsua_start();  
  •     if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status);  
  •   
  • //    设置SIP用户帐号  
  •     /* Register to SIP server by creating SIP account. */  
  •     {  
  •     pjsua_acc_config cfg;  
  •   
  •     pjsua_acc_config_default(&cfg);  
  •      = pj_str("sip:" SIP_USER "@" SIP_DOMAIN);  
  •     cfg.reg_uri = pj_str("sip:" SIP_DOMAIN);  
  •     cfg.cred_count = 1;  
  •     cfg.cred_info[0].realm = pj_str(SIP_DOMAIN);  
  •     cfg.cred_info[0].scheme = pj_str("digest");  
  •     cfg.cred_info[0].username = pj_str(SIP_USER);  
  •     cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;  
  •     cfg.cred_info[0].data = pj_str(SIP_PASSWD);  
  •   
  •     status = pjsua_acc_add(&cfg, PJ_TRUE, &acc_id);  
  •     if (status != PJ_SUCCESS) error_exit("Error adding account", status);  
  •     }  
  •   
  • //    发起一个呼叫  
  •     /* If URL is specified, make call to the URL. */  
  •     if (argc > 1) {  
  •     pj_str_t uri = pj_str(argv[1]);  
  •     status = pjsua_call_make_call(acc_id, &uri, 0, NULL, NULL, NULL);  
  •     if (status != PJ_SUCCESS) error_exit("Error making call", status);  
  •     }  
  •   
  • //    循环等待  
  •     /* Wait until user press "q" to quit. */  
  •     for (;;) {  
  •     char option[10];  
  •   
  •     puts("Press 'h' to hangup all calls, 'q' to quit");  
  •     if (fgets(option, sizeof(option), stdin) == NULL) {  
  •         puts("EOF while reading stdin, will quit now..");  
  •         break;  
  •     }  
  •   
  •     if (option[0] == 'q')  
  •         break;  
  •   
  •     if (option[0] == 'h')  
  •         pjsua_call_hangup_all();  
  •     }  
  •   
  •     /* Destroy pjsua */  
  •     pjsua_destroy();  
  •   
  •     return 0;  
  • }