Skip to content

libobs: Keep a timing of its own for audio on its own clock - #4049

Merged
zackees merged 1 commit into
masterfrom
ci/flaky-audio-own-clock
Nov 8, 2026
Merged

zackees merged 1 commit into
masterfrom
ci/flaky-audio-own-clock

Conversation

@zackees

@zackees zackees commented Nov 8, 2026

Copy link
Copy Markdown
Member

Why

test_async_audio_own_clock ejected the merge queue twice. Both times it was the Rust Tier 2 Linux 🐧 (tests OFF, all) job, and the only failure was small_packets_audio_ahead_of_video_is_heard:

merge_group run head log line
42488016838 (job 128755762643) 005e8ae 1024-frame packets, audio +14400 s from video: tone in 84 % of the program → heard_percent(1024, …) >= 90 is not true (test_async_audio_own_clock.c:361)
42489291203 (job 128761123828) 48ca274 same case, tone in 87 %

Diagnosis: a load-sensitive libobs bug, not #4019 and not the test

Local measurement, Tier 2 harness build tree, test_async_audio_own_clock run directly, 16 busy loops on 16 CPUs:

libobs runs failing 1024:+4h 4800:+4h 4800:−3h 4800:same clock (guard)
master 2d6ae72, C reference (OFF) 6 / 6 73–89 % 73–93 % 71–93 % 93 % (all 6)
this PR, C reference (OFF) 0 / 5 93 % 93 % 93 % 93 %
this PR, hybrid (ON) 0 / 5 93 % 93 % 92–93 % 93 %

The same-clock guard holds at 93 % on master under the same load, so the load starves only the own-clock path. That isolates the bug, not the machine.

Fix

This changes production code. The test is not changed: same assertions, bar and timeout.

  • Audio on a clock of its own gets own_clock_timing_adjust / own_clock_timing_set (under audio_buf_mutex). The first such packet seeds them from the system clock, as reset_audio_timing() does. Later packets are appended where their own timing puts them, and video frames no longer move it.
  • Audio on the video's clock, or before any frame, is unchanged.
  • C: libobs/obs-source-audio-output.c.h, plus two seam getter/setter pairs. Rust: obs_source::audio_output and the obs-source-seam FFI.
  • Tier 1: two cases, the seed and a late continuous packet. Both fail with the old wiring.
  • Tier 3: the C-oracle parity fixture now generates last_output_frame_ts and the own timing, plus a deterministic own-clock case. Parity fails against the old Rust.
  • Ledger line in docs/features.md.

Validation

  • ./lint clean. soldr cargo test -p obs-data --test obs-tests-core audio_output: 33 passed.

  • Local gate: both Tier 2 modes ran test_async_audio_own_clock green (30.40 s / 30.34 s). The gate did not stamp, for two reasons unrelated to this change:

    • an unrelated local test_pulse_stream_restart failure;
    • the lint lane's check_test_binary_inventory got soldr porcelain JSON instead of cargo metadata output (KeyError: 'workspace_members').

    So PR CI runs the full suite.

  • Proof runs (repeat, merge_group, non-vacuity red run, master after merge) are added in comments below as they finish.

Refs #3747.

🤖 Generated with Claude Code

test_async_audio_own_clock ejected the merge queue twice
(merge_group runs 42488016838 and 42489291203, "tests OFF" job):
small_packets_audio_ahead_of_video_is_heard heard the tone in 84 % and
87 % of the program, under its 90 % bar. Both runs completed in their
normal 30.3 s, so it was not a hang.

The cause is in libobs, not the test. Since #3752 a continuous packet
on a clock of its own (hours from last_output_frame_ts) does not follow
the video's timing. But it still read source->timing_adjust, which
obs_source_update_async_video() sets to the video's timing on every
shown frame. So nearly every packet hit under_lock's MAX_TS_VAR
reset and was placed at os_gettime_ns(), when it arrived, instead of
by its timestamp. On a busy machine the arrival jitter turns into gaps
and overlaps in the mix, more so with 21 ms packets than with 100 ms
ones.

