Skip to content

fix(gl): prevent periodic PTS jumps from 33ms MAX_DRIFT clamp in GlTi… - #2235

Open
Mike-the-one wants to merge 1 commit into
pedroSG94:masterfrom
Mike-the-one:fix/gltimestamp-drift
Open

Mike-the-one wants to merge 1 commit into
pedroSG94:masterfrom
Mike-the-one:fix/gltimestamp-drift

Conversation

@Mike-the-one

Copy link
Copy Markdown

Problem

In GlTimestamp, setFps(fps) shrinks drift down to 1_000_000_000L / fps (33.3ms at 30fps). Because sourceTimestamp (SurfaceTexture.getTimestamp()) is driven by the camera sensor clock while clockTimestamp comes from System.nanoTime(), two issues occur:

  1. Even on flagship hardware (tested on Galaxy S25+ at 30fps), normal sensor-vs-CPU clock drift (~0.35%) or HandlerThread scheduling jitter causes currentTimestamp to hit clockTimestamp ± 33.3ms every ~9.5 seconds, clamping back to wall-clock time and producing 40–70ms PTS jumps (plus occasional ~5ms catch-up steps).
  2. When forced/static frames run (sourceTimestamp <= lastSourceTimestamp), GlTimestamp advances by clockDelta without deducting that credit from the next real sourceTimestamp delta, causing a jump when real frames resume.

Fix

  • Keep MAX_DRIFT = 500_000_000L (500ms) instead of shrinking it to 1 frame in setFps(fps) so Camera2 preserves smooth sensor deltas while still staying bounded against wall clock.
  • Track forcedAdvanceNs whenever sourceTimestamp <= lastSourceTimestamp and deduct it from the next real sourceDelta.

Verification

Tested on Samsung Galaxy S25+ (1080p @ 30fps MP4 recording checked with ffprobe):

  • Before (2.8.2): 22 PTS jumps (40–71ms / 5.17ms) over ~178s (~every 9.5s).
  • After: 0 PTS jumps (>40ms or <10ms) over a full live recording.

Before the fix

ffprobe -v error -select_streams v:0 -show_entries frame=pts_time,pict_type -of csv=p=0 n.mp4
| awk -F, 'NR>1 { diff=($1-prev)*1000; if (diff > 40 || diff < 10) printf "frame %d (%s): pts=%.3fs JUMP=%.2fms\n", NR, $2, $1, diff } { prev=$1 }'
frame 48 (P): pts=1.580s JUMP=46.48ms
frame 317 (P): pts=10.559s JUMP=46.27ms
frame 326 (P): pts=10.857s JUMP=40.86ms
frame 615 (P): pts=20.504s JUMP=46.60ms
frame 912 (P): pts=30.432s JUMP=61.30ms
frame 921 (P): pts=30.727s JUMP=48.69ms
frame 1191 (P): pts=39.723s JUMP=42.00ms
frame 1450 (P): pts=48.366s JUMP=42.88ms
frame 1719 (P): pts=57.315s JUMP=49.83ms
frame 2016 (P): pts=67.244s JUMP=61.92ms
frame 2284 (P): pts=76.212s JUMP=68.59ms
frame 2582 (P): pts=86.168s JUMP=55.99ms
frame 2591 (P): pts=86.465s JUMP=43.28ms
frame 2851 (I): pts=95.164s JUMP=65.94ms
frame 2859 (P): pts=95.430s JUMP=41.08ms
frame 3118 (P): pts=104.102s JUMP=71.11ms
frame 3686 (P): pts=123.048s JUMP=48.13ms
frame 3954 (P): pts=131.998s JUMP=50.28ms
frame 3959 (P): pts=132.130s JUMP=5.17ms
frame 4790 (P): pts=159.849s JUMP=53.84ms
frame 5087 (P): pts=169.760s JUMP=44.56ms
frame 5096 (P): pts=170.060s JUMP=40.37ms
frame 5355 (P): pts=178.709s JUMP=49.49ms

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant