Skip to content

libobs: Amend timestamp semantics for async video and audio - #12431

Open
stephematician wants to merge 5 commits into
obsproject:masterfrom
stephematician:async_time_step
Open

stephematician wants to merge 5 commits into
obsproject:masterfrom
stephematician:async_time_step

Conversation

@stephematician

@stephematician stephematician commented Jul 25, 2025 •

Copy link
Copy Markdown
Contributor

Description

This fixes (kludgey?) assignments to last_frame_ts; restoring it to mean exactly the (supplied) timestamp of the last "readied" frame; and replaces last_sys_timestamp with last_frame_sys_ts - which tracks the system (video) time that corresponds to the last readied frames' (supplied) timestamp. This eliminates a faulty comparison between last_frame_ts and the incoming frames' timestamp(s).

Some small adjustments are made to the tolerance mechanism introduced in 11b0fe1 to ensure comparably smooth delivery of frames.

TODO:

  • De-interlaced sources are not yet refactored (need a suitable test) - this will be covered in another PR.
  • This will be broken into smaller atomic commits.
  • A timestamp invalidation check needs to be added back in (need a suitable test).

Motivation and Context

  1. The issue is summarised in Buffered async video lags behind #12459. The issue illustrates a simple example of how the buffered async video (and audio) is broken using the "Sync Test (Async Video/Audio)" source from the test-input plugin.
    Instead of the tone playing while the square is white - the tone is being played while it is black.
  2. Moreover - multiple users have noted drift between "Video Capture Device (V4L2)" sources and the OBS video when using buffered mode; this is due to incoherent updates to last_frame_ts.
  3. Using debug output; it can be seen that master displays frames only when there is at least one more frame available; introducing a latency of one frame (and a false duplicate at the start of the sequence).

How Has This Been Tested?

Using test input sources

I tested this with the "Sync Test (Async Video/Audio Source)" added to a scene and:

  1. Start with OBS opened at 5FPS output, record for about 5-10 seconds; then switch to 50FPS, record for 5-10 seconds, close OBS.
  2. Start with OBS opened at 50FPS, then 5FPS, as above.

With the recorded video I checked frame-by-frame (e.g. with Shotcut) that the audio and video were aligned when expected (i.e. that if the frame was black, the audio at the start of the frame is silent; and for a white frame, the audio starts with the tone).

One-frame latency was eliminated and audio was synchronised.

Gstreamer test source

Started a loopback device using modprobe video_nr=10 and fed it a test via gstreamer:

gst-launch-1.0 videotestsrc horizontal_speed=1 ! \
    'video/x-raw,format=YUY2,width=1280,height=720,framerate=30/1' ! \
    v4l2sink device=/dev/video10

Added a "Video Capture Device (V4L2)" to a scene; and recorded with OBS at 30, 50, and 60 FPS. Checked frame-by-frame for audio synchronisation and consistency of pattern of new frames, e.g.:

  • At 30FPS, no dropped or duplicated frames.
  • At 50FPS, e.g. given 5 frames in recording, the 2nd and 4th duplicated (e.g. T, T, F, T, F if T when new frame)
  • At 60FPS, every frame duplicated once (e.g. T,F,T,F,T,F if T when new frame).

These were all the same results as master; except the one-frame latency is eliminated.

Logitech C270 webcam

Same tests as for gstreamer test feed, but instead of gstreamer, use a real webcam. The camera tested is a Logitech C270 at "30FPS". The Logitech C270 produces frame intervals of 32ms, 32ms, 36ms and an actual frame rate of about 29.85 - so something of a 'stress' test of how to deliver smooth frames.

  • Recorded at 30FPS; frame duplication was minimal (every hundred frames or so due to the misaligned frame rate).
  • Recorded at 50FPS; the desired pattern holds for about 50 (output) frames, then a one-frame correction inserted.
  • Recorded at 60FPS; the desired pattern holds, with a duplication similar to the 30FPS case.

These all had the same (or very similar) result as master; except the one-frame latency is eliminated (false duplication at start of sequence).

Media source (FFMPEG)

Inserted a "Media Source" in the scene playing an MP4 at 25FPS (from sample-videos.com), recorded with OBS at 25, 30, and 50 FPS. Checked frame-by-frame for audio synchronisation and consistency of pattern of new frames.

Audio synchronisation was one-frame better than master (e.g. no longer advanced by one frame compared to video); pattern of new frames as per master except the one-frame latency is eliminated.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

@WizardCM WizardCM added kind/bug Categorizes issue or PR as related to a bug. kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. labels Jul 25, 2025
@stephematician
stephematician force-pushed the async_time_step branch 4 times, most recently from 34c4483 to b46b5d3 Compare July 27, 2025 13:34
@stephematician

This comment was marked as outdated.

@stephematician

stephematician commented Jul 28, 2025 •

Copy link
Copy Markdown
Contributor Author

Another TODO: likely need to add back or adapt the frame 'de-duplication' tolerance

Some de-duplication or smoothing logic was added in 11b0fe1 (see also 3ea98b8 for deinterlaced source), and then adjusted further in 76870d1.

The existing code basically says if a frame is less than 2ms away; we might as well render it.

