Skip to content

obs-core: Port the audio input discard steps (#2403) - #2404

Merged
zackees merged 5 commits into
obs-rust:masterfrom
vpacher:port/audio-discard
Oct 25, 2026
Merged

zackees merged 5 commits into
obs-rust:masterfrom
vpacher:port/audio-discard

Conversation

@vpacher

@vpacher vpacher commented Oct 25, 2026

Copy link
Copy Markdown
Member

Fixes #2403

Slice 2 of obs-audio.c, after #1903: the steps of the audio tick that drop audio from a source's input buffer (ignore_audio, discard_if_stopped, discard_audio) move to libobs/obs-audio-discard.c.h and are ported to obs_core::audio_discard.

What changed

  • libobs/obs-audio-discard.{h,c.h}: the three steps on the source's fields by pointer (struct obs_audio_input: audio_input_buf, audio_ts, last_audio_input_buf_size, audio_pending, pending_stop, timing_set). They return a step (struct obs_audio_ignore_step, enum obs_audio_stop_check, struct obs_audio_discard_step) that obs-audio.c logs. obs-audio.c.h keeps the locks, the logging and static wrappers with the old names. Declared without EXPORT.
  • rust/obs-core/src/audio_discard.rs (safe core) and src/ffi/audio_discard.rs (shim). The core returns what to pop; the shim pops the C struct deques in place through obs_util::ffi::deque. timing_set is volatile and other threads set it without the audio_buf_mutex: the shim never reads it and writes it (volatile) only when the source's audio restarts, as the C does.
  • Own unity group libobs.ported-audio-discard; the shims are hidden (rust-hidden.txt, rust-exports.map, rust-unexports-macos.txt); ci/test_ported_sources.py; rust/README.md.
  • audio_mix's mul_div64, NS and rate_u64 become pub(crate), so both modules use the same util_mul_div64 per host.

Kept, characterized, not endorsed

  • The sample count is channel 0's, and every channel pops it.
  • Without a timestamp, ignore_audio clears only channel 0. It pops audio_input_buf[0].size from each channel and reads that size again for each one, so after channel 0 is empty the other channels pop 0 bytes and keep their audio. The same code is in upstream OBS. Kept as it is (Pop::Channel0); a fix would be a separate issue.
  • The drop rounds up, so a source 1 ns behind drops a whole sample and lands after the tick start; the lag logged at LOG_DEBUG then wraps.

Intentional differences (C undefined or corrupting; libobs never gets there)

  • A sample rate of 0 in ignore_audio: 0 ns for the dropped samples (the C divides by 0).
  • A channel holding less than channel 0's count: the shim empties it. The C's deque_pop_front underflows the deque's size, and the Rust deque would panic on the audio thread. libobs gives every input channel of a source the same bytes.
  • Null pointers do nothing; channels above 8 is cut to 8.

RED → GREEN

  1. Extraction 06e09f052 (behaviour-neutral, no Rust): the unchanged C tests pass; the exported symbols are unchanged.
  2. Characterization: 12 new cases in test_audio_buffering_subsample (30/30 on libobs, libobs-c-reference and the hybrid): 5f4dd3a33.
  3. RED 3c777cec4: Tier 1 obs_core::audio_discard, the 11 C cases 1:1 plus edge cases and proptests: 0 of 18 pass (todo!()).
  4. GREEN fd11cba57: 18 of 18.
  5. Tier 3 bce15609f: the C oracle (the file unmodified, functions renamed) against the shim: layout, 27 chosen cases, 20 000 random cases; the step, the five fields, and every deque's fields and samples, bit for bit. obs_core::audio* 54/54 on Linux x86_64 and macOS arm64.

Mutation check (done once, not committed): 19 of 19 mutants fail Tier 1 or Tier 3. Core: the drop rounded down or not cut at what is buffered; no 1 ns adjustment; in sync as >; the warning only when not pending; the no-timestamp path clearing every channel or keeping last_size; the stop check inverted or one check early; ahead as <; the behind stop check without the size gate; start - 1 not treated as on the tick; the start point at the end as >=; the pending path setting audio_ts after a clear; a whole tick keeping pending_stop. Shim: last_size not written back; timing_set cleared only with the warning; one channel short; channel 0's count popped from every channel.

Tier 2 (Linux x86_64, Debian 13, Xvfb)

Lint lane (ci/local_gate.py --lane lint) on the same host: clang-format, rustfmt, clippy -D warnings for the workspace and every feature set, the Dylints and the ratchets pass; its self-test step skips test_hybrid_packaging there (no zstd on that host, the same on master).

test_audio_buffering_subsample, test_audio_clock_drift, test_audio_sync_offset_change, test_audio_capture_mix_race and test_async_delay_audio pass on the hybrid libobs and on libobs-c-reference (5/5 each). Exported symbols: 1766 on both; the three shims are local.

port_comparison (Linux x86_64, one pinned core, criterion median, Rust / C)

audio_discard/discard_tick_stereo   88.25 ns / 79.59 ns
audio_discard/pending_tick_stereo    9.45 ns /  5.28 ns

Both include pushing the popped frames back, on both sides. discard_audio runs once per tick for every audio source: the difference is about 8 ns per source per tick, about 0.4 µs per second per source at 48 kHz.

The real-host before/after (CT104, NDI/ALSA audio) follows with the next production rebuild after the merge, as for #1909.

Checklist

  • Base is master
  • 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
  • User-visible change: none
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): none
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

