Skip to content

🎥 Make videos programmatically with React

6.4 KB
Raw Blame
// Contexts shared between <Player> and <Thumbnail>

import type {ComponentType, LazyExoticComponent} from 'react';
import React, {useCallback, useMemo, useRef, useState} from 'react';
import type {
	CompositionManagerContext,
	LoggingContextValue,
	LogLevel,
	MediaVolumeContextValue,
	PlaybackRateContextValue,
	RemotionEnvironment,
	SetMediaVolumeContextValue,
	TimelineContextValue,
} from 'remotion';
import {Internals} from 'remotion';
import {getPreferredVolume, persistVolume} from './volume-persistence.js';

export const PLAYER_COMP_ID = 'player-comp';

export const SharedPlayerContexts: React.FC<{
	readonly children: React.ReactNode;
	readonly timelineContext: TimelineContextValue;
	readonly playbackRateContext: PlaybackRateContextValue;
	readonly preservePitch: boolean;
	readonly fps: number;
	readonly compositionWidth: number;
	readonly compositionHeight: number;
	readonly durationInFrames: number;
	readonly component: LazyExoticComponent<ComponentType<unknown>>;
	readonly numberOfSharedAudioTags: number;
	readonly initiallyMuted: boolean;
	readonly logLevel: LogLevel;
	readonly audioLatencyHint: AudioContextLatencyCategory;
	readonly sampleRate: number;
	readonly volumePersistenceKey?: string;
	readonly initialVolume?: number;
	readonly inputProps: Record<string, unknown>;
	readonly audioEnabled: boolean;
	readonly _experimentalKeepAudioContextAlive: boolean;
}> = ({
	children,
	timelineContext,
	playbackRateContext,
	preservePitch,
	fps,
	compositionHeight,
	compositionWidth,
	durationInFrames,
	component,
	numberOfSharedAudioTags,
	initiallyMuted,
	logLevel,
	audioLatencyHint,
	sampleRate,
	volumePersistenceKey,
	initialVolume,
	inputProps,
	audioEnabled,
	_experimentalKeepAudioContextAlive,
}) => {
	const persistVolumeToStorage = initialVolume === undefined;
	const compositionManagerContext: CompositionManagerContext = useMemo(() => {
		const context: CompositionManagerContext = {
			compositions: [
				{
					component: component as React.LazyExoticComponent<
						ComponentType<unknown>
					>,
					durationInFrames,
					height: compositionHeight,
					width: compositionWidth,
					fps,
					id: PLAYER_COMP_ID,
					order: null,
					folderName: null,
					parentFolderName: null,
					schema: null,
					calculateMetadata: null,
					stack: null,
				},
			],
			folders: [],
			currentAssetMetadata: null,
			currentCompositionMetadata: {
				defaultCodec: null,
				defaultOutName: null,
				defaultPixelFormat: null,
				defaultProResProfile: null,
				defaultSampleRate: null,
				defaultVideoImageFormat: null,
				durationInFrames,
				fps,
				height: compositionHeight,
				width: compositionWidth,
				props: inputProps,
			},
			canvasContent: {type: 'composition', compositionId: 'player-comp'},
		};
		return context;
	}, [
		component,
		durationInFrames,
		compositionHeight,
		compositionWidth,
		fps,
		inputProps,
	]);

	const [playerMuted, setPlayerMuted] = useState<boolean>(() => initiallyMuted);
	const [mediaVolume, setMediaVolume] = useState<number>(() =>
		persistVolumeToStorage
			? getPreferredVolume(volumePersistenceKey ?? null)
			: initialVolume,
	);

	const mediaVolumeContextValue = useMemo((): MediaVolumeContextValue => {
		return {
			playerMuted,
			mediaVolume,
		};
	}, [playerMuted, mediaVolume]);

	const audioContextWasCreated = useRef(false);
	const shouldCreateAudioContext =
		audioContextWasCreated.current ||
		(audioEnabled && !playerMuted && mediaVolume > 0);
	audioContextWasCreated.current = shouldCreateAudioContext;

	const setMediaVolumeAndPersist = useCallback(
		(vol: number) => {
			setMediaVolume(vol);
			if (persistVolumeToStorage) {
				persistVolume(vol, logLevel, volumePersistenceKey ?? null);
			}
		},
		[persistVolumeToStorage, logLevel, volumePersistenceKey],
	);

	const setMediaVolumeContextValue = useMemo((): SetMediaVolumeContextValue => {
		return {
			setPlayerMuted,
			setMediaVolume: setMediaVolumeAndPersist,
		};
	}, [setMediaVolumeAndPersist]);

	const logLevelContext: LoggingContextValue = useMemo(() => {
		return {
			logLevel,
			mountTime: Date.now(),
		};
	}, [logLevel]);

	const env: RemotionEnvironment = useMemo(() => {
		return {
			isPlayer: true,
			isRendering: false,
			isStudio: false,
			isClientSideRendering: false,
			isReadOnlyStudio: false,
		};
	}, []);

	return (
		<Internals.RemotionEnvironmentContext.Provider value={env}>
			<Internals.LogLevelContext.Provider value={logLevelContext}>
				<Internals.CanUseRemotionHooksProvider>
					<Internals.AbsoluteTimeContext.Provider value={timelineContext}>
						<Internals.PlaybackRateContext.Provider value={playbackRateContext}>
							<Internals.PlaybackPitchContext.Provider value={preservePitch}>
								<Internals.TimelineContext.Provider value={timelineContext}>
									<Internals.CompositionManager.Provider
										value={compositionManagerContext}
									>
										<Internals.VideoProvider>
											<Internals.PrefetchProvider>
												<Internals.DurationsContextProvider>
													<Internals.MediaVolumeContext.Provider
														value={mediaVolumeContextValue}
													>
														<Internals.SetMediaVolumeContext.Provider
															value={setMediaVolumeContextValue}
														>
															<Internals.BufferingProvider>
																<Internals.SharedAudioContextProvider
																	audioLatencyHint={audioLatencyHint}
																	audioEnabled={shouldCreateAudioContext}
																	previewSampleRate={sampleRate}
																	_experimentalKeepAudioContextAlive={
																		_experimentalKeepAudioContextAlive
																	}
																>
																	<Internals.SharedAudioTagsContextProvider
																		numberOfAudioTags={numberOfSharedAudioTags}
																	>
																		{children}
																	</Internals.SharedAudioTagsContextProvider>
																</Internals.SharedAudioContextProvider>
															</Internals.BufferingProvider>
														</Internals.SetMediaVolumeContext.Provider>
													</Internals.MediaVolumeContext.Provider>
												</Internals.DurationsContextProvider>
											</Internals.PrefetchProvider>
										</Internals.VideoProvider>
									</Internals.CompositionManager.Provider>
								</Internals.TimelineContext.Provider>
							</Internals.PlaybackPitchContext.Provider>
						</Internals.PlaybackRateContext.Provider>
					</Internals.AbsoluteTimeContext.Provider>
				</Internals.CanUseRemotionHooksProvider>
			</Internals.LogLevelContext.Provider>
		</Internals.RemotionEnvironmentContext.Provider>
	);
};