Skip to content

libobs: Saturate push-to-talk stop times and clamp the audio balance - #2058

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/audio-delay-balance-range
Oct 24, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/audio-delay-balance-range

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #2051

What and why

Two per-source audio values were not range-checked (#2051).

Push-to-mute and push-to-talk delays. The delay is in milliseconds, and
the stop time was os_time + delay * 1000000. For a delay above
UINT64_MAX / 1000000 the multiply wraps; just below it the sum with the
clock wraps. Either way the stop time landed just after or before the
release, so push-to-talk closed (or push-to-mute let audio through) the
moment the key was let go. Scene collections store the delays as signed
integers, so a negative one became a huge uint64_t and hit the same wrap.

  • The stop time now saturates at UINT64_MAX, in both places that compute
    it: source_muted() (capture callbacks) and get_source_volume() (the
    mix). One helper, push_to_stop_time(), does it for both.
  • obs_load_source() reads a negative delay as 0. The setters take
    uint64_t, so they keep accepting and returning any value.

Audio balance. obs_source_set_balance_value() and the scene-collection
load accepted any float. Outside 0 to 1 the balance laws give negative
gains, gains above 1 or NaN (the sine law at a balance of 2 inverts the left
channel).

  • The setter clamps to 0 to 1, and NaN becomes the default, 0.5. It clamps
    the value it passes to the audio_balance signal and again after the
    handlers, since the signal's balance is in/out.
  • obs_load_source() clamps the stored double before the float cast, so a
    value beyond float's range no longer goes through an out-of-range
    conversion.

Every value that was valid behaves as before: delays of 0 and 300 ms keep
their stop times, and balances of 0, 0.25, 0.5 and 1 are kept bit for bit
(0.25 still goes through the sine law sample for sample).

What users see: a very large push-to-talk or push-to-mute delay holds the
key for as long as it can instead of ending at release; a negative delay in
a scene collection acts as no delay; a balance outside full left to full
right, from a scene collection, a plugin or a script, acts as full left or
full right (NaN as centred) instead of inverting or overdriving a channel.
The settings dialog (0 to INT_MAX ms), the balance slider and obs-websocket's
SetInputAudioBalance (validates 0 to 1) could not produce these values, so
nothing changes for them.

Write paths checked:

  • setters: obs_source_set_push_to_mute_delay(),
    obs_source_set_push_to_talk_delay(), obs_source_set_balance_value()
    (libobs/obs-source.c.h);
  • scene-collection load: obs_load_source() in libobs/obs.c.h (fixed);
  • frontend (unchanged): OBSBasicSettings spin boxes, range 0 to INT_MAX ms,
    and OBSAdvAudioCtrl's slider, 0 to 100 / 100;
  • obs-websocket (submodule, unchanged): SetInputAudioBalance validates 0 to
    1; there is no request that sets the delays. Its InputAudioBalanceChanged
    event now reports the clamped value.

The balance laws and their Rust port (#2021) are untouched.
test_source_audio_input_steps and the Rust parity tests call the laws
directly, so their "characterized, not endorsed" out-of-range cases still
pin the laws as they are; no characterization went through the setter, and
no test expectation changes. Exported symbols stay at 1766 for C, hybrid and
libobs-c-reference. docs/features.md has the line under Fixed upstream
bugs, Audio; the API reference notes the balance range and that the delays
are in milliseconds.

RED → GREEN

  • RED commit: 1f61aa0 adds
    test_source_audio_ranges (core suite, Linux, registered in
    test-graph.json). It presses the keys through the hotkey router and reads
    the stop time one packet sets, and loads sources through
    obs_load_source(). 6 of 9 fail on C, hybrid and libobs-c-reference alike
    (first run on a178fdcf4, the same commit before a rebase; re-checked on
    1f61aa0 on C): delay_multiply_saturates and delay_sum_saturates (stop
    time just after and just before the press), loaded_negative_delay_is_zero
    (the getter returns UINT64_MAX), loaded_delays_keep_their_value (an
    INT64_MAX delay wraps), setter_clamps_balance and
    loaded_balance_is_clamped (-1 kept as -1). The 3 valid-value tests pass.
  • GREEN: 1be62ee.
    • test_source_audio_ranges 9/9, test_source_audio_balance_mono 4/4,
      test_source_audio_input_steps 22/22 (and _rust 22/22),
      test_audio_controls, test_hotkey_load 7/7, test_wayland_hotkeys 8/8
      and test_portal_shortcuts 32/32, on C, hybrid and libobs-c-reference
      (each rebuilt explicitly). test_source_audio_ranges passed 30 of 30
      repeated runs.
    • Full ctest on 4786038d1 (the same change before the rebase onto
      6891f69), private Xvfb and D-Bus, fresh HOME: C 416/416, hybrid
      424/424, 0 skipped.
    • Local gate on 1be62ee: --lane lint ok, --lane cross ok,
      --test-diff origin/master 3048 passed, 2 skipped.
  • Real app (UI, protocol output or logging changes): N/A. No UI, protocol
    or log change; the dialog and slider can't reach these values.

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)

test_source_audio_ranges presses push-to-mute and push-to-talk through
the hotkey router and reads back the stop time one packet sets. It checks
that a negative delay in a scene collection loads as 0, that delays of
UINT64_MAX / 1000000 + 1 ms (the multiply wraps) and UINT64_MAX / 1000000
ms (the sum wraps) saturate the stop time, and that 0 and 300 ms keep
their behaviour. It sets balances of -1, 2, NaN and +-inf through
obs_source_set_balance_value() and through obs_load_source(), and expects
them clamped to 0 to 1, NaN as 0.5, with the samples as at the clamped
value; 0, 0.25, 0.5 and 1 stay bit for bit.

Refs obs-rust#2051
@rdoupe
rdoupe requested a review from zackees as a code owner October 24, 2026 05:31
The push-to-mute and push-to-talk stop time is os_time plus the delay in
milliseconds times 1000000. Both the multiply and the sum could wrap, so
a large delay ended the moment the key was released. They now saturate at
UINT64_MAX, in source_muted() and get_source_volume() alike.

Scene collections store the delays as signed integers, and a negative one
became a huge uint64_t. obs_load_source() now reads it as 0.

obs_source_set_balance_value() clamps the balance to 0 to 1, before the
audio_balance signal and again after its handlers, and NaN becomes the
default 0.5. obs_load_source() clamps the stored double before the float
cast. Every value that was in range is unchanged, so the balance laws
(and their Rust port) only ever see 0 to 1.

Fixes obs-rust#2051
@rdoupe
rdoupe force-pushed the fix/audio-delay-balance-range branch from 6dd899c to 1be62ee Compare October 24, 2026 05:31
@zackees
zackees added this pull request to the merge queue Oct 24, 2026
Merged via the queue into obs-rust:master with commit d132335 Oct 24, 2026
31 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 24, 2026
* libobs: Move the push-to-mute/talk gate into obs-source-ptm-gate.c

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: Characterize the push-to-mute/talk gate

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

* obs-source: Add Tier 1 tests for the push-to-mute/talk gate port

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

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

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

* obs-source: Check the push-to-mute/talk gate port against the C

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

* docs: Record the push-to-mute/talk gate port

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
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
…e-load.c

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

- obs_source_load_prev_ver(stored): the saved "prev_ver" int64 as the
  uint32_t the source is created with (its low 32 bits, as the cast did);
- obs_source_load_volume(volume): obs-rust#2128's rule, 0 when negative, INFINITY
  past FLT_MAX, then the float cast;
- obs_source_load_balance(balance): obs-rust#2058's rule, 0 to 1, then the cast;
- obs_source_load_delay(delay): obs-rust#2058's rule, a negative push-to-mute or
  push-to-talk delay is none;
- obs_source_load_deinterlace_mode(mode): obs-rust#2120's rule, a mode past the int
  range is OBS_DEINTERLACE_MODE_DISABLE;
- obs_source_load_monitoring_migration(prev_ver, caps, monitoring_type):
  before 23.2.2 a source with OBS_SOURCE_MONITOR_BY_DEFAULT becomes
  monitor-only and gets all six mixers; before 33.0.0 "monitoring_enabled"
  is stored as the (migrated) type not being none. It takes the saved int64
  type and reads it as an int, as the (int) cast did. The answer is a
  struct: whether to set the mixers and to what, and whether to store
  "monitoring_enabled" and its value.

obs.c.h keeps every obs_data_*() call and setter, in the same order and the
same number of calls: it reads each value, passes it in, and hands the
answer to the setter (obs_source_set_audio_mixers() and obs_data_set_bool()
only when the plan says so, as the two ifs did). The source creation, the
canvas lookup, the private settings and the filter recursion stay as they
were.

The constants are copied into the header (MAKE_SEMANTIC_VERSION(23, 2, 2)
and (33, 0, 0), OBS_SOURCE_MONITOR_BY_DEFAULT, the two monitoring types,
the six-mixer mask and OBS_DEINTERLACE_MODE_DISABLE), so it needs only
stdbool.h and stdint.h; obs.c.h checks each against obs-config.h,
obs-source.h, obs.h and MAX_AUDIO_MIXES with _Static_assert. The new file
joins libobs.core, before obs.c.h, with libobs.pch.h, which includes the new
header; source-sets.json lists it. docs/rust-port/naming.md maps it to
rust/obs-core. The exported symbol set is unchanged (1766); the six
functions are local.

Checked in a scratch harness (not committed) that compiles origin/master's
obs_load_source_type() verbatim next to this one and the moved file, on stub
obs_data and source calls that record every call with its arguments
(doubles and floats by bit pattern, so NaN payloads and -0 count): 10,000,000
loads under ASan and UBSan, at -O0 and -O2, with saved values drawn from the
edges (NaN payloads of both signs, +-inf, -0, FLT_MAX and its neighbours,
subnormals, the int and int64 ends, versions at and around 23.2.2 and 33.0.0
and those plus 2^32), random bit patterns, keys present or missing, caps with
and without the cap, scene, group and filter types, and filters loaded
through the recursion: 859,000,000 recorded calls, equal in order and value.
Deliberately broken moves (each comparison of the volume, balance, delay and
deinterlace rules, the prev_ver cast, both version bounds, the migrated type,
the mixer mask, the type cast) all fail it, except a balance bound of
>= 1.0, which is equivalent.

git log -L on obs_load_source_type shows obs-rust#2058 (balance and delays), obs-rust#2120
(deinterlace mode) and obs-rust#2128 (volume) as the fork's changes; each rule and
its comment moves as it is; the volume's
"volume = INFINITY" becomes "volume = (double)INFINITY" (the same value),
since INFINITY is a float and the C oracle builds with -Wdouble-promotion
-Werror.

Refs obs-rust#2143
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
* libobs: Move the source load migration and value rules into obs-source-load.c

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

- obs_source_load_prev_ver(stored): the saved "prev_ver" int64 as the
  uint32_t the source is created with (its low 32 bits, as the cast did);
- obs_source_load_volume(volume): obs-rust#2128's rule, 0 when negative, INFINITY
  past FLT_MAX, then the float cast;
- obs_source_load_balance(balance): obs-rust#2058's rule, 0 to 1, then the cast;
- obs_source_load_delay(delay): obs-rust#2058's rule, a negative push-to-mute or
  push-to-talk delay is none;
- obs_source_load_deinterlace_mode(mode): obs-rust#2120's rule, a mode past the int
  range is OBS_DEINTERLACE_MODE_DISABLE;
- obs_source_load_monitoring_migration(prev_ver, caps, monitoring_type):
  before 23.2.2 a source with OBS_SOURCE_MONITOR_BY_DEFAULT becomes
  monitor-only and gets all six mixers; before 33.0.0 "monitoring_enabled"
  is stored as the (migrated) type not being none. It takes the saved int64
  type and reads it as an int, as the (int) cast did. The answer is a
  struct: whether to set the mixers and to what, and whether to store
  "monitoring_enabled" and its value.

obs.c.h keeps every obs_data_*() call and setter, in the same order and the
same number of calls: it reads each value, passes it in, and hands the
answer to the setter (obs_source_set_audio_mixers() and obs_data_set_bool()
only when the plan says so, as the two ifs did). The source creation, the
canvas lookup, the private settings and the filter recursion stay as they
were.

The constants are copied into the header (MAKE_SEMANTIC_VERSION(23, 2, 2)
and (33, 0, 0), OBS_SOURCE_MONITOR_BY_DEFAULT, the two monitoring types,
the six-mixer mask and OBS_DEINTERLACE_MODE_DISABLE), so it needs only
stdbool.h and stdint.h; obs.c.h checks each against obs-config.h,
obs-source.h, obs.h and MAX_AUDIO_MIXES with _Static_assert. The new file
joins libobs.core, before obs.c.h, with libobs.pch.h, which includes the new
header; source-sets.json lists it. docs/rust-port/naming.md maps it to
rust/obs-core. The exported symbol set is unchanged (1766); the six
functions are local.

Checked in a scratch harness (not committed) that compiles origin/master's
obs_load_source_type() verbatim next to this one and the moved file, on stub
obs_data and source calls that record every call with its arguments
(doubles and floats by bit pattern, so NaN payloads and -0 count): 10,000,000
loads under ASan and UBSan, at -O0 and -O2, with saved values drawn from the
edges (NaN payloads of both signs, +-inf, -0, FLT_MAX and its neighbours,
subnormals, the int and int64 ends, versions at and around 23.2.2 and 33.0.0
and those plus 2^32), random bit patterns, keys present or missing, caps with
and without the cap, scene, group and filter types, and filters loaded
through the recursion: 859,000,000 recorded calls, equal in order and value.
Deliberately broken moves (each comparison of the volume, balance, delay and
deinterlace rules, the prev_ver cast, both version bounds, the migrated type,
the mixer mask, the type cast) all fail it, except a balance bound of
>= 1.0, which is equivalent.

git log -L on obs_load_source_type shows obs-rust#2058 (balance and delays), obs-rust#2120
(deinterlace mode) and obs-rust#2128 (volume) as the fork's changes; each rule and
its comment moves as it is; the volume's
"volume = INFINITY" becomes "volume = (double)INFINITY" (the same value),
since INFINITY is a float and the C oracle builds with -Wdouble-promotion
-Werror.

Refs obs-rust#2143

* test: Characterize the source load migration and value rules

test_source_load_migration_steps compiles libobs/obs-source-load.c.h into
itself (ASan and UBSan) and calls only its six hidden functions, through
obs-source-load.h. 19 cases pin:

- the header's constants: 0x17020002 (23.2.2), 0x21000000 (33.0.0), the
  cap bit 0x800, the two monitoring types, the mixer mask 0x3F and the
  disabled deinterlace mode;
- prev_ver: 0, 1, INT32_MAX and the step past it, and UINT32_MAX as
  themselves; characterized, not endorsed: an int64 past UINT32_MAX keeps
  its low 32 bits (2^32 + 23.2.1 reads as 23.2.1 and migrates, 2^32 as 0),
  and a negative one wraps (-1 is UINT32_MAX);
- the monitoring migration at, just below and just above 23.2.2 and 33.0.0
  (and 22.0.3, 23.2.0, 23.2.1, 32.255.65535 and 34.0.0): below 23.2.2 a
  source with the cap is monitor-only with all six mixers whatever its type;
  below 33.0.0 "monitoring_enabled" is stored as the type not being none; at
  and past 33.0.0 nothing is stored, for every type and cap;
- prev_ver 0 (never saved) migrating both ways and UINT32_MAX neither;
- each monitoring type and values outside the enum (3, -1, INT_MAX, INT_MIN)
  with no cap, the cap, every other bit and every bit; characterized, not
  endorsed: the saved int64 type is read as an int, so 2^32 is none and
  2^32 + 1 monitor-only;
- a cross of 12 versions, 8 cap sets and 11 types against the rule written
  out;
- the volume: in range as the nearest float (ties to even, subnormals, a
  double below half the smallest subnormal as 0), negative values and -inf
  as 0, FLT_MAX and below as themselves, anything past FLT_MAX as INFINITY
  (characterized, not endorsed: also a value float rounding would take back
  to FLT_MAX), NaN of either sign and any payload as NaN; characterized, not
  endorsed: -0.0 stays -0.0f;
- the balance: 0 to 1 as the nearest float (just under 1 rounds to 1), past
  1 and +inf as 1, negative and -inf as 0, NaN as NaN, -0.0 as -0.0f
  (characterized, not endorsed);
- the delays: 0 up to INT64_MAX as themselves, -1 down to INT64_MIN as 0;
- the deinterlace mode: every enum value, 9, -1, INT_MAX and INT_MIN as
  themselves (the setter disables a mode it does not know, obs-rust#2120), and past
  the int range (INT_MAX + 1, INT_MIN - 1, UINT32_MAX, 2^32 + 1, 2^32 + 5,
  the int64 ends) as disabled, not as the low 32 bits.

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 and
test_deinterlace_mode_load cover the caller through the real libobs.

Refs obs-rust#2143

* obs-core: Add Tier 1 tests for the source load migration port

The safe API of obs_core::source_load, with todo!() bodies: the constants
(MONITOR_ONLY_BEFORE and MONITORING_ENABLED_BEFORE through
semantic_version(), MONITOR_BY_DEFAULT, the two monitoring types,
ALL_MIXERS, DEINTERLACE_DISABLE), MonitoringPlan (struct
obs_source_load_monitoring), and prev_ver(), volume(), balance(), delay(),
deinterlace_mode() and monitoring_migration().

tests/obs_core/source_load.rs ports test_source_load_migration_steps.c 1:1:
its 19 cases as steps_*, under the same names, in the same order, with the
same values, the "characterized, not endorsed" ones included. Six new
edge_* proptests (4096 cases each) check each function against its rule
written out independently:
- prev_ver() as the low 32 bits of any int64;
- the migration over any u32 version (weighted to 23.2.2 and 33.0.0 +-2 and
  the ends), any caps with and without the cap bit, and any int64 type;
- volume() and balance() over f64 bit patterns (every NaN payload,
  subnormals, both zeros), f32 bit patterns widened, and the 64 doubles
  either side of FLT_MAX, 1 and 0; the in-range result is checked against
  the nearest float found by a search over the float bits, ties to even,
  not against a cast;
- delay() and deinterlace_mode() over any int64, weighted to the int and
  int64 ends.

soldr cargo test -p obs-data --test obs-tests-core -- obs_core::source_load::
fails 24 of 25 with "not yet implemented"; steps_constants, which reads
only the constants, passes.

Refs obs-rust#2143

* obs-core: Port the source load migration and value rules

Ports libobs/obs-source-load.c.h, the source load migration and value rules
of obs_load_source_type(), to the source_load module of rust/obs-core.
obs.c.h stays C around it: every obs_data_*() call and setter, in the same
order and the same number of calls, the source creation, the canvas lookup,
the private settings and the filter recursion. The hybrid libobs drops the
C file and links the Rust shims in its place.

Safe core (src/source_load.rs):
- prev_ver(): the low 32 bits of the saved int64, as the C's cast;
- volume(): 0 when below 0, infinity when above f32::MAX, else the value;
  then as f32 (round to nearest, ties to even, as the C's cast). The
  comparisons are the C's, so NaN passes both and stays NaN, and -0.0 stays
  -0.0;
- balance(): f64::clamp(0.0, 1.0), which is the C's two comparisons
  (NaN and -0.0 pass through), then as f32;
- delay(): u64::try_from, 0 when negative;
- deinterlace_mode(): i32::try_from, DEINTERLACE_DISABLE when past the int
  range;
- monitoring_migration(): the saved type read as its low 32 bits; before
  23.2.2 with MONITOR_BY_DEFAULT, monitor-only with ALL_MIXERS; before
  33.0.0, store enabled as the type not being none.
The module denies clippy's arithmetic, indexing, unwrap, expect, panic,
todo, unimplemented and unreachable lints, keeps no state and takes no
lock.

Shims (src/ffi/source_load.rs): the six functions keep the C signatures.
struct obs_source_load_monitoring is mirrored #[repr(C)] and returned by
value. No pointer crosses. No intentional differences.

C side: the file leaves libobs.core for its own group,
libobs.ported-source-load (LIBOBS_PORTED_SOURCES), whose PCH is float.h,
limits.h, math.h and obs-source-load.h; libobs.pch.h keeps the header for
obs.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 six 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_load_migration_steps still compiles the C file into itself,
unchanged. It has no _rust variant: linking it against libobs_rust-static
was tried and fails with 404 undefined references, because the shims'
archive member (an obs_core codegen unit) also holds the views and the other
obs-core shims, which pull in obs-util and obs-media-io members that need
libobs (blog, bcrash, the graphics context, GIO). The real-libobs tests
(test_source_audio_ranges, test_source_volume_mix,
test_deinterlace_mode_load and the scene collection loads) see the swap
through the hybrid.

Tier 1 obs_core::source_load passes 25/25.

Refs obs-rust#2143

* obs-core: Check the source load migration port against the C

rust/obs-c-oracle/oracle/source_load.c compiles the unchanged
libobs/obs-source-load.c.h with its six functions renamed oracle_*, plus
its constants and the layout of struct obs_source_load_monitoring.
tests/obs_core/source_load_parity.rs (13 tests) runs the shim, the C and
the safe core on every input and compares the results exactly: the plan
field by field, the version, delay and mode as integers, the volume and
balance bit for bit (so -0.0 is not 0.0). A NaN must come out as a NaN on
every side; its payload and sign are not compared, since Rust leaves them
unspecified for an f64 to f32 cast and both setters map every NaN alike.

- the 7 constants, and the size, alignment and every offset of the struct,
  against the C;
- the migration exhaustively over the cap (each of the 32 caps bits alone,
  none, all, all but the cap) x 30 types (-3 to 5, the int and int64 ends,
  2^32 +-2 either way) x 80 versions (both bounds +-3, every power of two
  and one below, the ends): 84,000 combinations, every branch reached;
  every major.minor version (65,536) with patches 0 to 3 and 0xffff, with
  and without the cap, for each monitoring type and 3 (2,621,440 calls);
  saved int64 versions (those, +-2^32 and negated) through prev_ver() into
  the migration;
- the value rules over their edges (the 64 doubles either side of 0, 1,
  -1 and +-FLT_MAX, subnormals, the half-ulp and tie cases), ten NaN
  classes, every int64 within 64 of 0, the int ends, UINT32_MAX, +-2^32 and
  the int64 ends; and every 4093rd f32 bit pattern widened with its double
  neighbours (3,148,035 doubles, each through volume() and balance());
- proptests (8192 cases each) over any u32 version, caps and int64 type;
  versions within 2^24 of each bound; any int64 version through prev_ver();
  f64 bit patterns; f32 bit patterns widened; and any int64 value.

Tier 1 gains edge_semantic_version(): semantic_version() against
MAKE_SEMANTIC_VERSION() written out for every major, minor and 16-bit
patch.

The parity module passed 10 runs in a row with fresh seeds.
test_source_load_migration_steps passed 19/19 under ASan and UBSan. The
committed regression files hold the seeds proptest saved while mutants
failed; all pass on the port.

Mutation check (not committed): 41 mutants, 32 in the core and 9 in the
shims, each run against Tier 1 (26 tests) and Tier 3 (13). 40 fail; the one
that survives is equivalent (the balance clamped to 1 - f64::EPSILON, which
rounds to 1.0f). Tier 1 does not reach the shims; Tier 3 kills every shim
mutant (the arguments swapped, the struct's fields reordered or swapped, the
volume and balance crossed, the delay, mode and version pre-mangled, the
mixers forced). A semantic_version() that drops the patch's high byte is
killed by Tier 1 only (Tier 3 builds its versions with the same helper).

Refs obs-rust#2143

* docs: Record the source load migration port

rust/README.md gets the obs-core crate row and a port row for
libobs/obs-source-load.c.h: the six hidden functions, what
obs_load_source_type() keeps (every obs_data_*() call and setter in the same
order and number, the source creation, the canvas lookup, the filter
recursion), the C's comparisons kept (NaN through, -0.0 kept, infinity past
FLT_MAX), the 32-bit truncations characterized and not endorsed, why there
is no _rust variant, and what Tier 3 covers. The crate's description and
lib.rs list the module.

port_comparison gets its own bench file, port_comparison/source_load.rs:
1024 loads' worth of saved values (versions either side of 23.2.2 and
33.0.0, with and without the cap, every monitoring type, volumes and
balances in and out of range with a NaN now and then, delays and modes
either side of their ranges) through all six functions, folded into a
checksum both sides must match. On this host (x86_64 Linux, release): Rust
8.92 us, C 14.34 us per 1024 loads; obs_load_source_type() makes these
calls once per source or filter when a scene collection loads, none per
frame.

Fixes obs-rust#2143
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
…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
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
@rdoupe rdoupe mentioned this pull request Oct 27, 2026
6 of 7 tasks
rdoupe added a commit that referenced this pull request Oct 27, 2026
Behaviour-neutral extraction before the Rust port. The decisions of
obs_source_set_flags(), obs_source_set_audio_mixers(),
obs_source_get_audio_mixers(), obs_source_enable_push_to_mute(),
obs_source_enable_push_to_talk(), obs_source_set_monitoring_enabled(),
obs_source_set_monitoring_type(), obs_source_get_monitoring_type(),
clamp_balance() and obs_source_set_audio_active() in obs-source.c.h move
to libobs/obs-source-monitoring.c.h, as thirteen functions on plain
values declared in the new libobs/obs-source-monitoring.h without
EXPORT (hidden, as -fvisibility=hidden keeps them):

- _changes(): a set does something only when the value changes (the
  flags, the mixers, monitoring on or off, and the push-to enables'
  signal);
- _has_audio(): the mixers' gate for a source that does not own its
  info id (OBS_SOURCE_AUDIO in the output flags);
- _mixers_store(): the low 32 bits of what an audio_mixers handler
  leaves;
- _push_to_logs(): the push-to enables log a change on an audio source;
- _on_type(): monitoring on and muted is Monitor Only, else Monitor and
  Output (#2447), for the compatibility signal and the getter;
- _stored_type() and _monitor() -> KEEP | CREATE | DESTROY: the type
  obs_source_set_monitoring_enabled() stores and the monitor route;
- _route() -> OFF | MONITOR_ONLY | MONITOR_AND_OUTPUT | ON: the steps of
  obs_source_set_monitoring_type() for a type (#2444, #2447);
- _off_reads_mute() and _off_unmutes(): none looks at the mute only when
  Monitor Only set it, and undoes it if it is still on;
- _only_mutes(): Monitor Only mutes first if not muted;
- _balance(): clamp_balance(), NaN centred, else clamped to [0, 1]
  (#2051);
- _audio_active() -> NONE | ACTIVATE | DEACTIVATE: the signal of a
  change.

Split per step, as #2153 did, so every call and read stays where it was
and under its condition: obs_source_valid(), audio_mutex,
os_atomic_load_bool() of monitor_only_muted once before the steps and
os_atomic_set_bool() of it and of audio_active, the reads of
owns_info_id (and of the output flags only when it is false),
obs_source_get_output_flags() before the changed test, user_muted read
only when monitoring turns on, monitoring_enabled read again after the
signals, obs_source_muted() only where the C's && reached it (after
monitoring is off, for none), obs_source_set_muted() and
obs_source_set_monitoring_enabled() in their order (the mute before
monitoring turns on), every calldata_*(), signal and log, the (float)
of the handler's balance, audio_monitor_create() and _destroy(), and
the getters' answers for an invalid source (0, false and 0.5).
clamp_balance() stays as an inline wrapper for its two calls. The
monitoring type crosses as int; the header copies OBS_SOURCE_AUDIO and
the three monitoring types, and obs-source.c.h checks them and that
enum obs_monitoring_type is int-sized with _Static_assert.

Checked in a scratch harness (not committed). It compiles
origin/master's fifteen functions (the ten above with
obs_source_get_flags(), obs_source_get_monitoring_enabled(),
obs_source_get_balance_value(), obs_source_set_balance_value() and
obs_source_audio_active()) verbatim next to this branch's and the moved
file, as C++, over a stand-in source whose fields log every read and
write with its value. Every call is logged in order with its arguments:
obs_source_valid(), the lock and unlock, the calldata sets and gets,
each signal, blog(), obs_source_get_output_flags(), the atomics,
obs_source_muted(), obs_source_set_muted(), obs_source_dosignal(), the
push-to signal and audio_monitor_create() and _destroy(). A signal
handler may leave any mixers (above UINT32_MAX, negative, the ends of
int64_t) or any balance, and another thread may write any field at any
event. Inputs: an invalid source or any start state, flags and mixers
at the edges and random, types 0 to 4, -1, the ends of int and random,
balances at NaN (both signs), the signed zeros, the smallest
subnormals, 1.0 and its neighbours, the ends of float, the infinities
and random bit patterns; one to four operations each.

5,800,000 runs: 2,000,000 each with GCC and Clang at -O2, 500,000 each
at -O0, 300,000 each under ASan and UBSan with no recovery (GCC -O0,
Clang -O2, without C++'s enum range check, which C does not have) and
200,000 with gcov. All 82,503,972 logged events match, as does the
final state. Every branch of origin/master's functions is taken both
ways.

30 deliberately broken moves were planted, and all 30 fail it: each
function's rule inverted, widened or made constant (changes as == and
>, the audio bit as bit 0 or any flag, the mixers shifted or saturated,
the log rule without the flag or without the change, the on type
constant or inverted, the stored type Monitor Only, the monitor never
kept or created on the wrong edge, each route moved, the default OFF,
the three mute steps constant, NaN as 0.0, the clamp at <= 0 and below
1.0, the active signal never NONE or on prev), and at the call sites
the mute read before the mark, user_muted read without its log, the
output flags read only after a change, the mixers' gate reading the
output flags first, and the type taken mod 4.

git blame on the moved lines shows the upstream commits d2934ec and
646836a, and the fork's #2058 (the balance clamp) and #2450 (the
Monitor Only steps); they move as they are.

The new file joins libobs.core-source with libobs.pch.h, which includes
the new header; source-sets.json lists it. docs/rust-port/naming.md
already maps libobs/obs-source*.c.h to rust/obs-source. The exported
symbol set is unchanged (1766 on the C backend); the thirteen functions
are local.

Refs #2621.
MCPVOT pushed a commit to MCPVOT/obs-studio that referenced this pull request Oct 30, 2026
* libobs: Move the source monitoring decisions

Behaviour-neutral extraction before the Rust port. The decisions of
obs_source_set_flags(), obs_source_set_audio_mixers(),
obs_source_get_audio_mixers(), obs_source_enable_push_to_mute(),
obs_source_enable_push_to_talk(), obs_source_set_monitoring_enabled(),
obs_source_set_monitoring_type(), obs_source_get_monitoring_type(),
clamp_balance() and obs_source_set_audio_active() in obs-source.c.h move
to libobs/obs-source-monitoring.c.h, as thirteen functions on plain
values declared in the new libobs/obs-source-monitoring.h without
EXPORT (hidden, as -fvisibility=hidden keeps them):

- _changes(): a set does something only when the value changes (the
  flags, the mixers, monitoring on or off, and the push-to enables'
  signal);
- _has_audio(): the mixers' gate for a source that does not own its
  info id (OBS_SOURCE_AUDIO in the output flags);
- _mixers_store(): the low 32 bits of what an audio_mixers handler
  leaves;
- _push_to_logs(): the push-to enables log a change on an audio source;
- _on_type(): monitoring on and muted is Monitor Only, else Monitor and
  Output (obs-rust#2447), for the compatibility signal and the getter;
- _stored_type() and _monitor() -> KEEP | CREATE | DESTROY: the type
  obs_source_set_monitoring_enabled() stores and the monitor route;
- _route() -> OFF | MONITOR_ONLY | MONITOR_AND_OUTPUT | ON: the steps of
  obs_source_set_monitoring_type() for a type (obs-rust#2444, obs-rust#2447);
- _off_reads_mute() and _off_unmutes(): none looks at the mute only when
  Monitor Only set it, and undoes it if it is still on;
- _only_mutes(): Monitor Only mutes first if not muted;
- _balance(): clamp_balance(), NaN centred, else clamped to [0, 1]
  (obs-rust#2051);
- _audio_active() -> NONE | ACTIVATE | DEACTIVATE: the signal of a
  change.

Split per step, as obs-rust#2153 did, so every call and read stays where it was
and under its condition: obs_source_valid(), audio_mutex,
os_atomic_load_bool() of monitor_only_muted once before the steps and
os_atomic_set_bool() of it and of audio_active, the reads of
owns_info_id (and of the output flags only when it is false),
obs_source_get_output_flags() before the changed test, user_muted read
only when monitoring turns on, monitoring_enabled read again after the
signals, obs_source_muted() only where the C's && reached it (after
monitoring is off, for none), obs_source_set_muted() and
obs_source_set_monitoring_enabled() in their order (the mute before
monitoring turns on), every calldata_*(), signal and log, the (float)
of the handler's balance, audio_monitor_create() and _destroy(), and
the getters' answers for an invalid source (0, false and 0.5).
clamp_balance() stays as an inline wrapper for its two calls. The
monitoring type crosses as int; the header copies OBS_SOURCE_AUDIO and
the three monitoring types, and obs-source.c.h checks them and that
enum obs_monitoring_type is int-sized with _Static_assert.

Checked in a scratch harness (not committed). It compiles
origin/master's fifteen functions (the ten above with
obs_source_get_flags(), obs_source_get_monitoring_enabled(),
obs_source_get_balance_value(), obs_source_set_balance_value() and
obs_source_audio_active()) verbatim next to this branch's and the moved
file, as C++, over a stand-in source whose fields log every read and
write with its value. Every call is logged in order with its arguments:
obs_source_valid(), the lock and unlock, the calldata sets and gets,
each signal, blog(), obs_source_get_output_flags(), the atomics,
obs_source_muted(), obs_source_set_muted(), obs_source_dosignal(), the
push-to signal and audio_monitor_create() and _destroy(). A signal
handler may leave any mixers (above UINT32_MAX, negative, the ends of
int64_t) or any balance, and another thread may write any field at any
event. Inputs: an invalid source or any start state, flags and mixers
at the edges and random, types 0 to 4, -1, the ends of int and random,
balances at NaN (both signs), the signed zeros, the smallest
subnormals, 1.0 and its neighbours, the ends of float, the infinities
and random bit patterns; one to four operations each.

5,800,000 runs: 2,000,000 each with GCC and Clang at -O2, 500,000 each
at -O0, 300,000 each under ASan and UBSan with no recovery (GCC -O0,
Clang -O2, without C++'s enum range check, which C does not have) and
200,000 with gcov. All 82,503,972 logged events match, as does the
final state. Every branch of origin/master's functions is taken both
ways.

30 deliberately broken moves were planted, and all 30 fail it: each
function's rule inverted, widened or made constant (changes as == and
>, the audio bit as bit 0 or any flag, the mixers shifted or saturated,
the log rule without the flag or without the change, the on type
constant or inverted, the stored type Monitor Only, the monitor never
kept or created on the wrong edge, each route moved, the default OFF,
the three mute steps constant, NaN as 0.0, the clamp at <= 0 and below
1.0, the active signal never NONE or on prev), and at the call sites
the mute read before the mark, user_muted read without its log, the
output flags read only after a change, the mixers' gate reading the
output flags first, and the type taken mod 4.

git blame on the moved lines shows the upstream commits d2934ec and
646836a, and the fork's obs-rust#2058 (the balance clamp) and obs-rust#2450 (the
Monitor Only steps); they move as they are.

The new file joins libobs.core-source with libobs.pch.h, which includes
the new header; source-sets.json lists it. docs/rust-port/naming.md
already maps libobs/obs-source*.c.h to rust/obs-source. The exported
symbol set is unchanged (1766 on the C backend); the thirteen functions
are local.

Refs obs-rust#2621.

* test: Characterize the source monitoring rules

test_source_monitoring_steps compiles libobs/obs-source-monitoring.c.h
(ASan and UBSan) and calls only its functions through the header. 16
cases:

- the header's copies of OBS_SOURCE_AUDIO and the three monitoring
  types, the enum's size and the monitor, route and active values;
- the same-value rule over pairs of 32-bit edges and the four bools;
- the audio gate over every bit and the mixers' stored value;
- the push-to log rule over flags and changes;
- the on and stored types and the monitor route, every combination;
- the route over -1000..1000 and the ends of int, and the three mute
  steps;
- the balance at NaN (quiet, negative and signalling), the signed
  zeros, the smallest subnormals, 1.0 and its neighbours, the ends of
  float and the infinities;
- the audio active signal, every combination.

obs_source_set_monitoring_type(), _set_monitoring_enabled(),
_get_monitoring_type(), _set_balance_value(), _set_audio_mixers(),
_get_audio_mixers(), _set_flags(), the push-to enables and
_set_audio_active() are also copied onto a stand-in source that records
its signals, logs, mute calls and monitor, to pin end to end: every
type (none, Monitor Only, Monitor and Output, 3 and -1) from every
start state (on or off, muted or not, Monitor Only's mark set or not),
with the steps in order (the mute before monitoring turns on), the
compatibility signal's type and the getter after each; Monitor Only
then none (the mute goes) and with a mute set in between (it stays);
the same value of monitoring on or off doing nothing; the balance sent
and the one a handler leaves (NaN, 2.0, -1.0, 0.75, -0.0); the mixers
of an audio source, a video-only source with and without its own info
id and an invalid source, and a handler's value; the flags; the
push-to enables with the same value (no signal, no log), a change
(log, then signal) and on a video-only source (signal, no log); and
audio active set twice (one signal).

Characterized, not endorsed:
- the mixers a handler leaves are truncated to their low 32 bits:
  2^32 + 3 is 3, -1 is UINT32_MAX;
- a balance of -0.0 stays -0.0;
- Monitor and Output unmutes a mute the user set, while none keeps it.

It is in the native test graph, in name order, with the header as a
watch input and libm off MSVC. No assert_in_range or fail_msg, so it
builds with cmocka 1.x and 2.x, and no two enum types are compared.
The real-libobs tests test_source_monitoring_type,
test_source_monitor_only_load and test_source_audio_balance_mono cover
the callers.

Refs obs-rust#2621.

* obs-source: Test the monitoring port

RED for the port of libobs/obs-source-monitoring.c.h to a new
monitoring module of rust/obs-source, next to ptm_gate and
volume_apply. The safe core is a stub (todo!()): changes(),
has_audio(), mixers_store(), push_to_logs(), on_type(), stored_type(),
monitor() -> Monitor (Keep, Create, Destroy), route() -> Route (Off,
MonitorOnly, MonitorAndOutput, On), off_reads_mute(), off_unmutes(),
only_mutes(), balance() and audio_active() -> Active (None, Activate,
Deactivate), with AUDIO and the three monitoring types.

The plan sketched wider steps: monitoring_enable(cur, enabled, muted)
returning the compatibility type, the stored type and the monitor at
once, monitoring_type_first(type, muted) and
monitoring_type_after_disable(muted_for_monitor_only, muted_now), and
type_of(valid, enabled, muted). Each would take a read the C makes only
under a condition (user_muted only when monitoring turns on,
monitoring_enabled again after the signals, obs_source_muted() only
where its && reaches it) before the C knows it makes it; so the C keeps
those conditions and asks one rule per read. The validity checks and
the getters' literals stay C.

tests/source/monitoring.rs ports test_source_monitoring_steps 1:1: its
16 cases under the same names (steps_*), in the same order, with the
same values and expectations, the characterized-not-endorsed cases and
the stand-in source's copies of the callers included.

Edge cases (edge_*) check the core against models of origin/master's
expressions:
- every decision over every combination of its flags;
- the route over -100,000..100,000, the ends of i32 and every power of
  two with its neighbours;
- the mixers at every power of two of i64 with its neighbours and
  negations, and the audio gate at every bit;
- the balance at every special value (both NaNs, signalling NaNs, the
  signed zeros, the subnormals, 1.0 and its neighbours, the ends and
  the infinities), every 1/4096 from -2.0 to 2.0 and a stride over
  every bit pattern;
- proptests (4,096 cases each): every decision over full-range values,
  and sequences of up to eleven operations (flags, mixers, push-to,
  monitoring on or off, any type, the getters, any balance, audio
  active, a user mute, a handler that leaves any mixers or balance,
  the output flags and the info id) through the stand-in against a
  copy of the callers with origin/master's expressions inline; both
  end in the same state after every step.

soldr cargo test -p obs-data --test obs-tests-core --
source::monitoring passes 1 of 22 (the constants) and fails 21 with
"not yet implemented".

Refs obs-rust#2621.

* obs-source: Port the source monitoring rules

Ports libobs/obs-source-monitoring.c.h, the monitoring, monitor-only
mute, balance, mixer, flag, push-to and audio active rules of
obs-source.c, to the monitoring module of rust/obs-source.
obs-source.c.h stays C around it: obs_source_valid(), audio_mutex, the
atomics on monitor_only_muted and audio_active, every read of the
source's fields at its point (the output flags only when the source does
not own its info id, user_muted only when monitoring turns on,
monitoring_enabled again after the signals),
obs_source_get_output_flags(), obs_source_muted() and
obs_source_set_muted() under the C's own &&, every calldata_*(), signal
and log, the (float) of the handler's balance, audio_monitor_create()
and _destroy(), and the getters' literals. The hybrid libobs drops the C
file and links the Rust shims in its place.

Safe core (src/monitoring.rs), thirteen functions:
- changes(): cur != value;
- has_audio(): AUDIO (1 << 1) in the output flags;
- mixers_store(): the low 32 bits of the i64, by its bytes;
- push_to_logs(): has_audio() && changed;
- on_type(), stored_type(): Monitor Only when muted, else Monitor and
  Output; Monitor and Output when on, else none;
- monitor() -> Monitor: Create on false -> true, Destroy on true ->
  false, else Keep;
- route() -> Route: 0, 1 and 2 to Off, MonitorOnly and
  MonitorAndOutput, any other i32 On;
- off_reads_mute(), off_unmutes(), only_mutes(): the mark, the mute,
  and not muted;
- balance(): NaN is 0.5, then the C's two compares in its order (not
  f32::clamp()), so -0.0 stays -0.0;
