2019独角兽企业重金招聘Python工程师标准>>>
从官网里抄袭的例子"PJSIP-Dev-Guide"第27页, vs2017编译之后运行有异常。
PJ_INLINE(int) pj_cis_match( const pj_cis_t *cis, pj_uint8_t c )
{
return PJ_CIS_ISSET(cis, c);
}
跟进源码里发现pjsip有些东西根本就没还初始化。
那么就创建个endpoint,让它顺便初始化一下试试吧。
// uri.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
#include
static pj_caching_pool cp;
static void my_error_exit(const char* title, pj_status_t errcode) {
char errbuf[128];
pjsip_strerror(errcode, errbuf, sizeof(errbuf));
PJ_LOG(3, ("main", "%s: %s", title, errbuf));
exit(1);
}
static void my_init_pjlib() {
pj_status_t status;
status = pj_init();
if (status != PJ_SUCCESS)
my_error_exit("pj_init() error", status);
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
status = pjsua_create(); // <--------------------加入了这个
if (status != PJ_SUCCESS)
my_error_exit("pjsua_create() error", status);
}
static void my_print_uri(const char* title, pjsip_uri* uri) {
char buf[128];
int len;
len = pjsip_uri_print(PJSIP_URI_IN_OTHER, uri, buf, sizeof(buf) - 1);
if (len < 0)
my_error_exit("Not enough buffer to print URI", -1);
buf[len] = '\0';
PJ_LOG(3, ("main", "%s: %s", title, buf));
}
int main(int argc, char* argv[]) {
pj_pool_t *pool;
pjsip_name_addr *name_addr;
pjsip_sip_uri *sip_uri;
char user[] = "Alice";
char host[] = "example.com";
my_init_pjlib();
pool = pj_pool_create(&cp.factory, "mypool", 4000, 4000, nullptr);
if (!pool)
my_error_exit("Unable to create pool", PJ_ENOMEM);
sip_uri = pjsip_sip_uri_create(pool, PJ_FALSE);
sip_uri->user = pj_str(user);
sip_uri->host = pj_str(host);
my_print_uri("The SIP URI is", (pjsip_uri*)sip_uri);
name_addr = pjsip_name_addr_create(pool);
name_addr->uri = (pjsip_uri*)sip_uri;
name_addr->display = pj_str_t{(char*)("Alice Cooper"), 12 };
my_print_uri("The name address is", (pjsip_uri*)name_addr);
pjsua_destroy(); // <--------------------加入了这个
pj_pool_release(pool);
return 0;
}
可以运行了。