Repository navigation
libobs: Saturate the PulseAudio monitor volume on integer sinks - #1709
Merged
Merged
Conversation
test_pulse_monitor_format pinned volumes above 1 on the U8, S16 and
S32 sinks only where the result stays in range ("characterized, not
endorsed"). Above that range process_byte(), process_s16() and
process_s32() convert the scaled sample back to the integer type
without a clamp, which is undefined behaviour; with GCC on x86-64 U8
and S16 wrap (S16 20000 * 2 gives -25536) and a positive S32 overload
gives INT32_MIN, so the monitor plays loud distortion instead of a
clipped tone. Advanced Audio Properties reaches +26 dB, obs-websocket
20x, and a scene file anything.
The out-of-range cases now expect the sample to saturate to the
format's range, as the server clips a float sample: U8 0 and 255,
S16 INT16_MIN and INT16_MAX, S32 INT32_MIN and INT32_MAX, at 2, 4 and
+26 dB, including the first value past each bound (U8 192 * 2 is 256,
S16 16384 * 2 is 32768, S32 2^30 * 2 is 2^31). The comment that
characterized the wrap now describes the clamp. The in-range cases and
the other nine cases are unchanged.
Refs obs-rust#1697
The PulseAudio monitor applies a source's volume itself, in the sink's format. For U8, S16 and S32 sinks, process_byte(), process_s16() and process_s32() converted the scaled sample back to the integer type without a clamp. Above a volume of 1 (Advanced Audio Properties reaches +26 dB, obs-websocket 20x, a scene file anything) a loud sample leaves the type's range and the conversion is undefined behaviour: in practice U8 and S16 wrap and a positive S32 overload gives INT32_MIN, so the monitor plays loud distortion. pipewire-pulse sinks are S32 by default. Each integer sample now saturates to its format's range (U8 0..255, S16, S32) before the conversion, as the server clips a float sample. The bounds are exact: the product stays in float, and S32 compares it with 2^31 and -2^31, which a float holds exactly, so no edge value reaches the cast out of range. Inside the range the result is the same truncated product as before; the float path is unchanged. The sink-spec log printed the channel counts with %c (a character, not a number); it uses PRIu8 now. Upstream OBS has the same code in libobs/audio-monitoring/pulse/pulseaudio-output.c. Refs obs-rust#1697
rdoupe
force-pushed
the
fix/pulse-monitor-volume-clamp
branch
from
October 22, 2026 03:43
9c61e8e to
cfb2d67
Compare
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
chriscoveries
pushed a commit
to chriscoveries/obsrust-fork
that referenced
this pull request
Oct 22, 2026
…nitoring (obs-rust#1721) * libobs: Add Tier 1 tests for the PulseAudio monitor format port 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 * libobs: Port the PulseAudio monitor format and volume to obs-audio-monitoring 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
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.
Refs #1697 (PR 2 of the plan: the C fix of the two bugs that #1698 pinned. The port, step 3, follows and closes the issue.)
What and why
Two fixes in the PulseAudio monitor (
libobs/audio-monitoring/pulse/), described on #1697. This is C only, and the port copies the fixed C. It must land before the port: Rust'sassaturates, so a port that came first would change the behaviour without a test noticing.process_byte(),process_s16()andprocess_s32()(pulseaudio-monitor-format.c.h) converted the scaled sample back to the integer type without a clamp. Above a volume of 1, a loud sample leaves the type's range, and that float-to-integer conversion is undefined behaviour. In practice, with GCC on x86-64, S16 and U8 wrap (S16 20000 × 2 gives -25536), and a positive S32 overload givesINT32_MIN, so the monitor plays loud distortion. Users reach such volumes through Advanced Audio Properties (up to +26 dB), obs-websocketSetInputVolume(up to 20x) or a scene file. pipewire-pulse sinks are S32 by default. Each integer sample now saturates to its format's range (U8 0..255, S16, S32) before the conversion, which matches the float path, where the server clips.INT32_MAXis not a float). So no edge value reaches the cast out of range, and NaN aside, the conversion is always defined.origin/masterand the fixed code over 393,984,000 samples: every U8 and S16 value, plus random S32 values and edge cases, at 3,000 volumes between 0 and 20 (random values, every float bit pattern from 1 to about 20, and 0, 0.5, 0.9999, 1.0001, 1.5, 2, 4, +26 dB and 20). The 256,592,183 samples that stay in range are identical, the 137,391,817 that leave it saturate to the right bound, and none differ in any other way. The float path is unchanged.pulseaudio_sink_info()(pulseaudio-output.c.h) logged an unsupported channel count with%c. For a 7-channel sink, it printed the bytes 0x07 and 0x02 (control characters) instead of the numbers. It usesPRIu8now, like the line above it:7 channels not supported by OBS,using 2 instead for recording.libobs/audio-monitoring/pulse/pulseaudio-output.c:process_byte(),process_s16()andprocess_s32()have no clamp, and the channel-count log uses%c. Nobody has reported it upstream.This deliberately changes an existing test (needs maintainer sign-off). The RED commit changes only the >1.0 volume cases in
test/cmocka/test_pulse_monitor_format.cthat #1698 pinned as "characterized, not endorsed". It changes no other case:volume_u8: the 8 in-range cases stay. 10 cases are added that now expect 0 or 255: 192 × 2 (256, the first value past the range) → 255, 255 × 2 → 255, 63 × 2 → 0, 0 × 2 → 0, 200 × 4 → 255, 50 × 4 → 0, 255, 0, 135 and 121 at +26 dB → 255, 0, 255 and 0.volume_s16: the 9 in-range cases stay. 12 cases are added that now expectINT16_MAXorINT16_MIN: ±20000 × 2, 16384 × 2 (32768, the first value past the range), -16385 × 2, 32767 and -32768 × 2, ±10000 × 4, ±1643 at +26 dB (1642 still gives 32762) and 32767 and -32768 at +26 dB.volume_s32: the 4 in-range cases stay. 10 cases are added that now expectINT32_MAXorINT32_MIN: 2^30 × 2 (2^31, one pastINT32_MAX), -2^30 × 2 (INT32_MINitself, in range), ±(2^30 + 128) × 2,INT32_MAXandINT32_MIN× 2, ±1000000000 × 4 and ±123456789 at +26 dB.No other test changes. The other 9 cases are unchanged and pass. The in-range rows of the three volume cases are unchanged, and so is the float case, which pins values above 1 without a clamp. One case is still not pinned: S32
INT32_MAXat a volume of exactly 1, which the caller never applies because it is withinEPSILONof 1. It now givesINT32_MAXinstead of undefined behaviour.Headless (private Xvfb, private D-Bus, fresh HOME). Sound server: a private PipeWire, WirePlumber and pipewire-pulse stack (own runtime dir and D-Bus, no hardware), as
test_pulse_monitor_device.shstarts it.test_pulse_monitor_formatvolume_u8(0 != 255),volume_s16(-25536 != 32767),volume_s32(-2147483648 != 2147483647)-fsanitize=address,undefined,float-cast-overflowtest_pulse_monitor_format256 is outside the range of representable values of type 'unsigned char',40000 is outside the range of representable values of type 'short int'and2.14748e+09 is outside the range of representable values of type 'int'test_pulse_monitor_formatfloat-cast-overflowbuildtest_pulse_monitor_formatparec(1.47 s)25712, -29844, -20924, ...)INT32_MINand 64 at the top.INT32_MINRED → GREEN
test: Expect the PulseAudio monitor volume to saturate on integer sinks. On master,test_pulse_monitor_formatfails 3 of 12 against libobs withOBS_LIBOBS_BACKEND=C, and against the hybrid libobs and libobs-c-reference (HYBRID,OBS_BUILD_C_REFERENCE=ON):volume_u8,volume_s16andvolume_s32. The test already builds with-fsanitize=address,undefined. GCC'sundefinedgroup does not includefloat-cast-overflow, so I built the test a second time withfloat-cast-overflowadded. On master, UBSan then reports the undefined conversion in all three helpers (table above).test_pulse_monitor_formatpasses 12/12 in both lanes and against all three libraries, 3 runs each. It also passes 12/12 with no report underfloat-cast-overflow. The exported symbols of libobs (C backend), the hybrid libobs and libobs-c-reference are identical (1766), andpulseaudio_monitor_process_volumestays local. Fullctest -j4:OBS_LIBOBS_BACKEND=CHYBRID,OBS_BUILD_C_REFERENCE=ONHYBRID,OBS_BUILD_C_REFERENCE=ONNone skipped, failed or not run. The hybrid lane has one more test,
test_effect_parser_lifecycle_rust. All the pulse tests ran: the real-stream ones (test_pulse_monitor_device,test_pulse_two_sources,test_pulse_stream_restart) ran against a private sound server, and so didtest_pulse_monitor_reset(5 cases),test_pulse_monitor_flags,test_pulse_monitor_backlog,test_pulse_default_id,test_pulse_reconnectandtest_pulse_timing. After the final rebase onto e001d38 (#1706; CI, macOS graphics build and docs only), I rebuilt libobs and the test in both trees, andtest_pulse_monitor_format(12/12 against all three libraries) and the symbol comparison passed again.uv run ci/local_gate.py --lane lintpassed, and--lane crosspassed. clang-format 23.1 is clean on the changed files.parecrecords the sink's monitor. Before the fix, the recording wraps (S16) or flips its positive peaks to full negative (S32). After the fix, it clips at full scale on both sinks (table above). A 7-channel null sink logs7 channels not supported by OBS,using 2 instead for recording. The OBS frontend was not run: OBS has no built-in tone source, and the volume code path is the same.Windows/macOS: not affected. The PulseAudio monitor is built on Linux and FreeBSD only, with
ENABLE_PULSEAUDIO. The change is portable C, and the test has no cmocka range assertions.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (Fixed upstream bugs, Audio)-W..., ratchets): built with scripting ON (PR CI builds no OBS app: rungh workflow run ci.yml --ref <branch>for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)