- audio_active() -> Active: Activate on false -> true, Deactivate on
  true -> false, else None.
The module denies clippy's arithmetic, indexing, as, float_cmp,
unwrap, expect, panic, todo, unimplemented and unreachable lints,
keeps no state and takes no lock.

Shims (src/ffi/source_monitoring.rs): the thirteen C signatures by
value; the monitoring types as int, the handler's mixers as int64_t,
the balance as float, the monitor, route and active outcomes as the
C's enums (u32). No pointer crosses and no struct is mirrored. No
intentional difference from the C.

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

test_source_monitoring_steps still compiles the C file into itself,
unchanged. The new test_source_monitoring_steps_rust builds the same
test source against libobs_rust-static instead, with libm off MSVC
for nextafterf(), as test_transition_load_steps_rust does, and links:
the test includes the header and obs.h and calls only hidden
functions. It passes 16/16 on the C backend, the hybrid and
libobs-c-reference, as does test_source_monitoring_steps; so do
test_source_monitoring_type, test_source_monitor_only_load,
test_source_audio_balance_mono and the other 80 source, audio,
monitor, mute, push-to and volume tests (three failed once each under
a parallel run and pass alone on both). The exported symbol sets are
unchanged and equal: 1766 on the C backend, the hybrid and
libobs-c-reference; the thirteen functions are local in all three, and
the group's object is built only for libobs-c-reference.

