Repository navigation
Description
Port the scene item visibility audio ramp of libobs/obs-scene.c.h to the existing crate rust/obs-scene as a new module, audio_actions. Showing or hiding a scene item whose source has audio does not change the mix at once. obs_sceneitem_set_visible() (and obs_scene_add(), for the first show) queues an item_action {visible, timestamp} with the time of the call, and 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. So a hide never cuts a source off in the middle of a tick, and each hide it applies drops one of the item's active references. Every scene and group runs it on the audio thread, for every item, on every tick. The frontend, obs-websocket (SetSceneItemEnabled), scripts, hotkeys (show/hide a source) and scene item transitions all reach it, but only as a bool and a time that libobs takes itself.
The unit, about 75 lines:
apply_scene_item_audio_actions(item, buf, ts, sample_rate): it consumes the queued actions up to the end of the tick atts, and places each one on the framefloor((timestamp - ts) * sample_rate / 1e9). A time before the tick goes on frame 0, and the first action on frame 1024 or later stops the loop. It fillsbuf(1024 gains) with the visibility before each change and setsitem->visible. Withts0, it applies every action, whatever its time. Then it drops one active reference per hide, and callsobs_source_remove_active_child()when that reaches 0.apply_scene_item_volume(item, buf, ts, sample_rate): it applies the tick when the first queued action falls beforetsplus 1024 frames in ns (ortsis 0), and returns whether it did. That tells the mix whether to use the buffer.process_all_audio_actions(item, sample_rate): it applies everything, whatever the time.- Callers, which stay C:
scene_audio_render()callsapply_scene_item_volume()for each item with the tick's timestamp, orprocess_all_audio_actions()when no item of the scene has audio in the tick.scene_video_tick()callsapply_scene_item_audio_actions(item, NULL, 0, 0)for a scene that is showing but not active (the Studio Mode preview, a projector, the multiview), because the audio thread only renders the active tree (bug: libobs: hiding a source that is only in the Studio Mode preview never hides it ("Shutdown source when not visible" never fires) (upstream #4120) #357, libobs: Apply scene item visibility changes in scenes that are only shown #358).
These stay C: actions_mutex, the active_refs decrement and obs_source_remove_active_child() (driven by the hide count), the mixing (mix_audio_with_buf(), scene_mix_audio()), the queueing in obs_sceneitem_set_visible() and obs_scene_add_internal(), and set_visibility() for items without audio.
Phase and position
Phase 6 (#444), libobs core through opaque seams. It joins rust/obs-scene, which already holds the bounds, bounds placement, blend table, coordinates and group transform (#1719, #1724). It is part of a large file, so it goes in through function extraction (testing-policy.md, "Extracting a function before porting it"), then an accessor, then the port. Three PRs. Its Rust tests join the crate's existing tests/scene/ binary. No new test binary.
Dependencies and existing tests
- It reads and writes four fields of
struct obs_scene_item(obs-scene.h):visible,active_refs,actions_mutexandaudio_actions(aDARRAY(struct item_action), which it erases in place). It also readsitem->parent->sourceanditem->sourcefor the removal. - Seams into C, all hidden (declared in
obs-internal.h, notEXPORT), as for the hotkey press state machine (libobs: Reach hotkey bindings through a hidden accessor #1779) and the hotkey pair (libobs: Move the hotkey pair press latch into obs-hotkey-pair.c #1799):- the accessor (PR 2), a struct of pointers to
audio_actionsandvisible, so Rust never mirrorsstruct obs_scene_item. Rust erases in place withobs_util::ffi::darray, whose erase is already ported (rust: Port the obs-output.c interleaver to obs-output #1594); - C keeps the lock and the hide count: it takes
actions_mutex, calls the core, and drops one reference per hide that the core reports. Rust takes no lock and calls no libobs function.
- the accessor (PR 2), a struct of pointers to
- Entry points out of the unit, also hidden: the three functions, under the same names and signatures.
apply_scene_item_volume()divides bysample_rate. Only the audio path calls it, with the real rate. The preview path passes rate 0 toapply_scene_item_audio_actions()only, where the rate is a multiplier. The port must keep it that way, because the Rustmul_div64panics on a zero divisor.- Existing tests (real libobs and GL, Linux):
test_preview_only_hide(libobs: Apply scene item visibility changes in scenes that are only shown #358),test_group_hide_transition_audio,test_group_active_refsandtest_item_transition_empty_source. They check that a hide is applied and what it deactivates. None checks where in the tick it lands, several toggles in one tick, actions past the tick, thets0 path or the hide count with several hides. PR 1 adds two tests (below). - Input: the toggles come from the frontend, obs-websocket, scripts and hotkeys, but only as a bool. libobs takes the time itself (
os_gettime_ns()), and the rate is the audio rate. The unit allocates nothing, and with a tick it writes at most 1024 gains.
Plan
- Characterization and extraction PR (behaviour-neutral):
- New test
test_scene_audio_actions, its own commit on the unchanged C. It links the real libobs, plays constant-level tones in the program scene, and captures the program mix withobs_add_raw_audio_callback(). Besides the public API it reads only the item'sactive_refs, its queue length and the source'sactivate_refs, astest_group_hide_transition_audiodoes, so it covers the hybrid libobs too and will see the port. The action time is taken insideobs_sceneitem_set_visible(), so each case brackets the call withos_gettime_ns()and accepts exactly the samples that a time in that bracket maps to. The check does not depend on how fast the machine is. It pins the sample of a hide of the only audible item, of a hide over a second item and of a show over a second item, and that hide, show, hide back to back drops both references. - The three functions move verbatim into
libobs/obs-scene-audio-actions.c.h, in the same unity group (libobs.core-source, just beforeobs-scene.c.h), and losestatic. No test compilesobs-scene.c.hitself, and no platform conditional moves. - New test
test_scene_audio_action_steps, with the move. It compiles the moved file into itself with a fake scene item and pins exact buffers, queues and counts: the change on every frame at 48 kHz and on chosen frames at 11 rates from 8 to 192 kHz, frame 1024, late actions, several changes in one tick, later actions kept in order, the hide count, thets0 path, the volume window andprocess_all_audio_actions().
- New test
- Accessor PR (RED, then GREEN; behaviour-neutral): the field view,
obs_sceneitem_audio_action_fields(item), and the split that keeps the lock and the hide count in C. Afterwards the moved file reaches the item only through them. The export set is unchanged. - Port PR (RED, then GREEN):
obs_scene::audio_actionsgets the pure core:fn apply(actions, ts, rate, visible, buf) -> (consumed, visible, hides), plus the volume window.src/ffi/audio_actions.rsexports the three functions with the C signatures. The C file moves to its own ported group,libobs.ported-scene-audio-actions, with its own.pch.h,source-sets.jsonentry and export fragment (rust-exports.map,rust-hidden.txt,rust-unexports-macos.txt), aslibobs.ported-scene-group-transformhas.- Tests:
- Tier 1 ports every
test_scene_audio_action_stepsexpectation 1:1, over the pure core. - Tier 3 checks parity against the moved C: a proptest over action lists (unsorted timestamps too,
ts0, timestamps at 0 and nearu64::MAX, rates from 8 000 to 192 000 and rate 0 withts0) that compares the buffers byte for byte, the queue left in the darray, the visibility, the return value and the hide count. - Tier 2 runs with
OBS_LIBOBS_BACKEND=Cand in the hybrid withlibobs-c-reference, unchanged.test_scene_audio_actions,test_preview_only_hide,test_group_hide_transition_audio,test_group_active_refsandtest_item_transition_empty_sourcesee the swap.test_scene_audio_action_stepscompiles the C, so only Tier 3 compares the swap there (as fortest_scene_coords). - Tier 4 does not apply (no protocol change).
- Tier 1 ports every
- Docs: the
rust/README.mdrow. Thedocs/rust-port/naming.mdrow (obs-scene*.c.h) already covers the file.
For the reviewer to decide
- The accessor is its own PR, as planned. For the hotkey pair it went into PR 1 (libobs: Move the hotkey pair press latch into obs-hotkey-pair.c #1799), because there it was two
boolpointers and one call wrapper. Here it is like the press state machine (libobs: Reach hotkey bindings through a hidden accessor #1779), whose accessor was its own PR because the field view changed how the moved C read every binding. The moved loop reads and erases the darray, writesvisible, holdsactions_mutexaround all of it, and drops the active references after it. So the accessor PR changes how the moved code reaches every field, and where the lock and the removal live, on the audio thread. - The shape of PR 2. Instead of a field view, the C callers could pass
&item->audio_actionsand&item->visibleto a core that returns the hide count, and keep the lock and the reference drop around the call, as libobs: Move the context name helpers into obs-context-names.c #1788 passes&obs->data.unnamed_index. Then no accessor would be needed. The plan keeps the field view, as for the otherobs-sceneports. - Threads. It runs on the audio thread under the scene's
audio_lockand the item'sactions_mutex. For a preview-only scene it runs on the video thread, underaudio_lock.obs_sceneitem_set_visible()pushes underactions_mutexfrom any thread. The Rust core keeps no state between calls and takes no lock. - Behaviours the characterization pins, not endorses (none is changed by this plan):
- Actions out of time order. A change timed before the previous one does not rewrite the buffer, so that change never reaches the gain buffer, but a hide among them is still counted. The loop and the volume window also stop at the first action that is not due, even when a later one is. Each action takes its time at the start of
obs_sceneitem_set_visible()and is pushed later, so this needs two threads toggling one item at once, which is already a race onuser_visible. - The volume window uses the tick duration rounded down to whole ns. At 44.1 kHz an action on the last nanosecond of frame 1023 waits for the next tick and lands on its frame 0, one frame late.
- More hides than references take
active_refsbelow 0, and the child is removed only at 0.obs_sceneitem_set_visible()keeps them paired, so libobs does not get here. - With no audio in the scene for a tick (
process_all_audio_actions()), and on the preview path (ts0), every queued action is applied at once, future ones included. So a show of the only audible item takes effect on a tick boundary near the call, not on its sample.
None looks like a user-facing bug today. I propose no fix PR before the port. If one is wanted, it should come before PR 3, so the port copies fixed C.
- Actions out of time order. A change timed before the previous one does not rewrite the buffer, so that change never reaches the gain buffer, but a hide among them is still counted. The loop and the volume window also stop at the first action that is not due, even when a later one is. Each action takes its time at the start of
- Found while reading, outside the unit (stays C): in
scene_audio_render(),mix_audio_with_buf()appliesbuf[i]to output samplepos + i.bufis indexed from the scene's tick timestamp, so for an item whose audio startspossamples into the tick, its show or hide ispossamples late (less than one tick, 21 ms at 48 kHz). With one item, or items whose audio is aligned,posis 0. Not pinned here. It could be a fix of its own, separate from this port.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
rdoupe commentedon Oct 22, 2026
Claiming: #1814
libobs/obs-scene-audio-actions.c.hbehind hidden entry points, withtest_scene_audio_actions(real libobs mix capture, exact hide/show sample) andtest_scene_audio_action_steps(fake item, exact buffers/queues/counts, 11 rates). No behaviour change.rust/obs-scene(moduleaudio_actions) with oracle parity.libobs: Move the scene item visibility audio ramp into obs-scene-audi…
libobs: Apply scene item audio actions through a core on the item's f…
rdoupe commentedon Oct 22, 2026
Port is up in #1842. Intentional differences from C, listed in the parity test header and rust/README.md: with ts 0 and a buffer, C writes past the 1024 gains; Rust makes the same writes inside the buffer and none past it. A sample rate of 0 in
obs_scene_audio_action_due()returns true (C divides by zero; apply already puts every action on frame 0 at rate 0). A null queue or visibility does nothing. Everything pinned as not endorsed in #1815 is kept.obs-scene: Add Tier 1 tests for the scene audio action ramp port
obs-scene: Port the scene audio action ramp core
obs-scene: Check the scene audio action ramp port against the C
docs: Record the scene audio action ramp port
obs-scene: Add Tier 1 tests for the scene audio action ramp port
obs-scene: Port the scene audio action ramp core
obs-scene: Check the scene audio action ramp port against the C
docs: Record the scene audio action ramp port
obs-scene: Port the scene audio action ramp core (obs-rust#1842)