Skip to content

obs-util: Add the darray.h operations the interleaver uses - #1600

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/darray-ops
Oct 21, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/darray-ops

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Refs #1594 (step 3, part 2 of 2; part 1, #1596, added the characterization test test_darray_ops)

What and why

The interleaver port (#1594, step 4) uses three C darrays in place: interleaved_packets, encoder_packet_times[] and keyframe_group_tracking. obs-output.c resets and frees all three. From Rust, the port has to insert into those arrays, erase ranges, pop from the front and push new items, with the same semantics as the C inlines. obs-util's darray equivalent had push back array, erase, pop back and free, but none of these. libobs/util/darray.h is header-inline, so nothing swaps: the Rust equivalent is a library that ports call, and the struct layout is the contract (testing-policy.md, "Header-inline code"). The C inlines and libobs are unchanged.

  • Raw API (rust/obs-util/src/ffi/darray.rs):
    • darray_insert, darray_erase_range, darray_pop_front and darray_push_back_new, plus darray_push_back, which an insert at num calls. They have their C signatures and are plain unsafe fns.
    • darray_push_back_new covers both the function and the GCC/Clang da_push_back_new macro. The two take the same steps.
    • They work in place on a struct darray with bmem memory and follow the header step by step. So C and Rust can hand one array back and forth: C's darray_free frees what Rust allocated, and Rust grows what C allocated.
      • num grows before the capacity check.
      • Growth copies the whole old capacity.
      • Removal moves the tail down and leaves the old values in the slots past num (characterized, not endorsed).
      • push_back_new zeroes only the new item.
    • An index or range the header asserts on panics, because the NDEBUG behaviour there is undefined.
    • pop_front on an empty array does nothing, like the existing pop_back: the C body checks num again after its assert.
    • Nothing is exported (no #[no_mangle]), so rust-exports.d, rust-hidden.txt and the export maps are unchanged.
  • Safe core (rust/obs-util/src/darray.rs): the same four operations on DArray: insert, erase_range, pop_front and push_back_new (returns &mut T, zeroed).
  • Oracle (rust/obs-c-oracle/oracle/darray.c, src/darray.rs):
    • non-inline oracle_* wrappers of the five inline functions;
    • oracle_da_push_back_new, the macro itself on a typed DARRAY that aliases the array, for 1-, 4-, 12- and 16-byte items.
  • Intentional differences: none.
  • Docs: the rust/README.md darray row. testing-policy.md already lists util/darray.h as header-inline.

test_darray_ops (14 cases) and test_darray are unchanged. The functions are static inline, so those tests check the C header in both libobs builds and cannot see the Rust side. Tier 3 is the proof.

RED → GREEN

  • RED commit: 16c8ef92f obs-util: Add Tier 1 tests for the darray.h interleaver operations. The new functions are todo!(). soldr cargo test -p obs-util --lib tests::containers::darray_ops fails 30 of 31 with "not yet implemented". layout passes, because it checks the existing struct mirror. The three should_panic cases fail on the wrong panic message.
    • Tier 1 (tests/containers/darray_ops.rs) ports every test_darray_ops case 1:1, under the same names without test_. The cases run on the raw API with bmem memory, as the C test runs on the header.
    • 11 new edge cases:
      • an insert at num into a full array, into a reserved empty array, and with a 3-byte item;
      • growth copying the stale tail;
      • push back new zeroing only the new item, and push back new on a zeroed array;
      • pop front on an empty array;
      • an erase range at the front that leaves one item;
      • the three precondition panics.
    • 6 safe_* cases run the same operations on the safe core, including its two panics.
    • The tests are a module of the existing obs-util harness (containers category). No new test binary.
  • GREEN: 76faf2273 obs-util: Add the darray.h operations the interleaver uses. tests::containers::darray passes 45/45: 31 darray_ops, 9 darray, 1 layout and 4 Tier 3. Tier 3 (darray_parity.rs):
    • darray_ops_match_c: a proptest over random sequences of up to 64 operations on two twins, the Rust raw functions and the C oracle, plus the safe core for one-byte items. Element sizes are 1, 4, 12 and 16 bytes.
      • The operations are every function on the Rust side: push back array, reserve, ensure capacity, resize, clear, erase, pop back, push back, push back new, insert, erase range, pop front and free. The C side of push back new uses the function or the da_push_back_new macro at random.
      • After every step it compares num, capacity, whether the buffer is null, each return value (the push back index, and the offset of the item push back new returns) and every determined byte.
      • The determined bytes include the stale slots past num that removal leaves, which survive growth. A reallocating reserve copies only num items, so the test stops comparing the slots past num after one. Other bytes are not compared, because C leaves new memory uninitialized.
    • darray_mixed_in_place_matches_c: one struct darray that each step hands to the Rust or the C functions at random, darray_free included, against a C-only twin. This is the interleaver's case.
    • interleaver_patterns_match_c: the test_darray_ops packet-time and resort patterns, run the same mixed way.
    • bmem_accounting_matches_c: the bnum_allocs() deltas of a hand-off in both directions, in its own process. C allocates and Rust grows and frees; Rust allocates and C grows and frees; removal on either side neither allocates nor frees.
  • Deliberate-break check, in the Rust raw functions, restored after each:
    • darray_insert writes the item at idx + 1: 6 Tier 1 tests and 3 of 4 Tier 3 tests failed. The proptests shrank to short sequences of one-byte items.
    • darray_erase_range zeroes the slots past the new num: 4 Tier 1 tests (removal_leaves_tail_bytes among them) and 3 of 4 Tier 3 tests failed.
    • darray_push_back_new zeroes one byte less: 2 Tier 1 tests and both proptests failed.
    • After the restore, 45/45 pass. The proptest regression files from the breaks were not kept.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON). Each runs the tests against libobs and libobs-with-rust, headless (private Xvfb, private D-Bus, fresh HOME). The machine's cmocka package had been removed, so the cmocka tests used an extracted copy of the same cmocka 2.0.2 package.
Lane test_darray test_darray_ops Exported symbols
C (ENABLE_RUST_LIBOBS=OFF), libobs / libobs-with-rust 1/1, 1/1 14/14, 14/14 IDENTICAL (1766, same as master)
Rust (ENABLE_RUST_LIBOBS=ON), libobs / libobs-with-rust 1/1, 1/1 14/14, 14/14 IDENTICAL (1766)
  • The export lists are identical across all four builds. test_darray_ops tests the C header, so this shows only that nothing around it moved.
  • 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, 1766 tests, none failed. The obs-util harness passes 740/740 (706 before, plus the 31 Tier 1 and 3 new Tier 3 tests here). No Rust test binary was added.
  • Real app: not applicable (no behaviour change, and nothing in libobs uses it yet).

Checklist

  • Base is master
  • 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_darray_ops landed in test/cmocka: Characterize the util/darray.h operations the interleaver uses #1596)
  • 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
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): none

