Skip to content

obs-source: Port the push-to-mute/talk gate - #2089

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

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

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator

Fixes #2066

What and why

Ports the push-to-mute and push-to-talk gate of source_muted() in libobs/obs-source.c.h to rust/obs-source, as a new module ptm_gate. Every audio packet a source outputs goes to its capture callbacks with the muted flag this gate computes. The stop time also covers the second use in get_source_volume(): both go through push_to_stop_time(), the saturating helper #2058 added, so it moves too and its saturation is kept bit for bit.

The pressed flags are written by the hotkey thread without atomics, and get_source_volume() on the audio mix thread stores the same two stop times. So the port moves only decisions on values. source_muted() and get_source_volume() stay C and keep every field read and write, in the same order; Rust never sees struct obs_source.

Six commits:

  1. Move (behaviour-neutral): four functions go to libobs/obs-source-ptm-gate.c.h, declared in the new obs-source-ptm-gate.h without EXPORT (hidden):

    • obs_source_ptm_gate_stop_time(): push_to_stop_time() unchanged, os_time + delay_ms * 1000000, saturated at UINT64_MAX where the product or the sum would wrap. get_source_volume() calls it too;
    • obs_source_ptm_gate_held(): os_time < stop_time;
    • obs_source_ptm_gate_mutes() and obs_source_ptm_gate_talk_mutes(): push-to-mute mutes while enabled and active, push-to-talk while enabled and not active.

    Why the reads and writes are the same. A stop time is stored only while its gate is enabled and its key pressed, as before. An unconditional store would race with the mix thread's. Each pressed flag is read again for its active flag, so a key pressed between the two reads is still seen at the second one. A stop time is read only when its key is not pressed. enabled, user_muted and the two enabled flags are read only where the C's || and && reached them, because each gate is its own call inside the same || chain. A scratch harness (not committed) compiles origin/master's push_to_stop_time() and source_muted() verbatim next to the moved version, with every field access logged. On 20,000,000 random states (special and full-range delays, stop times and clocks, every flag) both give the same result, the same fields afterwards, and the same reads and writes in the same order.

    get_source_volume() keeps its own active and muted expressions, on push_to_*_pressed and muted. Only its stop time goes through the helper. The file stays in libobs.core-source for this commit.

  2. Characterization: test_source_ptm_gate_steps, 16 cases. It compiles the moved file and pins the four functions, plus source_muted() and the gate of get_source_volume() copied onto a stand-in source:

    • each flag: every combination of enabled, user_muted and each gate's enabled flag and key (64 rows), with the stop times each one stores. Disabled and user-muted sources are muted whatever the gates say, and still store stop times;
    • a release inside the delay (1 ns after, half way, 1 ns before the stop time) and after it (at it, 1 ns and 10 s later), for push-to-talk and push-to-mute;
    • a delay of 0: the next packet after the release, even at the same time, is past the stop time;
    • the saturation edges: UINT64_MAX / 1000000 (18446744073709) ms is the last delay whose product fits, and one more saturates. It wrapped to 448384 ns before libobs: Saturate push-to-talk stop times and clamp the audio balance #2058. So do INT64_MAX, UINT64_MAX and negative int64 delays converted, which the setters still take. That delay from a clock of 551614, 551615 and 551616 ns gives UINT64_MAX - 1, exactly UINT64_MAX, and the saturated UINT64_MAX. 1 ms and 300 ms delays at the end of the clock's range, each +-1 ns, behave the same way. Saturated delays hold up to UINT64_MAX - 1;
    • the hold, strictly before the stop time, and both gates together on one source.

    All 16 cases pass against origin/master's functions compiled verbatim (scratch).

  3. RED: Tier 1 in the crate against todo!(). 25 of 25 fail.

  4. GREEN: the safe core and #[no_mangle] shims. The core's stop time is os_time.saturating_add(delay_ms.saturating_mul(NS_PER_MS)), which matches the C's two checks: a saturated product is u64::MAX, any clock plus u64::MAX saturates too, and otherwise the sum saturates exactly where delay_ns > UINT64_MAX - os_time does. They go in a new libobs.ported-source-ptm-gate group with its own PCH (only the header). The commit also updates source-sets.json, the unity lists, the three export lists and ci/test_ported_sources.py, all kept sorted, and adds a _rust variant of the steps test (see below).

  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.

