@hyperframes/studio 0.8.209 → 0.8.211

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/dist/assets/{hyperframes-player-CMH8OeX2.js → hyperframes-player-CzrqgMUX.js} +1 -1
  2. package/dist/assets/{index-DjHzuJGR.js → index-B3SPHPi1.js} +1 -1
  3. package/dist/assets/{index-CYbvOVp-.css → index-RUPGNyct.css} +1 -1
  4. package/dist/assets/{index-DQam8zzE.js → index-k4nVfS2D.js} +123 -123
  5. package/dist/{chunk-HPIJDMWG.js → chunk-HD6NXWGI.js} +6 -5
  6. package/dist/index.d.ts +4 -0
  7. package/dist/index.html +2 -2
  8. package/dist/index.js +485 -241
  9. package/dist/player.js +1 -1
  10. package/dist/styles.css +1 -1
  11. package/package.json +8 -8
  12. package/src/components/DesktopGuideCard.tsx +1 -1
  13. package/src/components/nle/NLEPreview.test.ts +9 -0
  14. package/src/components/nle/NLEPreview.tsx +1 -1
  15. package/src/player/components/Player.test.ts +10 -0
  16. package/src/player/components/Player.tsx +0 -12
  17. package/src/player/components/VideoThumbnail.tsx +6 -0
  18. package/src/player/hooks/previewMessageRouter.test.ts +4 -0
  19. package/src/player/lib/mediaFetch.test.ts +466 -0
  20. package/src/player/lib/mediaFetch.ts +133 -26
  21. package/src/player/lib/thumbnailFrameCache.ts +69 -0
  22. package/src/player/lib/thumbnailPlaybackHold.test.ts +86 -0
  23. package/src/player/lib/thumbnailPlaybackHold.ts +15 -0
  24. package/src/player/lib/thumbnailScheduler.test.ts +49 -0
  25. package/src/player/lib/thumbnailScheduler.ts +22 -6
  26. package/src/player/lib/thumbnailSourcePool.ts +163 -0
  27. package/src/player/lib/thumbnailVideoDecoder.test.ts +492 -8
  28. package/src/player/lib/thumbnailVideoDecoder.ts +67 -75
  29. package/src/player/lib/timelineViewportBudgets.test.ts +1 -0
  30. package/src/player/lib/timelineViewportBudgets.ts +4 -0
  31. package/src/player/store/previewQualitySlice.ts +11 -0
  32. package/src/styles/hex-baseline.json +0 -1
@@ -3,10 +3,14 @@
3
3
  import { beforeEach, describe, expect, it, vi } from "vitest";
4
4
  import { decodeVideoThumbnail, videoThumbnailTimestamps } from "./thumbnailVideoDecoder";
5
5
  import { MediaFetchError } from "./mediaFetch";
6
+ import { resolveTimelineViewportBudgets } from "./timelineViewportBudgets";
7
+ import { releaseSharedFrames } from "./thumbnailFrameCache";
6
8
 
7
9
  const dispose = vi.fn();
8
10
  const sourceRetryRules: string[] = [];
11
+ const cancelledReads: ReturnType<typeof vi.fn>[] = [];
9
12
  const canvasesAtTimestamps = vi.fn();
13
+ const canvasSinks: unknown[] = [];
10
14
  const getKeyPacket = vi.fn(async (_time: number) => null as { timestamp: number } | null);
