alpha2 0.4.6 → 0.5.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.
Files changed (119) hide show
  1. package/README.md +5 -4
  2. package/THIRD_PARTY_NOTICES.md +4 -0
  3. package/dist/abort.d.ts +12 -0
  4. package/dist/abort.js +48 -0
  5. package/dist/active-clock.d.ts +35 -0
  6. package/dist/active-clock.js +99 -0
  7. package/dist/assets.d.ts +28 -22
  8. package/dist/assets.js +8 -12
  9. package/dist/audio-layout.d.ts +52 -74
  10. package/dist/audio-layout.js +114 -148
  11. package/dist/audio-timeline.d.ts +89 -117
  12. package/dist/audio-timeline.js +102 -118
  13. package/dist/audio-worker.d.ts +12 -21
  14. package/dist/audio-worker.js +8 -13
  15. package/dist/audio-worklet-processor.js +45 -58
  16. package/dist/audio-worklet.d.ts +45 -76
  17. package/dist/audio-worklet.js +44 -79
  18. package/dist/audio.d.ts +15 -23
  19. package/dist/audio.js +9 -13
  20. package/dist/builder.d.ts +45 -63
  21. package/dist/builder.js +49 -45
  22. package/dist/capabilities.d.ts +99 -16
  23. package/dist/capabilities.js +2 -2
  24. package/dist/captions.d.ts +45 -22
  25. package/dist/captions.js +60 -15
  26. package/dist/chroma-siting.d.ts +24 -41
  27. package/dist/chroma-siting.js +38 -71
  28. package/dist/containers.d.ts +41 -30
  29. package/dist/containers.js +59 -27
  30. package/dist/decoder-fallback.d.ts +13 -0
  31. package/dist/decoder-fallback.js +38 -0
  32. package/dist/decoder-order.d.ts +24 -39
  33. package/dist/decoder-order.js +28 -42
  34. package/dist/edit.d.ts +77 -154
  35. package/dist/edit.js +98 -180
  36. package/dist/effects.d.ts +108 -177
  37. package/dist/effects.js +7 -9
  38. package/dist/export-destination.d.ts +25 -41
  39. package/dist/export-destination.js +20 -32
  40. package/dist/export-failure.d.ts +34 -59
  41. package/dist/export-failure.js +54 -48
  42. package/dist/export-metrics.d.ts +116 -0
  43. package/dist/export-metrics.js +74 -0
  44. package/dist/export-retry.d.ts +46 -29
  45. package/dist/export-retry.js +86 -51
  46. package/dist/exporter.d.ts +67 -116
  47. package/dist/exporter.js +133 -133
  48. package/dist/extensions.d.ts +69 -115
  49. package/dist/extensions.js +55 -82
  50. package/dist/frame-intake.d.ts +92 -0
  51. package/dist/frame-intake.js +114 -0
  52. package/dist/frame-supply.d.ts +96 -162
  53. package/dist/frame-supply.js +181 -171
  54. package/dist/generated/project-schema.d.ts +701 -485
  55. package/dist/generated/project-schema.js +55 -2
  56. package/dist/generated/project-shape.js +261 -16
  57. package/dist/geometry.d.ts +28 -34
  58. package/dist/geometry.js +18 -23
  59. package/dist/gpu-recovery.d.ts +92 -0
  60. package/dist/gpu-recovery.js +135 -0
  61. package/dist/hdr-frames.d.ts +82 -67
  62. package/dist/hdr-frames.js +112 -49
  63. package/dist/index.d.ts +4 -0
  64. package/dist/index.js +4 -0
  65. package/dist/json-text.d.ts +13 -23
  66. package/dist/json-text.js +10 -20
  67. package/dist/layout-values.d.ts +14 -22
  68. package/dist/layout-values.js +18 -21
  69. package/dist/layout.d.ts +48 -63
  70. package/dist/layout.js +37 -37
  71. package/dist/loudness.d.ts +27 -32
  72. package/dist/loudness.js +19 -23
  73. package/dist/media.d.ts +85 -237
  74. package/dist/media.js +133 -287
  75. package/dist/mediabunny.d.ts +166 -245
  76. package/dist/mediabunny.js +741 -580
  77. package/dist/physical-frames.d.ts +5 -17
  78. package/dist/physical-frames.js +4 -11
  79. package/dist/player.d.ts +56 -47
  80. package/dist/player.js +155 -40
  81. package/dist/presented-frames.d.ts +2 -7
  82. package/dist/presented-frames.js +3 -8
  83. package/dist/project.d.ts +73 -117
  84. package/dist/project.js +384 -445
  85. package/dist/rate-control.d.ts +9 -23
  86. package/dist/rate-control.js +7 -18
  87. package/dist/renderer-choice.d.ts +41 -0
  88. package/dist/renderer-choice.js +82 -0
  89. package/dist/repacked-bytes.d.ts +0 -2
  90. package/dist/repacked-bytes.js +2 -5
  91. package/dist/robustness.d.ts +25 -37
  92. package/dist/robustness.js +71 -54
  93. package/dist/sha256.d.ts +5 -13
  94. package/dist/sha256.js +9 -24
  95. package/dist/text.d.ts +97 -71
  96. package/dist/text.js +33 -70
  97. package/dist/video-frames.d.ts +7 -25
  98. package/dist/video-frames.js +12 -29
  99. package/dist/waveform.d.ts +5 -11
  100. package/dist/waveform.js +4 -9
  101. package/dist/worker-error.d.ts +5 -20
  102. package/dist/worker-error.js +8 -25
  103. package/dist/worker.d.ts +82 -95
  104. package/dist/worker.js +235 -143
  105. package/package.json +26 -2
  106. package/wasm/alpha2_audio_worklet.wasm +0 -0
  107. package/wasm/alpha2_extensions.d.ts +26 -47
  108. package/wasm/alpha2_extensions.js +26 -47
  109. package/wasm/alpha2_extensions_bg.wasm +0 -0
  110. package/wasm/alpha2_wasm.d.ts +460 -567
  111. package/wasm/alpha2_wasm.js +594 -594
  112. package/wasm/alpha2_wasm_bg.wasm +0 -0
  113. package/wasm/alpha2_wasm_bg.wasm.d.ts +15 -8
  114. package/wasm/rust-crate-licenses.md +2 -2
  115. package/wasm/snippets/{alpha2-wasm-c62e60c964e88a52 → alpha2-wasm-1313375d424845c0}/inline1.js +1 -0
  116. package/wasm/snippets/alpha2-wasm-1313375d424845c0/inline2.js +82 -0
  117. package/wasm/snippets/alpha2-wasm-c62e60c964e88a52/inline2.js +0 -52
  118. /package/wasm/snippets/{alpha2-wasm-c62e60c964e88a52 → alpha2-wasm-1313375d424845c0}/inline0.js +0 -0
  119. /package/wasm/snippets/{alpha2-wasm-c62e60c964e88a52 → alpha2-wasm-1313375d424845c0}/inline3.js +0 -0
