From d18d25fff73a4991be7d42d3b70748a278e874ed Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Thu, 19 Nov 2026 09:51:30 -0500 Subject: [PATCH 1/2] test: mirror source audio-capture callback registration against a Rust stub (RED, Refs #5243) Moves the caption registry's ordered-pair update into callback_registry so both source callback lists share it. Co-Authored-By: Claude Opus 5.5 --- .../obs-source/src/audio_capture_callbacks.rs | 14 ++++ rust/obs-source/src/callback_registry.rs | 32 ++++++++ rust/obs-source/src/caption_callbacks.rs | 31 +++----- rust/obs-source/src/lib.rs | 2 + .../tests/source/audio_capture_callbacks.rs | 78 +++++++++++++++++++ rust/obs-source/tests/source/main.rs | 1 + 6 files changed, 138 insertions(+), 20 deletions(-) create mode 100644 rust/obs-source/src/audio_capture_callbacks.rs create mode 100644 rust/obs-source/src/callback_registry.rs create mode 100644 rust/obs-source/tests/source/audio_capture_callbacks.rs diff --git a/rust/obs-source/src/audio_capture_callbacks.rs b/rust/obs-source/src/audio_capture_callbacks.rs new file mode 100644 index 0000000000..918ee0f0db --- /dev/null +++ b/rust/obs-source/src/audio_capture_callbacks.rs @@ -0,0 +1,14 @@ +//! Ordered audio-capture subscriptions; duplicates and null tokens are retained. +use core::ffi::CStr; + +pub use crate::callback_registry::Registry; + +pub fn update( + registry: Option<&mut R>, + pair: R::Pair, + add: bool, + log: impl FnOnce(&'static CStr), +) { + // RED stub: the registration is not ported yet. + let _ = (registry, pair, add, log); +} diff --git a/rust/obs-source/src/callback_registry.rs b/rust/obs-source/src/callback_registry.rs new file mode 100644 index 0000000000..f122d5d626 --- /dev/null +++ b/rust/obs-source/src/callback_registry.rs @@ -0,0 +1,32 @@ +//! Ordered (callback, param) subscriptions shared by the source callback +//! lists; duplicates and null tokens are retained, removal erases the first +//! exact pair. +use core::ffi::CStr; + +pub trait Registry { + type Pair: Copy + Eq; + fn count(&self) -> usize; + fn pair(&self, index: usize) -> Self::Pair; + fn append(&mut self, pair: Self::Pair); + fn erase(&mut self, index: usize); +} + +/// `names` are the public add and remove entry names the NULL-source +/// diagnostic reports, in that order. +pub fn update( + registry: Option<&mut R>, + pair: R::Pair, + add: bool, + names: [&'static CStr; 2], + log: impl FnOnce(&'static CStr), +) { + let Some(registry) = registry else { + log(if add { names[0] } else { names[1] }); + return; + }; + if add { + registry.append(pair); + } else if let Some(index) = (0..registry.count()).find(|&i| registry.pair(i) == pair) { + registry.erase(index); + } +} diff --git a/rust/obs-source/src/caption_callbacks.rs b/rust/obs-source/src/caption_callbacks.rs index 0dc64f5003..6e50190ebc 100644 --- a/rust/obs-source/src/caption_callbacks.rs +++ b/rust/obs-source/src/caption_callbacks.rs @@ -1,13 +1,7 @@ //! Ordered caption subscriptions; duplicates and null tokens are retained. use core::ffi::CStr; -pub trait Registry { - type Pair: Copy + Eq; - fn count(&self) -> usize; - fn pair(&self, index: usize) -> Self::Pair; - fn append(&mut self, pair: Self::Pair); - fn erase(&mut self, index: usize); -} +pub use crate::callback_registry::Registry; pub fn update( registry: Option<&mut R>, @@ -15,17 +9,14 @@ pub fn update( add: bool, log: impl FnOnce(&'static CStr), ) { - let Some(registry) = registry else { - log(if add { - c"obs_source_add_caption_callback" - } else { - c"obs_source_remove_caption_callback" - }); - return; - }; - if add { - registry.append(pair); - } else if let Some(index) = (0..registry.count()).find(|&i| registry.pair(i) == pair) { - registry.erase(index); - } + crate::callback_registry::update( + registry, + pair, + add, + [ + c"obs_source_add_caption_callback", + c"obs_source_remove_caption_callback", + ], + log, + ); } diff --git a/rust/obs-source/src/lib.rs b/rust/obs-source/src/lib.rs index bb620c074c..75d5897c81 100644 --- a/rust/obs-source/src/lib.rs +++ b/rust/obs-source/src/lib.rs @@ -189,6 +189,7 @@ pub mod async_rotation; pub mod async_upload; pub mod audio_active; pub mod audio_active_setter; +pub mod audio_capture_callbacks; pub mod audio_input; pub mod audio_input_path; pub mod audio_mixers_getter; @@ -200,6 +201,7 @@ pub mod audio_settings; pub mod audio_timing; pub mod base_size; pub mod boolean_queries; +pub mod callback_registry; pub mod canvas_getter; pub mod caption_callbacks; pub mod color_space; diff --git a/rust/obs-source/tests/source/audio_capture_callbacks.rs b/rust/obs-source/tests/source/audio_capture_callbacks.rs new file mode 100644 index 0000000000..0ade74a002 --- /dev/null +++ b/rust/obs-source/tests/source/audio_capture_callbacks.rs @@ -0,0 +1,78 @@ +//! Mirrors test_source_audio_capture_callbacks.c without FFI, locking or +//! callback invocation (Tier 3 checks the lock and the real C list). +use obs_source::audio_capture_callbacks::{Registry, update}; +#[derive(Default)] +struct List(Vec<(u8, u8)>); +impl Registry for List { + type Pair = (u8, u8); + fn count(&self) -> usize { + self.0.len() + } + fn pair(&self, i: usize) -> Self::Pair { + self.0[i] + } + fn append(&mut self, p: Self::Pair) { + self.0.push(p); + } + fn erase(&mut self, i: usize) { + self.0.remove(i); + } +} +fn apply(list: &mut List, pair: (u8, u8), add: bool) { + update(Some(list), pair, add, |_| panic!("valid source logged")); +} +#[test] +fn test_source_audio_capture_callbacks_invalid_source() { + for (add, name) in [ + (true, c"obs_source_add_audio_capture_callback"), + (false, c"obs_source_remove_audio_capture_callback"), + ] { + let mut logs = Vec::new(); + update::(None, (1, 0), add, |name| logs.push(name)); + assert_eq!(logs, [name]); + } +} +#[test] +fn test_source_audio_capture_callbacks_pair_identity_order_and_removal() { + // (callback, param): callback 1 = a, 2 = b; param n = &tokens[n]. + let mut list = List::default(); + apply(&mut list, (1, 0), false); + assert!(list.0.is_empty()); + for pair in [(1, 0), (2, 0), (1, 1), (1, 0), (2, 2)] { + apply(&mut list, pair, true); + } + assert_eq!(list.0, [(1, 0), (2, 0), (1, 1), (1, 0), (2, 2)]); + apply(&mut list, (1, 2), false); + assert_eq!(list.0, [(1, 0), (2, 0), (1, 1), (1, 0), (2, 2)]); + apply(&mut list, (1, 0), false); + assert_eq!(list.0, [(2, 0), (1, 1), (1, 0), (2, 2)]); + apply(&mut list, (1, 0), false); + assert_eq!(list.0, [(2, 0), (1, 1), (2, 2)]); + apply(&mut list, (1, 0), false); + assert_eq!(list.0, [(2, 0), (1, 1), (2, 2)]); + apply(&mut list, (1, 1), false); + apply(&mut list, (2, 2), false); + assert_eq!(list.0, [(2, 0)]); + apply(&mut list, (2, 0), false); + assert!(list.0.is_empty()); +} +#[test] +fn test_source_audio_capture_callbacks_null_pairs_are_stored() { + // 0 is NULL; param 1 = &tokens[0], 2 = &tokens[1]. + let mut list = List::default(); + for pair in [(0, 0), (1, 0), (0, 1), (0, 0)] { + apply(&mut list, pair, true); + } + assert_eq!(list.0, [(0, 0), (1, 0), (0, 1), (0, 0)]); + apply(&mut list, (0, 2), false); + assert_eq!(list.0, [(0, 0), (1, 0), (0, 1), (0, 0)]); + apply(&mut list, (0, 0), false); + assert_eq!(list.0, [(1, 0), (0, 1), (0, 0)]); + apply(&mut list, (1, 0), false); + apply(&mut list, (0, 1), false); + assert_eq!(list.0, [(0, 0)]); + apply(&mut list, (0, 0), false); + assert!(list.0.is_empty()); + apply(&mut list, (0, 0), false); + assert!(list.0.is_empty()); +} diff --git a/rust/obs-source/tests/source/main.rs b/rust/obs-source/tests/source/main.rs index b123d261b3..9125071625 100644 --- a/rust/obs-source/tests/source/main.rs +++ b/rust/obs-source/tests/source/main.rs @@ -16,6 +16,7 @@ mod audio_active; mod audio_active_parity; mod audio_active_setter; mod audio_active_setter_parity; +mod audio_capture_callbacks; mod audio_input; mod audio_input_parity; mod audio_input_path; From 1f21b357f5f0dc58bd615c26320a89dbc689d54a Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Thu, 19 Nov 2026 10:06:48 -0500 Subject: [PATCH 2/2] obs-source: swap source audio-capture callback registration to safe Rust (GREEN, Refs #5243) obs_source_add/remove_audio_capture_callback now run in obs-source over the shared ordered-pair registry. The real private adapter keeps audio_cb_mutex, the C darray and its bmem storage. The unchanged C characterization links the Rust through private _rust entries, the real adapter compiled over its recording mutex and the real variadic blog (test_audio_capture_callbacks_rust, outside the test_source_ prefix that gives source-step fixtures an aborting blog stub); Tier 3 compares the Rust with the original C. The audio-controls oracle keeps its no-op stand-ins private. Co-Authored-By: Claude Opus 5.5 Ci-Attestation: {"at":1795114575,"gate":"data/all/compatibility","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":94,"stamp":"1cf32ef3d25d7ad68f11c761ca5b0c2a","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114575,"gate":"data/all/flatpak-manifest","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":94,"stamp":"62ef1be030ea8f161f8a9ae7e613319d","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114575,"gate":"data/all/qt-xml","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":94,"stamp":"c4ba0ebed76aabac8d320df8c0397140","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114575,"gate":"data/all/services-schema","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":94,"stamp":"21d9bdd81c24166994a34c0ddb080b4b","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114480,"gate":"general/all/client-lint","host":"linux-x86_64","key":"b7d03511ca333ed8ee4e48a83faaef561e36d8e4f42ef5f377fef4faeec4237d","lane":"client-lint","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":75,"stamp":"18f81e9c602268dfa767dd40d9a6f35d","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114575,"gate":"python/all/self-test","host":"linux-x86_64","key":"","lane":"client-checks","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":94,"stamp":"bf56ddd441a588f10d278d288a84ef5d","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114480,"gate":"rust/aarch64-apple-darwin/client-clippy","host":"linux-x86_64","key":"b7d03511ca333ed8ee4e48a83faaef561e36d8e4f42ef5f377fef4faeec4237d","lane":"client-lint","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":75,"stamp":"b22fcc56d6f6a5cda5858de671433cbe","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114480,"gate":"rust/aarch64-apple-darwin/client-dylint","host":"linux-x86_64","key":"b7d03511ca333ed8ee4e48a83faaef561e36d8e4f42ef5f377fef4faeec4237d","lane":"client-lint","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":75,"stamp":"11829d15e37422a15d2427fe10d889e9","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114480,"gate":"rust/aarch64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"b7d03511ca333ed8ee4e48a83faaef561e36d8e4f42ef5f377fef4faeec4237d","lane":"client-lint","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":75,"stamp":"4c72da0388614c58b987a607e468e606","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114480,"gate":"rust/aarch64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"b7d03511ca333ed8ee4e48a83faaef561e36d8e4f42ef5f377fef4faeec4237d","lane":"client-lint","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":75,"stamp":"9b383d5d7a32aef4f1bbce0e285656b4","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114480,"gate":"rust/aarch64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"b7d03511ca333ed8ee4e48a83faaef561e36d8e4f42ef5f377fef4faeec4237d","lane":"client-lint","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":75,"stamp":"bc525eb29423f62b73a048e4c23e0a54","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114480,"gate":"rust/aarch64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"b7d03511ca333ed8ee4e48a83faaef561e36d8e4f42ef5f377fef4faeec4237d","lane":"client-lint","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":75,"stamp":"db8b8d44ef63656cd710667f242b5e14","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114480,"gate":"rust/x86_64-apple-darwin/client-clippy","host":"linux-x86_64","key":"b7d03511ca333ed8ee4e48a83faaef561e36d8e4f42ef5f377fef4faeec4237d","lane":"client-lint","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":75,"stamp":"68d34003beaab2e4d21ed4f25ab8e975","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114480,"gate":"rust/x86_64-apple-darwin/client-dylint","host":"linux-x86_64","key":"b7d03511ca333ed8ee4e48a83faaef561e36d8e4f42ef5f377fef4faeec4237d","lane":"client-lint","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":75,"stamp":"eda790340083d7feb43d4f14f3518c51","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114480,"gate":"rust/x86_64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"b7d03511ca333ed8ee4e48a83faaef561e36d8e4f42ef5f377fef4faeec4237d","lane":"client-lint","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":75,"stamp":"7d01fa4bad64dc5e26a57181c1510dcb","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114480,"gate":"rust/x86_64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"b7d03511ca333ed8ee4e48a83faaef561e36d8e4f42ef5f377fef4faeec4237d","lane":"client-lint","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":75,"stamp":"0eb5cf1ab8650f89b9b3cde52159ced5","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114480,"gate":"rust/x86_64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"b7d03511ca333ed8ee4e48a83faaef561e36d8e4f42ef5f377fef4faeec4237d","lane":"client-lint","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":75,"stamp":"e23772d7a95438198513827fcab8bfdf","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114480,"gate":"rust/x86_64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"b7d03511ca333ed8ee4e48a83faaef561e36d8e4f42ef5f377fef4faeec4237d","lane":"client-lint","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":75,"stamp":"09b2b26ecc7c101a7210721d77dfb682","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Ci-Attestation: {"at":1795114803,"gate":"rust/x86_64-unknown-linux-gnu/client-test","host":"linux-x86_64","key":"1986f1abdc20e80e7195d630b7e7cc3f532989b914690c5c50efbe470ce9c860","lane":"client-test-x86_64-unknown-linux-gnu","parents":["d18d25fff73a4991be7d42d3b70748a278e874ed"],"secs":227,"stamp":"f751ac40697d3c983668c2349c415cbe","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5","v":1,"via":"run"} Client-Checks: {"checks":{"base":"6c3417a871453a8b68bb78bc5dd70d07b0bfefc9","coverage":"all-tracked-applicable-files","diff":["libobs/build/libobs.core-source+.h.c","libobs/build/libobs.ported-source-audio-capture-callbacks+.h.c","libobs/build/ported-source-sets/source-audio-capture-callbacks.json","libobs/cmake/rust-exports.d/obs-source-source_audio_capture_callbacks.txt","libobs/cmake/rust-hidden.d/obs-source-source_audio_capture_callbacks.txt","libobs/obs-internal.h","libobs/obs-source-audio-capture-callback-fields.c.h","rust/README.md","rust/obs-build/tests/native/registration/canonical_video_info_query_is_registered.rs","rust/obs-build/tests/native/registration/rust-static/cases/header/test_audio_capture_callbacks.json","rust/obs-c-oracle/oracle/audio_controls.c","rust/obs-c-oracle/oracle/source_audio_capture_callbacks.c","rust/obs-source/src/audio_capture_callbacks.rs","rust/obs-source/src/callback_registry.rs","rust/obs-source/src/caption_callbacks.rs","rust/obs-source/src/ffi/callback_list.rs","rust/obs-source/src/ffi/mod.rs","rust/obs-source/src/ffi/source_audio_capture_callbacks.rs","rust/obs-source/src/ffi/source_caption_callbacks.rs","rust/obs-source/src/lib.rs","rust/obs-source/tests/source/audio_capture_callbacks.rs","rust/obs-source/tests/source/audio_capture_callbacks_parity.rs","rust/obs-source/tests/source/main.rs","test/cmocka/build/test-graph.json","test/cmocka/source-audio-capture-callback-link-support.c","test/cmocka/source-audio-capture-callback-record-support.c","test/cmocka/tests.d/test_audio_capture_callbacks.cmake","test/cmocka/tests.d/video_info_query_link_support.cmake"],"head":"d26fc5bcd8650254811d38ada567bffd5801a146","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":"e3f2b115e013875f432fbe50bab6ee5e0fbbf49fd3cd2a5a12ce72ff6c8ee23b","tree":"eab26f59bb8fdccbbb768130e0f311bc2d39bcd5"}} --- libobs/build/libobs.core-source+.h.c | 2 +- ...ported-source-audio-capture-callbacks+.h.c | 4 + .../source-audio-capture-callbacks.json | 5 + ...-source-source_audio_capture_callbacks.txt | 2 + ...-source-source_audio_capture_callbacks.txt | 2 + libobs/obs-internal.h | 5 + ...s-source-audio-capture-callback-fields.c.h | 16 ++ rust/README.md | 1 + ...anonical_video_info_query_is_registered.rs | 5 +- .../header/test_audio_capture_callbacks.json | 43 +++++ rust/obs-c-oracle/oracle/audio_controls.c | 4 + .../oracle/source_audio_capture_callbacks.c | 105 +++++++++++ .../obs-source/src/audio_capture_callbacks.rs | 12 +- rust/obs-source/src/ffi/callback_list.rs | 78 ++++++++ rust/obs-source/src/ffi/mod.rs | 3 + .../src/ffi/source_audio_capture_callbacks.rs | 87 +++++++++ .../src/ffi/source_caption_callbacks.rs | 63 ++----- .../source/audio_capture_callbacks_parity.rs | 173 ++++++++++++++++++ rust/obs-source/tests/source/main.rs | 1 + test/cmocka/build/test-graph.json | 36 ++++ ...urce-audio-capture-callback-link-support.c | 2 + ...ce-audio-capture-callback-record-support.c | 13 ++ .../test_audio_capture_callbacks.cmake | 5 + .../video_info_query_link_support.cmake | 1 + 24 files changed, 612 insertions(+), 56 deletions(-) create mode 100644 libobs/build/libobs.ported-source-audio-capture-callbacks+.h.c create mode 100644 libobs/build/ported-source-sets/source-audio-capture-callbacks.json create mode 100644 libobs/cmake/rust-exports.d/obs-source-source_audio_capture_callbacks.txt create mode 100644 libobs/cmake/rust-hidden.d/obs-source-source_audio_capture_callbacks.txt create mode 100644 libobs/obs-source-audio-capture-callback-fields.c.h create mode 100644 rust/obs-build/tests/native/registration/rust-static/cases/header/test_audio_capture_callbacks.json create mode 100644 rust/obs-c-oracle/oracle/source_audio_capture_callbacks.c create mode 100644 rust/obs-source/src/ffi/callback_list.rs create mode 100644 rust/obs-source/src/ffi/source_audio_capture_callbacks.rs create mode 100644 rust/obs-source/tests/source/audio_capture_callbacks_parity.rs create mode 100644 test/cmocka/source-audio-capture-callback-link-support.c create mode 100644 test/cmocka/source-audio-capture-callback-record-support.c create mode 100644 test/cmocka/tests.d/test_audio_capture_callbacks.cmake diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index e4e271ffbb..4610ad8702 100644 --- a/libobs/build/libobs.core-source+.h.c +++ b/libobs/build/libobs.core-source+.h.c @@ -52,7 +52,7 @@ #include "libobs/obs-source-async-rotation-fields.c.h" #include "libobs/obs-source-audio-active-fields.c.h" #include "libobs/obs-source-audio-active-setter-fields.c.h" -#include "libobs/obs-source-audio-capture-callbacks.c.h" +#include "libobs/obs-source-audio-capture-callback-fields.c.h" #include "libobs/obs-source-audio-input-path-seam.c.h" #include "libobs/obs-source-audio-mixers-fields.c.h" #include "libobs/obs-source-audio-output-seam.c.h" diff --git a/libobs/build/libobs.ported-source-audio-capture-callbacks+.h.c b/libobs/build/libobs.ported-source-audio-capture-callbacks+.h.c new file mode 100644 index 0000000000..a8adf18554 --- /dev/null +++ b/libobs/build/libobs.ported-source-audio-capture-callbacks+.h.c @@ -0,0 +1,4 @@ +/* Unity group (BUILD.md): original audio-capture subscription registration. */ +#include "libobs/build/libobs.pch.h" + +#include "libobs/obs-source-audio-capture-callbacks.c.h" diff --git a/libobs/build/ported-source-sets/source-audio-capture-callbacks.json b/libobs/build/ported-source-sets/source-audio-capture-callbacks.json new file mode 100644 index 0000000000..ff75000f3f --- /dev/null +++ b/libobs/build/ported-source-sets/source-audio-capture-callbacks.json @@ -0,0 +1,5 @@ +{ + "LIBOBS_PORTED_SOURCES": [ + "build/libobs.ported-source-audio-capture-callbacks+.h.c" + ] +} diff --git a/libobs/cmake/rust-exports.d/obs-source-source_audio_capture_callbacks.txt b/libobs/cmake/rust-exports.d/obs-source-source_audio_capture_callbacks.txt new file mode 100644 index 0000000000..4eb5df64f7 --- /dev/null +++ b/libobs/cmake/rust-exports.d/obs-source-source_audio_capture_callbacks.txt @@ -0,0 +1,2 @@ +obs_source_add_audio_capture_callback +obs_source_remove_audio_capture_callback diff --git a/libobs/cmake/rust-hidden.d/obs-source-source_audio_capture_callbacks.txt b/libobs/cmake/rust-hidden.d/obs-source-source_audio_capture_callbacks.txt new file mode 100644 index 0000000000..1e2d7f01a4 --- /dev/null +++ b/libobs/cmake/rust-hidden.d/obs-source-source_audio_capture_callbacks.txt @@ -0,0 +1,2 @@ +obs_source_add_audio_capture_callback_rust +obs_source_remove_audio_capture_callback_rust diff --git a/libobs/obs-internal.h b/libobs/obs-internal.h index 90f7894bca..0bb9b3ca59 100644 --- a/libobs/obs-internal.h +++ b/libobs/obs-internal.h @@ -1579,6 +1579,11 @@ struct obs_source_properties_fields { struct obs_source_properties_fields obs_source_properties_info_fields(const struct obs_source_info *info); struct obs_source_properties_fields obs_source_properties_instance_fields(const obs_source_t *source); +/* Audio-capture list address and mutex operations for Rust registration. Non-NULL only. */ +extern struct darray *obs_source_audio_capture_callback_fields(obs_source_t *source); +extern void obs_source_audio_capture_callback_lock(obs_source_t *source); +extern void obs_source_audio_capture_callback_unlock(obs_source_t *source); + /* Caption list addresses and mutex operations for Rust registration. Non-NULL only. */ extern struct darray *obs_source_caption_callback_fields(obs_source_t *source); extern void obs_source_caption_callback_lock(obs_source_t *source); diff --git a/libobs/obs-source-audio-capture-callback-fields.c.h b/libobs/obs-source-audio-capture-callback-fields.c.h new file mode 100644 index 0000000000..f35b40a2ba --- /dev/null +++ b/libobs/obs-source-audio-capture-callback-fields.c.h @@ -0,0 +1,16 @@ +#include "libobs/build/libobs.pch.h" + +struct darray *obs_source_audio_capture_callback_fields(obs_source_t *source) +{ + return &source->audio_cb_list.da; +} + +void obs_source_audio_capture_callback_lock(obs_source_t *source) +{ + pthread_mutex_lock(&source->audio_cb_mutex); +} + +void obs_source_audio_capture_callback_unlock(obs_source_t *source) +{ + pthread_mutex_unlock(&source->audio_cb_mutex); +} diff --git a/rust/README.md b/rust/README.md index d729c84d7d..84e6a68477 100644 --- a/rust/README.md +++ b/rust/README.md @@ -419,6 +419,7 @@ The private output starting/encoder-demand queries use lazy safe Rust decisions | `libobs/obs-source-audio-output.c.h` | `obs-source::audio_output`, shims in `obs-source-seam::ffi::source_audio_output` | Swapped in hybrid `libobs` (group `libobs.ported-source-audio-output`); Tier 3 in `obs-source-seam/tests/source_seam/audio_output_parity.rs` | None. | | `libobs/obs-source-audio-pause-callbacks.c.h` | `obs-source::audio_pause_callbacks`, shims in `obs-source::ffi::source_audio_pause_callbacks` | Swapped in hybrid `libobs` (group `libobs.ported-source-audio-pause-callbacks`). `obs_source_add_audio_pause_callback` and `obs_source_remove_audio_pause_callback`: a NULL source logs `: Null 'source' parameter` at `LOG_DEBUG` and touches nothing; otherwise the source's signal handler is fetched once and the (callback, param) pair is connected to, or disconnected from, `media_pause` then `media_stopped`. The signal handler (`obs-callback`), `obs_source_get_signal_handler()` and the macOS monitor that subscribes stay as they are. `test_source_audio_pause_callbacks` sees the swap through the hybrid libobs. Tier 3 compares the core's ordered call trace with the original C over every NULL source, NULL callback and NULL param combination and proptest sequences, with callback and param forwarding checked by the C recorder. | None. | | `libobs/obs-source-audio-place.c.h` | `obs-source::audio_place` | Swapped in hybrid `libobs` (group `libobs.ported-source-audio-place`). The source audio placement and action timeline, which every audio packet a source outputs and every tick with queued audio actions go through, as eleven functions on plain values, each asked where the C made the decision: `obs_source_audio_place_started()` and `_older()` (`audio_ts != 0` and `timestamp < audio_ts`: the reset of `source_output_audio_place()` with no `audio_ts` or an older packet, the push-back rule of `source_output_audio_data()` and the action clamp of `apply_audio_actions()`), `_size()`, `_offset()`, `_fits()` and `_pop_back()` (the packet's bytes, `frames * sizeof(float)`; its byte placement, `get_buf_placement()`'s `(size_t)util_mul_div64(timestamp - audio_ts, rate, 1e9) * sizeof(float)`; the drop when the place or the push back would pass `MAX_BUF_SIZE`; the bytes popped off the back of each channel after the place), `_outputs()` (nothing for `OBS_MONITORING_TYPE_MONITOR_ONLY`), `_action_frame()` (`conv_time_to_frames()`, which goes), `_action_stops()` (at `AUDIO_OUTPUT_FRAMES`), `_fill_end()` (how far the previous volume fills) and `_mix_on()` (the mixes the volume array multiplies). They are declared in `obs-source-audio-place.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps every read where it was, with the C's `&&` and `||` around the calls: `audio_ts` once or twice for the place's reset, again after `reset_audio_data()` for the placement, in the route only when `push_back` is set, and in the action loop once for the clamp, again when it applies and again for the frame; `monitoring_type` before `audio_ts`; each channel's size after its `deque_place()` and the first channel's before a push back; `audio_mixers` once per mix. It keeps `audio_buf_mutex` and `audio_actions_mutex`, `audio_output_get_channels()`, `get_buf_placement()` with its `audio_output_get_sample_rate()` call (the timestamps are read before it, as when their difference was its argument), `reset_audio_data()`, the deques, the `DEBUG_AUDIO` log, the stores of `last_audio_input_buf_size`, `da_erase()`, `apply_audio_action()`, every `get_source_volume()` call (before the loop and after each applied action, so each action's push-to-talk and push-to-mute state is seen), the volume array and `multiply_vol_data()`. The header mirrors `AUDIO_OUTPUT_FRAMES`, `MAX_BUF_SIZE` and `OBS_MONITORING_TYPE_MONITOR_ONLY`, and `obs-source.c.h` checks them and the enum's size. No pointer crosses. The arithmetic wraps where the C's does; the conversion is `obs-source::transition_render`'s `util_mul_div64()` per host. Kept, characterized, not endorsed: a packet or action less than one sample after `audio_ts` lands on frame 0, on top of the samples there; a packet larger than `MAX_BUF_SIZE` on its own is dropped whole; a push back checks only the first channel's size; an action before `audio_ts` applies at frame 0, and once one is at frame 1024 or later every later action waits, even a due one; a monitoring type outside the enum outputs. Intentional differences, on inputs libobs never passes: `_mix_on()` with a mix of 32 or more says no mix (the C's `int` shift is undefined from 31 on; libobs asks 0 to 5), and on MSVC x64 a frame count past 64 bits (a rate above 1e9 Hz), where the C's `_udiv128()` faults, saturates. `test_source_audio_place_steps` compiles the C into itself, and `test_source_audio_place_steps_rust` runs the same test source against `libobs_rust-static`; `test_async_delay_audio`, `test_audio_sync_offset_change`, `test_audio_clock_drift`, `test_audio_buffering_subsample`, `test_media_sync_offset_restart`, `test_audio_restart_resampler_tail`, `test_source_volume_mix`, `test_source_audio_ranges` and `test_audio_controls` see the swap through the real libobs. Tier 3 compares the oracle, the shims and the core: the header's values, the reset rules, the placement and the action frame over every pair of 30 special timestamps at 16 rates (to `u32::MAX`, and `size_t` rates for the frame), every frame edge to 2048 at eleven rates, the sizes over every pair and triple of 17 special values, every monitoring type from -100,000 to 100,000, the mix rule for every low 16-bit mask under eight high words at every mix to 30, the callers copied onto a stand-in source over 36,864 packet pairs and 21,316 action lists, and full-range proptests. | +| `libobs/obs-source-audio-capture-callbacks.c.h` | `obs-source::audio_capture_callbacks` (shared ordered-pair update in `obs-source::callback_registry`) | Public audio-capture registration swapped in hybrid libobs (group `libobs.ported-source-audio-capture-callbacks`); append order and duplicates, first exact-pair removal, NULL callback/param entries and the exact NULL-source diagnostics preserved. The real private adapter (`obs-source-audio-capture-callback-fields.c.h`) keeps `audio_cb_mutex`, the C darray and its bmem storage; audio delivery and teardown are unchanged. `test_source_audio_capture_callbacks` compiles the C into itself, and `test_audio_capture_callbacks_rust` runs the same source against `libobs_rust-static` through the private `_rust` entries and the real adapter compiled over its recording mutex. Tier 3 compares generated operation sequences, both entry pairs, the diagnostics and real-header layouts against the original C. | None. | | `libobs/obs-source-audio-render.c.h` | `obs-source::audio_render_plan` | Swapped in hybrid `libobs` (group `libobs.ported-source-audio-render`). The audio render mix plan of `obs_source_audio_render()`, `custom_audio_render()` and `process_audio_source_tick()`, which decide for every audio source on every tick of the mix which mixes are cleared, copied and scaled, as thirteen functions on plain values: `obs_source_audio_render_route()` (pending without an output buffer, or for a custom render without data, 432017b2c and cf0a5e5c9; the custom render; else the tick), `_custom_clear_before()` (a mix in both masks is cleared before the plugin's `audio_render` callback), `_custom_ts()` and `_custom_pending()` (its timestamp when it succeeds, else 0 and pending), `_custom_unmixed()` (no timestamp or no tick mixers: nothing after it), `_custom_in_tick()` and `_custom_clear_after()` (a mix of the tick the source is not on is cleared after it, d6ac45702), `_tick_unset()` and `_tick_short()` (pending with no timestamp or less input than the tick, #130), `_tick_stop()` and `_tick_copy()` (mixes 1 to 5 copy mix 0 when in both masks, else are cleared; a submix source, `OBS_SOURCE_SUBMIX`, does mix 1 only, from bit 0 of its mixers), and `_tick_volume()` and `_tick_clear_main()` (a submix source stops there; any other clears mix 0 unless in both masks, then scales). They are declared in `obs-source-audio-render.h` without `EXPORT`, so the shims stay hidden. The plan is one function per decision, not one call per tick, because `obs-source.c.h` keeps each read where it was: `audio_mixers` (set by other threads without a lock) once per mix before and after the callback, the input size only with a timestamp, `audio_ts` only after a successful callback, and the output buffer, `audio_render` and `context.data` only where the checks before reached them. It keeps `audio_buf_mutex`, `deque_peek_front()`, every memset and memcpy, the plugin callbacks, `audio_submix()` and `apply_audio_volume()`. No pointer crosses; the route goes back as the C enum's value; the callback's `ts` crosses by value also when the callback failed and is not looked at. Kept, characterized, not endorsed: tick mixers past the six mixes (0x40 and up) are not "no mixers", so a custom render's clears look at each mix and its volume runs; a submix source keeps the peeked input in mix 0 even when it is on no mix, and fills mix 1 whatever the tick's mixers. Intentional difference: a mix of 31 or more for the shifting decisions, where the C's `int` shift is undefined, names bit 31 and then no mix (libobs passes a mix below 6). `test_source_audio_render_steps` compiles the C into itself, and `test_source_audio_render_steps_rust` runs the same test source against `libobs_rust-static`; `test_source_volume_mix`, `test_source_audio_ranges`, `test_audio_capture_mix_race`, `test_group_hide_transition_audio` and the transition audio tests see the swap through the real libobs. Tier 3 compares the oracle, the shims and the core: every mask decision over every pair of the low seven mask bits with and without the high ones and every mix up to 30, the submix copy and the stop over any index, the timestamp, pending and size decisions over every pair of 30 special values, the three callers copied onto a stand-in source over every combination of seven flags with every pair of the low seven mask bits and edge timestamps, masks, sizes and channels (the recorded lock, peeks, memsets, memcpys, callbacks and volume, `audio_ts` and `audio_pending`), and full-range proptests. | | `libobs/obs-source-audio-settings.c.h` | `obs-source::audio_settings`, shims in `obs-source-seam::ffi::source_audio_settings` | Swapped in hybrid `libobs` (group `libobs.ported-source-audio-settings`); Tier 3 in `obs-source-seam/tests/source_seam/audio_settings_parity.rs` | None. | | `libobs/obs-source-audio-timing.c.h` | `obs-source::audio_timing` | Swapped in hybrid `libobs` (group `libobs.ported-source-audio-timing`). The audio timestamp placement of `source_output_audio_data()`, in two pure steps around `audio_buf_mutex`: `obs_source_audio_timing_pre_lock()` (the direct timestamp check, then reset, jump, smooth with the drift error and the async decoupled re-anchor, or the log, and `next_audio_ts_min`) and `obs_source_audio_timing_under_lock()` (append or place, the reset when the system time jumped and the audio was not continuous, #522, the sync and resampler offsets, `next_audio_sys_ts_min`, and "the sync offset changed, so place", #333), plus `obs_source_audio_timing_buffer_start()` (where `reset_audio_data_at_sys_time()` starts an emptied buffer, #286). They are declared in `obs-source-audio-timing.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps the field reads and the stores, in their order (the volatile `timing_set` and `timing_adjust` included), the lock, `handle_ts_jump()`, `audio_resampler_set_compensation_error()`, `conv_frames_to_time()` (it passes the packet's duration, so nothing in the port divides), the deque, the signal, the muting and the log lines. The shims read the struct's input fields and write its output fields one at a time, so the inputs and the padding keep their bytes. Every sum and difference wraps as the C's `uint64_t` does; the drift error is `cast_signed()` and the sync offset `cast_unsigned()`, the C's conversions. The crate depends on no other crate. Kept, characterized, not endorsed: a timestamp ahead of the clock gives a `timing_adjust` near `u64::MAX`; `timestamp + timing_adjust`, the packet's end, a negative sync offset larger than the packet's system time and a resampler offset larger than it wrap; the distance is not modular; a direct timestamp more than 2 s from the expected one is logged, not a jump. Intentional difference: a null struct pointer does nothing (the C dereferences it; libobs passes one on its stack). `test_source_audio_timing_steps` compiles the C into itself, and `test_source_audio_timing_steps_rust` runs the same test source against `libobs_rust-static` (the first `_rust` variant since `test_scene_bounds_place_rust`: the `obs-source` member links on its own); `test_async_delay_audio`, `test_audio_sync_offset_change`, `test_media_sync_offset_restart`, `test_audio_restart_resampler_tail` and `test_audio_clock_drift` see the swap through the real libobs. Tier 3 compares every byte of the step structs (padding included, from a random fill) over the special values with every flag, proptests of each step, and 2-50-packet sequences through a model of the caller, with the written-back state. | diff --git a/rust/obs-build/tests/native/registration/canonical_video_info_query_is_registered.rs b/rust/obs-build/tests/native/registration/canonical_video_info_query_is_registered.rs index 4373d128a0..34d317f917 100644 --- a/rust/obs-build/tests/native/registration/canonical_video_info_query_is_registered.rs +++ b/rust/obs-build/tests/native/registration/canonical_video_info_query_is_registered.rs @@ -76,9 +76,10 @@ target_link_libraries(reference PRIVATE OBS::libobs) include("{root}/test/cmocka/tests.d/video_info_query_link_support.cmake") function(check_support) get_target_property(adapter_sources video_info_query_link_support SOURCES) - foreach(adapter IN ITEMS encoder-video-tex-active-fields encoder-video-tex-active-mix encoder-get-name output-interleave-name) + foreach(adapter IN ITEMS encoder-video-tex-active-fields encoder-video-tex-active-mix encoder-get-name + output-interleave-name source-audio-capture-callback) if(NOT "{root}/test/cmocka/${{adapter}}-link-support.c" IN_LIST adapter_sources) - message(FATAL_ERROR "missing real encoder adapter source: ${{adapter}}") + message(FATAL_ERROR "missing real private adapter source: ${{adapter}}") endif() endforeach() foreach(name IN ITEMS standalone with_shared future_consumer reference) diff --git a/rust/obs-build/tests/native/registration/rust-static/cases/header/test_audio_capture_callbacks.json b/rust/obs-build/tests/native/registration/rust-static/cases/header/test_audio_capture_callbacks.json new file mode 100644 index 0000000000..36cad8cbce --- /dev/null +++ b/rust/obs-build/tests/native/registration/rust-static/cases/header/test_audio_capture_callbacks.json @@ -0,0 +1,43 @@ +{ + "schema": 1, + "targets": { + "test_audio_capture_callbacks_rust": { + "sources": [ + "test/cmocka/test_source_audio_capture_callbacks.c", + "test/cmocka/source-audio-capture-callback-record-support.c", + "libobs/util/base-variadic.c.h", + "test/cmocka/source-identity-getter-link-stubs.c" + ], + "definitions": [ + "SOURCE_AUDIO_CAPTURE_EXTERNAL", + "obs_source_add_audio_capture_callback=obs_source_add_audio_capture_callback_rust", + "obs_source_remove_audio_capture_callback=obs_source_remove_audio_capture_callback_rust" + ], + "libraries": [ + "libobs_rust-static", + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ], + "includes": [ + ".", + "libobs" + ], + "requires": "HAVE_LIBOBS_RUST_STATIC" + } + }, + "tests": { + "test_audio_capture_callbacks_rust": { + "target": "test_audio_capture_callbacks_rust", + "command": [ + "${target}" + ], + "requires": "HAVE_LIBOBS_RUST_STATIC" + } + } +} diff --git a/rust/obs-c-oracle/oracle/audio_controls.c b/rust/obs-c-oracle/oracle/audio_controls.c index fd4f6bf7f2..c588e997d8 100644 --- a/rust/obs-c-oracle/oracle/audio_controls.c +++ b/rust/obs-c-oracle/oracle/audio_controls.c @@ -34,6 +34,10 @@ #define obs_fader_db_to_def oracle_obs_fader_db_to_def #define signal_handler_connect oracle_audio_controls_signal_handler_connect #define signal_handler_disconnect oracle_audio_controls_signal_handler_disconnect +/* Rust obs-source exports the real registration (#5243); keep these no-op + * stand-ins private to this oracle. */ +#define obs_source_add_audio_capture_callback oracle_audio_controls_add_audio_capture_callback +#define obs_source_remove_audio_capture_callback oracle_audio_controls_remove_audio_capture_callback size_t oracle_obs_audio_info_size(void); size_t oracle_obs_audio_info_align(void); diff --git a/rust/obs-c-oracle/oracle/source_audio_capture_callbacks.c b/rust/obs-c-oracle/oracle/source_audio_capture_callbacks.c new file mode 100644 index 0000000000..056d8c606d --- /dev/null +++ b/rust/obs-c-oracle/oracle/source_audio_capture_callbacks.c @@ -0,0 +1,105 @@ +/* Original C registration and production adapter over real source fixtures (audio-capture list). */ +#include +#include +#include +#include +#define obs_source_add_audio_capture_callback oracle_obs_source_add_audio_capture_callback +#define obs_source_remove_audio_capture_callback oracle_obs_source_remove_audio_capture_callback +#include "obs-source-audio-capture-callbacks.c.h" +#undef obs_source_add_audio_capture_callback +#undef obs_source_remove_audio_capture_callback +#include "source-audio-capture-callback-fields.inc" + +void *oracle_audio_capture_new(void); +void *oracle_audio_capture_new(void) +{ + struct obs_source *source = calloc(1, sizeof(*source)); + if (source && pthread_mutex_init(&source->audio_cb_mutex, NULL) != 0) { + free(source); + return NULL; + } + return source; +} +void oracle_audio_capture_free(obs_source_t *source); +void oracle_audio_capture_free(obs_source_t *source) +{ + da_free(source->audio_cb_list); + pthread_mutex_destroy(&source->audio_cb_mutex); + free(source); +} +size_t oracle_audio_capture_count(obs_source_t *source); +size_t oracle_audio_capture_count(obs_source_t *source) +{ + return source->audio_cb_list.num; +} +struct audio_cb_info oracle_audio_capture_pair(obs_source_t *source, size_t index); +struct audio_cb_info oracle_audio_capture_pair(obs_source_t *source, size_t index) +{ + return source->audio_cb_list.array[index]; +} +bool oracle_audio_capture_unlocked(obs_source_t *source); +bool oracle_audio_capture_unlocked(obs_source_t *source) +{ + if (pthread_mutex_trylock(&source->audio_cb_mutex) != 0) + return false; + pthread_mutex_unlock(&source->audio_cb_mutex); + return true; +} +void oracle_audio_capture_layout(size_t *layout); +void oracle_audio_capture_layout(size_t *layout) +{ + layout[0] = sizeof(struct audio_cb_info); + layout[1] = _Alignof(struct audio_cb_info); + layout[2] = offsetof(struct audio_cb_info, callback); + layout[3] = offsetof(struct audio_cb_info, param); + layout[4] = sizeof(struct darray); + layout[5] = _Alignof(struct darray); + layout[6] = offsetof(struct darray, array); + layout[7] = offsetof(struct darray, num); + layout[8] = offsetof(struct darray, capacity); +} +struct audio_capture_log { + unsigned count; + int level; + char message[256]; +}; +#if defined(__GNUC__) || defined(__clang__) +static void audio_capture_capture(int level, const char *format, va_list args, void *param) + __attribute__((format(printf, 2, 0))); +#endif +static void audio_capture_capture(int level, const char *format, va_list args, void *param) +{ + struct audio_capture_log *log = param; + log->count++; + log->level = level; + vsnprintf(log->message, sizeof(log->message), format, args); +} +typedef void (*audio_capture_operation)(obs_source_t *, obs_source_audio_capture_t, void *); +typedef void (*audio_capture_get_log)(log_handler_t *, void **); +typedef void (*audio_capture_set_log)(log_handler_t, void *); +bool oracle_audio_capture_logs(audio_capture_operation add, audio_capture_operation remove, audio_capture_get_log get, + audio_capture_set_log set); +bool oracle_audio_capture_logs(audio_capture_operation add, audio_capture_operation remove, audio_capture_get_log get, + audio_capture_set_log set) +{ + log_handler_t old; + void *param; + get(&old, ¶m); + struct audio_capture_log r = {0}, c = {0}; + set(audio_capture_capture, &r); + add(NULL, NULL, NULL); + set(audio_capture_capture, &c); + oracle_obs_source_add_audio_capture_callback(NULL, NULL, NULL); + bool match = r.count == 1 && c.count == 1 && r.level == LOG_DEBUG && c.level == LOG_DEBUG && + strcmp(r.message, "obs_source_add_audio_capture_callback: Null 'source' parameter") == 0 && + strcmp(r.message, c.message) == 0; + r.count = c.count = 0; + set(audio_capture_capture, &r); + remove(NULL, NULL, NULL); + set(audio_capture_capture, &c); + oracle_obs_source_remove_audio_capture_callback(NULL, NULL, NULL); + set(old, param); + return match && r.count == 1 && c.count == 1 && r.level == LOG_DEBUG && c.level == LOG_DEBUG && + strcmp(r.message, "obs_source_remove_audio_capture_callback: Null 'source' parameter") == 0 && + strcmp(r.message, c.message) == 0; +} diff --git a/rust/obs-source/src/audio_capture_callbacks.rs b/rust/obs-source/src/audio_capture_callbacks.rs index 918ee0f0db..0e32a31c43 100644 --- a/rust/obs-source/src/audio_capture_callbacks.rs +++ b/rust/obs-source/src/audio_capture_callbacks.rs @@ -9,6 +9,14 @@ pub fn update( add: bool, log: impl FnOnce(&'static CStr), ) { - // RED stub: the registration is not ported yet. - let _ = (registry, pair, add, log); + crate::callback_registry::update( + registry, + pair, + add, + [ + c"obs_source_add_audio_capture_callback", + c"obs_source_remove_audio_capture_callback", + ], + log, + ); } diff --git a/rust/obs-source/src/ffi/callback_list.rs b/rust/obs-source/src/ffi/callback_list.rs new file mode 100644 index 0000000000..53e81396f8 --- /dev/null +++ b/rust/obs-source/src/ffi/callback_list.rs @@ -0,0 +1,78 @@ +//! A source's private C callback darray, held under its mutex, as the safe +//! ordered-registry interface. The darray and its bmem storage stay C-owned. +use crate::callback_registry::Registry; +use core::ffi::{CStr, c_char, c_int, c_void}; +use core::marker::PhantomData; +use obs_util::ffi::darray::{darray, darray_erase, darray_push_back}; + +unsafe extern "C" { + fn blog(level: c_int, format: *const c_char, ...); +} + +/// The C adapters of one list: its address and its mutex operations. +pub(crate) struct Adapters { + pub fields: unsafe extern "C" fn(*mut c_void) -> *mut darray, + pub lock: unsafe extern "C" fn(*mut c_void), + pub unlock: unsafe extern "C" fn(*mut c_void), +} + +pub(crate) struct Locked

{ + source: *mut c_void, + list: *mut darray, + unlock: unsafe extern "C" fn(*mut c_void), + pair: PhantomData

, +} + +impl Registry for Locked

{ + type Pair = P; + fn count(&self) -> usize { + // SAFETY: the source owns this list and its mutex is held. + unsafe { (*self.list).num } + } + fn pair(&self, index: usize) -> P { + assert!(index < self.count()); + // SAFETY: the index is in the initialized list under its mutex; P + // mirrors the C element layout (checked against the real headers). + unsafe { *(*self.list).array.cast::

().add(index) } + } + fn append(&mut self, pair: P) { + // SAFETY: exclusive mutex access; darray uses libobs's bmem allocator. + unsafe { darray_push_back(size_of::

(), self.list, (&pair as *const P).cast()) }; + } + fn erase(&mut self, index: usize) { + // SAFETY: the core chooses an initialized index; the mutex is held. + unsafe { darray_erase(size_of::

(), self.list, index) }; + } +} + +impl

Drop for Locked

{ + fn drop(&mut self) { + // SAFETY: construction acquired this live source's list mutex. + unsafe { (self.unlock)(self.source) }; + } +} + +pub(crate) fn log(name: &'static CStr) { + // SAFETY: static NUL-terminated format and argument match %s. + unsafe { blog(400, c"%s: Null 'source' parameter".as_ptr(), name.as_ptr()) }; +} + +/// Rejects NULL before any adapter, else locks the source's list. +/// +/// # Safety +/// A non-null source must be live, and `adapters` must name its list. +pub(crate) unsafe fn lock

(source: *mut c_void, adapters: &Adapters) -> Option> { + if source.is_null() { + return None; + } + // SAFETY: the caller supplies a live source for these adapters. + unsafe { + (adapters.lock)(source); + Some(Locked { + source, + list: (adapters.fields)(source), + unlock: adapters.unlock, + pair: PhantomData, + }) + } +} diff --git a/rust/obs-source/src/ffi/mod.rs b/rust/obs-source/src/ffi/mod.rs index 6ea9f18609..95040d4415 100644 --- a/rust/obs-source/src/ffi/mod.rs +++ b/rust/obs-source/src/ffi/mod.rs @@ -1,5 +1,7 @@ //! C ABI shims, one module per C file. +mod callback_list; + pub mod source_async_compensation; pub mod source_async_flag_getters; pub mod source_async_pacing; @@ -7,6 +9,7 @@ pub mod source_async_rotation; pub mod source_async_upload; pub mod source_audio_active; pub mod source_audio_active_setter; +pub mod source_audio_capture_callbacks; pub mod source_audio_input; pub mod source_audio_mixers_getter; pub mod source_audio_pause_callbacks; diff --git a/rust/obs-source/src/ffi/source_audio_capture_callbacks.rs b/rust/obs-source/src/ffi/source_audio_capture_callbacks.rs new file mode 100644 index 0000000000..ac58c893b2 --- /dev/null +++ b/rust/obs-source/src/ffi/source_audio_capture_callbacks.rs @@ -0,0 +1,87 @@ +//! Converts the private C audio-capture list to the safe ordered-registry +//! interface. The source mutex, darray and bmem storage stay in C. +use super::callback_list::{self, Adapters}; +use crate::audio_capture_callbacks; +use core::ffi::c_void; +use obs_util::ffi::darray::darray; + +/// `obs_source_audio_capture_t`: (param, source, audio_data, muted). +pub type Callback = Option; +/// `struct audio_cb_info`, with its native pointer alignment. +#[repr(C)] +#[derive(Clone, Copy)] +pub struct Pair { + pub callback: Callback, + pub param: *mut c_void, +} +impl PartialEq for Pair { + fn eq(&self, other: &Self) -> bool { + self.callback.map(|f| f as usize) == other.callback.map(|f| f as usize) + && self.param == other.param + } +} +impl Eq for Pair {} +unsafe extern "C" { + fn obs_source_audio_capture_callback_fields(source: *mut c_void) -> *mut darray; + fn obs_source_audio_capture_callback_lock(source: *mut c_void); + fn obs_source_audio_capture_callback_unlock(source: *mut c_void); +} +const ADAPTERS: Adapters = Adapters { + fields: obs_source_audio_capture_callback_fields, + lock: obs_source_audio_capture_callback_lock, + unlock: obs_source_audio_capture_callback_unlock, +}; +unsafe fn update(source: *mut c_void, pair: Pair, add: bool) { + // SAFETY: caller supplies a live source or NULL; NULL takes no adapter. + let mut locked = unsafe { callback_list::lock::(source, &ADAPTERS) }; + audio_capture_callbacks::update(locked.as_mut(), pair, add, callback_list::log); +} +/// # Safety +/// A non-null source must be live; callback and param follow the C API contract. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn obs_source_add_audio_capture_callback( + source: *mut c_void, + callback: Callback, + param: *mut c_void, +) { + // SAFETY: the private entry has the same contract as this API. + unsafe { obs_source_add_audio_capture_callback_rust(source, callback, param) }; +} +/// # Safety +/// A non-null source must be live; callback and param follow the C API contract. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn obs_source_remove_audio_capture_callback( + source: *mut c_void, + callback: Callback, + param: *mut c_void, +) { + // SAFETY: the private entry has the same contract as this API. + unsafe { obs_source_remove_audio_capture_callback_rust(source, callback, param) }; +} +/// Private static-link entry: shared libobs must never satisfy a fixture's +/// Rust registration before its archive member is selected. +/// +/// # Safety +/// As obs_source_add_audio_capture_callback. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn obs_source_add_audio_capture_callback_rust( + source: *mut c_void, + callback: Callback, + param: *mut c_void, +) { + // SAFETY: forward the caller's source lifetime contract; NULL is accepted. + unsafe { update(source, Pair { callback, param }, true) }; +} +/// Private static-link entry, as the add entry. +/// +/// # Safety +/// As obs_source_remove_audio_capture_callback. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn obs_source_remove_audio_capture_callback_rust( + source: *mut c_void, + callback: Callback, + param: *mut c_void, +) { + // SAFETY: forward the caller's source lifetime contract; NULL is accepted. + unsafe { update(source, Pair { callback, param }, false) }; +} diff --git a/rust/obs-source/src/ffi/source_caption_callbacks.rs b/rust/obs-source/src/ffi/source_caption_callbacks.rs index f4798e7069..33f645b340 100644 --- a/rust/obs-source/src/ffi/source_caption_callbacks.rs +++ b/rust/obs-source/src/ffi/source_caption_callbacks.rs @@ -1,7 +1,8 @@ //! Converts the private C list to the safe ordered-registry interface. -use crate::caption_callbacks::{self, Registry}; -use core::ffi::{CStr, c_char, c_int, c_void}; -use obs_util::ffi::darray::{darray, darray_erase, darray_push_back}; +use super::callback_list::{self, Adapters}; +use crate::caption_callbacks; +use core::ffi::c_void; +use obs_util::ffi::darray::darray; pub type Callback = Option; #[repr(C)] @@ -21,56 +22,16 @@ unsafe extern "C" { fn obs_source_caption_callback_fields(source: *mut c_void) -> *mut darray; fn obs_source_caption_callback_lock(source: *mut c_void); fn obs_source_caption_callback_unlock(source: *mut c_void); - fn blog(level: c_int, format: *const c_char, ...); -} -struct Locked { - source: *mut c_void, - list: *mut darray, -} -impl Registry for Locked { - type Pair = Pair; - fn count(&self) -> usize { - // SAFETY: the source owns this list and its mutex is held. - unsafe { (*self.list).num } - } - fn pair(&self, index: usize) -> Pair { - assert!(index < self.count()); - // SAFETY: the index is in the initialized list under its mutex. - unsafe { *(*self.list).array.cast::().add(index) } - } - fn append(&mut self, pair: Pair) { - // SAFETY: exclusive mutex access; darray uses libobs's bmem allocator. - unsafe { darray_push_back(size_of::(), self.list, (&pair as *const Pair).cast()) }; - } - fn erase(&mut self, index: usize) { - // SAFETY: the core chooses an initialized index; the mutex is held. - unsafe { darray_erase(size_of::(), self.list, index) }; - } -} -impl Drop for Locked { - fn drop(&mut self) { - // SAFETY: construction acquired this live source's caption mutex. - unsafe { obs_source_caption_callback_unlock(self.source) }; - } -} -fn log(name: &'static CStr) { - // SAFETY: static NUL-terminated format and argument match %s. - unsafe { blog(400, c"%s: Null 'source' parameter".as_ptr(), name.as_ptr()) }; } +const ADAPTERS: Adapters = Adapters { + fields: obs_source_caption_callback_fields, + lock: obs_source_caption_callback_lock, + unlock: obs_source_caption_callback_unlock, +}; unsafe fn update(source: *mut c_void, pair: Pair, add: bool) { - let mut locked = if source.is_null() { - None - } else { - // SAFETY: caller supplies a live source; reject NULL before any adapter. - unsafe { - obs_source_caption_callback_lock(source); - Some(Locked { - source, - list: obs_source_caption_callback_fields(source), - }) - } - }; - caption_callbacks::update(locked.as_mut(), pair, add, log); + // SAFETY: caller supplies a live source or NULL; NULL takes no adapter. + let mut locked = unsafe { callback_list::lock::(source, &ADAPTERS) }; + caption_callbacks::update(locked.as_mut(), pair, add, callback_list::log); } /// # Safety /// A non-null source must be live; callback and param follow the C API contract. diff --git a/rust/obs-source/tests/source/audio_capture_callbacks_parity.rs b/rust/obs-source/tests/source/audio_capture_callbacks_parity.rs new file mode 100644 index 0000000000..4ff213ede8 --- /dev/null +++ b/rust/obs-source/tests/source/audio_capture_callbacks_parity.rs @@ -0,0 +1,173 @@ +//! Tier 3: original C audio-capture registration over real headers; no +//! intentional differences or exclusions. +use core::ffi::c_void; +use core::mem::{align_of, offset_of, size_of}; +use obs_source::ffi::source_audio_capture_callbacks::{ + Callback, Pair, obs_source_add_audio_capture_callback as add, + obs_source_add_audio_capture_callback_rust as add_rust, + obs_source_remove_audio_capture_callback as remove, + obs_source_remove_audio_capture_callback_rust as remove_rust, +}; +use obs_util::ffi::{ + base::{base_get_log_handler, base_set_log_handler}, + darray::darray, +}; +use proptest::prelude::*; +use std::sync::atomic::{AtomicUsize, Ordering}; +type Operation = unsafe extern "C" fn(*mut c_void, Callback, *mut c_void); +unsafe extern "C" { + fn oracle_audio_capture_new() -> *mut c_void; + fn oracle_audio_capture_free(source: *mut c_void); + fn oracle_audio_capture_count(source: *mut c_void) -> usize; + fn oracle_audio_capture_pair(source: *mut c_void, index: usize) -> Pair; + fn oracle_audio_capture_unlocked(source: *mut c_void) -> bool; + fn oracle_audio_capture_layout(layout: *mut usize); + fn oracle_obs_source_add_audio_capture_callback( + source: *mut c_void, + callback: Callback, + param: *mut c_void, + ); + fn oracle_obs_source_remove_audio_capture_callback( + source: *mut c_void, + callback: Callback, + param: *mut c_void, + ); + fn oracle_audio_capture_logs( + add: Operation, + remove: Operation, + get: unsafe extern "C" fn(*mut *mut c_void, *mut *mut c_void), + set: unsafe extern "C" fn(*mut c_void, *mut c_void), + ) -> bool; +} +struct Fixture(*mut c_void); +impl Fixture { + fn new() -> Self { + // SAFETY: C allocates and initializes a real source audio-capture fixture. + let fixture = Self(unsafe { oracle_audio_capture_new() }); + assert!(!fixture.0.is_null()); + fixture + } +} +impl Drop for Fixture { + fn drop(&mut self) { + // SAFETY: owns the source, list and initialized mutex. + unsafe { oracle_audio_capture_free(self.0) }; + } +} +static INVOKED: AtomicUsize = AtomicUsize::new(0); +unsafe extern "C" fn callback_a(_: *mut c_void, _: *mut c_void, _: *const c_void, _: bool) { + INVOKED.fetch_add(1, Ordering::Relaxed); +} +unsafe extern "C" fn callback_b(_: *mut c_void, _: *mut c_void, _: *const c_void, _: bool) { + INVOKED.fetch_add(2, Ordering::Relaxed); +} +fn check(ops: &[(bool, u8, u8)]) { + // The public exports and the private static-link entries alike. + check_with(ops, add, remove); + check_with(ops, add_rust, remove_rust); +} +fn check_with(ops: &[(bool, u8, u8)], add: Operation, remove: Operation) { + let r = Fixture::new(); + let c = Fixture::new(); + let mut tokens = [0u8; 3]; + for &(insert, cb, param) in ops { + let callback: Callback = match cb % 3 { + 0 => None, + 1 => Some(callback_a), + _ => Some(callback_b), + }; + let param = if param % 4 == 0 { + core::ptr::null_mut() + } else { + (&mut tokens[usize::from(param % 4 - 1)] as *mut u8).cast() + }; + // SAFETY: both real source fixtures and token addresses remain live; + // each operation retains the mutex and C allocator contracts. + unsafe { + if insert { + add(r.0, callback, param); + oracle_obs_source_add_audio_capture_callback(c.0, callback, param); + } else { + remove(r.0, callback, param); + oracle_obs_source_remove_audio_capture_callback(c.0, callback, param); + } + assert!(oracle_audio_capture_unlocked(r.0)); + assert!(oracle_audio_capture_unlocked(c.0)); + let count = oracle_audio_capture_count(r.0); + assert_eq!(count, oracle_audio_capture_count(c.0)); + for i in 0..count { + let rp = oracle_audio_capture_pair(r.0, i); + let cp = oracle_audio_capture_pair(c.0, i); + assert!(rp == cp, "pair {i} differs"); + } + } + assert_eq!(INVOKED.load(Ordering::Relaxed), 0); + } +} +#[test] +fn real_header_layout() { + let mut layout = [0; 9]; + // SAFETY: the C oracle writes exactly nine measured layout values. + unsafe { oracle_audio_capture_layout(layout.as_mut_ptr()) }; + assert_eq!( + layout, + [ + size_of::(), + align_of::(), + offset_of!(Pair, callback), + offset_of!(Pair, param), + size_of::(), + align_of::(), + offset_of!(darray, array), + offset_of!(darray, num), + offset_of!(darray, capacity) + ] + ); +} +#[test] +fn characterized_edges() { + check(&[ + (false, 1, 1), + (true, 1, 1), + (true, 2, 1), + (true, 1, 2), + (true, 1, 1), + (false, 1, 1), + (false, 2, 2), + (false, 1, 2), + (false, 2, 1), + (false, 1, 1), + (true, 0, 0), + (false, 0, 0), + ]); + // Cover growth, every null/non-null pair and repeated first-match erasure. + let mut ops = Vec::new(); + for insert in [true, true, false, false, false] { + for cb in 0..3 { + for param in 0..4 { + ops.push((insert, cb, param)); + } + } + } + check(&ops); +} +#[test] +fn null_diagnostics_match_c() { + if !crate::output::isolated(concat!(module_path!(), "::null_diagnostics_match_c")) { + return; + } + // SAFETY: isolated process owns the log handler; C restores it after probing. + unsafe { + assert!(oracle_audio_capture_logs( + add, + remove, + base_get_log_handler, + base_set_log_handler + )); + } +} +proptest! { + #![proptest_config(ProptestConfig { cases: obs_c_oracle::parity::cases(256), failure_persistence: None, ..ProptestConfig::default() })] + #[test] + fn sequences(ops in prop::collection::vec((any::(), 0u8..3, 0u8..4), 0..128)) { check(&ops); } +} diff --git a/rust/obs-source/tests/source/main.rs b/rust/obs-source/tests/source/main.rs index 9125071625..730a300f65 100644 --- a/rust/obs-source/tests/source/main.rs +++ b/rust/obs-source/tests/source/main.rs @@ -17,6 +17,7 @@ mod audio_active_parity; mod audio_active_setter; mod audio_active_setter_parity; mod audio_capture_callbacks; +mod audio_capture_callbacks_parity; mod audio_input; mod audio_input_parity; mod audio_input_path; diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index c9b5daa111..7b06c1e282 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -605,6 +605,35 @@ "m" ] }, + "test_audio_capture_callbacks_rust": { + "sources": [ + "test/cmocka/test_source_audio_capture_callbacks.c", + "test/cmocka/source-audio-capture-callback-record-support.c", + "libobs/util/base-variadic.c.h", + "test/cmocka/source-identity-getter-link-stubs.c" + ], + "definitions": [ + "SOURCE_AUDIO_CAPTURE_EXTERNAL", + "obs_source_add_audio_capture_callback=obs_source_add_audio_capture_callback_rust", + "obs_source_remove_audio_capture_callback=obs_source_remove_audio_capture_callback_rust" + ], + "libraries": [ + "libobs_rust-static", + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ], + "includes": [ + ".", + "libobs" + ], + "requires": "HAVE_LIBOBS_RUST_STATIC" + }, "test_audio_capture_mix_race": { "sources": [ "test/cmocka/test_audio_capture_mix_race.c" @@ -22260,6 +22289,13 @@ "test_audio_buffering_subsample": { "target": "test_audio_buffering_subsample" }, + "test_audio_capture_callbacks_rust": { + "target": "test_audio_capture_callbacks_rust", + "command": [ + "${target}" + ], + "requires": "HAVE_LIBOBS_RUST_STATIC" + }, "test_audio_capture_mix_race": { "target": "test_audio_capture_mix_race", "command": [ diff --git a/test/cmocka/source-audio-capture-callback-link-support.c b/test/cmocka/source-audio-capture-callback-link-support.c new file mode 100644 index 0000000000..1a4eb79a51 --- /dev/null +++ b/test/cmocka/source-audio-capture-callback-link-support.c @@ -0,0 +1,2 @@ +/* Real private audio-capture list adapters for Rust-static consumers. */ +#include "libobs/obs-source-audio-capture-callback-fields.c.h" diff --git a/test/cmocka/source-audio-capture-callback-record-support.c b/test/cmocka/source-audio-capture-callback-record-support.c new file mode 100644 index 0000000000..2c94ea8b08 --- /dev/null +++ b/test/cmocka/source-audio-capture-callback-record-support.c @@ -0,0 +1,13 @@ +/* The real private audio-capture adapters, compiled against the recording + * mutex of test_source_audio_capture_callbacks.c, so its unchanged lock and + * mutation-under-lock assertions see the Rust registration (#5243). */ +#include "libobs/build/libobs.pch.h" + +int source_audio_capture_record_lock(pthread_mutex_t *mutex); +int source_audio_capture_record_unlock(pthread_mutex_t *mutex); + +#define pthread_mutex_lock source_audio_capture_record_lock +#define pthread_mutex_unlock source_audio_capture_record_unlock +#include "libobs/obs-source-audio-capture-callback-fields.c.h" +#undef pthread_mutex_unlock +#undef pthread_mutex_lock diff --git a/test/cmocka/tests.d/test_audio_capture_callbacks.cmake b/test/cmocka/tests.d/test_audio_capture_callbacks.cmake new file mode 100644 index 0000000000..9ec8faa933 --- /dev/null +++ b/test/cmocka/tests.d/test_audio_capture_callbacks.cmake @@ -0,0 +1,5 @@ +# Registered in the canonical native test graph (test/cmocka/build/test-graph.json). +# test_audio_capture_callbacks_rust runs the unchanged test_source_audio_capture_callbacks.c +# against libobs_rust-static. It sits outside the test_source_ prefix so the source-step +# fragments do not give it their aborting blog stub: it links the real variadic blog +# (base-variadic.c.h) over the Rust log handler and checks the real diagnostics. diff --git a/test/cmocka/tests.d/video_info_query_link_support.cmake b/test/cmocka/tests.d/video_info_query_link_support.cmake index 7e72d5f80c..8193568b98 100644 --- a/test/cmocka/tests.d/video_info_query_link_support.cmake +++ b/test/cmocka/tests.d/video_info_query_link_support.cmake @@ -11,6 +11,7 @@ function(_video_info_query_link_support) "${CMAKE_CURRENT_SOURCE_DIR}/output-interleave-name-link-support.c" "${CMAKE_CURRENT_SOURCE_DIR}/encoder-video-tex-active-fields-link-support.c" "${CMAKE_CURRENT_SOURCE_DIR}/encoder-video-tex-active-mix-link-support.c" + "${CMAKE_CURRENT_SOURCE_DIR}/source-audio-capture-callback-link-support.c" ) target_include_directories( video_info_query_link_support