Skip to content

libobs: Port the output stream delay to obs-output - #1567

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-output-delay
Oct 20, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-output-delay

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1546 (PR 4 of 4; #1547 added the characterization test test_output_delay, #1550 the hidden accessors this port reads the output through, #1548 and #1561 the Rust util/deque.h)

What and why

This PR ports libobs/obs-output-delay.c.h, the stream delay, to a new crate, rust/obs-output. The file has seven functions. obs_output_set_delay, obs_output_get_delay and obs_output_get_active_delay are EXPORT. process_delay, obs_output_delay_start, obs_output_delay_stop and obs_output_cleanup_delay are declared in obs-internal.h without EXPORT; obs-output.c calls them, and installs process_delay as the encoded packet callback by name. libobs-with-rust drops the C file and links the Rust shims.

  • Crate: obs-output, named after the component (meta: Phase 6 libobs core migration through opaque C seams #444), so later units of obs-output.c can join it. It is listed in naming.md, in both rust/README.md tables and in the testing-policy obs-tests-core row. Its one dependency is obs-util (the deque, os_gettime_ns and bmem).
  • Safe core (src/delay.rs):
    • pop_decision(front_ts, now, active_delay_ns, preserve, reconnecting) returns Hold, Pop or Extend(age). It is a pure function with the C's strict > and wrapping subtraction.
    • dispatch runs an entry that left the queue: a packet is delivered while the delay is active and capturing and released otherwise; a start or stop runs whatever the delay state.
    • active_delay_secs is the whole-seconds getter, with the C's truncating u32 cast.
    • process_delay, pop_packet, start, stop and cleanup are generic over a DelayOutput trait (the delay fields, the queue, the lock and the libobs calls), as libobs: Port the Wayland hotkey key state to obs-hotkey #1516 is generic over its lock and store. They take the lock where the C takes delay_mutex: once per push and once per pass over the front, never in cleanup, never around a callback.
    • The setter and getters take Option<&O> and a NullLog, so the null-output case (logged under the setter's name by all three) is in the core and Tier 1 ports it 1:1.
    • The clock is a parameter. No host cfg, no unsafe.
    • The six behaviours test_output_delay marks "characterized, not endorsed" are kept: the getters log a null output under the setter's name; the seconds getter wraps past u32::MAX s; an entry is held while its age equals the delay (so a zero delay waits for a later packet); a clock that goes backwards releases at once; a start whose data capture fails leaves starting set; a delay grown while reconnecting never shrinks back.
  • Shim (src/ffi/output_delay.rs):
    • #[repr(C)] encoder_packet, encoder_packet_time, delay_data and obs_output_delay_fields, with layout tests.
    • It never reads struct obs_output: it goes through obs_output_delay_fields() and obs_output_delay_signal() (libobs: Read the output delay fields through a hidden accessor #1550). Each field is read and written where the C does; the os_atomic_* fields are sequentially consistent atomics of their C size (bool, and long picked by size_of::<c_long>(), no host cfg).
    • delay_data stays the C struct deque of struct delay_data that obs-output.c frees. It is pushed, peeked and popped in place with obs_util::ffi::deque (obs-util: Add a Rust equivalent of util/deque.h #1561).
    • Held packets are libobs's reference-counted copies. obs_encoder_packet_create_instance is hidden in libobs, but libobs-with-rust links the Rust staticlib with the same objects, so the call resolves there as libobs: Port the Wayland hotkey key state to obs-hotkey #1516's obs_hotkeys_mutex_lock does. delay_mutex is locked with libobs's pthread_mutex_lock on the opaque pointer (glibc, libSystem, w32-pthreads).
    • Each entry point reads os_gettime_ns() once, as the C does, and calls an *_at function with that time. The *_at functions are Rust-mangled (hidden by _ZN*) and are what the parity test drives with a test clock.
    • Symbols: the four hidden ones are in rust-hidden.txt, rust-exports.map and rust-unexports-macos.txt; the three exported ones are in rust-exports.d/obs-output-output_delay.txt. In both libobs.so builds nm shows the three as T and the four as t.
  • Build group:
    • obs-output-delay.c.h leaves libobs.core-output for its own group, libobs.ported-output-delay, listed in LIBOBS_PORTED_SOURCES. The group is not host-specific, so sources.cmake, os-*.cmake, obs-build and ported_sources.py need no change.
    • Its PCH, libobs-ported-output-delay.pch.h, is obs-internal.h alone: since libobs: Read the output delay fields through a hidden accessor #1550 the file needs nothing else, so the oracle and test_output_delay compile it without the core. The comment in tests.d/test_output_delay.cmake that named the old PCH is updated; the test and its sources are unchanged.
  • Oracle (oracle/output_delay.c): renames the seven functions with #defines, renames os_gettime_ns to a clock the test sets (as test_output_delay does), and includes the original file. Everything else it calls keeps its libobs name and is defined by the test. It also exports the layouts and values, and a pthread_mutex_t for the test outputs with a held check (the C test's mutex_held()). obs_output_set_delay uses __FUNCTION__, which -Wpedantic rejects, so the include is wrapped in a GCC diagnostic push/ignore/pop; the file is compiled unmodified.
  • Intentional differences: none. Two things differ only where nothing can see it: a packet entry without timing stores zeros where the C leaves its stack copy uninitialized, and a null delay_callback aborts where the C calls through a null pointer (libobs sets it before installing the delay).

test_output_delay (20 cases) compiles the C file into itself and is unchanged. It passes in every lane but cannot see the swap, so Tier 3 is the proof of the swap, as the issue planned. No existing C test drives a delayed output through real libobs (none calls obs_output_set_delay), so there is no end-to-end test of the swap either; see the deliberate break below.

RED → GREEN

  • RED commit: 8d60c765f libobs: Add Tier 1 tests for the output delay port. The core is todo!(). soldr cargo test -p obs-data --test obs-tests-core output:: fails 35 of 35 with "not yet implemented".
    • Tier 1 (tests/output/delay.rs) mirrors test_output_delay 1:1: the same twenty names and assertions, in order, on a model output that records the libobs calls and whether the lock was held, as the C test's stubs do.
    • 15 new edge cases: the strict comparison and the wrapping age at the boundaries, when preserve extends the delay (also to a shorter one), the truncating cast at u32::MAX s, one lock per push and per pass and none in cleanup or the setters, an empty queue, a start or stop at the front while preserving, the capture flag not read when inactive, a failed capture releasing packets queued before it, restart references, a stop on an empty queue, and an entry the file never queues.
    • The tests are a module of the existing core category binary, obs-tests-core. No new test binary: ci/test_binary_cap.py passes (8 of 8).
  • GREEN: 66da956a3 libobs: Port the output stream delay to obs-output. output:: passes 40/40 (35 Tier 1, 5 Tier 2/3), and the whole obs-tests-core binary passes 280/280.
    • Tier 3 (tests/output/delay_parity.rs). Both sides run on a test output through the same stand-ins: the two accessors, the five obs-output.c calls, the packet copy and release (tracked, so a delivered packet can be told from the encoder's), and the delay callback. Each records its call, with whether delay_mutex was held.
      • the size, alignment and every field offset of the four structs, plus struct deque and the C long, and the eleven values the file uses (Tier 2 layout);
      • the characterization scenarios, the six "not endorsed" behaviours among them, step by step;
      • a proptest, 1024 random sequences of up to 51 steps: packets (random fields, data and timing), starts, stops, cleanups, setter and getter calls (also on a null output), and changes of the delay flags, info.flags, the stub results, the running delay, the restart count and the clock (forward, backward, wrapping). After every step it compares the return value, the recorded calls, the log line, the packet copies made and released, every delay field, the deque's size, positions and capacity, and every queued entry field by field.
      • The log comparison swaps the process-wide log handler, so those two runs re-exec the test binary (testing-policy). One oracle artifact is undone before comparing: the renamed setter names itself oracle_obs_output_set_delay through __FUNCTION__.
      • the exported entry points with the real clock: the queued times lie between two os_gettime_ns() readings.
    • Deliberate-break check: in the Rust core I changed the strict elapsed > active_delay_ns to >=, then restored it. Nine more mutants of the core and shim (one pass per packet, the capture flag not checked, a forced stop, refs not cleared or not counted, starting kept on encoder failure, any flag preserving, the timing dropped, the warning text) were each killed by the two parity runs.
      • Tier 1 and 3: 10 tests failed, 8 Tier 1 (packets_held_then_released_in_order, zero_delay_still_waits_for_the_next_packet, pop_decision_is_strict_and_wrapping, ...) and both parity runs (parity_fixed_sequences, parity_random_sequences).
      • With libobs-with-rust rebuilt: none of the 34 output, delay, encoder, mux and interleave cmocka tests failed in mode ON (34/34 passed with the mutant linked into libobs-with-rust), because none of them sets a delay; test_output_delay compiles the C itself and cannot see the swap. So the swap of process_delay and the start/stop path is proven by Tier 3 only, as the issue planned. A real-libobs test of a delayed output would be a follow-up.
      • After the restore, all of these pass again.
    • ASan: output:: (Tier 1 and 3, the 1024-case proptest included) passes 40/40 under AddressSanitizer (nightly -Zsanitizer=address on the Rust side; the C oracle is uninstrumented but allocates through the ASan allocator), with no reports.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON). Each tree runs the full cmocka suite against libobs (OFF) and against libobs-with-rust (ON):
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 340/340 340/340 IDENTICAL (1766, same as master)
Rust (ENABLE_RUST_LIBOBS=ON) 340/340 340/340 IDENTICAL (1766)
  • No test was skipped or not run in any of the four runs. The export lists are also identical across the two lanes. After the final rebase onto master (four new commits, among them callback: Fix signal callback removal across nesting, recursion and registration (split from #1464) #1556 and fix: accept C11 flexible arrays in the MSVC oracle build #1563), both lanes were rebuilt and the output, delay, encoder, mux, interleave, signal and callback tests rerun OFF and ON, with the same symbol result.
  • test_output_delay and test_async_delay_audio pass in all four runs.
  • libobs.ported-output-delay+.h.c is linked into libobs and not into libobs-with-rust.
  • Environment: headless (private Xvfb, private D-Bus, fresh HOME).
  • ci/local_gate.py:
    • --lane lint (clang-format, gersemi, rustfmt, clippy -D warnings, Dylints, ifdef ratchet, test-binary cap): passed.
    • --lane cross (clippy and Dylints for x86_64-pc-windows-msvc and aarch64-apple-darwin, plus a link of every test binary, the oracle included): passed.
    • --lane cross-release: passed.
    • --lane rust-test (soldr cargo test --workspace --locked, plus a --no-run link for x86_64-pc-windows-gnu): passed (obs-tests-core 280/280, the obs-util harness 706/706 including export_list).
  • ci/test_build_groups.py, ci/test_naming.py, ci/test_ported_sources.py, ci/test_rust_scope.py, ci/test_binary_cap.py, ci/test_cmocka_includes.py: OK.
  • Real app: not applicable (no behaviour change).

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 (characterization test_output_delay landed in test: Characterize the output stream delay #1547)
  • 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): not applicable (the oracle's -Wpedantic pragma covers one include in one test-only file)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): none

RED for the port of libobs/obs-output-delay.c.h, the stream delay, to a
new crate, obs-output. The safe core (obs_output::delay) is generic over
the output: its delay fields, the queue, the delay lock and the libobs
calls the C file makes (DelayOutput), so the C ABI shim can run it on the
real struct obs_output through the hidden accessors and take delay_mutex
exactly where the C takes it. The pop decision is a pure function of the
front entry's time, the clock, the running delay and the preserve and
reconnecting flags; the clock is a parameter, read once per call. Every
function is a todo!() stub.

The first twenty cases mirror test_output_delay 1:1 (same names, same
assertions, in order) on a model output that records the libobs calls
and whether the lock was held, as the C test's stubs do. The rest pin the
edges: the strict comparison and the wrapping age, when preserve extends
the delay (also shorter than it was), the truncating seconds cast, one
lock per push and per pass and none in cleanup, an empty queue, a start
or stop at the front while preserving, a failed capture that releases
packets queued before it, and an entry the file never queues.

They are a module of obs-tests-core, so no new test binary
(ci/test_binary_cap.py: 8 of 8).

soldr cargo test -p obs-data --test obs-tests-core output:: fails 35 of
35 with "not yet implemented".

Refs obs-rust#1546
libobs/obs-output-delay.c.h moves to obs_output::delay, a new crate named
after the component, so later units of obs-output.c can join it. Its
seven functions are what obs-output.c calls to hold an output's packets,
start and stop for the stream delay: obs_output_set_delay,
obs_output_get_delay and obs_output_get_active_delay (EXPORT), and
process_delay, obs_output_delay_start, obs_output_delay_stop and
obs_output_cleanup_delay (hidden; obs-output.c installs process_delay as
the encoded packet callback by name).

The safe core runs on any DelayOutput (the C output through the shim, or
a model in the tests) and takes the delay lock exactly where the C takes
delay_mutex: once per push and once per pass over the front, never in
cleanup, and never around a callback. The pop decision is a pure
function of the front entry's time, the clock, the running delay and the
preserve and reconnecting flags, with the strict comparison and the
wrapping age of the C. The six behaviours test_output_delay marks
"characterized, not endorsed" are kept.

The shim never reads struct obs_output: it goes through the hidden
obs_output_delay_fields() and obs_output_delay_signal() (obs-rust#1550), and
reads and writes each field at the point the C does, the os_atomic ones
as atomics of their C size. delay_data stays the C struct deque of
struct delay_data that obs-output.c frees; it is pushed, peeked and
popped in place with obs_util::ffi::deque (obs-rust#1561). Held packets are
libobs's reference-counted copies (obs_encoder_packet_create_instance,
hidden in libobs) and go back through obs_encoder_packet_release. Each
entry point reads os_gettime_ns() once and calls an *_at function with
it, which the parity test drives with a test clock.

The file leaves libobs.core-output for its own group,
libobs.ported-output-delay, whose PCH is obs-internal.h alone, so the
oracle and test_output_delay compile it without the core. The four
hidden symbols go in rust-hidden.txt, rust-exports.map and
rust-unexports-macos.txt; the three exported ones in
rust-exports.d/obs-output-output_delay.txt. The exported symbol sets are
unchanged.

Tier 3 (oracle/output_delay.c) compares the layouts of encoder_packet,
encoder_packet_time, delay_data and obs_output_delay_fields, the values
the file uses, the characterization scenarios, and random sequences of
packets, starts, stops, cleanups, setter and getter calls (null outputs
too), flag, result and clock changes: after every step the return value,
the recorded calls and whether the lock was held, the log line, the
packet copies, every delay field, the deque state and every queued
entry. Both sides go through the same accessor and libobs stand-ins. No
intentional differences. test_output_delay compiles the C into itself
and is unchanged.

Fixes obs-rust#1546
@rdoupe
rdoupe requested a review from zackees as a code owner October 20, 2026 22:01
@zackees
zackees enabled auto-merge October 20, 2026 22:12
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 obs-output-delay.c to obs-output

2 participants