Ejemplo n.º 1
1
static BOOL OnInitStack (void)
{
    pjsua_config	    cfg;
    pjsua_logging_config    log_cfg;
    pjsua_media_config	    media_cfg;
    pjsua_transport_config  udp_cfg;
    pjsua_transport_config  rtp_cfg;
    pjsua_transport_id	    transport_id;
    pjsua_transport_info    transport_info;
    pj_str_t		    tmp;
    pj_status_t status;

    /* Create pjsua */
    status = pjsua_create();

    if (status != PJ_SUCCESS) {
        OnError (TEXT ("Error creating pjsua"), status);
        return FALSE;
    }

    /* Create global pool for application */
    g_pool = pjsua_pool_create ("pjsua", 4000, 4000);

    /* Init configs */
    pjsua_config_default (&cfg);
    pjsua_media_config_default (&media_cfg);
    pjsua_transport_config_default (&udp_cfg);
    udp_cfg.port = SIP_PORT;

    pjsua_transport_config_default (&rtp_cfg);
    rtp_cfg.port = 40000;

    pjsua_logging_config_default (&log_cfg);
    log_cfg.level = 5;
    log_cfg.log_filename = pj_str ("\\pjsua.txt");
    log_cfg.msg_logging = 1;
    log_cfg.decor = pj_log_get_decor() | PJ_LOG_HAS_CR;

    /* Setup media */
    media_cfg.clock_rate = 8000;
    media_cfg.ec_options = PJMEDIA_ECHO_SIMPLE;
    media_cfg.ec_tail_len = 256;
    // use default quality setting
    //media_cfg.quality = 1;
    media_cfg.ptime = 20;
    media_cfg.enable_ice = USE_ICE;

    /* Initialize application callbacks */
    cfg.cb.on_call_state = &on_call_state;
    cfg.cb.on_call_media_state = &on_call_media_state;
    cfg.cb.on_incoming_call = &on_incoming_call;
    cfg.cb.on_reg_state = &on_reg_state;
    cfg.cb.on_buddy_state = &on_buddy_state;
    cfg.cb.on_pager = &on_pager;
    cfg.cb.on_typing = &on_typing;
    cfg.cb.on_nat_detect = &nat_detect_cb;

    if (SIP_PROXY) {
        cfg.outbound_proxy_cnt = 1;
        cfg.outbound_proxy[0] = pj_str (SIP_PROXY);
    }

    if (NAMESERVER) {
        cfg.nameserver_count = 1;
        cfg.nameserver[0] = pj_str (NAMESERVER);
    }

    if (NAMESERVER && STUN_DOMAIN) {
        cfg.stun_domain = pj_str (STUN_DOMAIN);
    } else if (STUN_SERVER) {
        cfg.stun_host = pj_str (STUN_SERVER);
    }


    /* Initialize pjsua */
    status = pjsua_init (&cfg, &log_cfg, &media_cfg);

    if (status != PJ_SUCCESS) {
        OnError (TEXT ("Initialization error"), status);
        return FALSE;
    }

    /* Set codec priority */
    pjsua_codec_set_priority (pj_cstr (&tmp, "pcmu"), 240);
    pjsua_codec_set_priority (pj_cstr (&tmp, "pcma"), 230);
    pjsua_codec_set_priority (pj_cstr (&tmp, "speex/8000"), 190);
    pjsua_codec_set_priority (pj_cstr (&tmp, "ilbc"), 189);
    pjsua_codec_set_priority (pj_cstr (&tmp, "speex/16000"), 180);
    pjsua_codec_set_priority (pj_cstr (&tmp, "speex/32000"), 0);
    pjsua_codec_set_priority (pj_cstr (&tmp, "gsm"), 100);


    /* Add UDP transport and the corresponding PJSUA account */
    status = pjsua_transport_create (PJSIP_TRANSPORT_UDP,
                                     &udp_cfg, &transport_id);

    if (status != PJ_SUCCESS) {
        OnError (TEXT ("Error starting SIP transport"), status);
        return FALSE;
    }

    pjsua_transport_get_info (transport_id, &transport_info);

    g_local_uri.ptr = (char*) pj_pool_alloc (g_pool, 128);
    g_local_uri.slen = pj_ansi_sprintf (g_local_uri.ptr,
                                        "<sip:%.*s:%d>",
                                        (int) transport_info.local_name.host.slen,
                                        transport_info.local_name.host.ptr,
                                        transport_info.local_name.port);


    /* Add local account */
    pjsua_acc_add_local (transport_id, PJ_TRUE, &g_current_acc);
    pjsua_acc_set_online_status (g_current_acc, PJ_TRUE);

    /* Add account */
    if (HAS_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_REALM);
        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, &g_current_acc);

        if (status != PJ_SUCCESS) {
            pjsua_destroy();
            return PJ_FALSE;
        }
    }

    /* Add buddy */
    if (SIP_DST_URI) {
        pjsua_buddy_config bcfg;

        pjsua_buddy_config_default (&bcfg);
        bcfg.uri = pj_str (SIP_DST_URI);
        bcfg.subscribe = PJ_FALSE;

        pjsua_buddy_add (&bcfg, NULL);
    }

    /* Start pjsua */
    status = pjsua_start();

    if (status != PJ_SUCCESS) {
        OnError (TEXT ("Error starting pjsua"), status);
        return FALSE;
    }

    return TRUE;
}
Ejemplo n.º 2
0
//----------------------------------------------------------------------
void SipPhone::init()
{
    pj_status_t status;
    /* Create pjsua first! */
    status = pjsua_create();

    if (status != PJ_SUCCESS)
    {
        LogInfo info(LogInfo::STATUS_FATAL_ERROR, "pjsip", status, "Error in pjsua_create()");
        signalLogData(info);
        return;
    }

    /* Init pjsua */
    {
        pjsua_config cfg;
        pjsua_logging_config log_cfg;
        ConfigFileHandler &config = ConfigFileHandler::getInstance();
        QString stun = config.getStunServer();
        pjsua_config_default(&cfg);

        if (stun.size())
        {
            char ch_stun[100];
            if (stun.size() > 99)
            {
                LogInfo info(LogInfo::STATUS_ERROR, "pjsip", 0, "Error init pjsip, stun-server too long");
                signalLogData(info);
                return;
            }

            strcpy(ch_stun, stun.toLocal8Bit().data());
            ch_stun[stun.size()] = 0;

            cfg.stun_srv[cfg.stun_srv_cnt++] = pj_str(ch_stun);
        }
        cfg.enable_unsolicited_mwi = PJ_FALSE;
        cfg.cb.on_incoming_call = &incomingCallCb;
        cfg.cb.on_call_state = &callStateCb;
        cfg.cb.on_call_media_state = &callMediaStateCb;
        cfg.cb.on_reg_state = &regStateCb;

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

        status = pjsua_init(&cfg, &log_cfg, NULL);
        printf("init successfull\n");
        if (status != PJ_SUCCESS)
        {
            LogInfo info(LogInfo::STATUS_FATAL_ERROR, "pjsip", status, "Error in pjsua_init()");
            signalLogData(info);
            return;
        }
    }

    /* Add UDP transport. */
    {
        pjsua_transport_config cfg;
        pjsua_acc_id aid;
        pjsua_transport_id transport_id = -1;
        pjsua_transport_config tcp_cfg;

        pjsua_transport_config_default(&cfg);
        cfg.port = 5060;

        status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, &transport_id);

        if (status != PJ_SUCCESS)
        {
            LogInfo info(LogInfo::STATUS_FATAL_ERROR, "pjsip", status, "Error creating transport");
            signalLogData(info);
            return;
        }

        /* Add local account */
        pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
        pjsua_acc_set_online_status(aid, PJ_TRUE);

        if (cfg.port == 0)
        {
            pjsua_transport_info ti;
            pj_sockaddr_in *a;

            pjsua_transport_get_info(transport_id, &ti);
            a = (pj_sockaddr_in*)&ti.local_addr;

            tcp_cfg.port = pj_ntohs(a->sin_port);
        }

    }
    /* Initialization is done, now start pjsua */
    status = pjsua_start();

    if (status != PJ_SUCCESS)
    {
        LogInfo info(LogInfo::STATUS_FATAL_ERROR, "pjsip", status, "Error starting PJSUA");
        signalLogData(info);
        return;
    }
    pjsua_conf_adjust_rx_level(0, 1.f);
    pjsua_conf_adjust_tx_level(0, 1.f);
    speaker_level_ = 1.f;
    mic_level_ = 1.f;
}
Ejemplo n.º 3
0
int main()
{
	/* system variables */
	pj_status_t status;
	pjsua_config			ua_cfg;
	pjsua_acc_config		acc_cfg;
	pjsua_logging_config	log_cfg;
	pjsua_media_config		media_cfg;
	pjsua_transport_config	udp_cfg;
	pjsua_transport_id		transport_id;
	pjsua_acc_id			local_aid;
	pjsua_acc_id			user_aid;
	// used to send message
	pj_str_t		to; 
	pj_str_t		mime_type; 
	pj_str_t		content;
	bShutdown = 0;
	pthread_cond_init(&_cond, NULL);
	pthread_mutex_init(&_mutex, NULL);

   if ( signal( SIGINT, signalHandler ) == SIG_ERR )
   {
      exit( -1 );
   }

   if ( signal( SIGTERM, signalHandler ) == SIG_ERR )
   {
      exit( -1 );
   }


	/**
	 * create pjsua first 
	 */
	status = pjsua_create();
	if(status != PJ_SUCCESS) {
		pjsua_perror(THIS_FILE, "Error initializing pjsua", status);
		return status;
	}
	/**
	 * init pjsua framework 
	 */
	/* configuration of ua */
	pjsua_config_default(&(ua_cfg));
	/**
	 * init callback
	 * set callback functions you need
	 * all the prototype of call are declared in pjsua.h
	 */
//	/* call related handle */
//	ua_cfg.cb.on_call_state					  = on_call_state;
//	ua_cfg.cb.on_incoming_call                = on_incoming_call;
//	ua_cfg.cb.on_call_tsx_state               = on_call_tsx_state;
//	ua_cfg.cb.on_call_media_state             = on_call_media_state;
//	ua_cfg.cb.on_call_sdp_created             = on_call_sdp_created;
//	ua_cfg.cb.on_stream_created               = on_stream_created;
//	ua_cfg.cb.on_stream_destroyed             = on_stream_destroyed;
//	ua_cfg.cb.on_dtmf_digit                   = on_dtmf_digit;
//	ua_cfg.cb.on_call_transfer_request        = on_call_transfer_request;
//	ua_cfg.cb.on_call_transfer_request2       = on_call_transfer_request2;
//	ua_cfg.cb.on_call_transfer_status         = on_call_transfer_status;
//	ua_cfg.cb.on_call_replace_request         = on_call_replace_request;
//	ua_cfg.cb.on_call_replace_request2        = on_call_replace_request2;
//	ua_cfg.cb.on_call_replaced                = on_call_replaced;
//	ua_cfg.cb.on_call_rx_offer                = on_call_rx_offer;
//	/* register handle */
	ua_cfg.cb.on_reg_started                  = on_reg_started;
	ua_cfg.cb.on_reg_state                    = on_reg_state;
	ua_cfg.cb.on_reg_state2                   = on_reg_state2;
//	/* subscirbe handle */
//	ua_cfg.cb.on_incoming_subscribe           = on_incoming_subscribe;
//	ua_cfg.cb.on_srv_subscribe_state          = on_srv_subscribe_state;
//	ua_cfg.cb.on_buddy_state                  = on_buddy_state;
//	ua_cfg.cb.on_buddy_evsub_state            = on_buddy_evsub_state;
//	/* message handle */
//	ua_cfg.cb.on_pager                        = on_pager;
	ua_cfg.cb.on_pager2                       = on_pager2;
//	ua_cfg.cb.on_pager_status                 = on_pager_status;
//	ua_cfg.cb.on_pager_status2                = on_pager_status2;
//	ua_cfg.cb.on_typing                       = on_typing;
//	ua_cfg.cb.on_typing2                      = on_typing2;
//	/* network handle */
//	ua_cfg.cb.on_nat_detect                   = on_nat_detect;
	ua_cfg.cb.on_call_redirected              = on_call_redirected;
//	ua_cfg.cb.on_mwi_state                    = on_mwi_state;
//	ua_cfg.cb.on_mwi_info                     = on_mwi_info;
//	ua_cfg.cb.on_transport_state              = on_transport_state;
//	ua_cfg.cb.on_call_media_transport_state   = on_call_media_transport_state;
//	ua_cfg.cb.on_ice_transport_error          = on_ice_transport_error;
//	ua_cfg.cb.on_snd_dev_operation            = on_snd_dev_operation;
//	/* media module handle */
//	ua_cfg.cb.on_call_media_event             = on_call_media_event;
//	ua_cfg.cb.on_create_media_transport       = on_create_media_transport;
	/* configure logging */
	pjsua_logging_config_default(&(log_cfg));
	log_cfg.msg_logging = PJ_FALSE;
	/* configure media */
	pjsua_media_config_default(&(media_cfg));
	status = pjsua_init(&(ua_cfg), 
			&(log_cfg), 
			NULL);
//			&(media_cfg));
	if(status != PJ_SUCCESS) {
		pjsua_perror(THIS_FILE, "Error initializing pjsua", status);
		return status;
	}

	status = pjsua_start();
	if(status != PJ_SUCCESS) {
		pjsua_perror(THIS_FILE, "Error starting pjsua", status);
		return status;
	}

	/**
	 * Setup transport
	 */
	pjsua_transport_config_default(&udp_cfg);
	udp_cfg.port = 5091;
	status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &udp_cfg, &transport_id);
	if(status != PJ_SUCCESS)
		return -1;

	/**
	 * add local account (sip:domain) which handle sip messages that don't matches any
	 * other accounts
	 */
	pjsua_acc_add_local(transport_id, PJ_TRUE, &local_aid);

	/**
	 * add user account and set it to default account 
	 */
	pjsua_acc_config_default(&acc_cfg);
	acc_cfg.id = pj_str("sip:[email protected]");
	acc_cfg.reg_uri = pj_str("sip:192.168.1.197");
	acc_cfg.reg_timeout = 300;
	acc_cfg.cred_count = 1;
	acc_cfg.cred_info[0].realm = pj_str("192.168.1.197");
	acc_cfg.cred_info[0].scheme = pj_str("digest");
	acc_cfg.cred_info[0].username = pj_str("Server_301007");
	acc_cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
	acc_cfg.cred_info[0].data = pj_str("Server");
	acc_cfg.proxy_cnt = 1;
	acc_cfg.proxy[0] = pj_str("sip:192.168.1.197;lr");

	/**
	 * register_on_acc_add is true by default, add:
	 *
	 *		acc_cfg.register_on_acc_add = PJ_FALSE;
	 *
	 * to disable register on add.add later you can call the following 
	 * code to send a REGISTER.
	 *
	 *		pjsua_acc_set_registration(id_of_the_account, PJ_TRUE);
	 *
	 */
	status = pjsua_acc_add(&acc_cfg, PJ_TRUE, &user_aid);
	sleep(2);
	/**
	 * unregister account before exit
	 */


	to = pj_str("sip:[email protected]");
	to = pj_str("sip:[email protected]");
	mime_type = pj_str("text/plain");
	content = pj_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
			"<SIP_XML EventType=\"TimeSync\">"
			"<Item operation=\"query\" time=\"0-0-0 0:0:0\"/>"
			"</SIP_XML>");
	content = pj_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
			"<SIP_XML EventType=\"MediaPlay\">"
			"<User uri=\"sip:[email protected]\"/>"
			"<Media type=\"1\" id=\"000001-001\"/>"
			"</SIP_XML>");
	content = pj_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
			"<SIP_XML EventType=\"MediaTeardown\">"
			"<User uri=\"sip:[email protected]\"/>"
			"<Media type=\"1\" id=\"000001-001\"/>"
			"</SIP_XML>");
	{
		pthread_t tid;
//		pthread_create(&tid, NULL, test_thread, &user_aid);
	}
	pjsua_im_send(user_aid, &to, &mime_type, &content, NULL, NULL);
	sleep(1);
	printf("*******************************\n");
	printf("pjsua_im_send ok @ main\n");
	printf("*******************************\n");
	pthread_mutex_lock(&_mutex);
	pthread_cond_signal(&_cond);
	pthread_mutex_unlock(&_mutex);
