Skip to content

libobs: start a divisor encoder's phase at the group's start frame (#2920) - #2926

Merged
zackees merged 5 commits into
masterfrom
fix/2920-grouped-encoder-divisor-start
Oct 29, 2026
Merged

zackees merged 5 commits into
masterfrom
fix/2920-grouped-encoder-divisor-start

Conversation

@zackees

@zackees zackees commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Fixes #2920.

Symptom

A grouped encoder starts only on the frame whose timestamp equals
group->start_timestamp (receive_video(), libobs/obs-encoder.c.h), and
video_sleep() sets that timestamp to the next frame the video thread renders.
video-io, however, decides which frames an input receives with a per-input
frame_rate_divisor_counter that starts at 0 when the input connects. A frame
already rendered before the group start consumes that first slot, so an encoder
with a frame rate divisor above 1 is one phase ahead, never receives the start
frame, and the encoder never starts. In the app that is a rung of an Enhanced
Broadcasting ladder that runs at a lower frame rate than the canvas: it sends
nothing.

What

  • video_output_align_frame_rate_divisor(video, start_timestamp, callback, param) (libobs/media-io/video-io.h) marks where a divisor phase starts:
    video-io drops the frames rendered before start_timestamp without moving
    the counter
    , and delivers the first frame at or after it whatever the
    counter was. It clears the counter under video->input_mutex and returns
    false when the callback is not connected.
  • obs_encoder_align_frame_rate_divisor(encoder, start_timestamp) calls it for
    a grouped encoder whose divisor is above 1 (a divisor-1 input takes every
    frame, so it needs nothing).
  • video_sleep() aligns every member of the group at the moment it sets
    group->start_timestamp.

Lock order: the video thread holds video->input_mutex while it calls
receive_video(), which takes the group's mutex. video_sleep() therefore
takes the member references under the group mutex and runs the alignment after
dropping it, so the order is never group -> input.

Why the timestamp, and not just a counter reset. A plain reset is racy: the
real-libobs case below still recorded zero frames for the divisor 2 and 3
encoders with a reset-only fix (the video thread rendered further frames
between the reset and the start frame, and each of them moved the counter
again). Tying the phase to the frame the group starts on is deterministic.

Rust half. video-io.c is a port: production libobs links
rust/obs-media-io, where the input's counter lives. The PR therefore adds
Input::start_timestamp + VideoOutput::align_divisor(), the
video_output_align_frame_rate_divisor shim in ffi/video_io.rs, and the
Windows /EXPORT entry in libobs/cmake/rust-exports.d/obs-media-io-video_io.txt.
libobs/media-io/video-io.c keeps the C reference, and the C oracle renames the
new symbol (oracle/), so its copy does not collide with the shim in
cargo test.

GPU path checked, no change needed. start_gpu_encode() has its own
encoder->frame_rate_divisor_counter, but it advances only after the group's
start_timestamp check (libobs/obs-video-gpu-encode.c.h, the continue at
the group check precedes the skip = ...counter lines), so pre-start frames
never consume its slots and its first post-start frame is already phase 0.

RED

New Tier 3 case divisor_encoders_start_on_the_group_frame in
test/cmocka/test_encoder_group_reconfig_walk.c (the #2919 harness): a group of
three test encoders at divisors 1, 2 and 3 on one multitrack video output must
each encode a frame, and each one's first frame must be the group's start frame.

On origin/master (68771844d + this test commit only), 5 of 5 runs fail:

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
[   LINE   ] --- test/cmocka/test_encoder_group_reconfig_walk.c:471: error: Failure!
[  FAILED  ] divisor_encoders_start_on_the_group_frame
[  PASSED  ] 4 test(s).

The unit-level RED (group_start_aligns_the_divisor_counter in
test_video_io_hidden.c, 66667666 != 33334333 on master) is the first commit
of the branch and still fails for the same reason. It pins the two properties a
counter reset cannot give either: the frame rendered before the phase start is
dropped (and does not move the counter) and the frame at the phase start is
delivered. Its post-phase cadence assertion was dropped after Windows Tier 2
measured it red: on that runner the frame the video thread was already holding
is consumed after the alignment, so the cadence there is timing dependent. The
end-to-end cadence is pinned by the real-libobs case instead.

GREEN

Same real-libobs case, built and run in the obs-rust-linux-validate
RelWithDebInfo tree under xvfb-run:

1/2 Test #293: test_video_io_hidden ...............   Passed    0.07 sec
2/2 Test #391: test_encoder_group_reconfig_walk ...   Passed    3.22 sec
100% tests passed, 0 tests failed out of 2

The encoder N: … frames, first pts lines now show all three encoders
encoding from the same first pts.

Tier 1 + Tier 3 parity (soldr cargo test -p obs-media-io --lib divisor):

test tests::video::video_io::align_divisor_restarts_the_phase ... ok
test tests::video::video_io_parity::divisor_align_matches ... ok
test tests::video::video_io::frame_rate_divisor_skips_every_other ... ok
test tests::video::video_io_parity::divisor_skip_matches ... ok
...
test result: ok. 6 passed; 0 failed

divisor_align_matches drives the Rust implementation and the C oracle through
the same sequence and compares the delivered timestamps byte for byte.

docs/features.md carries the user-visible line (last entry under Fixed
upstream bugs → Encoding, streaming and recording
).

CI

Fast Gate ⚡, Format 🔍 / checks, Rust Tier 2 Linux (build) and all six
Rust Tier 2 Linux (tests OFF/ON) partitions, plus Rust Tier 2 (windows-2025-vs2026),
are green on this head. macOS Tier 2 was still running when this was written.

Note: another checkout of this worker fleet force-pushed this branch back to its
earlier reset-only state while the phase fix was in review; the head here is the
phase fix (see the comment of 2026-10-29 16:04Z for the measurement that rules
the reset-only version out).

Notes for review

  • EXPORT on the new function. It is a new dynamic symbol in libobs.
    Without it the binary that compiles video-io.c itself cannot link it. A
    hidden symbol would need a test binary that also compiles the caller side, or
    the call routed through an already-exported entry point.
  • One-shot alignment, not a permanent offset. The input's phase marker is
    consumed by the first frame at or after start_timestamp; after that the
    input is a normal divisor input again, so a later reconfigure does not shift
    its cadence.
  • The harness starts the group once per run (the obs-output: Port the encoder group reconfigure rules #2919 cases are timed by
    cmocka's 5 s wait); the run above is 5/5 green and the master base is 5/5 red.

@zackees
zackees force-pushed the fix/2920-grouped-encoder-divisor-start branch from f0f3f31 to e899662 Compare October 29, 2026 15:23
@zackees zackees changed the title libobs: align a grouped encoder's divisor phase with the group start (#2920) libobs: start a divisor encoder's phase at the group's start frame (#2920) Oct 29, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this to In progress in obs-rust Multiview Oct 29, 2026
zackees and others added 5 commits October 29, 2026 09:00
#2920)

video-io hands each input every frame_rate_divisor-th frame from a counter
that starts at 0 when the input connects. A frame already queued before a
grouped encoder's start_timestamp therefore consumes its first slot, so an
encoder with a divisor above 1 is one frame out of phase and never receives
the frame the group starts on.

The new case connects a divisor-2 input, delivers one frame, then asserts the
next frame it receives is the group's start frame. It fails on master with
`66667666 != 33334333`: the input gets the frame after the start frame.
…2920)

video_sleep() sets group->start_timestamp to the next frame the video thread
renders, but video-io's per-input divisor counter was left wherever the
pre-start frames had moved it, so an encoder with a frame-rate divisor above 1
never received that frame and the group never started.

video_output_reset_frame_rate_divisor() clears one input's counter under
video->input_mutex. obs_encoder_align_frame_rate_divisor() calls it for a
grouped encoder whose divisor is above 1 (divisor 1 always takes every frame),
and video_sleep() now aligns every member of a group at the moment it sets the
start timestamp. The references are taken under the group mutex and the
alignment runs after it is dropped: aligning takes video-io's input mutex, and
the video thread already holds that mutex when receive_video() takes the group
mutex, so doing it under both would invert the order.

GREEN: test_video_io_hidden's group_start_aligns_the_divisor_counter now resets
before the start frame and gets 4 test(s) passing; on master the same case
reports `66667666 != 33334333`.
…2920)

video_sleep() sets group->start_timestamp to the next frame the video thread
renders, but video-io decides which frames an input gets with a per-input
counter that starts at 0 when the input connects. A frame already rendered
before the group start consumes that first slot, so an encoder with a
frame-rate divisor above 1 is one phase ahead and never receives the start
frame: group_start == timestamp never holds and the encoder never starts.

Clearing the counter alone is not enough - the video thread renders more
frames between the reset and the start frame in some runs (measured: the
real-libobs case below still saw zero frames for the divisor 2 and 3
encoders), so the input has to know where the phase starts.

video_output_align_frame_rate_divisor(video, start_timestamp, cb, param)
clears one input's counter and marks the phase start: video-io drops the
frames rendered before start_timestamp without moving the counter, and the
first frame at or after it is delivered whatever the counter was.
obs_encoder_align_frame_rate_divisor() calls it for a grouped encoder whose
divisor is above 1 (divisor 1 takes every frame), and video_sleep() aligns
every member of a group at the moment it sets the start timestamp. The
references are taken under the group mutex and the alignment runs after it
is dropped: aligning takes video-io's input mutex, and the video thread
already holds that mutex when receive_video() takes the group mutex, so
doing it under both would invert the order.

video-io is a Rust port, so the production half is Input::start_timestamp
and VideoOutput::align_divisor() in rust/obs-media-io (with the shim in
ffi/video_io.rs and the Windows /EXPORT entry); libobs/media-io/video-io.c
keeps the reference implementation, and the C oracle renames the new symbol
so its copy does not collide with the shim in cargo test.
…ame (#2920)

A group of three encoders at frame rate 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. This is the case the issue reported; it is red on
the counter-reset fix and on master (the divisor 2 and 3 encoders record zero
frames) and green with the phase alignment.

The fixture gains start_group_divisors() so a case can set a divisor before
the output starts, and the encoder records its first pts.
@zackees
zackees force-pushed the fix/2920-grouped-encoder-divisor-start branch from e899662 to bb9b70d Compare October 29, 2026 16:00
@zackees

zackees commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

Reopening to trigger CI: no check runs were ever created for bb9b70d, so the PR sat with zero checks.

@zackees zackees closed this Oct 29, 2026
@zackees zackees reopened this Oct 29, 2026
@zackees
zackees force-pushed the fix/2920-grouped-encoder-divisor-start branch from bb9b70d to 076a60a Compare October 29, 2026 16:04
@zackees

zackees commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

Branch collision notice — this branch is being force-pushed by two checkouts of the fleet, and the two versions are not equivalent. Please do not push the reset-only state again.

  • The state just pushed (076a60a2, "clang-format the divisor-reset declaration" on top of "align a grouped encoder's divisor phase") is this branch's earlier state: it clears frame_rate_divisor_counter and nothing else.
  • That version was measured and does not fix bug: libobs: a grouped encoder with a frame rate divisor can miss the group's start frame and never start #2920. With it, the real-libobs case from the issue (divisor_encoders_start_on_the_group_frame in test_encoder_group_reconfig_walk.c, divisors 1/2/3 on one multitrack video output) still records zero frames for the divisor 2 and 3 encoders in every run: the video thread renders further frames between the reset and the start frame, and each one moves the counter again, so "the next frame is the start frame" is not something a counter reset can give.
  • The head pushed here now carries the phase-based fix: video_output_align_frame_rate_divisor(video, start_timestamp, cb, param) drops the frames rendered before the start timestamp without moving the counter, and delivers the first frame at or after it. It also has the Rust half (video-io is a port: Input::start_timestamp, VideoOutput::align_divisor(), the FFI shim and the Windows /EXPORT entry), the C-oracle rename, Tier 1 + Tier 3 parity cases, the real-libobs case, and the docs/features.md line. That state is green on Linux Tier 2 (OFF and ON), Fast Gate and Format, and the real-libobs case is 5/5 green there against 5/5 red on master.

Evidence for all of the above is in the PR body and in the #2920 progress comment.

@zackees
zackees force-pushed the fix/2920-grouped-encoder-divisor-start branch from 076a60a to bb9b70d Compare October 29, 2026 16:04
@zackees
zackees marked this pull request as ready for review October 29, 2026 17:15
@zackees
zackees added this pull request to the merge queue Oct 29, 2026
@zackees

zackees commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

Handed off to the coordinator.

State: ready, auto-merge on, and the PR is in the master merge queue (entry 3 when this was written). PR CI is fully green on this head — Fast Gate ⚡, Format 🔍 / checks, Rust Cross Lint, Rust Tests (macOS, Windows), Rust Tier 2 (macOS, Windows, Linux OFF/ON) — all pass, none skipping. There is no local-gate stamp on HEAD: the CI lanes ran in full, so nothing in this PR depends on one.

What is proven where:

  • Real libobs, Linux: divisor_encoders_start_on_the_group_frame is 5/5 red on 68771844d (divisor 2 and 3 encoders at 0 frames) and 5/5 green with the fix; ran in the obs-rust-linux-validate tree under xvfb-run.
  • C reference vs Rust: divisor_align_matches drives both implementations through the same sequence and compares the delivered timestamps; align_divisor_restarts_the_phase is the Tier 1 pin. soldr cargo test -p obs-media-io --lib divisor is 6/6.
  • Windows Tier 2 and macOS Tier 2 both compile and run the suites green.

Two things a rebase may hit:

  1. libobs/cmake/rust-exports.d/obs-media-io-video_io.txt — the new symbol is one line in an otherwise untouched fragment.
  2. rust/obs-media-io/src/video_output.rs + ffi/video_io.rs — a sibling port of video-io would collide here; the change is one field, one method, one shim.

Note on the branch: another checkout of this fleet force-pushed it back to its earlier reset-only state at 16:00Z while this work was in review. The reset-only version does not fix the issue (measured: the real-libobs case still sees 0 frames for the divisor encoders), so the head here is the phase fix; the comment above records that measurement. If a rebase lands on a head whose video-io change only clears frame_rate_divisor_counter, it is the superseded version.

@zackees
zackees removed this pull request from the merge queue due to a manual request Oct 29, 2026
@zackees

zackees commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

Coordinator: this PR is out of the queue — its own new test failed in the merge-group run, at the same head that passed PR CI.

Evidence — merge-group run 40919686083 (head df5cbc08, built on the queue's 2944 group), job Build 🧱 / Fast Gate ⚡:

FAIL [0.017s] (4051/5174) obs-media-io tests::video::video_io::align_divisor_restarts_the_phase
thread 'tests::video::video_io::align_divisor_restarts_the_phase' panicked at rust/obs-media-io/tests/video/video_io.rs:600:14:
assertion `left == right` failed
  left: 3
 right: 4

Line 600 is assert_eq!(video_output_get_total_frames(video), 4) — three of the four pushed frames were counted, so one push never reached the video thread's counter (the 2000 push is the one the divisor skips, so that is the frame whose accounting is in question).

Why this is not a re-queue-and-see: PR CI at this exact head bb9b70d6 is green (40908026658), so the same code passes and fails on different runs. That is either a race in the test's own handoff (push → wait_hits → video_output_get_total_frames after video_output_stop) or a real nondeterminism in the alignment path, and re-queueing it would just eject it again and hold 2948 and 2934 behind it.

Author: please reproduce locally before re-queueing, e.g.

cargo nextest run -p obs-media-io align_divisor_restarts_the_phase --test-threads 1

repeated ~20 times, and if it does not reproduce, harden the assertion's handoff (wait on the video thread's own frame counter rather than assuming video_output_stop has drained it) — a test that can read 3 or 4 on identical code is not evidence for #2920 either way.

The queue entry is removed so the PRs behind it can proceed; re-queue with gh pr merge 2926 --auto once the test is deterministic.

@zackees
zackees added this pull request to the merge queue Oct 29, 2026
Merged via the queue into master with commit 050eba4 Oct 29, 2026
39 checks passed
@zackees
zackees deleted the fix/2920-grouped-encoder-divisor-start branch October 29, 2026 18:39
@obs-rust-project-manager obs-rust-project-manager Bot moved this from In progress to Done in obs-rust Multiview Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

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

1 participant