From 98d453c7d908ca57ca1cd6d575d96cc0034089ce Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Thu, 14 Nov 2013 04:18:59 +0500
Subject: [PATCH] move media reset code a little deeper so it does not
 interrupt media when there is no change

---
 src/switch_core_media.c | 35 ++++++++++++++++-------------------
 1 file changed, 16 insertions(+), 19 deletions(-)

diff --git a/src/switch_core_media.c b/src/switch_core_media.c
index eee0affc86..b03b90aae8 100644
--- a/src/switch_core_media.c
+++ b/src/switch_core_media.c
@@ -1469,29 +1469,10 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_read_frame(switch_core_session
 						engine->read_impl.samples_per_packet;
 				}
 			}
-
 			
-			if (session->read_resampler) {
-				switch_mutex_lock(session->resample_mutex);
-				switch_resample_destroy(&session->read_resampler);
-				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Deactivating read resampler\n");
-				switch_mutex_unlock(session->resample_mutex);
-			}
-
-			if (session->write_resampler) {
-				switch_mutex_lock(session->resample_mutex);
-				switch_resample_destroy(&session->write_resampler);
-				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Deactivating write resampler\n");
-				switch_mutex_unlock(session->resample_mutex);
-			}
-
-			switch_core_session_reset(session, 0, 0);
-
-
 			engine->check_frames = 0;
 			engine->last_ts = 0;
 
-			switch_channel_audio_sync(session->channel);
 			do_cng = 1;
 		}
 
@@ -1995,6 +1976,22 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_set_codec(switch_core_session_
 			(uint32_t) a_engine->read_impl.microseconds_per_packet / 1000 != a_engine->cur_payload_map->codec_ms ||
 			a_engine->read_impl.samples_per_second != a_engine->cur_payload_map->rm_rate ) {
 			
+			if (session->read_resampler) {
+				switch_mutex_lock(session->resample_mutex);
+				switch_resample_destroy(&session->read_resampler);
+				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Deactivating read resampler\n");
+				switch_mutex_unlock(session->resample_mutex);
+			}
+
+			if (session->write_resampler) {
+				switch_mutex_lock(session->resample_mutex);
+				switch_resample_destroy(&session->write_resampler);
+				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Deactivating write resampler\n");
+				switch_mutex_unlock(session->resample_mutex);
+			}
+
+			switch_core_session_reset(session, 0, 0);
+			switch_channel_audio_sync(session->channel);
 
 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, 
 							  "Changing Codec from %s@%dms@%dhz to %s@%dms@%luhz\n",