Skip to content

libobs, obs-x264: Ask a running encoder for a keyframe when an output joins - #594

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/replay-record-keyframe
Oct 13, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/replay-record-keyframe

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

API note (please review): this adds one optional callback, request_keyframe, at the end of struct obs_encoder_info, documented in reference-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 when get_priming_samples was 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, optional request_keyframe (appended to obs_encoder_info, so the struct stays compatible through obs_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.

  1. test/cmocka: Add failing test for an output joining a running encoder: adds request_keyframe to obs_encoder_info (nothing calls it yet) and test_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:
    • joining a running encoder: the first packet must come within 1 s (fails today: nothing within 3 s, no request);
    • controls: the output that starts the encoder makes no request; an encoder without request_keyframe still works, its joining output waiting for the next scheduled keyframe.
  2. 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_keyframe here, 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_LIBOBS OFF 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).
  • Full ctest at GREEN, rebased on current master: 175 of 175 in each lane, with every PipeWire and PulseAudio test actually run (TMPDIR unset; only the known open_crashes_without_obs_core skips).
  • clang-format and gersemi are clean; no new compiler warnings in the changed files.

By hand: the end-to-end runs above with a frontend build of this branch.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

rdoupe and others added 2 commits October 13, 2026 11:21
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]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 13, 2026 15:27
@zackees

zackees commented Oct 13, 2026

Copy link
Copy Markdown
Member

Verified locally under xvfb-run (Tier 2 image + mesa):

  • RED 38dd186: test_encoder_join_keyframe fails joining_output_gets_a_keyframe_soon
  • GREEN 4b811b1: 3/3. It needs a display, so plain CI skips it.
  • ABI: request_keyframe is appended as the last member of struct obs_encoder_info, which encoders register with its size, so older plugins are unaffected.

Holding the merge for @zackees: this adds a public encoder API member (documented in reference-encoders.rst).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

2 participants