Introducing a new way to review your PRs.

Read the announcement

walterlow / freecut

FreeCut is a professional-grade video editor that runs entirely in your browser. Professional video editing, zero installation. Create stunning videos with multi-track editing, keyframe animations, real-time preview, and high-quality exports.

View on GitHub

19 documents · Updated Oct 30, 2026

docs / playback-engine

Playback engine

The playback engine turns a composition timeline into a time-aware React view. It keeps one frame clock for play, pause, seek, rate, loop, and end behavior, then uses that clock to show the right composition layers and keep video media at the matching source time. The engine also provides events, layout, and control interfaces for preview surfaces and other callers. See Composition runtime for the composition model and GPU rendering pipeline for the downstream visual render path.

Playback clock

The playback clock is the single source of truth for the playhead. It stores the current frame and converts it to time with the configured frames per second. While playing, it advances from an animation loop; when available, a running shared audio clock supplies the time base so audio and video use the same source of time. The clock also catches up after the page becomes visible or gains focus. (src/…/Clock.ts• L13-28, src/…/main-composition.tsx• L221-231)

The clock keeps the playhead within the active range. An in point and an out point define that range when set; otherwise the range is the full composition. A forward play at the last active frame starts at the first active frame. Reverse play uses the opposite boundary. Looping returns to the matching boundary, while non-looping playback stops at the edge and emits an end event. (src/…/Clock.ts• L169-186, src/…/Clock.ts• L251-289, src/…/Clock.ts• L548-568)

Playback controls operate in frames so the editor can make exact timeline changes. Seek and step operations clamp to the active range. A rate change keeps the current frame continuous by re-anchoring the clock, and negative rates support reverse shuttle behavior. (src/…/Clock.ts• L229-245, src/…/Clock.ts• L317-381)

Composition timing

The composition runtime reads the clock through React subscriptions. useClockFrame updates consumers when the frame changes or a seek occurs, while selectors let a component stay still when its relevant frame range has not changed. Play state and playback rate use the same event-driven model. (src/…/clock-hooks.ts• L18-36, src/…/clock-hooks.ts• L40-65, src/…/clock-hooks.ts• L68-107)

The player bridge exposes the clock through a Composition-like interface. It supplies the current frame, play state, rate, active bounds, and setters to runtime components. The bridge entry point re-exports these providers and hooks, while compatibility hooks make the values available as useCurrentFrame, useIsPlaying, and useVideoConfig. Composition content can therefore use one timing contract without owning the clock. (src/…/ClockBridgeProvider.tsx• L41-130, src/…/ClockBridge.ts• L1-9, src/…/use-player-compat.ts• L14-32)

The following flow shows how a control action reaches the clock and then reaches composition consumers.

flowchart LR
  A[Control or imperative call] --> B[Player methods]
  B --> C[Clock]
  C --> D[Frame and play events]
  D --> E[React clock subscriptions]
  E --> F[Composition sequences and media]

Composition layers

The composition view maps tracks and timeline items into a stable set of visual and audio layers. The main composition keeps the background, audio scene, video groups, and non-media layers coordinated under the same frame clock. Track order controls stacking, while track visibility and mute state pass through to the rendered content. (src/…/main-composition.tsx• L251-319, src/…/main-composition.tsx• L681-748)

Sequence is the time window used by composition items. It converts a sequence's local from value into an absolute frame, mounts content only in its active or premount range, and gives children a local frame. Premounting lets media prepare before it becomes visible; hidden content remains in the React tree when needed so media elements do not remount at each boundary. (src/…/Sequence.tsx• L22-51, src/…/Sequence.tsx• L64-105, src/…/Sequence.tsx• L109-157)

Nested compositions reuse this model with a new local video configuration and composition space. Their source window is mapped into the parent timeline, and nested items are rendered by their own track order and time ranges. A render-depth guard prevents a circular composition graph from recursing without limit. (src/…/composition-content.tsx• L117-181, src/…/composition-content.tsx• L231-249, src/…/composition-content.tsx• L554-635, src/…/item-content.tsx• L51-74)

