Repository navigation
feat(obs-source): the own-clock drift error and its filter (#4182) - #4856
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 usesi128internally and saturates at thei64range; a rate of 0 counts no frames.OwnClockDrift: the filter that gives the error foraudio_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 ofdrift_error, and the #4143 bound checked on the buffered end. PR 2 also adds the Tier 2 case and thedocs/features.mdline.Tests
todo!()stubs. On Linux (Debian 13, x86_64), all 15 new cases inobs-tests-corefailed at the stubs.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.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-corebinary (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) andrustfmt --checkall passed.cargo doc -D warningson obs-source fails on older files (redundant links inaudio_input_path.rsandaudio_output.rs, and a link inffi/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