Skip to content

libobs: Move the audio tick arithmetic into obs-audio-mix.c (#1903) - #1905

Merged
zackees merged 2 commits into
obs-rust:masterfrom
vpacher:rust/1903-audio-mix
Oct 23, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
vpacher:rust/1903-audio-mix

Conversation

@vpacher

@vpacher vpacher commented Oct 23, 2026

Copy link
Copy Markdown
Member

Summary

Step 1 of #1903 (#1846 unit 10, slice 1), as #1815 did for #1814: a behaviour-neutral extraction and a characterization, no Rust. The port follows in its own PR.

  1. Move (libobs: Move the audio tick arithmetic into obs-audio-mix.c): convert_time_to_frames, mix_audio, audio_buffering_maxed, set_fixed_audio_buffering and add_audio_buffering move verbatim from obs-audio.c.h to libobs/obs-audio-mix.c.h. They only lose static / inline. They and struct ts_info are declared in obs-internal.h without EXPORT. The new file joins libobs.core-output just before obs-audio.c.h, so the calls stay in one translation unit. No group, PCH or source-sets.json change.
  2. Characterization (test: Characterize the audio tick arithmetic): 11 new cases in test_audio_buffering_subsample, the existing binary that compiles obs-audio.c.h into itself (no new executable).

Cases

  • convert_time_to_frames: every frame 0..1024 at 6 rates (8 kHz to 192 kHz), at the frame's time and 1 ns later, against floor(ns × rate / 1e9). Characterized, not endorsed: audio_frames_to_ns also rounds down, so at 44.1 kHz frame 1's own time maps to frame 0.
  • mix_audio: a whole tick; a start point on frames 1, 2, 511, 512 and 1023 at 44.1 and 48 kHz; audio before the window, at its end and after it adds nothing; audio on frame 1024 of a window longer than one tick adds nothing. Each case checks every sample of all 6 mixes and 8 channels: frames before the start point and channels past channels keep their values, frame f gets source frame f − start.
  • audio_buffering_maxed: true only when the total equals the maximum (characterized, not endorsed: a total above the maximum gives false).
  • set_fixed_audio_buffering: from 0 it adds all 45 ticks and queues 45 contiguous windows before buffered_ts; while ticks are pending it keeps buffered_ts and adds the remaining ones; at the maximum it changes nothing.
  • add_audio_buffering: ticks round up (1, 1023, 1024, 1025, 2048, 2049 frames late, at 44.1 and 48 kHz); 5 ticks late with room for 2 adds 2 and stops at the maximum.

Tests

CT104 (Linux x86_64, GCC), base f83cb73 and this branch:

Test base, libobs / C reference this branch, libobs / C reference
test_audio_buffering_subsample 5/5 / 5/5 16/16 / 16/16
test_audio_clock_drift 6/6 / 6/6 6/6 / 6/6
test_audio_sync_offset_change 5/5 / 5/5 5/5 / 5/5
test_audio_capture_mix_race 2/2 / 2/2 2/2 / 2/2
  • Exported symbols: 1766 in base and branch, for libobs and libobs-c-reference, no difference. The five moved functions are local symbols in libobs.so.30.
  • 0 compiler warnings in base and branch. ci/test_build_groups.py 6/6, ci/test_ported_sources.py 2/2.
  • The graphics tests ran from rundir/RelWithDebInfo through xvfb-run-or-display.sh, as ctest runs test_audio_capture_mix_race.

Notes

  • test_audio_buffering_subsample tests the C directly, as its header says. The real-mixer tests (test_audio_clock_drift, test_audio_sync_offset_change, test_audio_capture_mix_race) run through OBS::libobs and will see the swap; they are the Tier 2 for the port.
  • test_audio_sync_offset_change is registered without xvfb-run-or-display.sh (unlike test_audio_capture_mix_race), so under ctest without a display its 5 cases skip and ctest reports Passed. Above it ran through the wrapper. Not changed here.
  • The functions keep their names. They are hidden, and no other libobs symbol has these names (obs-scene.c.h has scene_mix_audio).

Refs #1903

🤖 Generated with Claude Code

vpacher and others added 2 commits October 23, 2026 08:00
Behavior-neutral extraction before the port to obs-core (obs-rust#1903): the
arithmetic of the audio tick in obs-audio.c.h moves verbatim into
libobs/obs-audio-mix.c.h:
- convert_time_to_frames(): a time in the tick to frames;
- mix_audio(): the mix window of a root source and the add loop over the
  mixes and channels;
- audio_buffering_maxed(), set_fixed_audio_buffering() and
  add_audio_buffering(): the buffering steps.

The only change is that they lose static (and inline). audio_callback()
and its helpers stay in obs-audio.c.h and call them. They and struct
ts_info are declared in obs-internal.h without EXPORT, so
-fvisibility=hidden keeps them local symbols. The new file joins
libobs.core-output just before obs-audio.c.h, with libobs.pch.h, so the
calls stay in one translation unit. No group, PCH or source-sets.json
change, and the exported symbols are unchanged.

test_audio_buffering_subsample compiles obs-audio.c.h into itself, so it
now also includes obs-audio-mix.c.h. Its cases do not change.

Refs obs-rust#1903

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Characterization of libobs/obs-audio-mix.c.h before its port to
obs-core (obs-rust#1903). The 11 new cases go into test_audio_buffering_subsample,
which already compiles obs-audio.c.h into itself, so no new executable:

- convert_time_to_frames(): every frame 0..1024 at 6 rates from 8 kHz to
  192 kHz, at the frame's time and 1 ns later, against
  floor(ns * rate / 1e9). Characterized, not endorsed: audio_frames_to_ns
  also rounds down, so at 44.1 kHz frame 1's own time maps to frame 0.
- mix_audio(): a whole tick; a start point on frames 1, 2, 511, 512 and
  1023 at 44.1 and 48 kHz; audio before the window, at its end and after
  it adds nothing; audio on frame 1024 of a window longer than one tick
  adds nothing. Each case checks every sample of the 6 mixes and 8
  channels: frames before the start point and channels past `channels`
  keep their values, frame f gets source frame f - start.
- audio_buffering_maxed(): true only when the total equals the maximum.
  Characterized, not endorsed: a total above the maximum gives false.
- set_fixed_audio_buffering(): from 0 it adds all 45 ticks and queues 45
  contiguous windows before buffered_ts; while ticks are pending it keeps
  buffered_ts and adds the rest; at the maximum it changes nothing.
- add_audio_buffering(): ticks round up (1, 1023, 1024, 1025, 2048 and
  2049 frames late, at 44.1 and 48 kHz); 5 ticks late with room for 2
  adds 2 and stops at the maximum.

CT104 (Linux x86_64, GCC): 16/16 against libobs and libobs-c-reference.

Refs obs-rust#1903

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vpacher
vpacher requested a review from zackees as a code owner October 23, 2026 06:11
@zackees
zackees added this pull request to the merge queue Oct 23, 2026
Merged via the queue into obs-rust:master with commit f03679c Oct 23, 2026
31 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 23, 2026
* test: Call the audio tick core from the subsample test

Two cases for the core that the next commit splits out of mix_audio(),
set_fixed_audio_buffering() and add_audio_buffering(), so the Rust port
can take plain values without mirroring struct obs_source or struct
obs_core_audio:

- obs_audio_mix_add() on plain buffers and an audio_ts of its own, with
  no source: the same mix as the source path from frame 511 (frame 512's
  time at 48 kHz), and nothing at the end of the tick.
  obs_audio_mix_time_to_frames() rounds down as convert_time_to_frames().
- obs_audio_buffering_fixed() and obs_audio_buffering_add() on standalone
  fields, with no struct obs_core_audio: nothing at the maximum (added
  false); fixed from 0 adds 45 ticks (960 ms at 48 kHz); 5 ticks late
  with room for 2 is cut at the maximum (max_reached, 42 ms); one sample
  late at 44.1 kHz is one tick (23 ms); a sub-sample delay adds 0 ticks
  and 0 ms and leaves ts. The ms values are the ones obs-audio.c logs.

The 16 existing cases are unchanged. This does not build yet: the core
and obs-audio-mix.h do not exist.

Refs obs-rust#1903

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

* libobs: Run the audio tick arithmetic through a core on plain values

The seam before the Rust port of the audio tick arithmetic (obs-rust#1903): the
core it ports takes the source's and the core's fields from the caller,
as obs_scene_audio_actions_apply() takes the item's fields (obs-rust#1825), so
Rust never mirrors struct obs_source or struct obs_core_audio.

libobs/obs-audio-mix.c.h is now that core, declared in the new
libobs/obs-audio-mix.h:
- obs_audio_mix_time_to_frames(sample_rate, t): convert_time_to_frames();
- obs_audio_mix_add(mixes, buf, audio_ts, ts, channels, sample_rate): the
  window and the add loop of mix_audio(), on the source's
  audio_output_buf and audio_ts;
- obs_audio_buffering_maxed(), obs_audio_buffering_fixed() and
  obs_audio_buffering_add(): the buffering steps on struct
  obs_audio_buffering, the buffered_ts, buffered_timestamps,
  buffering_wait_ticks and total_buffering_ticks fields of struct
  obs_core_audio by pointer, and max_buffering_ticks. They push the new
  windows to the deque in place and return struct
  obs_audio_buffering_step (added, max_reached, ticks, ms, total_ms).
struct ts_info moves into that header. The core takes no lock, logs
nothing and calls nothing in libobs.

convert_time_to_frames(), mix_audio(), audio_buffering_maxed(),
set_fixed_audio_buffering() and add_audio_buffering() go back into
obs-audio.c.h as static wrappers with their old signatures: they pass
the fields and log the step. The log lines and their values are
unchanged; "Max audio buffering reached!" and the info line now come
after the windows are pushed, not before. The per-tick DEBUG_AUDIO lines
(compiled out) become one "new buffered ts" line. The declarations that
the move added to obs-internal.h go.

The one behaviour difference is in a state libobs does not reach: with
a total above the maximum, the fixed path looped on a negative tick
count and left ts->end uninitialized; the core starts from *ts.

The header joins libobs.pch.h and source-sets.json. The core stays in
libobs.core-output. No exported symbol changes (1766); the core
functions are local symbols.

test_audio_buffering_subsample builds unchanged and passes 18/18 on
libobs and libobs-c-reference; test_audio_clock_drift (6),
test_audio_sync_offset_change (5) and test_audio_capture_mix_race (2)
pass on both.

Refs obs-rust#1903

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

* obs-core: Add Tier 1 tests for the audio tick arithmetic port

RED for the port of libobs/obs-audio-mix.c.h, the arithmetic of the audio
tick (obs-rust#1903), to a new module, audio_mix, of rust/obs-core. The safe core
is a stub (todo!()): TsInfo (struct ts_info), Buffering (the buffering
fields of struct obs_core_audio by value), Step (struct
obs_audio_buffering_step), time_to_frames(), mix_start() and mix_into()
(the window and the add loop of obs_audio_mix_add() for one mix and
channel), buffering_maxed(), buffering_fixed() and buffering_add(), which
push their windows through a callback.

tests/obs_core/audio_mix.rs ports test_audio_buffering_subsample 1:1,
under the same case names, in the same order, with the same inputs and
expectations: all 18 cases. struct obs_core_audio is a Buffering plus a
Vec queue whose front is index 0; mix_all() is the loop of
obs_audio_mix_add() over the mixes and channels. Samples compare bit for
bit.

New edge cases (edge_*): audio after frame 1024 of a two-tick window, the
add loop on short slices, a rate of 0, a total above the maximum,
timestamps that wrap, and a proptest that no input panics and that no
call pushes more than max - total windows (a sum that overflows int must
still be cut at the maximum).

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

Refs obs-rust#1903

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

* obs-core: Port the audio tick arithmetic

Ports libobs/obs-audio-mix.c.h, the arithmetic of the audio tick on plain
values (obs-rust#1903), to the audio_mix module of rust/obs-core. The source and
core side stays C in obs-audio.c.h: the source walk, the locks, the
logging, and the static wrappers that pass the source's audio_output_buf
and audio_ts and the buffering fields of struct obs_core_audio. The
hybrid libobs drops the C core and links the Rust shims in its place.

Safe core (src/audio_mix.rs):
- time_to_frames(): floor(t * rate / 1e9) through util_mul_div64 as the
  C computes it on each host (written out here, saturating on MSVC x64
  where the C faults, 0 for a divisor of 0);
- mix_start(): the frame the source's audio starts on, None outside the
  tick or from frame 1024 on; mix_into(): the add loop of one mix and
  channel over slices, element by element in the C's order, so every sum
  is bit for bit the C's;
- buffering_maxed(), buffering_fixed() and buffering_add(): the ticks,
  the cut at the maximum, the windows (pushed through a callback, each
  one tick before the last) and the step with the ms values obs-audio.c
  logs. The int and uint64_t arithmetic is the C's: wrapping timestamps,
  the (int) truncation of the tick count, the int-to-size_t conversion of
  the ms values.
It keeps no state, takes no lock, and denies clippy's panic, indexing
and arithmetic lints.

Shim (src/ffi/audio_mix.rs): the five functions keep the C signatures.
struct ts_info, struct audio_output_data, struct obs_audio_buffering and
struct obs_audio_buffering_step are mirrored #[repr(C)]. The buffering
shims read the fields through their pointers, push each window to the
front of the C struct deque in place through obs_util::ffi::deque (bmem
memory, so obs.c frees it with deque_free as before), and write the
fields back (obs-core already depends on obs-util).

Kept, characterized, not endorsed (obs-rust#1905): times round down twice, so a
frame's own time can map to the frame before it; the maximum is equality
only; a sub-sample delay is an added step of 0 ticks and 0 ms.

Intentional differences, where C has undefined behaviour and libobs never
gets there: audio past frame 1024 of a window longer than one tick adds
nothing (the C makes an out-of-range pointer); a total above the maximum,
or a tick count whose sum with the total overflows int, pushes at most
max - total windows (the C loops); a rate of 0 gives 0 frames, windows
and ms (the C divides by 0); null pointers do nothing; channels above
MAX_AUDIO_CHANNELS is cut to it; on MSVC x64 an overflowing quotient
saturates (the C faults).

C side: the file leaves libobs.core-output for its own group,
libobs.ported-audio-mix (LIBOBS_PORTED_SOURCES), whose PCH is
obs-audio-mix.h with media-io/audio-io.h and util/util_uint64.h. The five
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_mix passes 24/24.

Refs obs-rust#1903

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

* obs-core: Check the audio tick arithmetic port against the C

Oracle (oracle/audio_mix.c): the original obs-audio-mix.c.h, unmodified,
with its five functions renamed to oracle_*, plus the layout of struct
ts_info, struct audio_output_data, struct obs_audio_buffering, struct
obs_audio_buffering_step and struct deque, and AUDIO_OUTPUT_FRAMES,
MAX_AUDIO_MIXES, MAX_AUDIO_CHANNELS and sizeof(int). util_mul_div64, the
audio-io.h time helpers and deque_push_front stay the header inlines, so
the oracle pushes with the C's deque code and the shim with the Rust
port, into the same bmem allocator.

Tier 3 (tests/obs_core/audio_mix_parity.rs): both sides get the same
inputs through their C signatures and are compared:
- the layout;
- obs_audio_mix_add on every start frame at 44.1 and 48 kHz, on the
  frame's time and 1 ns either side, and on 4096 random cases (windows of
  one and two ticks, audio before, on, inside, at the end of and after
  them, 0 to 8 channels, rates from 8 kHz to 400 kHz, start times near 0
  and near u64::MAX): every sample of the 6 mixes and 8 channels, bit for
  bit, from the same random start values and source audio;
- obs_audio_mix_time_to_frames on 20 000 random rates and times,
  including rates where util_mul_div64 wraps (not on MSVC x64);
- obs_audio_buffering_maxed, _fixed and _add on 210 chosen and 20 000
  random cases (totals up to the maximum, waiting ticks, delays from 0 ns
  to beyond the maximum and huge, timestamps near 0 and u64::MAX, 0 to 4
  queued windows): the step, the three fields, ts, the struct deque fields
  (size, start, end, capacity) and every window in the queue.

Excluded, the intentional differences: audio past frame 1024 of a longer
window (only the shim runs; the mix must stay as it was), a delay whose
int tick count is negative or overflows the total (only the shim runs;
it must push at most max - total windows), a rate of 0 in the buffering
steps and null pointers (Tier 1).

port_comparison (benches/port_comparison/audio.rs): a stereo mix of a
whole tick and of a tick from frame 300 into all 6 mixes, time to frames,
and one added buffering tick on a queue popped back after each call.

Mutation check (done once, not committed): 20 of 21 mutants fail Tier 1
or Tier 3. Core: the window end inclusive; = instead of +=; the start
frame ignored; ticks rounded down; the cut only above the maximum;
max_reached not set; one window too many; ms from the total; buffered_ts
always reset; windows from the next tick; maxed as >=; exact 128-bit
util_mul_div64; the ms as an unsigned int; fixed keeping the old total.
Shim: total, waiting ticks or ts not written back; channels ignored or
one short; windows pushed to the back. The survivor, frame 1024 mixed, is
equivalent: a start of 1024 leaves no frame to add.

obs_core::audio_mix and audio_mix_parity pass 30/30 on macOS arm64 and
Linux x86_64 (all of obs_core: 86/86).

port_comparison on Linux x86_64 (CT104, pinned to one core, criterion
median, Rust / C):
  audio_mix/mix_whole_tick           1.051 us / 1.089 us
  audio_mix/mix_from_frame_300       700.4 ns / 759.7 ns
  audio_mix/time_to_frames           1.827 ns / 1.700 ns
  audio_mix/buffering_add_one_tick   30.74 ns / 25.70 ns
The mix runs every tick for every root source; the buffering step only
when a source falls behind.

Refs obs-rust#1903

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@vpacher
vpacher deleted the rust/1903-audio-mix branch October 24, 2026 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants