Skip to content

feat(obs-source): the own-clock drift error and its filter (#4182) - #4856

Merged
vpacher merged 2 commits into
masterfrom
feat/4182-own-clock-drift-error
Nov 14, 2026
Merged

vpacher merged 2 commits into
masterfrom
feat/4182-own-clock-drift-error

Conversation

@vpacher

@vpacher vpacher commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

Refs #4182 (PR 1 of 2: the error and its filter, not wired in yet)

Problem

Audio on a clock of its own (NDI, #3747) from a fast sender plays later and later. #4143 bounds the delay with a step every time it passes 70 ms. Each step overwrites about 60 ms of buffered audio, which you hear as a click (#4182). The resampler already has a clock drift loop filter (obs_media_io::audio_resampler_drift, after obsproject#6351) that could take the drift out smoothly, but nothing gives it an error for own-clock audio.

The gap proposed in #4182 (data->timestamp + own_clock_timing_adjust - os_time) is in the sender's time. Resampling does not change the sender's timestamps, so that gap never settles, and it would only drive the loop into its ±1000 ppm clamp (see the claim comment on #4182).

Change

rust/obs-source/src/own_clock_drift.rs, new code with no C original:

  • buffered_end_gap(): how far the end of the source's buffered audio is ahead of a packet's arrival (audio_ts + buffered frames / rate - os_time). This is the quantity that resampling changes. It uses i128 internally and saturates at the i64 range; a rate of 0 counts no frames.
  • OwnClockDrift: the filter that gives the error for audio_resampler_set_compensation_error(). It is a sliding minimum over 1 s (8 buckets of 125 ms), so late packets do not move it, less a baseline taken over the first second. It returns no error until the first second has passed. It has no allocation, lock or clock, and it ignores a sample older than its window.

Nothing calls it yet. PR 2 wires it into source_output_audio_data() in C and Rust: an internal compensation flag for own-clock sources, this error in place of drift_error, and the #4143 bound checked on the buffered end. PR 2 also adds the Tier 2 case and the docs/features.md line.

Tests

  • RED 36bb546: todo!() stubs. On Linux (Debian 13, x86_64), all 15 new cases in obs-tests-core failed at the stubs.
  • GREEN (this commit): the 16 cases pass. The 16th, loop_beyond_the_clamp_still_needs_the_step, came with GREEN.
    • gap_*: the gap and its edges: negative, a rate of 0, saturation.
    • filter_*: the start window, the baseline, the sliding minimum over a growing and a shrinking gap with late packets, a sample older than the window, reset, extreme values.
    • loop_*: the filter drives the real loop filter (drift::set_error, step, delta) in simulated time. A sender delivers 100 ms packets that arrive up to 20 ms late, the resampler changes each packet by the loop's correction, and the buffer plays at the system rate. 30 simulated minutes take about 13 ms.
Sender clock Largest gap after the first second Smallest Filter error, last 5 min
+500 ppm, no compensation (control) past 70 ms (asserted)
+500 ppm 39 ms -11 ms ≤ 11 ms
-500 ppm 18 ms -23 ms ≤ 6 ms
0 ppm 19 ms 0 ms ≤ 14 ms
950 ppm (probe, not kept) 50 ms -12 ms ≤ 9 ms
1000 ppm, the clamp (probe, not kept) 51 ms 1 ms 43 ms
1200 ppm 403 ms (asserted > 70 ms)

The loop holds a drift of up to about 950 ppm under the 70 ms step. Past its 1000 ppm clamp, the #4143 step stays the bound. A real crystal is off by about 50 ppm. The error moves by up to about 14 ms with the arrival jitter, which the loop's 10 ms dead band and 1/180 Hz bandwidth absorb.

Tiers: Tier 1 only. No Tier 2, because nothing in libobs calls the new code yet (PR 2). No Tier 3, because it is new code with no C behaviour to compare. No Tier 4, because there is no protocol change.

Test cost: 16 new tests in the existing obs-tests-core binary (no new test executable). The longest takes 13 ms (debug, Linux), and the whole module runs in at most 45 ms over 20 runs, as a process. Peak RSS is 11 MB, with no proptests and no threads of their own.

Local checks: these ran on Linux, because soldr's build daemon does not start on my Mac today. Tests, clippy --all-targets -D warnings (obs-source, obs-data) and rustfmt --check all passed. cargo doc -D warnings on obs-source fails on older files (redundant links in audio_input_path.rs and audio_output.rs, and a link in ffi/source_frame_copy.rs); the new module has no doc warnings. There is no attestation, so PR CI runs the lanes.

🤖 Generated with Claude Code

vpacher and others added 2 commits November 14, 2026 10:10
The clock drift error of audio on a clock of its own: the end of the
buffered audio against each packet's arrival, a sliding minimum over 1 s
and a baseline from the first second. todo!() stubs, plus Tier 1 cases for
the gap, the filter, and four simulated-time runs through the resampler's
loop filter (obs_media_io::audio_resampler_drift) that check a +-500 ppm
sender stays under the 70 ms step of #4143 and settles.

Refs #4182

Co-Authored-By: Claude Opus 5.5 <[email protected]>
buffered_end_gap() is how far the end of a source's buffered audio is
ahead of a packet's arrival: the quantity the resampler's clock drift
compensation changes. A gap measured from the sender's own timestamps
does not change with resampling, so it would only drive the loop into its
clamp. OwnClockDrift filters the gap: a sliding minimum over 1 s (8
buckets of 125 ms, so late packets do not move it), less a baseline taken
over the first second. No allocation, lock or clock.

Not wired into source_output_audio_data() yet; that is PR 2 of #4182.

Tier 1, simulated time through obs_media_io::audio_resampler_drift
(100 ms packets, up to 20 ms late, 30 minutes): without compensation a
+500 ppm sender passes the 70 ms step of #4143; with it, +500 ppm stays
under 39 ms and -500 ppm over -23 ms, and the error settles within 15 ms.
The loop holds up to about 950 ppm; at 1200 ppm, past its 1000 ppm clamp,
the step stays the bound.

Refs #4182

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vpacher
vpacher requested a review from zackees as a code owner November 14, 2026 09:19
@vpacher
vpacher enabled auto-merge November 14, 2026 09:20
@vpacher
vpacher added this pull request to the merge queue Nov 14, 2026
Merged via the queue into master with commit 2b0380d Nov 14, 2026
42 checks passed
@vpacher
vpacher deleted the feat/4182-own-clock-drift-error branch November 14, 2026 10:37
@vpacher vpacher mentioned this pull request Nov 14, 2026
5 of 7 tasks
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.

1 participant