Skip to content

obs-audio-monitoring: Port WASAPI layout and stall - #3297

Merged
rdoupe merged 8 commits into
masterfrom
fix/rust-port-the-wasapi-monitor-s-s-395
Nov 1, 2026
Merged

rdoupe merged 8 commits into
masterfrom
fix/rust-port-the-wasapi-monitor-s-s-395

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #3292

What and why

Port of the two decisions the WASAPI monitor keeps in shared headers to rust/obs-audio-monitoring:

  • audio_monitor_speaker_layout() (monitor-speaker-layout.h), the speaker layout the monitor resamples to for the device's channel mask and count: the five listed KSAUDIO_SPEAKER_* masks map to 2.1, 4.0, 4.1, 5.1 and 7.1, any other mask takes the layout numbered by the channel count, and the result is SPEAKERS_UNKNOWN unless its channel count is the device's. New module speaker_layout.
  • audio_monitor_track_video_ts() (monitor-video-sync.h), the video stall tracker that feeds audio_monitor_delay_stalled(): the first packet stores the video timestamp, the same timestamp adds the packet's duration, a new one resets the count. Into monitor_delay, beside the stall check.

Commits:

  1. libobs: Move WASAPI speaker layout, stall tracker (behaviour-neutral): both static inline bodies, verbatim, into monitor-speaker-layout.c.h and monitor-video-sync.c.h as hidden functions with the same names, in their own Windows group libobs.monitor-wasapi (in the Windows module of modules.d until GREEN removes it) with no Windows header. The headers keep the comments, the mask defines, the stall threshold and the declarations. wasapi-output.c and its #if check of the masks against ksmedia.h are unchanged. The existing tests (test_monitor_speaker_layout, test_monitor_video_sync, test_media_cover_art_monitor) stay unchanged and build the moved file.
  2. test: Characterize WASAPI speaker layout, stall (test_monitor_wasapi_steps, 10 cases, every platform): every mask with 0 to 9 channels, 7 channels, counts past 8 up to UINT32_MAX, and the tracker's first packet, a 0 video timestamp, a 0 duration, a wrapping count and timestamps that go back.
  3. obs-audio-monitoring: Test layout and stall port (RED).
  4. obs-audio-monitoring: Port layout and stall (GREEN): the cores, the hidden shims (ffi::speaker_layout, ffi::monitor_delay), both files' includes moved into libobs.ported-monitor-delay (its precompiled header now includes monitor-speaker-layout.h; libobs.monitor-wasapi goes away), the rust-hidden.d fragments, ci/test_ported_sources.py, and _rust runs of test_monitor_speaker_layout, test_monitor_video_sync and test_monitor_wasapi_steps against libobs_rust-static. The crate now depends on obs-media-io for get_audio_channels().
  5. obs-audio-monitoring: Compare layout, stall to C (oracle, Tier 3, port_comparison, mutation check).
  6. docs: Record WASAPI speaker layout, stall port.
  7. obs-audio-monitoring: Test aliased stall tracker (RED, from review): with both pointers at one uint64_t, which C allows, the C writes only what its branch writes (0 and a video timestamp of 1 leave 1), but the shim read both values first and wrote both back (leaving 0). aliased_pointers_match_the_c compares oracle and shim that way and failed at its first case.
  8. obs-audio-monitoring: Keep the C's tracker writes (GREEN): the shim reads the stored timestamp, takes the C's branch (the core's new video_ts_step()) and makes only that branch's reads and writes, in its order. All 9 mutants of the new code are killed; zeroing the count before the store is caught only by the new test.

The layout stays the raw enum speaker_layout value (c_int) in the core: the default turns 7 channels into 7, which no layout has, before the channel count check refuses it.

Kept and characterized, not endorsed: a 0 video timestamp is stored as 0, so every packet counts as the first and the video never counts as still; the count wraps at 2^64.

The moved code is upstream's (Lain's WASAPI monitor) and the fork's port of upstream obs-studio PR 14091 by prgmitchell; both are credited in the move commit. No upstream issue.

No intentional difference, and no input the C leaves undefined. Null pointers, which the C dereferences, read as 0 and are not written (wasapi-output.c always passes its fields), checked on the Rust side only.

