Repository navigation
libobs: Compensate for the clock drift of audio devices - #271
Merged
Merged
Conversation
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]>
This was referenced Oct 11, 2026
bug: libobs: audio capture drops out every few minutes from device clock drift (upstream #4600)
#270
Closed
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"}
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.
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.libobs: Add failing test for audio device clock drift dropouts:test_audio_clock_driftdrives 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. Addsobs_source_set_async_compensation()/obs_source_async_compensation()as stubs. 4 of 6 cases fail: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 slightswr_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:
timing_adjustandresample_offset, so it is also right for sources whose timestamps are not on the system clock.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_LIBOBSOFF and ON), rebased on current master:test_audio_clock_drift: 4 of 6 fail at the RED commit, 6 of 6 pass at GREEN.ci/test_registration.pyare clean.Not tested:
Types of changes
Checklist:
🤖 Generated with Claude Code