AI note: Claude Code (Claude Opus 5.5) wrote the code, the tests, the commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

vpacher and others added 5 commits October 25, 2026 18:00
ignore_audio, discard_if_stopped and discard_audio move from obs-audio.c
to obs-audio-discard.c as obs_audio_discard_ignore,
obs_audio_discard_if_stopped and obs_audio_discard_tick. They take the
source's fields by pointer (struct obs_audio_input) and return the step
that obs-audio.c logs, so the core has no lock, no logging and no
obs-internal.h layout. Behaviour-neutral: the same pops, the same field
updates in the same order, the same log lines.

Refs obs-rust#2403

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cases for obs-audio-discard.c before its port (obs-rust#2403), on plain fields:
ignore_audio's drop (rounded up, cut at what is buffered, the 1 ns
adjustment, the restart and when it warns), the two-tick stop check,
and every path of discard_audio (render source, ahead, behind with and
without the stop check, a start point on AUDIO_OUTPUT_FRAMES, pending
data, a full or partial tick on 1, 2 and 8 channels), plus one case
through the obs-audio.c wrappers on a struct obs_source.

Kept, characterized, not endorsed: the count is channel 0's and every
channel pops it in ignore_audio (the stop check clears each channel by
its own size), and the lag ignore_audio logs wraps when the drop
overshoots start_ts.

Refs obs-rust#2403

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RED for the port of libobs/obs-audio-discard.c.h, the steps of the audio
tick that drop audio from a source's input buffer (obs-rust#2403), to a new
module, audio_discard, of rust/obs-core. The safe core is a stub
(todo!()): InputState (the audio_ts, last_audio_input_buf_size,
audio_pending and pending_stop fields of struct obs_source by value),
Pop (what the caller pops from the input deques: nothing, bytes from
every channel, or each channel by its own size), IgnoreStep, StopCheck,
DiscardPath and DiscardStep, and ignore(), if_stopped() and
discard_tick() on channel 0's size in bytes.

tests/obs_core/audio_discard.rs ports the 11 obs-audio-discard.c cases
of test_audio_buffering_subsample 1:1, under the same names, in the same
order, with the same inputs and expectations. The input deques are one
Vec per channel; Input::apply() pops what the core returns, as the shim
will. The twelfth cmocka case checks the C wrappers and has no
counterpart.

New edge cases (edge_*): a start point past the tick (size_t wraps as in
C, so the source waits), a tick at time 0 (ts.start - 1 wraps, so every
source is behind), a sample rate of 0 (the C divides by 0), and the stop
check after a size change. Proptests: ignore never pops more than
channel 0 holds and audio_ts only moves forward or to 0; discard pops at
most one tick and never more than is buffered; no input panics.

audio_mix's mul_div64, NS and rate_u64 become pub(crate), so both
modules use the same util_mul_div64 per host.

cargo test -p obs-data --test obs-tests-core obs_core::audio_discard
fails 18 of 18 with "not yet implemented".

Refs obs-rust#2403

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Ports libobs/obs-audio-discard.c.h, the steps of the audio tick that
drop audio from a source's input buffer (obs-rust#2403), to the audio_discard
module of rust/obs-core. The source side stays C in obs-audio.c.h: the
locks, the logging (from the returned step), and the static wrappers
ignore_audio() and discard_audio() that pass the source's fields. The
hybrid libobs drops the C file and links the Rust shims in its place.

