@ahmedrowaihi/mediabunny 1.65.2-beta.329 → 1.65.2-beta.415

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.
@@ -312,6 +312,8 @@ var Mediabunny = (() => {
312
312
  buildMdcvBox: () => buildMdcvBox,
313
313
  buildPlayReadyObject: () => buildPlayReadyObject,
314
314
  buildPlayReadyPssh: () => buildPlayReadyPssh,
315
+ buildTfrfBox: () => buildTfrfBox,
316
+ buildTfxdBox: () => buildTfxdBox,
315
317
  buildWidevinePssh: () => buildWidevinePssh,
316
318
  canDecode: () => canDecode,
317
319
  canDecodeAudio: () => canDecodeAudio,
@@ -412,6 +414,7 @@ var Mediabunny = (() => {
412
414
  hlsVariant: () => hlsVariant,
413
415
  hlsVersionOf: () => hlsVersionOf,
414
416
  insertBoxesBeforeMoof: () => insertBoxesBeforeMoof,
417
+ insertBoxesIntoTraf: () => insertBoxesIntoTraf,
415
418
  isInitializationSegment: () => isInitializationSegment,
416
419
  iterateIsobmffBoxes: () => iterateIsobmffBoxes,
417
420
  keepCodecs: () => keepCodecs,
@@ -65266,6 +65269,220 @@ ${baseUrl}${p.getFileName()}`;
65266
65269
  return out;
65267
65270
  };
65268
65271
 
65272
+ // src/smooth-streaming.ts
65273
+ var UUID_TYPE = [117, 117, 105, 100];
65274
+ var TFXD_UUID = [
65275
+ 109,
65276
+ 29,
65277
+ 155,
65278
+ 5,
65279
+ 66,
65280
+ 213,
65281
+ 68,
65282
+ 230,
65283
+ 128,
65284
+ 226,
65285
+ 20,
65286
+ 29,
65287
+ 175,
65288
+ 247,
65289
+ 87,
65290
+ 178
65291
+ ];
65292
+ var TFRF_UUID = [
65293
+ 212,
65294
+ 128,
65295
+ 126,
65296
+ 242,
65297
+ 202,
65298
+ 57,
65299
+ 70,
65300
+ 149,
65301
+ 142,
65302
+ 84,
65303
+ 38,
65304
+ 203,
65305
+ 158,
65306
+ 70,
65307
+ 167,
65308
+ 159
65309
+ ];
65310
+ var uuidBox = (uuid, body) => Uint8Array.from([...u32Bytes(8 + uuid.length + body.length), ...UUID_TYPE, ...uuid, ...body]);
65311
+ var checkTime = (label, value) => {
65312
+ if (!Number.isSafeInteger(value) || value < 0) {
65313
+ throw new TypeError(`${label} must be a non-negative integer below 2^53.`);
65314
+ }
65315
+ };
65316
+ var buildTfxdBox = (timing) => {
65317
+ checkTime("time", timing.time);
65318
+ checkTime("duration", timing.duration);
65319
+ return uuidBox(TFXD_UUID, [1, 0, 0, 0, ...u64Bytes(timing.time), ...u64Bytes(timing.duration)]);
65320
+ };
65321
+ var buildTfrfBox = (fragments, options = {}) => {
65322
+ if (fragments.length === 0) {
65323
+ throw new InvalidUsageError("A tfrf box must announce at least one fragment.");
65324
+ }
65325
+ const countBytes = options.fragmentCountBytes ?? 1;
65326
+ if (countBytes !== 1 && countBytes !== 4) {
65327
+ throw new InvalidUsageError("fragmentCountBytes must be 1 or 4.");
65328
+ }
65329
+ if (countBytes === 1 && fragments.length > 255) {
65330
+ throw new InvalidUsageError("A one-byte fragment count cannot state more than 255 fragments.");
65331
+ }
65332
+ const body = [1, 0, 0, 0, ...countBytes === 1 ? [fragments.length] : u32Bytes(fragments.length)];
65333
+ for (const fragment of fragments) {
65334
+ checkTime("time", fragment.time);
65335
+ checkTime("duration", fragment.duration);
65336
+ body.push(...u64Bytes(fragment.time), ...u64Bytes(fragment.duration));
65337
+ }
65338
+ return uuidBox(TFRF_UUID, body);
65339
+ };
65340
+ var u32At = (data, offset) => (data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3]) >>> 0;
65341
+ var setU32At = (data, offset, value) => {
65342
+ data[offset] = value >>> 24 & 255;
65343
+ data[offset + 1] = value >>> 16 & 255;
65344
+ data[offset + 2] = value >>> 8 & 255;
65345
+ data[offset + 3] = value & 255;
65346
+ };
65347
+ var boxFlags2 = (data) => data[1] << 16 | data[2] << 8 | data[3];
65348
+ var TFHD_BASE_DATA_OFFSET_PRESENT = 1;
65349
+ var TFHD_DEFAULT_BASE_IS_MOOF = 131072;
65350
+ var TRUN_DATA_OFFSET_PRESENT2 = 1;
65351
+ var TRUN_DATA_OFFSET_POSITION2 = 8;
65352
+ var childrenOf = (box2) => box2.children ?? [];
65353
+ var trafsOf = (moof2) => childrenOf(moof2).filter((box2) => box2.type === "traf");
65354
+ var trackIdOf2 = (traf2) => {
65355
+ const tfhd2 = childrenOf(traf2).find((box2) => box2.type === "tfhd");
65356
+ return tfhd2?.data === void 0 ? null : u32At(tfhd2.data, 4);
65357
+ };
65358
+ var offsetsFollowMoof = (traf2, indexInMoof) => {
65359
+ const tfhd2 = childrenOf(traf2).find((box2) => box2.type === "tfhd");
65360
+ if (tfhd2?.data === void 0) {
65361
+ return indexInMoof === 0;
65362
+ }
65363
+ const flags = boxFlags2(tfhd2.data);
65364
+ if (flags & TFHD_BASE_DATA_OFFSET_PRESENT) {
65365
+ return false;
65366
+ }
65367
+ return (flags & TFHD_DEFAULT_BASE_IS_MOOF) !== 0 || indexInMoof === 0;
65368
+ };
65369
+ var shiftSaioOffsets = (traf2, added) => {
65370
+ for (const saio2 of childrenOf(traf2).filter((box2) => box2.type === "saio")) {
65371
+ const data = saio2.data;
65372
+ if (data === void 0) {
65373
+ continue;
65374
+ }
65375
+ const version = data[0];
65376
+ let position = 4 + (boxFlags2(data) & 1 ? 8 : 0);
65377
+ const entryCount = u32At(data, position);
65378
+ position += 4;
65379
+ const view2 = new DataView(data.buffer, data.byteOffset, data.byteLength);
65380
+ for (let i = 0; i < entryCount; i++) {
65381
+ if (version === 0) {
65382
+ setU32At(data, position, u32At(data, position) + added);
65383
+ position += 4;
65384
+ } else {
65385
+ view2.setBigUint64(position, view2.getBigUint64(position) + BigInt(added));
65386
+ position += 8;
65387
+ }
65388
+ }
65389
+ }
65390
+ };
65391
+ var growSidxReferencedSize = (sidx2, fragmentIndex, added) => {
65392
+ const data = sidx2.data;
65393
+ if (data === void 0) {
65394
+ return;
65395
+ }
65396
+ const version = data[0];
65397
+ const position = 4 + 4 + 4 + (version === 0 ? 8 : 16) + 2;
65398
+ const referenceCount = data[position] << 8 | data[position + 1];
65399
+ if (fragmentIndex >= referenceCount) {
65400
+ return;
65401
+ }
65402
+ const at = position + 2 + fragmentIndex * 12;
65403
+ const word = u32At(data, at);
65404
+ setU32At(data, at, word & 2147483648 | (word & 2147483647) + added & 2147483647);
65405
+ };
65406
+ var insertBoxesIntoTraf = (segment, trackId, boxes) => {
65407
+ if (boxes.length === 0) {
65408
+ return segment;
65409
+ }
65410
+ const inserted = boxes.map((bytes2) => {
65411
+ const boxList = parseBoxes(bytes2, 0, bytes2.length);
65412
+ if (boxList.length !== 1) {
65413
+ throw new InvalidUsageError("Each entry must be exactly one complete top-level box.");
65414
+ }
65415
+ return boxList[0];
65416
+ });
65417
+ const added = inserted.reduce((total, box2) => total + measureBox(box2), 0);
65418
+ const parsed = parseBoxes(segment, 0, segment.length);
65419
+ const sidxFirstFragment = /* @__PURE__ */ new Map();
65420
+ let fragmentIndex = -1;
65421
+ let target = null;
65422
+ for (const box2 of parsed) {
65423
+ if (box2.type === "sidx") {
65424
+ sidxFirstFragment.set(box2, fragmentIndex + 1);
65425
+ continue;
65426
+ }
65427
+ if (box2.type !== "moof") {
65428
+ continue;
65429
+ }
65430
+ fragmentIndex++;
65431
+ if (target !== null) {
65432
+ continue;
65433
+ }
65434
+ const traf3 = trafsOf(box2).find((candidate) => trackIdOf2(candidate) === trackId);
65435
+ if (traf3 !== void 0) {
65436
+ target = { moof: box2, traf: traf3 };
65437
+ }
65438
+ }
65439
+ if (target === null) {
65440
+ throw new MalformedInputError(`The segment has no traf for track ${trackId}.`);
65441
+ }
65442
+ const targetFragment = parsed.filter((box2) => box2.type === "moof").indexOf(target.moof);
65443
+ const { moof: moof2, traf: traf2 } = target;
65444
+ const children = childrenOf(traf2);
65445
+ const lastTrun = children.map((box2) => box2.type).lastIndexOf("trun");
65446
+ if (lastTrun === -1) {
65447
+ throw new MalformedInputError(`The traf for track ${trackId} has no trun to insert after.`);
65448
+ }
65449
+ traf2.children = [...children.slice(0, lastTrun + 1), ...inserted, ...children.slice(lastTrun + 1)];
65450
+ const trafs = trafsOf(moof2);
65451
+ const targetIndex = trafs.indexOf(traf2);
65452
+ for (const [index, other] of trafs.entries()) {
65453
+ if (!offsetsFollowMoof(other, index)) {
65454
+ continue;
65455
+ }
65456
+ for (const trun2 of childrenOf(other).filter((box2) => box2.type === "trun")) {
65457
+ if (trun2.data !== void 0 && boxFlags2(trun2.data) & TRUN_DATA_OFFSET_PRESENT2) {
65458
+ setU32At(
65459
+ trun2.data,
65460
+ TRUN_DATA_OFFSET_POSITION2,
65461
+ u32At(trun2.data, TRUN_DATA_OFFSET_POSITION2) + added
65462
+ );
65463
+ }
65464
+ }
65465
+ if (index >= targetIndex) {
65466
+ shiftSaioOffsets(other, added);
65467
+ }
65468
+ }
65469
+ for (const [sidx2, firstFragment] of sidxFirstFragment) {
65470
+ if (targetFragment >= firstFragment) {
65471
+ growSidxReferencedSize(sidx2, targetFragment - firstFragment, added);
65472
+ }
65473
+ }
65474
+ const out = serializeBoxes(parsed);
65475
+ let moofStart = 0;
65476
+ for (const box2 of parsed) {
65477
+ if (box2 === moof2) {
65478
+ break;
65479
+ }
65480
+ moofStart += measureBox(box2);
65481
+ }
65482
+ shiftBaseDataOffsets(out.subarray(moofStart), added);
65483
+ return out;
65484
+ };
65485
+
65269
65486
  // src/mpeg-ts/scte35.ts
65270
65487
  var SYNC_BYTE = 71;
65271
65488
  var SPLICE_INFO_TABLE_ID = 252;