Composition runtime
The composition runtime turns timeline data into a live, frame-based preview. It builds a tree of timed visual and audio content, supplies each item with its composition space and animation state, and keeps that tree connected to the player clock so the editor can scrub and play the result.
Runtime model
The runtime treats a composition as a timed scene rather than as a list of independent widgets. A composition has tracks, tracks have ordered items, and each item occupies a frame range that determines when it contributes to the scene.
Timeline representation
src/…/timeline.ts defines the runtime input model. A TimelineItem is a tagged item such as video, audio, image, Lottie animation, text, shape, nested composition, adjustment layer, controller, or subtitle segment. Items carry their timeline range and may also carry source timing, transforms, effects, masks, audio settings, and links to related items.
The runtime does not change the timeline into a new product model. It enriches the items for display with resolved media sources, track order, visibility, and timing data. MainComposition then separates the enriched data into visual layers and audio segments while preserving track order for compositing.
References: src/…/timeline.ts• L57-75 src/…/timeline.ts• L191-234 src/…/timeline.ts• L392-474 src/…/main-composition.tsx• L251-264
Render tree
MainComposition is the root of the runtime scene. It provides the shared composition state, paints the background, renders audio outside the visual layer, and places videos and non-media items into one visual stack. A Sequence gives each item a timed boundary and a local frame.
The main scene follows this path:
flowchart TD
A[Timeline tracks and items] --> B[MainComposition]
B --> C[Render plan]
C --> D[Timed audio segments]
C --> E[Timed visual layers]
E --> F[ItemContent]
F --> G[Media or drawing content]
F --> H[Nested CompositionContent]
H --> I[Child composition tree]
This structure lets the same item model support ordinary playback, nested compositions, and alternate preview paths. Visual order and audio timing are resolved before the item components render.
References: src/…/main-composition.tsx• L450-465 src/…/main-composition.tsx• L467-543 src/…/main-composition.tsx• L681-748 src/…/Sequence.tsx• L47-51
Item rendering
The item layer maps a timeline item to the content that can present it. It also applies shared visual state around that content, so transforms, masks, crop, blend mode, and effects remain separate from the media or drawing itself.
Content dispatch
ItemContent is the central item dispatcher. It sends each item type to its content renderer and passes down mute state, visibility, masks, nesting depth, and audio settings. Controllers and adjustment layers affect the scene without producing pixels of their own.
The main content roles are:
- Video items use the preview video path and can expose a separate audio path.
- Audio items use timeline trim and rate information for audio playback.
- Images and animated images use media presentation suited to their format.
- Lottie items use the animation player.
- Text and shapes render in composition space.
- Subtitle segments resolve one active cue and reuse text presentation.
- Composition items render another composition inside the current item.
References: src/…/item-content.tsx• L144-170 src/…/item-content.tsx• L214-227 src/…/item-content.tsx• L438-495 src/…/item-content.tsx• L497-610 src/…/item-content.tsx• L613-657
Visual item state
ItemVisualWrapper is the common visual boundary for rendered items. It resolves the item transform and related live state, then presents the content inside the item’s visual rectangle. Masks can apply to the full composition canvas, while media content can use crop and fit rules inside the item rectangle.
The wrapper keeps its visual structure stable while state changes. This supports live editor changes without making a media item or effect layer restart only because a mask or effect changed.
References: src/…/item-visual-wrapper.tsx• L254-265 src/…/item-visual-wrapper.tsx• L267-305 src/…/item-visual-wrapper.tsx• L797-835
Visual content types
Text and shape content use the current composition space and resolve animation at the item’s local frame before they paint. Text applies shared style rules to its text and span data, supports inline or stacked span flow, and merges live editor values. Shape content resolves animated geometry, paint, and live gizmo changes before it draws standard or custom paths with fills, strokes, and gradients.
Subtitle segments use a different data shape but keep the same visual contract. At the current local frame they select the active cue, parse its inline marks and alignment, and pass a temporary text item to TextContent.
References: src/…/text-content.tsx• L24-32 src/…/text-content.tsx• L34-59 src/…/text-content.tsx• L62-102 src/…/text-content.tsx• L156-227 src/…/shape-content.tsx• L18-22 src/…/shape-content.tsx• L23-65 src/…/shape-content.tsx• L66-83 src/…/subtitle-segment-content.tsx• L11-17 src/…/subtitle-segment-content.tsx• L26-79
Nested compositions
A composition item is a reusable child composition placed on a parent timeline. The runtime renders the child inline and maps the child’s local frames into the parent item’s source window, so trim and speed changes affect the child as one timed unit.
Composition nesting
CompositionContent finds the referenced child composition, resolves its media sources, applies instance control overrides, and maps child items into the parent window. The child renders at its own width, height, and frame rate inside its own providers. The result is then scaled to the parent item’s resolved dimensions.
Nested audio can render with the child composition alone when the parent item is used as an audio source. A visual-only mode prevents duplicate audio when a linked audio companion owns that sound.
References: src/…/composition-content.tsx• L81-115 src/…/composition-content.tsx• L117-169 src/…/composition-content.tsx• L172-182 src/…/composition-content.tsx• L197-257 src/…/composition-content.tsx• L539-635
Nesting safeguards
The runtime limits composition depth before it follows another composition reference. This prevents a bad or circular composition graph from causing unbounded rendering. A missing child composition produces a visible placeholder instead of silently changing the parent tree.
References: src/…/item-content.tsx• L51-62 src/…/item-content.tsx• L613-642 src/…/composition-content.tsx• L539-551
Runtime contexts and hooks
React contexts carry the state that many item types need while keeping the item API small. They define the coordinate space, animation lookup, media source mode, and live editor state for one composition subtree.
Composition space
CompositionSpaceProvider defines the logical project size and the render size for a subtree. It exposes horizontal, vertical, and uniform scale values so text, shapes, masks, and nested content can convert project coordinates to the current display size.
Nested compositions replace the parent space with the child’s native space while the child tree renders. This keeps child geometry meaningful even when the parent scales the child item.
References: src/…/composition-space-context.tsx• L4-12 src/…/composition-space-context.tsx• L16-29 src/…/composition-space-context.tsx• L31-64 src/…/composition-content.tsx• L554-585
Keyframe context
KeyframesProvider makes item keyframes and item lookup available to descendants. Renderers use this context to resolve animated properties at the local frame and to follow item relationships when one item’s transform or expression depends on another.
The provider also accepts the current canvas settings. This gives animation resolution the same project dimensions and frame rate as the composition that owns the item.
References: src/…/KeyframesProvider.tsx• L8-13 src/…/KeyframesProvider.tsx• L21-49 src/…/keyframes-context-core.ts• L6-17
Live transform context
LiveItemTransformProvider supplies a narrow live source for editor transform changes. Rendered items merge only the current transform onto their enriched runtime item, while transform dependency tracking wakes items whose parent, link, or expression can affect them.
This separation matters during editing. Position changes can update the visual boundary without rebuilding unrelated content or losing runtime-only fields such as resolved media data.
References: src/…/live-item-transform-provider.tsx• L7-18 src/…/live-item-transform-context.ts• L20-25 src/…/live-item-transform-context.ts• L158-185 src/…/live-item-transform-context.ts• L342-378
Player compatibility hooks
The composition runtime uses a small compatibility layer for player state. src/…/player-contract.ts defines the player-facing contract for sequence helpers, video configuration, clock state, and video source pooling. Runtime hooks use this boundary so composition code does not depend on player internals.
useVideoConfig reads the active player’s frame rate, dimensions, and duration. useCurrentFrame and useIsPlaying read the current clock state without making composition code depend on player internals. Sequence adds the local frame that an item needs. A nested sequence adds its start frame to its parent, so the same rule works for clips inside nested compositions.
References: src/…/player-contract.ts• L1-22 src/…/use-player-compat.ts• L1-32 src/…/Sequence.tsx• L64-105 src/…/sequence-context.ts• L3-14
Frame and media timing
The runtime keeps timeline timing in frames and converts to source time only at media boundaries. This allows the player, sequences, animation state, and editor controls to use one frame position even when a source has a different frame rate or playback speed.
Frame scheduling
Sequence mounts content for its active range and may mount it early for preloading. Premounted content stays hidden until its start frame. Active content receives the local frame, which is the basis for video seeking, subtitle cue selection, animated text, and animated shapes.
Stable video groups use an origin-based identity for split clips. The active clip can change at a split boundary without forcing the preview video lane to start over, while transition participants can be held and synchronized across an overlap.
References: src/…/Sequence.tsx• L69-95 src/…/Sequence.tsx• L126-157 src/…/stable-video-sequence.tsx• L77-83 src/…/stable-video-sequence.tsx• L308-337 src/…/stable-video-sequence.tsx• L440-468
Video timing
Video content maps the item’s local timeline frame to a source time using trim, source frame rate, playback speed, and reverse state. The preview uses pooled video elements and keeps their source position aligned with the clock during play, pause, scrub, and reverse shuttle movement.
If a browser cannot decode a source, the video path can fall back to live canvas decoding. Missing or invalid media produces an unavailable state instead of allowing an invalid source position to control the player.
References: src/…/item-content.tsx• L220-337 src/…/video-content.tsx• L251-264 src/…/video-content.tsx• L313-426 src/…/video-content.tsx• L1260-1297
Audio timing
Audio is assembled separately from visual layers so visual masks, effects, and video transitions do not cause audio playback to restart. The runtime creates continuous and overlap-aware segments, then selects a native or custom decoder based on the media source and its required processing.
Audio items carry the same frame window as their visual peers. Their playback path can apply trim, speed, reverse direction, fades, pitch, gain, and equalizer stages while following the composition clock.
References: src/…/main-composition.tsx• L261-357 src/…/main-composition.tsx• L467-679 src/…/item-content.tsx• L438-495 src/…/audio-playback-props.ts• L4-40
Audio worklet path
The SoundTouch preview worklet is the audio path for time and pitch changes that need processing outside the main render path. The host prepares channel data, loads one processor per audio context, and sends source, seek, tempo, pitch, and play-state messages to it.
The worklet emits processed stereo samples while it is playing. If the browser cannot create or load the worklet graph, the audio component requests its fallback path.
References: src/…/soundtouch-preview-worklet.ts• L8-19 src/…/soundtouch-preview-worklet.ts• L141-170 src/…/soundtouch-worklet-audio.tsx• L144-227 src/…/soundtouch-worklet-audio.tsx• L274-324 src/…/soundtouch-preview-processor.worklet.ts• L23-26 src/…/soundtouch-preview-processor.worklet.ts• L51-123
Player integration
The custom player owns transport state while the composition runtime owns scene presentation. The player supplies one clock and one video configuration to the composition tree, and the tree reports no separate notion of time back to the player.
Clock and transport
Clock is the timing source for the player. It stores the current frame, play state, playback rate, duration, and optional in/out range. It advances independently of React and emits frame and transport events to subscribers.
When an audio context is active, the clock can use the hardware audio time as its timing base. This keeps the visual frame position and processed audio on the same time source.
References: src/…/Clock.ts• L13-28 src/…/Clock.ts• L57-80 src/…/Clock.ts• L276-344 src/…/Clock.ts• L486-506
Player providers
Player creates the clock bridge and video configuration before it renders composition children. The player also scales the native composition canvas into its display rectangle, while the composition space tells items how to resolve project coordinates inside that canvas.
The player exposes imperative transport controls and event callbacks. Preview-specific coordination can choose between the DOM composition tree and the separate rendered preview path without changing the composition’s item model.
References: src/…/Player.tsx• L302-325 src/…/Player.tsx• L391-435 src/…/Player.tsx• L460-516 src/…/Player.tsx• L552-585
Preview and alternate render paths
The editor preview mounts MainComposition as the normal player content. The preview layer can also use a fast rendered overlay for effects, transitions, and other cases where a separate frame renderer is better suited than the DOM tree. During such periods the DOM tree can remain mounted for continuity while the overlay supplies the displayed frame.
Export is a separate path. The composition runtime provides the preview component model, while export uses its own canvas and WebCodecs renderer.
References: src/…/preview-stage.tsx• L1-20 src/…/video-preview.tsx• L515-549 src/…/video-preview.tsx• L672-766 src/…/use-player-compat.ts• L1-6
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to
TimelineItemtiming or nesting fields must remain compatible with source-frame mapping inCompositionContentand media timing inItemContent. - Changes to
Sequenceframe semantics must be checked against nested composition offsets, stable split-video identities, subtitle cue timing, and video synchronization. - Changes to composition or render dimensions must keep
CompositionSpaceProvider, nestedVideoConfigProvider, text and shape scaling, and player layout aligned. - Changes to text style resolution or shape animation must keep live preview values and the shared render-space contract aligned with the other visual render paths.
- Changes to the player dependency adapter must preserve the sequence, video configuration, clock, and source-pool contracts used by composition components.
- Changes to audio segment ownership must preserve the rule that linked video audio is not played again by the visual video element.
- Changes to the player clock or clock bridge must preserve both frame subscriptions and the audio-clock handoff used by preview audio.
- Changes to the preview overlay route must keep the DOM composition mounted and must use the same source or proxy media choice as the composition tree.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A composition references a missing child | The item cannot render its child scene | CompositionContent shows a composition-not-found state |
| A composition graph is circular or too deep | Recursive rendering can grow without bound | ItemContent stops nested rendering at the maximum depth |
| A video has invalid source timing | The browser can seek outside the usable source range | Shared timing checks reject invalid positions and clamp safe trim values |
| A browser cannot decode the video source | The native video lane reports an error or shows no frame | VideoContent switches to live canvas decoding when supported, then shows media unavailable |
| A worklet cannot load or start | Pitch or time-stretched audio cannot use its preferred path | SoundTouchWorkletAudio requests its configured fallback |
| A linked video and audio companion both remain audible | The same source audio can be heard twice | Render planning and item routing assign ownership to one audio path |
Key files
| File | Why to read it |
|---|---|
| src/…/main-composition.tsx | Root scene assembly for tracks, visual layers, audio segments, masks, and runtime providers |
| src/…/item-content.tsx | Central mapping from timeline item types to runtime content |
| src/…/composition-content.tsx | Nested composition timing, source resolution, and child-space rendering |
| src/…/item-visual-wrapper.tsx | Shared transform, mask, crop, blend, and effect boundary for visual items |
| src/…/text-content.tsx | Text style, span layout, animation, composition scaling, and live preview contract |
| src/…/shape-content.tsx | Shape animation, paint, geometry, and live gizmo preview contract |
| src/…/stable-video-sequence.tsx | Stable video identity and transition-aware video presentation |
| src/…/composition-space-context.tsx | Project-to-render coordinate contract |
| src/…/KeyframesProvider.tsx | Keyframe and item lookup contract for animated content |
| src/…/live-item-transform-context.ts | Live editor transform updates and dependency tracking |
| src/…/player-contract.ts | Boundary between composition components and player hooks, providers, and media pools |
| src/…/Clock.ts | Central frame and transport timing model |
| src/…/Sequence.tsx | Timed mounting and local frame contract |
| src/…/Player.tsx | Player provider boundary and composition display integration |
| src/…/soundtouch-worklet-audio.tsx | Preview audio processing boundary and fallback behavior |
docs/playback-engine.md | Detailed playback transport and media synchronization subsystem |
docs/gpu-rendering-pipeline.md | Detailed alternate GPU frame rendering and overlay subsystem |