不学网

 找回密码
 立即注册

只需一步,快速开始

手机号码,快捷登录

查看: 231|回复: 0

[c/c++] pjsua直接控制voip网关拨打电话

[复制链接]
admin 发表于 2018-4-26 16:38:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
官方的例子是向服务器注册之后发起请求,改了一下就可以直接向voip网关发起请求了。
只要不设置这个字段就不会注册 //        cfg.reg_uri = pj_str("sip:" SIP_DOMAIN);   具体可以跟进去看官方源码
官方解释如下:
  1. /**
  2.      * This is the URL to be put in the request URI for the registration,
  3.      * and will look something like "sip:serviceprovider".
  4.      *
  5.      * This field should be specified if registration is desired. If the
  6.      * value is empty, no account registration will be performed.
  7.      */
  8.     pj_str_t            reg_uri;
复制代码

也就是 这个字段为空就不会执行注册操作


main.c

  1. #include <pjsua-lib/pjsua.h>

  2. #define SIP_DOMAIN "192.168.4.101"
  3. #define SIP_USER "110"
  4. #define SIP_PASSWD "secret"
  5. #define THIS_FILE "app"

  6. //来电回调函数
  7. /* Callback called by the library upon receiving incoming call */
  8. static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
  9.                              pjsip_rx_data *rdata)
  10. {
  11.     pjsua_call_info ci;

  12.     PJ_UNUSED_ARG(acc_id);
  13.     PJ_UNUSED_ARG(rdata);

  14.     //    获得呼叫信息
  15.     pjsua_call_get_info(call_id, &ci);

  16.     PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",
  17.             (int)ci.remote_info.slen,
  18.             ci.remote_info.ptr));

  19.     //    自动应答呼叫
  20.     /* Automatically answer incoming calls with 200/OK */
  21.     pjsua_call_answer(call_id, 200, NULL, NULL);
  22. }


  23. //呼叫状态改变的回调函数
  24. /* Callback called by the library when call's state has changed */
  25. static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
  26. {
  27.     pjsua_call_info ci;

  28.     PJ_UNUSED_ARG(e);

  29.     pjsua_call_get_info(call_id, &ci);
  30.     PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,
  31.             (int)ci.state_text.slen,
  32.             ci.state_text.ptr));
  33. }


  34. //媒体状态改变的回调函数
  35. /* Callback called by the library when call's media state has changed */
  36. static void on_call_media_state(pjsua_call_id call_id)
  37. {
  38.     pjsua_call_info ci;

  39.     pjsua_call_get_info(call_id, &ci);

  40. //    当媒体为激活时,连接呼叫和声音设备
  41.     if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
  42.         // When media is active, connect call to sound device.
  43.         pjsua_conf_connect(ci.conf_slot, 0);
  44.         pjsua_conf_connect(0, ci.conf_slot);
  45.     }
  46. }

  47. /*
  48. * main()
  49. *
  50. * argv[1] may contain URL to call.
  51. */
  52. int main(int argc, char *argv[])
  53. {
  54.     pjsua_acc_id acc_id;
  55.     pj_status_t status;

  56. //  创建PJSIP
  57.     /* Create pjsua first! */
  58.     status = pjsua_create();
  59.     if (status != PJ_SUCCESS) return status;


  60. //    初始化PJSUA,设置回调函数
  61.     /* Init pjsua */
  62.     {
  63.         pjsua_config cfg;
  64.         pjsua_logging_config log_cfg;

  65.         pjsua_config_default(&cfg);
  66.         cfg.cb.on_incoming_call = &on_incoming_call;
  67.         cfg.cb.on_call_media_state = &on_call_media_state;
  68.         cfg.cb.on_call_state = &on_call_state;

  69.         pjsua_logging_config_default(&log_cfg);
  70.         log_cfg.console_level = 4;

  71.         status = pjsua_init(&cfg, &log_cfg, NULL);
  72.         if (status != PJ_SUCCESS) return status;
  73.     }


  74. //    创建PJSIP的传输端口
  75.     /* Add UDP transport. */
  76.     {
  77.         pjsua_transport_config cfg;

  78.         pjsua_transport_config_default(&cfg);
  79.         cfg.port = 5060;
  80.         status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, NULL);
  81.         if (status != PJ_SUCCESS) return status;
  82.     }

  83. //    启动PJSIP
  84.     /* Initialization is done, now start pjsua */
  85.     status = pjsua_start();
  86.     if (status != PJ_SUCCESS) return status;

  87. //    设置SIP用户帐号
  88.     /* Register to SIP server by creating SIP account. */
  89.     {
  90.         pjsua_acc_config cfg;

  91.         pjsua_acc_config_default(&cfg);
  92.         cfg.id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN);
  93. //        cfg.reg_uri = pj_str("sip:" SIP_DOMAIN);
  94.         cfg.cred_count = 1;
  95. //        cfg.cred_info[0].realm = pj_str(SIP_DOMAIN);
  96. //        cfg.cred_info[0].scheme = pj_str("digest");
  97. //        cfg.cred_info[0].username = pj_str(SIP_USER);
  98. //        cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
  99. //        cfg.cred_info[0].data = pj_str(SIP_PASSWD);


  100.         status = pjsua_acc_add(&cfg, PJ_TRUE, &acc_id);
  101.         if (status != PJ_SUCCESS) return status;
  102.     }

  103.     pjsua_set_no_snd_dev();

  104.     pj_str_t uri = pj_str("sip:13758277505@192.168.4.101");
  105.     status = pjsua_call_make_call(acc_id, &uri, 0, NULL, NULL, NULL);
  106.     if (status != PJ_SUCCESS) return status;

  107. //    循环等待
  108.     /* Wait until user press "q" to quit. */
  109.     for (;;) {
  110.         char option[10];

  111.         puts("Press 'h' to hangup all calls, 'q' to quit");
  112.         if (fgets(option, sizeof(option), stdin) == NULL) {
  113.             puts("EOF while reading stdin, will quit now..");
  114.             break;
  115.         }

  116.         if (option[0] == 'q')
  117.             break;

  118.         if (option[0] == 'h')
  119.             pjsua_call_hangup_all();
  120.     }

  121.     /* Destroy pjsua */
  122.     pjsua_destroy();

  123.     return 0;
  124. }
