Repository navigation
libobs: Port the output interleaver to obs-output - #1603
Merged
Merged
Conversation
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/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
zackees
enabled auto-merge
October 21, 2026 05:11
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Oct 21, 2026
Member
|
Coordinator: the merge queue ejected this (run 39549304820, Tier 2 Linux tests ON shard 2/2). |
6 of 7 tasks
chriscoveries
pushed a commit
to chriscoveries/obsrust-fork
that referenced
this pull request
Oct 21, 2026
…1606) obs-rust#1603 ports obs-output-interleave.c.h to rust/obs-output, and its deliberate-break check showed that the tests run through a real libobs catch only the catch-up limit (test_output_interleave_restart). test_output_interleave compiles the C file into itself, so with the premature prune made non-strict (>=) or the video-before-audio insertion rule removed in the Rust core, every real-libobs test still passed against libobs-with-rust; only Tier 3 caught them. test_output_interleave_e2e drives an encoded output with one video and two audio tracks, all registered in the test, through obs_startup() and real core video, and feeds the media by hand as the restart test does, so nothing depends on the wall clock. It checks what the output's encoded_packet callback receives: the first packet is a video keyframe at dts 0 and each audio track starts after it, near 0; the dts never decrease across the tracks, with video before audio at an equal dts; audio exactly one frame after the first keyframe keeps it, and audio two frames after drops it, so the output starts at the next keyframe (as test_output_interleave characterizes); and the same output, stopped, receives nothing and starts afresh the same way. About 2 s. Refs obs-rust#1594
This was referenced Oct 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1594 (PR 4 of the plan; #1595 moved the interleaver into
libobs/obs-output-interleave.c.hand added the characterization testtest_output_interleave, #1599 the hidden accessors and wrappers this port reads the output and its encoders through, #1596 and #1600 the Rustutil/darray.hoperations it uses in place)What and why
This PR ports
libobs/obs-output-interleave.c.h, the encoded packet interleaver, to theinterleavemodule ofrust/obs-output(#1567). It is the encoded packet callback of an output with both video and audio.interleave_packetsholds the encoders' packets in one queue sorted bydts_usecuntil every track has started. It then picks the start where the first video and audio are closest, shifts every track to start at 0, and sends the front of the queue once a packet of the opposing type is ahead of it. Each sent video packet gets its encoder timing and, when one is due, a caption. The function also checks that grouped encoders emit their keyframes together.interleave_packets,get_encoder_indexandfree_packetsare declared inobs-internal.hwithoutEXPORT.obs-output.ccalls them, and passesinterleave_packetstoobs_encoder_start()/obs_encoder_stop()(or to the delay as itsdelay_callback) by name. The encoder finds its callback again by comparing the function pointer, so the Rust symbol replaces the C one exactly, asprocess_delaydoes.libobs-with-rustdrops the C file and links the Rust shims.src/interleave.rs), in two layers:PacketViewtrait: kind, track, dts, pts, dts_usec, keyframe, time base, encoder). They cover the first and last packet of a track andinterleaved_start_idx(closest audio, earlier on a tie, the video if first, the move back for priming audio).premature_prunereturnsNoVideo,MissingAudio,KeeporThrough(idx), withc_value()giving the C's -1/0/idx. The rest:get_encoder_duration,insert_position,apply_offsetandpacket_dts_usec,Highest::has_higher_opposingandstreamable, the catch-up rulesends_for,discard_count, the caption timing (frame_timestamp,caption_due,raw_caption_due),find_packet_time,encoder_slotandgroup_complete.interleave_packets,send_interleaved,initialize_interleaved_packets,resort_interleaved_packets,check_encoder_group_keyframe_alignment, ...) are generic over anInterleaveOutputtrait, as the delay core is overDelayOutput. The trait covers the fields, the queue and the other darrays,interleaved_mutex,pkt_callbacks_mutexand the caption mutex, the encoders and the libobs calls. The locks are taken exactly where the C takes them: oneinterleaved_mutexper packet (released before a waiting delayed packet is released), the caption mutex around the caption decision, andpkt_callbacks_mutexaround the callbacks.PacketTime,KeyframeGroup), so the core runs on the C structs in the shim and on plain structs in Tier 1. No hostcfg, nounsafe.group_completeis pure. The plan's "start plan" isinitialize_interleaved_packetsitself, built fromget_audio_and_video_packets,interleaved_start_idxandapply_offset: the C reads the first packets again after the second discard, which a precomputed plan cannot do.get_encoder_indexgoes throughencoder_slot(anOption) and keeps the C'sassert(false)as adebug_assert, then 0, as a build without asserts does.test_output_interleavemarks "characterized, not endorsed" are kept:get_encoder_durationtruncates (20480 us, not 21333 us);src/ffi/output_interleave.rs):#[repr(C)]keyframe_group_data,packet_callback,caption_text(its first two fields and the text offset),obs_output_interleave_fields,obs_output_caption_track_fieldsandobs_encoder_interleave_info, with layout tests;encoder_packetandencoder_packet_timeare the delay shim's.struct obs_outputorstruct obs_encoder. It goes throughobs_output_interleave_fields(),obs_output_caption_track_fields(),obs_encoder_interleave_info(),obs_encoder_group_num_started()and theobs_output_interleave_*wrappers ofactive(), the two first-encoder-index helpers andadd_caption()(libobs: Read the output interleaver fields through hidden accessors #1599), and reads and writes each field where the C does.interleaved_packets,encoder_packet_times[]andkeyframe_group_trackingstay the C darrays thatobs-output.cresets and frees. They are inserted into, erased, popped and pushed in place withobs_util::ffi::darray(test/cmocka: Characterize the util/darray.h operations the interleaver uses #1596, obs-util: Add the darray.h operations the interleaver uses #1600). The resort moves the queue header out and frees the old buffer after the reinsertion, as the C does. Queued packets are libobs's reference-counted copies (obs_encoder_packet_create_instance, hidden in libobs; it resolves insidelibobs-with-rustas for the delay) or the delay's copy.interleave_packets_with_clockand thestaticsmodule (the C file's static functions on a live output) are Rust-mangled (hidden by_ZN*), and the parity test drives them with a test clock.rust-hidden.txt,rust-exports.mapandrust-unexports-macos.txt. None isEXPORT, so there is norust-exports.dfragment. In bothlibobs.sobuildsnmshows all three ast.obs-output-interleave.c.hleaveslibobs.core-outputfor its own group,libobs.ported-output-interleave, listed insource-sets.json(LIBOBS_PORTED_SOURCES). Its PCH,libobs-ported-output-interleave.pch.h, isobs-internal.halone: since libobs: Read the output interleaver fields through hidden accessors #1599 the file needs nothing else. The comment intests.d/test_output_interleave.cmakethat named the old PCH is updated; the test and its sources are unchanged.oracle/output_interleave.c):os_gettime_nswith#defines, includes the original file, and addsoracle_output_interleave_*wrappers around ten of its static functions. Everything else it calls keeps its libobs name and is defined by the test.GCC diagnosticpush/ignore/pop for-Wpedantic(__FUNCTION__),-Wsign-conversion(the file's implicit int/size_t conversions) and-Wformat(asize_tprinted with%lu, which mingw rejects), none of which libobs warns about. The file is compiled unmodified.NDEBUG, as libobs is. One of the file's asserts is reachable:discard_to_idx()pops the packet time of each video packet it drops, anddarray_pop_front()asserts there is one, then does nothing. A video packet that came without a packet time hits it in an assert-enabled build. The Rust darray does nothing there too.OBS_ENCODER_VIDEOreads as audio everywhere;test_output_interleave(30 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.RED → GREEN
6c7610309libobs: Add Tier 1 tests for the output interleaver port. The core istodo!().soldr cargo test -p obs-data --test obs-tests-core output::interleave::fails 53 of 53 with "not yet implemented".tests/output/interleave.rs) mirrorstest_output_interleave1:1: the same thirty names and assertions, in order. It runs on a model output that records what the C test's stubs record: each sent packet with whetherinterleaved_mutexwas held, each packet callback with its packet time and whetherpkt_callbacks_mutexwas held, eachadd_captionwith whether the caption mutex was held, and every held copy until it is released.dts_usecand encoder-duration truncation;pirper callback, and a start without video encoders.obs-tests-core. No new test binary:ci/test_binary_cap.pypasses (8 of 8).dc56bf082libobs: Port the output interleaver to obs-output.output::passes 98/98 (40 delay, 53 Tier 1, 5 Tier 2/3), and the wholeobs-tests-corebinary passes 338/338.tests/output/interleave_parity.rs). Both sides run on a test output through the same stand-ins: the four accessors, the fourobs-output.cwrappers, the output'sencoded_packet, its packet callbacks and the two name getters, plus the delay parity's tracked packet copy and release (one definition per test binary).interleaved_mutex,pkt_callbacks_mutexand the caption mutex were held.add_captionand the callbacks change the packet (and the callbacks the packet time), so a side that passed a copy instead of its packet would show.struct darrayand the four darray typedefs, plus the nine values the file uses.active, the delay (the packet is then the delay's held copy), the batch limit, the captions, theadd_captionresult, the callbacks and the encoder group; restarts (reset_packet_data()thenfree_packets);get_encoder_index; and ten static functions called directly where their C preconditions hold.pirbetween twoos_gettime_ns()readings.diff > duration_usecmade>=: 3 tests failed, 2 Tier 1 (premature_audio_prunes_through_it,premature_prune_widens_to_the_audio_frame) andparity_random_sequences;one_packet_per_call_then_catch_up,catch_up_rule) and both parity runs;insertion_order,resort_reinserts_and_raises_highest,start_offsets_and_resort, ...) and all three parity runs that drive the shim.libobs-with-rustrebuilt for each mutant, the 48 output, delay, encoder, mux, caption, ffmpeg, record, remux and stream cmocka tests in mode ON caught only the catch-up mutant (test_output_interleave_restart, which checks that limit). The prune and insertion mutants passed 48/48: no real-libobs test starts an output with audio exactly one frame after the keyframe, or queues video and audio at the samedts_usec.test_output_interleavecompiles the C itself and cannot see the swap. So the start and the ordering are proven by Tier 3, as the issue planned; step 5 of the plan (an end-to-end multi-track start through real libobs, like test: Run the output stream delay end to end through libobs #1578) is the follow-up for them.output::(Tier 1 and 3, both proptests included) passes 98/98 under AddressSanitizer (-Zsanitizer=addresson the Rust side; the C oracle is uninstrumented but allocates through the ASan allocator), with no reports. The first run found a use-after-free in the parity test itself, not in the port: with a delay, the caller's packet shares its data with the queued copy, which the same call may send and release, and the snapshot read the caller's bytes. The snapshot now reads them only while they are held.ENABLE_RUST_LIBOBS=OFFandON). Each tree runs the full cmocka suite againstlibobs(OFF) and againstlibobs-with-rust(ON):ENABLE_RUST_LIBOBS=OFF)ENABLE_RUST_LIBOBS=ON)test_video_io_hiddenin the Rust lane againstlibobs, was the private Xvfb display going away (XOpenDisplay failed during hotkey initialization, soobs_startupfailed). It does not use the interleaver, and it passed three of three reruns. No test was skipped or not run. master on the same trees: 348/348 in all four runs, the same symbol sets.test_output_interleave,test_output_interleave_restart,test_output_delay,test_output_delay_e2eandtest_output_delay_restartpass in all four runs.libobs.ported-output-interleave+.h.cis linked intolibobsand not intolibobs-with-rust.test_output_delay_e2e), both lanes were rebuilt and the 48 output, delay, encoder, mux, caption, ffmpeg, record, remux and stream tests rerun OFF and ON (48/48 each), with the same symbol result, andoutput::98/98.CMocka_DIR, ctest ran withLD_LIBRARY_PATHpointing at itslib,.test_core_module_load, which clearsLD_LIBRARY_PATH, finds that cmocka through its build RUNPATH and passed in every run.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 forx86_64-pc-windows-msvcandaarch64-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-runlink forx86_64-pc-windows-gnu): passed (obs-tests-core338/338, the obs-util harness 740/740 includingexport_list). Its first run failed the mingw link: the original file prints asize_twith%lu, which mingw's-Wformatrejects under the oracle's-Werror(libobs builds it with MSVC there). The oracle's pragma now also ignores-Wformat; the shim passes the track as asize_tfor that%lutoo, as the C does.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.Checklist
master(or the description starts with a Stacked: note)test_output_interleavelanded in libobs: Move the output packet interleaver into obs-output-interleave.c #1595)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): not applicable (the oracle's-Wpedantic/-Wsign-conversion/-Wformatpragma covers one include in one test-only file)