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.
Files changed (91) hide show
  1. package/README.md +71 -5
  2. package/THIRD_PARTY_NOTICES.md +191 -1
  3. package/dist/apply-edit.d.ts +124 -0
  4. package/dist/apply-edit.js +15 -0
  5. package/dist/assets.d.ts +12 -0
  6. package/dist/audio-layout.js +6 -3
  7. package/dist/audio-worklet-processor.js +27 -33
  8. package/dist/audio.d.ts +5 -4
  9. package/dist/audio.js +8 -6
  10. package/dist/compose.d.ts +105 -0
  11. package/dist/compose.js +63 -0
  12. package/dist/decoder-fallback.d.ts +3 -2
  13. package/dist/decoder-fallback.js +11 -3
  14. package/dist/decoder-stall.d.ts +15 -0
  15. package/dist/decoder-stall.js +52 -0
  16. package/dist/edit-data.d.ts +24 -0
  17. package/dist/edit-data.js +224 -0
  18. package/dist/edit-keys.d.ts +16 -0
  19. package/dist/edit-keys.js +233 -0
  20. package/dist/edit.d.ts +41 -8
  21. package/dist/edit.js +123 -30
  22. package/dist/export-failure.d.ts +10 -2
  23. package/dist/export-failure.js +55 -4
  24. package/dist/export-retry.d.ts +6 -1
  25. package/dist/export-retry.js +12 -1
  26. package/dist/exporter.d.ts +11 -7
  27. package/dist/exporter.js +10 -9
  28. package/dist/extensions.d.ts +86 -28
  29. package/dist/extensions.js +204 -61
  30. package/dist/extract.d.ts +65 -0
  31. package/dist/extract.js +291 -0
  32. package/dist/extractor.d.ts +151 -0
  33. package/dist/extractor.js +141 -0
  34. package/dist/float32.d.ts +2 -0
  35. package/dist/float32.js +10 -0
  36. package/dist/frame-intake.d.ts +5 -0
  37. package/dist/frame-intake.js +10 -3
  38. package/dist/generated/builtin-effects.d.ts +3 -0
  39. package/dist/generated/builtin-effects.js +1804 -0
  40. package/dist/generated/project-schema.d.ts +696 -41
  41. package/dist/generated/project-schema.js +67 -0
  42. package/dist/generated/project-shape.js +986 -383
  43. package/dist/index.d.ts +1 -0
  44. package/dist/index.js +1 -0
  45. package/dist/json-text.js +14 -2
  46. package/dist/layout-values.js +7 -1
  47. package/dist/layout.d.ts +12 -4
  48. package/dist/media.d.ts +44 -11
  49. package/dist/media.js +256 -67
  50. package/dist/mediabunny.d.ts +32 -12
  51. package/dist/mediabunny.js +276 -90
  52. package/dist/player.d.ts +9 -3
  53. package/dist/player.js +114 -18
  54. package/dist/plugins.d.ts +62 -0
  55. package/dist/plugins.js +506 -0
  56. package/dist/project-data.d.ts +56 -0
  57. package/dist/project-data.js +765 -0
  58. package/dist/project.d.ts +36 -1
  59. package/dist/project.js +515 -42
  60. package/dist/robustness.d.ts +6 -33
  61. package/dist/robustness.js +2 -63
  62. package/dist/sha256.d.ts +3 -2
  63. package/dist/sha256.js +14 -6
  64. package/dist/wasm-module.d.ts +37 -0
  65. package/dist/wasm-module.js +47 -0
  66. package/dist/worker-error.d.ts +3 -2
  67. package/dist/worker-error.js +3 -1
  68. package/dist/worker.d.ts +13 -3
  69. package/dist/worker.js +36 -9
  70. package/package.json +36 -2
  71. package/wasm/alpha2_compose.d.ts +92 -0
  72. package/wasm/alpha2_compose.js +445 -0
  73. package/wasm/alpha2_compose_bg.wasm +0 -0
  74. package/wasm/alpha2_compose_bg.wasm.d.ts +17 -0
  75. package/wasm/alpha2_extensions.d.ts +4 -4
  76. package/wasm/alpha2_extensions.js +16 -4
  77. package/wasm/alpha2_extensions_bg.wasm +0 -0
  78. package/wasm/alpha2_extensions_pdf.d.ts +227 -0
  79. package/wasm/alpha2_extensions_pdf.js +841 -0
  80. package/wasm/alpha2_extensions_pdf_bg.wasm +0 -0
  81. package/wasm/alpha2_extensions_pdf_bg.wasm.d.ts +38 -0
  82. package/wasm/alpha2_wasm.d.ts +151 -18
  83. package/wasm/alpha2_wasm.js +403 -59
  84. package/wasm/alpha2_wasm_bg.wasm +0 -0
  85. package/wasm/alpha2_wasm_bg.wasm.d.ts +21 -6
  86. package/wasm/rust-crate-licenses.md +493 -54
  87. package/wasm/snippets/alpha2-wasm-1b02e9f302d283ba/inline3.js +7 -0
  88. /package/wasm/snippets/{alpha2-wasm-a21734ffde826659/inline1.js → alpha2-wasm-1b02e9f302d283ba/inline0.js} +0 -0
  89. /package/wasm/snippets/{alpha2-wasm-a21734ffde826659/inline2.js → alpha2-wasm-1b02e9f302d283ba/inline1.js} +0 -0
  90. /package/wasm/snippets/{alpha2-wasm-a21734ffde826659/inline3.js → alpha2-wasm-1b02e9f302d283ba/inline2.js} +0 -0
  91. /package/wasm/snippets/{alpha2-wasm-a21734ffde826659/inline0.js → alpha2-wasm-1b02e9f302d283ba/inline4.js} +0 -0
