Repository navigation
libobs, obs-x264: Ask a running encoder for a keyframe when an output joins - #594
Merged
Merged
Conversation
With the replay buffer running, stopping a recording takes seconds and leaves a broken file, at frame rates below 30. In the current release at 20 fps (Simple output, x264), a 6 s recording started while the replay buffer runs holds 0.9 s; stopped again later it takes 4.8 s to stop and leaves a 717-byte file ffprobe cannot read. Without the replay buffer, or at 30 fps in that run, the recordings are whole. The recording joins the video encoder the replay buffer already runs, and libobs sends an output nothing before a keyframe (send_first_video_packet in obs-encoder.c). The recording therefore waits for the encoder's next scheduled keyframe: with x264's default interval of 250 frames, up to 12.5 s at 20 fps. A recording stopped before then has nothing; ffmpeg_muxer only finishes stopping on a packet past the stop time, so it waits for that keyframe and writes headers and nothing else. This adds request_keyframe to obs_encoder_info (optional, video encoders, appended at the end so the struct stays compatible through obs_register_encoder_s), for libobs to ask a running encoder for a keyframe when another output starts on it; nothing calls it yet. The test runs a video encoder that makes a keyframe every 300 frames (10 s) or when asked, starts a "replay buffer" output on it, then a "recording" on the same encoder. On master: joining a running encoder: no packet within 3 s, no keyframe request 1 of 3 cases fails. The controls pass: the output that starts the encoder makes no request (an encoder starts with a keyframe), and an encoder without request_keyframe still works, its joining output waiting for the next scheduled keyframe. Core video needs a display on Linux; the cases skip without one (run under xvfb-run). Reported upstream by Flopblopper. Refs obs-rust#593. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… joins An output that starts on a video encoder another output already runs (a recording started while the replay buffer runs) gets no packet before a keyframe, so it waited for the encoder's next scheduled one: up to 12.5 s with x264's default interval at 20 fps. The recording missed its first seconds, and one stopped before that keyframe took seconds to stop and left a file with nothing but headers. When an output joins a running video encoder, libobs now calls the encoder's request_keyframe, and obs-x264 makes its next frame an IDR frame. The joining output gets its first packet within a frame or two. Encoders that do not implement request_keyframe behave as before. In the current release at 20 fps with the replay buffer running, a 6 s recording held 0.9 s, and the next one took 4.8 s to stop and could not be read; with this change both hold 6.1 s and stop in 1.4 s, as without the replay buffer. Fixes obs-rust#593. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Member
|
Verified locally under xvfb-run (Tier 2 image + mesa):
Holding the merge for @zackees: this adds a public encoder API member (documented in reference-encoders.rst). |
This was referenced Oct 13, 2026
Closed
12 tasks done
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.
API note (please review): this adds one optional callback,
request_keyframe, at the end ofstruct obs_encoder_info, documented inreference-encoders.rst. Registration goes by struct size, so encoders built against the old struct keep working and simply don't get the request (same approach as whenget_priming_sampleswas added). If you'd rather not grow the encoder API, the alternative is to finish a stop immediately when the muxer received nothing; that avoids the slow stop but still leaves an empty recording.Description
A recording started while the replay buffer runs now starts right away, instead of waiting for the shared encoder's next scheduled keyframe. Before, at 20 fps with x264's default keyframe interval, a 6 s recording held 0.9 s, and one stopped before that keyframe took about 5 s to stop and left an unreadable file.
An output gets no video packet before a keyframe (
send_first_video_packet()), and a recording started next to the replay buffer joins the encoder the replay buffer already runs. When an output starts on a running video encoder, libobs now calls the encoder's new, optionalrequest_keyframe(appended toobs_encoder_info, so the struct stays compatible throughobs_register_encoder_s; documented in reference-encoders.rst), and obs-x264 makes its next frame an IDR frame. Encoders that do not implement it behave exactly as before.test/cmocka: Add failing test for an output joining a running encoder: addsrequest_keyframetoobs_encoder_info(nothing calls it yet) andtest_encoder_join_keyframe: a video encoder that makes a keyframe every 10 s or when asked, a "replay buffer" output on it, then a "recording" on the same encoder. The cases are:request_keyframestill works, its joining output waiting for the next scheduled keyframe.libobs, obs-x264: Ask a running encoder for a keyframe when an output joins: the fix. 3 of 3 pass (first packet after 25-30 ms).Motivation and Context
Fixes #593 (upstream obsproject/obs-studio#12069, reported by Flopblopper, who found it happens below 30 fps).
Reproduced in the current release (AppImage 33.0.0-beta6-799), headless, private config, over obs-websocket: Simple output, x264 veryfast, mkv, 20 fps, replay buffer running, a 6 s recording, twice. The first recording held 0.9 s; the second took 4.79 s to stop and its 717-byte file could not be read.
With the fix, the same steps in a frontend build of this branch (Rust libobs, obs-websocket), two runs of two recordings each: every recording holds 6.1 s (121 video, 279 audio packets) and stops in 1.36 s.
Scope: only obs-x264 implements
request_keyframehere, which covers the reproduction. Hardware encoders (NVENC, QSV, AMF, VA-API) and the FFmpeg-based encoders keep the old behaviour until they implement it; the reporter's encoder is not known (their log could not be read). This was not tested on Windows.Why this one: data loss in recordings for a common setup (replay buffer always on), reported by a real user with a clear trigger, reproduced in our release, fixed at the root rather than by hiding the stop delay.
How Has This Been Tested?
Automated, in both libobs lanes (
ENABLE_RUST_LIBOBSOFF and ON), on current master:test_encoder_join_keyframe: 1 of 3 cases fails at the RED commit and 3 of 3 pass at GREEN in each lane (core video under Xvfb; all cases ran).open_crashes_without_obs_coreskips).By hand: the end-to-end runs above with a frontend build of this branch.
Types of changes
Checklist:
🤖 Generated with Claude Code