Skip to content

libobs: Move the scene item visibility audio ramp into obs-scene-audio-actions.c - #1815

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

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

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Refs #1814 (PR 1 of the plan: the characterization and the extraction. The accessor and the port follow.)

What and why

This is a behaviour-neutral extraction before the Rust port, as the testing policy asks for part of a large file. The scene item visibility audio ramp moves from libobs/obs-scene.c.h into libobs/obs-scene-audio-actions.c.h. When an item whose source has audio is shown or hidden, obs_sceneitem_set_visible() queues an action with the time of the call. These functions apply it on the sample of the scene's audio tick that the time falls on, through a 0/1 gain buffer, and drop one active reference per hide. The move comes with two characterization tests, because the existing tests check that a hide is applied but not where it lands. There is no Rust, no behaviour change and no public header change.

  • What moves: apply_scene_item_audio_actions(), apply_scene_item_volume() and process_all_audio_actions(), verbatim. The only change is that they lose static: scene_video_tick() (the preview-only path, libobs: Apply scene item visibility changes in scenes that are only shown #358) and scene_audio_render() stay in obs-scene.c.h and call them. The forward declaration before scene_video_tick() goes.
  • What stays: the callers, the mixing (mix_audio_with_buf(), scene_mix_audio()), the queueing in obs_sceneitem_set_visible() and obs_scene_add_internal(), and set_visibility(). In this PR the lock, the active_refs decrement and obs_source_remove_active_child() are still inside the moved code. The accessor PR splits them out.
  • Hidden: the three functions are declared in obs-internal.h without EXPORT, so -fvisibility=hidden keeps them local (t) symbols. The exported symbols are unchanged.
  • Build: the new file joins the existing libobs.core-source group just before obs-scene.c.h, with libobs.pch.h like every member of that group. The group, the PCH, source-sets.json and the CMake source lists of libobs are unchanged. No test compiles obs-scene.c.h itself, and the moved code has no platform conditional.

Why the accessor is not in this PR: for the hotkey pair it was (#1799), because there it was two bool pointers and one call wrapper. Here it is like the press state machine (#1779), whose accessor was its own PR. The moved loop reads and erases the item's darray in place, writes visible, holds actions_mutex around it, and drops the active references after it, on the audio thread. The accessor changes how the moved code reaches every one of those, and where the lock and the removal live. So it gets its own review, as the plan has it.

test_scene_audio_actions (new, its own commit before the move) links the real libobs, plays constant-level tones in the program scene, and captures the program mix (track 1) 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. The structs stay C in the hybrid libobs, so it covers the hybrid 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 (floor((t - tick) * rate / 1e9)). The check does not depend on how fast the machine is. The waits only bound how long it waits for the audio thread. Its 4 cases:

  • a hide of the only audible item: the mix is the tone, sample for sample, up to the sample of the call and silent from it on, over contiguous ticks. The item's one reference is dropped and the source is deactivated;
  • a hide of one of two items: only its part of the mix stops, on its sample;
  • a show over a second item: its part starts on its sample. The show takes its reference at once, not when it is applied;
  • hide, show, hide back to back: all three are applied, the item ends hidden and the mix silent, and the two hides drop both references (one from the add, one from the show).

It needs core video and audio, so it builds on Linux only and runs under xvfb-run, like test_group_hide_transition_audio. It skips without a display.

test_scene_audio_action_steps (new, with the move) compiles the moved file (ASan and UBSan, no libobs core) and calls only the three functions, so the port can link the same file against the Rust staticlib. It uses a fake scene item: the real struct, zeroed, with its actions, visibility, references and mutex set. Its scene has no source, so obs_source_remove_active_child() only logs its NULL parent, and the test counts the removals from the log. Its 15 cases pin exact buffers, queues and counts:

  • no actions; the change on each of the 1024 frames at 48 kHz, for a hide and a show, and on 8 chosen frames at 11 rates from 8 to 192 kHz; one nanosecond earlier is one frame earlier;
  • an action on frame 1024 stays queued; one timed before the tick lands on frame 0; several changes in one tick, and two on one frame; later actions stay queued in order, and the next tick places them from its own start;
  • the hide count: one reference per hide, and the child removed once, at 0;
  • ts 0 (the preview-only path, rate 0) and process_all_audio_actions() apply everything, future actions included;
  • apply_scene_item_volume(): nothing queued (false, buffer not written), the window of ts plus the tick duration rounded down to whole ns at 6 rates, the first action only, and ts 0;
  • that nothing is written past the 1024 gains.

It is in the obs-build C test manifest.

Characterized, not endorsed (a comment in the tests, and in the issue). None is changed here, and the issue proposes no fix before the port:

  • Actions out of time order: a change timed before the previous one does not rewrite the buffer, so it 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.
  • The volume window rounds the tick duration 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.

Found while reading, outside the moved code and not pinned here: mix_audio_with_buf() in scene_audio_render() applies buf[i] to output sample pos + i, but buf is indexed from the scene's tick. So for an item whose audio starts pos samples into the tick, its change is pos samples late (less than one tick). It is in the issue, as a possible fix of its own.

RED → GREEN

  • RED commit: N/A, behaviour-neutral (no fix and no port). 3699009d8 test: Characterize the scene item visibility audio ramp adds test_scene_audio_actions and passes 4/4 on the unchanged C, against libobs with the C backend, the hybrid libobs and libobs-c-reference. The move is cd92afd58 libobs: Move the scene item visibility audio ramp into obs-scene-audio-actions.c.

  • GREEN: test_scene_audio_actions 4/4 and test_scene_audio_action_steps 15/15, against all three.

  • Equivalence: apart from static and one trailing blank line, the moved text is identical to the three functions on origin/master. A differential harness (local, not committed) compiles the original static functions from origin/master next to the moved file and 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, on the tick boundaries), ticks at 0, near 0 and near UINT64_MAX, and 13 rates (0, 1, 8 kHz to 192 kHz, 1 GHz). The buffers, return values, visibility, references, removals and remaining queues were identical in every trial.

  • Mutation check of the two tests: 21 of 22 mutants fail at least one of them. Each mutant of obs-scene-audio-actions.c.h was built into libobs (C backend) and both tests. The differential harness flags all 21 too:

    • placement: a late action not clamped to the tick; frame 1024 counted as this tick; the ts 0 path stopping at frame 1024 like a tick (it then never drains, and process_all_audio_actions() loops: both tests hang); rounding to the nearest frame; one frame late;
    • the buffer: a span filled with the new visibility, a span that includes the change frame, the tail always 1.0, the visibility before a change not updated;
    • the queue: the erase skipping the next action;
    • the hide count: a show counted as a hide; the child removed at 1, or at and below 0; one reference per call, not per hide; no reference dropped;
    • the volume window: <= instead of < (one ns more), no ts 0 case, false after applying, a 1023-frame duration, the last action peeked instead of the first;
    • process_all_audio_actions() with a tick of 1 instead of 0.

    Seven of them (late action, frame 1024, rounding, removal at or below 0, the <= window, the 1023-frame duration, the last action peeked) pass the real-libobs test and fail only the steps test, because they need a boundary or an order that wall-clock toggles do not hit. The survivor is process_all_audio_actions() calling once instead of looping. It is equivalent: with ts 0, one call consumes the whole queue, so the loop's second pass never has work (the harness also finds 0 differences).

  • Local, two build trees: OBS_LIBOBS_BACKEND=C, and HYBRID with OBS_BUILD_C_REFERENCE=ON, both full builds. 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 or failed to run, and nothing needed a second run. 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, against all three. test_scene_audio_actions also passed 15 runs in a row while the lint lane ran beside it. After the final rebase onto e1b649910 (#1806), both trees were rebuilt, and the six scene audio tests, the symbol comparison, ci/test_build_groups.py and ci/test_ported_sources.py passed again.

  • 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. clang-format 22.1.3 and gersemi 0.25.0 are clean on the changed files.
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change).

Windows/macOS: not built there. The moved code has no host conditional. test_scene_audio_action_steps is built on every OS, like test_context_name_helpers, and is portable C (no cmocka range assertions, no fail_msg()). test_scene_audio_actions is Linux-only, like the other scene audio tests.

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)

