alpha2 0.4.3 → 0.4.5

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 CHANGED
@@ -37,6 +37,7 @@ engine.renderAt(0n); // draws the frame at time 0 on the canvas
37
37
 
38
38
  - `init()` loads `alpha2_wasm_bg.wasm` from beside the module. A bundler that moves it passes its URL, a `Response`, its bytes or a compiled `WebAssembly.Module` as `init({ module_or_path })`; the files are `alpha2/wasm/alpha2_wasm_bg.wasm` (the engine), `alpha2/wasm/alpha2_audio_worklet.wasm` (the audio worklet) and `alpha2/wasm/alpha2_extensions_bg.wasm` (SVG, animated images, Lottie, OpenEXR and image sequences, loaded only for such a source).
39
39
  - `createForHtmlCanvas` draws on a page canvas; a worker passes an `OffscreenCanvas` to `createForCanvas`. Either renders with WebGPU, or with WebGL2 when the browser has no WebGPU.
40
+ - With neither (Chrome with acceleration off, a blocklisted GPU), `WebEffectRegistry.createCpu(canvas)` makes the engine's CPU renderer: the same calls, drawn in wasm on one thread, the preview put on the canvas's 2D context. It draws exactly what the native CPU renderer draws, refuses runtime WGSL effects with `EFFECT_ERROR`, and needs no canvas to export. `detectBrowserCapabilities()` reports which renderer the browser gets (`renderer`).
40
41
  - A project is the engine's JSON document. `alpha2/project` has its TypeScript types, tick conversions and `validateProject`, which reports what the engine would refuse before the document is sent; `alpha2/builder` writes one with times in seconds. `loadProject` checks the document again and throws what it refuses.
41
42
  - `renderAt(ticks)` returns JSON that names the sources it had no frame for (`missingSources`, `missingFrames`). The engine decides which frame of each source a time needs: `frameNeeds(ticks)` lists them with the exact source time, and the application decodes and uploads those (`uploadVideoFrame` for a `VideoFrame`, `uploadRgbaFrame` for pixels at a source time, `uploadRgbaImage` for a still). `FrameSupply` in `alpha2/frame-supply` does this for a decoder per clip, and keeps the uploads within a byte budget.
42
43
 
@@ -76,7 +77,7 @@ For preview, `alpha2/audio-timeline` moves the mixed blocks to the audio thread
76
77
 
77
78
  ## Preview in a worker
78
79
 
79
- `alpha2/worker` runs the engine in a worker that owns the GPU device and an `OffscreenCanvas` (`installRenderWorker`, `createWasmRenderWorkerHost`), and `WorkerRenderBackend` drives it from the page. `Alpha2Player` in `alpha2/player` presents times through such a backend: it serializes presentations, asks for the frames a presentation lacks and shows them once they arrive, and makes the device again after a device loss.
80
+ `alpha2/worker` runs the engine in a worker that owns the GPU device and an `OffscreenCanvas` (`installRenderWorker`, `createWasmRenderWorkerHost`), and `WorkerRenderBackend` drives it from the page. The host's `renderer` option is `"auto"` (WebGPU, else WebGL2, else the CPU renderer), `"gpu"` or `"cpu"`; `WorkerRenderBackend.rendererChoice` says which renderer the worker made and why it draws on the CPU, as the native host's `renderer` and `rendererReason` do. `Alpha2Player` in `alpha2/player` presents times through such a backend: it serializes presentations, asks for the frames a presentation lacks and shows them once they arrive, and makes the device again after a device loss.
80
81
 
81
82
  ## Subpaths
82
83
 
@@ -10,6 +10,72 @@ The `alpha2` npm package and the native archives each list the third-party Rust
10
10
 
11
11
  The native archives load FFmpeg 8, built as shared libraries under the LGPL 2.1 or later with its GPL and nonfree components disabled. Each archive carries the license texts of FFmpeg and the libraries it is built with in `licenses/`, and says where to get the corresponding source in `compliance/SOURCE.md`.
12
12
 
