@@ -118,6 +118,8 @@ export class Html5Player extends Player {
118118 private static bakes = new BakeQueue ( ) ;
119119 // Pages still mounting. A bake joins the queue once they have, so the queue orders every clip that loaded together.
120120 private static readonly mounts = new Set < Promise < void > > ( ) ;
121+ // Frames every bake has in flight. Bakes take turns, so they share one window and one memory budget.
122+ private static readonly inFlight = { frames : 0 , bytes : 0 } ;
121123
122124 private iframe : HTMLIFrameElement | null = null ;
123125 private renderedWidth : number = 0 ;
@@ -405,11 +407,11 @@ export class Html5Player extends Player {
405407
406408 private async queueBake ( queuedFor : string | null , minScale : number ) : Promise < Blob [ ] | null > {
407409 while ( Html5Player . mounts . size > 0 ) await Promise . allSettled ( [ ...Html5Player . mounts ] ) ;
408- return Html5Player . bakes . add ( this . bakeSlot , async ( ) => {
410+ return Html5Player . bakes . add ( this . bakeSlot , async turn => {
409411 // A content change while this waited in the queue would bake the new content at this capture's floor.
410412 if ( this . disposed || this . contentHash !== queuedFor ) return null ;
411413 try {
412- return await this . runCapture ( minScale ) ;
414+ return await this . runCapture ( minScale , turn ) ;
413415 } finally {
414416 this . bakeNextIndex = null ;
415417 }
@@ -452,7 +454,7 @@ export class Html5Player extends Player {
452454 }
453455
454456 // A re-bake passes the scale it was started for, so a later, smaller sample can't bake softer frames.
455- private async runCapture ( minScale : number = 0 ) : Promise < Blob [ ] | null > {
457+ private async runCapture ( minScale : number = 0 , turn : ( ) => Promise < void > = async ( ) => { } ) : Promise < Blob [ ] | null > {
456458 if ( ! this . iframe || ! this . iframe . contentDocument || ! this . iframe . contentWindow ) return null ;
457459
458460 const { contentHash } = this ;
@@ -485,7 +487,11 @@ export class Html5Player extends Player {
485487 this . captureFramesTotal = frameCount ;
486488 this . captureFramesDone = 0 ;
487489
488- const framesInFlight = captureFramesInFlight ( navigator . hardwareConcurrency , Math . trunc ( W * scale ) * Math . trunc ( H * scale ) * 4 ) ;
490+ const frameBytes = Math . trunc ( W * scale ) * Math . trunc ( H * scale ) * 4 ;
491+ const framesInFlight = captureFramesInFlight ( navigator . hardwareConcurrency , frameBytes ) ;
492+ const { inFlight } = Html5Player ;
493+ const hasRoom = ( ) : boolean =>
494+ inFlight . frames === 0 || ( inFlight . frames < framesInFlight && inFlight . bytes + frameBytes <= CAPTURE_MEMORY_BUDGET_BYTES ) ;
489495 // The playhead's frame goes first, so the clip under it plays soonest.
490496 const segments = passSegments (
491497 Array . from ( { length : frameCount } , ( _ , i ) => i ) ,
@@ -505,15 +511,21 @@ export class Html5Player extends Player {
505511 let sliceStart = - Infinity ;
506512 // Frames publish in the order they were issued, so the oldest in flight gates the window.
507513 while ( published < frameCount ) {
508- if ( issued < frameCount && pending . length < framesInFlight ) {
514+ if ( issued < frameCount && hasRoom ( ) ) {
509515 const idx = order [ issued ] ;
510516 this . bakeNextIndex = idx ;
511- if ( freshPageAt . has ( issued ) && ! ( await this . loadFreshPage ( this . iframe , stale , scale ) ) ) return null ;
512517 if ( performance . now ( ) - sliceStart >= CAPTURE_SLICE_MS ) {
513518 await nextFrame ( ) ;
514519 if ( stale ( ) ) return null ;
520+ await turn ( ) ;
521+ if ( stale ( ) ) return null ;
515522 sliceStart = performance . now ( ) ;
516523 }
524+ }
525+ // Other bakes draw during their turns, so the window is read again once this one has its turn back.
526+ if ( issued < frameCount && hasRoom ( ) ) {
527+ const idx = order [ issued ] ;
528+ if ( freshPageAt . has ( issued ) && ! ( await this . loadFreshPage ( this . iframe , stale , scale ) ) ) return null ;
517529 this . seekHarness ( idx / fps ) ;
518530 forceLayout ( this . iframe . contentDocument . body ) ;
519531 const { styles, body } = this . serialiseIframe ( W , H ) ;
@@ -523,9 +535,14 @@ export class Html5Player extends Player {
523535 }
524536 let drawn = recent . get ( body ) ;
525537 if ( ! drawn ) {
526- drawn = foreignObjectSvgToWebp ( foreignObjectSvg ( W , H , styles , body ) , W , H , scale ) . then (
527- bytes => new Blob ( [ bytes as BlobPart ] , { type : "image/webp" } )
528- ) ;
538+ inFlight . frames += 1 ;
539+ inFlight . bytes += frameBytes ;
540+ drawn = foreignObjectSvgToWebp ( foreignObjectSvg ( W , H , styles , body ) , W , H , scale )
541+ . then ( bytes => new Blob ( [ bytes as BlobPart ] , { type : "image/webp" } ) )
542+ . finally ( ( ) => {
543+ inFlight . frames -= 1 ;
544+ inFlight . bytes -= frameBytes ;
545+ } ) ;
529546 // Marks a queued frame's failure handled; awaiting it rethrows.
530547 drawn . catch ( ( ) => undefined ) ;
531548 // A Map iterates in insertion order, so its first key is the oldest frame.
@@ -534,7 +551,7 @@ export class Html5Player extends Player {
534551 }
535552 pending . push ( { idx, drawn } ) ;
536553 issued += 1 ;
537- } else {
554+ } else if ( pending . length > 0 ) {
538555 const { idx, drawn } = pending . shift ( ) ! ;
539556 const frame = await drawn ;
540557 if ( stale ( ) ) return null ;
@@ -547,6 +564,10 @@ export class Html5Player extends Player {
547564 progressStep = step ;
548565 this . emitCaptureProgress ( ) ;
549566 }
567+ } else {
568+ // Other bakes' frames fill the window, and they finish without their bakes' turns.
569+ await nextFrame ( ) ;
570+ if ( stale ( ) ) return null ;
550571 }
551572 }
552573
0 commit comments