Skip to content

🎥 Make videos programmatically with React

adding buffering

JonnyBurger2f621723b215
diff --git a/packages/media/src/audio/audio-for-preview.tsx b/packages/media/src/audio/audio-for-preview.tsx
index 88a9fcbee6..750878dbd6 100644
--- a/packages/media/src/audio/audio-for-preview.tsx
+++ b/packages/media/src/audio/audio-for-preview.tsx
@@ -149,6 +149,7 @@ const NewAudioForPreview: React.FC<NewAudioForPreviewProps> = ({
playbackRate,
audioStreamIndex: audioStreamIndex ?? 0,
debugOverlay: false,
+ bufferState: buffer,
});
mediaPlayerRef.current = player;
@@ -271,6 +272,7 @@ const NewAudioForPreview: React.FC<NewAudioForPreviewProps> = ({
videoConfig.fps,
audioStreamIndex,
disallowFallbackToHtml5Audio,
+ buffer,
]);
useEffect(() => {
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index d699164e81..673d8ad9f7 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -6,7 +6,7 @@ import {
Input,
UrlSource,
} from 'mediabunny';
-import type {LogLevel} from 'remotion';
+import type {LogLevel, useBufferState} from 'remotion';
import {Internals} from 'remotion';
import {
HEALTHY_BUFFER_THRESHOLD_SECONDS,
@@ -81,6 +81,8 @@ export class MediaPlayer {
private initializationPromise: Promise<MediaPlayerInitResult> | null = null;
+ private bufferState: ReturnType<typeof useBufferState> | null = null;
+
constructor({
canvas,
src,
@@ -93,6 +95,7 @@ export class MediaPlayer {
audioStreamIndex,
fps,
debugOverlay,
+ bufferState,
}: {
canvas: HTMLCanvasElement | null;
src: string;
@@ -105,6 +108,7 @@ export class MediaPlayer {
audioStreamIndex: number;
fps: number;
debugOverlay: boolean;
+ bufferState: ReturnType<typeof useBufferState>;
}) {
this.canvas = canvas ?? null;
this.src = src;
@@ -117,6 +121,7 @@ export class MediaPlayer {
this.audioStreamIndex = audioStreamIndex ?? 0;
this.fps = fps;
this.debugOverlay = debugOverlay;
+ this.bufferState = bufferState;
if (canvas) {
const context = canvas.getContext('2d', {
@@ -263,10 +268,8 @@ export class MediaPlayer {
this.initialized = true;
try {
- await Promise.all([
- this.startAudioIterator(startTime),
- this.startVideoIterator(startTime),
- ]);
+ this.startAudioIterator(startTime);
+ this.startVideoIterator(startTime);
} catch (error) {
if (this.isDisposalError()) {
return {type: 'disposed'};
@@ -350,10 +353,8 @@ export class MediaPlayer {
this.mediaEnded = false;
this.audioSyncAnchor = this.sharedAudioContext.currentTime - newTime;
- await Promise.all([
- this.startAudioIterator(newTime),
- this.startVideoIterator(newTime),
- ]);
+ this.startAudioIterator(newTime);
+ this.startVideoIterator(newTime);
}
public async play(): Promise<void> {
@@ -515,6 +516,12 @@ export class MediaPlayer {
this.renderPromiseChain = this.renderDoNotCallDirectly(nonce);
await this.renderPromiseChain;
this.renderPromiseChain = null;
+ if (this.playing) {
+ this.animationFrameId = requestAnimationFrame(this.render);
+ } else if (this.animationFrameId !== null) {
+ cancelAnimationFrame(this.animationFrameId);
+ this.animationFrameId = null;
+ }
};
private drawFrame = (frame: WrappedCanvas): void => {
@@ -558,14 +565,21 @@ export class MediaPlayer {
if (nextFrame.type === 'got-frame-or-end') {
if (nextFrame.frame) {
this.drawFrame(nextFrame.frame);
+ } else {
+ // TODO: Media ended
}
} else {
if (nonce !== this.currentRenderNonce) {
return;
}
+ // Frame is not immediately available, so we are buffering
+ // until it is
+ const delayHandle = this.bufferState?.delayPlayback();
const frame = (await nextFrame.waitPromise()) ?? null;
- if (this.videoFrameIterator.isDestroyed()) {
+ delayHandle?.unblock();
+
+ if (!this.videoFrameIterator.isDestroyed() && frame) {
return;
}
@@ -574,12 +588,6 @@ export class MediaPlayer {
}
}
}
-
- if (this.playing) {
- this.animationFrameId = requestAnimationFrame(this.render);
- } else {
- this.animationFrameId = null;
- }
};
private shouldRenderNewFrame(): boolean {
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index 0135ee5217..8f9fb4868d 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -155,6 +155,7 @@ export const VideoForPreview: React.FC<VideoForPreviewProps> = ({
playbackRate,
audioStreamIndex,
debugOverlay,
+ bufferState: buffer,
});
mediaPlayerRef.current = player;
@@ -272,6 +273,7 @@ export const VideoForPreview: React.FC<VideoForPreviewProps> = ({
disallowFallbackToOffthreadVideo,
audioStreamIndex,
debugOverlay,
+ buffer,
]);
const classNameValue = useMemo(() => {