复制代码

CMakeList.txt

  1. cmake_minimum_required(VERSION 3.10)
  2. project(phone C)

  3. set(CMAKE_C_STANDARD 11)

  4. add_executable(phone main.c)

  5. target_link_libraries(phone -lpjsua2-x86_64-unknown-linux-gnu -lpjsua-x86_64-unknown-linux-gnu -lpjsip-ua-x86_64-unknown-linux-gnu -lpjsip-simple-x86_64-unknown-linux-gnu -lpjsip-x86_64-unknown-linux-gnu -lpjmedia-codec-x86_64-unknown-linux-gnu -lpjmedia-x86_64-unknown-linux-gnu -lpjmedia-videodev-x86_64-unknown-linux-gnu -lpjmedia-audiodev-x86_64-unknown-linux-gnu -lpjmedia-x86_64-unknown-linux-gnu -lpjnath-x86_64-unknown-linux-gnu -lpjlib-util-x86_64-unknown-linux-gnu  -lsrtp-x86_64-unknown-linux-gnu -lresample-x86_64-unknown-linux-gnu -lgsmcodec-x86_64-unknown-linux-gnu -lspeex-x86_64-unknown-linux-gnu -lilbccodec-x86_64-unknown-linux-gnu -lg7221codec-x86_64-unknown-linux-gnu -lyuv-x86_64-unknown-linux-gnu -lwebrtc-x86_64-unknown-linux-gnu  -lpj-x86_64-unknown-linux-gnu -lm -lrt -lpthread  -lasound)
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|不学网

GMT+8, 2018-7-23 07:59

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表