Skip to content

obs-scene: Port the scene audio render window decisions - #2191

Merged
zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/scene-audio-window
Oct 24, 2026
Merged

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/scene-audio-window

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator

Fixes #2178

What and why

Ports the audio window decisions of scene_audio_render() (libobs/obs-scene.c.h) to rust/obs-scene, as a new module audio_window, next to audio_actions. These decisions choose the source each scene item's audio is read from (the show or hide transition while it plays, or the source), the tick's earliest timestamp, and the frames of the tick each item is mixed on. A wrong one cuts or doubles audio on show and hide transitions (the #388 and group-hide classes) or shifts an item's audio within the tick. Our earlier fix in this area, the group hide transition audio work, keeps all its rules: the pick and the "hidden with no volume buffer and no hide transition" skip move unchanged, and test_group_hide_transition_audio passes on every lane.

Six commits:

  1. Move (behaviour-neutral) into libobs/obs-scene-audio-window.c.h. It has seven functions, declared in the new obs-scene-audio-window.h without EXPORT (hidden):

    • obs_scene_audio_window_pick(): the source read, an enum the scene maps back to the item's pointer;
    • _counts() and _earliest(): which items take part in the tick's timestamp (not pending, and visible or hiding), and the fold to the earliest non-zero one;
    • _silent(): there is no such timestamp, so the scene only processes every item's pending audio actions and reports no audio;
    • _frames(): skip a pending source, one with no timestamp, or one that starts on frame AUDIO_OUTPUT_FRAMES or later. Otherwise pos = ns_to_audio_frames(rate, source_ts - timestamp) (the same uint64_t arithmetic) and count = AUDIO_OUTPUT_FRAMES - pos. The result is a struct obs_scene_audio_window returned by value;
    • _mixes(): skip a hidden item that has no volume buffer and no hide transition;
    • _adds(): read duplicated audio but do not add it.

    obs-scene.c.h keeps audio_lock, apply_scene_item_volume() and process_all_audio_actions() (it passes their result), obs_source_audio_pending(), obs_source_get_audio_timestamp(), obs_source_get_audio_mix(), the two mix loops, and every item and source field read.

    • Why seven calls and not one plan per item. The C reads between its decisions:

      • apply_scene_item_volume() runs before the pending check;
      • the timestamp is read only for a source that is not pending;
      • visible and the hide transition are read only after the window, and only without a volume buffer;
      • audio_is_duplicated is read only after obs_source_get_audio_mix().

      So each decision is made where the C made it. The scene asks for each value only where the old short circuits asked for it, and passes false or 0 for a value they did not ask for. No decision looks at those values. The plan sketched item_window() -> Skip | Mix; it became frames(), mixes() and adds() for this reason. min_timestamp() stays in the core as a fold of the step that the C calls once per item.

    • Read order and count unchanged. item->visible is still read a second time when the show transition is not picked, as the old else if did.

    • Checked against the unchanged function. A scratch harness (not committed) extracts origin/master's scene_audio_render() verbatim and the moved one, together with obs-scene-audio-window.c.h. Every item and source field read in both is rewritten to a logging accessor:

      • visible, show_transition, hide_transition, source, next, first_item, audio_is_duplicated, and transitioning_audio/_video inside scene_transition_active();
      • visible, the transition flags and audio_is_duplicated can flip from one read to the next, so a read moved or added changes what follows.

      Stubs log every lock and unlock and every apply_scene_item_volume() call: its arguments, its return, and a hash of the gain buffer it fills. Stubs also log every process_all_audio_actions(), obs_source_audio_pending(), obs_source_get_audio_timestamp() and obs_source_get_audio_mix() call, and every mix loop call (output and input position, pos, count, and the gain buffer's hash). The return value, *ts_out and every output sample are compared too.

      The random scenes have 0 to 7 items over 1 to 8 shared sources, with and without transitions. Timestamps sit on and around frames 1023/1024/1025, before the minimum, near 0 and near UINT64_MAX. The 22 rates go from 0 up to SIZE_MAX, plus random ones. Mixers are random masks and channels go from 0 to 8.

      The two traces are byte-identical under GCC -O2 for 2,000,000 scenes and clang -O0 for 1,000,000: 89.6M and 44.7M field reads, 4.2M and 2.1M buffered mix calls. A planted extra hide-transition read, a missing second visible read and a > for >= are each caught within 120 scenes. Master's copy is unchanged at the final base, and the harness was rerun on the final head.

    The file stays in libobs.core-source for this commit. Exports are unchanged (1766).

  2. Characterization: test_scene_audio_window_steps, 14 cases, compiling the moved file (ASan and UBSan). It pins:

    • every pick for the 8 combinations of visibility and running show and hide transitions;
    • the counts rule for all 8 combinations;
    • the minimum over item sets:
      • no items, one item of each kind, and three items in all 6 orders;
      • zeros around a timestamp, no timestamps, all pending, and pending and hidden items left out;
      • equal timestamps, 1 and UINT64_MAX;
      • the fold step, and silent();
    • the item window at pos = AUDIO_OUTPUT_FRAMES - 1, at it and + 1, on the first and last ns of each frame, at 48 and 44.1 kHz. It also pins the 1023/1024 edges at 8, 16, 22.05, 32, 88.2, 96, 192 and 384 kHz;
    • every skip rule;
    • a timestamp before the minimum, and the u64 edges (see below).

    It is in the test graph in name order, with libm except on MSVC. It reports failures with fprintf + fail(), with no fail_msg() and no assert_in_range. It builds with -Wall -Wextra -Werror and passes 14/14 against cmocka 1.1.7 and 2.0.2 with both GCC and clang.

  3. RED: Tier 1 in the crate, against todo!().

  4. GREEN: the safe core and the #[no_mangle] shims, in a new libobs.ported-scene-audio-window group. Its PCH has only media-io/audio-io.h and the new header.

    • source-sets.json, the unity lists, the three export lists and ci/test_ported_sources.py are updated, in sorted places.
    • The frame uses audio_actions' util_mul_div64() (now pub(crate)), and the subtraction is wrapping_sub.
    • A _rust variant of the characterization is added.
  5. Tier 3: a C oracle and parity tests, with the mutation check.

  6. Docs: the README rows, the crate's description and lib.rs, and a port_comparison bench file in the sorted modules![...] list.

Kept, characterized, not endorsed:

  • An item left out of the minimum can start before it. This is a hidden item with no hide transition, or a source that was pending on the first pass. source_ts - timestamp then wraps in uint64_t, normally to a frame far past the tick, so the item is skipped. Across the end of the u64 range the wrap lands back in the tick and the item is mixed: 1 is 2 ns after UINT64_MAX - 1. Timestamps are monotonic-clock ns, so the second case does not happen in practice.
  • A rate of 0 puts every item on frame 0.
  • At rates of 1 GHz and up, util_mul_div64()'s wrapping formula wraps the frame: 2 s at 2^63 Hz is frame 0. The characterization pins this only where the formula wraps (not MSVC x64).

The port keeps all three.

Intentional difference: on MSVC x64, util_mul_div64() is exact 128-bit math. It faults in _udiv128 when the frame does not fit in 64 bits, which needs a rate above 1 GHz. The core saturates there, so the item is skipped (as audio_actions already does). Tier 3 does not give those rates on MSVC.

  • How values cross. Bools as bools, uint64_t as u64, size_t as usize, the child as the C's enum value (int), and the window as struct obs_scene_audio_window by value. No scene, item or source pointer crosses.
  • The _rust variant. test_scene_audio_window_steps_rust builds the unchanged test source against libobs_rust-static alone, with libm except on MSVC. The test includes only the header and media-io/audio-io.h, so it needs neither SIMDe nor libobs's include directories. It links and passes 14/14 against the shims.
  • Exported symbols: 1766 on the C backend, the hybrid and libobs-c-reference, identical sets. The seven functions are local (t) in all three. In the hybrid they come from the Rust archive: the C group is compiled only into the C backend and libobs-c-reference.
  • Performance (port_comparison, x86_64 Linux, GCC, pinned to one core, load average near 40, so noisy; medians, two runs): one audio tick of a 16-item scene at 48 kHz takes 185 / 332 ns in Rust and 215 / 508 ns in C.
  • No user-visible change, so no docs/features.md line.

Overlap check: none of the 14 open PRs touches obs-scene.c.h, scene_audio_render() or any obs-scene file. Five of them (#2171, #2175, #2181, #2184, #2188) add their own entries to test-graph.json, at other places in name order.

RED → GREEN

  • RED commit: 532028579 (recorded as 50c040b1c, the same commit before the rebase). soldr cargo test -p obs-data --test obs-tests-core scene::audio_window fails 21 of 22, each with "not yet implemented". These are the 13 ported steps_* cases that call the core and the 8 edge_* cases, four of them proptests. steps_child_values calls no stub and passes.
  • GREEN: 4077ff9ac. Tier 1 passes 22/22.
  • Lane runs were made on b565d2b3c, the same six commits on a8316f66d. The final rebase onto 6c98110db brought only frontend and test changes outside these files; after it, the quick checks and --test-diff were rerun on the head, 9ef01b56c.
    • Each run used a private Xvfb -noreset on a display checked to be free, dbus-run-session, a fresh HOME, ulimit -c 0 and TMPDIR unset.
    • libobs-c-reference was built with ninja libobs-c-reference right before the c-ref run; it is newer than the head commit. The c-ref lane puts libobs-c-reference/ first on LD_LIBRARY_PATH, checked with ldd.

The required set is every test whose source mentions both scene and audio, plus test_group_hide_transition_audio, test_audio_capture_mix_race, test_scene_blend, test_visibility_transition and test_preview_only_hide: 37 tests, plus 6 _rust variants in the hybrid tree.

Test C backend hybrid libobs-c-reference
required set (37 tests; 43 with the _rust variants), including test_scene_audio_actions, test_scene_audio_action_steps, test_audio_capture_mix_race, test_scene_blend, test_visibility_transition, test_preview_only_hide, test_source_volume_mix, test_transition_audio_rescale 37/37 43/43 43/43
test_scene_audio_window_steps 14/14 14/14 14/14
test_scene_audio_window_steps_rust (not built: needs the Rust archive) 14/14 14/14
test_group_hide_transition_audio (#838 flake), 5 runs each 5/5 passed 5/5 passed 5/5 passed
full ctest (-j4) 458/458, 0 skipped 474/474, 0 skipped (not run: the two lanes)

ctest skipped nothing, and no cmocka case skipped in any of these runs. The flake runs were at load averages of 4 to 16. All 15 passed with no reruns.

  • Tier 3, 318f3184a (scene::audio_window_parity, 8 tests): the layout, the flags exhaustively, the special-value grids, every frame start, and proptests of 20,000 cases each, including scene_audio_render()'s decisions in its order over random scenes. Tier 1 and Tier 3 passed 5 runs in a row (30/30). The two regression files keep the seeds the mutants failed on.
  • Mutation check (once, not committed): 34 mutants. The table gives failures per mutant (Tier 1 has 22 tests, Tier 3 has 8).
Mutant Tier 1 Tier 3
pick: show of a hidden item / hide of a visible item / Show and Hide swapped 2 / 2 / 1 1 / 1 / 2
counts: pending ignored / hiding ignored 3 / 2 1 / 2
earliest: 0 counts / first not taken / latest kept 4 / 4 / 4 3 / 3 / 3
silent: inverted 2 3
frames: pending mixed / no timestamp mixed 3 / 0 2 / 3
frames: frame 1024 mixed / frame 1023 skipped 6 / 7 3 / 2
frames: saturating subtraction / subtraction reversed 2 / 7 4 / 4
frames: count the whole tick / count one short 7 / 7 4 / 4
frame: exact 128-bit (no wrap) / rounded up / divided by 1e9 + 1 3 / 9 / 7 1 / 4 / 2
mixes: volume buffer ignored / hiding ignored 2 / 1 2 / 2
adds: duplicated added 1 2
shim: Show and Hide values swapped / pick flags swapped / counts arguments swapped 0 / 0 / 0 2 / 2 / 2
shim: frames timestamps swapped / skipped window with pos 1 / rate through u32 / adds inverted 0 / 0 / 0 / 0 4 / 4 / 1 / 2
equivalent: pick tests hide first / earliest <= / shim earliest arguments swapped / shim mixes arguments swapped 0 / 0 / 0 / 0 0 / 0 / 0 / 0

Tier 3 kills 30 mutants. The four that survive are equivalent:

  • the pick's two tests exclude each other (one needs the item visible, the other hidden);
  • an equal timestamp keeps the same value;
  • earliest() is symmetric;
  • the mix rule is an OR.

Tier 1 does not reach the shims; test_scene_audio_window_steps_rust and Tier 3 do. "No timestamp mixed" passes Tier 1 because a timestamp of 0 below any tick wraps past the tick anyway; Tier 3 kills it with ticks near 0.

  • Real app: no UI, protocol or logging change.

Other runs:

  • uv run ci/local_gate.py --test-diff origin/master: ok on b565d2b3c and again on the head 9ef01b56c (3520 of 3520 passed; the 4 skipped are tests ignored by default). --lane lint: ok. --lane cross: ok.
  • Windows clippy. --lane cross runs clippy --workspace --exclude obs-c-oracle --lib --bins --target x86_64-pc-windows-msvc -- -D warnings. It has -D warnings but covers neither the test crates nor obs-c-oracle; the test binaries are only compiled (cargo test --no-run), not linted. That is how the Windows-only useless_conversion on obs-source: Port the source size query routing #2171 got through. So soldr cargo clippy --workspace --target x86_64-pc-windows-msvc --all-targets --locked -- -D warnings was run separately: clean. The port has no width-dependent conversion that is a no-op on one target: usize↔u64 uses try_from between distinct types, and nothing converts c_long.
  • soldr cargo test -p obs-c-oracle --locked --bench port_comparison: passes, all 181 bench pairs. The new module allocates nothing, so the bmem leak check (obs-c-oracle: Free what port_comparison's fixtures allocate #2159) holds.
  • ci/test_build_groups.py, ci/test_ported_sources.py, ci/test_oracle_standins.py, ci/test_sorted_shared_lists.py, ci/test_test_graph_sorted.py, ci/test_cmocka_rust_static_guard.py, ci/test_cmocka_windows_diagnostics.py, ci/test_cmocka_includes.py and ci/test_naming.py: OK on the head. clang-format 22.1.3 and gersemi 0.25.0 are clean.
  • After each rebase, source-sets.json, test-graph.json, the three export lists, ci/test_ported_sources.py, the port_comparison lists, libobs.pch.h and the oracle's lib.rs were diffed by hand against origin/master. Each one is master's list plus this PR's lines. There was one conflict, in the rust/README.md crate table (obs-source: Port the deinterlace draw decisions #2153 changed the obs-source/ row next to ours): master's row was kept and ours updated.

Observed, not changed: audio_actions' util_mul_div64() model treats clang-cl like MSVC (exact 128-bit, saturating). clang-cl's C path truncates the 128-bit quotient instead, so for a rate above 1 GHz the two differ there. libobs never gets there, and Windows CI builds with MSVC.

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (N/A for docs-only; a focused CI fix may push RED and GREEN together)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite (the gate lanes passed locally; the head is not pushed or stamped yet)
  • Warning or flag changes (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag changes)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

@rdoupe
rdoupe requested a review from zackees as a code owner October 24, 2026 17:26
@zackees
zackees enabled auto-merge October 24, 2026 17:42
@zackees
zackees added this pull request to the merge queue Oct 24, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Oct 24, 2026
@zackees
zackees enabled auto-merge October 24, 2026 18:11
@zackees
zackees force-pushed the port/scene-audio-window branch from 9ef01b5 to c54de61 Compare October 24, 2026 18:13
@zackees

zackees commented Oct 24, 2026

Copy link
Copy Markdown
Member

Coordinator: rebased onto master. The only conflict was the obs-scene/ and obs-source/ rows in rust/README.md, next to each other, each changed by one side. I kept both changes. cargo check --workspace --tests --benches passes. Auto-merge is on.

@zackees
zackees added this pull request to the merge queue Oct 24, 2026
@zackees
zackees force-pushed the port/scene-audio-window branch from c54de61 to 6f5b792 Compare October 24, 2026 18:42
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Oct 24, 2026
@zackees
zackees enabled auto-merge October 24, 2026 19:02
@zackees zackees closed this Oct 24, 2026
auto-merge was automatically disabled October 24, 2026 19:12

Pull request was closed

@zackees zackees reopened this Oct 24, 2026
Behaviour-neutral extraction before the Rust port. The decisions of
scene_audio_render() in obs-scene.c.h move to
libobs/obs-scene-audio-window.c.h, declared in the new
libobs/obs-scene-audio-window.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- obs_scene_audio_window_pick(): the source an item's audio is read
  from, the show transition while it plays for a visible item, the hide
  transition for a hidden one, else the source (an enum the scene maps
  back to the item's pointer);
- obs_scene_audio_window_counts() and obs_scene_audio_window_earliest():
  the items whose timestamp takes part in the tick's (not pending, and
  visible or hiding), and the fold to the earliest non-zero one;
- obs_scene_audio_window_silent(): no such timestamp, so the scene only
  processes every item's pending audio actions and reports no audio;
- obs_scene_audio_window_frames(): skip a pending source, one with no
  timestamp and one starting on frame AUDIO_OUTPUT_FRAMES or later;
  otherwise pos = ns_to_audio_frames(rate, source_ts - timestamp), the
  same uint64_t arithmetic, and count = AUDIO_OUTPUT_FRAMES - pos;
- obs_scene_audio_window_mixes(): skip a hidden item with no volume
  buffer and no hide transition;
- obs_scene_audio_window_adds(): skip adding duplicated audio.

obs-scene.c.h keeps audio_lock, apply_scene_item_volume() and
process_all_audio_actions() (it passes the volume result),
obs_source_audio_pending(), obs_source_get_audio_timestamp(),
obs_source_get_audio_mix() and the two mix loops, and reads every item
and source field itself.

Why several calls and not one plan per item: the C reads between its
decisions. apply_scene_item_volume() runs before the pending check,
obs_source_get_audio_timestamp() only for a source that is not pending,
item->visible and the hide transition only after the window and only
without a volume buffer, and audio_is_duplicated only after
obs_source_get_audio_mix(). One plan would have to read them all up
front. So each decision is made where the C made it, and the scene asks
each value only where the old short circuits asked it, passing false or
0 for one they did not; no decision looks at those. The read order and
count of every field are unchanged: item->visible is still read a second
time when the show transition is not picked, as the old else-if did.

The new file joins libobs.core-source with libobs.pch.h, which includes
the new header. The exported symbol sets are unchanged (1766).

Refs obs-rust#2178
test_scene_audio_window_steps compiles libobs/obs-scene-audio-window.c.h
(ASan and UBSan) and calls only its hidden functions, so it runs on
every platform. 14 cases:

- the source read for all 8 combinations of visibility and running show
  and hide transitions (a show transition of a hidden item and a hide
  transition of a visible one are not read); the enum values and size;
  AUDIO_OUTPUT_FRAMES is 1024;
- which items count for the tick's timestamp, all 8 combinations of
  pending, visible and hiding;
- the earliest timestamp over item sets: none, one (visible, hiding,
  hidden, pending, no timestamp), three in all 6 orders, zeros around a
  timestamp, no timestamps, all pending, pending and hidden items left
  out of the minimum, equal timestamps, 1 and UINT64_MAX; the fold step
  on its own; no audio exactly when the earliest is 0;
- the item window at pos AUDIO_OUTPUT_FRAMES - 1, at it and + 1, on the
  first and last ns of each frame, at 48 kHz and 44.1 kHz, and at the
  frame 1023/1024 edges of 8, 16, 22.05, 32, 88.2, 96, 192 and 384 kHz;
- the skips: pending (whatever the timestamp), no timestamp, from frame
  1024 on, a hidden item with no volume buffer and no hide transition
  (all 8 combinations), duplicated audio;
- a timestamp before the minimum (an item left out of it): the
  uint64_t subtraction wraps to a frame far past the tick and the item
  is skipped, at every rate; across the end of uint64_t it wraps back
  into the tick and is mixed (characterized, not endorsed);
- the uint64_t edges at both ends, rate 0 (every item on frame 0) and
  rate 1 (characterized, not endorsed); and, where util_mul_div64() is
  the wrapping formula (everywhere but MSVC x64, where it faults
  instead), rates of 2^62, 2^63 and SIZE_MAX, where the frame wraps
  (2 s at 2^63 Hz is frame 0).

It is in the native test graph in name order, with libm except on
MSVC, in the core suite, so no pinned count changes. It reports
failures with fprintf(stderr, ...) and fail(), not fail_msg(), and uses
no assert_in_range. It passes 14/14 on the C backend. The real-libobs
scene audio tests are unchanged.

Refs obs-rust#2178
Failing tests for the port of libobs/obs-scene-audio-window.c.h to a
new audio_window module of rust/obs-scene. The safe core is a stub
(todo!()), one function per hidden C function: pick() giving a Child
(Source, Show, Hide); counts(), earliest() and silent(); frames()
giving an Option<Frames> (pos and count); mixes() and adds(); plus
ns_to_audio_frames(), the frame of a distance as the C computes it on
the target, and min_timestamp(), earliest() folded over a scene.

The plan sketched one item_window() returning Skip or Mix; it is three
steps (frames(), mixes(), adds()) because the C reads between them: the
item's visibility only once the window is known and only without a
volume buffer, and audio_is_duplicated only after
obs_source_get_audio_mix().

tests/scene/audio_window.rs ports test_scene_audio_window_steps 1:1:
its 14 cases under the same names (steps_*), in the same order, with
the same inputs and expectations, the characterized-not-endorsed cases
included, and the wrapping-rate cases only where util_mul_div64() wraps
(not MSVC x64), as the C test does.

Edge cases (edge_*): the values the C does not ask for left unread; the
first ns of every frame from 0 to 1025 and the ns before it at the ten
rates OBS offers; small frames and the u64 ends; the minimum over small
sets; and four proptests (4000 cases each): the frame against exact
128-bit math at the real rates for any u64 distance, against the C's
wrapping formula for any rate (saturating on MSVC x64), the window
against its rule over full-range timestamps and rates, and the minimum
against a model in any order.

The tests are a module of obs-tests-core (rust/obs-data/tests/core.rs),
as the other obs-scene units' are.

soldr cargo test -p obs-data --test obs-tests-core scene::audio_window
fails 21 of 22 with "not yet implemented"; steps_child_values, which
calls no stub, passes.

Refs obs-rust#2178
Ports libobs/obs-scene-audio-window.c.h, the audio window decisions of
scene_audio_render(), to the audio_window module of rust/obs-scene.
scene_audio_render() stays C in obs-scene.c.h: audio_lock,
apply_scene_item_volume() and process_all_audio_actions() (it passes
the volume result), obs_source_audio_pending(),
obs_source_get_audio_timestamp(), obs_source_get_audio_mix(), the two
mix loops and every item and source field read, each in its old order
and count. The hybrid libobs drops the C file and links the Rust shims
in its place.

Safe core (src/audio_window.rs), one function per hidden C function:
- pick(): Show when visible and the show transition is active, Hide
  when hidden and the hide transition is active, else Source;
- counts(): not pending, and visible or hiding; earliest(): the
  non-zero source_ts below the earliest so far, or the first one;
  min_timestamp(): earliest() folded from 0; silent(): the earliest is 0;
- frames(): None when pending, with no timestamp, or from frame 1024 on;
  else pos and AUDIO_OUTPUT_FRAMES - pos, pos being
  ns_to_audio_frames(rate, source_ts - timestamp) with the subtraction
  wrapping as the C's uint64_t;
- mixes(): a volume buffer, visible, or hiding; adds(): not duplicated.
ns_to_audio_frames() is audio_actions' util_mul_div64(), now
pub(crate): the wrapping uint64_t formula, and exact 128-bit math on
MSVC x64, where the C faults on a frame that does not fit (a rate above
1 GHz) and the core saturates (the item is skipped). The module denies
clippy's arithmetic, indexing, as, unwrap, expect, panic, unimplemented
and unreachable lints, allocates nothing, keeps no state and takes no
lock.

Shim (src/ffi/scene_audio_window.rs): the seven functions with the C
signatures. The child crosses as the C's enum value, the window as
struct obs_scene_audio_window by value (mix false with pos and count 0
when skipped), size_t as usize (the rate goes to the core's u64 by
try_from, saturating, which never happens on a 64-bit size_t). No
scene, item or source pointer crosses.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-scene-audio-window (LIBOBS_PORTED_SOURCES), whose PCH has
only media-io/audio-io.h and obs-scene-audio-window.h; libobs.pch.h
keeps the header for obs-scene.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. ci/test_ported_sources.py checks the group on every host. The
exported symbol sets are unchanged and equal (1766 on the C backend,
the hybrid and libobs-c-reference); the seven are local in all three.

test_scene_audio_window_steps still compiles the C file into itself,
unchanged. The new test_scene_audio_window_steps_rust builds the same
test source against libobs_rust-static alone (the test includes only
the header and media-io/audio-io.h, so it needs neither SIMDe nor
libobs's include directories), with libm except on MSVC. It passes
14/14 against the shims. The real-libobs scene audio tests see the
swap through the hybrid.

Tier 1 scene::audio_window passes 22/22.

Refs obs-rust#2178
Oracle (oracle/scene_audio_window.c): the original
obs-scene-audio-window.c.h, unmodified, with its seven functions
renamed to oracle_*, plus the size and values of enum
obs_scene_audio_window_child, the size, alignment and offsets of
struct obs_scene_audio_window and AUDIO_OUTPUT_FRAMES (10 values).
ns_to_audio_frames() and util_mul_div64() stay the header inlines.

Tier 3 (tests/scene/audio_window_parity.rs, 8 tests), each result
compared as the C has it (bools, the child's int, the u64, the window's
mix, pos and count):
- the layout against the C header;
- exhaustive over the flags: the pick, the count and the mix over all 8
  combinations of their three flags, and both duplicated values;
- the fold step over every pair of 22 special timestamps, and silent();
- the window over the pending flag, 8 ticks (0, 1, 1e9, the test tick,
  the middle and the top end of u64), and per tick and rate 11 source
  timestamps plus both sides of the first ns of frames 1022 to 1025 in
  both directions, at 24 rates (the 10 OBS offers, 0, 1, 7, 11025,
  999999999 and, not on MSVC x64, 1 GHz up to usize::MAX);
- the first ns of every frame from 0 to 1025, and the ns before, at the
  10 rates OBS offers;
- proptests (20,000 cases each): the window over full-range u64
  timestamps (and near 0, the tick and u64::MAX) and rates (the real
  ones, 8000 to 384000, below 1e9, and any usize off MSVC x64); the fold
  and silent(); and scene_audio_render()'s decisions in its order for
  random scenes of up to 8 items, one copy on each side, each side
  asking only the values the C asks.

Tier 1 and Tier 3 passed 5 runs in a row (30/30).

Mutation check (done once, not committed): 34 mutants, 25 in the core
and 9 in the shim, failures per mutant (Tier 1 has 22 tests, Tier 3 8).

| Mutant | Tier 1 | Tier 3 |
|---|---|---|
| pick: show transition of a hidden item read | 2 | 1 |
| pick: hide transition of a visible item read | 2 | 1 |
| pick: Show and Hide swapped | 1 | 2 |
| counts: pending ignored | 3 | 1 |
| counts: hiding ignored | 2 | 2 |
| earliest: 0 counts as a timestamp | 4 | 3 |
| earliest: first timestamp not taken | 4 | 3 |
| earliest: latest kept | 4 | 3 |
| silent: inverted | 2 | 3 |
| frames: pending mixed | 3 | 2 |
| frames: no timestamp mixed | 0 | 3 |
| frames: frame 1024 mixed | 6 | 3 |
| frames: frame 1023 skipped | 7 | 2 |
| frames: saturating subtraction | 2 | 4 |
| frames: subtraction reversed | 7 | 4 |
| frames: count is the whole tick | 7 | 4 |
| frames: count one short | 7 | 4 |
| frame: exact 128-bit math (no wrap) | 3 | 1 |
| frame: rounded up | 9 | 4 |
| frame: divided by 1e9 + 1 | 7 | 2 |
| mixes: volume buffer ignored | 2 | 2 |
| mixes: hiding ignored | 1 | 2 |
| adds: duplicated added | 1 | 2 |
| shim: Show and Hide values swapped | 0 | 2 |
| shim: pick flags swapped | 0 | 2 |
| shim: counts pending and visible swapped | 0 | 2 |
| shim: frames timestamps swapped | 0 | 4 |
| shim: a skipped window has pos 1 | 0 | 4 |
| shim: rate through u32 | 0 | 1 |
| shim: adds inverted | 0 | 2 |
| equivalent: pick tests hide first | 0 | 0 |
| equivalent: earliest `<=` for `<` | 0 | 0 |
| equivalent: shim earliest arguments swapped | 0 | 0 |
| equivalent: shim mixes buffer and visible swapped | 0 | 0 |

30 are killed by Tier 3. The four that survive are equivalent: the
pick's two tests exclude each other (visible against hidden); an equal
timestamp keeps the same value; earliest() is symmetric (the smaller
non-zero one); and the mix rule is an OR. Tier 1 does not reach the
shims; test_scene_audio_window_steps_rust and Tier 3 do. "no timestamp
mixed" passes Tier 1 because a timestamp of 0 below any tick wraps past
the tick anyway; Tier 3 gives it ticks near 0. The proptest regression
files keep the seeds the mutants failed on.

Refs obs-rust#2178
The rust/README.md crate table now lists
libobs/obs-scene-audio-window.c.h among the rust/obs-scene units, and
its port table has a row for it, next to the audio actions: the seven
functions, what stays C (audio_lock, the volume buffer and the audio
actions, every obs_source_* call, the mix loops and every field read,
in the old order and count), why several calls and not one plan per
item, how values cross, what the characterization pins and the port
keeps, the one intentional difference (MSVC x64 rates above 1 GHz), the
_rust variant of the steps test, the real-libobs tests that see the swap
and what Tier 3 compares. The crate's description and lib.rs say what
it holds. docs/rust-port/naming.md already maps libobs/obs-scene*.c.h
to rust/obs-scene. No docs/features.md line: no user-visible change.

port_comparison gets its own bench file,
benches/port_comparison/scene_audio_window.rs, in the sorted modules!
list: one audio tick of a 16-item scene at 48 kHz, both loops of
scene_audio_render()'s decisions in its order, folded into a checksum
both sides must match (checked before timing). It allocates nothing, so
the leak check of obs-rust#2159 holds. On x86_64 Linux (GCC, pinned to one
core, load average near 40, so noisy; medians, two runs): 185 / 332 ns
Rust against 215 / 508 ns C per scene tick, well under a microsecond
per 1024-frame tick.

Fixes obs-rust#2178
@rdoupe
rdoupe force-pushed the port/scene-audio-window branch from 6f5b792 to 7aa47b3 Compare October 24, 2026 19:35
@zackees
zackees enabled auto-merge October 24, 2026 19:41
@zackees
zackees added this pull request to the merge queue Oct 24, 2026
Merged via the queue into obs-rust:master with commit c9b6c67 Oct 24, 2026
72 of 74 checks passed
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 scene audio render window in obs-scene.c to obs-scene

2 participants