Safe core (src/audio_discard.rs): ignore(), if_stopped() and
discard_tick() on InputState (audio_ts, last_audio_input_buf_size,
audio_pending, pending_stop by value) and channel 0's size in bytes.
They return the step for the log lines and a Pop: nothing, bytes from
every channel, bytes from channel 0 only, or each channel by its own
size. The uint64_t and size_t arithmetic is the C's and wraps where it
wraps; util_mul_div64 is audio_mix's, per host. No state, no lock; the
module denies clippy's panic, indexing and arithmetic lints.

Shim (src/ffi/audio_discard.rs): the three functions keep the C
signatures. struct obs_audio_input, struct obs_audio_ignore_step and
struct obs_audio_discard_step are mirrored #[repr(C)], the enums as
int. The shim reads the fields through their pointers, pops the C
struct deques in place through obs_util::ffi::deque (bmem memory, so the
source frees them with deque_free as before) and writes the fields back.
timing_set is volatile and set by other threads without the
audio_buf_mutex: the shim never reads it and writes it (volatile) only
when the audio restarts, as the C does.

Kept, characterized, not endorsed: the count is channel 0's and every
channel pops it; without a timestamp, each channel pops channel 0's size
read again per channel, so only channel 0 is cleared; the lag logged
after a drop that overshoots wraps.

Intentional differences, where C has undefined behaviour, dereferences
null or corrupts a deque, and libobs never gets there: a sample rate of
0 gives 0 ns for the dropped samples (the C divides by 0); a channel
holding less than channel 0's count is emptied (the C's deque_pop_front
underflows the deque's size, and the Rust deque would panic on the audio
thread; libobs gives every input channel the same bytes); null pointers
do nothing and return the default step; channels above
MAX_AUDIO_CHANNELS is cut to it.

C side: the file leaves libobs.core-output for its own group,
libobs.ported-audio-discard (LIBOBS_PORTED_SOURCES), whose PCH is
obs-audio-discard.h with media-io/audio-io.h and util/util_uint64.h.
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. ci/test_ported_sources.py checks the group on
every host.

Tier 1 obs_core::audio_discard passes 18/18.

Refs obs-rust#2403

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Oracle (oracle/audio_discard.c): the original obs-audio-discard.c.h,
unmodified, with its three functions renamed to oracle_*, plus the
layout of struct obs_audio_input, struct obs_audio_ignore_step and
struct obs_audio_discard_step, the size of the two enums and their
values. util_mul_div64 and deque_pop_front stay the header inlines, so
the oracle pops with the C's deque code and the shim with the Rust
port, from deques built the same way in the same bmem allocator.

Tier 3 (tests/obs_core/audio_discard_parity.rs): both sides get the same
source fields and 8 input deques through their C signatures, and are
compared: the step, the five fields (audio_ts, last_size, pending,
pending_stop, timing_set), and for each deque its struct deque fields
(size, start, end, capacity) and every sample left in it, bit for bit:
- the layout;
- 27 chosen cases, one or more per path of the three functions (the
  round-up, the 1 ns adjustment, the overshoot, the restart with and
  without the warning, the stop check once and twice, a render source,
  ahead, behind, a whole and a partial tick, a start point on and past
  frame 1024 at 8 kHz, a tick at time 0, channels holding more than
  channel 0, and the no-timestamp path that only clears channel 0);
- 20 000 random cases (0 to 8 channels, 0 to 3000 frames, timestamps
  before, on, inside, at the end of and after one- and two-tick windows
  and near 0 and u64::MAX, rates from 8 kHz to 384 kHz and odd ones, the
  stop check state at random and equal to the buffer).

Excluded, the intentional differences, tested on the shim alone: a rate
of 0 in obs_audio_discard_ignore (the C divides by 0), a channel holding
less than channel 0's count (the C underflows the deque's size; the shim
empties it), null pointers.

port_comparison (benches/port_comparison/audio_discard.rs): discard_audio
of a whole tick from a stereo input holding two ticks, and the
not-enough-data path, with the popped frames pushed back after each
call on both sides.

obs_core::audio_discard and audio_discard_parity pass 24/24 on macOS
arm64.

