Repository navigation
obs-core: Port the video frame pacing decisions (video_sleep) - #2739
Merged
Merged
Conversation
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
force-pushed
the
port/2738-video-pacing
branch
from
October 28, 2026 07:44
27de362 to
51a3933
Compare
vpacher
marked this pull request as ready for review
October 28, 2026 08:00
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.
Port of the frame pacing decisions of
video_sleep()inlibobs/obs-video.c.htorust/obs-core(video_pacing), next tovideo_output_step(#2165) andvideo_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.libobs: Move the video frame pacing decisions into obs-video-pacing.c(behaviour-neutral).test: Characterize the video frame pacing decisions(test_video_pacing_steps).obs-core: Add Tier 1 tests for the video pacing port(RED: 9 tests ontodo!()stubs).obs-core: Port the video frame pacing decisions(GREEN:obs-core::video_pacing, the shims, grouplibobs.ported-video-pacing).obs-core: Check the video pacing port against the C(oracle, Tier 3 parity,port_comparison).docs: Record the video pacing port.1. Move
Two functions on plain values move to
libobs/obs-video-pacing.c.h, declared in the newlibobs/obs-video-pacing.hwithout 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()keepsos_sleepto_ns(), and it callsos_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 thevframe_infodeque 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_stepscompiles 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:intcount limit:INT_MAX,INT_MAX + 1(converted toINT_MIN), 2^32 (count 0), 2^32 + 3. These are characterized, not endorsed;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 asint64_t, the count truncated toint, the frame time plus the interval times thatintasuint64_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.rust-hidden.txt,rust-exports.map,rust-unexports-macos.txt). The group's header needs onlyobs-video-pacing.handstring.h, so the oracle and the C test compile it on their own.uint64_texplicitly ininterval_ns * (uint64_t)step.count. The value is the same as the implicit conversion; the oracle's-Wsign-conversionneeds it.test_video_pacing_steps1:1 (5 cases) and 4 edge cases (proptests against a model of the C's expressions, the interval 0 difference).INT_MAX, 2^31,UINT_MAXand 2^32; the group over theuint32_tandsize_tedges; 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
test_video_pacing_steps: 10/10. Hybridlibobs: the two symbols are the Rust shims, not dynamically exported. C reference: the C functions, not exported.obs-tests-corevideo_pacing: 18/18 (9 Tier 1, 9 parity); the bench passes in the test profile.obs-tests-core2795 passed (18/18 forvideo_pacing);test_video_pacing_steps5/5 with ASan + UBSan and-Wpedantic -Wmissing-prototypes -Wstrict-prototypes; obs-util export lists 3/3;clippy -D warningsclean on obs-core, obs-data and obs-c-oracle.parity_step_full_rangewith 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_steps16.2 MB, 0.03 s. The tests generate only scalars, no generated size drives an allocation.windows-ci-x64, MSVC/WX): libobs and the tests build with no warning in the changed files;test_video_pacing_steps5/5,test_video_output_step_steps19/19,test_video_copy_plan_steps22/22,test_encoder_group_null1/1,test_encoder_lifecycle_steps12/12,test_encoder_audio_start_steps20/20;obs-tests-corevideo_pacingwith MSVC (the oracle compiled by MSVC): 18/18.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