Skip to content

libobs: Apply scene item audio actions through a core on the item's fields - #1825

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:refactor/scene-audio-actions-seam
Oct 22, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:refactor/scene-audio-actions-seam

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Refs #1814 (PR 2 of the plan: the seam. The port follows.)

What and why

This is the behaviour-neutral seam before the Rust port of the scene item visibility audio ramp that #1815 moved into libobs/obs-scene-audio-actions.c.h. It does not add an accessor. As for the context names (#1788, which passes &obs->data.unnamed_index), the C caller passes the item's fields to a core function and keeps the lock and the reference drop around the call. So the port never mirrors struct obs_scene_item and never reaches a source. There is no Rust, no behaviour change and no export change.

  • The core: libobs/obs-scene-audio-actions.c.h keeps only the logic, declared in the new libobs/obs-scene-audio-actions.h:
    • size_t obs_scene_audio_actions_apply(obs_scene_item_action_array_t *actions, bool *visible, uint64_t ts, size_t sample_rate, float *buf) is the loop of apply_scene_item_audio_actions(), with the same statements. It consumes the due actions from the queue (da_erase in place), sets *visible, fills buf and returns the hide count. It takes no lock, calls nothing in libobs and keeps no state.
    • bool obs_scene_audio_action_due(uint64_t timestamp, uint64_t ts, size_t sample_rate) is the window of apply_scene_item_volume(): !ts || timestamp < ts + duration, with the duration rounded down to whole ns as before.
  • The boundary for the port: struct item_action moves from obs-scene.h into the new header, and the queue's darray gets a named typedef, obs_scene_item_action_array_t, which has the same layout. The field in struct obs_scene_item uses it. This follows the darray typedefs of the interleaver seam (libobs: Read the output interleaver fields through hidden accessors #1599). The pointer has a C type, and on the Rust side it is a *mut obs_util::ffi::darray, which the port can erase with the darray_erase ported in rust: Port the obs-output.c interleaver to obs-output #1594. The header includes only <stdbool.h>, <stddef.h>, <stdint.h> and util/darray.h, so the port's own ported group can use it as its .pch.h.
  • The item side moves to libobs/obs-scene-item-audio-actions.c.h, which stays C. It joins the same libobs.core-source unity group, and the hidden names and signatures do not change:
    • apply_scene_item_audio_actions() takes actions_mutex, calls the core with &item->audio_actions and &item->visible, and releases the lock. It then drops one active reference per hide that the core returned and calls obs_source_remove_active_child() at 0, as before.
    • apply_scene_item_volume() peeks the first action under the lock as before, releases it, and asks the core whether that action is due.
    • process_all_audio_actions() is unchanged.
  • One ordering change. The visibility that the buffer starts from is now read under actions_mutex. Before, it was read just before the lock was taken. Only set_visibility() writes it outside the ramp, and it does so under that lock, so the change only closes a race window. Every result on one thread is unchanged (see the equivalence check below).
  • Hidden: none of the five functions is EXPORT. The new two are local (t) symbols in all three libraries, and the exported set stays the same.
  • Build: the header goes into libobs.pch.h and the source-sets.json header list, as obs-scene-group-transform.h did. The group, the PCH layout and the CMake source lists stay the same apart from the one new member file. test_scene_audio_action_steps builds both files now, in CMake and in the obs-build manifest.

The pinned "characterized, not endorsed" behaviours of #1815 stay as they are: actions out of time order, the volume window rounded down to whole ns, more hides than references, and ts 0 applying every action. One more thing is now written down in the header: called with a buffer and ts 0, the fill is not bounded by the tick and writes past the 1024 gains. That is what the C has always done. libobs passes ts 0 only with a NULL buffer (process_all_audio_actions() and the preview-only video tick), and the core keeps it exactly so. The port has to do the same, along with not dividing by a zero rate in obs_scene_audio_action_due().

For the port (PR 3), the C to replace is now the one core file. The ported group drops obs-scene-audio-actions.c.h, and the Rust shim exports the two core functions. The item side stays C. Because test_scene_audio_action_steps compiles the item side and the core separately, the port can link it against the Rust core with the C item side unchanged.

