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,24 +1,16 @@
1
1
  /**
2
- * Output rate control for browser exports, resolved by the engine.
3
- *
4
- * The engine owns what a quality level means: `resolveRateControl` and `resolveAudioBitrate` in
5
- * `alpha2/wasm` are the same functions the native host calls, so a browser export and a native
6
- * export at one level and one output receive the same numbers. An app resolves once, at the top of
7
- * an export, and hands the numbers to whichever sink it built; a sink never sees a level, and
8
- * neither does anything in this package; the level names come from the engine too, through
9
- * `qualityLevels`.
2
+ * Output rate control for browser exports. Quality levels resolve through the same engine functions
3
+ * the native host calls, so both get the same numbers. Resolve once per export and pass the numbers
4
+ * to the sink.
10
5
  */
11
- /** The rate-control exports of the `alpha2/wasm` binding. */
12
6
  export interface RateControlBinding {
13
7
  resolveRateControl(codec: string, width: number, height: number, frameRateNum: number, frameRateDen: number, level: string): string;
14
8
  resolveAudioBitrate(codec: string, level: string): number;
15
9
  qualityLevels(): string;
16
10
  }
17
- /** The engine's spellings of the video codecs it resolves a rate for. */
18
- export type EngineVideoCodec = "h264" | "hevc" | "vp9" | "av1" | "mpeg4-part2";
19
- /** The WebCodecs/mediabunny spellings of the same codecs. */
11
+ export type EngineVideoCodec = "h264" | "hevc" | "vp9" | "av1" | "mpeg4-part2" | "prores";
12
+ /** WebCodecs/mediabunny codec names. */
20
13
  export type BrowserVideoCodec = "avc" | "hevc" | "vp9" | "av1";
