Skip to content

libobs: Move the PulseAudio monitor format and volume into pulseaudio-monitor-format.c - #1698

Merged
zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:refactor/pulse-format-move
Oct 22, 2026
Merged

zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:refactor/pulse-format-move

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Refs #1697 (PR 1 of the plan: the characterization and the extraction. The C fix and the port follow.)

What and why

This is a behaviour-neutral extraction before the Rust port, as the testing policy asks for part of a larger file. On Linux and FreeBSD, the PulseAudio monitor plays monitored sources in the chosen sink's sample format and applies each source's volume itself. That code moves, with the arithmetic of catching up after a stall and the feedback-loop device match, from pulseaudio-output.c.h and pulseaudio-wrapper.c.h into libobs/audio-monitoring/pulse/pulseaudio-monitor-format.c.h. The declarations go in pulseaudio-monitor-format.h, without EXPORT, as pulseaudio-stream-flags.h did before #1513. The move comes with a characterization test, test_pulse_monitor_format. There is no Rust, no behaviour change and no public header change.

  • What moves verbatim:
    • pulseaudio_channels_to_obs_speakers(), pulseaudio_to_obs_audio_format() and pulseaudio_channel_map(). They lose their static, so the test and later the port can reach them.
    • process_byte(), process_s16(), process_s32() and process_float(), which stay static.
  • What moves with a new signature:
    • process_volume() becomes pulseaudio_monitor_process_volume(). It takes the format and the channel count instead of the file-local struct audio_monitor, and its body is unchanged.
    • The spec fallbacks in pulseaudio_sink_info() become pulseaudio_monitor_sink_format() and pulseaudio_monitor_sink_channels(). The caller logs when the result differs from the sink's spec. That is exactly when it logged before, because each fallback (FLOAT32LE, 2 channels) is itself supported.
    • drop_backlog()'s arithmetic becomes pulseaudio_monitor_queued_bytes() and pulseaudio_monitor_drop_split(). The pa_stream_* calls, the tlength choice and the log stay in drop_backlog(). pa_timeval_age() is still called only while the stream is playing. Otherwise 0 is passed, which subtracts nothing, as before.
    • devices_match()'s comparison becomes pulseaudio_monitor_devices_match(), with the default sink's name passed in. devices_match() stays in pulseaudio-wrapper.c.h and calls get_default_id() once when either id is "default", as before: never when neither id is "default", and once when both are.
  • What stays: everything that calls pa_*, get_default_id(), bytes_per_sec(), the deque, the resampler, logging and audio_monitor_init().
  • Build: the new file joins the existing libobs.pulse group (Linux and FreeBSD, ENABLE_PULSEAUDIO) in alphabetical order, and its includes join libobs-pulse.pch.h. The header is listed next to pulseaudio-stream-flags.h in os-linux.cmake and os-freebsd.cmake. source-sets.json is unchanged, and ci/test_build_groups.py passes.
  • Tests that compile the moved code's old home: test_pulse_default_id compiles pulseaudio-wrapper.c.h against a fake libpulse, so it now compiles pulseaudio-monitor-format.c.h next to it (CMake, and the obs-build C test manifest). Its test file is unchanged.
  • Exported symbols: unchanged, 1766 in libobs with the C backend, in the hybrid libobs and in libobs-c-reference. The moved functions are local (t) symbols.