RED → GREEN

  • RED commit: 1979c2c53 test: Call the scene audio action core from the steps test adds two cases to test_scene_audio_action_steps and does not build: fatal error: obs-scene-audio-actions.h: No such file or directory. That was the only failed target in the OBS_LIBOBS_BACKEND=C tree. The cases are:

    • core_applies_the_fields: the core on a queue and a bool of its own, with no item, lock or source. It gives the exact buffer, visibility and remaining queue, returns 2 hides, drops no reference and removes no child. It also covers a tick with nothing due, ts 0 with no buffer, and an empty queue.
    • core_action_due: the window at 6 rates, and ts 0.
  • GREEN: c01a60b0a libobs: Apply scene item audio actions through a core on the item's fields. Results against libobs (C backend), the hybrid libobs and libobs-c-reference:

    • test_scene_audio_action_steps passes 17/17: the 15 cases of libobs: Move the scene item visibility audio ramp into obs-scene-audio-actions.c #1815, unchanged, plus the 2 new ones. Only its CMake source list and its header comments changed.
    • test_scene_audio_actions passes 4/4, unchanged.
    • test_preview_only_hide (6/6), test_group_hide_transition_audio (4/4), test_group_active_refs (10/10) and test_item_transition_empty_source (4/4) pass, unchanged.
  • Equivalence: a differential harness (local, not committed) compiles the three functions from origin/master (obs-scene-audio-actions.c.h of libobs: Move the scene item visibility audio ramp into obs-scene-audio-actions.c #1815, renamed) next to the new item side and core. It runs both on the same random fake items: 2 000 000 trials over the three entry points, with:

    • action lists of 0 to 7, unsorted, at 0, near UINT64_MAX and on the tick boundaries;
    • ticks at 0, near 0 and near UINT64_MAX;
    • 13 rates: 0, 1, 8 kHz to 192 kHz, and 1 GHz;
    • a buffer only with a tick, as libobs passes one.

    The buffers, return values, visibility, references, removals and remaining queues were identical in every trial.

  • Mutation check of the seam: 26 of 27 mutants fail at least one of the two scene audio tests. Each mutant of the core or the item side was built into libobs (C backend) and both tests. The differential harness flags all 26 as well:

    • the core: a late action not clamped to the tick; frame 1024 counted as this tick; the ts 0 path breaking like a tick (both tests hang); a show counted as a hide; a span filled with the new visibility; the tail always 1.0; the erase skipping the next action; one frame late; the visibility before a change not updated; *visible not written back; the start not read from *visible; 0 hides returned; one hide too many returned;
    • the window: <= instead of <, no ts 0 case, a 1023-frame duration;
    • the item side: the child removed at 1, or at and below 0; one reference per call, not per hide; no reference dropped; the buffer not passed to the core; the wrong tick passed to the core; the last action peeked; false returned after applying; the window ignored; process_all_audio_actions() with a tick of 1.

    The survivor is the same one as in libobs: Move the scene item visibility audio ramp into obs-scene-audio-actions.c #1815: process_all_audio_actions() calling once instead of looping. It is equivalent, because with ts 0 one call consumes the whole queue. The harness also finds 0 differences for it. Missing locks are not something a single-threaded test can see, so no mutant for them was counted.

  • Local, two build trees: OBS_LIBOBS_BACKEND=C, and HYBRID with OBS_BUILD_C_REFERENCE=ON. Full ctest -j4, headless (private Xvfb with -noreset, private D-Bus, fresh HOME):

Lane Library ctest Exported symbols
OBS_LIBOBS_BACKEND=C libobs 373/373 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 375/375 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 375/375 IDENTICAL (1766)

Nothing was skipped. On the final base, the six scene audio tests, the symbol comparison and the differential harness were run again against all three libraries, and so was full ctest for each library. In one of those full runs, libobs-c-reference under -j4 failed test_scene_audio_actions in hide_only_item: the hide landed on sample 0 of its tick instead of the sample of the call. test_pulse_monitor_device failed in the same run. Running that ctest again passed 375/375. test_scene_audio_actions then passed 20 runs in a row on its own and 24 runs with 8 at a time, and every placement was exact. The seam does not change any result (see the equivalence check). The sample-0 result looks like the pinned "every action applied at once" path (process_all_audio_actions(), used when a tick has no audio from the scene). Under load, the tone's audio for that tick can arrive late, and then that path runs. It is #1815's test, so making it robust to this is a follow-up, not part of this PR.

  • Also run: uv run ci/local_gate.py --lane lint passed, and --lane cross passed. ci/test_build_groups.py, ci/test_ported_sources.py and ci/platform_ifdef_ratchet.py pass.
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change).

Windows/macOS: not built there. The new files have no host conditional, and the header is plain C99 with util/darray.h. test_scene_audio_action_steps is still built on every OS.

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: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports) (not applicable: no port here)

Two cases for the core that the next commit splits out of
apply_scene_item_audio_actions() and apply_scene_item_volume(), so the
Rust port can take the item's fields without mirroring struct
obs_scene_item:

- obs_scene_audio_actions_apply() on a queue and a visibility of its own,
  with no item, no lock and no source: the same buffer, visibility and
  remaining queue as the item path, and the hide count returned, with no
  reference dropped and no child removed; a tick with nothing due; ts 0
  with no buffer; an empty queue.
- obs_scene_audio_action_due(): the volume window at 6 rates, and ts 0.

The 15 existing cases are unchanged. This does not build yet: the core
and obs-scene-audio-actions.h do not exist.

