Skip to content

libobs: Move the output packet interleaver into obs-output-interleave.c - #1595

Merged
zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:refactor/output-interleave-move
Oct 21, 2026
Merged

zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:refactor/output-interleave-move

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

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.h into libobs/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;
    • every function only it uses: get_encoder_index through has_higher_opposing_ts, and send_interleaved through interleave_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 #undef moved to the end of the new file.

    All of it stays static. The moved lines are byte-for-byte the same: obs-output.c.h loses 709 lines, and the new file is those lines plus the license header, the include and the four declarations below.

  • Not moved:

    • add_caption() and extract_buffer_from_sei(): the caption path is its own unit, and send_interleaved() only decides when to call it;
    • reset_packet_data() and calculate_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-output group just before obs-output.c.h (alphabetical), so it stays in the same translation unit as its callers and keeps internal linkage.

    • It declares the four obs-output.c helpers it calls, which obs-output.c.h defines after it: active(), get_first_audio_encoder_index(), get_first_video_encoder_index() and add_caption().
    • Its precompiled header is libobs.pch.h, like every member of that group. No group, PCH, source-sets.json or CMake source list changes. obs-build builds 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 a tests.d/ fragment (all platforms) and the Cargo C-test manifest. The functions are static, so the test compiles the moved file into itself, as test_output_delay and test_scene_blend do. That means it characterizes the C source only; Tier 3 will be the proof of the swap.

  • The output is a zeroed 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, and highest_video_ts[] at INT64_MIN as reset_packet_data() leaves it.
  • active() and add_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 whether interleaved_mutex, pkt_callbacks_mutex or 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's obs_encoder_packet_release(). Teardown checks bnum_allocs(), so a packet the interleaver fails to release fails the test.
  • Time base: video 1/30 (33333 us a frame), audio 1/48000 with 1024-sample frames. No libobs core, no display and no real time, so it runs on every platform.

The 30 cases:

  • encoder index and received flags:
    • the packet's index is its encoder's slot in the video or audio array, written into the caller's packet;
    • before the start, each video track sets its own flag and any audio track sets the one audio flag;
    • an inactive output ignores the packet.
  • keyframe wait:
    • a first video packet that is not a keyframe is dropped, and so is every queued packet below its dts_usec, other tracks' video and their packet times included;
    • after the keyframe, non-keyframes queue;
    • with a delay running, the packet is taken, not copied, and a dropped one is released.
  • start index:
    • the audio closest to track 0's first video, the earlier one on a tie, or the video if it is first;
    • priming audio (pts <= 0) moves the start back to every audio track's first packet.
  • premature prune:
    • more than one frame between the first video and the first audio (strict >) prunes through the latest first audio;
    • -1 without track 0 video, or with an audio track empty (that one also clears received_audio);
    • with several audio tracks the limit widens to the longest audio frame (get_encoder_duration()).
  • start:
    • it waits for every track;
    • it needs audio past the first video;
    • each track gets its own offset: the video one from the first pts, the audio one from the first dts if > 0;
    • highest_audio_ts is lowered by the first audio's dts_usec, then raised by the resort;
    • packet times queued before the start get the video offset, and later ones get it as they come;
    • the first packet is sent at once.
  • dts offsets: dts, pts and the packet time are offset, and dts_usec is recomputed as dts * 1e6 / den.
  • insertion order and resort:
    • by dts_usec; at equal dts_usec, video before audio, a lower video track first, and a new packet of the same track before the queued one;
    • audio after everything equal;
    • the resort re-inserts into a new array and only raises the highest timestamps.
  • sending:
    • a packet is streamable while the opposing types are strictly ahead (audio: every video track; video: the other video tracks and the audio);
    • one is sent per packet, and a second one only when more than interleaver_max_batch_size are left;
    • the matching packet time (by pts) goes to the packet callback with pir set, and is taken out of the array; otherwise NULL and a debug line;
    • total_frames counts video;
    • a caption is added once its time has come, under the track's caption mutex, and the next one waits for its display duration; raw CEA-608 data is added once per later frame time;
    • encoded_packet() runs with interleaved_mutex held, the callbacks with pkt_callbacks_mutex held.
  • discard of unused audio: everything below the dts_usec, whatever its type, each video packet with its track's oldest packet time.
  • encoder group keyframes: an entry per group keyframe until every started encoder of the group has had one; a track that skipped one gets the "Missing keyframe" warning; entries are kept in pts order.
  • free_packets: releases every queued packet and frees the array, nothing else.

