Skip to content

libobs: Apply a sync offset changed from 0 to a running source - #334

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/7912-sync-offset-from-zero
Oct 12, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/7912-sync-offset-from-zero

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

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.
  1. test/cmocka: Add failing test for a sync offset changed from 0: test_audio_sync_offset_change 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. 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:

    • 0 -> 300 ms on a running source;
    • obs_load_source() of a capturing source with a saved 300 ms offset;
    • 300 ms set after the source is shown;
    • guard: offset set before any audio;
    • guard: 300 -> 600 -> 0 ms.

    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.

  2. 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 (no audio_ts yet), 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's test_media_sync_offset_restart passes 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_LIBOBS OFF 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.
  • Full ctest at GREEN: 105 of 105 in each lane. Without a display, the new test's cases skip like the other core tests.
  • clang-format, gersemi and ci/test_registration.py are clean. The test is registered Linux-only, with the working directory inside the guard.

Not tested:

  • Real capture devices, real NDI or macOS sources, or the OBS UI. The test source copies how a live capture stamps its packets (system clock, steady 10 ms periods).
  • Audio monitoring. Monitoring takes the source's audio before the offset is applied, and only the Windows (WASAPI) monitor reads the offset itself. That behaviour is unchanged and not tested here.
  • A change from 0 to a negative offset. It goes through the same placement, which resets the buffer when the new packet lands before it starts, as 600 -> 0 ms does in the guard. It is not a separate case.
  • The part of Audio Sync Offset resets when opening the Advanced Audio Properties window. obsproject/obs-studio#11656 where just opening Advanced Audio Properties seemed to undo a working offset. The dialog does not set the offset when it opens (the spin box is connected after its value is set, and an unchanged value returns early), so this PR does not claim to explain that observation.
  • At offset 0, the first source of the process measures 21 ms (one 1024-frame mixer tick) until a packet is placed by its timestamp. That happens before and after this change, and it is not investigated here.

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

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]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 12, 2026 07:04
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"}
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Verified locally, rebased onto master and stamped. The test needs a display, so I ran it with xvfb-run ctest plus Mesa on each commit's Tier 2 build:

  • RED (056b3cd77): 2 of 5 passed. offset_from_zero_applies_to_running_source, offset_set_after_activation_applies and saved_offset_applies_to_live_capture_on_load failed.
  • GREEN (e1d43e78b): 5 of 5 passed. Plain Tier 2 passed 107 of 107 with ENABLE_RUST_LIBOBS OFF and 107 of 107 with it ON, and the local gate passed.

Merging once CI is green.

@zackees
zackees force-pushed the fix/7912-sync-offset-from-zero branch from e1d43e7 to f7c446e Compare October 12, 2026 07:15
@zackees
zackees merged commit af725bc into obs-rust:master Oct 12, 2026
23 checks passed
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: a sync offset changed from 0 is ignored on a running source (upstream #7912)

2 participants