Refs obs-rust#1814
…ields

The seam before the Rust port of the scene item visibility audio ramp:
the core it ports takes the item's fields from the caller, as
obs_context_name_dup() takes &obs->data.unnamed_index (obs-rust#1788), so Rust
never mirrors struct obs_scene_item and reaches no source.

libobs/obs-scene-audio-actions.c.h is now that core, declared in the new
libobs/obs-scene-audio-actions.h:
- obs_scene_audio_actions_apply(actions, visible, ts, sample_rate, buf)
  is the loop of apply_scene_item_audio_actions(): it consumes the due
  actions from the queue (da_erase in place), sets *visible, fills buf,
  and returns the hide count. It takes no lock and calls nothing in
  libobs.
- obs_scene_audio_action_due(timestamp, ts, sample_rate) is the window of
  apply_scene_item_volume().
struct item_action moves into that header, and the queue's darray gets a
named typedef, obs_scene_item_action_array_t (same layout), so the core
can take a pointer to it; Rust can take it as obs_util::ffi::darray.

The three item functions move to libobs/obs-scene-item-audio-actions.c.h,
in the same unity group, under the same hidden names and signatures.
apply_scene_item_audio_actions() takes actions_mutex, calls the core with
&item->audio_actions and &item->visible, releases it, and drops one active
reference per hide it returns, removing the active child at 0, as before.
apply_scene_item_volume() peeks the first action under the lock as before
and asks the core whether it is due. process_all_audio_actions() is
unchanged.

The one ordering change: the visibility the buffer starts from is read
under actions_mutex, where it was read just before it. set_visibility()
writes it under that lock, so this only closes a race window.

test_scene_audio_action_steps now builds both files; its 15 cases are
unchanged and the two core cases pass. It is in the obs-build manifest
with both sources. The header joins libobs.pch.h and source-sets.json.
No exported symbol changes.

Refs obs-rust#1814
@rdoupe
rdoupe requested a review from zackees as a code owner October 22, 2026 20:56
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
Merged via the queue into obs-rust:master with commit 6e84f3d Oct 22, 2026
30 checks passed
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 23, 2026
Ports libobs/obs-scene-audio-actions.c.h, the core of the scene item
visibility audio ramp (obs-rust#1825), to the audio_actions module of
rust/obs-scene. The item side stays C in obs-scene-item-audio-actions.c.h:
actions_mutex, the active reference dropped per hide and
obs_source_remove_active_child(), apply_scene_item_volume() and
process_all_audio_actions(). The hybrid libobs drops the C core and links
the Rust shims in its place.

Safe core (src/audio_actions.rs): apply() reads the queue in order, places
each action on frame floor((timestamp - ts) * rate / 1e9) through
util_mul_div64 as the C computes it on each host, a late one on frame 0, stops at the first one on
frame 1024 or later (with ts 0 it takes them all), fills the gain buffer
with the visibility before each change and the rest of the tick with the
last one, and returns the actions consumed (a prefix), the visibility and
the hide count. action_due() is the window, ts plus 1024 frames in ns
rounded down, with the C's wrapping sum. It keeps no state, takes no lock,
and denies clippy's panic, indexing and arithmetic lints.

obs-scene does not depend on obs-util: util_mul_div64 and the darray
erase are written out in this crate. test_scene_bounds_place_rust links
the staticlib for obs-scene alone, and the obs-util objects bring in that
crate's system libraries (libuuid, GIO, winmm and the base log handler on
Windows), which that test does not link.

Shim (src/ffi/scene_audio_actions.rs): obs_scene_audio_actions_apply()
and obs_scene_audio_action_due() keep the C signatures. The queue is the
item's obs_scene_item_action_array_t, mirrored with the layout of struct
darray and read in place. The consumed actions are erased from the front
one at a time, each as the C's da_erase(*actions, i--) with i 0 does (num
minus one, then a memmove of the rest down by one item), so the memory
left in the array, stale items included, is the C's. The erase allocates
and frees nothing. *visible is written only when an action was consumed.

