Repository navigation
libobs: Move the PulseAudio monitor format and volume into pulseaudio-monitor-format.c - #1698
Merged
Merged
Conversation
…-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
This was referenced Oct 22, 2026
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 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.handpulseaudio-wrapper.c.hintolibobs/audio-monitoring/pulse/pulseaudio-monitor-format.c.h. The declarations go inpulseaudio-monitor-format.h, withoutEXPORT, aspulseaudio-stream-flags.hdid 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.pulseaudio_channels_to_obs_speakers(),pulseaudio_to_obs_audio_format()andpulseaudio_channel_map(). They lose theirstatic, so the test and later the port can reach them.process_byte(),process_s16(),process_s32()andprocess_float(), which staystatic.process_volume()becomespulseaudio_monitor_process_volume(). It takes the format and the channel count instead of the file-localstruct audio_monitor, and its body is unchanged.pulseaudio_sink_info()becomepulseaudio_monitor_sink_format()andpulseaudio_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 becomespulseaudio_monitor_queued_bytes()andpulseaudio_monitor_drop_split(). Thepa_stream_*calls, thetlengthchoice and the log stay indrop_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 becomespulseaudio_monitor_devices_match(), with the default sink's name passed in.devices_match()stays inpulseaudio-wrapper.c.hand callsget_default_id()once when either id is"default", as before: never when neither id is"default", and once when both are.pa_*,get_default_id(),bytes_per_sec(), the deque, the resampler, logging andaudio_monitor_init().libobs.pulsegroup (Linux and FreeBSD,ENABLE_PULSEAUDIO) in alphabetical order, and its includes joinlibobs-pulse.pch.h. The header is listed next topulseaudio-stream-flags.hinos-linux.cmakeandos-freebsd.cmake.source-sets.jsonis unchanged, andci/test_build_groups.pypasses.test_pulse_default_idcompilespulseaudio-wrapper.c.hagainst a fake libpulse, so it now compilespulseaudio-monitor-format.c.hnext to it (CMake, and the obs-build C test manifest). Its test file is unchanged.t) symbols.test_pulse_monitor_formatcompiles the moved file into itself (plain C, no sound server, ASan and UBSan), so it checks the C, liketest_pulse_monitor_flags. Its 12 cases pin:pa_sample_format_t, including the big-endian, S24, A-law and mu-law formats,PA_SAMPLE_INVALIDandPA_SAMPLE_MAX. Each maps to itsaudio_format, and each unsupported one is played as FLOAT32LE. The libpulse ABI numbers are pinned too.PA_CHANNELS_MAX, 255, 256 andUINT32_MAXto their speaker layouts. Every count from 0 to 255 to the count it plays with (0, 7 and more than 8 play as stereo).pa_channel_map_valid()) with distinct positions.((int)x - 128) * vol + 128, truncated, at 0, 0.1, 0.25, 0.5 and 1.INT32_MAX * 0.5gives 1073741824.frames * channelssamples are written, for every count the monitor plays, checked with guard samples on both sides."default"on either side or both, an empty default, a default that is itself a.monitorname, 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):
process_byte(),process_s16()andprocess_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 givesINT32_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'sassaturates, so a port first would change the behaviour silently.INT32_MAXleaves the range too (it becomes 2^31 in the float). The caller never applies a volume withinEPSILONof 1, so this is not reached and not pinned.RED → GREEN
a25035e44libobs: Move the PulseAudio monitor format and volume into pulseaudio-monitor-format.c.test_pulse_monitor_formatpasses 12/12, run 3 times against each of libobs with the C backend, the hybrid libobs and libobs-c-reference.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 ofget_default_id()calls. The other functions moved verbatim.framessamples written;"default"as id2 without.monitor;.monitoradded to id1;"default"matched without case.OBS_LIBOBS_BACKEND=C, andHYBRIDwithOBS_BUILD_C_REFERENCE=ON, both full builds. Fullctest -j4, headless (private Xvfb, private D-Bus, fresh HOME):OBS_LIBOBS_BACKEND=CHYBRID,OBS_BUILD_C_REFERENCE=ONHYBRID,OBS_BUILD_C_REFERENCE=ONNothing 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_formatpasses 12/12, andtest_pulse_default_id(2/2),test_pulse_monitor_flags(5/5) andtest_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 teststest_pulse_monitor_device,test_pulse_monitor_reset(5 cases),test_pulse_two_sourcesandtest_pulse_stream_restartran 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.uv run ci/local_gate.py --lane lintpassed, and--lane crosspassed. clang-format and gersemi are clean on the changed files.ci/test_build_groups.pyandci/test_cmocka_includes.pypass.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
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-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)