Skip to content

feat(libobs): compensate the clock drift of own-clock audio (#4182) - #4864

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

vpacher merged 2 commits into
masterfrom
feat/4182-wire-drift

Conversation

@vpacher

@vpacher vpacher commented Nov 14, 2026

Copy link
Copy Markdown
Member

Refs #4182 (PR 2 of 2: the wiring. The device run on a real NDI sender follows on this PR.)

Problem

Audio on a clock of its own (NDI, #3747) from a sender whose clock drifts from the computer's builds up delay. #4143 bounds it with a step at 70 ms, which skips about 60 ms of audio every 23 minutes at 50 ppm. PR 1 (#4856) added the error that the resampler's clock drift loop needs (obs_source::own_clock_drift), but nothing used it yet.

Change

In C (libobs/obs-source-audio-output.c.h, which is also the C reference and the oracle) and in Rust (obs_source::audio_output):

  • libobs turns compensation on by itself. The first packet on a clock of its own sets own_clock_compensation (atomic, in struct obs_source). The input path ORs it into async_compensation where it makes the resampler (reset_resampler(), process_audio(), and the input-path seam that the Rust input path reads). So the next packet gets a pass-through resampler. obs_source_async_compensation() still returns what the plugin set. Audio on its video's clock, and a source with no video (microphones, desktop audio, media files), never set it.
  • The error. For such a source, the resampler gets the filtered gap between the end of the buffered audio and the packet's arrival, after each appended packet. Before, it got the timestamp error, which is about 0 for a sender that stamps by its own sample count. While the filter has no baseline (the first second), the error is 0, so the loop holds. A packet that arrives on the video's clock gives no error.
  • The bug: libobs: audio on its own clock from a fast sender plays later and later since #4049 #4143 step checks the buffered end. It used to check the sender's time, which compensation does not move, so it would still have stepped every 23 minutes. Now it steps when the buffer holds more than 70 ms more than right after a packet placed at its arrival (counted from the sync offset less the resampler's offset). Then the packet takes its timing again and is placed at its arrival. It does not wait for the filter.
  • Placement after long drift. With compensation on, the sender's time runs away from the arrival (about 180 ms an hour at 50 ppm). So a packet on its own clock that is placed instead of appended (a changed sync offset, a gap) takes its timing from its arrival first, and lands there. A placed packet starts the filter again.
  • The filter state lives in C as struct obs_source_own_clock_drift (libobs/obs-source-own-clock-drift.h). The header has the same filter as static inline functions for the C side; Rust converts it with OwnClockDrift::to_raw() and from_raw(). The seam gets one get and one set for the flag and for the filter.
  • docs/features.md: a line under the bug: libobs: audio on its own clock from a fast sender plays later and later since #4049 #4143 entry.

Tests

RED 98dde6b: the tests and the plumbing (struct, fields, seam accessors, raw conversion), with the old behaviour. On Linux (Debian 13, x86_64), 10 cases failed:

  • Tier 1, 9 cases: the flag is never set, the steady buffer is cut, the buffer over 70 ms is not cut, the placed packet keeps the sender's time, and the error is the timestamp error before the append.
  • Tier 3, 1 case: own_clock_compensation_and_its_step_match (the flag is never set).

GREEN (this commit):

  • Tier 1 (obs_source::audio_output, fake source):
    • The first own-clock packet turns compensation on. Video-clock audio (no frame, or a frame on the audio's clock) never does.
    • A steady buffer is appended however far the sender's time runs ahead.
    • A buffer more than 70 ms over a placement is cut at once. The step counts from the sync offset: 60 ms over stays, 80 ms over is cut.
    • A placed packet lands at its arrival, and it starts the filter again.
    • The resampler gets the filtered error after the append, 0 while the filter starts, and nothing for a video-clock packet of a compensated source.
  • Tier 1 (own_clock_drift): the raw struct round-trips at every stage, all zeroes is a new filter, a field whose flag is clear is not read, and excess() saturates step by step.
  • Tier 3 (audio_output_parity, C oracle against the Rust shims):
    • A new case: a 5 % fast own-clock sender with compensation, 160 packets. The filter gets its baseline, the resampler gets its errors, and the step cuts twice. Log, fields and buffer bytes are compared after every packet.
    • The proptest also draws the flag, and the oracle compares the flag and every filter field.
  • Tier 2 (test_async_audio_own_clock, both libobs and the C reference):
  • Tier 4: not applicable (no protocol change).

Local checks: the full local gate passed on Linux x86_64 and stamped this commit. That covers lint, dylint, the Rust tests, Tier 2 in both modes (932/932 against the C reference and 932/932 against the hybrid libobs), the cross clippy and dylint lanes for Windows and macOS, and the client lanes. The macOS and Windows native lanes do not run on this host, so PR CI runs them.

Test cost: 15 new Rust cases in the existing obs-tests-core binary (no new test executable). The 65 cases of the three touched modules run in 0.23 s together (debug, Linux), with no threads of their own. The 2 new Tier 2 cases take about 5 s each (real time, in the existing timing resource lock).

Not done here: the device run. A 2-hour NDI run on our Linux box, with and without this change, comparing the delay drift. I will post its numbers on this PR. Until then this is Refs, not Fixes.

🤖 Generated with Claude Code

vpacher and others added 2 commits November 14, 2026 16:19
…dio (#4182)

Audio on a clock of its own (NDI) from a fast sender still builds up delay
until the #4143 step cuts it. PR 2 of #4182 turns the resampler's clock
drift compensation on for it, with the filtered buffered-end error of
PR 1. This commit adds the tests and the plumbing they need, not the
behaviour:

- struct obs_source_own_clock_drift (libobs/obs-source-own-clock-drift.h),
  the C form of obs_source::own_clock_drift's filter, with the gap, the
  excess over a placement and the filter as static inline functions; the
  fields own_clock_compensation and own_clock_drift in struct obs_source
  and their seam accessors; OwnClockDrift::to_raw/from_raw and excess().
- Tier 1 (obs_source::audio_output): the first own-clock packet turns the
  compensation on, video-clock audio never does; a steady buffer is
  appended however far the sender's time runs ahead; a buffer more than
  70 ms over a placement is cut at once, counted from the sync offset; a
  placed packet lands at its arrival and restarts the filter; the
  resampler gets the filtered error after the append, 0 while the filter
  starts, and none for a video-clock packet of a compensated source.
- Tier 1 (own_clock_drift): the raw struct round-trips, zeroes are a new
  filter, excess() saturates step by step as the C.
- Tier 3: the oracle dumps the new fields; a 5 % fast own-clock sender
  with compensation; the proptest draws the flag.
- Tier 2: test_async_audio_own_clock: a sender 1 % fast on its own clock
  is resampled; audio on its video's clock is not.

Refs #4182

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Audio on a clock of its own (NDI, #3747) from a sender whose clock drifts
from the system clock built up delay until the #4143 step cut about 60 ms
of it, every 23 minutes at 50 ppm. libobs now turns the resampler's clock
drift compensation on for it, and feeds it the error of PR 1
(obs_source::own_clock_drift), in C (obs-source-audio-output.c.h, the C
reference and the oracle) and in Rust (obs_source::audio_output):

- The first packet on a clock of its own sets own_clock_compensation. The
  input path ORs it into async_compensation where it makes the resampler
  (reset_resampler(), process_audio() and the input-path seam), so the next
  packet gets a pass-through resampler. obs_source_async_compensation()
  still answers what the plugin set. Audio on its video's clock, and a
  source with no video, never set it.
- For such a source the resampler gets the filtered end of the buffered
  audio against the arrival, after each appended packet, instead of the
  timestamp error, which is about 0 for a sender that stamps by its own
  sample count; 0 (hold) while the filter starts; nothing for a packet that
  arrives on the video's clock.
- The #4143 step now checks the buffered end, not the sender's time, which
  compensation does not move: when the buffer holds more than 70 ms more
  than right after a packet placed at its arrival (counted from the sync
  offset), the packet takes its timing again and is placed at its arrival.
  No wait for the filter.
- With compensation on, the sender's time runs away from the arrival, so a
  packet on its own clock that is placed instead of appended (a changed sync
  offset, a gap) takes its timing from its arrival first. A placed packet
  starts the filter again.

Refs #4182

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=188ee17d8a997ac64375787a441f541d6114f153 secs=371 lanes=lint:reused@0bcd0165eaad,dylint:reused@b8b7868db709,rust-test:reused@7778bebbcf91,cross:reused@2dafe648bef9,tier2-linux:reused@0cc1cde42a6e,macos-native:n/a,windows-native:n/a,client-checks:run,client-swift:n/a,client-lint:run,client-test:run,client-test-x86_64-unknown-linux-gnu:run,client-test-aarch64-unknown-linux-gnu:n/a,client-test-x86_64-pc-windows-msvc:n/a,client-test-aarch64-pc-windows-msvc:n/a,client-test-x86_64-apple-darwin:n/a,client-test-aarch64-apple-darwin:n/a
Ci-Attestation: {"at":1794672128,"gate":"rust/all/fmt","host":"linux-x86_64","key":"0bcd0165eaadf7b8110021b020c5a9f244fddd2076b65f808b2d128ce728bb15","lane":"lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":null,"stamp":"9c42c509d12b24a6f25041e2ac81c3a1","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"reused"}
Ci-Attestation: {"at":1794672128,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"0bcd0165eaadf7b8110021b020c5a9f244fddd2076b65f808b2d128ce728bb15","lane":"lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":null,"stamp":"cd0dabc660d022c901caddb06b793c24","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"reused"}
Ci-Attestation: {"at":1794672128,"gate":"rust/x86_64-unknown-linux-gnu/dylint","host":"linux-x86_64","key":"b8b7868db7095a9d86176031f73958149ac8556dcb0bc46aefe799a302b340e4","lane":"dylint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":null,"stamp":"e5c3634f5eb75901be78f40bae456f64","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"reused"}
Ci-Attestation: {"at":1794672128,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"7778bebbcf91028b32589496b7052c625d40dab84ce3a1c9d349ceec5a8ec260","lane":"rust-test","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":null,"stamp":"033ec1e79e2404079cc136d1dfaec264","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"reused"}
Ci-Attestation: {"at":1794672264,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"2dafe648bef974da03e97f5311dd3b5838f49c7ed68ba3156be433e1d25963d9","lane":"cross","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":null,"stamp":"54a6c4943b4f6f6af1edea688073eb2a","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"reused"}
Ci-Attestation: {"at":1794672264,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"2dafe648bef974da03e97f5311dd3b5838f49c7ed68ba3156be433e1d25963d9","lane":"cross","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":null,"stamp":"1a324cd5a08f04b8a81d8a5d1218b6a0","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"reused"}
Ci-Attestation: {"at":1794672264,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"2dafe648bef974da03e97f5311dd3b5838f49c7ed68ba3156be433e1d25963d9","lane":"cross","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":null,"stamp":"51e41783a4d0752edc2a2d037d030f38","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"reused"}
Ci-Attestation: {"at":1794672264,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"2dafe648bef974da03e97f5311dd3b5838f49c7ed68ba3156be433e1d25963d9","lane":"cross","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":null,"stamp":"6a43b54cc6e64813ddee259223f6ccc9","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"reused"}
Ci-Attestation: {"at":1794672128,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"0cc1cde42a6e4520ef6dc3c8362b22e25dec884d75e9312ff26f957a87a449fc","lane":"tier2-linux","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":null,"stamp":"14575b1f2e99de5f734219a5555825d7","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"reused"}
Ci-Attestation: {"at":1794672661,"gate":"python/all/self-test","host":"linux-x86_64","key":"2f3ad0fc93ca4c5306ae2b221eb0a7f0f8c8d97d11912144a5e5045ff8525454","lane":"client-checks","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":39,"stamp":"0513567333e0a3412026281b67730b82","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
Ci-Attestation: {"at":1794672661,"gate":"general/all/client-lint","host":"linux-x86_64","key":"bfc7cf56855c5d0b25a27893ad5300ac92e7661c046a126689fbd9099c61e8ce","lane":"client-lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":129,"stamp":"f2f18420f5b08c3a1193bed1fcbe59d1","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
Ci-Attestation: {"at":1794672661,"gate":"rust/x86_64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"bfc7cf56855c5d0b25a27893ad5300ac92e7661c046a126689fbd9099c61e8ce","lane":"client-lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":129,"stamp":"08b27f62b5a22bd6d92bd15796fbb2b2","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
Ci-Attestation: {"at":1794672661,"gate":"rust/x86_64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"bfc7cf56855c5d0b25a27893ad5300ac92e7661c046a126689fbd9099c61e8ce","lane":"client-lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":129,"stamp":"a9397e3ec9bfef33429824dfe4fa0fcb","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
Ci-Attestation: {"at":1794672661,"gate":"rust/aarch64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"bfc7cf56855c5d0b25a27893ad5300ac92e7661c046a126689fbd9099c61e8ce","lane":"client-lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":129,"stamp":"dfef7c4e82edeefeb7c7adf3a4c8d411","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
Ci-Attestation: {"at":1794672661,"gate":"rust/aarch64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"bfc7cf56855c5d0b25a27893ad5300ac92e7661c046a126689fbd9099c61e8ce","lane":"client-lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":129,"stamp":"17e29c42ff1590ac18e2861739c9383b","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
Ci-Attestation: {"at":1794672661,"gate":"rust/x86_64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"bfc7cf56855c5d0b25a27893ad5300ac92e7661c046a126689fbd9099c61e8ce","lane":"client-lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":129,"stamp":"dd25549bcf57e2d75b23da1e9f3b83ff","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
Ci-Attestation: {"at":1794672661,"gate":"rust/x86_64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"bfc7cf56855c5d0b25a27893ad5300ac92e7661c046a126689fbd9099c61e8ce","lane":"client-lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":129,"stamp":"8f11785ee38cd40a909a80827fe5b0d2","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
Ci-Attestation: {"at":1794672661,"gate":"rust/aarch64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"bfc7cf56855c5d0b25a27893ad5300ac92e7661c046a126689fbd9099c61e8ce","lane":"client-lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":129,"stamp":"e0fe166882236c9e24e53695aacb577d","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
Ci-Attestation: {"at":1794672661,"gate":"rust/aarch64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"bfc7cf56855c5d0b25a27893ad5300ac92e7661c046a126689fbd9099c61e8ce","lane":"client-lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":129,"stamp":"7d3cd977b2ec4c6cd22a993c19e6d7b0","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
Ci-Attestation: {"at":1794672661,"gate":"rust/x86_64-apple-darwin/client-clippy","host":"linux-x86_64","key":"bfc7cf56855c5d0b25a27893ad5300ac92e7661c046a126689fbd9099c61e8ce","lane":"client-lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":129,"stamp":"af23df40becd7dc2937e5b6e7f212174","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
Ci-Attestation: {"at":1794672661,"gate":"rust/x86_64-apple-darwin/client-dylint","host":"linux-x86_64","key":"bfc7cf56855c5d0b25a27893ad5300ac92e7661c046a126689fbd9099c61e8ce","lane":"client-lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":129,"stamp":"acf8dd5c2f79baa14a2d5054084b8fbe","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
Ci-Attestation: {"at":1794672661,"gate":"rust/aarch64-apple-darwin/client-clippy","host":"linux-x86_64","key":"bfc7cf56855c5d0b25a27893ad5300ac92e7661c046a126689fbd9099c61e8ce","lane":"client-lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":129,"stamp":"c389efc0c2016905435105fd1ad72464","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
Ci-Attestation: {"at":1794672661,"gate":"rust/aarch64-apple-darwin/client-dylint","host":"linux-x86_64","key":"bfc7cf56855c5d0b25a27893ad5300ac92e7661c046a126689fbd9099c61e8ce","lane":"client-lint","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":129,"stamp":"d60b7c5f6a5222a8ddbace6e88c48b1d","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
Ci-Attestation: {"at":1794672661,"gate":"rust/x86_64-unknown-linux-gnu/client-test","host":"linux-x86_64","key":"99d89cf06a66bd524ae7d4e4d14a6b0d2474bd367b6cac13c714927406873c82","lane":"client-test-x86_64-unknown-linux-gnu","parents":["98dde6badd00f294af13ba6104a08e06899307a1"],"secs":100,"stamp":"70fb74501c56b43b40af9d6ef0d7204d","tree":"188ee17d8a997ac64375787a441f541d6114f153","v":1,"via":"run"}
@vpacher
vpacher requested a review from zackees as a code owner November 14, 2026 16:12
@vpacher
vpacher enabled auto-merge November 14, 2026 16:13
@vpacher
vpacher added this pull request to the merge queue Nov 14, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 14, 2026
@vpacher
vpacher added this pull request to the merge queue Nov 14, 2026
Merged via the queue into master with commit 22cae82 Nov 14, 2026
41 checks passed
@vpacher
vpacher deleted the feat/4182-wire-drift branch November 14, 2026 18:21
@vpacher vpacher mentioned this pull request Nov 14, 2026
7 tasks done
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