Repository navigation
libobs: Keep the sync offset when an audio buffer is reset - #287
Merged
Merged
Conversation
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]>
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"}
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/11640-media-sync-offset
branch
from
October 11, 2026 23:35
34eb9e1 to
bf3c8ea
Compare
This was referenced Oct 12, 2026
Closed
Closed
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
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, andreset_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.test/cmocka: Add failing test for a Media Source losing its sync offset:test_media_sync_offset_restartgenerates a clip (NUT, raw video and PCM audio) with a flash and a click at 300 ms and plays it through the realffmpeg_sourcewith 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: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.
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_LIBOBSOFF 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.ci/test_registration.pyare clean.Not tested:
Types of changes
Checklist:
🤖 Generated with Claude Code