Characterized, not endorsed (comments in the test):

  • audio that starts more than one video frame after the first keyframe (prune_start 2) 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;
  • the premature prune decides on the difference of the last audio track checked, not the largest (max_diff is 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;
  • priming audio on one track also keeps another track's audio from 100 ms before the video;
  • the start index follows video track 0 only. It can drop another audio track's only packet, and the start then waits;
  • a premature prune with more video queued starts a track on a non-keyframe;
  • a waiting non-keyframe discards other tracks' queued packets, their keyframes included, but leaves their received flags set;
  • a track 0 keyframe marks its own pending group entry "skipped" over "seen", without a warning;
  • an audio offset is taken only from a dts > 0, so priming audio keeps its own negative timestamps;
  • a delayed packet that reaches an inactive output is not released. As far as I can see this is not reached: the delay delivers only while delay_active and delay_capturing are set, and the stop path clears both before active.

These are upstream behaviours, not regressions. No bug filed.

RED → GREEN

  • RED commit: none. This is a move plus a characterization test, which passes on the moved code. 86ac531d5 libobs: 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.
  • Mutation check of the new test: 53 of 53 mutants of obs-output-interleave.c.h fail it. Each one was compiled into the test against the unchanged libobs. The mutants:
    • prune: >= for >; the largest difference instead of the last; through max_idx; no widening; received_audio not cleared; no truncation in get_encoder_duration;
    • start index: <= on a tie; video index ignored; priming < 0; no priming move;
    • start: audio past video <=; video offset from dts; audio offset from any non-zero dts; highest_audio_ts not lowered; no second discard; received_video not cleared;
    • offsets: pts not offset; packet time not offset; dts_usec not recomputed; no packet-time offsets at the start;
    • insertion: track order reversed; video not before equal audio; <= for <; resort without raising the highest timestamps;
    • discard: > for >=; no packet-time pop;
    • keyframe wait: non-keyframes accepted; queue not discarded; a delayed packet not released; a delayed packet copied;
    • received flags: not set; set on track 0 only. Encoder index: audio always slot 0. Inactive check removed;
    • sending: catch-up >=; 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; no pir;
    • captions: < for <=; display duration subtracted; raw data <=;
    • group keyframes: never purged; no warning; >= stops the walk; "skipped" not marked;
    • free_packets: no release; no da_free.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON, full builds). Each runs the full cmocka suite against libobs and against libobs-with-rust, headless (private Xvfb, private D-Bus, fresh HOME):
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 347/347 347/347 IDENTICAL (1766)
Rust (ENABLE_RUST_LIBOBS=ON) 347/347 347/347 IDENTICAL (1766)

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_text and the test_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, and ci/test_build_groups.py passes (group order, the #undef rule).
  • Real app (UI, protocol output or logging changes): not applicable (no behaviour change).

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.h with the same includes and libraries as test_scene_blend. The one case that depends on assert() being compiled out is under #ifdef NDEBUG.

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 (this is the extraction with its characterization; the port follows)
  • 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): 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)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): not applicable (no port here)

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
@rdoupe
rdoupe requested a review from zackees as a code owner October 21, 2026 01:43
@zackees
zackees enabled auto-merge October 21, 2026 01:44
@zackees
zackees added this pull request to the merge queue Oct 21, 2026
Merged via the queue into obs-rust:master with commit 69cc994 Oct 21, 2026
25 checks passed
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