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.
This commit is contained in:
Cameron Gutman
2026-05-10 21:53:09 -05:00
parent c21affd15a
commit 4cf498b09d
2 changed files with 5 additions and 3 deletions
+2 -1
View File
@@ -21,5 +21,6 @@ public:
private:
SDL_AudioDeviceID m_AudioDevice;
void* m_AudioBuffer;
int m_FrameSize;
Uint32 m_FrameSize;
Uint32 m_FrameDurationMs;
};
+3 -2
View File
@@ -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;
}