From 7bc43c226d4a958e712fa5d1be98f695be01ed39 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Thu, 13 Sep 2018 14:09:03 -0700 Subject: [PATCH] Fix underflow in audio drop code --- app/streaming/audio/renderers/sdl.h | 8 ++++---- app/streaming/audio/renderers/sdlaud.cpp | 13 +++++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/app/streaming/audio/renderers/sdl.h b/app/streaming/audio/renderers/sdl.h index 78c5d30e..614919a0 100644 --- a/app/streaming/audio/renderers/sdl.h +++ b/app/streaming/audio/renderers/sdl.h @@ -20,9 +20,9 @@ public: private: SDL_AudioDeviceID m_AudioDevice; - int m_ChannelCount; - int m_PendingDrops; - int m_PendingHardDrops; - unsigned int m_SampleIndex; + Uint32 m_ChannelCount; + Uint32 m_PendingDrops; + Uint32 m_PendingHardDrops; + Uint32 m_SampleIndex; Uint32 m_BaselinePendingData; }; diff --git a/app/streaming/audio/renderers/sdlaud.cpp b/app/streaming/audio/renderers/sdlaud.cpp index a414819d..cf705f62 100644 --- a/app/streaming/audio/renderers/sdlaud.cpp +++ b/app/streaming/audio/renderers/sdlaud.cpp @@ -155,7 +155,7 @@ void SdlAudioRenderer::submitAudio(short* audioBuffer, int audioSize) { m_SampleIndex++; - Uint32 queuedAudio = qMax((int)SDL_GetQueuedAudioSize(m_AudioDevice) - (int)m_BaselinePendingData, 0); + Uint32 queuedAudio = qMax(SDL_GetQueuedAudioSize(m_AudioDevice) - m_BaselinePendingData, 0U); Uint32 framesQueued = queuedAudio / (SAMPLES_PER_FRAME * m_ChannelCount * sizeof(short)); // We must check this prior to the below checks to ensure we don't @@ -163,14 +163,19 @@ void SdlAudioRenderer::submitAudio(short* audioBuffer, int audioSize) if (framesQueued <= MIN_QUEUED_FRAMES) { m_PendingDrops = m_PendingHardDrops = 0; } - // Pend enough drops to get us back to MIN_QUEUED_FRAMES - else if (framesQueued - m_PendingHardDrops > STOP_THE_WORLD_LIMIT) { + // Pend enough drops to get us back to MIN_QUEUED_FRAMES, checking first + // to ensure we don't underflow. + else if (framesQueued > m_PendingHardDrops && + framesQueued - m_PendingHardDrops > STOP_THE_WORLD_LIMIT) { m_PendingHardDrops = framesQueued - MIN_QUEUED_FRAMES; SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Pending hard drop of %u audio frames", m_PendingHardDrops); } - else if (framesQueued - m_PendingHardDrops - m_PendingDrops > MAX_QUEUED_FRAMES) { + // If we're under the stop the world limit, we can drop samples + // gracefully over the next little while. + else if (framesQueued > m_PendingHardDrops + m_PendingDrops && + framesQueued - m_PendingHardDrops - m_PendingDrops > MAX_QUEUED_FRAMES) { m_PendingDrops = framesQueued - MIN_QUEUED_FRAMES; }