// THE DECODED FRAMES OF ONE CLIP, waiting to be drawn (the Play view's // WebCodecs picture, components/projects/review/playEngine/codecPicture.ts): // a small queue in presentation order, bounded, and every frame that leaves // it -- drawn past, dropped for room, cleared on a seek -- CLOSED. A // VideoFrame holds decoder memory (often on the GPU) until it is closed, and a // decoder with too many open frames stalls; so nothing here ever forgets one. // // `frameAt(t)` is the frame due at time t: the latest at or before it. It // stays in the queue (a paused picture, a held last frame are drawn again // every tick) and everything older is closed. Pure: any object with a // `timestamp` (microseconds, as WebCodecs has it) and `close()`, so it is // unit-tested with fakes (frameQueue.test.mjs). export class FrameQueue { /** @param {{ max?: number }} [opts] */ constructor({ max = 12 } = {}) { this.max = max; /** @type {Array<{ timestamp: number, close: () => void }>} */ this.frames = []; } get size() { return this.frames.length; } /** The newest frame's time (µs), or -Infinity: how far ahead the queue reaches. */ get head() { return this.frames.length ? this.frames[this.frames.length - 1].timestamp : -Infinity; } /** Is there room for another decoded frame? */ get room() { return this.frames.length < this.max; } /** * A decoded frame, in its place. Over the bound, the OLDEST goes (it is * behind the clock), closed. A frame for a time already held replaces it. */ push(frame) { const i = this.frames.findIndex((f) => f.timestamp >= frame.timestamp); if (i < 0) this.frames.push(frame); else if (this.frames[i].timestamp === frame.timestamp) { this.frames[i].close(); this.frames[i] = frame; } else this.frames.splice(i, 0, frame); while (this.frames.length > this.max) this.frames.shift()?.close(); } /** The frame due at t (µs): the latest at or before it, kept; every older one closed. null when none is due yet. */ frameAt(t) { let k = -1; for (let i = 0; i < this.frames.length; i += 1) { if (this.frames[i].timestamp <= t) k = i; else break; } if (k < 0) return null; for (let i = 0; i < k; i += 1) this.frames[i].close(); this.frames.splice(0, k); return this.frames[0]; } /** Close and drop every frame before t (µs) -- a decode that ran from a keyframe toward a seek. */ dropBefore(t) { while (this.frames.length > 1 && this.frames[1].timestamp <= t) this.frames.shift()?.close(); } /** Close every frame (a seek, a clip let go). */ clear() { for (const f of this.frames) f.close(); this.frames = []; } }