Skip to content

obs-source: Port the source volume snap and apply decisions - #2175

Merged
zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/source-volume
Oct 24, 2026
Merged

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/source-volume

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #2158

What and why

Ports the remaining volume decisions of libobs/obs-source.c.h to rust/obs-source, as a new module volume_apply next to ptm_gate (#2089). These run for every audio source on every tick of the mix:

  • in get_source_volume(): the render-side gate (disabled, muted, push-to-mute and push-to-talk active) and the snap to 0 and 1 within 0.0001 (close_float);
  • in apply_audio_volume(): whether the first queued audio action is due this tick (action.timestamp < audio_ts + duration), and the volume step: skip at exactly 1.0, clear at exactly 0.0 or with no mixers, otherwise multiply the mixes in both masks.

The gate reuses #2089's helpers (obs_source_ptm_gate_held(), _mutes(), _talk_mutes()) instead of a second copy of their expressions. Its stop times already went through obs_source_ptm_gate_stop_time().

These stay in C: the action queue and its mutex, apply_audio_actions(), conv_frames_to_time() (C passes the duration), the memset, the multiply loops and multiply_output_audio(). Every read of the source's fields stays in C as well, in the same order:

  • each pressed flag is read where it was, once for the stop-time test and once for its active flag;
  • a stop time is read only when its key is not pressed;
  • enabled, muted and the gates' enabled flags are read only where the short-circuit reached them;
  • audio_mixers is still read once per mix, since another thread sets it.

The one change in reads: source->volume is now read once and passed to the snap, even when the source is muted. Before, it was read up to three times, and not at all when muted. Only apply_audio_action(), on the same mix thread, writes it, so every read sees the same value.

Six commits:

  1. Move (behaviour-neutral): the decisions go to libobs/obs-source-volume.c.h as four functions on plain values, declared in the new obs-source-volume.h without EXPORT, so they are hidden:

    • obs_source_volume_snap(muted, volume);
    • obs_source_volume_action_due(action_ts, audio_ts, duration);
    • obs_source_volume_step(vol, mixers), which returns enum obs_source_volume_op (SKIP, CLEAR, MULTIPLY);
    • obs_source_volume_mixes(audio_mixers, mixers, mix).

    The expressions move verbatim: vol == 1.0f, vol == 0.0f (so -0 clears), close_float()'s fabsf(f1 - f2) <= 0.0001f, and 1 << mix.

    This was checked against the original code. A scratch harness (not committed) compiles origin/master's get_source_volume() and apply_audio_volume() verbatim next to the moved ones, on a stand-in source. It records every call they make, with floats by bit pattern: the action mutex, conv_frames_to_time(), apply_audio_actions(), the memset and multiply_output_audio().

    • 30,000,000 runs of each function under ASan and UBSan, 20,000,000 at -O2 and 10,000,000 at -O0.
    • Inputs: every gate flag; stop times and clocks around each other and at the u64 ends; volumes from the snap edges and their float neighbours, -0, NaN payloads, ±inf, subnormals and random bit patterns; mixer masks at the edges and random; 0 to 2 queued actions, due and not due, with the sum wrapping; durations at the u64 ends.
    • 150,000 of the runs also clear and multiply real buffers filled with random bits.
    • 220,449,492 recorded calls are equal, and so are the returned volumes (by bits), the source after every call and the buffers.

    Eleven deliberately broken moves were tried: each snap edge, the muted test, the -0 result, the due compare and a modular version of it, clearing on <= 0 and on no mixers, either mask, the mix bit, and vol == 1.0f written as fabsf(vol - 1.0f) <= 0. The first ten fail the harness. The last is equivalent and passes.

    git log -L on both functions shows libobs: Saturate push-to-talk stop times and clamp the audio balance #2058 (saturated stop times) and obs-source: Port the push-to-mute/talk gate #2089 (their helper) as the fork's changes. The 0.0001 snap, the exact compares and the due compare date from the 2015 audio subsystem and move as they are.

  2. Characterization: test_source_volume_apply_steps, 14 cases. It compiles the moved file and obs-source-ptm-gate.c.h (ASan and UBSan) and calls only their functions. get_source_volume() and apply_audio_volume() are copied onto a stand-in source, with the memset and the multiply recorded. It pins:

    • all 64 gate flag combinations, with each stored stop time before, at and after the clock and saturated (1,600 runs);
    • the snap at 0 ± 0.0001f and at the last floats that snap to 1 (1 - 1677·2⁻²⁴ and 1 + 838·2⁻²³), each with its float neighbours; -0 gives +0; subnormals, NaN of either sign and payload, and ±inf;
    • each step with mixer masks 0, 1, 2, 0x20, 0x3f, 0x40, 0x41, 0x7f, 0x80, 0xff, 0x80000000 and UINT32_MAX;
    • the mix choice over every pair of those masks;
    • the due compare at the threshold − 1, at it and + 1, and the u64 wrap.

    It is in the core suite of the test graph and links libm except on MSVC.

  3. RED: Tier 1 against todo!(): the 14 C cases ported 1:1, plus 11 edge tests. The snap is checked against the rule computed in f64, over every float within 2¹⁴ ulps of each edge, a stride through all 2³² bit patterns and a bit-pattern proptest. The step is checked against the IEEE compares written another way, the due compare against u128, and the mix choice for every index up to 64. get_source_volume() runs over any source.

  4. GREEN: the safe core and four #[no_mangle] shims, in a new libobs.ported-source-volume group whose PCH is graphics/math-defs.h (for close_float) and the new header. This commit updates source-sets.json, the unity lists, the three export lists and ci/test_ported_sources.py, each in sorted place. It also adds test_source_volume_apply_steps_rust: the obs-source archive member links on its own, as it does for test_source_ptm_gate_steps_rust, and it holds both the volume shims and the ptm_gate shims the test calls.

  5. Tier 3: a C oracle of the unchanged file, parity tests, the proptest regression files and a mutation check.

  6. Docs: the README rows (including the ptm_gate row, which said get_source_volume() kept its own gate), the crate's description, lib.rs, and a port_comparison bench file. docs/rust-port/naming.md already maps libobs/obs-source*.c.h to rust/obs-source, so it needs no new row.

The core denies clippy's arithmetic, indexing, as, unwrap, expect, panic, todo, unimplemented and unreachable lints. It keeps no state, takes no lock, and cannot panic on any input.

  • The C's compares are kept, bit for bit. close() is (f1 - f2).abs() <= 0.0001 in f32, so NaN is never close and -0.0 is close to 0. The step uses IEEE ==. action_due() is action_ts < audio_ts.wrapping_add(duration).

  • Kept, characterized, not endorsed:

    • 0.9999f, 1.0001f and 1.0f ∓ 0.0001f all round to the floats just outside the snap, so none of them snaps to 1;
    • a volume of 1 is skipped even with no mixers; the callers have already cleared those buffers;
    • tick mixers with no bit below MAX_AUDIO_MIXES (0x40 and up) neither clear nor multiply; the callers have already cleared those mixes;
    • in the last tick before the u64 clock ends, audio_ts + duration wraps, so nothing at or after audio_ts is due.
  • Intentional difference: obs_source_volume_mixes() with a mix of 31 or more, where the C's 1 << mix on an int is undefined. The port answers for bit 31, then for no mix. libobs only passes a mix below 6. Tier 1 tests this; Tier 3 stays where the C is defined. It is listed in the parity test header, the shim docs and rust/README.md.

  • Tier 3 compares the oracle, the shim and the core on every input. Floats are compared by bits, the step as the u32 the C returns, and each bool as the byte the C returns.

    • The snap and the step run over 115 special floats with 14 masks, and every float within 2¹⁶ ulps of each snap edge (786,444).
    • The mix choice runs exhaustively over every pair of the low seven mask bits, with and without the high bits, for mixes 0 to 30 (1,015,808 calls).
    • The due compare runs over every triple of 35 special values (42,875).
    • get_source_volume() and apply_audio_volume() are copied onto a stand-in and run over each side's functions, the ptm_gate oracle included. Every flag runs with stop times, delays, clocks and 16 volumes (230,400 runs), and with masks and queue states (802,816 runs). The result's bits, the stored stop times and the recorded clear and multiply calls must match.
    • Proptests (20,000 cases each) run over any f32 bit pattern, any masks and full-range u64 times.
    • An ignored test runs all 2³² bit patterns through the snap (muted and not) and the step (with and without mixers). It passes in a release build in 35 s.
  • Mutation check (not committed): 25 mutants, each run against Tier 1 (25 tests) and Tier 3 (14).

    Area Mutants Tier 1 kills Tier 3 kills Survivors
    snap (epsilon 0.00011, <, muted ignored, -0 kept, snap to 1 only from below, NaN close) 6 6 6 0
    snap distance in f64 1 0 0 1, equivalent: within [0.5, 2] the f32 difference is exact, and outside it both distances are at least 0.5
    due (<=, saturating sum, modular distance) 3 3 3 0
    step (skip at >= 1, -0 not cleared, no mixers not cleared, NaN skips, skip near 1) 5 5 5 0
    mixes (either mask, bit off by one, none past 6) 3 3 3 0
    mixes (shift wraps past 31) 1 1 0 0 (Tier 3 stays where the C is defined)
    shims (muted inverted, CLEAR/MULTIPLY swapped, due arguments swapped, next mix, sign dropped, tick mixers ignored) 6 0 6 0
  • Exported symbols: 1766 on the C backend, the hybrid and libobs-c-reference, with identical sets, unchanged from master. This was checked on the move, on GREEN and on the head. The four functions are local (t) in all three. In the hybrid they come from the Rust archive: no C object of the group is in the hybrid libobs, and its obs_source_volume_snap is Rust code.

  • Performance (port_comparison, x86_64 Linux, GCC, release, not pinned to a core; median per 1024 calls):

    • apply_volume, through both callers: Rust 6.71 µs, C 9.84 µs;
    • snap: Rust 1.76 µs, C 1.70 µs.

    That is under 10 ns per source per 21.3 ms tick, either way. The run used -- source_volume with every fixture built; since obs-c-oracle: Free what port_comparison's fixtures allocate #2159 the full run no longer stops at bmem/count.

  • Overlap: no open PR touches get_source_volume() or apply_audio_volume(). obs-source: Port the deinterlace draw decisions #2153 touches obs-source-deinterlace.c.h and obs-core: Port the source load migration and value rules #2157 adds obs-source-load.c.h; neither touches obs-source.c.h. The size routing functions (rust: Port the source size query routing in obs-source.c to obs-source #2149, in progress) are not changed here. The shared lists (source-sets.json, the export lists, test-graph.json, port_comparison.rs) take one sorted entry each, as every port does.

  • No user-visible change, so no docs/features.md line.

RED → GREEN

  • The commits were rebased onto current master over frontend: open the Missing Source Types warning without blocking startup (#2161) #2166 (a frontend change that touches none of these files) after the runs below, with no conflicts; the runs name the pre-rebase SHAs where they were taken.
  • RED commit: a6abaa16a. soldr cargo test -p obs-data --test obs-tests-core source::volume_apply fails 23 of 25 on todo!() (obs_source_volume_snap, _action_due, _step, _mixes). The two that pass read only constants: the epsilon's bits and the enum's values.
  • GREEN: 62571ab1c, Tier 1 25/25. On the head 2cce5e152 (rebased on 2c594d844). The C builds, the ctest runs and the export counts ran on 11036f4f3: libobs/ and test/ are identical between the two, and both trees are current for the head (ninja has nothing to rebuild):
    • Tier 1 25/25 and Tier 3 14/14 (1 ignored; the ignored exhaustive test passes in release);
    • test_source_volume_apply_steps 14/14, compiled C and _rust;
    • the required tests (test_source_audio_ranges, test_source_volume_mix, test_source_ptm_gate_steps and _rust, test_audio_capture_mix_race, test_scene_audio_actions, test_scene_audio_action_steps, test_source_volume_apply_steps and _rust) pass 7/7 on the C backend and 9/9 against the hybrid libobs and libobs-c-reference, with no skips;
    • the full ctest: 453/453 on the C backend and 468/468 against the hybrid libobs, 0 skipped (private Xvfb, fresh HOME, TMPDIR unset); the known flakes, the five test_deinterlace_2x_field_order_* tests (test_deinterlace_2x_field_order is timing-sensitive (yadif 2x 60i into 60): startup underrun and load-induced hold #456) and test_group_hide_transition_audio (Flaky: real-time audio cmocka tests lose audio under machine load (test_vlc_playlist_gap, test_group_hide_transition_audio) #838), passed in both runs;
    • ci/local_gate.py --test-diff origin/master runs 3451 Rust tests, all passing (5 skipped, all #[ignore]: master's 4 and the exhaustive one); --lane lint passes on the head and --lane cross on 11036f4f3 (since then only the bench file changed in Rust);
    • the ci/ checks pass: build groups, ported sources, oracle stand-ins, sorted shared lists, test graph sorted, rust-static guard, cmocka Windows diagnostics, cmocka includes and naming.
  • Real app (UI, protocol output or logging changes): not applicable, no UI, protocol or log change.

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
  • 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
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

Rebased onto master after #2153 and #2157 merged (head 012703516). The only conflicts were the obs-source crate lists in rust/README.md, rust/obs-source/Cargo.toml and lib.rs, merged by keeping both entries. After the rebase, soldr cargo test -p obs-data --test obs-tests-core source:: and soldr cargo test -p obs-c-oracle --locked --bench port_comparison (370 benches) pass. The earlier runs above are on the pre-rebase SHAs.

Rebased onto master after #2171 and #2181 merged (head 5837736ad). The only conflicts were the shared lists and the crate prose lists, merged by keeping both entries. After the rebase, the crate's Tier 1 and Tier 3 tests and soldr cargo test -p obs-c-oracle --locked --bench port_comparison pass. The earlier lane runs above are on the pre-rebase SHAs.

@rdoupe
rdoupe requested a review from zackees as a code owner October 24, 2026 15:00
@rdoupe
rdoupe force-pushed the port/source-volume branch from 2cce5e1 to 0127035 Compare October 24, 2026 15:38
@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 failed status checks Oct 24, 2026
@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 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
…ce-volume.c

Behaviour-neutral extraction before the Rust port. The volume decisions of
get_source_volume() and apply_audio_volume() move to
libobs/obs-source-volume.c.h, as four functions on plain values declared in
the new libobs/obs-source-volume.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- obs_source_volume_snap(muted, volume): 0 when muted or
  close_float(volume, 0.0f, 0.0001f), 1 when close_float(volume, 1.0f,
  0.0001f), else the volume, the same expressions;
- obs_source_volume_action_due(action_ts, audio_ts, duration): whether the
  first queued action falls inside this tick, action_ts < audio_ts +
  duration, the sum wrapping as before;
- obs_source_volume_step(vol, mixers): SKIP at vol == 1.0f, CLEAR at
  vol == 0.0f (so -0 too) or mixers == 0, else MULTIPLY (NaN multiplies);
- obs_source_volume_mixes(audio_mixers, mixers, mix): whether the multiply
  touches a mix, in both masks, with the same (1 << mix).

get_source_volume()'s gate (disabled, muted, push-to-mute and push-to-talk
active) now goes through obs-rust#2089's helpers, obs_source_ptm_gate_held(),
_mutes() and _talk_mutes(), instead of a second copy of their expressions;
its stop times already went through obs_source_ptm_gate_stop_time().

obs-source.c.h keeps every read and write of the source's fields in the same
order: each pressed flag is read where it was (in the stop-time test and
again for its active flag), a stop time only when its key is not pressed,
and enabled, muted and the gates' enabled flags only where the
short-circuit reached them. source->audio_mixers is still read once per mix
(another thread sets it). The one change in reads: source->volume is read
once and passed to the snap, also when muted, where the C read it up to
three times and not at all when muted; it is written only by
apply_audio_action() on the same mix thread, so every read sees the same
value. The action queue and its mutex, apply_audio_actions(),
conv_frames_to_time() (the duration is passed in), the memset, the multiply
loops and multiply_output_audio() stay as they were.

The new file joins libobs.core-source, before obs-source.c.h, 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. The exported symbol set is unchanged (1766); the four
functions are local.

Checked in a scratch harness (not committed) that compiles origin/master's
get_source_volume() and apply_audio_volume() verbatim next to this branch's
and the moved file, on a stand-in source, and records every call they make
(the action mutex, conv_frames_to_time(), apply_audio_actions(), the memset
and multiply_output_audio(), floats by bit pattern): 30,000,000 runs of each
function under ASan and UBSan (20,000,000 at -O2, 10,000,000 at -O0) with
every gate flag, stop times and clocks around each other and at the u64
ends, volumes from the snap edges and their float neighbours, -0, NaN
payloads, +-inf, subnormals and random bit patterns, mixer masks at 0, 1,
0x3f, 0x40, 0xff, 0x80000000, UINT32_MAX and random, 0 to 2 queued actions
due and not due (the sum wrapping), and durations at the u64 ends; 150,000
of the runs also clear and multiply real buffers of random bits.
220,449,492 recorded calls, the returned volumes (by bits) and the source
after each call are equal, and so are the buffers. Ten deliberately broken
moves (each snap edge, the muted test, the -0 result, the due compare and a
modular one, the clear on <= 0 and on no mixers, either mask, the mix bit)
all fail it; vol == 1.0f written as fabsf(vol - 1.0f) <= 0.0f, which is
equivalent, passes.

git log -L on both functions shows obs-rust#2058 (the saturated stop times) and
and 1 compares and the due compare date from the 2015 audio subsystem and
move as they are.

Refs obs-rust#2158
test_source_volume_apply_steps compiles libobs/obs-source-volume.c.h and
libobs/obs-source-ptm-gate.c.h into itself (ASan and UBSan) and calls only
their hidden functions, through their headers. get_source_volume() and
apply_audio_volume() are copied from obs-source.c onto a stand-in source,
with apply_audio_actions(), the memset and multiply_output_audio() recorded
instead of run, so the compositions are pinned too. 14 cases pin:

- the snap at 0: 0, -0 (to +0), +-0.0001f and the float inside each, the
  smallest subnormal and FLT_MIN of either sign snap to 0; the float just
  past 0.0001f, 0.00011f and 0.001f are kept;
- the snap at 1: the last floats that snap, 1 - 1677 * 2^-24 and
  1 + 838 * 2^-23, and their outer neighbours, which do not (checked to be
  float neighbours); characterized, not endorsed: 0.9999f, 1.0001f,
  1.0f - 0.0001f and 1.0f + 0.0001f all round to those outer floats, so none
  of them snaps; 0.99995f and 1.00005f do;
- every other volume kept bit for bit: 0.5, 2, 20, +-FLT_MAX, -0.5, +-inf,
  -1 and NaNs of either sign and several payloads (a signalling NaN is still
  NaN); muted gives +0 for every volume, NaN and -0 included;
- the gate over all 64 combinations of enabled, muted and each gate's
  enabled flag and pressed key, with each stored stop time before, at and
  after the clock and saturated (1,600 runs against the rule written out,
  the stored stop times included), and an open gate giving the snapped
  volume held, inside the delay and after it;
- the action-due compare at audio_ts + duration - 1 (due), at it and + 1
  (not), actions from before the tick, a duration of 0, the u64 ends, and
  characterized, not endorsed, the wrap: in the last tick before the clock
  ends audio_ts + duration wraps, so nothing at or after audio_ts is due;
- the step for 15 volumes with 12 mixer masks (0, 1, 2, 0x20, 0x3f, 0x40,
  0x41, 0x7f, 0x80, 0xff, 0x80000000, UINT32_MAX): exactly 1 skips whatever
  the mixers (characterized, not endorsed: with none too, where the callers
  have already cleared the buffers), exactly 0 or -0 or no mixers clears,
  anything else multiplies, the float next to 0 or 1 and NaN included; the
  enum values and size;
- the mix choice over every pair of those masks and each of the six mixes;
- apply_audio_volume(): a due action hands the tick to
  apply_audio_actions() and nothing else; one not due, or an action
  timestamp with an empty queue, does not; each step with every pair of
  masks: the clear is one memset of all six mixes and eight channels, the
  multiply runs over the mixes in both masks in order with the channels and
  the snapped volume; characterized, not endorsed: tick mixers of 0x40 and
  above are not 0, so nothing is cleared, and no mix is in both masks, so
  nothing is multiplied (the callers have cleared those mixes already).

The test is in the core suite of the test graph and links libm except on
MSVC. test_source_audio_ranges, test_source_volume_mix,
test_audio_capture_mix_race and the scene item audio action tests cover the
callers through the real libobs.

Refs obs-rust#2158
Adds the volume_apply module to rust/obs-source with its API and every
function as todo!(): SNAP_EPSILON (the C's 0.0001f), MAX_AUDIO_MIXES, the
Step enum (SKIP, CLEAR, MULTIPLY with the C enum's values), snap(),
action_due(), step() and mixes().

tests/source/volume_apply.rs ports test_source_volume_apply_steps 1:1
(steps_*: the same 14 cases, inputs and expectations, with the stand-in
source and the copies of get_source_volume() and apply_audio_volume(), the
gate through obs_source::ptm_gate) and adds edge cases (edge_*):

- the snap against the rule computed in f64 (exact there; within [0.5, 2]
  the f32 difference is exact too, and outside it both distances are at
  least 0.5): every float within 2^14 ulps of +-0.0001, of the two last
  floats that snap to 1 and of +-0 (196,620 patterns, NaNs past the top
  included), a stride of 4,103 through all 2^32 bit patterns (1,046,788) and a
  proptest over any bit pattern, muted and not; the epsilon's bits
  (0x38d1b717);
- the step against the IEEE compares written another way, over any bit
  pattern and mixers, -0, NaN and the floats next to 0 and 1 included;
- the due compare against u128 arithmetic wrapping at 2^64, over every
  triple of 35 special values, full-range u64 proptests and actions within
  3 ns of the tick's end;
- the mix choice for every index below 32 with every pair of mask edges,
  and none chosen at 32 and past (an intentional difference: the C's int
  shift is undefined there; libobs passes mix < 6);
- get_source_volume() over any flags, stop times, delays, clocks and volume
  bits against the gate and the snap written out.

RED: soldr cargo test -p obs-data --test obs-tests-core
source::volume_apply: 2 passed (the epsilon's bits and the enum values),
23 failed, each on a todo!() (obs_source_volume_snap, _action_due, _step
and _mixes).

Refs obs-rust#2158
Ports libobs/obs-source-volume.c.h, the volume decisions of
get_source_volume() and apply_audio_volume(), to the volume_apply module of
rust/obs-source. obs-source.c.h stays C around it: every read and write of
the source's fields in the same order, the gate's short-circuits (through
obs-rust#2089's ptm_gate shims), the action queue and its mutex,
apply_audio_actions(), conv_frames_to_time(), the memset, the multiply loops
and multiply_output_audio(). The hybrid libobs drops the C file and links
the Rust shims in its place.

Safe core (src/volume_apply.rs):
- snap(): 0.0 when muted or close to 0, 1.0 when close to 1, else the
  volume with its bits; close() is close_float(): (f1 - f2).abs() <=
  SNAP_EPSILON in f32, the C's fabsf(f1 - f2) <= precision, so NaN is never
  close and -0.0 is close to 0 (and gives +0.0);
- action_due(): action_ts < audio_ts.wrapping_add(duration), the C's
  uint64_t sum;
- step(): Skip at vol == 1.0, Clear at vol == 0.0 (-0.0 too) or no
  mixers, else Multiply (NaN included), IEEE compares as the C's;
- mixes(): the mix's bit in both masks; a mix of 32 or more chooses none.
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_volume.rs): the four functions keep the C signatures;
the step goes back as the C enum's value (u32, 0 to 2). No pointer crosses.
Intentional difference: obs_source_volume_mixes() with a mix of 31 or more,
where the C's 1 << mix on an int is undefined (libobs passes mix < 6).

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-volume (LIBOBS_PORTED_SOURCES), whose PCH is
graphics/math-defs.h (close_float) and obs-source-volume.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 four functions are local in all three,
and the hybrid's come from the Rust archive (no C object of the group in
the hybrid libobs). ci/test_ported_sources.py checks the group on every
host.

test_source_volume_apply_steps still compiles the C files into itself,
unchanged, and test_source_volume_apply_steps_rust runs the same test
source against libobs_rust-static alone (and libm except on MSVC): the
obs-source member links on its own, as for test_source_ptm_gate_steps_rust,
and holds both the volume and the ptm_gate shims the test calls.

Tier 1 obs_source::volume_apply passes 25/25.

Refs obs-rust#2158
Adds the oracle_* build of the unchanged libobs/obs-source-volume.c.h to
obs-c-oracle (oracle/source_volume.c, its four functions renamed, plus the
enum's values and size), and tests/source/volume_apply_parity.rs, which
gives every input to the oracle, the shim and the core. Floats are compared
by bits (NaN payloads, -0 and the infinities count), the step as the u32
the C returns, each bool as the byte the C returns (it must be 0 or 1):

- the enum's values and size (4) against the C header;
- the snap, muted and not, and the step with 14 mixer masks, over 115
  special floats (the snap edges and the floats around them, -0, NaN
  payloads, +-inf, subnormals, 0.9999f and 1.0001f);
- the snap over every float within 2^16 ulps of +-0.0001, of the last
  floats that snap to 1 and of +-0 (786,444 patterns);
- the mix choice exhaustively over every pair of the low seven mask bits,
  with no high bits and with all of them, for every mix of 0 to 30
  (1,015,808 calls; from 31 the C's int shift is undefined, the one
  intentional difference, checked in Tier 1);
- the due compare over every triple of 35 special values (42,875);
- get_source_volume() copied from obs-source.c onto a stand-in source and
  run once over the oracle's functions (obs-rust#2089's ptm_gate oracle for the
  gate) and once over the shims: the 64 flag combinations, five stored stop
  times per gate around the clock and saturated, three pairs of delays,
  three clocks and 16 volumes (230,400 runs); the result's bits and both
  stored stop times must match;
- apply_audio_volume() copied the same way, apply_audio_actions(), the
  memset and multiply_output_audio() recorded: the 64 flag combinations, 16
  volumes, 14 x 14 masks and four queue states (none, not due, due, the
  wrapped end), 802,816 runs; the recorded calls and the source must match;
- proptests (20,000 cases each) over any f32 bit pattern for the snap and
  the step, any masks, full-range u64 times for the due compare and within
  3 ns of the tick's end, and full-range sources through both callers.

An ignored test runs all 2^32 bit patterns through the snap (muted and
not) and the step (with and without mixers) against the oracle; in a
release build it passes in 35 s.

The regression files hold the seeds proptest saved while the mutation
check below ran.

Mutation check (not committed): 25 mutants of the core and the shims, each
run against Tier 1 (25 tests) and Tier 3 (14). 23 are killed by both or
by the tier that reaches them. C6, the snap's distance computed in f64, is
equivalent (within [0.5, 2] the f32 difference is exact, outside it both
are at least 0.5) and survives both. C19, a mix shift that wraps past 31,
is killed by Tier 1 only, since Tier 3 stays where the C is defined. Tier
1 does not reach the shims; Tier 3 kills all six shim mutants.

Tier 1 25/25 and Tier 3 14/14 (1 ignored) pass.

Refs obs-rust#2158
rust/README.md: a row for libobs/obs-source-volume.c.h ->
obs-source::volume_apply (the four functions, what stays C and in what
order, what is kept and characterized, the one intentional difference, the
tests that see the swap and what Tier 3 compares), the obs-source crate row
lists the file, and the ptm_gate row now says get_source_volume() goes
through its four functions too. The crate's description and lib.rs list
the new module. docs/rust-port/naming.md already maps
libobs/obs-source*.c.h to rust/obs-source.

port_comparison gets source_volume.rs: apply_audio_volume() and
get_source_volume() over 1024 ticks (every gate flag, volumes at and
around 0 and 1, mixer masks from none to all, actions due and not due),
and 1024 snaps, each side through its own functions, with checksums that
must match. Measured with soldr cargo bench -p obs-c-oracle --bench
port_comparison -- source_volume, every fixture built (obs-rust#2159 made the full
run work again): apply_volume 6.71 us Rust, 9.84 us C per 1024 ticks; snap
1.76 us Rust, 1.70 us C per 1024.

Fixes obs-rust#2158
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 source volume snap and apply decisions in obs-source.c to obs-source

2 participants