@ahmedrowaihi/mediabunny 1.65.2-beta.556 → 1.65.2-beta.560

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.
@@ -65535,24 +65535,21 @@ ${baseUrl}${p.getFileName()}`;
65535
65535
  }
65536
65536
  }
65537
65537
  };
65538
- var growSidxReferencedSize = (sidx2, fragmentIndex, added) => {
65539
- const data = sidx2.data;
65540
- if (data === void 0) {
65541
- return;
65542
- }
65543
- const version = data[0];
65544
- const position = 4 + 4 + 4 + (version === 0 ? 8 : 16) + 2;
65545
- const referenceCount = data[position] << 8 | data[position + 1];
65538
+ var growSidxReferencedSizeAt = (bytes2, sidxStart, fragmentIndex, added) => {
65539
+ const content = sidxStart + 8;
65540
+ const version = bytes2[content];
65541
+ const position = content + 4 + 4 + 4 + (version === 0 ? 8 : 16) + 2;
65542
+ const referenceCount = bytes2[position] << 8 | bytes2[position + 1];
65546
65543
  if (fragmentIndex >= referenceCount) {
65547
65544
  return;
65548
65545
  }
65549
65546
  const at = position + 2 + fragmentIndex * 12;
65550
- const word = readU323(data, at);
65547
+ const word = readU323(bytes2, at);
65551
65548
  const grown = (word & 2147483647) + added;
65552
65549
  if (grown > 2147483647) {
65553
65550
  throw new InvalidUsageError("The grown fragment no longer fits a sidx referenced_size.");
65554
65551
  }
65555
- writeU32(data, at, word & 2147483648 | grown);
65552
+ writeU32(bytes2, at, word & 2147483648 | grown);
65556
65553
  };
65557
65554
  var insertBoxesIntoTraf = (segment, trackId, boxes) => {
65558
65555
  if (boxes.length === 0) {
@@ -65560,30 +65557,46 @@ ${baseUrl}${p.getFileName()}`;
65560
65557
  }
65561
65558
  const inserted = parseSingleBoxes(boxes);
65562
65559
  const added = inserted.reduce((total, box2) => total + measureBox(box2), 0);
65563
- const parsed = parseBoxes(segment, 0, segment.length);
65564
- const sidxes = [];
65565
65560
  let fragmentIndex = -1;
65566
- let start = 0;
65567
- let target = null;
65568
- for (const box2 of parsed) {
65561
+ let targetIndex = -1;
65562
+ let moofStart = -1;
65563
+ let moofEnd = -1;
65564
+ const sidxes = [];
65565
+ for (const box2 of iterateIsobmffBoxes(segment)) {
65569
65566
  if (box2.type === "sidx") {
65570
- sidxes.push({ sidx: box2, firstFragment: fragmentIndex + 1 });
65571
- } else if (box2.type === "moof") {
65572
- fragmentIndex++;
65573
- const traf3 = trafsOf(box2).find((candidate) => {
65574
- const tfhd2 = tfhdOf(candidate);
65575
- return tfhd2?.data !== void 0 && readU323(tfhd2.data, 4) === trackId;
65576
- });
65577
- if (traf3 !== void 0 && target === null) {
65578
- target = { moof: box2, traf: traf3, fragment: fragmentIndex, start };
65579
- }
65567
+ sidxes.push({ start: box2.start, firstFragment: fragmentIndex + 1 });
65568
+ continue;
65569
+ }
65570
+ if (box2.type !== "moof") {
65571
+ continue;
65572
+ }
65573
+ fragmentIndex++;
65574
+ if (moofStart !== -1) {
65575
+ continue;
65576
+ }
65577
+ const children2 = parseBoxes(segment, box2.start + box2.headerSize, box2.start + box2.size);
65578
+ const found = children2.filter((child) => child.type === "traf").findIndex((traf3) => {
65579
+ const tfhd2 = tfhdOf(traf3);
65580
+ return tfhd2?.data !== void 0 && readU323(tfhd2.data, 4) === trackId;
65581
+ });
65582
+ if (found !== -1) {
65583
+ moofStart = box2.start;
65584
+ moofEnd = box2.start + box2.size;
65585
+ targetIndex = fragmentIndex;
65580
65586
  }
65581
- start += measureBox(box2);
65582
65587
  }
65583
- if (target === null) {
65588
+ if (moofStart === -1) {
65584
65589
  throw new MalformedInputError(`The segment has no traf for track ${trackId}.`);
65585
65590
  }
65586
- const { moof: moof2, traf: traf2 } = target;
65591
+ const moof2 = {
65592
+ type: "moof",
65593
+ children: parseBoxes(segment, moofStart + 8, moofEnd)
65594
+ };
65595
+ const trafs = trafsOf(moof2);
65596
+ const traf2 = trafs.find((candidate) => {
65597
+ const tfhd2 = tfhdOf(candidate);
65598
+ return tfhd2?.data !== void 0 && readU323(tfhd2.data, 4) === trackId;
65599
+ });
65587
65600
  const children = traf2.children ?? [];
65588
65601
  const lastTrun = children.map((box2) => box2.type).lastIndexOf("trun");
65589
65602
  if (lastTrun === -1) {
@@ -65591,19 +65604,22 @@ ${baseUrl}${p.getFileName()}`;
65591
65604
  }
65592
65605
  traf2.children = [...children.slice(0, lastTrun + 1), ...inserted, ...children.slice(lastTrun + 1)];
65593
65606
  shiftTrunDataOffsets(moof2, added);
65594
- const trafs = trafsOf(moof2);
65595
65607
  for (let index = trafs.indexOf(traf2); index < trafs.length; index++) {
65596
65608
  if (offsetsFollowMoof(trafs[index], index)) {
65597
65609
  shiftSaioOffsets(trafs[index], added);
65598
65610
  }
65599
65611
  }
65600
- for (const { sidx: sidx2, firstFragment } of sidxes) {
65601
- if (target.fragment >= firstFragment) {
65602
- growSidxReferencedSize(sidx2, target.fragment - firstFragment, added);
65612
+ const rebuilt = serializeBoxes([moof2]);
65613
+ const out = new Uint8Array(segment.byteLength + added);
65614
+ out.set(segment.subarray(0, moofStart), 0);
65615
+ out.set(rebuilt, moofStart);
65616
+ out.set(segment.subarray(moofEnd), moofStart + rebuilt.byteLength);
65617
+ for (const { start, firstFragment } of sidxes) {
65618
+ if (targetIndex >= firstFragment) {
65619
+ growSidxReferencedSizeAt(out, start, targetIndex - firstFragment, added);
65603
65620
  }
65604
65621
  }
65605
- const out = serializeBoxes(parsed);
65606
- shiftBaseDataOffsets(out.subarray(target.start), added);
65622
+ shiftBaseDataOffsets(out.subarray(moofStart), added);
65607
65623
  return out;
65608
65624
  };
65609
65625