Skip to content

bug: libobs: a grouped encoder with a frame rate divisor can miss the group's start frame and never start #2920

Closed

Description

@vpacher

Location

libobs/obs-encoder.c.h receive_video(): a grouped encoder starts only on the frame whose timestamp equals the group's start_timestamp:

if (encoder->encoder_group && !encoder->start_ts) {
	...
	ready = obs_encoder_audio_start_group_ready(group->start_timestamp, frame->timestamp); /* group_start == timestamp */
	...
	if (!ready)
		goto wait_for_audio;
}

libobs/media-io/video-io.c.h video_output_cur_frame(): an input with a frame rate divisor takes every n-th frame. It counts from the first frame video-io hands it after video_output_connect2(), where frame_rate_divisor_counter starts at 0:

// an explicit counter is used instead of remainder calculation
// to allow multiple encoders started at the same time to start on
// the same frame
uint32_t skip = input->frame_rate_divisor_counter++;

libobs/obs-video.c.h video_sleep(): once every encoder of a ready group is started, it sets group->start_timestamp = *p_time, the time of the next frame the video thread renders.

Class

Logic error (race): a grouped encoder with a frame rate divisor above 1 sometimes never starts, so it never sends a packet.

How it is reached

The encoders connect to video-io (add_connection()) while the video thread runs. Sometimes video-io still has a frame queued from before start_timestamp when the encoders connect. That frame is the first one every input gets, so it sets the phase of the divisor counter. An encoder with divisor d then gets frames −1, d−1, 2d−1, … relative to the start frame, never the start frame itself, so group_start == timestamp never holds. Divisor 1 encoders skip the early frame and start on frame 0 as intended.

Seen in a scratch harness against the C libobs (master of 2026-10-17, macOS arm64, Metal, 30 fps): one group of three test encoders with divisors 1, 2 and 3 on one multitrack video output, started 8 times. An lldb log of receive_video() shows, relative to the start frame:

start divisor 1 divisor 2 divisor 3 divisor 2 and 3 started
clean (2 of 8) 0, 1, 2, … 0, 2, 4, … 0, 3, 6, … yes
early frame (6 of 8) −1, 0, 1, … −1, 1, 3, … −1, 2, 5, … never

A run without lldb: 5 of 8 starts left both divisor encoders at 0 frames. The divisor 2 and 3 encoders always fail together, because one early frame shifts both.

In the app: frontend/utility/MultitrackVideoOutput.cpp.hpp adjust_encoder_frame_rate_divisor() sets a divisor when the Go Live configuration asks for a rung at a lower frame rate than the canvas, for example a 30 fps rung on a 60 fps canvas. That rung's encoder can then send nothing. I have not reproduced this with a real Enhanced Broadcasting stream; the harness uses test encoders. No upstream issue found (searched obsproject/obs-studio for "frame rate divisor encoder group" and "multitrack divisor start").

Found while writing test_encoder_group_reconfig_walk for #2913 (PR #2919). That test runs every encoder at divisor 1 because of this race.

Only the user's own configuration and their streaming service's Go Live response reach this, and it stops a rung from starting; it reads or writes no memory out of bounds. So it is a normal public bug under SECURITY.md's two tiers.

Proposed fix and failing test

The fix is for the maintainers to choose. Options:

  • Align the divisor counters with the group start: when video_sleep() sets start_timestamp, also make each grouped encoder's input take the start frame, for example a video-io call that resets an input's frame_rate_divisor_counter so its next frame is not skipped.
  • Or connect a grouped encoder's video input only once the group is ready, so the first frame each input gets is the start frame.
  • Or start a grouped encoder on the first frame with timestamp >= start_timestamp. This alone moves divisor encoders off the shared start frame, which breaks the pts alignment the reconfigure rules (rust: Port the encoder group reconfigure rules in obs-encoder.c to obs-output #2913) depend on, so it would need the counter change as well.

RED: a real-libobs test (Linux, core video, under xvfb-run, like test_encoder_group_reconfig_walk) starts a group of encoders with divisors 1, 2 and 3 on a multitrack video output, 20 times. It asserts that every encoder encodes a frame within 1.5 s and that each encoder's first frame is the group's start frame. It fails on master in most runs.

GREEN: the fix, with the same test passing 20 of 20 starts. Then test_encoder_group_reconfig_walk can use divisors 1, 2 and 3 again.

Activity

  1. added
    bug
    on Oct 29, 2026
  2. zackees commented on Oct 29, 2026

    @zackees
    Member

    Claiming: investigation

    Taking this up as the worker on obs-studio2. Plan:

    1. Reproduce the divisor phase shift at the video-io level: confirm frame_rate_divisor_counter is per-input and starts at 0 on video_output_connect2(), so a frame queued before group->start_timestamp consumes the 0 slot.
    2. RED: a cmocka case over the real video_output_* API that connects a divisor-2 input, feeds one frame before the group start, then the start frame, and asserts the input accepts the start frame (fails on master).
    3. GREEN: align the counter with the group start (the issue's first option) — a video-io call that resets an input's frame_rate_divisor_counter from video_sleep() where group->start_timestamp is set — and keep test_encoder_group_reconfig_walk free to use divisors > 1.
    4. PR as fix/2920-grouped-encoder-divisor-start, Refs #2920.

    No Decision needed:: option 1 is the minimal change that preserves the shared start frame that the #2913 reconfigure rules depend on (the issue rules out option 3 alone for exactly that reason).

  3. added
    needs-investigation
    on Oct 29, 2026
  4. zackees commented on Oct 29, 2026

    @zackees
    Member

    /ready

    Triage (worker): meets the definition of Ready — path:line for all three mechanisms (obs-encoder.c.h receive_video(), video-io.c.h video_output_cur_frame(), obs-video.c.h video_sleep()), a named class (race / logic error), an 8-start harness with a per-divisor table, an explicit RED test sketch and a fix that is implementable without changing the shared start frame. Scope is one race; no Decision needed: remains once option 1 is chosen, and nothing blocks it.

    Priority P2: a streamed rung that silently sends no packets is user-visible, but it needs a Go Live response asking for a lower-fps rung, and the harness shows it on most (not all) starts. size:S–M: one counter reset plus one test.

  5. added
    ready
    and removed
    needs-investigation
    on Oct 29, 2026
  6. zackees commented on Oct 29, 2026

    @zackees
    Member

    Claiming: grouped encoder frame-rate-divisor start alignment

    Implementation of option 1 at the three sites listed; branch fix/2920-grouped-encoder-divisor-start, one PR, RED test first.

  7. zackees commented on Oct 29, 2026

    @zackees
    Member

    Progress: draft PR #2926 (RED + fix, local GREEN on test_video_io_hidden).

    • RED test/cmocka/test_video_io_hidden.c::group_start_aligns_the_divisor_counter reports 66667666 != 33334333 on master: a divisor-2 input receives the frame after the group start frame.
    • GREEN: video_output_reset_frame_rate_divisor() + obs_encoder_align_frame_rate_divisor() called from video_sleep() when group->start_timestamp is set; 4 test(s) pass locally (xvfb-run -a ./test/cmocka/test_video_io_hidden).

    Still open in the draft, per the acceptance in the issue: the real-libobs 20-start harness (modelled on test_encoder_group_reconfig_walk from #2919), the GPU-encode path (obs-video-gpu-encode.c.h keeps its own counter and is not aligned here), and the docs/features.md line. The new video-io helper is EXPORT; a hidden alternative is described in the PR.

  8. zackees commented on Oct 29, 2026

    @zackees
    Member

    Progress: the real-libobs case from the issue is in #2926, and it changed the fix.

    Measured, not assumed. I added the issue's RED as divisor_encoders_start_on_the_group_frame
    in test/cmocka/test_encoder_group_reconfig_walk.c (#2919's harness): three encoders at
    divisors 1, 2 and 3 on one multitrack video output, every encoder must encode a frame and its
    first frame must be the group's start frame. Against origin/master (68771844d) it is red 5 of
    5 runs, with the divisor 2 and 3 encoders at 0 frames:

    encoder 0 (divisor 1): 150 frames, first pts 0
    encoder 1 (divisor 2): 0 frames, first pts 0
    encoder 2 (divisor 3): 0 frames, first pts 0
    [  ERROR   ] --- started is not true
    

    The counter reset alone does not fix it. With the first version of the fix (clear
    frame_rate_divisor_counter when video_sleep() sets start_timestamp) the same case still
    reported 0 frames for both divisor encoders in every run: the video thread renders further
    frames between the reset and the start frame, and each of them advances the counter again, so
    "the next frame is the start frame" is not a property a reset can give.

    So the API now carries the frame the phase starts on:
    video_output_align_frame_rate_divisor(video, start_timestamp, callback, param) makes video-io
    drop the frames rendered before start_timestamp without moving the counter, and deliver
    the first frame at or after it whatever the counter was. Same case is now green 5 of 5 runs,
    with all three encoders encoding from the same first pts.

    Also settled in the PR:

    • video-io is a Rust port, so the production half is in rust/obs-media-io
      (Input::start_timestamp, VideoOutput::align_divisor(), shim + Windows /EXPORT entry);
      libobs/media-io/video-io.c keeps the C reference and the C oracle renames the symbol so
      it does not collide in cargo test. Tier 1 and a Tier 3 C-vs-Rust parity case
      (divisor_align_matches) cover it.
    • The GPU encode path needs no change: start_gpu_encode()'s own counter is advanced only
      after the group's start_timestamp check, so pre-start frames never consume its slots.
    • docs/features.md line added (Enhanced Broadcasting, lower-frame-rate rung).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugready

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions