Skip to content

Commit 4cf498b

Browse files
committed
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.
1 parent c21affd commit 4cf498b

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

app/streaming/audio/renderers/sdl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ class SdlAudioRenderer : public IAudioRenderer
2121
private:
2222
SDL_AudioDeviceID m_AudioDevice;
2323
void* m_AudioBuffer;
24-
int m_FrameSize;
24+
Uint32 m_FrameSize;
25+
Uint32 m_FrameDurationMs;
2526
};

app/streaming/audio/renderers/sdlaud.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ bool SdlAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION*
3232
// The buffering helps avoid audio underruns due to network jitter.
3333
want.samples = SDL_max(480, opusConfig->samplesPerFrame * 3);
3434

35+
m_FrameDurationMs = opusConfig->samplesPerFrame / (opusConfig->sampleRate / 1000);
3536
m_FrameSize = opusConfig->samplesPerFrame *
3637
opusConfig->channelCount *
3738
getAudioBufferSampleSize();
@@ -115,8 +116,8 @@ bool SdlAudioRenderer::submitAudio(int bytesWritten)
115116
return false;
116117
}
117118

118-
// Only queue more samples where there are 10 frames or less in SDL's queue
119-
if (SDL_GetQueuedAudioSize(m_AudioDevice) / m_FrameSize <= 10) {
119+
// Only queue more samples where there is 50 ms or less in SDL's queue
120+
if (SDL_GetQueuedAudioSize(m_AudioDevice) / m_FrameSize * m_FrameDurationMs <= 50) {
120121
break;
121122
}
122123

0 commit comments

Comments
 (0)