Audio on a clock of its own now has own_clock_timing_adjust and
own_clock_timing_set, under audio_buf_mutex. The first such packet
seeds them from the system clock, as reset_audio_timing() does. Later
packets are appended where their own timing puts them, and video
frames do not move it. Audio on the video's clock (or with no frame
yet) is unchanged. C in obs-source-audio-output.c.h, Rust in
obs_source::audio_output through two new seam getter/setter pairs.

RED (local, Tier 2 harness tree, 16 busy loops on 16 CPUs, master
2d6ae72): 6 of 6 runs of the C reference fail an own-clock case
(1024-frame 73-89 %, 4800-frame down to 71 %). The same-clock guard
stays at 93 % in all 6, so the load starves only the own-clock path.
GREEN: 5 of 5 runs each of the C reference and hybrid libobs at the
same load, every case 92-93 %. Rust: Tier 1 covers the seed and a late
continuous packet (both fail with the old wiring). Tier 3 parity
against the C oracle now generates last_output_frame_ts and the own
timing, and fails against the old Rust.

Refs #3747.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vpacher

vpacher commented Nov 8, 2026

Copy link
Copy Markdown
Member

Thanks for finding this. The cause makes sense to me: since #3752 an own-clock packet is not continuous, but it still compared against the video's timing_adjust, so nearly every packet took the MAX_TS_VAR reset.

One question about the new own timing: own_clock_timing_set is set once and never cleared. Is that intended?

The case I mean:

  1. obs-audio-discard.c.h:82 clears timing_set when the mix drops a source's buffered audio ("tell the timestamp adjustment code ... to reset everything"). The next packet then takes the OBS_SOURCE_AUDIO_TS_RESET path before the lock, which reseeds only timing_adjust. For audio on its own clock, own_clock_timing_adjust keeps its old value.
  2. After the seed, own_clock_timing_adjust changes only when under_lock sets reset_timing, and that compares the packet with next_audio_sys_ts_min, which uses the same offset. So a slow drift between the sender's clock and the system clock (NDI audio on its own clock, running all day) does not reach that reset. The discard reset in step 1 is then the only way back, and it does not reach the own timing.

If that reading is right, clearing own_clock_timing_set where timing_set is cleared (or in the TS_RESET branch) would keep the old recovery for own-clock audio. I have not tested this; it is from reading the diff.

@zackees

zackees commented Nov 8, 2026

Copy link
Copy Markdown
Member Author

Proof

(a) Repeat runs, local, Tier 2 harness build tree, ctest -R '^test_async_audio_own_clock$' --repeat until-fail:5, this PR (acc45cf):

  • OFF (C reference), idle: 5/5 passed (30.3–30.8 s).
  • ON (hybrid), idle: 5/5 passed.
  • ON, 16 busy loops: 5/5 passed.
  • OFF, 16 busy loops: 4/5. The 5th failed while the host's own load average was ~54 on 16 CPUs. A 10-run probe in that same window also failed the untouched same-clock guard (80–86 %), so that batch measured host overload, not this change.

Interleaved A/B (same container, master and fix libobs-c-reference alternating run by run, so both see the same load):

condition master (2d6ae72 audio output) this PR
host load ~25–47, no extra load, 8 pairs 0/8 fail (1024-frame 91–93 %) 0/8 fail (all 93 %)
host load ~25–47 + 16 busy loops, 8 pairs 4/8 fail: 1024-frame 77 / 85 / 89 %, 4800 +4 h 85 / 89 % 0/8 fail, every case 92–93 %

The same-clock guard read 93 % in all 32 runs. Caveat: the test still measures a real-time mix, so at roughly 4× CPU overload every case degrades, the untouched path included. This PR removes the own-clock path's extra sensitivity; it does not make the test immune to starvation.

PR CI run 42502627932: Rust Tier 2 Linux 🐧 (tests OFF, all) ✅, (tests ON, all) ✅, CI Gate ✅ ✅.

(c) Non-vacuity: draft #4050 = this PR plus one commit forcing on_video_clock = true in C and Rust, with every test kept. Run 42502653222 is red:

  • test_async_audio_own_clock fails in Tier 2 tests OFF (job 128803974278) and tests ON (job 128803974290): own-clock 4800-frame tone 0 %, same-clock guard 93 %. Asan fails the same test.
  • Rust (job 128800380837): a_late_continuous_packet_on_another_clock_keeps_its_own_timing, a_moved_system_time_resets_a_continuous_packet_on_another_clock and audio_on_its_own_clock_matches FAILED.

Forcing the flag also undoes #3752, so CI shows the full 0 % bug. The load-only part of the RED is the A/B above. #4050 is closed now that the run is recorded.

(d) Ruleset before enqueue (ruleset 25065309): required = ["CI Gate ✅"]; merge queue min 1, max_entries_to_build 5, max_entries_to_merge 5, ALLGREEN. Not modified.

@zackees
zackees added this pull request to the merge queue Nov 8, 2026
Merged via the queue into master with commit 26b6b9a Nov 8, 2026
42 checks passed
@zackees
zackees deleted the ci/flaky-audio-own-clock branch November 8, 2026 18:01
@zackees

zackees commented Nov 8, 2026

Copy link
Copy Markdown
Member Author

Post-merge proof

(b) merge_group run 42505884746 (head 26b6b9a), job list from the API:

  • Rust Tier 2 Linux 🐧 (build) 128808782270: completed / success, 17:40:57 → 17:54:22Z
  • Rust Tier 2 Linux 🐧 (tests OFF, all) 128812508455: completed / success, 17:54:36 → 18:00:01Z. It ran: log shows Test #2: test_async_audio_own_clock . Passed 30.20 sec
  • Rust Tier 2 Linux 🐧 (tests ON, all) 128812508406: completed / success
  • CI Gate ✅ 128814034203: completed / success

Merged 2026-11-08T18:01:06Z as 26b6b9a.

(d) Ruleset 25065309 re-read after merge, identical to before: required ["CI Gate ✅"], merge queue 1 / 5 / 5 ALLGREEN.

(e) Master push CI: the queue landed a batch (731a4eb → 26b6b9a → 835ce00 → d66a0fd), and Push concurrency cancels runs for intermediate commits. The run covering this change is 42507829200 on d66a0fd. It was still pending when this comment was written.

zackees pushed a commit that referenced this pull request Nov 9, 2026
…elay (#4143) (#4202)

* test(libobs): own-clock audio from a fast sender must not build up delay (RED)

#4143: since #4049, audio on a clock of its own is placed at its own
timestamps plus an offset that only the first packet sets. A sender whose
audio clock runs faster than the system clock delivers more audio than the
mix plays, and the delay between a packet's arrival and its playout grows
without a limit (about 0.18 s per hour at 50 ppm).

The new case sends the own-clock source's packets 1.10 times as fast as
real time, stamped by their samples, with every 10th packet silent as a
marker, and measures how long after its packet was sent each marker
starts in the program audio. It expects every delay under 200 ms. The
existing cases keep rate 1 and no markers, so they run as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(libobs): take the own-clock timing again when audio runs ahead (GREEN)

#4143: own_clock_timing_adjust, set by the first packet on a clock of its
own, is moved by nothing else: a continuous packet never takes
under_lock's MAX_TS_VAR reset, and a fast sender's audio is never late,
so the discard path never runs. A sender clock faster than the system
clock then places its audio later and later after its arrival.

When a packet on its own clock would be placed more than
TS_SMOOTHING_THRESHOLD (70 ms) after its arrival, its timing is taken
again from its arrival, as the first packet's is. under_lock then sees a
jump of more than the smoothing threshold and places the packet by its
timestamp. In C (obs-source-audio-output.c.h, also the C reference and
the oracle) and in Rust (obs_source::audio_output).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs(features): own-clock audio from a fast sender stays in sync (#4143)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants