Skip to content

obs-source: Port the source audio placement and action timeline - #2469

Merged
zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/audio-place
Oct 26, 2026
Merged

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/audio-place

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

Fixes #2461

What and why

Ports the source audio placement and action timeline of libobs/obs-source.c.h to rust/obs-source, as a new module audio_place, next to audio_timing and volume_apply. Every audio packet a source outputs goes through the placement, and every tick with queued audio actions (volume, mute, push-to-talk, push-to-mute) goes through the timeline:

  • source_output_audio_place() with get_buf_placement(): the reset when there is no audio_ts or the packet is older, the packet's size and byte placement, the drop past MAX_BUF_SIZE, and how much is popped off the back of each channel after the place;
  • source_output_audio_push_back(): the drop past MAX_BUF_SIZE;
  • the last step of source_output_audio_data(): nothing for a monitor-only source, else push back (with an audio_ts) or place;
  • apply_audio_actions(): each action's timestamp clamped to audio_ts, its frame, the stop at AUDIO_OUTPUT_FRAMES, how far the previous volume fills, and the mixes multiplied.

The audio timing unit (#1932, which created rust/obs-source) ported the timestamp decisions of source_output_audio_data() and explicitly left "the deque place/push-back" in C, so none of this was covered before.

The plan's API was split per step. The plan sketched place(in) -> Reset + Drop | Place { offset, pop_back }, route(monitoring_type, push_back, audio_ts) -> Skip | PushBack | Place and action_frame(ts, audio_ts, rate) -> Stop | At(n). Those would read audio_ts once where the C reads it twice (the place's reset, the action clamp) and read it in the route when push_back is not set. So each step is its own function, asked at its own point (as #2153 did), and eleven functions cross the C ABI:

  • obs_source_audio_place_started() (audio_ts != 0) and _older() (timestamp < audio_ts): the place's reset, the push-back rule and the action clamp;
  • _size(), _offset(), _fits() and _pop_back(): the packet's bytes, its byte placement, the drop for the place and the push back, and the pop;
  • _outputs(): not OBS_MONITORING_TYPE_MONITOR_ONLY;
  • _action_frame(), _action_stops(), _fill_end() and _mix_on().

What stays C, at its point and under its condition:

  • every read of the source, with the C's && and || around the calls: audio_ts once or twice for the place's reset, again after reset_audio_data() for the placement, in the route only when push_back is set, and in the action loop once for the clamp, again when it applies and again for the frame; monitoring_type before audio_ts; each channel's size after its deque_place(), the first channel's before a push back; audio_mixers once per mix;
  • audio_buf_mutex and audio_actions_mutex, audio_output_get_channels(), and get_buf_placement() with its audio_output_get_sample_rate() call. It now takes the two timestamps, read before it asks for the rate, as when their difference was its argument;
  • reset_audio_data(), deque_place(), deque_pop_back(), deque_push_back(), the DEBUG_AUDIO log and the stores of last_audio_input_buf_size;
  • da_erase(), apply_audio_action(), every get_source_volume() call (before the loop and after each applied action, so each action's push-to-talk and push-to-mute state is seen, as today), the volume array and multiply_vol_data().

conv_time_to_frames() had no other caller. Its expression is now _action_frame(), so it is deleted. The header copies AUDIO_OUTPUT_FRAMES, MAX_BUF_SIZE and OBS_MONITORING_TYPE_MONITOR_ONLY, and obs-source.c.h checks them, and that enum obs_monitoring_type is int-sized, with _Static_assert. No pointer crosses and no struct is mirrored.

Six commits:

  1. Move (behaviour-neutral, 22a2f4c36): the decisions go to libobs/obs-source-audio-place.c.h, declared in the new obs-source-audio-place.h without EXPORT, so they are hidden. The file joins libobs.core-source. git blame on the moved lines shows only upstream commits. The fork's one line there, the DEBUG_AUDIO #if, stays in place.

    The move was checked against the original code. A scratch harness (not committed) compiles origin/master's get_buf_placement(), conv_time_to_frames(), source_output_audio_place(), source_output_audio_push_back(), source_output_audio_data() (the whole function) and apply_audio_actions() verbatim, next to this branch's and the moved file, as C++:

    • the stand-in source's fields are wrappers that log every read and write, with the value;
    • every call is logged in order with its arguments: both mutexes, audio_output_get_sample_rate() and _get_channels(), os_gettime_ns(), both audio timing steps, handle_ts_jump(), the resampler's compensation, every deque call, reset_audio_data() (shared, with its own field accesses), blog(), da_erase(), apply_audio_action(), get_source_volume(), multiply_vol_data() (with a hash of the whole volume array), source_muted() and the audio signal;
    • the stubs answer from one seeded stream in call order. A concurrent writer may move audio_ts, monitoring_type, audio_mixers or a channel's size at any call;
    • the inputs cover audio_ts of 0 and around the packet; timestamps at it, a few ns, whole frames, blocks and seconds from it, the ends of int64 and uint64, and random; frames of 0 to UINT32_MAX around a block and MAX_BUF_SIZE; rates of 8000 to 384000 Hz, 0, 1, 1e9 and UINT32_MAX (any size_t for the actions); 0 to 8 channels; channel sizes around MAX_BUF_SIZE; every monitoring type and 7; 0 to 7 actions of every type; any mixer mask.

    25,400,000 runs (GCC and Clang -O2, 10,000,000 each; -O0, 2,000,000 each; ASan with UBSan, no recovery, under GCC -O0 and Clang -O2, 500,000 each; gcov, 400,000): all 1,077,683,735 logged events match on both sides, and so does the final state. Every branch of origin/master's functions is taken both ways, except the timing switch's default edge, which the stub never returns.

    22 deliberately broken moves were tried, and 21 fail the harness:

    • audio_ts read once for the place's reset; the placement taken before the reset; audio_ts read before push_back; monitoring_type read after audio_ts;
    • audio_ts read once for the action clamp; get_source_volume() before the fill; apply_audio_action() dropped; audio_mixers read once for the loop;
    • thirteen changed decisions: each comparison moved by one, the size in uint32_t, the placement in frames and rounded up, the fit as a saturating sum, the pop one short, monitor-only for NONE, the frame's rate and duration swapped, the fill one short, the next mix bit.

    The 22nd, _fill_end() always returning the new frame, is equivalent: the C's loop condition already fills nothing when the new frame is not past frame_num.

  2. Characterization (01e5bc174): test_source_audio_place_steps, 13 cases. It compiles the moved file with ASan and UBSan and calls only its functions. It pins every decision over edge values, at 8000, 44100, 48000 and 384000 Hz, and copies the four callers onto a stand-in source:

    • placement at 0, one frame, less than a sample, an older packet (reset) and one second later;
    • a block just under, exactly at and one frame past MAX_BUF_SIZE (placed, placed, dropped), and the same for the push back;
    • every monitoring type with and without push_back and an audio_ts;
    • actions before, at and after audio_ts, at frames 1023 and 1024, the fill, none, out of order, and the mixes.

    It is in the test graph, sorted, with the header as a watch input.

  3. RED (bab3988c2): Tier 1 against todo!(). It ports the 13 C cases 1:1 and adds 8 edge tests: every decision against models of the C over pairs of 26 special timestamps at ten exact rates, pairs and triples of 15 special sizes, the monitoring types from -70,000 to 70,000, every low 16-bit mask at every bit, the first nanosecond of every frame from 0 to 1025 at eleven rates, and proptests over full-range values and over packet and action sequences through the stand-in against origin/master's expressions inline.

  4. GREEN (b9cd6a191): the safe core (eleven functions) and eleven #[no_mangle] shims, in a new libobs.ported-source-audio-place group whose PCH is only the new header and util/util_uint64.h.

    • The commit updates source-sets.json, the unity lists, the three export lists and ci/test_ported_sources.py, each in sorted place.
    • The conversion reuses transition_render's per-host util_mul_div64() (made pub(crate), with to_size()), so the crate still depends on no other crate.
    • test_source_audio_place_steps_rust builds the unchanged test source against libobs_rust-static, as test_source_video_tick_steps_rust does.
  5. Tier 3 (5265f2c91): a C oracle of the unchanged file, the parity tests and a mutation check.

  6. Docs (b55677cc6): the README rows, the crate's description, lib.rs, and a port_comparison bench module in the sorted lists. The port changes nothing a user can see, so docs/features.md has no line.

The core denies clippy's arithmetic, indexing, as, unwrap, expect, panic, todo, unimplemented and unreachable lints. It keeps no state and takes no lock. Every sum, difference and product wraps where the C's unsigned arithmetic does.

  • Kept, characterized, not endorsed (upstream behaviour, unchanged):
    • a packet less than one sample after audio_ts is placed at 0, on top of the samples already there, and an action that close applies at frame 0: the conversion rounds down;
    • a packet larger than MAX_BUF_SIZE on its own (over 21.3 s at 48 kHz) is dropped whole, even into an empty buffer;
    • a push back checks only the first channel's size;
    • an action before audio_ts applies at frame 0. Once one action is at frame 1024 or later, the loop stops and every later action in the queue waits, even one that is already due. A later action at an earlier frame fills nothing;
    • a monitoring type outside the enum outputs;
    • the fit's sum and the pop wrap as size_t, and the placement and frame of a timestamp before audio_ts wrap. The callers reset or clamp first, so neither is reachable.
  • Intentional differences (inputs libobs never passes):
    • _mix_on() with a mix of 32 or more says no mix. The C's int shift is undefined from 31 on, and libobs asks only 0 to 5;
    • on MSVC x64, a frame count past 64 bits saturates where the C's _udiv128() faults. That needs a rate above 1e9 Hz.

Bench (x86_64 Linux, GCC, release, not pinned to a core, a loaded host; median per 1024 packets, each through the route, the place with two channels, one action and its six mixes): Rust 22.12 µs, C 20.97 µs (1.05x), about 1 ns a packet.

RED → GREEN

  • RED commit: bab3988c2 (obs-source: Test the audio_place port).
    • soldr cargo test -p obs-data --test obs-tests-core -- source::audio_place: 1 passed (the constants, which are not stubs), 20 failed, each on a todo!().
    • On that commit, local_gate --lane lint was clean, and --test-diff origin/master ran 4266 tests: 4246 passed, 6 skipped, and only those 20 failed.
  • GREEN (b9cd6a191): Tier 1 21/21.
    • test_source_audio_place_steps passes 13/13 on the C backend, on hybrid libobs and on libobs-c-reference, and test_source_audio_place_steps_rust passes 13/13 against the Rust archive.
    • test_async_delay_audio, test_audio_sync_offset_change, test_audio_clock_drift, test_audio_buffering_subsample, test_media_sync_offset_restart, test_audio_restart_resampler_tail, test_source_volume_mix, test_source_audio_ranges, test_audio_controls, test_source_audio_timing_steps and test_source_volume_apply_steps pass on all three, run through ctest under a private Xvfb, with every case run and none skipped (156 cmocka cases on the C backend, 169 with the _rust variant on the other two).
    • Exported symbols are identical on all three (1766). The eleven functions are local. In the hybrid they come from the Rust archive, and the group's object is built only for libobs-c-reference.
  • Tier 3 (5265f2c91): 11/11, oracle, shims and core compared exactly.
    • The header's values and sizeof(size_t).
    • The reset rules, the placement and the action frame over every pair of 30 special timestamps at 16 rates (to UINT32_MAX, and size_t rates past 32 bits for the frame).
    • Every frame edge from 0 to 2048 at eleven rates.
    • The sizes over every pair, and the pop over every triple, of 17 special values.
    • Every monitoring type from -100,000 to 100,000; every low 16-bit mask under eight high words at every mix to 30.
    • The callers copied onto a stand-in source, over the oracle and over the shims: 36,864 packet pairs and 21,316 action lists, comparing every trace, buffer, volume array and mix.
    • Proptests of 20,000 cases each, over full-range values and over packet and action sequences from any state.
    • Excluded, and only these: the MSVC x64 _udiv128() fault inputs and the mix rule at 31 and up.
  • Mutation check: 28 mutants (18 in the core and its conversion, 10 in the shims).
    • 25 are killed: Tier 1 kills 16 of the 18 core mutants. The other two (a rate truncated to 32 bits, the exact quotient on every host) fall to Tier 3. Tier 3 kills every shim mutant that changes a result.
    • The three survivors are equivalent: the shim's fits() and pop_back() with arguments swapped add them the same way, and fill_end() is the larger of two frames either way round.
  • Checks on b55677cc6 (the head, on 7ef75ab02):
    • local_gate --lane lint, --test-diff origin/master and --lane cross (clippy for x86_64-pc-windows-msvc and aarch64-apple-darwin, with SOLDR_RUSTC_WRAPPER=none) all pass (test-diff: 4277 passed, 6 skipped);
    • SOLDR_RUSTC_WRAPPER=none soldr cargo clippy --workspace --target x86_64-pc-windows-msvc --all-targets --locked -- -D warnings is clean (the cross lane lints only --lib --bins without the oracle);
    • soldr cargo test -p obs-c-oracle --locked --bench port_comparison passes.
  • Full ctest (C backend, hybrid libobs, libobs-c-reference) on b55677cc6, under a private Xvfb: 100% passed on each: 522/522 (C backend), 551/551 (hybrid), 551/551 (libobs-c-reference), with no reruns. The exported symbol sets of those builds are still identical (1766).
  • Pinned suite counts: none cover this suite.
  • Real app: not applicable (no UI, protocol or logging change).

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • 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 (N/A for docs-only; a focused CI fix may push RED and GREEN together)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): two, _mix_on() from mix 32 and the MSVC x64 fault inputs

Behaviour-neutral extraction before the Rust port. The decisions of
get_buf_placement(), source_output_audio_place(),
source_output_audio_push_back(), the last step of
source_output_audio_data() and apply_audio_actions() in obs-source.c.h
move to libobs/obs-source-audio-place.c.h, as eleven functions on plain
values declared in the new libobs/obs-source-audio-place.h without
EXPORT (hidden, as -fvisibility=hidden keeps them):

- _started() (audio_ts != 0) and _older() (timestamp < audio_ts): the
  place's reset, the push-back rule and the action clamp;
- _size(), _offset(), _fits() and _pop_back(): the packet's bytes, its
  byte placement (util_mul_div64() of the difference, times
  sizeof(float)), the drop past MAX_BUF_SIZE for the place and the push
  back, and the bytes popped off the back of each channel;
- _outputs(): nothing for a monitor-only source;
- _action_frame(), _action_stops(), _fill_end() and _mix_on(): each
  action's frame, the stop at AUDIO_OUTPUT_FRAMES, how far the previous
  volume fills and the mixes multiplied.

Split per read, as obs-rust#2153 did, so every read stays at its point and
under its condition: the C's || and && stay in obs-source.c.h, so
audio_ts is read once or twice for the place's reset as before, again
after reset_audio_data() for the placement, and in the route only when
push_back is set; the action clamp reads it again when it applies, and
the frame once more. get_buf_placement() stays C: it takes the two
timestamps, read before it asks audio_output_get_sample_rate() as when
their difference was its argument. Each channel's size is read after
its deque_place(), the first channel's before a push back,
monitoring_type before audio_ts, and audio_mixers once per mix.
audio_buf_mutex, audio_actions_mutex, reset_audio_data(),
deque_place(), deque_pop_back(), deque_push_back(), the DEBUG_AUDIO log,
the stores of last_audio_input_buf_size, da_erase(),
apply_audio_action(), every get_source_volume() call (before the loop
and after each applied action), the volume array and
multiply_vol_data() stay in obs-source.c.h. conv_time_to_frames() had
no other caller; its expression is _action_frame()'s, so it goes.

The expressions move as they were: the uint64_t differences, the
(size_t) casts of util_mul_div64(), the size_t sums and products, and
the int shift of the mix bit.

Checked in a scratch harness (not committed). It compiles
origin/master's get_buf_placement(), conv_time_to_frames(),
source_output_audio_place(), source_output_audio_push_back(),
source_output_audio_data() (whole) and apply_audio_actions() verbatim
next to this branch's and the moved file, as C++, over a stand-in
source whose fields are wrappers that log every read and write with
its value. Every call is logged in order with its arguments: both
mutexes, audio_output_get_sample_rate() and _get_channels(),
os_gettime_ns(), the two audio timing steps, handle_ts_jump(), the
resampler's compensation, every deque call, reset_audio_data() (shared,
with its own field accesses), blog(), da_erase(), apply_audio_action(),
get_source_volume(), multiply_vol_data() (with a hash of the whole
volume array), source_muted() and the audio signal. The stubs answer
from one seeded stream in call order, and a concurrent writer may move
audio_ts, monitoring_type, audio_mixers or a channel's size at any
call. Inputs: audio_ts of 0 and around the packet; packet timestamps
at, one to three ns either side of, whole frames and blocks from, and
seconds before audio_ts, the ends of int64 and uint64, and random;
frames of 0, 1, 480, 1023 to 1025, 4096, around 1024000 and UINT32_MAX;
rates of 8000 to 384000 Hz, 0, 1, 1e9 and UINT32_MAX (and any size_t
for the actions); 0 to 8 channels; channel sizes empty, around
MAX_BUF_SIZE and random; every monitoring type and 7; 0 to 7 actions
of each type at those timestamps; any mixer mask.

25,400,000 runs: 10,000,000 each with GCC and Clang at -O2, 2,000,000
each at -O0, 500,000 each under ASan and UBSan with no recovery (GCC
-O0, Clang -O2) and 400,000 with gcov. All 1,077,683,735 events match,
as does the final state. Every branch of origin/master's functions is
taken both ways, but the switch default of the timing action, which
the stub never returns.

22 deliberately broken moves were planted. 21 fail it: audio_ts read
once for the place's reset, the placement taken before the reset,
audio_ts read before push_back, monitoring_type read after audio_ts,
audio_ts read once for the action clamp, get_source_volume() before
the fill, apply_audio_action() dropped, audio_mixers read once, and
thirteen changed decisions (started at > 1, older at <=, the size in
uint32_t, the placement in frames, rounded up, the fit at < and as a
saturating sum, the pop one short, monitor-only for NONE, the frame's
rate and duration swapped, the stop at >, the fill one short, the next
mix bit). The 22nd, _fill_end() always the new frame, is equivalent:
the C's loop condition fills nothing when it is not past frame_num.

git blame on the moved lines shows only upstream commits (c1dd156,
bc0b85c, d2934ec, 0e4ea14, 2963959, a1fbf10 and earlier);
the fork's one line there (cb8ec4b, the DEBUG_AUDIO #if) stays.