soldr cargo test -p obs-data --test obs-tests-core --
source::monitoring:: passes 22 of 22.

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

Refs obs-rust#2621.

* obs-source: Check monitoring against the C

Tier 3 for the monitoring port. oracle/source_monitoring.c compiles the
unmodified libobs/obs-source-monitoring.c.h with its thirteen functions
renamed to oracle_*, and exports the header's values and sizes by
index (the renames also rename the monitor and route enum tags, which
share their functions' names, so the enums are sized by what the
functions return). tests/source/monitoring_parity.rs gives each input
to the oracle, the shim (the extern "C" function the hybrid links) and
the core, and compares each bool as the C's byte (0 or 1, the same on
both sides), each outcome as the C's enum value, each integer exactly
and the balance by its bits:

- the header's values and the sizes of the three enums, bool, int and
  float;
- every function of flags over every combination of them, and the
  push-to log rule with each flag bit;
- the same-value rule and the audio gate over every pair of 32-bit
  edges and every bit;
- the mixers at every power of two of int64_t with its neighbours and
  negations, the 65,536 values either side of 2^32 and -2^32 and at
  each end, and a stride over int64_t;
- the route over every int from -1,000,000 to 1,000,000, every 65,521st
  int and the ends;
- the balance over every bit pattern whose top byte is 0x00, 0x3f,
  0x7f, 0x80, 0xbf or 0xff (the zeros, the subnormals, 0.5 to 2.0, the
  infinities and every NaN, of both signs: 100,663,296 patterns) and
  every 251st pattern of the rest;
