Repository navigation
Description
Unit
Port the encoded packet interleaver of libobs/obs-output.c.h to the existing crate rust/obs-output (#1567). That is interleave_packets, the encoded packet callback of an output with both video and audio, and the static functions only it uses:
- the encoder index and the received flags:
get_encoder_index,check_received; - the dts offsets:
apply_interleaved_packet_offset,apply_ept_offsets; - the start:
get_interleaved_start_idx,get_encoder_duration,prune_premature_packets,discard_to_idx,prune_interleaved_packets,get_audio_and_video_packets,initialize_interleaved_packets; - the queue: the
find_first/last_packet_type(_idx)helpers,insert_interleaved_packet,resort_interleaved_packets,discard_unused_audio_packets,free_packets; - sending:
has_higher_opposing_ts,set_higher_ts,count_streamable_frames,send_interleaved(with its caption timing and packet-time lookup); purge_encoder_group_keyframe_dataandcheck_encoder_group_keyframe_alignment.
About 700 lines. The caption path itself (add_caption, extract_buffer_from_sei) is not part of the unit and stays C: send_interleaved only decides when to call it. reset_packet_data and calculate_batch_size also stay in obs-output.c.h, because data capture start calls them.
The unit reads and writes these struct obs_output fields: interleaved_packets (a darray of struct encoder_packet), interleaved_mutex, received_audio, received_video[], video_offsets[], audio_offsets[], highest_audio_ts, highest_video_ts[], video_encoders[], audio_encoders[], encoder_packet_times[], keyframe_group_tracking, pkt_callbacks and pkt_callbacks_mutex, caption_tracks[] (head, timestamps, raw data size and mutex), total_frames, interleaver_max_batch_size, active_delay_ns, active, info.encoded_packet, context.data and context.name. From each encoder it reads timebase_num, timebase_den, framesize and encoder_group (with num_encoders_started under the group's mutex). It calls active(), get_first_audio_encoder_index(), get_first_video_encoder_index() and add_caption() (static, in obs-output.c.h), obs_encoder_packet_create_instance() (hidden), obs_encoder_packet_release(), obs_output_get_name(), obs_encoder_get_name() and os_gettime_ns().
Phase and position
Phase 6 (#444), libobs core through opaque seams. It joins rust/obs-output, which #1567 created for obs-output-delay.c.h. obs-output.c is one of our modified files. The unit is part of a large file, so it enters through function extraction (testing-policy.md, "Extracting a function before porting it"), as #1521 did for the scene blend table. Its tests join the existing core category binary, obs-tests-core; no new test binary.
Dependencies and existing tests
- No cmocka test covers the interleaver directly.
test_output_interleave_restartdrives it through real libobs, but only checks the catch-up limit.test_output_interleave(PR 1) is the characterization. Every function is static, so it compiles the moved file into itself with a zeroedstruct obs_output, astest_output_delaydoes. It therefore characterizes the C source only; Tier 3 is the proof of the swap, plus the existing end-to-end tests that run through the interleaver (test_output_interleave_restart, the caption and ffmpeg output tests) in mode ON. struct obs_outputis private and platform-sized, and Rust should not mirror it. The rest ofobs-output.c.hstays C.interleaved_packets,encoder_packet_times[]andkeyframe_group_trackingare C darrays thatobs-output.cresets and frees. The port uses them in place, soobs-util's Rust darray needs the inline operations the interleaver uses and does not have yet: insert, erase range, pop front and push back new. Each one needs Tier 1 and parity against oracle wrappers of the inline functions, as the existing darray functions have.- Layout contracts:
struct encoder_packetandstruct encoder_packet_time(already inobs-output's FFI),struct keyframe_group_data,struct packet_callback,struct caption_text(its first two fields),struct darray, and the new accessor struct. interleave_packetsis passed toobs_encoder_start()andobs_encoder_stop(), and the encoder finds its callback by comparing the function pointer. So the Rust symbol must replace the C one exactly, asprocess_delaydoes (libobs: Port the output stream delay to obs-output #1567).
Plan
-
Extraction and characterization PR (behaviour-neutral): the functions above move verbatim from
obs-output.c.hintolibobs/obs-output-interleave.c.h, still static, in the same unity group (libobs.core-output, just beforeobs-output.c.h). The new file declares the fourobs-output.chelpers it calls. It addstest/cmocka/test_output_interleave.c(30 cases): the start index and the premature prune (including audio that starts more than one frame after the keyframe, which drops that keyframe), the keyframe wait, the dts offsets, insertion order and resort, sending and the catch-up limit, the discard of unused audio, the received flags, the encoder group keyframe check andfree_packets. -
Accessor PR (behaviour-neutral, like libobs: Read the output delay fields through a hidden accessor #1550):
- a hidden
obs_output_interleave_fields()inobs-output.c.h, declared inobs-internal.h, that returns pointers to the fields above in one plain struct; - a hidden per-track caption accessor, and a hidden encoder accessor in
obs-encoder.c.h(timebase, frame size, group, and the group's started count read under its mutex); - the three entry points (
interleave_packets,get_encoder_index,free_packets) and the four helpers the interleaver calls (active, the two first-encoder-index helpers,add_caption) get hidden external linkage, declared inobs-internal.h. That way the interleaver file can leave the unity group.
obs-output-interleave.c.hthen reads the output only through these. A newobs_output_interleave_fields()is used, not an extension ofobs_output_delay_fields(): the two units share almost no fields, and each port keeps onerepr(C)struct of its own. Its test harness gets a stub source for the accessors in itstests.dfragment, as libobs: Read the output delay fields through a hidden accessor #1550 did. The four helper stubs intest_output_interleave.cchange to match (linkage, and names if they get a prefix). That is the only change to the test: no case or assertion changes. - a hidden
-
obs-utildarray: insert, erase range, pop front and push back new, with Tier 1 and parity against oracle wrappers. This can go before or alongside 2. -
Port PR:
rust/obs-output, moduleinterleave.- Safe core, generic over a packet view trait (kind, track, dts, pts, dts_usec, keyframe, timebase), so the core runs over a slice of the
repr(C)packets in the shim and of plain test packets in Tier 1. The pure parts:- first and last packet index of a track;
- the start index (closest audio, tie to the earlier one, the video index, the move back for priming audio);
- the premature prune decision (wait and clear
received_audio, keep, or drop through an index; strict>, the last audio track's difference, the widening to the longest audio frame with the truncatingget_encoder_duration); - the start plan (
initialize_interleaved_packets: the flags to clear, the second discard, the offsets, thehighest_audio_tsadjustment); - the insertion position and the resort order;
- the offset arithmetic (
packet_dts_usecfrom the offset dts); - the opposing-timestamp test, the streamable count and the catch-up rule (one, or two when more than the limit is left);
- the discard count below a dts;
- the caption timing decision and the packet-time lookup by pts;
- the encoder group keyframe tracking.
interleave_packetsitself is generic over an output trait (the lock, the flags, the queue and packet-time arrays, the offsets and highest timestamps, the encoders, sending), asobs-output's delay core is generic overDelayOutput.- Shim, over
struct obs_outputinternals through the accessors only: the darrays in place (withbmallocmemory),interleaved_mutex,pkt_callbacks_mutexand the caption mutex (libobspthread_mutex_lock), the packet callbacks andinfo.encoded_packet,obs_encoder_packet_create_instanceand release,add_caption(C), the debug and warning log lines. - Tests: Tier 1 ports every
test_output_interleaveassertion 1:1, plus edge cases. Layout tests. Tier 3 parity of the C file (oracle, with non-staticoracle_*wrappers around its static functions) against the shim, with a proptest over random multi-track packet sequences (types, tracks, keyframes, timestamps, packet times, delay on or off, active or not, batch limits). The C file moves to its own ported group. Tier 2 OFF/ON unchanged.
- Safe core, generic over a packet view trait (kind, track, dts, pts, dts_usec, keyframe, timebase), so the core runs over a slice of the
-
If the deliberate-break check of 4 shows that the existing end-to-end tests miss part of the swap, an end-to-end test of a multi-track start through real libobs, as test: Run the output stream delay end to end through libobs #1578 did for the delay.
No intended behaviour changes. The odd behaviour that test_output_interleave marks "characterized, not endorsed" is kept:
- audio that starts more than one video frame after the first keyframe drops that keyframe, and the output waits for the next one (the late start a stream delay hits);
- the premature prune uses the difference of the last audio track, not the largest;
get_encoder_duration()truncates before multiplying by the frame size (20480 us, not 21333 us, for 1024 samples at 48 kHz);- the start index follows video track 0 only, and can drop another audio track's only packet;
- a premature prune with more video queued starts a track on a non-keyframe;
- a waiting non-keyframe discards other tracks' queued packets, keyframes included;
- an audio offset is taken only from a dts > 0, so priming audio keeps its own timestamps;
- a delayed packet that reaches an inactive output is not released.
The last one looks unreachable today: the delay delivers only while delay_active and delay_capturing are set, and the stop path clears those before active. A fix for it, if one is needed, would be its own bug issue.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
rdoupe commentedon Oct 21, 2026
Claiming: #1594
libobs/obs-output-interleave.c.h(same unity group), with the characterizationtest_output_interleave(30 cases). Opening it now.obs_output_interleave_fields()and encoder/caption accessors), after 1 merges.darrayoperations inobs-util(insert, erase range, pop front, push back new).rust/obs-output'sinterleavemodule, as its own PR.libobs: Move the output packet interleaver into obs-output-interleave…
test/cmocka: Characterize the util/darray.h operations the interleave…
test/cmocka: Characterize the util/darray.h operations the interleave…
libobs: Read the output interleaver fields through hidden accessors (o…
obs-util: Add the darray.h operations the interleaver uses (obs-rust#…
libobs: Port the output interleaver to obs-output (obs-rust#1603)
test: Run the output interleaver end to end through libobs (obs-rust#…