Kept, characterized, not endorsed (obs-rust#1815): actions out of time order are
left out of the buffer but counted; the loop and the window stop at the
first action that is not due; the window rounds the tick down to whole ns
(44.1 kHz); every hide is counted, without bound; with ts 0 every action
is applied.

Intentional differences, where C has undefined behaviour and libobs never
gets there: with ts 0 and a buffer, the C writes past the 1024 gains, and
the Rust writes the same gains inside them and none past them;
obs_scene_audio_action_due() with a rate of 0 (the C divides by 0) returns
true, as apply() puts every action on frame 0 at rate 0; a null queue or
visibility does nothing and returns 0; on MSVC x64, where the C's
128-bit util_mul_div64 faults (a rate above 1 GHz), the core saturates.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-scene-audio-actions (LIBOBS_PORTED_SOURCES), whose PCH is
obs-scene-audio-actions.h with media-io/audio-io.h and util/util_uint64.h.
The two functions are not EXPORT, so the shims are hidden: rust-hidden.txt,
the local: list of rust-exports.map and rust-unexports-macos.txt. The
exported symbol sets are unchanged (1766). test_scene_audio_action_steps
still compiles the C core into itself, unchanged; test_scene_audio_actions,
test_preview_only_hide, test_group_hide_transition_audio,
test_group_active_refs and test_item_transition_empty_source see the swap
through the real libobs. ci/test_ported_sources.py checks the group on
every host.

Tier 1 scene::audio_actions passes 23/23.

Refs obs-rust#1814
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 23, 2026
Ports libobs/obs-scene-audio-actions.c.h, the core of the scene item
visibility audio ramp (obs-rust#1825), to the audio_actions module of
rust/obs-scene. The item side stays C in obs-scene-item-audio-actions.c.h:
actions_mutex, the active reference dropped per hide and
obs_source_remove_active_child(), apply_scene_item_volume() and
process_all_audio_actions(). The hybrid libobs drops the C core and links
the Rust shims in its place.

Safe core (src/audio_actions.rs): apply() reads the queue in order, places
each action on frame floor((timestamp - ts) * rate / 1e9) through
util_mul_div64 as the C computes it on each host, a late one on frame 0, stops at the first one on
frame 1024 or later (with ts 0 it takes them all), fills the gain buffer
with the visibility before each change and the rest of the tick with the
last one, and returns the actions consumed (a prefix), the visibility and
the hide count. action_due() is the window, ts plus 1024 frames in ns
rounded down, with the C's wrapping sum. It keeps no state, takes no lock,
and denies clippy's panic, indexing and arithmetic lints.

obs-scene does not depend on obs-util: util_mul_div64 and the darray
erase are written out in this crate. test_scene_bounds_place_rust links
the staticlib for obs-scene alone, and the obs-util objects bring in that
crate's system libraries (libuuid, GIO, winmm and the base log handler on
Windows), which that test does not link.

Shim (src/ffi/scene_audio_actions.rs): obs_scene_audio_actions_apply()
and obs_scene_audio_action_due() keep the C signatures. The queue is the
item's obs_scene_item_action_array_t, mirrored with the layout of struct
darray and read in place. The consumed actions are erased from the front
one at a time, each as the C's da_erase(*actions, i--) with i 0 does (num
minus one, then a memmove of the rest down by one item), so the memory
left in the array, stale items included, is the C's. The erase allocates
and frees nothing. *visible is written only when an action was consumed.