RED → GREEN

  • RED commit: 0cee2b0. soldr cargo test -p obs-data --test obs-tests-core -- audio_monitoring::speaker_layout audio_monitoring::video_sync passes 1 of 22 (the constants) and fails 21 with "not yet implemented". On the RED commit (runs were on 820807e, before the rebase), local_gate.py --lane lint passed and --test-diff origin/master failed only those 21 (5749 of 5770 passed, 6 skipped).
  • GREEN: Tier 1 audio_monitoring::speaker_layout 10/10 and audio_monitoring::video_sync 12/12; Tier 3 speaker_layout_parity 5/5 and video_sync_parity 4/4. Linux x86_64 (GCC), under a private Xvfb: test_monitor_speaker_layout, test_monitor_video_sync, test_monitor_wasapi_steps, test_audio_monitor_delay_steps, test_media_cover_art_monitor and the four _rust runs pass 9/9 against the hybrid libobs and 9/9 against libobs-c-reference, none skipped; the five C tests pass on the C build. With a fault planted in each Rust core the three new _rust runs fail while the C ones pass. Exported symbols identical on the C, hybrid and C reference builds (1767 each), both shims local (t). clang-cl /W4 /WX with the Windows SDK accepts libobs.ported-monitor-delay and libobs.windows+.
  • Real app (UI, protocol output or logging changes): N/A, no UI, protocol output or logging change.

Move check (runs were on 877027e): origin/master's two static inline bodies, renamed, beside the moved files, every call logged on the same inputs, built with gcc and clang at -O0 and -O2, with and without ASan and UBSan: 100,000 runs over 10 seeds give identical logs in all 8 builds (18,903,128 lines). 19 of 21 planted mutants change the log; the two that don't are equivalent (a channel count above 8 fails the final channel count check whatever the default picks). On the C build, test_monitor_speaker_layout, test_monitor_video_sync, test_audio_monitor_delay_steps (and _rust) and test_media_cover_art_monitor pass, none skipped.

