PJSIP学习笔记——从simple_pjsua.c示例程序了解PJSUA-LIB的基本使用流程
2013-08-29 15:43
741 查看
要了解pjsip的使用,simple_pjsua.c是一个很好的例子,虽然代码只有短短的172行,却展示了pjsua-lib层的完整使用流程、注册流程和基本呼叫流程。
下面是学习过程中整理的simple_pjsua.c中的main函数主要流程:
先来看看pjsip-apps/src/samples/simple_pjsua.c的main函数
[cpp]
view plaincopy
/*
* 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);
cfg.cb.on_incoming_call = &on_incoming_call;
cfg.cb.on_call_media_state = &on_call_media_state;
cfg.cb.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);
cfg.port = 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);
cfg.id = 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;
}
来电的回调函数
[cpp]
view plaincopy
//来电回调函数
/* Callback called by the library upon receiving incoming call */
static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
pjsip_rx_data *rdata)
{
pjsua_call_info ci;
PJ_UNUSED_ARG(acc_id);
PJ_UNUSED_ARG(rdata);
// 获得呼叫信息
pjsua_call_get_info(call_id, &ci);
PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",
(int)ci.remote_info.slen,
ci.remote_info.ptr));
// 自动应答呼叫
/* Automatically answer incoming calls with 200/OK */
pjsua_call_answer(call_id, 200, NULL, NULL);
}
呼叫状态改变的回调函数,没有做实质性的操作:
[cpp]
view plaincopy
//呼叫状态改变的回调函数
/* Callback called by the library when call's state has changed */
static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
{
pjsua_call_info ci;
PJ_UNUSED_ARG(e);
pjsua_call_get_info(call_id, &ci);
PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,
(int)ci.state_text.slen,
ci.state_text.ptr));
}
媒体状态改变的回调函数:
[cpp]
view plaincopy
//媒体状态改变的回调函数
/* Callback called by the library when call's media state has changed */
static void on_call_media_state(pjsua_call_id call_id)
{
pjsua_call_info ci;
pjsua_call_get_info(call_id, &ci);
// 当媒体为激活时,连接呼叫和声音设备
if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
// When media is active, connect call to sound device.
pjsua_conf_connect(ci.conf_slot, 0);
pjsua_conf_connect(0, ci.conf_slot);
}
}
下面是学习过程中整理的simple_pjsua.c中的main函数主要流程:
先来看看pjsip-apps/src/samples/simple_pjsua.c的main函数
[cpp]
view plaincopy
/*
* 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);
cfg.cb.on_incoming_call = &on_incoming_call;
cfg.cb.on_call_media_state = &on_call_media_state;
cfg.cb.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);
cfg.port = 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);
cfg.id = 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;
}
来电的回调函数
[cpp]
view plaincopy
//来电回调函数
/* Callback called by the library upon receiving incoming call */
static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
pjsip_rx_data *rdata)
{
pjsua_call_info ci;
PJ_UNUSED_ARG(acc_id);
PJ_UNUSED_ARG(rdata);
// 获得呼叫信息
pjsua_call_get_info(call_id, &ci);
PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",
(int)ci.remote_info.slen,
ci.remote_info.ptr));
// 自动应答呼叫
/* Automatically answer incoming calls with 200/OK */
pjsua_call_answer(call_id, 200, NULL, NULL);
}
呼叫状态改变的回调函数,没有做实质性的操作:
[cpp]
view plaincopy
//呼叫状态改变的回调函数
/* Callback called by the library when call's state has changed */
static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
{
pjsua_call_info ci;
PJ_UNUSED_ARG(e);
pjsua_call_get_info(call_id, &ci);
PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,
(int)ci.state_text.slen,
ci.state_text.ptr));
}
媒体状态改变的回调函数:
[cpp]
view plaincopy
//媒体状态改变的回调函数
/* Callback called by the library when call's media state has changed */
static void on_call_media_state(pjsua_call_id call_id)
{
pjsua_call_info ci;
pjsua_call_get_info(call_id, &ci);
// 当媒体为激活时,连接呼叫和声音设备
if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
// When media is active, connect call to sound device.
pjsua_conf_connect(ci.conf_slot, 0);
pjsua_conf_connect(0, ci.conf_slot);
}
}
相关文章推荐
- PJSIP学习笔记——从simple_pjsua.c示例程序了解PJSUA-LIB的基本使用流程
- PJSIP学习笔记——从simple_pjsua.c示例程序了解PJSUA-LIB的基本使用流程
- PJSIP学习笔记——从simple_pjsua.c示例程序了解PJSUA-LIB的基本使用流程
- NDK学习笔记:动态链接库与静态链接库的基本使用流程简记
- 学习笔记:Linux驱动开发git基本使用流程
- grails学习笔记-4、通过例子了解grails开发的基本流程
- OpenGL学习笔记(四):了解OpenGL、OpenGL的操作模式、特性和基本使用步骤
- SpringMvc如何学习框架技术? 就像如上的图示一样,先掌握新技术的体系流程图。在快速弄明白程序执行流程后,在使用过程中 了解细节。
- Android(java)学习笔记142:使用Sqlite基本流程
- Lib库使用学习笔记
- 微信小程序学习2.js基本使用之block回调函数使用
- duilib学习笔记01:一个简单的Duilib程序的基本框架
- 从一个示例学习ER Studio8.0基本使用
- 学习笔记之qt4程序中引入自定义窗口部件之直接使用方式
- 基于node的websocket学习笔记三:scoket.io基础与利用scoket.io构建聊天程序示例
- HTML5独家分享:原生JS学习笔记2——程序流程控制
- 【Java学习笔记】04.流程控制语句与break,continue的使用
- 学习笔记_微信小程序开发--组建Swiper的使用
- java学习笔记(一)程序基本结构
- 黑马程序员_第一天视频学习 了解mac的基本使用和c语言基础语法