//	pjsua_call_setting setting;
//	pjsua_call_setting_default(&setting);
//	setting.aud_cnt = 0;
//	setting.vid_cnt = 0;
//	pjsua_call_id cid;
//	pjsua_msg_data msg_data;
//	pjsua_msg_data_init(&msg_data);
//	msg_data.content_type = pj_str("application/sdp");
//	msg_data.msg_body = pj_str(
//			"v=0\r\n"
//			"o=adapter 0 0 IN IP4 0.0.0.0\r\n"
//			"s=-\r\n"
//			"c=IN IP4 192.168.1.147\r\n"
//			"t=1371053400 1371057000\r\n"
//			"m=video 6000 TCP 96\r\n"
//			"a=rtpmap:98 L16/16000/2\r\n"
//			"a=ResID:301002-0005\r\n"
//			"a=WndIndex:1\r\n"
//			"a=recvonly\r\n");
//	sleep(2);
//	if(pjsua_call_make_call(user_aid, &to, &setting, NULL, NULL, &cid) != PJ_SUCCESS)
//		PJ_LOG(1, (__FILE__, "failed to make call\n"));
	while(!bShutdown)
	{
		sleep(1);
	}
	pjsua_acc_set_registration(user_aid, PJ_FALSE);
	sleep(1);
	return status;

}