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

要了解pjsip的使用,simple_pjsua.c是一个很好的例子,虽然代码只有短短的172行,却展示了pjsua-lib层的完整使用流程、注册流程和基本呼叫流程。

下面是学习过程中整理的simple_pjsua.c中的main函数主要流程:

先来看看pjsip-apps/src/samples/simple_pjsua.c的main函数

[cpp]  view plain copy
  1. /* 
  2.  * main() 
  3.  * 
  4.  * argv[1] may contain URL to call. 
  5.  */  
  6. int main(int argc, char *argv[])  
  7. {  
  8.     pjsua_acc_id acc_id;  
  9.     pj_status_t status;  
  10.   
  11. //  创建PJSIP  
  12.     /* Create pjsua first! */  
  13.     status = pjsua_create();  
  14.     if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status);  
  15.   
  16. //  校验被叫SIP地址是否正确  
  17.     /* If argument is specified, it's got to be a valid SIP URL */  
  18.     if (argc > 1) {  
  19.     status = pjsua_verify_url(argv[1]);  
  20.     if (status != PJ_SUCCESS) error_exit("Invalid URL in argv", status);  
  21.     }  
  22.   
  23. //    初始化PJSUA,设置回调函数  
  24.     /* Init pjsua */  
  25.     {  
  26.     pjsua_config cfg;  
  27.     pjsua_logging_config log_cfg;  
  28.   
  29.     pjsua_config_default(&cfg);  
  30.     cfg.cb.on_incoming_call = &on_incoming_call;  
  31.     cfg.cb.on_call_media_state = &on_call_media_state;  
  32.     cfg.cb.on_call_state = &on_call_state;  
  33.   
  34.     pjsua_logging_config_default(&log_cfg);  
  35.     log_cfg.console_level = 4;  
  36.   
  37.     status = pjsua_init(&cfg, &log_cfg, NULL);  
  38.     if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status);  
  39.     }  
  40.   
  41. //    创建PJSIP的传输端口  
  42.     /* Add UDP transport. */  
  43.     {  
  44.     pjsua_transport_config cfg;  
  45.   
  46.     pjsua_transport_config_default(&cfg);  
  47.     cfg.port = 5060;  
  48.     status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, NULL);  
  49.     if (status != PJ_SUCCESS) error_exit("Error creating transport", status);  
  50.     }  
  51.   
  52. //    启动PJSIP  
  53.     /* Initialization is done, now start pjsua */  
  54.     status = pjsua_start();  
  55.     if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status);  
  56.   
  57. //    设置SIP用户帐号  
  58.     /* Register to SIP server by creating SIP account. */  
  59.     {  
  60.     pjsua_acc_config cfg;  
  61.   
  62.     pjsua_acc_config_default(&cfg);  
  63.     cfg.id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN);  
  64.     cfg.reg_uri = pj_str("sip:" SIP_DOMAIN);  
  65.     cfg.cred_count = 1;  
  66.     cfg.cred_info[0].realm = pj_str(SIP_DOMAIN);  
  67.     cfg.cred_info[0].scheme = pj_str("digest");  
  68.     cfg.cred_info[0].username = pj_str(SIP_USER);  
  69.     cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;  
  70.     cfg.cred_info[0].data = pj_str(SIP_PASSWD);  
  71.   
  72.     status = pjsua_acc_add(&cfg, PJ_TRUE, &acc_id);  
  73.     if (status != PJ_SUCCESS) error_exit("Error adding account", status);  
  74.     }  
  75.   
  76. //    发起一个呼叫  
  77.     /* If URL is specified, make call to the URL. */  
  78.     if (argc > 1) {  
  79.     pj_str_t uri = pj_str(argv[1]);  
  80.     status = pjsua_call_make_call(acc_id, &uri, 0, NULL, NULL, NULL);  
  81.     if (status != PJ_SUCCESS) error_exit("Error making call", status);  
  82.     }  
  83.   
  84. //    循环等待  
  85.     /* Wait until user press "q" to quit. */  
  86.     for (;;) {  
  87.     char option[10];  
  88.   
  89.     puts("Press 'h' to hangup all calls, 'q' to quit");  
  90.     if (fgets(option, sizeof(option), stdin) == NULL) {  
  91.         puts("EOF while reading stdin, will quit now..");  
  92.         break;  
  93.     }  
  94.   
  95.     if (option[0] == 'q')  
  96.         break;  
  97.   
  98.     if (option[0] == 'h')  
  99.         pjsua_call_hangup_all();  
  100.     }  
  101.   
  102.     /* Destroy pjsua */  
  103.     pjsua_destroy();  
  104.   
  105.     return 0;  
  106. }  

来电的回调函数

[cpp]  view plain copy
  1. //来电回调函数  
  2. /* Callback called by the library upon receiving incoming call */  
  3. static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,  
  4.                  pjsip_rx_data *rdata)  
  5. {  
  6.     pjsua_call_info ci;  
  7.   
  8.     PJ_UNUSED_ARG(acc_id);  
  9.     PJ_UNUSED_ARG(rdata);  
  10.   
  11. //    获得呼叫信息  
  12.     pjsua_call_get_info(call_id, &ci);  
  13.   
  14.     PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",  
  15.              (int)ci.remote_info.slen,  
  16.              ci.remote_info.ptr));  
  17.   
  18. //    自动应答呼叫  
  19.     /* Automatically answer incoming calls with 200/OK */  
  20.     pjsua_call_answer(call_id, 200, NULL, NULL);  
  21. }  

呼叫状态改变的回调函数,没有做实质性的操作:

[cpp]  view plain copy
  1. //呼叫状态改变的回调函数  
  2. /* Callback called by the library when call's state has changed */  
  3. static void on_call_state(pjsua_call_id call_id, pjsip_event *e)  
  4. {  
  5.     pjsua_call_info ci;  
  6.   
  7.     PJ_UNUSED_ARG(e);  
  8.   
  9.     pjsua_call_get_info(call_id, &ci);  
  10.     PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,  
  11.              (int)ci.state_text.slen,  
  12.              ci.state_text.ptr));  
  13. }  


媒体状态改变的回调函数:

[cpp]  view plain copy
  1. //媒体状态改变的回调函数  
  2. /* Callback called by the library when call's media state has changed */  
  3. static void on_call_media_state(pjsua_call_id call_id)  
  4. {  
  5.     pjsua_call_info ci;  
  6.   
  7.     pjsua_call_get_info(call_id, &ci);  
  8.   
  9. //    当媒体为激活时,连接呼叫和声音设备  
  10.     if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {  
  11.     // When media is active, connect call to sound device.  
  12.     pjsua_conf_connect(ci.conf_slot, 0);  
  13.     pjsua_conf_connect(0, ci.conf_slot);  
  14.     }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值