Skip to content

Conversation

@yairzahavi
Copy link

@yairzahavi yairzahavi commented Feb 26, 2025

a fork of #4189

GOP Cache
This PR introduces Group of Pictures (GOP) caching to MediaMTX, enhancing its performance and reducing latency in streaming scenarios. By caching the last GOP for each stream, new subscribers can immediately receive the latest video data without waiting for the next keyframe, improving the user experience, especially for streams with long keyframe intervals.

This works for both H264 and H265, as well as for RTSP and WebRTC.

Configurable Cache Settings:
Introduced a new configuration parameter gopCache in mediamtx.yml for enabling/disabling GOP caching.

Fix: #1209 @jean343

The feature does work in the following scenarios:

Protocol:

  • WebRTC
  • RTSP

Codecs:

  • H.264
  • H.265

I have only tried videos without b-frames as WebRTC does not support b-frames. There might be adjustments to make when dealing with b-frames over RTSP.

In order to reduce RAM exhaustion, we do not cache anything until we get a key frame, this will prevent unsupported codecs from storing anything, and will save a little bit for supported codecs. In case the GOP is really long, we trim at 512 packets, conserving memory. In this case, clients will need to wait until the next key frame before video playback.

As per additional codecs, I could not find a reliable way to detect their keyframes.

In the WebRTC playback scenario, the PTS and Timestamp needs to be modified to prevent gaps. WebRTC will pause and stop playback if set incorrectly.

For example, incorrect Timestamp will look like:

Untitled.mov
Correct timestamp will look like:

Screen.Recording.2025-01-23.at.1.40.16.PM.mov

[ ] Fixed Compatibility with mediacommon v2
[ ] Added Support for AV1

@yairzahavi yairzahavi force-pushed the feature/mediamtx-gop-cache branch from ff6f8a2 to 276d15b Compare March 10, 2025 11:57
@yairzahavi
Copy link
Author

hey @jean343 @aler9.
I'd be happy if the both of you could take a look on my PR.
I changed @jean343 caching logic to try and limit cache usage and memory allocation as much as possible yet i'm not confident as i never worked with go or video encodings before today.

@yairzahavi yairzahavi force-pushed the feature/mediamtx-gop-cache branch from a5aef78 to 824f1cb Compare March 10, 2025 15:15
@yairzahavi yairzahavi closed this Mar 17, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Sep 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache H264 GOPs in order to allow readers to decode frames immediately

2 participants