From 4cf498b09df96a1ca3f236257e287388bdb2f31f Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sun, 10 May 2026 21:53:09 -0500 Subject: [PATCH] Use queued audio duration instead of queued frame count to constrain latency This avoids latency explosion when we negotiate 10 ms instead of 5 ms audio packets. --- app/streaming/audio/renderers/sdl.h | 3 ++- app/streaming/audio/renderers/sdlaud.cpp | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/streaming/audio/renderers/sdl.h b/app/streaming/audio/renderers/sdl.h index cbcf6631..05b2d003 100644 --- a/app/streaming/audio/renderers/sdl.h +++ b/app/streaming/audio/renderers/sdl.h @@ -21,5 +21,6 @@ public: private: SDL_AudioDeviceID m_AudioDevice; void* m_AudioBuffer; - int m_FrameSize; + Uint32 m_FrameSize; + Uint32 m_FrameDurationMs; }; diff --git a/app/streaming/audio/renderers/sdlaud.cpp b/app/streaming/audio/renderers/sdlaud.cpp index cb4b9ead..9ce494a7 100644 --- a/app/streaming/audio/renderers/sdlaud.cpp +++ b/app/streaming/audio/renderers/sdlaud.cpp @@ -32,6 +32,7 @@ bool SdlAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION* // The buffering helps avoid audio underruns due to network jitter. want.samples = SDL_max(480, opusConfig->samplesPerFrame * 3); + m_FrameDurationMs = opusConfig->samplesPerFrame / (opusConfig->sampleRate / 1000); m_FrameSize = opusConfig->samplesPerFrame * opusConfig->channelCount * getAudioBufferSampleSize(); @@ -115,8 +116,8 @@ bool SdlAudioRenderer::submitAudio(int bytesWritten) return false; } - // Only queue more samples where there are 10 frames or less in SDL's queue - if (SDL_GetQueuedAudioSize(m_AudioDevice) / m_FrameSize <= 10) { + // Only queue more samples where there is 50 ms or less in SDL's queue + if (SDL_GetQueuedAudioSize(m_AudioDevice) / m_FrameSize * m_FrameDurationMs <= 50) { break; }