Video items use stable groups when split clips share an origin. The group keeps one sequence and one pool identity across split boundaries, then selects the active clip from the current frame. Transition participants can remain mounted as hidden shadows so the visual transition can use both clips without a cold media start. (src/…/stable-video-sequence.tsx• L1-11, src/…/stable-video-sequence.tsx• L183-237, src/…/stable-video-sequence.tsx• L308-345, src/…/stable-video-sequence.tsx• L440-468)

Video coordination

Video coordination maps a local sequence frame to a source time. The mapping accounts for the timeline rate, source rate, trim start, per-item offset, and reverse playback. It snaps values near source-frame boundaries so a floating-point error does not select the preceding source frame. (src/…/video-timing.ts• L11-45)

Each native preview video follows the clock rather than its own independent timeline. It seeks before paint when a paused frame or segment changes, plays when its sequence is active, pauses during premount, and corrects drift during playback. When the browser supports presented-frame callbacks, small drift is corrected by a small rate change and large drift is reserved for real clock jumps. (src/…/video-content.tsx• L648-731, src/…/video-content.tsx• L733-962, src/…/video-content.tsx• L974-1075)

The video path also protects continuity at clip and transition boundaries. Pooled elements can move between stable lanes, hidden transition participants can warm a decoder, and reverse playback uses coalesced seeks because native video playback rates do not provide the needed reverse behavior. Browser-undecodable media can use a live canvas decode path instead of a native video element. (src/…/stable-video-sequence.tsx• L242-421, src/…/video-content.tsx• L785-837, src/…/video-content.tsx• L1260-1297)

Audio follows the same source-time rules as video. The main composition builds standalone, linked, and transition audio segments outside the visual layer so visual changes do not restart audio. The composition item path also separates video pixels from external video audio when the clip needs custom decoding, pitch correction, or transition mixing. (src/…/video-timing.ts• L47-71, src/…/main-composition.tsx• L261-356, src/…/item-content.tsx• L340-399)

Player events

The player event interface gives external callers a stable view of playback state changes. A typed emitter accepts listeners by event name and passes event data in a detail field. Listener errors are isolated and logged so one consumer cannot stop other listeners from receiving an event. (src/…/player-emitter.ts• L5-35, src/…/player-emitter.ts• L38-117)

The supported event contract is:

EventMeaningPayload
seekedThe playhead moved through a player seekframe
timeupdatePeriodic playback position updateframe
play / pausePlayback state changednone
endedPlayback reached its active end without loopingnone
ratechangePlayback rate changedplaybackRate
volumechangeVolume or mute state changedvolume, isMuted
fullscreenchangeFullscreen state changedisFullscreen
errorA player-level error occurrederror
waiting / resumePlayback wait or recovery signalnone

The clock and player emitter serve different audiences. Clock events drive internal React subscriptions and media synchronization, while the player emitter exposes the public player event names. The player maps seek, play, pause, and rate actions to that public emitter, and the clock supplies frame changes to the React tree. (src/…/use-player.ts• L86-103, src/…/use-player.ts• L105-198, src/…/player-emitter-context.ts• L4-11)

Player layout

The player lays out the composition as a centered native-size surface inside its container. It uses one uniform scale that fits both container dimensions, which preserves the composition aspect ratio and leaves any unused area around the content. Invalid or empty dimensions use a neutral scale of one. (src/…/player-layout.ts• L1-20)

The layout is measured again when the container changes size. The player applies the calculated surface size and scale without rebuilding the composition tree for every resize step. The same player can use measured container dimensions or an explicit layout rectangle. (src/…/Player.tsx• L339-389, src/…/Player.tsx• L482-517)

AbsoluteFill supplies the common full-parent layer geometry used by the player and composition runtime. It gives visual layers a shared positioning frame, while each composition can add its own transform, stacking, visibility, and media fit rules. (src/…/AbsoluteFill.tsx• L19-49, src/…/composition-content.tsx• L554-635)

React interfaces

The Player component is the main React boundary for playback. It accepts composition content plus timing and display options, creates the emitter and clock providers, and provides video configuration to all descendants. HeadlessPlayer uses the same boundary without the built-in control overlay. (src/…/Player.tsx• L31-83, src/…/Player.tsx• L551-585)

