Skip to content

obs-source: Port the source audio timestamp placement - #1936

Merged
zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/source-audio-timing
Oct 23, 2026
Merged

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/source-audio-timing

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1932

What and why

Ports the audio timestamp placement of source_output_audio_data() (libobs/obs-source.c.h) to a new crate, rust/obs-source: the crate for libobs/obs-source*.c.h units, as rust/obs-scene is for obs-scene*.c.h (#444). This is where every audio packet of every source goes in time, so it is A/V sync for all audio. The code was fixed four times for real users: #522 (a Video Delay (Async) over 2 s), the two sync offset fixes (f7c446e, an offset changed from 0, and bf3c8ea, the offset kept after a buffer reset) and #546 (the resampler tail, which shares the 70 ms threshold). Each rule those fixes added is pinned in the characterization and checked by a mutant.

Six commits:

  1. Move (behaviour-neutral): the decisions go to libobs/obs-source-audio-timing.c.h, in two pure steps around audio_buf_mutex plus the buffer-start rule of reset_audio_data_at_sys_time(). They are declared in the new obs-source-audio-timing.h without EXPORT, so they are hidden:

    source_output_audio_data() reads the fields into a struct, calls a step and writes the results back, so the stores of the volatile timing_set and timing_adjust stay where they were, in the same order. The volatile timing_adjust is still read twice, as before: before the lock for the packet, and under it for next_audio_sys_ts_min. These stay C: handle_ts_jump() (the lock and the buffer clear), audio_resampler_set_compensation_error() (it gets the step's drift error), conv_frames_to_time() (the caller passes the duration, so the steps never divide), the deque, the signal, the muting and both log lines, with the same messages. The file stays in libobs.core-source for this commit.

  2. Characterization: test_source_audio_timing_steps, 29 cases, compiles the moved file with a fake source. It covers:

    • every branch of both steps;
    • 70 ms and 2 s at, just under and just over each threshold;
    • direct timestamps, the decoupled re-anchor and next_audio_ts_min == 0;
    • a sync offset changed between packets;
    • seven packet sequences through a model of the caller.
  3. RED: Tier 1 in the new crate against todo!(). 35 of 35 fail.

  4. GREEN: the safe core and #[no_mangle] shims, in a new libobs.ported-source-audio-timing group with its own PCH (only the new header). The export lists, source-sets.json, the unity lists and ci/test_ported_sources.py are updated, and a _rust variant of the steps test is added (see below).

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

  6. Docs: the naming.md row, the README rows and a port_comparison bench file.

Every sum and difference is wrapping_* where the C's uint64_t wraps. The drift error is cast_signed() of the wrapped difference, the C's (int64_t). The sync offset is cast_unsigned(), the C's conversion of int64_t to uint64_t. The crate depends on no other crate.

  • When fields are read. Each step's inputs are read once, just before it. Before, timing_set was read just after the direct store, and sync_offset just after the reset branch. Only another thread writing those fields without the lock, in that window of a few statements, could see a difference, and that was already a data race. The stores and their order are unchanged.

  • Moved, but not deleted from obs-source.c.h. reset_audio_timing() stays for handle_ts_jump() and obs_source_show_preloaded_video(), and uint64_diff() for the resampler restart check of libobs: Drop audio held in the resampler when a new stream starts #546. The steps have their own copy of the distance. The thresholds are named in the header, and obs-source.c.h checks them against TS_SMOOTHING_THRESHOLD and MAX_TS_VAR with _Static_assert.

  • Kept, characterized, not endorsed:

    • a timestamp ahead of the clock gives a timing_adjust near u64::MAX;
    • timestamp + timing_adjust, the packet's end, a negative sync offset larger than the packet's system time, and a resampler offset larger than it all wrap;
    • the distance is not modular, so timestamps on either side of u64::MAX count as a jump;
    • a direct timestamp more than 2 s from the expected one is logged, not treated as a jump.
  • Intentional difference: a null struct pointer does nothing in the shims; the C dereferences it. libobs passes a struct on its stack.

  • The _rust variant links. test_source_audio_timing_steps_rust builds the unchanged test source against libobs_rust-static, as test_scene_bounds_place_rust does. The test includes only the new header and calls only the three functions, and the archive's obs-source member needs nothing from libobs or the other crates (it pulls in only libgcc_s and libm). It passes 29 of 29 on the hybrid tree. It is the first port since test_scene_bounds_place_rust whose _rust variant links.

  • Exported symbols: 1766 on the C backend, the hybrid and libobs-c-reference, with identical sets. The three functions are local (t) in all three, and in the hybrid they come from liblibobs_rust.a. The ported group is compiled only into libobs-c-reference.

  • Performance (port_comparison, x86_64, GCC, not pinned; median per 1024 packets):

    • pre_lock: 5.12 µs Rust, 4.78 µs C (1.07x);
    • under_lock: 4.50 µs Rust, 3.95 µs C (1.14x).

    That is under 0.6 ns more per packet, and a source sends a packet roughly every 21 ms.

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

Observed, not changed (upstream behaviour, outside the moved code). source_output_audio_place() empties the buffer with reset_audio_data(source, in->timestamp) when there is no audio_ts yet, or when the packet lands before it. That stores the placed time, which includes the sync offset and the resampler offset, in next_audio_sys_ts_min. Everywhere else, that field holds a time without the offsets. So with a non-zero sync offset, the packet after such a placement is compared against a time shifted by the offset:

  • with an offset of 70 ms or more, it is placed by its timestamp instead of appended. It lands right after the previous packet anyway; the steps test pins this in seq_jump_keeps_sync_offset and seq_sync_offset_changed;
  • with an offset over 2 s and a packet that is not continuous, the under-lock reset would fire.

This is not fixed here. Changing it would change behaviour, and it can be a separate issue.

RED → GREEN

  • RED commit: 9a155e99f. soldr cargo test -p obs-data --test obs-tests-core source:: fails 35 of 35, every failure "not yet implemented" (the 29 ported steps_* cases and 6 edge_* cases, two of them proptests).
  • GREEN: f97394500. Tier 1 passes 35/35. Tier 2 ran on the branch head when it was based on ed71dc387 (the same tree as 982fe2a66, except for test_view from test: characterize obs-view before the Rust port (#1933) #1934, which came with the final rebase onto dc5b192c9). It ran under a private Xvfb -noreset with dbus-run-session and a fresh HOME:
Test C backend hybrid libobs-c-reference
test_async_delay_audio 6/6 6/6 6/6
test_audio_sync_offset_change 5/5 5/5 5/5
test_media_sync_offset_restart 6/6 6/6 6/6
test_audio_restart_resampler_tail 4/4 4/4 4/4
test_audio_clock_drift 6/6 6/6 6/6
test_source_audio_timing_steps 29/29 29/29 29/29
test_source_audio_timing_steps_rust (not built: needs the Rust archive) 29/29 29/29
full ctest 392/392, 0 skipped 395/395, 0 skipped 395/395, 0 skipped

No case was skipped and no rerun was needed. The five real-libobs tests and the steps test also passed on all three builds at commit 1 (the move) and at commit 4 (GREEN), before the rebases. The full ctest also passed before the rebase onto ed71dc387 (391/391, 394/394 and 394/394, 0 skipped).

  • Tier 3, 359f2ac22 (source::audio_timing_parity, 10 tests). The structs cross the ABI as bytes. Each run fills a buffer with a random byte pattern (padding and output fields included), writes the inputs, runs the C step on one copy and the shim on the other, and compares every byte. Runs:

    • the layout of both structs, the enum's size, the action values and the thresholds, against the C header;
    • the special values of the u64 and i64 ranges with every flag: 186,624 pre-lock runs, 349,920 under-lock runs and 2,304 buffer starts;
    • proptests of each step over full-range values, with the comparisons drawn near the thresholds (20,000 cases each);
    • 10,000 random sequences of 2–50 packets through a model of the caller, driven once by the C steps and once by the shims. Between packets the sync offset changes, the flags toggle, the video thread moves timing_adjust (before the packet and between its two reads), and the audio mix clears timing_set. The step structs, the buffer start and the written-back state must match after every packet;
    • a check that the sequences reach every action and both under-lock outcomes, more than 100 times each.

    The parity module passed 26 runs in a row with fresh seeds (test: source_profiler_parity::parity_random_sequences finds Rust/C differences in about half of runs #1930), and no regression file came up.

  • Mutation check (once, not committed): 51 of 51 mutants fail.

Mutant Tier 1 Tier 3
direct at <= MAX_TS_VAR 2/35 fail 2/10 fail
direct does not set the timing 5 3
reset adjust reversed 14 3
next_audio_ts_min 0 compared like others 6 4
jump at >= 2 s 2 3
a direct timestamp can jump 2 3
smooth at <= 70 ms 2 3
re-anchor with either async flag 3 3
re-anchor to the smoothed timestamp 3 3
drift error sign reversed 6 3
smoothed packet keeps its timestamp 7 3
smoothed packet not continuous 8 3
exceeded diff not reported 5 3
jump adjust not reported 4 3
end from the unsmoothed timestamp 7 3
no push back on an exact match 1 2
next_audio_sys_ts_min 0 compared like others 4 3
push back at <= 70 ms 2 3
reset at >= 2 s 2 3
reset when continuous (#522 reverted) 4 3
reset to the smoothed timestamp 4 2
reset keeps the old system time 7 3
sync offset not applied 8 3
resampler offset added 4 3
next_audio_sys_ts_min from the packet's adjust 4 3
changed offset does not place (#333 reverted) 4 3
a change from 0 does not place (f7c446e reverted) 3 3
last_sync_offset not kept 4 4
sync offset in the comparison 8 3
buffer start for 0 too 2 3
distance not symmetric 17 5
saturating instead of wrapping: packet end, reset adjust, drift (wrapping_sub before the (int64_t)), drift clamped instead of cast, timestamp + timing_adjust, sync add, resampler sub, next_audio_sys_ts_min, reset adjust under the lock, reset system time, buffer start sync add, buffer start resampler sub (12 mutants) 2–5 each 3 each
shim: diff not written / direct not written / last_sync_offset not written 0 (does not reach the shim) 3 / 4 / 4
shim: KEEP and EXCEEDED swapped / an input written / the two adjusts swapped / continuous read wrong / buffer start argument changed 0 4 / 3 / 3 / 3 / 3
  • Real app: no UI, protocol or logging change. The log lines are unchanged and are formatted in C from the returned action.

Rebased onto b4a58944c (#1935, the obs-view port); only the port_comparison bench list conflicted. After that rebase, ci/test_build_groups.py, ci/test_ported_sources.py, ci/test_oracle_standins.py, source:: in obs-tests-core (45/45) and the lint lane were re-run and pass.

Other runs on the head before that rebase (on dc5b192c9):

  • soldr cargo test -p obs-data --test obs-tests-core: 1033/1033 before the final rebase; source:: 45/45 on the final head. soldr cargo test -p obs-source: 0 tests; as for obs-scene, its tests are a module of obs-tests-core.
  • ci/test_build_groups.py, ci/test_ported_sources.py and ci/test_oracle_standins.py: OK. The obs-util export-list tests: 3/3.
  • uv run ci/local_gate.py --lane lint: ok. --lane cross: ok. cargo clippy --workspace --all-targets -D warnings is clean on every commit.

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 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)

@rdoupe
rdoupe requested a review from zackees as a code owner October 23, 2026 14:16
@zackees
zackees added this pull request to the merge queue Oct 23, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Oct 23, 2026
…io-timing.c

Behaviour-neutral extraction before the Rust port. The decisions of
source_output_audio_data() in obs-source.c.h, which place each audio
packet of a source in time, move to libobs/obs-source-audio-timing.c.h as
two pure steps around audio_buf_mutex and the buffer start rule, declared
in the new libobs/obs-source-audio-timing.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- obs_source_audio_timing_pre_lock(): the direct timestamp check, then
  reset (timing not set), jump (more than MAX_TS_VAR off and not direct),
  smooth (within TS_SMOOTHING_THRESHOLD, with the async decoupled
  re-anchor and the drift error) or log, and next_audio_ts_min;
- obs_source_audio_timing_under_lock(): push back or place, the reset when
  the system time jumped and the audio was not continuous (obs-rust#522), the sync
  and resampler offsets, next_audio_sys_ts_min and the "sync offset
  changed, so place" rule (obs-rust#333);
- obs_source_audio_timing_buffer_start(): the audio_ts rule of
  reset_audio_data_at_sys_time() (obs-rust#286).

source_output_audio_data() reads the fields into a struct, calls a step,
and writes the results back itself, in the order the stores happened
before: timing_adjust = 0 then timing_set for a direct timestamp,
timing_set then timing_adjust for a reset, the re-anchor, then
next_audio_ts_min; under the lock the reset, next_audio_sys_ts_min and
last_sync_offset. The volatile timing_adjust is still read before the
lock for the packet and again under it for next_audio_sys_ts_min, as
before. handle_ts_jump() (the lock and the buffer clear),
audio_resampler_set_compensation_error(), conv_frames_to_time() (the
caller passes the packet's duration, so the steps never divide by the
sample rate), the deque place and push-back, the signal, the muting and
both log lines, with the same messages, stay in obs-source.c.h. All
arithmetic is the same uint64_t arithmetic, wrapping where it wrapped.

What differs is only when the fields are read: each step's inputs are
read once, just before it, where the code read some of them a few
statements later (timing_set after the direct store, sync_offset after
the reset branch). That is visible only to a thread writing them in that
window without the lock, which was already a data race.

reset_audio_timing() stays for handle_ts_jump() and
obs_source_show_preloaded_video(), and uint64_diff() for the resampler
restart check of obs-rust#546; the steps have their own copy of the distance.
The thresholds are named in the header, and obs-source.c.h checks them
against TS_SMOOTHING_THRESHOLD and MAX_TS_VAR 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#1932
test_source_audio_timing_steps compiles libobs/obs-source-audio-timing.c.h
(ASan and UBSan, no libobs core) and calls only its three hidden
functions, so it runs on every platform. Each case pins every output of a
step, that the step writes its numeric outputs (they start as a garbage
byte pattern) and that it leaves its inputs alone. 29 cases:

- obs_source_audio_timing_pre_lock(): direct timestamps on either side of
  the clock; direct at MAX_TS_VAR - 1, MAX_TS_VAR and MAX_TS_VAR + 1 (it
  is strict); the reset when the timing is not set, which ignores the
  expected timestamp and the async flags; next_audio_ts_min == 0 keeps
  the packet's timestamp; smoothing with a positive, zero and negative
  drift error; the 70 ms threshold at 70 ms - 1 ns (smoothed), 70 ms and
  70 ms + 1 ns (logged), either side; MAX_TS_VAR at 2 s - 1 ns and 2 s
  (logged) and 2 s + 1 ns (a jump, which resets the timing to the packet);
  a direct timestamp never jumps; the re-anchor of an unbuffered,
  decoupled async source (to os_time minus the packet's own timestamp,
  after the direct store) and none with only one of the flags.
- obs_source_audio_timing_under_lock(): appended when contiguous, placed
  when next_audio_sys_ts_min is 0; the 70 ms threshold either side; the
  reset at 2 s + 1 ns and none at 2 s; no reset when the audio was
  continuous (obs-rust#522), and the reset using the packet's own timestamp; the
  sync and resampler offsets applied after the comparison (41fa9c1);
  next_audio_sys_ts_min from the timing_adjust read under the lock, not
  the packet's; any sync offset change places the packet and is kept,
  from 0 too (obs-rust#333).
- obs_source_audio_timing_buffer_start(): os_time plus the sync offset
  minus the resampler offset, 0 for 0 (obs-rust#286).
- Seven packet sequences through a fake source (the fields, and a model
  of handle_ts_jump() and of the buffer start of
  source_output_audio_place()): contiguous packets with jitter, a
  system-clock source, the sync offset changed between packets (up, down
  and negative, before the buffer's start), a timestamp jump keeping the
  sync offset, a Video Delay (Async) timing move of 4 s followed rather
  than reset (obs-rust#522), the decoupled re-anchor, and next_audio_ts_min 0
  followed by an audio-mix reset.

Characterized, not endorsed: a timestamp ahead of the clock gives a
timing_adjust near UINT64_MAX; the packet's end, timestamp +
timing_adjust, a negative sync offset larger than the packet's system
time and a resampler offset larger than it all wrap; the distance is not
modular, so timestamps on either side of UINT64_MAX are a jump; a direct
timestamp more than 2 s from the expected one is logged as exceeding the
smoothing threshold, not treated as a jump. The sequences also show that
the first placement sets next_audio_sys_ts_min to the packet's time with
its sync offset, so with a non-zero offset the second packet is placed
too, right after the first.

It is in the native test graph. The five real-libobs tests are
unchanged.

Refs obs-rust#1932
… port

RED for the port of libobs/obs-source-audio-timing.c.h to rust/obs-source,
a new crate for the units extracted from libobs/obs-source.c (one crate per
libobs area, as rust/obs-scene is for obs-scene.c, obs-rust#444). The safe core,
audio_timing, is a stub (todo!()): Action (keep, reset, jump, smooth,
exceeded), PreLockInput and PreLock (obs_source_audio_timing_pre_lock()),
UnderLockInput and UnderLock (obs_source_audio_timing_under_lock()),
buffer_start() (obs_source_audio_timing_buffer_start()), distance()
(uint64_diff()), TS_SMOOTHING_THRESHOLD and MAX_TS_VAR. The crate depends
on nothing: plain u64, i64 and bool in and out.

tests/source/audio_timing.rs ports test_source_audio_timing_steps 1:1:
its 29 cases under the same names (steps_*), in the same order, with the
same inputs and expectations, the characterized-not-endorsed wraps and
the fake source of the sequences included. The C test's checks that a
step writes its outputs and leaves its inputs alone have nothing to check
in the safe API, which takes its inputs by value and returns every
output.

Edge cases (edge_*): the distance (not modular, at both ends of the
range); every flag combination with the timestamp, clock, expected
timestamp and duration over the special values of the u64 range and the
thresholds +-1, checking no panic and what each action implies (direct
exactly when within MAX_TS_VAR, the reset and jump adjust os_time -
timestamp, a smoothed drift under 70 ms, no diff or adjust outside their
actions, the end is timestamp + duration wrapped); the same for the
under-lock step over special u64 and i64 values (the reset exactly when
over MAX_TS_VAR and not continuous, the placed time with wrapped offsets,
push back exactly when contiguous and the offset unchanged); the buffer
start over special values; and two proptests of the same over full-range
values (4000 cases each).

The tests are a module of the existing core category binary,
obs-tests-core (rust/obs-data/tests/core.rs), as obs-scene's are; no new
test binary. The crate joins the workspace through its rust/* glob.

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

Refs obs-rust#1932
Ports libobs/obs-source-audio-timing.c.h, the audio timestamp placement
of source_output_audio_data(), to the audio_timing module of the new
rust/obs-source. source_output_audio_data() and
reset_audio_data_at_sys_time() stay C in obs-source.c.h: the field reads
and the stores in their order (the volatile timing_set and timing_adjust
included), audio_buf_mutex, handle_ts_jump(), the resampler's
compensation, conv_frames_to_time(), the deque, the signal, the muting
and the log lines. The hybrid libobs drops the C file and links the Rust
shims in its place.

Safe core (src/audio_timing.rs): pre_lock() decides direct (strictly
within MAX_TS_VAR of the clock), then reset (timing not set), jump (more
than MAX_TS_VAR from the expected timestamp and not direct), smooth
(strictly within TS_SMOOTHING_THRESHOLD; the re-anchor of an unbuffered,
decoupled async source; the drift error) or exceeded, and the packet's
end. under_lock() appends when the packet's system time is the buffer's
end or within 70 ms of it, resets the timing to the packet's own
timestamp when it is more than 2 s off and the audio was not continuous
(obs-rust#522), adds the sync offset and takes off the resampler offset after
the comparison, sets next_audio_sys_ts_min from the timing_adjust read
under the lock, and places the packet whenever the sync offset changed
(obs-rust#333). buffer_start() is the obs-rust#286 rule. Every sum and difference is
wrapping_add/wrapping_sub where the C's uint64_t wraps; the drift error
is cast_signed() of the wrapped difference, the C's (int64_t), and the
sync offset is cast_unsigned(), the C's conversion of int64_t to
uint64_t. 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_audio_timing.rs): the three functions keep the C
signatures. The two structs are mirrored #[repr(C)], the enum as u32 (C
stores it as an int or unsigned int; every value fits). Each shim reads
the input fields and writes the output fields through the pointer one
field at a time, so the inputs and the padding keep their bytes, as with
the C, and it never reads an output field. Intentional difference: a null
pointer does nothing, where the C dereferences it; libobs passes a struct
on its stack.

Kept, characterized, not endorsed (test_source_audio_timing_steps): a
timestamp ahead of the clock gives a timing_adjust near u64::MAX;
timestamp + timing_adjust, the packet's end, a negative sync offset
larger than the packet's system time and a resampler offset larger than
it wrap; the distance is not modular; a direct timestamp more than 2 s
from the expected one is logged, not a jump.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-audio-timing (LIBOBS_PORTED_SOURCES), whose PCH is
only obs-source-audio-timing.h; libobs.pch.h now includes the header for
obs-source.c. The three functions are not EXPORT, so the shims are
hidden: rust-hidden.txt, the local: list of rust-exports.map and
rust-unexports-macos.txt. libobs-rust links the crate's ffi. The exported
symbol sets are unchanged and equal: 1766 on the C backend, the hybrid
and libobs-c-reference. ci/test_ported_sources.py checks the group on
every host.

test_source_audio_timing_steps still compiles the C file into itself,
unchanged. The new test_source_audio_timing_steps_rust builds the same
test source against libobs_rust-static instead, as
test_scene_bounds_place_rust does, and links: the test includes only the
header and calls only the three functions, and the archive's obs-source
member needs nothing from libobs or the other crates (it pulls in only
libgcc_s and libm). It passes 29 of 29. The five real-libobs tests see
the swap through the hybrid.

Tier 1 source::audio_timing passes 35/35.

Refs obs-rust#1932
…he C

Oracle (oracle/source_audio_timing.c): the original
obs-source-audio-timing.c.h, unmodified, with its three functions renamed
to oracle_*, plus the layout of both structs, the size of the enum, the
action values and the two thresholds. Like the calldata and audio
controls oracles, it turns off -Wsign-conversion around the include: the
C adds the int64_t sync offset to a uint64_t, as libobs always did, and
libobs builds without that warning.

Tier 3 (tests/source/audio_timing_parity.rs). The structs cross the ABI
as bytes: each run fills a buffer with a random byte pattern (padding and
output fields included), writes the inputs, runs the C step on one copy
and the shim on another, and compares every byte, so the outputs must be
equal and neither side may write an input, the padding or anything the
other does not write. Runs:
- the layout of both structs (size, alignment, every offset), the enum's
  size, the action values and the thresholds against the C header;
- each step over the special values of the u64 and i64 ranges (0, 1,
  70 ms and 2 s +-1, 2^63 +-1, the ends, the thresholds below u64::MAX)
  with every flag: 186624 pre-lock runs, 349920 under-lock runs, and
  2304 buffer starts;
- proptests of each step over full-range values with the expected
  timestamp, the system time and next_audio_sys_ts_min drawn near the
  thresholds as often as anywhere (20000 cases each);
- 10000 random sequences of 2 to 50 packets through a model of
  source_output_audio_data() (the stores in their order,
  handle_ts_jump(), the buffer start of source_output_audio_place()),
  driven once by the C steps and once by the shims, from a random or empty
  source state, with the sync offset changed, the async flags toggled,
  the timing moved by the video thread (before the packet and between
  the two reads of timing_adjust) and timing_set cleared by the audio mix
  between packets. The step structs, the buffer start and the
  written-back state must be equal after every packet. A second test
  checks that the sequences reach every action and both under-lock
  outcomes, the reset, the re-anchor, direct timestamps and sync offset
  changes, more than 100 times each in 2000 sequences.

The whole parity module passed 26 runs in a row with fresh random seeds
(obs-rust#1930); no regression file came up.

Excluded, the intentional difference: a null struct pointer, which the C
dereferences and the shims ignore (checked on the Rust side only).

Mutation check (done once, not committed): 51 of 51 mutants fail. The 43
core mutants fail Tier 1 and Tier 3: each comparison at its threshold
(direct, jump, smooth, push back and the reset, < and <= or > and >=),
direct not setting the timing, a direct timestamp allowed to jump, the
reset adjust reversed, next_audio_ts_min 0 or next_audio_sys_ts_min 0
compared like other values, the re-anchor with either flag or to the
smoothed timestamp, the drift error reversed or clamped, the smoothed
timestamp or continuity dropped, diff or the jump's adjust not reported,
the end from the unsmoothed timestamp, no push back on an exact match,
obs-rust#522 reverted (a reset when continuous), the reset to the smoothed
timestamp or keeping the old system time, the sync offset not applied or
in the comparison, the resampler offset added, next_audio_sys_ts_min from
the packet's adjust, obs-rust#333 reverted, the pre-obs-rust#333 rule (a change from 0
does not place), last_sync_offset not kept, the buffer start for 0, the
distance not symmetric, and every wrapping operation (twelve of them,
the (int64_t) included) made saturating. The 8 shim mutants (an output
not written, an input written, two actions swapped, the two adjusts
swapped, an argument changed) fail Tier 3; Tier 1 does not reach the
shims.

Refs obs-rust#1932
docs/rust-port/naming.md maps libobs/obs-source*.c.h to the new
rust/obs-source, as libobs/obs-scene*.c.h maps to rust/obs-scene. The
rust/README.md crate table lists rust/obs-source, and its port table has
a row for libobs/obs-source-audio-timing.c.h: what stays C, the seam (the
field reads and the stores in their order), the wrapping arithmetic, what
the characterization pins and the port keeps, the intentional difference
(a null struct pointer), the _rust variant of the steps test, the five
real-libobs tests and what Tier 3 compares. The crate's description and
lib.rs say what it holds.

port_comparison gets its own bench file,
benches/port_comparison/source_audio_timing.rs: each step over 1024
packets that reach every branch, the outputs folded into a checksum both
sides must match. On x86_64 Linux (GCC, not pinned; median per 1024
packets): pre_lock 5.12 us Rust and 4.78 us C (1.07x),
under_lock 4.50 us Rust and 3.95 us C (1.14x), under 0.6 ns more per
packet.

Fixes obs-rust#1932
@rdoupe
rdoupe force-pushed the port/source-audio-timing branch from 2d0f59c to 982fe2a Compare October 23, 2026 14:52
@zackees
zackees enabled auto-merge October 23, 2026 15:12
@zackees
zackees added this pull request to the merge queue Oct 23, 2026
Merged via the queue into obs-rust:master with commit f53d2fd Oct 23, 2026
33 checks passed
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 23, 2026
RED for the port of libobs/obs-source-deinterlace-pacing.c.h to the
deinterlace_pacing module of rust/obs-source, next to audio_timing (obs-rust#1936).
The safe core is a stub (todo!()): hold(), wait_for_next(),
waiting_frame_ts(), half_duration(), buffered_offset() and reanchor(), one
per hidden C function, on plain u64 and u32 values, and FRAME_TOLERANCE,
TWOX_TOLERANCE and TS_JUMP_THRESHOLD.

tests/source/deinterlace_pacing.rs ports test_deinterlace_pacing_steps
1:1: its 20 cases under the same names (steps_*), in the same order, with
the same inputs and expectations, the characterized-not-endorsed wraps, the
first frame's half duration and the model of the unbuffered sequences
included.

Edge cases (edge_*) check each function against a model of the C's
uint64_t arithmetic written in i128 with the modulo explicit (the
guard's end as next + offset, the waiting timestamp as 2 * timestamp -
next, the lateness negative from 2^63): over the special values of the
u64 range and the thresholds +-1 (every combination), the hold at
TS_JUMP_THRESHOLD +-1 before every end, the re-anchor at its threshold and
at the int64_t sign from every offset, and four proptests over
full-range values with the clock drawn near the end and the lateness near
the thresholds (4000 cases each).

The tests are a module of obs-tests-core (rust/obs-data/tests/core.rs),
as audio_timing's are.

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

Refs obs-rust#1950
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 23, 2026
* libobs: Move the 2x deinterlace frame pacing into obs-source-deinterlace-pacing.c

Behaviour-neutral extraction before the Rust port. The pacing decisions of
double-rate deinterlacing (Yadif 2x, Linear 2x, Discard 2x) in
obs-source-deinterlace.c.h move to libobs/obs-source-deinterlace-pacing.c.h
as six pure functions on plain integers, declared in the new
libobs/obs-source-deinterlace-pacing.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- obs_source_deinterlace_pacing_hold(): an unbuffered source keeps its
  frame while the frame's end (frame_ts + offset + 2 * half_duration -
  TWOX_TOLERANCE) is ahead of the clock by less than TS_JUMP_THRESHOLD;
- obs_source_deinterlace_pacing_wait_for_next() and
  obs_source_deinterlace_pacing_waiting_frame_ts(): the "don't skip
  ahead" guard of ready_deinterlace_frames() for an unbuffered source with
  two frames queued, and the timestamp it then stores;
- obs_source_deinterlace_pacing_half_duration(): half the time from the
  frame before, truncated to uint32_t;
- obs_source_deinterlace_pacing_buffered_offset(): the offset of a
  buffered source, from the source clock that picks frames (obs-rust#395);
- obs_source_deinterlace_pacing_reanchor(): the offset of an unbuffered
  source, kept unless the frame comes early or misses its tick and its
  first field (obs-rust#395).

obs-source-deinterlace.c.h keeps everything else: the async_frames queue,
the buffered walk, first_frame(), deinterlace_process_last_frame(), the
textures and the render. It still tests async_unbuffered and
deinterlace_offset before the hold and the guard, reads the same fields
and obs->video values, and stores deinterlace_frame_ts,
deinterlace_half_duration and deinterlace_offset in the same order. The
re-anchor now stores the offset it returns every time; when it keeps the
offset, that is the value already there. All arithmetic is the same
uint64_t arithmetic, wrapping where it wrapped, the (int64_t) cast of
the re-anchor's lateness included; TWOX_TOLERANCE is now 1000000ULL,
which every use already converted to uint64_t.

The thresholds are named in the header. obs-source-deinterlace.c.h keeps
FRAME_TOLERANCE (the buffered walk) and TWOX_TOLERANCE (the render) and
checks them against the header at compile time; TS_JUMP_THRESHOLD was
used only by the hold and moves. The obs-rust#395 comments move with the code.

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#1950

* test: Characterize the 2x deinterlace frame pacing

test_deinterlace_pacing_steps compiles
libobs/obs-source-deinterlace-pacing.c.h (ASan and UBSan, no libobs
core) and calls only its six hidden functions, so it runs on every
platform. 20 cases:

- obs_source_deinterlace_pacing_hold(): held until the frame's end and
  not at it; TS_JUMP_THRESHOLD - 1 (held), TS_JUMP_THRESHOLD and
  TS_JUMP_THRESHOLD + 1 (moves on, the clock jumped: strict); the half
  duration widened before it is doubled (UINT32_MAX gives 8.6 s, no
  32-bit wrap); a half duration of 0; the end wrapping past UINT64_MAX
  and below 0.
- obs_source_deinterlace_pacing_wait_for_next() and _waiting_frame_ts():
  wait until timestamp + offset + duration (no tolerance, no threshold),
  the waiting timestamp one duration back, so its half duration is half
  the duration; a next frame before the first.
- obs_source_deinterlace_pacing_half_duration(): 50i and 59.94i, rounding
  down, the largest that fits.
- obs_source_deinterlace_pacing_buffered_offset(): video_time -
  last_frame_ts + FRAME_TOLERANCE + TWOX_TOLERANCE (obs-rust#395).
- obs_source_deinterlace_pacing_reanchor(): no offset yet; a frame early
  by 1 ns; on time and later by less than the larger of the field and the
  tick, less TWOX_TOLERANCE (kept); at it and past it (re-anchored), for
  50i into 60 fps (the field is longer) and into 30 fps (the tick is),
  ties, a field and a tick of 0, a tick over UINT32_MAX.
- Unbuffered sequences of 100 frames through a model of
  deinterlace_get_closest_frames() (hold, newest arrived frame, half
  duration, re-anchor), on exact rational clocks: 25 and 50 frames a
  second into 30 and 60 fps never re-anchor and never pick early (obs-rust#395),
  50 into 30 drops 76 frames; a frame a field late re-anchors once and
  an early one re-anchors back.

Characterized, not endorsed:
- half a duration of 2^32 ns or more is truncated to 32 bits (a 10 s gap
  gives 705 ms);
- a timestamp going backwards wraps: the half duration of a frame 40 ms
  back is 2^32 - 20 ms, the guard's end is the next frame's timestamp
  plus the offset, the waiting timestamp lands after the first frame;
- the first frame with no second one queued takes its half duration
  against deinterlace_frame_ts, which is 0 for a source that never showed
  a frame: half its own timestamp truncated (1.78 s for a capture
  timestamp 1000 s after boot), or against the last frame shown before a
  queue reset. An unbuffered source then moves on at the next tick as if
  the clock jumped, or, when the truncation leaves 30 ms, holds the
  frame to 59 ms where a 50i frame ends at 39 ms;
- the buffered offset and the re-anchored offset wrap when the frame's
  clock is ahead of the canvas, and the re-anchor's lateness is an
  int64_t, so 2^63 late or more counts as early;
- 29.97 frames a second into 30 and 60 fps, and 59.94 into 60, re-anchor
  on every frame: the canvas ticks a little faster than the frames, so a
  tick lands within TWOX_TOLERANCE before the frame's time and the frame
  counts as early; the offset follows the canvas, 33 or 17 us a frame.

It is in the native test graph, in the core suite. The real-libobs
test_deinterlace_2x_field_order is unchanged.

Refs obs-rust#1950

* obs-source: Add Tier 1 tests for the 2x deinterlace frame pacing port

RED for the port of libobs/obs-source-deinterlace-pacing.c.h to the
deinterlace_pacing module of rust/obs-source, next to audio_timing (obs-rust#1936).
The safe core is a stub (todo!()): hold(), wait_for_next(),
waiting_frame_ts(), half_duration(), buffered_offset() and reanchor(), one
per hidden C function, on plain u64 and u32 values, and FRAME_TOLERANCE,
TWOX_TOLERANCE and TS_JUMP_THRESHOLD.

tests/source/deinterlace_pacing.rs ports test_deinterlace_pacing_steps
1:1: its 20 cases under the same names (steps_*), in the same order, with
the same inputs and expectations, the characterized-not-endorsed wraps, the
first frame's half duration and the model of the unbuffered sequences
included.

Edge cases (edge_*) check each function against a model of the C's
uint64_t arithmetic written in i128 with the modulo explicit (the
guard's end as next + offset, the waiting timestamp as 2 * timestamp -
next, the lateness negative from 2^63): over the special values of the
u64 range and the thresholds +-1 (every combination), the hold at
TS_JUMP_THRESHOLD +-1 before every end, the re-anchor at its threshold and
at the int64_t sign from every offset, and four proptests over
full-range values with the clock drawn near the end and the lateness near
the thresholds (4000 cases each).

The tests are a module of obs-tests-core (rust/obs-data/tests/core.rs),
as audio_timing's are.

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

Refs obs-rust#1950

* obs-source: Port the 2x deinterlace frame pacing

Ports libobs/obs-source-deinterlace-pacing.c.h, the frame pacing of
double-rate deinterlacing, to the deinterlace_pacing module of
rust/obs-source. deinterlace_get_closest_frames() and
ready_deinterlace_frames() stay C in obs-source-deinterlace.c.h: the
async_unbuffered and deinterlace_offset tests before the hold and the
guard, the field reads and the stores in their order, the async_frames
queue, the buffered walk, first_frame(), the textures and the render.
The hybrid libobs drops the C file and links the Rust shims in its place.

Safe core (src/deinterlace_pacing.rs): hold() keeps an unbuffered frame
while its end (frame_ts + offset + twice the widened half duration, less
TWOX_TOLERANCE) is ahead of the clock by less than TS_JUMP_THRESHOLD;
wait_for_next() waits while the clock is before timestamp + offset + the
frame's duration, and waiting_frame_ts() is the timestamp one duration
back; half_duration() is half the difference, its low 32 bits as the C's
(uint32_t); buffered_offset() is video_time - last_frame_ts +
FRAME_TOLERANCE + TWOX_TOLERANCE (obs-rust#395); reanchor() returns video_time -
frame_ts when there is no offset, when the frame is early (the lateness,
cast_signed() of the wrapped difference as the C's (int64_t), is
negative) or when the lateness plus TWOX_TOLERANCE reaches the larger of
the half duration and the canvas interval, and the old offset otherwise
(obs-rust#395). 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_deinterlace_pacing.rs): the six functions keep the C
signatures, plain integers in and out, so nothing is mirrored and no
pointer is checked. No intentional differences.

Kept, characterized, not endorsed (test_deinterlace_pacing_steps): half a
duration of 2^32 ns or more is truncated; a timestamp going backwards
wraps the half duration, the guard's end and the waiting timestamp; the
first frame with no second one queued takes its half duration against a
deinterlace_frame_ts of 0; the offsets wrap when the frame's clock is
ahead of the canvas; a lateness of 2^63 or more counts as early; at 29.97
and 59.94 frames a second into a slightly faster canvas the offset
re-anchors on every frame and follows the canvas.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-deinterlace-pacing (LIBOBS_PORTED_SOURCES), whose PCH
is only obs-source-deinterlace-pacing.h; libobs.pch.h now includes the
header for obs-source-deinterlace.c. The six functions are not EXPORT, so
the shims are 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; the six are local in all three, and the
hybrid's come from the Rust archive. ci/test_ported_sources.py checks the
group on every host.

test_deinterlace_pacing_steps still compiles the C file into itself,
unchanged. The new test_deinterlace_pacing_steps_rust builds the same
test source against libobs_rust-static instead, as
test_source_audio_timing_steps_rust does, and links: the test includes
only the header and calls only the six functions. test_deinterlace_2x_field_order
sees the swap through the hybrid.

Tier 1 source::deinterlace_pacing passes 30/30.

Refs obs-rust#1950

* obs-source: Check the 2x deinterlace frame pacing port against the C

Oracle (oracle/source_deinterlace_pacing.c): the original
obs-source-deinterlace-pacing.c.h, unmodified, with its six functions
renamed to oracle_*, plus the three thresholds. The bool results are
declared as u8 on the Rust side, the byte the C returns.

Tier 3 (tests/source/deinterlace_pacing_parity.rs). Every function takes
and returns plain integers, so each run compares the bytes of the two
results: the C's bool as its byte, the uint32_t and uint64_t results as
their bytes. Runs:
- the three thresholds against the C header;
- each function over the special values of the u64 range (0, 1, the
  thresholds +-1, 50i and 60 fps durations, 2^32, 2^63 +-1, the end of
  the range) and ten half durations, every combination: 138240 holds,
  331776 guards, 576 half durations and buffered offsets, 3317760
  re-anchors;
- proptests of each function over full-range u64 and u32 values, with
  the clock drawn near the frame's end and the lateness near the
  thresholds as often as anywhere (20000 cases each);
- 3000 random sequences of 2 to 300 canvas ticks through a model of
  deinterlace_get_closest_frames(), ready_deinterlace_frames() (the
  unbuffered queue trim and guard, and the buffered walk with
  frame_out_of_bounds() and the MAX_TS_VAR jump) and first_frame(), with
  the stores in the C's order, driven once by the C functions and once
  by the shims: sources of 25, 29.97, 50 and 59.94 frames a second into
  30 and 60 fps canvases, buffered and unbuffered, with timestamp and
  arrival jitter, late deliveries, late ticks, timestamps that jump or go
  backwards or land anywhere, queue resets (last_frame_ts 0, the
  first-frame path), a full queue and the buffering toggled. The bytes
  of every call's result and the model's state after every tick must be
  equal;
- a check that 2000 sequences reach every branch of every function more
  than 100 times (held, moved on, the clock jumped; waited or not; the
  half duration from a pair and from deinterlace_frame_ts, truncated;
  the buffered offset; the re-anchor with no offset, early, late and
  kept; the first frame) and every rate pair.

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 the two test modules
built into a scratch test binary: 53 mutants. The 45 non-equivalent
core mutants fail Tier 1 (2 to 10 of 30) and Tier 3 (1 to 4 of 11):
each comparison at its threshold (< and <=, >= and >), the hold without
its jump threshold or its "before the end" test, one half duration
instead of two, the half duration doubled in 32 bits, TWOX_TOLERANCE or
FRAME_TOLERANCE dropped, the guard without its duration or offset, the
waiting timestamp ahead or unchanged, the half duration not halved,
rounded up, saturated to UINT32_MAX or from the high bits, the buffered
offset reversed, the re-anchor without its no-offset, early or late rule,
early only past TWOX_TOLERANCE, the smaller of field and tick or only
one of them, the pre-obs-rust#395 re-anchor at every pick, the lateness clamped
or computed without the modulo, and every wrapping operation made
saturating. Two mutants survive and are equivalent: end - sys_time is
computed only when sys_time is before the end, and the lateness plus
TWOX_TOLERANCE is reached only for a lateness of at most INT64_MAX, so
neither can wrap. The 6 shim mutants (a result negated, arguments
swapped) fail Tier 3; Tier 1 does not reach the shims.

No intentional differences: there is no pointer to check.

Refs obs-rust#1950

* docs: Record the 2x deinterlace frame pacing port

The rust/README.md crate table now says rust/obs-source also holds the
units extracted from libobs/obs-source-deinterlace.c, and its port table
has a row for libobs/obs-source-deinterlace-pacing.c.h: the six functions,
what stays C, the seam (the async_unbuffered and deinterlace_offset tests,
the field and obs->video reads and the stores in their order), the
wrapping arithmetic and the (int64_t) lateness, what the characterization
pins and the port keeps, no intentional differences, the _rust variant of
the steps test, test_deinterlace_2x_field_order 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_deinterlace_pacing.rs: 1024 ticks of 50i
and 59.94i sources into 60 fps that reach every branch, as an unbuffered
source runs the hold, the guard, the half duration and the re-anchor, and
as a buffered one runs the half duration and its offset, the results
folded into a checksum both sides must match. On x86_64 Linux (GCC, not
pinned; median per 1024 ticks): unbuffered_tick 8.67 us Rust and 7.59 us
C (1.14x), buffered_pick 3.20 us Rust and 3.13 us C (1.02x), about 1 ns
more per tick.

Fixes obs-rust#1950
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 source audio timestamp placement in obs-source.c to a new obs-source crate

2 participants