Kept, characterized, not endorsed:

  • a saturated stop time does not hold at os_time == UINT64_MAX itself;

  • a stop time stored before a gate was turned off still holds when the gate is turned back on;

  • while a key is held, each packet stores its own time plus the delay, so a packet earlier than the last one moves the stop time back;

  • the mix (whose pressed flags come from the queued audio actions) and the capture callbacks (whose flags come from the hotkey thread) share the two stop times, so a store by either one holds the other open. The mix reads source->muted, not user_muted.

  • No intentional difference from C. The shims take and return uint64_t and bool by value and touch no memory.

  • The _rust variant links. test_source_ptm_gate_steps_rust builds the unchanged test source against libobs_rust-static, as test_source_color_space_steps_rust does. The test includes only the new header and calls only the four functions, so it runs the shims. It needs no libm and no SIMDe headers. It passes 16 of 16 on the hybrid tree.

  • Exported symbols: 1766 on the C backend, the hybrid and libobs-c-reference, with identical sets. The four functions are local (t) in all three; the hybrid compiles no libobs.ported-source-ptm-gate object and takes them from liblibobs_rust.a.

  • Performance (port_comparison, x86_64 Linux, GCC, not pinned, a loaded machine; median per 1024 calls):

    • source_muted: 4.77 µs Rust, 4.73 µs C (1.01x);
    • stop_time: 2.11 µs Rust, 2.66 µs C (0.79x).

    That is under 1 ns a packet either way.

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

Observed, not changed: the active and muted expressions of get_source_volume() are the same shape as source_muted()'s, on other fields. This PR moves only their stop time, and they could use the same two gate functions in a later PR.

RED → GREEN

  • RED commit: 2da9ed551. soldr cargo test -p obs-data --test obs-tests-core source::ptm_gate fails 25 of 25, every failure "not yet implemented": the 16 ported steps_* cases and 9 edge_* cases, 4 of them proptests. Run on d936c98ed, the same commit before the last rebase, which touched nothing in rust/obs-source.
  • GREEN: d78255cab. Tier 1 passes 25/25, and 26/26 from 8a53158ec, which adds the case the mutation check asked for. Tier 2 ran on 77625b46f (the head before the last rebase, based on d13233558), under a private Xvfb -noreset with dbus-run-session, a fresh HOME and ulimit -c 0, with no TMPDIR set. libobs-c-reference was built with ninja libobs-c-reference before the c-ref runs and is newer than that head commit.
