fix(transcoder): snap A/V to a common origin at startup for AV-path sources#30
Merged
Merged
Conversation
…ources The transcoder maps audio and video onto the output timeline with a single shared offset, assuming both ride one source clock — true for MPEG-TS (shared PCR) but not always for AV-path/RTMP sources, where audio can flow for seconds before the first video keyframe and the two tracks may carry independently-based timestamps. The first track to emit seeded the offset, so a late-arriving second track inherited it and baked a large, permanent A/V offset into the output (observed up to ~22s on an RTMP-pull source: HLS preserved it as audible lip-sync, DASH distorted it differently). Source probes showed the upstream A/V synced to a few ms — the skew was introduced here, not at ingest. Add a per-track cross-track origin snap mirroring the ingest timeline normaliser's CrossTrackSnapMs: when the second track's mapped start diverges from the already-emitting track's output by more than crossTrackSnapMs (1 s), the source clocks are treated as independently based and the late track is snapped onto the early track's output line so both start in lockstep. Skews below the threshold are preserved so a genuine intrinsic A/V offset survives. The shared offset, releaseAudio gate, and regress re-anchor are unchanged; the common steady-state path is byte-identical (snap is zero unless triggered). Validated live: an RTMP-pull stream went from a ~22 s output A/V offset to within a few ms, stable across a 1.5 h soak with no drift or regression. Tests: TestCrossTrackSnap, TestRebase_RTMPStartupAudioBeforeVideo_SnapsVideo, TestRebase_CoherentStartup_NoSnap.
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.
What
The transcoder maps audio and video onto its output timeline with a single shared offset, assuming both ride one source clock — true for MPEG-TS (shared PCR) but not always for AV-path/RTMP sources, where audio can flow for seconds before the first video keyframe and the two tracks may carry independently-based timestamps. The first track to emit seeded the offset, so a late-arriving second track inherited it and baked a large, permanent A/V offset into the output.
Observed on an RTMP-pull source: output A/V offset up to ~22 s (audio ahead / video lagging). HLS preserved it (audible lip-sync); DASH distorted it differently. Source probes showed the upstream A/V synced to a few ms, and ingest traces showed the raw-ingest (post-normaliser) V/A coherent — so the skew was introduced in the transcoder, not at ingest.
Fix
A per-track cross-track origin snap mirroring the ingest timeline normaliser's
CrossTrackSnapMs: when the second track's mapped start diverges from the already-emitting track's output by more thancrossTrackSnapMs(1 s), the source clocks are treated as independently based and the late track is snapped onto the early track's output line so both start in lockstep. Skews below the threshold are preserved so a genuine intrinsic A/V offset survives.The shared offset,
releaseAudiogate, and regress re-anchor are unchanged; the common steady-state path is byte-identical (the snap is zero unless triggered).Validation
TestCrossTrackSnap,TestRebase_RTMPStartupAudioBeforeVideo_SnapsVideo,TestRebase_CoherentStartup_NoSnap.make checkgreen (native package built + raced with local libav; lint 0 issues).