Skip to content

libobs: Keep the sync offset when an audio buffer is reset - #287

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/11640-media-sync-offset
Oct 11, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/11640-media-sync-offset

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

Description

A Media Source keeps its audio sync offset (Advanced Audio Properties) when it starts or restarts playback.

When playback starts, obs_source_show_preloaded_video() empties the source's audio buffer so the audio continues from the preloaded frame, and reset_audio_data() made the buffer start at the current system time. source_output_audio_data() appends every packet that continues seamlessly from there to the buffer as it is, so the sync offset, which is only added to a packet's own timestamp, never applied to them. handle_ts_jump() resets the buffer the same way after a timestamp jump of more than 2 s. Whether the packets after the reset continue seamlessly depends on the media timeline: upstream they did after a scene round trip; here they do when the first Media Source of the process starts.

  1. test/cmocka: Add failing test for a Media Source losing its sync offset: test_media_sync_offset_restart generates a clip (NUT, raw video and PCM audio) with a flash and a click at 300 ms and plays it through the real ffmpeg_source with a 300 ms offset, measuring how far the program audio's click lags the program video's flash before and after a round trip through a second scene with another media source:

    • looping clip, first activation: −69 ms (fails; should be ~230 ms)
    • looping clip after the round trip, a long (2.5 s) time away, and an ended clip: 233 ms (pass, guards)
    • no-offset guard: −66 ms before and after (the constant A/V baseline of the test, the same on master)
    • after obs_source_show_preloaded_video() and after a >2 s timestamp jump, driven directly: the buffer starts 0 ms after the reset (fails; should be 300 ms)

    3 of 6 fail.

  2. libobs: Keep the sync offset when an audio buffer is reset: both reset paths start the buffer at that system time shifted by the sync offset (and the resampler offset), where a packet placed on its own lands. Packets placed by their own timestamp are unchanged. First activation 221 ms, round trips 233 ms, buffer starts 299-300 ms after the reset. 6 of 6 pass.

Motivation and Context

Fixes #286 (upstream obsproject/obs-studio#11640, reported by Penwy, reproduced on Linux and Windows)

Why this one: an audio delay is how people line up a microphone or capture card with a delayed video source; losing it silently mid-stream leaves the stream out of sync until the source is touched. No upstream fix exists.

How Has This Been Tested?

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

  • test_media_sync_offset_restart: 3 of 6 fail at the RED commit (checked 3 times), 6 of 6 pass at GREEN (checked 4 times); the numbers are identical in both lanes.
  • Full ctest at GREEN: 92 of 92 in each lane, under a private Xvfb with software GL.
  • Without a display the test's cases skip, like the other core tests.
  • clang-format, gersemi and ci/test_registration.py are clean.

Not tested:

  • The exact upstream sequence (failure only after a round trip): in this fork the end-to-end failure shows at the first activation and the round-trip cases pass on master; the cause and both reset paths are covered by the direct cases.
  • Media Source with hardware decoding or the full-decode cache, a real recording output, Windows and macOS. The change is platform-independent C in libobs.

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 Media Source with an audio sync offset (Advanced Audio Properties) must
keep it when it restarts on activation. The test plays a generated clip,
with a flash and a click at the same moment, through the real
ffmpeg_source with a 300 ms offset, and measures how far the click on the
program audio lags the flash on the program video, when the scene is first
shown and after a round trip through a second scene. Two more cases drive
the libobs buffer resets directly: showing a preloaded frame (what a Media
Source does when it starts) and an audio timestamp jump over 2 s, and
check that the packets after the reset still start 300 ms late.

On master, under xvfb (both ENABLE_RUST_LIBOBS lanes):
- the first Media Source of the process plays its audio 69 ms ahead of the
  picture instead of 231 ms behind (the 300 ms offset less the -66 ms the
  no-offset guard measures): the offset is gone;
- after showing a preloaded frame and after a timestamp jump the buffer
  starts 0 ms after the reset instead of 300 ms.
The round-trip cases and the no-offset guard pass (233 ms / -66 ms).

Reported upstream in obsproject/obs-studio#11640 by Penwy.

Refs obs-rust#286.

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 23:24
A Media Source lost its audio sync offset (Advanced Audio Properties)
when it restarted. Starting playback shows the preloaded first frame, and
obs_source_show_preloaded_video() then empties the source's audio buffer
so the audio continues from that frame. It made the buffer start at the
current system time, and source_output_audio_data() appends every packet
that continues seamlessly from there to the buffer as it is, so the sync
offset, which is only added to a packet's own timestamp, never applied to
them. handle_ts_jump() resets the buffer the same way after a timestamp
jump of more than 2 s. Whether the packets after the reset continue
seamlessly depends on the media timeline: upstream they did after a scene
round trip, here they do when the first Media Source of the process starts.

Start the buffer after either reset at that system time shifted by the
sync offset (and the resampler offset), where a packet placed on its own
lands. Packets placed by their own timestamp are unchanged.

With the fix, under xvfb (both ENABLE_RUST_LIBOBS lanes), the first Media
Source plays its audio 221 ms behind the picture (the 300 ms offset less
the -66 ms of the no-offset guard; was -69 ms), the round trips stay at
233 ms, and after a preloaded frame or a timestamp jump the buffer starts
299-300 ms after the reset (was 0 ms).

Fixes obs-rust#286.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=58cdce065628c8b072aa9360fdcba484aa8632bf secs=139 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791761727,"gate":"rust/all/fmt","host":"linux-x86_64","key":"945d65e8f9fc0105345ed40a199425d8591e5920cc9ef630975630850a6402eb","lane":"lint","parents":["8e52822d2819cf3d60539a6f0d747940b7310dde"],"secs":51,"stamp":"0ce6d3d91a71afdce6d0b1f88d91f45f","tree":"58cdce065628c8b072aa9360fdcba484aa8632bf","v":1,"via":"run"}
Ci-Attestation: {"at":1791761727,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"945d65e8f9fc0105345ed40a199425d8591e5920cc9ef630975630850a6402eb","lane":"lint","parents":["8e52822d2819cf3d60539a6f0d747940b7310dde"],"secs":51,"stamp":"e1693825706b6332382e70d18c6ce232","tree":"58cdce065628c8b072aa9360fdcba484aa8632bf","v":1,"via":"run"}
Ci-Attestation: {"at":1791761727,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"21217e3b94b5886239738c6f6887760e19198a0c48a5511dede59ffa491af1eb","lane":"rust-test","parents":["8e52822d2819cf3d60539a6f0d747940b7310dde"],"secs":46,"stamp":"b6582d6c9789b2b0e40e1be5c7fdca34","tree":"58cdce065628c8b072aa9360fdcba484aa8632bf","v":1,"via":"run"}
Ci-Attestation: {"at":1791761727,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"c0b2746d36092d38627d12341e85d0260e10706e6c5eea81663441111f294e21","lane":"tier2-linux","parents":["8e52822d2819cf3d60539a6f0d747940b7310dde"],"secs":43,"stamp":"ec55e95c9e684521314860e6232531f6","tree":"58cdce065628c8b072aa9360fdcba484aa8632bf","v":1,"via":"run"}
@zackees

zackees commented Oct 11, 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 (8e52822d2): 4 of 6 passed. preloaded_frame_keeps_offset and timestamp_jump_keeps_offset failed.
  • GREEN (34eb9e1b8): the test passed. Plain Tier 2 passed 92 of 92 with ENABLE_RUST_LIBOBS OFF and 92 of 92 with it ON, and the local gate passed.

Merging once CI is green.

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: Media Source loses its audio sync offset when it restarts (upstream #11640)

2 participants