@@ -1,17 +1,15 @@
1
- // Browser extension providers (SVG, animated images, Lottie, OpenEXR, image sequences), run by the
2
- // native host's Rust code from a wasm module loaded only when a page opens such a source.
1
+ // Browser extension providers (SVG, animated images, Lottie, OpenEXR, image sequences), running the
2
+ // native host's Rust code in a lazily loaded wasm module.
3
3
  /**
4
- * The provider module stopped inside the call that throws this: a panic in a provider, or memory it
5
- * could not allocate, which in wasm aborts the instance. The module has been restarted, so later
6
- * calls work; the source the call was for, if any, stays failed.
4
+ * The provider module aborted during this call (a panic or failed allocation). The module has been
5
+ * restarted so later calls work, but the source involved stays failed.
7
6
  */
8
7
  export class ExtensionModuleError extends Error {
9
8
  name = "ExtensionModuleError";
10
9
  }
11
10
  /**
12
- * The source was opened before the provider module restarted (see {@link ExtensionModuleError}),
13
- * and the new instance does not hold it: open it again, after registering again the resources it
14
- * reads.
11
+ * The source was opened before the provider module restarted. Re-register its resources and open
12
+ * it again.
15
13
  */
16
14
  export class StaleExtensionSourceError extends Error {
17
15
  name = "StaleExtensionSourceError";
@@ -20,32 +18,27 @@ export class StaleExtensionSourceError extends Error {
20
18
  }
21
19
  }
22
20
  /**
23
- * Whether `error` is the instance aborting: a trap (a panic or a failed allocation), or its stack
24
- * running out. Either leaves it unusable: Rust's state is mid-update, and every object a call had
25
- * borrowed stays borrowed.
21
+ * Whether `error` is a trap or stack overflow. Either leaves the instance unusable, with Rust state
22
+ * mid-update and borrows never released.
26
23
  */
27
24
  function isTrap(error) {
28
25
  return (error instanceof WebAssembly.RuntimeError || error instanceof RangeError);
29
26
  }
30
27
  /**
31
- * The provider module. Load it with {@link loadExtensions}; one instance serves every source a page
32
- * opens.
28
+ * The provider module, shared by every source on the page. Load it with {@link loadExtensions}.
33
29
  *
34
- * Providers parse untrusted files. A panic in one aborts the wasm instance, so a call that aborts
35
- * it restarts the module instead of leaving every later call to fail: it throws {@link
36
- * ExtensionModuleError}, the fonts {@link setFonts} named are registered again, and the sources and
37
- * resources of the old instance are gone. A source opened before then throws {@link
38
- * StaleExtensionSourceError} until it is opened again, with its resources registered again; the one
39
- * whose call aborted the module stays failed.
30
+ * Providers parse untrusted files, and a panic aborts the wasm instance. The call that aborted it
31
+ * throws {@link ExtensionModuleError} and the module restarts with its fonts re-registered, but
32
+ * sources and resources are lost. Older sources throw {@link StaleExtensionSourceError} until
33
+ * reopened.
40
34
  */
