Repository navigation
obs-source: Port the source video tick decisions - #2448
Merged
Merged
Conversation
Behaviour-neutral extraction before the Rust port. The per-frame decisions of obs_source_video_tick() and async_tick() in obs-source.c.h move to libobs/obs-source-video-tick.c.h, as seven functions on plain values declared in the new libobs/obs-source-video-tick.h without EXPORT (hidden, as -fvisibility=hidden keeps them): - obs_source_video_tick_transition(), _async() and _media(): which steps run for the source's type and output flags (the transition tick, the async tick, the media actions); - obs_source_video_tick_deferred_update(): a loaded defer_update_count above 0; - obs_source_video_tick_edge(): the show/hide and activate/deactivate edge of a loaded reference count against the flag it sets (NONE, ON or OFF); - obs_source_video_tick_calls_tick(): data and a callback; - obs_source_video_tick_async_route(): deinterlace the last frame or take the closest one. Split per step, so every read and call stays at its point and under its condition. The output flags are still read once for the async step and again, after async_tick(), for the media step. The three unlocked os_atomic_load_long() reads stay where they were: defer_update_count after process_media_actions(), show_refs before the show edge, and activate_refs only after the show edge has run (a show callback can change it). async_tick() keeps both deinterlacing_enabled() calls, the second one asking the route again for the previous frame's filter. obs_transition_tick(), process_media_actions(), obs_source_deferred_update(), gs_texrender_reset() and its null check, show_source(), hide_source(), activate_source(), deactivate_source(), the filter walks in reverse, the stores of showing and active, the plugin callback, the per-tick resets, async_mutex, the frame calls and set_async_texture_size() with their null checks all stay in obs-source.c.h. The header mirrors OBS_SOURCE_TYPE_TRANSITION, OBS_SOURCE_ASYNC and OBS_SOURCE_CONTROLLABLE_MEDIA, and obs-source.c.h checks them with _Static_assert. Checked in a scratch harness (not committed). It compiles origin/master's async_tick() and obs_source_video_tick() verbatim next to this branch's and the moved file, on a stand-in source with three filters whose every field read and write is logged with its value (a script rewrites each access into a logging accessor). Every call is logged in order with its arguments and the whole state: the atomic loads, the lock and unlock, each callback, the filter walks and the frame calls. The stubs answer from one seeded stream in call order and may change the state: a concurrent writer moves a count just before its load, and a callback moves the ref counts, the flags, the defer count, the deinterlace mode, the frames, the data or the filter count. Inputs cover every source type and values outside the enum, any output flags, counts at 0, +-1, +-2, the ends of long and 2^32, and NaN and infinite seconds. 52,000,000 random states: 20,000,000 each with GCC and Clang at -O2, 5,000,000 each at -O0, and 1,000,000 each under ASan and UBSan with no recovery (GCC -O0, Clang -O2). All 58 billion logged values match, as does the final state, after one normalization: the branch reads info.video_tick (set when the source is created) also when context.data is NULL, where master's && did not, so that one read is dropped. 12 deliberately broken moves all fail it: the activate refs loaded before the show edge, the output flags read once, the defer count at >= 0, the edge ignoring the flag, the flag read before the load, one deinterlacing_enabled() call for both uses, the callback without the data check, the media actions before the async tick, the deferred update before the media actions, showing stored before the callbacks, the texrender reset after the show edge, and the route inverted. The new file joins libobs.core-source with libobs.pch.h, which includes the new header; source-sets.json lists it. docs/rust-port/naming.md already maps libobs/obs-source*.c.h to rust/obs-source. Refs obs-rust#2443
test_source_video_tick_steps compiles libobs/obs-source-video-tick.c.h (ASan and UBSan) and calls only its functions through obs-source-video-tick.h. 12 cases: - the enum values and the header's mirrors of OBS_SOURCE_TYPE_TRANSITION, OBS_SOURCE_ASYNC and OBS_SOURCE_CONTROLLABLE_MEDIA; - the transition tick for the four types, values outside the enum and every type from -64 to 64; - the async tick and the media actions for every single flag bit, every bit but one, and mixed flags; - each type with each combination of the two flags and other bits: the three steps are independent; - the deferred update for counts from -100 to 100 and at the ends of long; - the show/hide and activate/deactivate edges in both directions and none, for every count, and a run of ticks with the caller's store; - the callback rule's truth table and both async routes; - a copy of obs_source_video_tick()'s and async_tick()'s order of steps on a stand-in source with a trace: every step, the filters walked in reverse, a hide and deactivate, a show callback that adds an activate reference (activated in the same tick, since the activate refs are loaded after the show edge), and no data (the edges still run, the callback does not). Characterized, not endorsed: - the type is not checked against the flags: a filter or scene flagged async gets the async tick, and a transition flagged controllable media gets all three steps; - a negative defer count is not due; - a negative reference count (one release too many) is not 0, so it shows or activates the source and keeps it so. It is in the native test graph, in name order, with the header as a watch input. No assert_in_range or fail_msg, so it builds with cmocka 1.x and 2.x. Refs obs-rust#2443
RED for the port of libobs/obs-source-video-tick.c.h to a new
video_tick module of rust/obs-source. The safe core is a stub
(todo!()): transition_tick(type), async_tick(flags),
media_actions(flags), deferred_update(count), edge(refs, was) -> Edge
{ None, On, Off }, calls_tick(has_data, has_callback) and
async_route(deinterlacing) -> AsyncRoute { Closest, Deinterlace },
with the C's discriminants, and TYPE_TRANSITION, ASYNC and
CONTROLLABLE_MEDIA. The counts are i64: the shim will widen the C's
long, 32 bits on Windows.
tests/source/video_tick.rs ports test_source_video_tick_steps 1:1: its
12 cases under the same names (steps_*), in the same order, with the
same values and expectations, the characterized-not-endorsed cases and
the copy of the tick's order of steps on a stand-in source included.
Edge cases (edge_*):
- the edge and the deferred update over 38 special counts (around 0,
the ends of int, unsigned int and long, and 2^32), both flags;
- the transition tick for every type from -70,000 to 70,000 and at the
ends of int;
- the async and media steps for every low 16-bit flag word under four
high words;
- proptests (20,000 cases each): the steps over any type and flags
against the rules written another way, the edge, the deferred
update, the callback rule and the route over any count and flags,
and runs of up to 63 ticks through a model of the caller (after each
tick the stored flag is refs != 0, and the edges alternate).
soldr cargo test -p obs-data --test obs-tests-core source::video_tick
fails 17 of 18 with "not yet implemented"; the 18th,
steps_enum_values, checks the discriminants and constants, which are
not stubs.
Refs obs-rust#2443
Ports libobs/obs-source-video-tick.c.h, the per-frame decisions of obs_source_video_tick() and async_tick() in obs-source.c, to the video_tick module of rust/obs-source. obs-source.c.h stays C around them: the three unlocked os_atomic_load_long() reads at their points (the activate refs only after the show edge has run), every call (obs_transition_tick(), async_tick(), process_media_actions(), obs_source_deferred_update(), gs_texrender_reset(), the show, hide, activate and deactivate calls), the filter walks in reverse, the stores of showing and active, the plugin callback, the per-tick resets, async_mutex and both deinterlacing_enabled() calls. The hybrid libobs drops the C file and links the Rust shims in its place. Safe core (src/video_tick.rs), seven const fns: - transition_tick(type): type == TYPE_TRANSITION; - async_tick(flags) and media_actions(flags): the flag's bit; - deferred_update(count): count > 0; - edge(refs, was): None when refs != 0 equals was, else On or Off; - calls_tick(has_data, has_callback): both; - async_route(deinterlacing): Deinterlace or Closest. The module denies clippy's arithmetic, indexing, as, unwrap, expect, panic, todo, unimplemented and unreachable lints, keeps no state and takes no lock. Shims (src/ffi/source_video_tick.rs): the seven C signatures by value. The counts are the C's long (c_long, 32 bits on Windows), widened by widen(), i64::from() with clippy's useless_conversion allowed (the same type on 64-bit Unix); every rule compares with 0, so the width changes nothing. The two enums go back as their values, 0 to 2. No pointer crosses, so there is no intentional difference from the C. C side: the file leaves libobs.core-source for its own group, libobs.ported-source-video-tick (LIBOBS_PORTED_SOURCES), whose PCH is only obs-source-video-tick.h (stdbool.h and stdint.h); libobs.pch.h keeps the header for obs-source.c. The functions are not EXPORT, so the shims are hidden: rust-hidden.txt, the local: list of rust-exports.map and rust-unexports-macos.txt. libobs-rust already links the crate's ffi. The exported symbol sets are unchanged and equal: 1766 on the C backend, the hybrid and libobs-c-reference; the seven functions are local in all three, and the hybrid's come from the Rust archive. ci/test_ported_sources.py checks the group on every host. test_source_video_tick_steps still compiles the C file into itself, unchanged. The new test_source_video_tick_steps_rust builds the same test source against libobs_rust-static instead, as test_source_media_admission_steps_rust does, and links: the test includes the header and obs.h and calls only the seven hidden functions. test_visibility_transition, test_preview_only_hide, test_studio_mode_swap, test_studio_mode_exit, test_canvas_destroy_scene_refs and test_item_transition_empty_source see the swap through the real libobs, and pass on the C backend, the hybrid and libobs-c-reference. soldr cargo test -p obs-data --test obs-tests-core source::video_tick passes 18 of 18. No user-visible change, so no docs/features.md line. Refs obs-rust#2443
Tier 3 for the video_tick port. oracle/source_video_tick.c compiles the unchanged libobs/obs-source-video-tick.c.h with its seven functions renamed to oracle_*, plus the header's values and the enum sizes (taken under the oracle_ tag, since the defines rename the enum tags that share a function's name) and sizeof(long). tests/source/video_tick_parity.rs gives each input to the oracle, to the shim the hybrid links and to the core, and compares each bool as the byte the C returns (it must be 0 or 1) and each enum as its value: - the header's values and enum sizes, and sizeof(long) against c_long; - the transition tick for every type from -100,000 to 100,000, every 65,521st int and the ends; - the async and media steps for every low 16-bit flag word under 16 high words, and every 65,521st uint32_t; - the deferred update and both edges for every count from -100,000 to 100,000 and 38 special counts (those that fit long), both flags; - the callback rule and the route over every input; - obs_source_video_tick()'s order of steps copied onto a stand-in source, run once over the oracle's functions and once over the shims: every type with every flag pair, count pairs around 0, both stored flags, filters, data, callback, deinterlacing and a show callback that adds an activate reference (131,072 ticks), and proptest runs of up to 31 ticks; the traces and stored flags must match; - proptests of every function over full-range inputs. The proptests run 20,000 cases each. Mutation check (not committed): 19 mutants, each run against Tier 1 (18 tests) and Tier 3 (8). | Mutant | Tier 1 | Tier 3 | | --- | --- | --- | | core: transition on >= 2 | 5 | 4 | | core: transition on the scene type | 4 | 3 | | core: async on the media bit | 4 | 4 | | core: async on ASYNC or VIDEO | 4 | 3 | | core: media on bit 12 | 5 | 4 | | core: deferred at >= 0 | 3 | 3 | | core: deferred at != 0 | 3 | 4 | | core: edge ignores the flag | 7 | 4 | | core: edge on and off swapped | 7 | 4 | | core: edge on refs > 0 | 5 | 4 | | core: callback on data or callback | 3 | 4 | | core: callback ignores data | 3 | 4 | | core: route inverted | 3 | 4 | | shim: edge values swapped | 0 | 4 | | shim: route values swapped | 0 | 4 | | shim: media asks the async step | 0 | 4 | | shim: transition for negative types | 0 | 4 | | shim: widen truncates to 32 bits | 0 | 4 | | shim: callback arguments swapped | 0 | 0 | 18 are killed; Tier 1 calls the core, so only Tier 3 sees the shims'. The survivor is equivalent (&& is symmetric). There is no intentional difference: the shims touch no memory. soldr cargo test -p obs-data --test obs-tests-core source::video_tick passes 26 of 26 (Tier 1 18, Tier 3 8). Refs obs-rust#2443
The rust/README.md rows for the video_tick port: the obs-source crate row names libobs/obs-source-video-tick.c.h, and its own row in the ported-units table says what moved and what stays C (the three unlocked atomic loads at their points, every call, the filter walks in reverse, the stores, the plugin callback, async_mutex and both deinterlacing_enabled() calls), the characterized behaviours, no intentional differences, the tests that see the swap and what Tier 3 covers. The obs-source Cargo description and the lib.rs crate docs name the new module. docs/rust-port/naming.md already maps libobs/obs-source*.c.h to rust/obs-source. The port_comparison bench gets a source_video_tick module, in the sorted mod and modules![...] lists: 1024 ticks over fixed sources (every type, the async and media flags on and off, counts moving between 0 and not 0, deinterlacing on and off), each asking the nine decisions of one tick in obs_source_video_tick()'s order, the Rust shims against the C oracle with a checksum both must match. On x86_64 Linux (GCC, release, not pinned, a loaded host; median per 1024 ticks): 11.90 us Rust and 11.80 us C (1.01x), about 0.1 ns a tick. No user-visible change, so no docs/features.md line. Fixes obs-rust#2443
Member
|
Coordinator review: behavior-preserving extraction. Each decision now lives in a pure function in obs-source-video-tick.c.h, and the call sites still read every field at the same point. That covers the media flags re-read after async_tick and the activate refs loaded after the show edge. The info.video_tick read without data is commented and harmless. The _Static_asserts tie the mirrored flags to the real ones. A new cmocka test was added and no existing C test changed. The C-vs-Rust parity test, the bench and the export maps are all in. It's an internal port, so no ledger line is needed. Queuing. |
This was referenced Oct 26, 2026
6 of 7 tasks
This was referenced Oct 26, 2026
5 of 7 tasks
This was referenced Oct 31, 2026
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.
Fixes #2443
What and why
Ports the per-frame decisions of
obs_source_video_tick()andasync_tick()(libobs/obs-source.c.h) torust/obs-source, as a new modulevideo_tick. Every source runs these once per rendered frame:OBS_SOURCE_TYPE_TRANSITION), the async tick (OBS_SOURCE_ASYNC) and the media actions (OBS_SOURCE_CONTROLLABLE_MEDIA);defer_update_countabove 0);showingoractiveflag, and which way;video_tickcallback is called (data and a callback);async_tick(): deinterlace the last frame or take the closest one, and filter the previous frame too.The plan's API was split per step. The plan sketched one
tick_steps(type, flags) -> { transition, async, media }. That would readinfo.output_flagsonce, beforeobs_transition_tick(), instead of once for the async step and again, afterasync_tick(), for the media step. So each step is its own function, asked at its own point (as #2153 did), and seven functions cross the C ABI:obs_source_video_tick_transition(),_async(),_media();obs_source_video_tick_deferred_update();obs_source_video_tick_edge():NONE,ONorOFF;obs_source_video_tick_calls_tick();obs_source_video_tick_async_route():CLOSESTorDEINTERLACE.async_tick()keeps bothdeinterlacing_enabled()calls and asks the route again with the second one, for the previous frame's filter.What stays C, at its point and under its condition:
os_atomic_load_long()reads:defer_update_countafter the media actions,show_refsbefore the show edge, andactivate_refsonly after the show edge has run (a show callback can change it);obs_transition_tick(),async_tick(),process_media_actions(),obs_source_deferred_update(),gs_texrender_reset()and its null check;show_source(),hide_source(),activate_source(),deactivate_source(), and the filter walks in reverse;showingandactive, the plugin callback, the per-tick resets;async_mutex, the frame calls andset_async_texture_size(), with their null checks.The header mirrors the type and the two flags, and
obs-source.c.hchecks them with_Static_assert. The counts are the C'slong(32 bits on Windows), widened toi64for the core. Every rule compares with 0, so the width changes nothing. No pointer crosses.Six commits:
Move (behaviour-neutral,
49c05aafd): the decisions go tolibobs/obs-source-video-tick.c.h, declared in the newobs-source-video-tick.hwithoutEXPORT, so they are hidden. The file joinslibobs.core-source.The move was checked against the original code. A scratch harness (not committed) compiles origin/master's
async_tick()andobs_source_video_tick()verbatim, next to this branch's and the moved file:longand 2^32, and NaN and infinite seconds.52,000,000 random states: 20,000,000 each with GCC and Clang at
-O2, 5,000,000 each at-O0, and 1,000,000 each under ASan and UBSan with no recovery (GCC-O0, Clang-O2). All 58 billion logged values match, as does the final state, after one normalization. The branch readsinfo.video_tick(set when the source is created) also whencontext.datais NULL, where master's&&did not, so that one read is dropped.12 deliberately broken moves were tried, and all 12 fail the harness:
>= 0;deinterlacing_enabled()call for both uses;showingstored before the callbacks;Characterization (
c4212531b):test_source_video_tick_steps, 12 cases. It compiles the moved file (ASan and UBSan) and calls only its functions. It pins:long;It is in the test graph, sorted, with the header as a watch input.
RED (
111e6d27f): Tier 1 againsttodo!(). It ports the 12 C cases 1:1 and adds 6 edge tests:GREEN (
aa571ced7): the safe core (sevenconst fns) and seven#[no_mangle]shims, in a newlibobs.ported-source-video-tickgroup whose PCH is only the new header.source-sets.json, the unity lists, the three export lists andci/test_ported_sources.py, each in sorted place.test_source_video_tick_steps_rust, which linkslibobs_rust-staticalone, astest_source_media_admission_steps_rustdoes.Tier 3 (
869c0cbc6): a C oracle of the unchanged file, the parity tests and a mutation check.Docs (
96bd83067): the README rows, the crate's description,lib.rsand aport_comparisonbench file in the sorted lists.docs/rust-port/naming.mdalready mapslibobs/obs-source*.c.htorust/obs-source. The port changes nothing a user can see, sodocs/features.mdhas no line.The core denies clippy's arithmetic, indexing,
as, unwrap, expect, panic, todo, unimplemented and unreachable lints. It keeps no state and takes no lock.Bench (x86_64 Linux, GCC, release, not pinned to a core, a loaded host; median per 1024 ticks of nine decisions each): Rust 11.90 µs, C 11.80 µs (1.01x), about 0.1 ns a tick.
RED → GREEN
111e6d27f(obs-source: Test the video_tick port; it was1c1a42008before the rebase ontocceb543c3).soldr cargo test -p obs-data --test obs-tests-core source::video_tick: 1 passed (the enum values and constants), 17 failed, each on atodo!().local_gate --lane lintwas clean, and--test-diff origin/masterran 4180 tests: 4163 passed, 6 skipped, and only those 17 failed.aa571ced7): Tier 1 18/18.test_source_video_tick_stepspasses 12/12 on the C backend, on hybridlibobsand onlibobs-c-reference, andtest_source_video_tick_steps_rustpasses 12/12.test_visibility_transition,test_preview_only_hide,test_studio_mode_swap,test_studio_mode_exit,test_canvas_destroy_scene_refsandtest_item_transition_empty_sourcepass on all three (run through ctest, every case run, none skipped).libobs-c-reference.869c0cbc6): 8/8. Each bool is compared as the C's byte and each enum as its value.sizeof(long).int.uint32_t.&&is symmetric).96bd83067:local_gate --lane lint,--test-diff origin/master(4188 passed, 6 skipped) and--lane crossall pass;soldr cargo clippy --workspace --target x86_64-pc-windows-msvc --all-targets --locked -- -D warningsis clean;soldr cargo test -p obs-c-oracle --locked --bench port_comparisonpasses.libobs,libobs-c-reference) on96bd83067: 100% passed on each: 518/518 (C backend), 546/546 (hybrid), 546/546 (libobs-c-reference), with no reruns needed. On every lane,test_v4l2_vivid_replugandtest_v4l2_vivid_stallskip themselves (they need root and the vivid module), and so doestest_pulse_default_follow(it needs a PulseAudio null-sink module it can load, which this headless host's private session lacks). Neither touches the source tick.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): built with scripting ON (PR CI builds no OBS app: rungh workflow run ci.yml --ref <branch>for the full builds) and cross-checked with clang and MSVC (not applicable)