13
+ ## OpenH264
14
+
15
+ alpha2 does not include OpenH264. On request (`python -m alpha2 install --openh264`, `npx @alpha2-sh/node install --openh264`) it downloads Cisco's prebuilt OpenH264 binary from Cisco to the user's machine, and `alpha2-host` loads it to encode H.264 in software; `ALPHA2_OPENH264=0` turns it off. Cisco's binary comes with the licence below (https://www.openh264.org/BINARY_LICENSE.txt, v1.0, its typographic quotes and copyright sign written in ASCII).
16
+
17
+ OpenH264 Video Codec provided by Cisco Systems, Inc.
18
+
19
+ ```
20
+ -------------------------------------------------------
21
+ About The Cisco-Provided Binary of OpenH264 Video Codec
22
+ -------------------------------------------------------
23
+
24
+ Cisco provides this program under the terms of the BSD license.
25
+
26
+ Additionally, this binary is licensed under Cisco's AVC/H.264 Patent Portfolio License from MPEG LA, at no cost to you, provided that the requirements and conditions shown below in the AVC/H.264 Patent Portfolio sections are met.
27
+
28
+ As with all AVC/H.264 codecs, you may also obtain your own patent license from MPEG LA or from the individual patent owners, or proceed at your own risk. Your rights from Cisco under the BSD license are not affected by this choice.
29
+
30
+ For more information on the OpenH264 binary licensing, please see the OpenH264 FAQ found at http://www.openh264.org/faq.html#binary
31
+
32
+ A corresponding source code to this binary program is available under the same BSD terms, which can be found at http://www.openh264.org
33
+
34
+ -----------
35
+ BSD License
36
+ -----------
37
+
38
+ Copyright (c) 2014 Cisco Systems, Inc.
39
+
40
+ All rights reserved.
41
+
42
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
43
+
44
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
45
+
46
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
47
+
48
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49
+
50
+ -----------------------------------------
51
+ AVC/H.264 Patent Portfolio License Notice
52
+ -----------------------------------------
53
+
54
+ The binary form of this Software is distributed by Cisco under the AVC/H.264 Patent Portfolio License from MPEG LA, and is subject to the following requirements, which may or may not be applicable to your use of this software:
55
+
56
+ THIS PRODUCT IS LICENSED UNDER THE AVC PATENT PORTFOLIO LICENSE FOR THE PERSONAL USE OF A CONSUMER OR OTHER USES IN WHICH IT DOES NOT RECEIVE REMUNERATION TO (i) ENCODE VIDEO IN COMPLIANCE WITH THE AVC STANDARD ("AVC VIDEO") AND/OR (ii) DECODE AVC VIDEO THAT WAS ENCODED BY A CONSUMER ENGAGED IN A PERSONAL ACTIVITY AND/OR WAS OBTAINED FROM A VIDEO PROVIDER LICENSED TO PROVIDE AVC VIDEO. NO LICENSE IS GRANTED OR SHALL BE IMPLIED FOR ANY OTHER USE. ADDITIONAL INFORMATION MAY BE OBTAINED FROM MPEG LA, L.L.C. SEE HTTP://WWW.MPEGLA.COM
57
+
58
+ Accordingly, please be advised that content providers and broadcasters using AVC/H.264 in their service may be required to obtain a separate use license from MPEG LA, referred to as "(b) sublicenses" in the SUMMARY OF AVC/H.264 LICENSE TERMS from MPEG LA found at http://www.openh264.org/mpegla
59
+
60
+ ---------------------------------------------
61
+ AVC/H.264 Patent Portfolio License Conditions
62
+ ---------------------------------------------
63
+
64
+ In addition, the Cisco-provided binary of this Software is licensed under Cisco's license from MPEG LA only if the following conditions are met:
65
+
66
+ 1. The Cisco-provided binary is separately downloaded to an end user's device, and not integrated into or combined with third party software prior to being downloaded to the end user's device;
67
+
68
+ 2. The end user must have the ability to control (e.g., to enable, disable, or re-enable) the use of the Cisco-provided binary;
69
+
70
+ 3. Third party software, in the location where end users can control the use of the Cisco-provided binary, must display the following text:
71
+
72
+ "OpenH264 Video Codec provided by Cisco Systems, Inc."
73
+
74
+ 4. Any third-party software that makes use of the Cisco-provided binary must reproduce all of the above text, as well as this last condition, in the EULA and/or in another location where licensing information is to be presented to the end user.
75
+
76
+ v1.0
77
+ ```
78
+
13
79
  ## gl-transitions
14
80
 
