Skip to content

rust: Port the audio input discard steps in obs-audio.c to obs-core (#1903 slice 2) #2403

Closed

Description

@vpacher

Unit

The steps of the audio tick that drop audio from a source's input buffer in libobs/obs-audio.c.h, into rust/obs-core as obs_core::audio_discard. Slice 2 of obs-audio.c, after #1903 (slice 1, obs_core::audio_mix, #1905 / #1909). It moves first to libobs/obs-audio-discard.c.h.

  • ignore_audio: a source that lags behind the tick start at maximum buffering. Drop the lagging samples (rounded up, cut at what is buffered) and move audio_ts on; adjust a 1 ns rounding error; or, when that is not enough or there is no timestamp, restart the source's audio (audio_pending, audio_ts = 0, timing_set = false). The result tells the caller to render the source again.
  • discard_if_stopped: the "pending stop" check. When the buffered size does not change for two ticks, clear the buffer and reset the timestamp.
  • discard_audio: after the mix, drop the frames of this tick from the input buffer and set audio_ts to the tick end. It covers the early returns (render sources, a source ahead of the window, a source behind it, a start point equal to AUDIO_OUTPUT_FRAMES) and the not-enough-data path (audio_ts = ts->end).

These stay C in obs-audio.c.h: audio_callback, the source tree walk, the locks, the blog lines (the core returns the values they print), the DEBUG_AUDIO asserts, the min-timestamp search (find_min_ts, audio_buffer_insufficient, mark_invalid_sources, calc_min_ts: slice 3) and the monitoring checks.

Phase and position

Phase 6 (#444), the second slice of obs-audio.c. Crate obs-core, next to audio_mix. Same shape as slice 1: helpers declared in a header without EXPORT, hidden shims, and the C core compiled into the cmocka test for Tier 3.

Seam

The functions change seven fields of struct obs_source: audio_input_buf[] (deques), audio_ts, last_audio_input_buf_size, audio_pending, pending_stop, timing_set, and read info.audio_render. As in slice 1 (struct obs_audio_buffering), the C passes them by pointer in a small struct declared in obs-audio-discard.h:

struct obs_audio_input {
	struct deque *buf; /* audio_input_buf, `channels` entries */
	uint64_t *audio_ts;
	size_t *last_size;
	bool *pending;
	bool *pending_stop;
	bool *timing_set;
};

The deque pops use obs-util::deque on the C-owned struct deque in place (ffi::deque), as slice 1 does for buffered_timestamps. No obs-internal.h layout goes to Rust, and no public header changes. The functions run under the source's audio_buf_mutex, which the C keeps.

Kept as in C: the sample count comes from channel 0's buffer, and every channel pops that size.

Existing tests

New characterization cases:

  • ignore_audio: data without a timestamp (all popped, restart, no rerender); a lag of 1 ns, of one sample and of a partial sample (the round-up); a lag larger than the buffer (cut, then restart); the start_ts - 1 rounding adjustment; back in sync (rerender); no data while pending (no warning) and while not pending (warning).
  • discard_if_stopped: empty buffer; the size changed (remembered); the same size once (pending_stop set) and twice (cleared).
  • discard_audio: a render source; audio_ts at or after the tick end; behind the tick start, with and without the stopped check; on ts->start and ts->start - 1; a start point inside the tick and at AUDIO_OUTPUT_FRAMES; not enough data (audio_ts = ts->end); a full discard on 1, 2 and 8 channels.

Plan

One PR, in this order:

  1. Extraction commit, behaviour-neutral, no Rust: move the three functions into libobs/obs-audio-discard.c.h with the struct obs_audio_input signatures, declared in libobs/obs-audio-discard.h without EXPORT. They return a small step struct with the values that obs-audio.c.h logs. The callers fill the struct and log. 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: the dropped sample count never exceeds what is buffered, audio_ts never moves backwards except to 0, the buffer is either untouched, cut by the tick, or empty), failing.
  4. GREEN: the core, a thin hidden #[no_mangle] shim, the include moved to a libobs.ported-audio-discard group in libobs/build/source-sets.json, Tier 3 proptest against rust/obs-c-oracle/legacy/ (the extracted C), and port_comparison numbers for discard_audio.
  5. Tier 2: the real-mixer tests above with Rust off and on. Tier 4: n/a.

discard_audio runs on every audio tick for every audio source. Besides port_comparison, I measure it on a real OBS host (NDI/ALSA audio) before and after, and put the numbers in the PR.

Next slice

Slice 3: the min-timestamp search (find_min_ts, audio_buffer_insufficient, mark_invalid_sources, calc_min_ts). It walks the audio source list under audio_sources_mutex, so it needs its own check for a seam. Filed when this one closes.

Activity

  1. vpacher commented on Oct 25, 2026

    @vpacher
    MemberAuthor

    Claiming: #2403

    Plan as in the description: one PR (extraction, characterization, RED, GREEN with Tier 3 and port_comparison, Tier 2), with a short progress note here after each step that lands. First PR expected within two days.

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

    @vpacher
    MemberAuthor

    Progress: #2404 is open with the whole plan (extraction, characterization, RED, GREEN, Tier 3). Tier 1/3 54/54 on Linux and macOS, Tier 2 5/5 on the hybrid and the C reference, mutation check 19/19. The real-host before/after follows with the next production rebuild after the merge.

    Found while characterizing: without a timestamp, ignore_audio clears only channel 0 (it pops audio_input_buf[0].size from each channel and reads that size again per channel). The port keeps it and the PR documents it; a fix would be its own issue.

  4. moved this from In progress to Done in obs-rust Multiviewon Oct 25, 2026
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