Repository navigation
libobs: Port the PulseAudio monitor format and volume to obs-audio-monitoring - #1721
Merged
Merged
Conversation
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
zackees
enabled auto-merge
October 22, 2026 05:41
4 of 7 tasks
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
6 of 7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1697 (PR 3 of 3; #1698 moved the code into
libobs/audio-monitoring/pulse/pulseaudio-monitor-format.c.hand addedtest_pulse_monitor_format, #1709 added the integer volume saturation and fixed the%clog)What and why
This PR ports
libobs/audio-monitoring/pulse/pulseaudio-monitor-format.c.hto a newpulse_formatmodule ofrust/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 callspa_*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.src/pulse_format.rs):channels_to_speakers,to_obs_audio_formatandchannel_mapmap formats and channel counts.channel_mapreturns the channel count andmap[0..8], filled past the count in the default order, as the C does.sink_formatandsink_channelsare 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_s32andscale_f32convert the volume per format. Each one uses the C's f32 operations in the C's order (int → f32 → multiply, and for U8 the+ 128added 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^31and their lower bounds. Inside the range, the result is the product truncated toward zero, whichasgives exactly.aswould 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_bytesanddrop_splitare the backlog arithmetic. Theint64_tanduint64_tarithmetic 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 withoutobs_util's panics: the divisor is a constant.devices_matchcomparessourcewithoutput + ".monitor"as chained byte iterators, so it does not allocate. The C builds both strings withbzallocandstrcat."default"stands for the default sink, and ids are bytes, not UTF-8.arithmetic_side_effects,indexing_slicing,unwrap_used,expect_used,panic,todo,unimplementedandunreachable, as libobs: Port the hotkey binding codec to obs-hotkey #1628 does. It has nounsafe, no hostcfgand no libpulse dependency. Thepa_sample_format_t,pa_channel_position_t,enum audio_formatandenum speaker_layoutvalues are Rust constants, which the parity test checks against the headers.src/ffi/pulse_format.rs):#[no_mangle]functions with the C signatures for the nine functions of the header.pa_sample_format_tandpa_channel_position_tcross asintbecause they have negative values.enum speaker_layoutandenum audio_formatcross asunsigned int, the type GCC and Clang give them.uint_fast32_tanduint_fast8_thave the host's width: 64-bituint_fast32_ton 64-bit glibc and Bionic, and 32-bituint_fast8_ton FreeBSD and OpenBSD. The layout test compares both with the C. (src/ffi/is inside the platform boundary.)pa_channel_mapis mirrored as#[repr(C)]and returned by value, as isstruct 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_volumetakesresample_frames * channelsfrom its caller with checked arithmetic, and it never computes a larger count. As in C, it readsresample_dataonly for the four formats it changes.pulseaudio-monitor-format.c.hleaveslibobs.pulsefor its own group,libobs.ported-pulse-format. The group is listed inLIBOBS_PULSE_PORTED_SOURCESnext tolibobs.ported-pulse, so the existingENABLE_PULSEAUDIOwiring inos-linux.cmakeandos-freebsd.cmake,rust/tools/ported_sources.pyand 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.hand the header), so the oracle and the C tests compile the file without the core.libobs-pulse.pch.hloses that section. The output section keeps the format header, becausepulseaudio-output.candpulseaudio-wrapper.ccall these functions. The only change to the file is its include.ci/test_ported_sources.pynow asserts the new object as well.EXPORT, so the nine shims are hidden, as in libobs: Port the PulseAudio stream flags to obs-audio-monitoring #1513. They are listed inrust-hidden.txt,rust-exports.map(local:) andrust-unexports-macos.txt, and norust-exports.dfragment is added (export_listpasses).nmshows them as localtin all three libraries, and the exported symbol sets are unchanged (1766).oracle/pulse_format.c): the original file, unmodified, with its nine functions renamed. It also has fixed-width entry points for the twouint_fast*functions, the 41 enum values, and the layouts and widths.<pulse/sample.h>and<pulse/channelmap.h>get stand-ins inoracle/pulse-shim/, in the style of libobs: Port the PulseAudio stream flags to obs-audio-monitoring #1513'sdef.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 -Werrorunder both GCC and Clang.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, isPA_CHANNEL_POSITION_INVALID;resample_data(orresample_data[0]) with samples to process does nothing (C dereferences it);drop_splitwithbytes_per_frame == 0drops nothing (C divides by zero);devices_matchwith an id of"default"and a nulldefault_sinkis no match (C callsstrlen(NULL)).INT32_MIN. Rust gives 0. A signed overflow of theint64_tindex arithmetic is also left out. Real indices never get near it.rust/README.mdrows. Thedocs/rust-port/naming.mdrow forlibobs/audio-monitoring/already covers the crate. There is nodocs/features.mdline because nothing changes for users.test_pulse_monitor_formatandtest_pulse_default_idcompile 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
fbeb062b3libobs: Add Tier 1 tests for the PulseAudio monitor format port. The safe core hastodo!()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.tests/audio_monitoring/pulse_format.rs) ports all 12 cases oftest_pulse_monitor_format1: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 ofapply(). The C test'spa_channel_map_validcheck is written out: 1 toPA_CHANNELS_MAXchannels, each a position in0..PA_CHANNEL_POSITION_MAX.u64::MAX,1 << 32 | 2,u32::MAX,i32::MIN);frames * channels, an overflowing frame count, and 0 frames or 0 channels;"default"or.monitor, ids that are not UTF-8, and"default"with no default sink.obs-tests-core. There is no new test binary:ci/test_binary_cap.pypasses.8c0134972libobs: 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).pa_channel_mapand ofstruct pulseaudio_monitor_drop, and the widths ofuint_fast32_tanduint_fast8_t, all compared with the C. The 41 enum values are also compared with the oracle.tests/audio_monitoring/pulse_format_parity.rs) compares every output byte: the whole sample buffer with its guard samples, the channel count andmap[0..8], and both halves of the drop split.test_pulse_monitor_formatinput 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.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;u64), sink channels and layouts;_udiv128faults past that);.monitor,"default"and its near misses, or any bytes, withdefault_sinknever null when an id is"default".nan_volume_documentedruns NaN through both sides and asserts only Rust's result. The C gives00,00 00and00 00 00 80here.v as i32 as i16; a bareaswould still saturate). Then I restored it.volume_s16(the ported C case) andnan_and_infinite_volume(edge case) in Tier 1, andcmocka_cases_match_oracleandrandom_volumes_match_oraclein Tier 3. The proptest shrank to format S16LE, 1 channel, 1 frame, vol 9.599313.-16384instead of+32767. All 14 existingtest_pulse*ctests still passed, because none of them checks sample values.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 withparec. The probe is not part of this PR. Results:5db4658e1(rust: port scene bounds placement to obs-scene #1661 and docs(ci): record trusted Flatpak publication and native retention gaps #1711 landed):audio_monitoring::58/58,export_list, both lanes rebuilt, exported symbols 1766 and IDENTICAL in all three, and the 14test_pulse*ctests pass against all three libraries.ENABLE_PULSEAUDIO=ON:OBS_LIBOBS_BACKEND=C, andHYBRIDwithOBS_BUILD_C_REFERENCE=ON. Fullctest -j4:OBS_LIBOBS_BACKEND=CHYBRID,OBS_BUILD_C_REFERENCE=ONHYBRID,OBS_BUILD_C_REFERENCE=ONtest_pulse_monitor_device, the fivetest_pulse_monitor_reset*,test_pulse_stream_restartandtest_pulse_two_sources.test_pulse_monitor_format(12/12),test_pulse_default_id(2/2),test_pulse_monitor_flags(5/5) andtest_pulse_monitor_backlog(6/6) passed against all three libraries.libobs.sodoes not linklibobs.ported-pulse-format+.h.c.o, andlibobs-c-referencedoes.CMocka_DIR; ctest ran withLD_LIBRARY_PATHpointing at itslib).audio_monitoring::pulse_formatandpulse_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--fixrewrapped two oracle prototypes;--lane cross: passed;--lane cross-release: passed;--lane rust-test: passed (2050 tests, 1 skipped, the lane's usual one), withexport_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.pyandci/test_platform_ifdef_ratchet.py: OK.FreeBSD, Windows, macOS: not built there. FreeBSD is the one other host that calls the shims; there
uint_fast8_tis 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
master(or the description starts with a Stacked: note)test_pulse_monitor_format, landed in libobs: Move the PulseAudio monitor format and volume into pulseaudio-monitor-format.c #1698 and libobs: Saturate the PulseAudio monitor volume on integer sinks #1709)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): not applicable