Skip to content

libobs: Saturate the PulseAudio monitor volume on integer sinks - #1709

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/pulse-monitor-volume-clamp
Oct 22, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/pulse-monitor-volume-clamp

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

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's as saturates, so a port that came first would change the behaviour without a test noticing.

  • Monitoring above 100% volume clips instead of wrapping (bug 3). The monitor applies a source's volume itself, in the sink's sample format. For U8, S16 and S32 sinks, process_byte(), process_s16() and process_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 gives INT32_MIN, so the monitor plays loud distortion. Users reach such volumes through Advanced Audio Properties (up to +26 dB), obs-websocket SetInputVolume (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.
    • Exact bounds. The product stays in float, as before. S16 and U8 compare it with their bounds, which a float holds exactly. S32 compares it with 2^31 and -2^31, which a float also holds exactly (INT32_MAX is not a float). So no edge value reaches the cast out of range, and NaN aside, the conversion is always defined.
    • In range, nothing changes. The result is the same truncated product, bit for bit. I checked this with a differential harness that ran the code from origin/master and 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.
  • The channel count in the sink-spec log is a number (bug 9). 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 uses PRIu8 now, like the line above it: 7 channels not supported by OBS,using 2 instead for recording.
  • Compatibility. No format, API, header or exported-symbol change (1766 in each library, identical). Volumes up to 100% sound exactly as before. Above 100%, loud samples on an integer sink now clip, as they already did on a float sink.
  • Upstream. OBS master has the same code in libobs/audio-monitoring/pulse/pulseaudio-output.c: process_byte(), process_s16() and process_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.c that #1698 pinned as "characterized, not endorsed". It changes no other case:

  • The header comment that characterized the wrap ("Characterized, not endorsed: ... in practice it wraps. Only results inside the range are pinned below.") now describes the saturation. The three "Above 1, only samples that stay in range (bug 3)" comments now say that out-of-range samples saturate.
  • 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 expect INT16_MAX or INT16_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 expect INT32_MAX or INT32_MIN: 2^30 × 2 (2^31, one past INT32_MAX), -2^30 × 2 (INT32_MIN itself, in range), ±(2^30 + 128) × 2, INT32_MAX and INT32_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_MAX at a volume of exactly 1, which the caller never applies because it is within EPSILON of 1. It now gives INT32_MAX instead 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.sh starts it.

Build Case Result
master 3352d17 + RED (before the rebase), libobs (C backend), hybrid libobs, libobs-c-reference test_pulse_monitor_format 3 of 12 fail in each: volume_u8 (0 != 255), volume_s16 (-25536 != 32767), volume_s32 (-2147483648 != 2147483647)
master + RED, built with -fsanitize=address,undefined,float-cast-overflow test_pulse_monitor_format 3 of 12 fail, and UBSan reports 256 is outside the range of representable values of type 'unsigned char', 40000 is outside the range of representable values of type 'short int' and 2.14748e+09 is outside the range of representable values of type 'int'
This branch, the three libraries test_pulse_monitor_format 12/12, 3 runs each
This branch, float-cast-overflow build test_pulse_monitor_format 12/12, no UBSan report
master libobs, real monitoring stream: a 440 Hz tone at 0.5, 400% volume, monitored on a null sink, monitor recorded with parec (1.47 s) S16LE sink wraps: 2577 jumps of more than half the range between neighbouring samples, and the positive peaks flip negative (25712, -29844, -20924, ...)
the same S32LE sink positive peaks go to full negative: 47218 samples at INT32_MIN and 64 at the top.
This branch, the same S16LE sink clips: no jumps, 23659 samples at 32767 and 23623 at -32768 (one third each, as for |sin| > 0.5)
This branch, the same S32LE sink clips: no jumps, 23659 samples at the top and 23623 at INT32_MIN

RED → GREEN

  • RED commit: fce5a13 test: Expect the PulseAudio monitor volume to saturate on integer sinks. On master, test_pulse_monitor_format fails 3 of 12 against libobs with OBS_LIBOBS_BACKEND=C, and against the hybrid libobs and libobs-c-reference (HYBRID, OBS_BUILD_C_REFERENCE=ON): volume_u8, volume_s16 and volume_s32. The test already builds with -fsanitize=address,undefined. GCC's undefined group does not include float-cast-overflow, so I built the test a second time with float-cast-overflow added. On master, UBSan then reports the undefined conversion in all three helpers (table above).
  • GREEN: cfb2d67. test_pulse_monitor_format passes 12/12 in both lanes and against all three libraries, 3 runs each. It also passes 12/12 with no report under float-cast-overflow. The exported symbols of libobs (C backend), the hybrid libobs and libobs-c-reference are identical (1766), and pulseaudio_monitor_process_volume stays local. Full ctest -j4:
Lane Library ctest
OBS_LIBOBS_BACKEND=C libobs 358/358
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 359/359
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 359/359

None 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 did test_pulse_monitor_reset (5 cases), test_pulse_monitor_flags, test_pulse_monitor_backlog, test_pulse_default_id, test_pulse_reconnect and test_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, and test_pulse_monitor_format (12/12 against all three libraries) and the symbol comparison passed again.

  • Also run: uv run ci/local_gate.py --lane lint passed, and --lane cross passed. clang-format 23.1 is clean on the changed files.
  • Real app: libobs monitoring against a private PipeWire sound server, with null sinks in S16LE and S32LE. A small driver links the real libobs. It plays a 440 Hz tone at 0.5 amplitude through a source at 400% monitoring volume, and parec records 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 logs 7 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

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (the characterization is libobs: Move the PulseAudio monitor format and volume into pulseaudio-monitor-format.c #1698)
  • User-visible change: its line in docs/features.md is added or updated (Fixed upstream bugs, Audio)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports) (not applicable: no port here)

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
@rdoupe
rdoupe requested a review from zackees as a code owner October 22, 2026 03:43
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
rdoupe force-pushed the fix/pulse-monitor-volume-clamp branch from 9c61e8e to cfb2d67 Compare October 22, 2026 03:43
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
Merged via the queue into obs-rust:master with commit 51569f9 Oct 22, 2026
23 checks passed
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
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.

2 participants