Skip to content

🎥 Make videos programmatically with React

remove premount context and sync to audiocontext

Jonny Burger736aa00e4d92
diff --git a/packages/core/src/PremountContext.tsx b/packages/core/src/PremountContext.tsx
index 2e45d63513..5347ae38c5 100644
--- a/packages/core/src/PremountContext.tsx
+++ b/packages/core/src/PremountContext.tsx
@@ -2,7 +2,6 @@ import {createContext} from 'react';
export type PremountContextValue = {
premountFramesRemaining: number;
- playing: boolean;
};
/**
@@ -13,5 +12,4 @@ export type PremountContextValue = {
*/
export const PremountContext = createContext<PremountContextValue>({
premountFramesRemaining: 0,
- playing: false,
});
diff --git a/packages/core/src/Sequence.tsx b/packages/core/src/Sequence.tsx
index 8ba51519f4..2b10a421da 100644
--- a/packages/core/src/Sequence.tsx
+++ b/packages/core/src/Sequence.tsx
@@ -366,32 +366,20 @@ const PremountedPostmountedSequenceRefForwardingFunction: React.ForwardRefRender
styleWhilePostmounted,
]);
- const {playing} = useTimelineContext();
- const premountFramesRemaining = premountingActive ? from - frame : 0;
-
- const premountContextValue = useMemo(() => {
- return {
- premountFramesRemaining,
- playing: parentPremountContext.playing || playing,
- };
- }, [premountFramesRemaining, parentPremountContext.playing, playing]);
-
return (
- <PremountContext.Provider value={premountContextValue}>
- <Freeze frame={freezeFrame} active={isFreezingActive}>
- <Sequence
- ref={ref}
- from={from}
- durationInFrames={durationInFrames}
- style={style}
- _remotionInternalPremountDisplay={premountFor}
- _remotionInternalPostmountDisplay={postmountFor}
- _remotionInternalIsPremounting={premountingActive}
- _remotionInternalIsPostmounting={postmountingActive}
- {...otherProps}
- />
- </Freeze>
- </PremountContext.Provider>
+ <Freeze frame={freezeFrame} active={isFreezingActive}>
+ <Sequence
+ ref={ref}
+ from={from}
+ durationInFrames={durationInFrames}
+ style={style}
+ _remotionInternalPremountDisplay={premountFor}
+ _remotionInternalPostmountDisplay={postmountFor}
+ _remotionInternalIsPremounting={premountingActive}
+ _remotionInternalIsPostmounting={postmountingActive}
+ {...otherProps}
+ />
+ </Freeze>
);
};
diff --git a/packages/core/src/audio/shared-audio-tags.tsx b/packages/core/src/audio/shared-audio-tags.tsx
index 65e9be47dc..2290a4eb6b 100644
--- a/packages/core/src/audio/shared-audio-tags.tsx
+++ b/packages/core/src/audio/shared-audio-tags.tsx
@@ -85,6 +85,7 @@ type SharedContext = {
scheduleAudioNode: (
options: ScheduleAudioNodeOptions,
) => ScheduleAudioNodeResult;
+ resume: () => Promise<void>;
};
const compareProps = (
@@ -169,6 +170,8 @@ export const SharedAudioContextProvider: React.FC<{
mediaEndTime: number | null;
}>({scheduledEndTime: null, mediaEndTime: null});
+ const nodesToResume = useRef<(() => void)[]>([]);
+
const scheduleAudioNode = useMemo(() => {
return ({
node,
@@ -184,7 +187,13 @@ export const SharedAudioContextProvider: React.FC<{
}
if (duration > 0) {
- node.start(scheduledTime, offset, duration);
+ if (audioContext.state === 'suspended') {
+ nodesToResume.current.push(() =>
+ node.start(scheduledTime, offset, duration),
+ );
+ } else {
+ node.start(scheduledTime, offset, duration);
+ }
}
const scheduledEndTime =
@@ -445,6 +454,17 @@ export const SharedAudioContextProvider: React.FC<{
const env = useRemotionEnvironment();
+ const resume = useCallback(() => {
+ if (!audioContext) {
+ return Promise.resolve();
+ }
+
+ return audioContext.resume().then(() => {
+ nodesToResume.current.forEach((r) => r());
+ nodesToResume.current = [];
+ });
+ }, [audioContext]);
+
const playAllAudios = useCallback(() => {
refs.forEach((ref) => {
const audio = audios.current.find((a) => a.el === ref.ref);
@@ -462,8 +482,8 @@ export const SharedAudioContextProvider: React.FC<{
isPlayer: env.isPlayer,
});
});
- audioContext?.resume();
- }, [audioContext, logLevel, mountTime, refs, env.isPlayer]);
+ resume();
+ }, [logLevel, mountTime, refs, env.isPlayer, resume]);
const value: SharedContext = useMemo(() => {
return {
@@ -475,6 +495,7 @@ export const SharedAudioContextProvider: React.FC<{
audioContext,
audioSyncAnchor,
scheduleAudioNode,
+ resume,
};
}, [
numberOfAudioTags,
@@ -485,6 +506,7 @@ export const SharedAudioContextProvider: React.FC<{
audioContext,
audioSyncAnchor,
scheduleAudioNode,
+ resume,
]);
return (
diff --git a/packages/media/src/audio-iterator-manager.ts b/packages/media/src/audio-iterator-manager.ts
index 65bf1fcafb..1873f1f7b1 100644
--- a/packages/media/src/audio-iterator-manager.ts
+++ b/packages/media/src/audio-iterator-manager.ts
@@ -83,25 +83,17 @@ export const audioIteratorManager = ({
playbackRate,
scheduleAudioNode,
debugAudioScheduling,
- getAudioContextState,
}: {
buffer: AudioBuffer;
mediaTimestamp: number;
playbackRate: number;
scheduleAudioNode: ScheduleAudioNode;
- getAudioContextState: () => AudioContextState;
debugAudioScheduling: boolean;
}) => {
if (!audioBufferIterator) {
throw new Error('Audio buffer iterator not found');
}
- if (getAudioContextState() !== 'running') {
- throw new Error(
- 'Tried to schedule node while audio context is not running',
- );
- }
-
if (muted) {
return;
}
@@ -146,13 +138,11 @@ export const audioIteratorManager = ({
};
const onAudioChunk = ({
- getAudioContextState,
buffer,
playbackRate,
scheduleAudioNode,
debugAudioScheduling,
}: {
- getAudioContextState: () => AudioContextState;
buffer: WrappedAudioBuffer;
playbackRate: number;
scheduleAudioNode: ScheduleAudioNode;
@@ -180,7 +170,6 @@ export const audioIteratorManager = ({
playbackRate,
scheduleAudioNode,
debugAudioScheduling,
- getAudioContextState,
});
drawDebugOverlay();
@@ -256,7 +245,6 @@ export const audioIteratorManager = ({
notifyNodeScheduled();
onAudioChunk({
- getAudioContextState,
buffer: result.value,
playbackRate,
scheduleAudioNode,
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index b7f648416b..0be5084c23 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -735,4 +735,12 @@ export class MediaPlayer {
});
}
};
+
+ public audioSyncAnchorChanged = () => {
+ if (!this.audioIteratorManager) {
+ return;
+ }
+
+ this.audioIteratorManager.destroyIterator();
+ };
}
diff --git a/packages/media/src/set-global-time-anchor.ts b/packages/media/src/set-global-time-anchor.ts
index 19a8bc9887..6124c9a975 100644
--- a/packages/media/src/set-global-time-anchor.ts
+++ b/packages/media/src/set-global-time-anchor.ts
@@ -16,7 +16,7 @@ export const setGlobalTimeAnchor = ({
globalPlaybackRate: number;
debugAudioScheduling: boolean;
logLevel: LogLevel;
-}): void => {
+}): boolean => {
const newAnchor =
audioContext.currentTime - absoluteTimeInSeconds / globalPlaybackRate;
const shift = (newAnchor - audioSyncAnchor.value) * globalPlaybackRate;
@@ -26,7 +26,7 @@ export const setGlobalTimeAnchor = ({
// Skip small shifts to avoid audio glitches from frame-quantized re-anchoring
if (Math.abs(shift) < ALLOWED_GLOBAL_TIME_ANCHOR_SHIFT + latency) {
- return;
+ return false;
}
if (debugAudioScheduling) {
@@ -40,4 +40,5 @@ export const setGlobalTimeAnchor = ({
}
audioSyncAnchor.value = newAnchor;
+ return true;
};
diff --git a/packages/media/src/use-common-effects.ts b/packages/media/src/use-common-effects.ts
index e2016bec9c..18bd4be62f 100644
--- a/packages/media/src/use-common-effects.ts
+++ b/packages/media/src/use-common-effects.ts
@@ -1,5 +1,5 @@
import type React from 'react';
-import {useContext, useLayoutEffect} from 'react';
+import {useLayoutEffect} from 'react';
import type {LogLevel} from 'remotion';
import {Internals} from 'remotion';
import type {MediaPlayer} from './media-player';
@@ -58,16 +58,13 @@ export const useCommonEffects = ({
readonly label: string;
}) => {
const absoluteTime = Internals.useAbsoluteTimelinePosition();
- const {playing: playingWhilePremounting} = useContext(
- Internals.PremountContext,
- );
useLayoutEffect(() => {
if (
sharedAudioContext?.audioContext &&
sharedAudioContext.audioSyncAnchor
) {
- setGlobalTimeAnchor({
+ const changed = setGlobalTimeAnchor({
audioContext: sharedAudioContext.audioContext,
audioSyncAnchor: sharedAudioContext.audioSyncAnchor,
absoluteTimeInSeconds: absoluteTime / fps,
@@ -75,6 +72,9 @@ export const useCommonEffects = ({
debugAudioScheduling,
logLevel,
});
+ if (changed) {
+ mediaPlayerRef.current?.audioSyncAnchorChanged();
+ }
}
}, [
absoluteTime,
@@ -83,6 +83,7 @@ export const useCommonEffects = ({
fps,
debugAudioScheduling,
logLevel,
+ mediaPlayerRef,
]);
useLayoutEffect(() => {
diff --git a/packages/player/src/use-player.ts b/packages/player/src/use-player.ts
index cd886f8ba5..40d6e3e20e 100644
--- a/packages/player/src/use-player.ts
+++ b/packages/player/src/use-player.ts
@@ -82,7 +82,7 @@ export const usePlayer = (): UsePlayerMethods => {
seek(0);
}
- audioContext?.audioContext?.resume();
+ audioContext?.resume();
/**
* Play silent audio tags to warm them up for autoplay