🎥 Make videos programmatically with React
Make Player playback dependencies and audio rescheduling explicit
JonnyBurgerc29212559d13
diff --git a/packages/player/src/use-playback.ts b/packages/player/src/use-playback.tsindex 4a93db04fb..fb7a282e20 100644--- a/packages/player/src/use-playback.ts+++ b/packages/player/src/use-playback.ts@@ -35,8 +35,9 @@ const shouldForceAnchorChange = (newState: RemotionAudioContextState) => {// restart the clock from the current frame and lose the already elapsed// portion of the frame.// A fresh clock is created only where a restart is correct: when starting-// playback, or when the fps or playback rate changed mid-playback.+// playback, or when the composition, fps or playback rate changed mid-playback.type PlaybackClock = {+ compositionId: string;startedTime: number;framesAdvanced: number;fps: number;@@ -63,6 +64,9 @@ export const usePlayback = ({muted: boolean;}) => {const config = Internals.useUnsafeVideoConfig();+ const fps = config?.fps ?? null;+ const durationInFrames = config?.durationInFrames ?? null;+ const compositionId = config?.id ?? null;const frame = Internals.Timeline.useTimelinePosition();const playing = Internals.usePlaying();const {pause, emitter, isPlaying} = usePlayerMethods();@@ -102,7 +106,7 @@ export const usePlayback = ({return;}- if (!config) {+ if (fps === null) {return;}@@ -113,7 +117,7 @@ export const usePlayback = ({const changed = setGlobalTimeAnchor({audioContext: sharedAudioContext.audioContext,audioSyncAnchor: sharedAudioContext.audioSyncAnchor,- absoluteTimeInSeconds: frame / config.fps,+ absoluteTimeInSeconds: frame / fps,globalPlaybackRate: playbackRate,logLevel,force: false,@@ -121,7 +125,7 @@ export const usePlayback = ({if (changed) {sharedAudioContext.audioSyncAnchorEmitter.dispatch('changed');}- }, [config, frame, logLevel, playbackRate, sharedAudioContext, muted]);+ }, [fps, frame, logLevel, playbackRate, sharedAudioContext, muted]);// When the audio context is suspended, we use the opportunity to// re-anchor the time to be exact.@@ -131,7 +135,7 @@ export const usePlayback = ({return;}- if (!config) {+ if (fps === null) {return;}@@ -145,7 +149,7 @@ export const usePlayback = ({const changed = setGlobalTimeAnchor({audioContext,audioSyncAnchor: sharedAudioContext.audioSyncAnchor,- absoluteTimeInSeconds: getCurrentFrame() / config.fps,+ absoluteTimeInSeconds: getCurrentFrame() / fps,globalPlaybackRate: playbackRate,logLevel,force: true,@@ -160,20 +164,14 @@ export const usePlayback = ({return () => {audioContext?.removeEventListener('statechange', callback);};- }, [- config,- getCurrentFrame,- logLevel,- muted,- playbackRate,- sharedAudioContext,- ]);+ }, [fps, getCurrentFrame, logLevel, muted, playbackRate, sharedAudioContext]);useEffect(() => {const wasMuted = previousMutedRef.current;previousMutedRef.current = muted;- if (!config) {+ if (fps === null || durationInFrames === null || compositionId === null) {+ playbackClockRef.current = null;return;}@@ -183,19 +181,23 @@ export const usePlayback = ({return;}- // Either carry over the previous clock or start a fresh one.+ // Duration and range changes update the endpoint without losing elapsed+ // sub-frame time. A different composition or timebase needs a fresh clock.const previousClock = playbackClockRef.current;- const clock: PlaybackClock =- previousClock !== null &&- previousClock.fps === config.fps &&- previousClock.playbackRate === playbackRate- ? previousClock- : {- startedTime: performance.now(),- framesAdvanced: 0,- fps: config.fps,- playbackRate,- };+ const shouldRestartClock =+ previousClock === null ||+ previousClock.compositionId !== compositionId ||+ previousClock.fps !== fps ||+ previousClock.playbackRate !== playbackRate;+ const clock: PlaybackClock = shouldRestartClock+ ? {+ compositionId,+ startedTime: performance.now(),+ framesAdvanced: 0,+ fps,+ playbackRate,+ }+ : previousClock;playbackClockRef.current = clock;if (@@ -203,31 +205,28 @@ export const usePlayback = ({sharedAudioContext.audioContext &&!muted) {- // With _experimentalKeepAudioContextAlive, the context clock keeps- // running while frames are not advancing (pauses, buffering, muted playback), so- // the anchor is stale by the length of the stall. Without this mode,- // the 'statechange' listener above re-anchors on the- // suspended-to-running transition, but that transition never happens- // here. Re-anchor from the current frame instead, and tell the audio- // iterators so they drop the nodes they queued against the old- // anchor and reschedule.- // A config-only re-run keeps the same playback clock. In that case,- // respect the drift tolerance instead of forcing a frame-quantized shift.- // A replacement AudioContext has its own time origin, so its first- // anchor must be exact even when the playback clock was reused.+ const didUnmute = wasMuted && !muted;+ const didReplaceAudioContext =+ anchoredAudioContextRef.current !== sharedAudioContext.audioContext;+ const shouldRescheduleAudio =+ shouldRestartClock || didUnmute || didReplaceAudioContext;++ // The native clock keeps running through pauses and muted playback.+ // These transitions need an exact anchor, as does a replacement context+ // with its own time origin. Other effect re-runs use the drift tolerance+ // so duration changes do not introduce frame-quantized audio shifts.const changed = setGlobalTimeAnchor({audioContext: sharedAudioContext.audioContext,audioSyncAnchor: sharedAudioContext.audioSyncAnchor,- absoluteTimeInSeconds: getCurrentFrame() / config.fps,+ absoluteTimeInSeconds: getCurrentFrame() / fps,globalPlaybackRate: playbackRate,logLevel,- force:- clock !== previousClock ||- wasMuted ||- anchoredAudioContextRef.current !== sharedAudioContext.audioContext,+ force: shouldRescheduleAudio,});anchoredAudioContextRef.current = sharedAudioContext.audioContext;- if (changed) {+ // A changed timebase can invalidate queued audio even if the anchor+ // happens to remain numerically equal (for example at frame zero).+ if (changed || shouldRescheduleAudio) {sharedAudioContext.audioSyncAnchorEmitter.dispatch('changed');}}@@ -277,7 +276,7 @@ export const usePlayback = ({}const time = performance.now() - clock.startedTime;- const actualLastFrame = outFrame ?? config.durationInFrames - 1;+ const actualLastFrame = outFrame ?? durationInFrames - 1;const actualFirstFrame = inFrame ?? 0;const seekRevision = seek?.revision.current ?? null;@@ -292,7 +291,7 @@ export const usePlayback = ({time,currentFrame,playbackSpeed: playbackRate,- fps: config.fps,+ fps,actualFirstFrame,actualLastFrame,framesAdvanced: clock.framesAdvanced,@@ -311,7 +310,7 @@ export const usePlayback = ({if (frameToDisplay !== currentFrame && !isBuffering()) {playbackFrame = frameToDisplay;- setFrameFromPlayback((c) => ({...c, [config.id]: frameToDisplay}));+ setFrameFromPlayback((c) => ({...c, [compositionId]: frameToDisplay}));}if (hasEnded) {@@ -385,7 +384,7 @@ export const usePlayback = ({reqAnimFrameCall = {type: 'timeout',// Note: Most likely, this will not be 1000 / fps, but the browser will throttle it to ~1/sec.- id: setTimeout(callback, 1000 / config.fps),+ id: setTimeout(callback, 1000 / fps),};return;}@@ -413,7 +412,9 @@ export const usePlayback = ({stop();};}, [- config,+ compositionId,+ durationInFrames,+ fps,loop,pause,playing,diff --git a/packages/player/src/use-player-methods.ts b/packages/player/src/use-player-methods.tsindex 31a2ffff9e..73829e889c 100644--- a/packages/player/src/use-player-methods.ts+++ b/packages/player/src/use-player-methods.ts@@ -32,6 +32,8 @@ export const usePlayerMethods = (): UsePlayerMethods => {const audioTagsContext = useContext(Internals.SharedAudioTagsContext);const environment = useRemotionEnvironment();const video = Internals.useVideo();+ const videoId = video?.id ?? null;+ const videoDurationInFrames = video?.durationInFrames ?? null;const config = Internals.useUnsafeVideoConfig();const emitter = useContext(PlayerEventEmitterContext);const playStart = useRef(0);@@ -43,7 +45,7 @@ export const usePlayerMethods = (): UsePlayerMethods => {}const getCurrentFrame = useCallback(() => {- if (!video) {+ if (videoId === null || videoDurationInFrames === null) {return (fallbackFrame.current ??(typeof window === 'undefined'@@ -53,16 +55,16 @@ export const usePlayerMethods = (): UsePlayerMethods => {}const unclamped =- frameRef.current[video.id] ??+ frameRef.current[videoId] ??(environment.isPlayer? 0- : Internals.Timeline.getFrameForComposition(video.id));+ : Internals.Timeline.getFrameForComposition(videoId));return Internals.Timeline.clampFrameToCompositionRange(unclamped,- video.durationInFrames,+ videoDurationInFrames,);- }, [environment.isPlayer, frameRef, video]);+ }, [environment.isPlayer, frameRef, videoId, videoDurationInFrames]);const seek = useCallback((newFrame: number) => {@@ -176,7 +178,6 @@ export const usePlayerMethods = (): UsePlayerMethods => {}}, [config, emitter, frameRef, readIsPlaying, setPlaying, seekFrame]);- const videoId = video?.id;const lastFrame = (config?.durationInFrames ?? 1) - 1;const frameBack = useCallback(