Skip to content

libobs: Port the PulseAudio stream flags to obs-audio-monitoring - #1513

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-pulse-stream-flags
Oct 20, 2026
Merged

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

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1512

What and why

This PR ports libobs/audio-monitoring/pulse/pulseaudio-stream-flags.c.h to a new crate, rust/obs-audio-monitoring. The file has three functions:

  • pulseaudio_monitor_stream_flags
  • pulseaudio_monitor_reset_on_sink_event
  • pulseaudio_monitor_backlog_excess

All three are pure. pulseaudio-output.c calls them. libobs-with-rust drops the C file and links the Rust shims.

  • Crate: naming.md says a libobs port is obs-<area>, named after the libobs directory, and that a new libobs area gets a new row. The file is in libobs/audio-monitoring/, so the crate is obs-audio-monitoring, with the row libobs/audio-monitoring/ → rust/obs-audio-monitoring. It is not obs-media-io, because that crate's row is libobs/media-io/. ci/test_naming.py passes. The crate is a plain rlib, a dependency of libobs-rust, with deny(unsafe_code) outside ffi.
  • Safe core (src/pulse_stream_flags.rs):
    • stream_flags(Option<&[u8]>): None is the C's null id.
    • reset_on_sink_event(...).
    • Backlog::excess(...): it uses the C's wrapping uint64_t arithmetic.
    • The <pulse/def.h> values are Rust constants. The core has no libpulse dependency and no host cfg.
  • Shim (src/ffi/pulse_stream_flags.rs):
    • #[repr(C)] pulseaudio_monitor_backlog.
    • The two PulseAudio enums cross as c_uint. All their values are non-negative and fit in 32 bits.
    • The C file has no EXPORT, so the three symbols are hidden: they are listed in rust-hidden.txt, rust-exports.map and rust-unexports-macos.txt, and there is no rust-exports.d fragment. This is the same as the scene-bounds port (libobs: Port obs_bounds_fit_scale to Rust #1486).
    • The shims are built on every host. Only Linux and FreeBSD with ENABLE_PULSEAUDIO call them, so Windows and macOS need nothing special.
  • Build group:
    • The file leaves libobs.pulse for libobs.ported-pulse. Its PCH, libobs-ported-pulse.pch.h, holds only <string.h> and the file's own header, so the oracle and the C tests compile the file without the core.
    • The group is listed as LIBOBS_PULSE_PORTED_SOURCES in source-sets.json. It is added to libobs only, under ENABLE_PULSEAUDIO, in os-linux.cmake and os-freebsd.cmake. This mirrors LIBOBS_HEVC_PORTED_SOURCES.
    • Two consumers of the list follow it: rust/tools/ported_sources.py, which drives the Tier 2 "compiled into libobs, not into libobs-with-rust" object check (new --without-pulse, test extended), and obs-build's Linux C variant.
    • libobs-pulse.pch.h loses the moved file's <string.h> section. The remaining pulse files get it through obs-internal.h, and the C lane builds.
  • Oracle (oracle/pulse_stream_flags.c): it renames the three functions with #defines, includes the original file, and exports the def values and the struct layout.
    • pulseaudio-stream-flags.h includes <pulse/def.h>, and the Fast Gate runner installs only uuid-dev. The oracle therefore has a stand-in, oracle/pulse-shim/pulse/def.h, in the spirit of oracle/sse-shim and the vendored uthash.
    • Where libpulse's header is installed (GCC/Clang, __has_include_next), the stand-in forwards to it and defines nothing. The oracle then uses libpulse's own values, and the parity test checks the Rust constants against them.
    • Elsewhere (MSVC, macOS, a runner without libpulse-dev), the stand-in defines the 13 values the file uses. I checked them against libpulse 17 with _Static_asserts.
    • The stand-in passes -Wpedantic -Werror with GCC and Clang in both modes.
  • Intentional difference: a null backlog returns 0. C dereferences it, and its only caller never passes null. It is listed in the parity test header and in the rust/README.md row. Nothing else differs, quirks included (both are pinned in Tier 1):
    • a sample at now_ns == 0 opens no window;
    • the window start, the threshold and the result wrap as uint64_t.

test_pulse_monitor_flags and test_pulse_monitor_backlog compile the C into themselves and are unchanged. They still pass in every lane, but they cannot see the swap, so Tier 3 is the swap's proof, as with scene-bounds. (The issue says the backlog test has 3 cases; it has 6, and all 6 are mirrored.)

RED → GREEN

  • RED commit: e3c06e7cf libobs: Add Tier 1 tests for the PulseAudio stream flags port. The safe core has todo!() bodies. soldr cargo test -p obs-data --test obs-tests-core audio_monitoring:: fails 19 of 21 tests with "not yet implemented". The two constant checks pass.
    • Tier 1 (tests/audio_monitoring/pulse_flags.rs, pulse_backlog.rs) mirrors both cmocka tests 1:1, including the backlog test's queue model (steady, 3.2 s and 0.6 s stalls, drift, bursty source, draining spike). New cases:
      • the exact flags, a null id and near misses of "default";
      • event bits outside both masks, type 0x30, other facilities, PA_INVALID_INDEX on both sides;
      • the window boundary (one ns short, exactly the window), and the slack rule (the larger of the target and 200 ms, at both edges);
      • the two characterized quirks.
    • The tests are modules of the existing core category binary, obs-tests-core, registered in obs-data/tests/core.rs and in the testing-policy category table. There is no new test binary: ci/test_binary_cap.py reports 8 of 8.
  • GREEN: d873d7852 libobs: Port the PulseAudio stream flags to obs-audio-monitoring. audio_monitoring:: passes 28/28, and the whole obs-tests-core binary passes (149/149).
    • Layout: size, alignment and both field offsets of struct pulseaudio_monitor_backlog, compared with the C header. The 13 def values and the two backlog constants are compared with the oracle. Locally the oracle used libpulse's header.
    • Tier 3 (tests/audio_monitoring/pulse_parity.rs):
      • fixed cases: every cmocka input plus the edge cases;
      • three proptests over device ids (null, "default", near misses, any bytes), sink events (facility, type and stray high bits, or any u32; colliding indexes including PA_INVALID_INDEX), and backlog runs of up to 200 steps from a zero or arbitrary state, with a clock that mostly advances by up to a packet, sometimes by exactly the window or one ns short of it, and sometimes jumps or wraps;
      • after each backlog step, the result and both state fields are compared.
  • Deliberate-break check. Each break makes the random parity test fail, and each also fails Tier 1 or the fixed parity cases:
    • slack max → min: 4 failures;
    • window < → <=: 5;
    • remove dropping the sink_idx != PA_INVALID_INDEX guard: 3. The first run caught this one only in the fixed cases, so I weighted the event strategy toward sink events. Its minimal input is event = 0x20, event_idx = sink_idx = PA_INVALID_INDEX;
    • "default" matched as a prefix: 3.
  • Local, two build trees with ENABLE_PULSEAUDIO=ON. Each tree runs the cmocka tests against libobs (OFF) and libobs-with-rust (ON):
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 329/329 329/329 IDENTICAL (1766, same set as master)
Rust (ENABLE_RUST_LIBOBS=ON) 329/329 329/329 IDENTICAL (1766)
  • 0 skipped in all four runs. test_pulse_monitor_flags and test_pulse_monitor_backlog pass in all four, and so do the PipeWire end-to-end monitoring tests (test_pulse_monitor_device, test_pulse_monitor_reset*, test_pulse_stream_restart, test_pulse_two_sources).
  • libobs.ported-pulse+.h.c.o is linked into libobs and not into libobs-with-rust. In both libraries the three symbols are local (t).
  • Environment: headless (private Xvfb, private D-Bus, fresh HOME).
  • ci/local_gate.py:
    • --lane lint (clang-format, gersemi, rustfmt, clippy -D warnings, Dylints, ifdef ratchet, test-binary cap): passed.
    • --lane cross (clippy and Dylints for x86_64-pc-windows-msvc and aarch64-apple-darwin, plus a link of every test binary, oracle included): passed.
    • --lane cross-release (aarch64-pc-windows-msvc, x86_64-apple-darwin, aarch64-unknown-linux-gnu): passed.
    • --lane rust-test (soldr cargo test --workspace --locked, plus a --no-run link for x86_64-pc-windows-gnu): passed. obs-tests-core 149/149, and the obs-util harness 637/637, including the export_list check that every shim is exported or hidden.
  • ci/test_build_groups.py, ci/test_naming.py, ci/test_ported_sources.py and ci/test_rust_scope.py: OK.
  • Real app: not applicable (no behaviour change).

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 (characterized already by test_pulse_monitor_flags and test_pulse_monitor_backlog)
  • 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): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

