Repository navigation
libobs: Move the output packet interleaver into obs-output-interleave.c - #1595
Merged
Merged
Conversation
Behavior-neutral extraction before the Rust port: the encoded packet interleaver, interleave_packets() and the functions only it uses (get_encoder_index through has_higher_opposing_ts, send_interleaved through apply_ept_offsets and count_streamable_frames, plus free_packets), moves verbatim from libobs/obs-output.c.h into libobs/obs-output-interleave.c.h, with DEBUG_STARTING_PACKETS and its #undef. Everything stays static and unchanged. The new file joins libobs.core-output just before obs-output.c.h (alphabetical), so it is in the same translation unit as its callers; it declares the four obs-output.c helpers it calls (active, the two first-encoder-index helpers and add_caption), which obs-output.c.h defines after it. No signature, header or exported symbol changes. add_caption() and extract_buffer_from_sei() stay in obs-output.c.h: the caption path is its own unit, and send_interleaved() only decides when to call it. reset_packet_data() and calculate_batch_size() also stay: data capture start calls them. New characterization test test_output_interleave (all platforms, plus the Cargo C-test manifest). It compiles the moved file with a zeroed struct obs_output and stubs, as test_output_delay does, and pins the encoder index and received flags, the keyframe wait and the discard of the packets below it, the start index (closest audio, priming audio), the premature prune (strict, through the latest first audio, widened to the audio frame with several audio tracks), the dts offsets, insertion order and resort, sending and the catch-up limit, packet times, the caption decision, the encoder group keyframe check and free_packets(). Characterized, not endorsed: audio more than one frame after the first keyframe drops that keyframe and the start waits for the next one; the premature prune uses the last audio track's difference, not the largest; get_encoder_duration() truncates before multiplying by the frame size; the start index follows video track 0 only and can drop another track's only packet, and a premature prune can start a track on a non-keyframe; a waiting non-keyframe discards other tracks' queued packets; an audio offset is taken only from a dts > 0; a delayed packet that reaches an inactive output is not released. Refs obs-rust#1594
zackees
enabled auto-merge
October 21, 2026 01:44
zackees
approved these changes
Oct 21, 2026
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.
Refs #1594 (PR 1 of the plan: the extraction and the characterization; the accessors, the darray operations and the port follow)
What and why
Behaviour-neutral extraction before the Rust port, as the testing policy asks for part of a large file: the encoded packet interleaver moves verbatim from
libobs/obs-output.c.hintolibobs/obs-output-interleave.c.h. The move comes with a characterization test,test_output_interleave. No Rust, no signature or behaviour change, and no header change.What moves:
interleave_packets(), the encoded packet callback of an output with both video and audio;get_encoder_indexthroughhas_higher_opposing_ts, andsend_interleavedthroughinterleave_packets(the start index, the premature prune, the find helpers, the start, insertion and resort, the discard of unused audio, the encoder group keyframe check,apply_ept_offsets,count_streamable_frames);free_packets();DEBUG_STARTING_PACKETS, with its#undefmoved to the end of the new file.All of it stays
static. The moved lines are byte-for-byte the same:obs-output.c.hloses 709 lines, and the new file is those lines plus the license header, the include and the four declarations below.Not moved:
add_caption()andextract_buffer_from_sei(): the caption path is its own unit, andsend_interleaved()only decides when to call it;reset_packet_data()andcalculate_batch_size(): they reset interleaver state, but data capture start calls them, and they move only with the accessors.Build: the new file joins the existing
libobs.core-outputgroup just beforeobs-output.c.h(alphabetical), so it stays in the same translation unit as its callers and keeps internal linkage.obs-output.chelpers it calls, whichobs-output.c.hdefines after it:active(),get_first_audio_encoder_index(),get_first_video_encoder_index()andadd_caption().libobs.pch.h, like every member of that group. No group, PCH,source-sets.jsonor CMake source list changes.obs-buildbuilds the same group file, so the Rust lane needs no change either.Exported symbols: unchanged. Everything moved is
static.New characterization test
test_output_interleave, registered with atests.d/fragment (all platforms) and the Cargo C-test manifest. The functions are static, so the test compiles the moved file into itself, astest_output_delayandtest_scene_blenddo. That means it characterizes the C source only; Tier 3 will be the proof of the swap.struct obs_output. Only the fields the file reads are set up: two video and two audio encoders (used one or two at a time), a caption track per video track, a packet callback, andhighest_video_ts[]atINT64_MINasreset_packet_data()leaves it.active()andadd_caption()are recording stubs, and the two first-encoder-index helpers are copies.encoded_packet()and the packet callback record every sent packet. They also record whetherinterleaved_mutex,pkt_callbacks_mutexor the caption mutex was held at the time.obs_encoder_packet_create_instance()is hidden, so the test has a copy of it. Packets are released with libobs'sobs_encoder_packet_release(). Teardown checksbnum_allocs(), so a packet the interleaver fails to release fails the test.The 30 cases:
dts_usec, other tracks' video and their packet times included;>) prunes through the latest first audio;received_audio);get_encoder_duration()).highest_audio_tsis lowered by the first audio'sdts_usec, then raised by the resort;dts_usecis recomputed as dts * 1e6 / den.dts_usec; at equaldts_usec, video before audio, a lower video track first, and a new packet of the same track before the queued one;interleaver_max_batch_sizeare left;pirset, and is taken out of the array; otherwise NULL and a debug line;total_framescounts video;encoded_packet()runs withinterleaved_mutexheld, the callbacks withpkt_callbacks_mutexheld.dts_usec, whatever its type, each video packet with its track's oldest packet time.Characterized, not endorsed (comments in the test):
prune_start2) drops that keyframe with it. The start then fails, and the frames after it are dropped as non-keyframes, so the output starts at the next keyframe, here one second later. This is the late start a stream delay hits;max_diffis computed but not used);get_encoder_duration()truncates 1e6 / 48000 to 20 before multiplying by the frame size, so the widened limit is 20480 us, not 21333 us;delay_activeanddelay_capturingare set, and the stop path clears both beforeactive.These are upstream behaviours, not regressions. No bug filed.
RED → GREEN
86ac531d5libobs: Move the output packet interleaver into obs-output-interleave.c.test_output_interleave: 30/30, run 3 times in each lane against both libraries, all passing. Also 30/30 when built with ASan and UBSan (leak detection on), with no reports.obs-output-interleave.c.hfail it. Each one was compiled into the test against the unchanged libobs. The mutants:>=for>; the largest difference instead of the last; throughmax_idx; no widening;received_audionot cleared; no truncation inget_encoder_duration;<=on a tie; video index ignored; priming< 0; no priming move;<=; video offset from dts; audio offset from any non-zero dts;highest_audio_tsnot lowered; no second discard;received_videonot cleared;dts_usecnot recomputed; no packet-time offsets at the start;<=for<; resort without raising the highest timestamps;>for>=; no packet-time pop;>=; no catch-up; highest not raised after the start;>=in the opposing test; own track counted; audio needing higher audio; video highest lowered; packet time matched on!=; no frame count; nopir;<for<=; display duration subtracted; raw data<=;>=stops the walk; "skipped" not marked;free_packets: no release; noda_free.ENABLE_RUST_LIBOBS=OFFandON, full builds). Each runs the full cmocka suite againstlibobsand againstlibobs-with-rust, headless (private Xvfb, private D-Bus, fresh HOME):ENABLE_RUST_LIBOBS=OFF)ENABLE_RUST_LIBOBS=ON)Nothing was skipped or failed to run. These runs are on master with #1572 and #1578; a final rebase onto four CI and docs commits changed no C, and the lint lane passed again on it. The export lists are also identical across the two lanes, with the same count as master.
test_output_interleave_restart,test_output_delay,test_output_caption_queue,test_output_caption_textand thetest_ffmpeg_output_*tests pass in all four, unchanged.uv run ci/local_gate.py --lane lint: passed.--lane cross: passed. clang-format 23.1 and gersemi are clean on the new files, andci/test_build_groups.pypasses (group order, the#undefrule).Windows/macOS: not built there. The moved code has no host conditionals of its own. The test is portable C and builds against
libobs.pch.hwith the same includes and libraries astest_scene_blend. The one case that depends onassert()being compiled out is under#ifdef NDEBUG.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): built with scripting ON (a flag change under cmake/ or build-aux/ runs the full builds) and cross-checked with clang and MSVC (not applicable: no flag change)