The constants AUDIO_OUTPUT_FRAMES, MAX_BUF_SIZE and
OBS_MONITORING_TYPE_MONITOR_ONLY are copied into the header, so it
needs no core header; obs-source.c.h checks them, and that enum
obs_monitoring_type is int-sized, with _Static_assert. The new file
joins libobs.core-source with libobs.pch.h, which includes the new
header; source-sets.json lists it. docs/rust-port/naming.md already
maps libobs/obs-source*.c.h to rust/obs-source. The exported symbol
sets are unchanged; the eleven functions are local.

Refs obs-rust#2461
test_source_audio_place_steps compiles
libobs/obs-source-audio-place.c.h (ASan and UBSan) and calls only its
functions through obs-source-audio-place.h. 13 cases:

- the header's copies of AUDIO_OUTPUT_FRAMES, MAX_BUF_SIZE and
  OBS_MONITORING_TYPE_MONITOR_ONLY, and the enum's size;
- the started and older rules over every pair of nine edge timestamps;
- the packet's size from 0 to UINT32_MAX frames, in size_t;
- the byte placement at 0, just under and at one frame, and one second,
  at 8000, 44100, 48000 and 384000 Hz, one block short of MAX_BUF_SIZE,
  a rate of 0, and the wrap of an older timestamp;