11
15
  const input = {
12
16
  getPrimaryVideoTrack: vi.fn(),
@@ -18,9 +22,12 @@ vi.mock("mediabunny", () => ({
18
22
  UrlSource: class {
19
23
  constructor(
20
24
  readonly url: string,
21
- readonly options?: { getRetryDelay?: unknown },
25
+ readonly options?: { getRetryDelay?: unknown; cancelReads?: () => void },
22
26
  ) {
23
27
  sourceRetryRules.push(typeof options?.getRetryDelay);
28
+ // Spied where the pool reads it, after constructing the source.
29
+ if (options?.cancelReads)
30
+ cancelledReads.push((options.cancelReads = vi.fn(options.cancelReads)));
24
31
  }
25
32
  },
26
33
  Input: class {
@@ -28,6 +35,9 @@ vi.mock("mediabunny", () => ({
28
35
  dispose = input.dispose;
29
36
  },
30
37
  CanvasSink: class {
38
+ constructor() {
39
+ canvasSinks.push(this);
40
+ }
31
41
  canvasesAtTimestamps = canvasesAtTimestamps;
32
42
  },
33
43
  EncodedPacketSink: class {
@@ -48,13 +58,23 @@ function recordDecodes(decoded: number[][]): void {
48
58
 
49
59
  const endOf = (start: number, duration: number) => start + Math.max(0, duration - 0.001);
50
60
 
61
+ // Keeps no released frame and no idle source, so a release revokes and disposes at once.
62
+ const NOTHING_KEPT = resolveTimelineViewportBudgets({
63
+ sharedFrameCacheBytes: 0,
64
+ thumbnailSourceIdleMs: 0,
65
+ });
66
+
51
67
  // Decoded frames are shared per source for the module's life, so each test decodes its own file.
52
68
  let sources = 0;
53
69
  let source = "";
54
70
 
55
71
  beforeEach(() => {
56
72
  source = `/clip-${++sources}.mp4`;
73
+ // Released frames outlive their test; evict them so each test counts only its own revokes.
74
+ releaseSharedFrames([], 0);
57
75
  vi.clearAllMocks();
76
+ canvasSinks.length = 0;
77
+ cancelledReads.length = 0;
58
78
  getKeyPacket.mockImplementation(async () => null);
59
79
  vi.spyOn(URL, "createObjectURL")
60
80
  .mockReturnValueOnce("blob:one")
@@ -96,6 +116,7 @@ describe("decodeVideoThumbnail", () => {
96
116
  const result = await decodeVideoThumbnail(
97
117
  { source, sourceStart: 2, sourceRangeDuration: 6, frameCount: 2 },
98
118
  new AbortController().signal,
119
+ NOTHING_KEPT,
99
120
  );
100
121
 
101
122
  expect(decoded).toEqual([[2, 5, endOf(2, 6)]]);
@@ -251,10 +272,12 @@ describe("decodeVideoThumbnail", () => {
251
272
  const original = await decodeVideoThumbnail(
252
273
  { source, contentVersion: "A:1", frameCount: 1 },
253
274
  signal,
275
+ NOTHING_KEPT,
254
276
  );
255
277
  const other = await decodeVideoThumbnail(
256
278
  { source: `${source}?B`, contentVersion: "B:2", frameCount: 1 },
257
279
  signal,
280
+ NOTHING_KEPT,
258
281
  );
259
282
  input.getPrimaryVideoTrack.mockResolvedValue({
260
283
  getDisplayWidth: vi.fn(async () => 1280),
@@ -264,10 +287,12 @@ describe("decodeVideoThumbnail", () => {
264
287
  const reopened = await decodeVideoThumbnail(
265
288
  { source, contentVersion: "A:3", frameCount: 1 },
266
289
  signal,
290
+ NOTHING_KEPT,
267
291
  );
268
292
  const shared = await decodeVideoThumbnail(
269
293
  { source, contentVersion: "A:3", frameCount: 1 },
270
294
  signal,
295
+ NOTHING_KEPT,
271
296
  );
272
297
  expect(reopened.value).toEqual({ kind: "image", url: "blob:version-3", aspect: 32 / 9 });
273
298
  expect(shared.value).toEqual(reopened.value);
@@ -298,11 +323,11 @@ describe("decodeVideoThumbnail", () => {
298
323
  });
299
324
  const range = { source, sourceStart: 0, sourceRangeDuration: 8 };
300
325
  const signal = new AbortController().signal;
301
- const decodingTwo = decodeVideoThumbnail({ ...range, frameCount: 2 }, signal);
326
+ const decodingTwo = decodeVideoThumbnail({ ...range, frameCount: 2 }, signal, NOTHING_KEPT);
302
327
  await vi.waitFor(() => expect(canvasesAtTimestamps).toHaveBeenCalledOnce());
303
328
  const [two, four] = await Promise.all([
304
329
  decodingTwo,
305
- decodeVideoThumbnail({ ...range, frameCount: 4 }, signal),
330
+ decodeVideoThumbnail({ ...range, frameCount: 4 }, signal, NOTHING_KEPT),
306
331
  ]);
307
332
  // Both decoded the frames at 0 s, 4 s and the end; the second copy of each goes at once.
308
333
  expect(URL.revokeObjectURL).toHaveBeenCalledTimes(3);
@@ -369,8 +394,8 @@ describe("decodeVideoThumbnail", () => {
369
394
  recordDecodes([]);
370
395
  const range = { source, sourceStart: 0, sourceRangeDuration: 8 };
371
396
  const signal = new AbortController().signal;
372
- const four = await decodeVideoThumbnail({ ...range, frameCount: 4 }, signal);
373
- const two = await decodeVideoThumbnail({ ...range, frameCount: 2 }, signal);
397
+ const four = await decodeVideoThumbnail({ ...range, frameCount: 4 }, signal, NOTHING_KEPT);
398
+ const two = await decodeVideoThumbnail({ ...range, frameCount: 2 }, signal, NOTHING_KEPT);
374
399
  four.dispose?.();
375
400
  // The three frames the 2-slice strip still shows stay; the other two go.
376
401
  expect(URL.revokeObjectURL).toHaveBeenCalledTimes(2);
@@ -378,6 +403,53 @@ describe("decodeVideoThumbnail", () => {
378
403
  expect(URL.revokeObjectURL).toHaveBeenCalledTimes(5);
379
404
  });
380
405
 
406
+ it("keeps a strip's frames after it is released, so scrolling back to it decodes nothing", async () => {
407
+ const decoded: number[][] = [];
408
+ recordDecodes(decoded);
409
+ const strip = { source, sourceStart: 0, sourceRangeDuration: 8, frameCount: 2 };
410
+ const signal = new AbortController().signal;
411
+ const first = await decodeVideoThumbnail(strip, signal);
412
+ first.dispose?.();
413
+ const again = await decodeVideoThumbnail(strip, signal);
414
+ expect(decoded).toEqual([[0, 4, endOf(0, 8)]]);
415
+ expect(again.value).toEqual(first.value);
416
+ expect(URL.revokeObjectURL).not.toHaveBeenCalled();
417
+ again.dispose?.();
418
+ });
419
+
420
+ it("revokes released frames oldest first once they pass the byte budget, never a shown one", async () => {
421
+ let frame = 0;
422
+ vi.mocked(URL.createObjectURL)
423
+ .mockReset()
424
+ .mockImplementation(() => `blob:frame-${++frame}`);
425
+ recordDecodes([]);
426
+ // Each test frame is the 5-byte blob "frame": the budget holds one released strip of three.
427
+ const budgets = resolveTimelineViewportBudgets({ sharedFrameCacheBytes: 15 });
428
+ const signal = new AbortController().signal;
429
+ const strip = (file: string) =>
430
+ decodeVideoThumbnail(
431
+ { source: `${source}?${file}`, sourceStart: 0, sourceRangeDuration: 8, frameCount: 2 },
432
+ signal,
433
+ budgets,
434
+ );
435
+ const older = await strip("older");
436
+ const newer = await strip("newer");
437
+ const shown = await strip("shown");
438
+ const sameFrames = await strip("shown");
439
+ const urls = (result: typeof older) =>
440
+ result.value.kind === "filmstrip" ? [...result.value.urls] : [];
441
+ older.dispose?.();
442
+ for (const url of urls(older)) expect(URL.revokeObjectURL).not.toHaveBeenCalledWith(url);
443
+ // Its frames are still on screen in `shown`, so they are not released and cannot be evicted.
444
+ sameFrames.dispose?.();
445
+ newer.dispose?.();
446
+ for (const url of urls(older)) expect(URL.revokeObjectURL).toHaveBeenCalledWith(url);
447
+ for (const url of [...urls(newer), ...urls(shown)]) {
448
+ expect(URL.revokeObjectURL).not.toHaveBeenCalledWith(url);
449
+ }
450
+ shown.dispose?.();
451
+ });
452
+
381
453
  it("reports a failed range fetch as one typed line and rethrows it", async () => {
382
454
  const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
383
455
  sourceRetryRules.length = 0;
@@ -397,7 +469,11 @@ describe("decodeVideoThumbnail", () => {
397
469
  it("releases input and degrades when the source has no video track", async () => {
398
470
  input.getPrimaryVideoTrack.mockResolvedValue(null);
399
471
  await expect(
400
- decodeVideoThumbnail({ source: "/audio.mp3", frameCount: 1 }, new AbortController().signal),
472
+ decodeVideoThumbnail(
473
+ { source: "/audio.mp3", frameCount: 1 },
474
+ new AbortController().signal,
475
+ NOTHING_KEPT,
476
+ ),
401
477
  ).rejects.toThrow("no decodable video track");
402
478
  expect(dispose).toHaveBeenCalledTimes(1);
403
479
  });
@@ -413,7 +489,7 @@ describe("decodeVideoThumbnail", () => {
413
489
  }
414
490
  });
415
491
  await expect(
416
- decodeVideoThumbnail({ source, frameCount: 2 }, controller.signal),
492
+ decodeVideoThumbnail({ source, frameCount: 2 }, controller.signal, NOTHING_KEPT),
417
493
  ).rejects.toMatchObject({ name: "AbortError" });
418
494
  expect(URL.revokeObjectURL).toHaveBeenCalledTimes(1);
419
495
  expect(dispose).toHaveBeenCalledTimes(1);
@@ -433,7 +509,11 @@ describe("decodeVideoThumbnail", () => {
433
509
  getDurationFromMetadata,
434
510
  });
435
511
 
436
- const decoding = decodeVideoThumbnail({ source, frameCount: 1 }, controller.signal);
512
+ const decoding = decodeVideoThumbnail(
513
+ { source, frameCount: 1 },
514
+ controller.signal,
515
+ NOTHING_KEPT,
516
+ );
437
517
  await vi.waitFor(() => expect(getDisplayWidth).toHaveBeenCalledOnce());
438
518
  controller.abort();
439
519
  resolveWidth(1080);
@@ -444,3 +524,407 @@ describe("decodeVideoThumbnail", () => {
444
524
  expect(dispose).toHaveBeenCalledTimes(1);
445
525
  });
446
526
  });
527
+
528
+ describe("decodeVideoThumbnail's shared source", () => {
529
+ const signal = new AbortController().signal;
530
+ const clip = (sourceStart: number, extra: object = {}) => ({
531
+ source,
532
+ sourceStart,
533
+ sourceRangeDuration: 4,
534
+ frameCount: 2,
535
+ ...extra,
536
+ });
537
+
538
+ it("opens a source once for every clip cut from it", async () => {
539
+ recordDecodes([]);
540
+ await decodeVideoThumbnail(clip(0), signal);
541
+ await decodeVideoThumbnail(clip(6), signal);
542
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(1);
543
+ });
544
+
545
+ it("opens a source once for clips decoding at the same time, each with its own canvas sink", async () => {
546
+ recordDecodes([]);
547
+ await Promise.all([
548
+ decodeVideoThumbnail(clip(0), signal),
549
+ decodeVideoThumbnail(clip(6), signal),
550
+ ]);
551
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(1);
552
+ expect(canvasSinks).toHaveLength(2);
553
+ });
554
+
555
+ it("closes a source once it has sat idle for its budget", async () => {
556
+ vi.useFakeTimers();
557
+ try {
558
+ recordDecodes([]);
559
+ const budgets = resolveTimelineViewportBudgets({ thumbnailSourceIdleMs: 1_000 });
560
+ await decodeVideoThumbnail(clip(0), signal, budgets);
561
+ await vi.advanceTimersByTimeAsync(999);
562
+ expect(dispose).not.toHaveBeenCalled();
563
+ await decodeVideoThumbnail(clip(6), signal, budgets);
564
+ await vi.advanceTimersByTimeAsync(999);
565
+ expect(dispose).not.toHaveBeenCalled();
566
+ await vi.advanceTimersByTimeAsync(1);
567
+ expect(dispose).toHaveBeenCalledTimes(1);
568
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(1);
569
+ // Disposing alone leaves a stalled fetch waiting; its reads are cancelled too.
570
+ expect(cancelledReads[0]).toHaveBeenCalledOnce();
571
+ // A closed reader has left the pool: the next clip opens a live one.
572
+ await expect(decodeVideoThumbnail(clip(12), signal, budgets)).resolves.toBeDefined();
573
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(2);
574
+ } finally {
575
+ vi.useRealTimers();
576
+ }
577
+ });
578
+
579
+ it("opens a new reader when the source's content version changes, and closes the old one at once", async () => {
580
+ recordDecodes([]);
581
+ const budgets = resolveTimelineViewportBudgets({ thumbnailSourceIdleMs: 60_000 });
582
+ await decodeVideoThumbnail(clip(0, { contentVersion: "A:1" }), signal, budgets);
583
+ await decodeVideoThumbnail(clip(0, { contentVersion: "A:2" }), signal, budgets);
584
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(2);
585
+ expect(dispose).toHaveBeenCalledTimes(1);
586
+ });
587
+
588
+ it("closes the old version's reader only once the job still decoding on it ends", async () => {
589
+ recordDecodes([]);
590
+ let finish!: () => void;
591
+ const held = new Promise<void>((resolve) => (finish = resolve));
592
+ canvasesAtTimestamps.mockImplementationOnce(async function* (
593
+ timestamps: AsyncIterable<number>,
594
+ ) {
595
+ for await (const _time of timestamps) {
596
+ await held;
597
+ yield { canvas: document.createElement("canvas") };
598
+ }
599
+ });
600
+ const budgets = resolveTimelineViewportBudgets({ thumbnailSourceIdleMs: 60_000 });
601
+ const old = decodeVideoThumbnail(clip(0, { contentVersion: "A:1" }), signal, budgets);
602
+ await vi.waitFor(() => expect(canvasesAtTimestamps).toHaveBeenCalledOnce());
603
+ await decodeVideoThumbnail(clip(6, { contentVersion: "A:2" }), signal, budgets);
604
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(2);
605
+ expect(dispose).not.toHaveBeenCalled();
606
+
607
+ finish();
608
+ await old;
609
+ expect(dispose).toHaveBeenCalledTimes(1);
610
+ });
611
+
612
+ it("stops opening a source every job let go of before its track was read", async () => {
613
+ let giveTrack!: (track: unknown) => void;
614
+ input.getPrimaryVideoTrack.mockImplementationOnce(
615
+ () => new Promise((resolve) => (giveTrack = resolve)),
616
+ );
617
+ const controller = new AbortController();
618
+ const decoding = decodeVideoThumbnail(clip(0), controller.signal, NOTHING_KEPT);
619
+ await vi.waitFor(() => expect(input.getPrimaryVideoTrack).toHaveBeenCalledOnce());
620
+ controller.abort();
621
+ await expect(decoding).rejects.toMatchObject({ name: "AbortError" });
622
+ expect(dispose).toHaveBeenCalledTimes(1);
623
+
624
+ const getDisplayWidth = vi.fn(async () => 1080);
625
+ giveTrack({ getDisplayWidth, getDisplayHeight: vi.fn(async () => 1920) });
626
+ await new Promise((resolve) => setTimeout(resolve, 0));
627
+ expect(getDisplayWidth).not.toHaveBeenCalled();
628
+ });
629
+
630
+ it("keeps no source whose open failed, so the next clip opens it again", async () => {
631
+ recordDecodes([]);
632
+ input.getPrimaryVideoTrack.mockRejectedValueOnce(new Error("unreadable"));
633
+ await expect(decodeVideoThumbnail(clip(0), signal)).rejects.toThrow("unreadable");
634
+ expect(dispose).toHaveBeenCalledTimes(1);
635
+ await expect(decodeVideoThumbnail(clip(6), signal)).resolves.toBeDefined();
636
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(2);
637
+ });
638
+
639
+ it("reopens a source whose open failed after every clip waiting on it had cancelled", async () => {
640
+ recordDecodes([]);
641
+ let failOpen!: (error: Error) => void;
642
+ input.getPrimaryVideoTrack.mockImplementationOnce(
643
+ () => new Promise((_, reject) => (failOpen = reject)),
644
+ );
645
+ const budgets = resolveTimelineViewportBudgets({ thumbnailSourceIdleMs: 60_000 });
646
+ const controller = new AbortController();
647
+ const cancelled = decodeVideoThumbnail(clip(0), controller.signal, budgets);
648
+ await vi.waitFor(() => expect(input.getPrimaryVideoTrack).toHaveBeenCalledOnce());
649
+ controller.abort();
650
+ await expect(cancelled).rejects.toMatchObject({ name: "AbortError" });
651
+ failOpen(new Error("unreadable"));
652
+ await new Promise((resolve) => setTimeout(resolve, 0));
653
+ await expect(decodeVideoThumbnail(clip(6), signal, budgets)).resolves.toBeDefined();
654
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(2);
655
+ });
656
+
657
+ it("keeps the reader when a clip's own decode yields no frames", async () => {
658
+ canvasesAtTimestamps.mockImplementationOnce(async function* (
659
+ timestamps: AsyncIterable<number>,
660
+ ) {
661
+ for await (const _time of timestamps) yield null;
662
+ });
663
+ await expect(decodeVideoThumbnail(clip(0), signal)).rejects.toThrow("no thumbnail frames");
664
+ recordDecodes([]);
665
+ await decodeVideoThumbnail(clip(6), signal);
666
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(1);
667
+ expect(dispose).not.toHaveBeenCalled();
668
+ });
669
+
670
+ it("keeps the reader when a clip's frame will not encode", async () => {
671
+ HTMLCanvasElement.prototype.toBlob = function toBlob(callback) {
672
+ callback(null);
673
+ };
674
+ recordDecodes([]);
675
+ await expect(decodeVideoThumbnail(clip(0), signal)).rejects.toThrow("encode failed");
676
+ HTMLCanvasElement.prototype.toBlob = function toBlob(callback) {
677
+ callback(new Blob(["frame"], { type: "image/jpeg" }));
678
+ };
679
+ await decodeVideoThumbnail(clip(6), signal);
680
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(1);
681
+ expect(dispose).not.toHaveBeenCalled();
682
+ });
683
+
684
+ it("keeps the reader when an offscreen frame will not encode", async () => {
685
+ const offscreen = { width: 1, height: 1, convertToBlob: () => Promise.reject(new Error("no")) };
686
+ canvasesAtTimestamps.mockImplementationOnce(async function* (
687
+ timestamps: AsyncIterable<number>,
688
+ ) {
689
+ for await (const _time of timestamps) yield { canvas: offscreen };
690
+ });
691
+ await expect(decodeVideoThumbnail(clip(0), signal)).rejects.toThrow("encode failed");
692
+ recordDecodes([]);
693
+ await decodeVideoThumbnail(clip(6), signal);
694
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(1);
695
+ expect(dispose).not.toHaveBeenCalled();
696
+ });
697
+
698
+ it("keeps the reader of a clip scrolled away from, for the next clip", async () => {
699
+ vi.useFakeTimers();
700
+ try {
701
+ const budgets = resolveTimelineViewportBudgets({ thumbnailSourceIdleMs: 60_000 });
702
+ const controller = new AbortController();
703
+ getKeyPacket.mockImplementationOnce(async () => (controller.abort(), null));
704
+ recordDecodes([]);
705
+ await expect(decodeVideoThumbnail(clip(0), controller.signal, budgets)).rejects.toMatchObject(
706
+ {
707
+ name: "AbortError",
708
+ },
709
+ );
710
+ // Past a load timeout: nothing a cancel left behind may close the reader.
711
+ await vi.advanceTimersByTimeAsync(31_000);
712
+ await decodeVideoThumbnail(clip(6), signal, budgets);
713
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(1);
714
+ expect(dispose).not.toHaveBeenCalled();
715
+ } finally {
716
+ vi.useRealTimers();
717
+ }
718
+ });
719
+
720
+ const timedOut = () => new DOMException("Thumbnail load timed out", "TimeoutError");
721
+ const TIMEOUT_LINE = "[Studio] thumbnail range fetch failed: network timeout after 0 retries";
722
+ // A read that never returns until its reader is disposed, like one on a source that never answers.
723
+ const untilDisposed = () =>
724
+ new Promise<never>((_, reject) =>
725
+ dispose.mockImplementationOnce(() => reject(new Error("Input disposed"))),
726
+ );
727
+
728
+ it("fails a job that times out without progress as its source's failure, once, and drops the reader", async () => {
729
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
730
+ recordDecodes([]);
731
+ await decodeVideoThumbnail(clip(0), signal);
732
+ const stalled = untilDisposed();
733
+ canvasesAtTimestamps.mockImplementationOnce(async function* () {
734
+ yield await stalled;
735
+ });
736
+ const controller = new AbortController();
737
+ const stuck = decodeVideoThumbnail(clip(6), controller.signal);
738
+ await vi.waitFor(() => expect(canvasesAtTimestamps).toHaveBeenCalledTimes(2));
739
+ controller.abort(timedOut());
740
+
741
+ await expect(stuck).rejects.toThrow("Input disposed");
742
+ expect(warn.mock.calls).toEqual([[TIMEOUT_LINE]]);
743
+ await decodeVideoThumbnail(clip(12), signal);
744
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(2);
745
+ });
746
+
747
+ it("keeps the reader when a job times out after decoding a frame", async () => {
748
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
749
+ recordDecodes([]);
750
+ await decodeVideoThumbnail(clip(0), signal);
751
+ canvasesAtTimestamps.mockImplementationOnce(async function* (
752
+ timestamps: AsyncIterable<number>,
753
+ ) {
754
+ await timestamps[Symbol.asyncIterator]().next();
755
+ yield { canvas: document.createElement("canvas") };
756
+ await new Promise(() => {});
757
+ });
758
+ const controller = new AbortController();
759
+ void decodeVideoThumbnail(clip(6), controller.signal).catch(() => {});
760
+ await vi.waitFor(() => expect(URL.createObjectURL).toHaveBeenCalledTimes(4));
761
+ controller.abort(timedOut());
762
+
763
+ await decodeVideoThumbnail(clip(12), signal);
764
+ expect(warn).not.toHaveBeenCalled();
765
+ expect(dispose).not.toHaveBeenCalled();
766
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(1);
767
+ });
768
+
769
+ it("keeps the reader when a clip times out while another clip decodes frames from it", async () => {
770
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
771
+ recordDecodes([]);
772
+ await decodeVideoThumbnail(clip(0), signal);
773
+ canvasesAtTimestamps.mockImplementationOnce(async function* () {
774
+ await new Promise(() => {});
775
+ yield null;
776
+ });
777
+ canvasesAtTimestamps.mockImplementationOnce(async function* (
778
+ timestamps: AsyncIterable<number>,
779
+ ) {
780
+ await timestamps[Symbol.asyncIterator]().next();
781
+ yield { canvas: document.createElement("canvas") };
782
+ await new Promise(() => {});
783
+ });
784
+ const controller = new AbortController();
785
+ void decodeVideoThumbnail(clip(6), controller.signal).catch(() => {});
786
+ void decodeVideoThumbnail(clip(12), new AbortController().signal).catch(() => {});
787
+ await vi.waitFor(() => expect(URL.createObjectURL).toHaveBeenCalledTimes(4));
788
+ controller.abort(timedOut());
789
+
790
+ expect(warn).not.toHaveBeenCalled();
791
+ await decodeVideoThumbnail(clip(18), signal);
792
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(1);
793
+ });
794
+
795
+ it("fails a job that times out waiting for its source to open, and the next clip opens afresh", async () => {
796
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
797
+ const stalled = untilDisposed();
798
+ input.getPrimaryVideoTrack.mockImplementationOnce(() => stalled);
799
+ const controller = new AbortController();
800
+ const waiting = decodeVideoThumbnail(clip(0), controller.signal);
801
+ await vi.waitFor(() => expect(input.getPrimaryVideoTrack).toHaveBeenCalledOnce());
802
+ controller.abort(timedOut());
803
+
804
+ await expect(waiting).rejects.toMatchObject({ name: "AbortError" });
805
+ expect(warn.mock.calls).toEqual([[TIMEOUT_LINE]]);
806
+ expect(dispose).toHaveBeenCalledTimes(1);
807
+ recordDecodes([]);
808
+ await expect(decodeVideoThumbnail(clip(6), signal)).resolves.toBeDefined();
809
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(2);
810
+ });
811
+
812
+ it("keeps the reader of a slow open that finished, when the clip that waited for it times out", async () => {
813
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
814
+ let open!: () => void;
815
+ const opening = new Promise<void>((resolve) => (open = resolve));
816
+ const track = await input.getPrimaryVideoTrack();
817
+ input.getPrimaryVideoTrack.mockClear();
818
+ input.getPrimaryVideoTrack.mockImplementationOnce(async () => (await opening, track));
819
+ canvasesAtTimestamps.mockImplementationOnce(async function* () {
820
+ await new Promise(() => {});
821
+ yield null;
822
+ });
823
+ const controller = new AbortController();
824
+ void decodeVideoThumbnail(clip(0), controller.signal).catch(() => {});
825
+ open();
826
+ await vi.waitFor(() => expect(canvasesAtTimestamps).toHaveBeenCalledOnce());
827
+ controller.abort(timedOut());
828
+
829
+ recordDecodes([]);
830
+ await decodeVideoThumbnail(clip(6), signal);
831
+ expect(warn).not.toHaveBeenCalled();
832
+ expect(dispose).not.toHaveBeenCalled();
833
+ expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(1);
834
+ });
835
+
836
+ it("lets a reader close when idle while a cancelled job is still stuck in a read on it", async () => {
837
+ vi.useFakeTimers();
838
+ try {
839
+ const budgets = resolveTimelineViewportBudgets({ thumbnailSourceIdleMs: 1_000 });
840
+ const stalled = untilDisposed();
841
+ canvasesAtTimestamps.mockImplementationOnce(async function* () {
842
+ yield await stalled;
843
+ });
844
+ const controller = new AbortController();
845
+ const stuck = decodeVideoThumbnail(clip(0), controller.signal, budgets);
846
+ stuck.catch(() => {});
847
+ await vi.waitFor(() => expect(canvasesAtTimestamps).toHaveBeenCalledOnce());
848
+ controller.abort();
849
+ await vi.advanceTimersByTimeAsync(1_000);
850
+ expect(dispose).toHaveBeenCalledTimes(1);
851
+ await expect(stuck).rejects.toThrow("Input disposed");
852
+ } finally {
853
+ vi.useRealTimers();
854
+ }
855
+ });
856
+
857
+ it("drops a reader for one clip's timeout only once the clip still decoding on it ends", async () => {
858
+ vi.spyOn(console, "warn").mockImplementation(() => {});
859
+ recordDecodes([]);
860
+ await decodeVideoThumbnail(clip(0), signal);
861
+ let go!: () => void;
862
+ const healthy = new Promise<void>((resolve) => (go = resolve));
863
+ canvasesAtTimestamps.mockImplementationOnce(async function* () {
864
+ await new Promise(() => {});
865
+ yield null;
866
+ });
867
+ canvasesAtTimestamps.mockImplementationOnce(async function* (
868
+ timestamps: AsyncIterable<number>,
869
+ ) {
870
+ for await (const _time of timestamps) {
871
+ await healthy;
872
+ yield { canvas: document.createElement("canvas") };
873
+ }
874
+ });
875
+ const controller = new AbortController();
876
+ void decodeVideoThumbnail(clip(6), controller.signal).catch(() => {});
877
+ const decoding = decodeVideoThumbnail(clip(12), signal);
878
+ await vi.waitFor(() => expect(canvasesAtTimestamps).toHaveBeenCalledTimes(3));
879
+ controller.abort(timedOut());
880
+ expect(dispose).not.toHaveBeenCalled();
881
+
882
+ go();
883
+ await expect(decoding).resolves.toMatchObject({ value: { kind: "filmstrip" } });
884
+ expect(dispose).toHaveBeenCalledTimes(1);
885
+ });
886
+
887
+ it("keeps the reader, and the clip decoding on it, when another clip on it times out", async () => {
888
+ let unstick!: () => void;
889
+ const stuck = new Promise<void>((resolve) => (unstick = resolve));
890
+ let go!: () => void;
891
+ const healthy = new Promise<void>((resolve) => (go = resolve));
892
+ canvasesAtTimestamps.mockImplementationOnce(async function* () {
893
+ yield await stuck;
894
+ });
895
+ canvasesAtTimestamps.mockImplementationOnce(async function* (
896
+ timestamps: AsyncIterable<number>,
897
+ ) {
898
+ for await (const _time of timestamps) {
899
+ await healthy;
900
+ yield { canvas: document.createElement("canvas") };
901
+ }
902
+ });
903
+ const timedOut = new AbortController();
904
+ const first = decodeVideoThumbnail(clip(0), timedOut.signal);
905
+ first.catch(() => {});
906
+ const second = decodeVideoThumbnail(clip(6), signal);
907
+ await vi.waitFor(() => expect(canvasesAtTimestamps).toHaveBeenCalledTimes(2));
908
+ timedOut.abort(new DOMException("Thumbnail load timed out", "TimeoutError"));
909
+ go();
910
+
911
+ await expect(second).resolves.toMatchObject({ value: { kind: "filmstrip" } });
912
+ expect(dispose).not.toHaveBeenCalled();
913
+ unstick();
914
+ await expect(first).rejects.toMatchObject({ name: "AbortError" });
915
+ });
916
+
917
+ it("drops a reader whose reads gave up, so the next clip retries on a fresh one", async () => {
918
+ vi.spyOn(console, "warn").mockImplementation(() => {});
919
+ sourceRetryRules.length = 0;
920
+ canvasesAtTimestamps.mockImplementationOnce(() => {
921
+ throw new MediaFetchError("thumbnail", null, "TypeError");
922
+ });
923
+ await expect(decodeVideoThumbnail(clip(0), signal)).rejects.toBeInstanceOf(MediaFetchError);
924
+ expect(dispose).toHaveBeenCalledTimes(1);
925
+ recordDecodes([]);
926
+ await decodeVideoThumbnail(clip(6), signal);
927
+ // A UrlSource counts its own retries (mediaUrlSourceOptions), so a fresh one retries afresh.
928
+ expect(sourceRetryRules).toEqual(["function", "function"]);
929
+ });
930
+ });