pjsip视频通信开发(底层实现)之用户注册(1)

一、PJSIP简介

对于pjsip的介绍可以看http://www.cnblogs.com/my_life/articles/2175462.html 文章,里面详细介绍了它的组成框架以及各部份的组成介绍,我把官网中提供的一个pjsip的整体框架图贴到这里

pjsip视频通信开发(底层实现)之用户注册(1)

二、simple_pjsua.c分析

我今天要实现的是UA这部份内容,主要作用可以查看http://www.cnblogs.com/flyfish10000/category/268759.html 里面有作者一系列关于UA的介绍,这里我要实现的是帐号的注册,要做这个我们可以看一下官网一个提供的例子simple_pjsua.c (pjsip-apps/src/samples/),它的内容如下:

 

/**

 * simple_pjsua.c

 *

 * This is a very simple but fully featured SIP user agent, with the 

 * following capabilities:

 *  - SIP registration

 *  - Making and receiving call

 *  - Audio/media to sound device.

 *

 * Usage:

 *  - To make outgoing call, start simple_pjsua with the URL of remote

 *    destination to contact.

 *    E.g.:

 *	 simpleua sip:user@remote

 *

 *  - Incoming calls will automatically be answered with 200.

 *

 * This program will quit once it has completed a single call.

 */



#include <pjsua-lib/pjsua.h>



#define THIS_FILE	"APP"



#define SIP_DOMAIN	"example.com"

#define SIP_USER	"alice"

#define SIP_PASSWD	"secret"





/* 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);

}



/* 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));

}



/* 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);

    }

}



/* Display error and exit application */

static void error_exit(const char *title, pj_status_t status)

{

    pjsua_perror(THIS_FILE, title, status);

    pjsua_destroy();

    exit(1);

}



/*

 * main()

 *

 * argv[1] may contain URL to call.

 */

int main(int argc, char *argv[])

{

    pjsua_acc_id acc_id;

    pj_status_t status;



    /* Create pjsua first! */

    status = pjsua_create();

    if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status);



    /* 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);

    }



    /* 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);

    }



    /* 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);

    }



    /* Initialization is done, now start pjsua */

    status = pjsua_start();

    if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status);



    /* 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;

}

我们这里可以分析一下它的代码:

 

1、一开始是回调使用的函数,例如on_incoming_call当来电话的时候,pjsip会自动去调用你写的这个函数,前提是你在初始化pjsua的时候设置了on_incoming_call = &on_incoming_call,
2、error_exit退出应用所需要的操作

3、main函数:

    (1)pjsua_create()创建pjsua的第一步,如果是要打电话要确认URL是否是正确的pjsua_verify_url

    (2)初始化pjsua,pjsua_config_default(&cfg)来初始化配置,然后设置一些回调函数,设置日志,最后初始化pjsua_init(&cfg, &log_cfg, NULL);

    (3)创建UDP的传输,设置端口号

    (4)接下来就是启动pjsua,通过pjsua_start();

    (5)创建账户,这个是这篇文章的主要内容,pjsua_acc_config_default初始化配置,然后设置相关的内容,id对应这url,realm是服务器的域名,还有密码和用户名,最后调用 pjsua_acc_add(&cfg, PJ_TRUE, &acc_id);来实现帐号的注册。

4、打电话,上面也提到过,你打电话的话需要验证URL是否正确的 pjsua_verify_url,然后调用pjsua_call_make_call来打电话。

5、挂电话,调用 pjsua_call_hangup_all();

6、最后销毁,pjsua_destroy();

三、PJSIP帐号注册开发

下一章中介绍

 

你可能感兴趣的:(JSI)