- the fit just under, at and just over MAX_BUF_SIZE, and the pop off the
  back, with their size_t wraps;
- each monitoring type, and every type from -64 to 64 and the ends of
  int;
- the action frames at, just under and at one frame, at frames 1023 and
  1024 and one second for each rate, the unclamped wrap, and the stop;
- the fill's end and the mix rule over every 8-bit mask.

source_output_audio_place(), source_output_audio_push_back(), the last
step of source_output_audio_data() and apply_audio_actions() are also
copied onto a stand-in source that counts each channel's bytes, to pin
end to end: no audio_ts (reset, placed at 0), one frame later, less
than a sample later (on top of the start, cut back), an older packet
(reset at its own time) and one second later, at each rate; at 48 kHz a
block just under, exactly at and one frame past MAX_BUF_SIZE (placed,
placed, dropped); the push back just under, at and over it; every
monitoring type with and without push_back and an audio_ts; and actions
before, at and after audio_ts, at frames 1023 and 1024 (the second stays
queued with everything after it), the fill up to an action, none, out
of order, and the mixes.

Characterized, not endorsed:
- a packet less than one sample after audio_ts is placed at 0, on top
  of the samples already there, and an action that close applies at
  frame 0: the conversion rounds down;
- a packet larger than MAX_BUF_SIZE on its own is dropped whole, even
  into an empty buffer;
