Skip to content

libobs: correct own-clock audio drift smoothly (resampler), without the #4143 steps #4182

Closed

Description

@vpacher

Summary

#4143 bounds the delay of audio on its own clock from a fast sender by taking its timing again when a packet would be placed more than 70 ms after its arrival. Each such step overwrites the audio already buffered for those ~60 ms: a short skip, a click on a steady tone. At 50 ppm that is once every 23 minutes. This issue is the smooth correction: the audio keeps pace with the system clock without steps.

Measurement (from #4143)

A stimulus run (voice, clicks, sweep, noise and a 60 s 997 Hz sine, 135 s) through the own-clock test source, sender clock 1.01 times the system clock, program audio recorded from obs_add_raw_audio_callback:

Sender clock Delay from send to program Discontinuities in the 997 Hz sine
1.00 0.0 ms none
1.01, #4143 sawtooth 2 to 63 ms, one step every 7.03 s one at each step: residual -22 to -24 dBFS over about 85 ms

Proposal

Feed the gap between a packet's placement and its arrival (data->timestamp + own_clock_timing_adjust - os_time, smoothed over seconds) to the resampler as compensation, as audio_resampler_set_compensation_error already does for samples against timestamps, so the resampler takes out the extra samples a few at a time. The #4143 step stays as the bound for jumps the resampler cannot follow.

Open points:

  1. A source in the mix's format and rate has no resampler today. It needs one when it is on its own clock (or a simple drop or insert of single samples).
  2. Arrival jitter (network, packet size) must not drive the compensation: a smoothed estimate over several seconds, or a minimum over a window.
  3. A slow sender: today the late-audio path places it again; the same compensation could also cover it.

Done when

The stimulus run at 1.01 (and 0.99) shows a bounded delay and no discontinuities in the sine, and the #4143 Tier 2 case still passes.

Activity

  1. added
    enhancement
    on Nov 9, 2026
  2. vpacher commented on Nov 13, 2026

    @vpacher
    MemberAuthor

    Coordinator triage: the body lists three open design points (no resampler in the mix format, jitter filtering, the slow-sender path). Its "done when" uses the stimulus run, and that tool is not in the repo yet (#4066).

    Missing:

    /investigate

  3. added
    needs-investigation
    on Nov 13, 2026
  4. moved this from Triage to Investigating in obs-rust Multiviewon Nov 14, 2026
  5. vpacher commented on Nov 14, 2026

    @vpacher
    MemberAuthor

    Claiming: #4182

    I read the own-clock path on master (libobs/obs-source-audio-output.c.h, obs-source-audio-timing.c.h, media-io/audio-resampler-drift.h, and the Rust in obs_source::audio_output). Most of what the proposal needs is already in libobs, but the error in the proposal does not settle. Findings first, then the decisions and the plan.

    Findings

    1. The resampler compensation exists. audio_resampler_set_compensation_error() drives a lag-lead loop filter (after obsproject#6351): dead band 10 ms, correction clamped to ±1000 ppm, unity gain at 1/180 Hz. A source with async_compensation on gets a resampler even in the mix format (obs_source_audio_input_resampler_needed()), which passes the audio through until the error leaves the dead band. ALSA and PulseAudio inputs turn it on; an NDI source does not.
    2. Own-clock packets are already appended without gaps. In obs_source_audio_timing_under_lock() a packet within 70 ms of next_audio_sys_ts_min is pushed back onto the buffer, whatever its timestamp. So a packet that the resampler made shorter leaves no hole.
    3. The gap in the proposal does not settle. data->timestamp + own_clock_timing_adjust - os_time is in the sender's time. Resampling changes how many samples are buffered, not the sender's timestamps, so this gap keeps growing at the sender's drift rate, and the loop would only run into its clamp. The quantity that resampling moves is the end of the buffered audio against the arrival time: audio_ts + buffered frames / rate - os_time. A fast sender makes it grow, and a slow one makes it shrink.
    4. The bug: libobs: audio on its own clock from a fast sender plays later and later since #4049 #4143 bound checks the same sender-time gap. With compensation on, it would still step every 23 min at 50 ppm. It has to check the buffered end too.
    5. One error per packet. obs-source-audio-output.c.h:181 already writes pre.drift_error to the resampler on every packet when compensation is on. For own-clock audio, the new error must replace that write, not come after it. For own-clock audio pre.drift_error is about 0 anyway, because the sender stamps its packets from its own sample count.

    Decisions

    1. Resampler in the mix format: libobs turns compensation on by itself for audio on its own clock. The output stage knows that only after process_audio() (on_video_clock). When the first own-clock packet sets own_clock_timing_set, it also sets an internal flag. That flag is ORed into the compensation argument of obs_source_audio_input_reset_needed() and _resampler_needed(), so the next packet resets the resampler once, as a pass-through. The public obs_source_async_compensation() keeps returning what the plugin set.
    2. Jitter: the error is buffered end - arrival - baseline, measured when each packet is appended. The baseline is the first second's minimum (the start offset, like own_clock_timing_adjust), and the error uses a sliding minimum over about 1 s. That takes out packet-size and network jitter and the 21 ms mix-tick phase. The loop's 1/180 Hz bandwidth and 10 ms dead band do the rest. This needs no new filter.
    3. Slow sender: the same error, negative, so the resampler stretches. The loop is symmetric. The existing late-audio paths and the bug: libobs: audio on its own clock from a fast sender plays later and later since #4049 #4143 step stay as bounds for drift beyond ±1000 ppm and for real jumps. The step checks the buffered end (finding 4).

    Tests

    The "done when" in the body cannot pass as written. 1.01 is 10,000 ppm, ten times the clamp, and the loop locks over minutes, which a real-time Tier 2 case cannot wait for. So:

    • Tier 1 and Tier 3 (simulated time): a model of the sender, the mix tick and the buffered-end error, driving audio_resampler_comp_step() without real time. At ±500 ppm with 100 ms packets and ±20 ms arrival jitter, the gap stays bounded under the 70 ms step threshold, it never steps, and it settles near the baseline. The same run finds the largest drift the loop holds. Rust and the C oracle give the same corrections (obs_source::audio_output carries the same placement code).
    • Tier 2 (wiring): an own-clock source in the mix's format and rate gets a resampler, and the compensation engages, within a bounded wait (no fixed sleep, timing lock). A source on the video clock gets none. The bug: libobs: audio on its own clock from a fast sender plays later and later since #4049 #4143 case at 1.10 still passes: beyond the clamp, the step stays the bound.
    • Device check: a long run through obs-avip with a real sender, comparing the delay drift with and without the change.

    Plan

    1. PR 1, RED then GREEN: the buffered-end error and its filter as a pure helper (Tier 1 plus the simulated-time cases), not wired in yet.
    2. PR 2: wire it in (the internal compensation flag, the error replacing drift_error for own-clock audio, the bug: libobs: audio on its own clock from a fast sender plays later and later since #4049 #4143 check on the buffered end) in C and Rust, plus the Tier 2 case and a docs/features.md line.
    3. The device run, with its numbers on the PR.
  6. github-actions commented on Nov 14, 2026

    @github-actions

    Claim bot: @vpacher, this can't be claimed yet: it needs investigation before anyone can implement it. Comment Claiming: investigation to take the research.

  7. vpacher commented on Nov 14, 2026

    @vpacher
    MemberAuthor

    Claiming: investigation

    The findings are in my comment above (the own-clock path, the existing resampler compensation, and why the proposed error does not settle).

  8. vpacher commented on Nov 14, 2026

    @vpacher
    MemberAuthor

    /ready

    The investigation is done: the design points are answered and the acceptance tests are defined in my claim comment above.

  9. added
    ready
    and removed
    needs-investigation
    on Nov 14, 2026
  10. vpacher commented on Nov 14, 2026

    @vpacher
    MemberAuthor

    Claiming: #4182

    The plan and the decisions are in my claim comment above: PR 1 is the buffered-end error and its filter as a pure helper with the simulated-time tests; PR 2 wires it in (C and Rust) with the Tier 2 case.

  11. moved this from Investigating to In progress in obs-rust Multiviewon Nov 14, 2026
  12. moved this from In progress to Ready in obs-rust Multiviewon Nov 15, 2026
  13. vpacher commented on Nov 23, 2026

    @vpacher
    MemberAuthor

    Done: #4856 (PR 1, the buffered-end error and its filter, Tier 1 and Tier 3 with simulated time) and #4864 (PR 2, the wiring in C and Rust, the Tier 2 case, the features line).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementready

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions