alpha2 0.5.2 → 0.6.0
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/README.md +71 -5
- package/THIRD_PARTY_NOTICES.md +191 -1
- package/dist/apply-edit.d.ts +124 -0
- package/dist/apply-edit.js +15 -0
- package/dist/assets.d.ts +12 -0
- package/dist/audio-layout.js +6 -3
- package/dist/audio-worklet-processor.js +27 -33
- package/dist/audio.d.ts +5 -4
- package/dist/audio.js +8 -6
- package/dist/compose.d.ts +105 -0
- package/dist/compose.js +63 -0
- package/dist/decoder-fallback.d.ts +3 -2
- package/dist/decoder-fallback.js +11 -3
- package/dist/decoder-stall.d.ts +15 -0
- package/dist/decoder-stall.js +52 -0
- package/dist/edit-data.d.ts +24 -0
- package/dist/edit-data.js +224 -0
- package/dist/edit-keys.d.ts +16 -0
- package/dist/edit-keys.js +233 -0
- package/dist/edit.d.ts +41 -8
- package/dist/edit.js +123 -30
- package/dist/export-failure.d.ts +10 -2
- package/dist/export-failure.js +55 -4
- package/dist/export-retry.d.ts +6 -1
- package/dist/export-retry.js +12 -1
- package/dist/exporter.d.ts +11 -7
- package/dist/exporter.js +10 -9
- package/dist/extensions.d.ts +86 -28
- package/dist/extensions.js +204 -61
- package/dist/extract.d.ts +65 -0
- package/dist/extract.js +291 -0
- package/dist/extractor.d.ts +151 -0
- package/dist/extractor.js +141 -0
- package/dist/float32.d.ts +2 -0
- package/dist/float32.js +10 -0
- package/dist/frame-intake.d.ts +5 -0
- package/dist/frame-intake.js +10 -3
- package/dist/generated/builtin-effects.d.ts +3 -0
- package/dist/generated/builtin-effects.js +1804 -0
- package/dist/generated/project-schema.d.ts +696 -41
- package/dist/generated/project-schema.js +67 -0
- package/dist/generated/project-shape.js +986 -383
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/json-text.js +14 -2
- package/dist/layout-values.js +7 -1
- package/dist/layout.d.ts +12 -4
- package/dist/media.d.ts +44 -11
- package/dist/media.js +256 -67
- package/dist/mediabunny.d.ts +32 -12
- package/dist/mediabunny.js +276 -90
- package/dist/player.d.ts +9 -3
- package/dist/player.js +114 -18
- package/dist/plugins.d.ts +62 -0
- package/dist/plugins.js +506 -0
- package/dist/project-data.d.ts +56 -0
- package/dist/project-data.js +765 -0
- package/dist/project.d.ts +36 -1
- package/dist/project.js +515 -42
- package/dist/robustness.d.ts +6 -33
- package/dist/robustness.js +2 -63
- package/dist/sha256.d.ts +3 -2
- package/dist/sha256.js +14 -6
- package/dist/wasm-module.d.ts +37 -0
- package/dist/wasm-module.js +47 -0
- package/dist/worker-error.d.ts +3 -2
- package/dist/worker-error.js +3 -1
- package/dist/worker.d.ts +13 -3
- package/dist/worker.js +36 -9
- package/package.json +36 -2
- package/wasm/alpha2_compose.d.ts +92 -0
- package/wasm/alpha2_compose.js +445 -0
- package/wasm/alpha2_compose_bg.wasm +0 -0
- package/wasm/alpha2_compose_bg.wasm.d.ts +17 -0
- package/wasm/alpha2_extensions.d.ts +4 -4
- package/wasm/alpha2_extensions.js +16 -4
- package/wasm/alpha2_extensions_bg.wasm +0 -0
- package/wasm/alpha2_extensions_pdf.d.ts +227 -0
- package/wasm/alpha2_extensions_pdf.js +841 -0
- package/wasm/alpha2_extensions_pdf_bg.wasm +0 -0
- package/wasm/alpha2_extensions_pdf_bg.wasm.d.ts +38 -0
- package/wasm/alpha2_wasm.d.ts +151 -18
- package/wasm/alpha2_wasm.js +403 -59
- package/wasm/alpha2_wasm_bg.wasm +0 -0
- package/wasm/alpha2_wasm_bg.wasm.d.ts +21 -6
- package/wasm/rust-crate-licenses.md +493 -54
- package/wasm/snippets/alpha2-wasm-1b02e9f302d283ba/inline3.js +7 -0
- /package/wasm/snippets/{alpha2-wasm-a21734ffde826659/inline1.js → alpha2-wasm-1b02e9f302d283ba/inline0.js} +0 -0
- /package/wasm/snippets/{alpha2-wasm-a21734ffde826659/inline2.js → alpha2-wasm-1b02e9f302d283ba/inline1.js} +0 -0
- /package/wasm/snippets/{alpha2-wasm-a21734ffde826659/inline3.js → alpha2-wasm-1b02e9f302d283ba/inline2.js} +0 -0
- /package/wasm/snippets/{alpha2-wasm-a21734ffde826659/inline0.js → alpha2-wasm-1b02e9f302d283ba/inline4.js} +0 -0
package/dist/extract.js
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// `runExtractor` drives the engine's session in wasm over media the page or a worker decodes.
|
|
2
|
+
// Re-exports the shared contract from `alpha2/extractor`.
|
|
3
|
+
import { ALL_FORMATS, BlobSource, BufferSource, EncodedPacketSink, Input, VideoSampleSink, } from "mediabunny";
|
|
4
|
+
import { collectEmits, schemaProblem, } from "./extractor.js";
|
|
5
|
+
export { collectEmits, schemaProblem } from "./extractor.js";
|
|
6
|
+
import { copyPlanarYuvFrame, DecodeUnavailableError, decodeRefusalMessage, } from "./hdr-frames.js";
|
|
7
|
+
import { microsecondsToTicks, readVideoOrientation } from "./media.js";
|
|
8
|
+
import { MediabunnyAudioDecoder, streamChromaSiting, videoTrackFacts, } from "./mediabunny.js";
|
|
9
|
+
const TICKS_PER_SECOND = 120_000;
|
|
10
|
+
/** An error with the code the native host answers with. */
|
|
11
|
+
function coded(code, message, cause) {
|
|
12
|
+
return Object.assign(new Error(message, { cause }), { code });
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* One source's frames, decoded in software so they carry planes: the frame shown at a time, the
|
|
16
|
+
* one after it in either direction.
|
|
17
|
+
*/
|
|
18
|
+
class SourceFrames {
|
|
19
|
+
#sink;
|
|
20
|
+
#input;
|
|
21
|
+
#orientation;
|
|
22
|
+
#chromaSiting;
|
|
23
|
+
#forward;
|
|
24
|
+
constructor(input, sink, orientation, chromaSiting) {
|
|
25
|
+
this.#input = input;
|
|
26
|
+
this.#sink = sink;
|
|
27
|
+
this.#orientation = orientation;
|
|
28
|
+
this.#chromaSiting = chromaSiting;
|
|
29
|
+
}
|
|
30
|
+
static async open(resource, id) {
|
|
31
|
+
const source = resource instanceof Blob
|
|
32
|
+
? new BlobSource(resource)
|
|
33
|
+
: new BufferSource(resource);
|
|
34
|
+
const input = new Input({ source, formats: ALL_FORMATS });
|
|
35
|
+
const track = await input.getPrimaryVideoTrack();
|
|
36
|
+
if (track === null) {
|
|
37
|
+
input.dispose();
|
|
38
|
+
throw coded("INVALID_INPUT", "the source has no video track");
|
|
39
|
+
}
|
|
40
|
+
// `canDecode` does not ask for a software decoder, so a hardware-only codec would pass it and
|
|
41
|
+
// then fail on its first read.
|
|
42
|
+
const config = (await track.canDecode())
|
|
43
|
+
? await track.getDecoderConfig()
|
|
44
|
+
: null;
|
|
45
|
+
const software = config !== null &&
|
|
46
|
+
(await VideoDecoder.isConfigSupported({
|
|
47
|
+
...config,
|
|
48
|
+
hardwareAcceleration: "prefer-software",
|
|
49
|
+
})).supported === true;
|
|
50
|
+
if (!software) {
|
|
51
|
+
const facts = await videoTrackFacts(track);
|
|
52
|
+
input.dispose();
|
|
53
|
+
throw new DecodeUnavailableError(decodeRefusalMessage(`source ${id}`, facts), facts);
|
|
54
|
+
}
|
|
55
|
+
const orientation = (await readVideoOrientation(resource, track.id)) ?? {
|
|
56
|
+
rotationDegrees: track.rotation,
|
|
57
|
+
mirror: false,
|
|
58
|
+
};
|
|
59
|
+
const chromaSiting = await streamChromaSiting(track, new EncodedPacketSink(track));
|
|
60
|
+
// Software frames carry planes, which present alike on every surface.
|
|
61
|
+
const sink = new VideoSampleSink(track, {
|
|
62
|
+
hardwareAcceleration: "prefer-software",
|
|
63
|
+
});
|
|
64
|
+
return new SourceFrames(input, sink, orientation, chromaSiting);
|
|
65
|
+
}
|
|
66
|
+
/** The frame shown at `ticks`, or the first frame when none is; a forward pass continues. */
|
|
67
|
+
async start(ticks, forward) {
|
|
68
|
+
await this.#forward?.return();
|
|
69
|
+
const seconds = Number(ticks) / TICKS_PER_SECOND;
|
|
70
|
+
this.#forward = this.#sink.samples(seconds);
|
|
71
|
+
const first = await this.#forward.next();
|
|
72
|
+
if (!forward) {
|
|
73
|
+
await this.#forward.return();
|
|
74
|
+
this.#forward = undefined;
|
|
75
|
+
}
|
|
76
|
+
return first.done === true ? null : first.value;
|
|
77
|
+
}
|
|
78
|
+
/** The frame after the one at `pts` in the pass's direction, or `null` past the end. */
|
|
79
|
+
async after(pts, forward) {
|
|
80
|
+
if (forward) {
|
|
81
|
+
const next = await this.#forward?.next();
|
|
82
|
+
return next === undefined || next.done === true ? null : next.value;
|
|
83
|
+
}
|
|
84
|
+
if (pts <= 0n)
|
|
85
|
+
return null;
|
|
86
|
+
const sample = await this.#sink.getSample(Number(pts - 1n) / TICKS_PER_SECOND);
|
|
87
|
+
if (sample === null)
|
|
88
|
+
return null;
|
|
89
|
+
if (ticksOf(sample) >= pts) {
|
|
90
|
+
sample.close();
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return sample;
|
|
94
|
+
}
|
|
95
|
+
async supply(session, sample) {
|
|
96
|
+
const frame = sample.toVideoFrame();
|
|
97
|
+
sample.close();
|
|
98
|
+
const planes = await copyPlanarYuvFrame(frame, null, {
|
|
99
|
+
rotation: this.#orientation.rotationDegrees,
|
|
100
|
+
mirror: this.#orientation.mirror,
|
|
101
|
+
chromaSiting: this.#chromaSiting,
|
|
102
|
+
});
|
|
103
|
+
session.supplyYuvFrame(microsecondsToTicks(planes.physicalTimeMicroseconds), planes.format, planes.codedWidth, planes.codedHeight, planes.data, Uint32Array.from(planes.layout), Uint32Array.from(planes.visibleRect), planes.displayWidth, planes.displayHeight, JSON.stringify(planes.color), planes.chromaSiting, planes.rotation ?? 0, planes.mirror ?? false);
|
|
104
|
+
}
|
|
105
|
+
async close() {
|
|
106
|
+
await this.#forward?.return();
|
|
107
|
+
this.#input.dispose();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function ticksOf(sample) {
|
|
111
|
+
return microsecondsToTicks(sample.microsecondTimestamp);
|
|
112
|
+
}
|
|
113
|
+
/** Calls an extractor step, turning its failure into `EXTRACTOR_FAILED`. */
|
|
114
|
+
async function step(call) {
|
|
115
|
+
try {
|
|
116
|
+
return await call();
|
|
117
|
+
}
|
|
118
|
+
catch (cause) {
|
|
119
|
+
// A plugin's failure already carries the code every surface reports it with.
|
|
120
|
+
if (cause instanceof Error &&
|
|
121
|
+
typeof cause.code === "string")
|
|
122
|
+
throw cause;
|
|
123
|
+
const message = cause instanceof Error ? cause.message : String(cause);
|
|
124
|
+
throw coded("EXTRACTOR_FAILED", `the extractor failed: ${message}`, cause);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Runs `extractor` over `request.request` in the page or a worker: source frames are decoded in
|
|
129
|
+
* software and presented by the engine exactly as the native hosts present them, so the data
|
|
130
|
+
* matches theirs byte for byte; timeline frames come from `request.timelineFrame`. Resolves with
|
|
131
|
+
* the data sets the session validated.
|
|
132
|
+
*
|
|
133
|
+
* Rejects with `INVALID_INPUT` for a request the session refuses or params the descriptor's
|
|
134
|
+
* `params_schema` does not take, `INVALID_EMIT` (with `details`) for output the session refuses,
|
|
135
|
+
* `EXTRACTOR_FAILED` when a step throws, and `ABORTED` on `request.signal`.
|
|
136
|
+
*/
|
|
137
|
+
export async function runExtractor(request, extractor) {
|
|
138
|
+
const started = performance.now();
|
|
139
|
+
const descriptor = extractor.describe();
|
|
140
|
+
const problem = schemaProblem(descriptor.params_schema, request.request.params ?? {});
|
|
141
|
+
if (problem !== undefined)
|
|
142
|
+
throw coded("INVALID_INPUT", `invalid params: ${problem}`);
|
|
143
|
+
const session = new request.binding.ExtractSession(JSON.stringify(request.project), JSON.stringify(descriptor), JSON.stringify(request.request));
|
|
144
|
+
const sources = new Map();
|
|
145
|
+
const audio = new Map();
|
|
146
|
+
const resource = (id) => {
|
|
147
|
+
const found = request.media?.[id];
|
|
148
|
+
if (found === undefined)
|
|
149
|
+
throw coded("MISSING_INPUT", `no media for source ${id}`);
|
|
150
|
+
return found;
|
|
151
|
+
};
|
|
152
|
+
try {
|
|
153
|
+
const context = JSON.parse(session.context());
|
|
154
|
+
await step(() => extractor.begin?.(context));
|
|
155
|
+
let pass = 0;
|
|
156
|
+
for (;;) {
|
|
157
|
+
if (request.signal?.aborted === true)
|
|
158
|
+
throw coded("ABORTED", "the extraction was cancelled");
|
|
159
|
+
const next = JSON.parse(session.next());
|
|
160
|
+
if (next.step === "done")
|
|
161
|
+
break;
|
|
162
|
+
if (next.step === "need" && next.need !== undefined) {
|
|
163
|
+
const need = next.need;
|
|
164
|
+
if (need.need === "source_frame") {
|
|
165
|
+
const id = need.source ?? "";
|
|
166
|
+
let frames = sources.get(id);
|
|
167
|
+
if (frames === undefined) {
|
|
168
|
+
frames = await SourceFrames.open(resource(id), id);
|
|
169
|
+
sources.set(id, frames);
|
|
170
|
+
}
|
|
171
|
+
const forward = need.direction !== "backward";
|
|
172
|
+
const time = BigInt(need.time ?? 0);
|
|
173
|
+
const sample = need.from === "start"
|
|
174
|
+
? await frames.start(time, forward)
|
|
175
|
+
: await frames.after(time, forward);
|
|
176
|
+
if (sample === null)
|
|
177
|
+
session.supplyEnd();
|
|
178
|
+
else
|
|
179
|
+
await frames.supply(session, sample);
|
|
180
|
+
}
|
|
181
|
+
else if (need.need === "timeline_frame") {
|
|
182
|
+
if (request.timelineFrame === undefined)
|
|
183
|
+
throw coded("INVALID_INPUT", "a timeline input needs request.timelineFrame");
|
|
184
|
+
const time = BigInt(need.time ?? 0);
|
|
185
|
+
const frame = await request.timelineFrame(time, need.width ?? 0, need.height ?? 0);
|
|
186
|
+
session.supplyRgbaFrame(time, frame.width, frame.height, frame.rgba);
|
|
187
|
+
}
|
|
188
|
+
else if (need.need === "source_audio") {
|
|
189
|
+
const id = need.source ?? "";
|
|
190
|
+
const binding = request.audioBinding;
|
|
191
|
+
if (binding === undefined)
|
|
192
|
+
throw coded("INVALID_INPUT", "an audio input needs request.audioBinding");
|
|
193
|
+
let decoder = audio.get(id);
|
|
194
|
+
if (decoder === undefined) {
|
|
195
|
+
decoder = await MediabunnyAudioDecoder.open(id, resource(id), binding, { stream: need.stream ?? 0 });
|
|
196
|
+
audio.set(id, decoder);
|
|
197
|
+
}
|
|
198
|
+
const rate = need.rate ?? 48_000;
|
|
199
|
+
const first = BigInt(need.startSample ?? 0);
|
|
200
|
+
const frames = need.frames ?? 0;
|
|
201
|
+
const toTicks = (sample) => (sample * BigInt(TICKS_PER_SECOND)) / BigInt(rate);
|
|
202
|
+
const block = await decoder.refill({
|
|
203
|
+
sourceId: id,
|
|
204
|
+
startMediaTimeTicks: toTicks(first),
|
|
205
|
+
endMediaTimeTicks: toTicks(first + BigInt(frames)) + 1n,
|
|
206
|
+
generation: 0,
|
|
207
|
+
}, rate, request.signal);
|
|
208
|
+
const offset = Number(first - block.startSample);
|
|
209
|
+
const samples = new Float32Array(frames * block.channels.length);
|
|
210
|
+
block.channels.forEach((plane, channel) => {
|
|
211
|
+
for (let index = 0; index < frames; index += 1) {
|
|
212
|
+
const at = offset + index;
|
|
213
|
+
samples[channel * frames + index] =
|
|
214
|
+
at >= 0 && at < plane.length ? (plane[at] ?? 0) : 0;
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
session.supplyAudio(block.channels.length, samples, block.speakers === undefined
|
|
218
|
+
? undefined
|
|
219
|
+
: JSON.stringify(block.speakers));
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
throw coded("INVALID_INPUT", "timeline audio is not supplied in the browser yet; extract from the source");
|
|
223
|
+
}
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
const { emits, out } = collectEmits();
|
|
227
|
+
if (next.step === "pass") {
|
|
228
|
+
pass = next.pass ?? 0;
|
|
229
|
+
await step(() => extractor.pass?.({
|
|
230
|
+
pass,
|
|
231
|
+
direction: next.direction === "backward" ? "backward" : "forward",
|
|
232
|
+
}));
|
|
233
|
+
}
|
|
234
|
+
else if (next.step === "video") {
|
|
235
|
+
const data = session.videoBytes();
|
|
236
|
+
await step(() => extractor.video?.({
|
|
237
|
+
time: next.time ?? 0,
|
|
238
|
+
width: next.width ?? 0,
|
|
239
|
+
height: next.height ?? 0,
|
|
240
|
+
format: next.format === "luma8" ? "luma8" : "rgba8",
|
|
241
|
+
data,
|
|
242
|
+
}, out));
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
const samples = session.audioSamples();
|
|
246
|
+
await step(() => extractor.audio?.({
|
|
247
|
+
start: next.start ?? 0,
|
|
248
|
+
startSample: next.startSample ?? 0,
|
|
249
|
+
frames: next.frames ?? 0,
|
|
250
|
+
rate: next.rate ?? 0,
|
|
251
|
+
channels: next.channels ?? 0,
|
|
252
|
+
samples,
|
|
253
|
+
}, out));
|
|
254
|
+
}
|
|
255
|
+
session.emit(JSON.stringify(emits));
|
|
256
|
+
const progress = JSON.parse(session.progress());
|
|
257
|
+
const current = progress[pass];
|
|
258
|
+
if (current !== undefined && next.step !== "pass")
|
|
259
|
+
request.onProgress?.({ phase: "extract", pass, ...current });
|
|
260
|
+
}
|
|
261
|
+
const { emits, out } = collectEmits();
|
|
262
|
+
await step(() => extractor.end?.(out));
|
|
263
|
+
session.emit(JSON.stringify(emits));
|
|
264
|
+
const dataSets = JSON.parse(session.finish(request.madeFrom === undefined
|
|
265
|
+
? undefined
|
|
266
|
+
: JSON.stringify(request.madeFrom)));
|
|
267
|
+
const plugin = request.plugin;
|
|
268
|
+
const recorded = plugin === undefined
|
|
269
|
+
? dataSets
|
|
270
|
+
: dataSets.map((set) => ({
|
|
271
|
+
...set,
|
|
272
|
+
made_by: {
|
|
273
|
+
...set.made_by,
|
|
274
|
+
plugin: {
|
|
275
|
+
version: plugin.version,
|
|
276
|
+
artifact_sha256: plugin.artifactSha256,
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
}));
|
|
280
|
+
return {
|
|
281
|
+
dataSets: recorded,
|
|
282
|
+
timings: { totalMs: performance.now() - started },
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
finally {
|
|
286
|
+
await Promise.all([...sources.values()].map((frames) => frames.close()));
|
|
287
|
+
for (const decoder of audio.values())
|
|
288
|
+
decoder.close();
|
|
289
|
+
session.free();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { DataSet } from "./generated/project-schema.js";
|
|
2
|
+
/** What an extractor reads and emits; the same object `describe()` returns on every surface. */
|
|
3
|
+
export interface ExtractorDescriptor {
|
|
4
|
+
/** Reverse-DNS id; `alpha2.` is reserved for first-party plugins. */
|
|
5
|
+
readonly id: string;
|
|
6
|
+
/** The output contract's version. */
|
|
7
|
+
readonly version: number;
|
|
8
|
+
readonly input: {
|
|
9
|
+
readonly media: "video";
|
|
10
|
+
readonly from: readonly ("source" | "timeline")[];
|
|
11
|
+
readonly format: "rgba8" | "luma8";
|
|
12
|
+
readonly max_size?: number;
|
|
13
|
+
} | {
|
|
14
|
+
readonly media: "audio";
|
|
15
|
+
readonly from: readonly ("source" | "timeline")[];
|
|
16
|
+
readonly format: "f32";
|
|
17
|
+
readonly sample_rate?: number;
|
|
18
|
+
readonly channels?: "mono" | "source";
|
|
19
|
+
readonly block_frames?: number;
|
|
20
|
+
};
|
|
21
|
+
/** JSON Schema of the request's `params`; checked before the request is sent. */
|
|
22
|
+
readonly params_schema?: unknown;
|
|
23
|
+
readonly outputs: readonly {
|
|
24
|
+
readonly name: string;
|
|
25
|
+
readonly kind: "points" | "scalars" | "similarity" | "words" | "intervals";
|
|
26
|
+
readonly channels?: readonly string[];
|
|
27
|
+
readonly interpolation?: "hold" | "linear";
|
|
28
|
+
readonly suggest?: "bind" | "remove_ranges" | "split_at";
|
|
29
|
+
}[];
|
|
30
|
+
}
|
|
31
|
+
/** One run: the descriptor's id and version, its params, the input read and the data set ids. */
|
|
32
|
+
export interface ExtractRun {
|
|
33
|
+
readonly extractor: string;
|
|
34
|
+
readonly version: number;
|
|
35
|
+
readonly params?: Readonly<Record<string, unknown>>;
|
|
36
|
+
readonly input: {
|
|
37
|
+
readonly from: "source" | "timeline";
|
|
38
|
+
readonly source_id?: string;
|
|
39
|
+
/** Ticks on the input's clock. */
|
|
40
|
+
readonly range: {
|
|
41
|
+
readonly start: number;
|
|
42
|
+
readonly end: number;
|
|
43
|
+
};
|
|
44
|
+
readonly passes?: readonly {
|
|
45
|
+
readonly start: number;
|
|
46
|
+
readonly direction: "forward" | "backward";
|
|
47
|
+
}[];
|
|
48
|
+
readonly size?: number;
|
|
49
|
+
readonly frame_rate?: {
|
|
50
|
+
readonly num: number;
|
|
51
|
+
readonly den: number;
|
|
52
|
+
};
|
|
53
|
+
readonly stream?: number;
|
|
54
|
+
readonly sample_rate?: number;
|
|
55
|
+
readonly channels?: "mono" | "source";
|
|
56
|
+
};
|
|
57
|
+
/** Output name to the id of the data set it becomes. */
|
|
58
|
+
readonly outputs: Readonly<Record<string, string>>;
|
|
59
|
+
}
|
|
60
|
+
/** What `begin` is told: the range, passes, and the delivered video size or audio layout. */
|
|
61
|
+
export interface ExtractContext {
|
|
62
|
+
readonly range: {
|
|
63
|
+
readonly start: number;
|
|
64
|
+
readonly end: number;
|
|
65
|
+
};
|
|
66
|
+
readonly passes: readonly {
|
|
67
|
+
readonly start: number;
|
|
68
|
+
readonly direction: "forward" | "backward";
|
|
69
|
+
}[];
|
|
70
|
+
readonly from: "source" | "timeline";
|
|
71
|
+
readonly video: {
|
|
72
|
+
readonly size: number;
|
|
73
|
+
readonly format: string;
|
|
74
|
+
} | null;
|
|
75
|
+
readonly audio: {
|
|
76
|
+
readonly rate: number;
|
|
77
|
+
readonly channels: "mono" | "source";
|
|
78
|
+
readonly blockFrames: number;
|
|
79
|
+
} | null;
|
|
80
|
+
}
|
|
81
|
+
/** A delivered frame: tightly packed `rgba8` (straight alpha, sRGB) or `luma8`. */
|
|
82
|
+
export interface ExtractFrame {
|
|
83
|
+
/** Presentation time in ticks on the input's clock. */
|
|
84
|
+
readonly time: number;
|
|
85
|
+
readonly width: number;
|
|
86
|
+
readonly height: number;
|
|
87
|
+
readonly format: "rgba8" | "luma8";
|
|
88
|
+
/** Owned by the call; copy what must outlive it. */
|
|
89
|
+
readonly data: Uint8Array;
|
|
90
|
+
}
|
|
91
|
+
/** A delivered audio block: planar f32, channel after channel. */
|
|
92
|
+
export interface ExtractAudio {
|
|
93
|
+
/** The first sample's time, in ticks (the tick at or before it). */
|
|
94
|
+
readonly start: number;
|
|
95
|
+
/** The first sample, at `rate` from the clock's zero. */
|
|
96
|
+
readonly startSample: number;
|
|
97
|
+
readonly frames: number;
|
|
98
|
+
readonly rate: number;
|
|
99
|
+
readonly channels: number;
|
|
100
|
+
readonly samples: Float32Array;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Where an extractor's values go; the host validates them as the session does. Times are integer
|
|
104
|
+
* tick counts (numbers, not bigints) and values and scores finite; anything else throws.
|
|
105
|
+
*/
|
|
106
|
+
export interface ExtractEmitter {
|
|
107
|
+
/** A sample of a `points`, `scalars` or `similarity` output; `null` marks a lost channel. */
|
|
108
|
+
sample(output: string, time: number, values: readonly (number | null)[]): void;
|
|
109
|
+
word(output: string, start: number, end: number, text: string, speaker?: string): void;
|
|
110
|
+
interval(output: string, start: number, end: number, label?: string, score?: number): void;
|
|
111
|
+
}
|
|
112
|
+
/** A client-side extractor. Every callback may return a promise; the host waits for it. */
|
|
113
|
+
export interface Extractor {
|
|
114
|
+
describe(): ExtractorDescriptor;
|
|
115
|
+
begin?(context: ExtractContext): void | Promise<void>;
|
|
116
|
+
pass?(pass: {
|
|
117
|
+
readonly pass: number;
|
|
118
|
+
readonly direction: "forward" | "backward";
|
|
119
|
+
}): void | Promise<void>;
|
|
120
|
+
video?(frame: ExtractFrame, out: ExtractEmitter): void | Promise<void>;
|
|
121
|
+
audio?(block: ExtractAudio, out: ExtractEmitter): void | Promise<void>;
|
|
122
|
+
/** Whole-range work (speech recognition) belongs here and may emit anywhere in the range. */
|
|
123
|
+
end?(out: ExtractEmitter): void | Promise<void>;
|
|
124
|
+
}
|
|
125
|
+
/** Progress of one pass; `total` is `null` for source frames, whose count is not known ahead. */
|
|
126
|
+
export interface ExtractProgress {
|
|
127
|
+
readonly phase: "extract";
|
|
128
|
+
readonly pass: number;
|
|
129
|
+
readonly completed: number;
|
|
130
|
+
readonly total: number | null;
|
|
131
|
+
}
|
|
132
|
+
/** What an extraction returns: the data sets, ready to put in the project. */
|
|
133
|
+
export interface ExtractResult {
|
|
134
|
+
readonly dataSets: readonly DataSet[];
|
|
135
|
+
readonly timings: Readonly<Record<string, number>>;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* An emitter that keeps the emits, in the host protocol's JSON form. An emit the wire cannot
|
|
139
|
+
* carry throws a `TypeError` naming it, which fails the step.
|
|
140
|
+
*/
|
|
141
|
+
export declare function collectEmits(): {
|
|
142
|
+
emits: Record<string, unknown>[];
|
|
143
|
+
out: ExtractEmitter;
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Where `value` breaks `schema`, as a path and reason, or `undefined`. Covers the JSON Schema
|
|
147
|
+
* keywords descriptors use, which are `type`, `enum`, `const`, `properties`, `required`,
|
|
148
|
+
* `additionalProperties`, `items`, `minItems`, `maxItems`, `minimum`, `maximum`, `minLength` and
|
|
149
|
+
* `maxLength`.
|
|
150
|
+
*/
|
|
151
|
+
export declare function schemaProblem(schema: unknown, value: unknown, at?: string): string | undefined;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// The extractor contract every surface shares and its pure helpers; imports no media library.
|
|
2
|
+
function text(value, what) {
|
|
3
|
+
if (typeof value !== "string")
|
|
4
|
+
throw new TypeError(`${what} must be a string, not ${String(value)}`);
|
|
5
|
+
return value;
|
|
6
|
+
}
|
|
7
|
+
function ticks(value, what) {
|
|
8
|
+
if (typeof value === "bigint")
|
|
9
|
+
throw new TypeError(`${what} is a bigint; pass a number of ticks`);
|
|
10
|
+
if (!Number.isSafeInteger(value))
|
|
11
|
+
throw new TypeError(`${what} must be an integer tick count, not ${String(value)}`);
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
function finite(value, what) {
|
|
15
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
16
|
+
throw new TypeError(`${what} must be a finite number, not ${String(value)}`);
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* An emitter that keeps the emits, in the host protocol's JSON form. An emit the wire cannot
|
|
21
|
+
* carry throws a `TypeError` naming it, which fails the step.
|
|
22
|
+
*/
|
|
23
|
+
export function collectEmits() {
|
|
24
|
+
const emits = [];
|
|
25
|
+
return {
|
|
26
|
+
emits,
|
|
27
|
+
out: {
|
|
28
|
+
sample(output, time, values) {
|
|
29
|
+
const what = `sample "${String(output)}"`;
|
|
30
|
+
emits.push({
|
|
31
|
+
output: text(output, "an emit's output"),
|
|
32
|
+
time: ticks(time, `${what}: time`),
|
|
33
|
+
values: [...values].map((value, index) => value === null ? null : finite(value, `${what}: values[${index}]`)),
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
word(output, start, end, words, speaker) {
|
|
37
|
+
const what = `word "${String(output)}"`;
|
|
38
|
+
emits.push({
|
|
39
|
+
output: text(output, "an emit's output"),
|
|
40
|
+
start: ticks(start, `${what}: start`),
|
|
41
|
+
end: ticks(end, `${what}: end`),
|
|
42
|
+
text: text(words, `${what}: text`),
|
|
43
|
+
...(speaker === undefined
|
|
44
|
+
? {}
|
|
45
|
+
: { speaker: text(speaker, `${what}: speaker`) }),
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
interval(output, start, end, label, score) {
|
|
49
|
+
const what = `interval "${String(output)}"`;
|
|
50
|
+
emits.push({
|
|
51
|
+
output: text(output, "an emit's output"),
|
|
52
|
+
start: ticks(start, `${what}: start`),
|
|
53
|
+
end: ticks(end, `${what}: end`),
|
|
54
|
+
...(label === undefined
|
|
55
|
+
? {}
|
|
56
|
+
: { label: text(label, `${what}: label`) }),
|
|
57
|
+
...(score === undefined
|
|
58
|
+
? {}
|
|
59
|
+
: { score: finite(score, `${what}: score`) }),
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Where `value` breaks `schema`, as a path and reason, or `undefined`. Covers the JSON Schema
|
|
67
|
+
* keywords descriptors use, which are `type`, `enum`, `const`, `properties`, `required`,
|
|
68
|
+
* `additionalProperties`, `items`, `minItems`, `maxItems`, `minimum`, `maximum`, `minLength` and
|
|
69
|
+
* `maxLength`.
|
|
70
|
+
*/
|
|
71
|
+
export function schemaProblem(schema, value, at = "params") {
|
|
72
|
+
if (typeof schema !== "object" || schema === null)
|
|
73
|
+
return undefined;
|
|
74
|
+
const rules = schema;
|
|
75
|
+
const kind = (item) => item === null
|
|
76
|
+
? "null"
|
|
77
|
+
: Array.isArray(item)
|
|
78
|
+
? "array"
|
|
79
|
+
: Number.isInteger(item)
|
|
80
|
+
? "integer"
|
|
81
|
+
: typeof item;
|
|
82
|
+
if (rules.type !== undefined) {
|
|
83
|
+
const types = Array.isArray(rules.type) ? rules.type : [rules.type];
|
|
84
|
+
const actual = kind(value);
|
|
85
|
+
const fits = types.some((type) => type === actual || (type === "number" && actual === "integer"));
|
|
86
|
+
if (!fits)
|
|
87
|
+
return `${at} must be ${types.join(" or ")}`;
|
|
88
|
+
}
|
|
89
|
+
if (Array.isArray(rules.enum) &&
|
|
90
|
+
!rules.enum.some((item) => JSON.stringify(item) === JSON.stringify(value)))
|
|
91
|
+
return `${at} must be one of ${JSON.stringify(rules.enum)}`;
|
|
92
|
+
if ("const" in rules && JSON.stringify(rules.const) !== JSON.stringify(value))
|
|
93
|
+
return `${at} must be ${JSON.stringify(rules.const)}`;
|
|
94
|
+
if (typeof value === "number") {
|
|
95
|
+
if (typeof rules.minimum === "number" && value < rules.minimum)
|
|
96
|
+
return `${at} must be at least ${rules.minimum}`;
|
|
97
|
+
if (typeof rules.maximum === "number" && value > rules.maximum)
|
|
98
|
+
return `${at} must be at most ${rules.maximum}`;
|
|
99
|
+
}
|
|
100
|
+
if (typeof value === "string") {
|
|
101
|
+
if (typeof rules.minLength === "number" && value.length < rules.minLength)
|
|
102
|
+
return `${at} is too short`;
|
|
103
|
+
if (typeof rules.maxLength === "number" && value.length > rules.maxLength)
|
|
104
|
+
return `${at} is too long`;
|
|
105
|
+
}
|
|
106
|
+
if (Array.isArray(value)) {
|
|
107
|
+
if (typeof rules.minItems === "number" && value.length < rules.minItems)
|
|
108
|
+
return `${at} has too few items`;
|
|
109
|
+
if (typeof rules.maxItems === "number" && value.length > rules.maxItems)
|
|
110
|
+
return `${at} has too many items`;
|
|
111
|
+
for (const [index, item] of value.entries()) {
|
|
112
|
+
const problem = schemaProblem(rules.items, item, `${at}[${index}]`);
|
|
113
|
+
if (problem !== undefined)
|
|
114
|
+
return problem;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
118
|
+
const record = value;
|
|
119
|
+
const properties = (rules.properties ?? {});
|
|
120
|
+
for (const name of Array.isArray(rules.required) ? rules.required : []) {
|
|
121
|
+
if (!(String(name) in record))
|
|
122
|
+
return `${at}.${String(name)} is required`;
|
|
123
|
+
}
|
|
124
|
+
for (const [name, item] of Object.entries(record)) {
|
|
125
|
+
if (name in properties) {
|
|
126
|
+
const problem = schemaProblem(properties[name], item, `${at}.${name}`);
|
|
127
|
+
if (problem !== undefined)
|
|
128
|
+
return problem;
|
|
129
|
+
}
|
|
130
|
+
else if (rules.additionalProperties === false) {
|
|
131
|
+
return `${at}.${name} is not allowed`;
|
|
132
|
+
}
|
|
133
|
+
else if (typeof rules.additionalProperties === "object") {
|
|
134
|
+
const problem = schemaProblem(rules.additionalProperties, item, `${at}.${name}`);
|
|
135
|
+
if (problem !== undefined)
|
|
136
|
+
return problem;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
package/dist/float32.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** The shortest decimal that reads back as the same 32-bit float, as the engine writes it. */
|
|
2
|
+
export function shortestFloat32(value) {
|
|
3
|
+
const target = Math.fround(value);
|
|
4
|
+
for (let digits = 1; digits < 9; digits++) {
|
|
5
|
+
const short = Number(target.toPrecision(digits));
|
|
6
|
+
if (Math.fround(short) === target)
|
|
7
|
+
return short;
|
|
8
|
+
}
|
|
9
|
+
return Number(target.toPrecision(9));
|
|
10
|
+
}
|
package/dist/frame-intake.d.ts
CHANGED
|
@@ -53,6 +53,11 @@ export declare function intakeVideoFrame(frame: VideoFrame, options: VideoFrameI
|
|
|
53
53
|
* failures. Rejects with {@link NonPlanarVideoFrameError} or the last read's error.
|
|
54
54
|
*/
|
|
55
55
|
export declare function readPlanesWithRetries(frame: VideoFrame, declared: ColorMetadata | null, identity: PlanarFrameIdentity, pool: PlanarCopyPool, counters: Pick<ReadbackCounters, "readbackRetries">): Promise<PlanarYuvFrame>;
|
|
56
|
+
/**
|
|
57
|
+
* Runs `read`, retrying up to {@link READBACK_RETRIES} times a task apart while it fails with
|
|
58
|
+
* {@link isUnreadableFrame}. Rejects with the last read's error.
|
|
59
|
+
*/
|
|
60
|
+
export declare function withReadbackRetries<T>(read: () => Promise<T>, onRetry?: () => void): Promise<T>;
|
|
56
61
|
/**
|
|
57
62
|
* Whether a planar copy failed because the browser could not read back a GPU-backed frame. Chrome
|
|
58
63
|
* intermittently rejects `copyTo` on hardware-decoded frames with `InvalidStateError`. A closed
|
package/dist/frame-intake.js
CHANGED
|
@@ -42,15 +42,22 @@ export async function intakeVideoFrame(frame, options) {
|
|
|
42
42
|
* Reads the frame's planes, retrying up to {@link READBACK_RETRIES} times a task apart on readback
|
|
43
43
|
* failures. Rejects with {@link NonPlanarVideoFrameError} or the last read's error.
|
|
44
44
|
*/
|
|
45
|
-
export
|
|
45
|
+
export function readPlanesWithRetries(frame, declared, identity, pool, counters) {
|
|
46
|
+
return withReadbackRetries(() => readPlanarYuvFrame(frame, declared, identity, pool), () => (counters.readbackRetries += 1));
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Runs `read`, retrying up to {@link READBACK_RETRIES} times a task apart while it fails with
|
|
50
|
+
* {@link isUnreadableFrame}. Rejects with the last read's error.
|
|
51
|
+
*/
|
|
52
|
+
export async function withReadbackRetries(read, onRetry = () => { }) {
|
|
46
53
|
for (let retry = 0;; retry += 1) {
|
|
47
54
|
try {
|
|
48
|
-
return await
|
|
55
|
+
return await read();
|
|
49
56
|
}
|
|
50
57
|
catch (error) {
|
|
51
58
|
if (retry >= READBACK_RETRIES || !isUnreadableFrame(error))
|
|
52
59
|
throw error;
|
|
53
|
-
|
|
60
|
+
onRetry();
|
|
54
61
|
await nextTask();
|
|
55
62
|
}
|
|
56
63
|
}
|