The public ref is the imperative control surface for integrations. It exposes play, pause, toggle, frame seek, state queries, rate control, and event listener registration. The usePlayer hook supplies the same core actions, clamps seeks and frame steps to the active bounds, and tracks whether playback has started. It keeps the current frame in a ref so controls can act without subscribing the player shell to every clock tick. (src/…/Player.tsx• L85-96, src/…/Player.tsx• L392-480, src/…/use-player.ts• L60-92, src/…/use-player.ts• L97-198)

React descendants use context according to the kind of information they need:

InterfaceRole
useClock and clock hooksRead the live clock and subscribe to frame, play state, or rate changes.
Compatibility hooksRead current frame, playing state, and video configuration through the composition-facing API.
useSequenceContextRead a sequence's absolute start and local frame.
useVideoConfigRead frames per second, dimensions, duration, and player identity.
usePlayerEmitterRegister internal or integration-facing player event listeners within a player boundary.

The bridge separates frame subscriptions from transport state. Components that only need play state or rate do not re-render for each frame tick, while controls that show the current frame subscribe directly to the clock. These contexts require the matching player or provider boundary; a missing boundary is treated as a usage error. (src/…/clock-bridge-context.ts• L34-61, src/…/clock-hooks.ts• L18-36, src/…/video-config.ts• L11-17, src/…/player-emitter-context.ts• L6-11, src/…/sequence-context.ts• L10-14)

Change checklist

When these files or sections are changed, remember to consider these:

  • When changing frame bounds or rate rules in Clock, check the bridge setters and the public usePlayer seek and replay behavior together.
  • When changing Sequence local-frame or premount rules, check source-time mapping and video visibility at split and nested-composition boundaries.
  • When changing player event names or payloads, update the emitter map and the PlayerRef listener contract as one public interface.
  • When changing composition dimensions or scale rules, check the root player layout and nested composition space so transforms do not use mixed coordinate systems.
  • When changing video boundary sync, check native video playback, reverse playback, transition shadows, and the live decode path as separate media cases.

Important failure modes

TriggerConsequenceGuard
A seek or active range exceeds its valid frame boundsThe player can show a frame outside the composition or source windowClock and bridge setters clamp to the active first and last frames.
A source frame rate differs from the timeline rate and source time uses the wrong rateVideo and audio seek to different contentShared timing helpers convert trim and local offsets with the source FPS.
A split video changes identity at a boundaryThe browser releases and rebuilds the media pipeline, causing a stall or flashStable origin groups and pooled video identities keep the lane continuous.
A transition participant is not ready when it becomes visibleThe transition can show a black or stale framePremount, shadow, and decoder warm-up paths prepare the entering media.
A browser cannot decode a video sourceNative video reports an error and the item becomes unavailableThe player tries live canvas decoding before showing the unavailable state.
A composition refers to itself through nested itemsRendering can recurse without endItem rendering stops after the maximum render depth.
A consumer uses a clock, sequence, config, or emitter hook outside its providerThe runtime cannot supply valid playback contextContext hooks fail fast with an explicit boundary error.

Key files

FileWhy to read it
src/…/Player.tsxMain React boundary, built-in controls, layout host, and imperative player ref.
src/…/Clock.tsCore frame clock, active range, rate, loop, end, and time-source behavior.
src/…/ClockBridgeProvider.tsxBridge between the clock and the composition-facing React timing API.
src/…/ClockBridge.tsPublic bridge exports for the clock provider and composition-facing playback hooks.
src/…/use-player.tsPlayer actions, frame-bound handling, and public event dispatch for play, pause, seek, and rate changes.
src/…/player-emitter.tsTyped event names and payloads for external playback integration.
src/…/player-layout.tsAspect-preserving content layout calculation.
src/…/Sequence.tsxTimeline visibility, local frame context, and premount behavior.
src/…/main-composition.tsxAssembly of the clock-driven audio and visual composition layers.
src/…/stable-video-sequence.tsxStable video grouping, split continuity, and transition preparation.
src/…/video-content.tsxClock-to-video source-time coordination and native/live decode choices.
src/…/video-timing.tsShared frame-to-source-time mapping for video and audio.
src/…/video-sync-plan.tsRules for paused seeks, premounts, drift correction, and reverse seeks.
src/…/use-player-compat.tsComposition-facing React hooks for frame, play state, and configuration.