Skip to content

obs-scene: Port the scene audio action ramp core - #1842

Merged
zackees merged 4 commits into
obs-rust:masterfrom
rdoupe:port/scene-audio-actions
Oct 23, 2026
Merged

zackees merged 4 commits into
obs-rust:masterfrom
rdoupe:port/scene-audio-actions

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1814 (PR 3 of 3; #1815 moved the scene item visibility audio ramp into libobs/obs-scene-audio-actions.c.h and added test_scene_audio_actions and test_scene_audio_action_steps; #1825 split it into a core on the item's fields and the C item side)

What and why

This PR ports libobs/obs-scene-audio-actions.c.h, the core of the scene item visibility audio ramp, to a new audio_actions module of rust/obs-scene (#444, phase 6). When an item whose source has audio is shown or hidden, obs_sceneitem_set_visible() queues an action with the time of the call. The audio thread applies it on the sample of the scene's audio tick that the time falls on, through a 0/1 gain buffer, and drops one active reference per hide. The frontend, obs-websocket (SetSceneItemEnabled), scripts, hotkeys and scene item transitions all reach it, but only as a bool and a time that libobs takes itself.

The item side stays C in obs-scene-item-audio-actions.c.h: actions_mutex, the active reference dropped per hide, obs_source_remove_active_child(), apply_scene_item_volume() and process_all_audio_actions(). So does the mixing in obs-scene.c.h. The hybrid libobs drops the C core and links the Rust shims in its place.

  • Safe core (src/audio_actions.rs):
    • apply(actions, visible, ts, rate, buf) -> Applied { consumed, visible, hides }.
      • It reads the queue in order and places each action on frame floor((timestamp - ts) * rate / 1e9), through util_mul_div64 as the C computes it on each host. A late action goes on frame 0.
      • It stops at the first action on frame 1024 or later. With ts 0 it takes them all.
      • It fills the buffer with the visibility before each change, and the rest of the tick with the last one.
      • What it consumed is always a prefix of the queue.
    • action_due(timestamp, ts, rate) is the window: ts plus 1024 frames in ns, rounded down, with the C's wrapping sum.
    • The one host cfg, which util_mul_div64 formula the C compiles (MSVC x64's 128-bit one or the wrapping one), is in src/platform/, as the lint requires.
    • It keeps no state and takes no lock. It denies clippy's arithmetic_side_effects, indexing_slicing, unwrap_used, expect_used, panic, todo, unimplemented and unreachable. No unsafe.
  • Shim (src/ffi/scene_audio_actions.rs): obs_scene_audio_actions_apply() and obs_scene_audio_action_due(), with 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. struct item_action is mirrored too, and the layout test checks both against the C.
    • The erase removes the consumed actions from the front one at a time, each as the C's da_erase(*actions, i--) with i 0 does (darray_erase() in util/darray.h: num minus one, then a memmove of the rest down by one item). So the memory left in the array is the C's byte for byte, the stale bytes past num included. A single move of the rest by the consumed count would leave the same items but different bytes, and the mutation check catches it. The erase allocates and frees nothing.
    • No obs-util dependency. obs-scene does not depend on obs-util: util_mul_div64 and the darray erase are written out in the crate. test_scene_bounds_place_rust links libobs_rust-static for obs-scene alone, and the obs-util objects bring in that crate's system libraries (the first push of this PR used obs-util, and that test then failed to link on Windows: timeBeginPeriod, UuidCreate, the base log handler). On Linux, nm -u on the linked test shows no obs-util, uuid or GIO symbol, and no obs-scene object in the staticlib references obs-util.
    • *visible is written only when an action was consumed, as in the C.
    • Threads. The C caller holds actions_mutex around the call, on the audio thread, or on the video thread for a preview-only scene. The shims take no lock and call nothing in libobs.
  • C side:
    • obs-scene-audio-actions.c.h leaves libobs.core-source for its own group, libobs.ported-scene-audio-actions, listed in source-sets.json (LIBOBS_PORTED_SOURCES). Its PCH, libobs-ported-scene-audio-actions.pch.h, is obs-scene-audio-actions.h with media-io/audio-io.h and util/util_uint64.h. The only change to the file is that include and its header comment. The block of libobs.pch.h that names it now names the item side, the group member that still uses the header.
    • test_scene_audio_action_steps still compiles the C core into itself, unchanged.
    • ci/test_ported_sources.py checks the group on Linux, Windows and macOS, and that the item side stays in production.
  • Symbols: the two functions are declared in obs-scene-audio-actions.h without EXPORT, so the shims are hidden: rust-hidden.txt, the local: list of rust-exports.map, and rust-unexports-macos.txt. nm shows them as local (t) in all three libraries. The exported symbol sets are unchanged (1766).
  • Oracle (oracle/scene_audio_actions.c): the original file, unmodified, with the 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 runs the C's own darray code and arithmetic.
  • Kept, characterized, not endorsed (libobs: Move the scene item visibility audio ramp into obs-scene-audio-actions.c #1815, libobs: Apply scene item audio actions through a core on the item's fields #1825):
    • Actions out of time order are left out of the buffer but still counted. The loop and the window stop at the first action that is not due, even when a later one is.
    • The window rounds the tick down to whole ns. At 44.1 kHz an action on the last ns of frame 1023 waits for the next tick.
    • Every hide is counted, without bound.
    • With ts 0 (no tick) every action is applied.
  • Intentional differences (listed in the parity test header, the core, the shim and the rust/README.md row). They are all where C has undefined behaviour, and libobs never gets there:
    • ts 0 with a buffer. The C fill is not bounded by the tick and writes past the 1024 gains. The Rust makes the same writes inside the buffer and none past it. libobs passes ts 0 only with a NULL buffer.
    • Rate 0 in obs_scene_audio_action_due(). The C divides by 0. The Rust returns true, because apply() puts every action on frame 0 at rate 0. libobs calls it only with the audio rate. For every rate other than 0, the result is the C's.
    • A null queue or visibility does nothing and returns 0. The C dereferences it.
    • MSVC x64 overflow. There util_mul_div64 is 128-bit math that faults when the quotient does not fit in 64 bits, which needs a rate above 1 GHz. The Rust saturates.
  • Docs: the rust/README.md rows and the crate's Cargo description. The docs/rust-port/naming.md row (libobs/obs-scene*.c.h) already covers the file. No docs/features.md line: there is no user-visible change.

test_scene_audio_actions, test_preview_only_hide, test_group_hide_transition_audio, test_group_active_refs and test_item_transition_empty_source link OBS::libobs. In the hybrid libobs they run the Rust core through the real scene, on the audio thread and the preview-only video tick.

There is no _rust variant of test_scene_audio_action_steps linked against libobs_rust-static, as test_scene_bounds_place_rust has. I tried it while the shim still used obs-util, and it did not link (libuuid, then GIO). It is not part of this port, so Tier 3 is the direct comparison on every OS.

RED → GREEN

  • RED commit: 887ca6f8f obs-scene: Add Tier 1 tests for the scene audio action ramp port. The core is todo!(). 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.
    • Tier 1 (tests/scene/audio_actions.rs) ports test_scene_audio_action_steps 1:1: all 17 cases (steps_*), under the same names, in the same order, with the same steps and expectations. 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.
    • 6 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;
      • the two intentional differences: the ts 0 fill stops at the buffer end, and a rate of 0 makes every action due.
    • The tests are a module of the existing core category binary, obs-tests-core. No new test binary.
  • GREEN: 77f7672da obs-scene: Port the scene audio action ramp core. scene::audio_actions passes 23/23.
  • Tier 3: 2563d278a obs-scene: Check the scene audio action ramp port against the C (tests/scene/audio_actions_parity.rs).
    • What is compared. Both sides get the same queue (the same bytes in the same capacity, padding and spare slots included), visibility, tick, rate and gain buffer. The test compares:
      • the hide count and 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;
      • the window, for every action's time.
    • Runs:
      • the layout;
      • every queue of up to 3 actions over 8 chosen times, 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. The times are 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;
      • 20 000 random queues of up to 40 actions in any time order:
        • times at 0, near the tick, near u64::MAX and at the wrap points;
        • ticks at 0, near 0, anywhere and near u64::MAX;
        • 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 only. With ts 0 and a buffer the C writes past the tick. So the oracle gets a buffer large enough for its writes. Past 64 Ki gains, it runs twice instead: once with no buffer (the state), and once on the queue up to its first action past the tick (the gains, which are exactly 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 (run again on the final code).
      • The core: a late action not clamped; frame 1024 counted as this tick; ts 0 breaking like a tick (steps_process_all then loops forever, and 3 Tier 1 cases and both queue parity tests 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; util_mul_div64 as exact 128-bit math on every host; the visibility before a change not updated; the frame 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.
      • The shim: no move on erase; one move of the rest by the consumed count (the 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 first run of the one-step product mutant passed Tier 3 (Tier 1 caught it): random times almost never land where the wrapped product falls back into the tick. The wrap-point times above were added for it, and Tier 3 then fails it too.
      • The two survivors are equivalent. A fill that writes one frame more: the next fill or the tail fill always rewrites that frame. The erase without its num 0 check: the move is then of 0 items.
  • Local, on the final base (frontend: Say why a VA-API or QSV encoder from a profile is missing #1841): the HYBRID tree with OBS_BUILD_C_REFERENCE=ON. Full ctest -j4, headless (private Xvfb with -noreset, private D-Bus, fresh HOME):
Lane Library ctest Exported symbols
HYBRID, OBS_BUILD_C_REFERENCE=ON hybrid libobs 381/381 IDENTICAL (1766)
HYBRID, OBS_BUILD_C_REFERENCE=ON libobs-c-reference 381/381 IDENTICAL (1766)
  • Nothing was skipped. nm shows both functions as local (t) in both libraries.
  • OBS_LIBOBS_BACKEND=C was last run before the obs-util change, on the base with libobs, frontend: Take a NULL protocol in the output protocol lookups #1828: 377/377, IDENTICAL (1766). The obs-util change touches only Rust, which that lane does not build.
  • test_scene_bounds_place_rust 16/16, with no obs-util symbol in the binary and no undefined uuid, GIO or base log symbol (nm -u).
  • The six scene audio tests against both libraries: test_scene_audio_actions 4/4, test_scene_audio_action_steps 17/17, test_preview_only_hide 6/6, test_group_hide_transition_audio 4/4, test_group_active_refs 10/10, test_item_transition_empty_source 4/4.
  • soldr cargo test -p obs-data --test obs-tests-core scene:: passes 100/100 (29 of them new: 23 Tier 1, 6 Tier 3), and the whole obs-tests-core binary passes 757/757.
  • test_scene_audio_actions on its own, with the machine under heavy load from other builds (load average 22 to 24): the hybrid failed once in show_over_other_item, and the C-backend libobs, which has no Rust in it, failed once in hide_over_other_item. In both, a change landed on sample 0 of a tick: the known flake of that test under load, which a separate fix covers. Run alone again, it passed. On the final base it passes in full ctest and on its own.
  • Also run: uv run ci/local_gate.py --lane lint passed, and --lane cross passed. ci/test_build_groups.py and ci/test_ported_sources.py pass.
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change for libobs).

Windows/macOS: not built there. The core and the shim have no host conditional. The parity test leaves out, on MSVC only, the rates above 1 GHz where util_mul_div64 faults.

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)

@zackees

zackees commented Oct 22, 2026

Copy link
Copy Markdown
Member

Coordinator note: the Windows Tier 2 failure is caused by this PR, not a flake. rust/obs-scene/Cargo.toml now depends on obs-util, so libobs_rust.lib pulls obs_util objects into test_scene_bounds_place_rust, which does not link their native dependencies:

libobs_rust.lib(obs_util-…) : error LNK2019: unresolved external symbol __imp_timeBeginPeriod referenced in function DllMain
… __imp_timeEndPeriod, base_default_log_handler, base_log_literal, __imp_UuidCreate
test_scene_bounds_place_rust.exe : fatal error LNK1120: 5 unresolved externals

(run 39868571554, job 120139189967)

Either avoid the obs-util dependency in obs-scene (if only a small helper is needed), or link the scene _rust cmocka targets with what obs-util needs on Windows (winmm, rpcrt4, and the C base_* log symbols).

@rdoupe

rdoupe commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator Author

Agreed, thanks. The fix is in progress: obs-scene does the front erase of the action darray itself (same bytes as C's da_erase) and drops the obs-util dependency, so test_scene_bounds_place_rust links as before. I'll push it to this PR shortly.

@rdoupe
rdoupe force-pushed the port/scene-audio-actions branch from f691c82 to bbacba6 Compare October 23, 2026 00:08
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
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
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
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
@zackees
zackees force-pushed the port/scene-audio-actions branch from bbacba6 to a6d2da3 Compare October 23, 2026 00:42
@zackees

zackees commented Oct 23, 2026

Copy link
Copy Markdown
Member

Coordinator: rebased onto master after #1848 landed. The conflicts in ci/test_ported_sources.py, libobs/build/source-sets.json and rust/README.md were list entries, and I kept both sides. ci/test_ported_sources.py passes. CI is running again.

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 scene item visibility audio ramp in obs-scene.c to obs-scene

2 participants