Local gates, after the rebase onto aef81ae (runs were on cc8685d): the quick CI guard tests (build groups, libobs modules, ported sources, oracle stand-ins and MSVC, sorted shared lists, test graph sorted, cmocka Rust-static guard, Windows diagnostics and includes, naming) pass, and the module and build-group checks pass on every commit; ./lint, local_gate.py --lane lint and --lane cross ok; ./unittest Rust tiers 5837/5837 and 896/896 passed, 6 skipped; soldr cargo clippy --workspace --target x86_64-pc-windows-msvc --all-targets --locked -- -D warnings clean; soldr cargo test -p obs-c-oracle --locked --bench port_comparison passes; the Tier 2 tests above pass again natively (hybrid, C reference and C build) and the export sets are again identical (1767 each, both shims local). Not run: Tier 2 in Docker (./unittest's tier2-linux lane), and so the local-gate stamp: this build host can't reach the Docker socket, and ci-lint local-gate run passes lint, rust-test and cross and stops at tier2-linux. The head is unstamped, so PR CI runs the full suite. Nothing here has run on a Windows machine: the Windows Rust tests and Tier 2 in PR CI are the first.

Test cost: new tests (31): longest 0.26 s at the full parity level and 0.03 s at reduced (the channel count sweep), peak RSS 22 MB over 25 runs, no threads of their own.

Mutation check of the port: 24 mutants (19 in the cores, 5 in the shims), 23 killed; table in commit 5, and 9 more of the reworked tracker shim and its branch, all killed, in commit 8. The survivor, SPEAKERS_STEREO for more than 8 channels, can't change the result: the final channel count check refuses it, as in the C. The shim mutants are killed by Tier 3, the tier that reaches the shims.

port_comparison (monitor_wasapi/cases/64, Linux x86_64): Rust 203 ns, C 287 ns for 64 layouts and tracker steps.

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 (not applicable: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports) (none)

rdoupe and others added 8 commits October 31, 2026 22:09
Behaviour-neutral extraction before the Rust port. The two static
inline bodies the WASAPI monitor keeps in shared headers move, verbatim,
into new implementation files as hidden functions with the same names:

- audio_monitor_speaker_layout() from monitor-speaker-layout.h into
  monitor-speaker-layout.c.h;
- audio_monitor_track_video_ts() from monitor-video-sync.h into
  monitor-video-sync.c.h.

The headers keep their comments, the KSAUDIO_SPEAKER_* mask defines,
AUDIO_MONITOR_VIDEO_STALL_NS and now the two declarations, without
EXPORT. wasapi-output.c and its #if check of the masks against
ksmedia.h don't change.

The files have their own group, libobs.monitor-wasapi, in the Windows
sources (the only host whose libobs calls them), with a precompiled
header of the two headers and no windows.h, so the C oracle and the C
tests compile them on their own. test_monitor_speaker_layout and
test_monitor_video_sync build the moved file they test, and
test_media_cover_art_monitor, which links the Linux libobs, builds
monitor-video-sync.c.h; none of the three C tests changes.

Checked in a scratch harness (not committed). It builds origin/master's
two static inline bodies, renamed, beside this branch's moved files
and logs every call of both on the same inputs: every listed mask, 0,
each single bit, the masks with a bit dropped, all bits and random
masks, with 0 to 40 channels, 258, 0x80000000, UINT32_MAX - 1,
UINT32_MAX and random counts; and runs of 1 to 40 packets from a
zeroed tracker, from a random one and from no timestamp with a count,
with video timestamps that are 0, stand still, advance, step back,
sit at UINT64_MAX or are random, and durations of 0, a 1024-frame
packet, near UINT64_MAX (the count wraps) or random. 100,000 runs over
10 seeds in 8 builds (GCC and Clang, -O0 and -O2, with and without
ASan and UBSan without recovery): the logs are identical, 18,903,128
lines in all.

21 broken moves were planted and the harness fails 19: each listed
mask's layout swapped, the default's limit at 6 or below 8, the
default off by one, the final check loosened or dropped, a case
falling through, and in the tracker the first-packet test on the new
timestamp, the count set instead of added, halved, reset to 1 or
reset on the first packet, a new timestamp not stored, a step back
taken as the same and a 0 duration taken as new. The two it passes
cannot change the result: a default limit of 9 and SPEAKERS_STEREO
for more than 8 channels both still fail the final channel count
check. Changing a case label passes too, and is left out: each listed
mask's layout has the mask's own channel count, so the switch agrees
with the default on every input.

The four C tests that reach the code pass on the C build:
test_monitor_speaker_layout, test_monitor_video_sync,
test_audio_monitor_delay_steps (and its _rust run) and
test_media_cover_art_monitor, none skipped.

git blame on the moved lines shows Lain's WASAPI monitor and
prgmitchell's cover art fix (upstream obs-studio PR 14091).

Refs #3292.

Co-authored-by: Lain <[email protected]>
Co-authored-by: prgmitchell <[email protected]>
test_monitor_speaker_layout and test_monitor_video_sync pin what #2559
and #2530 fixed. test_monitor_wasapi_steps pins the rest of the two
moved functions before the port:

- every listed mask, and unlisted ones (none, mono, stereo, quad, 5.1
  and 7.1 with side speakers, 6.1, every bit), with 0 to 9 channels: a
  listed mask gives its layout only for its own channel count, so 5.1
  with 2 channels gives none where an unlisted mask gives stereo;
- 7 channels, whose layout value 7 no layout has, under every mask;
- counts past 8, up to UINT32_MAX, with nothing truncated;
- the tracker's first packet, which stores the timestamp and leaves the
  count; a 0 video timestamp, stored as 0, so every packet is the first
  again and the video never counts as still (characterized, not
  endorsed); the same timestamp with a 0 duration; a count that wraps
  at 2^64 (characterized, not endorsed); earlier timestamps, which are
  new; and the packets still video takes to pass
  AUDIO_MONITOR_VIDEO_STALL_NS.

It builds the moved files on their own, under ASan and UBSan, on every
platform. It passes on the C build.

Refs #3292.
Tier 1 for the WASAPI monitor's speaker layout and video stall tracker,
against todo!() cores: speaker_layout::speaker_layout() (new module)
and monitor_delay::track_video_ts().

- speaker_layout ports test_monitor_speaker_layout.c and the speaker
  layout half of test_monitor_wasapi_steps.c 1:1, and checks the core
  against a model of origin/master's C over every listed mask, each
  with a bit flipped, each single bit, none and every bit, with 0 to 40
  channels and counts up to UINT32_MAX, and a proptest over any mask
  and count;
- video_sync ports test_monitor_video_sync.c and the tracker half of
  test_monitor_wasapi_steps.c 1:1, and checks the core against a model
  of the C's three branches from every start among the edge values,
  and over proptest runs of up to 40 packets from any start.

The layout stays the raw enum speaker_layout value (c_int): the
default turns 7 channels into 7, which no layout has. The crate now
depends on obs-media-io for get_audio_channels().

21 of the 22 cases fail with "not yet implemented"; the constants case
passes.

Refs #3292.
The WASAPI monitor's speaker layout and video stall tracker move to
Rust:

- speaker_layout::speaker_layout(): the five listed masks map to their
  layouts, any other mask to the layout numbered by the channel count
  up to 8, and the result is SPEAKERS_UNKNOWN unless obs-media-io's
  get_audio_channels() of it is the device's count. It stays the raw
  enum speaker_layout value, so 7 channels give 7 before the check
  refuses it;
- monitor_delay::track_video_ts(): the first packet (no timestamp yet)
  stores the video timestamp and leaves the count, the same timestamp
  adds the packet's duration, wrapping, and any other stores it and
  resets the count.

The shims, audio_monitor_speaker_layout() (ffi::speaker_layout) and
audio_monitor_track_video_ts() (ffi::monitor_delay), keep the C
signatures; the layout crosses as c_int. A null pointer, which the C
dereferences, reads as 0 and is not written, as the other monitor
delay shims do. They are hidden: rust-hidden.d gains
obs-audio-monitoring-speaker_layout.txt and the monitor_delay fragment
the tracker.

monitor-speaker-layout.c.h and monitor-video-sync.c.h move into
libobs.ported-monitor-delay, whose precompiled header now includes
monitor-speaker-layout.h too, and their libobs.monitor-wasapi group and
its header go away. ci/test_ported_sources.py checks both are ported on
Windows only.

test_monitor_speaker_layout_rust, test_monitor_video_sync_rust and
test_monitor_wasapi_steps_rust run the three unchanged C tests against
the shims in libobs_rust-static (Linux).

Refs #3292.
Tier 3 for the speaker layout and video stall tracker. The oracles
compile the unchanged C with the functions renamed to oracle_*:
oracle/speaker_layout.c builds monitor-speaker-layout.c.h and gives
its five masks and the enum's size (bound in src/speaker_layout.rs);
oracle/monitor_delay.c now builds monitor-video-sync.c.h too.

tests/audio_monitoring/speaker_layout_parity.rs compares oracle, shim
and core exactly: the constants; every mask the monitor can tell apart
(the listed ones, each with a bit flipped, each single bit, none, every
bit, common unlisted ones) against 0 to 64 channels and +-3 around
255, 2^31 and UINT32_MAX, with every layout but 7 coming up; every mask
of the low 16 bits with 0 to 9 channels; a sweep of 2^20 channel
counts under the listed masks and none; and a proptest of any mask and
count.

tests/audio_monitoring/video_sync_parity.rs compares both values the
tracker writes, after every step: every start, timestamp and duration
among +-3 around 0, a packet, the stall, 2^63 and UINT64_MAX (each
branch taken), and proptest runs of up to 64 packets from any start
with timestamps that are 0, stand still, step back, move on or are
anything. Null pointers, which the C dereferences, are checked on the
Rust side only: each reads as 0 and is not written.

No intentional difference, and no input the C leaves undefined.

port_comparison gets benches/port_comparison/monitor_wasapi.rs: the
layout for 64 masks and counts and the tracker over 64 packets,
folded into a checksum both sides must match. Linux x86_64: Rust
203 ns, C 287 ns. It allocates nothing with bmalloc.

Mutation check (done once, not committed); failing tests per mutant,
at the reduced parity level:

| Mutant | Tier 1 | Tier 3 |
|---|---|---|
| core: 2.1 mask to 4.0 | 3 | 2 |
| core: surround mask to 4.1 | 3 | 2 |
| core: 4.1 mask to 5.1 | 3 | 2 |
| core: 5.1 mask to 4.1 | 3 | 2 |
| core: 7.1 mask to 5.1 | 3 | 2 |
| core: default limit < 8 | 4 | 3 |
| core: default limit <= 6 | 4 | 3 |
| core: default one less | 4 | 3 |
| core: check >= | 5 | 3 |
| core: check dropped | 8 | 4 |
| core: value 7 has 7 channels | 7 | 3 |
| core: unknown is stereo | 0 | 0 |
| core: first-packet test inverted | 11 | 3 |
| core: count saturates | 3 | 2 |
| core: count set, not added | 9 | 2 |
| core: reset to 1 | 4 | 2 |
| core: new timestamp not stored | 6 | 3 |
| core: first packet resets count | 4 | 3 |
| core: earlier timestamp taken as same | 4 | 2 |
| shim: layout arguments swapped | 0 | 3 |
| shim: tracker reads prev as 0 | 0 | 2 |
| shim: count not written back | 0 | 2 |
| shim: prev not written back | 0 | 3 |
| shim: timestamp and duration swapped | 0 | 3 |

23 of 24 are killed. The survivor, SPEAKERS_STEREO for more than 8
channels, cannot change the result: the final channel count check
refuses it, as in the C. The shim mutants are killed by Tier 3, the
tier that reaches the shims.

Refs #3292.
rust/README.md gets rows for monitor-speaker-layout.c.h
(obs-audio-monitoring::speaker_layout) and monitor-video-sync.c.h
(monitor_delay::track_video_ts), the crate's row names both, and the
monitor-delay row no longer calls the tracker a static inline. The
crate's description and lib.rs module docs name the two, and the
monitor_delay module docs and the delay parity test's tracker copy say
where the tracker now lives. naming.md already maps
libobs/audio-monitoring/ to the crate.

Fixes #3292.
The C's audio_monitor_track_video_ts() writes only what its branch
writes: the first packet stores the timestamp and leaves the count; a
new timestamp stores it and then zeroes the count. Called with both
pointers at one uint64_t, which C allows, 0 and a video timestamp of 1
leave 1. The shim reads both values first and writes both back, so it
leaves 0.

aliased_pointers_match_the_c compares the oracle and the shim with
both pointers at one value, over every value and timestamp among the
edges and durations of 0, 1, a packet and UINT64_MAX. It fails at the
first case.

Refs #3292.
The tracker shim now does what the C does through the pointers: it
reads the stored timestamp, takes the C's branch, and makes only that
branch's reads and writes, in its order. The first packet stores the
timestamp; the same one reads the count and adds the duration; a new
one stores the timestamp, then zeroes the count. Both pointers at one
uint64_t now behave as in C, and aliased_pointers_match_the_c passes.

The branch is the core's new video_ts_step() (First, Same, New), which
track_video_ts() uses too. Null pointers still read as 0 and are not
written. rust/README.md says so.

Mutation check of the new code (done once, not committed); failing
tests per mutant, at the reduced parity level:

| Mutant | Tier 1 | Tier 3 |
|---|---|---|
| shim: first packet resets count | 0 | 4 |
| shim: count read as 0 | 0 | 3 |
| shim: count not written | 0 | 3 |
| shim: new timestamp not stored | 0 | 3 |
| shim: count zeroed before the store | 0 | 1 |
| shim: prev read as 0 | 0 | 3 |
| shim: timestamp and duration swapped | 0 | 3 |
| core: step first test inverted | 11 | 4 |
| core: step same as >= | 4 | 3 |

All 9 are killed. Zeroing the count before the store is caught only
by aliased_pointers_match_the_c, the case this fixes.

Refs #3292.
@rdoupe
rdoupe force-pushed the fix/rust-port-the-wasapi-monitor-s-s-395 branch from 820807e to cc8685d Compare November 1, 2026 02:32
@rdoupe
rdoupe marked this pull request as ready for review November 1, 2026 02:49
@rdoupe
rdoupe requested a review from zackees as a code owner November 1, 2026 02:50
@rdoupe
rdoupe added this pull request to the merge queue Nov 1, 2026
Merged via the queue into master with commit 3b9d1a1 Nov 1, 2026
41 checks passed
@rdoupe
rdoupe deleted the fix/rust-port-the-wasapi-monitor-s-s-395 branch November 1, 2026 03:26
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Needs fix to Done in obs-rust Multiview Nov 1, 2026
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 WASAPI monitor's speaker layout and video stall tracker

1 participant