fix/media-looping-boundary#268
Merged
Merged
Conversation
Guard against large forward PTS/DTS jumps that can occur when looping source MP4s or when upstream RTSP/ffmpeg inserts offsets/stalls. In flushPendingVideoSample() clamp an excessively large video sample duration to a plausible ceiling (1s hard cap, or LastVideoSampleDTS*10 if smaller), falling back to LastVideoSampleDTS or 33ms, and log a warning. In AddSampleToTrack() clamp audio sample durations if the new dts is >10x the previous audio DTS and log a warning. These changes prevent huge sample durations that cause trun/sidx/mvhd discontinuities and browser playback errors.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens MP4 fragment generation against large forward PTS/DTS discontinuities (e.g., MP4 loop boundaries or upstream RTSP/ffmpeg stalls) by clamping unexpectedly large sample durations for video and audio to avoid broken trun/sidx/mvhd timelines and browser playback failures.
Changes:
- Added a max-duration clamp in
flushPendingVideoSample()to handle large forward video PTS jumps and emit a warning. - Added a duration clamp in
AddSampleToTrack()to handle large forward audio PTS jumps and emit a warning.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+248
to
+252
| log.Log.Warning(fmt.Sprintf("mp4.flushPendingVideoSample(): video PTS jumped forward (nextPTS=%d, prevDTS=%d, gap=%d ms) - clamping to %d ms (likely source loop/stall discontinuity)", nextPTS, mp4.VideoFullSample.DecodeTime, duration, maxPlausible)) | ||
| duration = mp4.LastVideoSampleDTS | ||
| if duration == 0 { | ||
| duration = 33 | ||
| } |
Comment on lines
+247
to
+249
| if duration > maxPlausible { | ||
| log.Log.Warning(fmt.Sprintf("mp4.flushPendingVideoSample(): video PTS jumped forward (nextPTS=%d, prevDTS=%d, gap=%d ms) - clamping to %d ms (likely source loop/stall discontinuity)", nextPTS, mp4.VideoFullSample.DecodeTime, duration, maxPlausible)) | ||
| duration = mp4.LastVideoSampleDTS |
| // audio trun would carry an enormous sample duration that | ||
| // renders the recording unplayable in browsers. | ||
| if mp4.LastAudioSampleDTS > 0 && dts > mp4.LastAudioSampleDTS*10 { | ||
| log.Log.Warning(fmt.Sprintf("mp4.AddSampleToTrack(): audio PTS jumped forward (pts=%d, prevDTS=%d, gap=%d) - clamping to last known duration", pts, mp4.AudioFullSample.DecodeTime, dts)) |
Comment on lines
+236
to
+242
| // Guard against forward PTS jumps (e.g. when looping a source MP4 | ||
| // through virtual-rtsp the upstream ffmpeg may insert a large offset | ||
| // at the loop boundary, or the RTSP stream may stall briefly). | ||
| // Without this clamp the sample gets a huge duration which appears | ||
| // as a discontinuity in the trun/sidx/mvhd and causes browsers | ||
| // (Video.js / MSE) to abort playback with a "media corruption" | ||
| // error around the loop boundary. |
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.
Guard against large forward PTS/DTS jumps that can occur when looping source MP4s or when upstream RTSP/ffmpeg inserts offsets/stalls. In flushPendingVideoSample() clamp an excessively large video sample duration to a plausible ceiling (1s hard cap, or LastVideoSampleDTS*10 if smaller), falling back to LastVideoSampleDTS or 33ms, and log a warning. In AddSampleToTrack() clamp audio sample durations if the new dts is >10x the previous audio DTS and log a warning. These changes prevent huge sample durations that cause trun/sidx/mvhd discontinuities and browser playback errors.