🎥 Make videos programmatically with React
schedule enough frames in advance
JonnyBurger7347f0f7e0ec
diff --git a/packages/media/src/audio/audio-preview-iterator.ts b/packages/media/src/audio/audio-preview-iterator.tsindex 8d20848daa..6bc4af7ecc 100644--- a/packages/media/src/audio/audio-preview-iterator.ts+++ b/packages/media/src/audio/audio-preview-iterator.ts@@ -184,10 +184,10 @@ export const makeAudioIterator = (queuedAudioNodes.push({node, timestamp, duration});},removeQueuedAudioNode: (node: AudioBufferSourceNode) => {- queuedAudioNodes.splice(- queuedAudioNodes.findIndex((n) => n.node === node),- 1,- );+ const index = queuedAudioNodes.findIndex((n) => n.node === node);+ if (index !== -1) {+ queuedAudioNodes.splice(index, 1);+ }},getQueuedPeriod: () => {const lastNode = queuedAudioNodes[queuedAudioNodes.length - 1];diff --git a/packages/media/src/debug-overlay/preview-overlay.ts b/packages/media/src/debug-overlay/preview-overlay.tsindex 8cacba1755..779763dd89 100644--- a/packages/media/src/debug-overlay/preview-overlay.ts+++ b/packages/media/src/debug-overlay/preview-overlay.ts@@ -1,18 +1,29 @@+import type {AudioIterator} from '../audio/audio-preview-iterator';+export type DebugStats = {videoIteratorsCreated: number;audioIteratorsCreated: number;framesRendered: number;};-export const drawPreviewOverlay = (- context: CanvasRenderingContext2D,- stats: DebugStats,- audioContextState: AudioContextState,- audioSyncAnchor: number,-) => {+export const drawPreviewOverlay = ({+ context,+ stats,+ audioTime,+ audioContextState,+ audioIterator,+ audioSyncAnchor,+}: {+ context: CanvasRenderingContext2D;+ stats: DebugStats;+ audioTime: number;+ audioContextState: AudioContextState;+ audioSyncAnchor: number;+ audioIterator: AudioIterator | null;+}) => {// Optionally, set a background for text legibilitycontext.fillStyle = 'rgba(0, 0, 0, 1)';- context.fillRect(20, 20, 600, 220);+ context.fillRect(20, 20, 600, 260);context.fillStyle = 'white';context.font = '24px sans-serif';@@ -30,5 +41,20 @@ export const drawPreviewOverlay = ();context.fillText(`Frames rendered: ${stats.framesRendered}`, 30, 120);context.fillText(`Audio context state: ${audioContextState}`, 30, 150);- context.fillText(`Audio time: ${audioSyncAnchor.toFixed(3)}s`, 30, 180);+ context.fillText(+ `Audio time: ${(audioTime - audioSyncAnchor).toFixed(3)}s`,+ 30,+ 180,+ );++ if (audioIterator) {+ const queuedPeriod = audioIterator.getQueuedPeriod();+ if (queuedPeriod) {+ context.fillText(+ `Audio queued until: ${(queuedPeriod.until - (audioTime - audioSyncAnchor)).toFixed(3)}s`,+ 30,+ 210,+ );+ }+ }};diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.tsindex 7f545689f8..df1be58167 100644--- a/packages/media/src/media-player.ts+++ b/packages/media/src/media-player.ts@@ -370,7 +370,6 @@ export class MediaPlayer {const queuedPeriod = this.audioBufferIterator?.getQueuedPeriod();const currentTimeIsAlreadyQueued = isAlreadyQueued(newTime, queuedPeriod);- console.log({currentTimeIsAlreadyQueued});const toBeScheduled: WrappedAudioBuffer[] = [];if (!currentTimeIsAlreadyQueued) {@@ -394,12 +393,16 @@ export class MediaPlayer {}// TODO: What is this is beyond the end of the video- const nextTime = newTime + 1 / this.fps;+ const nextTime =+ newTime ++ // start of next frame+ 1 / this.fps ++ // need the full duration of the next frame to be queued+ 1 / this.fps;const nextIsAlreadyQueued = isAlreadyQueued(nextTime, queuedPeriod);if (!nextIsAlreadyQueued) {const audioSatisfyResult =await this.audioBufferIterator?.tryToSatisfySeek(nextTime);- console.log({audioSatisfyResult, nextTime});if (this.currentSeekNonce !== nonce) {return;@@ -418,13 +421,6 @@ export class MediaPlayer {}for (const buffer of toBeScheduled) {- console.log(- 'buffering',- buffer.timestamp,- '-',- buffer.timestamp + buffer.duration,- this.getPlaybackTime(),- );this.scheduleAudioChunk(buffer.buffer, buffer.timestamp, buffer.duration);}}@@ -582,47 +578,61 @@ export class MediaPlayer {if (!this.hasAudio()) return;this.audioBufferIterator?.destroy();+ const delayHandle = this.bufferState?.delayPlayback();+const iterator = makeAudioIterator(this.audioSink!, startFromSecond);this.debugStats.audioIteratorsCreated++;- console.log('new audio iterator');this.audioBufferIterator = iterator;- const result = await iterator.getNext();+ // Schedule up to 3 buffers ahead of the current time+ for (let i = 0; i < 3; i++) {+ const result = await iterator.getNext();- if (iterator.isDestroyed()) {- return;- }+ if (iterator.isDestroyed()) {+ delayHandle?.unblock();+ return;+ }- if (nonce !== this.currentSeekNonce) {- return;- }+ if (nonce !== this.currentSeekNonce) {+ delayHandle?.unblock();+ return;+ }- if (this.audioBufferIterator.isDestroyed()) {- return;- }+ if (this.audioBufferIterator.isDestroyed()) {+ delayHandle?.unblock();+ return;+ }- if (!result.value) {- // media ended- return;- }+ if (!result.value) {+ // media ended+ delayHandle?.unblock();+ return;+ }- const {buffer, timestamp} = result.value;+ const {buffer, timestamp} = result.value;- if (this.playing) {- // TODO: Keep it ready for future use if it is not playing- this.scheduleAudioChunk(buffer, timestamp, buffer.duration);+ if (this.playing) {+ console.log('yes good');+ // TODO: Need a few more+ // TODO: Keep it ready for future use if it is not playing+ this.scheduleAudioChunk(buffer, timestamp, buffer.duration);+ }}++ delayHandle?.unblock();};private drawDebugOverlay(): void {if (!this.debugOverlay) return;if (this.context && this.canvas) {- drawPreviewOverlay(- this.context,- this.debugStats,- this.sharedAudioContext.state,- this.sharedAudioContext.currentTime,- );+ drawPreviewOverlay({+ context: this.context,+ stats: this.debugStats,+ audioTime: this.sharedAudioContext.currentTime,+ audioContextState: this.sharedAudioContext.state,+ audioSyncAnchor: this.audioSyncAnchor,+ audioIterator: this.audioBufferIterator,+ });}}