Kept, characterized, not endorsed (obs-rust#1815): actions out of time order are
left out of the buffer but counted; the loop and the window stop at the
first action that is not due; the window rounds the tick down to whole ns
(44.1 kHz); every hide is counted, without bound; with ts 0 every action
is applied.

Intentional differences, where C has undefined behaviour and libobs never
gets there: with ts 0 and a buffer, the C writes past the 1024 gains, and
the Rust writes the same gains inside them and none past them;
obs_scene_audio_action_due() with a rate of 0 (the C divides by 0) returns
true, as apply() puts every action on frame 0 at rate 0; a null queue or
visibility does nothing and returns 0; on MSVC x64, where the C's
128-bit util_mul_div64 faults (a rate above 1 GHz), the core saturates.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-scene-audio-actions (LIBOBS_PORTED_SOURCES), whose PCH is
obs-scene-audio-actions.h with media-io/audio-io.h and util/util_uint64.h.
The two functions are not EXPORT, so the shims are hidden: rust-hidden.txt,
the local: list of rust-exports.map and rust-unexports-macos.txt. The
exported symbol sets are unchanged (1766). test_scene_audio_action_steps
still compiles the C core into itself, unchanged; test_scene_audio_actions,
test_preview_only_hide, test_group_hide_transition_audio,
test_group_active_refs and test_item_transition_empty_source see the swap
through the real libobs. ci/test_ported_sources.py checks the group on
every host.

Tier 1 scene::audio_actions passes 23/23.

Refs obs-rust#1814
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 23, 2026
* obs-scene: Add Tier 1 tests for the scene audio action ramp port

RED for the port of libobs/obs-scene-audio-actions.c.h, the core of the
scene item visibility audio ramp, to a new module, audio_actions, of
rust/obs-scene. The safe core is a stub (todo!()): ItemAction (the
queued visibility and time), Applied (the actions consumed, the
visibility after them and the hides among them), frame() (the frame of
the tick an action falls on), apply() (obs_scene_audio_actions_apply(),
over the queue in order, with an optional gain buffer), tick_duration()
and action_due() (obs_scene_audio_action_due()).

tests/scene/audio_actions.rs ports test_scene_audio_action_steps 1:1,
under the same case names (steps_*), in the same order, with the same
steps and expectations: all 17 cases. A small model repeats the C item
side around the core: the queue, the visibility, the active references
and the child removal at 0, apply_scene_item_volume() and
process_all_audio_actions(). The buffer has one gain past the tick,
which no case may write.

New edge cases (edge_*): the frame helper (late, exact, one ns before,
ts 0, rate 0, u64::MAX), the window wrapping at the end of the u64
range, the core reading no further than the first action past the tick,
the exact 1.0 and 0.0 gains, and the two intentional differences: with
ts 0 and a buffer the fill stops at the buffer end (the C writes past
it), and a rate of 0 makes every action due (the C divides by 0).

soldr cargo test -p obs-data --test obs-tests-core scene::audio_actions
fails 22 of 23 with "not yet implemented". The one that passes,
steps_volume_without_actions, never reaches the core.

Refs obs-rust#1814

* obs-scene: Port the scene audio action ramp core

Ports libobs/obs-scene-audio-actions.c.h, the core of the scene item
visibility audio ramp (obs-rust#1825), to the audio_actions module of
rust/obs-scene. The item side stays C in obs-scene-item-audio-actions.c.h:
actions_mutex, the active reference dropped per hide and
obs_source_remove_active_child(), apply_scene_item_volume() and
process_all_audio_actions(). The hybrid libobs drops the C core and links
the Rust shims in its place.

Safe core (src/audio_actions.rs): apply() reads the queue in order, places
each action on frame floor((timestamp - ts) * rate / 1e9) through
util_mul_div64 as the C computes it on each host, a late one on frame 0, stops at the first one on
frame 1024 or later (with ts 0 it takes them all), fills the gain buffer
with the visibility before each change and the rest of the tick with the
last one, and returns the actions consumed (a prefix), the visibility and
the hide count. action_due() is the window, ts plus 1024 frames in ns
rounded down, with the C's wrapping sum. It keeps no state, takes no lock,
and denies clippy's panic, indexing and arithmetic lints.

obs-scene does not depend on obs-util: util_mul_div64 and the darray
erase are written out in this crate. test_scene_bounds_place_rust links
the staticlib for obs-scene alone, and the obs-util objects bring in that
crate's system libraries (libuuid, GIO, winmm and the base log handler on
Windows), which that test does not link.

Shim (src/ffi/scene_audio_actions.rs): obs_scene_audio_actions_apply()
and obs_scene_audio_action_due() keep the C signatures. The queue is the
item's obs_scene_item_action_array_t, mirrored with the layout of struct
darray and read in place. The consumed actions are erased from the front
one at a time, each as the C's da_erase(*actions, i--) with i 0 does (num
minus one, then a memmove of the rest down by one item), so the memory
left in the array, stale items included, is the C's. The erase allocates
and frees nothing. *visible is written only when an action was consumed.