Test C backend hybrid libobs-c-reference
test_source_ptm_gate_steps 16/16 16/16 16/16
test_source_ptm_gate_steps_rust (not built: needs the Rust archive) 16/16 16/16
test_source_audio_ranges (#2058) 9/9 9/9 9/9
test_source_audio_balance_mono 4/4 4/4 4/4
test_source_audio_input_steps 22/22 22/22 22/22
test_source_audio_input_steps_rust (not built) 22/22 22/22
test_hotkey_load 7/7 7/7 7/7
test_wayland_hotkeys (holds push-to-talk) 8/8 8/8 8/8
test_portal_shortcuts 32/32 32/32 32/32
test_hotkey_press_state 18/18 18/18 18/18
test_hotkey_pair_latch 11/11 11/11 11/11
full ctest 421/421, 0 skipped 431/431, 0 skipped 431/431, 0 skipped

On the C backend at the characterization commit (4e553efad, before the rebase), all of the tests above ran too, and test_source_ptm_gate_steps passed 16/16. No case was skipped in any run.

The last rebase onto 5f5d2cdbc brought in only #2050 (obs-ffmpeg) and #2069 (claim bot), with no conflicts. Afterwards, I hand-diffed the unity lists, source-sets.json, test-graph.json and the export lists against origin/master: each shows only this PR's entries. I then re-ran these on the head 4108e4d35: the ci/ checks, source:: in obs-tests-core (225 passed, 1 ignored), the lint lane and --test-diff.

  • Tier 3, 8a53158ec (source::ptm_gate_parity, 9 tests). Each input goes to the oracle, to the shim the hybrid links and to the core. It covers:

    • the constant against the C header;
    • the two gates over all four inputs each;
    • the stop time and the hold over every pair of 46 special values (0, 1 ms, 1 s, a clock, the 551615 ns slack, the largest unwrapped delay and its product, u64::MAX / 2, i64::MAX, the end of the range less 1 ms and less the slack, u64::MAX, each +-2), 2,116 pairs each. Proptests cover full-range u64 clocks and delays, and values near the edges;
    • source_muted() copied onto a stand-in, run once over the oracle and once over the shims: every combination of its six flags, with stop times before, at and after the clock and saturated, three pairs of delays and three clocks (14,400 runs), plus a full-range proptest. The result and both stored stop times must match.

    The proptests run 20,000 cases each. Tier 1 and the parity module passed 10 runs in a row with fresh seeds. No regression file came out, so none is committed.

  • Mutation check (once, not committed): 24 mutants. 23 fail, and 1 survives and is equivalent. The table gives failing tests out of 26 (Tier 1) and 9 (Tier 3).

Mutant Tier 1 Tier 3
stop time wraps (#2058 reverted) 10 5
only the product saturates / only the sum 10 / 7 5 / 5
delay ignored 18 5
NS_PER_MS 1000 18 (with the case added in 8a53158ec; 1 before) 6
product edge one early (>= MAX / NS_PER_MS) 4 2
sum edge one early (a sum of MAX - 1 saturates) 5 2
saturates at MAX - 1 10 5
held <= / reversed / never 11 / 13 / 12 2 / 4 / 4
push-to-mute || / ignores enabled / as push-to-talk 9 / 6 / 10 3 each
push-to-talk as push-to-mute / ignores enabled / || 12 / 8 / 14 3 each
shim: stop time arguments swapped / 1 ms more 0 (Tier 1 does not reach the shims) 3 / 3
shim: held arguments swapped / held at the stop time 0 4 / 2
shim: mutes calls talk_mutes / talk_mutes arguments swapped 0 3 / 3
equivalent: the sum edge as >= (at equality the sum is UINT64_MAX either way) 0 0
  • Real app: no UI, protocol or logging change.

Other runs:

  • uv run ci/local_gate.py --test-diff origin/master: ok on 77625b46f and again on the head 4108e4d35. Both used workspace scope: 3111 of 3111 passed, 2 skipped (ignored tests). --lane lint: ok on both. --lane cross: ok on 77625b46f.
  • ci/test_build_groups.py, ci/test_ported_sources.py, ci/test_oracle_standins.py, ci/test_sorted_shared_lists.py and ci/test_test_graph_sorted.py: OK on the head.

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 test-diff, lint and cross lanes passed locally; nothing is stamped)
  • 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 change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports) (there are none, and both say so)

Rebased onto e1fbd9c72 after #2055 (the transition clock port) merged. The conflicts were only the shared lists and the obs-source crate description, lib.rs docs and README row; both entries kept, in sorted order. After it: ci/test_build_groups.py, test_ported_sources.py, test_oracle_standins.py, test_sorted_shared_lists.py, test_test_graph_sorted.py, source:: in obs-tests-core (281 passed, 3 ignored), the lint lane, and a hybrid rebuild with test_source_ptm_gate_steps, its _rust variant and test_source_audio_ranges (3/3) all pass. The full ctest above ran before this rebase.

Behaviour-neutral extraction before the Rust port. The decisions of
source_muted() in obs-source.c.h, which tells every audio capture callback
whether a packet is muted, move to libobs/obs-source-ptm-gate.c.h as four
functions on values, declared in the new libobs/obs-source-ptm-gate.h
without EXPORT (hidden, as -fvisibility=hidden keeps them):

- obs_source_ptm_gate_stop_time(): push_to_stop_time() from obs-rust#2058, moved
  unchanged: the moment a held key stops holding, os_time plus the delay in
  milliseconds, saturated at UINT64_MAX where the product or the sum would
  wrap. get_source_volume() calls it too, in place of push_to_stop_time();
- obs_source_ptm_gate_held(): os_time < stop_time, a key let go still holds
  until its stop time;
