Skip to content

Open source CapCut alternative.

TypeScript

perf(site): draw light bars in one row and warm once [rebuild]

David Le590921e18c92
diff --git a/evals/cut-perf.latest-report.json b/evals/cut-perf.latest-report.json
index e9be48c9..35559705 100644
--- a/evals/cut-perf.latest-report.json
+++ b/evals/cut-perf.latest-report.json
@@ -51,10 +51,10 @@
},
"frameMs": 16.7,
"scrub": {
- "p50": 15.8,
+ "p50": 15.7,
"p95": 32.5,
- "mean": 18.42,
- "max": 32.7,
+ "mean": 18.48,
+ "max": 33.5,
"n": 41
},
"longTasks": {
@@ -64,7 +64,7 @@
"max": 0,
"n": 0
},
- "liveSamples": 36,
+ "liveSamples": 57,
"machine": "desktop"
},
{
@@ -75,16 +75,16 @@
"late": {
"p50": 0,
"p95": 0,
- "mean": 0.05,
- "max": 1,
+ "mean": 0,
+ "max": 0,
"n": 41
},
"frameMs": 16.7,
"scrub": {
- "p50": 15.7,
- "p95": 16.6,
- "mean": 16.28,
- "max": 31.9,
+ "p50": 15.3,
+ "p95": 17.4,
+ "mean": 15.43,
+ "max": 17.6,
"n": 41
},
"longTasks": {
@@ -94,7 +94,7 @@
"max": 0,
"n": 0
},
- "liveSamples": 56,
+ "liveSamples": 65,
"machine": "desktop"
},
{
@@ -112,9 +112,9 @@
"frameMs": 16.7,
"scrub": {
"p50": 15.5,
- "p95": 16,
- "mean": 14.83,
- "max": 32,
+ "p95": 17.5,
+ "mean": 15.89,
+ "max": 32.1,
"n": 41
},
"longTasks": {
@@ -124,7 +124,7 @@
"max": 0,
"n": 0
},
- "liveSamples": 76,
+ "liveSamples": 77,
"machine": "desktop"
},
{
@@ -135,16 +135,16 @@
"late": {
"p50": 0,
"p95": 0,
- "mean": 0.04,
- "max": 2,
+ "mean": 0.01,
+ "max": 1,
"n": 81
},
"frameMs": 16.7,
"scrub": {
- "p50": 15.3,
- "p95": 16,
- "mean": 15.31,
- "max": 51.8,
+ "p50": 15.4,
+ "p95": 17.2,
+ "mean": 15.1,
+ "max": 33,
"n": 81
},
"longTasks": {
@@ -165,16 +165,16 @@
"late": {
"p50": 0,
"p95": 1,
- "mean": 0.17,
+ "mean": 0.2,
"max": 2,
"n": 41
},
"frameMs": 16.7,
"scrub": {
"p50": 15.7,
- "p95": 32.3,
- "mean": 18.42,
- "max": 49.2,
+ "p95": 32.8,
+ "mean": 18.84,
+ "max": 48.6,
"n": 41
},
"longTasks": {
@@ -184,7 +184,7 @@
"max": 0,
"n": 0
},
- "liveSamples": 58,
+ "liveSamples": 63,
"machine": "desktop"
},
{
@@ -195,16 +195,16 @@
"late": {
"p50": 0,
"p95": 1,
- "mean": 0.2,
- "max": 2,
+ "mean": 0.15,
+ "max": 1,
"n": 41
},
"frameMs": 16.7,
"scrub": {
"p50": 15.5,
- "p95": 32.4,
- "mean": 18.3,
- "max": 48.3,
+ "p95": 32,
+ "mean": 17.9,
+ "max": 33.3,
"n": 41
},
"longTasks": {
@@ -214,7 +214,7 @@
"max": 0,
"n": 0
},
- "liveSamples": 63,
+ "liveSamples": 65,
"machine": "desktop"
},
{
@@ -225,16 +225,16 @@
"late": {
"p50": 1,
"p95": 1,
- "mean": 0.95,
- "max": 2,
+ "mean": 0.88,
+ "max": 1,
"n": 41
},
"frameMs": 16.7,
"scrub": {
"p50": 32.1,
- "p95": 33.2,
- "mean": 31.18,
- "max": 64.6,
+ "p95": 33.1,
+ "mean": 30.01,
+ "max": 33.2,
"n": 41
},
"longTasks": {
@@ -289,13 +289,13 @@
"pass": true,
"notes": [],
"drops": 0,
- "presented": 810,
+ "presented": 811,
"lag": {
"p50": 0.02,
"p95": 0.03,
"mean": 0.02,
"max": 0.03,
- "n": 810
+ "n": 811
},
"stallShare": 0,
"decay": {
@@ -303,8 +303,8 @@
"last": 0
},
"clock": {
- "lead": 0.22,
- "reported": 0.22
+ "lead": 0.04,
+ "reported": 0.04
},
"longTasks": {
"p50": 0,
@@ -317,7 +317,7 @@
"liveSources": 12,
"keptSources": 20,
"warmMb": 354,
- "memMb": 1111,
+ "memMb": 1110,
"machine": "desktop"
},
{
@@ -326,13 +326,13 @@
"pass": true,
"notes": [],
"drops": 0,
- "presented": 3236,
+ "presented": 3237,
"lag": {
"p50": 0.02,
"p95": 0.03,
"mean": 0.02,
"max": 0.03,
- "n": 3236
+ "n": 3237
},
"stallShare": 0,
"decay": {
@@ -340,8 +340,8 @@
"last": 0
},
"clock": {
- "lead": 0.22,
- "reported": 0.22
+ "lead": 0.04,
+ "reported": 0.04
},
"longTasks": {
"p50": 0,
@@ -354,7 +354,7 @@
"liveSources": 12,
"keptSources": 20,
"warmMb": 354,
- "memMb": 1111,
+ "memMb": 1110,
"machine": "desktop"
},
{
@@ -377,8 +377,8 @@
"last": 0
},
"clock": {
- "lead": 0.22,
- "reported": 0.22
+ "lead": 0.04,
+ "reported": 0.04
},
"longTasks": {
"p50": 0,
@@ -414,8 +414,8 @@
"last": 0
},
"clock": {
- "lead": 0.22,
- "reported": 0.22
+ "lead": 0.04,
+ "reported": 0.04
},
"longTasks": {
"p50": 0,
@@ -436,30 +436,30 @@
"bucket": "playback",
"pass": true,
"notes": [],
- "drops": 0,
- "presented": 3444,
+ "drops": 5,
+ "presented": 3404,
"lag": {
"p50": 0.01,
"p95": 0.02,
"mean": 0.01,
- "max": 0.02,
- "n": 3444
+ "max": 0.22,
+ "n": 3404
},
"stallShare": 0,
"decay": {
"first": 0,
- "last": 0
+ "last": 0.004
},
"clock": {
- "lead": 0.22,
- "reported": 0.22
+ "lead": 0.04,
+ "reported": 0.04
},
"longTasks": {
- "p50": 0,
- "p95": 0,
- "mean": 0,
- "max": 0,
- "n": 0
+ "p50": 61,
+ "p95": 354,
+ "mean": 127.4,
+ "max": 354,
+ "n": 5
},
"liveSamples": 10,
"liveSources": 1,
@@ -474,13 +474,13 @@
"pass": true,
"notes": [],
"drops": 0,
- "presented": 3444,
+ "presented": 3441,
"lag": {
"p50": 0.02,
"p95": 0.03,
- "mean": 0.01,
+ "mean": 0.02,
"max": 0.03,
- "n": 3444
+ "n": 3441
},
"stallShare": 0,
"decay": {
@@ -488,14 +488,14 @@
"last": 0
},
"clock": {
- "lead": 0.22,
- "reported": 0.22
+ "lead": 0.04,
+ "reported": 0.04
},
"longTasks": {
- "p50": 66,
- "p95": 66,
- "mean": 66,
- "max": 66,
+ "p50": 79,
+ "p95": 79,
+ "mean": 79,
+ "max": 79,
"n": 1
},
"liveSamples": 10,
@@ -513,9 +513,9 @@
"drops": 0,
"presented": 3444,
"lag": {
- "p50": 0.02,
+ "p50": 0.01,
"p95": 0.03,
- "mean": 0.02,
+ "mean": 0.01,
"max": 0.03,
"n": 3444
},
@@ -525,15 +525,15 @@
"last": 0
},
"clock": {
- "lead": 0.22,
- "reported": 0.22
+ "lead": 0.04,
+ "reported": 0.04
},
"longTasks": {
- "p50": 64,
- "p95": 64,
- "mean": 62,
- "max": 64,
- "n": 2
+ "p50": 54,
+ "p95": 54,
+ "mean": 54,
+ "max": 54,
+ "n": 1
},
"liveSamples": 10,
"liveSources": 1,
@@ -553,7 +553,7 @@
"p50": 0.01,
"p95": 0.01,
"mean": 0.01,
- "max": 0.01,
+ "max": 0.02,
"n": 3444
},
"stallShare": 0,
@@ -562,8 +562,8 @@
"last": 0
},
"clock": {
- "lead": 0.22,
- "reported": 0.22
+ "lead": 0.04,
+ "reported": 0.04
},
"longTasks": {
"p50": 0,
@@ -599,8 +599,8 @@
"last": 0
},
"clock": {
- "lead": 0.22,
- "reported": 0.22
+ "lead": 0.04,
+ "reported": 0.04
},
"longTasks": {
"p50": 0,
@@ -622,13 +622,13 @@
"pass": true,
"notes": [],
"drops": 0,
- "presented": 1618,
+ "presented": 1621,
"lag": {
"p50": 0.02,
"p95": 0.03,
"mean": 0.02,
"max": 0.03,
- "n": 1618
+ "n": 1621
},
"stallShare": 0,
"decay": {
@@ -636,8 +636,8 @@
"last": 0
},
"clock": {
- "lead": 0.22,
- "reported": 0.22
+ "lead": 0.04,
+ "reported": 0.04
},
"longTasks": {
"p50": 0,
@@ -650,7 +650,7 @@
"liveSources": 12,
"keptSources": 20,
"warmMb": 354,
- "memMb": 1112,
+ "memMb": 1111,
"machine": "desktop"
},
{
@@ -692,13 +692,13 @@
"pass": true,
"notes": [],
"drops": 3,
- "presented": 1094,
+ "presented": 1095,
"lag": {
"p50": 0.02,
"p95": 0.03,
- "mean": 0.02,
+ "mean": 0.01,
"max": 0.03,
- "n": 1091
+ "n": 1092
},
"stallShare": 0,
"decay": {
@@ -706,21 +706,21 @@
"last": 0
},
"clock": {
- "lead": 0.22,
- "reported": 0.22
+ "lead": 0.04,
+ "reported": 0.04
},
"longTasks": {
- "p50": 177,
- "p95": 177,
- "mean": 119.5,
- "max": 177,
+ "p50": 151,
+ "p95": 151,
+ "mean": 109,
+ "max": 151,
"n": 2
},
"liveSamples": 10,
"liveSources": 1,
"keptSources": 1,
"warmMb": 0,
- "memMb": 218,
+ "memMb": 209,
"machine": "desktop"
}
]
diff --git a/site/packages/effects-kit/src/effects.ts b/site/packages/effects-kit/src/effects.ts
index fdf85b64..679855e5 100644
--- a/site/packages/effects-kit/src/effects.ts
+++ b/site/packages/effects-kit/src/effects.ts
@@ -727,34 +727,21 @@ export function lightbarsAt(tLocal: number, dur: number, k: number): LightBar[]
const u = clip01(tLocal / Math.max(dur, 1e-3));
const appear = Math.min(1, u / LB_APPEAR);
const gain = 0.4 + 0.6 * k;
- return RIBBON.flatMap((l) => {
+ // One array a frame, with only the bars that show in it.
+ const bars: LightBar[] = [];
+ for (const l of RIBBON) {
const left = l.T - u;
- if (left <= 1e-3) return [];
+ if (left <= 1e-3) continue;
const s = l.T / left;
const x = LB_AXIS + l.th * s;
const w = l.w * s;
const on = l.on ? clip01((u - l.on) / LB_ON_SPAN) : 1;
const off = l.off ? clip01((l.off - u) / LB_OFF_SPAN) : 1;
const alpha = l.a * gain * appear * on * off * Math.min(1, left / LB_NEAR);
- if (alpha <= 0.002 || x + LB_HALO * w < 0 || x - LB_HALO * w > 1) return [];
- return [{ x, w, rgb: l.rgb, alpha }];
- });
-}
-
-/** One bar as a CSS box — the DOM twin of the canvas pass's horizontal
- * gradient: a full-height layer only as wide as the bar's glow (cut to the
- * frame), its gradient in the box's own coordinates. Add it onto the picture
- * (plus-lighter); compositing scales with the bars' width, not their count. */
-export function lightbarBox(b: LightBar): { left: string; width: string; background: string } {
- const lo = Math.max(0, b.x + LIGHTBAR_STOPS[0].d * b.w);
- const hi = Math.min(1, b.x + LIGHTBAR_STOPS[LIGHTBAR_STOPS.length - 1].d * b.w);
- const span = Math.max(hi - lo, 1e-6);
- const at = (s: { d: number }) => fmt(((b.x + s.d * b.w - lo) / span) * 100);
- return {
- left: `${fmt(lo * 100)}%`,
- width: `${fmt(span * 100)}%`,
- background: `linear-gradient(to right, ${LIGHTBAR_STOPS.map((s) => `${rgba(b.rgb, b.alpha * s.f)} ${at(s)}%`).join(", ")})`,
- };
+ if (alpha <= 0.002 || x + LB_HALO * w < 0 || x - LB_HALO * w > 1) continue;
+ bars.push({ x, w, rgb: l.rgb, alpha });
+ }
+ return bars;
}
/** The ellipse a wash covers, in frame fractions: center (x, y), radii (rx,
diff --git a/site/packages/effects-kit/src/lightbars.test.ts b/site/packages/effects-kit/src/lightbars.test.ts
index c03048ca..635c6181 100644
--- a/site/packages/effects-kit/src/lightbars.test.ts
+++ b/site/packages/effects-kit/src/lightbars.test.ts
@@ -1,7 +1,7 @@
import { spawnSync } from "node:child_process";
import { describe, expect, test } from "bun:test";
import { Canvas } from "skia-canvas";
-import { applyEffectToCanvas, EFFECT_IDS, effectFilterLines, effectPreviewState, lightbarBox, lightbarsAt } from "./effects";
+import { applyEffectToCanvas, EFFECT_IDS, effectFilterLines, effectPreviewState, lightbarsAt } from "./effects";
import { TEST_FFMPEG } from "./testing/ffmpeg";
type Surface = HTMLCanvasElement;
@@ -76,20 +76,6 @@ describe("the light bars' course", () => {
expect(at(DUR)).toEqual([]);
});
- test("the CSS twin places every bar where the canvas draws it, in a box of its own width", () => {
- for (const b of lightbarsAt(0.5, DUR, 1)) {
- const box = lightbarBox(b);
- const left = parseFloat(box.left) / 100;
- const width = parseFloat(box.width) / 100;
- expect(left).toBeGreaterThanOrEqual(0);
- expect(left + width).toBeLessThanOrEqual(1 + 1e-9);
- expect(width).toBeLessThan(0.5);
- // The profile's peak stop lands on the bar's center.
- const stops = [...box.background.matchAll(/ (-?[\d.]+)%/g)].map((m) => Number(m[1]) / 100);
- expect(Math.abs(left + stops[3] * width - b.x)).toBeLessThan(1e-4);
- }
- });
-
test("the canvas pass adds every bar in one composite, however many there are", () => {
// Over a clear-edged layer each pass is a full-frame mask and draw; one
// per bar was well over a hundred a frame at the burst.
diff --git a/site/src/cut/components/EffectsPanel.tsx b/site/src/cut/components/EffectsPanel.tsx
index f0734910..43efa688 100644
--- a/site/src/cut/components/EffectsPanel.tsx
+++ b/site/src/cut/components/EffectsPanel.tsx
@@ -11,7 +11,6 @@ import {
isAudioEffect,
LEAK_TINT,
leakGradient,
- lightbarBox,
streakGradient,
type EffectId,
} from "@donkeycut/effects-kit";
@@ -39,6 +38,7 @@ import { Input } from "@/components/ui/input";
import { ScrollArea } from "@/components/ui/scroll-area";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import "./grain.css";
+import { LightbarCanvas } from "./LightbarCanvas";
/**
* The Effects tab: time-ranged treatments over the finished cut. A segmented
@@ -1010,13 +1010,7 @@ export function EffectSwatch({
{!!st.flash && (
<span className="absolute inset-0 bg-white" style={{ opacity: Math.min(0.75, st.flash) }} />
)}
- {st.bars?.map((b, j) => (
- <span
- key={`bar-${j}`}
- className="absolute inset-y-0"
- style={{ ...lightbarBox(b), mixBlendMode: "plus-lighter" }}
- />
- ))}
+ {!!st.bars?.length && <LightbarCanvas bars={st.bars} />}
</span>
);
}
diff --git a/site/src/cut/components/LightbarCanvas.tsx b/site/src/cut/components/LightbarCanvas.tsx
new file mode 100644
index 00000000..900cbf9f
--- /dev/null
+++ b/site/src/cut/components/LightbarCanvas.tsx
@@ -0,0 +1,104 @@
+"use client";
+
+import { useEffect, useLayoutEffect, useRef } from "react";
+import { LIGHTBAR_STOPS, type LightBar } from "@donkeycut/effects-kit";
+
+/** Texels across one bar's profile sprite. */
+const SPRITE_TEXELS = 256;
+
+const STOP_LO = LIGHTBAR_STOPS[0].d;
+const STOP_HI = LIGHTBAR_STOPS[LIGHTBAR_STOPS.length - 1].d;
+
+/** One bar's profile per ribbon color, drawn once and stretched over every
+ * bar of that color. The ribbon shares its palette tuples, so the tuple is
+ * the key and a frame looks sprites up without building a string. */
+const sprites = new WeakMap<LightBar["rgb"], HTMLCanvasElement>();
+
+function spriteOf(rgb: LightBar["rgb"]): HTMLCanvasElement | null {
+ const hit = sprites.get(rgb);
+ if (hit) {
+ return hit;
+ }
+ const c = document.createElement("canvas");
+ c.width = SPRITE_TEXELS;
+ c.height = 1;
+ const ctx = c.getContext("2d");
+ if (!ctx) {
+ return null;
+ }
+ const g = ctx.createLinearGradient(0, 0, SPRITE_TEXELS, 0);
+ for (const st of LIGHTBAR_STOPS) {
+ g.addColorStop((st.d - STOP_LO) / (STOP_HI - STOP_LO), `rgba(${rgb[0]},${rgb[1]},${rgb[2]},${st.f})`);
+ }
+ ctx.fillStyle = g;
+ ctx.fillRect(0, 0, SPRITE_TEXELS, 1);
+ sprites.set(rgb, c);
+ return c;
+}
+
+/** Add every bar into the one-pixel row, each its sprite at its opacity. */
+function paintBars(c: HTMLCanvasElement, bars: LightBar[]) {
+ const ctx = c.getContext("2d");
+ if (!ctx) {
+ return;
+ }
+ const W = c.width;
+ ctx.globalCompositeOperation = "source-over";
+ ctx.globalAlpha = 1;
+ ctx.clearRect(0, 0, W, 1);
+ ctx.globalCompositeOperation = "lighter";
+ for (const b of bars) {
+ const sprite = spriteOf(b.rgb);
+ if (!sprite) {
+ continue;
+ }
+ ctx.globalAlpha = b.alpha;
+ ctx.drawImage(sprite, (b.x + STOP_LO * b.w) * W, 0, (STOP_HI - STOP_LO) * b.w * W, 1);
+ }
+}
+
+/**
+ * Light bars added onto the picture, the DOM twin of the canvas pass. Bars
+ * are full-height and vary only across, so they paint into a single row of
+ * pixels that CSS stretches to the frame: a frame of bars is one draw per bar
+ * and one composited layer, however many are up.
+ */
+export function LightbarCanvas({ bars }: { bars: LightBar[] }) {
+ const ref = useRef<HTMLCanvasElement>(null);
+ const latest = useRef(bars);
+
+ // Repaint on every new set of bars, before the frame shows.
+ useLayoutEffect(() => {
+ latest.current = bars;
+ if (ref.current) {
+ paintBars(ref.current, bars);
+ }
+ }, [bars]);
+
+ // The row is as wide as the box in device pixels; a resize repaints it.
+ useEffect(() => {
+ const c = ref.current;
+ if (!c) {
+ return;
+ }
+ const ro = new ResizeObserver(([entry]) => {
+ const w = Math.max(1, Math.round(entry.contentRect.width * window.devicePixelRatio));
+ if (c.width === w) {
+ return;
+ }
+ c.width = w;
+ paintBars(c, latest.current);
+ });
+ ro.observe(c);
+ return () => ro.disconnect();
+ }, []);
+
+ return (
+ <canvas
+ ref={ref}
+ height={1}
+ className="pointer-events-none absolute inset-0 size-full"
+ style={{ mixBlendMode: "plus-lighter" }}
+ />
+ );
+}
diff --git a/site/src/cut/components/StageEffects.tsx b/site/src/cut/components/StageEffects.tsx
index 085f696c..96de0961 100644
--- a/site/src/cut/components/StageEffects.tsx
+++ b/site/src/cut/components/StageEffects.tsx
@@ -8,7 +8,6 @@ import {
isAudioEffect,
LEAK_TINT,
leakGradient,
- lightbarBox,
streakGradient,
washBackground,
} from "@donkeycut/effects-kit";
@@ -23,6 +22,7 @@ import {
type LiveEffect,
} from "@/cut/lib/effectStack";
import "./grain.css";
+import { LightbarCanvas } from "./LightbarCanvas";
import { rgbSplitFilter } from "./rgbSplit";
/**
@@ -238,15 +238,7 @@ export function StageEffectPaint({ lane }: { lane: number }) {
)}
</div>
))}
- {bars.map((b, i) => (
- // Each light bar added onto the picture, as the canvas pass's
- // "lighter" composite does.
- <div
- key={`bar-${i}`}
- className="absolute inset-y-0"
- style={{ ...lightbarBox(b), mixBlendMode: "plus-lighter" }}
- />
- ))}
+ {bars.length > 0 && <LightbarCanvas bars={bars} />}
</div>
);
}
diff --git a/site/src/cut/lib/spanWarm.test.ts b/site/src/cut/lib/spanWarm.test.ts
index 92ad373f..8385f857 100644
--- a/site/src/cut/lib/spanWarm.test.ts
+++ b/site/src/cut/lib/spanWarm.test.ts
@@ -1,5 +1,17 @@
import { expect, test } from "bun:test";
-import { warmOrder } from "./spanWarm";
+import { stubModule } from "@/lib/testing/stubModule";
+import type * as MediaRead from "./mediaRead";
+import { warmClipSpans, warmOrder } from "./spanWarm";
+
+// Every open is counted; a file with no video track reads as done at once.
+const opened: string[] = [];
+await stubModule<typeof MediaRead>("./mediaRead", import.meta.url, {
+ openMediaShared: ((url: string) => {
+ opened.push(url);
+ return { input: {}, release: () => {} };
+ }) as unknown as typeof MediaRead.openMediaShared,
+ videoTrackOf: (async () => null) as unknown as typeof MediaRead.videoTrackOf,
+});
// A cut that changes file every half second stalls at every cold join, so the
// clips still to play are read first, each from the file the preview decodes.
@@ -31,3 +43,13 @@ test("a reversed or sped-up clip reads its source span", () => {
const [span] = warmOrder([{ assetId: "a", start: 0, in: 6, out: 2, speed: 4 }], assets, 0);
expect(span).toEqual({ url: "https://m/a.mp4", from: 2, to: 6 });
});
+
+test("a play restarts the walk from the playhead without reading again what it already read", async () => {
+ const spans = warmOrder(clips, assets, 0);
+ opened.length = 0;
+ await warmClipSpans(spans);
+ expect(opened.length).toBe(spans.length);
+ opened.length = 0;
+ await warmClipSpans(warmOrder(clips, assets, 0.7));
+ expect(opened).toEqual([]);
+});
diff --git a/site/src/cut/lib/spanWarm.ts b/site/src/cut/lib/spanWarm.ts
index e0b31a6e..96ed957d 100644
--- a/site/src/cut/lib/spanWarm.ts
+++ b/site/src/cut/lib/spanWarm.ts
@@ -55,19 +55,28 @@ export function warmOrder(
});
}
+/** Spans read to the end since the project opened, e.g. "url|4|4.5". A play
+ * restarts the walk from the playhead, and the walk passes over these, so a
+ * play costs only what is still unread. */
+const warmed = new Set<string>();
+const spanKey = (span: WarmSpan) => `${span.url}|${span.from}|${span.to}`;
+
/** Read one span's packets through the shared reader, so the index the
* playing decoder parses next is the one this parsed. */
async function warmSpan(span: WarmSpan, signal: AbortSignal): Promise<void> {
const handle = openMediaShared(span.url);
try {
const track = await videoTrackOf(handle.input);
- if (!track || signal.aborted) return;
- const sink = new EncodedPacketSink(track);
- let packet =
- (await sink.getKeyPacket(span.from, { verifyKeyPackets: true })) ?? (await sink.getFirstPacket());
- while (packet && packet.timestamp <= span.to && !signal.aborted) {
- packet = await sink.getNextPacket(packet);
+ if (signal.aborted) return;
+ if (track) {
+ const sink = new EncodedPacketSink(track);
+ let packet =
+ (await sink.getKeyPacket(span.from, { verifyKeyPackets: true })) ?? (await sink.getFirstPacket());
+ while (packet && packet.timestamp <= span.to && !signal.aborted) {
+ packet = await sink.getNextPacket(packet);
+ }
}
+ if (!signal.aborted) warmed.add(spanKey(span));
} finally {
handle.release();
}
@@ -82,10 +91,11 @@ let walk: Promise<void> | null = null;
* Resolves when every span has been read or the walk was cancelled. A span
* that fails is left for the decoder's own read: this is only a head start.
*/
-export function warmClipSpans(spans: WarmSpan[]): Promise<void> {
+export function warmClipSpans(all: WarmSpan[]): Promise<void> {
running?.abort();
const ctrl = new AbortController();
running = ctrl;
+ const spans = all.filter((span) => !warmed.has(spanKey(span)));
let next = 0;
const lane = async () => {
while (!ctrl.signal.aborted && next < spans.length) {
@@ -116,4 +126,5 @@ export function stopClipWarm(): void {
running?.abort();
running = null;
walk = null;
+ warmed.clear();
}