sketchmark 2.1.1 → 2.1.3

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 (47) hide show
  1. package/bin/editor-ui.cjs +368 -123
  2. package/bin/editor-ui.d.ts +11 -0
  3. package/bin/vendor/mp4-muxer.LICENSE.txt +21 -0
  4. package/bin/vendor/mp4-muxer.mjs +1879 -0
  5. package/dist/src/browser-export.d.ts +10 -0
  6. package/dist/src/browser-export.js +220 -0
  7. package/package.json +59 -46
  8. package/dist/src/builders/index.d.ts +0 -64
  9. package/dist/src/builders/index.js +0 -212
  10. package/dist/src/compounds.d.ts +0 -13
  11. package/dist/src/compounds.js +0 -118
  12. package/dist/src/deck.d.ts +0 -4
  13. package/dist/src/deck.js +0 -91
  14. package/dist/src/export/index.d.ts +0 -8
  15. package/dist/src/export/index.js +0 -15
  16. package/dist/src/kernel.d.ts +0 -8
  17. package/dist/src/kernel.js +0 -68
  18. package/dist/src/motion.d.ts +0 -4
  19. package/dist/src/motion.js +0 -262
  20. package/dist/src/patch.d.ts +0 -5
  21. package/dist/src/patch.js +0 -72
  22. package/dist/src/player/index.d.ts +0 -68
  23. package/dist/src/player/index.js +0 -600
  24. package/dist/src/project.d.ts +0 -11
  25. package/dist/src/project.js +0 -107
  26. package/dist/src/render/raw-three.d.ts +0 -7
  27. package/dist/src/render/raw-three.js +0 -17
  28. package/dist/src/render/three-html.d.ts +0 -2
  29. package/dist/src/render/three-html.js +0 -257
  30. package/dist/src/render/three-preview-svg.d.ts +0 -3
  31. package/dist/src/render/three-preview-svg.js +0 -102
  32. package/dist/src/scenes.d.ts +0 -4
  33. package/dist/src/scenes.js +0 -26
  34. package/dist/src/sequences.d.ts +0 -43
  35. package/dist/src/sequences.js +0 -109
  36. package/dist/src/shapes/builtins.d.ts +0 -2
  37. package/dist/src/shapes/builtins.js +0 -393
  38. package/dist/src/shapes/common.d.ts +0 -9
  39. package/dist/src/shapes/common.js +0 -76
  40. package/dist/src/shapes/geometry.d.ts +0 -22
  41. package/dist/src/shapes/geometry.js +0 -166
  42. package/dist/src/shapes/index.d.ts +0 -2
  43. package/dist/src/shapes/index.js +0 -18
  44. package/dist/src/shapes/registry.d.ts +0 -8
  45. package/dist/src/shapes/registry.js +0 -31
  46. package/dist/src/shapes/types.d.ts +0 -32
  47. package/dist/src/shapes/types.js +0 -2
