@@ -28,8 +28,27 @@ const STALL_ADJUSTED_LAG_MS = 33;
2828const HAVE_FUTURE_DATA = 3 ;
2929const AUDIO_START_WAIT_MS = 1500 ;
3030const MEDIA_SYNC_STEER_TOLERANCE_SECONDS = 0.04 ;
31+ const MAX_MEDIA_STEP_SECONDS = 0.25 ;
32+ const MEDIA_STEPS_KEPT = 8 ;
3133
3234type PendingAudioStart = { sinceMs : number ; at : number ; from : number | null } ;
35+ type MediaReading = {
36+ el : HTMLMediaElement ;
37+ time : number ;
38+ steps : number [ ] ;
39+ from : number ;
40+ fromMs : number ;
41+ } ;
42+
43+ const furthestAhead = ( reading : MediaReading | null ) =>
44+ reading ? Math . max ( 0 , ...reading . steps ) : 0 ;
45+
46+ function noteStep ( reading : MediaReading , time : number ) : void {
47+ const step = time - reading . time ;
48+ if ( step > 0 && step <= MAX_MEDIA_STEP_SECONDS )
49+ reading . steps = [ ...reading . steps . slice ( 1 - MEDIA_STEPS_KEPT ) , step ] ;
50+ reading . time = time ;
51+ }
3352
3453export class TransportClock {
3554 private _baseTime = 0 ;
@@ -45,6 +64,8 @@ export class TransportClock {
4564 private _lastReadMs : number | null = null ;
4665 private _lastNow = 0 ;
4766 private _audioStart : PendingAudioStart | null = null ;
67+ private _reading : MediaReading | null = null ;
68+ private _ahead : { el : HTMLMediaElement ; seconds : number } | null = null ;
4869
4970 constructor ( opts ?: {
5071 initialTime ?: number ;
@@ -59,6 +80,7 @@ export class TransportClock {
5980 }
6081
6182 now ( ) : number {
83+ this . _ahead = null ;
6284 if ( this . _playStartMs === null ) return this . _baseTime ;
6385
6486 // Audio-master: drift is impossible because audio IS the clock. Clearing
@@ -97,12 +119,43 @@ export class TransportClock {
97119 const spec = rate ?? 1 ;
98120 const time = timeAtSourceTime ( spec , el . currentTime - mediaStart ) + compositionStart ;
99121 const held = this . _heldUntilAudioOutputStarts ( el . seeking , time ) ;
100- if ( held !== undefined ) return held ;
101- if ( time >= this . _lastNow ) return time ;
122+ if ( held !== undefined ) {
123+ this . _reading = null ;
124+ return held ;
125+ }
126+ const between = this . _betweenUpdates ( el , time ) ;
127+ if ( between !== null ) return between ;
102128 const wrapped = el . loop && this . _lastNow - time > timeAtSourceTime ( spec , el . duration ) / 2 ;
103129 return ! wrapped && el . readyState < HAVE_FUTURE_DATA ? this . _lastNow : null ;
104130 }
105131
132+ // Chromium moves currentTime in 10-50 ms steps, so reading it as is holds a frame, then jumps. Run on at the play
133+ // rate, kept within [element time, element time + its largest recent step], never back; null when time is behind
134+ // the playhead by more than that step (a late clip, not a step).
135+ private _betweenUpdates ( el : HTMLMediaElement , time : number ) : number | null {
136+ const nowMs = this . _nowMs ( ) ;
137+ const last = this . _reading ?. el === el ? this . _reading : null ;
138+ if ( time < this . _lastNow - furthestAhead ( last ) ) return null ;
139+ if ( ! last ) {
140+ this . _reading = { el, time, steps : [ ] , from : time , fromMs : nowMs } ;
141+ return this . _runAhead ( el , time , Math . max ( time , this . _lastNow ) ) ;
142+ }
143+ noteStep ( last , time ) ;
144+ const run = last . from + ( ( nowMs - last . fromMs ) / 1000 ) * this . _rate ;
145+ const held = Math . min ( Math . max ( run , time ) , time + furthestAhead ( last ) ) ;
146+ if ( held !== run ) Object . assign ( last , { from : held , fromMs : nowMs } ) ;
147+ return this . _runAhead ( el , time , Math . max ( held , this . _lastNow ) ) ;
148+ }
149+
150+ private _runAhead ( el : HTMLMediaElement , time : number , now : number ) : number {
151+ this . _ahead = { el, seconds : now - time } ;
152+ return now ;
153+ }
154+
155+ aheadOf ( el : HTMLMediaElement ) : number {
156+ return this . _ahead ?. el === el ? this . _ahead . seconds : 0 ;
157+ }
158+
106159 private _heldUntilAudioOutputStarts ( seeking : boolean , time : number ) : number | null | undefined {
107160 const pending = this . _pendingAudioStart ( ) ;
108161 if ( seeking ) return pending ? this . _lastNow : null ;
@@ -139,6 +192,7 @@ export class TransportClock {
139192 this . _playStartMs = this . _nowMs ( ) ;
140193 this . _audioStart = { sinceMs : this . _playStartMs , at : this . _baseTime , from : null } ;
141194 this . _lastNow = this . _baseTime ;
195+ this . _reading = null ;
142196 // Not a stall: the gap since the clock was last read (possibly a long
143197 // paused idle) says nothing about lost playback time, since none was
144198 // playing. `_applyStallCorrection` treats null as "nothing to compare
@@ -151,6 +205,7 @@ export class TransportClock {
151205 if ( this . _playStartMs === null ) return false ;
152206 this . _baseTime = this . now ( ) ;
153207 this . _playStartMs = null ;
208+ this . _reading = null ;
154209 return true ;
155210 }
156211
@@ -160,6 +215,7 @@ export class TransportClock {
160215 : Math . max ( 0 , timeSeconds ) ;
161216 this . _baseTime = clamped ;
162217 this . _lastNow = clamped ;
218+ this . _reading = null ;
163219 if ( this . _playStartMs !== null ) {
164220 this . _playStartMs = this . _nowMs ( ) ;
165221 this . _audioStart = { sinceMs : this . _playStartMs , at : clamped , from : null } ;
@@ -175,6 +231,7 @@ export class TransportClock {
175231
176232 setRate ( rate : number ) : void {
177233 const safe = Number . isFinite ( rate ) && rate > 0 ? Math . max ( 0.1 , Math . min ( 5 , rate ) ) : 1 ;
234+ this . _reading = null ;
178235 if ( this . _playStartMs !== null ) {
179236 this . _baseTime = this . now ( ) ;
180237 this . _playStartMs = this . _nowMs ( ) ;
@@ -230,6 +287,7 @@ export class TransportClock {
230287 this . _lastReadMs = null ;
231288 }
232289 this . _audioSource = null ;
290+ this . _reading = null ;
233291 }
234292
235293 /** The element the playhead currently follows, if the source is one. */
0 commit comments