Skip to content

MP4 DVR: Fix audio/video synchronization issues in WebRTC recordings. v6.0.172 v7.0.52#4230

Merged
winlinvip merged 5 commits intoossrs:developfrom
suzp1984:issue/3993-offsync-av-dvr
Aug 12, 2025
Merged

MP4 DVR: Fix audio/video synchronization issues in WebRTC recordings. v6.0.172 v7.0.52#4230
winlinvip merged 5 commits intoossrs:developfrom
suzp1984:issue/3993-offsync-av-dvr

Conversation

@suzp1984
Copy link
Copy Markdown
Contributor

@suzp1984 suzp1984 commented Nov 12, 2024

Problem

Fixes #3993 - WebRTC streams recorded to MP4 via DVR exhibit audio/video synchronization issues, with audio typically ahead of video. Note: This issue is specific to MP4 format; FLV recordings are not affected.

Root Cause

When WebRTC streams are converted to RTMP and then muxed to MP4, the audio and video tracks may start at different timestamps. The MP4 muxer was not accounting for this timing offset between the first audio and video samples in the STTS (Sample Time-to-Sample) table, causing the tracks to be misaligned in the final MP4 file.

Solution

Introduces SrsMp4DvrJitter class specifically for MP4 audio/video synchronization:

  • Timestamp Tracking: Records the DTS of the first audio and video samples
  • Offset Calculation: Computes the timing difference between track start times
  • MP4 STTS Correction: Sets appropriate sample_delta values in the MP4 STTS table to maintain proper A/V sync

Key Changes

  • Added SrsMp4DvrJitter class in srs_kernel_mp4.hpp/cpp
  • Integrated jitter correction into SrsMp4SampleManager::write_track() for MP4 format only
  • Added comprehensive unit tests covering various timing scenarios
  • Scope: Changes are isolated to MP4 kernel code and do not affect FLV processing

This fix ensures that MP4 DVR recordings from WebRTC streams maintain proper audio/video synchronization regardless of the relative timing of the first audio and video frames, while leaving FLV format processing unchanged.


Co-authored-by: Haibo Chen 495810242@qq.com
Co-authored-by: john hondaxiao@tencent.com

@winlinvip winlinvip added the EnglishNative This issue is conveyed exclusively in English. label Nov 12, 2024
@winlinvip winlinvip force-pushed the develop branch 2 times, most recently from 195435d to 97e2b64 Compare July 1, 2025 14:39
@winlinvip winlinvip changed the title fix av off sync for mp4 dvr. MP4 DVR: Fix audio/video synchronization issues in WebRTC recordings Aug 12, 2025
@winlinvip winlinvip force-pushed the issue/3993-offsync-av-dvr branch from 2d8b405 to 45734e7 Compare August 12, 2025 13:44
@winlinvip winlinvip changed the title MP4 DVR: Fix audio/video synchronization issues in WebRTC recordings MP4 DVR: Fix audio/video synchronization issues in WebRTC recordings. v6.0.172 v7.0.52 Aug 12, 2025
@winlinvip winlinvip added the RefinedByAI Refined by AI/GPT. label Aug 12, 2025
@winlinvip winlinvip merged commit a5fee3d into ossrs:develop Aug 12, 2025
14 of 15 checks passed
winlinvip added a commit that referenced this pull request Aug 12, 2025
… v6.0.172 v7.0.52 (#4230)

Fixes #3993 - WebRTC streams recorded to MP4 via DVR exhibit audio/video
synchronization issues, with audio typically ahead of video. **Note:
This issue is specific to MP4 format; FLV recordings are not affected.**

When WebRTC streams are converted to RTMP and then muxed to MP4, the
audio and video tracks may start at different timestamps. The MP4 muxer
was not accounting for this timing offset between the first audio and
video samples in the STTS (Sample Time-to-Sample) table, causing the
tracks to be misaligned in the final MP4 file.

Introduces `SrsMp4DvrJitter` class specifically for MP4 audio/video
synchronization:

- **Timestamp Tracking**: Records the DTS of the first audio and video
samples
- **Offset Calculation**: Computes the timing difference between track
start times
- **MP4 STTS Correction**: Sets appropriate `sample_delta` values in the
MP4 STTS table to maintain proper A/V sync

- Added `SrsMp4DvrJitter` class in `srs_kernel_mp4.hpp/cpp`
- Integrated jitter correction into `SrsMp4SampleManager::write_track()`
for MP4 format only
- Added comprehensive unit tests covering various timing scenarios
- **Scope**: Changes are isolated to MP4 kernel code and do not affect
FLV processing

This fix ensures that MP4 DVR recordings from WebRTC streams maintain
proper audio/video synchronization regardless of the relative timing of
the first audio and video frames, while leaving FLV format processing
unchanged.

---------

Co-authored-by: Haibo Chen <495810242@qq.com>
Co-authored-by: john <hondaxiao@tencent.com>
Co-authored-by: winlin <winlinvip@gmail.com>
Co-authored-by: OSSRS-AI <winlinam@gmail.com>
winlinvip added a commit that referenced this pull request Aug 12, 2025
… v6.0.172 (#4230)

Fixes #3993 - WebRTC streams recorded to MP4 via DVR exhibit audio/video
synchronization issues, with audio typically ahead of video. **Note:
This issue is specific to MP4 format; FLV recordings are not affected.**

When WebRTC streams are converted to RTMP and then muxed to MP4, the
audio and video tracks may start at different timestamps. The MP4 muxer
was not accounting for this timing offset between the first audio and
video samples in the STTS (Sample Time-to-Sample) table, causing the
tracks to be misaligned in the final MP4 file.

Introduces `SrsMp4DvrJitter` class specifically for MP4 audio/video
synchronization:

- **Timestamp Tracking**: Records the DTS of the first audio and video
samples
- **Offset Calculation**: Computes the timing difference between track
start times
- **MP4 STTS Correction**: Sets appropriate `sample_delta` values in the
MP4 STTS table to maintain proper A/V sync

- Added `SrsMp4DvrJitter` class in `srs_kernel_mp4.hpp/cpp`
- Integrated jitter correction into `SrsMp4SampleManager::write_track()`
for MP4 format only
- Added comprehensive unit tests covering various timing scenarios
- **Scope**: Changes are isolated to MP4 kernel code and do not affect
FLV processing

This fix ensures that MP4 DVR recordings from WebRTC streams maintain
proper audio/video synchronization regardless of the relative timing of
the first audio and video frames, while leaving FLV format processing
unchanged.

---------

Co-authored-by: Haibo Chen <495810242@qq.com>
Co-authored-by: john <hondaxiao@tencent.com>
Co-authored-by: winlin <winlinvip@gmail.com>
Co-authored-by: OSSRS-AI <winlinam@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

EnglishNative This issue is conveyed exclusively in English. RefinedByAI Refined by AI/GPT.

Development

Successfully merging this pull request may close these issues.

WebRTC: Off-sync audio/video in DVR files

5 participants