Fix underflow in audio drop code

This commit is contained in:
Cameron Gutman
2018-09-13 14:09:03 -07:00
parent da4697794d
commit 7bc43c226d
2 changed files with 13 additions and 8 deletions
+4 -4
View File
@@ -20,9 +20,9 @@ public:
private: private:
SDL_AudioDeviceID m_AudioDevice; SDL_AudioDeviceID m_AudioDevice;
int m_ChannelCount; Uint32 m_ChannelCount;
int m_PendingDrops; Uint32 m_PendingDrops;
int m_PendingHardDrops; Uint32 m_PendingHardDrops;
unsigned int m_SampleIndex; Uint32 m_SampleIndex;
Uint32 m_BaselinePendingData; Uint32 m_BaselinePendingData;
}; };
+9 -4
View File
@@ -155,7 +155,7 @@ void SdlAudioRenderer::submitAudio(short* audioBuffer, int audioSize)
{ {
m_SampleIndex++; 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)); Uint32 framesQueued = queuedAudio / (SAMPLES_PER_FRAME * m_ChannelCount * sizeof(short));
// We must check this prior to the below checks to ensure we don't // 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) { if (framesQueued <= MIN_QUEUED_FRAMES) {
m_PendingDrops = m_PendingHardDrops = 0; m_PendingDrops = m_PendingHardDrops = 0;
} }
// Pend enough drops to get us back to MIN_QUEUED_FRAMES // Pend enough drops to get us back to MIN_QUEUED_FRAMES, checking first
else if (framesQueued - m_PendingHardDrops > STOP_THE_WORLD_LIMIT) { // to ensure we don't underflow.
else if (framesQueued > m_PendingHardDrops &&
framesQueued - m_PendingHardDrops > STOP_THE_WORLD_LIMIT) {
m_PendingHardDrops = framesQueued - MIN_QUEUED_FRAMES; m_PendingHardDrops = framesQueued - MIN_QUEUED_FRAMES;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Pending hard drop of %u audio frames", "Pending hard drop of %u audio frames",
m_PendingHardDrops); 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; m_PendingDrops = framesQueued - MIN_QUEUED_FRAMES;
} }