Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions libobs/audio-monitoring/null/null-audio-monitoring-reference.c.h
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 9 additions & 10 deletions libobs/audio-monitoring/null/null-audio-monitoring.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
24 changes: 24 additions & 0 deletions libobs/audio-monitoring/null/null-audio-monitoring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <stdbool.h>

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);
3 changes: 3 additions & 0 deletions libobs/build/libobs-nix.pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@
/* access() in log_system_info; came with util/platform-nix-inhibit.c,
* which is in its own group now. */
#include <unistd.h>

/* audio-monitoring/null/null-audio-monitoring.c */
#include "audio-monitoring/null/null-audio-monitoring.h"
12 changes: 12 additions & 0 deletions libobs/build/libobs-ported-null-audio-monitoring.pch.h
Original file line number Diff line number Diff line change
@@ -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 <stddef.h>

#include "libobs/audio-monitoring/null/null-audio-monitoring.h"
#include "libobs/util/c99defs.h"
7 changes: 7 additions & 0 deletions libobs/build/libobs.ported-null-audio-monitoring+.h.c
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"LIBOBS_LINUX_PORTED_SOURCES": [
"build/libobs.ported-null-audio-monitoring+.h.c"
],
"headers": [
"audio-monitoring/null/null-audio-monitoring.h"
]
}
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<pulse/def.h>` 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. |
Expand Down
2 changes: 1 addition & 1 deletion rust/obs-audio-monitoring/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions rust/obs-audio-monitoring/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
73 changes: 73 additions & 0 deletions rust/obs-audio-monitoring/src/ffi/null_monitoring.rs
Original file line number Diff line number Diff line change
@@ -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<unsafe extern "C" fn(*mut c_void, *const c_char, *const c_char) -> 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)
}
Loading
Loading