The interleaver port (obs-rust#1594) uses the C darrays that obs-output.c resets
and frees (interleaved_packets, encoder_packet_times[] and
keyframe_group_tracking) in place, so obs-util's darray equivalent needs
the inline operations it uses and does not have yet: insert, erase
range, pop front and push back new (the function and the GCC/Clang
macro, which do the same steps). obs-rust#1596 characterized them in
test_darray_ops.

This adds those functions with todo!() bodies, to the raw API that runs
on a struct darray with bmem memory (plus darray_push_back, which insert
at num calls) and to the safe DArray, and Tier 1: every test_darray_ops
case ported 1:1 under the same names on the raw API, as the C test runs
the header, including the "characterized, not endorsed" stale slots
past num after removal. The bnum_allocs() cases run in their own
process. New edge cases: insert at num into a full array, into a
reserved empty one and with an odd item size, growth copying the stale
tail, push back new zeroing only the new item, pop front on an empty
array, the precondition panics (where C asserts), and the same
operations on the safe core.

30 of 31 fail with "not yet implemented"; layout passes, as it checks
the existing struct mirror.

Refs obs-rust#1594
The interleaver port (obs-rust#1594) uses interleaved_packets,
encoder_packet_times[] and keyframe_group_tracking in place: C darrays
that obs-output.c resets and frees. darray.h is header-inline, so
nothing swaps; the Rust equivalent is a library the port calls, and the
struct darray layout is the contract.

- obs_util::ffi::darray: darray_insert, darray_erase_range,
  darray_pop_front and darray_push_back_new (the function, and the
  GCC/Clang da_push_back_new macro, which does the same steps), plus
  darray_push_back, which insert at num calls. They follow the header
  step by step on a struct darray with bmem memory, so C and Rust can
  hand one array back and forth: num grows before the capacity check,
  growth copies the whole old capacity, removal moves the tail down and
  leaves the old values past num (characterized, not endorsed), and only
  the new item of push back new is zeroed. An index or range the header
  asserts on panics; pop front on an empty array is a no-op, as the C
  body checks num after its assert.
- obs_util::darray::DArray: the same four operations on the safe core.
- Oracle: oracle_* wrappers of the five inline functions, and
  oracle_da_push_back_new, the macro on a typed DARRAY for 1, 4, 12 and
  16-byte items.
- Tier 3: darray_ops_match_c now covers every Rust function on twin
  arrays (and the safe core for one-byte items), comparing num,
  capacity, the null-ness of the buffer, the return values and every
  determined byte, including the stale slots past num.
  darray_mixed_in_place_matches_c hands one array to Rust or C at random
  against a C-only twin; interleaver_patterns_match_c runs the
  test_darray_ops packet-time and resort patterns that way, and
  bmem_accounting_matches_c checks the bnum_allocs() deltas of a hand-off
  in both directions, in its own process.
- rust/README.md: the darray row.

No intentional differences.

Refs obs-rust#1594
@rdoupe
rdoupe requested a review from zackees as a code owner October 21, 2026 03:13
@zackees
zackees added this pull request to the merge queue Oct 21, 2026
Merged via the queue into obs-rust:master with commit b9cbc65 Oct 21, 2026
25 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 21, 2026
* libobs: Add Tier 1 tests for the output interleaver port

RED for the port of libobs/obs-output-interleave.c.h, the encoded packet
interleaver, to obs-output's new interleave module. The pure parts work
on a slice of packets (a PacketView trait): the first and last packet of
a track, the start index, the premature prune, the insertion position,
the offset arithmetic, the streamable count and the catch-up rule, the
discard count, the caption timing and the packet-time lookup. The
functions named after the C ones are generic over the output
(InterleaveOutput): its fields, the queue and the other darrays, the
locks, the encoders and the libobs calls the C file makes, so the C ABI
shim can run them on the real struct obs_output through the hidden
accessors of obs-rust#1599, and the tests on a model. Every function is a
todo!() stub.

The first thirty cases mirror test_output_interleave 1:1 (same names,
same assertions, in order) on a model output that records what the C
test's stubs record: each sent packet with whether interleaved_mutex was
held, each packet callback call with its packet time and whether
pkt_callbacks_mutex was held, each add_caption call with whether the
track's caption mutex was held, and every held copy until it is
released. The other 23 pin the edges: the dts_usec and encoder duration
truncation, the first and last packet of a track, the start index and
the premature prune without audio, the duration lookups stopping at the
missing track, the insertion position at equal times, the opposing-type
test without video encoders, the catch-up boundaries, the discard
count, the caption timing at equal times, the first matching packet
time, single-encoder and foreign group entries, one lock per packet and
none when inactive, a delayed waiting packet released after the unlock,
a packet time offset after the start, an audio packet's time going to
the same-numbered array, one pir per callback, and a start without video
encoders.

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::interleave::
fails 53 of 53 with "not yet implemented".

Refs obs-rust#1594

* libobs: Port the output interleaver to obs-output

libobs/obs-output-interleave.c.h moves to obs_output::interleave. It is
the encoded packet callback of an output with video and audio:
interleave_packets queues the encoders' packets, starts every track
together at timestamp 0 and sends the packets in dts order, with their
encoder timing, captions and the encoder group keyframe check.
interleave_packets, get_encoder_index and free_packets are declared in
obs-internal.h without EXPORT; obs-output.c calls them, and passes
interleave_packets to the encoders by name, so the Rust symbol replaces
the C one the encoder later compares against.

The pure parts work on a slice of packets (PacketView): the first and
last packet of a track, the start index, the premature prune, the
insertion position, the offset arithmetic, the streamable count and the
catch-up rule, the discard count, the caption timing and the packet-time
lookup. The functions named after the C ones are generic over the output
(InterleaveOutput), as the delay core is over DelayOutput, and take
interleaved_mutex, pkt_callbacks_mutex and the caption mutex exactly
where the C does. The nine behaviours test_output_interleave marks
"characterized, not endorsed" are kept, among them the late-audio
keyframe drop, the last-track prune difference, the truncating encoder
duration, the track-0-only start index, the non-keyframe start, the
waiting non-keyframe discard, priming audio at offset 0 and the early
return that does not release a delayed packet.

The shim never reads struct obs_output or struct obs_encoder: it goes
through the hidden accessors and wrappers of obs-rust#1599, and reads and writes
each field where the C does. interleaved_packets, encoder_packet_times[]
and keyframe_group_tracking stay the C darrays obs-output.c resets and
frees; they are changed in place with obs_util::ffi::darray (obs-rust#1596,
obs-rust#1600). Queued packets are libobs's reference-counted copies or the
delay's. The clock is read once per packet callback, as in C; the
*_with_clock entry point and the statics module (the C file's static
functions on a live output) are Rust-mangled and are what the parity
test drives.

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

Tier 3 (oracle/output_interleave.c) compares the layouts of the
keyframe group entry, the packet callback, the caption text, the darray
and the three accessor structs, the values the file uses, thirteen
characterization scenarios, and 1024 random runs of up to 80 steps over
one to two video and audio encoders: packets with random tracks,
keyframes, running timestamps and packet times, changes of active, the
delay, the batch limit, the captions, add_caption, the callbacks and the
encoder group, restarts, get_encoder_index, and the static functions
where their C preconditions hold. After every step it compares the
return value, every recorded call and whether each lock was held, the
log line, the held copies, the caller's packet, every queued packet,
packet time and group entry, the darray capacities and every
interleaver field. Both sides go through the same accessor and libobs
stand-ins. The oracle builds the file with NDEBUG, as libobs is built:
darray_pop_front asserts when a video packet without a packet time is
discarded, then does nothing, as the Rust darray does. No intentional
differences. test_output_interleave compiles the C into itself and is
unchanged.

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