Index: channels/chan_sip.c =================================================================== --- channels/chan_sip.c (revision 402448) +++ channels/chan_sip.c (working copy) @@ -5999,11 +5999,14 @@ } ast_sockaddr_copy(&bindaddr_tmp, &bindaddr); + if (!(dialog->rtp = ast_rtp_instance_new(dialog->engine, sched, &bindaddr_tmp, NULL))) { return -1; } - if (!ast_test_flag(&dialog->flags[2], SIP_PAGE3_ICE_SUPPORT) && (ice = ast_rtp_instance_get_ice(dialog->rtp))) { + if (ast_test_flag(&dialog->flags[2], SIP_PAGE3_ICE_SUPPORT)) { + ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_STUN, 1); + } else if ((ice = ast_rtp_instance_get_ice(dialog->rtp))) { ice->stop(dialog->rtp); } Index: res/res_rtp_asterisk.c =================================================================== --- res/res_rtp_asterisk.c (revision 402448) +++ res/res_rtp_asterisk.c (working copy) @@ -1759,10 +1759,6 @@ { struct ast_rtp *rtp = NULL; int x, startplace; -#ifdef HAVE_PJPROJECT - pj_stun_config stun_config; - pj_str_t ufrag, passwd; -#endif /* Create a new RTP structure to hold all of our data */ if (!(rtp = ast_calloc(1, sizeof(*rtp)))) { @@ -1820,27 +1816,7 @@ } } -#ifdef HAVE_PJPROJECT - pj_thread_register_check(); - - pj_stun_config_init(&stun_config, &cachingpool.factory, 0, ioqueue, timerheap); - - generate_random_string(rtp->local_ufrag, sizeof(rtp->local_ufrag)); - ufrag = pj_str(rtp->local_ufrag); - generate_random_string(rtp->local_passwd, sizeof(rtp->local_passwd)); - passwd = pj_str(rtp->local_passwd); -#endif ast_rtp_instance_set_data(instance, rtp); -#ifdef HAVE_PJPROJECT - /* Create an ICE session for ICE negotiation */ - if (icesupport && pj_ice_sess_create(&stun_config, NULL, PJ_ICE_SESS_ROLE_UNKNOWN, 2, &ast_rtp_ice_sess_cb, &ufrag, &passwd, NULL, &rtp->ice) == PJ_SUCCESS) { - /* Make this available for the callbacks */ - rtp->ice->user_data = rtp; - - /* Add all of the available candidates to the ICE session */ - rtp_add_candidates_to_ice(instance, rtp, addr, x, AST_RTP_ICE_COMPONENT_RTP, TRANSPORT_SOCKET_RTP, &ast_rtp_turn_rtp_sock_cb, &rtp->turn_rtp); - } -#endif /* Record any information we may need */ rtp->sched = sched; @@ -3938,6 +3914,11 @@ static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value) { struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); +#ifdef HAVE_PJPROJECT + pj_stun_config stun_config; + pj_str_t ufrag, passwd; + struct ast_sockaddr addr = { { 0, }, }; +#endif if (property == AST_RTP_PROPERTY_RTCP) { if (value) { @@ -4007,6 +3988,31 @@ return; } } +#ifdef HAVE_PJPROJECT + else if (property == AST_RTP_PROPERTY_STUN) { + if (value && !rtp->ice) { + pj_thread_register_check(); + + pj_stun_config_init(&stun_config, &cachingpool.factory, 0, ioqueue, timerheap); + + generate_random_string(rtp->local_ufrag, sizeof(rtp->local_ufrag)); + ufrag = pj_str(rtp->local_ufrag); + generate_random_string(rtp->local_passwd, sizeof(rtp->local_passwd)); + passwd = pj_str(rtp->local_passwd); + /* Create an ICE session for ICE negotiation */ + if (icesupport && pj_ice_sess_create(&stun_config, NULL, PJ_ICE_SESS_ROLE_UNKNOWN, 2, &ast_rtp_ice_sess_cb, &ufrag, &passwd, NULL, &rtp->ice) == PJ_SUCCESS) { + /* Make this available for the callbacks */ + rtp->ice->user_data = rtp; + + ast_rtp_instance_get_local_address(instance, &addr); + /* Add all of the available candidates to the ICE session */ + rtp_add_candidates_to_ice(instance, rtp, &addr, ast_sockaddr_port(&addr), AST_RTP_ICE_COMPONENT_RTP, TRANSPORT_SOCKET_RTP, &ast_rtp_turn_rtp_sock_cb, &rtp->turn_rtp); + ast_debug(1, "Setup ICE on RTP instance '%p'\n", instance); + } + } + } +#endif + return; } Index: res/res_pjsip_sdp_rtp.c =================================================================== --- res/res_pjsip_sdp_rtp.c (revision 402448) +++ res/res_pjsip_sdp_rtp.c (working copy) @@ -123,7 +123,9 @@ ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND); } - if (!session->endpoint->media.rtp.ice_support && (ice = ast_rtp_instance_get_ice(session_media->rtp))) { + if (session->endpoint->media.rtp.ice_support) { + ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_STUN, 1); + } else if ((ice = ast_rtp_instance_get_ice(session_media->rtp))) { ice->stop(session_media->rtp); }