Skip to content

🎥 Make videos programmatically with React

Add experimental Studio Activity settings

Jonny Burgercc628727380b
diff --git a/packages/core/src/SequenceActivityBudget.tsx b/packages/core/src/SequenceActivityBudget.tsx
index e77b4ce96b..beba47ca99 100644
--- a/packages/core/src/SequenceActivityBudget.tsx
+++ b/packages/core/src/SequenceActivityBudget.tsx
@@ -1,12 +1,15 @@
import React, {createContext, useContext, useMemo, useState} from 'react';
import {createRegistryStore} from './registry-store.js';
-import {SequenceActivityContext} from './sequence-activity-context.js';
+import {
+ DEFAULT_SEQUENCE_ACTIVITY_LIMIT,
+ SequenceActivityContext,
+ SequenceActivitySettingsContext,
+} from './sequence-activity-context.js';
import {TimelineContext, type TimelineContextValue} from './TimelineContext.js';
import {useSyncExternalStore} from './use-sync-external-store.js';
// Count nested dormant content too: one admitted scene must not be able to
// recursively mount another thousand expensive scenes behind its Activity.
-const MAX_DORMANT_SEQUENCES = 20;
const useCommitEffect = React.useInsertionEffect ?? React.useLayoutEffect;
type ActivityCandidate = {
@@ -21,6 +24,7 @@ const createActivityBudget = () => {
const registry = createRegistryStore<ReadonlySet<string>>(new Set());
const candidates = new Map<string, ActivityCandidate>();
let frame: Record<string, number> = {};
+ let limit = DEFAULT_SEQUENCE_ACTIVITY_LIMIT;
let scheduled = false;
const schedule = () => {
if (scheduled) {
@@ -52,7 +56,7 @@ const createActivityBudget = () => {
let ancestor: ActivityCandidate | undefined = candidate;
while (ancestor && !selected.has(ancestor.id)) {
ancestors.push(ancestor.id);
- if (selected.size + ancestors.length > MAX_DORMANT_SEQUENCES) {
+ if (selected.size + ancestors.length > limit) {
break;
}
@@ -62,7 +66,7 @@ const createActivityBudget = () => {
: candidates.get(ancestor.parent);
}
- if (selected.size + ancestors.length > MAX_DORMANT_SEQUENCES) {
+ if (selected.size + ancestors.length > limit) {
continue;
}
@@ -70,14 +74,14 @@ const createActivityBudget = () => {
selected.add(id);
}
- if (selected.size === MAX_DORMANT_SEQUENCES) {
+ if (selected.size === limit) {
break;
}
}
// Reserve nearby visible trees too, so becoming dormant retains their
- // state immediately. All other visible trees bypass admission. At most
- // 20 reserved IDs can become dormant, including during a large seek.
+ // state immediately. All other visible trees bypass admission. Reserved
+ // IDs bound dormant content, including during a large seek.
registry.setSnapshot((previous) =>
previous.size === selected.size &&
[...previous].every((id) => selected.has(id))
@@ -89,6 +93,12 @@ const createActivityBudget = () => {
return {
...registry,
+ setLimit: (next: number) => {
+ if (limit !== next) {
+ limit = next;
+ schedule();
+ }
+ },
setFrame: (next: Record<string, number>) => {
frame = next;
schedule();
@@ -117,23 +127,38 @@ export const SequenceActivityBudgetProvider: React.FC<{
readonly children: React.ReactNode;
}> = ({children}) => {
const enabled = useContext(SequenceActivityContext);
+ const settings = useContext(SequenceActivitySettingsContext);
+ const limit = settings?.limit ?? DEFAULT_SEQUENCE_ACTIVITY_LIMIT;
+ const admitting = enabled && limit > 0;
const [budget] = useState(createActivityBudget);
const timeline = useContext(
- enabled ? TimelineContext : InactiveTimelineContext,
+ admitting ? TimelineContext : InactiveTimelineContext,
);
const selected = useSyncExternalStore(
budget.subscribe,
budget.getSnapshot,
budget.getSnapshot,
);
+ // Shrinking the setting must not keep the old, larger selection for a commit
+ // while the budget schedules its new snapshot.
+ const admitted = useMemo(
+ () =>
+ !admitting
+ ? emptySelection
+ : selected.size > limit
+ ? new Set([...selected].slice(0, limit))
+ : selected,
+ [admitting, limit, selected],
+ );
useCommitEffect(() => {
- if (enabled && timeline !== null) {
+ budget.setLimit(admitting ? limit : 0);
+ if (admitting && timeline !== null) {
budget.setFrame(timeline.frame);
}
- }, [budget, enabled, timeline]);
+ }, [admitting, budget, limit, timeline]);
return (
- <ActivityBudgetContext.Provider value={enabled ? budget : null}>
- <ActivitySelectionContext.Provider value={selected}>
+ <ActivityBudgetContext.Provider value={admitting ? budget : null}>
+ <ActivitySelectionContext.Provider value={admitted}>
{children}
</ActivitySelectionContext.Provider>
</ActivityBudgetContext.Provider>
diff --git a/packages/core/src/SequenceManager.tsx b/packages/core/src/SequenceManager.tsx
index 4ff8f8c86b..4b437d9c95 100644
--- a/packages/core/src/SequenceManager.tsx
+++ b/packages/core/src/SequenceManager.tsx
@@ -15,7 +15,10 @@ import {
reconcileRegistryEntries,
type RegistryStore,
} from './registry-store.js';
-import {SequenceActivityContext} from './sequence-activity-context.js';
+import {
+ SequenceActivityContext,
+ SequenceActivitySettingsContext,
+} from './sequence-activity-context.js';
import {SequenceActivityBudgetProvider} from './SequenceActivityBudget.js';
import {useRemotionEnvironment} from './use-remotion-environment.js';
import type {
@@ -391,16 +394,7 @@ export const SequenceManagerProvider: React.FC<{
readonly children: React.ReactNode;
}> = ({children}) => {
const {isStudio} = useRemotionEnvironment();
- const [activityRequested] = useState(
- () =>
- isStudio &&
- typeof window !== 'undefined' &&
- (new URLSearchParams(window.location.search).get(
- 'experimentalSequenceActivity',
- ) === 'true' ||
- localStorage.getItem('remotion.experimentalSequenceActivity') ===
- 'true'),
- );
+ const activitySettings = useContext(SequenceActivitySettingsContext);
const sequenceRegistrationEnabled = useContext(SequenceRegistrationContext);
const shouldObserveCommits = isStudio || sequenceRegistrationEnabled;
@@ -1161,7 +1155,7 @@ export const SequenceManagerProvider: React.FC<{
>
<SequenceActivityContext.Provider
value={
- activityRequested &&
+ activitySettings?.enabled === true &&
isStudio &&
commitRegistrationEnabled &&
typeof React.Activity !== 'undefined'
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index 97e4f5ff4a..799757d281 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -178,7 +178,11 @@ import {
useResolvedVideoConfig,
} from './ResolveCompositionConfig.js';
import {createRuntimeValueStore} from './runtime-value-store.js';
-import {SequenceContent} from './sequence-activity-context.js';
+import {
+ DEFAULT_SEQUENCE_ACTIVITY_LIMIT,
+ SequenceActivitySettingsContext,
+ SequenceContent,
+} from './sequence-activity-context.js';
import {resolveSequenceCrop} from './sequence-crop.js';
import type {
OverrideIdToNodePaths,
@@ -392,6 +396,8 @@ export const Internals = {
baseSchema,
sequenceSchema,
SequenceContent,
+ DEFAULT_SEQUENCE_ACTIVITY_LIMIT,
+ SequenceActivitySettingsContext,
SequenceWithoutSchema,
sequenceStyleSchema,
sequenceVisualStyleSchema,
diff --git a/packages/core/src/sequence-activity-context.ts b/packages/core/src/sequence-activity-context.ts
index 798f951ec6..68d0e79cf2 100644
--- a/packages/core/src/sequence-activity-context.ts
+++ b/packages/core/src/sequence-activity-context.ts
@@ -1,10 +1,18 @@
import {createContext, useContext} from 'react';
import type {TimelineContextValue} from './TimelineContext.js';
+export const DEFAULT_SEQUENCE_ACTIVITY_LIMIT = 20;
+
// Internal Studio experiment. Hidden Activity trees register on commit, so the
// experiment is disabled when the sequence manager falls back to effects.
export const SequenceActivityContext = createContext(false);
export const SequenceActivityDormantContext = createContext(false);
+export const SequenceActivitySettingsContext = createContext<{
+ readonly enabled: boolean;
+ readonly limit: number;
+ readonly setEnabled: (enabled: boolean) => void;
+ readonly setLimit: (limit: number) => void;
+} | null>(null);
// React propagates changed ancestor contexts through hidden Activity trees,
// even when a nested provider keeps their value stable. Use separate context
diff --git a/packages/studio/src/Studio.tsx b/packages/studio/src/Studio.tsx
index f1b8291615..db64475ca0 100644
--- a/packages/studio/src/Studio.tsx
+++ b/packages/studio/src/Studio.tsx
@@ -5,6 +5,7 @@ import {Internals} from 'remotion';
import {Editor} from './components/Editor';
import {EditorContexts} from './components/EditorContexts';
import {ServerDisconnected} from './components/Notifications/ServerDisconnected';
+import {SequenceActivitySettingsProvider} from './components/SequenceActivitySettingsProvider';
import {StaticFilesProvider} from './components/use-static-files';
import {FastRefreshProvider} from './FastRefreshProvider';
import {injectCSS} from './helpers/inject-css';
@@ -77,7 +78,9 @@ export const Studio: React.FC<{
return (
<FastRefreshProvider>
- <StudioInner rootComponent={rootComponent} readOnly={readOnly} />
+ <SequenceActivitySettingsProvider>
+ <StudioInner rootComponent={rootComponent} readOnly={readOnly} />
+ </SequenceActivitySettingsProvider>
</FastRefreshProvider>
);
};
diff --git a/packages/studio/src/components/SequenceActivitySettingsProvider.tsx b/packages/studio/src/components/SequenceActivitySettingsProvider.tsx
new file mode 100644
index 0000000000..23565b1db2
--- /dev/null
+++ b/packages/studio/src/components/SequenceActivitySettingsProvider.tsx
@@ -0,0 +1,106 @@
+import React, {useCallback, useEffect, useMemo, useState} from 'react';
+import {Internals} from 'remotion';
+
+const enabledKey = 'remotion.experimentalSequenceActivity';
+const limitKey = 'remotion.experimentalSequenceActivityLimit';
+
+export const SequenceActivitySettingsProvider: React.FC<{
+ readonly children: React.ReactNode;
+}> = ({children}) => {
+ const [settings, setSettings] = useState(() => {
+ const requested = new URLSearchParams(window.location.search).get(
+ 'experimentalSequenceActivity',
+ );
+ let enabled = false;
+ let limit: number = Internals.DEFAULT_SEQUENCE_ACTIVITY_LIMIT;
+ try {
+ enabled = localStorage.getItem(enabledKey) === 'true';
+ const storedLimit = localStorage.getItem(limitKey);
+ if (storedLimit !== null && storedLimit.trim() !== '') {
+ const parsedLimit = Number(storedLimit);
+ if (Number.isSafeInteger(parsedLimit) && parsedLimit >= 0) {
+ limit = parsedLimit;
+ }
+ }
+ } catch {
+ // The experiment remains usable when browser storage is unavailable.
+ }
+
+ return {
+ enabled:
+ requested === 'true' ? true : requested === 'false' ? false : enabled,
+ limit,
+ };
+ });
+ const setEnabled = useCallback((enabled: boolean) => {
+ try {
+ localStorage.setItem(enabledKey, String(enabled));
+ } catch {
+ // Apply the setting for this session even if it cannot be persisted.
+ }
+
+ setSettings((current) =>
+ current.enabled === enabled ? current : {...current, enabled},
+ );
+ }, []);
+ const setLimit = useCallback((limit: number) => {
+ if (!Number.isSafeInteger(limit) || limit < 0) {
+ return;
+ }
+
+ try {
+ localStorage.setItem(limitKey, String(limit));
+ } catch {
+ // Apply the setting for this session even if it cannot be persisted.
+ }
+
+ setSettings((current) =>
+ current.limit === limit ? current : {...current, limit},
+ );
+ }, []);
+ useEffect(() => {
+ const onStorage = (event: StorageEvent) => {
+ try {
+ if (event.storageArea !== localStorage) {
+ return;
+ }
+ } catch {
+ return;
+ }
+
+ if (event.key === enabledKey || event.key === null) {
+ const enabled = event.newValue === 'true';
+ setSettings((current) =>
+ current.enabled === enabled ? current : {...current, enabled},
+ );
+ }
+
+ if (event.key === limitKey || event.key === null) {
+ const parsedLimit =
+ event.newValue === null || event.newValue.trim() === ''
+ ? Internals.DEFAULT_SEQUENCE_ACTIVITY_LIMIT
+ : Number(event.newValue);
+ const limit =
+ Number.isSafeInteger(parsedLimit) && parsedLimit >= 0
+ ? parsedLimit
+ : Internals.DEFAULT_SEQUENCE_ACTIVITY_LIMIT;
+ setSettings((current) =>
+ current.limit === limit ? current : {...current, limit},
+ );
+ }
+ };
+
+ window.addEventListener('storage', onStorage);
+ return () => window.removeEventListener('storage', onStorage);
+ }, []);
+ const value = useMemo(
+ () => ({...settings, setEnabled, setLimit}),
+ [setEnabled, setLimit, settings],
+ );
+
+ return (
+ <Internals.SequenceActivitySettingsContext.Provider value={value}>
+ {children}
+ </Internals.SequenceActivitySettingsContext.Provider>
+ );
+};
diff --git a/packages/studio/src/components/StudioSettings.tsx b/packages/studio/src/components/StudioSettings.tsx
index b68576cb08..160fc3f56f 100644
--- a/packages/studio/src/components/StudioSettings.tsx
+++ b/packages/studio/src/components/StudioSettings.tsx
@@ -4,7 +4,14 @@ import type {
ConfigFileStudioSettings,
ConfigUpdate,
} from '@remotion/studio-shared';
-import React, {useCallback, useEffect, useMemo, useState} from 'react';
+import React, {
+ useCallback,
+ useContext,
+ useEffect,
+ useMemo,
+ useState,
+} from 'react';
+import {Internals} from 'remotion';
import {NoReactInternals} from 'remotion/no-react';
import {LIGHT_TEXT} from '../helpers/colors';
import {UndoIcon} from '../icons/undo';
@@ -112,6 +119,9 @@ const ConfigNumber = ({
export const StudioSettings: React.FC = () => {
const {error: settingsError, revision, studioRuntimeConfig} = useSettings();
+ const activitySettings = useContext(
+ Internals.SequenceActivitySettingsContext,
+ );
const [settings, setSettings] =
useState<ConfigFileStudioSettings>(initialSettings);
const [committedNumberSettings, setCommittedNumberSettings] = useState<{
@@ -417,6 +427,103 @@ export const StudioSettings: React.FC = () => {
}
value={settings.defaultPremountInSeconds}
/>
+ {activitySettings === null ? null : (
+ <>
+ <div style={optionRow}>
+ <div style={label}>
+ Sequence Activity (experimental)
+ <InfoBubble
+ aria-label="About experimental Sequence Activity"
+ horizontalAlignment="right"
+ >
+ <div
+ style={{
+ padding: 12,
+ maxWidth: 280,
+ fontSize: 14,
+ lineHeight: 1.5,
+ }}
+ >
+ Discovers layers in nearby scenes before they become visible.
+ This experimental feature may use more CPU. Changes apply
+ immediately and are saved in this browser.
+ </div>
+ </InfoBubble>
+ </div>
+ <label
+ style={rightRow}
+ aria-label="Sequence Activity (experimental)"
+ >
+ <Checkbox
+ checked={activitySettings.enabled}
+ name="Sequence Activity (experimental)"
+ onChange={(event) =>
+ activitySettings.setEnabled(event.target.checked)
+ }
+ />
+ </label>
+ </div>
+ <div style={optionRow}>
+ <div style={label}>
+ Hidden activity limit (experimental)
+ <InfoBubble
+ aria-label="About the hidden activity limit"
+ horizontalAlignment="right"
+ >
+ <div
+ style={{
+ padding: 12,
+ maxWidth: 280,
+ fontSize: 14,
+ lineHeight: 1.5,
+ }}
+ >
+ Limits nearby hidden scenes, including nested sequences and
+ their ancestors. Visible, premounted, and postmounted scenes
+ always render. Set to 0 to disable hidden discovery. Default:{' '}
+ {Internals.DEFAULT_SEQUENCE_ACTIVITY_LIMIT}.
+ </div>
+ </InfoBubble>
+ </div>
+ <div style={{...rightRow, gap: 6}}>
+ <InputDragger
+ aria-label="Hidden activity limit (experimental)"
+ buttonStyle={{textAlign: 'right', width: 140}}
+ formatter={String}
+ integerOnly
+ min={0}
+ onTextChange={() => undefined}
+ onValueChange={activitySettings.setLimit}
+ rightAlign
+ status="ok"
+ step={1}
+ value={activitySettings.limit}
+ />
+ <Button
+ disabled={
+ activitySettings.limit ===
+ Internals.DEFAULT_SEQUENCE_ACTIVITY_LIMIT
+ }
+ onClick={() =>
+ activitySettings.setLimit(
+ Internals.DEFAULT_SEQUENCE_ACTIVITY_LIMIT,
+ )
+ }
+ size="compact"
+ style={{color: LIGHT_TEXT}}
+ aria-label={`Reset hidden activity limit to ${Internals.DEFAULT_SEQUENCE_ACTIVITY_LIMIT}`}
+ >
+ <UndoIcon style={resetIcon} />
+ </Button>
+ </div>
+ </div>
+ <div
+ style={{padding: '4px 16px 8px', color: LIGHT_TEXT, fontSize: 12}}
+ >
+ Activity settings save in this browser and apply immediately.
+ </div>
+ </>
+ )}
<p style={sectionTitle}>Audio</p>
<ConfigSelect