Skip to content

rust: Port the obs-output.c interleaver to obs-output #1594

Closed

Description

@rdoupe

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_data and check_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_restart drives 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 zeroed struct obs_output, as test_output_delay does. 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_output is private and platform-sized, and Rust should not mirror it. The rest of obs-output.c.h stays C.
  • interleaved_packets, encoder_packet_times[] and keyframe_group_tracking are C darrays that obs-output.c resets and frees. The port uses them in place, so obs-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_packet and struct encoder_packet_time (already in obs-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_packets is passed to obs_encoder_start() and obs_encoder_stop(), and the encoder finds its callback by comparing the function pointer. So the Rust symbol must replace the C one exactly, as process_delay does (libobs: Port the output stream delay to obs-output #1567).

Plan

  1. Extraction and characterization PR (behaviour-neutral): the functions above move verbatim from obs-output.c.h into libobs/obs-output-interleave.c.h, still static, in the same unity group (libobs.core-output, just before obs-output.c.h). The new file declares the four obs-output.c helpers it calls. It adds test/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 and free_packets.

  2. Accessor PR (behaviour-neutral, like libobs: Read the output delay fields through a hidden accessor #1550):

    • a hidden obs_output_interleave_fields() in obs-output.c.h, declared in obs-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 in obs-internal.h. That way the interleaver file can leave the unity group.

    obs-output-interleave.c.h then reads the output only through these. A new obs_output_interleave_fields() is used, not an extension of obs_output_delay_fields(): the two units share almost no fields, and each port keeps one repr(C) struct of its own. Its test harness gets a stub source for the accessors in its tests.d fragment, as libobs: Read the output delay fields through a hidden accessor #1550 did. The four helper stubs in test_output_interleave.c change to match (linkage, and names if they get a prefix). That is the only change to the test: no case or assertion changes.

  3. obs-util darray: insert, erase range, pop front and push back new, with Tier 1 and parity against oracle wrappers. This can go before or alongside 2.

  4. Port PR: rust/obs-output, module interleave.

    • 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 truncating get_encoder_duration);
      • the start plan (initialize_interleaved_packets: the flags to clear, the second discard, the offsets, the highest_audio_ts adjustment);
      • the insertion position and the resort order;
      • the offset arithmetic (packet_dts_usec from 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_packets itself is generic over an output trait (the lock, the flags, the queue and packet-time arrays, the offsets and highest timestamps, the encoders, sending), as obs-output's delay core is generic over DelayOutput.
    • Shim, over struct obs_output internals through the accessors only: the darrays in place (with bmalloc memory), interleaved_mutex, pkt_callbacks_mutex and the caption mutex (libobs pthread_mutex_lock), the packet callbacks and info.encoded_packet, obs_encoder_packet_create_instance and release, add_caption (C), the debug and warning log lines.
    • Tests: Tier 1 ports every test_output_interleave assertion 1:1, plus edge cases. Layout tests. Tier 3 parity of the C file (oracle, with non-static oracle_* 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.
  5. 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.

Activity

  1. rdoupe commented on Oct 21, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #1594

    1. Move PR: the interleaver moved verbatim into libobs/obs-output-interleave.c.h (same unity group), with the characterization test_output_interleave (30 cases). Opening it now.
    2. Accessor PR, behaviour-neutral (hidden obs_output_interleave_fields() and encoder/caption accessors), after 1 merges.
    3. The missing darray operations in obs-util (insert, erase range, pop front, push back new).
    4. The port, as rust/obs-output's interleave module, as its own PR.
  2. added
    claimed
    on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    claimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions