From 5288041da522ac34c539dda7f740eef0c219bf77 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 3 Aug 2019 12:47:44 -0700 Subject: [PATCH] Prevent us from consuming more than 4 samples per write callback to bound latency --- app/streaming/audio/renderers/soundioaudiorenderer.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/streaming/audio/renderers/soundioaudiorenderer.cpp b/app/streaming/audio/renderers/soundioaudiorenderer.cpp index ff479b58..4a7eb89a 100644 --- a/app/streaming/audio/renderers/soundioaudiorenderer.cpp +++ b/app/streaming/audio/renderers/soundioaudiorenderer.cpp @@ -386,15 +386,19 @@ void SoundIoAudioRenderer::sioWriteCallback(SoundIoOutStream* stream, int frameC (me->m_OpusChannelCount * stream->bytes_per_sample); int bytesRead = 0; - // Clamp framesLeft to frameCountMax - framesLeft = qMin(framesLeft, frameCountMax); - // Ensure we always write at least a buffer, even if it's silence, to avoid // busy looping when no audio data is available while libsoundio tries to keep // us from starving the output device. frameCountMin = qMax(frameCountMin, (int)(stream->sample_rate * k_MinSampleLengthSec)); + + // Clamp frameCountMax to minSampleLen * 4 to stop our latency from growing if audio packets lag. + // This makes sure that we never increase our latency far beyond what the sink is consuming. + frameCountMax = qMin(frameCountMax, (int)(stream->sample_rate * k_MinSampleLengthSec) * 4); frameCountMin = qMin(frameCountMin, frameCountMax); + // Clamp framesLeft to frameCountMax + framesLeft = qMin(framesLeft, frameCountMax); + // Track latency on queueing-based backends if (me->m_SoundIo->current_backend != SoundIoBackendCoreAudio && me->m_SoundIo->current_backend != SoundIoBackendJack) { soundio_outstream_get_latency(stream, &me->m_Latency);