Skip to content

rust: Port the scene item visibility audio ramp in obs-scene.c to obs-scene #1814

Closed

Description

@rdoupe

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 at ts, and places each one on the frame floor((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 fills buf (1024 gains) with the visibility before each change and sets item->visible. With ts 0, it applies every action, whatever its time. Then it drops one active reference per hide, and calls obs_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 before ts plus 1024 frames in ns (or ts is 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:

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_mutex and audio_actions (a DARRAY(struct item_action), which it erases in place). It also reads item->parent->source and item->source for the removal.
  • Seams into C, all hidden (declared in obs-internal.h, not EXPORT), 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_actions and visible, so Rust never mirrors struct obs_scene_item. Rust erases in place with obs_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.
  • Entry points out of the unit, also hidden: the three functions, under the same names and signatures.
  • apply_scene_item_volume() divides by sample_rate. Only the audio path calls it, with the real rate. The preview path passes rate 0 to apply_scene_item_audio_actions() only, where the rate is a multiplier. The port must keep it that way, because the Rust mul_div64 panics 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_refs and test_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, the ts 0 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

  1. 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 with obs_add_raw_audio_callback(). Besides the public API it reads only the item's active_refs, its queue length and the source's activate_refs, as test_group_hide_transition_audio does, so it covers the hybrid libobs too and will see the port. The action time is taken inside obs_sceneitem_set_visible(), so each case brackets the call with os_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 before obs-scene.c.h), and lose static. No test compiles obs-scene.c.h itself, 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, the ts 0 path, the volume window and process_all_audio_actions().
  2. 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.
  3. Port PR (RED, then GREEN):
    • obs_scene::audio_actions gets the pure core: fn apply(actions, ts, rate, visible, buf) -> (consumed, visible, hides), plus the volume window. src/ffi/audio_actions.rs exports 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.json entry and export fragment (rust-exports.map, rust-hidden.txt, rust-unexports-macos.txt), as libobs.ported-scene-group-transform has.
    • Tests:
      • Tier 1 ports every test_scene_audio_action_steps expectation 1:1, over the pure core.
      • Tier 3 checks parity against the moved C: a proptest over action lists (unsorted timestamps too, ts 0, timestamps at 0 and near u64::MAX, rates from 8 000 to 192 000 and rate 0 with ts 0) 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=C and in the hybrid with libobs-c-reference, 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. test_scene_audio_action_steps compiles the C, so only Tier 3 compares the swap there (as for test_scene_coords).
      • Tier 4 does not apply (no protocol change).
    • Docs: the rust/README.md row. The docs/rust-port/naming.md row (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 bool pointers 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, writes visible, holds actions_mutex around 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_actions and &item->visible to 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 other obs-scene ports.
  • Threads. It runs on the audio thread under the scene's audio_lock and the item's actions_mutex. For a preview-only scene it runs on the video thread, under audio_lock. obs_sceneitem_set_visible() pushes under actions_mutex from 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 on user_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_refs below 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 (ts 0), 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.
  • Found while reading, outside the unit (stays C): in scene_audio_render(), mix_audio_with_buf() applies buf[i] to output sample pos + i. buf is indexed from the scene's tick timestamp, so for an item whose audio starts pos samples into the tick, its show or hide is pos samples late (less than one tick, 21 ms at 48 kHz). With one item, or items whose audio is aligned, pos is 0. Not pinned here. It could be a fix of its own, separate from this port.

Activity

  1. rdoupe commented on Oct 22, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #1814

    1. Characterization and move PR (next, ready): the three functions moved verbatim into libobs/obs-scene-audio-actions.c.h behind hidden entry points, with test_scene_audio_actions (real libobs mix capture, exact hide/show sample) and test_scene_audio_action_steps (fake item, exact buffers/queues/counts, 11 rates). No behaviour change.
    2. Field access for the port (accessor, or C passing the fields to the core with the lock kept in C).
    3. Port to rust/obs-scene (module audio_actions) with oracle parity.
  2. added
    claimed
    on Oct 22, 2026
  3. rdoupe commented on Oct 22, 2026

    @rdoupe
    CollaboratorAuthor

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    claimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions