Repository navigation
libobs: Apply a sync offset changed from 0 to a running source - #334
Merged
Merged
Conversation
A source's audio sync offset must apply while the source is producing audio. The test registers an audio source that behaves like a live capture: a thread delivers 10 ms packets stamped with the system clock, silent except for a click every 1.5 s. It puts the source on the program and measures how far each click's timestamp in the mixed program audio (obs_add_raw_audio_callback) lags the timestamp it was captured with. Cases: - the offset is changed from 0 to 300 ms on a running source that is on the program (Advanced Audio Properties, a script or a plugin); - obs_load_source() loads a capture with a saved 300 ms offset; the capture produces audio from creation, before the offset is set, as Pulse/PipeWire, macOS Audio Capture and NDI sources do; - the offset is set to 300 ms after the source is shown; - guard: the offset is set before the source produces any audio; - guard: the offset goes 300 -> 600 -> 0 ms. On master, under xvfb (both ENABLE_RUST_LIBOBS lanes), the first three fail with the audio delayed by 0 ms (21 ms, its no-offset baseline, for the first source of the process) instead of 300 ms; the guards pass (299, 599 and 0 ms). Without a display every case skips. Reported upstream in obsproject/obs-studio#7912 by Daniel-Noethen; also #11656 (jetpackpaul, davidtriggs), #11873 (ndfan77) and obs-rust#5356 (MasterZap). Refs obs-rust#333. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Changing the audio sync offset of a source that was producing audio did nothing when the previous offset was 0: the audio kept its old delay. source_output_audio_data() appends each packet that continues the previous one seamlessly to the source's buffer, and the sync offset is only added to a packet's own timestamp, which an appended packet ignores. When the offset changes, the next packet is meant to be placed by its timestamp instead, but since 2016 (41fa9c1) that was skipped whenever the previous offset was 0, presumably to leave the first packet alone. The first packet is placed by its timestamp anyway (the source has no audio_ts yet), so the condition only ever skipped real changes. That is the default for every source, so a change in Advanced Audio Properties, by a script, a plugin or obs-websocket was ignored unless the offset was already non-zero, which is why setting it twice worked. It also broke loading: obs_load_source() creates the source before it sets the saved offset, and a live capture (Pulse/PipeWire, macOS Audio Capture, NDI) produces audio from creation, so its saved offset was ignored until it was changed by hand. Place the next packet by its timestamp after any change of the offset. With the fix, under xvfb (both ENABLE_RUST_LIBOBS lanes), the audio is delayed by 299 ms after a change from 0 to 300 ms on a running source (was 21 ms), after loading a capturing source with a saved 300 ms offset (was 0 ms) and after setting 300 ms on a shown source (was 0 ms); the guards are unchanged (299, 599 and 0 ms). Fixes obs-rust#333. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Local-Gate: v1 tree=1c7da5f9c7cf28562bb11a93941cb7d16878b570 secs=140 lanes=lint:run,rust-test:run,tier2-linux:run Ci-Attestation: {"at":1791789322,"gate":"rust/all/fmt","host":"linux-x86_64","key":"be78b0feff8a3df43723205a88d21d12dc04fed3d2bfcd9a8a50e059e79d24e8","lane":"lint","parents":["056b3cd7799c7371105410644424f0d9f0012a09"],"secs":53,"stamp":"d78b85b77c12b72d5ebc1db497436b35","tree":"1c7da5f9c7cf28562bb11a93941cb7d16878b570","v":1,"via":"run"} Ci-Attestation: {"at":1791789322,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"be78b0feff8a3df43723205a88d21d12dc04fed3d2bfcd9a8a50e059e79d24e8","lane":"lint","parents":["056b3cd7799c7371105410644424f0d9f0012a09"],"secs":53,"stamp":"5636f500159681c53dcd586d6311ba6a","tree":"1c7da5f9c7cf28562bb11a93941cb7d16878b570","v":1,"via":"run"} Ci-Attestation: {"at":1791789322,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"57ddf58eb30c3f6a037c31dda90f2d5a2c39f5188fc9cfd8941ebf07fe60bedc","lane":"rust-test","parents":["056b3cd7799c7371105410644424f0d9f0012a09"],"secs":46,"stamp":"bbbac5dc5048d37c91b291779e077d8e","tree":"1c7da5f9c7cf28562bb11a93941cb7d16878b570","v":1,"via":"run"} Ci-Attestation: {"at":1791789322,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"31d7d39989767414b938efe1879e966f7e3fa30c9f6ed9182fedcb97c1b35cb4","lane":"tier2-linux","parents":["056b3cd7799c7371105410644424f0d9f0012a09"],"secs":42,"stamp":"9eb97821d3ba371868d9c4b0670031ba","tree":"1c7da5f9c7cf28562bb11a93941cb7d16878b570","v":1,"via":"run"}
Member
|
Verified locally, rebased onto master and stamped. The test needs a display, so I ran it with
Merging once CI is green. |
zackees
force-pushed
the
fix/7912-sync-offset-from-zero
branch
from
October 12, 2026 07:15
e1d43e7 to
f7c446e
Compare
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
An audio sync offset now takes effect when it is changed from 0 on a source that is producing audio, and a live capture's saved offset is applied when a scene collection loads.
source_output_audio_data()appends each packet that continues the previous one seamlessly to the source's buffer. The sync offset is only added to a packet's own timestamp, which an appended packet ignores. When the offset changes, the next packet is meant to be placed by its timestamp instead. Since 2016 that was skipped whenever the previous offset was 0 (if (source->last_sync_offset) push_back = false;). 0 is every source's default, so a change in Advanced Audio Properties, by a script, by a plugin or through obs-websocket did nothing. A second change, now from a non-zero value, worked.obs_load_source()creates the source and sets the saved offset afterwards. A live capture (Pulse/PipeWire, macOS Audio Capture, NDI) already produces audio at that point, so its saved offset was ignored after every restart.test/cmocka: Add failing test for a sync offset changed from 0:test_audio_sync_offset_changeregisters an audio source that behaves like a live capture. A thread delivers 10 ms packets stamped with the system clock, silent except for a click every 1.5 s. The test puts the source on the program and measures how far each click's timestamp in the mixed program audio (obs_add_raw_audio_callback) lags the timestamp it was captured with. The cases are:obs_load_source()of a capturing source with a saved 300 ms offset;Today the first three measure 0 ms (21 ms for the first source of the process) instead of 300 ms, and the guards measure 299, 599 and 0 ms. 3 of 5 fail.
libobs: Apply a sync offset changed from 0 to a running source: drops the condition, so the next packet is placed by its timestamp after any change of the offset. The first packet of a source is placed by its timestamp anyway (noaudio_tsyet), so the condition only ever skipped real changes. All cases now measure 299 ms (599 and 0 ms for the guards). 5 of 5 pass.Motivation and Context
Fixes #333 (upstream obsproject/obs-studio#7912, reported by Daniel-Noethen, who found that setting the offset twice works around it). The same bug is reported in obsproject/obs-studio#11656 by jetpackpaul, with an NDI camera report from davidtriggs; in obsproject/obs-studio#11873 by ndfan77, where macOS Audio Capture's offset is not applied at startup; and in obsproject/obs-studio#5356 by MasterZap (with forum thread 148876). There is no upstream PR for it, and no code from upstream is used here.
This is not #286 / PR #287. That fix made the buffer start at the right place after it is reset when a Media Source restarts or its timestamps jump. This PR is about the offset itself changing while audio flows, which is a different branch of
source_output_audio_data(). #287'stest_media_sync_offset_restartpasses before and after this change.Why this one: the sync offset is the standard fix for a camera or capture card that runs ahead of its audio. When it silently does nothing, people end up with recordings that are out of sync, and nothing in the UI tells them so. Reports go back to 2020.
How Has This Been Tested?
Automated, in both libobs lanes (
ENABLE_RUST_LIBOBSOFF and ON), on current master:test_audio_sync_offset_change: 3 of 5 cases fail at the RED commit and 5 of 5 pass at GREEN. All five cases run (about 19 s) under a private Xvfb with software GL.ci/test_registration.pyare clean. The test is registered Linux-only, with the working directory inside the guard.Not tested:
Types of changes
Checklist:
🤖 Generated with Claude Code