Skip to content

libobs: Port the PulseAudio monitor format and volume to obs-audio-monitoring - #1721

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-pulse-format
Oct 22, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-pulse-format

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1697 (PR 3 of 3; #1698 moved the code into libobs/audio-monitoring/pulse/pulseaudio-monitor-format.c.h and added test_pulse_monitor_format, #1709 added the integer volume saturation and fixed the %c log)

What and why

This PR ports libobs/audio-monitoring/pulse/pulseaudio-monitor-format.c.h to a new pulse_format module of rust/obs-audio-monitoring, the crate that already has the stream flags (#1513). On Linux and FreeBSD, audio monitoring plays each monitored source on the chosen sink through PulseAudio. It plays in the sink's spec when OBS can produce it, and it applies the source's volume itself, in that format. Everything that calls pa_* stays C. The hybrid libobs drops the C file and links the Rust shims in its place. The port copies the C as #1709 fixed it, so Tier 1 and Tier 3 carry no "characterized, not endorsed" cases.

  • Safe core (src/pulse_format.rs):
    • channels_to_speakers, to_obs_audio_format and channel_map map formats and channel counts. channel_map returns the channel count and map[0..8], filled past the count in the default order, as the C does.
    • sink_format and sink_channels are the sink fallbacks: an unsupported format plays as FLOAT32LE, and an unsupported channel count (0, 7, more than 8) plays as stereo.
    • scale_u8, scale_s16, scale_s32 and scale_f32 convert the volume per format. Each one uses the C's f32 operations in the C's order (int → f32 → multiply, and for U8 the + 128 added in float). The libobs: Saturate the PulseAudio monitor volume on integer sinks #1709 saturation is written out as the C's comparisons: >= 255.0, >= 32767.0, >= 2^31 and their lower bounds. Inside the range, the result is the product truncated toward zero, which as gives exactly. as would also saturate outside the range, but the rule is spelled out so it is the C's rule, not Rust's. FLOAT32LE is not clamped (the server clips).
    • process_volume(format, channels, frames, vol, &mut [u8]) takes the sample buffer as bytes and reads and writes samples in native byte order, as the C's pointer casts do, so it needs no alignment. It never touches more than the slice holds.
    • queued_bytes and drop_split are the backlog arithmetic. The int64_t and uint64_t arithmetic wraps as in C. util_mul_div64(x, y, 1000000) is computed as the C computes it on each host (obs_util::platform::MUL_DIV64_IS_UDIV128), but without obs_util's panics: the divisor is a constant.
    • devices_match compares source with output + ".monitor" as chained byte iterators, so it does not allocate. The C builds both strings with bzalloc and strcat. "default" stands for the default sink, and ids are bytes, not UTF-8.
    • The module denies clippy's arithmetic_side_effects, indexing_slicing, unwrap_used, expect_used, panic, todo, unimplemented and unreachable, as libobs: Port the hotkey binding codec to obs-hotkey #1628 does. It has no unsafe, no host cfg and no libpulse dependency. The pa_sample_format_t, pa_channel_position_t, enum audio_format and enum speaker_layout values are Rust constants, which the parity test checks against the headers.
  • Shim (src/ffi/pulse_format.rs): #[no_mangle] functions with the C signatures for the nine functions of the header.
    • pa_sample_format_t and pa_channel_position_t cross as int because they have negative values. enum speaker_layout and enum audio_format cross as unsigned int, the type GCC and Clang give them.
    • uint_fast32_t and uint_fast8_t have the host's width: 64-bit uint_fast32_t on 64-bit glibc and Bionic, and 32-bit uint_fast8_t on FreeBSD and OpenBSD. The layout test compares both with the C. (src/ffi/ is inside the platform boundary.)
    • pa_channel_map is mirrored as #[repr(C)] and returned by value, as is struct pulseaudio_monitor_drop. Both get layout tests. (The issue planned a C filler for the map; a mirror with a layout test keeps the hybrid free of C.)
    • pulseaudio_monitor_process_volume takes resample_frames * channels from its caller with checked arithmetic, and it never computes a larger count. As in C, it reads resample_data only for the four formats it changes.
  • C side: pulseaudio-monitor-format.c.h leaves libobs.pulse for its own group, libobs.ported-pulse-format. The group is listed in LIBOBS_PULSE_PORTED_SOURCES next to libobs.ported-pulse, so the existing ENABLE_PULSEAUDIO wiring in os-linux.cmake and os-freebsd.cmake, rust/tools/ported_sources.py and obs-build pick it up unchanged. Its PCH, libobs-ported-pulse-format.pch.h, holds the file's own section (<string.h>, util/bmem.h, util/util_uint64.h and the header), so the oracle and the C tests compile the file without the core. libobs-pulse.pch.h loses that section. The output section keeps the format header, because pulseaudio-output.c and pulseaudio-wrapper.c call these functions. The only change to the file is its include. ci/test_ported_sources.py now asserts the new object as well.
  • Symbols: the C file has no EXPORT, so the nine shims are hidden, as in libobs: Port the PulseAudio stream flags to obs-audio-monitoring #1513. They are listed in rust-hidden.txt, rust-exports.map (local:) and rust-unexports-macos.txt, and no rust-exports.d fragment is added (export_list passes). nm shows them as local t in all three libraries, and the exported symbol sets are unchanged (1766).
  • Oracle (oracle/pulse_format.c): the original file, unmodified, with its nine functions renamed. It also has fixed-width entry points for the two uint_fast* functions, the 41 enum values, and the layouts and widths. <pulse/sample.h> and <pulse/channelmap.h> get stand-ins in oracle/pulse-shim/, in the style of libobs: Port the PulseAudio stream flags to obs-audio-monitoring #1513's def.h. They forward to libpulse's headers where those are installed and define the subset the oracle needs elsewhere (MSVC, macOS, a runner without libpulse-dev). Locally the oracle used libpulse's headers. I also built the oracle with the stand-ins forced on: the 41 values and the layout came out identical to libpulse 17's, with -Wall -Wextra -Wpedantic -Werror under both GCC and Clang.
  • Intentional differences (listed in the parity test header and in rust/README.md). Each one is an input on which the C crashes or reads an indeterminate value, and its only caller never produces it:
    • map[8..] of the returned channel map, which C never writes, is PA_CHANNEL_POSITION_INVALID;
    • a null resample_data (or resample_data[0]) with samples to process does nothing (C dereferences it);
    • drop_split with bytes_per_frame == 0 drops nothing (C divides by zero);
    • devices_match with an id of "default" and a null default_sink is no match (C calls strlen(NULL)).
  • Undefined in C, documented, not compared: a NaN volume on an integer format. C converts NaN to an integer: on x86-64, the U8 and S16 results are 0 and the S32 result is INT32_MIN. Rust gives 0. A signed overflow of the int64_t index arithmetic is also left out. Real indices never get near it.
  • Docs: the rust/README.md rows. The docs/rust-port/naming.md row for libobs/audio-monitoring/ already covers the crate. There is no docs/features.md line because nothing changes for users.

test_pulse_monitor_format and test_pulse_default_id compile the C into themselves and are unchanged. They pass in every lane, but they do not see the swap. The real-stream tests (test_pulse_monitor_device, test_pulse_monitor_reset*, test_pulse_stream_restart, test_pulse_two_sources) run the swapped format, channel-map, fallback, backlog and device-match code inside the hybrid libobs against a private PipeWire server. None of them checks sample values, though, so for the volume path see the end-to-end run below.

RED → GREEN

  • RED commit: fbeb062b3 libobs: Add Tier 1 tests for the PulseAudio monitor format port. The safe core has todo!() bodies. soldr cargo test -p obs-data --test obs-tests-core audio_monitoring:: fails 20 of 48 with "not yet implemented": all 20 new tests fail, and the 28 existing tests pass.
    • Tier 1 (tests/audio_monitoring/pulse_format.rs) ports all 12 cases of test_pulse_monitor_format 1:1. Each case is one test under the C case's name, with the same inputs and expectations, including the saturation rows and the guard samples of apply(). The C test's pa_channel_map_valid check is written out: 1 to PA_CHANNELS_MAX channels, each a position in 0..PA_CHANNEL_POSITION_MAX.
    • 8 new edge cases:
      • layouts, counts and formats far out of range (u64::MAX, 1 << 32 | 2, u32::MAX, i32::MIN);
      • the sample sizes;
      • the clamp bounds themselves and the first values inside them (U8 255.0 and 0.0, S16 ±32767/-32768, S32 2^31 and 2147483520, which is the largest f32 below it);
      • NaN and infinite volumes (Rust's own result);
      • a buffer shorter than frames * channels, an overflowing frame count, and 0 frames or 0 channels;
      • the wrapping queued-bytes sum and difference;
      • a zero or oversized frame size;
      • device ids that nearly match "default" or .monitor, ids that are not UTF-8, and "default" with no default sink.
    • The tests are modules of the existing core category binary, obs-tests-core. There is no new test binary: ci/test_binary_cap.py passes.
  • GREEN: 8c0134972 libobs: Port the PulseAudio monitor format and volume to obs-audio-monitoring. audio_monitoring:: passes 58/58 (20 Tier 1, 10 Tier 2/3, and the 28 existing tests).
    • Layout: the size, alignment and both offsets of pa_channel_map and of struct pulseaudio_monitor_drop, and the widths of uint_fast32_t and uint_fast8_t, all compared with the C. The 41 enum values are also compared with the oracle.
    • Tier 3 (tests/audio_monitoring/pulse_format_parity.rs) compares every output byte: the whole sample buffer with its guard samples, the channel count and map[0..8], and both halves of the drop split.
      • Fixed cases: every test_pulse_monitor_format input plus the Tier 1 edge cases. These cover all 256 U8 values and the S16 and S32 edge samples, at 14 volumes, 1-8 channels and 0-32 frames; every format, channel count 0-300 and layout 0-40; and every drop split at every frame remainder up to 32.
      • Proptests, 2000 cases each:
        • volumes: weighted toward the four played formats, plus any format. Channels 1-8, 0 or any u8; up to 1024 frames. Volumes from 0 to 20, the special values and a few ulp around 0.5, 1, 2, 4 and +26 dB. Random sample bytes, or the loud S16 and S32 values, or random floats, NaN bit patterns included;
        • formats, channel counts (any u64), sink channels and layouts;
        • queued bytes, without signed overflow;
        • drop splits, where the 128-bit quotient fits (MSVC's _udiv128 faults past that);
        • device ids from a small colliding alphabet with .monitor, "default" and its near misses, or any bytes, with default_sink never null when an id is "default".
      • nan_volume_documented runs NaN through both sides and asserts only Rust's result. The C gives 00, 00 00 and 00 00 00 80 here.
    • Deliberate-break check: in Rust I dropped the S16 saturation and put back the pre-libobs: Saturate the PulseAudio monitor volume on integer sinks #1709 wrap (v as i32 as i16; a bare as would still saturate). Then I restored it.
      • Tier 1 and Tier 3: 4 of 30 failed: volume_s16 (the ported C case) and nan_and_infinite_volume (edge case) in Tier 1, and cmocka_cases_match_oracle and random_volumes_match_oracle in Tier 3. The proptest shrank to format S16LE, 1 channel, 1 frame, vol 9.599313.
      • Real libobs: I rebuilt the hybrid libobs with the break and ran an end-to-end probe (below). It caught the break: S16LE at volume 2 recorded -16384 instead of +32767. All 14 existing test_pulse* ctests still passed, because none of them checks sample values.
      • After the restore, everything passes again.
    • End to end, with the hybrid libobs and libobs-c-reference: I ran a local probe through test_pulse_monitor_device.sh (a private PipeWire, WirePlumber and pipewire-pulse server, no hardware) under private Xvfb. A monitored source played a constant 0.75 on a null sink, and the sink's monitor was recorded with parec. The probe is not part of this PR. Results:
Sink format Volume hybrid libobs libobs-c-reference
s16le 0.5 12288 12288
s16le 2.0 32767 (saturated) 32767
u8 0.5 12288 12288
u8 2.0 32512 (255, saturated) 32512
s32le 0.5 12288 12288
s32le 2.0 32767 (saturated) 32767
float32le 0.5 12288 12288
float32le 2.0 32767 (server clip) 32767
s16le, deliberate break 2.0 -16384 (wrapped) 32767
Lane Library ctest Exported symbols
OBS_LIBOBS_BACKEND=C libobs 358/358 1766
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 359/359 1766, IDENTICAL
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 359/359 1766, IDENTICAL
  • No test was skipped. The private-server tests ran in every lane: test_pulse_monitor_device, the five test_pulse_monitor_reset*, test_pulse_stream_restart and test_pulse_two_sources.
  • test_pulse_monitor_format (12/12), test_pulse_default_id (2/2), test_pulse_monitor_flags (5/5) and test_pulse_monitor_backlog (6/6) passed against all three libraries.
  • Ninja's inputs: the hybrid libobs.so does not link libobs.ported-pulse-format+.h.c.o, and libobs-c-reference does.
  • The suites ran with an extracted cmocka 2.0.2 (CMake found it through CMocka_DIR; ctest ran with LD_LIBRARY_PATH pointing at its lib).
  • Environment: headless (private Xvfb, private D-Bus, fresh HOME).
  • ASan: audio_monitoring::pulse_format and pulse_format_parity (Tier 1 and 3, every proptest) passed 30/30 under AddressSanitizer with no reports. The Rust side was built with -Zsanitizer=address, and the C oracle with -fsanitize=address.
  • ci/local_gate.py:
    • --lane lint: passed, after --fix rewrapped two oracle prototypes;
    • --lane cross: passed;
    • --lane cross-release: passed;
    • --lane rust-test: passed (2050 tests, 1 skipped, the lane's usual one), with export_list.
  • ci/test_build_groups.py, ci/test_naming.py, ci/test_ported_sources.py, ci/test_rust_scope.py, ci/test_binary_cap.py, ci/test_cmocka_includes.py and ci/test_platform_ifdef_ratchet.py: OK.
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change). The volume path ran end to end through the real hybrid libobs in the probe above.

FreeBSD, Windows, macOS: not built there. FreeBSD is the one other host that calls the shims; there uint_fast8_t is 32 bits, and the layout test checks that width on any host that runs it. Windows and macOS build the shims unused and hidden, and the oracle uses its stand-in headers there.

Checklist

The next unit of obs-audio-monitoring is
audio-monitoring/pulse/pulseaudio-monitor-format.c.h: the monitoring
stream's sample format and channel map, the volume it applies in the
sink format (with the integer saturation from obs-rust#1709), the catch-up
arithmetic after a stall, and the feedback-loop device match.

This adds the safe core's API as pulse_format, with the libpulse and
libobs enum values it uses and todo!() bodies, and its Tier 1 tests in
the existing core category binary (obs-tests-core, no new binary). The
12 cases of test_pulse_monitor_format are ported 1:1, one test each
under the C case's name, guard samples included. New cases: layouts,
counts and formats far out of range, the sample sizes, the clamp
bounds themselves and the first values inside them, NaN and infinite
volumes (Rust's own result; NaN is undefined in C), a buffer shorter
than frames * channels, the wrapping of the queued-bytes sum and
difference, a zero frame size, and device ids that nearly match,
are not UTF-8 or say "default" with no default sink.

audio_monitoring::pulse_format fails 20 of 20 with "not yet
implemented".

Refs obs-rust#1697
…nitoring

The safe core, pulse_format, takes the sink format and channel count
to the OBS format, speaker layout and channel map, picks the fallback
spec (FLOAT32LE, stereo), applies the volume per format, does the
queued-bytes and drop-split arithmetic, and matches a source against
the monitoring output's .monitor device. Sample buffers are byte
slices, read and written in native order, so no alignment is needed.
The volume is the C's f32 arithmetic with the saturation of obs-rust#1709
written out as the C's comparisons, so in-range results are the
truncated product, bit for bit. The module denies clippy's panic,
indexing and arithmetic lints; util_mul_div64 is computed without
obs_util's panics (the divisor is a constant).

The nine functions get #[no_mangle] shims with the C signatures,
uint_fast8_t and uint_fast32_t at the host's width and pa_channel_map
mirrored, all hidden like obs-rust#1513's. The C file moves from libobs.pulse
to its own group, libobs.ported-pulse-format, listed under
LIBOBS_PULSE_PORTED_SOURCES, so only the C targets compile it. The
exported symbols are unchanged.

Tier 3 compares the shims with the original C (oracle/pulse_format.c)
byte for byte: every test_pulse_monitor_format input, then proptests
over formats, channel counts, layouts, random sample buffers with
guards, volumes from 0 to 20, the backlog numbers and device ids. The
oracle reads libpulse's headers where installed, else stand-ins with
the same values. Intentional differences, all inputs that crash the
C: map[8..] (indeterminate in C) is PA_CHANNEL_POSITION_INVALID, and a
null sample buffer, a zero frame size and a "default" id with a null
default sink do nothing, drop nothing and do not match.

Fixes obs-rust#1697
@rdoupe
rdoupe requested a review from zackees as a code owner October 22, 2026 05:14
@zackees
zackees enabled auto-merge October 22, 2026 05:41
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 22, 2026
…at (obs-rust#1728)

None of the PulseAudio tests looked at sample values: with the s16
saturation (obs-rust#1709) replaced by a wrap in the Rust port of obs-rust#1721, all 14
test_pulse* tests still passed in hybrid.

test_pulse_monitor_samples runs libobs monitoring against the private
PipeWire server of test_pulse_monitor_device.sh. For null sinks in s16le,
s32le, u8 and float32le it plays a constant +0.75 / -0.75 at source
volumes 0.5, 1 and 2 and records the sink's monitor with parec. Each
expected level must be in the recording for at least 0.1 s, wherever it
starts: the integer formats at their rail at volume 2, float at 1.5. It
skips (77) without PipeWire, the pulse tools or parec.

Refs obs-rust#1697
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
Merged via the queue into obs-rust:master with commit 7d06fad Oct 22, 2026
75 of 81 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

rust: Port the PulseAudio monitor format and volume code to obs-audio-monitoring

2 participants