🎥 Make videos programmatically with React
`@remotion/media`: Avoid race condition with seeking during initialization phase
Jonny Burger36dbc66a2540
diff --git a/packages/media/src/audio/audio-for-preview.tsx b/packages/media/src/audio/audio-for-preview.tsxindex bb4af37fb8..8b6eb8be8a 100644--- a/packages/media/src/audio/audio-for-preview.tsx+++ b/packages/media/src/audio/audio-for-preview.tsx@@ -156,6 +156,7 @@ const AudioForPreviewAssertedShowing: React.FC<NewAudioForPreviewProps> = ({}const isPlayerBuffering = Internals.useIsPlayerBuffering(bufferingContext);+ const initialPlaying = useRef(playing && !isPlayerBuffering);const initialIsPremounting = useRef(isPremounting);const initialIsPostmounting = useRef(isPostmounting);const initialGlobalPlaybackRate = useRef(globalPlaybackRate);@@ -183,6 +184,7 @@ const AudioForPreviewAssertedShowing: React.FC<NewAudioForPreviewProps> = ({isPremounting: initialIsPremounting.current,globalPlaybackRate: initialGlobalPlaybackRate.current,onVideoFrameCallback: null,+ playing: initialPlaying.current,});mediaPlayerRef.current = player;diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.tsindex 792fe4fc4e..d3da2e5c46 100644--- a/packages/media/src/media-player.ts+++ b/packages/media/src/media-player.ts@@ -66,7 +66,7 @@ export class MediaPlayer {private isPremounting: boolean;private isPostmounting: boolean;- private seekPromiseChain: Promise<void> = Promise.resolve();+ private seekPromiseChain: Promise<unknown> = Promise.resolve();constructor({canvas,@@ -85,6 +85,7 @@ export class MediaPlayer {isPremounting,isPostmounting,onVideoFrameCallback,+ playing,}: {canvas: HTMLCanvasElement | OffscreenCanvas | null;src: string;@@ -102,6 +103,7 @@ export class MediaPlayer {isPremounting: boolean;isPostmounting: boolean;onVideoFrameCallback: null | ((frame: CanvasImageSource) => void);+ playing: boolean;}) {this.canvas = canvas ?? null;this.src = src;@@ -120,6 +122,7 @@ export class MediaPlayer {this.isPostmounting = isPostmounting;this.nonceManager = makeNonceManager();this.onVideoFrameCallback = onVideoFrameCallback;+ this.playing = playing;this.input = new Input({source: new UrlSource(this.src),@@ -153,12 +156,14 @@ export class MediaPlayer {): Promise<MediaPlayerInitResult> {const promise = this._initialize(startTimeUnresolved);this.initializationPromise = promise;+ this.seekPromiseChain = promise;return promise;}private async _initialize(startTimeUnresolved: number,): Promise<MediaPlayerInitResult> {+ const delayHandle = this.delayPlaybackHandleIfNotPremounting();try {if (this.input.disposed) {return {type: 'disposed'};@@ -259,18 +264,20 @@ export class MediaPlayer {const nonce = this.nonceManager.createAsyncOperation();try {- // intentionally not awaited- if (this.audioIteratorManager) {- this.audioIteratorManager.startAudioIterator({- nonce,- playbackRate: this.playbackRate * this.globalPlaybackRate,- startFromSecond: startTime,- getIsPlaying: () => this.playing,- scheduleAudioNode: this.scheduleAudioNode,- });- }-- await this.videoIteratorManager?.startVideoIterator(startTime, nonce);+ await Promise.all([+ this.audioIteratorManager+ ? this.audioIteratorManager.startAudioIterator({+ nonce,+ playbackRate: this.playbackRate * this.globalPlaybackRate,+ startFromSecond: startTime,+ getIsPlaying: () => this.playing,+ scheduleAudioNode: this.scheduleAudioNode,+ })+ : Promise.resolve(),+ this.videoIteratorManager+ ? this.videoIteratorManager.startVideoIterator(startTime, nonce)+ : Promise.resolve(),+ ]);} catch (error) {if (this.isDisposalError()) {return {type: 'disposed'};@@ -302,6 +309,8 @@ export class MediaPlayer {error,);throw error;+ } finally {+ delayHandle.unblock();}}diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsxindex c12841e3bd..e6de1349ff 100644--- a/packages/media/src/video/video-for-preview.tsx+++ b/packages/media/src/video/video-for-preview.tsx@@ -166,6 +166,7 @@ const VideoForPreviewAssertedShowing: React.FC<VideoForPreviewProps> = ({}const isPlayerBuffering = Internals.useIsPlayerBuffering(buffering);+ const initialPlaying = useRef(playing && !isPlayerBuffering);const initialIsPremounting = useRef(isPremounting);const initialIsPostmounting = useRef(isPostmounting);const initialGlobalPlaybackRate = useRef(globalPlaybackRate);@@ -193,6 +194,7 @@ const VideoForPreviewAssertedShowing: React.FC<VideoForPreviewProps> = ({isPostmounting: initialIsPostmounting.current,globalPlaybackRate: initialGlobalPlaybackRate.current,onVideoFrameCallback: initialOnVideoFrameRef.current ?? null,+ playing: initialPlaying.current,});mediaPlayerRef.current = player;