package/dist/player.js CHANGED
@@ -8,6 +8,10 @@ import { decoderConsumerId, } from "./media.js";
8
8
  * It keeps no clock: `present` shows the time it is given. For playback with audio, pass
9
9
  * `AudioPlaybackClock.timelineTicks` so the picture follows the audio device.
10
10
  *
11
+ * At most one presentation runs on the backend at a time, and at most one waits: a `present` or
12
+ * `seek` that arrives while another is waiting replaces it, so a caller may present on every
13
+ * animation frame without building a backlog.
14
+ *
11
15
  * A presentation that lacks source frames asks `onRefill` for them, then presents the same time
12
16
  * again. Frames uploaded after `onRefill` resolves show only on the next presentation.
13
17
  *
@@ -21,8 +25,16 @@ import { decoderConsumerId, } from "./media.js";
21
25
  export class Alpha2Player {
22
26
  #backend;
23
27
  #initialized = false;
28
+ #disposed = false;
24
29
  #latestRequest = 0;
25
30
  #generation = 0;
31
+ #waiting;
32
+ #running = false;
33
+ // A seek's backend reset still to run; a later request that supersedes the seek runs it.
34
+ #resetOwed = false;
35
+ #seeks = 0;
36
+ // A recovery finished and no presentation ran since, so the next refills are recovery refills.
37
+ #recovered = false;
26
38
  // Skips re-presenting after a refill that uploaded nothing.
27
39
  #uploads = 0;
28
40
  // Shared by every presentation that finds the device lost.
@@ -86,7 +98,8 @@ export class Alpha2Player {
86
98
  }
87
99
  /**
88
100
  * Presents `timeTicks`, recovering a lost device first and refilling missing frames through
89
- * `onRefill`. Resolves `undefined` when a later `present` or `seek` superseded this one.
101
+ * `onRefill`. Resolves `undefined` when a later `present` or `seek` superseded this one, whether
102
+ * it was still waiting (then the backend never saw it) or already running.
90
103
  *
91
104
  * Rejects with a fatal `GPU_DEVICE_LOST` when no renderer can be made (or a fresh surface was
92
105
  * needed without `replaceSurface`); every later presentation rejects the same way.
@@ -97,18 +110,12 @@ export class Alpha2Player {
97
110
  this.#assertInitialized();
98
111
  if (this.#terminal !== undefined)
99
112
  throw this.#terminal;
100
- const request = ++this.#latestRequest;
101
- let reason = "prefetch";
102
- if (this.#recovering !== undefined || this.#backend.isDeviceLost()) {
103
- await this.#recover();
104
- reason = "device-recovery";
105
- }
106
- const result = await this.#presentFilled(timeTicks, request, reason);
107
- return request === this.#latestRequest ? result : undefined;
113
+ return this.#enqueue(timeTicks);
108
114
  }
109
115
  /**
110
116
  * Seeks to `timeTicks`, dropping frames decoded for earlier requests, and presents the target
111
- * once `onRefill` has delivered. Resolves `undefined` when a later request superseded it.
117
+ * once `onRefill` has delivered, recovering a lost device first as `present` does. Resolves
118
+ * `undefined` when a later request superseded it.
112
119
  *
113
120
  * @throws Before {@link Alpha2Player.initialize} has resolved.
114
121
  */
@@ -117,16 +124,97 @@ export class Alpha2Player {
117
124
  this.#generation += 1;
118
125
  this.#softwareFrames.clear();
119
126
  this.#reissued.clear();
120
- const request = ++this.#latestRequest;
121
- await this.#backend.reset();
122
- return this.#presentFilled(timeTicks, request, "seek");
127
+ this.#resetOwed = true;
128
+ this.#seeks += 1;
129
+ return this.#enqueue(timeTicks);
123
130
  }
124
- /** Disposes the backend; a presentation in flight resolves `undefined`. */
131
+ /** Disposes the backend; presentations waiting or in flight resolve `undefined`. */
125
132
  dispose() {
133
+ this.#disposed = true;
126
134
  this.#latestRequest += 1;
135
+ const waiting = this.#waiting;
136
+ this.#waiting = undefined;
137
+ waiting?.resolve(undefined);
127
138
  this.#backend.dispose();
128
139
  this.#initialized = false;
129
140
  }
141
+ #enqueue(timeTicks) {
142
+ const request = ++this.#latestRequest;
143
+ const replaced = this.#waiting;
144
+ const done = new Promise((resolve, reject) => {
145
+ this.#waiting = {
146
+ timeTicks,
147
+ request,
148
+ seeks: this.#seeks,
149
+ resolve,
150
+ reject,
151
+ };
152
+ });
153
+ replaced?.resolve(undefined);
154
+ if (!this.#running)
155
+ void this.#run();
156
+ return done;
157
+ }
158
+ async #run() {
159
+ this.#running = true;
160
+ try {
161
+ for (let next = this.#waiting; next !== undefined; next = this.#waiting) {
162
+ this.#waiting = undefined;
163
+ try {
164
+ next.resolve(await this.#presentOne(next));
165
+ }
166
+ catch (error) {
167
+ // A disposed backend rejects its pending calls.
168
+ if (this.#disposed)
169
+ next.resolve(undefined);
170
+ else
171
+ next.reject(error);
172
+ }
173
+ }
174
+ }
175
+ finally {
176
+ this.#running = false;
177
+ }
178
+ }
179
+ async #presentOne(presentation) {
180
+ try {
181
+ return await this.#presentOnce(presentation);
182
+ }
183
+ catch (error) {
184
+ // A device lost during the presentation is refused by the renderer; recover and retry once.
185
+ if (error?.code !== "GPU_DEVICE_LOST" ||
186
+ error === this.#terminal ||
187
+ this.#disposed ||
188
+ !this.#backend.isDeviceLost())
189
+ throw error;
190
+ return this.#presentOnce(presentation);
191
+ }
192
+ }
193
+ async #presentOnce({ timeTicks, request, seeks, }) {
194
+ if (this.#disposed)
195
+ return undefined;
196
+ if (this.#terminal !== undefined)
197
+ throw this.#terminal;
198
+ if (this.#recovering !== undefined || this.#backend.isDeviceLost()) {
199
+ await this.#recover();
200
+ this.#recovered = true;
201
+ }
202
+ if (request !== this.#latestRequest || this.#disposed)
203
+ return undefined;
204
+ let reason = this.#recovered
205
+ ? "device-recovery"
206
+ : "prefetch";
207
+ if (this.#resetOwed) {
208
+ this.#resetOwed = false;
209
+ await this.#backend.reset();
210
+ reason = "seek";
211
+ }
212
+ this.#recovered = false;
213
+ const result = await this.#presentFilled(timeTicks, request, seeks, reason);
214
+ return request === this.#latestRequest && !this.#disposed
215
+ ? result
216
+ : undefined;
217
+ }
130
218
  #recover() {
131
219
  this.#recovering ??= (async () => {
132
220
  try {
@@ -197,19 +285,27 @@ export class Alpha2Player {
197
285
  }
198
286
  }
199
287
  // Re-presents after each refill while frames are missing. Each round must lack fewer frames
200
- // than the last, so a source that never arrives ends the loop.
201
- async #presentFilled(timeTicks, request, reason) {
288
+ // than the last, so a source that never arrives ends the loop. A presentation superseded by a
289
+ // later `present` still shows the frames it refilled once, so a caller that presents faster than
290
+ // frames arrive sees the picture advance; a seek or recovery ends it at once.
291
+ async #presentFilled(timeTicks, request, seeks, reason) {
292
+ const generation = this.#generation;
293
+ const current = () => generation === this.#generation &&
294
+ seeks === this.#seeks &&
295
+ !this.#disposed;
202
296
  let result = await this.#backend.present(timeTicks);
203
297
  let lacking = Number.POSITIVE_INFINITY;
204
298
  while (result.missingFrames.length > 0 &&
205
299
  result.missingFrames.length < lacking &&
206
- request === this.#latestRequest) {
300
+ current()) {
207
301
  lacking = result.missingFrames.length;
208
302
  const uploads = this.#uploads;
209
303
  await this.#requestMissing(result, reason);
210
- if (uploads === this.#uploads || request !== this.#latestRequest)
304
+ if (uploads === this.#uploads || !current())
211
305
  break;
212
306
  result = await this.#backend.present(timeTicks);
307
+ if (request !== this.#latestRequest)
308
+ break;
213
309
  }
214
310
  return result;
215
311
  }
@@ -0,0 +1,62 @@
1
+ import type { Extractor, ExtractorDescriptor } from "./extract.js";
2
+ /** The ABI this loader implements. */
3
+ export declare const PLUGIN_ABI = "alpha2-plugin/1";
4
+ /** Linear memory a plugin may grow to when the caller sets no cap. */
5
+ export declare const DEFAULT_PLUGIN_MEMORY_BYTES: number;
6
+ /** A plugin that was refused, did not load or failed, with the code every surface uses. */
7
+ export declare class PluginError extends Error {
8
+ readonly code: string;
9
+ constructor(code: string, message: string, options?: ErrorOptions);
10
+ }
11
+ /** Where {@link loadPlugin} reads a plugin from. */
12
+ export interface LoadPluginOptions {
13
+ /** The most linear memory the module may have, in bytes; at most 2 GiB. */
14
+ readonly memoryBytes?: number;
15
+ /** Fetches the manifest's files; `globalThis.fetch` when left out. */
16
+ readonly fetch?: typeof fetch;
17
+ readonly signal?: AbortSignal;
18
+ }
19
+ /** A loaded plugin: its manifest's identity and an extractor per run. */
20
+ export interface LoadedPlugin {
21
+ readonly id: string;
22
+ readonly version: string;
23
+ /** SHA-256 of the artifact that runs, as `made_by.plugin` records it. */
24
+ readonly artifactSha256: string;
25
+ readonly descriptor: ExtractorDescriptor;
26
+ /** An extractor that runs the module with `params`; each run instantiates it anew. */
27
+ extractor(params?: Readonly<Record<string, unknown>>): Extractor;
28
+ }
29
+ /** One record of an `emit` batch, as `docs/plugin-abi.md` lays it out. */
30
+ type PluginRecord = {
31
+ readonly kind: "sample";
32
+ readonly output: number;
33
+ readonly time: number;
34
+ readonly values: (number | null)[];
35
+ } | {
36
+ readonly kind: "word";
37
+ readonly output: number;
38
+ readonly start: number;
39
+ readonly end: number;
40
+ readonly text: string;
41
+ readonly speaker?: string;
42
+ } | {
43
+ readonly kind: "interval";
44
+ readonly output: number;
45
+ readonly start: number;
46
+ readonly end: number;
47
+ readonly label?: string;
48
+ readonly score?: number;
49
+ };
50
+ /** Reads a whole batch; throws a reason for the first record that does not read. */
51
+ export declare function decodeRecords(batch: Uint8Array): PluginRecord[];
52
+ /**
53
+ * Loads the plugin whose manifest is at `manifest` (a URL, absolute or relative to the page) and
54
+ * whose bytes have SHA-256 `sha256`, with its wasm artifact and resources, each checked against
55
+ * the hash and length the manifest pins.
56
+ *
57
+ * Rejects with a {@link PluginError} whose code is `PLUGIN_FETCH_FAILED`, `PLUGIN_HASH_MISMATCH`,
58
+ * `PLUGIN_ABI_UNSUPPORTED`, `PLUGIN_NO_ARTIFACT` (no wasm artifact), `PLUGIN_UNSUPPORTED_FEATURE`,
59
+ * `PLUGIN_LOAD_FAILED` or `PLUGIN_DESCRIPTOR_MISMATCH`.
60
+ */
61
+ export declare function loadPlugin(manifest: string | URL, sha256: string, options?: LoadPluginOptions): Promise<LoadedPlugin>;
62
+ export {};