21
- /** A resolved video rate control, in the shape a browser sink receives. */
22
14
  export interface VideoRateControl {
23
15
  /** Target, or with a quantizer the fallback, bits per second. */
24
16
  readonly bitrate: number;
@@ -37,18 +29,13 @@ export type VideoQualityRequest = string | {
37
29
  export type AudioQualityRequest = string | {
38
30
  readonly bitrate: number;
39
31
  };
40
- /**
41
- * Maps a browser codec name onto the engine's.
42
- *
43
- * This is the one place `avc` becomes `h264`: the engine's mapping is keyed on one codec
44
- * vocabulary, and the browser's is translated at this edge.
45
- */
32
+ /** Maps a browser codec name onto the engine's (`avc` becomes `h264`). */
46
33
  export declare function engineVideoCodec(codec: BrowserVideoCodec): EngineVideoCodec;
47
34
  /** The engine's quality level names, lowest first. */
48
35
  export declare function qualityLevels(binding: RateControlBinding): readonly string[];
49
36
  /**
50
- * Resolves a video quality request: a level name through the engine, for this
51
- * codec, size, and rate; an explicit rate as given, `variable` by default.
37
+ * Resolves a level name through the engine for this codec, size and rate. An explicit rate is
38
+ * returned as given, `variable` by default.
52
39
  *
53
40
  * @throws RangeError When an explicit `bitrate` is not a positive integer.
54
41
  * @throws When the engine does not know the level or codec.
@@ -64,8 +51,7 @@ export declare function resolveVideoRateControl(binding: RateControlBinding, req
64
51
  readonly quality: VideoQualityRequest;
65
52
  }): VideoRateControl;
66
53
  /**
67
- * Resolves an audio quality request to bits per second: a level name through
68
- * the engine, an explicit bitrate as given.
54
+ * Resolves an audio quality request to bits per second.
69
55
  *
70
56
  * @throws RangeError When an explicit `bitrate` is not a positive integer.
71
57
  * @throws When the engine does not know the level.
@@ -1,19 +1,9 @@
1
1
  /**
2
- * Output rate control for browser exports, resolved by the engine.
3
- *
4
- * The engine owns what a quality level means: `resolveRateControl` and `resolveAudioBitrate` in
5
- * `alpha2/wasm` are the same functions the native host calls, so a browser export and a native
6
- * export at one level and one output receive the same numbers. An app resolves once, at the top of
7
- * an export, and hands the numbers to whichever sink it built; a sink never sees a level, and
8
- * neither does anything in this package; the level names come from the engine too, through
9
- * `qualityLevels`.
10
- */
11
- /**
12
- * Maps a browser codec name onto the engine's.
13
- *
14
- * This is the one place `avc` becomes `h264`: the engine's mapping is keyed on one codec
15
- * vocabulary, and the browser's is translated at this edge.
2
+ * Output rate control for browser exports. Quality levels resolve through the same engine functions
3
+ * the native host calls, so both get the same numbers. Resolve once per export and pass the numbers
4
+ * to the sink.
16
5
  */
6
+ /** Maps a browser codec name onto the engine's (`avc` becomes `h264`). */
17
7
  export function engineVideoCodec(codec) {
18
8
  return codec === "avc" ? "h264" : codec;
19
9
  }
@@ -22,8 +12,8 @@ export function qualityLevels(binding) {
22
12
  return JSON.parse(binding.qualityLevels());
23
13
  }
24
14
  /**
25
- * Resolves a video quality request: a level name through the engine, for this
26
- * codec, size, and rate; an explicit rate as given, `variable` by default.
15
+ * Resolves a level name through the engine for this codec, size and rate. An explicit rate is
16
+ * returned as given, `variable` by default.
27
17
  *
28
18
  * @throws RangeError When an explicit `bitrate` is not a positive integer.
29
19
  * @throws When the engine does not know the level or codec.
@@ -48,8 +38,7 @@ export function resolveVideoRateControl(binding, request) {
48
38
  };
49
39
  }
50
40
  /**
51
- * Resolves an audio quality request to bits per second: a level name through
52
- * the engine, an explicit bitrate as given.
41
+ * Resolves an audio quality request to bits per second.
53
42
  *
54
43
  * @throws RangeError When an explicit `bitrate` is not a positive integer.
55
44
  * @throws When the engine does not know the level.
@@ -0,0 +1,41 @@
1
+ import type { WasmCanvasRenderer, WasmEffectRegistryBinding } from "./effects.js";
2
+ /**
3
+ * Renderer selection for a browser render host.
4
+ *
5
+ * - `"auto"` uses a hardware GPU (WebGPU, else WebGL2) and falls back to the wasm CPU renderer when
6
+ * only a software adapter or none is available.
7
+ * - `"gpu"` uses any GPU adapter, software included, and never falls back.
8
+ * - `"cpu"` uses the CPU renderer and opens no GPU context.
9
+ */
10
+ export type RendererMode = "auto" | "gpu" | "cpu";
11
+ /** The renderer a render host draws with, in the same shape the native host reports. */
12
+ export interface RendererChoice {
13
+ readonly renderer: "gpu" | "cpu";
14
+ /** Why it draws on the CPU (no hardware GPU, asked for, or GPU loss); `null` on a GPU. */
15
+ readonly rendererReason: string | null;
16
+ readonly backend: "webgpu" | "webgl2" | "cpu";
17
+ /** The browser's adapter name; `null` on the CPU or when the browser names none. */
18
+ readonly adapter: string | null;
19
+ }
20
+ export type RendererSurface = OffscreenCanvas | HTMLCanvasElement;
21
+ /**
22
+ * Creates the renderer `mode` asks for on `surface` and reports which one it is.
23
+ *
24
+ * `"auto"` falls back to the CPU renderer on the same surface when the GPU renderer fails with
25
+ * `GPU_INIT_FAILED` or `INVALID_INPUT`. A failed GPU attempt leaves the surface free for the CPU
26
+ * renderer's 2D context.
27
+ *
28
+ * Rejects when the binding lacks the constructor `mode` needs, or with the GPU's error when `"gpu"`
29
+ * fails, `"auto"` fails for another reason, or the CPU fallback also fails.
30
+ */
31
+ export declare function createRenderer(binding: WasmEffectRegistryBinding, surface: RendererSurface, mode: RendererMode): Promise<{
32
+ wasm: WasmCanvasRenderer;
33
+ choice: RendererChoice;
34
+ }>;
35
+ /**
36
+ * Creates a GPU renderer on `surface`. Rejects as the constructor does, or when the binding has no
37
+ * constructor for the surface's kind.
38
+ */
39
+ export declare function createGpuRenderer(binding: WasmEffectRegistryBinding, surface: RendererSurface, softwareAdapter: "allow" | "refuse"): Promise<WasmCanvasRenderer>;
40
+ /** The choice a GPU renderer reports; a binding that names no backend is WebGPU. */
41
+ export declare function gpuChoice(wasm: WasmCanvasRenderer): RendererChoice;
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Creates the renderer `mode` asks for on `surface` and reports which one it is.
3
+ *
4
+ * `"auto"` falls back to the CPU renderer on the same surface when the GPU renderer fails with
5
+ * `GPU_INIT_FAILED` or `INVALID_INPUT`. A failed GPU attempt leaves the surface free for the CPU
6
+ * renderer's 2D context.
7
+ *
8
+ * Rejects when the binding lacks the constructor `mode` needs, or with the GPU's error when `"gpu"`
9
+ * fails, `"auto"` fails for another reason, or the CPU fallback also fails.
10
+ */
11
+ export async function createRenderer(binding, surface, mode) {
12
+ if (mode !== "auto" && mode !== "gpu" && mode !== "cpu")
13
+ throw new RangeError('renderer must be "auto", "gpu" or "cpu"');
14
+ const cpu = (rendererReason) => {
15
+ if (binding.createCpu === undefined)
16
+ throw new Error("wasm binding does not expose createCpu");
17
+ return {
18
+ wasm: binding.createCpu(surface),
19
+ choice: {
20
+ renderer: "cpu",
21
+ rendererReason,
22
+ backend: "cpu",
23
+ adapter: null,
24
+ },
25
+ };
26
+ };
27
+ if (mode === "cpu")
28
+ return cpu('the CPU renderer was asked for (renderer "cpu")');
29
+ try {
30
+ const wasm = await createGpuRenderer(binding, surface, mode === "auto" ? "refuse" : "allow");
31
+ return { wasm, choice: gpuChoice(wasm) };
32
+ }
33
+ catch (error) {
34
+ const code = error?.code;
35
+ const missing = error instanceof MissingConstructorError;
36
+ if (mode !== "auto" ||
37
+ binding.createCpu === undefined ||
38
+ (!missing && code !== "GPU_INIT_FAILED" && code !== "INVALID_INPUT"))
39
+ throw error;
40
+ if (missing)
41
+ return cpu("the wasm binding has no GPU renderer");
42
+ const message = error instanceof Error ? error.message : String(error);
43
+ try {
44
+ return cpu(message.startsWith("the browser offers only the software adapter")
45
+ ? `${message}, which renderer "auto" does not draw on`
46
+ : `the browser offers no WebGPU adapter and no WebGL2 context (${message})`);
47
+ }
48
+ catch {
49
+ // Report the GPU's failure, which explains why there is no renderer at all.
50
+ throw error;
51
+ }
52
+ }
53
+ }
54
+ class MissingConstructorError extends Error {
55
+ }
56
+ /**
57
+ * Creates a GPU renderer on `surface`. Rejects as the constructor does, or when the binding has no
58
+ * constructor for the surface's kind.
59
+ */
60
+ export async function createGpuRenderer(binding, surface, softwareAdapter) {
61
+ const html = typeof HTMLCanvasElement !== "undefined" &&
62
+ surface instanceof HTMLCanvasElement;
63
+ if (html) {
64
+ if (binding.createForHtmlCanvas === undefined)
65
+ throw new MissingConstructorError("wasm binding does not expose createForHtmlCanvas");
66
+ return binding.createForHtmlCanvas(surface, { softwareAdapter });
67
+ }
68
+ if (binding.createForCanvas === undefined)
69
+ throw new MissingConstructorError("wasm binding does not expose createForCanvas");
70
+ return binding.createForCanvas(surface, {
71
+ softwareAdapter,
72
+ });
73
+ }
74
+ /** The choice a GPU renderer reports; a binding that names no backend is WebGPU. */
75
+ export function gpuChoice(wasm) {
76
+ return {
77
+ renderer: "gpu",
78
+ rendererReason: null,
79
+ backend: wasm.backend ?? "webgpu",
80
+ adapter: wasm.adapter ? wasm.adapter : null,
81
+ };
82
+ }
@@ -1,4 +1,2 @@
1
- /** Records that building `frame` copied `bytes`; 0 records nothing. */
2
1
  export declare function recordRepackedBytes(frame: VideoFrame, bytes: number): void;
3
- /** Bytes recorded for `frame`; 0 for a frame nothing was recorded for. */
4
2
  export declare function repackedBytesOf(frame: VideoFrame): number;
@@ -1,15 +1,12 @@
1
1
  /**
2
- * Bytes script copied to pack a `VideoFrame`'s planes tight, kept per frame object. A frame posted
3
- * to another agent arrives there as another object, so whoever transfers one carries its count
4
- * across and records it on the frame that arrives.
2
+ * Bytes copied to pack a `VideoFrame`'s planes tight. A frame posted to another agent arrives as a
3
+ * new object, so whoever transfers one must re-record its count on the arriving frame.
5
4
  */
6
5
  const repackedBytes = new WeakMap();
7
- /** Records that building `frame` copied `bytes`; 0 records nothing. */
8
6
  export function recordRepackedBytes(frame, bytes) {
9
7
  if (bytes > 0)
10
8
  repackedBytes.set(frame, bytes);
11
9
  }
12
- /** Bytes recorded for `frame`; 0 for a frame nothing was recorded for. */
13
10
  export function repackedBytesOf(frame) {
14
11
  return repackedBytes.get(frame) ?? 0;
15
12
  }
@@ -2,78 +2,66 @@
2
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
- /** GPU texture memory the host allows, in bytes. */
6
5
  readonly textureBytes: number;
7
- /** Video decoders the host allows open at once. */
6
+ /** Video decoders allowed open at once. */
8
7
  readonly decoderSessions: number;
9
8
  }
10
- /** Stable facts used by hosts to select WebGPU, WebGL2, and media fallbacks. */
9
+ /**
10
+ * Facts hosts use to select WebGPU, WebGL2 and media fallbacks. They hold only for the probing
11
+ * thread: a worker can get different answers than the page, so worker hosts should use
12
+ * `WorkerRenderBackend.detectCapabilities()`.
13
+ */
11
14
  export interface BrowserCapabilities {
12
- /** The browser hands out a WebGPU adapter, so the engine renders through WebGPU. */
15
+ /** A non-fallback (hardware) WebGPU adapter is available. */
13
16
  readonly webgpu: boolean;
14
- /** A WebGL2 context can be made, which the engine renders through without WebGPU. */
17
+ /** Whether the WebGPU adapter is the software fallback; `null` when there is none. */
18
+ readonly webgpuFallbackAdapter: boolean | null;
15
19
  readonly webgl2: boolean;
20
+ /** The WebGL2 renderer name, unmasked where allowed; `null` without a context. */
21
+ readonly webgl2Renderer: string | null;
16
22
  /**
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.
23
+ * What `renderer: "auto"` would draw with here. Software rasterizers (SwiftShader, llvmpipe,
24
+ * WARP, a WebGPU fallback adapter) count as `"cpu"`.
19
25
  */
20
26
  readonly renderer: "gpu" | "cpu";
21
27
  readonly webcodecs: boolean;
22
28
  readonly offscreenCanvas: boolean;
23
29
  /**
24
- * The browser compiles WebAssembly with SIMD and reference types, which the engine, audio
25
- * worklet, and extension modules all use. When it is `false` none of them loads, and
26
- * `diagnostics` names each missing feature.
30
+ * WebAssembly SIMD and reference types are supported. Without them neither the engine, the audio
31
+ * worklet, nor extension modules load; `diagnostics` names the missing feature.
27
32
  */
28
33
  readonly wasm: boolean;
29
34
  readonly diagnostics: readonly BrowserCapabilityDiagnostic[];
30
35
  }
31
36
  /**
32
- * Probes the current environment.
37
+ * Probes the calling thread's environment (see {@link BrowserCapabilities}).
33
38
  *
34
- * `webgpu` asks for an adapter, as the engine does when it picks its backend: a browser can expose
35
- * `navigator.gpu` and still have no adapter to give (Chrome on Linux without its flag, a
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
41
- * two tiny modules, one per WebAssembly feature the packaged modules need past what every WebCodecs
42
- * browser has (SIMD and reference types; Chrome 94 and 95 have WebCodecs and SIMD but no reference
43
- * types), so a browser that cannot load the engine is told so before it fetches and compiles it.
39
+ * `webgpu` requests an adapter, since `navigator.gpu` can exist without one. `webgl2` is probed on
40
+ * a throwaway canvas because a canvas's first context fixes its kind. `wasm` validates tiny probe
41
+ * modules, so it is cheap to check before fetching the engine.
44
42
  */
45
43
  export declare function detectBrowserCapabilities(): Promise<BrowserCapabilities>;
46
44
  /**
47
- * Tracks browser lifecycle events against a {@link BrowserResourceBudget} and
48
- * reports each degradation through `notify`. It owns no player or renderer:
49
- * the host acts on what it reports.
45
+ * Tracks browser lifecycle events against a {@link BrowserResourceBudget} and reports each
46
+ * degradation through `notify`. The host acts on what it reports.
50
47
  *
51
- * @throws RangeError From the constructor when either budget is not a positive
52
- * safe integer.
48
+ * @throws RangeError From the constructor when either budget is not a positive safe integer.
53
49
  */
54
50
  export declare class BrowserRobustnessCoordinator {
55
51
  #private;
56
52
  constructor(budget: BrowserResourceBudget, notify: (diagnostic: BrowserCapabilityDiagnostic) => void);
57
53
  /**
58
- * Sizes `canvas`'s backing store to its CSS size times `scale` (the device
59
- * pixel ratio), rounded, at least 1x1.
54
+ * Sizes `canvas`'s backing store to its CSS size times `scale` (device pixel ratio), min 1x1.
60
55
  *
61
56
  * @throws RangeError When the scaled size is not a safe integer.
62
57
  */
63
58
  resize(canvas: HTMLCanvasElement | OffscreenCanvas, cssWidth: number, cssHeight: number, scale?: number): void;
64
59
  /** Records page visibility; hiding a visible page reports `visibility-suspended`. */
65
60
  setVisible(visible: boolean): void;
66
- /**
67
- * Reports `memory-pressure` and returns the texture budget to fall back to:
68
- * half the configured one, at least one byte.
69
- */
61
+ /** Reports `memory-pressure` and returns the reduced texture budget (half, at least 1 byte). */
70
62
  memoryPressure(): number;
71
- /** Reports `context-lost`. */
72
63
  contextLost(): void;
73
- /**
74
- * Takes one decoder session. Returns `false`, and reports
75
- * `decoder-session-limit`, when all of the budget's sessions are taken.
76
- */
64
+ /** Takes one decoder session, or reports `decoder-session-limit` and returns `false`. */
77
65
  acquireDecoder(): boolean;
78
66
  /** Returns a session taken by {@link acquireDecoder}; extra calls are ignored. */
79
67
  releaseDecoder(): void;
@@ -1,20 +1,15 @@
1
1
  /**
2
- * Probes the current environment.
2
+ * Probes the calling thread's environment (see {@link BrowserCapabilities}).
3
3
  *
4
- * `webgpu` asks for an adapter, as the engine does when it picks its backend: a browser can expose
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; 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
11
- * two tiny modules, one per WebAssembly feature the packaged modules need past what every WebCodecs
12
- * browser has (SIMD and reference types; Chrome 94 and 95 have WebCodecs and SIMD but no reference
13
- * types), so a browser that cannot load the engine is told so before it fetches and compiles it.
4
+ * `webgpu` requests an adapter, since `navigator.gpu` can exist without one. `webgl2` is probed on
5
+ * a throwaway canvas because a canvas's first context fixes its kind. `wasm` validates tiny probe
6
+ * modules, so it is cheap to check before fetching the engine.
14
7
  */
15
8
  export async function detectBrowserCapabilities() {
16
- const webgpu = await detectWebgpu();
17
- const webgl2 = detectWebgl2();
9
+ const adapter = await detectWebgpu();
10
+ const webgpu = adapter !== null && !adapter.fallback;
11
+ const webgl2Renderer = detectWebgl2();
12
+ const webgl2 = webgl2Renderer !== null;
18
13
  const webcodecs = globalThis.VideoDecoder !== undefined &&
19
14
  globalThis.VideoEncoder !== undefined;
20
15
  const offscreenCanvas = globalThis.OffscreenCanvas !== undefined;
@@ -33,27 +28,29 @@ export async function detectBrowserCapabilities() {
33
28
  diagnostics.push("wasm-simd-unavailable");
34
29
  if (!referenceTypes)
35
30
  diagnostics.push("wasm-reference-types-unavailable");
31
+ const hardwareWebgl2 = webgl2Renderer !== null && !softwareRenderer(webgl2Renderer);
36
32
  return {
37
33
  webgpu,
34
+ webgpuFallbackAdapter: adapter === null ? null : adapter.fallback,
38
35
  webgl2,
39
- renderer: webgpu || webgl2 ? "gpu" : "cpu",
36
+ webgl2Renderer,
37
+ renderer: webgpu || hardwareWebgl2 ? "gpu" : "cpu",
40
38
  webcodecs,
41
39
  offscreenCanvas,
42
40
  wasm: simd && referenceTypes,
43
41
  diagnostics,
44
42
  };
45
43
  }
46
- /** `(func (result v128) i32.const 0 i8x16.splat i8x16.popcnt)`: valid only with final SIMD. */
44
+ // `(func (result v128) i32.const 0 i8x16.splat i8x16.popcnt)`, valid only with final SIMD.
47
45
  const WASM_SIMD_PROBE = [
48
46
  0, 97, 115, 109, 1, 0, 0, 0, 1, 5, 1, 96, 0, 1, 123, 3, 2, 1, 0, 10, 10, 1, 8,
49
47
  0, 65, 0, 253, 15, 253, 98, 11,
50
48
  ];
51
- /** `(func ref.null func drop)`: valid only with reference types. */
49
+ // `(func ref.null func drop)`, valid only with reference types.
52
50
  const WASM_REFERENCE_TYPES_PROBE = [
53
51
  0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 3, 2, 1, 0, 10, 7, 1, 5, 0,
54
52
  208, 112, 26, 11,
55
53
  ];
56
- /** Whether `WebAssembly.validate` accepts `bytes`; no `WebAssembly` at all accepts nothing. */
57
54
  function validatesWasm(bytes) {
58
55
  try {
59
56
  return globalThis.WebAssembly?.validate(new Uint8Array(bytes)) === true;
@@ -62,46 +59,74 @@ function validatesWasm(bytes) {
62
59
  return false;
63
60
  }
64
61
  }
65
- /** Whether `navigator.gpu` gives an adapter; a refused request counts as none. */
62
+ // Case-insensitive; must match the engine's native and wasm adapter selection list.
63
+ const SOFTWARE_RENDERERS = [
64
+ "mesa offscreen",
65
+ "swiftshader",
66
+ "llvmpipe",
67
+ "microsoft basic render",
68
+ ];
69
+ function softwareRenderer(name) {
70
+ const lower = name.toLowerCase();
71
+ return SOFTWARE_RENDERERS.some((software) => lower.includes(software));
72
+ }
73
+ // Older browsers flag `isFallbackAdapter` on the adapter rather than its `info`.
66
74
  async function detectWebgpu() {
67
75
  const gpu = globalThis.navigator?.gpu;
68
76
  if (gpu === undefined)
69
- return false;
77
+ return null;
70
78
  try {
71
- return (await gpu.requestAdapter()) != null;
79
+ const adapter = await gpu.requestAdapter();
80
+ if (adapter == null)
81
+ return null;
82
+ const info = adapter.info ?? {};
83
+ const fallback = info.isFallbackAdapter === true ||
84
+ adapter.isFallbackAdapter === true ||
85
+ [info.vendor, info.architecture, info.description].some((field) => typeof field === "string" && softwareRenderer(field));
86
+ return { fallback };
72
87
  }
73
88
  catch {
74
- return false;
89
+ return null;
75
90
  }
76
91
  }
77
- /**
78
- * Whether a WebGL2 context can be created on a throwaway DOM canvas, or an `OffscreenCanvas` where
79
- * there is no DOM.
80
- */
92
+ const UNMASKED_RENDERER = 0x9246;
93
+ const RENDERER = 0x1f01;
94
+ // The probe context is lost at once because browsers cap live WebGL contexts.
81
95
  function detectWebgl2() {
82
- const probe = (surface) => {
83
- try {
84
- return surface.getContext("webgl2") != null;
85
- }
86
- catch {
87
- return false;
88
- }
89
- };
90
- if (typeof document !== "undefined") {
91
- return probe(document.createElement("canvas"));
96
+ let surface;
97
+ if (globalThis.OffscreenCanvas !== undefined)
98
+ surface = new OffscreenCanvas(1, 1);
99
+ else if (typeof document !== "undefined")
100
+ surface = document.createElement("canvas");
101
+ else
102
+ return null;
103
+ let context;
104
+ try {
105
+ context = surface.getContext("webgl2");
106
+ }
107
+ catch {
108
+ return null;
109
+ }
110
+ if (context == null)
111
+ return null;
112
+ let renderer = "";
113
+ try {
114
+ const unmasked = context.getExtension("WEBGL_debug_renderer_info");
115
+ const name = context.getParameter(unmasked === null ? RENDERER : UNMASKED_RENDERER);
116
+ if (typeof name === "string")
117
+ renderer = name;
118
+ context.getExtension("WEBGL_lose_context")?.loseContext();
92
119
  }
93
- if (globalThis.OffscreenCanvas !== undefined) {
94
- return probe(new OffscreenCanvas(1, 1));
120
+ catch {
121
+ // The context exists even if its name is unknown.
95
122
  }
96
- return false;
123
+ return renderer;
97
124
  }
98
125
  /**
99
- * Tracks browser lifecycle events against a {@link BrowserResourceBudget} and
100
- * reports each degradation through `notify`. It owns no player or renderer:
101
- * the host acts on what it reports.
126
+ * Tracks browser lifecycle events against a {@link BrowserResourceBudget} and reports each
127
+ * degradation through `notify`. The host acts on what it reports.
102
128
  *
103
- * @throws RangeError From the constructor when either budget is not a positive
104
- * safe integer.
129
+ * @throws RangeError From the constructor when either budget is not a positive safe integer.
105
130
  */
106
131
  export class BrowserRobustnessCoordinator {
107
132
  #budget;
@@ -119,8 +144,7 @@ export class BrowserRobustnessCoordinator {
119
144
  this.#notify = notify;
120
145
  }
121
146
  /**
122
- * Sizes `canvas`'s backing store to its CSS size times `scale` (the device
123
- * pixel ratio), rounded, at least 1x1.
147
+ * Sizes `canvas`'s backing store to its CSS size times `scale` (device pixel ratio), min 1x1.
124
148
  *
125
149
  * @throws RangeError When the scaled size is not a safe integer.
126
150
  */
@@ -139,22 +163,15 @@ export class BrowserRobustnessCoordinator {
139
163
  this.#notify("visibility-suspended");
140
164
  this.#visible = visible;
141
165
  }
142
- /**
143
- * Reports `memory-pressure` and returns the texture budget to fall back to:
144
- * half the configured one, at least one byte.
145
- */
166
+ /** Reports `memory-pressure` and returns the reduced texture budget (half, at least 1 byte). */
146
167
  memoryPressure() {
147
168
  this.#notify("memory-pressure");
148
169
  return Math.max(1, Math.floor(this.#budget.textureBytes / 2));
149
170
  }
150
- /** Reports `context-lost`. */
151
171
  contextLost() {
152
172
  this.#notify("context-lost");
153
173
  }
154
- /**
155
- * Takes one decoder session. Returns `false`, and reports
156
- * `decoder-session-limit`, when all of the budget's sessions are taken.
157
- */
174
+ /** Takes one decoder session, or reports `decoder-session-limit` and returns `false`. */
158
175
  acquireDecoder() {
159
176
  if (this.#decoders >= this.#budget.decoderSessions) {
160
177
  this.#notify("decoder-session-limit");
package/dist/sha256.d.ts CHANGED
@@ -3,24 +3,16 @@ export declare class Sha256 {
3
3
  #private;
4
4
  /** Adds `bytes` to the message. Throws once {@link Sha256.hex} has been called. */
5
5
  update(bytes: Uint8Array): this;
6
- /**
7
- * Finishes the message and returns its hash as 64 lowercase hex digits.
8
- * The hash can be finished once; a second call throws.
9
- */
6
+ /** Returns the hash as 64 lowercase hex digits. Throws if called twice. */
10
7
  hex(): string;
11
8
  }
12
9
  /**
13
- * The SHA-256 of a blob's bytes, read as a stream, as 64 lowercase hex digits.
10
+ * Streams a blob through SHA-256 and returns 64 lowercase hex digits.
14
11
  *
15
- * `onProgress` receives the bytes hashed so far after each piece of at most 1 MiB. Memory stays
16
- * bounded by one chunk of the blob's stream.
12
+ * `onProgress` receives the bytes hashed so far after each piece of at most 1 MiB. Hashing yields
13
+ * to the event loop about every 8 ms so the page stays responsive and `signal` can abort it.
17
14
  *
18
- * A blob whose chunks are already read (a cached local file) resolves each read without a task in
19
- * between, so hashing yields to the event loop about every 8 ms: the page keeps painting and
20
- * handling input, and `signal` can abort it partway.
21
- *
22
- * Rejects with `signal.reason` when `signal` aborts, and with the stream's error when the blob
23
- * cannot be read.
15
+ * Rejects with `signal.reason` on abort, or with the stream's error when the blob cannot be read.
24
16
  */
25
17
  export declare function sha256Blob(blob: Blob, options?: {
26
18
  readonly signal?: AbortSignal;