Skip to content

obs-core: Port the minimum audio timestamp decisions (#2405) - #2406

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

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

Conversation

@vpacher

@vpacher vpacher commented Oct 25, 2026

Copy link
Copy Markdown
Member

Fixes #2405

Slice 3 of obs-audio.c, after #1903 and #2403 (#2404): the per-source decisions of the "get minimum audio timestamp" step of the audio tick move to libobs/obs-audio-min-ts.c.h and are ported to obs_core::audio_min_ts.

What changed

  • libobs/obs-audio-min-ts.{h,c.h}, on plain values:
    • obs_audio_min_ts_earlier(): find_min_ts's test for one source (not pending, a timestamp, strictly earlier than the minimum);
    • obs_audio_min_ts_insufficient(): audio_buffer_insufficient, on audio_render, audio_pending by pointer, audio_ts and channel 0's buffered size;
    • obs_audio_min_ts_buffering(): the choice after the search between fixed, added and no buffering.
  • obs-audio.c.h keeps the walk over the audio source list, each source's audio_buf_mutex, the two passes of calc_min_ts, the names and the logging. The walk stays C on purpose: each source is locked on its own, one after the other, and other threads change the values between the passes, so a walk over a snapshot would change when the values are read.
  • rust/obs-core/src/audio_min_ts.rs (safe core) and src/ffi/audio_min_ts.rs (shim). Own unity group libobs.ported-audio-min-ts; the shims are hidden; ci/test_ported_sources.py; rust/README.md.

Kept, characterized, not endorsed

  • calc_min_ts's second pass never raises the minimum. When the source with the minimum is short for a tick at its own timestamp, mark_invalid_sources marks it pending and calc_min_ts searches again, but from the first pass's minimum, not from the tick start. The comparison is strict, so the second pass finds nothing: the minimum stays the timestamp of the source just marked pending, audio_callback adds buffering for it in that tick, and the "adding N milliseconds of audio buffering" line names (null). This is in the code since the audio subsystem of 2015 (c1dd156db, also upstream). calc_min_ts_through_the_list pins it on libobs and libobs-c-reference. A change would be its own issue.
  • The sufficiency check stops at a start point >= frame 1024, where discard_audio stops only on ==.
  • Ties keep the first source in the list (the newest).

Intentional differences (C dereferences null; libobs never gets there)

  • A null min_ts or pending does nothing and returns false.

RED → GREEN

  1. Extraction 66f7e539f (behaviour-neutral, no Rust). audio_buffering_maxed() is still only read with fixed_buffer set.
  2. Characterization 0d756de4d: 5 new cases in test_audio_buffering_subsample (35/35 on libobs, libobs-c-reference and the hybrid), 4 on plain values and 1 through calc_min_ts on a list of sources.
  3. RED 62f59a61c: Tier 1 obs_core::audio_min_ts, the 4 plain-value cases 1:1 plus edge cases and proptests: 0 of 8 pass (todo!()).
  4. GREEN 0606d6aeb: 8 of 8.
  5. Tier 3 4ede2cd2f: the C oracle (the file unmodified, functions renamed) against the shim: the enum, every start frame at 44.1 and 48 kHz with the buffer one sample short, exact and over, and 50 000 random cases per function. obs_core::audio* (audio_mix, audio_discard, audio_min_ts) 68/68 on Linux x86_64; 14/14 for this module on macOS arm64.

Mutation check (done once, not committed): 13 of 14 mutants fail Tier 1 or Tier 3 (untimed or pending sources taken; <= instead of <; the minimum not updated; render or pending sources checked; min_ts - 1 not on the tick; an exact tick short; pending not set; the start point ignored; maxed inverted; added buffering on <=; the shim's enum values swapped). The survivor, >= → > on the start point, is equivalent: at a start point of exactly 1024 no frame is needed, so nothing is short.

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_min_ts/earlier                    1.939 ns / 2.374 ns
audio_min_ts/insufficient_inside_tick   5.272 ns / 5.280 ns

Both run once per audio tick for every audio source. At this size the runs vary by about 0.4 ns: an earlier run on the same host gave 1.971 / 1.805 ns for earlier, so the two are the same within noise. The real-host before/after (CT104, NDI/ALSA audio) for #2404 and this slice together follows with the next production rebuild after the merge.

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:51
…-ts.c

The per-source decisions of the "get minimum audio timestamp" step of
the audio tick move from obs-audio.c to obs-audio-min-ts.c, on plain
values:
- obs_audio_min_ts_earlier(): find_min_ts's test for one source (not
  pending, a timestamp, strictly earlier than the minimum);
- obs_audio_min_ts_insufficient(): audio_buffer_insufficient_locked, on
  audio_render, audio_pending by pointer, audio_ts and channel 0's
  buffered size;
- obs_audio_min_ts_buffering(): the choice after the search between
  fixed, added and no buffering.

The walk over the audio sources, the locks, the two passes of
calc_min_ts, the names and the logging stay in obs-audio.c. Each source
is still locked on its own while it is checked, so the values are read
when they were before. Behaviour-neutral: audio_buffering_maxed() is
still only read with fixed_buffer set.

Refs obs-rust#2405

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cases for obs-audio-min-ts.c before its port, on plain values: the
earlier-source test (pending, no timestamp, equal, earlier), the
sufficiency check (render, pending and untimed sources; a whole tick
and one sample less on min_ts and min_ts - 1; a start point inside the
tick; a start point at and past AUDIO_OUTPUT_FRAMES, which is never
short, where discard_audio stops only on ==), and the choice between
fixed, added and no buffering.

Through calc_min_ts in obs-audio.c, on a list of sources: the earlier
source and its name, ties kept by the first (newest) source, no source
earlier than the tick.

Kept, characterized, not endorsed: when the source with the minimum is
short for a tick at its own timestamp, mark_invalid_sources marks it
pending and calc_min_ts searches again, but from the first pass's
minimum. The comparison is strict, so the second pass finds nothing:
the minimum stays the timestamp of the source just marked pending,
audio_callback adds buffering for it, and the name is NULL. This is in
the code since the audio subsystem of 2015 (c1dd156).

Refs obs-rust#2405

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

RED for the port of libobs/obs-audio-min-ts.c.h, the per-source
decisions of the minimum audio timestamp step of the audio tick, to a
new module, audio_min_ts, of rust/obs-core. The safe core is a stub
(todo!()): earlier() (find_min_ts's test for one source), insufficient()
(audio_buffer_insufficient, pending by reference) and buffering() with
BufferingChoice (none, fixed, added).

tests/obs_core/audio_min_ts.rs ports the 4 plain-value obs-audio-min-ts.c
cases of test_audio_buffering_subsample 1:1, under the same names, in
the same order, with the same inputs and expectations. The fifth cmocka
case checks the walk in obs-audio.c and has no counterpart.

New edge cases (edge_*): min_ts of 0 (min_ts - 1 wraps, as in C) and a
sample rate of 0 (every offset maps to frame 0). Proptests: earlier()
only lowers min_ts, to a valid earlier source's timestamp; insufficient()
sets pending exactly when it returns true and never clears it, and is
never short with a whole tick buffered.

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

Refs obs-rust#2405

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Ports libobs/obs-audio-min-ts.c.h, the per-source decisions of the
minimum audio timestamp step of the audio tick, to the audio_min_ts
module of rust/obs-core. The walk over the audio source list, the
per-source locks, the two passes of calc_min_ts, the names and the
logging stay C in obs-audio.c.h, so each value is read when it was
before. The hybrid libobs drops the C file and links the Rust shims in
its place.

Safe core (src/audio_min_ts.rs): earlier() (not pending, a timestamp,
strictly earlier: the new minimum), insufficient() (whether channel 0's
buffered bytes are short for a tick at min_ts; a short source is marked
pending) and buffering() (fixed, added or no buffering). 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_min_ts.rs): the three functions keep the C
signatures; the choice is returned as the int of enum
obs_audio_buffering_choice.

Kept, characterized, not endorsed: the sufficiency check stops at a
start point >= frame 1024 (discard_audio: ==); ties keep the first
(newest) source; calc_min_ts's second pass never raises the minimum
(C side).

Intentional differences, where C dereferences null and libobs never
gets there: a null min_ts or pending does nothing and returns false.

C side: the file leaves libobs.core-output for its own group,
libobs.ported-audio-min-ts (LIBOBS_PORTED_SOURCES), whose PCH is
obs-audio-min-ts.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_min_ts passes 8/8.

Refs obs-rust#2405

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Oracle (oracle/audio_min_ts.c): the original obs-audio-min-ts.c.h,
unmodified, with its three functions renamed to oracle_*, plus the size
and values of enum obs_audio_buffering_choice. util_mul_div64 stays the
header inline.

Tier 3 (tests/obs_core/audio_min_ts_parity.rs): both sides get the same
values through their C signatures, and are compared: the result, the
minimum after obs_audio_min_ts_earlier, and pending after
obs_audio_min_ts_insufficient:
- the enum's size and values;
- every start frame at 44.1 and 48 kHz, on the frame's time and 1 ns
  either side, with the buffer one sample short, exact and one sample
  over;
- 50 000 random cases per function (timestamps near the minimum, near 0
  and near u64::MAX, any buffered size, rates from 0 to 384 kHz and
  any).

Excluded, the intentional difference, tested on the shim alone: null
pointers.

port_comparison (benches/port_comparison/audio_min_ts.rs): the
earlier-source test, and the sufficiency check of a source with enough
input and a start point inside the tick.

obs_core::audio_min_ts and audio_min_ts_parity pass 14/14 on macOS
arm64.

Refs obs-rust#2405

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

@zackees zackees left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed.

  • The three decisions match the removed C: earlier is the same !pending && ts && ts < min test. insufficient keeps the audio_ts != min_ts && != min_ts - 1 start-point trim, the >= AUDIO_OUTPUT_FRAMES early return and setting audio_pending through the pointer. The switch keeps the fixed / maxed / min_ts < ts.start branches.
  • The audio_buffering_maxed() call stays behind fixed_buffer through the &&, so it is still only called when the C called it.
  • The walk, the locks and the logging stay in C, for the reason given.
  • test_audio_buffering_subsample.c is the obs-rust characterization file of the earlier slices (#1905, #2404). The cases are only added, and the one removed line is its header comment.
  • The export, hidden and macOS lists, the unity group, source-sets.json and the Tier 3 parity tests are all there. LGTM.

@zackees
zackees enabled auto-merge October 25, 2026 18:02
@zackees
zackees added this pull request to the merge queue Oct 25, 2026
Merged via the queue into obs-rust:master with commit b652da8 Oct 25, 2026
35 checks passed
@vpacher
vpacher deleted the port/audio-min-ts branch October 25, 2026 18:53
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 min-timestamp decisions in obs-audio.c to obs-core (#1903 slice 3)

2 participants