Refs obs-rust#2403

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@zackees
zackees added this pull request to the merge queue Oct 25, 2026
Merged via the queue into obs-rust:master with commit e6478ac Oct 25, 2026
35 checks passed
@vpacher
vpacher deleted the port/audio-discard branch October 25, 2026 17:52
MCPVOT pushed a commit to MCPVOT/obs-studio that referenced this pull request Oct 30, 2026
)

* test(obs-core): Make audio_discard_parity's random cases cheap and fewer

random_cases_match took 147.9 s in Fast Gate (58 s on an M-series Mac),
the slowest test of the step (obs-rust#2891).

Two changes:

- Each side built its 8 input deques with one deque_push_back() per
  f32 (up to 3,200 frames per channel) and read them back with one
  deque_pop_front() per f32: about 100,000 calls per case. Source::new
  now reserves the capacity that pushing one f32 at a time from empty
  reaches (4 bytes doubled until it fits) and pushes each channel once;
  end() pops each deque once. Each deque starts in the same state as
  before (start 0, end at the size, the same capacity) and the same
  samples come back. The new one_push_matches_one_push_per_sample
  checks that for 0 to 300 frames and the power-of-two edges up to 3,199.
  With 20,000 cases: 58 s to 11 s.
- 20,000 cases to 1,000. The 19 mutants of obs-rust#2404, rebuilt and run
  again: every one still fails the audio_discard tests. Run against
  random_cases_match alone, 5 runs each, 16 fail within 125 cases in
  every run. The other 3 (no 1 ns adjustment, in sync as `>`, the shim
  clearing timing_set only with the warning) need an exact boundary that
  random inputs do not reach: at 20,000 cases two never failed and one
  failed in 1 of 2 runs. The chosen cases find all three.

random_cases_match: 0.56 s, 17 MB peak RSS over 25 runs (macOS arm64,
debug). The comparison, the input ranges and every other test are
unchanged. The proptest regression file keeps the seeds the mutants
failed on.

Refs obs-rust#2891

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(obs-core): Sample the start frames and cut the mix cases of audio_mix_parity

mix_on_every_start_frame took 31.8 s and mix_matches 21.6 s in Fast
Gate (17.6 s and 12.1 s on an M-series Mac), about 3 ms per mix: each
compare_mix() makes two sets of 6 x 8 x 1024 random samples and checks
every sample bit for bit (obs-rust#2891).

- mix_on_every_start_frame becomes mix_on_sampled_start_frames: the
  first and the last 8 start frames and every 61st between them (32 of
  1024; 61 is prime, so the frames fall on varied offsets), each on the
  frame's time and one ns before and after, at 44.1 and 48 kHz, as
  before.
- mix_matches: 4096 cases to 512.

The mix mutants of obs-rust#1909, rebuilt (the window end inclusive, = instead
of +=, the start frame ignored, the window start as <=, the start frame
plus one, frames counted from one ns late, the shim ignoring the channel
count or one channel short, and the equivalent frame 1024 accepted):
all 8 real ones still fail the audio_mix tests, and the equivalent one
survives, as before. mix_matches alone, 5 runs each: 7 of 8 fail within
64 cases in every run; frames counted from one ns late fails
mix_on_sampled_start_frames. The window end inclusive needs audio at
the window's end, which the every-frame test did not check either;
mix_matches and Tier 1 find it.

macOS arm64, debug, 25 runs: mix_on_sampled_start_frames 0.62 s and
13 MB peak RSS, mix_matches 1.88 s and 15 MB. The comparison, the
buffering and frame-count tests and the input ranges are unchanged.

Refs obs-rust#2891

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(obs-graphics): Cut state_stacks_parity's random sequences to 500

sequences took 30.9 s in Fast Gate (14.3 s on an M-series Mac): 5,000
random sequences of up to 60 calls, each compared on both sides with the
whole stack state after every call (obs-rust#2891).

5,000 cases to 500. obs-rust#2528 lists 16 mutants without naming the 14 it
killed, so they were rebuilt from the core and the shim: push keeping
cur, the pop floor at 1, pop without the erase, the multiply in the
other order, transpose and identity ignored, translate as scale,
state_pop returning the first entry or not popping, the blend reset
with enable not inverted, the op ignoring the function, the function
without dest_a, and in the shim: the erase of the last element keeping
num, push not counting, and the capacity growing by one. All 15 fail
sequences alone within 125 cases in every one of 5 runs; the equivalent
one obs-rust#2528 names (no max(new_size) in the growth) survives, as before.
1,000 cases would still take about 6 s on the CI runner, so 500 (4x the
need) keeps it under the 5 s that obs-rust#2888 proposes.

macOS arm64, debug, 25 runs: 1.50 s and 15 MB peak RSS. The calls, the
comparison after every call and every other test are unchanged. The
proptest regression files keep the seeds the mutants failed on.

Refs obs-rust#2891

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=8cec7172b177aead0ee3374cc102d09e830003a5 secs=340 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run,macos-native:n/a,windows-native:n/a
Ci-Attestation: {"at":1793263864,"gate":"rust/all/fmt","host":"linux-x86_64","key":"be9de4060c380e3fd3e326cc0ce090e9ac1874b2ad14138f5f0d3a9c1bc97d08","lane":"lint","parents":["c5fdd95feb1daa789bb69e17b52aff8821e3be74"],"secs":75,"stamp":"9eaa0726b440abd9db3f8c736ab1f641","tree":"8cec7172b177aead0ee3374cc102d09e830003a5","v":1,"via":"run"}
Ci-Attestation: {"at":1793263864,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"be9de4060c380e3fd3e326cc0ce090e9ac1874b2ad14138f5f0d3a9c1bc97d08","lane":"lint","parents":["c5fdd95feb1daa789bb69e17b52aff8821e3be74"],"secs":75,"stamp":"467728ef5aaebb56014fa6076e352270","tree":"8cec7172b177aead0ee3374cc102d09e830003a5","v":1,"via":"run"}
Ci-Attestation: {"at":1793263864,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"db8feb23281ca0e220c448d68f1be14a5f1062a0216f9e0340321e38e7dfee04","lane":"rust-test","parents":["c5fdd95feb1daa789bb69e17b52aff8821e3be74"],"secs":169,"stamp":"80eb40a7c4812ab98f972778a57d6bdd","tree":"8cec7172b177aead0ee3374cc102d09e830003a5","v":1,"via":"run"}
Ci-Attestation: {"at":1793263864,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"7cc8841f5fa7a06f3090aade0155e0457b9c4356c17fa44b5418f11c40df3119","lane":"cross","parents":["c5fdd95feb1daa789bb69e17b52aff8821e3be74"],"secs":96,"stamp":"dd713ca81ab3614076faf09ec3a0f5f3","tree":"8cec7172b177aead0ee3374cc102d09e830003a5","v":1,"via":"run"}
Ci-Attestation: {"at":1793263864,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"7cc8841f5fa7a06f3090aade0155e0457b9c4356c17fa44b5418f11c40df3119","lane":"cross","parents":["c5fdd95feb1daa789bb69e17b52aff8821e3be74"],"secs":96,"stamp":"4c9c4ba7cf6c909e381e9266b8bd25d6","tree":"8cec7172b177aead0ee3374cc102d09e830003a5","v":1,"via":"run"}
Ci-Attestation: {"at":1793263864,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"7cc8841f5fa7a06f3090aade0155e0457b9c4356c17fa44b5418f11c40df3119","lane":"cross","parents":["c5fdd95feb1daa789bb69e17b52aff8821e3be74"],"secs":96,"stamp":"558779126ebd09147e4147bd5bea2b6a","tree":"8cec7172b177aead0ee3374cc102d09e830003a5","v":1,"via":"run"}
Ci-Attestation: {"at":1793263864,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"7cc8841f5fa7a06f3090aade0155e0457b9c4356c17fa44b5418f11c40df3119","lane":"cross","parents":["c5fdd95feb1daa789bb69e17b52aff8821e3be74"],"secs":96,"stamp":"228629c266e4919d49763da44bdfe222","tree":"8cec7172b177aead0ee3374cc102d09e830003a5","v":1,"via":"run"}
Ci-Attestation: {"at":1793263864,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"53488f90bfeb235b9eecee55e15ed8e9bc3e38e2bc5df515126f9b13b5b82908","lane":"tier2-linux","parents":["c5fdd95feb1daa789bb69e17b52aff8821e3be74"],"secs":312,"stamp":"71a9f175dda786dd227f70632db58ab7","tree":"8cec7172b177aead0ee3374cc102d09e830003a5","v":1,"via":"run"}

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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 the audio input discard steps in obs-audio.c to obs-core (#1903 slice 2)

2 participants