Skip to content

🎥 Make videos programmatically with React

`@remotion/media`: Don't trigger buffer state while premounting

JonnyBurger8bbeccc0cf6d
diff --git a/packages/media/src/audio/audio-for-preview.tsx b/packages/media/src/audio/audio-for-preview.tsx
index 16c1359b9a..e224db6260 100644
--- a/packages/media/src/audio/audio-for-preview.tsx
+++ b/packages/media/src/audio/audio-for-preview.tsx
@@ -107,6 +107,8 @@ const NewAudioForPreview: React.FC<NewAudioForPreviewProps> = ({
const preloadedSrc = usePreload(src);
const parentSequence = useContext(SequenceContext);
+ const isPremounting = Boolean(parentSequence?.premounting);
+ const isPostmounting = Boolean(parentSequence?.postmounting);
const loopDisplay = useLoopDisplay({
loop,
@@ -160,6 +162,8 @@ const NewAudioForPreview: React.FC<NewAudioForPreviewProps> = ({
audioStreamIndex: audioStreamIndex ?? 0,
debugOverlay: false,
bufferState: buffer,
+ isPostmounting,
+ isPremounting,
});
mediaPlayerRef.current = player;
@@ -283,6 +287,8 @@ const NewAudioForPreview: React.FC<NewAudioForPreviewProps> = ({
audioStreamIndex,
disallowFallbackToHtml5Audio,
buffer,
+ isPremounting,
+ isPostmounting,
]);
useEffect(() => {
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index d4314f8b80..909b6f9d30 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -77,6 +77,9 @@ export class MediaPlayer {
private bufferState: ReturnType<typeof useBufferState>;
+ private isPremounting: boolean;
+ private isPostmounting: boolean;
+
constructor({
canvas,
src,
@@ -90,6 +93,8 @@ export class MediaPlayer {
fps,
debugOverlay,
bufferState,
+ isPremounting,
+ isPostmounting,
}: {
canvas: HTMLCanvasElement | null;
src: string;
@@ -103,6 +108,8 @@ export class MediaPlayer {
fps: number;
debugOverlay: boolean;
bufferState: ReturnType<typeof useBufferState>;
+ isPremounting: boolean;
+ isPostmounting: boolean;
}) {
this.canvas = canvas ?? null;
this.src = src;
@@ -116,6 +123,8 @@ export class MediaPlayer {
this.fps = fps;
this.debugOverlay = debugOverlay;
this.bufferState = bufferState;
+ this.isPremounting = isPremounting;
+ this.isPostmounting = isPostmounting;
if (canvas) {
const context = canvas.getContext('2d', {
@@ -499,6 +508,14 @@ export class MediaPlayer {
this.fps = fps;
}
+ public setIsPremounting(isPremounting: boolean): void {
+ this.isPremounting = isPremounting;
+ }
+
+ public setIsPostmounting(isPostmounting: boolean): void {
+ this.isPostmounting = isPostmounting;
+ }
+
public setLoop(loop: boolean): void {
this.loop = loop;
}
@@ -601,6 +618,16 @@ export class MediaPlayer {
);
};
+ private delayPlaybackHandleIfNotPremounting() {
+ if (this.isPremounting || this.isPostmounting) {
+ return {
+ unblock: () => {},
+ };
+ }
+
+ return this.bufferState.delayPlayback();
+ }
+
private startAudioIterator = async (
startFromSecond: number,
nonce: number,
@@ -609,7 +636,7 @@ export class MediaPlayer {
this.audioBufferIterator?.destroy();
this.audioChunksForAfterResuming = [];
- const delayHandle = this.bufferState.delayPlayback();
+ const delayHandle = this.delayPlaybackHandleIfNotPremounting();
const iterator = makeAudioIterator(this.audioSink!, startFromSecond);
this.debugStats.audioIteratorsCreated++;
@@ -675,7 +702,7 @@ export class MediaPlayer {
this.debugStats.videoIteratorsCreated++;
this.videoFrameIterator = iterator;
- const delayHandle = this.bufferState.delayPlayback();
+ const delayHandle = this.delayPlaybackHandleIfNotPremounting();
const frameResult = await iterator.getNext();
delayHandle.unblock();
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index 686001fd9c..7bbdfe5721 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -106,6 +106,8 @@ export const VideoForPreview: React.FC<VideoForPreviewProps> = ({
warnAboutTooHighVolume(userPreferredVolume);
const parentSequence = useContext(SequenceContext);
+ const isPremounting = Boolean(parentSequence?.premounting);
+ const isPostmounting = Boolean(parentSequence?.postmounting);
const loopDisplay = useLoopDisplay({
loop,
@@ -172,6 +174,8 @@ export const VideoForPreview: React.FC<VideoForPreviewProps> = ({
audioStreamIndex,
debugOverlay,
bufferState: buffer,
+ isPremounting,
+ isPostmounting,
});
mediaPlayerRef.current = player;
@@ -290,6 +294,8 @@ export const VideoForPreview: React.FC<VideoForPreviewProps> = ({
audioStreamIndex,
debugOverlay,
buffer,
+ isPremounting,
+ isPostmounting,
]);
const classNameValue = useMemo(() => {
@@ -371,6 +377,24 @@ export const VideoForPreview: React.FC<VideoForPreviewProps> = ({
mediaPlayer.setLoop(loop);
}, [loop, mediaPlayerReady]);
+ useEffect(() => {
+ const mediaPlayer = mediaPlayerRef.current;
+ if (!mediaPlayer || !mediaPlayerReady) {
+ return;
+ }
+
+ mediaPlayer.setIsPremounting(isPremounting);
+ }, [isPremounting, mediaPlayerReady]);
+
+ useEffect(() => {
+ const mediaPlayer = mediaPlayerRef.current;
+ if (!mediaPlayer || !mediaPlayerReady) {
+ return;
+ }
+
+ mediaPlayer.setIsPostmounting(isPostmounting);
+ }, [isPostmounting, mediaPlayerReady]);
+
useEffect(() => {
const mediaPlayer = mediaPlayerRef.current;
if (!mediaPlayer || !mediaPlayerReady) {