From 647cc257fdb76268019d22c2f813f7bb291d16e2 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Wed, 23 Mar 2016 15:46:03 -0500
Subject: [PATCH] FS-8913 #resolve [Problem with transfer when using
 bypass_media + SRTP + Inbound late negotiation]

---
 src/include/switch_ivr.h            |  2 +
 src/mod/endpoints/mod_sofia/sofia.c | 51 +++++++++++-----------
 src/switch_core_media.c             | 12 +++++-
 src/switch_ivr.c                    | 67 +++++++++++++++++++++++++++++
 src/switch_ivr_bridge.c             |  5 ++-
 5 files changed, 108 insertions(+), 29 deletions(-)

diff --git a/src/include/switch_ivr.h b/src/include/switch_ivr.h
index ca0c710273..7d6d6ac1db 100644
--- a/src/include/switch_ivr.h
+++ b/src/include/switch_ivr.h
@@ -600,6 +600,8 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_3p_media(const char *uuid, switch_med
 SWITCH_DECLARE(switch_status_t) switch_ivr_nomedia(const char *uuid, switch_media_flag_t flags);
 SWITCH_DECLARE(switch_status_t) switch_ivr_3p_nomedia(const char *uuid, switch_media_flag_t flags);
 
+SWITCH_DECLARE(void) switch_ivr_bg_media(const char *uuid, switch_media_flag_t flags, switch_bool_t on, switch_bool_t is3p, uint32_t delay);
+
 /*!
   \brief Signal the session with a protocol specific hold message.
   \param uuid the uuid of the session to hold
diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c
index 7be98271cb..4c28d6577a 100644
--- a/src/mod/endpoints/mod_sofia/sofia.c
+++ b/src/mod/endpoints/mod_sofia/sofia.c
@@ -2027,18 +2027,14 @@ void sofia_process_dispatch_event_in_thread(sofia_dispatch_event_t **dep)
 	sofia_dispatch_event_t *de = *dep;
 	switch_memory_pool_t *pool;
 	//sofia_profile_t *profile = (*dep)->profile;
-	switch_thread_data_t *td;
+
 
 	switch_core_new_memory_pool(&pool);
 
 	*dep = NULL;
 	de->pool = pool;
 
-	td = switch_core_alloc(pool, sizeof(*td));
-	td->func = sofia_msg_thread_run_once;
-	td->obj = de;
 
-	switch_thread_pool_launch_thread(&td);
 
 }
 
@@ -6630,9 +6626,9 @@ void *SWITCH_THREAD_FUNC media_on_hold_thread_run(switch_thread_t *thread, void
 				switch_channel_wait_for_flag(other_channel, CF_MEDIA_ACK, SWITCH_TRUE, 10000, NULL);
 
 				if (switch_channel_direction(tech_pvt->channel) == SWITCH_CALL_DIRECTION_INBOUND) {
-					switch_ivr_media(switch_core_session_get_uuid(other_session), SMF_REBRIDGE|SMF_REPLYONLY_B);
+					switch_ivr_3p_media(switch_core_session_get_uuid(other_session), SMF_REBRIDGE|SMF_REPLYONLY_B);
 				} else {
-					switch_ivr_media(switch_core_session_get_uuid(other_session), SMF_REBRIDGE);
+					switch_ivr_3p_media(switch_core_session_get_uuid(other_session), SMF_REBRIDGE);
 				}
 
 
@@ -7187,6 +7183,7 @@ static void sofia_handle_sip_i_state(switch_core_session_t *session, int status,
 	case nua_callstate_received:
 		tech_pvt->recv_invites++;
 		tech_pvt->sent_last_invite = 0;
+
 		if (!sofia_test_flag(tech_pvt, TFLAG_SDP)) {
 			if (switch_core_session_get_partner(session, &other_session) == SWITCH_STATUS_SUCCESS) {
 				private_object_t *other_tech_pvt = switch_core_session_get_private(other_session);
@@ -7206,6 +7203,7 @@ static void sofia_handle_sip_i_state(switch_core_session_t *session, int status,
 					goto done;
 				}
 			}
+			
 
 			if (r_sdp && !sofia_test_flag(tech_pvt, TFLAG_SDP)) {
 				if (switch_channel_test_flag(channel, CF_PROXY_MODE)) {
@@ -7435,6 +7433,7 @@ static void sofia_handle_sip_i_state(switch_core_session_t *session, int status,
 					is_t38 = 1;
 				}
 
+
 				if (switch_channel_test_flag(channel, CF_PROXY_MODE) || switch_channel_test_flag(channel, CF_PROXY_MEDIA)) {
 					if ((sofia_test_media_flag(profile, SCMF_DISABLE_HOLD)
 						 || ((var = switch_channel_get_variable(channel, "rtp_disable_hold")) && switch_true(var)))
@@ -7456,6 +7455,7 @@ static void sofia_handle_sip_i_state(switch_core_session_t *session, int status,
 						int media_on_hold = switch_true(switch_channel_get_variable_dup(channel, "bypass_media_resume_on_hold", SWITCH_FALSE, -1));
 
 						switch_core_media_clear_rtp_flag(other_session, SWITCH_MEDIA_TYPE_AUDIO, SWITCH_RTP_FLAG_AUTOADJ);
+
 						
 						if (switch_channel_test_flag(channel, CF_PROXY_MODE) && !is_t38 && 
 							((profile->media_options & MEDIA_OPT_MEDIA_ON_HOLD) || media_on_hold)) {
@@ -7467,7 +7467,7 @@ static void sofia_handle_sip_i_state(switch_core_session_t *session, int status,
 								switch_core_media_clear_rtp_flag(tech_pvt->session, SWITCH_MEDIA_TYPE_AUDIO, SWITCH_RTP_FLAG_AUTOADJ);
 
 								if (!switch_channel_media_ready(channel)) {
-									if (switch_channel_direction(tech_pvt->channel) == SWITCH_CALL_DIRECTION_INBOUND) {
+									//if (switch_channel_direction(tech_pvt->channel) == SWITCH_CALL_DIRECTION_INBOUND) {
 										//const char *r_sdp = switch_channel_get_variable(channel, SWITCH_R_SDP_VARIABLE);
 
 										
@@ -7476,9 +7476,10 @@ static void sofia_handle_sip_i_state(switch_core_session_t *session, int status,
 											switch_channel_set_variable(channel, SWITCH_ENDPOINT_DISPOSITION_VARIABLE, "CODEC NEGOTIATION ERROR");
 											status = SWITCH_STATUS_FALSE;
 											switch_core_session_rwunlock(other_session);
+											switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
 											goto done;
 										}
-									}
+										//}
 								}
 
 
@@ -7490,6 +7491,7 @@ static void sofia_handle_sip_i_state(switch_core_session_t *session, int status,
 										goto done;
 									}
 								}
+
 								switch_core_media_gen_local_sdp(session, SDP_TYPE_RESPONSE, NULL, 0, NULL, 1);
 
 								if (sofia_use_soa(tech_pvt)) {
@@ -7524,8 +7526,9 @@ static void sofia_handle_sip_i_state(switch_core_session_t *session, int status,
 							}
 						}
 
-							other_tech_pvt = switch_core_session_get_private(other_session);
-						if(sofia_test_flag(other_tech_pvt, TFLAG_REINVITED)) {
+						other_tech_pvt = switch_core_session_get_private(other_session);
+
+						if (sofia_test_flag(other_tech_pvt, TFLAG_REINVITED)) {
 							/* The other leg won the reinvite race */
 							switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Other leg already handling reinvite, so responding with 491\n");
 							nua_respond(tech_pvt->nh, SIP_491_REQUEST_PENDING, TAG_END());
@@ -7621,25 +7624,18 @@ static void sofia_handle_sip_i_state(switch_core_session_t *session, int status,
 					}
 				}
 