@@ -0,0 +1,1879 @@
1
+ var __accessCheck = (obj, member, msg) => {
2
+ if (!member.has(obj))
3
+ throw TypeError("Cannot " + msg);
4
+ };
5
+ var __privateGet = (obj, member, getter) => {
6
+ __accessCheck(obj, member, "read from private field");
7
+ return getter ? getter.call(obj) : member.get(obj);
8
+ };
9
+ var __privateAdd = (obj, member, value) => {
10
+ if (member.has(obj))
11
+ throw TypeError("Cannot add the same private member more than once");
12
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
13
+ };
14
+ var __privateSet = (obj, member, value, setter) => {
15
+ __accessCheck(obj, member, "write to private field");
16
+ setter ? setter.call(obj, value) : member.set(obj, value);
17
+ return value;
18
+ };
19
+ var __privateWrapper = (obj, member, setter, getter) => ({
20
+ set _(value) {
21
+ __privateSet(obj, member, value, setter);
22
+ },
23
+ get _() {
24
+ return __privateGet(obj, member, getter);
25
+ }
26
+ });
27
+ var __privateMethod = (obj, member, method) => {
28
+ __accessCheck(obj, member, "access private method");
29
+ return method;
30
+ };
31
+
32
+ // src/misc.ts
33
+ var bytes = new Uint8Array(8);
34
+ var view = new DataView(bytes.buffer);
35
+ var u8 = (value) => {
36
+ return [(value % 256 + 256) % 256];
37
+ };
38
+ var u16 = (value) => {
39
+ view.setUint16(0, value, false);
40
+ return [bytes[0], bytes[1]];
41
+ };
42
+ var i16 = (value) => {
43
+ view.setInt16(0, value, false);
44
+ return [bytes[0], bytes[1]];
45
+ };
46
+ var u24 = (value) => {
47
+ view.setUint32(0, value, false);
48
+ return [bytes[1], bytes[2], bytes[3]];
49
+ };
50
+ var u32 = (value) => {
51
+ view.setUint32(0, value, false);
52
+ return [bytes[0], bytes[1], bytes[2], bytes[3]];
53
+ };
54
+ var i32 = (value) => {
55
+ view.setInt32(0, value, false);
56
+ return [bytes[0], bytes[1], bytes[2], bytes[3]];
57
+ };
58
+ var u64 = (value) => {
59
+ view.setUint32(0, Math.floor(value / 2 ** 32), false);
60
+ view.setUint32(4, value, false);
61
+ return [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]];
62
+ };
63
+ var fixed_8_8 = (value) => {
64
+ view.setInt16(0, 2 ** 8 * value, false);
65
+ return [bytes[0], bytes[1]];
66
+ };
67
+ var fixed_16_16 = (value) => {
68
+ view.setInt32(0, 2 ** 16 * value, false);
69
+ return [bytes[0], bytes[1], bytes[2], bytes[3]];
70
+ };
71
+ var fixed_2_30 = (value) => {
72
+ view.setInt32(0, 2 ** 30 * value, false);
73
+ return [bytes[0], bytes[1], bytes[2], bytes[3]];
74
+ };
75
+ var ascii = (text, nullTerminated = false) => {
76
+ let bytes2 = Array(text.length).fill(null).map((_, i) => text.charCodeAt(i));
77
+ if (nullTerminated)
78
+ bytes2.push(0);
79
+ return bytes2;
80
+ };
81
+ var last = (arr) => {
82
+ return arr && arr[arr.length - 1];
83
+ };
84
+ var lastPresentedSample = (samples) => {
85
+ let result = void 0;
86
+ for (let sample of samples) {
87
+ if (!result || sample.presentationTimestamp > result.presentationTimestamp) {
88
+ result = sample;
89
+ }
90
+ }
91
+ return result;
92
+ };
93
+ var intoTimescale = (timeInSeconds, timescale, round = true) => {
94
+ let value = timeInSeconds * timescale;
95
+ return round ? Math.round(value) : value;
96
+ };
97
+ var rotationMatrix = (rotationInDegrees) => {
98
+ let theta = rotationInDegrees * (Math.PI / 180);
99
+ let cosTheta = Math.cos(theta);
100
+ let sinTheta = Math.sin(theta);
101
+ return [
102
+ cosTheta,
103
+ sinTheta,
104
+ 0,
105
+ -sinTheta,
106
+ cosTheta,
107
+ 0,
108
+ 0,
109
+ 0,
110
+ 1
111
+ ];
112
+ };
113
+ var IDENTITY_MATRIX = rotationMatrix(0);
114
+ var matrixToBytes = (matrix) => {
115
+ return [
116
+ fixed_16_16(matrix[0]),
117
+ fixed_16_16(matrix[1]),
118
+ fixed_2_30(matrix[2]),
119
+ fixed_16_16(matrix[3]),
120
+ fixed_16_16(matrix[4]),
121
+ fixed_2_30(matrix[5]),
122
+ fixed_16_16(matrix[6]),
123
+ fixed_16_16(matrix[7]),
124
+ fixed_2_30(matrix[8])
125
+ ];
126
+ };
127
+ var deepClone = (x) => {
128
+ if (!x)
129
+ return x;
130
+ if (typeof x !== "object")
131
+ return x;
132
+ if (Array.isArray(x))
133
+ return x.map(deepClone);
134
+ return Object.fromEntries(Object.entries(x).map(([key, value]) => [key, deepClone(value)]));
135
+ };
136
+ var isU32 = (value) => {
137
+ return value >= 0 && value < 2 ** 32;
138
+ };
139
+
140
+ // src/box.ts
141
+ var box = (type, contents, children) => ({
142
+ type,
143
+ contents: contents && new Uint8Array(contents.flat(10)),
144
+ children
145
+ });
146
+ var fullBox = (type, version, flags, contents, children) => box(
147
+ type,
148
+ [u8(version), u24(flags), contents ?? []],
149
+ children
150
+ );
151
+ var ftyp = (details) => {
152
+ let minorVersion = 512;
153
+ if (details.fragmented)
154
+ return box("ftyp", [
155
+ ascii("iso5"),
156
+ // Major brand
157
+ u32(minorVersion),
158
+ // Minor version
159
+ // Compatible brands
160
+ ascii("iso5"),
161
+ ascii("iso6"),
162
+ ascii("mp41")
163
+ ]);
164
+ return box("ftyp", [
165
+ ascii("isom"),
166
+ // Major brand
167
+ u32(minorVersion),
168
+ // Minor version
169
+ // Compatible brands
170
+ ascii("isom"),
171
+ details.holdsAvc ? ascii("avc1") : [],
172
+ ascii("mp41")
173
+ ]);
174
+ };
175
+ var mdat = (reserveLargeSize) => ({ type: "mdat", largeSize: reserveLargeSize });
176
+ var free = (size) => ({ type: "free", size });
177
+ var moov = (tracks, creationTime, fragmented = false) => box("moov", null, [
178
+ mvhd(creationTime, tracks),
179
+ ...tracks.map((x) => trak(x, creationTime)),
180
+ fragmented ? mvex(tracks) : null
181
+ ]);
182
+ var mvhd = (creationTime, tracks) => {
183
+ let duration = intoTimescale(Math.max(
184
+ 0,
185
+ ...tracks.filter((x) => x.samples.length > 0).map((x) => {
186
+ const lastSample = lastPresentedSample(x.samples);
187
+ return lastSample.presentationTimestamp + lastSample.duration;
188
+ })
189
+ ), GLOBAL_TIMESCALE);
190
+ let nextTrackId = Math.max(...tracks.map((x) => x.id)) + 1;
191
+ let needsU64 = !isU32(creationTime) || !isU32(duration);
192
+ let u32OrU64 = needsU64 ? u64 : u32;
193
+ return fullBox("mvhd", +needsU64, 0, [
194
+ u32OrU64(creationTime),
195
+ // Creation time
196
+ u32OrU64(creationTime),
197
+ // Modification time
198
+ u32(GLOBAL_TIMESCALE),
199
+ // Timescale
200
+ u32OrU64(duration),
201
+ // Duration
202
+ fixed_16_16(1),
203
+ // Preferred rate
204
+ fixed_8_8(1),
205
+ // Preferred volume
206
+ Array(10).fill(0),
207
+ // Reserved
208
+ matrixToBytes(IDENTITY_MATRIX),
209
+ // Matrix
210
+ Array(24).fill(0),
211
+ // Pre-defined
212
+ u32(nextTrackId)
213
+ // Next track ID
214
+ ]);
215
+ };
216
+ var trak = (track, creationTime) => box("trak", null, [
217
+ tkhd(track, creationTime),
218
+ mdia(track, creationTime)
219
+ ]);
220
+ var tkhd = (track, creationTime) => {
221
+ let lastSample = lastPresentedSample(track.samples);
222
+ let durationInGlobalTimescale = intoTimescale(
223
+ lastSample ? lastSample.presentationTimestamp + lastSample.duration : 0,
224
+ GLOBAL_TIMESCALE
225
+ );
226
+ let needsU64 = !isU32(creationTime) || !isU32(durationInGlobalTimescale);
227
+ let u32OrU64 = needsU64 ? u64 : u32;
228
+ let matrix;
229
+ if (track.info.type === "video") {
230
+ matrix = typeof track.info.rotation === "number" ? rotationMatrix(track.info.rotation) : track.info.rotation;
231
+ } else {
232
+ matrix = IDENTITY_MATRIX;
233
+ }
234
+ return fullBox("tkhd", +needsU64, 3, [
235
+ u32OrU64(creationTime),
236
+ // Creation time
237
+ u32OrU64(creationTime),
238
+ // Modification time
239
+ u32(track.id),
240
+ // Track ID
241
+ u32(0),
242
+ // Reserved
243
+ u32OrU64(durationInGlobalTimescale),
244
+ // Duration
245
+ Array(8).fill(0),
246
+ // Reserved
247
+ u16(0),
248
+ // Layer
249
+ u16(0),
250
+ // Alternate group
251
+ fixed_8_8(track.info.type === "audio" ? 1 : 0),
252
+ // Volume
253
+ u16(0),
254
+ // Reserved
255
+ matrixToBytes(matrix),
256
+ // Matrix
257
+ fixed_16_16(track.info.type === "video" ? track.info.width : 0),
258
+ // Track width
259
+ fixed_16_16(track.info.type === "video" ? track.info.height : 0)
260
+ // Track height
261
+ ]);
262
+ };
263
+ var mdia = (track, creationTime) => box("mdia", null, [
264
+ mdhd(track, creationTime),
265
+ hdlr(track.info.type === "video" ? "vide" : "soun"),
266
+ minf(track)
267
+ ]);
268
+ var mdhd = (track, creationTime) => {
269
+ let lastSample = lastPresentedSample(track.samples);
270
+ let localDuration = intoTimescale(
271
+ lastSample ? lastSample.presentationTimestamp + lastSample.duration : 0,
272
+ track.timescale
273
+ );
274
+ let needsU64 = !isU32(creationTime) || !isU32(localDuration);
275
+ let u32OrU64 = needsU64 ? u64 : u32;
276
+ return fullBox("mdhd", +needsU64, 0, [
277
+ u32OrU64(creationTime),
278
+ // Creation time
279
+ u32OrU64(creationTime),
280
+ // Modification time
281
+ u32(track.timescale),
282
+ // Timescale
283
+ u32OrU64(localDuration),
284
+ // Duration
285
+ u16(21956),
286
+ // Language ("und", undetermined)
287
+ u16(0)
288
+ // Quality
289
+ ]);
290
+ };
291
+ var hdlr = (componentSubtype) => fullBox("hdlr", 0, 0, [
292
+ ascii("mhlr"),
293
+ // Component type
294
+ ascii(componentSubtype),
295
+ // Component subtype
296
+ u32(0),
297
+ // Component manufacturer
298
+ u32(0),
299
+ // Component flags
300
+ u32(0),
301
+ // Component flags mask
302
+ ascii("mp4-muxer-hdlr", true)
303
+ // Component name
304
+ ]);
305
+ var minf = (track) => box("minf", null, [
306
+ track.info.type === "video" ? vmhd() : smhd(),
307
+ dinf(),
308
+ stbl(track)
309
+ ]);
310
+ var vmhd = () => fullBox("vmhd", 0, 1, [
311
+ u16(0),
312
+ // Graphics mode
313
+ u16(0),
314
+ // Opcolor R
315
+ u16(0),
316
+ // Opcolor G
317
+ u16(0)
318
+ // Opcolor B
319
+ ]);
320
+ var smhd = () => fullBox("smhd", 0, 0, [
321
+ u16(0),
322
+ // Balance
323
+ u16(0)
324
+ // Reserved
325
+ ]);
326
+ var dinf = () => box("dinf", null, [
327
+ dref()
328
+ ]);
329
+ var dref = () => fullBox("dref", 0, 0, [
330
+ u32(1)
331
+ // Entry count
332
+ ], [
333
+ url()
334
+ ]);
335
+ var url = () => fullBox("url ", 0, 1);
336
+ var stbl = (track) => {
337
+ const needsCtts = track.compositionTimeOffsetTable.length > 1 || track.compositionTimeOffsetTable.some((x) => x.sampleCompositionTimeOffset !== 0);
338
+ return box("stbl", null, [
339
+ stsd(track),
340
+ stts(track),
341
+ stss(track),
342
+ stsc(track),
343
+ stsz(track),
344
+ stco(track),
345
+ needsCtts ? ctts(track) : null
346
+ ]);
347
+ };
348
+ var stsd = (track) => fullBox("stsd", 0, 0, [
349
+ u32(1)
350
+ // Entry count
351
+ ], [
352
+ track.info.type === "video" ? videoSampleDescription(
353
+ VIDEO_CODEC_TO_BOX_NAME[track.info.codec],
354
+ track
355
+ ) : soundSampleDescription(
356
+ AUDIO_CODEC_TO_BOX_NAME[track.info.codec],
357
+ track
358
+ )
359
+ ]);
360
+ var videoSampleDescription = (compressionType, track) => box(compressionType, [
361
+ Array(6).fill(0),
362
+ // Reserved
363
+ u16(1),
364
+ // Data reference index
365
+ u16(0),
366
+ // Pre-defined
367
+ u16(0),
368
+ // Reserved
369
+ Array(12).fill(0),
370
+ // Pre-defined
371
+ u16(track.info.width),
372
+ // Width
373
+ u16(track.info.height),
374
+ // Height
375
+ u32(4718592),
376
+ // Horizontal resolution
377
+ u32(4718592),
378
+ // Vertical resolution
379
+ u32(0),
380
+ // Reserved
381
+ u16(1),
382
+ // Frame count
383
+ Array(32).fill(0),
384
+ // Compressor name
385
+ u16(24),
386
+ // Depth
387
+ i16(65535)
388
+ // Pre-defined
389
+ ], [
390
+ VIDEO_CODEC_TO_CONFIGURATION_BOX[track.info.codec](track),
391
+ track.info.decoderConfig.colorSpace ? colr(track) : null
392
+ ]);
393
+ var COLOR_PRIMARIES_MAP = {
394
+ "bt709": 1,
395
+ // ITU-R BT.709
396
+ "bt470bg": 5,
397
+ // ITU-R BT.470BG
398
+ "smpte170m": 6
399
+ // ITU-R BT.601 525 - SMPTE 170M
400
+ };
401
+ var TRANSFER_CHARACTERISTICS_MAP = {
402
+ "bt709": 1,
403
+ // ITU-R BT.709
404
+ "smpte170m": 6,
405
+ // SMPTE 170M
406
+ "iec61966-2-1": 13
407
+ // IEC 61966-2-1
408
+ };
409
+ var MATRIX_COEFFICIENTS_MAP = {
410
+ "rgb": 0,
411
+ // Identity
412
+ "bt709": 1,
413
+ // ITU-R BT.709
414
+ "bt470bg": 5,
415
+ // ITU-R BT.470BG
416
+ "smpte170m": 6
417
+ // SMPTE 170M
418
+ };
419
+ var colr = (track) => box("colr", [
420
+ ascii("nclx"),
421
+ // Colour type
422
+ u16(COLOR_PRIMARIES_MAP[track.info.decoderConfig.colorSpace.primaries]),
423
+ // Colour primaries
424
+ u16(TRANSFER_CHARACTERISTICS_MAP[track.info.decoderConfig.colorSpace.transfer]),
425
+ // Transfer characteristics
426
+ u16(MATRIX_COEFFICIENTS_MAP[track.info.decoderConfig.colorSpace.matrix]),
427
+ // Matrix coefficients
428
+ u8((track.info.decoderConfig.colorSpace.fullRange ? 1 : 0) << 7)
429
+ // Full range flag
430
+ ]);
431
+ var avcC = (track) => track.info.decoderConfig && box("avcC", [
432
+ // For AVC, description is an AVCDecoderConfigurationRecord, so nothing else to do here
433
+ ...new Uint8Array(track.info.decoderConfig.description)
434
+ ]);
435
+ var hvcC = (track) => track.info.decoderConfig && box("hvcC", [
436
+ // For HEVC, description is a HEVCDecoderConfigurationRecord, so nothing else to do here
437
+ ...new Uint8Array(track.info.decoderConfig.description)
438
+ ]);
439
+ var vpcC = (track) => {
440
+ if (!track.info.decoderConfig) {
441
+ return null;
442
+ }
443
+ let decoderConfig = track.info.decoderConfig;
444
+ if (!decoderConfig.colorSpace) {
445
+ throw new Error(`'colorSpace' is required in the decoder config for VP9.`);
446
+ }
447
+ let parts = decoderConfig.codec.split(".");
448
+ let profile = Number(parts[1]);
449
+ let level = Number(parts[2]);
450
+ let bitDepth = Number(parts[3]);
451
+ let chromaSubsampling = 0;
452
+ let thirdByte = (bitDepth << 4) + (chromaSubsampling << 1) + Number(decoderConfig.colorSpace.fullRange);
453
+ let colourPrimaries = 2;
454
+ let transferCharacteristics = 2;
455
+ let matrixCoefficients = 2;
456
+ return fullBox("vpcC", 1, 0, [
457
+ u8(profile),
458
+ // Profile
459
+ u8(level),
460
+ // Level
461
+ u8(thirdByte),
462
+ // Bit depth, chroma subsampling, full range
463
+ u8(colourPrimaries),
464
+ // Colour primaries
465
+ u8(transferCharacteristics),
466
+ // Transfer characteristics
467
+ u8(matrixCoefficients),
468
+ // Matrix coefficients
469
+ u16(0)
470
+ // Codec initialization data size
471
+ ]);
472
+ };
473
+ var av1C = () => {
474
+ let marker = 1;
475
+ let version = 1;
476
+ let firstByte = (marker << 7) + version;
477
+ return box("av1C", [
478
+ firstByte,
479
+ 0,
480
+ 0,
481
+ 0
482
+ ]);
483
+ };
484
+ var soundSampleDescription = (compressionType, track) => box(compressionType, [
485
+ Array(6).fill(0),
486
+ // Reserved
487
+ u16(1),
488
+ // Data reference index
489
+ u16(0),
490
+ // Version
491
+ u16(0),
492
+ // Revision level
493
+ u32(0),
494
+ // Vendor
495
+ u16(track.info.numberOfChannels),
496
+ // Number of channels
497
+ u16(16),
498
+ // Sample size (bits)
499
+ u16(0),
500
+ // Compression ID
501
+ u16(0),
502
+ // Packet size
503
+ fixed_16_16(track.info.sampleRate)
504
+ // Sample rate
505
+ ], [
506
+ AUDIO_CODEC_TO_CONFIGURATION_BOX[track.info.codec](track)
507
+ ]);
508
+ var esds = (track) => {
509
+ let description = new Uint8Array(track.info.decoderConfig.description);
510
+ return fullBox("esds", 0, 0, [
511
+ // https://stackoverflow.com/a/54803118
512
+ u32(58753152),
513
+ // TAG(3) = Object Descriptor ([2])
514
+ u8(32 + description.byteLength),
515
+ // length of this OD (which includes the next 2 tags)
516
+ u16(1),
517
+ // ES_ID = 1
518
+ u8(0),
519
+ // flags etc = 0
520
+ u32(75530368),
521
+ // TAG(4) = ES Descriptor ([2]) embedded in above OD
522
+ u8(18 + description.byteLength),
523
+ // length of this ESD
524
+ u8(64),
525
+ // MPEG-4 Audio
526
+ u8(21),
527
+ // stream type(6bits)=5 audio, flags(2bits)=1
528
+ u24(0),
529
+ // 24bit buffer size
530
+ u32(130071),
531
+ // max bitrate
532
+ u32(130071),
533
+ // avg bitrate
534
+ u32(92307584),
535
+ // TAG(5) = ASC ([2],[3]) embedded in above OD
536
+ u8(description.byteLength),
537
+ // length
538
+ ...description,
539
+ u32(109084800),
540
+ // TAG(6)
541
+ u8(1),
542
+ // length
543
+ u8(2)
544
+ // data
545
+ ]);
546
+ };
547
+ var dOps = (track) => {
548
+ let preskip = 3840;
549
+ let gain = 0;
550
+ const description = track.info.decoderConfig?.description;
551
+ if (description) {
552
+ if (description.byteLength < 18) {
553
+ throw new TypeError("Invalid decoder description provided for Opus; must be at least 18 bytes long.");
554
+ }
555
+ const view2 = ArrayBuffer.isView(description) ? new DataView(description.buffer, description.byteOffset, description.byteLength) : new DataView(description);
556
+ preskip = view2.getUint16(10, true);
557
+ gain = view2.getInt16(14, true);
558
+ }
559
+ return box("dOps", [
560
+ u8(0),
561
+ // Version
562
+ u8(track.info.numberOfChannels),
563
+ // OutputChannelCount
564
+ u16(preskip),
565
+ u32(track.info.sampleRate),
566
+ // InputSampleRate
567
+ fixed_8_8(gain),
568
+ // OutputGain
569
+ u8(0)
570
+ // ChannelMappingFamily
571
+ ]);
572
+ };
573
+ var stts = (track) => {
574
+ return fullBox("stts", 0, 0, [
575
+ u32(track.timeToSampleTable.length),
576
+ // Number of entries
577
+ track.timeToSampleTable.map((x) => [
578
+ // Time-to-sample table
579
+ u32(x.sampleCount),
580
+ // Sample count
581
+ u32(x.sampleDelta)
582
+ // Sample duration
583
+ ])
584
+ ]);
585
+ };
586
+ var stss = (track) => {
587
+ if (track.samples.every((x) => x.type === "key"))
588
+ return null;
589
+ let keySamples = [...track.samples.entries()].filter(([, sample]) => sample.type === "key");
590
+ return fullBox("stss", 0, 0, [
591
+ u32(keySamples.length),
592
+ // Number of entries
593
+ keySamples.map(([index]) => u32(index + 1))
594
+ // Sync sample table
595
+ ]);
596
+ };
597
+ var stsc = (track) => {
598
+ return fullBox("stsc", 0, 0, [
599
+ u32(track.compactlyCodedChunkTable.length),
600
+ // Number of entries
601
+ track.compactlyCodedChunkTable.map((x) => [
602
+ // Sample-to-chunk table
603
+ u32(x.firstChunk),
604
+ // First chunk
605
+ u32(x.samplesPerChunk),
606
+ // Samples per chunk
607
+ u32(1)
608
+ // Sample description index
609
+ ])
610
+ ]);
611
+ };
612
+ var stsz = (track) => fullBox("stsz", 0, 0, [
613
+ u32(0),
614
+ // Sample size (0 means non-constant size)
615
+ u32(track.samples.length),
616
+ // Number of entries
617
+ track.samples.map((x) => u32(x.size))
618
+ // Sample size table
619
+ ]);
620
+ var stco = (track) => {
621
+ if (track.finalizedChunks.length > 0 && last(track.finalizedChunks).offset >= 2 ** 32) {
622
+ return fullBox("co64", 0, 0, [
623
+ u32(track.finalizedChunks.length),
624
+ // Number of entries
625
+ track.finalizedChunks.map((x) => u64(x.offset))
626
+ // Chunk offset table
627
+ ]);
628
+ }
629
+ return fullBox("stco", 0, 0, [
630
+ u32(track.finalizedChunks.length),
631
+ // Number of entries
632
+ track.finalizedChunks.map((x) => u32(x.offset))
633
+ // Chunk offset table
634
+ ]);
635
+ };
636
+ var ctts = (track) => {
637
+ return fullBox("ctts", 0, 0, [
638
+ u32(track.compositionTimeOffsetTable.length),
639
+ // Number of entries
640
+ track.compositionTimeOffsetTable.map((x) => [
641
+ // Time-to-sample table
642
+ u32(x.sampleCount),
643
+ // Sample count
644
+ u32(x.sampleCompositionTimeOffset)
645
+ // Sample offset
646
+ ])
647
+ ]);
648
+ };
649
+ var mvex = (tracks) => {
650
+ return box("mvex", null, tracks.map(trex));
651
+ };
652
+ var trex = (track) => {
653
+ return fullBox("trex", 0, 0, [
654
+ u32(track.id),
655
+ // Track ID
656
+ u32(1),
657
+ // Default sample description index
658
+ u32(0),
659
+ // Default sample duration
660
+ u32(0),
661
+ // Default sample size
662
+ u32(0)
663
+ // Default sample flags
664
+ ]);
665
+ };
666
+ var moof = (sequenceNumber, tracks) => {
667
+ return box("moof", null, [
668
+ mfhd(sequenceNumber),
669
+ ...tracks.map(traf)
670
+ ]);
671
+ };
672
+ var mfhd = (sequenceNumber) => {
673
+ return fullBox("mfhd", 0, 0, [
674
+ u32(sequenceNumber)
675
+ // Sequence number
676
+ ]);
677
+ };
678
+ var fragmentSampleFlags = (sample) => {
679
+ let byte1 = 0;
680
+ let byte2 = 0;
681
+ let byte3 = 0;
682
+ let byte4 = 0;
683
+ let sampleIsDifferenceSample = sample.type === "delta";
684
+ byte2 |= +sampleIsDifferenceSample;
685
+ if (sampleIsDifferenceSample) {
686
+ byte1 |= 1;
687
+ } else {
688
+ byte1 |= 2;
689
+ }
690
+ return byte1 << 24 | byte2 << 16 | byte3 << 8 | byte4;
691
+ };
692
+ var traf = (track) => {
693
+ return box("traf", null, [
694
+ tfhd(track),
695
+ tfdt(track),
696
+ trun(track)
697
+ ]);
698
+ };
699
+ var tfhd = (track) => {
700
+ let tfFlags = 0;
701
+ tfFlags |= 8;
702
+ tfFlags |= 16;
703
+ tfFlags |= 32;
704
+ tfFlags |= 131072;
705
+ let referenceSample = track.currentChunk.samples[1] ?? track.currentChunk.samples[0];
706
+ let referenceSampleInfo = {
707
+ duration: referenceSample.timescaleUnitsToNextSample,
708
+ size: referenceSample.size,
709
+ flags: fragmentSampleFlags(referenceSample)
710
+ };
711
+ return fullBox("tfhd", 0, tfFlags, [
712
+ u32(track.id),
713
+ // Track ID
714
+ u32(referenceSampleInfo.duration),
715
+ // Default sample duration
716
+ u32(referenceSampleInfo.size),
717
+ // Default sample size
718
+ u32(referenceSampleInfo.flags)
719
+ // Default sample flags
720
+ ]);
721
+ };
722
+ var tfdt = (track) => {
723
+ return fullBox("tfdt", 1, 0, [
724
+ u64(intoTimescale(track.currentChunk.startTimestamp, track.timescale))
725
+ // Base Media Decode Time
726
+ ]);
727
+ };
728
+ var trun = (track) => {
729
+ let allSampleDurations = track.currentChunk.samples.map((x) => x.timescaleUnitsToNextSample);
730
+ let allSampleSizes = track.currentChunk.samples.map((x) => x.size);
731
+ let allSampleFlags = track.currentChunk.samples.map(fragmentSampleFlags);
732
+ let allSampleCompositionTimeOffsets = track.currentChunk.samples.map((x) => intoTimescale(x.presentationTimestamp - x.decodeTimestamp, track.timescale));
733
+ let uniqueSampleDurations = new Set(allSampleDurations);
734
+ let uniqueSampleSizes = new Set(allSampleSizes);
735
+ let uniqueSampleFlags = new Set(allSampleFlags);
736
+ let uniqueSampleCompositionTimeOffsets = new Set(allSampleCompositionTimeOffsets);
737
+ let firstSampleFlagsPresent = uniqueSampleFlags.size === 2 && allSampleFlags[0] !== allSampleFlags[1];
738
+ let sampleDurationPresent = uniqueSampleDurations.size > 1;
739
+ let sampleSizePresent = uniqueSampleSizes.size > 1;
740
+ let sampleFlagsPresent = !firstSampleFlagsPresent && uniqueSampleFlags.size > 1;
741
+ let sampleCompositionTimeOffsetsPresent = uniqueSampleCompositionTimeOffsets.size > 1 || [...uniqueSampleCompositionTimeOffsets].some((x) => x !== 0);
742
+ let flags = 0;
743
+ flags |= 1;
744
+ flags |= 4 * +firstSampleFlagsPresent;
745
+ flags |= 256 * +sampleDurationPresent;
746
+ flags |= 512 * +sampleSizePresent;
747
+ flags |= 1024 * +sampleFlagsPresent;
748
+ flags |= 2048 * +sampleCompositionTimeOffsetsPresent;
749
+ return fullBox("trun", 1, flags, [
750
+ u32(track.currentChunk.samples.length),
751
+ // Sample count
752
+ u32(track.currentChunk.offset - track.currentChunk.moofOffset || 0),
753
+ // Data offset
754
+ firstSampleFlagsPresent ? u32(allSampleFlags[0]) : [],
755
+ track.currentChunk.samples.map((_, i) => [
756
+ sampleDurationPresent ? u32(allSampleDurations[i]) : [],
757
+ // Sample duration
758
+ sampleSizePresent ? u32(allSampleSizes[i]) : [],
759
+ // Sample size
760
+ sampleFlagsPresent ? u32(allSampleFlags[i]) : [],
761
+ // Sample flags
762
+ // Sample composition time offsets
763
+ sampleCompositionTimeOffsetsPresent ? i32(allSampleCompositionTimeOffsets[i]) : []
764
+ ])
765
+ ]);
766
+ };
767
+ var mfra = (tracks) => {
768
+ return box("mfra", null, [
769
+ ...tracks.map(tfra),
770
+ mfro()
771
+ ]);
772
+ };
773
+ var tfra = (track, trackIndex) => {
774
+ let version = 1;
775
+ return fullBox("tfra", version, 0, [
776
+ u32(track.id),
777
+ // Track ID
778
+ u32(63),
779
+ // This specifies that traf number, trun number and sample number are 32-bit ints
780
+ u32(track.finalizedChunks.length),
781
+ // Number of entries
782
+ track.finalizedChunks.map((chunk) => [
783
+ u64(intoTimescale(chunk.startTimestamp, track.timescale)),
784
+ // Time
785
+ u64(chunk.moofOffset),
786
+ // moof offset
787
+ u32(trackIndex + 1),
788
+ // traf number
789
+ u32(1),
790
+ // trun number
791
+ u32(1)
792
+ // Sample number
793
+ ])
794
+ ]);
795
+ };
796
+ var mfro = () => {
797
+ return fullBox("mfro", 0, 0, [
798
+ // This value needs to be overwritten manually from the outside, where the actual size of the enclosing mfra box
799
+ // is known
800
+ u32(0)
801
+ // Size
802
+ ]);
803
+ };
804
+ var VIDEO_CODEC_TO_BOX_NAME = {
805
+ "avc": "avc1",
806
+ "hevc": "hvc1",
807
+ "vp9": "vp09",
808
+ "av1": "av01"
809
+ };
810
+ var VIDEO_CODEC_TO_CONFIGURATION_BOX = {
811
+ "avc": avcC,
812
+ "hevc": hvcC,
813
+ "vp9": vpcC,
814
+ "av1": av1C
815
+ };
816
+ var AUDIO_CODEC_TO_BOX_NAME = {
817
+ "aac": "mp4a",
818
+ "opus": "Opus"
819
+ };
820
+ var AUDIO_CODEC_TO_CONFIGURATION_BOX = {
821
+ "aac": esds,
822
+ "opus": dOps
823
+ };
824
+
825
+ // src/target.ts
826
+ var isTarget = Symbol("isTarget");
827
+ var Target = class {
828
+ };
829
+ isTarget;
830
+ var ArrayBufferTarget = class extends Target {
831
+ constructor() {
832
+ super(...arguments);
833
+ this.buffer = null;
834
+ }
835
+ };
836
+ var StreamTarget = class extends Target {
837
+ constructor(options) {
838
+ super();
839
+ this.options = options;
840
+ if (typeof options !== "object") {
841
+ throw new TypeError("StreamTarget requires an options object to be passed to its constructor.");
842
+ }
843
+ if (options.onData) {
844
+ if (typeof options.onData !== "function") {
845
+ throw new TypeError("options.onData, when provided, must be a function.");
846
+ }
847
+ if (options.onData.length < 2) {
848
+ throw new TypeError(
849
+ "options.onData, when provided, must be a function that takes in at least two arguments (data and position). Ignoring the position argument, which specifies the byte offset at which the data is to be written, can lead to broken outputs."
850
+ );
851
+ }
852
+ }
853
+ if (options.chunked !== void 0 && typeof options.chunked !== "boolean") {
854
+ throw new TypeError("options.chunked, when provided, must be a boolean.");
855
+ }
856
+ if (options.chunkSize !== void 0 && (!Number.isInteger(options.chunkSize) || options.chunkSize < 1024)) {
857
+ throw new TypeError("options.chunkSize, when provided, must be an integer and not smaller than 1024.");
858
+ }
859
+ }
860
+ };
861
+ var FileSystemWritableFileStreamTarget = class extends Target {
862
+ constructor(stream, options) {
863
+ super();
864
+ this.stream = stream;
865
+ this.options = options;
866
+ if (!(stream instanceof FileSystemWritableFileStream)) {
867
+ throw new TypeError("FileSystemWritableFileStreamTarget requires a FileSystemWritableFileStream instance.");
868
+ }
869
+ if (options !== void 0 && typeof options !== "object") {
870
+ throw new TypeError("FileSystemWritableFileStreamTarget's options, when provided, must be an object.");
871
+ }
872
+ if (options) {
873
+ if (options.chunkSize !== void 0 && (!Number.isInteger(options.chunkSize) || options.chunkSize <= 0)) {
874
+ throw new TypeError("options.chunkSize, when provided, must be a positive integer");
875
+ }
876
+ }
877
+ }
878
+ };
879
+
880
+ // src/writer.ts
881
+ var _helper, _helperView;
882
+ var Writer = class {
883
+ constructor() {
884
+ this.pos = 0;
885
+ __privateAdd(this, _helper, new Uint8Array(8));
886
+ __privateAdd(this, _helperView, new DataView(__privateGet(this, _helper).buffer));
887
+ /**
888
+ * Stores the position from the start of the file to where boxes elements have been written. This is used to
889
+ * rewrite/edit elements that were already added before, and to measure sizes of things.
890
+ */
891
+ this.offsets = /* @__PURE__ */ new WeakMap();
892
+ }
893
+ /** Sets the current position for future writes to a new one. */
894
+ seek(newPos) {
895
+ this.pos = newPos;
896
+ }
897
+ writeU32(value) {
898
+ __privateGet(this, _helperView).setUint32(0, value, false);
899
+ this.write(__privateGet(this, _helper).subarray(0, 4));
900
+ }
901
+ writeU64(value) {
902
+ __privateGet(this, _helperView).setUint32(0, Math.floor(value / 2 ** 32), false);
903
+ __privateGet(this, _helperView).setUint32(4, value, false);
904
+ this.write(__privateGet(this, _helper).subarray(0, 8));
905
+ }
906
+ writeAscii(text) {
907
+ for (let i = 0; i < text.length; i++) {
908
+ __privateGet(this, _helperView).setUint8(i % 8, text.charCodeAt(i));
909
+ if (i % 8 === 7)
910
+ this.write(__privateGet(this, _helper));
911
+ }
912
+ if (text.length % 8 !== 0) {
913
+ this.write(__privateGet(this, _helper).subarray(0, text.length % 8));
914
+ }
915
+ }
916
+ writeBox(box2) {
917
+ this.offsets.set(box2, this.pos);
918
+ if (box2.contents && !box2.children) {
919
+ this.writeBoxHeader(box2, box2.size ?? box2.contents.byteLength + 8);
920
+ this.write(box2.contents);
921
+ } else {
922
+ let startPos = this.pos;
923
+ this.writeBoxHeader(box2, 0);
924
+ if (box2.contents)
925
+ this.write(box2.contents);
926
+ if (box2.children) {
927
+ for (let child of box2.children)
928
+ if (child)
929
+ this.writeBox(child);
930
+ }
931
+ let endPos = this.pos;
932
+ let size = box2.size ?? endPos - startPos;
933
+ this.seek(startPos);
934
+ this.writeBoxHeader(box2, size);
935
+ this.seek(endPos);
936
+ }
937
+ }
938
+ writeBoxHeader(box2, size) {
939
+ this.writeU32(box2.largeSize ? 1 : size);
940
+ this.writeAscii(box2.type);
941
+ if (box2.largeSize)
942
+ this.writeU64(size);
943
+ }
944
+ measureBoxHeader(box2) {
945
+ return 8 + (box2.largeSize ? 8 : 0);
946
+ }
947
+ patchBox(box2) {
948
+ let endPos = this.pos;
949
+ this.seek(this.offsets.get(box2));
950
+ this.writeBox(box2);
951
+ this.seek(endPos);
952
+ }
953
+ measureBox(box2) {
954
+ if (box2.contents && !box2.children) {
955
+ let headerSize = this.measureBoxHeader(box2);
956
+ return headerSize + box2.contents.byteLength;
957
+ } else {
958
+ let result = this.measureBoxHeader(box2);
959
+ if (box2.contents)
960
+ result += box2.contents.byteLength;
961
+ if (box2.children) {
962
+ for (let child of box2.children)
963
+ if (child)
964
+ result += this.measureBox(child);
965
+ }
966
+ return result;
967
+ }
968
+ }
969
+ };
970
+ _helper = new WeakMap();
971
+ _helperView = new WeakMap();
972
+ var _target, _buffer, _bytes, _maxPos, _ensureSize, ensureSize_fn;
973
+ var ArrayBufferTargetWriter = class extends Writer {
974
+ constructor(target) {
975
+ super();
976
+ __privateAdd(this, _ensureSize);
977
+ __privateAdd(this, _target, void 0);
978
+ __privateAdd(this, _buffer, new ArrayBuffer(2 ** 16));
979
+ __privateAdd(this, _bytes, new Uint8Array(__privateGet(this, _buffer)));
980
+ __privateAdd(this, _maxPos, 0);
981
+ __privateSet(this, _target, target);
982
+ }
983
+ write(data) {
984
+ __privateMethod(this, _ensureSize, ensureSize_fn).call(this, this.pos + data.byteLength);
985
+ __privateGet(this, _bytes).set(data, this.pos);
986
+ this.pos += data.byteLength;
987
+ __privateSet(this, _maxPos, Math.max(__privateGet(this, _maxPos), this.pos));
988
+ }
989
+ finalize() {
990
+ __privateMethod(this, _ensureSize, ensureSize_fn).call(this, this.pos);
991
+ __privateGet(this, _target).buffer = __privateGet(this, _buffer).slice(0, Math.max(__privateGet(this, _maxPos), this.pos));
992
+ }
993
+ };
994
+ _target = new WeakMap();
995
+ _buffer = new WeakMap();
996
+ _bytes = new WeakMap();
997
+ _maxPos = new WeakMap();
998
+ _ensureSize = new WeakSet();
999
+ ensureSize_fn = function(size) {
1000
+ let newLength = __privateGet(this, _buffer).byteLength;
1001
+ while (newLength < size)
1002
+ newLength *= 2;
1003
+ if (newLength === __privateGet(this, _buffer).byteLength)
1004
+ return;
1005
+ let newBuffer = new ArrayBuffer(newLength);
1006
+ let newBytes = new Uint8Array(newBuffer);
1007
+ newBytes.set(__privateGet(this, _bytes), 0);
1008
+ __privateSet(this, _buffer, newBuffer);
1009
+ __privateSet(this, _bytes, newBytes);
1010
+ };
1011
+ var DEFAULT_CHUNK_SIZE = 2 ** 24;
1012
+ var MAX_CHUNKS_AT_ONCE = 2;
1013
+ var _target2, _sections, _chunked, _chunkSize, _chunks, _writeDataIntoChunks, writeDataIntoChunks_fn, _insertSectionIntoChunk, insertSectionIntoChunk_fn, _createChunk, createChunk_fn, _flushChunks, flushChunks_fn;
1014
+ var StreamTargetWriter = class extends Writer {
1015
+ constructor(target) {
1016
+ super();
1017
+ __privateAdd(this, _writeDataIntoChunks);
1018
+ __privateAdd(this, _insertSectionIntoChunk);
1019
+ __privateAdd(this, _createChunk);
1020
+ __privateAdd(this, _flushChunks);
1021
+ __privateAdd(this, _target2, void 0);
1022
+ __privateAdd(this, _sections, []);
1023
+ __privateAdd(this, _chunked, void 0);
1024
+ __privateAdd(this, _chunkSize, void 0);
1025
+ /**
1026
+ * The data is divided up into fixed-size chunks, whose contents are first filled in RAM and then flushed out.
1027
+ * A chunk is flushed if all of its contents have been written.
1028
+ */
1029
+ __privateAdd(this, _chunks, []);
1030
+ __privateSet(this, _target2, target);
1031
+ __privateSet(this, _chunked, target.options?.chunked ?? false);
1032
+ __privateSet(this, _chunkSize, target.options?.chunkSize ?? DEFAULT_CHUNK_SIZE);
1033
+ }
1034
+ write(data) {
1035
+ __privateGet(this, _sections).push({
1036
+ data: data.slice(),
1037
+ start: this.pos
1038
+ });
1039
+ this.pos += data.byteLength;
1040
+ }
1041
+ flush() {
1042
+ if (__privateGet(this, _sections).length === 0)
1043
+ return;
1044
+ let chunks = [];
1045
+ let sorted = [...__privateGet(this, _sections)].sort((a, b) => a.start - b.start);
1046
+ chunks.push({
1047
+ start: sorted[0].start,
1048
+ size: sorted[0].data.byteLength
1049
+ });
1050
+ for (let i = 1; i < sorted.length; i++) {
1051
+ let lastChunk = chunks[chunks.length - 1];
1052
+ let section = sorted[i];
1053
+ if (section.start <= lastChunk.start + lastChunk.size) {
1054
+ lastChunk.size = Math.max(lastChunk.size, section.start + section.data.byteLength - lastChunk.start);
1055
+ } else {
1056
+ chunks.push({
1057
+ start: section.start,
1058
+ size: section.data.byteLength
1059
+ });
1060
+ }
1061
+ }
1062
+ for (let chunk of chunks) {
1063
+ chunk.data = new Uint8Array(chunk.size);
1064
+ for (let section of __privateGet(this, _sections)) {
1065
+ if (chunk.start <= section.start && section.start < chunk.start + chunk.size) {
1066
+ chunk.data.set(section.data, section.start - chunk.start);
1067
+ }
1068
+ }
1069
+ if (__privateGet(this, _chunked)) {
1070
+ __privateMethod(this, _writeDataIntoChunks, writeDataIntoChunks_fn).call(this, chunk.data, chunk.start);
1071
+ __privateMethod(this, _flushChunks, flushChunks_fn).call(this);
1072
+ } else {
1073
+ __privateGet(this, _target2).options.onData?.(chunk.data, chunk.start);
1074
+ }
1075
+ }
1076
+ __privateGet(this, _sections).length = 0;
1077
+ }
1078
+ finalize() {
1079
+ if (__privateGet(this, _chunked)) {
1080
+ __privateMethod(this, _flushChunks, flushChunks_fn).call(this, true);
1081
+ }
1082
+ }
1083
+ };
1084
+ _target2 = new WeakMap();
1085
+ _sections = new WeakMap();
1086
+ _chunked = new WeakMap();
1087
+ _chunkSize = new WeakMap();
1088
+ _chunks = new WeakMap();
1089
+ _writeDataIntoChunks = new WeakSet();
1090
+ writeDataIntoChunks_fn = function(data, position) {
1091
+ let chunkIndex = __privateGet(this, _chunks).findIndex((x) => x.start <= position && position < x.start + __privateGet(this, _chunkSize));
1092
+ if (chunkIndex === -1)
1093
+ chunkIndex = __privateMethod(this, _createChunk, createChunk_fn).call(this, position);
1094
+ let chunk = __privateGet(this, _chunks)[chunkIndex];
1095
+ let relativePosition = position - chunk.start;
1096
+ let toWrite = data.subarray(0, Math.min(__privateGet(this, _chunkSize) - relativePosition, data.byteLength));
1097
+ chunk.data.set(toWrite, relativePosition);
1098
+ let section = {
1099
+ start: relativePosition,
1100
+ end: relativePosition + toWrite.byteLength
1101
+ };
1102
+ __privateMethod(this, _insertSectionIntoChunk, insertSectionIntoChunk_fn).call(this, chunk, section);
1103
+ if (chunk.written[0].start === 0 && chunk.written[0].end === __privateGet(this, _chunkSize)) {
1104
+ chunk.shouldFlush = true;
1105
+ }
1106
+ if (__privateGet(this, _chunks).length > MAX_CHUNKS_AT_ONCE) {
1107
+ for (let i = 0; i < __privateGet(this, _chunks).length - 1; i++) {
1108
+ __privateGet(this, _chunks)[i].shouldFlush = true;
1109
+ }
1110
+ __privateMethod(this, _flushChunks, flushChunks_fn).call(this);
1111
+ }
1112
+ if (toWrite.byteLength < data.byteLength) {
1113
+ __privateMethod(this, _writeDataIntoChunks, writeDataIntoChunks_fn).call(this, data.subarray(toWrite.byteLength), position + toWrite.byteLength);
1114
+ }
1115
+ };
1116
+ _insertSectionIntoChunk = new WeakSet();
1117
+ insertSectionIntoChunk_fn = function(chunk, section) {
1118
+ let low = 0;
1119
+ let high = chunk.written.length - 1;
1120
+ let index = -1;
1121
+ while (low <= high) {
1122
+ let mid = Math.floor(low + (high - low + 1) / 2);
1123
+ if (chunk.written[mid].start <= section.start) {
1124
+ low = mid + 1;
1125
+ index = mid;
1126
+ } else {
1127
+ high = mid - 1;
1128
+ }
1129
+ }
1130
+ chunk.written.splice(index + 1, 0, section);
1131
+ if (index === -1 || chunk.written[index].end < section.start)
1132
+ index++;
1133
+ while (index < chunk.written.length - 1 && chunk.written[index].end >= chunk.written[index + 1].start) {
1134
+ chunk.written[index].end = Math.max(chunk.written[index].end, chunk.written[index + 1].end);
1135
+ chunk.written.splice(index + 1, 1);
1136
+ }
1137
+ };
1138
+ _createChunk = new WeakSet();
1139
+ createChunk_fn = function(includesPosition) {
1140
+ let start = Math.floor(includesPosition / __privateGet(this, _chunkSize)) * __privateGet(this, _chunkSize);
1141
+ let chunk = {
1142
+ start,
1143
+ data: new Uint8Array(__privateGet(this, _chunkSize)),
1144
+ written: [],
1145
+ shouldFlush: false
1146
+ };
1147
+ __privateGet(this, _chunks).push(chunk);
1148
+ __privateGet(this, _chunks).sort((a, b) => a.start - b.start);
1149
+ return __privateGet(this, _chunks).indexOf(chunk);
1150
+ };
1151
+ _flushChunks = new WeakSet();
1152
+ flushChunks_fn = function(force = false) {
1153
+ for (let i = 0; i < __privateGet(this, _chunks).length; i++) {
1154
+ let chunk = __privateGet(this, _chunks)[i];
1155
+ if (!chunk.shouldFlush && !force)
1156
+ continue;
1157
+ for (let section of chunk.written) {
1158
+ __privateGet(this, _target2).options.onData?.(
1159
+ chunk.data.subarray(section.start, section.end),
1160
+ chunk.start + section.start
1161
+ );
1162
+ }
1163
+ __privateGet(this, _chunks).splice(i--, 1);
1164
+ }
1165
+ };
1166
+ var FileSystemWritableFileStreamTargetWriter = class extends StreamTargetWriter {
1167
+ constructor(target) {
1168
+ super(new StreamTarget({
1169
+ onData: (data, position) => target.stream.write({
1170
+ type: "write",
1171
+ data,
1172
+ position
1173
+ }),
1174
+ chunked: true,
1175
+ chunkSize: target.options?.chunkSize
1176
+ }));
1177
+ }
1178
+ };
1179
+
1180
+ // src/muxer.ts
1181
+ var GLOBAL_TIMESCALE = 1e3;
1182
+ var SUPPORTED_VIDEO_CODECS = ["avc", "hevc", "vp9", "av1"];
1183
+ var SUPPORTED_AUDIO_CODECS = ["aac", "opus"];
1184
+ var TIMESTAMP_OFFSET = 2082844800;
1185
+ var FIRST_TIMESTAMP_BEHAVIORS = ["strict", "offset", "cross-track-offset"];
1186
+ var _options, _writer, _ftypSize, _mdat, _videoTrack, _audioTrack, _creationTime, _finalizedChunks, _nextFragmentNumber, _videoSampleQueue, _audioSampleQueue, _finalized, _validateOptions, validateOptions_fn, _writeHeader, writeHeader_fn, _computeMoovSizeUpperBound, computeMoovSizeUpperBound_fn, _prepareTracks, prepareTracks_fn, _generateMpeg4AudioSpecificConfig, generateMpeg4AudioSpecificConfig_fn, _createSampleForTrack, createSampleForTrack_fn, _addSampleToTrack, addSampleToTrack_fn, _validateTimestamp, validateTimestamp_fn, _finalizeCurrentChunk, finalizeCurrentChunk_fn, _finalizeFragment, finalizeFragment_fn, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn, _ensureNotFinalized, ensureNotFinalized_fn;
1187
+ var Muxer = class {
1188
+ constructor(options) {
1189
+ __privateAdd(this, _validateOptions);
1190
+ __privateAdd(this, _writeHeader);
1191
+ __privateAdd(this, _computeMoovSizeUpperBound);
1192
+ __privateAdd(this, _prepareTracks);
1193
+ // https://wiki.multimedia.cx/index.php/MPEG-4_Audio
1194
+ __privateAdd(this, _generateMpeg4AudioSpecificConfig);
1195
+ __privateAdd(this, _createSampleForTrack);
1196
+ __privateAdd(this, _addSampleToTrack);
1197
+ __privateAdd(this, _validateTimestamp);
1198
+ __privateAdd(this, _finalizeCurrentChunk);
1199
+ __privateAdd(this, _finalizeFragment);
1200
+ __privateAdd(this, _maybeFlushStreamingTargetWriter);
1201
+ __privateAdd(this, _ensureNotFinalized);
1202
+ __privateAdd(this, _options, void 0);
1203
+ __privateAdd(this, _writer, void 0);
1204
+ __privateAdd(this, _ftypSize, void 0);
1205
+ __privateAdd(this, _mdat, void 0);
1206
+ __privateAdd(this, _videoTrack, null);
1207
+ __privateAdd(this, _audioTrack, null);
1208
+ __privateAdd(this, _creationTime, Math.floor(Date.now() / 1e3) + TIMESTAMP_OFFSET);
1209
+ __privateAdd(this, _finalizedChunks, []);
1210
+ // Fields for fragmented MP4:
1211
+ __privateAdd(this, _nextFragmentNumber, 1);
1212
+ __privateAdd(this, _videoSampleQueue, []);
1213
+ __privateAdd(this, _audioSampleQueue, []);
1214
+ __privateAdd(this, _finalized, false);
1215
+ __privateMethod(this, _validateOptions, validateOptions_fn).call(this, options);
1216
+ options.video = deepClone(options.video);
1217
+ options.audio = deepClone(options.audio);
1218
+ options.fastStart = deepClone(options.fastStart);
1219
+ this.target = options.target;
1220
+ __privateSet(this, _options, {
1221
+ firstTimestampBehavior: "strict",
1222
+ ...options
1223
+ });
1224
+ if (options.target instanceof ArrayBufferTarget) {
1225
+ __privateSet(this, _writer, new ArrayBufferTargetWriter(options.target));
1226
+ } else if (options.target instanceof StreamTarget) {
1227
+ __privateSet(this, _writer, new StreamTargetWriter(options.target));
1228
+ } else if (options.target instanceof FileSystemWritableFileStreamTarget) {
1229
+ __privateSet(this, _writer, new FileSystemWritableFileStreamTargetWriter(options.target));
1230
+ } else {
1231
+ throw new Error(`Invalid target: ${options.target}`);
1232
+ }
1233
+ __privateMethod(this, _prepareTracks, prepareTracks_fn).call(this);
1234
+ __privateMethod(this, _writeHeader, writeHeader_fn).call(this);
1235
+ }
1236
+ addVideoChunk(sample, meta, timestamp, compositionTimeOffset) {
1237
+ if (!(sample instanceof EncodedVideoChunk)) {
1238
+ throw new TypeError("addVideoChunk's first argument (sample) must be of type EncodedVideoChunk.");
1239
+ }
1240
+ if (meta && typeof meta !== "object") {
1241
+ throw new TypeError("addVideoChunk's second argument (meta), when provided, must be an object.");
1242
+ }
1243
+ if (timestamp !== void 0 && (!Number.isFinite(timestamp) || timestamp < 0)) {
1244
+ throw new TypeError(
1245
+ "addVideoChunk's third argument (timestamp), when provided, must be a non-negative real number."
1246
+ );
1247
+ }
1248
+ if (compositionTimeOffset !== void 0 && !Number.isFinite(compositionTimeOffset)) {
1249
+ throw new TypeError(
1250
+ "addVideoChunk's fourth argument (compositionTimeOffset), when provided, must be a real number."
1251
+ );
1252
+ }
1253
+ let data = new Uint8Array(sample.byteLength);
1254
+ sample.copyTo(data);
1255
+ this.addVideoChunkRaw(
1256
+ data,
1257
+ sample.type,
1258
+ timestamp ?? sample.timestamp,
1259
+ sample.duration,
1260
+ meta,
1261
+ compositionTimeOffset
1262
+ );
1263
+ }
1264
+ addVideoChunkRaw(data, type, timestamp, duration, meta, compositionTimeOffset) {
1265
+ if (!(data instanceof Uint8Array)) {
1266
+ throw new TypeError("addVideoChunkRaw's first argument (data) must be an instance of Uint8Array.");
1267
+ }
1268
+ if (type !== "key" && type !== "delta") {
1269
+ throw new TypeError("addVideoChunkRaw's second argument (type) must be either 'key' or 'delta'.");
1270
+ }
1271
+ if (!Number.isFinite(timestamp) || timestamp < 0) {
1272
+ throw new TypeError("addVideoChunkRaw's third argument (timestamp) must be a non-negative real number.");
1273
+ }
1274
+ if (!Number.isFinite(duration) || duration < 0) {
1275
+ throw new TypeError("addVideoChunkRaw's fourth argument (duration) must be a non-negative real number.");
1276
+ }
1277
+ if (meta && typeof meta !== "object") {
1278
+ throw new TypeError("addVideoChunkRaw's fifth argument (meta), when provided, must be an object.");
1279
+ }
1280
+ if (compositionTimeOffset !== void 0 && !Number.isFinite(compositionTimeOffset)) {
1281
+ throw new TypeError(
1282
+ "addVideoChunkRaw's sixth argument (compositionTimeOffset), when provided, must be a real number."
1283
+ );
1284
+ }
1285
+ __privateMethod(this, _ensureNotFinalized, ensureNotFinalized_fn).call(this);
1286
+ if (!__privateGet(this, _options).video)
1287
+ throw new Error("No video track declared.");
1288
+ if (typeof __privateGet(this, _options).fastStart === "object" && __privateGet(this, _videoTrack).samples.length === __privateGet(this, _options).fastStart.expectedVideoChunks) {
1289
+ throw new Error(`Cannot add more video chunks than specified in 'fastStart' (${__privateGet(this, _options).fastStart.expectedVideoChunks}).`);
1290
+ }
1291
+ let videoSample = __privateMethod(this, _createSampleForTrack, createSampleForTrack_fn).call(this, __privateGet(this, _videoTrack), data, type, timestamp, duration, meta, compositionTimeOffset);
1292
+ if (__privateGet(this, _options).fastStart === "fragmented" && __privateGet(this, _audioTrack)) {
1293
+ while (__privateGet(this, _audioSampleQueue).length > 0 && __privateGet(this, _audioSampleQueue)[0].decodeTimestamp <= videoSample.decodeTimestamp) {
1294
+ let audioSample = __privateGet(this, _audioSampleQueue).shift();
1295
+ __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _audioTrack), audioSample);
1296
+ }
1297
+ if (videoSample.decodeTimestamp <= __privateGet(this, _audioTrack).lastDecodeTimestamp) {
1298
+ __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _videoTrack), videoSample);
1299
+ } else {
1300
+ __privateGet(this, _videoSampleQueue).push(videoSample);
1301
+ }
1302
+ } else {
1303
+ __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _videoTrack), videoSample);
1304
+ }
1305
+ }
1306
+ addAudioChunk(sample, meta, timestamp) {
1307
+ if (!(sample instanceof EncodedAudioChunk)) {
1308
+ throw new TypeError("addAudioChunk's first argument (sample) must be of type EncodedAudioChunk.");
1309
+ }
1310
+ if (meta && typeof meta !== "object") {
1311
+ throw new TypeError("addAudioChunk's second argument (meta), when provided, must be an object.");
1312
+ }
1313
+ if (timestamp !== void 0 && (!Number.isFinite(timestamp) || timestamp < 0)) {
1314
+ throw new TypeError(
1315
+ "addAudioChunk's third argument (timestamp), when provided, must be a non-negative real number."
1316
+ );
1317
+ }
1318
+ let data = new Uint8Array(sample.byteLength);
1319
+ sample.copyTo(data);
1320
+ this.addAudioChunkRaw(data, sample.type, timestamp ?? sample.timestamp, sample.duration, meta);
1321
+ }
1322
+ addAudioChunkRaw(data, type, timestamp, duration, meta) {
1323
+ if (!(data instanceof Uint8Array)) {
1324
+ throw new TypeError("addAudioChunkRaw's first argument (data) must be an instance of Uint8Array.");
1325
+ }
1326
+ if (type !== "key" && type !== "delta") {
1327
+ throw new TypeError("addAudioChunkRaw's second argument (type) must be either 'key' or 'delta'.");
1328
+ }
1329
+ if (!Number.isFinite(timestamp) || timestamp < 0) {
1330
+ throw new TypeError("addAudioChunkRaw's third argument (timestamp) must be a non-negative real number.");
1331
+ }
1332
+ if (!Number.isFinite(duration) || duration < 0) {
1333
+ throw new TypeError("addAudioChunkRaw's fourth argument (duration) must be a non-negative real number.");
1334
+ }
1335
+ if (meta && typeof meta !== "object") {
1336
+ throw new TypeError("addAudioChunkRaw's fifth argument (meta), when provided, must be an object.");
1337
+ }
1338
+ __privateMethod(this, _ensureNotFinalized, ensureNotFinalized_fn).call(this);
1339
+ if (!__privateGet(this, _options).audio)
1340
+ throw new Error("No audio track declared.");
1341
+ if (typeof __privateGet(this, _options).fastStart === "object" && __privateGet(this, _audioTrack).samples.length === __privateGet(this, _options).fastStart.expectedAudioChunks) {
1342
+ throw new Error(`Cannot add more audio chunks than specified in 'fastStart' (${__privateGet(this, _options).fastStart.expectedAudioChunks}).`);
1343
+ }
1344
+ let audioSample = __privateMethod(this, _createSampleForTrack, createSampleForTrack_fn).call(this, __privateGet(this, _audioTrack), data, type, timestamp, duration, meta);
1345
+ if (__privateGet(this, _options).fastStart === "fragmented" && __privateGet(this, _videoTrack)) {
1346
+ while (__privateGet(this, _videoSampleQueue).length > 0 && __privateGet(this, _videoSampleQueue)[0].decodeTimestamp <= audioSample.decodeTimestamp) {
1347
+ let videoSample = __privateGet(this, _videoSampleQueue).shift();
1348
+ __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _videoTrack), videoSample);
1349
+ }
1350
+ if (audioSample.decodeTimestamp <= __privateGet(this, _videoTrack).lastDecodeTimestamp) {
1351
+ __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _audioTrack), audioSample);
1352
+ } else {
1353
+ __privateGet(this, _audioSampleQueue).push(audioSample);
1354
+ }
1355
+ } else {
1356
+ __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _audioTrack), audioSample);
1357
+ }
1358
+ }
1359
+ /** Finalizes the file, making it ready for use. Must be called after all video and audio chunks have been added. */
1360
+ finalize() {
1361
+ if (__privateGet(this, _finalized)) {
1362
+ throw new Error("Cannot finalize a muxer more than once.");
1363
+ }
1364
+ if (__privateGet(this, _options).fastStart === "fragmented") {
1365
+ for (let videoSample of __privateGet(this, _videoSampleQueue))
1366
+ __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _videoTrack), videoSample);
1367
+ for (let audioSample of __privateGet(this, _audioSampleQueue))
1368
+ __privateMethod(this, _addSampleToTrack, addSampleToTrack_fn).call(this, __privateGet(this, _audioTrack), audioSample);
1369
+ __privateMethod(this, _finalizeFragment, finalizeFragment_fn).call(this, false);
1370
+ } else {
1371
+ if (__privateGet(this, _videoTrack))
1372
+ __privateMethod(this, _finalizeCurrentChunk, finalizeCurrentChunk_fn).call(this, __privateGet(this, _videoTrack));
1373
+ if (__privateGet(this, _audioTrack))
1374
+ __privateMethod(this, _finalizeCurrentChunk, finalizeCurrentChunk_fn).call(this, __privateGet(this, _audioTrack));
1375
+ }
1376
+ let tracks = [__privateGet(this, _videoTrack), __privateGet(this, _audioTrack)].filter(Boolean);
1377
+ if (__privateGet(this, _options).fastStart === "in-memory") {
1378
+ let mdatSize;
1379
+ for (let i = 0; i < 2; i++) {
1380
+ let movieBox2 = moov(tracks, __privateGet(this, _creationTime));
1381
+ let movieBoxSize = __privateGet(this, _writer).measureBox(movieBox2);
1382
+ mdatSize = __privateGet(this, _writer).measureBox(__privateGet(this, _mdat));
1383
+ let currentChunkPos = __privateGet(this, _writer).pos + movieBoxSize + mdatSize;
1384
+ for (let chunk of __privateGet(this, _finalizedChunks)) {
1385
+ chunk.offset = currentChunkPos;
1386
+ for (let { data } of chunk.samples) {
1387
+ currentChunkPos += data.byteLength;
1388
+ mdatSize += data.byteLength;
1389
+ }
1390
+ }
1391
+ if (currentChunkPos < 2 ** 32)
1392
+ break;
1393
+ if (mdatSize >= 2 ** 32)
1394
+ __privateGet(this, _mdat).largeSize = true;
1395
+ }
1396
+ let movieBox = moov(tracks, __privateGet(this, _creationTime));
1397
+ __privateGet(this, _writer).writeBox(movieBox);
1398
+ __privateGet(this, _mdat).size = mdatSize;
1399
+ __privateGet(this, _writer).writeBox(__privateGet(this, _mdat));
1400
+ for (let chunk of __privateGet(this, _finalizedChunks)) {
1401
+ for (let sample of chunk.samples) {
1402
+ __privateGet(this, _writer).write(sample.data);
1403
+ sample.data = null;
1404
+ }
1405
+ }
1406
+ } else if (__privateGet(this, _options).fastStart === "fragmented") {
1407
+ let startPos = __privateGet(this, _writer).pos;
1408
+ let mfraBox = mfra(tracks);
1409
+ __privateGet(this, _writer).writeBox(mfraBox);
1410
+ let mfraBoxSize = __privateGet(this, _writer).pos - startPos;
1411
+ __privateGet(this, _writer).seek(__privateGet(this, _writer).pos - 4);
1412
+ __privateGet(this, _writer).writeU32(mfraBoxSize);
1413
+ } else {
1414
+ let mdatPos = __privateGet(this, _writer).offsets.get(__privateGet(this, _mdat));
1415
+ let mdatSize = __privateGet(this, _writer).pos - mdatPos;
1416
+ __privateGet(this, _mdat).size = mdatSize;
1417
+ __privateGet(this, _mdat).largeSize = mdatSize >= 2 ** 32;
1418
+ __privateGet(this, _writer).patchBox(__privateGet(this, _mdat));
1419
+ let movieBox = moov(tracks, __privateGet(this, _creationTime));
1420
+ if (typeof __privateGet(this, _options).fastStart === "object") {
1421
+ __privateGet(this, _writer).seek(__privateGet(this, _ftypSize));
1422
+ __privateGet(this, _writer).writeBox(movieBox);
1423
+ let remainingBytes = mdatPos - __privateGet(this, _writer).pos;
1424
+ __privateGet(this, _writer).writeBox(free(remainingBytes));
1425
+ } else {
1426
+ __privateGet(this, _writer).writeBox(movieBox);
1427
+ }
1428
+ }
1429
+ __privateMethod(this, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn).call(this);
1430
+ __privateGet(this, _writer).finalize();
1431
+ __privateSet(this, _finalized, true);
1432
+ }
1433
+ };
1434
+ _options = new WeakMap();
1435
+ _writer = new WeakMap();
1436
+ _ftypSize = new WeakMap();
1437
+ _mdat = new WeakMap();
1438
+ _videoTrack = new WeakMap();
1439
+ _audioTrack = new WeakMap();
1440
+ _creationTime = new WeakMap();
1441
+ _finalizedChunks = new WeakMap();
1442
+ _nextFragmentNumber = new WeakMap();
1443
+ _videoSampleQueue = new WeakMap();
1444
+ _audioSampleQueue = new WeakMap();
1445
+ _finalized = new WeakMap();
1446
+ _validateOptions = new WeakSet();
1447
+ validateOptions_fn = function(options) {
1448
+ if (typeof options !== "object") {
1449
+ throw new TypeError("The muxer requires an options object to be passed to its constructor.");
1450
+ }
1451
+ if (!(options.target instanceof Target)) {
1452
+ throw new TypeError("The target must be provided and an instance of Target.");
1453
+ }
1454
+ if (options.video) {
1455
+ if (!SUPPORTED_VIDEO_CODECS.includes(options.video.codec)) {
1456
+ throw new TypeError(`Unsupported video codec: ${options.video.codec}`);
1457
+ }
1458
+ if (!Number.isInteger(options.video.width) || options.video.width <= 0) {
1459
+ throw new TypeError(`Invalid video width: ${options.video.width}. Must be a positive integer.`);
1460
+ }
1461
+ if (!Number.isInteger(options.video.height) || options.video.height <= 0) {
1462
+ throw new TypeError(`Invalid video height: ${options.video.height}. Must be a positive integer.`);
1463
+ }
1464
+ const videoRotation = options.video.rotation;
1465
+ if (typeof videoRotation === "number" && ![0, 90, 180, 270].includes(videoRotation)) {
1466
+ throw new TypeError(`Invalid video rotation: ${videoRotation}. Has to be 0, 90, 180 or 270.`);
1467
+ } else if (Array.isArray(videoRotation) && (videoRotation.length !== 9 || videoRotation.some((value) => typeof value !== "number"))) {
1468
+ throw new TypeError(`Invalid video transformation matrix: ${videoRotation.join()}`);
1469
+ }
1470
+ if (options.video.frameRate !== void 0 && (!Number.isInteger(options.video.frameRate) || options.video.frameRate <= 0)) {
1471
+ throw new TypeError(
1472
+ `Invalid video frame rate: ${options.video.frameRate}. Must be a positive integer.`
1473
+ );
1474
+ }
1475
+ }
1476
+ if (options.audio) {
1477
+ if (!SUPPORTED_AUDIO_CODECS.includes(options.audio.codec)) {
1478
+ throw new TypeError(`Unsupported audio codec: ${options.audio.codec}`);
1479
+ }
1480
+ if (!Number.isInteger(options.audio.numberOfChannels) || options.audio.numberOfChannels <= 0) {
1481
+ throw new TypeError(
1482
+ `Invalid number of audio channels: ${options.audio.numberOfChannels}. Must be a positive integer.`
1483
+ );
1484
+ }
1485
+ if (!Number.isInteger(options.audio.sampleRate) || options.audio.sampleRate <= 0) {
1486
+ throw new TypeError(
1487
+ `Invalid audio sample rate: ${options.audio.sampleRate}. Must be a positive integer.`
1488
+ );
1489
+ }
1490
+ }
1491
+ if (options.firstTimestampBehavior && !FIRST_TIMESTAMP_BEHAVIORS.includes(options.firstTimestampBehavior)) {
1492
+ throw new TypeError(`Invalid first timestamp behavior: ${options.firstTimestampBehavior}`);
1493
+ }
1494
+ if (typeof options.fastStart === "object") {
1495
+ if (options.video) {
1496
+ if (options.fastStart.expectedVideoChunks === void 0) {
1497
+ throw new TypeError(`'fastStart' is an object but is missing property 'expectedVideoChunks'.`);
1498
+ } else if (!Number.isInteger(options.fastStart.expectedVideoChunks) || options.fastStart.expectedVideoChunks < 0) {
1499
+ throw new TypeError(`'expectedVideoChunks' must be a non-negative integer.`);
1500
+ }
1501
+ }
1502
+ if (options.audio) {
1503
+ if (options.fastStart.expectedAudioChunks === void 0) {
1504
+ throw new TypeError(`'fastStart' is an object but is missing property 'expectedAudioChunks'.`);
1505
+ } else if (!Number.isInteger(options.fastStart.expectedAudioChunks) || options.fastStart.expectedAudioChunks < 0) {
1506
+ throw new TypeError(`'expectedAudioChunks' must be a non-negative integer.`);
1507
+ }
1508
+ }
1509
+ } else if (![false, "in-memory", "fragmented"].includes(options.fastStart)) {
1510
+ throw new TypeError(`'fastStart' option must be false, 'in-memory', 'fragmented' or an object.`);
1511
+ }
1512
+ if (options.minFragmentDuration !== void 0 && (!Number.isFinite(options.minFragmentDuration) || options.minFragmentDuration < 0)) {
1513
+ throw new TypeError(`'minFragmentDuration' must be a non-negative number.`);
1514
+ }
1515
+ };
1516
+ _writeHeader = new WeakSet();
1517
+ writeHeader_fn = function() {
1518
+ __privateGet(this, _writer).writeBox(ftyp({
1519
+ holdsAvc: __privateGet(this, _options).video?.codec === "avc",
1520
+ fragmented: __privateGet(this, _options).fastStart === "fragmented"
1521
+ }));
1522
+ __privateSet(this, _ftypSize, __privateGet(this, _writer).pos);
1523
+ if (__privateGet(this, _options).fastStart === "in-memory") {
1524
+ __privateSet(this, _mdat, mdat(false));
1525
+ } else if (__privateGet(this, _options).fastStart === "fragmented") {
1526
+ } else {
1527
+ if (typeof __privateGet(this, _options).fastStart === "object") {
1528
+ let moovSizeUpperBound = __privateMethod(this, _computeMoovSizeUpperBound, computeMoovSizeUpperBound_fn).call(this);
1529
+ __privateGet(this, _writer).seek(__privateGet(this, _writer).pos + moovSizeUpperBound);
1530
+ }
1531
+ __privateSet(this, _mdat, mdat(true));
1532
+ __privateGet(this, _writer).writeBox(__privateGet(this, _mdat));
1533
+ }
1534
+ __privateMethod(this, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn).call(this);
1535
+ };
1536
+ _computeMoovSizeUpperBound = new WeakSet();
1537
+ computeMoovSizeUpperBound_fn = function() {
1538
+ if (typeof __privateGet(this, _options).fastStart !== "object")
1539
+ return;
1540
+ let upperBound = 0;
1541
+ let sampleCounts = [
1542
+ __privateGet(this, _options).fastStart.expectedVideoChunks,
1543
+ __privateGet(this, _options).fastStart.expectedAudioChunks
1544
+ ];
1545
+ for (let n of sampleCounts) {
1546
+ if (!n)
1547
+ continue;
1548
+ upperBound += (4 + 4) * Math.ceil(2 / 3 * n);
1549
+ upperBound += 4 * n;
1550
+ upperBound += (4 + 4 + 4) * Math.ceil(2 / 3 * n);
1551
+ upperBound += 4 * n;
1552
+ upperBound += 8 * n;
1553
+ }
1554
+ upperBound += 4096;
1555
+ return upperBound;
1556
+ };
1557
+ _prepareTracks = new WeakSet();
1558
+ prepareTracks_fn = function() {
1559
+ if (__privateGet(this, _options).video) {
1560
+ __privateSet(this, _videoTrack, {
1561
+ id: 1,
1562
+ info: {
1563
+ type: "video",
1564
+ codec: __privateGet(this, _options).video.codec,
1565
+ width: __privateGet(this, _options).video.width,
1566
+ height: __privateGet(this, _options).video.height,
1567
+ rotation: __privateGet(this, _options).video.rotation ?? 0,
1568
+ decoderConfig: null
1569
+ },
1570
+ // The fallback contains many common frame rates as factors
1571
+ timescale: __privateGet(this, _options).video.frameRate ?? 57600,
1572
+ samples: [],
1573
+ finalizedChunks: [],
1574
+ currentChunk: null,
1575
+ firstDecodeTimestamp: void 0,
1576
+ lastDecodeTimestamp: -1,
1577
+ timeToSampleTable: [],
1578
+ compositionTimeOffsetTable: [],
1579
+ lastTimescaleUnits: null,
1580
+ lastSample: null,
1581
+ compactlyCodedChunkTable: []
1582
+ });
1583
+ }
1584
+ if (__privateGet(this, _options).audio) {
1585
+ __privateSet(this, _audioTrack, {
1586
+ id: __privateGet(this, _options).video ? 2 : 1,
1587
+ info: {
1588
+ type: "audio",
1589
+ codec: __privateGet(this, _options).audio.codec,
1590
+ numberOfChannels: __privateGet(this, _options).audio.numberOfChannels,
1591
+ sampleRate: __privateGet(this, _options).audio.sampleRate,
1592
+ decoderConfig: null
1593
+ },
1594
+ timescale: __privateGet(this, _options).audio.sampleRate,
1595
+ samples: [],
1596
+ finalizedChunks: [],
1597
+ currentChunk: null,
1598
+ firstDecodeTimestamp: void 0,
1599
+ lastDecodeTimestamp: -1,
1600
+ timeToSampleTable: [],
1601
+ compositionTimeOffsetTable: [],
1602
+ lastTimescaleUnits: null,
1603
+ lastSample: null,
1604
+ compactlyCodedChunkTable: []
1605
+ });
1606
+ if (__privateGet(this, _options).audio.codec === "aac") {
1607
+ let guessedCodecPrivate = __privateMethod(this, _generateMpeg4AudioSpecificConfig, generateMpeg4AudioSpecificConfig_fn).call(
1608
+ this,
1609
+ 2,
1610
+ // Object type for AAC-LC, since it's the most common
1611
+ __privateGet(this, _options).audio.sampleRate,
1612
+ __privateGet(this, _options).audio.numberOfChannels
1613
+ );
1614
+ __privateGet(this, _audioTrack).info.decoderConfig = {
1615
+ codec: __privateGet(this, _options).audio.codec,
1616
+ description: guessedCodecPrivate,
1617
+ numberOfChannels: __privateGet(this, _options).audio.numberOfChannels,
1618
+ sampleRate: __privateGet(this, _options).audio.sampleRate
1619
+ };
1620
+ }
1621
+ }
1622
+ };
1623
+ _generateMpeg4AudioSpecificConfig = new WeakSet();
1624
+ generateMpeg4AudioSpecificConfig_fn = function(objectType, sampleRate, numberOfChannels) {
1625
+ let frequencyIndices = [96e3, 88200, 64e3, 48e3, 44100, 32e3, 24e3, 22050, 16e3, 12e3, 11025, 8e3, 7350];
1626
+ let frequencyIndex = frequencyIndices.indexOf(sampleRate);
1627
+ let channelConfig = numberOfChannels;
1628
+ let configBits = "";
1629
+ configBits += objectType.toString(2).padStart(5, "0");
1630
+ configBits += frequencyIndex.toString(2).padStart(4, "0");
1631
+ if (frequencyIndex === 15)
1632
+ configBits += sampleRate.toString(2).padStart(24, "0");
1633
+ configBits += channelConfig.toString(2).padStart(4, "0");
1634
+ let paddingLength = Math.ceil(configBits.length / 8) * 8;
1635
+ configBits = configBits.padEnd(paddingLength, "0");
1636
+ let configBytes = new Uint8Array(configBits.length / 8);
1637
+ for (let i = 0; i < configBits.length; i += 8) {
1638
+ configBytes[i / 8] = parseInt(configBits.slice(i, i + 8), 2);
1639
+ }
1640
+ return configBytes;
1641
+ };
1642
+ _createSampleForTrack = new WeakSet();
1643
+ createSampleForTrack_fn = function(track, data, type, timestamp, duration, meta, compositionTimeOffset) {
1644
+ let presentationTimestampInSeconds = timestamp / 1e6;
1645
+ let decodeTimestampInSeconds = (timestamp - (compositionTimeOffset ?? 0)) / 1e6;
1646
+ let durationInSeconds = duration / 1e6;
1647
+ let adjusted = __privateMethod(this, _validateTimestamp, validateTimestamp_fn).call(this, presentationTimestampInSeconds, decodeTimestampInSeconds, track);
1648
+ presentationTimestampInSeconds = adjusted.presentationTimestamp;
1649
+ decodeTimestampInSeconds = adjusted.decodeTimestamp;
1650
+ if (meta?.decoderConfig) {
1651
+ if (track.info.decoderConfig === null) {
1652
+ track.info.decoderConfig = meta.decoderConfig;
1653
+ } else {
1654
+ Object.assign(track.info.decoderConfig, meta.decoderConfig);
1655
+ }
1656
+ }
1657
+ let sample = {
1658
+ presentationTimestamp: presentationTimestampInSeconds,
1659
+ decodeTimestamp: decodeTimestampInSeconds,
1660
+ duration: durationInSeconds,
1661
+ data,
1662
+ size: data.byteLength,
1663
+ type,
1664
+ // Will be refined once the next sample comes in
1665
+ timescaleUnitsToNextSample: intoTimescale(durationInSeconds, track.timescale)
1666
+ };
1667
+ return sample;
1668
+ };
1669
+ _addSampleToTrack = new WeakSet();
1670
+ addSampleToTrack_fn = function(track, sample) {
1671
+ if (__privateGet(this, _options).fastStart !== "fragmented") {
1672
+ track.samples.push(sample);
1673
+ }
1674
+ const sampleCompositionTimeOffset = intoTimescale(sample.presentationTimestamp - sample.decodeTimestamp, track.timescale);
1675
+ if (track.lastTimescaleUnits !== null) {
1676
+ let timescaleUnits = intoTimescale(sample.decodeTimestamp, track.timescale, false);
1677
+ let delta = Math.round(timescaleUnits - track.lastTimescaleUnits);
1678
+ track.lastTimescaleUnits += delta;
1679
+ track.lastSample.timescaleUnitsToNextSample = delta;
1680
+ if (__privateGet(this, _options).fastStart !== "fragmented") {
1681
+ let lastTableEntry = last(track.timeToSampleTable);
1682
+ if (lastTableEntry.sampleCount === 1) {
1683
+ lastTableEntry.sampleDelta = delta;
1684
+ lastTableEntry.sampleCount++;
1685
+ } else if (lastTableEntry.sampleDelta === delta) {
1686
+ lastTableEntry.sampleCount++;
1687
+ } else {
1688
+ lastTableEntry.sampleCount--;
1689
+ track.timeToSampleTable.push({
1690
+ sampleCount: 2,
1691
+ sampleDelta: delta
1692
+ });
1693
+ }
1694
+ const lastCompositionTimeOffsetTableEntry = last(track.compositionTimeOffsetTable);
1695
+ if (lastCompositionTimeOffsetTableEntry.sampleCompositionTimeOffset === sampleCompositionTimeOffset) {
1696
+ lastCompositionTimeOffsetTableEntry.sampleCount++;
1697
+ } else {
1698
+ track.compositionTimeOffsetTable.push({
1699
+ sampleCount: 1,
1700
+ sampleCompositionTimeOffset
1701
+ });
1702
+ }
1703
+ }
1704
+ } else {
1705
+ track.lastTimescaleUnits = 0;
1706
+ if (__privateGet(this, _options).fastStart !== "fragmented") {
1707
+ track.timeToSampleTable.push({
1708
+ sampleCount: 1,
1709
+ sampleDelta: intoTimescale(sample.duration, track.timescale)
1710
+ });
1711
+ track.compositionTimeOffsetTable.push({
1712
+ sampleCount: 1,
1713
+ sampleCompositionTimeOffset
1714
+ });
1715
+ }
1716
+ }
1717
+ track.lastSample = sample;
1718
+ let beginNewChunk = false;
1719
+ if (!track.currentChunk) {
1720
+ beginNewChunk = true;
1721
+ } else {
1722
+ let currentChunkDuration = sample.presentationTimestamp - track.currentChunk.startTimestamp;
1723
+ if (__privateGet(this, _options).fastStart === "fragmented") {
1724
+ let mostImportantTrack = __privateGet(this, _videoTrack) ?? __privateGet(this, _audioTrack);
1725
+ const chunkDuration = __privateGet(this, _options).minFragmentDuration ?? 1;
1726
+ if (track === mostImportantTrack && sample.type === "key" && currentChunkDuration >= chunkDuration) {
1727
+ beginNewChunk = true;
1728
+ __privateMethod(this, _finalizeFragment, finalizeFragment_fn).call(this);
1729
+ }
1730
+ } else {
1731
+ beginNewChunk = currentChunkDuration >= 0.5;
1732
+ }
1733
+ }
1734
+ if (beginNewChunk) {
1735
+ if (track.currentChunk) {
1736
+ __privateMethod(this, _finalizeCurrentChunk, finalizeCurrentChunk_fn).call(this, track);
1737
+ }
1738
+ track.currentChunk = {
1739
+ startTimestamp: sample.presentationTimestamp,
1740
+ samples: []
1741
+ };
1742
+ }
1743
+ track.currentChunk.samples.push(sample);
1744
+ };
1745
+ _validateTimestamp = new WeakSet();
1746
+ validateTimestamp_fn = function(presentationTimestamp, decodeTimestamp, track) {
1747
+ const strictTimestampBehavior = __privateGet(this, _options).firstTimestampBehavior === "strict";
1748
+ const noLastDecodeTimestamp = track.lastDecodeTimestamp === -1;
1749
+ const timestampNonZero = decodeTimestamp !== 0;
1750
+ if (strictTimestampBehavior && noLastDecodeTimestamp && timestampNonZero) {
1751
+ throw new Error(
1752
+ `The first chunk for your media track must have a timestamp of 0 (received DTS=${decodeTimestamp}).Non-zero first timestamps are often caused by directly piping frames or audio data from a MediaStreamTrack into the encoder. Their timestamps are typically relative to the age of thedocument, which is probably what you want.
1753
+
1754
+ If you want to offset all timestamps of a track such that the first one is zero, set firstTimestampBehavior: 'offset' in the options.
1755
+ `
1756
+ );
1757
+ } else if (__privateGet(this, _options).firstTimestampBehavior === "offset" || __privateGet(this, _options).firstTimestampBehavior === "cross-track-offset") {
1758
+ if (track.firstDecodeTimestamp === void 0) {
1759
+ track.firstDecodeTimestamp = decodeTimestamp;
1760
+ }
1761
+ let baseDecodeTimestamp;
1762
+ if (__privateGet(this, _options).firstTimestampBehavior === "offset") {
1763
+ baseDecodeTimestamp = track.firstDecodeTimestamp;
1764
+ } else {
1765
+ baseDecodeTimestamp = Math.min(
1766
+ __privateGet(this, _videoTrack)?.firstDecodeTimestamp ?? Infinity,
1767
+ __privateGet(this, _audioTrack)?.firstDecodeTimestamp ?? Infinity
1768
+ );
1769
+ }
1770
+ decodeTimestamp -= baseDecodeTimestamp;
1771
+ presentationTimestamp -= baseDecodeTimestamp;
1772
+ }
1773
+ if (decodeTimestamp < track.lastDecodeTimestamp) {
1774
+ throw new Error(
1775
+ `Timestamps must be monotonically increasing (DTS went from ${track.lastDecodeTimestamp * 1e6} to ${decodeTimestamp * 1e6}).`
1776
+ );
1777
+ }
1778
+ track.lastDecodeTimestamp = decodeTimestamp;
1779
+ return { presentationTimestamp, decodeTimestamp };
1780
+ };
1781
+ _finalizeCurrentChunk = new WeakSet();
1782
+ finalizeCurrentChunk_fn = function(track) {
1783
+ if (__privateGet(this, _options).fastStart === "fragmented") {
1784
+ throw new Error("Can't finalize individual chunks if 'fastStart' is set to 'fragmented'.");
1785
+ }
1786
+ if (!track.currentChunk)
1787
+ return;
1788
+ track.finalizedChunks.push(track.currentChunk);
1789
+ __privateGet(this, _finalizedChunks).push(track.currentChunk);
1790
+ if (track.compactlyCodedChunkTable.length === 0 || last(track.compactlyCodedChunkTable).samplesPerChunk !== track.currentChunk.samples.length) {
1791
+ track.compactlyCodedChunkTable.push({
1792
+ firstChunk: track.finalizedChunks.length,
1793
+ // 1-indexed
1794
+ samplesPerChunk: track.currentChunk.samples.length
1795
+ });
1796
+ }
1797
+ if (__privateGet(this, _options).fastStart === "in-memory") {
1798
+ track.currentChunk.offset = 0;
1799
+ return;
1800
+ }
1801
+ track.currentChunk.offset = __privateGet(this, _writer).pos;
1802
+ for (let sample of track.currentChunk.samples) {
1803
+ __privateGet(this, _writer).write(sample.data);
1804
+ sample.data = null;
1805
+ }
1806
+ __privateMethod(this, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn).call(this);
1807
+ };
1808
+ _finalizeFragment = new WeakSet();
1809
+ finalizeFragment_fn = function(flushStreamingWriter = true) {
1810
+ if (__privateGet(this, _options).fastStart !== "fragmented") {
1811
+ throw new Error("Can't finalize a fragment unless 'fastStart' is set to 'fragmented'.");
1812
+ }
1813
+ let tracks = [__privateGet(this, _videoTrack), __privateGet(this, _audioTrack)].filter((track) => track && track.currentChunk);
1814
+ if (tracks.length === 0)
1815
+ return;
1816
+ let fragmentNumber = __privateWrapper(this, _nextFragmentNumber)._++;
1817
+ if (fragmentNumber === 1) {
1818
+ let movieBox = moov(tracks, __privateGet(this, _creationTime), true);
1819
+ __privateGet(this, _writer).writeBox(movieBox);
1820
+ }
1821
+ let moofOffset = __privateGet(this, _writer).pos;
1822
+ let moofBox = moof(fragmentNumber, tracks);
1823
+ __privateGet(this, _writer).writeBox(moofBox);
1824
+ {
1825
+ let mdatBox = mdat(false);
1826
+ let totalTrackSampleSize = 0;
1827
+ for (let track of tracks) {
1828
+ for (let sample of track.currentChunk.samples) {
1829
+ totalTrackSampleSize += sample.size;
1830
+ }
1831
+ }
1832
+ let mdatSize = __privateGet(this, _writer).measureBox(mdatBox) + totalTrackSampleSize;
1833
+ if (mdatSize >= 2 ** 32) {
1834
+ mdatBox.largeSize = true;
1835
+ mdatSize = __privateGet(this, _writer).measureBox(mdatBox) + totalTrackSampleSize;
1836
+ }
1837
+ mdatBox.size = mdatSize;
1838
+ __privateGet(this, _writer).writeBox(mdatBox);
1839
+ }
1840
+ for (let track of tracks) {
1841
+ track.currentChunk.offset = __privateGet(this, _writer).pos;
1842
+ track.currentChunk.moofOffset = moofOffset;
1843
+ for (let sample of track.currentChunk.samples) {
1844
+ __privateGet(this, _writer).write(sample.data);
1845
+ sample.data = null;
1846
+ }
1847
+ }
1848
+ let endPos = __privateGet(this, _writer).pos;
1849
+ __privateGet(this, _writer).seek(__privateGet(this, _writer).offsets.get(moofBox));
1850
+ let newMoofBox = moof(fragmentNumber, tracks);
1851
+ __privateGet(this, _writer).writeBox(newMoofBox);
1852
+ __privateGet(this, _writer).seek(endPos);
1853
+ for (let track of tracks) {
1854
+ track.finalizedChunks.push(track.currentChunk);
1855
+ __privateGet(this, _finalizedChunks).push(track.currentChunk);
1856
+ track.currentChunk = null;
1857
+ }
1858
+ if (flushStreamingWriter) {
1859
+ __privateMethod(this, _maybeFlushStreamingTargetWriter, maybeFlushStreamingTargetWriter_fn).call(this);
1860
+ }
1861
+ };
1862
+ _maybeFlushStreamingTargetWriter = new WeakSet();
1863
+ maybeFlushStreamingTargetWriter_fn = function() {
1864
+ if (__privateGet(this, _writer) instanceof StreamTargetWriter) {
1865
+ __privateGet(this, _writer).flush();
1866
+ }
1867
+ };
1868
+ _ensureNotFinalized = new WeakSet();
1869
+ ensureNotFinalized_fn = function() {
1870
+ if (__privateGet(this, _finalized)) {
1871
+ throw new Error("Cannot add new video or audio chunks after the file has been finalized.");
1872
+ }
1873
+ };
1874
+ export {
1875
+ ArrayBufferTarget,
1876
+ FileSystemWritableFileStreamTarget,
1877
+ Muxer,
1878
+ StreamTarget
1879
+ };