Kept, characterized, not endorsed (obs-rust#1815): actions out of time order are
left out of the buffer but counted; the loop and the window stop at the
first action that is not due; the window rounds the tick down to whole ns
(44.1 kHz); every hide is counted, without bound; with ts 0 every action
is applied.

Intentional differences, where C has undefined behaviour and libobs never
gets there: with ts 0 and a buffer, the C writes past the 1024 gains, and
the Rust writes the same gains inside them and none past them;
obs_scene_audio_action_due() with a rate of 0 (the C divides by 0) returns
true, as apply() puts every action on frame 0 at rate 0; a null queue or
visibility does nothing and returns 0; on MSVC x64, where the C's
128-bit util_mul_div64 faults (a rate above 1 GHz), the core saturates.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-scene-audio-actions (LIBOBS_PORTED_SOURCES), whose PCH is
obs-scene-audio-actions.h with media-io/audio-io.h and util/util_uint64.h.
The two functions are not EXPORT, so the shims are hidden: rust-hidden.txt,
the local: list of rust-exports.map and rust-unexports-macos.txt. The
exported symbol sets are unchanged (1766). test_scene_audio_action_steps
still compiles the C core into itself, unchanged; test_scene_audio_actions,
test_preview_only_hide, test_group_hide_transition_audio,
test_group_active_refs and test_item_transition_empty_source see the swap
through the real libobs. ci/test_ported_sources.py checks the group on
every host.

Tier 1 scene::audio_actions passes 23/23.

Refs obs-rust#1814

* obs-scene: Check the scene audio action ramp port against the C

Oracle (oracle/scene_audio_actions.c): the original
obs-scene-audio-actions.c.h, unmodified, with its two functions renamed
to oracle_*, plus the layout of struct item_action, of
obs_scene_item_action_array_t and AUDIO_OUTPUT_FRAMES. da_erase and
util_mul_div64 stay the header inlines, so the oracle erases with the C's
darray code and the shim with the Rust port.

Tier 3 (tests/scene/audio_actions_parity.rs): each run gives both sides
the same queue (the same bytes in the same capacity, padding and spare
slots included), visibility, tick, rate and gain buffer, and compares the
hide count, the visibility, the darray (array not moved, num, capacity
and every byte of the capacity), the gains bit for bit with a guard gain
past the tick, and the window for every action's time. Runs:
- the layout;
- every queue of up to 3 actions over 8 chosen times (before the tick,
  frames 0, 1, 511, the last ns of 1023, 1024 and 2000, and the first
  time where since * rate wraps a u64), with each start visibility, with
  and without a buffer, at 48 and 44.1 kHz, on a tick and with ts 0:
  69 904 runs;
- 20 000 random queues of up to 40 actions in any time order, at 0, near
  the tick, near u64::MAX and at the wrap points, ticks at 0, near 0,
  anywhere and near u64::MAX, and rates from 8000 to 192000 plus 0, 1,
  odd ones, 1 GHz and (not on MSVC) rates where util_mul_div64 wraps;
- 20 000 random windows, at the edge of the window too.

Excluded, the intentional differences: with ts 0 and a buffer the C
writes past the tick, so the oracle gets a buffer large enough for its
writes or, past 64 Ki gains, runs once with no buffer (the state) and
once on the queue up to its first action past the tick (the gains, which
are what the C writes inside the tick); the Rust side may not write the
guard. A rate of 0 in the window and a null queue or visibility are
checked on the Rust side only.

Mutation check (done once, not committed): 27 of 29 mutants fail Tier 3
or Tier 1. Core: a late action not clamped; frame 1024 counted as this
tick; ts 0 breaking like a tick (steps_process_all then loops, the rest
fail); a show counted as a hide; a span filled with the new visibility;
the tail always 1.0; one frame late; a one-step product instead of
util_mul_div64 (caught by the wrap-point times); util_mul_div64 as exact
128-bit math on every host; the visibility before a change not updated;
frame_num not advanced; two hides per hide; <= in the window; no ts 0
case in the window; a 1023-frame window; a saturating window sum; rate 0
not due. Shim: no move on erase; one move of the rest by the consumed
count (same items, different bytes left); one action too few erased; the
visibility not written back; the start not read from *visible; the
buffer ignored; the rate ignored; 0 hides returned; the window's
arguments swapped; the queue read backwards. The two survivors are
equivalent: a fill that writes one frame more (the next fill or the tail
always rewrites that frame), and the erase without its num 0 check (the
move is then of 0 items).

Refs obs-rust#1814

* docs: Record the scene audio action ramp port

The rust/README.md rows for rust/obs-scene (crate table and port table)
now include libobs/obs-scene-audio-actions.c.h. The port row lists what
stays C, how the shim reaches the item's queue, what the characterization
pins and the port keeps, the intentional differences (ts 0 with a buffer
stops at the buffer end; a rate of 0 makes every action due; a null queue
or visibility does nothing), the MSVC overflow above 1 GHz, what Tier 3
compares, and why there is no _rust variant of the steps test. The
naming.md row (libobs/obs-scene*.c.h) already covers the file.

Fixes obs-rust#1814
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 23, 2026
* test: Call the audio tick core from the subsample test

Two cases for the core that the next commit splits out of mix_audio(),
set_fixed_audio_buffering() and add_audio_buffering(), so the Rust port
can take plain values without mirroring struct obs_source or struct
obs_core_audio:

- obs_audio_mix_add() on plain buffers and an audio_ts of its own, with
  no source: the same mix as the source path from frame 511 (frame 512's
  time at 48 kHz), and nothing at the end of the tick.
  obs_audio_mix_time_to_frames() rounds down as convert_time_to_frames().
- obs_audio_buffering_fixed() and obs_audio_buffering_add() on standalone
  fields, with no struct obs_core_audio: nothing at the maximum (added
  false); fixed from 0 adds 45 ticks (960 ms at 48 kHz); 5 ticks late
  with room for 2 is cut at the maximum (max_reached, 42 ms); one sample
  late at 44.1 kHz is one tick (23 ms); a sub-sample delay adds 0 ticks
  and 0 ms and leaves ts. The ms values are the ones obs-audio.c logs.

The 16 existing cases are unchanged. This does not build yet: the core
and obs-audio-mix.h do not exist.

Refs obs-rust#1903

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

* libobs: Run the audio tick arithmetic through a core on plain values

The seam before the Rust port of the audio tick arithmetic (obs-rust#1903): the
core it ports takes the source's and the core's fields from the caller,
as obs_scene_audio_actions_apply() takes the item's fields (obs-rust#1825), so
Rust never mirrors struct obs_source or struct obs_core_audio.

libobs/obs-audio-mix.c.h is now that core, declared in the new
libobs/obs-audio-mix.h:
- obs_audio_mix_time_to_frames(sample_rate, t): convert_time_to_frames();
- obs_audio_mix_add(mixes, buf, audio_ts, ts, channels, sample_rate): the
  window and the add loop of mix_audio(), on the source's
  audio_output_buf and audio_ts;
- obs_audio_buffering_maxed(), obs_audio_buffering_fixed() and
  obs_audio_buffering_add(): the buffering steps on struct
  obs_audio_buffering, the buffered_ts, buffered_timestamps,
  buffering_wait_ticks and total_buffering_ticks fields of struct
  obs_core_audio by pointer, and max_buffering_ticks. They push the new
  windows to the deque in place and return struct
  obs_audio_buffering_step (added, max_reached, ticks, ms, total_ms).
struct ts_info moves into that header. The core takes no lock, logs
nothing and calls nothing in libobs.

convert_time_to_frames(), mix_audio(), audio_buffering_maxed(),
set_fixed_audio_buffering() and add_audio_buffering() go back into
obs-audio.c.h as static wrappers with their old signatures: they pass
the fields and log the step. The log lines and their values are
unchanged; "Max audio buffering reached!" and the info line now come
after the windows are pushed, not before. The per-tick DEBUG_AUDIO lines
(compiled out) become one "new buffered ts" line. The declarations that
the move added to obs-internal.h go.

The one behaviour difference is in a state libobs does not reach: with
a total above the maximum, the fixed path looped on a negative tick
count and left ts->end uninitialized; the core starts from *ts.

The header joins libobs.pch.h and source-sets.json. The core stays in
libobs.core-output. No exported symbol changes (1766); the core
functions are local symbols.

test_audio_buffering_subsample builds unchanged and passes 18/18 on
libobs and libobs-c-reference; test_audio_clock_drift (6),
test_audio_sync_offset_change (5) and test_audio_capture_mix_race (2)
pass on both.

Refs obs-rust#1903

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

* obs-core: Add Tier 1 tests for the audio tick arithmetic port

RED for the port of libobs/obs-audio-mix.c.h, the arithmetic of the audio
tick (obs-rust#1903), to a new module, audio_mix, of rust/obs-core. The safe core
is a stub (todo!()): TsInfo (struct ts_info), Buffering (the buffering
fields of struct obs_core_audio by value), Step (struct
obs_audio_buffering_step), time_to_frames(), mix_start() and mix_into()
(the window and the add loop of obs_audio_mix_add() for one mix and
channel), buffering_maxed(), buffering_fixed() and buffering_add(), which
push their windows through a callback.

tests/obs_core/audio_mix.rs ports test_audio_buffering_subsample 1:1,
under the same case names, in the same order, with the same inputs and
expectations: all 18 cases. struct obs_core_audio is a Buffering plus a
Vec queue whose front is index 0; mix_all() is the loop of
obs_audio_mix_add() over the mixes and channels. Samples compare bit for
bit.

New edge cases (edge_*): audio after frame 1024 of a two-tick window, the
add loop on short slices, a rate of 0, a total above the maximum,
timestamps that wrap, and a proptest that no input panics and that no
call pushes more than max - total windows (a sum that overflows int must
still be cut at the maximum).

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

Refs obs-rust#1903

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

* obs-core: Port the audio tick arithmetic

Ports libobs/obs-audio-mix.c.h, the arithmetic of the audio tick on plain
values (obs-rust#1903), to the audio_mix module of rust/obs-core. The source and
core side stays C in obs-audio.c.h: the source walk, the locks, the
logging, and the static wrappers that pass the source's audio_output_buf
and audio_ts and the buffering fields of struct obs_core_audio. The
hybrid libobs drops the C core and links the Rust shims in its place.

Safe core (src/audio_mix.rs):
- time_to_frames(): floor(t * rate / 1e9) through util_mul_div64 as the
  C computes it on each host (written out here, saturating on MSVC x64
  where the C faults, 0 for a divisor of 0);
- mix_start(): the frame the source's audio starts on, None outside the
  tick or from frame 1024 on; mix_into(): the add loop of one mix and
  channel over slices, element by element in the C's order, so every sum
  is bit for bit the C's;
- buffering_maxed(), buffering_fixed() and buffering_add(): the ticks,
  the cut at the maximum, the windows (pushed through a callback, each
  one tick before the last) and the step with the ms values obs-audio.c
  logs. The int and uint64_t arithmetic is the C's: wrapping timestamps,
  the (int) truncation of the tick count, the int-to-size_t conversion of
  the ms values.
It keeps no state, takes no lock, and denies clippy's panic, indexing
and arithmetic lints.

Shim (src/ffi/audio_mix.rs): the five functions keep the C signatures.
struct ts_info, struct audio_output_data, struct obs_audio_buffering and
struct obs_audio_buffering_step are mirrored #[repr(C)]. The buffering
shims read the fields through their pointers, push each window to the
front of the C struct deque in place through obs_util::ffi::deque (bmem
memory, so obs.c frees it with deque_free as before), and write the
fields back (obs-core already depends on obs-util).

Kept, characterized, not endorsed (obs-rust#1905): times round down twice, so a
frame's own time can map to the frame before it; the maximum is equality
only; a sub-sample delay is an added step of 0 ticks and 0 ms.

Intentional differences, where C has undefined behaviour and libobs never
gets there: audio past frame 1024 of a window longer than one tick adds
nothing (the C makes an out-of-range pointer); a total above the maximum,
or a tick count whose sum with the total overflows int, pushes at most
max - total windows (the C loops); a rate of 0 gives 0 frames, windows
and ms (the C divides by 0); null pointers do nothing; channels above
MAX_AUDIO_CHANNELS is cut to it; on MSVC x64 an overflowing quotient
saturates (the C faults).

C side: the file leaves libobs.core-output for its own group,
libobs.ported-audio-mix (LIBOBS_PORTED_SOURCES), whose PCH is
obs-audio-mix.h with media-io/audio-io.h and util/util_uint64.h. The five
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_mix passes 24/24.

Refs obs-rust#1903

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

* obs-core: Check the audio tick arithmetic port against the C

Oracle (oracle/audio_mix.c): the original obs-audio-mix.c.h, unmodified,
with its five functions renamed to oracle_*, plus the layout of struct
ts_info, struct audio_output_data, struct obs_audio_buffering, struct
obs_audio_buffering_step and struct deque, and AUDIO_OUTPUT_FRAMES,
MAX_AUDIO_MIXES, MAX_AUDIO_CHANNELS and sizeof(int). util_mul_div64, the
audio-io.h time helpers and deque_push_front stay the header inlines, so
the oracle pushes with the C's deque code and the shim with the Rust
port, into the same bmem allocator.

Tier 3 (tests/obs_core/audio_mix_parity.rs): both sides get the same
inputs through their C signatures and are compared:
- the layout;
- obs_audio_mix_add on every start frame at 44.1 and 48 kHz, on the
  frame's time and 1 ns either side, and on 4096 random cases (windows of
  one and two ticks, audio before, on, inside, at the end of and after
  them, 0 to 8 channels, rates from 8 kHz to 400 kHz, start times near 0
  and near u64::MAX): every sample of the 6 mixes and 8 channels, bit for
  bit, from the same random start values and source audio;
- obs_audio_mix_time_to_frames on 20 000 random rates and times,
  including rates where util_mul_div64 wraps (not on MSVC x64);
- obs_audio_buffering_maxed, _fixed and _add on 210 chosen and 20 000
  random cases (totals up to the maximum, waiting ticks, delays from 0 ns
  to beyond the maximum and huge, timestamps near 0 and u64::MAX, 0 to 4
  queued windows): the step, the three fields, ts, the struct deque fields
  (size, start, end, capacity) and every window in the queue.

Excluded, the intentional differences: audio past frame 1024 of a longer
window (only the shim runs; the mix must stay as it was), a delay whose
int tick count is negative or overflows the total (only the shim runs;
it must push at most max - total windows), a rate of 0 in the buffering
steps and null pointers (Tier 1).

port_comparison (benches/port_comparison/audio.rs): a stereo mix of a
whole tick and of a tick from frame 300 into all 6 mixes, time to frames,
and one added buffering tick on a queue popped back after each call.

Mutation check (done once, not committed): 20 of 21 mutants fail Tier 1
or Tier 3. Core: the window end inclusive; = instead of +=; the start
frame ignored; ticks rounded down; the cut only above the maximum;
max_reached not set; one window too many; ms from the total; buffered_ts
always reset; windows from the next tick; maxed as >=; exact 128-bit
util_mul_div64; the ms as an unsigned int; fixed keeping the old total.
Shim: total, waiting ticks or ts not written back; channels ignored or
one short; windows pushed to the back. The survivor, frame 1024 mixed, is
equivalent: a start of 1024 leaves no frame to add.

obs_core::audio_mix and audio_mix_parity pass 30/30 on macOS arm64 and
Linux x86_64 (all of obs_core: 86/86).

port_comparison on Linux x86_64 (CT104, pinned to one core, criterion
median, Rust / C):
  audio_mix/mix_whole_tick           1.051 us / 1.089 us
  audio_mix/mix_from_frame_300       700.4 ns / 759.7 ns
  audio_mix/time_to_frames           1.827 ns / 1.700 ns
  audio_mix/buffering_add_one_tick   30.74 ns / 25.70 ns
The mix runs every tick for every root source; the buffering step only
when a source falls behind.

Refs obs-rust#1903

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

2 participants