test_pulse_monitor_format compiles the moved file into itself (plain C, no sound server, ASan and UBSan), so it checks the C, like test_pulse_monitor_flags. Its 12 cases pin:

  • Sample formats: every pa_sample_format_t, including the big-endian, S24, A-law and mu-law formats, PA_SAMPLE_INVALID and PA_SAMPLE_MAX. Each maps to its audio_format, and each unsupported one is played as FLOAT32LE. The libpulse ABI numbers are pinned too.
  • Channel counts: 0-9, 10, 16, 32, PA_CHANNELS_MAX, 255, 256 and UINT32_MAX to their speaker layouts. Every count from 0 to 255 to the count it plays with (0, 7 and more than 8 play as stereo).
  • Channel maps: each layout's map, including the positions past the channel count, and the out-of-range layouts 7, 9 and 32. Every count the monitor plays gets a valid map (pa_channel_map_valid()) with distinct positions.
  • Volume, per format:
    • U8: ((int)x - 128) * vol + 128, truncated, at 0, 0.1, 0.25, 0.5 and 1.
    • S16: truncated toward zero, at 0, 0.1, 0.25, 0.5, 0.9999 and 1, including -32768 and 32767.
    • S32: at 0, 0.25, 0.5 and 1. A sample keeps only 24 bits through the float, so 123456789 becomes 123456792, and INT32_MAX * 0.5 gives 1073741824.
    • FLOAT32LE: at 0, 0.5, 1 and 2, signed zeros included. It has no clamp, and the server clips.
    • The other formats are left unchanged.
  • Sample count: exactly frames * channels samples are written, for every count the monitor plays, checked with guard samples on both sides.
  • Catch-up arithmetic: the queued bytes (what the server holds, less what it has played, never negative, plus what is not written yet, with rounding and large indices). The drop is rounded down to whole frames at every remainder for frame sizes 1-32, and split between what is not written yet and the rewind.
  • Device match: "default" on either side or both, an empty default, a default that is itself a .monitor name, ids that already end in .monitor, empty ids, case, NULL and a long name.

Characterized, not endorsed (a comment in the test, and in the issue):

  • A volume above 1 on an integer sink wraps instead of clipping. process_byte(), process_s16() and process_s32() convert 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 conversion is undefined behaviour. Advanced Audio Properties allows +26 dB, obs-websocket 20x, and a scene file anything. In practice, with GCC on x86-64, S16 and U8 wrap (20000 × 2 gives -25536), and S32 gives INT32_MIN. So only results that stay in range are pinned, at 1.5, 2 and +26 dB. The out-of-range cases come with the fix PR, which must land before the port: Rust's as saturates, so a port first would change the behaviour silently.
  • At a volume of exactly 1, S32 INT32_MAX leaves the range too (it becomes 2^31 in the float). The caller never applies a volume within EPSILON of 1, so this is not reached and not pinned.

RED → GREEN

  • RED commit: none. This is a move plus a characterization test, which passes on the moved code. The commit is a25035e44 libobs: Move the PulseAudio monitor format and volume into pulseaudio-monitor-format.c.
  • test_pulse_monitor_format passes 12/12, run 3 times against each of libobs with the C backend, the hybrid libobs and libobs-c-reference.
  • Equivalence with master, for the pieces that got a new signature: a differential harness ran master's original code (copied from origin/master) and the moved functions over 2,004,816 cases, with no differences. The cases were the spec fallbacks and their log conditions for every format and channel count, 2 million random inputs to the queued-bytes and drop arithmetic, and 720 id and default-sink pairs to the device match, including the number of get_default_id() calls. The other functions moved verbatim.
  • Mutation check of the new test: 20 of 20 mutants fail it. Each was compiled into the test. The mutants:
    • the 2.1 map's LFE moved;
    • 7 channels taken for 7.1;
    • S32BE accepted;
    • the format fallback S16LE;
    • the channel fallback 1;
    • the U8 offset 127;
    • S16 rounded instead of truncated;
    • S32 multiplied in double;
    • float clamped;
    • one sample too many written;
    • only frames samples written;
    • S16BE processed;
    • the played time not subtracted;
    • a negative held amount kept;
    • no frame rounding;
    • rounding up;
    • rewinding before dropping unwritten data;
    • "default" as id2 without .monitor;
    • .monitor added to id1;
    • "default" matched without case.
  • Local, two build trees: OBS_LIBOBS_BACKEND=C, and HYBRID with OBS_BUILD_C_REFERENCE=ON, both full builds. Full ctest -j4, headless (private Xvfb, private D-Bus, fresh HOME):
Lane Library ctest Exported symbols
OBS_LIBOBS_BACKEND=C libobs 358/358 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 359/359 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 358/359, then 3/3 (see below) IDENTICAL (1766)

Nothing was skipped or failed to run. The one failure was test_async_delay_audio (tone_continuous_with_3s_video_delay) against libobs-c-reference, in the first wave of -j4. It does not use audio monitoring. Run alone it passed 3/3, and it passed in the other two runs. This is the real-time audio load flake class of #838. test_pulse_monitor_format passes 12/12, and test_pulse_default_id (2/2), test_pulse_monitor_flags (5/5) and test_pulse_monitor_backlog (6/6) pass unchanged, against all three. The hybrid lane has one more test than the C lane (test_effect_parser_lifecycle_rust), hence 359. The real-stream tests test_pulse_monitor_device, test_pulse_monitor_reset (5 cases), test_pulse_two_sources and test_pulse_stream_restart ran against a private sound server, not skipped, and passed against all three. They reach the moved format, channel-map and catch-up code through a real monitoring stream. After the final rebase onto a4ff692 (#1692), which touches no libobs or pulse code, both trees were rebuilt, and the pulse tests and the symbol comparison passed again.

  • Also run: uv run ci/local_gate.py --lane lint passed, and --lane cross passed. clang-format and gersemi are clean on the changed files. ci/test_build_groups.py and ci/test_cmocka_includes.py pass.
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change).

Windows/macOS: not affected. The moved code is built on Linux and FreeBSD only, with ENABLE_PULSEAUDIO, and it has no host conditionals. The new test is portable C (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 (this is the extraction with its characterization; the fix and the port follow)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (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)

…-monitor-format.c

Behavior-neutral extraction before the Rust port: the PulseAudio
monitor's sample format, channel map and volume code, the arithmetic of
catching up after a stall, and the feedback-loop device match move from
pulseaudio-output.c.h and pulseaudio-wrapper.c.h into
pulseaudio-monitor-format.c.h, with their declarations in
pulseaudio-monitor-format.h (no EXPORT), as pulseaudio-stream-flags.h
did before obs-rust#1513.

- pulseaudio_channels_to_obs_speakers(), pulseaudio_to_obs_audio_format()
  and pulseaudio_channel_map() move verbatim and lose their static.
- process_byte/s16/s32/float move verbatim and stay static.
  process_volume() becomes pulseaudio_monitor_process_volume(), which
  takes the format and channel count instead of the file-local
  struct audio_monitor.
- The spec fallbacks in pulseaudio_sink_info() become
  pulseaudio_monitor_sink_format() and pulseaudio_monitor_sink_channels().
  The caller logs when they differ from the sink's spec, which is exactly
  when it logged before (each fallback is itself supported).
- drop_backlog()'s arithmetic becomes pulseaudio_monitor_queued_bytes()
  and pulseaudio_monitor_drop_split(). The pa_stream_* calls, the tlength
  choice and the log stay. pa_timeval_age() is still called only while
  playing; 0 otherwise, which subtracts nothing, as before.
- devices_match()'s comparison becomes pulseaudio_monitor_devices_match(),
  with the default sink's name passed in. devices_match() stays in
  pulseaudio-wrapper.c.h and calls get_default_id() once when either id
  is "default", as before (it never did for neither, at most once for
  both).

The file joins libobs.pulse (Linux and FreeBSD, ENABLE_PULSEAUDIO), in
alphabetical order, and its includes join libobs-pulse.pch.h. The
exported symbols are unchanged; the new functions are local.
test_pulse_default_id compiles pulseaudio-wrapper.c.h, so it now
compiles the new file next to it (CMake and the obs-build manifest).

New characterization test test_pulse_monitor_format, which compiles the
moved file (C only, no server, ASan and UBSan). It pins every
pa_sample_format_t and the played fallback, every channel count 0-255,
each layout's channel map (positions past the count included) and the
libpulse ABI numbers, the volume per format at 0, 0.1, 0.25, 0.5, just
under 1 and 1 (U8 truncation, S32's 24 bits through the float, other
formats untouched), that exactly frames * channels samples are written,
the queued-bytes and drop arithmetic at every frame remainder, and the
device match with "default" on either side, an empty default, names
ending in .monitor, and NULL.

Characterized, not endorsed: above a volume of 1 (Advanced Audio
Properties allows +26 dB), U8, S16 and S32 convert the scaled sample back
without a clamp. Out of range that is undefined behaviour (in practice
it wraps), so only in-range results are pinned; the fix follows before
the port.

Refs obs-rust#1697
@rdoupe
rdoupe requested a review from zackees as a code owner October 22, 2026 02:22
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
Merged via the queue into obs-rust:master with commit 8bbcb3e Oct 22, 2026
25 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.

2 participants