- a push back checks only the first channel's size;
- an action before audio_ts applies at frame 0; once one is at frame
  1024 or later, the loop stops and every later action waits, even one
  that is due; a later action at an earlier frame fills nothing;
- a monitoring type outside the enum outputs;
- the fit's sum and the pop wrap as size_t, and the placement and frame
  of a timestamp before audio_ts wrap (the callers reset or clamp
  first).

It is in the native test graph, in name order, with the header as a
watch input. No assert_in_range or fail_msg, so it builds with cmocka
1.x and 2.x, and no two enum types are compared. The real-libobs tests
test_async_delay_audio, test_audio_sync_offset_change,
test_audio_clock_drift, test_audio_buffering_subsample,
test_media_sync_offset_restart, test_audio_restart_resampler_tail,
test_source_volume_mix, test_source_audio_ranges and
test_audio_controls cover the callers.

Refs obs-rust#2461
RED for the port of libobs/obs-source-audio-place.c.h to a new
audio_place module of rust/obs-source, next to audio_timing and
volume_apply. The safe core is a stub (todo!()): started(), older(),
size(), offset(), fits(), pop_back(), outputs(), action_frame(),
action_stops(), fill_end() and mix_on(), with FRAMES, MAX_BUF_SIZE and
MONITOR_ONLY.