Characterization before the visibility audio ramp of obs-scene.c
(apply_scene_item_audio_actions, apply_scene_item_volume and
process_all_audio_actions) moves to its own file and is ported to
obs-scene. test_preview_only_hide, test_group_hide_transition_audio and
test_group_active_refs check that a hide is applied and what it
deactivates, but none checks the sample it lands on.

test_scene_audio_actions links the real libobs, plays constant-level
tones in the program scene, and captures the program mix (track 1)
with obs_add_raw_audio_callback(). It reads only the item's
active_refs, its queued actions and the source's activate_refs besides
the public API, as test_group_hide_transition_audio does, so it also
runs the hybrid libobs. 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 a time in that bracket
maps to (floor((t - tick) * rate / 1e9)): the check does not depend on
how fast the machine is, and the waits only bound how long it waits
for the audio thread. 4 cases pin:
- a hide of the only audible item: the mix is the tone up to that
  sample and silent from it on, the ticks contiguous; the item's one
  active reference is dropped and the source deactivated;
- a hide of one of two items: only its part of the mix stops, on its
  sample;
- a show over a second item: its part starts on its sample, and the
  show takes its active reference at once, not when it is applied;
- hide, show, hide back to back: all three are applied, the item ends
  hidden and the mix silent, and the two hides drop both references
  (one from the add, one from the show), so the source is deactivated.