-
+				
 				if (is_ok) {
 
 					if (switch_core_session_local_crypto_key(tech_pvt->session, SWITCH_MEDIA_TYPE_AUDIO)) {
 						switch_core_media_gen_local_sdp(session, SDP_TYPE_RESPONSE, NULL, 0, NULL, 0);
 					}
-					if (sofia_use_soa(tech_pvt)) {
-						nua_respond(tech_pvt->nh, SIP_200_OK,
-									SIPTAG_CONTACT_STR(tech_pvt->reply_contact),
-									SOATAG_USER_SDP_STR(tech_pvt->mparams.local_sdp_str),
-									SOATAG_REUSE_REJECTED(1),
-									SOATAG_AUDIO_AUX("cn telephone-event"),
-									TAG_IF(sofia_test_pflag(profile, PFLAG_DISABLE_100REL), NUTAG_INCLUDE_EXTRA_SDP(1)), TAG_END());
-					} else {
-						nua_respond(tech_pvt->nh, SIP_200_OK,
-									NUTAG_MEDIA_ENABLE(0),
-									SIPTAG_CONTACT_STR(tech_pvt->reply_contact),
-									SIPTAG_CONTENT_TYPE_STR("application/sdp"), SIPTAG_PAYLOAD_STR(tech_pvt->mparams.local_sdp_str), TAG_END());
-					}
+
+					nua_respond(tech_pvt->nh, SIP_200_OK,
+								NUTAG_MEDIA_ENABLE(0),
+								SIPTAG_CONTACT_STR(tech_pvt->reply_contact),
+								SIPTAG_CONTENT_TYPE_STR("application/sdp"), SIPTAG_PAYLOAD_STR(tech_pvt->mparams.local_sdp_str), TAG_END());
+					
 					if (switch_event_create_subclass(&s_event, SWITCH_EVENT_CUSTOM, MY_EVENT_REINVITE) == SWITCH_STATUS_SUCCESS) {
 						switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "Unique-ID", switch_core_session_get_uuid(session));
 						switch_event_fire(&s_event);
@@ -9211,6 +9207,11 @@ void sofia_handle_sip_i_reinvite(switch_core_session_t *session,
 	if (session) {
 		channel = switch_core_session_get_channel(session);
 		tech_pvt = switch_core_session_get_private(session);
+
+
+		if (sip->sip_payload && sip->sip_payload->pl_data) {
+			tech_pvt->mparams.last_sdp_str = switch_core_session_strdup(session, sip->sip_payload->pl_data);
+		}
 	}
 
 	if (session && profile && sip && sofia_test_pflag(profile, PFLAG_TRACK_CALLS)) {
diff --git a/src/switch_core_media.c b/src/switch_core_media.c
index 8b73e895ae..0ad27cd4eb 100644
--- a/src/switch_core_media.c
+++ b/src/switch_core_media.c
@@ -175,6 +175,7 @@ typedef struct switch_rtp_engine_s {
 	uint8_t new_ice;
 	uint8_t new_dtls;
 	uint32_t sdp_bw;
+	uint8_t reject_avp;
 } switch_rtp_engine_t;
 
 struct switch_media_handle_s {
@@ -3683,7 +3684,8 @@ SWITCH_DECLARE(uint8_t) switch_core_media_negotiate_sdp(switch_core_session_t *s
 	v_engine->new_ice = 1;
 	a_engine->new_dtls = 1;
 	a_engine->new_ice = 1;
-
+	a_engine->reject_avp = 0;
+			
 	switch_core_session_parse_crypto_prefs(session);
 
 	clear_pmaps(a_engine);
@@ -3905,6 +3907,8 @@ SWITCH_DECLARE(uint8_t) switch_core_media_negotiate_sdp(switch_core_session_t *s
 			/* do nothing here, mod_fax will trigger a response (if it's listening =/) */
 			match = 1;
 			goto done;
+		} else if (m->m_type == sdp_media_audio && m->m_port && got_audio && got_savp) {
+			a_engine->reject_avp = 1;
 		} else if (m->m_type == sdp_media_audio && m->m_port && !got_audio) {
 			sdp_rtpmap_t *map;
 			int ice = 0;
@@ -4957,7 +4961,7 @@ SWITCH_DECLARE(int) switch_core_media_toggle_hold(switch_core_session_t *session
 			if (b_channel && (switch_channel_test_flag(session->channel, CF_BYPASS_MEDIA_AFTER_HOLD) ||
 							  switch_channel_test_flag(b_channel, CF_BYPASS_MEDIA_AFTER_HOLD) || bypass_after_hold_a || bypass_after_hold_b)) {
 				/* try to stay out from media stream */
-				switch_ivr_nomedia(switch_core_session_get_uuid(session), SMF_REBRIDGE);
+				switch_ivr_bg_media(switch_core_session_get_uuid(session), SMF_REBRIDGE, SWITCH_FALSE, SWITCH_TRUE, 200);
 			}
 
 			if (a_engine->max_missed_packets && a_engine->rtp_session) {
@@ -8027,6 +8031,10 @@ SWITCH_DECLARE(void) switch_core_media_gen_local_sdp(switch_core_session_t *sess
 		//switch_snprintf(buf + strlen(buf), SDPBUFLEN - strlen(buf), "a=encryption:optional\r\n");
 		}
 
+		if (a_engine->reject_avp) {
+			switch_snprintf(buf + strlen(buf), SDPBUFLEN - strlen(buf), "m=audio 0 RTP/AVP 19\r\n");
+		}
+		
 	} else if (smh->mparams->num_codecs) {
 		int i;
 		int cur_ptime = 0, this_ptime = 0, cng_type = 0;
diff --git a/src/switch_ivr.c b/src/switch_ivr.c
index 0c2af8b9d8..5f8b9ea2bc 100644
--- a/src/switch_ivr.c
+++ b/src/switch_ivr.c
@@ -1557,6 +1557,10 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_unhold_uuid(const char *uuid)
 	return SWITCH_STATUS_SUCCESS;
 }
 
+
+
+
+
 SWITCH_DECLARE(switch_status_t) switch_ivr_3p_media(const char *uuid, switch_media_flag_t flags)
 {
 	const char *other_uuid = NULL;
@@ -1813,7 +1817,9 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_3p_nomedia(const char *uuid, switch_m
 				if (!switch_core_session_in_thread(session)) {
 					switch_channel_set_state(channel, CS_PARK);
 				}
+
 				switch_channel_set_state(other_channel, CS_PARK);
+				
 				if (switch_core_session_in_thread(session)) {
 					switch_yield(100000);
 				} else {
@@ -1884,6 +1890,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_nomedia(const char *uuid, switch_medi
 	msg.from = __FILE__;
 
 	if ((session = switch_core_session_locate(uuid))) {
+
 		status = SWITCH_STATUS_SUCCESS;
 		channel = switch_core_session_get_channel(session);
 
@@ -1969,6 +1976,64 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_nomedia(const char *uuid, switch_medi
 	return status;
 }
 
+typedef struct {
+	switch_memory_pool_t *pool;
+	const char *uuid;
+	switch_media_flag_t flags;
+	switch_bool_t on;
+	switch_bool_t is3p;
+	uint32_t delay;
+} media_job_t;
+
+static void *SWITCH_THREAD_FUNC media_thread_run(switch_thread_t *thread, void *obj)
+{
+	media_job_t *job = (media_job_t *) obj;
+
+	if (job->delay) {
+		switch_yield(job->delay * 1000);
+	}
+
+	if (job->on) {
+		if (job->is3p) {
+			switch_ivr_3p_media(job->uuid, job->flags);
+		} else {
+			switch_ivr_media(job->uuid, job->flags);
+		}
+	} else {
+		if (job->is3p) {
+			switch_ivr_3p_nomedia(job->uuid, job->flags);
+		} else {
+			switch_ivr_nomedia(job->uuid, job->flags);
+		}
+	}
+
+	return NULL;
+}
+
+
+SWITCH_DECLARE(void) switch_ivr_bg_media(const char *uuid, switch_media_flag_t flags, switch_bool_t on, switch_bool_t is3p, uint32_t delay)
+{
+	switch_thread_data_t *td;
+	switch_memory_pool_t *pool;
+	media_job_t *job;
+
+	switch_core_new_memory_pool(&pool);
+	td = switch_core_alloc(pool, sizeof(*td));
+	job = switch_core_alloc(pool, sizeof(*job));
+	td->func = media_thread_run;
+	job->pool = pool;
+	job->uuid = switch_core_strdup(pool, uuid);
+	job->flags = flags;
+	job->on = on;
+	job->is3p = is3p;
+	job->delay = delay;
+	td->obj = job;
+	td->pool = pool;
+	switch_thread_pool_launch_thread(&td);
+	
+}
+
+
 SWITCH_DECLARE(switch_status_t) switch_ivr_session_transfer(switch_core_session_t *session, const char *extension, const char *dialplan,
 															const char *context)
 {
@@ -2008,6 +2073,8 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_session_transfer(switch_core_session_
 	/* reset temp hold music */
 	switch_channel_set_variable(channel, SWITCH_TEMP_HOLD_MUSIC_VARIABLE, NULL);
 
+	switch_channel_execute_on(channel, "execute_on_blind_transfer");
+	
 	if ((profile = switch_channel_get_caller_profile(channel))) {
 		const char *var;
 
diff --git a/src/switch_ivr_bridge.c b/src/switch_ivr_bridge.c
index 63d6e567ca..46755e3b59 100644
--- a/src/switch_ivr_bridge.c
+++ b/src/switch_ivr_bridge.c
@@ -552,7 +552,7 @@ static void *audio_bridge_thread(switch_thread_t *thread, void *obj)
 			
 			if ((bypass_media_after_bridge || switch_channel_test_flag(chan_b, CF_BYPASS_MEDIA_AFTER_BRIDGE)) && switch_channel_test_flag(chan_a, CF_ANSWERED)
 				&& switch_channel_test_flag(chan_b, CF_ANSWERED)) {
-				switch_ivr_nomedia(switch_core_session_get_uuid(session_a), SMF_REBRIDGE);
+				switch_ivr_3p_nomedia(switch_core_session_get_uuid(session_a), SMF_REBRIDGE);
 				bypass_media_after_bridge = 0;
 				switch_channel_clear_flag(chan_b, CF_BYPASS_MEDIA_AFTER_BRIDGE);
 				goto end_of_bridge_loop;
@@ -781,7 +781,8 @@ static void *audio_bridge_thread(switch_thread_t *thread, void *obj)
 	switch_channel_clear_flag(chan_a, CF_BRIDGED);
 	
 	if (switch_channel_test_flag(chan_a, CF_LEG_HOLDING) || switch_channel_test_flag(chan_a, CF_HANGUP_HELD)) {
-		if (switch_channel_ready(chan_b) && switch_channel_get_state(chan_b) != CS_PARK && !data->other_leg_data->clean_exit) {
+		if (switch_channel_ready(chan_b) &&
+			switch_channel_get_state(chan_b) != CS_PARK && !data->other_leg_data->clean_exit && !switch_channel_test_flag(chan_b, CF_3P_NOMEDIA_REQUESTED)) {
 			const char *ext = switch_channel_get_variable(chan_a, "hold_hangup_xfer_exten");
 			
 			switch_channel_stop_broadcast(chan_b);