From 7961a50f913755d5c46511ad26590a236689f9e9 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Thu, 19 Nov 2026 14:44:12 -0500 Subject: [PATCH 1/2] test: Mirror the null audio-monitoring characterization over a stub (RED, Refs #5269) Tier 1 ports every assertion of test_null_audio_monitoring.c to the safe core of the null backend, which is todo!() here. Co-Authored-By: Claude Opus 5.5 --- rust/obs-audio-monitoring/src/lib.rs | 1 + .../src/null_monitoring.rs | 55 ++++++++++++ .../tests/audio_monitoring/main.rs | 1 + .../tests/audio_monitoring/null_monitoring.rs | 90 +++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 rust/obs-audio-monitoring/src/null_monitoring.rs create mode 100644 rust/obs-audio-monitoring/tests/audio_monitoring/null_monitoring.rs diff --git a/rust/obs-audio-monitoring/src/lib.rs b/rust/obs-audio-monitoring/src/lib.rs index 89b48d1409..c212cdb679 100644 --- a/rust/obs-audio-monitoring/src/lib.rs +++ b/rust/obs-audio-monitoring/src/lib.rs @@ -22,6 +22,7 @@ pub mod coreaudio_devices; pub mod coreaudio_pacing; pub mod monitor_delay; +pub mod null_monitoring; pub mod pulse_format; pub mod pulse_monitor; pub mod pulse_stream_flags; diff --git a/rust/obs-audio-monitoring/src/null_monitoring.rs b/rust/obs-audio-monitoring/src/null_monitoring.rs new file mode 100644 index 0000000000..57a15c1d1e --- /dev/null +++ b/rust/obs-audio-monitoring/src/null_monitoring.rs @@ -0,0 +1,55 @@ +//! Safe core of `libobs/audio-monitoring/null/null-audio-monitoring.c.h`, the +//! audio monitoring backend Linux and FreeBSD select when PulseAudio is off. +//! +//! The backend has no output device: monitoring is unavailable, the device +//! list is empty, no monitor is ever created, and no two device IDs match. +//! None of its six functions reads, writes, keeps or frees what the caller +//! passes, logs or allocates; every argument here is generic and dropped +//! unread, so the C ABI shims ([`crate::ffi::null_monitoring`]) pass their +//! pointers through without dereferencing them. + +/// A monitor of the null backend. It has no value: [`create`] never returns +/// one, so [`reset`] and [`destroy`] never get one either. +#[derive(Debug)] +pub enum Monitor {} + +/// `obs_audio_monitoring_available()`: always false. +#[must_use] +pub fn available() -> bool { + todo!("null monitoring: available") +} + +/// `obs_enum_audio_monitoring_devices()`: lists no device, so `visit` (the +/// callback and its data) is never called or touched. +pub fn enum_devices(visit: V) { + let _ = visit; + todo!("null monitoring: enum_devices") +} + +/// `audio_monitor_create()`: no monitor for any source, NULL or not. +#[must_use] +pub fn create(source: S) -> Option { + let _ = source; + todo!("null monitoring: create") +} + +/// `audio_monitor_reset()`: does nothing with any handle, NULL or not. +pub fn reset(monitor: M) { + let _ = monitor; + todo!("null monitoring: reset") +} + +/// `audio_monitor_destroy()`: does nothing with any handle, NULL or not; it +/// owns nothing to free. +pub fn destroy(monitor: M) { + let _ = monitor; + todo!("null monitoring: destroy") +} + +/// `devices_match()`: false for every pair of IDs, NULL, equal or not. The +/// IDs are never read, so they need not be terminated. +#[must_use] +pub fn devices_match(id1: I, id2: J) -> bool { + let _ = (id1, id2); + todo!("null monitoring: devices_match") +} diff --git a/rust/obs-audio-monitoring/tests/audio_monitoring/main.rs b/rust/obs-audio-monitoring/tests/audio_monitoring/main.rs index 83376e3d39..b7a7cbe1e4 100644 --- a/rust/obs-audio-monitoring/tests/audio_monitoring/main.rs +++ b/rust/obs-audio-monitoring/tests/audio_monitoring/main.rs @@ -7,6 +7,7 @@ mod coreaudio_pacing; mod coreaudio_pacing_parity; mod monitor_delay; mod monitor_delay_parity; +mod null_monitoring; mod pulse_backlog; mod pulse_flags; mod pulse_format; diff --git a/rust/obs-audio-monitoring/tests/audio_monitoring/null_monitoring.rs b/rust/obs-audio-monitoring/tests/audio_monitoring/null_monitoring.rs new file mode 100644 index 0000000000..30d2172398 --- /dev/null +++ b/rust/obs-audio-monitoring/tests/audio_monitoring/null_monitoring.rs @@ -0,0 +1,90 @@ +//! Tier 1: `obs_audio_monitoring::null_monitoring`, the audio monitoring +//! backend Linux and FreeBSD select when PulseAudio is off. +//! +//! Ports every assertion of `test/cmocka/test_null_audio_monitoring.c`, 1:1, +//! under the same names and in the same order, against the safe core. The C +//! test's teardown also checks that no case logs or allocates; the core has +//! no path that does either, and the unchanged C test checks it against the +//! Rust through `test_null_audio_monitoring_rust` (bmem and the log handler +//! are process-wide, so a parallel Rust test binary cannot count them). + +use std::ptr; + +use obs_audio_monitoring::null_monitoring::{ + available, create, destroy, devices_match, enum_devices, reset, +}; + +/// Bytes that are not an OBS object, as the C test's `storage` union. +fn storage() -> [u8; 64] { + [0xa5; 64] +} + +#[test] +fn availability_is_false_without_startup() { + assert!(!available()); +} + +#[test] +fn enumeration_never_calls_back_or_writes_data() { + let mut data = [0xa5u8, 0x00, 0xff, 0x5a]; + let saved = data; + // No callback, even one that would stop enumeration, may be invoked. + let unexpected = |_: &mut [u8; 4], _: &[u8], _: &[u8]| -> bool { + panic!("the null backend called a device callback") + }; + enum_devices((Some(unexpected), Some(&mut data))); + enum_devices((Some(unexpected), None::<&mut [u8; 4]>)); + enum_devices((None:: bool>, Some(&mut data))); + enum_devices(( + None:: bool>, + None::<&mut [u8; 4]>, + )); + assert_eq!(data, saved); +} + +#[test] +fn lifecycle_ignores_null_and_nonnull_handles() { + // The null backend never dereferences, initializes, takes ownership of, + // or frees the caller's pointer. + let mut storage = storage(); + let saved = storage; + let handle: *mut u8 = storage.as_mut_ptr(); + assert!(create(ptr::null_mut::()).is_none()); + assert!(create(handle).is_none()); + reset(ptr::null_mut::()); + reset(handle); + destroy(ptr::null_mut::()); + destroy(handle); + // Repeat to characterize the absence of ownership or state changes. + reset(handle); + destroy(handle); + assert!(create(handle).is_none()); + assert_eq!(storage, saved); +} + +#[test] +fn device_matching_is_always_false() { + let equal1 = *b"same\0"; + let equal2 = *b"same\0"; + let bytes = [0xffu8, 0x80, b'x', 0]; + let unterminated = *b"xy"; + let ids: [Option<&[u8]>; 8] = [ + None, + Some(b"\0"), + Some(b"default\0"), + Some(&equal1), + Some(&equal2), + Some(b"different\0"), + Some(&bytes), + Some(&unterminated), + ]; + for i in ids { + for j in ids { + assert!(!devices_match(i, j), "{i:?} {j:?}"); + } + } + assert_eq!(&equal1, b"same\0"); + assert_eq!(&equal2, b"same\0"); + assert_eq!(bytes, [0xff, 0x80, b'x', 0]); + assert_eq!(&unterminated, b"xy"); +} From fe84adf8e655625ee825c4fc7b96ce01d2132b91 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Thu, 19 Nov 2026 14:51:45 -0500 Subject: [PATCH 2/2] obs-audio-monitoring: Port the null audio-monitoring backend to Rust (GREEN, Refs #5269) The six canonical functions of null-audio-monitoring.c stay thin C forwarders in the selected libobs.null-audio group, so no other backend sees the names; they call six hidden adapters (null-audio-monitoring.h), Rust in hybrid libobs and the original C in the C reference group libobs.ported-null-audio-monitoring. The unchanged characterization is relinked against the Rust archive; Tier 3 compares the original C. Co-Authored-By: Claude Opus 5.5 Ci-Attestation: {"at":1795118585,"gate":"data/all/compatibility","host":"linux-x86_64","key":"","lane":"client-checks","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":94,"stamp":"04d3438467e627b991d8ae4ea38301ce","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118585,"gate":"data/all/flatpak-manifest","host":"linux-x86_64","key":"","lane":"client-checks","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":94,"stamp":"5429750b315068f8956eb1f591b43462","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118585,"gate":"data/all/qt-xml","host":"linux-x86_64","key":"","lane":"client-checks","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":94,"stamp":"9e9c7a3590d16a7435951f370789fba8","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118585,"gate":"data/all/services-schema","host":"linux-x86_64","key":"","lane":"client-checks","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":94,"stamp":"cbaf38e9a523344e288a8451b37a5289","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118489,"gate":"general/all/client-lint","host":"linux-x86_64","key":"bf140ca6bc993aca4242631716fa6008420da94313b0b85ce14d267f3e7394e8","lane":"client-lint","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":77,"stamp":"ad53031b7d430c09fc74cb70fd4f3baa","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118585,"gate":"python/all/self-test","host":"linux-x86_64","key":"","lane":"client-checks","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":94,"stamp":"15982da2ca6dd4abf6127f71c7eeec64","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118490,"gate":"rust/aarch64-apple-darwin/client-clippy","host":"linux-x86_64","key":"bf140ca6bc993aca4242631716fa6008420da94313b0b85ce14d267f3e7394e8","lane":"client-lint","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":77,"stamp":"8b18a8e62147e9dd2e91c9e43e652a15","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118490,"gate":"rust/aarch64-apple-darwin/client-dylint","host":"linux-x86_64","key":"bf140ca6bc993aca4242631716fa6008420da94313b0b85ce14d267f3e7394e8","lane":"client-lint","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":77,"stamp":"fee2cf12e08ac7e1e146d3c0f0b4886c","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118489,"gate":"rust/aarch64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"bf140ca6bc993aca4242631716fa6008420da94313b0b85ce14d267f3e7394e8","lane":"client-lint","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":77,"stamp":"f4ffe9a1fa9db69b05a74a621d5d10e9","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118489,"gate":"rust/aarch64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"bf140ca6bc993aca4242631716fa6008420da94313b0b85ce14d267f3e7394e8","lane":"client-lint","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":77,"stamp":"ef44afa257c359aa6433f9abc212f23c","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118489,"gate":"rust/aarch64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"bf140ca6bc993aca4242631716fa6008420da94313b0b85ce14d267f3e7394e8","lane":"client-lint","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":77,"stamp":"553ac59d156c1c0262997626d7bc309c","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118489,"gate":"rust/aarch64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"bf140ca6bc993aca4242631716fa6008420da94313b0b85ce14d267f3e7394e8","lane":"client-lint","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":77,"stamp":"1e2e4fd21a8d8e32c8b7fb179a304323","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118489,"gate":"rust/x86_64-apple-darwin/client-clippy","host":"linux-x86_64","key":"bf140ca6bc993aca4242631716fa6008420da94313b0b85ce14d267f3e7394e8","lane":"client-lint","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":77,"stamp":"dff74604a59f03329f733889b50f9d04","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118489,"gate":"rust/x86_64-apple-darwin/client-dylint","host":"linux-x86_64","key":"bf140ca6bc993aca4242631716fa6008420da94313b0b85ce14d267f3e7394e8","lane":"client-lint","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":77,"stamp":"cd9a9dd548d80ed4d77258aac5a7db89","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118489,"gate":"rust/x86_64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"bf140ca6bc993aca4242631716fa6008420da94313b0b85ce14d267f3e7394e8","lane":"client-lint","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":77,"stamp":"9d6997d33e50f35b23dceccfe9948f4c","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118489,"gate":"rust/x86_64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"bf140ca6bc993aca4242631716fa6008420da94313b0b85ce14d267f3e7394e8","lane":"client-lint","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":77,"stamp":"41605132cb93b2a80653c2513e19202f","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118489,"gate":"rust/x86_64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"bf140ca6bc993aca4242631716fa6008420da94313b0b85ce14d267f3e7394e8","lane":"client-lint","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":77,"stamp":"744a728919cb333bc2c5cd063d0173fe","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118489,"gate":"rust/x86_64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"bf140ca6bc993aca4242631716fa6008420da94313b0b85ce14d267f3e7394e8","lane":"client-lint","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":77,"stamp":"e0115a18a82f75bcbce7280fa84d7f8d","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Ci-Attestation: {"at":1795118812,"gate":"rust/x86_64-unknown-linux-gnu/client-test","host":"linux-x86_64","key":"85e26f106665f70b82c27d4e4842446d2174c1dc1a831a3446346edf7ba37c45","lane":"client-test-x86_64-unknown-linux-gnu","parents":["7961a50f913755d5c46511ad26590a236689f9e9"],"secs":226,"stamp":"cfe1610db666618ab2a8b4b5935afa39","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899","v":1,"via":"run"} Client-Checks: {"checks":{"base":"7d19e183b72303c24dfdb5569f032d1c21756cab","coverage":"all-tracked-applicable-files","diff":["libobs/audio-monitoring/null/null-audio-monitoring-reference.c.h","libobs/audio-monitoring/null/null-audio-monitoring.c.h","libobs/audio-monitoring/null/null-audio-monitoring.h","libobs/build/libobs-nix.pch.h","libobs/build/libobs-ported-null-audio-monitoring.pch.h","libobs/build/libobs.ported-null-audio-monitoring+.h.c","libobs/build/ported-source-sets/nix-null-audio-monitoring.json","libobs/cmake/rust-hidden.d/obs-audio-monitoring-null_monitoring.txt","rust/README.md","rust/obs-audio-monitoring/Cargo.toml","rust/obs-audio-monitoring/src/ffi/mod.rs","rust/obs-audio-monitoring/src/ffi/null_monitoring.rs","rust/obs-audio-monitoring/src/lib.rs","rust/obs-audio-monitoring/src/null_monitoring.rs","rust/obs-audio-monitoring/tests/audio_monitoring/main.rs","rust/obs-audio-monitoring/tests/audio_monitoring/null_monitoring.rs","rust/obs-audio-monitoring/tests/audio_monitoring/null_monitoring_parity.rs","rust/obs-build/tests/native/registration/rust-static/cases/core-relinked/test_null_audio_monitoring.json","rust/obs-c-oracle/oracle/null_monitoring.c","rust/obs-c-oracle/src/lib.rs","rust/obs-c-oracle/src/null_monitoring.rs","test/cmocka/build/test-graph.json"],"head":"0f35857beec13ceb477bad81e6ba349500063626","tools":{"orchestrator-python":"3.13.16 (main, Oct 14 2026, 01:04:01) [Clang 23.1.3 ]","schema-dependencies":"3.13.16 (main, Oct 14 2026, 01:04:01) [Clang 23.1.3 ]\n{'jsonschema': '4.26.0', 'json_source_map': '1.0.5'}","uv":"uv 0.13.5 (x86_64-unknown-linux-gnu)","xmllint":"xmllint: using libxml version 21504-GITv2.15.4\n compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 DTDValid HTML C14N Catalog XPath XPointer XInclude Iconv ICU ISO8859X Regexps Automata RelaxNG Schemas Schematron Modules Debug Zlib"},"tracked-digest":"d3bf2cd385214723465a6400857580c279c762b53bd2e13a04e228180853920b","tree":"2229ab36cc9d10270830d70e63d5b58883fb1899"}} --- .../null/null-audio-monitoring-reference.c.h | 39 ++++ .../null/null-audio-monitoring.c.h | 19 +- .../null/null-audio-monitoring.h | 24 +++ libobs/build/libobs-nix.pch.h | 3 + .../libobs-ported-null-audio-monitoring.pch.h | 12 ++ .../libobs.ported-null-audio-monitoring+.h.c | 7 + .../nix-null-audio-monitoring.json | 8 + .../obs-audio-monitoring-null_monitoring.txt | 8 + rust/README.md | 1 + rust/obs-audio-monitoring/Cargo.toml | 2 +- rust/obs-audio-monitoring/src/ffi/mod.rs | 1 + .../src/ffi/null_monitoring.rs | 73 +++++++ rust/obs-audio-monitoring/src/lib.rs | 6 +- .../src/null_monitoring.rs | 21 +- .../tests/audio_monitoring/main.rs | 1 + .../tests/audio_monitoring/null_monitoring.rs | 5 +- .../null_monitoring_parity.rs | 183 ++++++++++++++++++ .../test_null_audio_monitoring.json | 60 ++++++ rust/obs-c-oracle/oracle/null_monitoring.c | 12 ++ rust/obs-c-oracle/src/lib.rs | 1 + rust/obs-c-oracle/src/null_monitoring.rs | 21 ++ test/cmocka/build/test-graph.json | 2 + 22 files changed, 483 insertions(+), 26 deletions(-) create mode 100644 libobs/audio-monitoring/null/null-audio-monitoring-reference.c.h create mode 100644 libobs/audio-monitoring/null/null-audio-monitoring.h create mode 100644 libobs/build/libobs-ported-null-audio-monitoring.pch.h create mode 100644 libobs/build/libobs.ported-null-audio-monitoring+.h.c create mode 100644 libobs/build/ported-source-sets/nix-null-audio-monitoring.json create mode 100644 libobs/cmake/rust-hidden.d/obs-audio-monitoring-null_monitoring.txt create mode 100644 rust/obs-audio-monitoring/src/ffi/null_monitoring.rs create mode 100644 rust/obs-audio-monitoring/tests/audio_monitoring/null_monitoring_parity.rs create mode 100644 rust/obs-build/tests/native/registration/rust-static/cases/core-relinked/test_null_audio_monitoring.json create mode 100644 rust/obs-c-oracle/oracle/null_monitoring.c create mode 100644 rust/obs-c-oracle/src/null_monitoring.rs diff --git a/libobs/audio-monitoring/null/null-audio-monitoring-reference.c.h b/libobs/audio-monitoring/null/null-audio-monitoring-reference.c.h new file mode 100644 index 0000000000..5b48e320c3 --- /dev/null +++ b/libobs/audio-monitoring/null/null-audio-monitoring-reference.c.h @@ -0,0 +1,39 @@ +#include "libobs/build/libobs-ported-null-audio-monitoring.pch.h" + +/* The original null backend, for the C reference and the Tier 3 oracle. Its + * bodies are the C's, verbatim, under the private adapter names + * (null-audio-monitoring.h) the canonical forwarders call. */ + +bool obs_null_audio_monitoring_available(void) +{ + return false; +} + +void obs_null_audio_monitoring_enum_devices(bool (*cb)(void *data, const char *name, const char *id), void *data) +{ + UNUSED_PARAMETER(cb); + UNUSED_PARAMETER(data); +} + +struct audio_monitor *obs_null_audio_monitor_create(struct obs_source *source) +{ + UNUSED_PARAMETER(source); + return NULL; +} + +void obs_null_audio_monitor_reset(struct audio_monitor *monitor) +{ + UNUSED_PARAMETER(monitor); +} + +void obs_null_audio_monitor_destroy(struct audio_monitor *monitor) +{ + UNUSED_PARAMETER(monitor); +} + +bool obs_null_audio_monitoring_devices_match(const char *id1, const char *id2) +{ + UNUSED_PARAMETER(id1); + UNUSED_PARAMETER(id2); + return false; +} diff --git a/libobs/audio-monitoring/null/null-audio-monitoring.c.h b/libobs/audio-monitoring/null/null-audio-monitoring.c.h index 8060439aac..0fb89475f3 100644 --- a/libobs/audio-monitoring/null/null-audio-monitoring.c.h +++ b/libobs/audio-monitoring/null/null-audio-monitoring.c.h @@ -4,36 +4,35 @@ * header. The null backend has no wrapper header, but needs the same prototype. */ bool devices_match(const char *id1, const char *id2); +/* The canonical backend functions, defined only where this backend is + * selected. Each forwards to its private adapter (null-audio-monitoring.h). */ + bool obs_audio_monitoring_available(void) { - return false; + return obs_null_audio_monitoring_available(); } void obs_enum_audio_monitoring_devices(obs_enum_audio_device_cb cb, void *data) { - UNUSED_PARAMETER(cb); - UNUSED_PARAMETER(data); + obs_null_audio_monitoring_enum_devices(cb, data); } struct audio_monitor *audio_monitor_create(obs_source_t *source) { - UNUSED_PARAMETER(source); - return NULL; + return obs_null_audio_monitor_create(source); } void audio_monitor_reset(struct audio_monitor *monitor) { - UNUSED_PARAMETER(monitor); + obs_null_audio_monitor_reset(monitor); } void audio_monitor_destroy(struct audio_monitor *monitor) { - UNUSED_PARAMETER(monitor); + obs_null_audio_monitor_destroy(monitor); } bool devices_match(const char *id1, const char *id2) { - UNUSED_PARAMETER(id1); - UNUSED_PARAMETER(id2); - return false; + return obs_null_audio_monitoring_devices_match(id1, id2); } diff --git a/libobs/audio-monitoring/null/null-audio-monitoring.h b/libobs/audio-monitoring/null/null-audio-monitoring.h new file mode 100644 index 0000000000..bd91b53b00 --- /dev/null +++ b/libobs/audio-monitoring/null/null-audio-monitoring.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +struct audio_monitor; +struct obs_source; + +/* The null audio monitoring backend (null-audio-monitoring.c), a private + * adapter, not a new public API. It is Rust in hybrid libobs + * (obs-audio-monitoring::null_monitoring) and the original C in the C + * reference (null-audio-monitoring-reference.c). Hidden, not EXPORT. + * + * The canonical backend functions, which only the selected null backend + * defines, forward to these uniquely named ones, so every build can link them + * without clashing with another backend. None reads, writes, keeps or frees + * what it is passed, logs or allocates: monitoring is unavailable, no device + * is listed and no callback made, no monitor is created, reset and destroy do + * nothing, and no two device IDs match. */ +bool obs_null_audio_monitoring_available(void); +void obs_null_audio_monitoring_enum_devices(bool (*cb)(void *data, const char *name, const char *id), void *data); +struct audio_monitor *obs_null_audio_monitor_create(struct obs_source *source); +void obs_null_audio_monitor_reset(struct audio_monitor *monitor); +void obs_null_audio_monitor_destroy(struct audio_monitor *monitor); +bool obs_null_audio_monitoring_devices_match(const char *id1, const char *id2); diff --git a/libobs/build/libobs-nix.pch.h b/libobs/build/libobs-nix.pch.h index d7d7fc83ef..484a757226 100644 --- a/libobs/build/libobs-nix.pch.h +++ b/libobs/build/libobs-nix.pch.h @@ -62,3 +62,6 @@ /* access() in log_system_info; came with util/platform-nix-inhibit.c, * which is in its own group now. */ #include + +/* audio-monitoring/null/null-audio-monitoring.c */ +#include "audio-monitoring/null/null-audio-monitoring.h" diff --git a/libobs/build/libobs-ported-null-audio-monitoring.pch.h b/libobs/build/libobs-ported-null-audio-monitoring.pch.h new file mode 100644 index 0000000000..871d44cfe3 --- /dev/null +++ b/libobs/build/libobs-ported-null-audio-monitoring.pch.h @@ -0,0 +1,12 @@ +/* Precompiled header (BUILD.md) for the libobs unity group + * libobs.ported-null-audio-monitoring. null-audio-monitoring-reference.c needs + * only its adapter declarations, NULL and UNUSED_PARAMETER, not the core, so + * the C oracle and the C tests can compile it on its own. The implementation + * file includes this header and nothing else. */ + +#pragma once + +#include + +#include "libobs/audio-monitoring/null/null-audio-monitoring.h" +#include "libobs/util/c99defs.h" diff --git a/libobs/build/libobs.ported-null-audio-monitoring+.h.c b/libobs/build/libobs.ported-null-audio-monitoring+.h.c new file mode 100644 index 0000000000..d1d7f5aa8b --- /dev/null +++ b/libobs/build/libobs.ported-null-audio-monitoring+.h.c @@ -0,0 +1,7 @@ +/* Unity group (BUILD.md): the original null audio monitoring backend + * (audio-monitoring/null/null-audio-monitoring.c), for the C reference on + * Linux and FreeBSD. */ + +#include "libobs/build/libobs-ported-null-audio-monitoring.pch.h" + +#include "libobs/audio-monitoring/null/null-audio-monitoring-reference.c.h" diff --git a/libobs/build/ported-source-sets/nix-null-audio-monitoring.json b/libobs/build/ported-source-sets/nix-null-audio-monitoring.json new file mode 100644 index 0000000000..3d29a39472 --- /dev/null +++ b/libobs/build/ported-source-sets/nix-null-audio-monitoring.json @@ -0,0 +1,8 @@ +{ + "LIBOBS_LINUX_PORTED_SOURCES": [ + "build/libobs.ported-null-audio-monitoring+.h.c" + ], + "headers": [ + "audio-monitoring/null/null-audio-monitoring.h" + ] +} diff --git a/libobs/cmake/rust-hidden.d/obs-audio-monitoring-null_monitoring.txt b/libobs/cmake/rust-hidden.d/obs-audio-monitoring-null_monitoring.txt new file mode 100644 index 0000000000..43bc2fcf05 --- /dev/null +++ b/libobs/cmake/rust-hidden.d/obs-audio-monitoring-null_monitoring.txt @@ -0,0 +1,8 @@ +# null-audio-monitoring.c: the null backend's canonical functions forward to +# these inside libobs (null/null-audio-monitoring.h, no EXPORT). +obs_null_audio_monitoring_available +obs_null_audio_monitoring_enum_devices +obs_null_audio_monitor_create +obs_null_audio_monitor_reset +obs_null_audio_monitor_destroy +obs_null_audio_monitoring_devices_match diff --git a/rust/README.md b/rust/README.md index 2a4b3404f2..d33b8dba10 100644 --- a/rust/README.md +++ b/rust/README.md @@ -489,6 +489,7 @@ The private output starting/encoder-demand queries use lazy safe Rust decisions | `libobs/obs-source-volume-mul.c.h` | `obs-source::volume_mul` | Swapped in hybrid `libobs` (group `libobs.ported-source-volume-mul`). The two volume multiply kernels that scale every audio source once per mix it is in, every tick, as two functions on plain pointers and counts, declared in `obs-source-volume-mul.h` without `EXPORT` and hidden: `obs_source_volume_mul_block()` (`multiply_output_audio()`: one volume over a whole mix, which `apply_audio_volume()` passes as one block of `AUDIO_OUTPUT_FRAMES * channels` samples from the mix's first channel, because a mix's channels are consecutive in `allocate_audio_output_buffer()`'s one allocation) and `obs_source_volume_mul_data()` (`multiply_vol_data()`'s inner loop: one channel by a volume per frame, when `apply_audio_actions()` applies an action inside the tick). `obs-source.c.h` keeps the action queue and its mutex, `get_source_volume()`, the `vol_data` fill, the memset and the mix and channel loops. Each sample is one IEEE single multiply, as the C's; the safe core runs loops on slices, which cannot overlap, so LLVM vectorizes them on every target (#3007), where GCC 14 at `-O2`, as libobs ships, leaves the C loops scalar. The loop form of `mul_data` depends on the target (`platform::GROUPED_AUDIO_LOOPS`), because LLVM unrolls the two forms differently: 4-sample groups on x86_64 (3 % behind GCC's vectorized `-O3` C, where a zip loop was 17 % behind), a plain zip loop on aarch64 (level with clang's C, where the grouped loop was 49 % behind); Tier 1 checks that both forms agree on every host. A NaN sample times a NaN volume carries one of the two payloads, and the C itself gives the sample's at `-O0` and the volume's vectorized at `-O2`, so neither side pins it. No intentional differences. `test_source_volume_mul_steps` compiles the C into itself; `test_source_volume_mix` sees the swap through the hybrid libobs. Tier 3 compares the oracle, the shims and the core bit for bit over every count to 70 and the tick of 1 to 8 channels, 21 special bit patterns as samples and volumes, random bit patterns and a proptest; `port_comparison` has `audio_mix/source_volume_*`. | | `libobs/obs-audio-controls.c.h` | `obs-audio-controls` | Swapped in hybrid `libobs`. The fader and volmeter are opaque in the header, so there is no layout test for them. The shim's `obs_audio_info` matches the header struct. The safe core does not call obs. The shim keeps the source, the volume and destroy signals, the capture callback, and the one `bzalloc` the cmocka test counts. A bad fader type frees that allocation. A volmeter stores any type. `obs_fader_db_to_def` returns an unexported curve function. `obs_volmeter_get_nr_channels` reads `obs_source_get_speaker_layout`, which is `sample_info.speakers`. Null `get_nr_channels`, `set_peak_meter_type`, and `db_to_def` still fault. The curves and `obs_mul_to_db` / `obs_db_to_mul` match the C oracle. The meter path runs through the unchanged cmocka test, because the capture callback is file-local and needs a live source. No intentional differences. | | `libobs/audio-monitoring/pulse/pulseaudio-stream-flags.c.h` | `obs-audio-monitoring::pulse_stream_flags` | Swapped in hybrid `libobs` on Linux and FreeBSD with `ENABLE_PULSEAUDIO` (group `libobs.ported-pulse`); `pulseaudio-output.c` calls the three functions inside libobs. The C file has no `EXPORT`, so the shims stay hidden. They are built on every host and unused where PulseAudio monitoring is not. The `` values are Rust constants, compared against libpulse's header by the parity test where it is installed (the oracle's stand-in header defines them elsewhere). The `uint64_t` arithmetic wraps as in C. A null `backlog` returns 0 (C dereferences it). `test_pulse_monitor_flags` and `test_pulse_monitor_backlog` compile the C into themselves, so only Tier 3 compares the swap. | +| `libobs/audio-monitoring/null/null-audio-monitoring.c.h` | `obs-audio-monitoring::null_monitoring` | Swapped in hybrid `libobs` on Linux and FreeBSD without `ENABLE_PULSEAUDIO` (group `libobs.ported-null-audio-monitoring`, the original C as the C reference, `null-audio-monitoring-reference.c.h`). The six canonical functions (`obs_audio_monitoring_available`, `obs_enum_audio_monitoring_devices`, `audio_monitor_create`, `audio_monitor_reset`, `audio_monitor_destroy`, `devices_match`) stay thin C forwarders in the selected `libobs.null-audio` group, since Windows, macOS and PulseAudio builds define the same names in their own backends; they call six uniquely named adapters declared in `null/null-audio-monitoring.h` without `EXPORT` and hidden, which the always-linked archive builds on every host. No argument is read, written, kept or freed, no callback runs, nothing logs or allocates. The unchanged `test_null_audio_monitoring` (core-relinked) runs its public calls against the C reference and the hybrid, and its renamed copy of the forwarders against the Rust adapters. Tier 3 compares the original C and the shims over the C test's inputs and arbitrary addresses, with a mutation check. No intentional differences. | | `libobs/audio-monitoring/pulse/pulseaudio-monitor-format.c.h` | `obs-audio-monitoring::pulse_format` | Swapped in hybrid `libobs` on Linux and FreeBSD with `ENABLE_PULSEAUDIO` (group `libobs.ported-pulse-format`); `pulseaudio-output.c` and `pulseaudio-wrapper.c` call the nine functions inside libobs. The C file has no `EXPORT`, so the shims stay hidden. They are built on every host and unused where PulseAudio monitoring is not. The `pa_sample_format_t`, `pa_channel_position_t`, `enum audio_format` and `enum speaker_layout` values are Rust constants and `pa_channel_map` is mirrored, compared against libpulse's headers by the parity test where they are installed (the oracle's stand-in headers define them elsewhere); `uint_fast8_t` and `uint_fast32_t` follow the host's width. The volume is the C's f32 arithmetic with its explicit saturation (#1709), byte for byte. Intentional differences: `map[8..]` of the channel map, indeterminate in C, is `PA_CHANNEL_POSITION_INVALID`; a null sample buffer, a zero frame size and a `"default"` id with a null default sink, each of which crashes the C, do nothing, drop nothing and do not match. `test_pulse_monitor_format` and `test_pulse_default_id` compile the C into themselves, so Tier 3 compares the swap, and the real-stream tests run it. | | `libobs/audio-monitoring/pulse/pulseaudio-monitor-steps.c.h` | `obs-audio-monitoring::pulse_monitor` | Swapped in hybrid `libobs` on Linux and FreeBSD with `ENABLE_PULSEAUDIO` (group `libobs.ported-pulse-monitor`); `pulseaudio-output.c` asks the twenty functions inside libobs, one per decision of `do_stream_write()` (grow the server buffer past twice `tlength`, stay corked until a whole `tlength` is queued, cancel a write the server asks too much for), `on_audio_playback()` (skip a source with no activate refs, the bytes pushed, the volume step unless 1 within `EPSILON`), `pulseaudio_sink_info()` (error, end of list or a sink), `audio_monitor_init()` (no device, the feedback-loop flag, `"default"`, the fallback to the default sink when the chosen one is gone, the invalid format, the 25 ms buffer, the fallback's flags), `on_sink_event()` (new and remove only), `audio_monitor_reset()` (`PA_CONTEXT_IS_GOOD`) and `audio_monitor_destroy()` (the last monitor). C keeps both locks and the trylock, the monitoring mutex, every read of the monitor, source and settings at its place, every `pa_*` call, the server and sink info calls, `get_default_id()`, `devices_match()`, the resampler, the deque, the buffer attributes, the task queue, every allocation and log (split decisions, #2153). The C file has no `EXPORT`, so the shims stay hidden; they are built on every host and unused where PulseAudio monitoring is not. Characterized, not endorsed: `tlength * 2` is a `uint32_t` product (it wraps from 2^31), the new `tlength` is the queue truncated to `uint32_t`, a NaN volume is applied, negative activate refs are not idle, and an empty device id falls back like a name. The byte count is a product in the host's `uint_fast32_t` (64 bits on glibc, 32 on FreeBSD). Intentional difference: a null id given to `is_default` or to `fallback` (invalid format), which the C dereferences, is not `"default"` and does not fall back; the C asks both only after `has_device`. `test_pulse_monitor_steps` compiles the C into itself, `test_pulse_monitor_steps_rust` runs it against `libobs_rust-static`, Tier 3 compares oracle, shims and core, and the real-stream tests (`test_pulse_monitor_device`, `test_pulse_default_follow`, `test_pulse_monitor_reset`, `test_pulse_stream_restart`, `test_pulse_two_sources`, `test_pulse_monitor_samples`) run it against a private sound server. | | `libobs/audio-monitoring/monitor-delay.c.h` | `obs-audio-monitoring::monitor_delay` | Swapped in hybrid `libobs` on Windows (group `libobs.ported-monitor-delay`); the WASAPI monitor's `process_audio_delay()` (`win32/wasapi-output.c`) asks the five functions inside libobs for every packet of a monitored source with video: the stale cut (more than 1 s since the last packet empties the delay queue; the receive time is stored either way) and the front packet's verdict in the steps the C takes (`diff`: its timestamp less the device padding against the source's last video timestamp; the "bad diff" gate in two parts, `unsynced`: no video timestamp or more than 5 s apart, and `stalled`: video stood still past `AUDIO_MONITOR_VIDEO_STALL_NS`; then `judge_diff`: hold past 75 ms ahead, drag past 75 ms behind, else play, and play whenever the gate is shut). C keeps the delay queue (every deque call), `da_resize()`, `os_gettime_ns()`, the reads of the source and the monitor at their places (the stall count only when `unsynced` is false, through the C's `||`), both `util_mul_div64()` calls, the "more are queued" test after the pops (a drag drops the packet only then), the `DEBUG_AUDIO` logs and the loop (split decisions, #2153). The stall count it reads comes from the video stall tracker, `monitor-video-sync.c.h` (below). The C file has no `EXPORT` and no Windows header, so the shims stay hidden; they are built on every host and unused off Windows. The subtractions wrap and the difference is both timestamps cast to `i64`, as in C. Characterized, not endorsed: a clock that steps back makes the queue stale, and a padding longer than the timestamp wraps to a front timestamp before 0. No intentional difference: the inputs the C leaves undefined (an `int64_t` difference that overflows, or `INT64_MIN` given to `llabs()`) are left out of Tier 3, and the core plays on `INT64_MIN`; null pointers, which the C dereferences, read as 0 and are not written. `test_audio_monitor_delay_steps` compiles the C into itself on every platform, `test_audio_monitor_delay_steps_rust` runs it against `libobs_rust-static` (Linux), and Tier 3 compares oracle, shims and core, step by step and put together as `process_audio_delay()` asks them (whether the stall count is asked included), its loop too. No real-libobs test reaches the WASAPI monitor. | diff --git a/rust/obs-audio-monitoring/Cargo.toml b/rust/obs-audio-monitoring/Cargo.toml index 8ba12278e9..ee08a8c4d7 100644 --- a/rust/obs-audio-monitoring/Cargo.toml +++ b/rust/obs-audio-monitoring/Cargo.toml @@ -2,7 +2,7 @@ autotests = false name = "obs-audio-monitoring" version = "0.1.0" -description = "Rust port of libobs/audio-monitoring/: the PulseAudio monitor's stream flags, format, volume and open and write steps, the WASAPI monitor's delay decisions, speaker layout and video stall tracker, and the CoreAudio monitor's queue pacing and device rules" +description = "Rust port of libobs/audio-monitoring/: the PulseAudio monitor's stream flags, format, volume and open and write steps, the WASAPI monitor's delay decisions, speaker layout and video stall tracker, the CoreAudio monitor's queue pacing and device rules, and the null backend" edition.workspace = true rust-version.workspace = true license.workspace = true diff --git a/rust/obs-audio-monitoring/src/ffi/mod.rs b/rust/obs-audio-monitoring/src/ffi/mod.rs index 53202868f5..4f27fef114 100644 --- a/rust/obs-audio-monitoring/src/ffi/mod.rs +++ b/rust/obs-audio-monitoring/src/ffi/mod.rs @@ -3,6 +3,7 @@ pub mod coreaudio_devices; pub mod coreaudio_pacing; pub mod monitor_delay; +pub mod null_monitoring; pub mod pulse_format; pub mod pulse_monitor; pub mod pulse_stream_flags; diff --git a/rust/obs-audio-monitoring/src/ffi/null_monitoring.rs b/rust/obs-audio-monitoring/src/ffi/null_monitoring.rs new file mode 100644 index 0000000000..844784435d --- /dev/null +++ b/rust/obs-audio-monitoring/src/ffi/null_monitoring.rs @@ -0,0 +1,73 @@ +//! C ABI for `libobs/audio-monitoring/null/null-audio-monitoring.c.h`, the +//! audio monitoring backend Linux and FreeBSD select when PulseAudio is off. +//! +//! The six canonical functions (`obs_audio_monitoring_available()`, +//! `obs_enum_audio_monitoring_devices()`, `audio_monitor_create()`, +//! `audio_monitor_reset()`, `audio_monitor_destroy()` and `devices_match()`) +//! stay thin C forwarders in that file, which only the null backend's group +//! (`libobs.null-audio`) compiles: Windows, macOS and PulseAudio builds +//! define the same names in their own backends, so this always-linked +//! archive must not. The forwarders call these uniquely named functions, +//! declared in `null/null-audio-monitoring.h` without `EXPORT` and hidden +//! (`rust-hidden.d`). Every other build links them unused. +//! +//! No pointer is read, written, kept or freed: the null backend passes the +//! caller's handles, IDs, callback and data through unread. + +use core::ffi::{c_char, c_void}; + +use crate::null_monitoring; + +/// `obs_enum_audio_device_cb`: `bool (*)(void *data, const char *name, const +/// char *id)`, never called here. +pub type EnumDeviceCallback = + Option bool>; + +/// `obs_audio_monitoring_available()` of the null backend: false. +#[unsafe(no_mangle)] +pub extern "C" fn obs_null_audio_monitoring_available() -> bool { + null_monitoring::available() +} + +/// `obs_enum_audio_monitoring_devices()` of the null backend: calls nothing +/// and leaves `data` untouched, for any `cb` and `data`, NULL or not. +#[unsafe(no_mangle)] +pub extern "C" fn obs_null_audio_monitoring_enum_devices( + cb: EnumDeviceCallback, + data: *mut c_void, +) { + null_monitoring::enum_devices((cb, data)); +} + +/// `audio_monitor_create()` of the null backend: NULL for any `source`, which +/// is never read. +#[unsafe(no_mangle)] +pub extern "C" fn obs_null_audio_monitor_create(source: *mut c_void) -> *mut c_void { + match null_monitoring::create(source) { + Some(monitor) => match monitor {}, + None => core::ptr::null_mut(), + } +} + +/// `audio_monitor_reset()` of the null backend: does nothing with `monitor`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_null_audio_monitor_reset(monitor: *mut c_void) { + null_monitoring::reset(monitor); +} + +/// `audio_monitor_destroy()` of the null backend: does nothing with +/// `monitor`, which it does not own. +#[unsafe(no_mangle)] +pub extern "C" fn obs_null_audio_monitor_destroy(monitor: *mut c_void) { + null_monitoring::destroy(monitor); +} + +/// `devices_match()` of the null backend: false for any two IDs, which are +/// never read (they need not be valid or terminated). +#[unsafe(no_mangle)] +pub extern "C" fn obs_null_audio_monitoring_devices_match( + id1: *const c_char, + id2: *const c_char, +) -> bool { + null_monitoring::devices_match(id1, id2) +} diff --git a/rust/obs-audio-monitoring/src/lib.rs b/rust/obs-audio-monitoring/src/lib.rs index c212cdb679..259c647def 100644 --- a/rust/obs-audio-monitoring/src/lib.rs +++ b/rust/obs-audio-monitoring/src/lib.rs @@ -14,8 +14,10 @@ //! `audio-monitoring/osx/coreaudio-pacing.c.h` (the queue pacing of the //! CoreAudio monitor's `coreaudio-output.c`), and [`coreaudio_devices`] that //! of `audio-monitoring/osx/coreaudio-device-rules.c.h` (the device rules of -//! the CoreAudio monitoring device list, `coreaudio-enum-devices.c`). Their C -//! ABI shims live in [`ffi`]. +//! the CoreAudio monitoring device list, `coreaudio-enum-devices.c`), and +//! [`null_monitoring`] that of `audio-monitoring/null/null-audio-monitoring.c.h` +//! (the backend Linux and FreeBSD select without PulseAudio). Their C ABI +//! shims live in [`ffi`]. #![deny(unsafe_code)] diff --git a/rust/obs-audio-monitoring/src/null_monitoring.rs b/rust/obs-audio-monitoring/src/null_monitoring.rs index 57a15c1d1e..531afa3906 100644 --- a/rust/obs-audio-monitoring/src/null_monitoring.rs +++ b/rust/obs-audio-monitoring/src/null_monitoring.rs @@ -15,41 +15,38 @@ pub enum Monitor {} /// `obs_audio_monitoring_available()`: always false. #[must_use] -pub fn available() -> bool { - todo!("null monitoring: available") +pub const fn available() -> bool { + false } /// `obs_enum_audio_monitoring_devices()`: lists no device, so `visit` (the /// callback and its data) is never called or touched. pub fn enum_devices(visit: V) { - let _ = visit; - todo!("null monitoring: enum_devices") + drop(visit); } /// `audio_monitor_create()`: no monitor for any source, NULL or not. #[must_use] pub fn create(source: S) -> Option { - let _ = source; - todo!("null monitoring: create") + drop(source); + None } /// `audio_monitor_reset()`: does nothing with any handle, NULL or not. pub fn reset(monitor: M) { - let _ = monitor; - todo!("null monitoring: reset") + drop(monitor); } /// `audio_monitor_destroy()`: does nothing with any handle, NULL or not; it /// owns nothing to free. pub fn destroy(monitor: M) { - let _ = monitor; - todo!("null monitoring: destroy") + drop(monitor); } /// `devices_match()`: false for every pair of IDs, NULL, equal or not. The /// IDs are never read, so they need not be terminated. #[must_use] pub fn devices_match(id1: I, id2: J) -> bool { - let _ = (id1, id2); - todo!("null monitoring: devices_match") + drop((id1, id2)); + false } diff --git a/rust/obs-audio-monitoring/tests/audio_monitoring/main.rs b/rust/obs-audio-monitoring/tests/audio_monitoring/main.rs index b7a7cbe1e4..abe1306995 100644 --- a/rust/obs-audio-monitoring/tests/audio_monitoring/main.rs +++ b/rust/obs-audio-monitoring/tests/audio_monitoring/main.rs @@ -8,6 +8,7 @@ mod coreaudio_pacing_parity; mod monitor_delay; mod monitor_delay_parity; mod null_monitoring; +mod null_monitoring_parity; mod pulse_backlog; mod pulse_flags; mod pulse_format; diff --git a/rust/obs-audio-monitoring/tests/audio_monitoring/null_monitoring.rs b/rust/obs-audio-monitoring/tests/audio_monitoring/null_monitoring.rs index 30d2172398..c531895576 100644 --- a/rust/obs-audio-monitoring/tests/audio_monitoring/null_monitoring.rs +++ b/rust/obs-audio-monitoring/tests/audio_monitoring/null_monitoring.rs @@ -34,7 +34,10 @@ fn enumeration_never_calls_back_or_writes_data() { }; enum_devices((Some(unexpected), Some(&mut data))); enum_devices((Some(unexpected), None::<&mut [u8; 4]>)); - enum_devices((None:: bool>, Some(&mut data))); + enum_devices(( + None:: bool>, + Some(&mut data), + )); enum_devices(( None:: bool>, None::<&mut [u8; 4]>, diff --git a/rust/obs-audio-monitoring/tests/audio_monitoring/null_monitoring_parity.rs b/rust/obs-audio-monitoring/tests/audio_monitoring/null_monitoring_parity.rs new file mode 100644 index 0000000000..8df917a245 --- /dev/null +++ b/rust/obs-audio-monitoring/tests/audio_monitoring/null_monitoring_parity.rs @@ -0,0 +1,183 @@ +//! Tier 3 parity: the null audio monitoring shims against the original C +//! backend (oracle/null_monitoring.c, the C reference's copy). +//! +//! Both sides get the same arguments: NULL, pointers to caller storage that +//! is not an OBS object, unterminated and non-UTF-8 IDs, and arbitrary +//! addresses that are never valid (neither side may read them). Every result +//! must match, no callback may run, and the caller's storage must be +//! unchanged. A mutation check proves the comparison sees a backend that +//! answers differently. No intentional differences. + +use core::ffi::{c_char, c_void}; +use std::cell::Cell; +use std::ptr; + +use obs_audio_monitoring::ffi::null_monitoring::{ + EnumDeviceCallback, obs_null_audio_monitor_create, obs_null_audio_monitor_destroy, + obs_null_audio_monitor_reset, obs_null_audio_monitoring_available, + obs_null_audio_monitoring_devices_match, obs_null_audio_monitoring_enum_devices, +}; +use obs_c_oracle::null_monitoring::*; +use proptest::prelude::*; + +thread_local! { static CALLS: Cell = const { Cell::new(0) }; } + +unsafe extern "C" fn count_device(_: *mut c_void, _: *const c_char, _: *const c_char) -> bool { + CALLS.with(|calls| calls.set(calls.get() + 1)); + // Would stop enumeration: no backend may call it even once. + false +} + +unsafe extern "C" fn keep_going(_: *mut c_void, _: *const c_char, _: *const c_char) -> bool { + CALLS.with(|calls| calls.set(calls.get() + 1)); + true +} + +/// One backend's six functions. +struct Backend { + available: unsafe extern "C" fn() -> bool, + enum_devices: unsafe extern "C" fn(EnumDeviceCallback, *mut c_void), + create: unsafe extern "C" fn(*mut c_void) -> *mut c_void, + reset: unsafe extern "C" fn(*mut c_void), + destroy: unsafe extern "C" fn(*mut c_void), + devices_match: unsafe extern "C" fn(*const c_char, *const c_char) -> bool, +} + +const ORACLE: Backend = Backend { + available: oracle_null_audio_monitoring_available, + enum_devices: oracle_null_audio_monitoring_enum_devices, + create: oracle_null_audio_monitor_create, + reset: oracle_null_audio_monitor_reset, + destroy: oracle_null_audio_monitor_destroy, + devices_match: oracle_null_audio_monitoring_devices_match, +}; + +const RUST: Backend = Backend { + available: obs_null_audio_monitoring_available, + enum_devices: obs_null_audio_monitoring_enum_devices, + create: obs_null_audio_monitor_create, + reset: obs_null_audio_monitor_reset, + destroy: obs_null_audio_monitor_destroy, + devices_match: obs_null_audio_monitoring_devices_match, +}; + +/// A backend that matches equal, non-NULL ID pointers: what a real backend +/// that compares IDs would answer for the same ID twice. +unsafe extern "C" fn mutant_devices_match(id1: *const c_char, id2: *const c_char) -> bool { + !id1.is_null() && id1 == id2 +} + +const MUTANT: Backend = Backend { + devices_match: mutant_devices_match, + ..RUST +}; + +/// Everything one backend answers for `handles` and every pair of `ids`, +/// with the number of callbacks it made. +#[derive(Debug, PartialEq, Eq)] +struct Answers { + available: bool, + created: Vec, + matches: Vec, + callbacks: u32, +} + +fn answers(backend: &Backend, handles: &[*mut c_void], ids: &[*const c_char]) -> Answers { + CALLS.with(|calls| calls.set(0)); + // SAFETY: the null backends read none of their arguments, so any address + // is accepted; the callbacks only count. + unsafe { + let available = (backend.available)(); + for &data in handles { + for cb in [None, Some(count_device as _), Some(keep_going as _)] { + (backend.enum_devices)(cb, data); + } + } + let mut created = Vec::new(); + for &handle in handles { + created.push((backend.create)(handle) as usize); + (backend.reset)(handle); + (backend.destroy)(handle); + // Repeated: no ownership or state changes. + (backend.reset)(handle); + (backend.destroy)(handle); + created.push((backend.create)(handle) as usize); + } + let mut matches = Vec::new(); + for &id1 in ids { + for &id2 in ids { + matches.push((backend.devices_match)(id1, id2)); + } + } + Answers { + available, + created, + matches, + callbacks: CALLS.with(Cell::get), + } + } +} + +/// The C test's inputs, over this caller storage. +fn fixed(storage: &mut [u8; 64], ids: &mut [[u8; 5]; 4]) -> (Vec<*mut c_void>, Vec<*const c_char>) { + let handles = vec![ptr::null_mut(), storage.as_mut_ptr().cast()]; + let [equal1, equal2, bytes, unterminated] = ids; + let ids = vec![ + ptr::null(), + c"".as_ptr(), + c"default".as_ptr(), + equal1.as_ptr().cast(), + equal2.as_ptr().cast(), + c"different".as_ptr(), + bytes.as_ptr().cast(), + unterminated.as_ptr().cast(), + ]; + (handles, ids) +} + +fn the_c_inputs() -> [[u8; 5]; 4] { + [*b"same\0", *b"same\0", [0xff, 0x80, b'x', 0, 0], *b"xyxyx"] +} + +#[test] +fn the_c_characterization_matches() { + let mut storage = [0xa5u8; 64]; + let mut ids = the_c_inputs(); + let (handles, id_ptrs) = fixed(&mut storage, &mut ids); + let expected = answers(&ORACLE, &handles, &id_ptrs); + assert_eq!(answers(&RUST, &handles, &id_ptrs), expected); + // The C's own answers, as test_null_audio_monitoring.c pins them. + assert!(!expected.available); + assert!(expected.created.iter().all(|&monitor| monitor == 0)); + assert!(expected.matches.iter().all(|&matched| !matched)); + assert_eq!(expected.callbacks, 0); + assert_eq!(storage, [0xa5; 64]); + assert_eq!(ids, the_c_inputs()); +} + +#[test] +fn the_comparison_catches_a_backend_that_matches_ids() { + let mut storage = [0xa5u8; 64]; + let mut ids = the_c_inputs(); + let (handles, id_ptrs) = fixed(&mut storage, &mut ids); + assert_ne!( + answers(&MUTANT, &handles, &id_ptrs), + answers(&ORACLE, &handles, &id_ptrs) + ); +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(obs_c_oracle::parity::cases(256)))] + /// Arbitrary addresses, never valid: both backends must leave them unread + /// and answer the same. + #[test] + fn arbitrary_addresses( + handles in proptest::collection::vec(any::(), 0..6), + ids in proptest::collection::vec(any::(), 0..6), + ) { + let handles: Vec<*mut c_void> = handles.into_iter().map(|a| a as *mut c_void).collect(); + let ids: Vec<*const c_char> = ids.into_iter().map(|a| a as *const c_char).collect(); + let expected = answers(&ORACLE, &handles, &ids); + prop_assert_eq!(answers(&RUST, &handles, &ids), expected); + } +} diff --git a/rust/obs-build/tests/native/registration/rust-static/cases/core-relinked/test_null_audio_monitoring.json b/rust/obs-build/tests/native/registration/rust-static/cases/core-relinked/test_null_audio_monitoring.json new file mode 100644 index 0000000000..f6bf9c8554 --- /dev/null +++ b/rust/obs-build/tests/native/registration/rust-static/cases/core-relinked/test_null_audio_monitoring.json @@ -0,0 +1,60 @@ +{ + "schema": 1, + "targets": { + "test_null_audio_monitoring": { + "sources": [ + "test/cmocka/test_null_audio_monitoring.c" + ], + "includes": [ + ".", + "libobs" + ], + "libraries": [ + "libobs_rust-static", + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka" + ], + "watch_inputs": [ + "libobs/audio-monitoring/null/null-audio-monitoring.c.h", + "libobs/audio-monitoring/null/null-audio-monitoring.h", + "libobs/build/libobs-nix.pch.h" + ], + "requires": { + "all": [ + { + "any": [ + "OS_LINUX", + "OS_FREEBSD" + ] + }, + { + "not": "ENABLE_PULSEAUDIO" + } + ] + } + } + }, + "tests": { + "test_null_audio_monitoring": { + "target": "test_null_audio_monitoring", + "command": [ + "${target}" + ], + "requires": { + "all": [ + { + "any": [ + "OS_LINUX", + "OS_FREEBSD" + ] + }, + { + "not": "ENABLE_PULSEAUDIO" + } + ] + } + } + } +} diff --git a/rust/obs-c-oracle/oracle/null_monitoring.c b/rust/obs-c-oracle/oracle/null_monitoring.c new file mode 100644 index 0000000000..2b12120de0 --- /dev/null +++ b/rust/obs-c-oracle/oracle/null_monitoring.c @@ -0,0 +1,12 @@ +/* Test-only oracle: the original null audio monitoring backend + * (libobs/audio-monitoring/null/null-audio-monitoring-reference.c.h, the C + * reference's copy), unmodified, with its six functions renamed to oracle_* + * so it can link next to the Rust shims. It reads nothing it is passed. */ +#define obs_null_audio_monitoring_available oracle_null_audio_monitoring_available +#define obs_null_audio_monitoring_enum_devices oracle_null_audio_monitoring_enum_devices +#define obs_null_audio_monitor_create oracle_null_audio_monitor_create +#define obs_null_audio_monitor_reset oracle_null_audio_monitor_reset +#define obs_null_audio_monitor_destroy oracle_null_audio_monitor_destroy +#define obs_null_audio_monitoring_devices_match oracle_null_audio_monitoring_devices_match + +#include "libobs/audio-monitoring/null/null-audio-monitoring-reference.c.h" diff --git a/rust/obs-c-oracle/src/lib.rs b/rust/obs-c-oracle/src/lib.rs index 6c3ba5630d..0b380576bf 100644 --- a/rust/obs-c-oracle/src/lib.rs +++ b/rust/obs-c-oracle/src/lib.rs @@ -140,6 +140,7 @@ pub mod nix_wayland_seats; pub mod nix_x11_hotkeys; pub mod nix_x11_input; pub mod nix_x11_keys; +pub mod null_monitoring; pub mod obs_nvenc; pub mod obs_properties; pub mod output_begin; diff --git a/rust/obs-c-oracle/src/null_monitoring.rs b/rust/obs-c-oracle/src/null_monitoring.rs new file mode 100644 index 0000000000..4912fa47c2 --- /dev/null +++ b/rust/obs-c-oracle/src/null_monitoring.rs @@ -0,0 +1,21 @@ +//! `oracle_*` build of the original null audio monitoring backend +//! (`libobs/audio-monitoring/null/null-audio-monitoring-reference.c.h`, +//! oracle/null_monitoring.c). + +use core::ffi::{c_char, c_void}; + +/// `obs_enum_audio_device_cb`. +pub type EnumDeviceCallback = + Option bool>; + +unsafe extern "C" { + pub fn oracle_null_audio_monitoring_available() -> bool; + pub fn oracle_null_audio_monitoring_enum_devices(cb: EnumDeviceCallback, data: *mut c_void); + pub fn oracle_null_audio_monitor_create(source: *mut c_void) -> *mut c_void; + pub fn oracle_null_audio_monitor_reset(monitor: *mut c_void); + pub fn oracle_null_audio_monitor_destroy(monitor: *mut c_void); + pub fn oracle_null_audio_monitoring_devices_match( + id1: *const c_char, + id2: *const c_char, + ) -> bool; +} diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index 6586858424..8df1cddd61 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -9361,6 +9361,7 @@ "libobs" ], "libraries": [ + "libobs_rust-static", "OBS::libobs", "OBS::caption", "Uthash::Uthash", @@ -9368,6 +9369,7 @@ ], "watch_inputs": [ "libobs/audio-monitoring/null/null-audio-monitoring.c.h", + "libobs/audio-monitoring/null/null-audio-monitoring.h", "libobs/build/libobs-nix.pch.h" ], "requires": {