@alpha2-sh/editor 0.4.6 → 0.5.1
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/browser/bridge.d.ts +15 -16
- package/dist/browser/bridge.js +393 -312
- package/dist/browser/extensions-worker.d.ts +12 -16
- package/dist/browser/extensions-worker.js +17 -24
- package/dist/browser/hash-worker.d.ts +4 -5
- package/dist/browser/hash-worker.js +7 -8
- package/dist/browser/jobs.d.ts +17 -19
- package/dist/browser/jobs.js +41 -28
- package/dist/browser/resources.d.ts +6 -11
- package/dist/browser/resources.js +5 -9
- package/dist/browser/runtime/assets.d.ts +52 -28
- package/dist/browser/runtime/assets.js +136 -45
- package/dist/browser/runtime/audio.d.ts +16 -11
- package/dist/browser/runtime/audio.js +102 -59
- package/dist/browser/runtime/budget.d.ts +4 -5
- package/dist/browser/runtime/budget.js +4 -5
- package/dist/browser/runtime/caption-fonts.d.ts +3 -7
- package/dist/browser/runtime/caption-fonts.js +4 -8
- package/dist/browser/runtime/engine.d.ts +44 -44
- package/dist/browser/runtime/engine.js +87 -64
- package/dist/browser/runtime/export-audio.d.ts +6 -6
- package/dist/browser/runtime/export-audio.js +8 -9
- package/dist/browser/runtime/exporter.d.ts +33 -66
- package/dist/browser/runtime/exporter.js +111 -137
- package/dist/browser/runtime/extension-import.d.ts +28 -35
- package/dist/browser/runtime/extension-import.js +36 -46
- package/dist/browser/runtime/fingerprint.d.ts +11 -14
- package/dist/browser/runtime/fingerprint.js +20 -24
- package/dist/browser/runtime/generators.d.ts +2 -3
- package/dist/browser/runtime/generators.js +5 -7
- package/dist/browser/runtime/locks.d.ts +5 -7
- package/dist/browser/runtime/locks.js +5 -7
- package/dist/browser/runtime/output.d.ts +7 -9
- package/dist/browser/runtime/output.js +17 -21
- package/dist/browser/runtime/preview-utils.js +10 -6
- package/dist/browser/runtime/proxies.d.ts +19 -19
- package/dist/browser/runtime/proxies.js +72 -51
- package/dist/browser/runtime/proxy-storage.d.ts +15 -19
- package/dist/browser/runtime/proxy-storage.js +20 -24
- package/dist/browser/runtime/sources.d.ts +23 -31
- package/dist/browser/runtime/sources.js +85 -103
- package/dist/browser/runtime/staging.d.ts +2 -6
- package/dist/browser/runtime/staging.js +7 -11
- package/dist/browser/runtime/still.d.ts +2 -6
- package/dist/browser/runtime/still.js +6 -12
- package/dist/browser/runtime/text.d.ts +14 -0
- package/dist/browser/runtime/text.js +27 -0
- package/dist/browser/runtime/wasm.d.ts +4 -5
- package/dist/browser/runtime/wasm.js +4 -5
- package/dist/browser/types.d.ts +244 -239
- package/dist/browser/worker.d.ts +10 -10
- package/dist/browser/worker.js +64 -22
- package/dist/core/captions.d.ts +8 -10
- package/dist/core/captions.js +10 -18
- package/dist/core/clip-time.d.ts +8 -10
- package/dist/core/clip-time.js +12 -15
- package/dist/core/commands.d.ts +46 -46
- package/dist/core/commands.js +38 -36
- package/dist/core/compositions.d.ts +78 -112
- package/dist/core/compositions.js +66 -94
- package/dist/core/document.d.ts +59 -92
- package/dist/core/document.js +65 -90
- package/dist/core/keyframes.d.ts +11 -12
- package/dist/core/keyframes.js +11 -13
- package/dist/core/presets.d.ts +14 -20
- package/dist/core/presets.js +12 -18
- package/dist/core/session.d.ts +26 -38
- package/dist/core/session.js +38 -53
- package/dist/core/text.d.ts +2 -3
- package/dist/core/text.js +2 -3
- package/dist/core/timeline-index.d.ts +2 -3
- package/dist/core/timeline-index.js +11 -7
- package/dist/core/transitions.js +4 -5
- package/dist/core/validated.d.ts +6 -7
- package/dist/core/validated.js +8 -11
- package/dist/native-client/index.d.ts +31 -32
- package/dist/native-client/index.js +40 -41
- package/dist/ports/index.d.ts +19 -20
- package/dist/react/index.d.ts +5 -6
- package/dist/react/index.js +5 -6
- package/package.json +4 -4
|
@@ -1,45 +1,42 @@
|
|
|
1
1
|
// Where a browser export's file goes: streamed into a temporary OPFS file (or a user-picked file)
|
|
2
|
-
// so encoded video never accumulates in memory. In-memory output is for small outputs sized
|
|
3
|
-
// front, never a silent fallback for
|
|
2
|
+
// so encoded video never accumulates in memory. In-memory output is only for small outputs sized
|
|
3
|
+
// up front, never a silent fallback for large ones.
|
|
4
4
|
import { estimatedOutputBytes, memoryOutputPeakBytes, MEMORY_OUTPUT_COPIES, MEMORY_OUTPUT_MARGIN, WritableFileDestination, } from "alpha2/export-destination";
|
|
5
5
|
import { containerPolicy } from "alpha2/containers";
|
|
6
6
|
import { ExportFailureError } from "alpha2/export-failure";
|
|
7
7
|
import { holdShared, whenUnheld } from "./locks.js";
|
|
8
|
-
/**
|
|
8
|
+
/** Default peak size of an in-memory output. */
|
|
9
9
|
export const DEFAULT_MEMORY_OUTPUT_LIMIT = 512 * 1024 * 1024;
|
|
10
|
-
/** The origin-private directory temporary export files live in. */
|
|
11
10
|
const EXPORT_DIRECTORY = "alpha2-exports";
|
|
12
11
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* Unheld temporary exports older than this belong to closed pages. Without Web Locks, age alone
|
|
13
|
+
* decides.
|
|
15
14
|
*/
|
|
16
15
|
const STALE_EXPORT_MS = 24 * 60 * 60 * 1000;
|
|
17
16
|
/** The Web Lock a page holds on a temporary export file while it writes or keeps it. */
|
|
18
17
|
const exportLock = (fileName) => `alpha2-export:${fileName}`;
|
|
19
|
-
/** Free storage
|
|
18
|
+
/** Free storage reported as wanted beyond the estimate. */
|
|
20
19
|
const STORAGE_MARGIN = MEMORY_OUTPUT_MARGIN;
|
|
21
|
-
/** Which storage APIs this page has, as an export
|
|
20
|
+
/** Which storage APIs this page has, as an export result reports them. */
|
|
22
21
|
export function storageSupport() {
|
|
23
22
|
const storage = globalThis.navigator?.storage;
|
|
24
23
|
return {
|
|
25
|
-
// Handles gained `createWritable` later than the directory, so that is checked at open.
|
|
24
|
+
// Handles gained `createWritable` later than the directory API, so that is checked at open.
|
|
26
25
|
opfs: typeof storage?.getDirectory === "function",
|
|
27
26
|
savePicker: typeof globalThis.showSaveFilePicker === "function",
|
|
28
27
|
estimate: typeof storage?.estimate === "function",
|
|
29
28
|
};
|
|
30
29
|
}
|
|
31
30
|
/**
|
|
32
|
-
* Opens where an export writes, refusing
|
|
33
|
-
* limit.
|
|
31
|
+
* Opens where an export writes, refusing up front only an in-memory output past its limit.
|
|
34
32
|
*
|
|
35
33
|
* Storage is never refused ahead: `navigator.storage.estimate()` is a guess, and WebKit's usage
|
|
36
34
|
* counts every write since its last disk scan, so `quota - usage` can be near zero on an empty
|
|
37
35
|
* disk. A full disk fails at the write (`STORAGE_QUOTA_EXCEEDED`); the estimate is only reported.
|
|
38
36
|
*
|
|
39
|
-
* `container` (default `mp4`)
|
|
40
|
-
* type.
|
|
37
|
+
* `container` (default `mp4`) sets the temporary file's extension and the artifact's MIME type.
|
|
41
38
|
* Returns `{ mode, destination, storageEstimate, abort, finish }`: `destination` is the sink's,
|
|
42
|
-
* `abort` discards the file, and `finish(sinkOutput)` turns the finished output into
|
|
39
|
+
* `abort` discards the file, and `finish(sinkOutput)` turns the finished output into artifact
|
|
43
40
|
* fields.
|
|
44
41
|
*/
|
|
45
42
|
export async function openExportOutput({ jobId, name, container = "mp4", target, durationSeconds, videoBitrate, audioBitrate, }) {
|
|
@@ -92,7 +89,7 @@ async function openOriginPrivateFile(jobId, name, policy, estimate) {
|
|
|
92
89
|
let handle;
|
|
93
90
|
let writable;
|
|
94
91
|
const fileName = `${Date.now()}-${jobId}.${policy.extensions[0]}`;
|
|
95
|
-
// Held from before the file exists until it is removed, so
|
|
92
|
+
// Held from before the file exists until it is removed, so other pages' sweeps skip it.
|
|
96
93
|
const lease = await holdShared(exportLock(fileName));
|
|
97
94
|
try {
|
|
98
95
|
directory = await (await storage.getDirectory()).getDirectoryHandle(EXPORT_DIRECTORY, { create: true });
|
|
@@ -152,9 +149,8 @@ async function openOriginPrivateFile(jobId, name, policy, estimate) {
|
|
|
152
149
|
};
|
|
153
150
|
}
|
|
154
151
|
/**
|
|
155
|
-
*
|
|
156
|
-
* `
|
|
157
|
-
* `openExportOutput`); `null` where the browser says nothing.
|
|
152
|
+
* The origin's storage report beside this export's estimate, with `belowEstimate` when
|
|
153
|
+
* `quota - usage` is less. Informational only; `null` where the browser reports nothing.
|
|
158
154
|
*/
|
|
159
155
|
async function reportedStorage(storage, estimate) {
|
|
160
156
|
if (typeof storage.estimate !== "function")
|
|
@@ -193,8 +189,8 @@ function openMemory(limit, estimate, { name, mimeType, fellBack }) {
|
|
|
193
189
|
};
|
|
194
190
|
}
|
|
195
191
|
/**
|
|
196
|
-
* Deletes temporary export files left by pages
|
|
197
|
-
*
|
|
192
|
+
* Deletes temporary export files left by closed pages (started over a day ago, held by no page).
|
|
193
|
+
* Resolves to the names removed.
|
|
198
194
|
* @returns {Promise<string[]>}
|
|
199
195
|
*/
|
|
200
196
|
export async function sweepStaleExports(now = Date.now()) {
|
|
@@ -212,7 +208,7 @@ export async function sweepStaleExports(now = Date.now()) {
|
|
|
212
208
|
removed.push(entry);
|
|
213
209
|
}
|
|
214
210
|
catch {
|
|
215
|
-
// No directory yet, or no OPFS
|
|
211
|
+
// No directory yet, or no OPFS, so nothing to sweep.
|
|
216
212
|
}
|
|
217
213
|
return removed;
|
|
218
214
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
//
|
|
1
|
+
import { activeTimeout } from "alpha2/active-clock";
|
|
2
|
+
// Keep the source's display aspect ratio; transparent padding belongs to the clip and takes part in
|
|
3
|
+
// its transform and transition.
|
|
3
4
|
export function containRect(sourceWidth, sourceHeight, width, height) {
|
|
4
5
|
const scale = Math.min(width / sourceWidth, height / sourceHeight);
|
|
5
6
|
const drawWidth = sourceWidth * scale;
|
|
@@ -21,8 +22,9 @@ export async function seekVideoFrame(video, seconds, tolerance = 0.001) {
|
|
|
21
22
|
video.readyState >= 2)
|
|
22
23
|
return;
|
|
23
24
|
await new Promise((resolve, reject) => {
|
|
25
|
+
const timeout = new AbortController();
|
|
24
26
|
const cleanup = () => {
|
|
25
|
-
|
|
27
|
+
timeout.abort();
|
|
26
28
|
video.removeEventListener("seeked", done);
|
|
27
29
|
video.removeEventListener("error", failed);
|
|
28
30
|
};
|
|
@@ -36,13 +38,15 @@ export async function seekVideoFrame(video, seconds, tolerance = 0.001) {
|
|
|
36
38
|
cleanup();
|
|
37
39
|
reject(video.error ?? new Error("video seek failed"));
|
|
38
40
|
};
|
|
39
|
-
|
|
41
|
+
// Active time, so a page frozen mid-seek does not time out.
|
|
42
|
+
void activeTimeout(10_000, timeout.signal).then(() => {
|
|
40
43
|
cleanup();
|
|
41
44
|
reject(new Error("video seek timed out"));
|
|
42
|
-
}
|
|
45
|
+
});
|
|
43
46
|
video.addEventListener("seeked", done);
|
|
44
47
|
video.addEventListener("error", failed);
|
|
45
|
-
// currentTime changes before the new frame is available
|
|
48
|
+
// currentTime changes before the new frame is available, so wait on a pending seek to the
|
|
49
|
+
// target.
|
|
46
50
|
if (!video.seeking || Math.abs(video.currentTime - seconds) > tolerance)
|
|
47
51
|
video.currentTime = seconds;
|
|
48
52
|
});
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
/** A proxy's longer displayed edge, in pixels. */
|
|
2
2
|
export const PROXY_LONG_EDGE: 960;
|
|
3
|
+
/**
|
|
4
|
+
* How long a proxy conversion may go without progress before it fails as `proxy_stalled`. Some
|
|
5
|
+
* browser encoders stop emitting chunks without an error.
|
|
6
|
+
*/
|
|
7
|
+
export const PROXY_STALL_MS: 30000;
|
|
3
8
|
/**
|
|
4
9
|
* @typedef {{
|
|
5
10
|
* made_from: import("alpha2/project").MediaFingerprint,
|
|
@@ -11,18 +16,15 @@ export const PROXY_LONG_EDGE: 960;
|
|
|
11
16
|
* }} ProxyRecord
|
|
12
17
|
*/
|
|
13
18
|
/**
|
|
14
|
-
* Keeps proxies by their original's SHA-256
|
|
15
|
-
*
|
|
16
|
-
* otherwise.
|
|
19
|
+
* Keeps proxies by their original's SHA-256, in OPFS where writable, else in this page's memory
|
|
20
|
+
* within {@link DEFAULT_MEMORY_OUTPUT_LIMIT} by default.
|
|
17
21
|
*/
|
|
18
22
|
export class ProxyStore {
|
|
19
|
-
constructor({ memoryLimit }?: {
|
|
23
|
+
constructor({ memoryLimit, stallMs, }?: {
|
|
20
24
|
memoryLimit?: number | undefined;
|
|
25
|
+
stallMs?: number | undefined;
|
|
21
26
|
});
|
|
22
|
-
/**
|
|
23
|
-
* Holds the proxy of `hash` in use until `release()`, so no page removes it meanwhile (see
|
|
24
|
-
* `holdProxy`).
|
|
25
|
-
*/
|
|
27
|
+
/** Holds the proxy of `hash` in use until `release()` so no page removes it (see `holdProxy`). */
|
|
26
28
|
hold(hash: any): Promise<{
|
|
27
29
|
release(): Promise<void>;
|
|
28
30
|
}>;
|
|
@@ -31,27 +33,25 @@ export class ProxyStore {
|
|
|
31
33
|
record: any;
|
|
32
34
|
file: File;
|
|
33
35
|
} | null>;
|
|
34
|
-
/**
|
|
35
|
-
* The proxies kept: each original's hash and the bytes its proxy takes, on disk or in this page's
|
|
36
|
-
* memory.
|
|
37
|
-
*/
|
|
36
|
+
/** Each kept proxy's original hash and size in bytes, on disk or in this page's memory. */
|
|
38
37
|
list(): Promise<{
|
|
39
38
|
hash: string;
|
|
40
39
|
bytes: number;
|
|
41
40
|
}[]>;
|
|
42
41
|
/**
|
|
43
|
-
* Removes the proxies of `hashes
|
|
44
|
-
*
|
|
42
|
+
* Removes the proxies of `hashes` except those held in use; resolves to removed and in-use
|
|
43
|
+
* hashes.
|
|
45
44
|
*/
|
|
46
45
|
remove(hashes: any): Promise<import("../types.js").ProxyRemoval>;
|
|
47
46
|
/**
|
|
48
|
-
* Makes a proxy of `file
|
|
49
|
-
*
|
|
50
|
-
* rate
|
|
51
|
-
*
|
|
47
|
+
* Makes and keeps a proxy of `file` (with `fingerprint`): video scaled to fit
|
|
48
|
+
* {@link PROXY_LONG_EDGE} with a key frame every second, original timestamps and audio sample
|
|
49
|
+
* rate. Returns an existing proxy for the same bytes instead. Pages of the origin take turns per
|
|
50
|
+
* hash (`whileMakingProxy`).
|
|
52
51
|
*
|
|
53
52
|
* Refuses before encoding when the conversion would drop a track the fingerprint records. Rejects
|
|
54
|
-
* with `signal.reason` if `signal` aborts before the proxy is kept
|
|
53
|
+
* with `signal.reason` if `signal` aborts before the proxy is kept, and with `proxy_stalled` when
|
|
54
|
+
* the conversion makes no progress for {@link PROXY_STALL_MS}; neither keeps a file.
|
|
55
55
|
*/
|
|
56
56
|
generate(file: any, fingerprint: any, { signal, onProgress }?: {}): Promise<{
|
|
57
57
|
record: any;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Proxies: lighter copies of a source's video, kept in OPFS under the original's content hash so a
|
|
2
|
-
// reopened project previews before its files are relinked.
|
|
3
|
-
//
|
|
4
|
-
//
|
|
2
|
+
// reopened project previews before its files are relinked. The engine decides whether a proxy may
|
|
3
|
+
// stand in for its source. Proxies stream to their file while encoding; only a browser without
|
|
4
|
+
// writable OPFS keeps them in memory, for this page and within a size limit.
|
|
5
5
|
import { ALL_FORMATS, BlobSource, BufferTarget, Conversion, Input, Mp4OutputFormat, Output, StreamTarget, canEncodeAudio, } from "mediabunny";
|
|
6
6
|
import { MEMORY_OUTPUT_COPIES, WritableFileDestination, } from "alpha2/export-destination";
|
|
7
7
|
import { EditorError } from "../../core/document.js";
|
|
@@ -10,6 +10,11 @@ import { DEFAULT_MEMORY_OUTPUT_LIMIT } from "./output.js";
|
|
|
10
10
|
import { holdProxy, keptProxies, proxyDirectory, removeKeptProxies, removeUnfinishedProxies, unfinishedProxyName, whileMakingProxy, } from "./proxy-storage.js";
|
|
11
11
|
/** A proxy's longer displayed edge, in pixels. */
|
|
12
12
|
export const PROXY_LONG_EDGE = 960;
|
|
13
|
+
/**
|
|
14
|
+
* How long a proxy conversion may go without progress before it fails as `proxy_stalled`. Some
|
|
15
|
+
* browser encoders stop emitting chunks without an error.
|
|
16
|
+
*/
|
|
17
|
+
export const PROXY_STALL_MS = 30_000;
|
|
13
18
|
/**
|
|
14
19
|
* @typedef {{
|
|
15
20
|
* made_from: import("alpha2/project").MediaFingerprint,
|
|
@@ -21,24 +26,19 @@ export const PROXY_LONG_EDGE = 960;
|
|
|
21
26
|
* }} ProxyRecord
|
|
22
27
|
*/
|
|
23
28
|
/**
|
|
24
|
-
* Keeps proxies by their original's SHA-256
|
|
25
|
-
*
|
|
26
|
-
* otherwise.
|
|
29
|
+
* Keeps proxies by their original's SHA-256, in OPFS where writable, else in this page's memory
|
|
30
|
+
* within {@link DEFAULT_MEMORY_OUTPUT_LIMIT} by default.
|
|
27
31
|
*/
|
|
28
32
|
export class ProxyStore {
|
|
29
|
-
/**
|
|
30
|
-
* Proxies this page keeps in memory, where the browser cannot keep them on disk.
|
|
31
|
-
* @type {Map<string, { record: ProxyRecord, file: File }>}
|
|
32
|
-
*/
|
|
33
|
+
/** @type {Map<string, { record: ProxyRecord, file: File }>} */
|
|
33
34
|
#memory = new Map();
|
|
34
35
|
#memoryLimit;
|
|
35
|
-
|
|
36
|
+
#stallMs;
|
|
37
|
+
constructor({ memoryLimit = DEFAULT_MEMORY_OUTPUT_LIMIT, stallMs = PROXY_STALL_MS, } = {}) {
|
|
36
38
|
this.#memoryLimit = memoryLimit;
|
|
39
|
+
this.#stallMs = stallMs;
|
|
37
40
|
}
|
|
38
|
-
/**
|
|
39
|
-
* Holds the proxy of `hash` in use until `release()`, so no page removes it meanwhile (see
|
|
40
|
-
* `holdProxy`).
|
|
41
|
-
*/
|
|
41
|
+
/** Holds the proxy of `hash` in use until `release()` so no page removes it (see `holdProxy`). */
|
|
42
42
|
hold(hash) {
|
|
43
43
|
return holdProxy(hash);
|
|
44
44
|
}
|
|
@@ -53,17 +53,14 @@ export class ProxyStore {
|
|
|
53
53
|
try {
|
|
54
54
|
const record = JSON.parse(await (await (await directory.getFileHandle(`${hash}.json`)).getFile()).text());
|
|
55
55
|
const file = await (await directory.getFileHandle(`${hash}.mp4`)).getFile();
|
|
56
|
-
// Read each time
|
|
56
|
+
// Read each time, since another page may have removed it.
|
|
57
57
|
return { record, file };
|
|
58
58
|
}
|
|
59
59
|
catch {
|
|
60
60
|
return null;
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
-
/**
|
|
64
|
-
* The proxies kept: each original's hash and the bytes its proxy takes, on disk or in this page's
|
|
65
|
-
* memory.
|
|
66
|
-
*/
|
|
63
|
+
/** Each kept proxy's original hash and size in bytes, on disk or in this page's memory. */
|
|
67
64
|
async list() {
|
|
68
65
|
const kept = new Map((await keptProxies()).map(({ hash, bytes }) => [hash, bytes]));
|
|
69
66
|
for (const [hash, { file }] of this.#memory)
|
|
@@ -71,8 +68,8 @@ export class ProxyStore {
|
|
|
71
68
|
return [...kept].map(([hash, bytes]) => ({ hash, bytes }));
|
|
72
69
|
}
|
|
73
70
|
/**
|
|
74
|
-
* Removes the proxies of `hashes
|
|
75
|
-
*
|
|
71
|
+
* Removes the proxies of `hashes` except those held in use; resolves to removed and in-use
|
|
72
|
+
* hashes.
|
|
76
73
|
*/
|
|
77
74
|
async remove(hashes) {
|
|
78
75
|
const wanted = [...new Set(hashes)];
|
|
@@ -81,13 +78,14 @@ export class ProxyStore {
|
|
|
81
78
|
return removeKeptProxies(wanted);
|
|
82
79
|
}
|
|
83
80
|
/**
|
|
84
|
-
* Makes a proxy of `file
|
|
85
|
-
*
|
|
86
|
-
* rate
|
|
87
|
-
*
|
|
81
|
+
* Makes and keeps a proxy of `file` (with `fingerprint`): video scaled to fit
|
|
82
|
+
* {@link PROXY_LONG_EDGE} with a key frame every second, original timestamps and audio sample
|
|
83
|
+
* rate. Returns an existing proxy for the same bytes instead. Pages of the origin take turns per
|
|
84
|
+
* hash (`whileMakingProxy`).
|
|
88
85
|
*
|
|
89
86
|
* Refuses before encoding when the conversion would drop a track the fingerprint records. Rejects
|
|
90
|
-
* with `signal.reason` if `signal` aborts before the proxy is kept
|
|
87
|
+
* with `signal.reason` if `signal` aborts before the proxy is kept, and with `proxy_stalled` when
|
|
88
|
+
* the conversion makes no progress for {@link PROXY_STALL_MS}; neither keeps a file.
|
|
91
89
|
*/
|
|
92
90
|
async generate(file, fingerprint, { signal, onProgress } = {}) {
|
|
93
91
|
signal?.throwIfAborted();
|
|
@@ -104,7 +102,7 @@ export class ProxyStore {
|
|
|
104
102
|
const directory = held ? await proxyDirectory() : null;
|
|
105
103
|
if (directory)
|
|
106
104
|
await removeUnfinishedProxies(directory, hash);
|
|
107
|
-
// Held while
|
|
105
|
+
// Held while made so no page removes it before it is returned.
|
|
108
106
|
const hold = await holdProxy(hash);
|
|
109
107
|
try {
|
|
110
108
|
return await this.#make(file, fingerprint, hash, {
|
|
@@ -117,15 +115,14 @@ export class ProxyStore {
|
|
|
117
115
|
}
|
|
118
116
|
});
|
|
119
117
|
}
|
|
120
|
-
/** Makes the proxy of `file
|
|
118
|
+
/** Makes the proxy of `file` while holding the turn (see {@link generate}). */
|
|
121
119
|
async #make(file, fingerprint, hash, { signal, onProgress }) {
|
|
122
120
|
signal?.throwIfAborted();
|
|
123
121
|
const input = new Input({
|
|
124
122
|
source: new BlobSource(file),
|
|
125
123
|
formats: ALL_FORMATS,
|
|
126
124
|
});
|
|
127
|
-
// Installed before
|
|
128
|
-
// it exists.
|
|
125
|
+
// Installed before setup awaits, so an earlier abort cancels the conversion once it exists.
|
|
129
126
|
let cancel = () => { };
|
|
130
127
|
const onAbort = () => cancel();
|
|
131
128
|
signal?.addEventListener("abort", onAbort, { once: true });
|
|
@@ -137,13 +134,13 @@ export class ProxyStore {
|
|
|
137
134
|
// The browser's scaler tone-maps HDR its own way, so the proxy would not match the original.
|
|
138
135
|
if (space?.transfer === "pq" || space?.transfer === "hlg")
|
|
139
136
|
throw new EditorError("unsupported_media", `${file.name} is HDR; the browser cannot make a proxy that keeps its color`, "proxy");
|
|
140
|
-
//
|
|
137
|
+
// Proxies have square pixels, so use the original's displayed size.
|
|
141
138
|
const shown = await displaySize(video);
|
|
142
139
|
const scale = Math.min(1, PROXY_LONG_EDGE / Math.max(shown.width, shown.height));
|
|
143
140
|
const even = (value) => Math.max(2, 2 * Math.round((value * scale) / 2));
|
|
144
141
|
const audio = await input.getPrimaryAudioTrack();
|
|
145
|
-
// AAC keeps any sample rate, else PCM; Opus would resample and the engine
|
|
146
|
-
//
|
|
142
|
+
// AAC keeps any sample rate, else PCM; Opus would resample and the engine would refuse the
|
|
143
|
+
// proxy.
|
|
147
144
|
const audioCodec = audio &&
|
|
148
145
|
(await canEncodeAudio("aac", {
|
|
149
146
|
numberOfChannels: audio.numberOfChannels,
|
|
@@ -155,7 +152,7 @@ export class ProxyStore {
|
|
|
155
152
|
const sink = await this.#open(hash);
|
|
156
153
|
try {
|
|
157
154
|
const output = new Output({
|
|
158
|
-
// No fast start: samples stream out as
|
|
155
|
+
// No fast start: samples stream out as muxed and nothing rewrites the payload.
|
|
159
156
|
format: new Mp4OutputFormat({ fastStart: false }),
|
|
160
157
|
target: sink.target,
|
|
161
158
|
});
|
|
@@ -163,8 +160,8 @@ export class ProxyStore {
|
|
|
163
160
|
input,
|
|
164
161
|
output,
|
|
165
162
|
tracks: "primary",
|
|
166
|
-
// Without it a conversion starts at the first frame,
|
|
167
|
-
//
|
|
163
|
+
// Without it a conversion starts at the first frame, shifting an original with a late
|
|
164
|
+
// one.
|
|
168
165
|
trim: { start: 0 },
|
|
169
166
|
video: {
|
|
170
167
|
width: even(shown.width),
|
|
@@ -183,26 +180,51 @@ export class ProxyStore {
|
|
|
183
180
|
const left = conversion.discardedTracks.find(({ track }) => track === video || (track === audio && fingerprint.streams?.audio));
|
|
184
181
|
if (left)
|
|
185
182
|
throw new EditorError("unsupported_media", `Cannot make a proxy of ${file.name}: this browser cannot convert its ${left.track === video ? "video" : "audio"} (${left.reason})`, "proxy");
|
|
186
|
-
//
|
|
183
|
+
// Discard before the output closes it so a cancelled proxy is never committed.
|
|
187
184
|
cancel = () => {
|
|
188
185
|
void sink.discard();
|
|
189
186
|
void conversion.cancel();
|
|
190
187
|
};
|
|
191
188
|
sink.onOverflow = cancel;
|
|
192
189
|
signal?.throwIfAborted();
|
|
193
|
-
|
|
194
|
-
|
|
190
|
+
// Raced against the conversion, since a stalled encoder may not settle even when cancelled.
|
|
191
|
+
let stalled = null;
|
|
192
|
+
let stallTimer = 0;
|
|
193
|
+
let failStalled = () => { };
|
|
194
|
+
const stall = new Promise((_, reject) => {
|
|
195
|
+
failStalled = reject;
|
|
196
|
+
});
|
|
197
|
+
const armStall = () => {
|
|
198
|
+
clearTimeout(stallTimer);
|
|
199
|
+
stallTimer = setTimeout(() => {
|
|
200
|
+
stalled = new EditorError("proxy_stalled", `The proxy of ${file.name} made no progress for ${Math.round(this.#stallMs / 1000)} s; the browser's video decoder or encoder stopped`, "proxy");
|
|
201
|
+
failStalled(stalled);
|
|
202
|
+
cancel();
|
|
203
|
+
}, this.#stallMs);
|
|
204
|
+
};
|
|
205
|
+
conversion.onProgress = (fraction) => {
|
|
206
|
+
armStall();
|
|
207
|
+
onProgress?.(fraction);
|
|
208
|
+
};
|
|
209
|
+
armStall();
|
|
210
|
+
const executed = conversion.execute();
|
|
211
|
+
executed.catch(() => { });
|
|
195
212
|
try {
|
|
196
|
-
await
|
|
213
|
+
await Promise.race([executed, stall]);
|
|
197
214
|
}
|
|
198
215
|
catch (error) {
|
|
199
216
|
signal?.throwIfAborted();
|
|
200
|
-
throw sink.failure ?? error;
|
|
217
|
+
throw stalled ?? sink.failure ?? error;
|
|
218
|
+
}
|
|
219
|
+
finally {
|
|
220
|
+
clearTimeout(stallTimer);
|
|
201
221
|
}
|
|
202
222
|
signal?.throwIfAborted();
|
|
223
|
+
if (stalled)
|
|
224
|
+
throw stalled;
|
|
203
225
|
if (sink.failure)
|
|
204
226
|
throw sink.failure;
|
|
205
|
-
// From here a failure discards the file; an abort after
|
|
227
|
+
// From here a failure discards the file; an abort after keeping leaves it kept.
|
|
206
228
|
cancel = () => { };
|
|
207
229
|
const proxy = await sink.file();
|
|
208
230
|
const evidence = await probeEvidence(proxy);
|
|
@@ -238,7 +260,7 @@ export class ProxyStore {
|
|
|
238
260
|
}
|
|
239
261
|
/**
|
|
240
262
|
* Where the proxy of `hash` is written: `target` takes the muxer's writes, `keep(record)` keeps
|
|
241
|
-
* it, `discard()` leaves nothing
|
|
263
|
+
* it, `discard()` leaves nothing. The file is written under its own name and moved to
|
|
242
264
|
* `<hash>.mp4` by `keep`, so `discard` removes only what this sink wrote.
|
|
243
265
|
*/
|
|
244
266
|
async #open(hash) {
|
|
@@ -276,7 +298,7 @@ export class ProxyStore {
|
|
|
276
298
|
onOverflow: () => { },
|
|
277
299
|
file: () => handle.getFile(),
|
|
278
300
|
keep: async (record) => {
|
|
279
|
-
// A video without a record is a stopped page's leftover and this page holds the turn.
|
|
301
|
+
// A video without a record is a stopped page's leftover, and this page holds the turn.
|
|
280
302
|
await directory.removeEntry(kept).catch(() => { });
|
|
281
303
|
moved = true;
|
|
282
304
|
await moveFile(directory, handle, name, kept);
|
|
@@ -288,8 +310,8 @@ export class ProxyStore {
|
|
|
288
310
|
},
|
|
289
311
|
discard: () => (discarding ??= (async () => {
|
|
290
312
|
await destination.abort();
|
|
291
|
-
//
|
|
292
|
-
//
|
|
313
|
+
// An unrecorded file is this sink's own; no other page's proxy is in place while this
|
|
314
|
+
// page holds the turn.
|
|
293
315
|
if (recorded)
|
|
294
316
|
await directory.removeEntry(`${hash}.json`).catch(() => { });
|
|
295
317
|
if (moved)
|
|
@@ -326,14 +348,13 @@ export class ProxyStore {
|
|
|
326
348
|
return sink;
|
|
327
349
|
}
|
|
328
350
|
}
|
|
329
|
-
/** An id no other page's unfinished proxy has. */
|
|
330
351
|
function uniqueId() {
|
|
331
352
|
return (globalThis.crypto?.randomUUID?.() ??
|
|
332
353
|
`${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`);
|
|
333
354
|
}
|
|
334
355
|
/**
|
|
335
|
-
* Moves
|
|
336
|
-
*
|
|
356
|
+
* Moves finished file `handle` (named `from`) to `to`, with `move` where available, else by copy
|
|
357
|
+
* and remove.
|
|
337
358
|
* @param {FileSystemDirectoryHandle} directory
|
|
338
359
|
* @param {FileSystemFileHandle} handle
|
|
339
360
|
* @param {string} from
|
|
@@ -348,7 +369,7 @@ async function moveFile(directory, handle, from, to) {
|
|
|
348
369
|
return;
|
|
349
370
|
}
|
|
350
371
|
catch {
|
|
351
|
-
//
|
|
372
|
+
// This browser's move refuses here, so copy instead.
|
|
352
373
|
}
|
|
353
374
|
const writable = await (await directory.getFileHandle(to, { create: true })).createWritable();
|
|
354
375
|
try {
|
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The proxy directory, or `null`
|
|
3
|
-
*
|
|
2
|
+
* The proxy directory, or `null` without an origin-private file system (or, with `create: false`,
|
|
3
|
+
* when no proxy was ever kept).
|
|
4
4
|
* @returns {Promise<FileSystemDirectoryHandle | null>}
|
|
5
5
|
*/
|
|
6
6
|
export function proxyDirectory({ create }?: {
|
|
7
7
|
create?: boolean | undefined;
|
|
8
8
|
}): Promise<FileSystemDirectoryHandle | null>;
|
|
9
9
|
/**
|
|
10
|
-
* Holds the proxy of `hash` in use
|
|
11
|
-
* the origin
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* `release()` returns a promise that settles when the lock is let go.
|
|
10
|
+
* Holds the proxy of `hash` in use for this page until `release()`, so removals from any page of
|
|
11
|
+
* the origin skip it. Resolves once held; without Web Locks, at once and holding nothing.
|
|
12
|
+
* `release()` settles when the lock is let go.
|
|
15
13
|
* @param {string} hash
|
|
16
14
|
* @returns {Promise<{ release(): Promise<void> }>}
|
|
17
15
|
*/
|
|
@@ -19,10 +17,10 @@ export function holdProxy(hash: string): Promise<{
|
|
|
19
17
|
release(): Promise<void>;
|
|
20
18
|
}>;
|
|
21
19
|
/**
|
|
22
|
-
* Runs `make` as the only page of the origin making the proxy of `hash
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
20
|
+
* Runs `make` as the only page of the origin making the proxy of `hash`, waiting for a page already
|
|
21
|
+
* making it so the caller can find that proxy instead. `make` is told whether it holds the turn;
|
|
22
|
+
* without Web Locks it runs at once, holding nothing. Rejects with `signal.reason` if `signal`
|
|
23
|
+
* aborts while waiting.
|
|
26
24
|
* @template T
|
|
27
25
|
* @param {string} hash
|
|
28
26
|
* @param {{ signal?: AbortSignal }} options
|
|
@@ -33,23 +31,21 @@ export function whileMakingProxy<T>(hash: string, options: {
|
|
|
33
31
|
signal?: AbortSignal;
|
|
34
32
|
}, make: (held: boolean) => Promise<T>): Promise<T>;
|
|
35
33
|
/**
|
|
36
|
-
* Removes
|
|
37
|
-
*
|
|
38
|
-
* be writing one.
|
|
34
|
+
* Removes unfinished videos a stopped page left while making the proxy of `hash`. Only for the page
|
|
35
|
+
* holding the turn ({@link whileMakingProxy}), when no other page can be writing one.
|
|
39
36
|
* @param {FileSystemDirectoryHandle} directory
|
|
40
37
|
* @param {string} hash
|
|
41
38
|
*/
|
|
42
39
|
export function removeUnfinishedProxies(directory: FileSystemDirectoryHandle, hash: string): Promise<void>;
|
|
43
40
|
/**
|
|
44
|
-
*
|
|
41
|
+
* Each proxy kept in this origin's storage: the original's hash and the proxy's size in bytes.
|
|
45
42
|
* @returns {Promise<import("../types.js").KeptProxy[]>}
|
|
46
43
|
*/
|
|
47
44
|
export function keptProxies(): Promise<import("../types.js").KeptProxy[]>;
|
|
48
45
|
/**
|
|
49
|
-
* Removes the kept proxies of `hashes
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* listed in neither.
|
|
46
|
+
* Removes the kept proxies of `hashes` except those a page holds in use ({@link holdProxy}).
|
|
47
|
+
* Resolves to the removed hashes and those left in use; a hash with nothing kept counts as removed.
|
|
48
|
+
* Strings that are not lowercase hex SHA-256 are ignored and listed in neither.
|
|
53
49
|
* @param {Iterable<string>} hashes
|
|
54
50
|
* @returns {Promise<import("../types.js").ProxyRemoval>}
|
|
55
51
|
*/
|