@@ -26,6 +26,10 @@ export type AudioClockSource =
2626const STALL_THRESHOLD_MS = 500 ;
2727const STALL_ADJUSTED_LAG_MS = 33 ;
2828const HAVE_FUTURE_DATA = 3 ;
29+ const AUDIO_START_WAIT_MS = 1500 ;
30+ const MEDIA_SYNC_STEER_TOLERANCE_SECONDS = 0.04 ;
31+
32+ type PendingAudioStart = { sinceMs : number ; at : number ; from : number | null } ;
2933
3034export class TransportClock {
3135 private _baseTime = 0 ;
@@ -40,6 +44,7 @@ export class TransportClock {
4044 /** Wall-clock time of the last `now()` read while playing; null while paused. */
4145 private _lastReadMs : number | null = null ;
4246 private _lastNow = 0 ;
47+ private _audioStart : PendingAudioStart | null = null ;
4348
4449 constructor ( opts ?: {
4550 initialTime ?: number ;
@@ -89,14 +94,32 @@ export class TransportClock {
8994 ) : number | null {
9095 const { el, compositionStart, mediaStart, rate } = source ;
9196 if ( el . paused || ! Number . isFinite ( el . currentTime ) ) return null ;
92- if ( el . seeking ) return null ;
9397 const spec = rate ?? 1 ;
9498 const time = timeAtSourceTime ( spec , el . currentTime - mediaStart ) + compositionStart ;
99+ const held = this . _heldUntilAudioOutputStarts ( el . seeking , time ) ;
100+ if ( held !== undefined ) return held ;
95101 if ( time >= this . _lastNow ) return time ;
96102 const wrapped = el . loop && this . _lastNow - time > timeAtSourceTime ( spec , el . duration ) / 2 ;
97103 return ! wrapped && el . readyState < HAVE_FUTURE_DATA ? this . _lastNow : null ;
98104 }
99105
106+ private _heldUntilAudioOutputStarts ( seeking : boolean , time : number ) : number | null | undefined {
107+ const pending = this . _pendingAudioStart ( ) ;
108+ if ( seeking ) return pending ? this . _lastNow : null ;
109+ if ( ! pending ) return undefined ;
110+ pending . from ??= time ;
111+ const startsHere = Math . abs ( pending . from - pending . at ) <= MEDIA_SYNC_STEER_TOLERANCE_SECONDS ;
112+ if ( startsHere && time === pending . from ) return time ;
113+ this . _audioStart = null ;
114+ return undefined ;
115+ }
116+
117+ private _pendingAudioStart ( ) : PendingAudioStart | null {
118+ const pending = this . _audioStart ;
119+ if ( pending && this . _nowMs ( ) - pending . sinceMs > AUDIO_START_WAIT_MS ) this . _audioStart = null ;
120+ return this . _audioStart ;
121+ }
122+
100123 /** Folds a >500ms gap since the last read into `_playStartMs` so it's never reported as played time. */
101124 private _applyStallCorrection ( ) : void {
102125 if ( this . _playStartMs === null ) return ;
@@ -114,6 +137,7 @@ export class TransportClock {
114137 if ( this . _playStartMs !== null ) return false ;
115138 if ( this . _baseTime >= this . getEnd ( ) ) return false ;
116139 this . _playStartMs = this . _nowMs ( ) ;
140+ this . _audioStart = { sinceMs : this . _playStartMs , at : this . _baseTime , from : null } ;
117141 this . _lastNow = this . _baseTime ;
118142 // Not a stall: the gap since the clock was last read (possibly a long
119143 // paused idle) says nothing about lost playback time, since none was
@@ -138,6 +162,7 @@ export class TransportClock {
138162 this . _lastNow = clamped ;
139163 if ( this . _playStartMs !== null ) {
140164 this . _playStartMs = this . _nowMs ( ) ;
165+ this . _audioStart = { sinceMs : this . _playStartMs , at : clamped , from : null } ;
141166 // Same reasoning as `play()`: the seek itself, not any elapsed gap
142167 // since the last read, is why the reported time is moving now.
143168 this . _lastReadMs = null ;
@@ -208,6 +233,7 @@ export class TransportClock {
208233 }
209234
210235 /** The element the playhead currently follows, if the source is one. */
236+ // fallow-ignore-next-line unused-class-member
211237 audioElement ( ) : HTMLMediaElement | null {
212238 return this . _audioSource && "el" in this . _audioSource ? this . _audioSource . el : null ;
213239 }
0 commit comments