@@ -84,6 +84,7 @@ import { sourceTimeAt } from "../speedRamp";
8484import { createWaapiAdapter } from "./adapters/waapi" ;
8585import {
8686 MEDIA_SYNC_TOLERANCE_SECONDS ,
87+ MEDIA_HARD_SYNC_SECONDS ,
8788 isUnplayable ,
8889 mediaSourceTimeAt ,
8990 primeMediaForLaterStarts ,
@@ -3102,14 +3103,17 @@ export function initSandboxRuntimeModular(): void {
31023103 startPrefetch ( next . el ) ;
31033104 }
31043105 } ;
3106+ let bufferingFor : HTMLMediaElement [ ] = [ ] ;
3107+ let keepLateMedia = false ;
31053108 const preloadNearPlayhead = (
31063109 el : HTMLMediaElement ,
31073110 visible : boolean ,
31083111 upcoming : boolean ,
31093112 time : number ,
31103113 forLoop : boolean ,
31113114 ) => {
3112- const playing = clock . isPlaying ( ) ;
3115+ // A hold for data is still playing: fetching other clips now would take bandwidth from the starved one.
3116+ const playing = clock . isPlaying ( ) || bufferingFor . length > 0 ;
31133117 if ( ! ( visible && playing ) ) armedFrom . delete ( el ) ;
31143118 else if ( ! armedFrom . has ( el ) ) {
31153119 armedFrom . add ( el ) ;
@@ -3440,7 +3444,6 @@ export function initSandboxRuntimeModular(): void {
34403444 let lastSyncedMediaTimeSeconds : number | null = null ;
34413445 let mediaClipsInWindow : RuntimeMediaClip [ ] = [ ] ;
34423446 // Clips due now that could not play: every track holds until they can, so none runs on alone.
3443- let bufferingFor : HTMLMediaElement [ ] = [ ] ;
34443447
34453448 // The one definition of "media the transport drives": timed itself, or hosted
34463449 // by a composition whose timing it inherits. Both the media cache and the
@@ -3724,6 +3727,7 @@ export function initSandboxRuntimeModular(): void {
37243727 userMuted : state . bridgeMuted ,
37253728 userVolume : state . bridgeVolume ,
37263729 forceSync,
3730+ keepLateMedia,
37273731 cueAheadSeconds,
37283732 clockAdvancing :
37293733 clock . getSource ( ) !== "audio" ||
@@ -4749,18 +4753,20 @@ export function initSandboxRuntimeModular(): void {
47494753 postState ( true ) ;
47504754 } ;
47514755
4752- const haltPlayback = ( ) => {
4756+ const haltPlayback = ( starved = false ) => {
47534757 webAudio . stopAll ( ) ;
47544758 clock . detachAudioSource ( ) ;
47554759 clock . pause ( ) ;
47564760 state . isPlaying = false ;
47574761 state . currentTime = clock . now ( ) ;
4758- state . mediaForceSyncNextTick = true ;
4759- hardSyncAllMedia ( state . currentTime ) ;
4762+ state . mediaForceSyncNextTick = ! starved ;
4763+ keepLateMedia = starved ;
4764+ hardSyncAllMedia ( state . currentTime , starved ? MEDIA_HARD_SYNC_SECONDS : undefined ) ;
47604765 const tl = state . capturedTimeline ;
47614766 pauseTimelineIfPossible ( tl ) ;
47624767 runAdapters ( "pause" ) ;
47634768 syncMediaForCurrentState ( ) ;
4769+ keepLateMedia = false ;
47644770 colorGrading . redraw ( ) ;
47654771 // Paused: back to plain DOM so Studio can select inside the composition.
47664772 void paintCaptureMotionBlur ( state . currentTime ) ;
@@ -4770,6 +4776,7 @@ export function initSandboxRuntimeModular(): void {
47704776 const transport : RuntimePlayerTransport = {
47714777 play : ( ) => {
47724778 flushHeldSeek ( ) ;
4779+ const resumingStarved = bufferingFor . length > 0 ;
47734780 bufferingFor = [ ] ;
47744781 const tl = state . capturedTimeline ;
47754782 if ( clock . isPlaying ( ) ) return ;
@@ -4786,8 +4793,9 @@ export function initSandboxRuntimeModular(): void {
47864793 pauseTimelineIfPossible ( tl ) ;
47874794 if ( ! clock . play ( ) ) return ;
47884795 state . isPlaying = true ;
4789- state . mediaForceSyncNextTick = true ;
4790- hardSyncAllMedia ( clock . now ( ) ) ;
4796+ state . mediaForceSyncNextTick = ! resumingStarved ;
4797+ keepLateMedia = resumingStarved ;
4798+ hardSyncAllMedia ( clock . now ( ) , resumingStarved ? MEDIA_HARD_SYNC_SECONDS : undefined ) ;
47914799 // Schedule audio through WebAudio for sample-accurate timing.
47924800 // Falls back to HTMLMediaElement playback if WebAudio isn't ready
47934801 // or decoding fails (the syncRuntimeMedia path handles that).
@@ -4796,6 +4804,7 @@ export function initSandboxRuntimeModular(): void {
47964804 }
47974805 runAdapters ( "play" ) ;
47984806 syncMediaForCurrentState ( ) ;
4807+ keepLateMedia = false ;
47994808 primeMediaForLaterStarts ( resolveMediaClipIndex ( ) . clips . map ( ( clip ) => clip . el ) ) ;
48004809 colorGrading . redraw ( ) ;
48014810 postState ( true ) ;
@@ -5674,25 +5683,31 @@ export function initSandboxRuntimeModular(): void {
56745683 }
56755684
56765685 const READY_AHEAD_SECONDS = 1 ;
5677- // WebKit reports a paused element as HAVE_CURRENT_DATA however much it has buffered, so read the ranges too.
5678- const hasDataAhead = ( el : HTMLMediaElement ) : boolean => {
5679- if ( el . readyState >= HTMLMediaElement . HAVE_FUTURE_DATA ) return true ;
5680- if ( el . readyState < HTMLMediaElement . HAVE_CURRENT_DATA ) return false ;
5686+ // WebKit reports a paused element as HAVE_CURRENT_DATA however much it has buffered, so its ranges count too.
5687+ const hasDataAhead = ( el : HTMLMediaElement ) : boolean =>
5688+ el . readyState >= HTMLMediaElement . HAVE_FUTURE_DATA || ( isWebKit ( ) && canResumeFrom ( el ) ) ;
5689+ // HAVE_FUTURE_DATA can mean one frame, so a held film resumes only on the stricter reading.
5690+ const canResumeFrom = ( el : HTMLMediaElement ) : boolean => {
5691+ if ( el . readyState >= HTMLMediaElement . HAVE_ENOUGH_DATA ) return true ;
5692+ const least = isWebKit ( )
5693+ ? HTMLMediaElement . HAVE_CURRENT_DATA
5694+ : HTMLMediaElement . HAVE_FUTURE_DATA ;
5695+ if ( el . readyState < least ) return false ;
56815696 const until = Math . min ( el . currentTime + READY_AHEAD_SECONDS , el . duration ) - 0.01 ;
56825697 for ( let i = 0 ; i < el . buffered . length ; i ++ ) {
56835698 if ( el . buffered . start ( i ) <= el . currentTime && el . buffered . end ( i ) >= until ) return true ;
56845699 }
56855700 return false ;
56865701 } ;
56875702
5688- const mediaNotReadyAt = ( t : number ) : HTMLMediaElement [ ] =>
5703+ const mediaNotReadyAt = ( t : number , ready = hasDataAhead ) : HTMLMediaElement [ ] =>
56895704 state . nativeMediaSyncDisabled
56905705 ? [ ]
56915706 : mediaClipsInWindow
56925707 . filter (
56935708 ( { el, start, end } ) =>
56945709 isInClipWindow ( t , start , end ) &&
5695- ! hasDataAhead ( el ) &&
5710+ ! ready ( el ) &&
56965711 isPlayableTrack ( el ) &&
56975712 ! stillStandsIn ( el ) &&
56985713 ! webAudio . ownsElement ( el ) ,
@@ -5796,11 +5811,13 @@ export function initSandboxRuntimeModular(): void {
57965811 filmTime : t ,
57975812 fps : state . canonicalFps ,
57985813 rate : state . playbackRate ,
5799- playing,
5814+ playing : playing || bufferingFor . length > 0 ,
58005815 hidden : document . visibilityState === "hidden" ,
5801- waiting : mediaClipsInWindow . some (
5802- ( { el } ) => el . tagName === "VIDEO" && ! el . paused && el . readyState < 3 ,
5803- ) ,
5816+ waiting :
5817+ bufferingFor . length > 0 ||
5818+ mediaClipsInWindow . some (
5819+ ( { el } ) => el . tagName === "VIDEO" && ! el . paused && el . readyState < 3 ,
5820+ ) ,
58045821 frozen : hasFrozenPicture ( mediaClipsInWindow , t ) ,
58055822 clock : clock . followedClock ( ) ,
58065823 layoutKey : `${ window . innerWidth } x${ window . innerHeight } @${ window . devicePixelRatio } ` ,
@@ -5873,8 +5890,11 @@ export function initSandboxRuntimeModular(): void {
58735890 const notReady = mediaNotReadyAt ( state . currentTime ) ;
58745891 if ( clock . isPlaying ( ) && notReady . length > 0 ) {
58755892 bufferingFor = notReady ;
5876- haltPlayback ( ) ;
5877- } else if ( bufferingFor . length > 0 && ! notReady . some ( ( el ) => bufferingFor . includes ( el ) ) ) {
5893+ haltPlayback ( true ) ;
5894+ } else if (
5895+ bufferingFor . length > 0 &&
5896+ ! mediaNotReadyAt ( state . currentTime , canResumeFrom ) . some ( ( el ) => bufferingFor . includes ( el ) )
5897+ ) {
58785898 transport . play ( ) ;
58795899 }
58805900 postState ( false ) ;
@@ -5887,14 +5907,14 @@ export function initSandboxRuntimeModular(): void {
58875907 }
58885908 } ;
58895909
5890- const hardSyncAllMedia = ( timeSeconds : number ) => {
5910+ const hardSyncAllMedia = ( timeSeconds : number , tolerance = 0.001 ) => {
58915911 // Same clips and source time as the media sync that follows, so it has nothing left to seek.
58925912 const clips = withTimingResolver ( ( ) => buildRuntimeMediaCache ( ) . mediaClips ) ;
58935913 for ( const clip of transportMovableClips ( clips ) ?? [ ] ) {
58945914 if ( ! clip . el . isConnected || ! isInClipWindow ( timeSeconds , clip . start , clip . end ) ) continue ;
58955915 const target = mediaSourceTimeAt ( clip , timeSeconds ) ;
58965916 // A seek flushes the decoder even when it lands where the video already is.
5897- if ( Math . abs ( clip . el . currentTime - target ) <= 0.001 ) continue ;
5917+ if ( Math . abs ( clip . el . currentTime - target ) <= tolerance ) continue ;
58985918 try {
58995919 clip . el . currentTime = mediaElementTime ( target ) ;
59005920 } catch {
0 commit comments