Repository navigation
libobs: Drop audio held in the resampler when a new stream starts - #546
Merged
Merged
Conversation
When a Media Source plays a file whose sample rate differs from OBS's (44.1 kHz into 48 kHz) and is started again, the first thing heard is the end of the previous playback, then the file from its beginning. In the current release, a file of half a second of silence then half a second of tone, restarted over obs-websocket while recording, gives a short burst of the tone at every restart, half a second before the tone really starts. A 48 kHz copy of the file does not. libobs converts such audio with the source's resampler, which holds back the last samples of every packet for its filter. When the source stops sending audio (the file ended) those samples stay there, and the next packet, however much later it comes, is output after them. The test drives the real libobs input path (obs_source_output_audio and the source's resampler) with a 44.1 kHz tone, then a packet of silence, and checks what libobs made of that packet. On master: restarted 1 s after the end: 23 samples of the old tone (peak 0.9) restarted 100 ms after the end: 23 samples of the old tone (peak 0.9) 2 of 4 cases fail. The controls pass: silence that continues the tone without a gap, or 20 ms late (within libobs's 70 ms timestamp smoothing), is the same stream and keeps the end of the tone in front of it. obs_startup() needs a display on Linux; the cases skip without one (run under xvfb-run). Like the other core audio tests it is built on Linux only. Reported upstream by norihiro. Refs obs-rust#545. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A source's resampler holds back the last samples of every packet for its filter. When a Media Source playing a 44.1 kHz file into 48 kHz OBS reached the end of the file, those samples stayed in the resampler, and when the file was started again they were output in front of its beginning: the restart began with a click of the previous playback's end. Remember where the next packet into the resampler should start. A packet more than 70 ms (TS_SMOOTHING_THRESHOLD, the limit libobs already uses to tell a continuing stream from a new one) away from it begins a new stream: recreate the resampler so it starts empty. Packets that continue the stream, with or without timestamp jitter, keep everything as before. Fixes obs-rust#545. Co-Authored-By: Claude Opus 5.5 <[email protected]> Local-Gate: v1 tree=1d27d6f2c7194086997fc0b01f81608941bd4a3f secs=338 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run Ci-Attestation: {"at":1791891391,"gate":"rust/all/fmt","host":"linux-x86_64","key":"fd05414337c06b1b25b8b72ffd2b441c132be0fa5e81e3ed8d4ac587a4638dfc","lane":"lint","parents":["adc445837767a984cc33b37ecdebc17e0691f29d"],"secs":32,"stamp":"dad82586c71944d8dc714448a225c951","tree":"1d27d6f2c7194086997fc0b01f81608941bd4a3f","v":1,"via":"run"} Ci-Attestation: {"at":1791891391,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"fd05414337c06b1b25b8b72ffd2b441c132be0fa5e81e3ed8d4ac587a4638dfc","lane":"lint","parents":["adc445837767a984cc33b37ecdebc17e0691f29d"],"secs":32,"stamp":"3f1ca6636e58ac07e4a945c9594dceae","tree":"1d27d6f2c7194086997fc0b01f81608941bd4a3f","v":1,"via":"run"} Ci-Attestation: {"at":1791891391,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"f4bf98d2d649f14cf510e0313c5e7af894366a814e76fb4a7f80201c00acb818","lane":"rust-test","parents":["adc445837767a984cc33b37ecdebc17e0691f29d"],"secs":115,"stamp":"adac1afa403813ff6531e9d9271539e3","tree":"1d27d6f2c7194086997fc0b01f81608941bd4a3f","v":1,"via":"run"} Ci-Attestation: {"at":1791891391,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"3c6ee4091bf11debe7f9df6d768bc6cf00be403535a7e0f1dff066f8cc003f0b","lane":"cross","parents":["adc445837767a984cc33b37ecdebc17e0691f29d"],"secs":102,"stamp":"7c1e89658110e4cb5efc2035cce8b15b","tree":"1d27d6f2c7194086997fc0b01f81608941bd4a3f","v":1,"via":"run"} Ci-Attestation: {"at":1791891391,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"3c6ee4091bf11debe7f9df6d768bc6cf00be403535a7e0f1dff066f8cc003f0b","lane":"cross","parents":["adc445837767a984cc33b37ecdebc17e0691f29d"],"secs":102,"stamp":"fc8f6758774a3e0435ea2b203e4a0ed0","tree":"1d27d6f2c7194086997fc0b01f81608941bd4a3f","v":1,"via":"run"} Ci-Attestation: {"at":1791891391,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"3c6ee4091bf11debe7f9df6d768bc6cf00be403535a7e0f1dff066f8cc003f0b","lane":"cross","parents":["adc445837767a984cc33b37ecdebc17e0691f29d"],"secs":102,"stamp":"1fafcd19c7a15957e78b9151cf4c05e4","tree":"1d27d6f2c7194086997fc0b01f81608941bd4a3f","v":1,"via":"run"} Ci-Attestation: {"at":1791891391,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"3c6ee4091bf11debe7f9df6d768bc6cf00be403535a7e0f1dff066f8cc003f0b","lane":"cross","parents":["adc445837767a984cc33b37ecdebc17e0691f29d"],"secs":102,"stamp":"8ecd377acc8791bbda4f53a3fe6ec4e0","tree":"1d27d6f2c7194086997fc0b01f81608941bd4a3f","v":1,"via":"run"} Ci-Attestation: {"at":1791891391,"gate":"rust/x86_64-apple-darwin/clippy","host":"linux-x86_64","key":"3c6ee4091bf11debe7f9df6d768bc6cf00be403535a7e0f1dff066f8cc003f0b","lane":"cross","parents":["adc445837767a984cc33b37ecdebc17e0691f29d"],"secs":102,"stamp":"24d330fe979f92a4ab2f7af06f3572e1","tree":"1d27d6f2c7194086997fc0b01f81608941bd4a3f","v":1,"via":"run"} Ci-Attestation: {"at":1791891391,"gate":"rust/x86_64-apple-darwin/dylint","host":"linux-x86_64","key":"3c6ee4091bf11debe7f9df6d768bc6cf00be403535a7e0f1dff066f8cc003f0b","lane":"cross","parents":["adc445837767a984cc33b37ecdebc17e0691f29d"],"secs":102,"stamp":"159f97e3ea970853e5b3e4f7ab84fddf","tree":"1d27d6f2c7194086997fc0b01f81608941bd4a3f","v":1,"via":"run"} Ci-Attestation: {"at":1791891391,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"08c8acc62e19a6b30c8065421feb940b75fd6456b2cb10c9869d98ab7a16647e","lane":"tier2-linux","parents":["adc445837767a984cc33b37ecdebc17e0691f29d"],"secs":90,"stamp":"e4b883b7e6c72728a96db4cd52f2938b","tree":"1d27d6f2c7194086997fc0b01f81608941bd4a3f","v":1,"via":"run"}
Member
zackees
force-pushed
the
fix/media-restart-tail
branch
from
October 13, 2026 11:36
a4fb339 to
49ffbd6
Compare
6 of 7 tasks
rdoupe
added a commit
to rdoupe/obs-studio
that referenced
this pull request
Oct 23, 2026
…io-timing.c Behaviour-neutral extraction before the Rust port. The decisions of source_output_audio_data() in obs-source.c.h, which place each audio packet of a source in time, move to libobs/obs-source-audio-timing.c.h as two pure steps around audio_buf_mutex and the buffer start rule, declared in the new libobs/obs-source-audio-timing.h without EXPORT (hidden, as -fvisibility=hidden keeps them): - obs_source_audio_timing_pre_lock(): the direct timestamp check, then reset (timing not set), jump (more than MAX_TS_VAR off and not direct), smooth (within TS_SMOOTHING_THRESHOLD, with the async decoupled re-anchor and the drift error) or log, and next_audio_ts_min; - obs_source_audio_timing_under_lock(): push back or place, the reset when the system time jumped and the audio was not continuous (obs-rust#522), the sync and resampler offsets, next_audio_sys_ts_min and the "sync offset changed, so place" rule (obs-rust#333); - obs_source_audio_timing_buffer_start(): the audio_ts rule of reset_audio_data_at_sys_time() (obs-rust#286). source_output_audio_data() reads the fields into a struct, calls a step, and writes the results back itself, in the order the stores happened before: timing_adjust = 0 then timing_set for a direct timestamp, timing_set then timing_adjust for a reset, the re-anchor, then next_audio_ts_min; under the lock the reset, next_audio_sys_ts_min and last_sync_offset. The volatile timing_adjust is still read before the lock for the packet and again under it for next_audio_sys_ts_min, as before. handle_ts_jump() (the lock and the buffer clear), audio_resampler_set_compensation_error(), conv_frames_to_time() (the caller passes the packet's duration, so the steps never divide by the sample rate), the deque place and push-back, the signal, the muting and both log lines, with the same messages, stay in obs-source.c.h. All arithmetic is the same uint64_t arithmetic, wrapping where it wrapped. What differs is only when the fields are read: each step's inputs are read once, just before it, where the code read some of them a few statements later (timing_set after the direct store, sync_offset after the reset branch). That is visible only to a thread writing them in that window without the lock, which was already a data race. reset_audio_timing() stays for handle_ts_jump() and obs_source_show_preloaded_video(), and uint64_diff() for the resampler restart check of obs-rust#546; the steps have their own copy of the distance. The thresholds are named in the header, and obs-source.c.h checks them against TS_SMOOTHING_THRESHOLD and MAX_TS_VAR at compile time. The new file joins libobs.core-source with libobs.pch.h, which includes the new header. The exported symbol sets are unchanged (1766). Refs obs-rust#1932
rdoupe
added a commit
to rdoupe/obs-studio
that referenced
this pull request
Oct 23, 2026
* libobs: Move the source audio timestamp placement into obs-source-audio-timing.c Behaviour-neutral extraction before the Rust port. The decisions of source_output_audio_data() in obs-source.c.h, which place each audio packet of a source in time, move to libobs/obs-source-audio-timing.c.h as two pure steps around audio_buf_mutex and the buffer start rule, declared in the new libobs/obs-source-audio-timing.h without EXPORT (hidden, as -fvisibility=hidden keeps them): - obs_source_audio_timing_pre_lock(): the direct timestamp check, then reset (timing not set), jump (more than MAX_TS_VAR off and not direct), smooth (within TS_SMOOTHING_THRESHOLD, with the async decoupled re-anchor and the drift error) or log, and next_audio_ts_min; - obs_source_audio_timing_under_lock(): push back or place, the reset when the system time jumped and the audio was not continuous (obs-rust#522), the sync and resampler offsets, next_audio_sys_ts_min and the "sync offset changed, so place" rule (obs-rust#333); - obs_source_audio_timing_buffer_start(): the audio_ts rule of reset_audio_data_at_sys_time() (obs-rust#286). source_output_audio_data() reads the fields into a struct, calls a step, and writes the results back itself, in the order the stores happened before: timing_adjust = 0 then timing_set for a direct timestamp, timing_set then timing_adjust for a reset, the re-anchor, then next_audio_ts_min; under the lock the reset, next_audio_sys_ts_min and last_sync_offset. The volatile timing_adjust is still read before the lock for the packet and again under it for next_audio_sys_ts_min, as before. handle_ts_jump() (the lock and the buffer clear), audio_resampler_set_compensation_error(), conv_frames_to_time() (the caller passes the packet's duration, so the steps never divide by the sample rate), the deque place and push-back, the signal, the muting and both log lines, with the same messages, stay in obs-source.c.h. All arithmetic is the same uint64_t arithmetic, wrapping where it wrapped. What differs is only when the fields are read: each step's inputs are read once, just before it, where the code read some of them a few statements later (timing_set after the direct store, sync_offset after the reset branch). That is visible only to a thread writing them in that window without the lock, which was already a data race. reset_audio_timing() stays for handle_ts_jump() and obs_source_show_preloaded_video(), and uint64_diff() for the resampler restart check of obs-rust#546; the steps have their own copy of the distance. The thresholds are named in the header, and obs-source.c.h checks them against TS_SMOOTHING_THRESHOLD and MAX_TS_VAR at compile time. The new file joins libobs.core-source with libobs.pch.h, which includes the new header. The exported symbol sets are unchanged (1766). Refs obs-rust#1932 * test: Characterize the source audio timestamp placement test_source_audio_timing_steps compiles libobs/obs-source-audio-timing.c.h (ASan and UBSan, no libobs core) and calls only its three hidden functions, so it runs on every platform. Each case pins every output of a step, that the step writes its numeric outputs (they start as a garbage byte pattern) and that it leaves its inputs alone. 29 cases: - obs_source_audio_timing_pre_lock(): direct timestamps on either side of the clock; direct at MAX_TS_VAR - 1, MAX_TS_VAR and MAX_TS_VAR + 1 (it is strict); the reset when the timing is not set, which ignores the expected timestamp and the async flags; next_audio_ts_min == 0 keeps the packet's timestamp; smoothing with a positive, zero and negative drift error; the 70 ms threshold at 70 ms - 1 ns (smoothed), 70 ms and 70 ms + 1 ns (logged), either side; MAX_TS_VAR at 2 s - 1 ns and 2 s (logged) and 2 s + 1 ns (a jump, which resets the timing to the packet); a direct timestamp never jumps; the re-anchor of an unbuffered, decoupled async source (to os_time minus the packet's own timestamp, after the direct store) and none with only one of the flags. - obs_source_audio_timing_under_lock(): appended when contiguous, placed when next_audio_sys_ts_min is 0; the 70 ms threshold either side; the reset at 2 s + 1 ns and none at 2 s; no reset when the audio was continuous (obs-rust#522), and the reset using the packet's own timestamp; the sync and resampler offsets applied after the comparison (41fa9c1); next_audio_sys_ts_min from the timing_adjust read under the lock, not the packet's; any sync offset change places the packet and is kept, from 0 too (obs-rust#333). - obs_source_audio_timing_buffer_start(): os_time plus the sync offset minus the resampler offset, 0 for 0 (obs-rust#286). - Seven packet sequences through a fake source (the fields, and a model of handle_ts_jump() and of the buffer start of source_output_audio_place()): contiguous packets with jitter, a system-clock source, the sync offset changed between packets (up, down and negative, before the buffer's start), a timestamp jump keeping the sync offset, a Video Delay (Async) timing move of 4 s followed rather than reset (obs-rust#522), the decoupled re-anchor, and next_audio_ts_min 0 followed by an audio-mix reset. Characterized, not endorsed: a timestamp ahead of the clock gives a timing_adjust near UINT64_MAX; the packet's end, timestamp + timing_adjust, a negative sync offset larger than the packet's system time and a resampler offset larger than it all wrap; the distance is not modular, so timestamps on either side of UINT64_MAX are a jump; a direct timestamp more than 2 s from the expected one is logged as exceeding the smoothing threshold, not treated as a jump. The sequences also show that the first placement sets next_audio_sys_ts_min to the packet's time with its sync offset, so with a non-zero offset the second packet is placed too, right after the first. It is in the native test graph. The five real-libobs tests are unchanged. Refs obs-rust#1932 * obs-source: Add Tier 1 tests for the source audio timestamp placement port RED for the port of libobs/obs-source-audio-timing.c.h to rust/obs-source, a new crate for the units extracted from libobs/obs-source.c (one crate per libobs area, as rust/obs-scene is for obs-scene.c, obs-rust#444). The safe core, audio_timing, is a stub (todo!()): Action (keep, reset, jump, smooth, exceeded), PreLockInput and PreLock (obs_source_audio_timing_pre_lock()), UnderLockInput and UnderLock (obs_source_audio_timing_under_lock()), buffer_start() (obs_source_audio_timing_buffer_start()), distance() (uint64_diff()), TS_SMOOTHING_THRESHOLD and MAX_TS_VAR. The crate depends on nothing: plain u64, i64 and bool in and out. tests/source/audio_timing.rs ports test_source_audio_timing_steps 1:1: its 29 cases under the same names (steps_*), in the same order, with the same inputs and expectations, the characterized-not-endorsed wraps and the fake source of the sequences included. The C test's checks that a step writes its outputs and leaves its inputs alone have nothing to check in the safe API, which takes its inputs by value and returns every output. Edge cases (edge_*): the distance (not modular, at both ends of the range); every flag combination with the timestamp, clock, expected timestamp and duration over the special values of the u64 range and the thresholds +-1, checking no panic and what each action implies (direct exactly when within MAX_TS_VAR, the reset and jump adjust os_time - timestamp, a smoothed drift under 70 ms, no diff or adjust outside their actions, the end is timestamp + duration wrapped); the same for the under-lock step over special u64 and i64 values (the reset exactly when over MAX_TS_VAR and not continuous, the placed time with wrapped offsets, push back exactly when contiguous and the offset unchanged); the buffer start over special values; and two proptests of the same over full-range values (4000 cases each). The tests are a module of the existing core category binary, obs-tests-core (rust/obs-data/tests/core.rs), as obs-scene's are; no new test binary. The crate joins the workspace through its rust/* glob. soldr cargo test -p obs-data --test obs-tests-core source:: fails 35 of 35 with "not yet implemented". Refs obs-rust#1932 * obs-source: Port the source audio timestamp placement Ports libobs/obs-source-audio-timing.c.h, the audio timestamp placement of source_output_audio_data(), to the audio_timing module of the new rust/obs-source. source_output_audio_data() and reset_audio_data_at_sys_time() stay C in obs-source.c.h: the field reads and the stores in their order (the volatile timing_set and timing_adjust included), audio_buf_mutex, handle_ts_jump(), the resampler's compensation, conv_frames_to_time(), the deque, the signal, the muting and the log lines. The hybrid libobs drops the C file and links the Rust shims in its place. Safe core (src/audio_timing.rs): pre_lock() decides direct (strictly within MAX_TS_VAR of the clock), then reset (timing not set), jump (more than MAX_TS_VAR from the expected timestamp and not direct), smooth (strictly within TS_SMOOTHING_THRESHOLD; the re-anchor of an unbuffered, decoupled async source; the drift error) or exceeded, and the packet's end. under_lock() appends when the packet's system time is the buffer's end or within 70 ms of it, resets the timing to the packet's own timestamp when it is more than 2 s off and the audio was not continuous (obs-rust#522), adds the sync offset and takes off the resampler offset after the comparison, sets next_audio_sys_ts_min from the timing_adjust read under the lock, and places the packet whenever the sync offset changed (obs-rust#333). buffer_start() is the obs-rust#286 rule. Every sum and difference is wrapping_add/wrapping_sub where the C's uint64_t wraps; the drift error is cast_signed() of the wrapped difference, the C's (int64_t), and the sync offset is cast_unsigned(), the C's conversion of int64_t to uint64_t. The module denies clippy's arithmetic, indexing, as, unwrap, expect, panic, unimplemented and unreachable lints, keeps no state and takes no lock. Shim (src/ffi/source_audio_timing.rs): the three functions keep the C signatures. The two structs are mirrored #[repr(C)], the enum as u32 (C stores it as an int or unsigned int; every value fits). Each shim reads the input fields and writes the output fields through the pointer one field at a time, so the inputs and the padding keep their bytes, as with the C, and it never reads an output field. Intentional difference: a null pointer does nothing, where the C dereferences it; libobs passes a struct on its stack. Kept, characterized, not endorsed (test_source_audio_timing_steps): a timestamp ahead of the clock gives a timing_adjust near u64::MAX; timestamp + timing_adjust, the packet's end, a negative sync offset larger than the packet's system time and a resampler offset larger than it wrap; the distance is not modular; a direct timestamp more than 2 s from the expected one is logged, not a jump. C side: the file leaves libobs.core-source for its own group, libobs.ported-source-audio-timing (LIBOBS_PORTED_SOURCES), whose PCH is only obs-source-audio-timing.h; libobs.pch.h now includes the header for obs-source.c. The three functions are not EXPORT, so the shims are hidden: rust-hidden.txt, the local: list of rust-exports.map and rust-unexports-macos.txt. libobs-rust links the crate's ffi. The exported symbol sets are unchanged and equal: 1766 on the C backend, the hybrid and libobs-c-reference. ci/test_ported_sources.py checks the group on every host. test_source_audio_timing_steps still compiles the C file into itself, unchanged. The new test_source_audio_timing_steps_rust builds the same test source against libobs_rust-static instead, as test_scene_bounds_place_rust does, and links: the test includes only the header and calls only the three functions, and the archive's obs-source member needs nothing from libobs or the other crates (it pulls in only libgcc_s and libm). It passes 29 of 29. The five real-libobs tests see the swap through the hybrid. Tier 1 source::audio_timing passes 35/35. Refs obs-rust#1932 * obs-source: Check the source audio timestamp placement port against the C Oracle (oracle/source_audio_timing.c): the original obs-source-audio-timing.c.h, unmodified, with its three functions renamed to oracle_*, plus the layout of both structs, the size of the enum, the action values and the two thresholds. Like the calldata and audio controls oracles, it turns off -Wsign-conversion around the include: the C adds the int64_t sync offset to a uint64_t, as libobs always did, and libobs builds without that warning. Tier 3 (tests/source/audio_timing_parity.rs). The structs cross the ABI as bytes: each run fills a buffer with a random byte pattern (padding and output fields included), writes the inputs, runs the C step on one copy and the shim on another, and compares every byte, so the outputs must be equal and neither side may write an input, the padding or anything the other does not write. Runs: - the layout of both structs (size, alignment, every offset), the enum's size, the action values and the thresholds against the C header; - each step over the special values of the u64 and i64 ranges (0, 1, 70 ms and 2 s +-1, 2^63 +-1, the ends, the thresholds below u64::MAX) with every flag: 186624 pre-lock runs, 349920 under-lock runs, and 2304 buffer starts; - proptests of each step over full-range values with the expected timestamp, the system time and next_audio_sys_ts_min drawn near the thresholds as often as anywhere (20000 cases each); - 10000 random sequences of 2 to 50 packets through a model of source_output_audio_data() (the stores in their order, handle_ts_jump(), the buffer start of source_output_audio_place()), driven once by the C steps and once by the shims, from a random or empty source state, with the sync offset changed, the async flags toggled, the timing moved by the video thread (before the packet and between the two reads of timing_adjust) and timing_set cleared by the audio mix between packets. The step structs, the buffer start and the written-back state must be equal after every packet. A second test checks that the sequences reach every action and both under-lock outcomes, the reset, the re-anchor, direct timestamps and sync offset changes, more than 100 times each in 2000 sequences. The whole parity module passed 26 runs in a row with fresh random seeds (obs-rust#1930); no regression file came up. Excluded, the intentional difference: a null struct pointer, which the C dereferences and the shims ignore (checked on the Rust side only). Mutation check (done once, not committed): 51 of 51 mutants fail. The 43 core mutants fail Tier 1 and Tier 3: each comparison at its threshold (direct, jump, smooth, push back and the reset, < and <= or > and >=), direct not setting the timing, a direct timestamp allowed to jump, the reset adjust reversed, next_audio_ts_min 0 or next_audio_sys_ts_min 0 compared like other values, the re-anchor with either flag or to the smoothed timestamp, the drift error reversed or clamped, the smoothed timestamp or continuity dropped, diff or the jump's adjust not reported, the end from the unsmoothed timestamp, no push back on an exact match, obs-rust#522 reverted (a reset when continuous), the reset to the smoothed timestamp or keeping the old system time, the sync offset not applied or in the comparison, the resampler offset added, next_audio_sys_ts_min from the packet's adjust, obs-rust#333 reverted, the pre-obs-rust#333 rule (a change from 0 does not place), last_sync_offset not kept, the buffer start for 0, the distance not symmetric, and every wrapping operation (twelve of them, the (int64_t) included) made saturating. The 8 shim mutants (an output not written, an input written, two actions swapped, the two adjusts swapped, an argument changed) fail Tier 3; Tier 1 does not reach the shims. Refs obs-rust#1932 * docs: Record the source audio timestamp placement port docs/rust-port/naming.md maps libobs/obs-source*.c.h to the new rust/obs-source, as libobs/obs-scene*.c.h maps to rust/obs-scene. The rust/README.md crate table lists rust/obs-source, and its port table has a row for libobs/obs-source-audio-timing.c.h: what stays C, the seam (the field reads and the stores in their order), the wrapping arithmetic, what the characterization pins and the port keeps, the intentional difference (a null struct pointer), the _rust variant of the steps test, the five real-libobs tests and what Tier 3 compares. The crate's description and lib.rs say what it holds. port_comparison gets its own bench file, benches/port_comparison/source_audio_timing.rs: each step over 1024 packets that reach every branch, the outputs folded into a checksum both sides must match. On x86_64 Linux (GCC, not pinned; median per 1024 packets): pre_lock 5.12 us Rust and 4.78 us C (1.07x), under_lock 4.50 us Rust and 3.95 us C (1.14x), under 0.6 ns more per packet. Fixes obs-rust#1932
This was referenced Oct 23, 2026
rdoupe
added a commit
to rdoupe/obs-studio
that referenced
this pull request
Oct 24, 2026
Behaviour-neutral extraction before the Rust port. The decisions and the two sample loops of process_audio() and reset_resampler() in obs-source.c.h move to libobs/obs-source-audio-input.c.h, declared in the new libobs/obs-source-audio-input.h without EXPORT (hidden, as -fvisibility=hidden keeps them): - obs_source_audio_input_discontinuous(): the restart rule of obs-rust#546. With a resampler and next_resample_in_ts set, a packet 70 ms or more from it, either way, starts a new stream. - obs_source_audio_input_reset_needed(): the reset condition. The packet's rate, format or speakers differ from sample_info, the clock drift compensation flag changed, or the packet is discontinuous. - obs_source_audio_input_resampler_needed(): the "no resampler needed" rule of the clock drift fix. Same rate, format and speakers as the output and the compensation off; otherwise reset_resampler() creates one. - obs_source_audio_input_balance_needed() and _mono_needed(): the balance gate (output not mono, a stereo source, balance above 0.51f or below 0.49f) and the Force Mono gate (output not mono, the flag set). - obs_source_audio_input_balance() and _downmix(): process_audio_balancing() and downmix_to_mono_planar(), the same loops on a plain float ** and counts. The sine law keeps its casts as written. process_audio() keeps its order: the discontinuity, the reset check and reset_resampler(), then next_resample_in_ts (unchanged, still computed here), audio_failed, the resampler or the copy, and the two gates. reset_resampler() keeps the stores, the second load of async_compensation, the resampler's creation and the log line. The clock drift filter and audio_resampler_set_compensation_error() are untouched. The output's channel count is still read twice, for the mono check and for the downmix, and source->balance still for the gate and for the law. One read moves: async_compensation is now loaded on every packet before the reset check, where the C's || only loaded it when the format matched. It is an atomic read with no side effect. obs-source.c.h checks at compile time that the header's threshold, SPEAKERS_STEREO, OBS_SOURCE_FLAG_FORCE_MONO and balance law values are obs-source.c's, and that enum audio_format and enum speaker_layout are int-sized. The moved file checks that M_PI is <math.h>'s double, not graphics/math-defs.h's float fallback. uint64_diff() goes with its only user. The new file joins libobs.core-source with libobs.pch.h, which includes the new header. The exported symbol sets are unchanged (1766). Refs obs-rust#1994
rdoupe
added a commit
to rdoupe/obs-studio
that referenced
this pull request
Oct 24, 2026
test_source_audio_input_steps compiles libobs/obs-source-audio-input.c.h (ASan and UBSan) and calls only its seven hidden functions. 22 cases: - obs_source_audio_input_discontinuous() (obs-rust#546): 70 ms - 1 ns continues, 70 ms and 70 ms + 1 ns restart, later and earlier; the restart test's 20 ms, 100 ms and 1 s gaps; nothing without a resampler or with next_resample_in_ts == 0, and an end of 1 is set. - obs_source_audio_input_reset_needed(): no change, then the rate, the format, the speakers, the compensation turned on and off, and the discontinuity, each on its own; a zeroed sample_info against each field. - obs_source_audio_input_resampler_needed() (the clock drift fix): none for the output's format without compensation, one with it, and one for each difference. - The balance gate: 0.49f, 0.50f, 0.51f, 0.495f and the floats just inside are centred; the floats just outside, 0.25, 0.75, 0, -0 and 1 balance; never into a mono output or from a source that is not stereo. The Force Mono gate: only its bit counts, never into a mono output. - The laws, bit for bit: the sine law over nine balances (the gains with the C's casts, fully left and right exactly 1 and 0), and a balance for which the double product and a float-only product give different sinf arguments; the square and linear laws over five balances and on known samples; only the first frames and only planes 0 and 1 are written; an unknown law and zero frames read nothing, not even the plane pointers. - The downmix of 2, 6 and 8 channels (the C's sum, times 1 / channels), known samples, only the first frames; the sum is in plane order ((1 + 1e8) - 1e8 is 0) and scaled by the float reciprocal, not divided (they differ in the last bit for the sample used); zero frames read nothing. Characterized, not endorsed: - the distance is not modular, so timestamps on either side of UINT64_MAX restart; - a packet of all zeros against a zeroed sample_info resets nothing; - NaN balances are not balanced (the compares skip them); -1, 2 and +-inf are, giving negative gains, gains above 1 and NaN; - the downmix of one channel scales by 1, and of none scales the first plane by +inf (the gate never gets there). test_source_audio_balance_mono is the real-libobs case: a stereo source at the output's rate (no resampler) sends a packet, and an audio capture callback reads it back, bit for bit: centred it is unchanged, at 0.25 it is the sine law, with Force Mono both channels are the mean, and with both it is balanced, then the mean. 4 cases. Both are in the native test graph, in the core suite (the real-libobs one on Linux, like the other core audio tests), so no pinned suite count changes. On the C backend both pass, and test_audio_restart_resampler_tail, test_audio_clock_drift, test_media_sync_offset_restart and test_async_delay_audio are unchanged and pass. Refs obs-rust#1994
rdoupe
added a commit
to rdoupe/obs-studio
that referenced
this pull request
Oct 24, 2026
Ports libobs/obs-source-audio-input.c.h, the audio input steps of process_audio() and reset_resampler(), to the audio_input module of rust/obs-source. process_audio() and reset_resampler() stay C in obs-source.c.h: the field reads and stores, next_resample_in_ts (computed there and passed in), the resampler, copy_audio_data() and its allocation, the clock drift filter and audio_resampler_set_compensation_error(). The hybrid libobs drops the C file and links the Rust shims in its place. Safe core (src/audio_input.rs): - discontinuous(): a resampler, an end set, and abs_diff() of the two timestamps at or past the 70 ms threshold (obs-rust#546, kept exactly: the distance is not modular, as uint64_diff() is not). - reset_needed(): rate, format, speakers, the compensation flag, or the discontinuity. resampler_needed(): not (the output's rate, format and speakers and no compensation), the clock drift fix's rule. - balance_needed() and mono_needed(): the C's compares, so a NaN balance is not balanced, and only the Force Mono bit counts. - balance(): the three laws over the left and right slices; Law::from_c() gives None for any other value, which changes nothing. The sine law's gains are sine_gains(): (1.0f32 - balance) widened to f64, times the f64 PI / 2.0, rounded to f32 with `as` (the C's (float), round to nearest), then f32::sin, which is the platform's sinf; the same casts in the same order as the C. The square law is f32::sqrt, the linear law plain products. - downmix(): add_plane() for each other plane in turn, scale_plane() by 1.0f32 / channels as f32 (the C's reciprocal, +inf for 0), then copy_plane() back. Only the given lengths are touched. The module denies clippy's arithmetic, indexing, as (two allowed casts, commented), unwrap, expect, panic, unimplemented and unreachable lints, keeps no state and takes no lock. Shims (src/ffi/source_audio_input.rs): the five decisions take plain values and struct obs_source_audio_input_format by value (mirrored, C reads: nothing for zero frames or an unknown law, planes 0 and 1 for a law, planes 0 to channels - 1 for the downmix (plane 0 alone for 0 channels). The downmix holds two planes at a time, so it allocates nothing. Intentional difference: a null data, or a null plane the C would read, does nothing (the C dereferences it; libobs passes source->audio_data.data, which copy_audio_data() filled). C side: the file leaves libobs.core-source for its own group, libobs.ported-source-audio-input (LIBOBS_PORTED_SOURCES), whose PCH is the header and <math.h> (with _USE_MATH_DEFINES, as cmake/pch/obs-c-std.h gives the core); the file still checks that M_PI is a double. libobs.pch.h keeps the header for obs-source.c. The seven functions are not EXPORT, so the shims are hidden: rust-hidden.txt, the local: list of rust-exports.map and rust-unexports-macos.txt. libobs-rust already links the crate's ffi. The exported symbol sets are unchanged and equal: 1766 on the C backend, the hybrid and libobs-c-reference; the seven are local in all three, and the hybrid's come from the Rust archive, which calls glibc's sinf as the C does. ci/test_ported_sources.py checks the group on every host. test_source_audio_input_steps still compiles the C file into itself, unchanged. The new test_source_audio_input_steps_rust builds the same test source against libobs_rust-static instead: the test calls the seven hidden functions directly, so this variant runs the shims, the format passed by value and the plane pointers included. It passes 22 of 22 on the hybrid tree. test_source_audio_balance_mono, test_audio_restart_resampler_tail and test_audio_clock_drift see the swap through the hybrid. Tier 1 source::audio_input passes 32/32. Refs obs-rust#1994
rdoupe
added a commit
to rdoupe/obs-studio
that referenced
this pull request
Oct 24, 2026
Oracle (oracle/source_audio_input.c): the original obs-source-audio-input.c.h, unmodified, with its seven functions renamed to oracle_*, plus the format's layout (size, alignment, the three offsets), the threshold, the stereo, Force Mono and law constants, and the int and bool sizes. The format is mirrored on the Rust side and passed by value, as obs-source.c.h passes it. Tier 3 (tests/source/audio_input_parity.rs, 14 tests and one ignored): - the layout and constants against the C header, and the shim's mirror against the oracle's; - the gates exhaustively over their flags: the discontinuity over both resampler states and every pair of 14 special timestamps (around 0, the threshold, 2^63 and u64::MAX); the reset condition over the three bools and 64 x 64 formats with each field equal or not (values outside the enums too), and the resampler rule over the same with the compensation on and off; the balance gate over both output kinds, eleven speaker values and 44 special balances; the mono gate over every single bit, none and all; - proptests (20,000 cases each) over full-range u64 timestamps (half of them within a few thresholds of the end), any formats, any flags and balances drawn from bit patterns: finite, +-0, +-inf and NaN; - the laws (all three and five unknown values) over 0, 1, 7 and 64 frames and the special balances, and the downmix over 0 to 8 channels (0 passes plane 0 alone, as the C reads it): two copies of nine planes with guard samples, the C on one, the shim on the other through float **, every sample's bits compared; proptests over random samples, frame counts, laws and balances, all drawn from bit patterns (never a wide Range<f32>, obs-rust#1951); - the sine law's gains (a plane of ones) over every 4099th u32 pattern and every 257th one in [0, 1], 5.2 million balances; exhaustive_sine_gains (ignored, run with --release --ignored) covers all 2^32: it passed, so libm sinf and f32::sin with the C's casts give the same bits for every balance on x86_64 Linux. The gains stay in Rust; - zero frames and unknown laws read nothing on either side (a null data), and the intentional difference on the shim only: a null data or plane does nothing. One exception to the bit comparison, written out in the module header: where one multiply or add of the C had two NaN operands, which NaN comes out depends on the operand order the compiler picks (x86 returns the first operand's), and the C builds already differ there: the same C at -O0 and at -O2 gives a different NaN for a NaN sample times a NaN balance. There both sides must give a NaN; one NaN operand, a NaN made from other values, and everything else must match bit for bit. The proptest regression file is committed: the first run found that case (samples [0, 0, NaN], balance NaN, linear law) before the rule was written. The parity module and Tier 1 passed 10 runs in a row with fresh seeds; no other regression file came up. Mutation check (once, not committed): 48 mutants, Tier 1 (33 tests) and Tier 3 (14), failures per mutant: | Mutant | Tier 1 | Tier 3 | |---|---|---| | discontinuity at > threshold | 3 | 2 | | no end-set check / no resampler check | 2 / 2 | 2 / 2 | | distance one way (modular) | 4 | 2 | | never discontinuous (obs-rust#546 reverted) | 4 | 2 | | reset ignores rate / format / speakers / compensation / discontinuity | 2 / 3 / 2 / 2 / 2 | 2 each | | no resampler rule for compensation (clock drift fix reverted) | 2 | 2 | | resampler rule ignores rate / speakers / format | 2 each | 2 each | | balance gate at >= 0.51 / <= 0.49 | 3 / 3 | 2 / 2 | | NaN balanced | 2 | 2 | | balance gate ignores mono output / speakers | 2 / 2 | 2 / 2 | | mono gate on any flag / ignores mono output | 2 / 2 | 2 / 2 | | sine law product in f32 only | 3 | 3 | | sine law sin in f64, then rounded | 3 | 3 | | sine law 1 - balance in f64 | 1 | 3 | | sine / square / linear gains swapped | 4 / 3 / 3 | 3 / 2 / 2 | | square and linear laws swapped | 5 | 2 | | an unknown law is the sine law | 2 | 2 | | right plane not balanced | 5 | 3 | | downmix subtracts | 3 | 2 | | downmix divides instead of the reciprocal | 4 | 2 | | 0 channels scaled as 1 | 2 | 2 | | downmix sums in reverse / one plane too few / no copy back | 2 / 3 / 4 | 0 (the shim calls the loops) | | downmix adds one plane too many | 0, then 1 (see below) | 0 | | shim: one frame short / planes swapped | 0 | 3 / 3 | | shim: balance gate speakers fixed at stereo | 0 | 2 | | shim: resampler rule reads the source as the output | 0 | 2 | | shim: downmix scaled by the plane count / last plane skipped / one copy fewer | 0 | 2 each | | shim: discontinuity timestamps swapped (the end-set check moves) | 0 | 2 | Tier 1 does not reach the shims. Three survive and are equivalent: - the gate's edges compared as f64 0.49 and 0.51: no f32 lies between 0.49f and 0.49 or between 0.51f and 0.51; - the shim's format and speakers swapped in both formats: the rules only compare whole formats for equality; - the shim's two compensation flags swapped: != is symmetric. "Adds one plane too many" survived at first: downmix() with more planes than channels added one, and only the core reaches that (the shim calls the loops). edge_downmix_leaves_planes_past_channels, added to Tier 1 here, now fails on it (1 of 33). Refs obs-rust#1994
rdoupe
added a commit
to rdoupe/obs-studio
that referenced
this pull request
Oct 24, 2026
The rust/README.md crate table now lists libobs/obs-source-audio-input.c.h among the rust/obs-source units, and its port table has a row for it: the seven functions and their rules (obs-rust#546's restart and the clock drift fix's resampler rule included), what stays C (the field reads and stores, next_resample_in_ts, the resampler, copy_audio_data(), the drift filter and the compensation error), the format passed by value and the compile-time checks, the sine law's casts and the exhaustive Tier 3 result for its gains, what the shims read, what the characterization pins and the port keeps, the intentional difference (a null data or plane does nothing), the NaN-pair rule of the parity test, the _rust variant of the steps test, the real-libobs tests and what Tier 3 compares. The crate's description and lib.rs say what it holds. docs/rust-port/naming.md already maps libobs/obs-source*.c.h to rust/obs-source. port_comparison gets its own bench file, benches/port_comparison/source_audio_input.rs: 1024 packets through the discontinuity, the reset condition and the two gates (continuing, jittered and restarting timestamps, formats and flags changing now and then), the sine law over a stereo packet of 480 frames at 0.25, and the downmix of 480 frames of stereo and of 7.1, each folded into a checksum both sides must match. On x86_64 Linux (GCC, not pinned, a loaded machine; median): decisions 9.37 us Rust and 13.49 us C per 1024 packets (0.70x), sine_law 1.06 us and 1.70 us per packet (0.62x), downmix_2 1.13 us and 2.20 us (0.51x), downmix_8 3.30 us and 6.45 us (0.51x). The Rust loops are vectorized and give the same bits. Fixes obs-rust#1994
chriscoveries
pushed a commit
to chriscoveries/obsrust-fork
that referenced
this pull request
Oct 24, 2026
* libobs: Move the source audio input steps into obs-source-audio-input.c Behaviour-neutral extraction before the Rust port. The decisions and the two sample loops of process_audio() and reset_resampler() in obs-source.c.h move to libobs/obs-source-audio-input.c.h, declared in the new libobs/obs-source-audio-input.h without EXPORT (hidden, as -fvisibility=hidden keeps them): - obs_source_audio_input_discontinuous(): the restart rule of obs-rust#546. With a resampler and next_resample_in_ts set, a packet 70 ms or more from it, either way, starts a new stream. - obs_source_audio_input_reset_needed(): the reset condition. The packet's rate, format or speakers differ from sample_info, the clock drift compensation flag changed, or the packet is discontinuous. - obs_source_audio_input_resampler_needed(): the "no resampler needed" rule of the clock drift fix. Same rate, format and speakers as the output and the compensation off; otherwise reset_resampler() creates one. - obs_source_audio_input_balance_needed() and _mono_needed(): the balance gate (output not mono, a stereo source, balance above 0.51f or below 0.49f) and the Force Mono gate (output not mono, the flag set). - obs_source_audio_input_balance() and _downmix(): process_audio_balancing() and downmix_to_mono_planar(), the same loops on a plain float ** and counts. The sine law keeps its casts as written. process_audio() keeps its order: the discontinuity, the reset check and reset_resampler(), then next_resample_in_ts (unchanged, still computed here), audio_failed, the resampler or the copy, and the two gates. reset_resampler() keeps the stores, the second load of async_compensation, the resampler's creation and the log line. The clock drift filter and audio_resampler_set_compensation_error() are untouched. The output's channel count is still read twice, for the mono check and for the downmix, and source->balance still for the gate and for the law. One read moves: async_compensation is now loaded on every packet before the reset check, where the C's || only loaded it when the format matched. It is an atomic read with no side effect. obs-source.c.h checks at compile time that the header's threshold, SPEAKERS_STEREO, OBS_SOURCE_FLAG_FORCE_MONO and balance law values are obs-source.c's, and that enum audio_format and enum speaker_layout are int-sized. The moved file checks that M_PI is <math.h>'s double, not graphics/math-defs.h's float fallback. uint64_diff() goes with its only user. The new file joins libobs.core-source with libobs.pch.h, which includes the new header. The exported symbol sets are unchanged (1766). Refs obs-rust#1994 * test: Characterize the source audio input steps test_source_audio_input_steps compiles libobs/obs-source-audio-input.c.h (ASan and UBSan) and calls only its seven hidden functions. 22 cases: - obs_source_audio_input_discontinuous() (obs-rust#546): 70 ms - 1 ns continues, 70 ms and 70 ms + 1 ns restart, later and earlier; the restart test's 20 ms, 100 ms and 1 s gaps; nothing without a resampler or with next_resample_in_ts == 0, and an end of 1 is set. - obs_source_audio_input_reset_needed(): no change, then the rate, the format, the speakers, the compensation turned on and off, and the discontinuity, each on its own; a zeroed sample_info against each field. - obs_source_audio_input_resampler_needed() (the clock drift fix): none for the output's format without compensation, one with it, and one for each difference. - The balance gate: 0.49f, 0.50f, 0.51f, 0.495f and the floats just inside are centred; the floats just outside, 0.25, 0.75, 0, -0 and 1 balance; never into a mono output or from a source that is not stereo. The Force Mono gate: only its bit counts, never into a mono output. - The laws, bit for bit: the sine law over nine balances (the gains with the C's casts, fully left and right exactly 1 and 0), and a balance for which the double product and a float-only product give different sinf arguments; the square and linear laws over five balances and on known samples; only the first frames and only planes 0 and 1 are written; an unknown law and zero frames read nothing, not even the plane pointers. - The downmix of 2, 6 and 8 channels (the C's sum, times 1 / channels), known samples, only the first frames; the sum is in plane order ((1 + 1e8) - 1e8 is 0) and scaled by the float reciprocal, not divided (they differ in the last bit for the sample used); zero frames read nothing. Characterized, not endorsed: - the distance is not modular, so timestamps on either side of UINT64_MAX restart; - a packet of all zeros against a zeroed sample_info resets nothing; - NaN balances are not balanced (the compares skip them); -1, 2 and +-inf are, giving negative gains, gains above 1 and NaN; - the downmix of one channel scales by 1, and of none scales the first plane by +inf (the gate never gets there). test_source_audio_balance_mono is the real-libobs case: a stereo source at the output's rate (no resampler) sends a packet, and an audio capture callback reads it back, bit for bit: centred it is unchanged, at 0.25 it is the sine law, with Force Mono both channels are the mean, and with both it is balanced, then the mean. 4 cases. Both are in the native test graph, in the core suite (the real-libobs one on Linux, like the other core audio tests), so no pinned suite count changes. On the C backend both pass, and test_audio_restart_resampler_tail, test_audio_clock_drift, test_media_sync_offset_restart and test_async_delay_audio are unchanged and pass. Refs obs-rust#1994 * obs-source: Add Tier 1 tests for the source audio input steps port Tests for the port of libobs/obs-source-audio-input.c.h to the audio_input module of rust/obs-source, next to audio_timing, color_space and deinterlace_pacing. The safe core is a stub (todo!()), one function per hidden C function, plus the pieces they are made of: - discontinuous(), reset_needed() and resampler_needed() over a Format (struct obs_source_audio_input_format: format, rate, speakers); - balance_needed() and mono_needed(); - balance() of a left and a right slice with a Law (Law::from_c() gives None for a value outside enum obs_balance_type), and sine_gains(), the sine law's two gains with the C's casts; - downmix() of a slice of planes, from add_plane(), scale_plane() and copy_plane(), the C's three loops. tests/source/audio_input.rs ports test_source_audio_input_steps 1:1: its 22 cases under the same names (steps_*), in the same order, with the same inputs and expectations, the characterized-not-endorsed cases included. Edge cases (edge_*): proptests (4000 cases each) of the discontinuity against "within the threshold either way continues" over full-range u64 timestamps; the reset and resampler rules against "anything differs" over any formats; the balance gate against "outside [0.49f, 0.51f] and not NaN" over any f32 bit pattern; the mono gate over any flags; each law and the downmix against per-sample models over random samples (any bit pattern, none drawn from a wide float range), frame counts, plane lengths and 1 to 8 channels; the three downmix loops over planes of different lengths and any channel count. Plus the law values and the gate's float literals. The tests are a module of obs-tests-core (rust/obs-data/tests/core.rs), as the other obs-source modules' are. soldr cargo test -p obs-data --test obs-tests-core source::audio_input fails 32 of 32 with "not yet implemented". Refs obs-rust#1994 * obs-source: Port the source audio input steps Ports libobs/obs-source-audio-input.c.h, the audio input steps of process_audio() and reset_resampler(), to the audio_input module of rust/obs-source. process_audio() and reset_resampler() stay C in obs-source.c.h: the field reads and stores, next_resample_in_ts (computed there and passed in), the resampler, copy_audio_data() and its allocation, the clock drift filter and audio_resampler_set_compensation_error(). The hybrid libobs drops the C file and links the Rust shims in its place. Safe core (src/audio_input.rs): - discontinuous(): a resampler, an end set, and abs_diff() of the two timestamps at or past the 70 ms threshold (obs-rust#546, kept exactly: the distance is not modular, as uint64_diff() is not). - reset_needed(): rate, format, speakers, the compensation flag, or the discontinuity. resampler_needed(): not (the output's rate, format and speakers and no compensation), the clock drift fix's rule. - balance_needed() and mono_needed(): the C's compares, so a NaN balance is not balanced, and only the Force Mono bit counts. - balance(): the three laws over the left and right slices; Law::from_c() gives None for any other value, which changes nothing. The sine law's gains are sine_gains(): (1.0f32 - balance) widened to f64, times the f64 PI / 2.0, rounded to f32 with `as` (the C's (float), round to nearest), then f32::sin, which is the platform's sinf; the same casts in the same order as the C. The square law is f32::sqrt, the linear law plain products. - downmix(): add_plane() for each other plane in turn, scale_plane() by 1.0f32 / channels as f32 (the C's reciprocal, +inf for 0), then copy_plane() back. Only the given lengths are touched. The module denies clippy's arithmetic, indexing, as (two allowed casts, commented), unwrap, expect, panic, unimplemented and unreachable lints, keeps no state and takes no lock. Shims (src/ffi/source_audio_input.rs): the five decisions take plain values and struct obs_source_audio_input_format by value (mirrored, C reads: nothing for zero frames or an unknown law, planes 0 and 1 for a law, planes 0 to channels - 1 for the downmix (plane 0 alone for 0 channels). The downmix holds two planes at a time, so it allocates nothing. Intentional difference: a null data, or a null plane the C would read, does nothing (the C dereferences it; libobs passes source->audio_data.data, which copy_audio_data() filled). C side: the file leaves libobs.core-source for its own group, libobs.ported-source-audio-input (LIBOBS_PORTED_SOURCES), whose PCH is the header and <math.h> (with _USE_MATH_DEFINES, as cmake/pch/obs-c-std.h gives the core); the file still checks that M_PI is a double. libobs.pch.h keeps the header for obs-source.c. The seven functions are not EXPORT, so the shims are hidden: rust-hidden.txt, the local: list of rust-exports.map and rust-unexports-macos.txt. libobs-rust already links the crate's ffi. The exported symbol sets are unchanged and equal: 1766 on the C backend, the hybrid and libobs-c-reference; the seven are local in all three, and the hybrid's come from the Rust archive, which calls glibc's sinf as the C does. ci/test_ported_sources.py checks the group on every host. test_source_audio_input_steps still compiles the C file into itself, unchanged. The new test_source_audio_input_steps_rust builds the same test source against libobs_rust-static instead: the test calls the seven hidden functions directly, so this variant runs the shims, the format passed by value and the plane pointers included. It passes 22 of 22 on the hybrid tree. test_source_audio_balance_mono, test_audio_restart_resampler_tail and test_audio_clock_drift see the swap through the hybrid. Tier 1 source::audio_input passes 32/32. Refs obs-rust#1994 * obs-source: Check the source audio input steps port against the C Oracle (oracle/source_audio_input.c): the original obs-source-audio-input.c.h, unmodified, with its seven functions renamed to oracle_*, plus the format's layout (size, alignment, the three offsets), the threshold, the stereo, Force Mono and law constants, and the int and bool sizes. The format is mirrored on the Rust side and passed by value, as obs-source.c.h passes it. Tier 3 (tests/source/audio_input_parity.rs, 14 tests and one ignored): - the layout and constants against the C header, and the shim's mirror against the oracle's; - the gates exhaustively over their flags: the discontinuity over both resampler states and every pair of 14 special timestamps (around 0, the threshold, 2^63 and u64::MAX); the reset condition over the three bools and 64 x 64 formats with each field equal or not (values outside the enums too), and the resampler rule over the same with the compensation on and off; the balance gate over both output kinds, eleven speaker values and 44 special balances; the mono gate over every single bit, none and all; - proptests (20,000 cases each) over full-range u64 timestamps (half of them within a few thresholds of the end), any formats, any flags and balances drawn from bit patterns: finite, +-0, +-inf and NaN; - the laws (all three and five unknown values) over 0, 1, 7 and 64 frames and the special balances, and the downmix over 0 to 8 channels (0 passes plane 0 alone, as the C reads it): two copies of nine planes with guard samples, the C on one, the shim on the other through float **, every sample's bits compared; proptests over random samples, frame counts, laws and balances, all drawn from bit patterns (never a wide Range<f32>, obs-rust#1951); - the sine law's gains (a plane of ones) over every 4099th u32 pattern and every 257th one in [0, 1], 5.2 million balances; exhaustive_sine_gains (ignored, run with --release --ignored) covers all 2^32: it passed, so libm sinf and f32::sin with the C's casts give the same bits for every balance on x86_64 Linux. The gains stay in Rust; - zero frames and unknown laws read nothing on either side (a null data), and the intentional difference on the shim only: a null data or plane does nothing. One exception to the bit comparison, written out in the module header: where one multiply or add of the C had two NaN operands, which NaN comes out depends on the operand order the compiler picks (x86 returns the first operand's), and the C builds already differ there: the same C at -O0 and at -O2 gives a different NaN for a NaN sample times a NaN balance. There both sides must give a NaN; one NaN operand, a NaN made from other values, and everything else must match bit for bit. The proptest regression file is committed: the first run found that case (samples [0, 0, NaN], balance NaN, linear law) before the rule was written. The parity module and Tier 1 passed 10 runs in a row with fresh seeds; no other regression file came up. Mutation check (once, not committed): 48 mutants, Tier 1 (33 tests) and Tier 3 (14), failures per mutant: | Mutant | Tier 1 | Tier 3 | |---|---|---| | discontinuity at > threshold | 3 | 2 | | no end-set check / no resampler check | 2 / 2 | 2 / 2 | | distance one way (modular) | 4 | 2 | | never discontinuous (obs-rust#546 reverted) | 4 | 2 | | reset ignores rate / format / speakers / compensation / discontinuity | 2 / 3 / 2 / 2 / 2 | 2 each | | no resampler rule for compensation (clock drift fix reverted) | 2 | 2 | | resampler rule ignores rate / speakers / format | 2 each | 2 each | | balance gate at >= 0.51 / <= 0.49 | 3 / 3 | 2 / 2 | | NaN balanced | 2 | 2 | | balance gate ignores mono output / speakers | 2 / 2 | 2 / 2 | | mono gate on any flag / ignores mono output | 2 / 2 | 2 / 2 | | sine law product in f32 only | 3 | 3 | | sine law sin in f64, then rounded | 3 | 3 | | sine law 1 - balance in f64 | 1 | 3 | | sine / square / linear gains swapped | 4 / 3 / 3 | 3 / 2 / 2 | | square and linear laws swapped | 5 | 2 | | an unknown law is the sine law | 2 | 2 | | right plane not balanced | 5 | 3 | | downmix subtracts | 3 | 2 | | downmix divides instead of the reciprocal | 4 | 2 | | 0 channels scaled as 1 | 2 | 2 | | downmix sums in reverse / one plane too few / no copy back | 2 / 3 / 4 | 0 (the shim calls the loops) | | downmix adds one plane too many | 0, then 1 (see below) | 0 | | shim: one frame short / planes swapped | 0 | 3 / 3 | | shim: balance gate speakers fixed at stereo | 0 | 2 | | shim: resampler rule reads the source as the output | 0 | 2 | | shim: downmix scaled by the plane count / last plane skipped / one copy fewer | 0 | 2 each | | shim: discontinuity timestamps swapped (the end-set check moves) | 0 | 2 | Tier 1 does not reach the shims. Three survive and are equivalent: - the gate's edges compared as f64 0.49 and 0.51: no f32 lies between 0.49f and 0.49 or between 0.51f and 0.51; - the shim's format and speakers swapped in both formats: the rules only compare whole formats for equality; - the shim's two compensation flags swapped: != is symmetric. "Adds one plane too many" survived at first: downmix() with more planes than channels added one, and only the core reaches that (the shim calls the loops). edge_downmix_leaves_planes_past_channels, added to Tier 1 here, now fails on it (1 of 33). Refs obs-rust#1994 * docs: Record the source audio input steps port The rust/README.md crate table now lists libobs/obs-source-audio-input.c.h among the rust/obs-source units, and its port table has a row for it: the seven functions and their rules (obs-rust#546's restart and the clock drift fix's resampler rule included), what stays C (the field reads and stores, next_resample_in_ts, the resampler, copy_audio_data(), the drift filter and the compensation error), the format passed by value and the compile-time checks, the sine law's casts and the exhaustive Tier 3 result for its gains, what the shims read, what the characterization pins and the port keeps, the intentional difference (a null data or plane does nothing), the NaN-pair rule of the parity test, the _rust variant of the steps test, the real-libobs tests and what Tier 3 compares. The crate's description and lib.rs say what it holds. docs/rust-port/naming.md already maps libobs/obs-source*.c.h to rust/obs-source. port_comparison gets its own bench file, benches/port_comparison/source_audio_input.rs: 1024 packets through the discontinuity, the reset condition and the two gates (continuing, jittered and restarting timestamps, formats and flags changing now and then), the sine law over a stereo packet of 480 frames at 0.25, and the downmix of 480 frames of stereo and of 7.1, each folded into a checksum both sides must match. On x86_64 Linux (GCC, not pinned, a loaded machine; median): decisions 9.37 us Rust and 13.49 us C per 1024 packets (0.70x), sine_law 1.06 us and 1.70 us per packet (0.62x), downmix_2 1.13 us and 2.20 us (0.51x), downmix_8 3.30 us and 6.45 us (0.51x). The Rust loops are vectorized and give the same bits. Fixes obs-rust#1994
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.
Description
A Media Source playing a file at another sample rate than OBS's (a 44.1 kHz file in a 48 kHz OBS) no longer starts every restart with a click of the end of its previous playback.
libobs converts such audio with the source's resampler, which holds back the last samples of every packet for its filter. When the file ended, those samples stayed in the resampler, and when the file was started again they were output in front of its beginning.
process_audio()now remembers where the next packet into the resampler should start; a packet more than 70 ms away from it (TS_SMOOTHING_THRESHOLD, the limit libobs already uses to tell a continuing stream from a new one) begins a new stream, and the resampler is recreated empty. Packets that continue the stream, with or without timestamp jitter, are handled exactly as before.test/cmocka: Add failing test for audio held in the resampler on restart:test_audio_restart_resampler_taildrives the real libobs input path (obs_source_output_audio()and the source's resampler) with a 44.1 kHz tone, then a packet of silence, and checks what libobs made of that packet. The cases are:libobs: Drop audio held in the resampler when a new stream starts: the fix. 4 of 4 pass.Motivation and Context
Fixes #545 (upstream obsproject/obs-studio#7063, reported by norihiro, who traced it to the sample rate converter).
Reproduced in the current release (AppImage 33.0.0-beta6-799), headless, private config: a Media Source playing a 44.1 kHz WAV (0.5 s of silence, then 0.5 s of a 1 kHz tone), restarted three times 2 s apart over obs-websocket while recording. In the recording each restart after the first starts with a 2 ms burst of the tone, half a second before the tone really starts. The same file at 48 kHz has no burst.
With the fix, the same steps in a frontend build of this branch (Rust libobs, obs-websocket) give no burst at any restart: only the three half-second tones.
Why this one: an audible glitch in recordings and streams on a common setup (44.1 kHz music or effects in a 48 kHz OBS, restarted), reported with a precise analysis by a real user, a small fix in libobs, reproduced in our release.
How Has This Been Tested?
Automated, in both libobs lanes (
ENABLE_RUST_LIBOBSOFF and ON):test_audio_restart_resampler_tail: 2 of 4 cases fail at the RED commit and 4 of 4 pass at GREEN in each lane (run under Xvfb; all four cases ran). Like the other core audio tests it is built on Linux only, and its cases skip without a display.test_audio_clock_drift(which also runs the resampler, with jittered timestamps and clock drift compensation): 6 of 6 at GREEN in each lane.By hand (headless, private config): the end-to-end run above with a frontend build of this branch, for the 44.1 kHz and the 48 kHz file.
Types of changes
Checklist:
🤖 Generated with Claude Code