主题:关于pjsip
本人想用 pjsip写一个完成基本的publish会话的ua,但不知如何写publish消息体?请教高人。。代码如下:
#include <pj/pool.h>
#include <pj/log.h>
#include <pj/string.h>
#include <pj/assert.h>
#include <pj/lock.h>
#include <pj/os.h>
#include <pj/pool_buf.h>
#include <pjsip.h>
#include <stdlib.h>
#include <pjsip-simple/publish.h>
#include <pjlib-util.h>
#include <pjsip_ua.h>
#include <pjsip/sip_uri.h>
#include <pjsip_simple.h>
#include <pjlib-util.h>
#include <pjsip/sip_types.h>
#include <pj/list.h>
#include<pjsip/sip_module.h>
#include<pjsip/sip_auth.h>
//#define SERVER
//#define AGENT
static pj_caching_pool cp;
static pjsip_endpoint *my_endpt;
int end;
unsigned count;
static pj_bool_t on_rx_request( pjsip_rx_data *rdata );
static pj_bool_t on_rx_response( pjsip_rx_data *rdata );
//#ifdef AGENT
static pjsip_module mod_presenceua =
{
NULL, NULL,
{ "mod-presenceua", 14 },
-1,
PJSIP_MOD_PRIORITY_APPLICATION,
NULL,
NULL,
NULL,
NULL,
&on_rx_request,
&on_rx_response,
NULL,
NULL,
NULL,
};
//#endif
//#ifdef SERVER
static pjsip_module mod_presence_s =
{
NULL, NULL,
{ "mod-presence-server", 14 },
-1,
PJSIP_MOD_PRIORITY_APPLICATION,
NULL,
NULL,
NULL,
NULL,
&on_rx_request,
&on_rx_response,
NULL,
NULL,
NULL,
};
//#endif
static void my_init_pjlib(void)
{
pj_status_t status;
//初始化pjlib
status = pj_init();
if(status != PJ_SUCCESS) printf("pj_init() error");
//初始化缓冲池产生器
pj_caching_pool_init(&cp,&pj_pool_factory_default_policy,0);
}
static void my_print_uri(const char *title,pjsip_uri *uri)
{
char buf[80];
int len;
len = pjsip_uri_print(PJSIP_URI_IN_OTHER,uri,buf,sizeof(buf)-1);
if(len < 0) printf("Not enough buffer to print URI");
buf[len] = '\0';
printf("%s",buf);
}
void pjsip_publishc_callback(struct pjsip_publishc_cbparam *param)
{
printf("Publish callback is called!");
}
int main()
{
pj_pool_t *pool;
//pjsip_name_addr *name_addr;
pjsip_sip_uri *sip_uri;
pjsip_publishc * publish;
pj_status_t status;
// 1.初始化PJLIB
my_init_pjlib();
// 2.建立内存池用来分配内存
pool = pj_pool_create( &cp.factory,"mypool",4000,4000,NULL);
// 3.Then init PJLIB-UTIL
status = pjlib_util_init();
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 4.建立pjsip_endpoint
status = pjsip_endpt_create(&cp.factory,"myendpt",&my_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 5.add UDP transport to the endpoint
//#ifdef AGENT
pj_sockaddr_in addr;
addr.sin_family = PJ_AF_INET;
addr.sin_addr.s_addr = 0;
addr.sin_port = pj_htons(5060);
status = pjsip_udp_transport_start(my_endpt,&addr,NULL,1,NULL);
if(status != PJ_SUCCESS){
printf("Unable to start UDP transport");
}
//#endif
pj_sockaddr_in addr1;
addr1.sin_family = PJ_AF_INET;
addr1.sin_addr.s_addr = 0;
addr1.sin_port = pj_htons(5061);
status = pjsip_udp_transport_start(my_endpt,&addr1,NULL,1,NULL);
if(status != PJ_SUCCESS){
printf("Unable to start UDP transport");
}
// 6.Init transaction layer
status = pjsip_tsx_layer_init_module(my_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 7.Initialize UA layer module
status = pjsip_ua_init_module(my_endpt, NULL );
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 9.Register our new module
status = pjsip_endpt_register_module(my_endpt,&mod_presence_s);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
//#ifdef AGENT
// 8.(a)Init the callback for PUBLISH session
pjsip_publishc_cb *pub_cb;
pub_cb = &pjsip_publishc_callback;
// 8.(b)Initialize publish session module
status = pjsip_publishc_init_module(my_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 9.Register our new module
status = pjsip_endpt_register_module(my_endpt,&mod_presenceua);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// Register Server module
char temp[80] = "sip:mod-presenceua@192.168.0.58";
char dst[80] = "sip:mod-presenceua@192.168.0.58:5061";
//char vs[80]="abcdefghijilmn";
pj_str_t local_uri;
pj_str_t dst_uri;
pjsip_dialog *dlg;
pjsip_tx_data *tdata;
local_uri = pj_str(temp);
dst_uri = pj_str(dst);
// Create UAC dialog
status = pjsip_dlg_create_uac(pjsip_ua_instance(),
&local_uri,
NULL,
&dst_uri,
&dst_uri,
&dlg);
if (status != PJ_SUCCESS) {
printf("Unable to create UAC dialog");
return 1;
}
// 10.Create client publication structure
status = pjsip_publishc_create(my_endpt,0,NULL,pub_cb,&publish);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 11.Initialize client publication structure
pj_str_t p_event = {"hello",5};
status = pjsip_publishc_init(publish,&p_event,&dst_uri,&local_uri,&dst_uri,1000);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 12.Create PUBLISH request
status = pjsip_publishc_publish(publish,1,&tdata);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 12.Send initial PUBLISH request
status = pjsip_publishc_send(publish,tdata);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
//#endif
for (;!end;) {
// pj_time_val timeout = {0, 2};
pjsip_endpt_handle_events2(my_endpt,NULL,&count);
}
printf("The nember of events have been handled is : %d \n",count);
return 0;
}
static pj_bool_t on_rx_request( pjsip_rx_data *rdata ){
char * rdata_info;
pj_status_t status;
pjsip_transaction *tsx;
pjsip_tx_data *tdata;
rdata_info = pjsip_rx_data_get_info(rdata);
printf("The received transmission data info is %s \n",rdata_info);
printf("On_rx_repuest is called");
//Create and initialize transaction
status = pjsip_tsx_create_uas(&mod_presence_s,rdata,&tsx);
//Pass in the initial request message
pjsip_tsx_recv_msg(tsx,rdata);
//Create response
status = pjsip_endpt_create_response(my_endpt,rdata,200,&pj_str("OK"),&tdata);
//Send response with the specified transaction
pjsip_tsx_send_msg(tsx,tdata);
end = 1;
return PJ_TRUE;
}
//#ifdef AGENT
static pj_bool_t on_rx_response( pjsip_rx_data *rdata ){
char * rdata_info;
rdata_info = pjsip_rx_data_get_info(rdata);
printf("The received transmission data info is %s \n",rdata_info);
printf("On_rx_response is called");
end = 1;
return PJ_TRUE;
}
//#endif
#include <pj/pool.h>
#include <pj/log.h>
#include <pj/string.h>
#include <pj/assert.h>
#include <pj/lock.h>
#include <pj/os.h>
#include <pj/pool_buf.h>
#include <pjsip.h>
#include <stdlib.h>
#include <pjsip-simple/publish.h>
#include <pjlib-util.h>
#include <pjsip_ua.h>
#include <pjsip/sip_uri.h>
#include <pjsip_simple.h>
#include <pjlib-util.h>
#include <pjsip/sip_types.h>
#include <pj/list.h>
#include<pjsip/sip_module.h>
#include<pjsip/sip_auth.h>
//#define SERVER
//#define AGENT
static pj_caching_pool cp;
static pjsip_endpoint *my_endpt;
int end;
unsigned count;
static pj_bool_t on_rx_request( pjsip_rx_data *rdata );
static pj_bool_t on_rx_response( pjsip_rx_data *rdata );
//#ifdef AGENT
static pjsip_module mod_presenceua =
{
NULL, NULL,
{ "mod-presenceua", 14 },
-1,
PJSIP_MOD_PRIORITY_APPLICATION,
NULL,
NULL,
NULL,
NULL,
&on_rx_request,
&on_rx_response,
NULL,
NULL,
NULL,
};
//#endif
//#ifdef SERVER
static pjsip_module mod_presence_s =
{
NULL, NULL,
{ "mod-presence-server", 14 },
-1,
PJSIP_MOD_PRIORITY_APPLICATION,
NULL,
NULL,
NULL,
NULL,
&on_rx_request,
&on_rx_response,
NULL,
NULL,
NULL,
};
//#endif
static void my_init_pjlib(void)
{
pj_status_t status;
//初始化pjlib
status = pj_init();
if(status != PJ_SUCCESS) printf("pj_init() error");
//初始化缓冲池产生器
pj_caching_pool_init(&cp,&pj_pool_factory_default_policy,0);
}
static void my_print_uri(const char *title,pjsip_uri *uri)
{
char buf[80];
int len;
len = pjsip_uri_print(PJSIP_URI_IN_OTHER,uri,buf,sizeof(buf)-1);
if(len < 0) printf("Not enough buffer to print URI");
buf[len] = '\0';
printf("%s",buf);
}
void pjsip_publishc_callback(struct pjsip_publishc_cbparam *param)
{
printf("Publish callback is called!");
}
int main()
{
pj_pool_t *pool;
//pjsip_name_addr *name_addr;
pjsip_sip_uri *sip_uri;
pjsip_publishc * publish;
pj_status_t status;
// 1.初始化PJLIB
my_init_pjlib();
// 2.建立内存池用来分配内存
pool = pj_pool_create( &cp.factory,"mypool",4000,4000,NULL);
// 3.Then init PJLIB-UTIL
status = pjlib_util_init();
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 4.建立pjsip_endpoint
status = pjsip_endpt_create(&cp.factory,"myendpt",&my_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 5.add UDP transport to the endpoint
//#ifdef AGENT
pj_sockaddr_in addr;
addr.sin_family = PJ_AF_INET;
addr.sin_addr.s_addr = 0;
addr.sin_port = pj_htons(5060);
status = pjsip_udp_transport_start(my_endpt,&addr,NULL,1,NULL);
if(status != PJ_SUCCESS){
printf("Unable to start UDP transport");
}
//#endif
pj_sockaddr_in addr1;
addr1.sin_family = PJ_AF_INET;
addr1.sin_addr.s_addr = 0;
addr1.sin_port = pj_htons(5061);
status = pjsip_udp_transport_start(my_endpt,&addr1,NULL,1,NULL);
if(status != PJ_SUCCESS){
printf("Unable to start UDP transport");
}
// 6.Init transaction layer
status = pjsip_tsx_layer_init_module(my_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 7.Initialize UA layer module
status = pjsip_ua_init_module(my_endpt, NULL );
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 9.Register our new module
status = pjsip_endpt_register_module(my_endpt,&mod_presence_s);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
//#ifdef AGENT
// 8.(a)Init the callback for PUBLISH session
pjsip_publishc_cb *pub_cb;
pub_cb = &pjsip_publishc_callback;
// 8.(b)Initialize publish session module
status = pjsip_publishc_init_module(my_endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 9.Register our new module
status = pjsip_endpt_register_module(my_endpt,&mod_presenceua);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// Register Server module
char temp[80] = "sip:mod-presenceua@192.168.0.58";
char dst[80] = "sip:mod-presenceua@192.168.0.58:5061";
//char vs[80]="abcdefghijilmn";
pj_str_t local_uri;
pj_str_t dst_uri;
pjsip_dialog *dlg;
pjsip_tx_data *tdata;
local_uri = pj_str(temp);
dst_uri = pj_str(dst);
// Create UAC dialog
status = pjsip_dlg_create_uac(pjsip_ua_instance(),
&local_uri,
NULL,
&dst_uri,
&dst_uri,
&dlg);
if (status != PJ_SUCCESS) {
printf("Unable to create UAC dialog");
return 1;
}
// 10.Create client publication structure
status = pjsip_publishc_create(my_endpt,0,NULL,pub_cb,&publish);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 11.Initialize client publication structure
pj_str_t p_event = {"hello",5};
status = pjsip_publishc_init(publish,&p_event,&dst_uri,&local_uri,&dst_uri,1000);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 12.Create PUBLISH request
status = pjsip_publishc_publish(publish,1,&tdata);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
// 12.Send initial PUBLISH request
status = pjsip_publishc_send(publish,tdata);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
//#endif
for (;!end;) {
// pj_time_val timeout = {0, 2};
pjsip_endpt_handle_events2(my_endpt,NULL,&count);
}
printf("The nember of events have been handled is : %d \n",count);
return 0;
}
static pj_bool_t on_rx_request( pjsip_rx_data *rdata ){
char * rdata_info;
pj_status_t status;
pjsip_transaction *tsx;
pjsip_tx_data *tdata;
rdata_info = pjsip_rx_data_get_info(rdata);
printf("The received transmission data info is %s \n",rdata_info);
printf("On_rx_repuest is called");
//Create and initialize transaction
status = pjsip_tsx_create_uas(&mod_presence_s,rdata,&tsx);
//Pass in the initial request message
pjsip_tsx_recv_msg(tsx,rdata);
//Create response
status = pjsip_endpt_create_response(my_endpt,rdata,200,&pj_str("OK"),&tdata);
//Send response with the specified transaction
pjsip_tsx_send_msg(tsx,tdata);
end = 1;
return PJ_TRUE;
}
//#ifdef AGENT
static pj_bool_t on_rx_response( pjsip_rx_data *rdata ){
char * rdata_info;
rdata_info = pjsip_rx_data_get_info(rdata);
printf("The received transmission data info is %s \n",rdata_info);
printf("On_rx_response is called");
end = 1;
return PJ_TRUE;
}
//#endif