From 7c75c8e394ecfdfce17bd2fc5db3e41f1144757b Mon Sep 17 00:00:00 2001 From: Donat Zenichev Date: Fri, 23 May 2025 15:35:28 +0200 Subject: [PATCH] MT#59962 AmRtpAudio: setPlayoutType properly acquire the lock Cover the whole conditioning with the lock, because otherwise two threads competing on execution can suddenly come across the `(m_playout_type != type)` getting true (for both) and one of them by acquiring the lock can potentially manage to modify that, what can actually make `(m_playout_type != type)` not true anymore for the other thread in race condition here. Change-Id: Ie352da10a515837efc696063837b70dc2c9a304d --- core/AmRtpAudio.cpp | 46 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/core/AmRtpAudio.cpp b/core/AmRtpAudio.cpp index 4baad745..db39c970 100644 --- a/core/AmRtpAudio.cpp +++ b/core/AmRtpAudio.cpp @@ -468,31 +468,27 @@ void AmRtpAudio::add_to_history(int16_t *buffer, unsigned int size) void AmRtpAudio::setPlayoutType(PlayoutType type) { + session->lockAudio(); if (m_playout_type != type) - { - if (type == ADAPTIVE_PLAYOUT) { - session->lockAudio(); - m_playout_type = type; - if (fmt.get()) - playout_buffer.reset(new AmAdaptivePlayout(this,getSampleRate())); - session->unlockAudio(); - DBG("Adaptive playout buffer activated\n"); - } - else if (type == JB_PLAYOUT) { - session->lockAudio(); - m_playout_type = type; - if (fmt.get()) - playout_buffer.reset(new AmJbPlayout(this,getSampleRate())); - session->unlockAudio(); - DBG("Adaptive jitter buffer activated\n"); - } - else { - session->lockAudio(); - m_playout_type = type; - if (fmt.get()) - playout_buffer.reset(new AmPlayoutBuffer(this,getSampleRate())); - session->unlockAudio(); - DBG("Simple playout buffer activated\n"); - } + { + if (type == ADAPTIVE_PLAYOUT) { + m_playout_type = type; + if (fmt.get()) + playout_buffer.reset(new AmAdaptivePlayout(this,getSampleRate())); + DBG("Adaptive playout buffer activated\n"); + } + else if (type == JB_PLAYOUT) { + m_playout_type = type; + if (fmt.get()) + playout_buffer.reset(new AmJbPlayout(this,getSampleRate())); + DBG("Adaptive jitter buffer activated\n"); } + else { + m_playout_type = type; + if (fmt.get()) + playout_buffer.reset(new AmPlayoutBuffer(this,getSampleRate())); + DBG("Simple playout buffer activated\n"); + } + } + session->unlockAudio(); }