The existing code seems to short-circuit discarding a frame if the next frame is less than 2ms earlier than the current time (i.e. a frame must be at least 2ms 'old' to be displayed ... but only when we have more than one frame in the queue). This is justified as accounting for 'jitter' in source timestamps - but I don't understand exactly how this approach works. There are only partial answers provided in 76870d1. @jp9000 was the original author - perhaps they can explain the intention of the code.

@stephematician

Copy link
Copy Markdown
Contributor Author

@jpark37 - I noticed in an older commit that you mentioned (for async buffered) "GV-USB2, Buffered: No regression (still broken)."

Can you remember what "broken" meant in this case? Are there any tracked issues you are aware of for this?

@stephematician
stephematician force-pushed the async_time_step branch 3 times, most recently from f2e4c8f to 37e7851 Compare July 31, 2025 08:06
@stephematician
stephematician marked this pull request as ready for review July 31, 2025 08:15
@stephematician

This comment was marked as outdated.

@stephematician stephematician changed the title libobs: Fix offsets in ready_async_frame libobs: Amend timestamp semantics for async video and audio Jul 31, 2025
@stephematician

This comment was marked as resolved.

@stephematician
stephematician force-pushed the async_time_step branch 2 times, most recently from d9e4d1e to 6b9b0ad Compare August 11, 2025 12:03
@derrod
derrod self-requested a review August 11, 2025 12:15
@stephematician

stephematician commented Aug 11, 2025 •

Copy link
Copy Markdown
Contributor Author

Going to attack de-interlaced and 'directly set' timestamps in a different PR - but this at least fixes a number of cases that caused 'drift' for buffered video and fixes #12459 ✅

I was thinking @kkartaltepe might be well-placed to review, too.

@stephematician
stephematician force-pushed the async_time_step branch 2 times, most recently from b9d3c2e to b068ca4 Compare August 12, 2025 01:44
@stephematician

This comment was marked as outdated.

@stephematician

Copy link
Copy Markdown
Contributor Author

Rebased with master and updated the smoothing (in case @derrod is still interesting in reviewing).

I've added some video examples of the difference between master and updated timestamp + smoothing:

  • Recorded at 30FPS.
  • V4L2 source is a Logitech C270 (also at 30FPS) pointed at my screen.
  • Screen is showing a scrolling test pattern (courtesy of gstreamer).
  • I've added a "Sync Test" source with timestamps set to absolute clock - instead of a perfect 'relative' timestamp.
  • The camera is not nice, and delivers frame periods of 32ms, 32ms, 36ms, 32ms, 36ms - so its capture frame rate is around 29.7FPS.

Master branch:
https://github.com/user-attachments/assets/f041881c-84aa-412e-92a8-862eb77a2613

  1. The Sync Test is not in sync, the audio should be playing when the square is white.
  2. When the V4L2 frame is captured too close to video clock (see frame 43), a pause, then skip (dropped frame), then pause occurs.

With updated async timestamp + smoothing:
https://github.com/user-attachments/assets/b2c9c7f8-49cb-4c47-bb03-16ca67463ce4

  1. The Sync Test source is in sync.
  2. When the frame is delivered too close, a one frame pause occurs, then the frame is delivered smoothly. No frames are skipped.

A subtle difference (that I've outlined before) is that the master branch is always keeping one frame in hand (i.e. one frame behind). At 60FPS, this is 16ms of unnecessary latency, which may be perceptible for very sensitive people. At lower FPS it is definitely noticeable.

Small helper function to ensure that the last system timestamp and the
last frame timestamp are updated together
Amends logic of buffered async video tick procedure and the meaning of
'last_frame_ts' and 'last_frame_sys_ts' to be the timestamp from the
source for the last readied frame and the system (video) timestamp when
the last frame was ready, respectively.

Fixes obsproject#12459 which is caused by the faulty test 'last_frame_ts == 0' to
identify the start of a sequence. This generates false-positives when
the first frame has a timestamp of zero. Now we test 'last_frame_sys_ts'
as the system (video) timestamp is almost never zero.

Fixes drift between source timestamps and system timestamps which leads
to bogus lag. This was caused by incorrect accumulation of system
timestamp values.
Limit the number of out-of-sequence frames that are discarded in async
buffered source; after which the timestamp sequence is resent. Provides
more reliable system timestamp estimate when e.g. v4l2-input devices are
interrupted by setting changes.
Amends the smoothing mechanism introduced in 11b0fe1a. Incoming frames
whose estimated delivery is within 1ms of the video clock are held back,
and this triggers a 4ms threshold for readying a frame. After enough
frames have been displayed, the threshold reverts to 1ms.
@stephematician

Copy link
Copy Markdown
Contributor Author

Rebased onto 32.1.1.

@stephematician stephematician reopened this Apr 6, 2026

@derrod derrod left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine for now.

Eventually we'll have to redo the whole async frame cache thing because it's so jank (I'd prefer just a user-configurable fixed-size buffer). But nobody's got time for that right now :(

@stephematician

Copy link
Copy Markdown
Contributor Author

Tagging #13972 for tracking: This PR might need revising in the case that all timestamps are zero.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Categorizes issue or PR as related to a bug. kind/enhancement Enhancements are not bugs or new features but can improve usability or performance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants