From 6bfc05b81e2abc280e018397ad8b2758417631de Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Thu, 2 Oct 2014 11:55:53 -0500
Subject: [PATCH] FS-6887 #resolve #comment new bug flag always_auto_adjust
 (also implicitly sets accept_any_packets)

---
 src/include/switch_types.h | 11 ++++++++++-
 src/switch_core_media.c    |  8 ++++++++
 src/switch_rtp.c           | 20 ++++++++++++++++----
 3 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/src/include/switch_types.h b/src/include/switch_types.h
index 145d743274..910b44b338 100644
--- a/src/include/switch_types.h
+++ b/src/include/switch_types.h
@@ -839,7 +839,7 @@ typedef enum {
 	
 	/* FLUSH JITTERBUFFER When getting RFC2833 to reduce bleed through */
 
-	RTP_BUG_ACCEPT_ANY_PAYLOAD = (1 << 11)
+	RTP_BUG_ACCEPT_ANY_PAYLOAD = (1 << 11),
 
 	/* 
 	  Make FS accept any payload type instead of dropping and returning CNG frame. Workaround while FS only supports a single payload per rtp session.
@@ -847,6 +847,15 @@ typedef enum {
 	  This should probably be a flag, but flag enum is already full!
 	*/
 
+
+	RTP_BUG_ALWAYS_AUTO_ADJUST = (1 << 12)
+
+	/*
+	  Leave the auto-adjust behavior enableed permenantly rather than only at appropriate times.  (IMPLICITLY sets RTP_BUG_ACCEPT_ANY_PACKETS)
+
+	 */
+
+
 } switch_rtp_bug_flag_t;
 
 #ifdef _MSC_VER
diff --git a/src/switch_core_media.c b/src/switch_core_media.c
index ffb54a7448..b0b5ee4ec3 100644
--- a/src/switch_core_media.c
+++ b/src/switch_core_media.c
@@ -859,6 +859,14 @@ SWITCH_DECLARE(void) switch_core_media_parse_rtp_bugs(switch_rtp_bug_flag_t *fla
 	if (switch_stristr("~FLUSH_JB_ON_DTMF", str)) {
 		*flag_pole &= ~RTP_BUG_FLUSH_JB_ON_DTMF;
 	}
+
+	if (switch_stristr("ALWAYS_AUTO_ADJUST", str)) {
+		*flag_pole |= (RTP_BUG_ALWAYS_AUTO_ADJUST | RTP_BUG_ACCEPT_ANY_PACKETS);
+	}
+
+	if (switch_stristr("~ALWAYS_AUTO_ADJUST", str)) {
+		*flag_pole &= ~(RTP_BUG_ALWAYS_AUTO_ADJUST | RTP_BUG_ACCEPT_ANY_PACKETS);
+	}
 }
 
 
diff --git a/src/switch_rtp.c b/src/switch_rtp.c
index 086d7b0e1c..306a3217d3 100644
--- a/src/switch_rtp.c
+++ b/src/switch_rtp.c
@@ -1002,7 +1002,11 @@ static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *d
 								return;
 							}
 							
-							switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
+							if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
+								switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
+							} else {
+								switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
+							}
 							
 						}
 					}
@@ -5804,19 +5808,27 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
 					}
 					rtp_session->auto_adj_used = 1;
 					switch_rtp_set_remote_address(rtp_session, tx_host, switch_sockaddr_get_port(rtp_session->from_addr), 0, SWITCH_FALSE, &err);
-					switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
+					if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
+						switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
+					} else {
+						switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
+					}
 					if (rtp_session->ice.ice_user) {
 						rtp_session->ice.addr = rtp_session->remote_addr;
 					}
 				}
 			} else {
 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Correct ip/port confirmed.\n");
-				switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
+				if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
+					switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
+				} else {
+					switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
+				}
 				rtp_session->auto_adj_used = 0;
 			}
 		}
 
-		if (bytes && rtp_session->autoadj_window) {
+		if (bytes && !(rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST) && rtp_session->autoadj_window) {
 			if (--rtp_session->autoadj_window == 0) {
 				switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
 			}