The plan sketched place() -> Reset + Drop | Place, route() -> Skip |
PushBack | Place and action_frame() -> Stop | At(n). Those would read
audio_ts once where the C reads it twice (the place's reset, the
action clamp) and read it in the route when push_back is not set, so
each step is its own function, asked at its own point, as obs-rust#2153 did.

tests/source/audio_place.rs ports test_source_audio_place_steps 1:1:
its 13 cases under the same names (steps_*), in the same order, with
the same values and expectations, the characterized-not-endorsed cases
and the stand-in source's copies of the callers included.

Edge cases (edge_*) check the core against models of the original C:
- every pair of 26 special timestamps (around 0, 2^32, 2^63, u64::MAX,
  one second, one sample and one block at 48 kHz) for the reset rules,
  the placement and the action frame at ten rates where every host's
  util_mul_div64() is exact (0, 1, 8000 to 384000 Hz and 1e9);
- the size over special frame counts, and the fit, the fill's end and
  the stop over every pair, the pop over every triple, of 15 special
  sizes (around a block, MAX_BUF_SIZE, 2^32 and usize::MAX);
- every monitoring type from -70,000 to 70,000 and the ends of int;
- the mix rule for every low 16-bit mask under four high words at every
  bit, and indices past the width;
- the first nanosecond of every frame from 0 to 1025 at eleven rates;
- proptests (4,096 cases each): every decision over full-range values,
  and packet and action sequences through the stand-in against a copy
  of the callers with origin/master's expressions inline.

soldr cargo test -p obs-data --test obs-tests-core -- source::audio_place
passes 1 of 21 (the constants) and fails 20 with "not yet implemented".

Refs obs-rust#2461
Ports libobs/obs-source-audio-place.c.h, the source audio placement
and action timeline of obs-source.c, to the audio_place module of
rust/obs-source. obs-source.c.h stays C around it: every read of the
source (audio_ts at each of its points, monitoring_type, the channels'
sizes and audio_mixers once per mix), audio_buf_mutex and
audio_actions_mutex, audio_output_get_channels() and
get_buf_placement() with its audio_output_get_sample_rate() call,
reset_audio_data(), the deques, da_erase(), apply_audio_action(), every
get_source_volume() call (before the loop and after each applied
action), the volume array and multiply_vol_data(). The hybrid libobs
drops the C file and links the Rust shims in its place.

Safe core (src/audio_place.rs), eleven functions:
- started(): audio_ts != 0; older(): timestamp < audio_ts;
- size(): frames times 4, wrapping usize;
- offset() and action_frame(): util_mul_div64(timestamp - audio_ts,
  rate, 1e9) with the difference wrapping, cast to usize as the C casts
  to size_t; offset() times 4, wrapping;
- fits(): the wrapping usize sum at most MAX_BUF_SIZE; pop_back(): the
  wrapping difference;
