Skip to content

Open source CapCut alternative.

TypeScript

fix(site): a held frame never crosses a clip's split

David Le9cd328bf02f1
diff --git a/site/src/cut/hooks/usePlayback.ts b/site/src/cut/hooks/usePlayback.ts
index 888b997a..52b0483a 100644
--- a/site/src/cut/hooks/usePlayback.ts
+++ b/site/src/cut/hooks/usePlayback.ts
@@ -150,7 +150,7 @@ class Engine {
const asset = clip && s.assets.find((a) => a.id === clip.assetId);
if (!clip || !asset) return null;
const src = this.pool.get(keyOf(clip, asset), asset, this.decodeHeight());
- return src.frameAt(sourceTimeOf(clip, previewAt()))?.image ?? null;
+ return src.frameAt(sourceTimeOf(clip, previewAt()), clip.in, clip.out)?.image ?? null;
}
/**
@@ -170,8 +170,10 @@ class Engine {
// A failed source that already decoded frames keeps showing the nearest
// one it holds — a transient blip (a network drop, a signed URL mid
// re-mint) reads as a held frame, and only a source with nothing at all
- // to show goes missing.
- const frame = src.frameAt(st);
+ // to show goes missing. The clip's own span bounds the answer: two clips
+ // split from one file share a source, and a held frame from across the
+ // split would show the other scene at a paused playhead.
+ const frame = src.frameAt(st, span.clip.in, span.clip.out);
if (frame)
return { kind: "ready", image: frame.image, width: frame.width, height: frame.height };
return src.failed ? MISSING_FRAME : PENDING_FRAME;
@@ -315,7 +317,7 @@ class Engine {
const src = this.pool.get(keyOf(b.span.clip, b.span.asset), b.span.asset, this.decodeHeight());
this.used.add(src);
src.want(b.at, false);
- const f = src.frameAt(b.at);
+ const f = src.frameAt(b.at, b.span.clip.in, b.span.clip.out);
this.comp.drawLayer(
f ? { kind: "ready", image: f.image, width: f.width, height: f.height } : PENDING_FRAME,
b.span.clip,
@@ -492,7 +494,7 @@ class Engine {
this.decodeHeight()
);
const st = sourceTimeOf(master.clip, t);
- srcTs = src.frameAt(st)?.timestamp ?? null;
+ srcTs = src.frameAt(st, master.clip.in, master.clip.out)?.timestamp ?? null;
exact = src.hasExact(st);
}
markPresent({
diff --git a/site/src/cut/lib/frameSource.test.ts b/site/src/cut/lib/frameSource.test.ts
index b8074fd9..b3de9301 100644
--- a/site/src/cut/lib/frameSource.test.ts
+++ b/site/src/cut/lib/frameSource.test.ts
@@ -66,13 +66,34 @@ describe("FrameRing", () => {
expect(ring.at(0)?.timestamp).toBeCloseTo(3);
});
- test("keep() drops what a reader moved away from", () => {
+ test("a window keeps the answer on the asking clip's side of a split", () => {
+ // Two clips split from one file share a ring. The clip past the split asks
+ // inside its own span; a garden frame from before the split is nearer but
+ // shows the other scene.
const ring = new FrameRing<Timed>(16);
- for (let i = 0; i < 10; i++) ring.push(f(i));
- ring.keep(4, 6);
- expect(ring.size).toBe(3);
- expect(ring.oldest).toBeCloseTo(4);
- expect(ring.newest).toBeCloseTo(6);
+ ring.push(f(45.4));
+ ring.push(f(45.5));
+ ring.push(f(46.0));
+ // Ask at 45.68 in a clip whose span starts at the 45.57 split: the frames
+ // before the split are out, and the earliest in-window frame answers.
+ expect(ring.at(45.68, 45.57, 60)?.timestamp).toBeCloseTo(46.0);
+ // The clip before the split never shows the far side.
+ expect(ring.at(45.55, 40, 45.57)?.timestamp).toBeCloseTo(45.5);
+ expect(ring.at(46.2, 40, 45.57)?.timestamp).toBeCloseTo(45.5);
+ });
+
+ test("a frame spanning the window's edge still answers inside it", () => {
+ // A split lands mid-frame: the frame covering the second clip's `in`
+ // starts just before it, and it is that clip's real first picture.
+ const ring = new FrameRing<Timed>(16);
+ ring.push(f(45.55));
+ expect(ring.at(45.58, 45.57, 60)?.timestamp).toBeCloseTo(45.55);
+ });
+
+ test("nothing on the asking side answers null", () => {
+ const ring = new FrameRing<Timed>(16);
+ ring.push(f(45.4));
+ expect(ring.at(46.1, 45.57, 60)).toBe(null);
});
});
diff --git a/site/src/cut/lib/frameSource.ts b/site/src/cut/lib/frameSource.ts
index 4a175e0e..8641ab68 100644
--- a/site/src/cut/lib/frameSource.ts
+++ b/site/src/cut/lib/frameSource.ts
@@ -144,11 +144,21 @@ export class FrameRing<T extends Timed> {
* The frame covering `t`: the last one starting at or before it. Before the
* first frame held, the earliest — at a clip's head that is the frame the cut
* opens on, and showing it beats showing nothing.
+ *
+ * `from`/`to` bound the answer to frames whose span overlaps that window.
+ * Two clips split from one file share a ring, and near the split the nearest
+ * frame is often from the other clip's side of the cut — a paused playhead
+ * just past a scene change would show the old scene and call it held. A clip
+ * asking within its own source span can only ever be answered from it; with
+ * nothing held there yet, no answer is the honest one, and the reader keeps
+ * its last painted picture until the right frame lands.
*/
- at(t: number): T | null {
+ at(t: number, from = -Infinity, to = Infinity): T | null {
let best: T | null = null;
let earliest: T | null = null;
for (const i of this.items) {
+ if (i.timestamp > to + SAME) continue;
+ if (i.timestamp + Math.max(i.duration, SAME) < from - SAME) continue;
if (!earliest || i.timestamp < earliest.timestamp) earliest = i;
if (i.timestamp <= t + SAME && (!best || i.timestamp > best.timestamp)) best = i;
}
@@ -183,10 +193,6 @@ export class FrameRing<T extends Timed> {
return n;
}
- /** Drop frames outside a window a reader has moved away from. */
- keep(from: number, to: number): void {
- this.items = this.items.filter((i) => i.timestamp >= from - SAME && i.timestamp <= to + SAME);
- }
}
/**
@@ -272,11 +278,12 @@ export class ClipFrameSource {
*
* Exact when the ring holds the frame covering `t`; otherwise the nearest
* frame before it, which is what a decoder running slightly behind should
- * show. Null only when nothing has been decoded yet.
+ * show. `from`/`to` bound the answer to the asking clip's source span — see
+ * `FrameRing.at`. Null when nothing decoded yet answers inside it.
*/
- frameAt(t: number): SourceFrame | null {
+ frameAt(t: number, from = -Infinity, to = Infinity): SourceFrame | null {
if (this.still) return this.still;
- const c = this.ring.at(t);
+ const c = this.ring.at(t, from, to);
return c ? frameOfCanvas(c) : null;
}
@@ -330,12 +337,6 @@ export class ClipFrameSource {
}
}
- /** Frames outside [from, to] are of no further use — a clip whose window
- * moved on, or a scrub that jumped. */
- trim(from: number, to: number): void {
- this.ring.keep(from, to);
- }
-
close(): void {
this.closed = true;
this.stopStream();