RED for the port of libobs/audio-monitoring/pulse/pulseaudio-stream-flags.c.h
to the new crate obs-audio-monitoring (naming.md: obs-<area> after the
libobs directory, audio-monitoring/). The safe core's functions are
todo!() stubs.

The cases mirror test_pulse_monitor_flags and test_pulse_monitor_backlog
1:1, the backlog test's queue model included, plus edge cases: the exact
flags, a null id and near misses of "default", event bits outside both
masks, type 0x30, other facilities, the window boundary, the slack rule,
and two characterized C quirks (a sample at time 0 opens no window; the
unsigned arithmetic wraps). They run as a module of the core category
binary, obs-tests-core.

soldr cargo test -p obs-data --test obs-tests-core audio_monitoring::
fails 19 of 21 with "not yet implemented"; the two constant checks pass.

Refs obs-rust#1512
libobs/audio-monitoring/pulse/pulseaudio-stream-flags.c.h moves to
rust/obs-audio-monitoring, a new crate for libobs/audio-monitoring/
(naming.md row added). pulseaudio_monitor_stream_flags,
pulseaudio_monitor_reset_on_sink_event and
pulseaudio_monitor_backlog_excess are the safe core
(pulse_stream_flags.rs) with C ABI shims in src/ffi/; libobs-rust
re-exports them. The C file has no EXPORT, so the shims are hidden
(rust-hidden.txt, rust-exports.map, rust-unexports-macos.txt). They build
on every host and are called only on Linux and FreeBSD with
ENABLE_PULSEAUDIO.

The C file leaves libobs.pulse for its own ported group,
libobs.ported-pulse, with a PCH of just <string.h> and its header, listed
as LIBOBS_PULSE_PORTED_SOURCES and added under ENABLE_PULSEAUDIO in
os-linux.cmake and os-freebsd.cmake. rust/tools/ported_sources.py (the
Tier 2 object check) and obs-build's C variant read the new list.

The <pulse/def.h> values are Rust constants. The oracle compiles the
original C against libpulse's header where it is installed, else against
a stand-in (oracle/pulse-shim/pulse/def.h) with the same values, so
cargo test needs no libpulse. A test compares the constants with the
oracle's and the struct layout with the C header.

test_pulse_monitor_flags and test_pulse_monitor_backlog compile the C
into themselves and stay unchanged; Tier 3 parity is the proof of the
swap: fixed cases and proptests over device ids, sink events and backlog
runs (wrapping clocks and targets included). Intentional difference: a
null backlog returns 0 (C dereferences it).

Fixes obs-rust#1512
@rdoupe
rdoupe requested a review from zackees as a code owner October 20, 2026 10:18
@zackees
zackees added this pull request to the merge queue Oct 20, 2026
Merged via the queue into obs-rust:master with commit 36c7dfe Oct 20, 2026
25 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 22, 2026
…-monitor-format.c (obs-rust#1698)

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
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.

rust: Port audio-monitoring/pulse/pulseaudio-stream-flags.c to Rust

2 participants