- outputs(): not MONITOR_ONLY; action_stops(): frame >= FRAMES;
  fill_end(): the larger frame;
- mix_on(): bit mix of the mask, none past the u32's width.
The module denies clippy's arithmetic, indexing, as, unwrap, expect,
panic, todo, unimplemented and unreachable lints, keeps no state and
takes no lock. The conversion is transition_render's mul_div64() (now
pub(crate), with to_size()): the host's util_mul_div64(), the wrapping
uint64_t formula, or on MSVC x64 the exact 128-bit quotient, which in
the C faults when it does not fit and there saturates. With a divisor
of 1e9 both are the exact quotient for any rate up to 1e9 Hz.

Shims (src/ffi/source_audio_place.rs): the eleven C signatures by
value; the monitoring type as int. No pointer crosses and no struct is
mirrored.

Intentional differences, on inputs libobs never passes: mix_on() with
a mix at or past 32 (the C's int shift is undefined from 31 on) says no
mix; on MSVC x64 a frame count past 64 bits (a rate above 1e9 Hz),
where the C faults, saturates. libobs asks mixes 0 to 5 and passes the
output's sample rate.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-audio-place (LIBOBS_PORTED_SOURCES), whose PCH is
only obs-source-audio-place.h and util/util_uint64.h; libobs.pch.h
keeps the header for obs-source.c. The functions are not EXPORT, so the
shims are hidden: rust-hidden.txt, the local: list of rust-exports.map
and rust-unexports-macos.txt. libobs-rust already links the crate's
ffi. The exported symbol sets are unchanged and equal: 1766 on the C
backend, the hybrid and libobs-c-reference; the eleven functions are
local in all three, and the hybrid's come from the Rust archive.
ci/test_ported_sources.py checks the group on every host.

test_source_audio_place_steps still compiles the C file into itself,
unchanged. The new test_source_audio_place_steps_rust builds the same
test source against libobs_rust-static instead, as
test_source_video_tick_steps_rust does, and links: the test includes
the header and obs.h and calls only the eleven hidden functions.
test_async_delay_audio, test_audio_sync_offset_change,
test_audio_clock_drift, test_audio_buffering_subsample,
test_media_sync_offset_restart, test_audio_restart_resampler_tail,
test_source_volume_mix, test_source_audio_ranges, test_audio_controls,
test_source_audio_timing_steps and test_source_volume_apply_steps see
the swap through the real libobs, and pass on the C backend, the hybrid
and libobs-c-reference.

soldr cargo test -p obs-data --test obs-tests-core -- source::audio_place
passes 21 of 21.

No user-visible change, so no docs/features.md line.

Refs obs-rust#2461
Tier 3 for the audio_place port. oracle/source_audio_place.c compiles
the unchanged libobs/obs-source-audio-place.c.h with its eleven
functions renamed to oracle_*, plus the header's values and
sizeof(size_t).

tests/source/audio_place_parity.rs gives each input to the oracle, to
the shim the hybrid links and to the core, and compares each bool as
the byte the C returns (it must be 0 or 1) and each number exactly:
- the header's values and sizeof(size_t);
- the reset rules over every pair of 30 special timestamps, and the
  placement and the action frame over every pair at 16 rates (0, 1,
  8000 to 384000 Hz, 1e9, 1e9 + 1 and UINT32_MAX), and size_t rates
  past 32 bits for the frame;
- the first and last nanosecond of every frame from 0 to 2048 at
  eleven rates, and a stride of 70,000 differences at five;
- the size over every frame count to 100,000 and every 65,521st
  uint32_t; the fit, the fill's end and the stop over every pair, and
  the pop over every triple, of 17 special sizes;
- every monitoring type from -100,000 to 100,000, every 65,521st int
  and the ends;
- the mix rule for every low 16-bit mask under eight high words at
  every mix from 0 to 30;
- the callers (source_output_audio_place(),
  source_output_audio_push_back(), the last step of
  source_output_audio_data() and apply_audio_actions(), as the C
  test's stand-in source) on the oracle and on the shims: 36,864
  packet pairs over rates, starts, channels, monitoring types, push
  backs, timestamps and sizes around MAX_BUF_SIZE, and 21,316 action
  lists; every trace, buffer, volume array and mix must match;
- proptests (20,000 cases each) of every function over full-range
  values and of the callers over packet and action sequences from any
  state.

Not compared: on MSVC x64 (cfg!(all(target_env = "msvc", target_arch
= "x86_64"))) a placement or frame past 64 bits, where the C's
_udiv128() faults, and the callers at a rate above 1e9 Hz, the only
way to reach one; and the mix rule at 31 and up, where the C's int
shift is undefined. Those are the two intentional differences.
Otherwise no input is excluded.

Mutation check (once, not committed): 28 mutants, 18 in the core and
its conversion and 10 in the shims (failing tests per tier, Tier 1 of
21 and Tier 3 of 11):

  mutant                                  Tier 1  Tier 3
  core: started at > 1                         2       1
  core: older at <=                            4       3
  core: size of frames + 1                     7       4
  core: size in 2-byte samples                 7       4
  core: offset in frames                       7       5
  core: difference saturating                  4       2
  core: rate + 1                               6       5
  core: fits at <                              5       3
  core: fits saturating                        2       1
  core: pop saturating                         3       2
  core: pop one short                          6       4
  core: outputs above monitor only             5       4
  core: action rate truncated to u32           0       2
  core: stop at >                              3       2
  core: fill to the smaller frame              5       4
  core: mix on the next bit                    5       4
  core: no mix 31                              2       0
  conversion: exact quotient on every host     0       3
  shim: older arguments swapped                0       6
  shim: offset timestamps swapped              0       5
  shim: outputs inverted                       0       4
  shim: fits arguments swapped                 0       0
  shim: pop placement and size swapped         0       0
  shim: action rate masked to 32 bits          0       2
  shim: mix + 1                                0       4
  shim: fill_end arguments swapped             0       0
  shim: size of frames + 1                     0       4
  shim: stop asks frame + 1                    0       3

25 are killed. Tier 1 stays at rates up to 1e9 and below 2^32, so the
rate truncation and the per-host conversion fall to Tier 3; Tier 3
leaves out mix 31 (undefined in the C), so that one falls to Tier 1.
The three survivors are equivalent: the fit adds its arguments and the
pop adds placement and size, either way round, and the fill's end is
the larger of two frames.

soldr cargo test -p obs-data --test obs-tests-core -- source::audio_place
passes 32 of 32 (Tier 1 21, Tier 3 11).

Refs obs-rust#2461
rust/README.md gets the row for libobs/obs-source-audio-place.c.h
(obs-source::audio_place), in sorted place: what moved, what stays C
and why (every read at its point with the C's && and || around the
calls, both mutexes, the deques and every get_source_volume() call),
the characterized-not-endorsed behaviours, the two intentional
differences, the tests that see the swap and what Tier 3 covers. The
obs-source crate row, its Cargo description and the lib.rs crate docs
name the new module. docs/rust-port/naming.md already maps
libobs/obs-source*.c.h to rust/obs-source.

rust/obs-c-oracle/benches/port_comparison/source_audio_place.rs, in
the sorted mod and modules![...] lists, runs 1024 packets around an
audio timestamp at 48 kHz (no audio_ts yet, older, at it, a sample, a
block and seconds later, near MAX_BUF_SIZE; every monitoring type; push
back on and off) through the decisions of the last step of
source_output_audio_data() and of source_output_audio_place() with two
channels, then one action and its six mixes, on the shims and on the
oracle, and checks both checksums match. Release, x86_64 Linux, GCC, a
loaded host, median per 1024 packets: Rust 22.12 us, C 20.97 us
(1.05x), about 1 ns a packet.

The port changes nothing a user can see, so docs/features.md has no
line.

Fixes obs-rust#2461
@rdoupe
rdoupe requested a review from zackees as a code owner October 26, 2026 09:37
@zackees

zackees commented Oct 26, 2026

Copy link
Copy Markdown
Member

Coordinator review: a port in the same shape as #2448 and #2457. The audio placement and action-timeline decisions become pure functions with a C reference copy, a C-vs-Rust parity test, a bench and the export maps. A new cmocka test is added, and the only existing test file touched is the test-graph.json entry. Queuing; it merges when CI is green.

@zackees
zackees enabled auto-merge October 26, 2026 09:42
@zackees
zackees added this pull request to the merge queue Oct 26, 2026
Merged via the queue into obs-rust:master with commit 2fbf342 Oct 26, 2026
36 checks passed
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 source audio placement in obs-source.c to obs-source

2 participants