alpha2 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.
- package/README.md +6 -4
- package/dist/containers.d.ts +89 -0
- package/dist/containers.js +150 -0
- package/dist/effects.d.ts +16 -2
- package/dist/frame-supply.d.ts +8 -0
- package/dist/frame-supply.js +26 -4
- package/dist/generated/project-schema.d.ts +564 -100
- package/dist/generated/project-schema.js +36 -0
- package/dist/generated/project-shape.js +345 -55
- package/dist/geometry.d.ts +98 -0
- package/dist/geometry.js +71 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/mediabunny.d.ts +54 -27
- package/dist/mediabunny.js +261 -46
- package/dist/player.d.ts +5 -0
- package/dist/presented-frames.d.ts +16 -0
- package/dist/presented-frames.js +31 -0
- package/dist/project.d.ts +26 -2
- package/dist/project.js +358 -55
- package/dist/worker.js +15 -1
- package/package.json +9 -1
- package/wasm/alpha2_audio_worklet.wasm +0 -0
- package/wasm/alpha2_extensions_bg.wasm +0 -0
- package/wasm/alpha2_wasm.d.ts +178 -8
- package/wasm/alpha2_wasm.js +506 -34
- package/wasm/alpha2_wasm_bg.wasm +0 -0
- package/wasm/alpha2_wasm_bg.wasm.d.ts +23 -4
- /package/wasm/snippets/{alpha2-wasm-6cb97e5ef34d0f09 → alpha2-wasm-7f557ec9a60cc69b}/inline0.js +0 -0
- /package/wasm/snippets/{alpha2-wasm-6cb97e5ef34d0f09 → alpha2-wasm-7f557ec9a60cc69b}/inline1.js +0 -0
- /package/wasm/snippets/{alpha2-wasm-6cb97e5ef34d0f09 → alpha2-wasm-7f557ec9a60cc69b}/inline2.js +0 -0
package/dist/mediabunny.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { readAudioSpeakers } from "./audio-layout.js";
|
|
2
2
|
import { chromaSitingForStream } from "./chroma-siting.js";
|
|
3
3
|
import { decoderOrderKey, decoderPlaneOrder } from "./decoder-order.js";
|
|
4
|
-
import { ALL_FORMATS, AudioSample, AudioSampleSink, AudioSampleSource, BlobSource, BufferTarget, BufferSource, canEncodeAudio, canEncodeVideo, StreamTarget, EncodedPacketSink, Input, Mp4OutputFormat, Output, Quality, VideoSample, VideoSampleSink, VideoSampleSource, } from "mediabunny";
|
|
4
|
+
import { ALL_FORMATS, AudioSample, AudioSampleSink, AudioSampleSource, BlobSource, BufferTarget, BufferSource, canEncodeAudio, canEncodeVideo, StreamTarget, EncodedPacketSink, Input, MkvOutputFormat, MovOutputFormat, Mp4OutputFormat, Output, Quality, VideoSample, VideoSampleSink, VideoSampleSource, WebMOutputFormat, } from "mediabunny";
|
|
5
5
|
import { audioFrameGrid, MAX_AUDIO_CHANNELS, microsecondsToTicks, placeOnAudioFrameGrid, presentationSlackTicks, readMatroskaCodecDelay, readVideoOrientation, roundingQuantum, ROUNDING_SCAN_BYTES, ROUNDING_SCAN_FRAMES, validateStallTimeout, } from "./media.js";
|
|
6
|
+
import { resolveExportFormat } from "./containers.js";
|
|
6
7
|
import { ExportFailureError } from "./export-failure.js";
|
|
7
8
|
/**
|
|
8
9
|
* How far past the decode cursor a request may be (2 s) and still be reached by decoding forward;
|
|
@@ -28,6 +29,8 @@ const DEFAULT_MAX_CACHED_FRAMES = 2;
|
|
|
28
29
|
const MIN_CACHED_FRAMES = 2;
|
|
29
30
|
/** How long reading one frame may take before the decoder is taken to be stuck. */
|
|
30
31
|
const DEFAULT_STALL_TIMEOUT_MS = 5_000;
|
|
32
|
+
/** How long the video encoder may hold frames without output before it is taken to be stuck. */
|
|
33
|
+
const DEFAULT_ENCODER_STALL_TIMEOUT_MS = 10_000;
|
|
31
34
|
/** Bytes of copied frames the reverse window keeps by default: about forty 1080p frames. */
|
|
32
35
|
const DEFAULT_MAX_REVERSE_FRAME_BYTES = 128 * 1024 * 1024;
|
|
33
36
|
const TICKS_PER_SECOND = 120_000;
|
|
@@ -1082,7 +1085,7 @@ export class MediabunnyAudioDecoder {
|
|
|
1082
1085
|
}
|
|
1083
1086
|
/**
|
|
1084
1087
|
* The mediabunny quality a video track encodes at for the engine's rate control: what
|
|
1085
|
-
* {@link
|
|
1088
|
+
* {@link MediabunnySink} configures its encoder with.
|
|
1086
1089
|
*/
|
|
1087
1090
|
export function videoEncoderQuality(rate) {
|
|
1088
1091
|
return new Quality({
|
|
@@ -1093,7 +1096,7 @@ export function videoEncoderQuality(rate) {
|
|
|
1093
1096
|
}
|
|
1094
1097
|
/**
|
|
1095
1098
|
* Whether this browser can encode `codec` video at `width`x`height` with `rate`, asked with the
|
|
1096
|
-
* configurations {@link
|
|
1099
|
+
* configurations {@link MediabunnySink} would try. WebCodecs leaves the codecs to the browser
|
|
1097
1100
|
* (Chrome on Linux encodes no HEVC, for one), so an export asks before it opens its encoder.
|
|
1098
1101
|
*/
|
|
1099
1102
|
export async function canEncodeVideoTrack(codec, options) {
|
|
@@ -1104,7 +1107,122 @@ export async function canEncodeVideoTrack(codec, options) {
|
|
|
1104
1107
|
}).catch(() => false);
|
|
1105
1108
|
}
|
|
1106
1109
|
/**
|
|
1107
|
-
* The
|
|
1110
|
+
* The profiles mediabunny opens (H.264 High, HEVC Main, VP9 profile 0, AV1 Main), at a level for
|
|
1111
|
+
* the probe's frames.
|
|
1112
|
+
*/
|
|
1113
|
+
const LATENCY_PROBE_CODECS = {
|
|
1114
|
+
avc: "avc1.64001e",
|
|
1115
|
+
hevc: "hev1.1.6.L93.B0",
|
|
1116
|
+
vp9: "vp09.00.10.08",
|
|
1117
|
+
av1: "av01.0.04M.08",
|
|
1118
|
+
};
|
|
1119
|
+
const LATENCY_PROBE_WIDTH = 320;
|
|
1120
|
+
const LATENCY_PROBE_HEIGHT = 240;
|
|
1121
|
+
/** More frames than Safari's encoder takes in before it stops. */
|
|
1122
|
+
const LATENCY_PROBE_FRAMES = 6;
|
|
1123
|
+
/** How long a probe encoder has to put out its first chunk. */
|
|
1124
|
+
const LATENCY_PROBE_TIMEOUT_MS = 1_500;
|
|
1125
|
+
const latencyModes = new Map();
|
|
1126
|
+
/**
|
|
1127
|
+
* The latency mode {@link MediabunnySink} opens `codec`'s encoder with: `"realtime"` only where a
|
|
1128
|
+
* quality-mode encoder puts out nothing (Safari 27's H.264) and a realtime one works. Measured once
|
|
1129
|
+
* per codec.
|
|
1130
|
+
*/
|
|
1131
|
+
export function videoEncoderLatencyMode(codec) {
|
|
1132
|
+
let mode = latencyModes.get(codec);
|
|
1133
|
+
if (mode === undefined) {
|
|
1134
|
+
mode = measureLatencyMode(codec);
|
|
1135
|
+
latencyModes.set(codec, mode);
|
|
1136
|
+
}
|
|
1137
|
+
return mode;
|
|
1138
|
+
}
|
|
1139
|
+
async function measureLatencyMode(codec) {
|
|
1140
|
+
if (typeof VideoEncoder === "undefined")
|
|
1141
|
+
return "quality";
|
|
1142
|
+
// One at a time: on Chrome a realtime encoder opening at once delays the quality one.
|
|
1143
|
+
if ((await encoderPutsOut(codec, "quality")) !== false)
|
|
1144
|
+
return "quality";
|
|
1145
|
+
return (await encoderPutsOut(codec, "realtime")) === true
|
|
1146
|
+
? "realtime"
|
|
1147
|
+
: "quality";
|
|
1148
|
+
}
|
|
1149
|
+
/** `true` once the encoder puts out a chunk, `false` if it puts out none, `undefined` if it failed. */
|
|
1150
|
+
async function encoderPutsOut(codec, latencyMode) {
|
|
1151
|
+
const config = {
|
|
1152
|
+
codec: LATENCY_PROBE_CODECS[codec],
|
|
1153
|
+
width: LATENCY_PROBE_WIDTH,
|
|
1154
|
+
height: LATENCY_PROBE_HEIGHT,
|
|
1155
|
+
bitrate: 500_000,
|
|
1156
|
+
framerate: 30,
|
|
1157
|
+
latencyMode,
|
|
1158
|
+
...(codec === "avc" ? { avc: { format: "avc" } } : {}),
|
|
1159
|
+
};
|
|
1160
|
+
try {
|
|
1161
|
+
if (!(await VideoEncoder.isConfigSupported(config)).supported)
|
|
1162
|
+
return undefined;
|
|
1163
|
+
}
|
|
1164
|
+
catch {
|
|
1165
|
+
return undefined;
|
|
1166
|
+
}
|
|
1167
|
+
let outputs = 0;
|
|
1168
|
+
let failed = false;
|
|
1169
|
+
let encoder;
|
|
1170
|
+
let timer;
|
|
1171
|
+
let putOut = () => { };
|
|
1172
|
+
const firstOutput = new Promise((resolve) => (putOut = resolve));
|
|
1173
|
+
try {
|
|
1174
|
+
encoder = new VideoEncoder({
|
|
1175
|
+
output: () => {
|
|
1176
|
+
outputs += 1;
|
|
1177
|
+
putOut();
|
|
1178
|
+
},
|
|
1179
|
+
error: () => (failed = true),
|
|
1180
|
+
});
|
|
1181
|
+
encoder.configure(config);
|
|
1182
|
+
const pixels = new Uint8Array(LATENCY_PROBE_WIDTH * LATENCY_PROBE_HEIGHT * 1.5);
|
|
1183
|
+
for (let index = 0; index < LATENCY_PROBE_FRAMES; index += 1) {
|
|
1184
|
+
pixels.fill(16 + index * 32, 0, LATENCY_PROBE_WIDTH);
|
|
1185
|
+
const frame = new VideoFrame(pixels, {
|
|
1186
|
+
format: "I420",
|
|
1187
|
+
codedWidth: LATENCY_PROBE_WIDTH,
|
|
1188
|
+
codedHeight: LATENCY_PROBE_HEIGHT,
|
|
1189
|
+
timestamp: Math.round((index * 1_000_000) / 30),
|
|
1190
|
+
duration: Math.round(1_000_000 / 30),
|
|
1191
|
+
});
|
|
1192
|
+
try {
|
|
1193
|
+
encoder.encode(frame, { keyFrame: index === 0 });
|
|
1194
|
+
}
|
|
1195
|
+
finally {
|
|
1196
|
+
frame.close();
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
await Promise.race([
|
|
1200
|
+
firstOutput,
|
|
1201
|
+
encoder.flush().catch(() => (failed = true)),
|
|
1202
|
+
new Promise((resolve) => {
|
|
1203
|
+
timer = setTimeout(resolve, LATENCY_PROBE_TIMEOUT_MS);
|
|
1204
|
+
}),
|
|
1205
|
+
]);
|
|
1206
|
+
if (outputs > 0)
|
|
1207
|
+
return true;
|
|
1208
|
+
return failed ? undefined : false;
|
|
1209
|
+
}
|
|
1210
|
+
catch {
|
|
1211
|
+
return undefined;
|
|
1212
|
+
}
|
|
1213
|
+
finally {
|
|
1214
|
+
clearTimeout(timer);
|
|
1215
|
+
try {
|
|
1216
|
+
if (encoder?.state !== "closed")
|
|
1217
|
+
encoder?.close();
|
|
1218
|
+
}
|
|
1219
|
+
catch {
|
|
1220
|
+
// Already closed by its own failure.
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
/**
|
|
1225
|
+
* The sample rates each audio codec a {@link MediabunnySink} writes can carry, lowest first:
|
|
1108
1226
|
* AAC's MPEG-4 rates up to the project limit, and Opus's.
|
|
1109
1227
|
*/
|
|
1110
1228
|
const ENCODER_SAMPLE_RATES = {
|
|
@@ -1118,7 +1236,7 @@ const ENCODER_SAMPLE_RATES = {
|
|
|
1118
1236
|
* The rate this browser encodes `codec` audio of a project at `sampleRate` with, by the rule a
|
|
1119
1237
|
* native host follows: the project rate when the browser's encoder takes it, else the lowest rate
|
|
1120
1238
|
* the codec carries above it that the encoder takes, else the highest below it; `null` when the
|
|
1121
|
-
* encoder takes none. Pass it to {@link
|
|
1239
|
+
* encoder takes none. Pass it to {@link MediabunnySink} as `audioSampleRate`, so every project
|
|
1122
1240
|
* rate exports where the browser encodes the codec at all.
|
|
1123
1241
|
*/
|
|
1124
1242
|
export async function audioEncoderSampleRate(codec, options) {
|
|
@@ -1223,21 +1341,39 @@ class EncoderRateConverter {
|
|
|
1223
1341
|
return { startSample: start, channels };
|
|
1224
1342
|
}
|
|
1225
1343
|
}
|
|
1344
|
+
/**
|
|
1345
|
+
* The muxer for `container`. MP4 and MOV go without fast start and MKV and WebM are not
|
|
1346
|
+
* append-only, so the payload is never held in memory or rewritten: the muxer seeks back only to
|
|
1347
|
+
* patch headers.
|
|
1348
|
+
*/
|
|
1349
|
+
function outputFormat(container) {
|
|
1350
|
+
switch (container) {
|
|
1351
|
+
case "mp4":
|
|
1352
|
+
return new Mp4OutputFormat({ fastStart: false });
|
|
1353
|
+
case "mov":
|
|
1354
|
+
return new MovOutputFormat({ fastStart: false });
|
|
1355
|
+
case "mkv":
|
|
1356
|
+
return new MkvOutputFormat({ appendOnly: false });
|
|
1357
|
+
case "webm":
|
|
1358
|
+
return new WebMOutputFormat({ appendOnly: false });
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1226
1361
|
/**
|
|
1227
1362
|
* Optional mediabunny encoder/muxer implementing both public alpha2 sinks.
|
|
1228
1363
|
*
|
|
1229
1364
|
* The sink is the file: its `finish` finalizes every track, which suits one runner, or a host that
|
|
1230
1365
|
* writes both tracks itself and finishes once. When two runners share the file (`exportToSink` for
|
|
1231
1366
|
* the video and `renderOfflineAudio` for the audio, at once or one after the other), give each its
|
|
1232
|
-
* track instead, {@link
|
|
1367
|
+
* track instead, {@link MediabunnySink.videoTrack} and {@link MediabunnySink.audioTrack}: a
|
|
1233
1368
|
* runner's `finish` then ends its own track, and the file is finalized when every configured track
|
|
1234
1369
|
* has ended. Either track's `abort` discards the whole file, and the other runner's next write
|
|
1235
1370
|
* fails.
|
|
1236
1371
|
*
|
|
1237
|
-
* The constructor throws `RangeError` when no track is configured,
|
|
1238
|
-
* memory destination's `maxBytes` is not
|
|
1372
|
+
* The constructor throws `RangeError` when no track is configured, the container does not offer a
|
|
1373
|
+
* codec (an `ExportFormatError`), a codec lacks its rate, a memory destination's `maxBytes` is not
|
|
1374
|
+
* a positive safe integer, or `stallTimeoutMs` is out of range.
|
|
1239
1375
|
*/
|
|
1240
|
-
export class
|
|
1376
|
+
export class MediabunnySink {
|
|
1241
1377
|
#target;
|
|
1242
1378
|
/**
|
|
1243
1379
|
* A stream destination, written through the muxer and committed or discarded by this sink alone:
|
|
@@ -1248,7 +1384,14 @@ export class MediabunnyMp4Sink {
|
|
|
1248
1384
|
#output;
|
|
1249
1385
|
#written = 0;
|
|
1250
1386
|
#overflow;
|
|
1387
|
+
#hasVideo;
|
|
1251
1388
|
#video;
|
|
1389
|
+
#latencyMode;
|
|
1390
|
+
#stallTimeoutMs;
|
|
1391
|
+
#framesIn = 0;
|
|
1392
|
+
#framesOut = 0;
|
|
1393
|
+
/** When the encoder last put a frame out, or was given one while it owed none. */
|
|
1394
|
+
#progressAt = 0;
|
|
1252
1395
|
#audio;
|
|
1253
1396
|
#audioRate;
|
|
1254
1397
|
#audioResampler;
|
|
@@ -1263,6 +1406,17 @@ export class MediabunnyMp4Sink {
|
|
|
1263
1406
|
#videoTrack;
|
|
1264
1407
|
#audioTrack;
|
|
1265
1408
|
constructor(options) {
|
|
1409
|
+
// Before anything is opened or locked: a pair the container does not offer is the caller's
|
|
1410
|
+
// mistake, not a failed export.
|
|
1411
|
+
resolveExportFormat({
|
|
1412
|
+
container: options.container,
|
|
1413
|
+
...(options.videoCodec === undefined
|
|
1414
|
+
? {}
|
|
1415
|
+
: { videoCodec: options.videoCodec }),
|
|
1416
|
+
...(options.audioCodec === undefined
|
|
1417
|
+
? {}
|
|
1418
|
+
: { audioCodec: options.audioCodec }),
|
|
1419
|
+
});
|
|
1266
1420
|
const destination = options.destination;
|
|
1267
1421
|
if (destination.kind === "memory") {
|
|
1268
1422
|
if (!Number.isSafeInteger(destination.maxBytes) ||
|
|
@@ -1306,17 +1460,53 @@ export class MediabunnyMp4Sink {
|
|
|
1306
1460
|
};
|
|
1307
1461
|
}
|
|
1308
1462
|
this.#output = new Output({
|
|
1309
|
-
|
|
1310
|
-
format: new Mp4OutputFormat({ fastStart: false }),
|
|
1463
|
+
format: outputFormat(options.container),
|
|
1311
1464
|
target: this.#target,
|
|
1312
1465
|
});
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1466
|
+
this.#hasVideo = options.videoCodec !== undefined;
|
|
1467
|
+
if (this.#hasVideo && options.videoRateControl === undefined)
|
|
1468
|
+
throw new RangeError("videoRateControl is required with videoCodec");
|
|
1469
|
+
const frameRate = options.frameRate;
|
|
1470
|
+
this.#frameDuration =
|
|
1471
|
+
this.#hasVideo &&
|
|
1472
|
+
frameRate !== undefined &&
|
|
1473
|
+
frameRate.num > 0 &&
|
|
1474
|
+
frameRate.den > 0
|
|
1475
|
+
? frameRate.den / frameRate.num
|
|
1476
|
+
: undefined;
|
|
1477
|
+
this.#stallTimeoutMs =
|
|
1478
|
+
options.stallTimeoutMs ?? DEFAULT_ENCODER_STALL_TIMEOUT_MS;
|
|
1479
|
+
validateStallTimeout(this.#stallTimeoutMs);
|
|
1480
|
+
if (options.audioCodec !== undefined) {
|
|
1481
|
+
if (options.audioBitrate === undefined)
|
|
1482
|
+
throw new RangeError("audioBitrate is required with audioCodec");
|
|
1483
|
+
const rate = options.audioSampleRate;
|
|
1484
|
+
if (rate !== undefined && (!Number.isSafeInteger(rate) || rate <= 0))
|
|
1485
|
+
throw new RangeError("audioSampleRate must be a positive integer");
|
|
1486
|
+
this.#audioRate = rate;
|
|
1487
|
+
this.#audioResampler = options.audioResampler;
|
|
1488
|
+
this.#audio = new AudioSampleSource({
|
|
1489
|
+
codec: options.audioCodec,
|
|
1490
|
+
quality: new Quality({ bitrate: options.audioBitrate }),
|
|
1491
|
+
});
|
|
1492
|
+
}
|
|
1493
|
+
if (!this.#hasVideo && this.#audio === undefined)
|
|
1494
|
+
throw new RangeError("at least one output track is required");
|
|
1495
|
+
this.#ready = this.#open(options);
|
|
1496
|
+
}
|
|
1497
|
+
/** Adds the tracks once the video latency mode is known, and starts the file. */
|
|
1498
|
+
async #open(options) {
|
|
1499
|
+
const codec = options.videoCodec;
|
|
1500
|
+
const rate = options.videoRateControl;
|
|
1501
|
+
if (codec !== undefined && rate !== undefined) {
|
|
1502
|
+
const latencyMode = options.videoLatencyMode ?? (await videoEncoderLatencyMode(codec));
|
|
1503
|
+
if (this.#output.state === "canceled")
|
|
1504
|
+
return;
|
|
1505
|
+
this.#latencyMode = latencyMode;
|
|
1317
1506
|
this.#video = new VideoSampleSource({
|
|
1318
|
-
codec
|
|
1507
|
+
codec,
|
|
1319
1508
|
quality: videoEncoderQuality(rate),
|
|
1509
|
+
latencyMode,
|
|
1320
1510
|
// mediabunny reports each candidate config before it asks the browser whether it is
|
|
1321
1511
|
// supported and stops at the first that is, so the last one reported is the one that runs.
|
|
1322
1512
|
onEncoderConfig: (config) => {
|
|
@@ -1328,33 +1518,45 @@ export class MediabunnyMp4Sink {
|
|
|
1328
1518
|
: { bitrate: config.bitrate }),
|
|
1329
1519
|
};
|
|
1330
1520
|
},
|
|
1521
|
+
onEncodedPacket: () => {
|
|
1522
|
+
this.#framesOut += 1;
|
|
1523
|
+
this.#progressAt = performance.now();
|
|
1524
|
+
},
|
|
1331
1525
|
});
|
|
1332
|
-
const frameRate = options.frameRate;
|
|
1333
|
-
this.#frameDuration =
|
|
1334
|
-
frameRate !== undefined && frameRate.num > 0 && frameRate.den > 0
|
|
1335
|
-
? frameRate.den / frameRate.num
|
|
1336
|
-
: undefined;
|
|
1337
1526
|
this.#output.addVideoTrack(this.#video, this.#frameDuration === undefined
|
|
1338
1527
|
? undefined
|
|
1339
1528
|
: { frameRate: 1 / this.#frameDuration });
|
|
1340
1529
|
}
|
|
1341
|
-
if (
|
|
1342
|
-
if (options.audioBitrate === undefined)
|
|
1343
|
-
throw new RangeError("audioBitrate is required with audioCodec");
|
|
1344
|
-
const rate = options.audioSampleRate;
|
|
1345
|
-
if (rate !== undefined && (!Number.isSafeInteger(rate) || rate <= 0))
|
|
1346
|
-
throw new RangeError("audioSampleRate must be a positive integer");
|
|
1347
|
-
this.#audioRate = rate;
|
|
1348
|
-
this.#audioResampler = options.audioResampler;
|
|
1349
|
-
this.#audio = new AudioSampleSource({
|
|
1350
|
-
codec: options.audioCodec,
|
|
1351
|
-
quality: new Quality({ bitrate: options.audioBitrate }),
|
|
1352
|
-
});
|
|
1530
|
+
if (this.#audio !== undefined)
|
|
1353
1531
|
this.#output.addAudioTrack(this.#audio);
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1532
|
+
await this.#output.start();
|
|
1533
|
+
}
|
|
1534
|
+
/** `work`, unless the video encoder holds frames without output for `stallTimeoutMs`. */
|
|
1535
|
+
#watched(work) {
|
|
1536
|
+
return new Promise((resolve, reject) => {
|
|
1537
|
+
const timer = setInterval(() => {
|
|
1538
|
+
const waited = performance.now() - this.#progressAt;
|
|
1539
|
+
if (this.#framesOut >= this.#framesIn ||
|
|
1540
|
+
waited < this.#stallTimeoutMs)
|
|
1541
|
+
return;
|
|
1542
|
+
clearInterval(timer);
|
|
1543
|
+
const held = this.#framesIn - this.#framesOut;
|
|
1544
|
+
reject(Object.assign(new Error(`video encoder stalled: no output for ${Math.round(waited)} ms with ${held} frames queued`), {
|
|
1545
|
+
code: "ENCODE_STALLED",
|
|
1546
|
+
failure: {
|
|
1547
|
+
code: "GPU_TIMEOUT",
|
|
1548
|
+
phase: "encode",
|
|
1549
|
+
stage: "encoder-stalled",
|
|
1550
|
+
resource: "unknown",
|
|
1551
|
+
},
|
|
1552
|
+
}));
|
|
1553
|
+
}, Math.min(1_000, this.#stallTimeoutMs));
|
|
1554
|
+
void work.finally(() => clearInterval(timer)).then(resolve, reject);
|
|
1555
|
+
});
|
|
1556
|
+
}
|
|
1557
|
+
/** The latency mode the video encoder was opened with; undefined until the sink has started. */
|
|
1558
|
+
get videoLatencyMode() {
|
|
1559
|
+
return this.#latencyMode;
|
|
1358
1560
|
}
|
|
1359
1561
|
/**
|
|
1360
1562
|
* The video track as a sink of its own, for a runner that shares the file with another: its
|
|
@@ -1362,7 +1564,7 @@ export class MediabunnyMp4Sink {
|
|
|
1362
1564
|
* its `abort` discards the file. Throws `RangeError` when no video track is configured.
|
|
1363
1565
|
*/
|
|
1364
1566
|
get videoTrack() {
|
|
1365
|
-
if (this.#
|
|
1567
|
+
if (!this.#hasVideo)
|
|
1366
1568
|
throw new RangeError("video track is not configured");
|
|
1367
1569
|
this.#videoTrack ??= {
|
|
1368
1570
|
writeVideo: (output) => this.writeVideo(output),
|
|
@@ -1398,7 +1600,7 @@ export class MediabunnyMp4Sink {
|
|
|
1398
1600
|
await this.#flushAudio();
|
|
1399
1601
|
(track === "video" ? this.#video : this.#audio)?.close();
|
|
1400
1602
|
}
|
|
1401
|
-
if ((this.#
|
|
1603
|
+
if ((!this.#hasVideo || this.#ended.has("video")) &&
|
|
1402
1604
|
(this.#audio === undefined || this.#ended.has("audio")))
|
|
1403
1605
|
await this.finish();
|
|
1404
1606
|
}
|
|
@@ -1413,25 +1615,37 @@ export class MediabunnyMp4Sink {
|
|
|
1413
1615
|
/**
|
|
1414
1616
|
* Encodes one frame at its output time (`output.timeTicks`, counted from the output's zero).
|
|
1415
1617
|
* Closes `output.frame` whether or not it succeeds. Rejects with `RESOURCE_LIMIT` once a memory
|
|
1416
|
-
* destination has passed `maxBytes`,
|
|
1618
|
+
* destination has passed `maxBytes`, with `ENCODE_STALLED` when the encoder stalls, or when no
|
|
1619
|
+
* video track is configured.
|
|
1417
1620
|
*/
|
|
1418
1621
|
async writeVideo(output) {
|
|
1419
|
-
|
|
1622
|
+
try {
|
|
1623
|
+
await this.#ready;
|
|
1624
|
+
}
|
|
1625
|
+
catch (error) {
|
|
1626
|
+
output.frame.close();
|
|
1627
|
+
throw error;
|
|
1628
|
+
}
|
|
1420
1629
|
if (this.#overflow) {
|
|
1421
1630
|
output.frame.close();
|
|
1422
1631
|
throw this.#overflow;
|
|
1423
1632
|
}
|
|
1424
1633
|
if (this.#video === undefined) {
|
|
1425
1634
|
output.frame.close();
|
|
1426
|
-
throw new Error(
|
|
1635
|
+
throw new Error(this.#hasVideo
|
|
1636
|
+
? "the output was cancelled before its video track started"
|
|
1637
|
+
: "video track is not configured");
|
|
1427
1638
|
}
|
|
1428
1639
|
// Declaring the duration keeps sample durations uniform instead of inferred from float deltas.
|
|
1429
1640
|
const sample = new VideoSample(output.frame, {
|
|
1430
1641
|
timestamp: Number(output.timeTicks) / 120_000,
|
|
1431
1642
|
duration: this.#frameDuration,
|
|
1432
1643
|
});
|
|
1644
|
+
if (this.#framesOut >= this.#framesIn)
|
|
1645
|
+
this.#progressAt = performance.now();
|
|
1646
|
+
this.#framesIn += 1;
|
|
1433
1647
|
try {
|
|
1434
|
-
await this.#video.add(sample);
|
|
1648
|
+
await this.#watched(this.#video.add(sample));
|
|
1435
1649
|
}
|
|
1436
1650
|
finally {
|
|
1437
1651
|
sample.close();
|
|
@@ -1510,8 +1724,9 @@ export class MediabunnyMp4Sink {
|
|
|
1510
1724
|
}
|
|
1511
1725
|
/**
|
|
1512
1726
|
* Finalizes the file and commits a stream destination; calling it again after it resolved does
|
|
1513
|
-
* nothing. Rejects with `RESOURCE_LIMIT` when a memory destination passed `maxBytes`,
|
|
1514
|
-
* what the muxer or the destination refused; a
|
|
1727
|
+
* nothing. Rejects with `RESOURCE_LIMIT` when a memory destination passed `maxBytes`, with
|
|
1728
|
+
* `ENCODE_STALLED` when the encoder stalls, and with what the muxer or the destination refused; a
|
|
1729
|
+
* stream destination is then discarded.
|
|
1515
1730
|
*/
|
|
1516
1731
|
async finish() {
|
|
1517
1732
|
if (this.#finished)
|
|
@@ -1519,7 +1734,7 @@ export class MediabunnyMp4Sink {
|
|
|
1519
1734
|
await this.#ready;
|
|
1520
1735
|
try {
|
|
1521
1736
|
await this.#flushAudio();
|
|
1522
|
-
await this.#output.finalize();
|
|
1737
|
+
await this.#watched(this.#output.finalize());
|
|
1523
1738
|
if (this.#overflow)
|
|
1524
1739
|
throw this.#overflow;
|
|
1525
1740
|
await this.#file?.close();
|
package/dist/player.d.ts
CHANGED
|
@@ -48,6 +48,11 @@ export interface PresentedFrame {
|
|
|
48
48
|
readonly missingFrames: readonly MissingFrame[];
|
|
49
49
|
/** Whether the previous picture was shown again instead of a new one. */
|
|
50
50
|
readonly repeated: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Whether the frame equals the one on screen, so nothing was rendered or presented: the canvas
|
|
53
|
+
* still shows it.
|
|
54
|
+
*/
|
|
55
|
+
readonly unchanged: boolean;
|
|
51
56
|
}
|
|
52
57
|
/** The worker-side renderer an {@link Alpha2Player} drives. */
|
|
53
58
|
export interface RenderBackend {
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FrameUploadReceipt } from "./effects.js";
|
|
2
|
+
/** The engine calls {@link uploadPresentedFrame} makes. */
|
|
3
|
+
export interface PresentedFrameEngine {
|
|
4
|
+
/** `"bitmap"` where the engine copies frames through a canvas after WebGPU refused one. */
|
|
5
|
+
readonly videoFrameCopy?: string;
|
|
6
|
+
uploadPresentedVideoFrame(sourceId: string, mediaTimeTicks: bigint, frame: VideoFrame, width: number, height: number, rotation: number, mirror: boolean): FrameUploadReceipt;
|
|
7
|
+
/** `uploadPresentedVideoFrame` for a bitmap made with `premultiplyAlpha: "none"`. */
|
|
8
|
+
uploadPresentedImageBitmap?(sourceId: string, mediaTimeTicks: bigint, bitmap: ImageBitmap, width: number, height: number, rotation: number, mirror: boolean): FrameUploadReceipt;
|
|
9
|
+
}
|
|
10
|
+
/** Whether {@link uploadPresentedFrame} sends `engine` bitmaps instead of frames. */
|
|
11
|
+
export declare function sendsBitmaps(engine: PresentedFrameEngine): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Uploads `frame`, as a bitmap where {@link sendsBitmaps}, and closes it either way. A frame that
|
|
14
|
+
* cannot be made into a bitmap is sent as it is.
|
|
15
|
+
*/
|
|
16
|
+
export declare function uploadPresentedFrame(engine: PresentedFrameEngine, sourceId: string, mediaTimeTicks: bigint, frame: VideoFrame, width: number, height: number, rotation: number, mirror: boolean): Promise<FrameUploadReceipt>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Presented VideoFrame uploads. Where WebGPU refuses a VideoFrame (Firefox), the engine draws each
|
|
2
|
+
// frame on a 2D canvas on the calling thread; a bitmap from `createImageBitmap` skips that draw.
|
|
3
|
+
/** Whether {@link uploadPresentedFrame} sends `engine` bitmaps instead of frames. */
|
|
4
|
+
export function sendsBitmaps(engine) {
|
|
5
|
+
return (engine.videoFrameCopy === "bitmap" &&
|
|
6
|
+
typeof engine.uploadPresentedImageBitmap === "function" &&
|
|
7
|
+
typeof createImageBitmap === "function");
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Uploads `frame`, as a bitmap where {@link sendsBitmaps}, and closes it either way. A frame that
|
|
11
|
+
* cannot be made into a bitmap is sent as it is.
|
|
12
|
+
*/
|
|
13
|
+
export async function uploadPresentedFrame(engine, sourceId, mediaTimeTicks, frame, width, height, rotation, mirror) {
|
|
14
|
+
const send = () => engine.uploadPresentedVideoFrame(sourceId, mediaTimeTicks, frame, width, height, rotation, mirror);
|
|
15
|
+
if (!sendsBitmaps(engine) || engine.uploadPresentedImageBitmap === undefined)
|
|
16
|
+
return send();
|
|
17
|
+
let bitmap;
|
|
18
|
+
try {
|
|
19
|
+
// The display size, as the engine's canvas draws it.
|
|
20
|
+
bitmap = await createImageBitmap(frame, {
|
|
21
|
+
premultiplyAlpha: "none",
|
|
22
|
+
resizeWidth: frame.displayWidth,
|
|
23
|
+
resizeHeight: frame.displayHeight,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return send();
|
|
28
|
+
}
|
|
29
|
+
frame.close();
|
|
30
|
+
return engine.uploadPresentedImageBitmap(sourceId, mediaTimeTicks, bitmap, width, height, rotation, mirror);
|
|
31
|
+
}
|
package/dist/project.d.ts
CHANGED
|
@@ -150,6 +150,15 @@ export type LintOwner = {
|
|
|
150
150
|
readonly track: string;
|
|
151
151
|
readonly index: number;
|
|
152
152
|
};
|
|
153
|
+
/** The shapes generator a shape {@link LintIssue} is about. */
|
|
154
|
+
export type ShapeLintOwner = {
|
|
155
|
+
readonly type: "source";
|
|
156
|
+
readonly source: string;
|
|
157
|
+
} | {
|
|
158
|
+
readonly type: "node";
|
|
159
|
+
readonly composition: string;
|
|
160
|
+
readonly node: string;
|
|
161
|
+
};
|
|
153
162
|
/**
|
|
154
163
|
* Something in a valid project that is probably not meant. Every check is conservative: a reported
|
|
155
164
|
* issue is certain.
|
|
@@ -198,6 +207,20 @@ export type LintIssue =
|
|
|
198
207
|
| {
|
|
199
208
|
readonly kind: "hdr_source";
|
|
200
209
|
readonly source: string;
|
|
210
|
+
}
|
|
211
|
+
/** A path's subpath without vertices, which draws nothing. */
|
|
212
|
+
| {
|
|
213
|
+
readonly kind: "empty_subpath";
|
|
214
|
+
readonly owner: ShapeLintOwner;
|
|
215
|
+
/** Such as `items[0].path.subpaths[1]`. */
|
|
216
|
+
readonly at: string;
|
|
217
|
+
}
|
|
218
|
+
/** A fill or stroke with no geometry before it in its group, which paints nothing. */
|
|
219
|
+
| {
|
|
220
|
+
readonly kind: "paint_without_geometry";
|
|
221
|
+
readonly owner: ShapeLintOwner;
|
|
222
|
+
/** Such as `items[1].items[0]`. */
|
|
223
|
+
readonly at: string;
|
|
201
224
|
};
|
|
202
225
|
/** The part of `alpha2/wasm` {@link lintProject} uses. */
|
|
203
226
|
export interface LintBinding {
|
|
@@ -205,8 +228,9 @@ export interface LintBinding {
|
|
|
205
228
|
}
|
|
206
229
|
/**
|
|
207
230
|
* A valid project's non-fatal diagnostics, computed by the engine's own
|
|
208
|
-
* `Project::lint`, in a fixed order: sources, then
|
|
209
|
-
*
|
|
231
|
+
* `Project::lint`, in a fixed order: unused sources, then shapes generators
|
|
232
|
+
* (sources', then composition nodes'), then HDR sources, then each track's
|
|
233
|
+
* clips, transitions, and cues.
|
|
210
234
|
*
|
|
211
235
|
* Diagnostics never block a load or an export; they flag what is probably a
|
|
212
236
|
* mistake in a project the engine accepts.
|