Lead program audio by the jitter buffer's own depth - #4
Merged
Conversation
Program audio was stamped at capture time, which made the feed unusable. The buffered-audio source picks buffers by comparing their timestamps to its output clock: it consumes everything already in the past and returns only the newest. Audio stamped at capture time is ALWAYS behind "now", so every tick drained the whole queue and threw away all but one buffer of each burst — and emitted digital silence whenever it then found the queue empty. What reached viewers was a fraction of the audio interleaved with zeros, which is what "robotic and stuttery" sounds like. The built-in buffered-audio path already does this (`AudioUnit .appendBufferedBuiltinAudio` adds `latency` before queueing); program audio just never got the same treatment. Shift is applied inside the engine rather than asked of the caller — the caller stamps at capture time, which is the honest thing for it to know, and the engine owns its own jitter buffer. FakeStreamEngine models the shift too, and exposes the resulting timestamp: a fake that skipped it would let a consumer pass its tests against audio the real engine discards. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
Program audio reached viewers as a fraction of the real audio interleaved with digital silence — "robotic and stuttery" in the user's words.
BufferedAudio.getSampleBufferselects by timestamp: it walks the queue consuming every buffer whose PTS is at or behind the output clock, returns only the newest one consumed, and when it finds the queue empty it re-emits the previous buffer with its samples zeroed.We were stamping program audio at capture time. That is by definition always behind "now", so:
The fix
Shift the presentation timestamp forward by the source's own target latency before queueing — which is exactly what the built-in path already does:
Program audio simply never got the same treatment.
Applied inside the engine rather than pushed onto the caller: the caller stamps at capture time, which is the honest thing for it to know, and the engine owns its own jitter buffer.
Verification
appendLeadsByTargetLatency)FakeStreamEnginemodels the shift and exposeslastProgramAudioPresentationTime— a fake that skipped it would let a consumer pass against audio the real engine throws awayScripts/check-containment.shclean🤖 Generated with Claude Code