Skip to content

rust: Port the audio mix and buffering math in obs-audio.c to obs-core (#1846 unit 10, slice 1) #1903

Closed

Description

@vpacher

Unit

The arithmetic of the audio tick in libobs/obs-audio.c.h (746 lines), into rust/obs-core as obs_core::audio_mix. Unit 10 of #1846, slice 1. It moves first to libobs/obs-audio-mix.c.h.

  • convert_time_to_frames: ns to frames at the sample rate (util_mul_div64).
  • mix_audio, the part that does not read the source: the start point and the frame count of a source's audio in the tick window (ts_info), the early returns (audio outside the window, start point equal to AUDIO_OUTPUT_FRAMES), and the add loop over MAX_AUDIO_MIXES mixes and the channels.
  • set_fixed_audio_buffering and add_audio_buffering, the arithmetic: the number of ticks to add (rounded up from the frame offset, capped at max_buffering_ticks), the new totals, the millisecond values for the log lines, and the sequence of ts_info values that go to the front of buffered_timestamps.

These stay C in obs-audio.c.h: the source tree walk (push_audio_tree*), audio_callback, the locks, ignore_audio, discard_if_stopped, discard_audio, the min-timestamp search, the monitoring checks, the blog lines and the deque calls. The C passes the values in and takes the results back.

Correction to the meta: obs-audio.c.h does not clamp. Clamping happens in media-io/audio-io, after the mix.

Phase and position

Phase 6 (#444), first slice of a large file, as the meta says for unit 10. Crate obs-core (it exists; video_plan from #1713 has the same "C gives the values, Rust computes" shape). The precedent for an extracted libobs core helper is #1814 / #1842 (obs-scene-audio-actions.c.h): helpers declared in a header without EXPORT, hidden shims, and a test that compiles the C core into itself for Tier 3.

Seam: none needed

The extracted functions take plain values (timestamps, sample rate, channel count, float pointers, and the four buffering fields of struct obs_core_audio: buffered_ts, buffering_wait_ticks, total_buffering_ticks, max_buffering_ticks). The C reads source->audio_ts and source->audio_output_buf and passes them. No obs-internal.h layout goes to Rust, and no public header changes.

audio_frames_to_ns and ns_to_audio_frames are already in obs-media-io::audio_io.

Existing tests

  • test_audio_buffering_subsample (Flaky: real-time audio cmocka tests lose audio under machine load (test_vlc_playlist_gap, test_group_hide_transition_audio) #838) compiles libobs/obs-audio.c.h into itself. It tests the C buffering, not the swap, and says so. After the extraction it also needs obs-audio-mix.c.h: one #include line in the extraction commit, no change to its cases.
  • test_audio_clock_drift, test_audio_sync_offset_change and test_audio_capture_mix_race run the real mixer through OBS::libobs. They see the swap and are the Tier 2 for this slice.
  • No test pins the mix window edges. New cases go into test_audio_buffering_subsample (no new executable): audio before and at the window start, at and after the window end, a start point that rounds to AUDIO_OUTPUT_FRAMES, and a partial window. Also the buffering edge cases: a sub-sample offset (no ticks), an offset that reaches the cap ("Max audio buffering reached!"), and fixed buffering when it is already at the maximum.

Plan

One PR, in this order:

  1. Extraction commit, behaviour-neutral, no Rust: move the arithmetic into libobs/obs-audio-mix.c.h with plain-value signatures, declared in libobs/obs-audio-mix.h without EXPORT, included from libobs/build/libobs.core-output+.h.c. The callers in obs-audio.c.h pass the fields. The unchanged C tests pass.
  2. Characterization commit: the new 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 window and tick properties), failing.
  4. GREEN: the core, a thin hidden #[no_mangle] shim, the include moved to a libobs.ported-audio-mix group in libobs/build/source-sets.json, Tier 3 proptest against rust/obs-c-oracle/legacy/ (the extracted C), and port_comparison numbers for the add loop and the buffering step.
  5. Tier 2: the real-mixer tests above with Rust off and on. Tier 4: n/a.

The add loop runs on every audio tick for every root source, mix and channel. Besides port_comparison, I measure it on a real OBS host (CT104, NDI/ALSA audio) before and after, and put the numbers in the PR.

Next slices (filed here when this one closes)

ignore_audio, discard_if_stopped and discard_audio (the deque arithmetic over audio_input_buf), then the min-timestamp search. They read more source fields, so each needs its own check for a seam.

Activity

  1. vpacher commented on Oct 23, 2026

    @vpacher
    MemberAuthor

    Claiming: #1903 (#1846 unit 10, slice 1, the audio mix and buffering math in obs-audio.c.h). First PR: the extraction and characterization commits, then RED and GREEN in the same PR, as in the plan above.

  2. added
    claimed
    on Oct 23, 2026
  3. moved this from In progress to Triage in obs-rust Multiviewon Oct 25, 2026
  4. removed
    claimed
    on Oct 25, 2026
  5. zackees commented on Oct 25, 2026

    @zackees
    Member

    Closing as done. Both steps of the plan merged: #1905 moved the audio tick arithmetic into libobs/obs-audio-mix.c.h with characterization, and #1909 ported it to obs_core::audio_mix behind the seam with Tier 1 and Tier 3 tests (mutation check: 20 of 21 killed, and the survivor is equivalent). #1909 used Refs, so this didn't close by itself. The next slices of #1846 unit 10 go in their own issues. Thanks @vpacher.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions