Skip to content

libobs: Compensate for the clock drift of audio devices - #271

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/4600-audio-clock-drift
Oct 11, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/4600-audio-clock-drift

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

Description

Audio capture devices no longer drop out every few minutes because their clock runs slightly off its nominal rate.

libobs places each async audio packet by counting samples from the previous one while the packet's timestamp stays within 70 ms of the count (TS_SMOOTHING_THRESHOLD). A device on its own crystal, a little fast or slow, drifts against that count until libobs resyncs to the timestamp: 70 ms of audio lost (fast device) or 70 ms of silence (slow device), every 500 s at the +140 ppm measured upstream.

  1. libobs: Add failing test for audio device clock drift dropouts: test_audio_clock_drift drives the real input path (obs_source_output_audio, the source's resampler and the timestamp smoothing) with a sine wave from a simulated device whose clock is off, stamped like linux-jack, on a virtual clock so hours of audio take seconds. It counts packets that were not appended seamlessly and clicks in the buffered waveform. Adds obs_source_set_async_compensation() / obs_source_async_compensation() as stubs. 4 of 6 cases fail:
    • +140 ppm, 2 h: 14 dropouts (first at 500.0 s), 28 clicks
    • −140 ppm, 2 h: 14 dropouts, 54 clicks
    • +140 ppm with jitter: 14 dropouts
    • +800 ppm, 1 h: 41 dropouts (first at 87 s)
    • guards that pass: an exact-rate device is untouched, and a source that has not opted in behaves as before.
  2. libobs: Compensate for the clock drift of audio devices: a port of norihiro's asynchronous sample rate conversion (upstream PR libobs: Implement asynchronous sample rate conversion for audio obsproject/obs-studio#6351, credited as co-author). The source feeds how far its counted samples run ahead of their timestamps to its resampler (audio_resampler_set_compensation_error()), where a lag-lead loop filter (unity gain at 1/180 Hz, 60° phase margin) sets a slight swr_set_compensation(), so the samples keep pace with the device clock. All 6 pass: 0 dropouts and 0 clicks at ±140, −300 and ±800 ppm, with and without jitter; the buffer lag stays within 13 to 30 ms; an exact-rate device stays bit-exact.

Differences from #6351:

  • The error is taken from the smoothing decision itself, before timing_adjust and resample_offset, so it is also right for sources whose timestamps are not on the system clock.
  • Errors within 10 ms are ignored, and swr only starts resampling once the error leaves that band, so a device that is on the system clock passes through bit for bit.
  • The correction is capped at ±1000 ppm and set in 0.06 ppm steps.
  • No per-source checkbox: linux-jack, linux-pulseaudio and linux-alsa turn it on, since it stays idle where it is not needed. Other capture plugins (WASAPI, CoreAudio, …) are unchanged and can opt in with one call.

Motivation and Context

Fixes #270 (upstream obsproject/obs-studio#4600, reported by floele; also obsproject/obs-studio#11083 and the Linux/JACK reports in obsproject/obs-studio#12835)

Why this one: #4600 is open since 2021 with 100 comments of people losing audio in recordings and streams every few minutes; Basvg's and norihiro's measurements in that thread pinned it to device clock drift, and norihiro's fix has waited upstream since 2022. This brings it in with a test that reproduces the exact 500 s period.

How Has This Been Tested?

Automated, in both libobs lanes (ENABLE_RUST_LIBOBS OFF and ON), rebased on current master:

  • test_audio_clock_drift: 4 of 6 fail at the RED commit, 6 of 6 pass at GREEN.
  • Full ctest at GREEN: 86 of 86 in each lane, under a private Xvfb with no desktop environment.
  • clang-format and ci/test_registration.py are clean.

Not tested:

  • Real hardware over hours. The test's drifting device is simulated; the loop's behaviour on a real JACK/PulseAudio/ALSA device is not measured yet, and reports from people who hit the dropouts would help.
  • Windows and macOS capture plugins: not enabled by this change.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

rdoupe and others added 2 commits October 11, 2026 15:38
A capture device runs on its own clock, which is never exactly at its
nominal rate. libobs places each packet by counting samples from the
previous one while the packet's timestamp stays within 70 ms of the
count, so a device a few samples per second fast or slow drifts against
the count until libobs resyncs to the timestamp: 70 ms of audio are lost
or 70 ms of silence inserted, every 500 s at the 140 ppm Basvg measured
(obsproject/obs-studio#4600, reported by floele; #11083; JACK reports by
ogmkp on #12835).

Add obs_source_set_async_compensation()/obs_source_async_compensation()
as stubs and a test that feeds the real libobs input path a drifting
device on a virtual clock, hours of audio in seconds, and counts packets
that were not buffered seamlessly and clicks in the buffered waveform.

Refs obs-rust#270.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A capture device runs on its own clock, a little off its nominal rate,
and libobs counts its samples against timestamps from the system clock.
The two drift apart until libobs resyncs with an audible dropout, every
500 s at 140 ppm (obsproject/obs-studio#4600, #11083).

Port the asynchronous sample rate conversion of obsproject/obs-studio#6351:
the source feeds how far its counted samples run ahead of their
timestamps to its resampler, where a lag-lead loop filter (unity gain at
1/180 Hz, 60 degrees of phase margin) sets a slight swr compensation, so
the samples keep pace with the device clock. Differences from #6351:

- The error is taken from the smoothing decision itself, before
  timing_adjust and resample_offset, so it is right for sources whose
  timestamps are not on the system clock too.
- Errors within 10 ms are ignored and swr only starts resampling once
  the error leaves that band, so a device on the system clock passes
  through bit for bit.
- The correction is bounded to 1000 ppm and set in 0.06 ppm steps.
- No per-source setting: linux-jack, linux-pulseaudio and linux-alsa
  enable it, since the compensation stays idle where it is not needed.

Fixes obs-rust#270.

Co-Authored-By: Norihiro Kamae <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 11, 2026 21:50
@zackees
zackees merged commit 297bc02 into obs-rust:master Oct 11, 2026
22 of 23 checks passed
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 11, 2026
obs_startup() needs a display on Linux, and the Tier 2 Linux lane has
none. test_audio_clock_drift (obs-rust#271) and test_group_child_drift (obs-rust#280)
failed their group setup there instead of skipping, which turned that
lane red on master. test_pulse_monitor_device (obs-rust#275) has the same setup
and would fail the same way on a machine with PipeWire but no display.

Like the other core tests, they now skip their cases when libobs cannot
start (and the group test also when core video cannot be reset). Under
xvfb-run every case still runs.

Refs obs-rust#270, obs-rust#274 and obs-rust#279.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 11, 2026
obs_startup() needs a display on Linux, and the Tier 2 Linux lane has
none. test_audio_clock_drift (obs-rust#271) and test_group_child_drift (obs-rust#280)
failed their group setup there instead of skipping, which turned that
lane red on master. test_pulse_monitor_device (obs-rust#275) has the same setup
and would fail the same way on a machine with PipeWire but no display.

Like the other core tests, they now skip their cases when libobs cannot
start (and the group test also when core video cannot be reset). Under
xvfb-run every case still runs.

Refs obs-rust#270, obs-rust#274 and obs-rust#279.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=b23e288a822896090994f89892a7a96b56da5f80 secs=109 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791758098,"gate":"rust/all/fmt","host":"linux-x86_64","key":"9087272be2f5e155c698727d45922688d933308d2c4f08bc86b0f6e131472181","lane":"lint","parents":["35bbf88c776c3a4cb7073dae0b7869a9b75af833"],"secs":15,"stamp":"25c454c9d662469fb5114491251212e2","tree":"b23e288a822896090994f89892a7a96b56da5f80","v":1,"via":"run"}
Ci-Attestation: {"at":1791758098,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"9087272be2f5e155c698727d45922688d933308d2c4f08bc86b0f6e131472181","lane":"lint","parents":["35bbf88c776c3a4cb7073dae0b7869a9b75af833"],"secs":15,"stamp":"103e935b7c7d4896cf51a6593a16411a","tree":"b23e288a822896090994f89892a7a96b56da5f80","v":1,"via":"run"}
Ci-Attestation: {"at":1791758098,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"2163bf0ab811b6bf62c711d254cc4f2bbd240ef3efeeb7f5425a8b720e0132d7","lane":"rust-test","parents":["35bbf88c776c3a4cb7073dae0b7869a9b75af833"],"secs":45,"stamp":"7a7452f3822e6646eb2e2c481ee772ca","tree":"b23e288a822896090994f89892a7a96b56da5f80","v":1,"via":"run"}
Ci-Attestation: {"at":1791758098,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"93153b10b0a7fc6c2e9fe5745e8f1b417bf629afda1d66c21fe151867ea291a7","lane":"tier2-linux","parents":["35bbf88c776c3a4cb7073dae0b7869a9b75af833"],"secs":49,"stamp":"9d46f419860c544d1aeb889a3d125f74","tree":"b23e288a822896090994f89892a7a96b56da5f80","v":1,"via":"run"}
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.

bug: libobs: audio capture drops out every few minutes from device clock drift (upstream #4600)

2 participants