- obs_source_ptm_gate_mutes() and obs_source_ptm_gate_talk_mutes(): the
  push-to-mute gate mutes while enabled and active, the push-to-talk gate
  while enabled and not active.

source_muted() keeps every read and write of the source's fields, in the
same order, and passes the values it read:

- the stop time is stored only while the gate is enabled and its key
  pressed, as before. get_source_volume() on the audio mix thread stores
  the same two fields, so an unconditional store would be a new race;
- user_push_to_mute_pressed and user_push_to_talk_pressed are written by
  the hotkey thread without atomics, and each is read again for its active
  flag, as before, so a key pressed between the two reads is seen there;
- a stop time is read only when its key is not pressed, and enabled,
  user_muted and the two enabled flags only where the C's || and &&
  reached them: each gate is its own call inside the same || chain.

The arithmetic is the same uint64_t arithmetic (the constant is named
OBS_SOURCE_PTM_GATE_NS_PER_MS, 1000000ULL; the C's 1000000 was converted
to uint64_t in both expressions). get_source_volume() keeps its own
active and muted expressions on push_to_mute_pressed, push_to_talk_pressed
and muted; only its stop time goes through the moved helper.

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#2066
test_source_ptm_gate_steps compiles libobs/obs-source-ptm-gate.c.h into
itself and pins the four moved functions, plus source_muted() and the gate
of get_source_volume() copied from obs-source.c.h onto a stand-in source,
so the composition is pinned too (16 cases):

- the stop time: delays of 300 ms, 1 ms and INT_MAX ms (the settings
  dialog's largest); a delay of 0 at 0, 1, UINT64_MAX - 1 and UINT64_MAX
  (nothing added, nothing saturates);
- the product edge: UINT64_MAX / 1000000 (18446744073709) ms is the last
  delay that fits, one more saturates (it wrapped to 448384 ns before
  obs-rust#2058), and so do INT64_MAX, UINT64_MAX and negative int64 delays
  converted, as the setters still take them;
- the sum edge: that delay from a clock of 551614, 551615 and 551616 ns
  gives UINT64_MAX - 1, exactly UINT64_MAX and the saturated UINT64_MAX;
  1 ms and 300 ms from the end of the clock's range, each +-1 ns;
- the hold, strictly before the stop time (at it the key no longer holds),
  and the two gates' truth tables;
- source_muted() over every combination of enabled, user_muted and each
  gate's enabled flag and key (64 rows), with the stop times it stores;
  disabled and user-muted mute whatever the gates say and still store;
- push-to-talk and push-to-mute released inside the delay (1 ns, half, and
  1 ns before the stop time) and after it (at it, 1 ns and 10 s later);
  each packet while held moves the stop time on, a release does not;
- a delay of 0: the next packet after the release, at the same time, is
  past the stop time;
- saturated delays hold up to UINT64_MAX - 1;
- both gates on one source.

Characterized, not endorsed:
- a saturated stop time does not hold at os_time UINT64_MAX itself;
- a stop time stored before a gate was turned off still holds when it is
  turned back on;
- while held, each packet stores its own time plus the delay, so a packet
  earlier than the last moves the stop time back;
- get_source_volume() (the mix's pressed flags, from the queued audio
  actions) and source_muted() (the hotkey thread's) share the two stop
  times, so either one's store holds the other open; the mix reads
  source->muted, not user_muted.

The expectations were checked against origin/master's push_to_stop_time()
and source_muted() compiled verbatim on the stand-in (scratch, not
committed): all 16 cases pass there too. The same scratch harness ran the
original and the moved source_muted() on 20,000,000 random states (special
and full-range delays, stop times and clocks, every flag) with every field
access logged: the same result, the same fields after, and the same reads
and writes in the same order.

It is in the native test graph, in the core suite, and needs no libm and no
display. It includes only obs-source-ptm-gate.h.

Refs obs-rust#2066
rust/obs-source gets a ptm_gate module, todo!() for now: stop_time(),
held(), mutes() and talk_mutes(), and NS_PER_MS.

The 16 steps_* cases port test_source_ptm_gate_steps 1:1, under the same
names, in the same order, with the same inputs and expectations, the
stand-in source and the copies of source_muted() and of the gate of
get_source_volume() included.

The 9 edge_* cases are new:
- the stop time against u128 arithmetic clamped to u64::MAX, over every
  pair of 46 special clocks and delays (0, 1 ms, 1 s, the slack, the
  product's edge, INT64_MAX, the end of the range, each +-2), and two
  proptests, full range and near the edges; a stop time is never before
  the clock, and a delay of 0 is the clock;
- the hold against the comparison, over the same pairs and full range;
- the gates: when enabled, exactly one of the two mutes; when disabled,
  neither;
- source_muted() against the rule written another way (a match on
  enabled, user_muted and the gates, the stop times stored only while a
  gate's key is held), over every flag with stop times before, at and
  after the clock and saturated, and a proptest over full-range delays,
  stop times and clocks; nothing but the two stop times changes.

The proptests run 20,000 cases each.

soldr cargo test -p obs-data --test obs-tests-core source::ptm_gate fails
25 of 25 with "not yet implemented".

Refs obs-rust#2066
Ports libobs/obs-source-ptm-gate.c.h, the push-to-mute and push-to-talk
gate of source_muted(), to the ptm_gate module of rust/obs-source.
source_muted() and get_source_volume() stay C in obs-source.c.h, with
every read and write of the source's fields in the same order: the
conditional stores of the two stop times, the second read of each pressed
flag (the hotkey thread writes them without atomics) and the
short-circuits that read enabled, user_muted and the enabled flags. The
hybrid libobs drops the C file and links the Rust shims in its place.

Safe core (src/ptm_gate.rs):
- stop_time() is os_time.saturating_add(delay_ms.saturating_mul(NS_PER_MS)),
  the same as the C's two checks: a saturated product is u64::MAX, and
  any clock plus it saturates too; otherwise the sum saturates exactly
  where the C's delay_ns > UINT64_MAX - os_time does;
- held() is os_time < stop_time; mutes() is enabled && active and
  talk_mutes() enabled && !active.
The four are const fns. The module denies clippy's arithmetic, indexing,
as, unwrap, expect, panic, unimplemented and unreachable lints, keeps no
state and takes no lock.

Shims (src/ffi/source_ptm_gate.rs): the four C signatures on uint64_t
and bool, by value. They touch no memory, so there is no intentional
difference from the C.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-ptm-gate (LIBOBS_PORTED_SOURCES), whose PCH is only
obs-source-ptm-gate.h (stdbool.h and stdint.h, for UINT64_MAX);
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.
ci/test_ported_sources.py checks the group on every host.

test_source_ptm_gate_steps still compiles the C file into itself,
unchanged. The new test_source_ptm_gate_steps_rust builds the same test
source against libobs_rust-static instead, as
test_source_color_space_steps_rust does, and links: the test includes only
the header and calls only the four hidden functions. The real-libobs tests
(test_source_audio_ranges, test_wayland_hotkeys, test_portal_shortcuts)
see the swap through the hybrid.

Tier 1 source::ptm_gate passes 25/25.

Refs obs-rust#2066
oracle/source_ptm_gate.c compiles the unmodified
libobs/obs-source-ptm-gate.c.h with its four functions renamed to
oracle_*, plus OBS_SOURCE_PTM_GATE_NS_PER_MS. The C's bool results are
read as their byte, which must be 0 or 1.

source::ptm_gate_parity (9 tests) gives each input to the oracle, to the
shim the hybrid links and to the core:
- the constant against the C header;
- the two gates over all four inputs each;
- the stop time and the hold over every pair of 46 special values (0,
  1 ms, 1 s, a clock, the 551615 ns slack, the largest unwrapped delay
  and its product, u64::MAX / 2, i64::MAX, the end of the range less 1 ms
  and less the slack, u64::MAX, each +-2): 2,116 pairs each; and
  proptests over full-range u64 clocks and delays, and near the edges;
- source_muted() copied onto a stand-in source, run over the oracle's
  functions and over the shims: every combination of its six flags, stop
  times before, at and after the clock and saturated, three pairs of
  delays and three clocks (14,400 runs), and a full-range proptest; the
  result and both stored stop times must match.
The proptests run 20,000 cases each. Tier 1 and the parity module passed
10 runs in a row with fresh seeds; no regression file came out.

Mutation check (once, not committed): 24 mutants, 23 fail, 1 survives and
is equivalent. Failing tests out of 26 (Tier 1) and 9 (Tier 3):

| Mutant | Tier 1 | Tier 3 |
|---|---|---|
| stop time wraps (obs-rust#2058 reverted) | 10 | 5 |
| only the product saturates / only the sum | 10 / 7 | 5 / 5 |
| delay ignored | 18 | 5 |
| NS_PER_MS 1000 | 18 | 6 |
| product edge one early (>= MAX / NS_PER_MS) | 4 | 2 |
| sum edge one early (a sum of MAX - 1 saturates) | 5 | 2 |
| saturates at MAX - 1 | 10 | 5 |
| held <= / reversed / never | 11 / 13 / 12 | 2 / 4 / 4 |
| push-to-mute: || / ignores enabled / as push-to-talk | 9 / 6 / 10 | 3 each |
| push-to-talk: as push-to-mute / ignores enabled / || | 12 / 8 / 14 | 3 each |
| shim: stop time arguments swapped / 1 ms more | 0 | 3 / 3 |
| shim: held arguments swapped / held at the stop time | 0 | 4 / 2 |
| shim: mutes calls talk_mutes / talk_mutes arguments swapped | 0 | 3 / 3 |
| equivalent: the sum edge as >= (at equality the sum is UINT64_MAX) | 0 | 0 |

Tier 1 does not reach the shims. The first run found that NS_PER_MS 1000
failed only 1 Tier 1 test, because the Tier 1 cases took their
millisecond from the port's constant: they now write out 1000000, and a
new edge_ns_per_ms_is_a_millisecond checks the constant and two stop
times (18 fail with it).

There is no intentional difference: the shims touch no memory.

Refs obs-rust#2066
rust/README.md gets the libobs/obs-source-ptm-gate.c.h row: the four
functions and their rules, what stays C in source_muted() and
get_source_volume() (every field read and write, in order: the
conditional stores, the second read of each pressed flag, the
short-circuits; get_source_volume()'s own active and muted expressions),
the saturating stop time, what the characterization pins and the port
keeps, no intentional differences, the _rust variant of the steps test,
the real-libobs test that sees the swap, and what Tier 3 compares. The
crate's row and description and lib.rs say what it holds.
docs/rust-port/naming.md already maps libobs/obs-source*.c.h to
rust/obs-source.

port_comparison gets its own bench file,
benches/port_comparison/source_ptm_gate.rs: 1024 packets 10 ms apart
through source_muted() on a stand-in source (push-to-talk and
push-to-mute pressed, held inside their delays and released, then every
combination of the six flags), and 1024 stop times from valid delays, a
delay of 0 and delays and clocks at and past the saturation edges, the
results folded into a checksum both sides must match. On x86_64 Linux
(GCC, not pinned, a loaded host; median per 1024 calls): source_muted
4.77 us Rust and 4.73 us C (1.01x), stop_time 2.11 us Rust and 2.66 us C
(0.79x): under 1 ns a packet either way.

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

Fixes obs-rust#2066
@rdoupe
rdoupe requested a review from zackees as a code owner October 24, 2026 07:43
@zackees
zackees added this pull request to the merge queue Oct 24, 2026
Merged via the queue into obs-rust:master with commit 4bf93b0 Oct 24, 2026
35 checks passed
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request 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
obs-rust#2089 (their helper) as the fork's changes; the 0.0001 snap, the exact 0
and 1 compares and the due compare date from the 2015 audio subsystem and
move as they are.

Refs obs-rust#2158
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
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
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
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
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request 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
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
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
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
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
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 24, 2026
…#2175)

* libobs: Move the source volume snap and apply decisions into obs-source-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: Characterize the source volume snap and apply decisions

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

* obs-source: Add Tier 1 tests for the source volume snap and apply port

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

* obs-source: Port the source volume snap and apply decisions

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

* obs-source: Check the source volume snap and apply port against the C

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

* docs: Record the source volume snap and apply port

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 push-to-mute/talk gate in obs-source.c to obs-source

2 participants