Applying the actions needs core video and audio, so it builds on Linux
only and runs under xvfb-run, like test_group_hide_transition_audio;
without a display the cases skip.

Refs obs-rust#1814
…o-actions.c

Behavior-neutral extraction before the Rust port: the three functions
of obs-scene.c.h that apply the queued show/hide actions of a scene
item on the samples they fall on move verbatim into
libobs/obs-scene-audio-actions.c.h:
- apply_scene_item_audio_actions(): consumes the actions in the tick,
  fills the 0/1 gain buffer and drops one active reference per hide;
- apply_scene_item_volume(): applies them when the first one falls in
  the tick;
- process_all_audio_actions(): applies every queued action.

The only change is that they lose static: scene_video_tick() and
scene_audio_render() stay in obs-scene.c.h and call them. They are
declared in obs-internal.h without EXPORT, so -fvisibility=hidden keeps
them local symbols, and the forward declaration before
scene_video_tick() goes. The lock, the active_refs decrement and
obs_source_remove_active_child() stay in the moved code for now; the
accessor and the port come after.

The new file joins libobs.core-source just before obs-scene.c.h, with
libobs.pch.h. No group, PCH or source-sets.json change, and the
exported symbols are unchanged (1766).

New test test_scene_audio_action_steps compiles the moved file (ASan
and UBSan, no libobs core) and calls only the three functions, with a
fake scene item (the real struct, zeroed) whose scene has no source, so
obs_source_remove_active_child() only logs its NULL parent and the test
counts the removals from the log. 15 cases pin, as exact buffers,
queues and counts:
- the change on each of the 1024 frames at 48 kHz, and on chosen frames
  at 11 rates from 8 kHz to 192 kHz; one nanosecond earlier is one
  frame earlier;
- an action on frame 1024 stays queued; one before the tick lands on
  frame 0; several in one tick, two on one frame; later ones stay
  queued in order and the next tick places them from its start;
- the hide count: one reference per hide, the child removed once at 0;
- ts 0 (the preview-only path) and process_all_audio_actions() apply
  everything, future actions included;
- apply_scene_item_volume(): nothing queued, the window of ts plus the
  rounded-down tick duration, the first action only, ts 0;
- that nothing is written past the tick.
Characterized, not endorsed: actions out of time order (a change timed
before the previous one does not reach the buffer but is counted, and
a due action waits behind one that is not); an action at ts + duration
at 44.1 kHz falls on frame 1023 but waits for the next tick; more hides
than references go below 0. It is in the obs-build C test manifest.
test_scene_audio_actions passes unchanged.

Refs obs-rust#1814
@rdoupe
rdoupe requested a review from zackees as a code owner October 22, 2026 18:49
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
Merged via the queue into obs-rust:master with commit 4509ca5 Oct 22, 2026
30 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 22, 2026
…tick (obs-rust#1835)

test_scene_audio_actions (obs-rust#1815) failed now and then on a loaded machine,
for example hide_only_item seeing the hide on sample 0 instead of 833.
Each case requires every tone of the scene to have its audio in each tick
it captures. When a tone's thread wakes too late for a tick, libobs marks
the tone's audio pending and leaves it out of that tick. If that tone is
the scene's only audible source, the scene has no audio in the tick and
libobs applies every queued action at once (process_all_audio_actions(),
characterized in obs-rust#1815), so a hide lands on sample 0. Otherwise the tick's
mix is missing that tone. Neither says where the action would have landed.

The capture now records, in the raw audio callback, which tones of the
scene libobs left out of each tick. The callback runs on the audio thread
right after the tick is rendered, so it reads that tick's audio_pending,
which comes from the tone's buffer, not from the ramp code under test. A
toggle whose capture has such a tick is undone (toggled back, the mix
back at its level) and tried again, up to the test's existing deadline.
The checks are unchanged, on the try whose ticks all had their tones'
audio. show_over_other_item now shows through toggle() too, so its show
is retried the same way; its first hide only sets up the case.

Fixes obs-rust#1834
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
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