Skip to content

🎥 Make videos programmatically with React

better but not perfect

JonnyBurgerc4b9861f351e
diff --git a/packages/media/src/audio/audio-preview-iterator.ts b/packages/media/src/audio/audio-preview-iterator.ts
index b3f660dd01..342413b214 100644
--- a/packages/media/src/audio/audio-preview-iterator.ts
+++ b/packages/media/src/audio/audio-preview-iterator.ts
@@ -164,7 +164,6 @@ export const makeAudioIterator = (
};
return {
- cleanupAudioQueue,
destroy: () => {
cleanupAudioQueue();
destroyed = true;
@@ -190,7 +189,7 @@ export const makeAudioIterator = (
}
},
removeAndReturnAllQueuedAudioNodes: () => {
- const nodes = queuedAudioNodes;
+ const nodes = queuedAudioNodes.slice();
for (const node of nodes) {
node.node.stop();
}
diff --git a/packages/media/src/debug-overlay/preview-overlay.ts b/packages/media/src/debug-overlay/preview-overlay.ts
index 779763dd89..3111c0c09e 100644
--- a/packages/media/src/debug-overlay/preview-overlay.ts
+++ b/packages/media/src/debug-overlay/preview-overlay.ts
@@ -13,6 +13,8 @@ export const drawPreviewOverlay = ({
audioContextState,
audioIterator,
audioSyncAnchor,
+ audioChunksForAfterResuming,
+ playing,
}: {
context: CanvasRenderingContext2D;
stats: DebugStats;
@@ -20,41 +22,59 @@ export const drawPreviewOverlay = ({
audioContextState: AudioContextState;
audioSyncAnchor: number;
audioIterator: AudioIterator | null;
+ audioChunksForAfterResuming: {
+ buffer: AudioBuffer;
+ timestamp: number;
+ }[];
+ playing: boolean;
}) => {
- // Optionally, set a background for text legibility
- context.fillStyle = 'rgba(0, 0, 0, 1)';
- context.fillRect(20, 20, 600, 260);
-
- context.fillStyle = 'white';
- context.font = '24px sans-serif';
- context.textBaseline = 'top';
- context.fillText(`Debug overlay`, 30, 30);
- context.fillText(
+ // Collect all lines to be rendered
+ const lines: string[] = [
+ 'Debug overlay',
`Video iterators created: ${stats.videoIteratorsCreated}`,
- 30,
- 60,
- );
- context.fillText(
`Audio iterators created: ${stats.audioIteratorsCreated}`,
- 30,
- 90,
- );
- context.fillText(`Frames rendered: ${stats.framesRendered}`, 30, 120);
- context.fillText(`Audio context state: ${audioContextState}`, 30, 150);
- context.fillText(
+ `Frames rendered: ${stats.framesRendered}`,
+ `Audio context state: ${audioContextState}`,
`Audio time: ${(audioTime - audioSyncAnchor).toFixed(3)}s`,
- 30,
- 180,
- );
+ ];
if (audioIterator) {
const queuedPeriod = audioIterator.getQueuedPeriod();
if (queuedPeriod) {
- context.fillText(
+ lines.push(
`Audio queued until: ${(queuedPeriod.until - (audioTime - audioSyncAnchor)).toFixed(3)}s`,
- 30,
- 210,
+ );
+ } else if (audioChunksForAfterResuming.length > 0) {
+ lines.push(
+ `Audio chunks for after resuming: ${audioChunksForAfterResuming.length}`,
);
}
+
+ lines.push(`Playing: ${playing}`);
+ }
+
+ const lineHeight = 30; // px, should match or exceed font size
+ const boxPaddingX = 10;
+ const boxPaddingY = 10;
+ const boxLeft = 20;
+ const boxTop = 20;
+
+ const boxWidth = 600;
+ const boxHeight = lines.length * lineHeight + 2 * boxPaddingY;
+
+ // Draw background for text legibility
+ context.fillStyle = 'rgba(0, 0, 0, 1)';
+ context.fillRect(boxLeft, boxTop, boxWidth, boxHeight);
+
+ context.fillStyle = 'white';
+ context.font = '24px sans-serif';
+ context.textBaseline = 'top';
+
+ for (let i = 0; i < lines.length; i++) {
+ context.fillText(
+ lines[i],
+ boxLeft + boxPaddingX,
+ boxTop + boxPaddingY + i * lineHeight,
+ );
}
};
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index 1b884a7472..675b7b829c 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -305,6 +305,7 @@ export class MediaPlayer {
private seekPromiseChain: Promise<void> = Promise.resolve();
public async seekTo(time: number): Promise<void> {
+ console.log('seeking', {time});
this.currentSeekNonce++;
const nonce = this.currentSeekNonce;
await this.seekPromiseChain;
@@ -422,7 +423,14 @@ export class MediaPlayer {
}
for (const buffer of toBeScheduled) {
- this.scheduleAudioChunk(buffer.buffer, buffer.timestamp);
+ if (this.playing) {
+ this.scheduleAudioChunk(buffer.buffer, buffer.timestamp);
+ } else {
+ this.audioChunksForAfterResuming.push({
+ buffer: buffer.buffer,
+ timestamp: buffer.timestamp,
+ });
+ }
}
}
@@ -437,6 +445,7 @@ export class MediaPlayer {
}
this.audioChunksForAfterResuming.length = 0;
+ this.drawDebugOverlay();
}
public pause(): void {
@@ -452,6 +461,8 @@ export class MediaPlayer {
});
}
}
+
+ this.drawDebugOverlay();
}
public setMuted(muted: boolean): void {
@@ -541,9 +552,11 @@ export class MediaPlayer {
node.start(this.sharedAudioContext.currentTime, -delay);
}
- this.audioBufferIterator?.addQueuedAudioNode(node, mediaTimestamp, buffer);
+ this.audioBufferIterator!.addQueuedAudioNode(node, mediaTimestamp, buffer);
+ console.log('added queued audio node', mediaTimestamp);
node.onended = () => {
- return this.audioBufferIterator?.removeQueuedAudioNode(node);
+ console.log('removed', mediaTimestamp);
+ return this.audioBufferIterator!.removeQueuedAudioNode(node);
};
}
@@ -590,6 +603,7 @@ export class MediaPlayer {
if (!this.hasAudio()) return;
this.audioBufferIterator?.destroy();
+ this.audioChunksForAfterResuming = [];
const delayHandle = this.bufferState?.delayPlayback();
const iterator = makeAudioIterator(this.audioSink!, startFromSecond);
@@ -646,6 +660,8 @@ export class MediaPlayer {
audioContextState: this.sharedAudioContext.state,
audioSyncAnchor: this.audioSyncAnchor,
audioIterator: this.audioBufferIterator,
+ audioChunksForAfterResuming: this.audioChunksForAfterResuming,
+ playing: this.playing,
});
}
}