Skip to content

obs-core: Port the video frame pacing decisions (video_sleep) - #2739

Merged
zackees merged 6 commits into
masterfrom
port/2738-video-pacing
Oct 28, 2026
Merged

zackees merged 6 commits into
masterfrom
port/2738-video-pacing

Conversation

@vpacher

@vpacher vpacher commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Port of the frame pacing decisions of video_sleep() in libobs/obs-video.c.h to rust/obs-core (video_pacing), next to video_output_step (#2165) and video_copy_plan (#2118).

Fixes #2738

State

All five steps of the plan in #2738 are on this branch. PR CI on 51a393363 (run 40651994209) is green on every lane it selected.

  1. libobs: Move the video frame pacing decisions into obs-video-pacing.c (behaviour-neutral).
  2. test: Characterize the video frame pacing decisions (test_video_pacing_steps).
  3. obs-core: Add Tier 1 tests for the video pacing port (RED: 9 tests on todo!() stubs).
  4. obs-core: Port the video frame pacing decisions (GREEN: obs-core::video_pacing, the shims, group libobs.ported-video-pacing).
  5. obs-core: Check the video pacing port against the C (oracle, Tier 3 parity, port_comparison).
  6. docs: Record the video pacing port.

1. Move

Two functions on plain values move to libobs/obs-video-pacing.c.h, declared in the new libobs/obs-video-pacing.h without EXPORT (hidden):

  • obs_video_pacing_step(frame_time, interval_ns, slept, now): the new frame time and the frame count. After a sleep that reached the next frame time: the next frame time and 1. After a late wake-up: the whole intervals since the frame time (at least 1) and the frame time that many intervals on.
  • obs_video_pacing_group_starts(num_encoders_started, num_encoders, start_timestamp): a ready encoder's group gets its start timestamp once every encoder in it has started and it has none yet.

video_sleep() keeps os_sleepto_ns(), and it calls os_gettime_ns() only after a late wake-up, as before. It also keeps the stats, the ready encoder groups with their locks, the store of the group start timestamp and the vframe_info deque pushes.

One detail stays as it is: video_sleep() gives a group the new frame time as its start timestamp, and gives the frame info the old one. The characterization does not change this; if it is a bug, it is a separate issue.

2. Characterization

test_video_pacing_steps compiles the moved C file into itself (the functions are hidden in libobs), with ASan and UBSan. It needs no display and no libobs core. Cases:

  • sleep reached the next frame time (the clock value is not used);
  • late wake-up: exactly 1 interval, just under 2, 2, 2.5, 10.9, one second at 25 fps; the new frame time is on the interval grid;
  • less than one interval, and a clock earlier than the frame time (also a difference of 2^63 or more): one interval;
  • large jumps near the int count limit: INT_MAX, INT_MAX + 1 (converted to INT_MIN), 2^32 (count 0), 2^32 + 3. These are characterized, not endorsed;
  • group start for each combination of "all started" and "start timestamp set".

3. to 5. Port, Tier 1, parity

  • obs_core::video_pacing: step() keeps the C's conversions as the supported compilers do them: the clock difference read as int64_t, the count truncated to int, the frame time plus the interval times that int as uint64_t, wrapping. One intentional difference, unreachable: an interval of 0 after a late wake-up gives a count of 0 and the frame time unchanged (the C divides by zero; libobs computes the interval from the frame rate). group_starts() compares as the C promotes.
  • Both shims stay hidden like the C (rust-hidden.txt, rust-exports.map, rust-unexports-macos.txt). The group's header needs only obs-video-pacing.h and string.h, so the oracle and the C test compile it on their own.
  • The move commit casts the count to uint64_t explicitly in interval_ns * (uint64_t)step.count. The value is the same as the implicit conversion; the oracle's -Wsign-conversion needs it.
  • Tier 1: test_video_pacing_steps 1:1 (5 cases) and 4 edge cases (proptests against a model of the C's expressions, the interval 0 difference).
  • Tier 3: shim, C and core on every call, exactly: the layout; exhaustive edges of frame times, intervals and clock values; every whole multiple of a 25 and a 30 fps interval up to 20,000 frames late; the quotient around INT_MAX, 2^31, UINT_MAX and 2^32; the group over the uint32_t and size_t edges; full-range proptests. Deliberate-break check (by hand): a saturating count fails 3 parity tests, a missing clamp fails 4.
  • port_comparison (video_pacing/frame/1024, 60 fps, some late frames): macOS arm64 Rust 3.64 us, C 3.67 us.

Verification

  • Linux x86_64 (Debian 13, GCC, RelWithDebInfo): full build with no failed targets and no warnings in the changed files. ctest of the tests below plus test_video_pacing_steps: 10/10. Hybrid libobs: the two symbols are the Rust shims, not dynamically exported. C reference: the C functions, not exported. obs-tests-core video_pacing: 18/18 (9 Tier 1, 9 parity); the bench passes in the test profile.
  • macOS 26.7.1 arm64: obs-tests-core 2795 passed (18/18 for video_pacing); test_video_pacing_steps 5/5 with ASan + UBSan and -Wpedantic -Wmissing-prototypes -Wstrict-prototypes; obs-util export lists 3/3; clippy -D warnings clean on obs-core, obs-data and obs-c-oracle.
  • Memory and time of the new tests (Linux x86_64, debug, each test alone, 20 runs; peak RSS of the test process): the largest is parity_step_full_range with 13.0 MB and 0.20 s (the empty test exe is about 7.5 MB); all 18 together over 30 runs, max 15.9 MB and 0.27 s; test_video_pacing_steps 16.2 MB, 0.03 s. The tests generate only scalars, no generated size drives an allocation.
  • Windows 11 LTSC (VM, CI preset windows-ci-x64, MSVC /WX): libobs and the tests build with no warning in the changed files; test_video_pacing_steps 5/5, test_video_output_step_steps 19/19, test_video_copy_plan_steps 22/22, test_encoder_group_null 1/1, test_encoder_lifecycle_steps 12/12, test_encoder_audio_start_steps 20/20; obs-tests-core video_pacing with MSVC (the oracle compiled by MSVC): 18/18.
  • clang-format 22.1.3, gersemi, cargo fmt --all, ci/test_test_graph_sorted.py, ci/test_ported_sources.py, ci/test_naming.py: clean.

These real-libobs tests stay green: test_video_output_step_steps, test_video_copy_plan_steps, test_video_output_formats, test_raw_output_video_reset, test_encoding_lag_rescale, test_encoder_group_null, test_encoder_lifecycle_steps, test_encoder_audio_start_steps, test_media_restart_stale_frame.

🤖 Generated with Claude Code

vpacher and others added 6 commits October 28, 2026 08:24
Behaviour-neutral extraction before the Rust port (#2738). The decisions
of video_sleep() in obs-video.c.h move to libobs/obs-video-pacing.c.h, as
two functions on plain values declared in the new libobs/obs-video-pacing.h
without EXPORT (hidden, as -fvisibility=hidden keeps them):

- obs_video_pacing_step(frame_time, interval_ns, slept, now): the new
  frame time and the frame count. When os_sleepto_ns() reached the next
  frame time, the next frame time and 1. Otherwise the whole intervals
  since frame_time (at least 1, a negative difference counts as one
  interval) and the frame time that many intervals on;
- obs_video_pacing_group_starts(num_encoders_started, num_encoders,
  start_timestamp): a ready encoder's group gets its start timestamp once
  every encoder in it has started and it has none yet.

video_sleep() keeps os_sleepto_ns(), and calls os_gettime_ns() only after
a late wake-up, as before (it passes 0 otherwise, which the step does not
use). It keeps obs_video_stats_add_frames(count, count - 1), the ready
encoder groups with their weak references and both mutexes, the group's
start timestamp store (still the new frame time, while the frame info
gets the old one), and the vframe_info pushes into the mixes' deques.

Refs #2738

Co-Authored-By: Claude Opus 5.5 <[email protected]>
test_video_pacing_steps compiles libobs/obs-video-pacing.c into itself
(the functions are hidden in libobs) and pins the current C behaviour of
the two decisions moved out of video_sleep():

- obs_video_pacing_step(): after a sleep that reached the next frame time,
  one frame and the next frame time, whatever the clock reads; after a
  late wake-up, the whole intervals since the frame time (exactly one
  interval, just under two, two, 2.5, 10.9, one second at 25 fps) and the
  frame time on the interval grid, not the wake-up time; less than one
  interval, or a clock earlier than the frame time (also a difference of
  2^63 and more, negative as int64), as one interval; and large jumps near
  the int count: INT_MAX intervals, INT_MAX + 1 (converted to INT_MIN, the
  frame time wraps), 2^32 (count 0) and 2^32 + 3 (count 3). The large-jump
  cases are characterized, not endorsed: with a 25 fps interval INT_MAX
  frames are about 2.7 years.
- obs_video_pacing_group_starts(): a group starts once every encoder in it
  has started and it has no start timestamp; it keeps a set one. More
  started than encoders, and an empty group, are characterized.

No display and no libobs core, so it runs on every platform, with ASan and
UBSan like test_video_output_step_steps.

Refs #2738

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RED for #2738. obs_core::video_pacing has its API and docs, with todo!()
bodies: step() (the frame time step of video_sleep()) and group_starts()
(whether a ready encoder's group gets its start timestamp).

tests/obs_core/video_pacing.rs:
- steps_*: test_video_pacing_steps.c 1:1 (5 cases, same names, values
  and expectations, the characterized large jumps included);
- edge_*: proptests over full-range frame times, intervals (not 0),
  sleep results and clock values against a model of the C's expressions,
  realistic intervals and late wake-ups up to a day (the new frame time
  stays on the interval grid), any group counts; and the intentional
  difference for an interval of 0 (the C divides by zero; unreachable).

All 9 fail on the stubs (obs-tests-core, video_pacing).

Refs #2738

Co-Authored-By: Claude Opus 5.5 <[email protected]>
GREEN for #2738: obs_core::video_pacing replaces libobs/obs-video-pacing.c
in the hybrid libobs.

- step(): after a sleep that reached the next frame time, the next frame
  time and 1; after a late wake-up, the unsigned clock difference read as
  int64 (memcpy in the C, from_ne_bytes here), clamped to at least one
  interval, the whole intervals truncated to int as the C's (int) cast
  does, and the frame time plus the interval times that int as uint64,
  wrapping. Intentional difference, unreachable: an interval of 0 after a
  late wake-up gives a count of 0 and the frame time unchanged (the C
  divides by zero; libobs computes the interval from the frame rate).
- group_starts(): all encoders started (uint32 against size_t, as the C
  promotes) and no start timestamp yet.

C ABI: ffi::video_pacing, a #[repr(C)] mirror of struct
obs_video_pacing_step and the two shims, hidden like the C
(rust-hidden.txt, rust-exports.map, rust-unexports-macos.txt). libobs:
obs-video-pacing.c moves from libobs.core-output to its own group
libobs.ported-video-pacing with a header that needs only
obs-video-pacing.h and string.h, so the C oracle and the C test compile it
on their own; ci/test_ported_sources.py lists it.

Tier 1 (obs-tests-core, video_pacing): 9/9. All obs-tests-core: 2795
passed. obs-util export lists: 3/3. clippy -D warnings clean.

Refs #2738

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tier 2 layout and Tier 3 parity for #2738.

- obs-c-oracle: oracle/video_pacing.c builds the original
  libobs/obs-video-pacing.c.h, unmodified, with its two functions renamed
  oracle_* (the struct tag shares the step function's name, so the layout
  table names the renamed tag), plus the layout of struct
  obs_video_pacing_step as the C sees it; src/video_pacing.rs binds them.
- tests/obs_core/video_pacing_parity.rs runs every call on the shim, the C
  and the safe core and compares exactly: the layout; exhaustive over the
  sleep result, frame times and clock values at 0, 1, a frame time and the
  u64/i64 edges against intervals at 1, common frame rates and the u64
  edges; every whole multiple of a 25 and a 30 fps interval up to 20,000
  frames late; the quotient around INT_MAX, 2^31, UINT_MAX and 2^32 (the
  C's (int) truncation); the group start over the uint32_t and size_t
  edges; proptests over the full ranges. An interval of 0 after a late
  wake-up is not given to the C (it divides by zero); there the shim is
  compared with the core (count 0), the one intentional difference.
- Deliberate-break check (run by hand, not committed): a count that
  saturates at INT_MAX instead of truncating fails 3 of the 9 parity tests;
  dropping the clamp to one interval fails 4.
- port_comparison/video_pacing.rs: 1024 frames at 60 fps (on time, every
  17th 1 to 3 intervals late, a group waiting every 64th), both sides fold
  the answers into one checksum. macOS arm64 (M1 Max): Rust 3.64 us, C
  3.67 us per 1024 frames.

obs-tests-core video_pacing: 18/18 (9 Tier 1, 9 parity). clippy -D
warnings clean on obs-c-oracle, obs-core and obs-data.

Refs #2738

Co-Authored-By: Claude Opus 5.5 <[email protected]>
rust/README.md: obs-core's description and the swap-table row for
libobs/obs-video-pacing.c.h (obs-core::video_pacing, group
libobs.ported-video-pacing, the two hidden functions, the C conversions
kept, and the intentional difference for an interval of 0).
docs/rust-port/naming.md: obs-video-pacing.c.h among the units extracted
from libobs/obs-video.c.h that live in rust/obs-core.

Refs #2738

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vpacher
vpacher force-pushed the port/2738-video-pacing branch from 27de362 to 51a3933 Compare October 28, 2026 07:44
@vpacher
vpacher marked this pull request as ready for review October 28, 2026 08:00
@vpacher
vpacher requested a review from zackees as a code owner October 28, 2026 08:00
@zackees
zackees added this pull request to the merge queue Oct 28, 2026
Merged via the queue into master with commit 5ddade3 Oct 28, 2026
36 checks passed
@zackees
zackees deleted the port/2738-video-pacing branch October 28, 2026 08:19
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.

rust: Port the video frame pacing step (video_sleep) in obs-video.c to obs-core

2 participants