@alpha2-sh/editor 0.2.1 → 0.3.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.
@@ -9,6 +9,8 @@ import { EditorError } from "../../core/document.js";
9
9
  const RESULT_CACHE_FRAMES = 6;
10
10
  /** The diagnostics panel refreshes at this rate, not at the render rate. */
11
11
  const DIAGNOSTICS_INTERVAL_MS = 250;
12
+ /** A runtime effect definition's identity, `id@version`, as the engine names it. */
13
+ const effectKey = (definition) => `${definition.id}@${definition.version}`;
12
14
  /**
13
15
  * The wasm renderer of one preview or export: owns the engine instance, its frame supply, and a
14
16
  * render queue that collapses to the newest request.
@@ -25,6 +27,11 @@ export class Engine {
25
27
  #lastDiagnosticsAt = 0;
26
28
  #fps = [];
27
29
  #effectResources = new Map();
30
+ /** The runtime effects registered on this renderer, as `id@version`. */
31
+ #effects = new Set();
32
+ /** Frames the player drew, and frames it found equal to the one shown; cumulative. */
33
+ #renders = 0;
34
+ #unchanged = 0;
28
35
  /** What `supply` uploaded for the export frame the engine has not rendered yet. */
29
36
  #exportUploads = [];
30
37
  constructor(canvas, sources, library, runtimeAssets = {}) {
@@ -34,6 +41,8 @@ export class Engine {
34
41
  this.library = library;
35
42
  this.kind = "wasm";
36
43
  this.traceUploads = false;
44
+ /** Which preview draws with this renderer, as its diagnostics report it. */
45
+ this.backend = "main-thread";
37
46
  this.lastDiagnostics = null;
38
47
  this.lastRenderMs = 0;
39
48
  this.onDiagnostics = null;
@@ -71,6 +80,31 @@ export class Engine {
71
80
  #sizeResultCache() {
72
81
  this.#registry.setResultCacheBudget(RESULT_CACHE_FRAMES * this.canvas.width * this.canvas.height * 8);
73
82
  }
83
+ /**
84
+ * Turns per-pass GPU and CPU timing on or off; the passes then appear in `lastDiagnostics`.
85
+ * Off by default: timing is profiling cost, not rendering work.
86
+ */
87
+ setPassTiming(enabled) {
88
+ this.#registry?.setPassTiming(enabled);
89
+ }
90
+ /**
91
+ * Sets the preview resolution scale (the engine's `QualityProfile` resolution scale; 1 renders
92
+ * at the document's size) and returns the loaded project's render size as `[width, height]`, or
93
+ * null with no project loaded. Throws for a scale the engine refuses.
94
+ */
95
+ setPreviewScale(scale) {
96
+ if (!this.#registry)
97
+ throw new Error("the renderer is not initialized");
98
+ this.#registry.setPreviewScale(scale);
99
+ this.#revision += 1;
100
+ return this.previewSize;
101
+ }
102
+ /** The loaded project's render size at the preview scale, `[width, height]`, or null. */
103
+ get previewSize() {
104
+ if (!this.#registry || !this.#project)
105
+ return null;
106
+ return Array.from(this.#registry.previewSize);
107
+ }
74
108
  /** The renderer's part of the capability report: GPU, color, and playback. */
75
109
  capabilityReport() {
76
110
  return this.#registry
@@ -154,6 +188,46 @@ export class Engine {
154
188
  this.#revision += 1;
155
189
  this.#registry.resetPlayer();
156
190
  }
191
+ /**
192
+ * Compiles runtime effect definitions (`alpha2/effects` `EffectDefinition`s) on this renderer's
193
+ * device and registers them, all or none. An `id@version` this renderer holds already is skipped:
194
+ * a definition never changes under its version.
195
+ *
196
+ * Throws `invalid_effect` naming the first definition the engine refuses, with its diagnostics;
197
+ * then none of the call's definitions is registered.
198
+ */
199
+ async registerEffects(definitions, operation = "preview") {
200
+ if (!this.#registry)
201
+ throw new Error("the renderer is not initialized");
202
+ const fresh = definitions.filter((definition) => !this.#effects.has(effectKey(definition)));
203
+ const refusal = (definition, error) => new EditorError("invalid_effect", `Effect ${effectKey(definition)} is not one the engine runs: ${error instanceof Error ? error.message : String(error)}`, operation, effectKey(definition), true, { cause: error });
204
+ // `compileEffect` throws for a definition it refuses before compiling, else its promise
205
+ // rejects with the shader's diagnostics.
206
+ const compiled = await Promise.allSettled(fresh.map(async (definition) => this.#registry.compileEffect(JSON.stringify(definition))));
207
+ const failed = compiled.findIndex(({ status }) => status === "rejected");
208
+ if (failed !== -1) {
209
+ for (const result of compiled)
210
+ if (result.status === "fulfilled")
211
+ result.value.free?.();
212
+ throw refusal(fresh[failed], compiled[failed].reason);
213
+ }
214
+ const registered = [];
215
+ for (const [index, definition] of fresh.entries()) {
216
+ try {
217
+ this.#registry.registerCompiledEffect(compiled[index].value);
218
+ }
219
+ catch (error) {
220
+ for (const later of compiled.slice(index + 1))
221
+ later.value.free?.();
222
+ for (const done of registered)
223
+ this.#registry.unregisterEffect(done.id, done.version);
224
+ throw refusal(definition, error);
225
+ }
226
+ registered.push(definition);
227
+ }
228
+ for (const definition of registered)
229
+ this.#effects.add(effectKey(definition));
230
+ }
157
231
  /**
158
232
  * Registers the fonts caption tracks draw with, in fallback order. An SVG's text draws with the
159
233
  * same fonts, as the native host draws it with its caption fonts. Returns the fonts the engine
@@ -239,7 +313,9 @@ export class Engine {
239
313
  }
240
314
  /**
241
315
  * Requests one frame. Concurrent requests collapse to the most recent one so that scrubbing never
242
- * queues a backlog of stale frames.
316
+ * queues a backlog of stale frames. Resolves `{ rendered, unchanged }`: `rendered` is false when
317
+ * the request was superseded, and `unchanged` says the frame equals the one already shown, so
318
+ * nothing was drawn or presented and the surface still shows it.
243
319
  */
244
320
  render(seconds, { generation = null, context } = {}) {
245
321
  if (!this.#registry)
@@ -288,7 +364,7 @@ export class Engine {
288
364
  const isCurrent = () => revision === this.#revision &&
289
365
  (generation === null || generation === this.#generation);
290
366
  if (!isCurrent())
291
- return false;
367
+ return { rendered: false, unchanged: false };
292
368
  await this.sources.release(context.liveSourceIds);
293
369
  const started = performance.now();
294
370
  const playback = await this.#supply.render(BigInt(secondsToTicks(seconds)), {
@@ -302,10 +378,15 @@ export class Engine {
302
378
  isCurrent,
303
379
  });
304
380
  if (!playback)
305
- return false;
381
+ return { rendered: false, unchanged: false };
382
+ const unchanged = playback.unchanged === true;
383
+ if (unchanged)
384
+ this.#unchanged += 1;
385
+ else
386
+ this.#renders += 1;
306
387
  this.lastRenderMs = performance.now() - started;
307
388
  this.#sampleDiagnostics();
308
- return true;
389
+ return { rendered: true, unchanged };
309
390
  }
310
391
  #sampleDiagnostics() {
311
392
  const now = performance.now();
@@ -326,6 +407,10 @@ export class Engine {
326
407
  fps: Number(this.fps.toFixed(1)),
327
408
  renderMs: Number(this.lastRenderMs.toFixed(2)),
328
409
  uploadedFrames: this.#registry.uploadedFrameCount,
410
+ renders: this.#renders,
411
+ unchanged: this.#unchanged,
412
+ renderSize: [this.canvas.width, this.canvas.height],
413
+ backend: this.backend,
329
414
  };
330
415
  this.lastDiagnostics = diagnostics;
331
416
  this.onDiagnostics?.(diagnostics);
@@ -29,7 +29,8 @@ export function probeEncoders(width: any, height: any, { frameRate, videoQuality
29
29
  * opened output (not yet finished), and the export report. Any failure discards the output and is
30
30
  * rethrown as an `ExportFailureError` with the phase it happened in; a cancellation rethrows the
31
31
  * abort reason. `createSink(options)` makes the muxer the frames and audio are written to, a
32
- * `MediabunnyMp4Sink` unless a test supplies another.
32
+ * `MediabunnySink` unless a test supplies another. `openOutput` receives the export's container
33
+ * beside the size estimate, so the file is named and typed for it.
33
34
  */
34
35
  export function exportTimeline({ supplyFrame, renderFrame, sourceTransfer, engine, project, doc, library, audioSupplier, range, options, openOutput, onProgress, signal, createSink, }: {
35
36
  supplyFrame: any;
@@ -45,15 +46,16 @@ export function exportTimeline({ supplyFrame, renderFrame, sourceTransfer, engin
45
46
  openOutput: any;
46
47
  onProgress: any;
47
48
  signal: any;
48
- createSink?: ((sinkOptions: any) => MediabunnyMp4Sink) | undefined;
49
+ createSink?: ((sinkOptions: any) => MediabunnySink) | undefined;
49
50
  }): Promise<{
50
- written: import("alpha2/mediabunny").Mp4Output;
51
+ written: import("alpha2/mediabunny").ExportOutput;
51
52
  output: any;
52
53
  result: {
53
54
  renderMs: number;
54
55
  frames: number;
55
56
  audio: boolean;
56
- videoCodec: any;
57
+ container: import("alpha2/containers").ExportContainer;
58
+ videoCodec: import("alpha2/rate-control").BrowserVideoCodec;
57
59
  videoRateControl: {
58
60
  bitrate: number;
59
61
  mode: "constant" | "variable";
@@ -62,6 +64,7 @@ export function exportTimeline({ supplyFrame, renderFrame, sourceTransfer, engin
62
64
  source: string;
63
65
  };
64
66
  appliedVideoRateControl: import("alpha2/mediabunny").AppliedVideoRateControl | null;
67
+ videoLatencyMode: import("alpha2/mediabunny").VideoEncoderLatencyMode | null;
65
68
  audioCodec: "aac" | "opus" | undefined;
66
69
  audioRateControl: {
67
70
  bitrate: number;
@@ -107,17 +110,27 @@ export function exportTimeline({ supplyFrame, renderFrame, sourceTransfer, engin
107
110
  };
108
111
  };
109
112
  }>;
113
+ /**
114
+ * The export's container and video codec, with the container's defaults for what `options` leave
115
+ * out: `mp4` and its `avc`. A container that does not offer a requested codec (`webm` with `avc`,
116
+ * `mov` with `opus`) is refused before anything renders, as `INVALID_INPUT`.
117
+ */
118
+ export function exportFormat(options: any): import("alpha2/containers").ExportFormat;
110
119
  /**
111
120
  * The audio codec this browser encodes the export's audio with, and the rate its encoder is given:
112
- * AAC where it can, else Opus, each at the project rate where the encoder takes it and otherwise at
113
- * the rate a native host would convert to (`audioEncoderSampleRate` in `alpha2/mediabunny`), so
114
- * every project rate exports. A browser that encodes neither at any rate (Safari before 26 has a
115
- * VideoEncoder but no AudioEncoder) is refused here, before anything renders, as `INVALID_INPUT`:
116
- * a retry would meet the same browser.
121
+ * `codec` when one is asked for, else the first of the container's codecs this browser encodes, in
122
+ * the container's order (AAC, then Opus, in MP4 and MKV; AAC only in MOV; Opus only in WebM), each
123
+ * at the project rate where the encoder takes it and otherwise at the rate a native host would
124
+ * convert to (`audioEncoderSampleRate` in `alpha2/mediabunny`), so every project rate exports. A
125
+ * browser that encodes none of them at any rate (Safari before 26 has a VideoEncoder but no
126
+ * AudioEncoder; Firefox on Linux has no AAC encoder for a MOV) is refused here, before anything
127
+ * renders, as `INVALID_INPUT`: a retry would meet the same browser.
117
128
  *
118
129
  * @returns {Promise<{ codec: "aac" | "opus", sampleRate: number }>}
119
130
  */
120
- export function chooseAudioCodec(sampleRate: any, audioQuality: any): Promise<{
131
+ export function chooseAudioCodec(sampleRate: any, audioQuality: any, { container, codec: requested }?: {
132
+ container?: string | undefined;
133
+ }): Promise<{
121
134
  codec: "aac" | "opus";
122
135
  sampleRate: number;
123
136
  }>;
@@ -136,5 +149,5 @@ export const VIDEO_CODECS: {
136
149
  label: string;
137
150
  }[];
138
151
  export { engineVideoCodec };
139
- import { MediabunnyMp4Sink } from "alpha2/mediabunny";
152
+ import { MediabunnySink } from "alpha2/mediabunny";
140
153
  import { engineVideoCodec } from "alpha2/rate-control";
@@ -1,6 +1,7 @@
1
1
  // Deterministic export: the engine renders offscreen at export quality on its own exact rational
2
2
  // frame schedule, so no swapchain race can duplicate a frame and a missing source retries in place.
3
- import { audioEncoderSampleRate, canEncodeVideoTrack, MediabunnyMp4Sink, } from "alpha2/mediabunny";
3
+ import { ExportFormatError, offeredAudioCodecs, resolveExportFormat, } from "alpha2/containers";
4
+ import { audioEncoderSampleRate, canEncodeVideoTrack, MediabunnySink, } from "alpha2/mediabunny";
4
5
  import { engineVideoCodec, qualityLevels, resolveAudioBitrate, resolveVideoRateControl, } from "alpha2/rate-control";
5
6
  import { videoFrameFromI420, videoFrameRepackedBytes, } from "alpha2/video-frames";
6
7
  import * as engineBinding from "alpha2/wasm";
@@ -68,9 +69,10 @@ export async function probeEncoders(width, height, { frameRate = { num: 30, den:
68
69
  * opened output (not yet finished), and the export report. Any failure discards the output and is
69
70
  * rethrown as an `ExportFailureError` with the phase it happened in; a cancellation rethrows the
70
71
  * abort reason. `createSink(options)` makes the muxer the frames and audio are written to, a
71
- * `MediabunnyMp4Sink` unless a test supplies another.
72
+ * `MediabunnySink` unless a test supplies another. `openOutput` receives the export's container
73
+ * beside the size estimate, so the file is named and typed for it.
72
74
  */
73
- export async function exportTimeline({ supplyFrame, renderFrame, sourceTransfer, engine, project, doc, library, audioSupplier, range, options, openOutput, onProgress, signal, createSink = (sinkOptions) => new MediabunnyMp4Sink(sinkOptions), }) {
75
+ export async function exportTimeline({ supplyFrame, renderFrame, sourceTransfer, engine, project, doc, library, audioSupplier, range, options, openOutput, onProgress, signal, createSink = (sinkOptions) => new MediabunnySink(sinkOptions), }) {
74
76
  const started = performance.now();
75
77
  const diagnosticsBefore = rendererDiagnostics(engine);
76
78
  const transferBefore = sourceTransfer?.();
@@ -83,12 +85,19 @@ export async function exportTimeline({ supplyFrame, renderFrame, sourceTransfer,
83
85
  let frameCount = Math.max(1, Math.round((end - start) * fps));
84
86
  const audio = project ? audibleSources(project, library) : [];
85
87
  const wantsAudio = options.includeAudio !== false && audio.length > 0 && engine != null;
88
+ // The container's defaults fill what the options leave out; the audio codec is settled below,
89
+ // by what this browser encodes.
90
+ const format = exportFormat(options);
91
+ const container = format.container;
86
92
  const audioQuality = options.audioQuality ?? "high";
87
93
  const audioEncoding = wantsAudio
88
- ? await chooseAudioCodec(settings.sampleRate, audioQuality)
94
+ ? await chooseAudioCodec(settings.sampleRate, audioQuality, {
95
+ container,
96
+ codec: options.audioCodec,
97
+ })
89
98
  : undefined;
90
99
  const audioCodec = audioEncoding?.codec;
91
- const videoCodec = options.videoCodec ?? "avc";
100
+ const videoCodec = format.videoCodec;
92
101
  const videoQuality = options.videoQuality ?? "high";
93
102
  // Resolved by the engine, so `high` costs the same here as on the native path.
94
103
  const videoRateControl = resolvedVideoRate({
@@ -110,11 +119,13 @@ export async function exportTimeline({ supplyFrame, renderFrame, sourceTransfer,
110
119
  };
111
120
  // Before anything renders: refused here when the destination cannot hold the estimated file.
112
121
  const output = await openOutput({
122
+ container,
113
123
  durationSeconds: end - start,
114
124
  videoBitrate: videoRateControl.bitrate,
115
125
  audioBitrate: audioBitrate ?? 0,
116
126
  });
117
127
  const sink = createSink({
128
+ container,
118
129
  videoCodec,
119
130
  audioCodec,
120
131
  videoRateControl,
@@ -375,6 +386,7 @@ export async function exportTimeline({ supplyFrame, renderFrame, sourceTransfer,
375
386
  renderMs,
376
387
  frames: produced,
377
388
  audio: wantsAudio,
389
+ container,
378
390
  videoCodec,
379
391
  // Named like the native host's result for native-versus-Wasm comparison.
380
392
  videoRateControl: {
@@ -386,6 +398,7 @@ export async function exportTimeline({ supplyFrame, renderFrame, sourceTransfer,
386
398
  },
387
399
  // `quantizer` when the encoder accepted constant-quantizer encoding, else the bitrate path.
388
400
  appliedVideoRateControl: sink.appliedVideoRateControl ?? null,
401
+ videoLatencyMode: sink.videoLatencyMode ?? null,
389
402
  audioCodec,
390
403
  audioRateControl: audioBitrate === undefined
391
404
  ? null
@@ -688,20 +701,54 @@ async function i420VideoFrame(step, timestamp, duration) {
688
701
  colorSpace: step.colorSpace,
689
702
  });
690
703
  }
691
- /** The audio codecs an export may write, in the order they are preferred, with their names. */
704
+ /** The audio codecs an export may write, with their names. */
692
705
  const AUDIO_CODECS = { aac: "AAC", opus: "Opus" };
706
+ /**
707
+ * The export's container and video codec, with the container's defaults for what `options` leave
708
+ * out: `mp4` and its `avc`. A container that does not offer a requested codec (`webm` with `avc`,
709
+ * `mov` with `opus`) is refused before anything renders, as `INVALID_INPUT`.
710
+ */
711
+ export function exportFormat(options) {
712
+ try {
713
+ return resolveExportFormat({
714
+ container: options.container ?? "mp4",
715
+ ...(options.videoCodec === undefined
716
+ ? {}
717
+ : { videoCodec: options.videoCodec }),
718
+ ...(options.audioCodec === undefined
719
+ ? {}
720
+ : { audioCodec: options.audioCodec }),
721
+ });
722
+ }
723
+ catch (error) {
724
+ if (!(error instanceof ExportFormatError))
725
+ throw error;
726
+ throw new ExportFailureError({
727
+ code: "INVALID_INPUT",
728
+ message: `${error.message}.`,
729
+ origin: "browser",
730
+ operation: "export",
731
+ backend: "unknown",
732
+ phase: "prepare",
733
+ resource: "unknown",
734
+ });
735
+ }
736
+ }
693
737
  /**
694
738
  * The audio codec this browser encodes the export's audio with, and the rate its encoder is given:
695
- * AAC where it can, else Opus, each at the project rate where the encoder takes it and otherwise at
696
- * the rate a native host would convert to (`audioEncoderSampleRate` in `alpha2/mediabunny`), so
697
- * every project rate exports. A browser that encodes neither at any rate (Safari before 26 has a
698
- * VideoEncoder but no AudioEncoder) is refused here, before anything renders, as `INVALID_INPUT`:
699
- * a retry would meet the same browser.
739
+ * `codec` when one is asked for, else the first of the container's codecs this browser encodes, in
740
+ * the container's order (AAC, then Opus, in MP4 and MKV; AAC only in MOV; Opus only in WebM), each
741
+ * at the project rate where the encoder takes it and otherwise at the rate a native host would
742
+ * convert to (`audioEncoderSampleRate` in `alpha2/mediabunny`), so every project rate exports. A
743
+ * browser that encodes none of them at any rate (Safari before 26 has a VideoEncoder but no
744
+ * AudioEncoder; Firefox on Linux has no AAC encoder for a MOV) is refused here, before anything
745
+ * renders, as `INVALID_INPUT`: a retry would meet the same browser.
700
746
  *
701
747
  * @returns {Promise<{ codec: "aac" | "opus", sampleRate: number }>}
702
748
  */
703
- export async function chooseAudioCodec(sampleRate, audioQuality) {
704
- for (const codec of /** @type {("aac" | "opus")[]} */ (Object.keys(AUDIO_CODECS))) {
749
+ export async function chooseAudioCodec(sampleRate, audioQuality, { container = "mp4", codec: requested } = {}) {
750
+ const candidates = requested === undefined ? offeredAudioCodecs(container) : [requested];
751
+ for (const codec of candidates) {
705
752
  const encoderRate = await audioEncoderSampleRate(codec, {
706
753
  sampleRate,
707
754
  numberOfChannels: 2,
@@ -712,7 +759,7 @@ export async function chooseAudioCodec(sampleRate, audioQuality) {
712
759
  }
713
760
  throw new ExportFailureError({
714
761
  code: "INVALID_INPUT",
715
- message: `This browser cannot encode audio (${Object.values(AUDIO_CODECS).join(" or ")}, stereo) at ${sampleRate} Hz or at any rate it could be converted to. Export without audio, or use the native exporter.`,
762
+ message: `This browser cannot encode audio for ${container} (${candidates.map((codec) => AUDIO_CODECS[codec]).join(" or ")}, stereo) at ${sampleRate} Hz or at any rate it could be converted to. ${requested === undefined ? "Export without audio, choose another container" : "Choose another audio codec, export without audio"}, or use the native exporter.`,
716
763
  origin: "browser",
717
764
  operation: "export",
718
765
  backend: "unknown",
@@ -34,12 +34,7 @@ export function encodeGenerator(generator: any): {
34
34
  radius?: undefined;
35
35
  angle_degrees?: undefined;
36
36
  };
37
- shape?: undefined;
38
- center?: undefined;
39
- size?: undefined;
40
- rotation_degrees?: undefined;
41
- fill?: undefined;
42
- stroke?: undefined;
37
+ items?: undefined;
43
38
  } | {
44
39
  type: string;
45
40
  paint: {
@@ -66,12 +61,7 @@ export function encodeGenerator(generator: any): {
66
61
  radius?: undefined;
67
62
  angle_degrees?: undefined;
68
63
  };
69
- shape?: undefined;
70
- center?: undefined;
71
- size?: undefined;
72
- rotation_degrees?: undefined;
73
- fill?: undefined;
74
- stroke?: undefined;
64
+ items?: undefined;
75
65
  } | {
76
66
  type: string;
77
67
  paint: {
@@ -95,12 +85,7 @@ export function encodeGenerator(generator: any): {
95
85
  end?: undefined;
96
86
  angle_degrees?: undefined;
97
87
  };
98
- shape?: undefined;
99
- center?: undefined;
100
- size?: undefined;
101
- rotation_degrees?: undefined;
102
- fill?: undefined;
103
- stroke?: undefined;
88
+ items?: undefined;
104
89
  } | {
105
90
  type: string;
106
91
  paint: {
@@ -124,51 +109,45 @@ export function encodeGenerator(generator: any): {
124
109
  end?: undefined;
125
110
  radius?: undefined;
126
111
  };
127
- shape?: undefined;
128
- center?: undefined;
129
- size?: undefined;
130
- rotation_degrees?: undefined;
131
- fill?: undefined;
132
- stroke?: undefined;
112
+ items?: undefined;
133
113
  } | {
134
114
  type: string;
135
- shape: {
115
+ items: ({
116
+ rotation_degrees: {
117
+ mode: string;
118
+ value: any;
119
+ };
120
+ corner_radius?: {
121
+ mode: string;
122
+ value: any;
123
+ } | undefined;
136
124
  type: string;
137
- corner_radius: number;
125
+ center: {
126
+ mode: string;
127
+ value: any;
128
+ };
129
+ size: {
130
+ mode: string;
131
+ value: any;
132
+ };
138
133
  } | {
139
134
  type: string;
140
- corner_radius?: undefined;
141
- };
142
- center: {
143
- x: number;
144
- y: number;
145
- };
146
- size: {
147
- x: number;
148
- y: number;
149
- };
150
- rotation_degrees: any;
151
- fill: {
152
- type: string;
153
- color: {
154
- red: number;
155
- green: number;
156
- blue: number;
157
- alpha: number;
135
+ width: {
136
+ mode: string;
137
+ value: any;
158
138
  };
159
- } | null;
160
- stroke: {
161
- width: number;
162
139
  paint: {
163
- type: string;
164
- color: {
165
- red: number;
166
- green: number;
167
- blue: number;
168
- alpha: number;
169
- };
140
+ mode: string;
141
+ value: any;
142
+ };
143
+ } | {
144
+ type: string;
145
+ paint: {
146
+ mode: string;
147
+ value: any;
170
148
  };
171
- } | null;
149
+ width?: undefined;
150
+ })[];
172
151
  paint?: undefined;
173
152
  };
174
153
  /**
@@ -75,27 +75,45 @@ export function encodeGenerator(generator) {
75
75
  default: {
76
76
  const stroked = generator.stroke.enabled;
77
77
  const filled = generator.fill || !stroked;
78
- return {
79
- type: "shape",
80
- shape: generator.kind === "rectangle"
81
- ? {
82
- type: "rectangle",
83
- corner_radius: Math.max(0, generator.cornerRadius),
84
- }
85
- : { type: "ellipse" },
86
- center,
87
- size: {
78
+ const constant = (value) => ({ mode: "constant", value });
79
+ const geometry = {
80
+ type: generator.kind === "rectangle" ? "rectangle" : "ellipse",
81
+ center: constant(center),
82
+ size: constant({
88
83
  x: Math.max(0, generator.size.x),
89
84
  y: Math.max(0, generator.size.y),
90
- },
91
- rotation_degrees: generator.rotation,
92
- fill: filled ? { type: "solid", color: rgba(first) } : null,
93
- stroke: stroked
94
- ? {
95
- width: Math.max(0.001, generator.stroke.width),
96
- paint: { type: "solid", color: rgba(generator.stroke.color) },
97
- }
98
- : null,
85
+ }),
86
+ ...(generator.kind === "rectangle"
87
+ ? { corner_radius: constant(Math.max(0, generator.cornerRadius)) }
88
+ : {}),
89
+ rotation_degrees: constant(generator.rotation),
90
+ };
91
+ // The stroke is listed first, so it is drawn over the fill.
92
+ return {
93
+ type: "shapes",
94
+ items: [
95
+ geometry,
96
+ ...(stroked
97
+ ? [
98
+ {
99
+ type: "stroke",
100
+ width: constant(Math.max(0.001, generator.stroke.width)),
101
+ paint: constant({
102
+ type: "solid",
103
+ color: rgba(generator.stroke.color),
104
+ }),
105
+ },
106
+ ]
107
+ : []),
108
+ ...(filled
109
+ ? [
110
+ {
111
+ type: "fill",
112
+ paint: constant({ type: "solid", color: rgba(first) }),
113
+ },
114
+ ]
115
+ : []),
116
+ ],
99
117
  };
100
118
  }
101
119
  }
@@ -12,13 +12,16 @@ export function storageSupport(): {
12
12
  * counts every write since its last disk scan, so `quota - usage` can be near zero on an empty
13
13
  * disk. A full disk fails at the write (`STORAGE_QUOTA_EXCEEDED`); the estimate is only reported.
14
14
  *
15
+ * `container` (default `mp4`) gives the temporary file its extension and the artifact its MIME
16
+ * type.
15
17
  * Returns `{ mode, destination, storageEstimate, abort, finish }`: `destination` is the sink's,
16
18
  * `abort` discards the file, and `finish(sinkOutput)` turns the finished output into the artifact's
17
19
  * fields.
18
20
  */
19
- export function openExportOutput({ jobId, name, target, durationSeconds, videoBitrate, audioBitrate, }: {
21
+ export function openExportOutput({ jobId, name, container, target, durationSeconds, videoBitrate, audioBitrate, }: {
20
22
  jobId: any;
21
23
  name: any;
24
+ container?: string | undefined;
22
25
  target: any;
23
26
  durationSeconds: any;
24
27
  videoBitrate: any;