- proptests (20,000 cases) over full-range values.

Nothing is excluded: the C has no undefined or host-dependent result
here (isnan() and the compares are IEEE on every host, and the
(uint32_t) of an int64_t is modular), and the port has no intentional
difference. Every one of the 2^32 balance patterns was also checked
once (131 s in a debug build, too slow to keep).

Mutation check: 34 mutants, all killed. 22 in the core (changes as ==,
the audio bit as bit 0, the mixers saturated or byte-swapped, the log
rule without the flag or without the change, the on type inverted, the
stored type Monitor Only, the monitor never created or created on the
wrong edge, each route moved and the default Off, the three mute steps
constant or inverted, NaN as 0.0, the clamp at <= 0.0 and above 0.999,
the active signal on the wrong edge or never None): Tier 1 and Tier 3
each kill all 22. 12 in the shims (changes inverted, the mixers
shifted, the log rule always changed, the on type inverted, the
monitor's and the active signal's arguments swapped, the type taken
mod 4, the balance's absolute value, Create as Destroy, On as Off,
Activate as None, and off_unmutes() answered by the wrong rule): Tier 3
kills all 12 (Tier 1 calls only the core).

Refs obs-rust#2621.

* docs: Record the source monitoring port

rust/README.md gets the row for libobs/obs-source-monitoring.c.h
(obs-source::monitoring), in sorted place: what moved, what stays C
and why (every read and call at its point and under its condition, the
C's own && before each mute read, the atomics, the signals, the
monitor and the getters' literals), the characterized-not-endorsed
behaviours, the tests that see the swap and what Tier 3 covers. The
obs-source crate row, its Cargo description and the lib.rs crate docs
name the new module. docs/rust-port/naming.md already maps
libobs/obs-source*.c.h to rust/obs-source.

rust/obs-c-oracle/benches/port_comparison/source_monitoring.rs runs
1024 updates over every combination of the flags the C reads, with
monitoring types 0, 1, 2, 3 and -1, handler mixers of 0x3f, -1 and
past 32 bits, and balances at NaN, -0.0, 0.25, 0.5, 1.0, 2.0 and -1.0,
each through the same-value rule, the audio gate, the stored mixers,
the push-to log rule, the monitoring types and monitor route, the
type's route and its three mute steps, the balance twice and the
active signal, on the shims and on the oracle, and checks both
checksums match. Release, x86_64 Linux, GCC, a loaded host, median per
1024 updates: Rust 17.74 us, C 18.05 us (0.98x), about 17 ns an update.

The port changes nothing a user can see, so docs/features.md has no
line.

Fixes obs-rust#2621.
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.

bug: libobs: push-to-talk delays can wrap and the audio balance isn't range-checked

2 participants