diff --git a/src/switch_core_io.c b/src/switch_core_io.c index 64c0d11593..6807df760c 100644 --- a/src/switch_core_io.c +++ b/src/switch_core_io.c @@ -69,7 +69,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi { switch_io_event_hook_read_frame_t *ptr; switch_status_t status = SWITCH_STATUS_FALSE; - int need_codec, perfect, do_bugs = 0, do_resample = 0, is_cng = 0, tap_only = 0; + int need_codec, perfect, bug_locked = 0, global_prune = 0, do_bugs = 0, do_resample = 0, is_cng = 0, tap_only = 0; switch_codec_implementation_t codec_impl; unsigned int flag = 0; int i; @@ -121,6 +121,13 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi switch_mutex_lock(session->read_codec->mutex); + switch_thread_rwlock_rdlock(session->bug_rwlock); + if (session->bugs) { + bug_locked = 1; + } else { + switch_thread_rwlock_unlock(session->bug_rwlock); + } + top: for(i = 0; i < 2; i++) { @@ -237,8 +244,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi switch_bool_t ok = SWITCH_TRUE; int prune = 0; - switch_thread_rwlock_rdlock(session->bug_rwlock); - for (bp = session->bugs; bp; bp = bp->next) { ok = SWITCH_TRUE; @@ -275,10 +280,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi prune++; } } - switch_thread_rwlock_unlock(session->bug_rwlock); if (prune) { - switch_core_media_bug_prune(session); + global_prune++; } } @@ -299,7 +303,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi int prune = 0; if (session->bugs && switch_test_flag((*frame), SFF_CNG)) { - switch_thread_rwlock_rdlock(session->bug_rwlock); for (bp = session->bugs; bp; bp = bp->next) { ok = SWITCH_TRUE; @@ -342,10 +345,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi prune++; } } - switch_thread_rwlock_unlock(session->bug_rwlock); if (prune) { - switch_core_media_bug_prune(session); + global_prune++; } @@ -438,12 +440,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi } else { switch_codec_t *use_codec = read_frame->codec; if (do_bugs) { - switch_thread_rwlock_wrlock(session->bug_rwlock); - if (!session->bugs) { - switch_thread_rwlock_unlock(session->bug_rwlock); - goto done; - } - if (!switch_core_codec_ready(&session->bug_codec) && switch_core_codec_ready(read_frame->codec)) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Setting BUG Codec %s:%d\n", read_frame->codec->implementation->iananame, read_frame->codec->implementation->ianacode); @@ -457,14 +453,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi if (switch_core_codec_ready(&session->bug_codec)) { use_codec = &session->bug_codec; } - switch_thread_rwlock_unlock(session->bug_rwlock); - - switch_thread_rwlock_wrlock(session->bug_rwlock); - if (!session->bugs) { - do_bugs = 0; - } - switch_thread_rwlock_unlock(session->bug_rwlock); - if (!do_bugs) goto done; } if (!switch_test_flag(read_frame->codec, SWITCH_CODEC_FLAG_HAS_PLC) && @@ -486,7 +474,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi codec = read_frame->codec; } - switch_thread_rwlock_rdlock(session->bug_rwlock); codec->cur_frame = read_frame; session->read_codec->cur_frame = read_frame; status = switch_core_codec_decode(codec, @@ -501,8 +488,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi session->raw_read_frame.channels = codec->implementation->number_of_channels; codec->cur_frame = NULL; session->read_codec->cur_frame = NULL; - switch_thread_rwlock_unlock(session->bug_rwlock); - } if (status == SWITCH_STATUS_SUCCESS && session->read_impl.number_of_channels == 1) { @@ -636,7 +621,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi switch_media_bug_t *bp; switch_bool_t ok = SWITCH_TRUE; int prune = 0; - switch_thread_rwlock_rdlock(session->bug_rwlock); for (bp = session->bugs; bp; bp = bp->next) { ok = SWITCH_TRUE; @@ -677,9 +661,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi } - switch_thread_rwlock_unlock(session->bug_rwlock); + if (prune) { - switch_core_media_bug_prune(session); + global_prune++; } } @@ -687,7 +671,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi switch_media_bug_t *bp; switch_bool_t ok = SWITCH_TRUE; int prune = 0; - switch_thread_rwlock_rdlock(session->bug_rwlock); for (bp = session->bugs; bp; bp = bp->next) { ok = SWITCH_TRUE; @@ -738,9 +721,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi prune++; } } - switch_thread_rwlock_unlock(session->bug_rwlock); + if (prune) { - switch_core_media_bug_prune(session); + global_prune++; } } @@ -868,7 +851,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi switch_media_bug_t *bp; switch_bool_t ok = SWITCH_TRUE; int prune = 0; - switch_thread_rwlock_rdlock(session->bug_rwlock); + for (bp = session->bugs; bp; bp = bp->next) { ok = SWITCH_TRUE; @@ -907,9 +890,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi prune++; } } - switch_thread_rwlock_unlock(session->bug_rwlock); + if (prune) { - switch_core_media_bug_prune(session); + global_prune++; } } } @@ -922,6 +905,14 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi *frame = &runtime.dummy_cng_frame; } + if (global_prune) { + switch_core_media_bug_prune(session); + } + + if (bug_locked) { + switch_thread_rwlock_unlock(session->bug_rwlock); + } + switch_mutex_unlock(session->read_codec->mutex); switch_mutex_unlock(session->codec_read_mutex);