Skip to content

obs-source: Port the async video frame pacing - #2046

Merged
zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/source-async-pacing
Oct 24, 2026
Merged

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/source-async-pacing

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator

Fixes #2024

What and why

Ports the async video frame pacing of get_closest_frame() and ready_async_frame() in libobs/obs-source.c.h to rust/obs-source, as a new module async_pacing next to deinterlace_pacing (#1950). Once a tick, async_tick() uses these rules to choose which queued frame to show. That applies to every async source without 2x deinterlacing: media, cameras and capture cards. A wrong rule would drop or repeat frames for all of them, or put audio and video out of sync. So every rule from the functions' history is pinned in the characterization and checked by a mutant:

  • the 2 ms duplicate guard (upstream 76870d1, raised from 1 ms);
  • the guard applies only once a frame is passed (upstream 62500b5);
  • last_frame_ts is set for unbuffered sources too (upstream d60d222);
  • MAX_TS_VAR is used both for the jump and for the re-anchor (upstream 1c40ce6);
  • dropped frames go back to the cache (upstream c3e498d).

Six commits:

  1. Move (behaviour-neutral): the decisions go to libobs/obs-source-async-pacing.c.h as one function, obs_source_async_pacing(), declared in the new obs-source-async-pacing.h without EXPORT (hidden). It takes the queue's timestamps, oldest first, with sys_time, last_frame_ts, last_sys_timestamp and async_unbuffered. It returns struct obs_source_async_pacing_result { size_t drop_front; uint64_t last_frame_ts; bool take_first; }. frame_out_of_bounds() stays in obs-internal.h for obs-source-deinterlace.c.h; the new file has its own copy on values.

    obs-source.c.h keeps everything that touches the queue or the frames: async_frames, da_erase(), remove_async_frame(), the async cache, async_mutex (held by async_tick()) and the frame pointers. get_closest_frame() copies the timestamps into a stack array of MAX_ASYNC_FRAMES and calls the function. It then erases drop_front frames at index 0, each followed by remove_async_frame() as before, stores last_frame_ts, and takes the front frame when take_first is set.

    Why this order is the same as before. Every da_erase() in ready_async_frame() was at index 0 and erased the front frame, and remove_async_frame() of that frame followed it. In the walk, the passed frame is always array[0] and the next is array[1], so the erases are a prefix of the queue, in order. Neither da_erase() nor remove_async_frame() reads a timestamp or last_frame_ts, so deciding first and applying afterwards gives the same queue, cache, prev_frame flags and last_frame_ts. The walk also called remove_async_frame(source, NULL) before it had passed a frame. That did nothing, because no cached frame is NULL (obs_source_frame_create() uses bzalloc()).

    This was checked against the original code. A scratch harness (not committed) compiles origin/master's two functions verbatim on a stand-in source. On 20,000,000 random queues of 0 to 40 frames it gives the same drops, the same frame taken and the same last_frame_ts. Its erases are always a prefix of the queue, released in order, and the characterization of commit 2 passes against it too.

    The DEBUG_ASYNC_FRAMES logging (compiled out) goes. MAX_TS_VAR is checked against the header with _Static_assert. The file stays in libobs.core-source for this commit.

  2. Characterization: test_async_frame_pacing_steps, 12 cases. It compiles the moved file and calls the function through a model queue, which applies each result as get_closest_frame() does and stores last_sys_timestamp as async_tick() does. Every call also checks the contract the caller relies on: drop_front is below the queue length, and nothing is dropped without a frame taken. It pins:

    • unbuffered with 1, 2 and 30 frames;
    • last_frame_ts == 0 (the first-frame rule);
    • the jump at MAX_TS_VAR - 1, MAX_TS_VAR and MAX_TS_VAR + 1, ahead and behind;
    • the 2 ms guard at 1999999, 2000000 and 2000001, and that it is not applied before a frame is passed;
    • the re-anchor inside the walk (at MAX_TS_VAR + 1 but not at MAX_TS_VAR, backwards, and twice in one walk);
    • timestamps going backwards, and sys_time < last_sys_timestamp;
    • 30 frames all in the past;
    • 31 and 40 frames (see the first finding below);
    • buffered and unbuffered sequences on exact clocks.

    It is in the core suite of the test graph, so no pinned count changes. It needs no libm and includes only the new header.

  3. RED: Tier 1 in the crate against todo!(). 17 of 17 fail.

  4. GREEN: the safe core and the #[no_mangle] shim, in a new libobs.ported-source-async-pacing group whose PCH is only the new header. This commit updates the three export lists, source-sets.json, the unity lists and ci/test_ported_sources.py, each in sorted place, and adds test_async_frame_pacing_steps_rust.

  5. Tier 3: a C oracle and byte-level parity, plus the mutation check.

  6. Docs: the README rows, the crate's description and lib.rs, and a port_comparison bench file. docs/rust-port/naming.md already maps libobs/obs-source*.c.h to rust/obs-source.

Every sum and difference is wrapping_* where the C's uint64_t wraps: sys_time - last_sys_timestamp, the clock advance, frame_offset, the guard's difference and the re-anchor's next - frame_time and next - frame_offset. The walk keeps the C's index arithmetic as front, the index of async_frames.array[0]: each da_erase() at index 0 becomes front + 1, and "one frame left" means there is no timestamp at front + 1. The core denies clippy's arithmetic, indexing, as, unwrap, expect, panic, unimplemented and unreachable lints. The crate still depends on no other crate.

  • What changed in the C. get_closest_frame() now copies the timestamps, calls the function and applies the result. For a queue longer than MAX_ASYNC_FRAMES it copies into a bmalloc() buffer instead of the stack array (see the first finding). Nothing else changed.

  • Kept, characterized, not endorsed:

    • A frame at timestamp 0 seeds last_frame_ts 0, so the next tick takes the first-frame rule again. A timing jump to timestamp 0 does the same.
    • The first-frame rule shows the oldest frame even for an unbuffered source. The next tick then drops to the newest.
    • A timestamp before the passed frame's, by any amount, wraps past MAX_TS_VAR and re-anchors the clock.
    • sys_time before last_sys_timestamp wraps sys_offset and moves the source clock back, so a frame that was due is held. Below 0 the clock wraps to near UINT64_MAX, the walk drops to the last frame, and the next tick recovers through the jump rule. libobs's video_time does not go backwards, so this is reachable only through a source clock that wraps.
    • A re-anchor uses the passed frame's offset from the clock before this tick's advance, so the re-anchoring tick loses its sys_offset (up to one canvas interval).
    • A re-anchor that lands exactly on 0 shows the passed frame and re-seeds last_frame_ts from it.
    • At the same source and canvas rate on exact clocks, the clock must be strictly past a frame, so every frame after the first is shown one tick late (30 into 30: 59 shown in 60 ticks).
  • Intentional difference: if timestamps is NULL with a count above 0, the shim treats it as an empty queue: nothing is dropped or taken and last_frame_ts is kept. The C reads through the null pointer. libobs never passes an empty queue and always passes its own array. This is listed in the shim and parity test headers and in rust/README.md.

  • The _rust variant links. The test calls the hidden function directly, so test_async_frame_pacing_steps_rust builds the unchanged test source against libobs_rust-static, as test_deinterlace_pacing_steps_rust does. The test includes only the new header, which needs no SIMDe or graphics headers, so the target needs no libobs interface include directories. It passes 12/12 on the hybrid tree.

  • Exported symbols: 1766 on the C backend, the hybrid and libobs-c-reference, with identical sets, and the same as at commit 1 (checked on 2183ae742, at GREEN and at commit 1). obs_source_async_pacing is local (t) in all three. In the hybrid it comes from the Rust archive, whose async_pacing::pace is linked in, and no async-pacing object is on the hybrid's link line. The ported group is compiled only into libobs-c-reference.

  • Performance (port_comparison, x86_64, GCC, not pinned, a loaded host; median per 1024 ticks):

    • buffered_tick (the jump check and the walk over recorded queues of a 29.97 fps source into 60 fps): 4.34 µs Rust, 3.43 µs C (1.26x);
    • unbuffered_tick: 3.83 µs Rust, 3.05 µs C (1.25x);
    • walk_30 (30 frames in the past): 25.30 µs Rust, 22.98 µs C (1.10x).

    That is under 1 ns more per tick for the queues a source sees, and about 2 ns for a 30-frame walk, once per async source per canvas frame.

  • No user-visible change, so no docs/features.md line.

Observed, not changed (upstream behaviour; nothing here is fixed):

  1. MAX_ASYNC_FRAMES is not a hard limit. cache_video() checks async_frames.num >= MAX_ASYNC_FRAMES under async_mutex and then unlocks it to copy the frame. obs_source_output_video_internal() queues the frame later, under a second lock. So a source that outputs from more than one thread can queue one extra frame per concurrent caller past 30. Upstream handled any length, so nothing breaks today. A fixed stack array of 30 in the new copy, however, would have been a stack overflow. That is why get_closest_frame() falls back to bmalloc() for longer queues, and why the characterization pins 31 and 40 frames.
  2. A re-anchoring tick loses its canvas advance (see above). After a timestamp jump inside a buffered queue, the source clock lands one tick's advance behind where it would otherwise be, which delays the next frame by up to one canvas interval. This is small and happens once per jump.
  3. A source that stamps every frame 0 never leaves the first-frame rule. Each tick shows the oldest frame and drops nothing, so a producer faster than the canvas fills the queue until cache_video() clears it at 30.

RED → GREEN

Test C backend hybrid libobs-c-reference
test_media_restart_stale_frame 5/5 5/5 5/5
test_media_restart_first_frame 4/4 4/4 4/4
test_async_delay_audio 6/6 6/6 6/6
test_v4l2_framerate 11/11 11/11 11/11
test_v4l2_corrupt_frame 10/10 10/10 10/10
the other v4l2 tests (busy, busy_signal, controls, fd_cloexec, input_caps, udev_poll_error, vcam_write) 62/62 62/62 62/62
test_deinterlace_2x_field_order 55/55 55/55 55/55
test_deinterlace_pacing_steps 23/23 23/23 23/23
test_deinterlace_pacing_steps_rust (not built: needs the Rust archive) 23/23 23/23
test_async_frame_pacing_steps 12/12 12/12 12/12
test_async_frame_pacing_steps_rust (not built) 12/12 12/12
full ctest 415/415 424/424 (not run)

No ctest test was skipped. Inside the tests, three cases skip on each lane, as on master: open_crashes_without_obs_core in test_audio_io and test_video_io, and test_hex_float_values_ignore_comma_locale, which needs a comma-decimal locale this host does not have. No rerun was needed: none of the real-time tests (#838) failed in any run. The same set also passed on all three builds on 80cafa039 (before #2021), with the full ctest at 411/411 and 419/419, and on the C backend and the hybrid at GREEN on 46a482f83. Before the c-ref run, ninja libobs-c-reference was run explicitly (no work to do: its libobs.so.30 was from the head build), and the loader was checked to use libobs-c-reference/libobs.so.30 (the tests have a RUNPATH, so LD_LIBRARY_PATH wins).

  • Tier 3, 988dfd633 (source::async_pacing_parity, 8 tests). Each call compares the bytes of the two results: drop_front and last_frame_ts as their bytes, and take_first as the byte the C stores. Runs:

    • the two thresholds, and the result struct's size and field offsets, against the C header;

    • every queue of up to two of 20 special values (0, 1, the guard and MAX_TS_VAR ±1 around a clock, 2^63 ±1, the end of the range), with each of them as last_frame_ts, over 10 canvas steps (none, a tick, the guard and jump edges, backwards, wrapping) and both bufferings: 168,400 calls. Also every queue of three, buffered, from five last_frame_ts values: 400,000 calls;

    • proptests over full-range u64 queues of 1 to 30 frames, and over queues near the clock whose steps sit at the guard, at MAX_TS_VAR, at a frame interval, at 0 or anywhere (20,000 cases each);

    • 3000 random sequences of 2 to 300 ticks through a model of async_tick(), get_closest_frame() and cache_video(), driven once by the C and once by the shim. The model covers the queue, its 30-frame limit and reset, the erases, the stores and the frame taken. The bytes of every call's result and the model's state after every tick must be equal. The sequences use:

      • sources of 23.976, 25, 29.97, 30, 50 and 60 frames a second, into 30 and 60 fps canvases, buffered and unbuffered;
      • timestamp and arrival jitter, latency, bursts and lost frames;
      • late ticks, ticks that stop (the queue fills) and a canvas clock going back;
      • timestamps that jump ahead or back by MAX_TS_VAR, MAX_TS_VAR + 1 or any amount, or land anywhere;
      • resets (obs_source_output_video(NULL)) and the buffering toggled;
    • every rate pair, buffered and unbuffered, clean and jittered, from fixed seeds. The clean buffered runs show their frames in order and never twice;

    • a check that 2000 sequences reach every rule more than 100 times:

      Rule Times reached
      first frame 2817
      unbuffered 102863
      timing jump 116
      nothing due 29383
      front frame taken 84569
      frames dropped 17316
      dropped to the last frame 2226
      guard kept a frame 12614
      re-anchor or seed 370
      full queue 253

    The parity module passed 20 runs in a row with fresh seeds, and no regression file came up.

  • Mutation check (once, not committed; Tier 1 and Tier 3 in one run): 49 of 51 mutants fail, and the 2 that survive are equivalent.

Mutant Tier 1 (of 17) Tier 3 (of 8)
frame_out_of_bounds: > as >=, behind / ahead 4 / 4 3 / 3
frame_out_of_bounds: one-sided (the wrapped difference only) 10 6
no timing jump (frame_out_of_bounds false) 7 7
no first-frame rule 4 6
no seed of last_frame_ts / seed from the oldest frame 8 / 2 6 / 2
an empty queue takes a frame 2 1
unbuffered: keeps two / drops none / walks like buffered 6 / 6 / 6 6 / 6 / 6
unbuffered: keeps last_frame_ts / stores the oldest 6 / 6 6 / 6
jump: no store / returns false / off 6 / 6 / 6 7 / 7 / 7
sys_offset reversed / saturating 9 / 2 6 / 4
clock advance saturating / missing 3 / 8 5 / 6
frame_offset taken after the advance 4 6
walk while >= 4 5
guard <= 3 2
guard before a frame is passed (62500b5 reverted) 4 6
no guard / 1 ms guard (76870d1 reverted) 5 / 5 6 / 7
erase before a frame is passed 9 6
no erase (the walk never ends) 11 hung 6 hung
last-frame check one early 10 6
the walk returns true without a frame 7 6
one more dropped / the last frame not dropped 8 / 9 6 / 6
re-anchor >= / off / without the offset / adding it 3 / 5 / 5 / 5 3 / 6 / 6 / 6
re-anchor saturating (no backwards re-anchor) / two-sided 5 / 5 6 / 4
frame_offset / frame_time not updated 2 / 4 5 / 5
MAX_TS_VAR + 1 / - 1 2 / 2 5 / 4
shim: take_first negated, drop_front - 1, the old last_frame_ts, last_frame_ts and last_sys_timestamp swapped, async_unbuffered negated, a count of 1 read as empty, one timestamp short (7 mutants) 0 (Tier 1 does not reach the shim) 6 or 7 each
equivalent: ts <= last_frame_ts in frame_out_of_bounds (at equality both branches give 0) 0 0
equivalent: saturating guard difference (computed only inside while last_frame_ts > next) 0 0
  • Real app: no UI, protocol or logging change.

Other runs:

  • uv run ci/local_gate.py --test-diff origin/master on the final head 2ff3e5387 (and before that on 2183ae742, the same result): 3073/3073 passed, 2 skipped (the existing #[ignore] tests: the NVENC loader test, which needs an NVIDIA driver, and obs-source: Port the source audio input steps #2021's exhaustive balance test), and the doc-tests pass; all 25 source::async_pacing* tests are among them.
  • uv run ci/local_gate.py --lane lint and --lane cross: ok on 2183ae742, and before that on f26b44e1b. cargo clippy -p obs-data --tests -p obs-c-oracle --all-targets -D warnings is clean.
  • ci/test_build_groups.py, ci/test_ported_sources.py, ci/test_oracle_standins.py, ci/test_sorted_shared_lists.py, ci/test_test_graph_sorted.py, ci/test_cmocka_includes.py, ci/test_cmocka_rust_static_guard.py and ci/test_naming.py: OK on the final head 2ff3e5387.
  • .cmake files formatted with gersemi 0.25.0; C with clang-format 22.1.3.
  • Rebased three times: onto 80cafa039 with no conflicts; onto ec2a5a90b, after obs-source: Port the source audio input steps #2021 (the source audio input steps) merged. obs-source: Port the source audio input steps #2021's entries sit next to this PR's in libobs.pch.h, source-sets.json, ci/test_ported_sources.py, the three export lists, ffi/mod.rs, lib.rs, the test main.rs, the oracle's lib.rs, port_comparison.rs, the crate description and the README. Every conflict was resolved by keeping both entries, in sorted order. Then onto b3912c298, with no conflicts. After each rebase, source-sets.json, test-graph.json, the unity lists and the three export lists were compared by hand against master: only this PR's lines differ.

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 (N/A for docs-only; a focused CI fix may push RED and GREEN together)
  • 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 (the lint and cross lanes and --test-diff passed locally; nothing is stamped)
  • Warning or flag changes (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

Behaviour-neutral extraction before the Rust port. The pacing decisions of
get_closest_frame() and ready_async_frame() in obs-source.c.h, which pick
the frame async_tick() shows for every async source without 2x
deinterlacing (media, cameras, capture cards), move to
libobs/obs-source-async-pacing.c.h as one function on the queue's
timestamps, obs_source_async_pacing(), declared in the new
libobs/obs-source-async-pacing.h without EXPORT (hidden, as
-fvisibility=hidden keeps it). Every rule moves unchanged:

- the first-frame rule: with last_frame_ts 0 the oldest frame is shown,
  buffered or not, and seeds last_frame_ts;
- unbuffered: the newest frame, the older ones dropped, and last_frame_ts
  set even then (upstream d60d222);
- the timing jump: the oldest frame more than MAX_TS_VAR from
  last_frame_ts, either way (frame_out_of_bounds(), upstream 1c40ce6),
  is shown at once and becomes the clock;
- the catch-up walk: the source clock advances by sys_time -
  last_sys_timestamp and drops the frames it passed, keeps a passed frame
  while the next is due within 2 ms (upstream 76870d1; only once a frame
  is passed, upstream 62500b5), never drops the last queued frame, and
  re-anchors when the next frame jumps more than MAX_TS_VAR;
- the seed of last_frame_ts from the shown frame when it is 0.

obs-source.c.h keeps everything that touches the queue or the frames: the
async_frames queue, da_erase(), remove_async_frame(), the async cache,
async_mutex (async_tick() holds it) and the frame pointers. It copies the
timestamps, oldest first, into a stack array of MAX_ASYNC_FRAMES, calls the
function, erases drop_front frames from the front (remove_async_frame() on
each, after its erase, as before), stores last_frame_ts and takes the front
frame when take_first is set.

Why that order is the same: every da_erase() in ready_async_frame() was at
index 0 and erased the front frame, and remove_async_frame() of that frame
followed it. In the walk the passed frame is always array[0] and the next
array[1], so the erases are a prefix of the queue, in order. Neither
da_erase() nor remove_async_frame() reads a timestamp or last_frame_ts, so
computing first and applying after gives the same queue, cache, prev_frame
flags and last_frame_ts. remove_async_frame(source, NULL), which the walk
called before it had passed a frame, did nothing: no cached frame is NULL
(obs_source_frame_create() uses bzalloc()). Checked in a scratch harness
(not committed) that compiles origin/master's two functions verbatim on a
stand-in source: 20,000,000 random queues of 0 to 40 frames give the same
drops, frame and last_frame_ts, the erases are always a prefix released in
order, and the characterization test of the next commit passes against it.

cache_video() checks MAX_ASYNC_FRAMES before it unlocks async_mutex to copy
the frame, and obs_source_output_video_internal() queues it under a second
lock, so a source that outputs from several threads can queue a few frames
more than 30. get_closest_frame() then copies into a heap buffer instead,
so no queue length overflows the stack array.

The DEBUG_ASYNC_FRAMES logging (compiled out, 0) goes. All arithmetic is
the same uint64_t arithmetic, wrapping where it wrapped. frame_out_of_bounds()
stays in obs-internal.h for obs-source-deinterlace.c; the new file has its
own copy on values. The 2 ms guard and MAX_TS_VAR are named in the header,
and obs-source.c.h checks MAX_TS_VAR against it at compile time.

The new file joins libobs.core-source with libobs.pch.h, which includes the
new header. The exported symbol sets are unchanged (1766).

Refs obs-rust#2024
test_async_frame_pacing_steps compiles libobs/obs-source-async-pacing.c.h
(ASan and UBSan, no libobs core) and calls only obs_source_async_pacing(),
so it runs on every platform. A model queue stands in for async_frames: it
applies each result as get_closest_frame() does (drop_front frames off the
front, then the front frame taken) and stores last_sys_timestamp as
async_tick() does. Every call also checks the contract obs-source.c.h
relies on: drop_front below the queue length, and nothing dropped without a
frame taken. 12 cases:

- an empty queue (NULL timestamps): nothing dropped or taken;
- the first-frame rule: last_frame_ts 0 shows the oldest frame, buffered
  or unbuffered, whatever the clocks, and seeds last_frame_ts;
- unbuffered with 1, 2 and 30 frames: the newest, the others dropped, no
  jump rule and no walk; and 31 and 40 frames, which cache_video()'s
  unlocked check lets producers on several threads queue;
- the timing jump at MAX_TS_VAR - 1, MAX_TS_VAR and MAX_TS_VAR + 1, ahead
  and behind (strict: MAX_TS_VAR is not a jump), only on the oldest frame,
  and between 0 and UINT64_MAX;
- the 2 ms guard: the next frame due 1999999 ns ago keeps the passed one,
  2000000 and 2000001 drop it; not applied before a frame is passed
  (upstream 62500b5), so a frame 1 ns due is shown; a frame exactly at
  the clock is not due;
- the re-anchor inside the walk at MAX_TS_VAR + 1 (and not at MAX_TS_VAR),
  backwards, twice in one walk;
- timestamps going backwards, by 1 ns and down a queue;
- sys_time before last_sys_timestamp, the clock wrapping below 0 and the
  jump that recovers on the next tick;
- 30 frames all in the past: the walk stops at one frame left, or keeps
  the one before when the last is within the guard;
- sequences on exact integer clocks: 30 fps into 60 (each frame two
  ticks), 60 into 30 (every other frame dropped), 30 into 30, and 60 into
  30 unbuffered.

Characterized, not endorsed:
- a frame at timestamp 0 seeds last_frame_ts 0, so the next tick takes the
  first-frame rule again; a timing jump to timestamp 0 does the same;
- the first-frame rule shows the oldest frame even when the source is
  unbuffered;
- a timestamp before the passed frame's, by any amount, wraps past
  MAX_TS_VAR and re-anchors the clock;
- sys_time before last_sys_timestamp wraps sys_offset and moves the source
  clock back, so a frame that was due is held; below 0 it wraps to near
  UINT64_MAX and the walk drops to the last frame;
- the re-anchor uses the passed frame's offset from the clock before this
  tick's advance, so a re-anchoring tick loses its sys_offset;
- a re-anchor that lands on 0 shows the passed frame and re-seeds
  last_frame_ts from it;
- at the same source and canvas rate on exact clocks, every frame after the
  first is shown a tick late (the clock must be strictly past a frame).

The expectations were checked against origin/master's get_closest_frame()
and ready_async_frame() compiled verbatim on a stand-in source (scratch,
not committed): all 12 cases pass there too.

It is in the native test graph, in the core suite. It needs no libm and
includes only obs-source-async-pacing.h.

Refs obs-rust#2024
RED for the port of libobs/obs-source-async-pacing.c.h to the
async_pacing module of rust/obs-source, next to deinterlace_pacing (obs-rust#1950).
The safe core is a stub (todo!()): pace(timestamps, sys_time, state) ->
Pacing { drop_front, take_first, last_frame_ts }, the hidden C function on
a slice of the queue's timestamps and the source's last_frame_ts,
last_sys_timestamp and async_unbuffered; frame_out_of_bounds(), its own
copy of the obs-internal.h inline on values; and MAX_TS_VAR and
FRAME_TOLERANCE.

tests/source/async_pacing.rs ports test_async_frame_pacing_steps 1:1: its
12 cases under the same names (steps_*), in the same order, with the same
inputs and expectations, the characterized-not-endorsed cases and the model
queue of the sequences included.

Edge cases (edge_*) check pace() against a model of the original
get_closest_frame() and ready_async_frame(), written as the C was, on a Vec
queue that erases at index 0 and records each frame remove_async_frame()
releases, so each case also checks that the erases are a prefix of the
queue, released in order, and that the frame taken is the one after them.
They also check the contract obs-source.c.h relies on (drop_front below the
length, nothing dropped without a frame taken, the first-frame and newest-
only rules) over: every queue of up to two of 18 special values (0, 1, the
thresholds +-1 around a clock, 2^63, the end of the range), from each of
them as last_frame_ts, over 9 canvas steps (the guard and jump edges,
backwards, wrapping) and both bufferings; queues of three of them, buffered; frame_out_of_bounds()
against i128 arithmetic; and two proptests, full-range queues of 1 to 30
frames and frame-rate-like queues with jitter and a jump (4000 cases
each).

soldr cargo test -p obs-data --test obs-tests-core -- source::async_pacing
fails 17 of 17 with "not yet implemented".

Refs obs-rust#2024
Ports libobs/obs-source-async-pacing.c.h, the frame pacing of every async
source without 2x deinterlacing (media, cameras, capture cards), to the
async_pacing module of rust/obs-source. get_closest_frame() stays C in
obs-source.c.h: the copy of the queue's timestamps, the erases at index 0
with remove_async_frame() on each, the store of last_frame_ts and the frame
taken, the async_frames queue, the async cache, async_mutex and the frame
pointers. The hybrid libobs drops the C file and links the Rust shim in its
place.

Safe core (src/async_pacing.rs): pace() is the C function on a slice. With
last_frame_ts 0 it takes the oldest frame (the first-frame rule);
unbuffered it drops all but the newest; buffered it takes the oldest after
a timing jump (frame_out_of_bounds(), its own copy of the obs-internal.h
inline: more than MAX_TS_VAR either way), else walks: the clock advances by
the wrapped sys_time - last_sys_timestamp, the frames passed are dropped,
the passed frame is kept while the next is due within FRAME_TOLERANCE
(only once a frame is passed), the last frame is never dropped, and the
clock re-anchors to the next frame less the passed frame's offset when the
next frame is more than MAX_TS_VAR (wrapped) after it. A frame taken with
last_frame_ts 0 seeds it. The walk keeps the C's index arithmetic as
front, the index of async_frames.array[0]: each da_erase() at index 0 is
front + 1, and "one frame left" is no timestamp at front + 1. Every sum and
difference is wrapping_* where the C's uint64_t wraps. The module denies
clippy's arithmetic, indexing, as, unwrap, expect, panic, unimplemented and
unreachable lints, keeps no state and takes no lock.

Shim (src/ffi/source_async_pacing.rs): obs_source_async_pacing() keeps the
C signature and returns struct obs_source_async_pacing_result by value
(#[repr(C)]: size_t, uint64_t, bool). It reads count timestamps and
nothing else, and writes through no pointer. Intentional difference: a
null timestamps with a count above 0 reads as an empty queue (nothing
dropped or taken, last_frame_ts kept), where the C reads through the null
pointer; libobs never passes an empty queue and always passes its own
array.

Kept, characterized, not endorsed (test_async_frame_pacing_steps): a frame
at timestamp 0 seeds last_frame_ts 0; the first-frame rule shows the oldest
frame of an unbuffered source; a timestamp before the passed frame's
re-anchors; a canvas clock going backwards moves the source clock back,
below 0 to near UINT64_MAX; a re-anchoring tick loses its sys_offset; a
re-anchor onto 0 re-seeds; at equal rates on exact clocks frames come a
tick late.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-async-pacing (LIBOBS_PORTED_SOURCES), whose PCH is
only obs-source-async-pacing.h; libobs.pch.h keeps the header for
obs-source.c. The function is not EXPORT, so the shim is hidden:
rust-hidden.txt, the local: list of rust-exports.map and
rust-unexports-macos.txt. libobs-rust already links the crate's ffi. The
exported symbol sets are unchanged and equal: 1766 on the C backend, the
hybrid and libobs-c-reference; obs_source_async_pacing is local in all
three, and the hybrid's comes from the Rust archive.
ci/test_ported_sources.py checks the group on every host.

test_async_frame_pacing_steps still compiles the C file into itself,
unchanged. The new test_async_frame_pacing_steps_rust builds the same test
source against libobs_rust-static instead, as
test_deinterlace_pacing_steps_rust does, and links: the test includes only
the header and calls only the hidden function. The real-libobs tests
(test_media_restart_stale_frame, test_media_restart_first_frame,
test_async_delay_audio, the v4l2 tests) see the swap through the hybrid.

Tier 1 source::async_pacing passes 17/17.

Refs obs-rust#2024
Oracle (oracle/source_async_pacing.c): the original
obs-source-async-pacing.c.h, unmodified, with its function renamed to
oracle_obs_source_async_pacing, plus the two thresholds and the layout of
struct obs_source_async_pacing_result (its size and the offsets of its
three fields). take_first is declared as u8 on the Rust side, the byte the
C stores.

Tier 3 (tests/source/async_pacing_parity.rs, 8 tests). Each call compares
the bytes of the two results: drop_front and last_frame_ts as their bytes,
take_first as its byte. Runs:
- the thresholds and the result's size and offsets against the C header;
- every queue of up to two of 20 special values (0, 1, the guard and
  MAX_TS_VAR +-1 around a clock, 2^63 +-1, the end of the range) from each
  of them as last_frame_ts, over 10 canvas steps (none, a tick, the guard
  and jump edges, backwards, wrapping), both bufferings: 168,400 calls;
  and every queue of three from five last_frame_ts values, buffered:
  400,000 calls;
- proptests over full-range u64 queues of 1 to 30 frames, and over queues
  near the clock whose steps sit at the guard, at MAX_TS_VAR, at a frame
  interval, at 0 or anywhere (20000 cases each);
- 3000 random sequences of 2 to 300 canvas ticks through a model of
  async_tick(), get_closest_frame() and cache_video(): the queue and its
  30-frame limit (a full queue resets it and last_frame_ts), the erases
  from the front, the stores of last_frame_ts and last_sys_timestamp, and
  the frame taken, driven once by the C and once by the shim. Sources of
  23.976, 25, 29.97, 30, 50 and 60 frames a second into 30 and 60 fps
  canvases, buffered and unbuffered, with timestamp and arrival jitter,
  latency, bursts, lost frames, late ticks, ticks that stop (the queue
  fills), a canvas clock going back, timestamps that jump ahead or back by
  MAX_TS_VAR, MAX_TS_VAR + 1 or any amount or land anywhere, resets
  (obs_source_output_video(NULL)) and the buffering toggled. The bytes of
  every call's result and the model's state after every tick must be
  equal;
- every rate pair, buffered and unbuffered, clean and jittered, 600 ticks
  each from fixed seeds with a jump each way; the clean buffered runs show
  their frames in order and never twice;
- a check that 2000 sequences reach every rule more than 100 times: the
  first-frame rule (2817), unbuffered (102863), the timing jump (116),
  nothing due (29383), the front frame (84569), frames dropped (17316),
  dropped to the last (2226), the guard keeping a frame (12614), a
  re-anchor or seed (370) and a full queue (253).

The parity module passed 20 runs in a row with fresh random seeds; no
regression file came up.

Mutation check (done once, not committed), with Tier 1 (17 tests) and
Tier 3 (8) in one run: 51 mutants, 44 in the core and 7 in the shim.
Every non-equivalent core mutant fails both tiers:

| Mutant | Tier 1 | Tier 3 |
|---|---|---|
| frame_out_of_bounds > as >=, either side | 4 | 3 |
| frame_out_of_bounds one-sided (wrapping) | 10 | 6 |
| no timing jump at all | 7 | 7 |
| no first-frame rule | 4 | 6 |
| no seed of last_frame_ts / seed from the oldest frame | 8 / 2 | 6 / 2 |
| an empty queue takes a frame | 2 | 1 |
| unbuffered keeps two / drops none / walks | 6 | 6 |
| unbuffered keeps last_frame_ts / stores the oldest | 6 | 6 |
| jump without its store / returns false / off | 6 | 7 |
| sys_offset reversed / saturating | 9 / 2 | 6 / 4 |
| clock advance saturating / missing | 3 / 8 | 5 / 6 |
| frame_offset taken after the advance | 4 | 6 |
| walk while >= | 4 | 5 |
| guard <= | 3 | 2 |
| guard before a frame is passed (pre-62500b512) | 4 | 6 |
| no guard / 1 ms guard (pre-76870d176) | 5 | 6 / 7 |
| erase before a frame is passed | 9 | 6 |
| no erase (the walk never ends: killed by the hang) | 11 hung | 6 hung |
| last-frame check one early | 10 | 6 |
| walk returns true without a frame | 7 | 6 |
| drop_front one more / the last frame not dropped | 8 / 9 | 6 |
| re-anchor >= / off / without or adding the offset | 3 / 5 | 3 / 6 |
| re-anchor saturating (no backwards re-anchor) / two-sided | 5 | 6 / 4 |
| frame_offset or frame_time not updated | 2 / 4 | 5 |
| MAX_TS_VAR +-1 | 2 | 5 / 4 |

Two survive and are equivalent: frame_out_of_bounds with ts <= last_frame_ts
(at equality both branches compute 0), and the guard's difference
saturating (the guard runs only inside "while last_frame_ts > next", so it
cannot wrap). The 7 shim mutants (take_first negated, drop_front one less,
the old last_frame_ts returned, last_frame_ts and last_sys_timestamp
swapped, async_unbuffered negated, a count of 1 read as empty, one
timestamp short) fail Tier 3 (6 or 7 of 8); Tier 1 does not reach the shim.

Intentional difference, not compared: a null timestamps with a count above
0 is an empty queue for the shim; the C reads through it.

Refs obs-rust#2024
The rust/README.md crate table now lists libobs/obs-source-async-pacing.c.h
among the units rust/obs-source holds, and its port table has a row for it:
the one function, its rules, what stays C in get_closest_frame() (the copy
of the timestamps, with its heap fallback past MAX_ASYNC_FRAMES, the erases
at index 0 with remove_async_frame() after each, the stores and the frame
taken), the wrapping arithmetic, what the characterization pins and the port
keeps, the one intentional difference (a null timestamps with a count above
0 is an empty queue), the _rust variant of the steps test, the real-libobs
tests that see the swap, and what Tier 3 compares. The crate's description
and lib.rs say what it holds. docs/rust-port/naming.md already maps
libobs/obs-source*.c.h to rust/obs-source.

port_comparison gets its own bench file,
benches/port_comparison/source_async_pacing.rs: 1024 ticks' queues recorded
from a 29.97 frames a second source into a 60 fps canvas, with jitter, late
deliveries and clock jumps, run buffered (the jump check, the walk, the guard
and the re-anchor) and unbuffered, plus 1024 walks over 30 frames all in the
past, the results folded into a checksum both sides must match. On x86_64
Linux (GCC, not pinned, a loaded host; median per 1024 ticks):
buffered_tick 4.34 us Rust and 3.43 us C (1.26x), unbuffered_tick 3.83 us
Rust and 3.05 us C (1.25x), walk_30 25.30 us Rust and 22.98 us C (1.10x):
under 1 ns more per tick, about 2 ns for a 30-frame walk.

Fixes obs-rust#2024
@rdoupe
rdoupe requested a review from zackees as a code owner October 24, 2026 04:14
@zackees
zackees added this pull request to the merge queue Oct 24, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 24, 2026
@zackees
zackees added this pull request to the merge queue Oct 24, 2026
Merged via the queue into obs-rust:master with commit 1b96ca5 Oct 24, 2026
33 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.

rust: Port the async video frame pacing in obs-source.c to obs-source

2 participants