Skip to content

rust: Port the audio min-timestamp decisions in obs-audio.c to obs-core (#1903 slice 3) #2405

Closed

Description

@vpacher

Unit

The per-source decisions of the "get minimum audio timestamp" step of the audio tick in libobs/obs-audio.c.h, into rust/obs-core as obs_core::audio_min_ts. Slice 3 of obs-audio.c, after #1903 (audio_mix) and #2403 (audio_discard, #2404). It moves first to libobs/obs-audio-min-ts.c.h.

  • find_min_ts, the comparison for one source: a source counts when it is not pending, has a timestamp, and is strictly earlier than the current minimum.
  • audio_buffer_insufficient_locked: whether a source's input holds enough audio for a tick that starts at min_ts; if not, the source is marked pending.
  • The choice after the search in audio_callback: fixed buffering (when fixed_buffer and not at the maximum), added buffering (when min_ts is before the tick start), or none.

These stay C in obs-audio.c.h: the walk over first_audio_source / next_audio_source under audio_sources_mutex, each source's audio_buf_mutex, the two passes of calc_min_ts, obs_source_get_name, and the logging. The walk stays C on purpose: each source is locked on its own, one after the other, and other threads change audio_ts and the input buffers between the passes. A Rust walk over a snapshot would change when the values are read.

Phase and position

Phase 6 (#444), the third slice of obs-audio.c. Crate obs-core, next to audio_mix and audio_discard. Size: S. After it, obs-audio.c.h keeps the audio tree walk, the monitoring checks, the task queue and audio_callback itself.

Seam

Plain values and one field by pointer: audio_render (as a bool), audio_pending (by pointer, set when the input is short), audio_ts, channel 0's input size in bytes, the sample rate and min_ts. No struct of obs-internal.h goes to Rust, and no public header changes. The functions run under the source's audio_buf_mutex, which the C keeps.

Characterized behaviour to keep

  • The second pass of calc_min_ts cannot raise the minimum. When mark_invalid_sources marks a source pending (its input is short for a tick at min_ts), calc_min_ts runs find_min_ts again, but from the minimum of the first pass, not from the tick start. The comparison is strict, so the second pass finds no source and only sets the name to NULL. The minimum stays the timestamp of the source that was just marked pending, audio_callback adds buffering for it, and the "adding N milliseconds of audio buffering" line names (null). This is in OBS since the new audio subsystem (2015, c1dd156db). Only a concurrent change between the passes can lower the minimum. Confirmed on the C (libobs and libobs-c-reference) with a characterization case on a list of sources. The port keeps it; a change would be its own issue.
  • audio_buffer_insufficient returns false for a start point at or past AUDIO_OUTPUT_FRAMES (>=), while discard_audio stops only on ==.
  • Ties keep the first source in the list (the most recently created audio source, since sources are added at the head).

Existing tests

  • No test covers these decisions. The new cases go into test_audio_buffering_subsample, which compiles obs-audio.c.h and the extracted cores into itself.
  • test_audio_clock_drift, test_audio_sync_offset_change, test_audio_capture_mix_race and test_async_delay_audio run the real mixer through OBS::libobs and are the Tier 2 for this slice.

New characterization cases:

  • The comparison: pending, without a timestamp, equal to the minimum (not taken), earlier (taken).
  • Insufficient: a render source, a pending source, no timestamp (all false, not marked); a start on min_ts and on min_ts - 1 with exactly a tick and one sample less; a start point inside the tick; a start point at and past frame 1024 (false); the pending flag set only when short.
  • The choice: fixed and not at the maximum, fixed at the maximum, not fixed with min_ts before, at and after the tick start.
  • Through the C wrappers, on a struct obs_core_data with two and three sources in the list: the minimum, the name, and the case above where the source with the minimum is short (it is marked pending, the minimum does not change, the name becomes NULL).

Plan

One PR, in this order:

  1. Extraction commit, behaviour-neutral, no Rust: the three decisions into libobs/obs-audio-min-ts.c.h, declared in libobs/obs-audio-min-ts.h without EXPORT. The walk and the passes in obs-audio.c.h call them. The unchanged C tests pass.
  2. Characterization commit: the cases above, against the C reference and the hybrid libobs.
  3. RED: the safe core in obs-core with todo!(), Tier 1 tests in obs-tests-core (the cmocka cases 1:1, plus properties), failing.
  4. GREEN: the core, a thin hidden #[no_mangle] shim, the include moved to a libobs.ported-audio-min-ts group in libobs/build/source-sets.json, Tier 3 proptest against the C oracle, and port_comparison numbers for the per-source check.
  5. Tier 2: the real-mixer tests above with Rust off and on. Tier 4: n/a.

The per-source check runs on every audio tick for every audio source. The real-host before/after (CT104, NDI/ALSA audio) is measured once for #2403 and this slice together, at the production rebuild after both merge.

Activity

  1. vpacher commented on Oct 25, 2026

    @vpacher
    MemberAuthor

    Claiming: #2405

    Plan as in the description: one PR (extraction, characterization, RED, GREEN with Tier 3 and port_comparison, Tier 2). The work is done locally on top of #2404; the PR follows after the checks on the rebased head.

  2. added
    ready
    on Oct 25, 2026
  3. vpacher commented on Oct 25, 2026

    @vpacher
    MemberAuthor

    Progress: #2406 is open with the whole plan (extraction, characterization, RED, GREEN, Tier 3). Tier 1/3 68/68 for obs_core::audio* on Linux, Tier 2 5/5 on the hybrid and the C reference, mutation check 13/14 (the survivor is equivalent). The calc_min_ts second-pass behaviour is confirmed on the C and pinned by calc_min_ts_through_the_list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ready

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions