Skip to content

Conversation

@wltechblog
Copy link

Josh at WLTechBlog and others added 5 commits March 5, 2025 13:27
This patch implements comprehensive RTSP timestamp normalization for perfect audio/video sync:

1. Audio frame timestamp improvements:
   - Added timestamp field back to H264NALUnit structure
   - Start timestamps at zero instead of the raw encoder value
   - Calculate precise frame durations based on sample rate
   - Switch to microsecond precision for better accuracy

2. Video timestamp normalization:
   - Base timestamps from a zero reference point
   - Handle cases with missing or zero timestamps
   - Add small increments for frames in the same batch
   - Avoid zero and negative timestamps

3. Timestamp tracking infrastructure:
   - Added base_timestamp and timestamp_initialized fields to streams
   - Store the first timestamp as reference for normalization
   - Reset timestamp on new connections
   - Proper initialization in constructors

4. Better error handling:
   - Drop oversized frames instead of partial delivery
   - Start with non-zero timestamps to avoid player errors
   - Enhanced first frame handling

This approach ensures timestamps for both audio and video start from zero
and increment in perfect synchronization, preventing any A/V sync issues
in players like mpv.
This commits addresses several critical issues with RTSP stream synchronization:

1. Non-Zero Stream Start Time:
   - Streams were starting at arbitrary large timestamps (around 30000+ seconds)
   - Now normalized to start from zero for proper player compatibility

2. Audio/Video Sync Drift:
   - Added base_timestamp and timestamp_initialized fields to both streams
   - First frame timestamp is now exactly zero for clean synchronization
   - Implemented independent timestamp resets to prevent long-term drift

3. Frame Handling and Buffer Management:
   - Added 1ms frame pacing delay for regular frames
   - Added 3ms delay for larger I-frames to prevent network buffer overflows
   - Improved detection and handling of oversized frames
   - Added frame drop tracking and error reporting

4. AudioReframer Improvements:
   - Modified to use precise frame duration calculations with high precision
   - Implemented periodic timestamp reset (every 300 frames)
   - Better timestamp normalization for consistent audio timing

These changes together create a robust synchronization system that maintains
proper A/V sync across different players and network conditions.

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants