Files
hyperframes/packages/player/src/play-range.ts
T
Miguel Ángel 840f657028 feat(player): play and loop a range of a film with range-start and range-end (#4688)
* feat(player): play and loop a range of the film with range-start and range-end

The runtime takes a set-play-range control and stops on the range end frame itself.
The player wraps to range-start with loop, fires ended without it, and reports a
range cut to the film with rangeclamped. Video and __timelines players check it per tick.

* fix(player): hold a range on its last frame and let the runtime own range jumps

A runtime with the play-range capability jumps to range-start itself when a new range leaves
the playhead outside, so a stale state no longer ends the film; older runtimes keep the
player fallback, guarded until a state lands inside. Every range stop shows the last frame
before range-end; a paused playhead re-parks only when it falls outside the new range.

* fix(player): stop a range a check early and enforce its end on a video in a background tab

* test(player): share the range video setup

* fix(player): a slow frame no longer cuts a range short; a video without a range is unchanged

* fix(player): a range still stops early where video time moves once per frame

* refactor(player): cut the range look-ahead comment to one line

* fix(player): a per-frame video time rounded down still stops a range early

* test(player): pin no rounding tolerance at a film's own end

* test(studio): expect the play-range capability on control messages
2026-09-28 23:38:32 +00:00

39 lines
1.4 KiB
TypeScript

// Range playback: the `range-start` / `range-end` attributes, in film seconds.
import { playRangeHoldTime } from "@hyperframes/core/runtime/protocol";
export interface PlayRange {
start: number;
end: number | null;
}
/** The range the player applies for the attributes, and whether it differs from the one asked for:
* an end past the film is cut to the film's end, and an empty or negative range applies no range. */
export function resolvePlayRange(
start: number | null,
end: number | null,
duration: number,
): { range: PlayRange | null; clamped: boolean } {
if (start === null && end === null) return { range: null, clamped: false };
const from = start ?? 0;
const cut = end !== null && duration > 0 && end > duration;
const to = cut ? duration : end;
const limit = to ?? (duration > 0 ? duration : Infinity);
if (!(from >= 0) || !(limit > from)) return { range: null, clamped: true };
return { range: { start: from, end: to }, clamped: cut };
}
export function playRangeStopTime(range: PlayRange, duration: number, fps: number): number {
const end = range.end ?? duration;
return end < duration ? playRangeHoldTime(range.start, end, fps) : end;
}
export function isOutsidePlayRange(
time: number,
range: PlayRange,
duration: number,
fps: number,
): boolean {
return time < range.start || time >= playRangeStopTime(range, duration, fps);
}