@hyperframes/studio 0.8.211 → 0.8.213
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.
- package/dist/assets/{hyperframes-player-CzrqgMUX.js → hyperframes-player-Bb6n05RB.js} +1 -1
- package/dist/assets/{index-B3SPHPi1.js → index-DOtf09XR.js} +1 -1
- package/dist/assets/{index-k4nVfS2D.js → index-pq574E5O.js} +119 -119
- package/dist/{chunk-HD6NXWGI.js → chunk-UNZTUFD7.js} +2 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.html +1 -1
- package/dist/index.js +1078 -849
- package/dist/player.js +1 -1
- package/package.json +8 -8
- package/src/hooks/timelineEditingHelpers.test.ts +25 -0
- package/src/hooks/timelineEditingHelpers.ts +17 -1
- package/src/hooks/usePersistentEditHistory.test.ts +7 -3
- package/src/player/hooks/previewMessageRouter.test.ts +40 -0
- package/src/player/hooks/previewMessageRouter.ts +12 -0
- package/src/player/lib/mediaFetch.test.ts +106 -0
- package/src/player/lib/mediaFetch.ts +15 -4
- package/src/player/lib/thumbnailDiskCache.test.ts +158 -0
- package/src/player/lib/thumbnailDiskCache.ts +212 -0
- package/src/player/lib/thumbnailSourcePool.ts +3 -1
- package/src/player/lib/thumbnailVideoDecoder.test.ts +79 -0
- package/src/player/lib/thumbnailVideoDecoder.ts +97 -3
- package/src/player/store/previewQualitySlice.ts +5 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import type { SourceInfo } from "./thumbnailSourcePool";
|
|
2
|
+
|
|
3
|
+
/** A video as the server serves it now: its URL and the ETag of this version of its file. */
|
|
4
|
+
export interface StoredSource {
|
|
5
|
+
source: string;
|
|
6
|
+
etag: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface StoredFrame {
|
|
10
|
+
blob: Blob;
|
|
11
|
+
decodedAt: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const CACHE_PREFIX = "hf-thumbnails:";
|
|
15
|
+
const INDEX_KEY = "hf-thumbnails-index";
|
|
16
|
+
const ENTRY_ORIGIN = "https://hf-thumbnail.invalid/";
|
|
17
|
+
export const STORED_THUMBNAIL_BYTES = 256 * 1024 * 1024;
|
|
18
|
+
|
|
19
|
+
interface Index {
|
|
20
|
+
etags: Record<string, string>;
|
|
21
|
+
caches: Record<string, { usedAt: number; bytes: number }>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let index: Index | undefined;
|
|
25
|
+
// caches this tab dropped since its last save, which another tab's older snapshot must not bring back
|
|
26
|
+
const dropped = new Set<string>();
|
|
27
|
+
let saveTimer: ReturnType<typeof setTimeout> | undefined;
|
|
28
|
+
let warned = false;
|
|
29
|
+
|
|
30
|
+
const available = () => typeof caches !== "undefined" && typeof crypto?.subtle !== "undefined";
|
|
31
|
+
|
|
32
|
+
/** Disk is a shortcut, never a reason a thumbnail fails: a broken store says so once and is skipped. */
|
|
33
|
+
async function quietly<T>(work: () => Promise<T>): Promise<T | undefined> {
|
|
34
|
+
if (!available()) return undefined;
|
|
35
|
+
try {
|
|
36
|
+
return await work();
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (!warned) console.warn("[Studio] timeline thumbnails stay in memory:", error);
|
|
39
|
+
warned = true;
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function digest(text: string): Promise<string> {
|
|
45
|
+
const bytes = new Uint8Array(
|
|
46
|
+
await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text)),
|
|
47
|
+
);
|
|
48
|
+
return Array.from(bytes.subarray(0, 12), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const cacheNameOf = async ({ source, etag }: StoredSource) =>
|
|
52
|
+
`${CACHE_PREFIX}${await digest(source)}:${await digest(etag)}`;
|
|
53
|
+
|
|
54
|
+
function savedIndex(): Index | undefined {
|
|
55
|
+
try {
|
|
56
|
+
const saved = JSON.parse(localStorage.getItem(INDEX_KEY) ?? "null") as Index | null;
|
|
57
|
+
return saved?.etags && saved.caches ? saved : undefined;
|
|
58
|
+
} catch {
|
|
59
|
+
return undefined; // expected: no storage (a private window) or a value this version can't read
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function loadIndex(): Index {
|
|
64
|
+
return (index ??= savedIndex() ?? { etags: {}, caches: {} });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
type CacheEntry = Index["caches"][string];
|
|
68
|
+
|
|
69
|
+
/** Another tab's entry when it was used later than this tab's, with the larger count of bytes. */
|
|
70
|
+
function laterOf(own: CacheEntry | undefined, theirs: CacheEntry): CacheEntry | undefined {
|
|
71
|
+
if (own && own.usedAt >= theirs.usedAt) return undefined;
|
|
72
|
+
return { usedAt: theirs.usedAt, bytes: Math.max(theirs.bytes, own?.bytes ?? 0) };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** This tab's index with what other tabs saved since: each cache at its latest use, none this tab dropped. */
|
|
76
|
+
function mergedIndex(): Index {
|
|
77
|
+
const mine = loadIndex();
|
|
78
|
+
const theirs = savedIndex() ?? { etags: {}, caches: {} };
|
|
79
|
+
for (const [name, entry] of Object.entries(theirs.caches)) {
|
|
80
|
+
const later = dropped.has(name) ? undefined : laterOf(mine.caches[name], entry);
|
|
81
|
+
if (later) mine.caches[name] = later;
|
|
82
|
+
}
|
|
83
|
+
mine.etags = { ...theirs.etags, ...mine.etags };
|
|
84
|
+
dropped.clear();
|
|
85
|
+
return mine;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function saveIndexSoon(): void {
|
|
89
|
+
saveTimer ??= setTimeout(() => {
|
|
90
|
+
saveTimer = undefined;
|
|
91
|
+
try {
|
|
92
|
+
localStorage.setItem(INDEX_KEY, JSON.stringify(mergedIndex()));
|
|
93
|
+
} catch {
|
|
94
|
+
// expected: storage refused; the caches still hold the frames, only their order of use is lost
|
|
95
|
+
}
|
|
96
|
+
}, 1000);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function used(name: string, addedBytes = 0): void {
|
|
100
|
+
const caches = loadIndex().caches;
|
|
101
|
+
caches[name] = { usedAt: Date.now(), bytes: (caches[name]?.bytes ?? 0) + addedBytes };
|
|
102
|
+
saveIndexSoon();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The version stored for `source`, when one conditional one-byte read says the server still has it (304). */
|
|
106
|
+
export const storedVersion = (source: string) =>
|
|
107
|
+
quietly(async (): Promise<StoredSource | undefined> => {
|
|
108
|
+
const etag = loadIndex().etags[await digest(source)];
|
|
109
|
+
if (!etag) return undefined;
|
|
110
|
+
// oxlint-disable-next-line no-restricted-globals -- one validation read, on the media's own route like mediabunny's
|
|
111
|
+
const response = await fetch(source, {
|
|
112
|
+
headers: { Range: "bytes=0-0", "If-None-Match": etag },
|
|
113
|
+
});
|
|
114
|
+
await response.body?.cancel();
|
|
115
|
+
return response.status === 304 ? { source, etag } : undefined;
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
/** Records the version the server now has, dropping what was stored for the source's earlier versions. */
|
|
119
|
+
export const rememberVersion = (stored: StoredSource) =>
|
|
120
|
+
quietly(async () => {
|
|
121
|
+
const key = await digest(stored.source);
|
|
122
|
+
const saved = loadIndex();
|
|
123
|
+
if (saved.etags[key] === stored.etag) return;
|
|
124
|
+
saved.etags[key] = stored.etag;
|
|
125
|
+
saveIndexSoon();
|
|
126
|
+
const keep = await cacheNameOf(stored);
|
|
127
|
+
for (const name of await caches.keys()) {
|
|
128
|
+
if (!name.startsWith(`${CACHE_PREFIX}${key}:`) || name === keep) continue;
|
|
129
|
+
await caches.delete(name);
|
|
130
|
+
delete saved.caches[name];
|
|
131
|
+
dropped.add(name);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
async function entry(stored: StoredSource, path: string) {
|
|
136
|
+
const name = await cacheNameOf(stored);
|
|
137
|
+
return { name, cache: await caches.open(name), url: `${ENTRY_ORIGIN}${path}` };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** A read opens nothing: opening a cache creates it, and an empty one for a gone version would never be dropped. */
|
|
141
|
+
async function storedEntry(stored: StoredSource, path: string) {
|
|
142
|
+
const name = await cacheNameOf(stored);
|
|
143
|
+
return (await caches.has(name)) ? entry(stored, path) : undefined;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export const readStoredInfo = (stored: StoredSource) =>
|
|
147
|
+
quietly(async (): Promise<SourceInfo | undefined> => {
|
|
148
|
+
const found = await storedEntry(stored, "info");
|
|
149
|
+
if (!found) return undefined;
|
|
150
|
+
const { name, cache, url } = found;
|
|
151
|
+
const info = (await (await cache.match(url))?.json()) as SourceInfo | undefined;
|
|
152
|
+
if (!(info && info.aspect > 0)) return undefined;
|
|
153
|
+
used(name);
|
|
154
|
+
return info;
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
export const storeInfo = (stored: StoredSource, info: SourceInfo) =>
|
|
158
|
+
quietly(async () => {
|
|
159
|
+
const { name, cache, url } = await entry(stored, "info");
|
|
160
|
+
await cache.put(url, new Response(JSON.stringify(info)));
|
|
161
|
+
used(name);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
/** `frame` names one picture of the source: its fit, size and the time it was asked for. */
|
|
165
|
+
export const readStoredFrame = (stored: StoredSource, frame: string) =>
|
|
166
|
+
quietly(async (): Promise<StoredFrame | undefined> => {
|
|
167
|
+
const found = await storedEntry(stored, `frame?${encodeURIComponent(frame)}`);
|
|
168
|
+
if (!found) return undefined;
|
|
169
|
+
const { name, cache, url } = found;
|
|
170
|
+
const response = await cache.match(url);
|
|
171
|
+
const decodedAt = Number(response?.headers.get("x-decoded-at"));
|
|
172
|
+
if (!response || !Number.isFinite(decodedAt)) return undefined;
|
|
173
|
+
used(name);
|
|
174
|
+
return { blob: await response.blob(), decodedAt };
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
export const storeFrame = (stored: StoredSource, frame: string, decodedAt: number, blob: Blob) =>
|
|
178
|
+
quietly(async () => {
|
|
179
|
+
const { name, cache, url } = await entry(stored, `frame?${encodeURIComponent(frame)}`);
|
|
180
|
+
const replaced = Number((await cache.match(url))?.headers.get("x-bytes")) || 0;
|
|
181
|
+
const headers = {
|
|
182
|
+
"content-type": blob.type,
|
|
183
|
+
"x-decoded-at": String(decodedAt),
|
|
184
|
+
"x-bytes": String(blob.size),
|
|
185
|
+
};
|
|
186
|
+
await cache.put(url, new Response(blob, { headers }));
|
|
187
|
+
used(name, blob.size - replaced);
|
|
188
|
+
await dropLeastUsed(STORED_THUMBNAIL_BYTES);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
/** Whole sources go, least recently used first, until what is stored fits `budgetBytes`. */
|
|
192
|
+
async function dropLeastUsed(budgetBytes: number): Promise<void> {
|
|
193
|
+
const saved = loadIndex();
|
|
194
|
+
let total = Object.values(saved.caches).reduce((sum, { bytes }) => sum + bytes, 0);
|
|
195
|
+
const oldestFirst = Object.entries(saved.caches).sort(([, a], [, b]) => a.usedAt - b.usedAt);
|
|
196
|
+
for (const [name, { bytes }] of oldestFirst) {
|
|
197
|
+
if (total <= budgetBytes) break;
|
|
198
|
+
await caches.delete(name);
|
|
199
|
+
delete saved.caches[name];
|
|
200
|
+
dropped.add(name);
|
|
201
|
+
total -= bytes;
|
|
202
|
+
}
|
|
203
|
+
saveIndexSoon();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Tests only: forget the in-memory index and any pending save. */
|
|
207
|
+
export function resetStoredThumbnailsForTests(): void {
|
|
208
|
+
clearTimeout(saveTimer);
|
|
209
|
+
saveTimer = undefined;
|
|
210
|
+
index = undefined;
|
|
211
|
+
warned = false;
|
|
212
|
+
}
|
|
@@ -12,6 +12,7 @@ interface OpenedSource {
|
|
|
12
12
|
mediabunny: Mediabunny;
|
|
13
13
|
track: InputVideoTrack;
|
|
14
14
|
info: SourceInfo;
|
|
15
|
+
etag: string | undefined;
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
export interface ThumbnailSource extends OpenedSource {
|
|
@@ -88,7 +89,8 @@ async function open(source: string, pooled: () => PooledSource): Promise<OpenedS
|
|
|
88
89
|
}
|
|
89
90
|
const metadataDuration = await track.getDurationFromMetadata({ skipLiveWait: true });
|
|
90
91
|
live();
|
|
91
|
-
|
|
92
|
+
const info = { aspect: displayWidth / displayHeight, metadataDuration };
|
|
93
|
+
return { mediabunny, track, info, etag: options.etagOf() };
|
|
92
94
|
}
|
|
93
95
|
|
|
94
96
|
function pooledSource(source: string, version: string | undefined): PooledSource {
|
|
@@ -16,6 +16,8 @@ const input = {
|
|
|
16
16
|
getPrimaryVideoTrack: vi.fn(),
|
|
17
17
|
dispose,
|
|
18
18
|
};
|
|
19
|
+
// The ETag the mocked server answers the reader's reads with; none unless a test sets one.
|
|
20
|
+
let servedEtag: string | undefined;
|
|
19
21
|
|
|
20
22
|
vi.mock("mediabunny", () => ({
|
|
21
23
|
ALL_FORMATS: {},
|
|
@@ -25,6 +27,7 @@ vi.mock("mediabunny", () => ({
|
|
|
25
27
|
readonly options?: { getRetryDelay?: unknown; cancelReads?: () => void },
|
|
26
28
|
) {
|
|
27
29
|
sourceRetryRules.push(typeof options?.getRetryDelay);
|
|
30
|
+
if (servedEtag && options) Object.assign(options, { etagOf: () => servedEtag });
|
|
28
31
|
// Spied where the pool reads it, after constructing the source.
|
|
29
32
|
if (options?.cancelReads)
|
|
30
33
|
cancelledReads.push((options.cancelReads = vi.fn(options.cancelReads)));
|
|
@@ -69,6 +72,8 @@ let sources = 0;
|
|
|
69
72
|
let source = "";
|
|
70
73
|
|
|
71
74
|
beforeEach(() => {
|
|
75
|
+
vi.unstubAllGlobals();
|
|
76
|
+
servedEtag = undefined;
|
|
72
77
|
source = `/clip-${++sources}.mp4`;
|
|
73
78
|
// Released frames outlive their test; evict them so each test counts only its own revokes.
|
|
74
79
|
releaseSharedFrames([], 0);
|
|
@@ -109,6 +114,26 @@ describe("videoThumbnailTimestamps", () => {
|
|
|
109
114
|
});
|
|
110
115
|
});
|
|
111
116
|
|
|
117
|
+
/** Cache Storage as a Map of Maps, empty, with an empty index: what a browser keeps across sessions. */
|
|
118
|
+
function stubCacheStorage(): Map<string, Map<string, Response>> {
|
|
119
|
+
const stores = new Map<string, Map<string, Response>>();
|
|
120
|
+
vi.stubGlobal("caches", {
|
|
121
|
+
open: async (name: string) => {
|
|
122
|
+
const store = stores.get(name) ?? new Map<string, Response>();
|
|
123
|
+
stores.set(name, store);
|
|
124
|
+
return {
|
|
125
|
+
match: async (url: string) => store.get(url)?.clone(),
|
|
126
|
+
put: async (url: string, response: Response) => void store.set(url, response),
|
|
127
|
+
};
|
|
128
|
+
},
|
|
129
|
+
has: async (name: string) => stores.has(name),
|
|
130
|
+
keys: async () => [...stores.keys()],
|
|
131
|
+
delete: async (name: string) => stores.delete(name),
|
|
132
|
+
});
|
|
133
|
+
localStorage.clear();
|
|
134
|
+
return stores;
|
|
135
|
+
}
|
|
136
|
+
|
|
112
137
|
describe("decodeVideoThumbnail", () => {
|
|
113
138
|
it("extracts sparse frames, returns object URLs, and disposes once", async () => {
|
|
114
139
|
const decoded: number[][] = [];
|
|
@@ -927,4 +952,58 @@ describe("decodeVideoThumbnail's shared source", () => {
|
|
|
927
952
|
// A UrlSource counts its own retries (mediaUrlSourceOptions), so a fresh one retries afresh.
|
|
928
953
|
expect(sourceRetryRules).toEqual(["function", "function"]);
|
|
929
954
|
});
|
|
955
|
+
|
|
956
|
+
it("paints a strip from a past session's frames on disk, without opening the file", async () => {
|
|
957
|
+
const stores = stubCacheStorage();
|
|
958
|
+
const signal = new AbortController().signal;
|
|
959
|
+
servedEtag = '"take-v1"';
|
|
960
|
+
recordDecodes([]);
|
|
961
|
+
const request = { source, sourceStart: 0, sourceRangeDuration: 4, frameCount: 2 };
|
|
962
|
+
await decodeVideoThumbnail({ ...request, contentVersion: "session-1" }, signal);
|
|
963
|
+
// the source's facts and its three frames, then the index the next session reads
|
|
964
|
+
await vi.waitFor(() => expect([...stores.values()][0]?.size).toBe(4));
|
|
965
|
+
await vi.waitFor(
|
|
966
|
+
() => expect(localStorage.getItem("hf-thumbnails-index")).toContain("take-v1"),
|
|
967
|
+
{
|
|
968
|
+
timeout: 3000,
|
|
969
|
+
},
|
|
970
|
+
);
|
|
971
|
+
|
|
972
|
+
vi.resetModules();
|
|
973
|
+
const nextSession = await import("./thumbnailVideoDecoder");
|
|
974
|
+
const validate = vi.fn(async () => new Response(null, { status: 304 }));
|
|
975
|
+
vi.stubGlobal("fetch", validate);
|
|
976
|
+
input.getPrimaryVideoTrack.mockClear();
|
|
977
|
+
canvasesAtTimestamps.mockClear();
|
|
978
|
+
const result = await nextSession.decodeVideoThumbnail(
|
|
979
|
+
{ ...request, contentVersion: "session-2" },
|
|
980
|
+
signal,
|
|
981
|
+
);
|
|
982
|
+
|
|
983
|
+
expect(validate).toHaveBeenCalledTimes(1);
|
|
984
|
+
expect(input.getPrimaryVideoTrack).not.toHaveBeenCalled();
|
|
985
|
+
expect(canvasesAtTimestamps).not.toHaveBeenCalled();
|
|
986
|
+
expect(result.value).toMatchObject({ kind: "filmstrip" });
|
|
987
|
+
expect((result.value as { urls: string[] }).urls).toHaveLength(3);
|
|
988
|
+
});
|
|
989
|
+
|
|
990
|
+
it("checks the file again when a project opens anew, so a replaced video never paints its old frames", async () => {
|
|
991
|
+
const stores = stubCacheStorage();
|
|
992
|
+
const signal = new AbortController().signal;
|
|
993
|
+
servedEtag = '"v1"';
|
|
994
|
+
recordDecodes([]);
|
|
995
|
+
const request = { source, sourceStart: 0, sourceRangeDuration: 4, frameCount: 2 };
|
|
996
|
+
await decodeVideoThumbnail({ ...request, contentVersion: "open-1" }, signal);
|
|
997
|
+
await vi.waitFor(() => expect([...stores.values()][0]?.size).toBe(4));
|
|
998
|
+
|
|
999
|
+
// the video is replaced at the same path, then its project opened again in this page
|
|
1000
|
+
servedEtag = '"v2"';
|
|
1001
|
+
vi.stubGlobal(
|
|
1002
|
+
"fetch",
|
|
1003
|
+
vi.fn(async () => new Response("x", { status: 206, headers: { ETag: '"v2"' } })),
|
|
1004
|
+
);
|
|
1005
|
+
await decodeVideoThumbnail({ ...request, contentVersion: "open-2" }, signal);
|
|
1006
|
+
|
|
1007
|
+
expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(2);
|
|
1008
|
+
});
|
|
930
1009
|
});
|
|
@@ -17,6 +17,15 @@ import {
|
|
|
17
17
|
takeSharedFrame,
|
|
18
18
|
type HeldFrame,
|
|
19
19
|
} from "./thumbnailFrameCache";
|
|
20
|
+
import {
|
|
21
|
+
readStoredFrame,
|
|
22
|
+
readStoredInfo,
|
|
23
|
+
rememberVersion,
|
|
24
|
+
storedVersion,
|
|
25
|
+
storeFrame,
|
|
26
|
+
storeInfo,
|
|
27
|
+
type StoredSource,
|
|
28
|
+
} from "./thumbnailDiskCache";
|
|
20
29
|
|
|
21
30
|
export interface VideoThumbnailDecodeRequest {
|
|
22
31
|
source: string;
|
|
@@ -52,6 +61,17 @@ interface FrameWindow {
|
|
|
52
61
|
}
|
|
53
62
|
|
|
54
63
|
const sourceInfos = new Map<string, { version: string | undefined; info: SourceInfo }>();
|
|
64
|
+
const storedVersions = new Map<string, Promise<StoredSource | undefined>>();
|
|
65
|
+
const storedInfos = new Set<string>();
|
|
66
|
+
|
|
67
|
+
const openOf = (request: VideoThumbnailDecodeRequest) =>
|
|
68
|
+
`${request.source}\u0000${request.contentVersion ?? ""}`;
|
|
69
|
+
|
|
70
|
+
function storedVersionOf(request: VideoThumbnailDecodeRequest): Promise<StoredSource | undefined> {
|
|
71
|
+
let pending = storedVersions.get(openOf(request));
|
|
72
|
+
if (!pending) storedVersions.set(openOf(request), (pending = storedVersion(request.source)));
|
|
73
|
+
return pending;
|
|
74
|
+
}
|
|
55
75
|
|
|
56
76
|
const accepts = (window: FrameWindow, time: number, decodedAt: number) =>
|
|
57
77
|
decodedAt >= window.sourceStart && time - decodedAt <= (time >= window.end ? 0 : window.lead);
|
|
@@ -79,6 +99,7 @@ interface DecodedResources {
|
|
|
79
99
|
held: HeldFrame[];
|
|
80
100
|
canvases: Set<HTMLCanvasElement | OffscreenCanvas>;
|
|
81
101
|
releasedFrameBytes: number;
|
|
102
|
+
store?: (slot: number, decodedAt: number, blob: Blob) => void;
|
|
82
103
|
}
|
|
83
104
|
|
|
84
105
|
interface ThumbnailCanvasSink {
|
|
@@ -186,6 +207,7 @@ async function decodeFrames(
|
|
|
186
207
|
throwIfAborted(signal);
|
|
187
208
|
const frame = shareFrame(target.key, target.decodedAt, URL.createObjectURL(blob), blob.size);
|
|
188
209
|
progress.framesFromAnyJob += 1;
|
|
210
|
+
resources.store?.(target.slot, target.decodedAt, blob);
|
|
189
211
|
resources.urls[target.slot] = frame.url;
|
|
190
212
|
resources.held.push({ key: target.key, frame });
|
|
191
213
|
}
|
|
@@ -218,6 +240,64 @@ function loadedResult(
|
|
|
218
240
|
};
|
|
219
241
|
}
|
|
220
242
|
|
|
243
|
+
const storedFrameName =
|
|
244
|
+
(fit: string, info: SourceInfo, budgets: Readonly<TimelineViewportBudgets>) => (time: number) => {
|
|
245
|
+
const { width, height } = targetDimensions(info.aspect, budgets);
|
|
246
|
+
return `${fit} ${width}x${height} ${time}`;
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
async function takeStoredFrames(
|
|
250
|
+
{ timestamps, window }: ReturnType<typeof stripTimes>,
|
|
251
|
+
slots: number[],
|
|
252
|
+
keyOf: (time: number) => string,
|
|
253
|
+
nameOf: (time: number) => string,
|
|
254
|
+
resources: DecodedResources,
|
|
255
|
+
stored: StoredSource,
|
|
256
|
+
): Promise<number[]> {
|
|
257
|
+
const found = await Promise.all(
|
|
258
|
+
slots.map((slot) => readStoredFrame(stored, nameOf(timestamps[slot]!))),
|
|
259
|
+
);
|
|
260
|
+
return slots.filter((slot, i) => {
|
|
261
|
+
const time = timestamps[slot]!;
|
|
262
|
+
const hit = found[i];
|
|
263
|
+
if (!hit || !accepts(window, time, hit.decodedAt)) return true;
|
|
264
|
+
const key = keyOf(time);
|
|
265
|
+
const frame = shareFrame(key, hit.decodedAt, URL.createObjectURL(hit.blob), hit.blob.size);
|
|
266
|
+
resources.urls[slot] = frame.url;
|
|
267
|
+
resources.held.push({ key, frame });
|
|
268
|
+
return false;
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function versionRead(
|
|
273
|
+
request: VideoThumbnailDecodeRequest,
|
|
274
|
+
source: ThumbnailSource,
|
|
275
|
+
): StoredSource | undefined {
|
|
276
|
+
if (!source.etag) return undefined;
|
|
277
|
+
const version = { source: request.source, etag: source.etag };
|
|
278
|
+
storedVersions.set(openOf(request), Promise.resolve(version));
|
|
279
|
+
return version;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function storeForNextSession(
|
|
283
|
+
request: VideoThumbnailDecodeRequest,
|
|
284
|
+
info: SourceInfo,
|
|
285
|
+
budgets: Readonly<TimelineViewportBudgets>,
|
|
286
|
+
resources: DecodedResources,
|
|
287
|
+
version: StoredSource | undefined,
|
|
288
|
+
): void {
|
|
289
|
+
if (!version) return;
|
|
290
|
+
const id = `${version.source}\u0000${version.etag}`;
|
|
291
|
+
if (!storedInfos.has(id)) {
|
|
292
|
+
storedInfos.add(id);
|
|
293
|
+
void rememberVersion(version).then(() => storeInfo(version, info));
|
|
294
|
+
}
|
|
295
|
+
const nameOf = storedFrameName(request.fit ?? "cover", info, budgets);
|
|
296
|
+
const { timestamps } = stripTimes(request, info, budgets);
|
|
297
|
+
resources.store = (slot, decodedAt, blob) =>
|
|
298
|
+
void storeFrame(version, nameOf(timestamps[slot]!), decodedAt, blob);
|
|
299
|
+
}
|
|
300
|
+
|
|
221
301
|
/** Sparse Mediabunny extraction with one pooled canvas and one cleanup owner. */
|
|
222
302
|
export async function decodeVideoThumbnail(
|
|
223
303
|
request: VideoThumbnailDecodeRequest,
|
|
@@ -234,16 +314,28 @@ export async function decodeVideoThumbnail(
|
|
|
234
314
|
releasedFrameBytes: budgets.sharedFrameCacheBytes,
|
|
235
315
|
};
|
|
236
316
|
const entry = sourceInfos.get(request.source);
|
|
237
|
-
const known = entry?.version === request.contentVersion ? entry?.info : undefined;
|
|
238
317
|
try {
|
|
318
|
+
const stored = await storedVersionOf(request);
|
|
319
|
+
const known =
|
|
320
|
+
entry && entry.version === request.contentVersion
|
|
321
|
+
? entry.info
|
|
322
|
+
: stored && (await readStoredInfo(stored));
|
|
239
323
|
if (known) {
|
|
240
|
-
|
|
324
|
+
sourceInfos.set(request.source, { version: request.contentVersion, info: known });
|
|
325
|
+
const strip = stripTimes(request, known, budgets);
|
|
326
|
+
const missing = takeDecodedFrames(strip, keyOf, resources);
|
|
327
|
+
const nameOf = storedFrameName(fit, known, budgets);
|
|
328
|
+
const left =
|
|
329
|
+
stored && missing.length > 0
|
|
330
|
+
? await takeStoredFrames(strip, missing, keyOf, nameOf, resources, stored)
|
|
331
|
+
: missing;
|
|
332
|
+
if (left.length === 0) {
|
|
241
333
|
throwIfAborted(signal);
|
|
242
334
|
return loadedResult(resources, known.aspect, budgets);
|
|
243
335
|
}
|
|
244
336
|
releaseDecodedResources(resources);
|
|
245
337
|
}
|
|
246
|
-
return await decodeMissingFrames(request, signal, budgets, keyOf, resources);
|
|
338
|
+
return await decodeMissingFrames(request, signal, budgets, keyOf, resources, stored);
|
|
247
339
|
} catch (error) {
|
|
248
340
|
releaseDecodedResources(resources);
|
|
249
341
|
reportMediaFetchFailure(error);
|
|
@@ -257,6 +349,7 @@ async function decodeMissingFrames(
|
|
|
257
349
|
budgets: Readonly<TimelineViewportBudgets>,
|
|
258
350
|
keyOf: (time: number) => string,
|
|
259
351
|
resources: DecodedResources,
|
|
352
|
+
stored: StoredSource | undefined,
|
|
260
353
|
): Promise<ThumbnailLoadedResult> {
|
|
261
354
|
let source: ThumbnailSource;
|
|
262
355
|
try {
|
|
@@ -284,6 +377,7 @@ async function decodeMissingFrames(
|
|
|
284
377
|
sourceInfos.set(request.source, { version: request.contentVersion, info });
|
|
285
378
|
const strip = stripTimes(request, info, budgets);
|
|
286
379
|
const { timestamps, window } = strip;
|
|
380
|
+
storeForNextSession(request, info, budgets, resources, stored ?? versionRead(request, source));
|
|
287
381
|
const slots: { slot: number; key: string; decodedAt?: number }[] = takeDecodedFrames(
|
|
288
382
|
strip,
|
|
289
383
|
keyOf,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { StoreApi } from "zustand";
|
|
2
2
|
import { isPreviewQuality, type PreviewQuality } from "@hyperframes/core/preview-quality";
|
|
3
|
+
import type { MediaLoadFailure } from "@hyperframes/core/runtime/protocol";
|
|
3
4
|
import { readStudioUiPreferences, writeStudioUiPreferences } from "../../utils/studioUiPreferences";
|
|
4
5
|
|
|
5
6
|
/** The lighter copies of heavy videos the preview wants at its current quality. */
|
|
@@ -53,6 +54,8 @@ export interface PreviewQualitySlice {
|
|
|
53
54
|
setPlaybackHealth: (health: PlaybackHealth | null) => void;
|
|
54
55
|
previewFacts: PreviewFacts | null;
|
|
55
56
|
setPreviewFacts: (facts: PreviewFacts | null) => void;
|
|
57
|
+
mediaLoadFailure: MediaLoadFailure | null;
|
|
58
|
+
setMediaLoadFailure: (failure: MediaLoadFailure | null) => void;
|
|
56
59
|
/** Bumped by requestPreviewFacts; the preview owner forwards each bump and the answer lands in previewFacts. */
|
|
57
60
|
previewFactsRequests: number;
|
|
58
61
|
requestPreviewFacts: () => void;
|
|
@@ -87,6 +90,8 @@ export function createPreviewQualitySlice(
|
|
|
87
90
|
setPlaybackHealth: (playbackHealth) => set({ playbackHealth }),
|
|
88
91
|
previewFacts: null,
|
|
89
92
|
setPreviewFacts: (previewFacts) => set({ previewFacts }),
|
|
93
|
+
mediaLoadFailure: null,
|
|
94
|
+
setMediaLoadFailure: (mediaLoadFailure) => set({ mediaLoadFailure }),
|
|
90
95
|
previewFactsRequests: 0,
|
|
91
96
|
requestPreviewFacts: () =>
|
|
92
97
|
set((state) => ({ previewFactsRequests: state.previewFactsRequests + 1 })),
|