15
81
  The transitions in `crates/alpha2-shaders/shaders/runtime/transitions/` are WGSL ports of shaders from [gl-transitions](https://github.com/gl-transitions/gl-transitions) at commit `902218a1b63773ac0d0d9f491951da3392365bfe`. Each file names the shader it ports and its author.
package/dist/effects.d.ts CHANGED
@@ -150,11 +150,20 @@ export interface WasmEffectRegistry {
150
150
  * samples either way without a replay.
151
151
  */
152
152
  export type AudioContinuity = "exact" | "continue-small-gaps";
153
- /** Generated wasm surface used by the concrete render-worker host. */
153
+ /**
154
+ * Generated wasm surface used by the concrete render-worker host: the GPU renderer
155
+ * (`createForCanvas`) or the CPU one (`createCpu`), which answer the same calls.
156
+ */
154
157
  export interface WasmCanvasRenderer extends WasmEffectRegistry {
158
+ /**
159
+ * Which renderer draws: `"gpu"` (WebGPU or WebGL2) or `"cpu"` (the engine's CPU renderer in
160
+ * wasm, which refuses runtime effects with `EFFECT_ERROR`). Absent means `"gpu"`.
161
+ */
162
+ readonly renderer?: "gpu" | "cpu";
155
163
  /**
156
164
  * Whether the GPU device is gone: a WebGPU device-lost callback, or a WebGL canvas's
157
- * `webglcontextlost` event. A lost renderer rejects export readbacks and must be recreated.
165
+ * `webglcontextlost` event. A lost renderer rejects export readbacks and must be recreated. The
166
+ * CPU renderer is never lost.
158
167
  */
159
168
  readonly deviceLost: boolean;
160
169
  readonly uploadedFrameCount: number;
@@ -422,6 +431,12 @@ export interface WasmEffectRegistryBinding {
422
431
  create(): Promise<WasmEffectRegistry>;
423
432
  createForCanvas?(canvas: OffscreenCanvas): Promise<WasmCanvasRenderer>;
424
433
  createForHtmlCanvas?(canvas: HTMLCanvasElement): Promise<WasmCanvasRenderer>;
434
+ /**
435
+ * The renderer that draws on the CPU, for a browser with no WebGPU adapter and no WebGL2
436
+ * context: it presents on `canvas`'s 2D context (nowhere without a canvas) and needs no GPU. It
437
+ * throws `INVALID_INPUT` for a canvas that already holds a WebGL or WebGPU context.
438
+ */
439
+ createCpu?(canvas?: OffscreenCanvas | HTMLCanvasElement): WasmCanvasRenderer;
425
440
  }
426
441
  /** Typed adapter that keeps JSON serialization out of product integrations. */
427
442
  export declare class RuntimeEffectRegistry {
@@ -179,6 +179,12 @@ export interface MediabunnySinkOptions {
179
179
  * refuses it; `appliedVideoRateControl` says which ran.
180
180
  */
181
181
  readonly videoRateControl?: VideoRateControl;
182
+ /**
183
+ * The WebCodecs codec string the video encoder opens with: the `codecString`
184
+ * {@link canEncodeVideoTrack} chose, so H.264 opens in the profile this browser encodes. By
185
+ * default mediabunny's own (H.264 High at a level for the frame size and bitrate).
186
+ */
187
+ readonly videoCodecString?: string;
182
188
  /** The video encoder's latency mode; by default the one {@link videoEncoderLatencyMode} measures. */
183
189
  readonly videoLatencyMode?: VideoEncoderLatencyMode;
184
190
  /**
@@ -226,16 +232,60 @@ export type ExportDestination = {
226
232
  * {@link MediabunnySink} configures its encoder with.
227
233
  */
228
234
  export declare function videoEncoderQuality(rate: VideoRateControl): Quality;
235
+ /** An H.264 profile a browser export asks the encoder for, named as the standard names it. */
236
+ export type AvcProfile = "High" | "Main" | "Constrained Baseline";
237
+ /** One H.264 profile at the level an export needs, as {@link videoEncoderProfiles} lists it. */
238
+ export interface VideoEncoderProfile {
239
+ readonly profile: AvcProfile;
240
+ /** The WebCodecs codec string, for example `avc1.4d0028`. */
241
+ readonly codecString: string;
242
+ }
243
+ /**
244
+ * A profile asked for before the one an export opens, and refused. Shaped like a native export's
245
+ * `videoEncoderFallbacks` entries; the codec is the same, only the profile differs.
246
+ */
247
+ export interface VideoEncoderFallback {
248
+ /** The codec string asked for. */
249
+ readonly encoder: string;
250
+ readonly profile: AvcProfile;
251
+ readonly reason: "unsupported";
252
+ readonly detail: string;
253
+ }
254
+ /** How a video track is encoded, as {@link canEncodeVideoTrack} chose it. */
255
+ export interface VideoTrackEncoding {
256
+ /**
257
+ * The codec string to open the encoder with ({@link MediabunnySinkOptions.videoCodecString}).
258
+ * Undefined for codecs other than H.264, which open with mediabunny's own.
259
+ */
260
+ readonly codecString?: string;
261
+ /** H.264: the profile `codecString` names. */
262
+ readonly profile?: AvcProfile;
263
+ /** H.264: the profiles asked for first and refused, in order; empty when High is encoded. */
264
+ readonly fallbacks: readonly VideoEncoderFallback[];
265
+ }
266
+ /**
267
+ * The profiles {@link canEncodeVideoTrack} asks for, in order, at the level `width`x`height` at
268
+ * `rate`'s bitrate needs: for H.264, High, then Main, then Constrained Baseline, since a software
269
+ * encoder may take only the lower ones. Empty for the other codecs, which have one profile each
270
+ * (HEVC Main, VP9 profile 0, AV1 Main) at the level mediabunny picks.
271
+ */
272
+ export declare function videoEncoderProfiles(codec: "avc" | "hevc" | "vp9" | "av1", options: {
273
+ readonly width: number;
274
+ readonly height: number;
275
+ readonly rate: VideoRateControl;
276
+ }): readonly VideoEncoderProfile[];
229
277
  /**
230
- * Whether this browser can encode `codec` video at `width`x`height` with `rate`, asked with the
231
- * configurations {@link MediabunnySink} would try. WebCodecs leaves the codecs to the browser
232
- * (Chrome on Linux encodes no HEVC, for one), so an export asks before it opens its encoder.
278
+ * How this browser encodes `codec` video at `width`x`height` with `rate`, or `null` when it does
279
+ * not, asked with the configurations {@link MediabunnySink} would try. WebCodecs leaves the codecs
280
+ * to the browser (Chrome on Linux encodes no HEVC, for one), so an export asks before it opens its
281
+ * encoder. H.264 is asked for in each of {@link videoEncoderProfiles} in turn, and the first one
282
+ * encoded is chosen: pass its `codecString` to the sink, and report its `fallbacks`.
233
283
  */
234
284
  export declare function canEncodeVideoTrack(codec: "avc" | "hevc" | "vp9" | "av1", options: {
235
285
  readonly width: number;
236
286
  readonly height: number;
237
287
  readonly rate: VideoRateControl;
238
- }): Promise<boolean>;
288
+ }): Promise<VideoTrackEncoding | null>;
239
289
  /** How a browser's video encoder is opened: see {@link videoEncoderLatencyMode}. */
240
290
  export type VideoEncoderLatencyMode = "quality" | "realtime";
241
291
  /**
@@ -308,6 +358,11 @@ export declare class MediabunnySink implements BrowserEncodeSink, BrowserAudioBl
308
358
  * before then.
309
359
  */
310
360
  get appliedVideoRateControl(): AppliedVideoRateControl | undefined;
361
+ /**
362
+ * The WebCodecs codec string the video encoder was configured with (for H.264, its profile and
363
+ * level), once the first frame has been written. Undefined before then.
364
+ */
365
+ get videoCodecString(): string | undefined;
311
366
  /**
312
367
  * Encodes one frame at its output time (`output.timeTicks`, counted from the output's zero).
313
368
  * Closes `output.frame` whether or not it succeeds. Rejects with `RESOURCE_LIMIT` once a memory
@@ -1110,17 +1110,86 @@ export function videoEncoderQuality(rate) {
1110
1110
  ...(rate.quantizer === undefined ? {} : { quantizer: rate.quantizer }),
1111
1111
  });
1112
1112
  }
1113
+ /** The H.264 profiles in the order they are asked for, with `profile_idc` and constraint flags. */
1114
+ const AVC_PROFILES = [
1115
+ { profile: "High", prefix: "6400" },
1116
+ { profile: "Main", prefix: "4d00" },
1117
+ // constraint_set0, 1 and 2: decodable by Baseline, Main and Extended decoders.
1118
+ { profile: "Constrained Baseline", prefix: "42e0" },
1119
+ ];
1113
1120
  /**
1114
- * Whether this browser can encode `codec` video at `width`x`height` with `rate`, asked with the
1115
- * configurations {@link MediabunnySink} would try. WebCodecs leaves the codecs to the browser
1116
- * (Chrome on Linux encodes no HEVC, for one), so an export asks before it opens its encoder.
1121
+ * H.264 levels (`level_idc`) by frame size in macroblocks and bitrate: the table mediabunny picks
1122
+ * its own High profile level from, so the High string asked for here is the one it would build.
1123
+ */
1124
+ const AVC_LEVELS = [
1125
+ { macroblocks: 99, bitrate: 64_000, level: 0x0a },
1126
+ { macroblocks: 396, bitrate: 192_000, level: 0x0b },
1127
+ { macroblocks: 396, bitrate: 384_000, level: 0x0c },
1128
+ { macroblocks: 396, bitrate: 768_000, level: 0x0d },
1129
+ { macroblocks: 396, bitrate: 2_000_000, level: 0x14 },
1130
+ { macroblocks: 792, bitrate: 4_000_000, level: 0x15 },
1131
+ { macroblocks: 1_620, bitrate: 4_000_000, level: 0x16 },
1132
+ { macroblocks: 1_620, bitrate: 10_000_000, level: 0x1e },
1133
+ { macroblocks: 3_600, bitrate: 14_000_000, level: 0x1f },
1134
+ { macroblocks: 5_120, bitrate: 20_000_000, level: 0x20 },
1135
+ { macroblocks: 8_192, bitrate: 20_000_000, level: 0x28 },
1136
+ { macroblocks: 8_192, bitrate: 50_000_000, level: 0x29 },
1137
+ { macroblocks: 8_704, bitrate: 50_000_000, level: 0x2a },
1138
+ { macroblocks: 22_080, bitrate: 135_000_000, level: 0x32 },
1139
+ { macroblocks: 36_864, bitrate: 240_000_000, level: 0x33 },
1140
+ { macroblocks: 36_864, bitrate: 240_000_000, level: 0x34 },
1141
+ { macroblocks: 139_264, bitrate: 240_000_000, level: 0x3c },
1142
+ { macroblocks: 139_264, bitrate: 480_000_000, level: 0x3d },
1143
+ { macroblocks: 139_264, bitrate: 800_000_000, level: 0x3e },
1144
+ ];
1145
+ /**
1146
+ * The profiles {@link canEncodeVideoTrack} asks for, in order, at the level `width`x`height` at
1147
+ * `rate`'s bitrate needs: for H.264, High, then Main, then Constrained Baseline, since a software
1148
+ * encoder may take only the lower ones. Empty for the other codecs, which have one profile each
1149
+ * (HEVC Main, VP9 profile 0, AV1 Main) at the level mediabunny picks.
1150
+ */
1151
+ export function videoEncoderProfiles(codec, options) {
1152
+ if (codec !== "avc")
1153
+ return [];
1154
+ const macroblocks = Math.ceil(options.width / 16) * Math.ceil(options.height / 16);
1155
+ const level = (AVC_LEVELS.find((entry) => macroblocks <= entry.macroblocks &&
1156
+ options.rate.bitrate <= entry.bitrate) ?? AVC_LEVELS[AVC_LEVELS.length - 1]).level
1157
+ .toString(16)
1158
+ .padStart(2, "0");
1159
+ return AVC_PROFILES.map(({ profile, prefix }) => ({
1160
+ profile,
1161
+ codecString: `avc1.${prefix}${level}`,
1162
+ }));
1163
+ }
1164
+ /**
1165
+ * How this browser encodes `codec` video at `width`x`height` with `rate`, or `null` when it does
1166
+ * not, asked with the configurations {@link MediabunnySink} would try. WebCodecs leaves the codecs
1167
+ * to the browser (Chrome on Linux encodes no HEVC, for one), so an export asks before it opens its
1168
+ * encoder. H.264 is asked for in each of {@link videoEncoderProfiles} in turn, and the first one
1169
+ * encoded is chosen: pass its `codecString` to the sink, and report its `fallbacks`.
1117
1170
  */
1118
1171
  export async function canEncodeVideoTrack(codec, options) {
1119
- return canEncodeVideo(codec, {
1172
+ const encodes = (fullCodecString) => canEncodeVideo(codec, {
1120
1173
  width: options.width,
1121
1174
  height: options.height,
1122
1175
  quality: videoEncoderQuality(options.rate),
1176
+ ...(fullCodecString === undefined ? {} : { fullCodecString }),
1123
1177
  }).catch(() => false);
1178
+ const profiles = videoEncoderProfiles(codec, options);
1179
+ if (profiles.length === 0)
1180
+ return (await encodes()) ? { fallbacks: [] } : null;
1181
+ const fallbacks = [];
1182
+ for (const { profile, codecString } of profiles) {
1183
+ if (await encodes(codecString))
1184
+ return { codecString, profile, fallbacks };
1185
+ fallbacks.push({
1186
+ encoder: codecString,
1187
+ profile,
1188
+ reason: "unsupported",
1189
+ detail: `this browser does not encode H.264 ${profile} profile at ${options.width}x${options.height}`,
1190
+ });
1191
+ }
1192
+ return null;
1124
1193
  }
1125
1194
  /**
1126
1195
  * The profiles mediabunny opens (H.264 High, HEVC Main, VP9 profile 0, AV1 Main), at a level for
@@ -1417,6 +1486,7 @@ export class MediabunnySink {
1417
1486
  #frameDuration;
1418
1487
  #finished = false;
1419
1488
  #applied;
1489
+ #codecString;
1420
1490
  /** Tracks whose runner finished, through {@link videoTrack} or {@link audioTrack}. */
1421
1491
  #ended = new Set();
1422
1492
  #videoTrack;
@@ -1523,9 +1593,13 @@ export class MediabunnySink {
1523
1593
  codec,
1524
1594
  quality: videoEncoderQuality(rate),
1525
1595
  latencyMode,
1596
+ ...(options.videoCodecString === undefined
1597
+ ? {}
1598
+ : { fullCodecString: options.videoCodecString }),
1526
1599
  // mediabunny reports each candidate config before it asks the browser whether it is
1527
1600
  // supported and stops at the first that is, so the last one reported is the one that runs.
1528
1601
  onEncoderConfig: (config) => {
1602
+ this.#codecString = config.codec;
1529
1603
  const mode = config.bitrateMode;
1530
1604
  this.#applied = {
1531
1605
  bitrateMode: mode === "quantizer" || mode === "constant" ? mode : "variable",
@@ -1628,6 +1702,13 @@ export class MediabunnySink {
1628
1702
  get appliedVideoRateControl() {
1629
1703
  return this.#applied;
1630
1704
  }
1705
+ /**
1706
+ * The WebCodecs codec string the video encoder was configured with (for H.264, its profile and
1707
+ * level), once the first frame has been written. Undefined before then.
1708
+ */
1709
+ get videoCodecString() {
1710
+ return this.#codecString;
1711
+ }
1631
1712
  /**
1632
1713
  * Encodes one frame at its output time (`output.timeTicks`, counted from the output's zero).
1633
1714
  * Closes `output.frame` whether or not it succeeds. Rejects with `RESOURCE_LIMIT` once a memory
@@ -1,5 +1,5 @@
1
1
  /** Observable browser capability/degradation categories. */
2
- export type BrowserCapabilityDiagnostic = "webgpu-unavailable" | "webcodecs-unavailable" | "offscreen-canvas-unavailable" | "wasm-simd-unavailable" | "wasm-reference-types-unavailable" | "visibility-suspended" | "memory-pressure" | "decoder-session-limit" | "context-lost";
2
+ export type BrowserCapabilityDiagnostic = "webgpu-unavailable" | "webgl2-unavailable" | "webcodecs-unavailable" | "offscreen-canvas-unavailable" | "wasm-simd-unavailable" | "wasm-reference-types-unavailable" | "visibility-suspended" | "memory-pressure" | "decoder-session-limit" | "context-lost";
3
3
  /** Caller-configured browser resource limits; both must be positive integers. */
4
4
  export interface BrowserResourceBudget {
5
5
  /** GPU texture memory the host allows, in bytes. */
@@ -11,7 +11,13 @@ export interface BrowserResourceBudget {
11
11
  export interface BrowserCapabilities {
12
12
  /** The browser hands out a WebGPU adapter, so the engine renders through WebGPU. */
13
13
  readonly webgpu: boolean;
14
+ /** A WebGL2 context can be made, which the engine renders through without WebGPU. */
14
15
  readonly webgl2: boolean;
16
+ /**
17
+ * The renderer a render worker host's `renderer: "auto"` draws with here: `"gpu"` (WebGPU, else
18
+ * WebGL2) when either is available, else `"cpu"`, the engine's CPU renderer in wasm.
19
+ */
20
+ readonly renderer: "gpu" | "cpu";
15
21
  readonly webcodecs: boolean;
16
22
  readonly offscreenCanvas: boolean;
17
23
  /**
@@ -27,9 +33,11 @@ export interface BrowserCapabilities {
27
33
  *
28
34
  * `webgpu` asks for an adapter, as the engine does when it picks its backend: a browser can expose
29
35
  * `navigator.gpu` and still have no adapter to give (Chrome on Linux without its flag, a
30
- * blocklisted GPU, acceleration turned off), and then the engine runs on WebGL2. `webgl2` is probed
31
- * on a canvas of its own, since a canvas's first context fixes its kind for good: a caller's canvas
32
- * probed for WebGL2 could no longer render WebGPU or be transferred to a worker. `wasm` validates
36
+ * blocklisted GPU, acceleration turned off), and then the engine runs on WebGL2; with neither (a
37
+ * browser with no usable GPU, such as Chrome with acceleration off), it runs on the CPU, which
38
+ * `renderer` says and `diagnostics` explains. `webgl2` is probed on a canvas of its own, since a
39
+ * canvas's first context fixes its kind for good: a caller's canvas probed for WebGL2 could no
40
+ * longer render WebGPU or be transferred to a worker. `wasm` validates
33
41
  * two tiny modules, one per WebAssembly feature the packaged modules need past what every WebCodecs
34
42
  * browser has (SIMD and reference types; Chrome 94 and 95 have WebCodecs and SIMD but no reference
35
43
  * types), so a browser that cannot load the engine is told so before it fetches and compiles it.
@@ -3,9 +3,11 @@
3
3
  *
4
4
  * `webgpu` asks for an adapter, as the engine does when it picks its backend: a browser can expose
5
5
  * `navigator.gpu` and still have no adapter to give (Chrome on Linux without its flag, a
6
- * blocklisted GPU, acceleration turned off), and then the engine runs on WebGL2. `webgl2` is probed
7
- * on a canvas of its own, since a canvas's first context fixes its kind for good: a caller's canvas
8
- * probed for WebGL2 could no longer render WebGPU or be transferred to a worker. `wasm` validates
6
+ * blocklisted GPU, acceleration turned off), and then the engine runs on WebGL2; with neither (a
7
+ * browser with no usable GPU, such as Chrome with acceleration off), it runs on the CPU, which
8
+ * `renderer` says and `diagnostics` explains. `webgl2` is probed on a canvas of its own, since a
9
+ * canvas's first context fixes its kind for good: a caller's canvas probed for WebGL2 could no
10
+ * longer render WebGPU or be transferred to a worker. `wasm` validates
9
11
  * two tiny modules, one per WebAssembly feature the packaged modules need past what every WebCodecs
10
12
  * browser has (SIMD and reference types; Chrome 94 and 95 have WebCodecs and SIMD but no reference
11
13
  * types), so a browser that cannot load the engine is told so before it fetches and compiles it.
@@ -21,6 +23,8 @@ export async function detectBrowserCapabilities() {
21
23
  const diagnostics = [];
22
24
  if (!webgpu)
23
25
  diagnostics.push("webgpu-unavailable");
26
+ if (!webgl2)
27
+ diagnostics.push("webgl2-unavailable");
24
28
  if (!webcodecs)
25
29
  diagnostics.push("webcodecs-unavailable");
26
30
  if (!offscreenCanvas)
@@ -32,6 +36,7 @@ export async function detectBrowserCapabilities() {
32
36
  return {
33
37
  webgpu,
34
38
  webgl2,
39
+ renderer: webgpu || webgl2 ? "gpu" : "cpu",
35
40
  webcodecs,
36
41
  offscreenCanvas,
37
42
  wasm: simd && referenceTypes,
package/dist/worker.d.ts CHANGED
@@ -92,6 +92,11 @@ export interface RenderWorkerHost {
92
92
  * stamps the frame it reads back at `outputTimeTicks`, its time in the output.
93
93
  */
94
94
  beginExport(timeTicks: bigint, codec: EngineVideoCodec, outputTimeTicks: bigint): ExportTicket;
95
+ /**
96
+ * The renderer the host draws with; `initialize` answers it. Absent for a host that does not
97
+ * say.
98
+ */
99
+ readonly rendererChoice?: RendererChoice;
95
100
  /** Checked after every request; must not throw (a throw is read as lost). */
96
101
  isDeviceLost(): boolean;
97
102
  /** Replaces a lost device; the request loop calls it only while `isDeviceLost()` is true. */
@@ -128,6 +133,27 @@ export interface WasmRenderWorkerHostOptions {
128
133
  * default), or the browser (`"browser"`); see {@link VideoConversion}.
129
134
  */
130
135
  readonly videoConversion?: VideoConversion;
136
+ /**
137
+ * Which renderer draws. `"auto"` (the default) draws on the GPU, through WebGPU or else WebGL2,
138
+ * and on the engine's CPU renderer in wasm when the browser offers neither (Chrome with
139
+ * acceleration off, a blocklisted GPU); `"gpu"` never falls back, and `"cpu"` draws on the CPU
140
+ * and opens no GPU context. The CPU renderer previews at half resolution by default and refuses
141
+ * runtime effects (`effects`) with `EFFECT_ERROR`; it needs the binding's `createCpu`, without
142
+ * which `"auto"` is `"gpu"`. The host reports the renderer it chose ({@link RendererChoice}).
143
+ */
144
+ readonly renderer?: RendererMode;
145
+ }
146
+ /** The renderer a browser render host is asked for; see {@link WasmRenderWorkerHostOptions}. */
147
+ export type RendererMode = "auto" | "gpu" | "cpu";
148
+ /** The renderer a render host draws with, as the native host reports it. */
149
+ export interface RendererChoice {
150
+ /** The renderer that draws the frames. */
151
+ readonly renderer: "gpu" | "cpu";
152
+ /**
153
+ * Why it draws on the CPU: `"auto"` found neither WebGPU nor WebGL2, or `"cpu"` was asked for.
154
+ * `null` when it draws on a GPU.
155
+ */
156
+ readonly rendererReason: string | null;
131
157
  }
132
158
  /**
133
159
  * Installs a serial request loop in the worker that owns the GPU device, replacing
@@ -145,9 +171,13 @@ export declare function installRenderWorker(scope: RenderWorkerScope, createHost
145
171
  * binding. The callback makes a canvas renderer, registers `options`' effects, resources and
146
172
  * fonts, then loads `projectJson`.
147
173
  *
148
- * The callback rejects when the binding has no `createForCanvas`, or when making the renderer,
149
- * compiling an effect or loading the project fails. After a device loss, `recover()` repeats the
150
- * same registration on a new renderer.
174
+ * The renderer is the one `options.renderer` picks (see {@link WasmRenderWorkerHostOptions}); the
175
+ * host's `rendererChoice` says which, and `initialize` answers it.
176
+ *
177
+ * The callback rejects when the binding lacks the constructor the renderer needs
178
+ * (`createForCanvas`, or `createCpu` for `"cpu"`), or when making the renderer, compiling an
179
+ * effect or loading the project fails. After a device loss, `recover()` repeats the same
180
+ * registration on a new renderer of the same kind.
151
181
  */
152
182
  export declare function createWasmRenderWorkerHost(binding: WasmEffectRegistryBinding, projectJson: string, options?: WasmRenderWorkerHostOptions): (canvas: OffscreenCanvas) => Promise<RenderWorkerHost>;
153
183
  /**
@@ -162,6 +192,11 @@ export declare function createWasmRenderWorkerHost(binding: WasmEffectRegistryBi
162
192
  export declare class WorkerRenderBackend implements RenderBackend {
163
193
  #private;
164
194
  constructor(worker: Worker);
195
+ /**
196
+ * The renderer the worker's host draws with, once {@link WorkerRenderBackend.initialize} has
197
+ * resolved; `undefined` before, or for a host that does not say.
198
+ */
199
+ get rendererChoice(): RendererChoice | undefined;
165
200
  /** Transfers `canvas` to the worker and creates its host there. */
166
201
  initialize(canvas: OffscreenCanvas): Promise<void>;
167
202
  /**