41
35
  export class Extensions {
42
36
  #module;
43
37
  #wasm;
44
- /** Counts restarts: a source of an earlier generation is stale. */
38
+ /** Restart count; sources from an earlier generation are stale. */
45
39
  #generation = 0;
46
- /** Why the module could not be restarted, if it could not. */
40
+ /** Set when a restart failed. */
47
41
  #broken = null;
48
- /** The fonts {@link setFonts} registered, in order. */
49
42
  #fonts = [];
50
43
  #calls;
51
44
  /** Use {@link loadExtensions}. */
@@ -73,7 +66,7 @@ export class Extensions {
73
66
  #restart() {
74
67
  this.#generation += 1;
75
68
  try {
76
- // Reuses the compiled module, so this neither fetches nor compiles.
69
+ // Reuses the compiled module; no fetch or compile.
77
70
  this.#module.__wbg_reset_state();
78
71
  const wasm = new this.#module.ExtensionRegistry();
79
72
  for (const font of this.#fonts)
@@ -84,17 +77,17 @@ export class Extensions {
84
77
  this.#broken = error instanceof Error ? error : new Error(String(error));
85
78
  }
86
79
  }
87
- /** The providers the module was built with, in priority order. */
80
+ /** Built-in providers, in priority order. */
88
81
  get providers() {
89
82
  return JSON.parse(this.#call((wasm) => wasm.providers()));
90
83
  }
91
- /** Whether the module opens a project's provider source. */
84
+ /** Whether the module can open this provider source. */
92
85
  supports(provider) {
93
86
  return this.#call((wasm) => wasm.supports(JSON.stringify(provider)));
94
87
  }
95
88
  /**
96
- * What the first provider that claims `bytes` finds, or `null` when none does: then the bytes are
97
- * the host's own decoders' to read.
89
+ * Probes `bytes` with the first provider that claims them, or returns `null` so the host's own
90
+ * decoders can read them.
98
91
  *
99
92
  * @throws When a provider claims the bytes and cannot read them.
100
93
  */
@@ -105,38 +98,31 @@ export class Extensions {
105
98
  : JSON.parse(found);
106
99
  }
107
100
  /**
108
- * Registers bytes under a locator: an image sequence's frame, or an image an SVG names. They stay
109
- * until released; {@link withResources} registers them only for the call that reads them. A
110
- * restart forgets them (see {@link Extensions}).
101
+ * Registers bytes under a locator, such as an image sequence frame or an image an SVG references.
102
+ * They stay until released or a module restart; {@link withResources} scopes them to one call.
111
103
  */
112
104
  registerResource(locator, bytes) {
113
105
  this.#call((wasm) => wasm.registerResource(locator, bytes));
114
106
  }
115
- /** Forgets the bytes registered under a locator. */
116
107
  releaseResource(locator) {
117
108
  this.#call((wasm) => wasm.releaseResource(locator));
118
109
  }
119
- /** The bytes registered under locators, all together. */
110
+ /** Total registered resource bytes. */
120
111
  get resourceBytes() {
121
112
  return this.#call((wasm) => wasm.resourceBytes());
122
113
  }
123
- /**
124
- * The resources opening a project's provider source reads, by locator: an image sequence's first
125
- * frame.
126
- */
114
+ /** Locators of resources opening this source reads, such as an image sequence's first frame. */
127
115
  openNeeds(provider) {
128
116
  return JSON.parse(this.#call((wasm) => wasm.openNeeds(JSON.stringify(provider))));
129
117
  }
130
118
  /**
131
- * Runs `call` with the resources `needs` names registered, and only for it: each is read with
132
- * `read`, registered just before `call` runs, and released when it returns or throws. So an image
133
- * sequence's frames enter the module one render at a time, never the whole sequence, and nothing
134
- * stays registered after.
119
+ * Runs `call` with the resources `needs` names registered only for its duration. Each is
120
+ * fetched with `read`, registered just before `call`, and released afterwards, so an image
121
+ * sequence enters the module one frame at a time.
135
122
  *
136
- * `needs` is asked again once the reads finish, in the same task as `call`: what another call
137
- * decoded or let go meanwhile is accounted for, and no other call can release what this one
138
- * registered. A locator registered before, by {@link registerResource}, is released after the
139
- * call too: a page registers a resource one way or the other.
123
+ * `needs` is re-evaluated after the reads, in the same task as `call`, so concurrent calls cannot
124
+ * interfere. Locators previously registered with {@link registerResource} are also released, so
125
+ * use one mechanism or the other.
140
126
  *
141
127
  * @example
142
128
  * const source = await extensions.withResources(
@@ -163,8 +149,7 @@ export class Extensions {
163
149
  }
164
150
  finally {
165
151
  for (const locator of wanted) {
166
- // A call that stopped the module took its resources with it: the new instance has none
167
- // to release, and one that could not restart answers nothing.
152
+ // After a restart there is nothing to release, and a broken module throws.
168
153
  try {
169
154
  this.releaseResource(locator);
170
155
  }
@@ -179,14 +164,10 @@ export class Extensions {
179
164
  }
180
165
  }
181
166
  /**
182
- * The fonts an SVG's text draws with, in fallback order: they replace those set before. A source
183
- * reads them when it opens, so one opened before keeps the fonts it opened with; set them and
184
- * open in the same task to open with them. Setting the same fonts again (the same arrays, in the
185
- * same order) copies nothing into the module. They are kept, to be registered again if the module
186
- * restarts.
167
+ * Replaces the fonts SVG text draws with, in fallback order. Sources read fonts when they open,
168
+ * so existing sources keep their old fonts. Passing the same arrays again is a no-op.
187
169
  *
188
- * The native host draws an SVG's text with its caption fonts; a page that shows the same project
189
- * should give the same stack so both draw the same glyphs.
170
+ * Use the same font stack as the native host's caption fonts to get identical glyphs.
190
171
  */
191
172
  setFonts(fonts) {
192
173
  if (fonts.length === this.#fonts.length &&
@@ -200,8 +181,8 @@ export class Extensions {
200
181
  this.#fonts = [...fonts];
201
182
  }
202
183
  /**
203
- * Opens a project's provider source with its own bytes (none for an image sequence, whose frames
204
- * are registered resources: see {@link openNeeds} and {@link withResources}).
184
+ * Opens a provider source. Image sequences take no bytes; their frames are registered resources
185
+ * (see {@link openNeeds} and {@link withResources}).
205
186
  *
206
187
  * @throws When the source cannot be opened.
207
188
  */
@@ -212,14 +193,13 @@ export class Extensions {
212
193
  });
213
194
  }
214
195
  }
215
- /** An opened source: asked for frames by source tick. */
196
+ /** An opened source that renders frames by source tick. */
216
197
  export class ExtensionSource {
217
198
  #calls;
218
199
  #generation;
219
200
  #handle;
220
- /** Why this source's own call stopped the module: it is not run again. */
201
+ /** Set when this source's call aborted the module; the source is not run again. */
221
202
  #failed = null;
222
- /** What the source holds, as read when it opened. */
223
203
  report;
224
204
  /** Use {@link Extensions.open}. */
225
205
  constructor(calls, handle) {
@@ -229,12 +209,12 @@ export class ExtensionSource {
229
209
  this.report = JSON.parse(calls.call((wasm) => wasm.report(handle)));
230
210
  }
231
211
  /**
232
- * The frame shown at a source tick. A vector source is rasterized to fit `fit` keeping its shape;
233
- * a raster source answers at its own size.
212
+ * Renders the frame at a source tick. Vector sources are rasterized to fit `fit` preserving
213
+ * aspect; raster sources render at their own size.
234
214
  *
235
215
  * @throws {StaleExtensionSourceError} When the module restarted since the source was opened.
236
- * @throws {ExtensionModuleError} When this source's render stopped the module, then and on every
237
- * later render.
216
+ * @throws {ExtensionModuleError} When this source's render aborted the module, then and on every
217
+ * later render.
238
218
  */
239
219
  render(timeTicks, fit) {
240
220
  const handle = this.#handle;
@@ -273,9 +253,8 @@ export class ExtensionSource {
273
253
  }
274
254
  }
275
255
  /**
276
- * The resources rendering at a source tick reads, by locator: those the source does not already
277
- * hold, such as an image sequence's frame it has not decoded. Empty for a source that reads none,
278
- * or a stale or released one.
256
+ * Locators of resources rendering at a source tick still needs. Empty for stale or released
257
+ * sources.
279
258
  */
280
259
  needs(timeTicks) {
281
260
  const handle = this.#handle;
@@ -286,9 +265,8 @@ export class ExtensionSource {
286
265
  return JSON.parse(this.#calls.call((wasm) => wasm.needs(handle, timeTicks)));
287
266
  }
288
267
  /**
289
- * The bytes the source holds in the module between renders: its decoded frames and images, its
290
- * last rendered frame, its file's bytes when it keeps them. A host that bounds what its open
291
- * sources hold counts this and releases a source to free it. 0 for a stale or released source.
268
+ * Module memory the source holds between renders (decoded frames, last output, file bytes). 0 for
269
+ * stale or released sources.
292
270
  */
293
271
  get residentBytes() {
294
272
  const handle = this.#handle;
@@ -299,8 +277,8 @@ export class ExtensionSource {
299
277
  return this.#calls.call((wasm) => wasm.residentBytes(handle));
300
278
  }
301
279
  /**
302
- * Of {@link residentBytes}, what the source shares with the readers {@link reader} made from it:
303
- * the same bytes for each of them, which a host counts once. 0 for a stale or released source.
280
+ * The part of {@link residentBytes} shared with readers from {@link reader}; count it once. 0 for
281
+ * stale or released sources.
304
282
  */
305
283
  get sharedBytes() {
306
284
  const handle = this.#handle;
@@ -311,12 +289,9 @@ export class ExtensionSource {
311
289
  return this.#calls.call((wasm) => wasm.sharedBytes(handle));
312
290
  }
313
291
  /**
314
- * Another reader of this source, positioned on its own: it shares what the source decoded when it
315
- * opened (an animated image's file and the frames it keeps) and decodes on its own, so readers
316
- * far apart do not send each other back to the first frame, and the file is decoded once for all
317
- * of them. Readers stay valid after the source they were made from is released. `null` for a
318
- * source that draws any time as cheaply (one source serves every reader), or one whose render
319
- * stopped the module.
292
+ * Creates an independently positioned reader that shares this source's decoded data, so distant
293
+ * readers do not force each other back to the first frame. Readers outlive the source. `null`
294
+ * when random access is already cheap, or when this source aborted the module.
320
295
  *
321
296
  * @throws {StaleExtensionSourceError} When the module restarted since the source was opened.
322
297
  */
@@ -334,24 +309,22 @@ export class ExtensionSource {
334
309
  : new ExtensionSource(this.#calls, reader);
335
310
  }
336
311
  /**
337
- * Frees the source in the module. Idempotent; later renders throw. Like every module call, it
338
- * throws {@link ExtensionModuleError} if the release itself stops the module.
312
+ * Frees the source. Idempotent; later renders throw. Throws {@link ExtensionModuleError} if the
313
+ * release itself aborts the module.
339
314
  */
340
315
  release() {
341
316
  const handle = this.#handle;
342
317
  if (handle === null)
343
318
  return;
344
319
  this.#handle = null;
345
- // A source of an earlier instance has nothing left to release.
346
320
  if (this.#generation === this.#calls.generation())
347
321
  this.#calls.call((wasm) => wasm.release(handle));
348
322
  }
349
323
  }
350
324
  let loading = null;
351
325
  /**
352
- * Loads the provider module once per page. `wasmUrl` is where a bundler put
353
- * `alpha2_extensions_bg.wasm`; without it the module is found next to its glue. A load that fails
354
- * (the fetch, the instantiation) is not kept: the next call tries again.
326
+ * Loads the provider module once per page. `wasmUrl` locates `alpha2_extensions_bg.wasm` when a
327
+ * bundler moved it. A failed load is not cached.
355
328
  */
356
329
  export function loadExtensions(wasmUrl) {
357
330
  loading ??= (async () => {
@@ -0,0 +1,92 @@
1
+ import type { ColorMetadata } from "./generated/project-schema.js";
2
+ import { type PlanarCopyPool, type PlanarFrameIdentity, type PlanarYuvFrame } from "./hdr-frames.js";
3
+ /** Retries of a failed plane readback before falling back to a new decode. */
4
+ export declare const READBACK_RETRIES = 2;
5
+ /** Cumulative counts of frames the browser could not read back. */
6
+ export interface ReadbackRecoveryStats {
7
+ /** Frames still unreadable after retries, plus required frames with no readable planar format. */
8
+ readonly unreadableFrames: number;
9
+ readonly readbackRetries: number;
10
+ /** Frames decoded again in software. */
11
+ readonly redecodedFrames: number;
12
+ /** Frames given to the browser's conversion (`unreadableFrames: "present"`). */
13
+ readonly presentedFrames: number;
14
+ }
15
+ export type ReadbackCounters = {
16
+ -readonly [Key in keyof ReadbackRecoveryStats]: ReadbackRecoveryStats[Key];
17
+ };
18
+ export declare function readbackCounters(): ReadbackCounters;
19
+ /** Outcome of {@link intakeVideoFrame}. The frame stays open in every case. */
20
+ export type VideoFrameIntake = {
21
+ readonly kind: "planar";
22
+ readonly planar: PlanarYuvFrame;
23
+ }
24
+ /** Hand the frame to the browser's conversion (`unreadable: "present"`). */
25
+ | {
26
+ readonly kind: "present";
27
+ }
28
+ /** Decode again in software; `cause` is {@link NonPlanarVideoFrameError} or a readback error. */
29
+ | {
30
+ readonly kind: "redecode";
31
+ readonly cause: unknown;
32
+ };
33
+ export interface VideoFrameIntakeOptions {
34
+ /** Whether the engine must convert the frame (HDR, or project-declared color). */
35
+ readonly required: boolean;
36
+ /** Project-declared color, which overrides the frame's own. */
37
+ readonly declared: ColorMetadata | null;
38
+ readonly identity: PlanarFrameIdentity;
39
+ readonly pool: PlanarCopyPool;
40
+ readonly counters: ReadbackCounters;
41
+ /** Fallback for frames still unreadable after retries; `"present"` is ignored when `required`. */
42
+ readonly unreadable: "redecode" | "present";
43
+ /** Whether the engine accepts presented `VideoFrame` uploads, which `"present"` needs. */
44
+ readonly takesPresented: boolean;
45
+ }
46
+ /**
47
+ * Offers `frame` to the engine's planar conversion. The caller still owns the frame. Rejects when
48
+ * the copy fails for a reason other than an unreadable or non-planar frame.
49
+ */
50
+ export declare function intakeVideoFrame(frame: VideoFrame, options: VideoFrameIntakeOptions): Promise<VideoFrameIntake>;
51
+ /**
52
+ * Reads the frame's planes, retrying up to {@link READBACK_RETRIES} times a task apart on readback
53
+ * failures. Rejects with {@link NonPlanarVideoFrameError} or the last read's error.
54
+ */
55
+ export declare function readPlanesWithRetries(frame: VideoFrame, declared: ColorMetadata | null, identity: PlanarFrameIdentity, pool: PlanarCopyPool, counters: Pick<ReadbackCounters, "readbackRetries">): Promise<PlanarYuvFrame>;
56
+ /**
57
+ * Whether a planar copy failed because the browser could not read back a GPU-backed frame. Chrome
58
+ * intermittently rejects `copyTo` on hardware-decoded frames with `InvalidStateError`. A closed
59
+ * frame gives the same error, but callers only copy frames they own and have not closed.
60
+ */
61
+ export declare function isUnreadableFrame(error: unknown): boolean;
62
+ /** Why a host sent a frame back for a software decode. */
63
+ export type SoftwareDecodeReason = "non-planar" | "unreadable";
64
+ /**
65
+ * Thrown by a render host that needs a frame decoded again in software, because it has no planar
66
+ * format the engine reads (`"non-planar"`, which persists for the source) or its planes could not
67
+ * be read back (`"unreadable"`). Not an export failure: {@link Alpha2Player} and `exportToSink`
68
+ * refill the frame with `decode: "software"`. The request travels in `failure.diagnostics` so it
69
+ * survives a worker boundary.
70
+ */
71
+ export declare class SoftwareDecodeRequiredError extends Error {
72
+ readonly code = "SOFTWARE_DECODE_REQUIRED";
73
+ readonly fatal = false;
74
+ readonly failure: {
75
+ readonly code: "UNKNOWN";
76
+ readonly phase: "decode";
77
+ readonly stage: "software-decode-required";
78
+ readonly diagnostics: SoftwareDecodeRequest;
79
+ };
80
+ constructor(request: SoftwareDecodeRequest, options?: ErrorOptions);
81
+ }
82
+ /** Frame requested by a {@link SoftwareDecodeRequiredError}; the time is decimal ticks. */
83
+ export interface SoftwareDecodeRequest {
84
+ readonly sourceId: string;
85
+ readonly mediaTimeTicks: string;
86
+ readonly reason: SoftwareDecodeReason;
87
+ }
88
+ /**
89
+ * The request carried by a {@link SoftwareDecodeRequiredError}, including one rebuilt from a
90
+ * worker's report; otherwise `undefined`.
91
+ */
92
+ export declare function softwareDecodeRequest(error: unknown): SoftwareDecodeRequest | undefined;
@@ -0,0 +1,114 @@
1
+ // Shared path by which decoded frames reach the engine's planar conversion: read the planes (with
2
+ // retries), hand the frame to the browser's conversion, or request a software decode. Used by
3
+ // `FrameSupply` and the render worker host.
4
+ import { NonPlanarVideoFrameError, readPlanarYuvFrame, } from "./hdr-frames.js";
5
+ /** Retries of a failed plane readback before falling back to a new decode. */
6
+ export const READBACK_RETRIES = 2;
7
+ export function readbackCounters() {
8
+ return {
9
+ unreadableFrames: 0,
10
+ readbackRetries: 0,
11
+ redecodedFrames: 0,
12
+ presentedFrames: 0,
13
+ };
14
+ }
15
+ /**
16
+ * Offers `frame` to the engine's planar conversion. The caller still owns the frame. Rejects when
17
+ * the copy fails for a reason other than an unreadable or non-planar frame.
18
+ */
19
+ export async function intakeVideoFrame(frame, options) {
20
+ try {
21
+ return {
22
+ kind: "planar",
23
+ planar: await readPlanesWithRetries(frame, options.declared, options.identity, options.pool, options.counters),
24
+ };
25
+ }
26
+ catch (error) {
27
+ const nonPlanar = error instanceof NonPlanarVideoFrameError;
28
+ if (!nonPlanar && !isUnreadableFrame(error))
29
+ throw error;
30
+ options.counters.unreadableFrames += 1;
31
+ if (!nonPlanar &&
32
+ options.unreadable === "present" &&
33
+ !options.required &&
34
+ options.takesPresented) {
35
+ options.counters.presentedFrames += 1;
36
+ return { kind: "present" };
37
+ }
38
+ return { kind: "redecode", cause: error };
39
+ }
40
+ }
41
+ /**
42
+ * Reads the frame's planes, retrying up to {@link READBACK_RETRIES} times a task apart on readback
43
+ * failures. Rejects with {@link NonPlanarVideoFrameError} or the last read's error.
44
+ */
45
+ export async function readPlanesWithRetries(frame, declared, identity, pool, counters) {
46
+ for (let retry = 0;; retry += 1) {
47
+ try {
48
+ return await readPlanarYuvFrame(frame, declared, identity, pool);
49
+ }
50
+ catch (error) {
51
+ if (retry >= READBACK_RETRIES || !isUnreadableFrame(error))
52
+ throw error;
53
+ counters.readbackRetries += 1;
54
+ await nextTask();
55
+ }
56
+ }
57
+ }
58
+ /**
59
+ * Whether a planar copy failed because the browser could not read back a GPU-backed frame. Chrome
60
+ * intermittently rejects `copyTo` on hardware-decoded frames with `InvalidStateError`. A closed
61
+ * frame gives the same error, but callers only copy frames they own and have not closed.
62
+ */
63
+ export function isUnreadableFrame(error) {
64
+ return (typeof DOMException !== "undefined" &&
65
+ error instanceof DOMException &&
66
+ error.name === "InvalidStateError");
67
+ }
68
+ /**
69
+ * Thrown by a render host that needs a frame decoded again in software, because it has no planar
70
+ * format the engine reads (`"non-planar"`, which persists for the source) or its planes could not
71
+ * be read back (`"unreadable"`). Not an export failure: {@link Alpha2Player} and `exportToSink`
72
+ * refill the frame with `decode: "software"`. The request travels in `failure.diagnostics` so it
73
+ * survives a worker boundary.
74
+ */
75
+ export class SoftwareDecodeRequiredError extends Error {
76
+ code = "SOFTWARE_DECODE_REQUIRED";
77
+ fatal = false;
78
+ failure;
79
+ constructor(request, options) {
80
+ super(request.reason === "non-planar"
81
+ ? `the frame of ${request.sourceId} at ${request.mediaTimeTicks} has no planar format the engine reads: decode it again in software`
82
+ : `the browser could not read back the frame of ${request.sourceId} at ${request.mediaTimeTicks}: decode it again in software`, options);
83
+ this.name = "SoftwareDecodeRequiredError";
84
+ this.failure = {
85
+ code: "UNKNOWN",
86
+ phase: "decode",
87
+ stage: "software-decode-required",
88
+ diagnostics: { ...request },
89
+ };
90
+ }
91
+ }
92
+ /**
93
+ * The request carried by a {@link SoftwareDecodeRequiredError}, including one rebuilt from a
94
+ * worker's report; otherwise `undefined`.
95
+ */
96
+ export function softwareDecodeRequest(error) {
97
+ const value = error;
98
+ if (value?.code !== "SOFTWARE_DECODE_REQUIRED")
99
+ return undefined;
100
+ const request = value.failure?.diagnostics;
101
+ if (typeof request?.sourceId !== "string" ||
102
+ typeof request.mediaTimeTicks !== "string" ||
103
+ (request.reason !== "non-planar" && request.reason !== "unreadable"))
104
+ return undefined;
105
+ return {
106
+ sourceId: request.sourceId,
107
+ mediaTimeTicks: request.mediaTimeTicks,
108
+ reason: request.reason,
109
+ };
110
+ }
111
+ /** Yields a task so the browser can process GPU process replies. */
112
+ function nextTask() {
113
+ return new Promise((resolve) => setTimeout(resolve, 0));
114
+ }