@ahmedrowaihi/mediabunny 1.65.2-beta.718 → 1.65.2-beta.720

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 (33) hide show
  1. package/dist/bundles/mediabunny.cjs +471 -244
  2. package/dist/bundles/mediabunny.min.cjs +75 -74
  3. package/dist/bundles/mediabunny.min.mjs +72 -71
  4. package/dist/bundles/mediabunny.mjs +471 -244
  5. package/dist/bundles/mediabunny.node.cjs +471 -244
  6. package/dist/mediabunny.d.ts +44 -0
  7. package/dist/modules/src/hls/hls-manifest-emitter.js +16 -3
  8. package/dist/modules/src/index.d.ts +1 -1
  9. package/dist/modules/src/index.d.ts.map +1 -1
  10. package/dist/modules/src/index.js +1 -1
  11. package/dist/modules/src/isobmff/isobmff-misc.d.ts +67 -0
  12. package/dist/modules/src/isobmff/isobmff-misc.d.ts.map +1 -1
  13. package/dist/modules/src/isobmff/isobmff-misc.js +193 -0
  14. package/dist/modules/src/isobmff/isobmff-muxer.d.ts +1 -0
  15. package/dist/modules/src/isobmff/isobmff-muxer.d.ts.map +1 -1
  16. package/dist/modules/src/isobmff/isobmff-muxer.js +41 -4
  17. package/dist/modules/src/mpeg-ts/scte35-output.d.ts.map +1 -1
  18. package/dist/modules/src/mpeg-ts/scte35-output.js +9 -1
  19. package/dist/modules/src/output-format.d.ts +11 -0
  20. package/dist/modules/src/output-format.d.ts.map +1 -1
  21. package/dist/modules/src/output-format.js +5 -0
  22. package/dist/modules/src/segment-pipeline-muxer.d.ts +14 -0
  23. package/dist/modules/src/segment-pipeline-muxer.d.ts.map +1 -1
  24. package/dist/modules/src/segment-pipeline-muxer.js +91 -0
  25. package/dist/modules/src/tsconfig.tsbuildinfo +1 -1
  26. package/package.json +1 -1
  27. package/src/hls/hls-manifest-emitter.ts +18 -3
  28. package/src/index.ts +1 -0
  29. package/src/isobmff/isobmff-misc.ts +237 -0
  30. package/src/isobmff/isobmff-muxer.ts +46 -4
  31. package/src/mpeg-ts/scte35-output.ts +12 -1
  32. package/src/output-format.ts +19 -0
  33. package/src/segment-pipeline-muxer.ts +117 -0
@@ -384,6 +384,7 @@ var Mediabunny = (() => {
384
384
  getSegmentCompositionOffset: () => getSegmentCompositionOffset,
385
385
  getSegmentDecodeTime: () => getSegmentDecodeTime,
386
386
  getSegmentName: () => getSegmentName,
387
+ getSegmentPresentationTime: () => getSegmentPresentationTime,
387
388
  getSegmentTrackIds: () => getSegmentTrackIds,
388
389
  getSidxDurationSeconds: () => getSidxDurationSeconds,
389
390
  getSidxIndexRange: () => getSidxIndexRange,
@@ -6964,6 +6965,23 @@ var Mediabunny = (() => {
6964
6965
  }
6965
6966
  return pos + 4 <= contentEnd ? view2.getUint32(pos) : 0;
6966
6967
  };
6968
+ var getSegmentPresentationTime = (segment, init) => {
6969
+ const trackId = getSegmentTrackIds(segment)[0];
6970
+ if (trackId === void 0) {
6971
+ throw new MalformedInputError("This segment carries no fragment, so it has no presentation time.");
6972
+ }
6973
+ const track = getInitSegmentTracks(init).find((candidate) => candidate.trackId === trackId);
6974
+ if (!track) {
6975
+ throw new MalformedInputError(
6976
+ `This init declares no track ${trackId}, which the segment's first fragment references, so it states no edit list for it.`
6977
+ );
6978
+ }
6979
+ const decodeTime = getSegmentDecodeTime(segment);
6980
+ if (decodeTime === null) {
6981
+ throw new MalformedInputError("This segment carries no `tfdt`, so it states no decode time to start from.");
6982
+ }
6983
+ return decodeTime + getSegmentCompositionOffset(segment, init) - track.editMediaTime;
6984
+ };
6967
6985
  var getSegmentCompositionOffset = (segment, init) => {
6968
6986
  for (const box2 of iterateIsobmffBoxes(segment)) {
6969
6987
  void box2;
@@ -7043,6 +7061,39 @@ var Mediabunny = (() => {
7043
7061
  });
7044
7062
  return earliest ?? 0;
7045
7063
  };
7064
+ var shiftSidxPresentationTimes = (out, deltaFor) => {
7065
+ const view2 = new DataView(out.buffer, out.byteOffset, out.byteLength);
7066
+ for (const box2 of iterateIsobmffBoxes(out)) {
7067
+ if (box2.type !== "sidx") {
7068
+ continue;
7069
+ }
7070
+ const contentStart = box2.start + box2.headerSize;
7071
+ const version = view2.getUint8(contentStart);
7072
+ const referenceId = view2.getUint32(contentStart + 4);
7073
+ const delta = deltaFor(referenceId);
7074
+ if (delta === void 0 || delta === 0n) {
7075
+ continue;
7076
+ }
7077
+ const fieldPos = contentStart + 12;
7078
+ const current = version === 0 ? BigInt(view2.getUint32(fieldPos)) : view2.getBigUint64(fieldPos);
7079
+ const next = current + delta;
7080
+ if (next < 0n) {
7081
+ throw new InvalidUsageError(
7082
+ `Moving by ${delta} takes the sidx's earliest_presentation_time from ${current} below zero.`
7083
+ );
7084
+ }
7085
+ if (version === 0) {
7086
+ if (next > 0xFFFFFFFFn) {
7087
+ throw new InvalidUsageError(
7088
+ `Moving by ${delta} takes the sidx's version 0 earliest_presentation_time from ${current} to ${next}, outside the 32 bits it can hold.`
7089
+ );
7090
+ }
7091
+ view2.setUint32(fieldPos, Number(next));
7092
+ } else {
7093
+ view2.setBigUint64(fieldPos, next);
7094
+ }
7095
+ }
7096
+ };
7046
7097
  var rebaseSegmentDecodeTime = (segment, deltaTicks) => {
7047
7098
  if (!Number.isInteger(deltaTicks)) {
7048
7099
  throw new InvalidUsageError(`deltaTicks must be an integer, got ${deltaTicks}.`);
@@ -7078,6 +7129,7 @@ var Mediabunny = (() => {
7078
7129
  }
7079
7130
  return false;
7080
7131
  });
7132
+ shiftSidxPresentationTimes(out, () => BigInt(deltaTicks));
7081
7133
  }
7082
7134
  return out;
7083
7135
  };
@@ -7421,6 +7473,46 @@ var Mediabunny = (() => {
7421
7473
  }
7422
7474
  return chunks;
7423
7475
  };
7476
+ var movieTimescale = (init, view2) => {
7477
+ for (const moov2 of iterateIsobmffBoxes(init)) {
7478
+ if (moov2.type !== "moov") {
7479
+ continue;
7480
+ }
7481
+ for (const box2 of childBoxes(init, moov2)) {
7482
+ if (box2.type === "mvhd") {
7483
+ return readFullBoxU32AfterTimes(init, view2, box2);
7484
+ }
7485
+ }
7486
+ }
7487
+ return null;
7488
+ };
7489
+ var editMediaTimeOf = (init, view2, edts2, timescale, movieTimescaleValue) => {
7490
+ for (const elst of childBoxes(init, edts2)) {
7491
+ if (elst.type !== "elst") {
7492
+ continue;
7493
+ }
7494
+ const at = elst.start + elst.headerSize;
7495
+ const version = view2.getUint8(at);
7496
+ const entryCount = view2.getUint32(at + 4);
7497
+ const entrySize = version === 1 ? 20 : 12;
7498
+ let emptyDuration = 0;
7499
+ let pos = at + 8;
7500
+ for (let i = 0; i < entryCount; i++) {
7501
+ if (pos + entrySize > elst.start + elst.size) {
7502
+ throw new MalformedInputError(`The 'elst' at offset ${elst.start} ends inside an entry.`);
7503
+ }
7504
+ const segmentDuration = version === 1 ? Number(view2.getBigUint64(pos)) : view2.getUint32(pos);
7505
+ const mediaTime = version === 1 ? Number(view2.getBigInt64(pos + 8)) : view2.getInt32(pos + 4);
7506
+ if (mediaTime < 0) {
7507
+ emptyDuration += movieTimescaleValue ? segmentDuration * timescale / movieTimescaleValue : 0;
7508
+ } else {
7509
+ return mediaTime - emptyDuration;
7510
+ }
7511
+ pos += entrySize;
7512
+ }
7513
+ }
7514
+ return 0;
7515
+ };
7424
7516
  var getInitSegmentTracks = (init) => {
7425
7517
  const view2 = new DataView(init.buffer, init.byteOffset, init.byteLength);
7426
7518
  const tracks = [];
@@ -7436,9 +7528,12 @@ var Mediabunny = (() => {
7436
7528
  let timescale = null;
7437
7529
  let handler = null;
7438
7530
  let channels = null;
7531
+ let edts2 = null;
7439
7532
  for (const box2 of childBoxes(init, trak2)) {
7440
7533
  if (box2.type === "tkhd") {
7441
7534
  trackId = readFullBoxU32AfterTimes(init, view2, box2);
7535
+ } else if (box2.type === "edts") {
7536
+ edts2 = box2;
7442
7537
  } else if (box2.type === "mdia") {
7443
7538
  for (const mdiaChild of childBoxes(init, box2)) {
7444
7539
  if (mdiaChild.type === "mdhd") {
@@ -7479,7 +7574,8 @@ var Mediabunny = (() => {
7479
7574
  trackId,
7480
7575
  timescale,
7481
7576
  handler,
7482
- channels: handler === "soun" ? channels : null
7577
+ channels: handler === "soun" ? channels : null,
7578
+ editMediaTime: edts2 ? editMediaTimeOf(init, view2, edts2, timescale, movieTimescale(init, view2)) : 0
7483
7579
  });
7484
7580
  }
7485
7581
  }
@@ -7573,6 +7669,7 @@ var Mediabunny = (() => {
7573
7669
  }
7574
7670
  }
7575
7671
  }
7672
+ shiftSidxPresentationTimes(out, (referenceId) => deltas.get(referenceId));
7576
7673
  return out;
7577
7674
  };
7578
7675
  var setSidxReferencedSizes = (bytes2, sizes) => {
@@ -43067,6 +43164,7 @@ var Mediabunny = (() => {
43067
43164
  * caller cutting the segments, not to any track, so it reaches the muxer directly.
43068
43165
  */
43069
43166
  this.subtitleSegmentWindow = null;
43167
+ this.seededDecodeAnchors = null;
43070
43168
  this.format = format;
43071
43169
  this.formatOptions = { ...format._options };
43072
43170
  this.isQuickTime = format instanceof MovOutputFormat;
@@ -43713,6 +43811,32 @@ var Mediabunny = (() => {
43713
43811
  };
43714
43812
  return sample;
43715
43813
  }
43814
+ /**
43815
+ * The decode anchor each track settled on, by track ID. A segmented output opens one muxer per
43816
+ * segment, and the anchor is a property of the **track**: worked out again per segment it comes out
43817
+ * as zero for every segment after the first, whose `elst` then presents them all that much early.
43818
+ *
43819
+ * @internal
43820
+ */
43821
+ _decodeAnchors() {
43822
+ const anchors = /* @__PURE__ */ new Map();
43823
+ if (!this.isFragmented) {
43824
+ return anchors;
43825
+ }
43826
+ for (const trackData of this.trackDatas) {
43827
+ if (trackData.firstDecodeTimestamp !== null && trackData.statesDecodeTimeline !== null) {
43828
+ anchors.set(trackData.track.id, {
43829
+ firstDecodeTimestamp: trackData.firstDecodeTimestamp,
43830
+ statesDecodeTimeline: trackData.statesDecodeTimeline
43831
+ });
43832
+ }
43833
+ }
43834
+ return anchors;
43835
+ }
43836
+ /** The anchors a previous segment of the same track settled on. @internal */
43837
+ _seedDecodeAnchors(anchors) {
43838
+ this.seededDecodeAnchors = anchors;
43839
+ }
43716
43840
  processTimestamps(trackData, nextSample) {
43717
43841
  if (trackData.timestampProcessingQueue.length === 0) {
43718
43842
  return;
@@ -43746,10 +43870,16 @@ var Mediabunny = (() => {
43746
43870
  trackData.startTimestampOffset ??= sortedTimestamps[0];
43747
43871
  }
43748
43872
  if (trackData.firstDecodeTimestamp === null) {
43749
- const stated = trackData.timestampProcessingQueue[0].decodeTimestamp;
43750
- trackData.statesDecodeTimeline ??= false;
43751
- const anchor = trackData.statesDecodeTimeline ? stated : sortedTimestamps[0];
43752
- trackData.firstDecodeTimestamp = this.isFragmented ? Math.min(anchor, 0) : anchor;
43873
+ const seeded = this.seededDecodeAnchors?.get(trackData.track.id);
43874
+ if (seeded) {
43875
+ trackData.statesDecodeTimeline = seeded.statesDecodeTimeline;
43876
+ trackData.firstDecodeTimestamp = seeded.firstDecodeTimestamp;
43877
+ } else {
43878
+ const stated = trackData.timestampProcessingQueue[0].decodeTimestamp;
43879
+ trackData.statesDecodeTimeline ??= false;
43880
+ const anchor = trackData.statesDecodeTimeline ? stated : sortedTimestamps[0];
43881
+ trackData.firstDecodeTimestamp = this.isFragmented ? Math.min(anchor, 0) : anchor;
43882
+ }
43753
43883
  }
43754
43884
  for (let i = 0; i < trackData.timestampProcessingQueue.length; i++) {
43755
43885
  const sample = trackData.timestampProcessingQueue[i];
@@ -51952,6 +52082,23 @@ ${cue.notes ?? ""}`;
51952
52082
  this.host = host;
51953
52083
  this.options = options;
51954
52084
  }
52085
+ /**
52086
+ * `#EXT-X-PROGRAM-DATE-TIME` for a segment starting at `timestamp`, or nothing where the playlist
52087
+ * has no wall clock to state. A stated `programDateTime` anchors the media's own timeline to it;
52088
+ * failing that, timestamps that are already epoch-relative are the clock themselves.
52089
+ */
52090
+ programDateTimeLine(timestamp) {
52091
+ const anchor = this.options.programDateTime;
52092
+ if (anchor) {
52093
+ return `#EXT-X-PROGRAM-DATE-TIME:${new Date(anchor.getTime() + 1e3 * timestamp).toISOString()}
52094
+ `;
52095
+ }
52096
+ if (this.host.isRelativeToUnixEpoch) {
52097
+ return `#EXT-X-PROGRAM-DATE-TIME:${new Date(1e3 * timestamp).toISOString()}
52098
+ `;
52099
+ }
52100
+ return "";
52101
+ }
51955
52102
  async onSegmentAppended(playlist) {
51956
52103
  await this.writePlaylist(playlist);
51957
52104
  if (this.host.isLive) {
@@ -52096,8 +52243,7 @@ ${cue.notes ?? ""}`;
52096
52243
  #EXT-X-PART-INF:PART-TARGET=${+partTarget.toFixed(12)}
52097
52244
  ` : "") + (Number.isFinite(this.host.maxLiveSegmentCount) ? `#EXT-X-MEDIA-SEQUENCE:${playlist.mediaSequence}
52098
52245
  ` : "") + "#EXT-X-INDEPENDENT-SEGMENTS\n" + (isKeyPacketsOnly ? "#EXT-X-I-FRAMES-ONLY\n" : "") + (playlist.initSegment ? `#EXT-X-MAP:URI="${playlist.initSegment.path}"` + (playlist.initSegment.byteOffset !== null ? `,BYTERANGE="${playlist.initSegment.byteSize}@${playlist.initSegment.byteOffset}"` : "") + "\n" : "") + "\n" + playlist.writtenSegments.map((segment) => (partsRetained(segment) ? partLines(segment) : "") + `#EXTINF:${+segment.duration.toFixed(12)},
52099
- ` + (this.host.isRelativeToUnixEpoch ? `#EXT-X-PROGRAM-DATE-TIME:${new Date(1e3 * segment.timestamp).toISOString()}
52100
- ` : "") + (segment.byteOffset !== null ? `#EXT-X-BYTERANGE:${segment.byteSize}@${segment.byteOffset}
52246
+ ` + this.programDateTimeLine(segment.timestamp) + (segment.byteOffset !== null ? `#EXT-X-BYTERANGE:${segment.byteSize}@${segment.byteOffset}
52101
52247
  ` : "") + `${segment.path}
52102
52248
  `).join("") + preloadHint + (playlist.done ? (playlist.writtenSegments.length > 0 ? "\n" : "") + "#EXT-X-ENDLIST\n" : "");
52103
52249
  this.options.onPlaylist?.(playlistText, this.toPlaylistInfo(playlist));
@@ -52326,6 +52472,208 @@ ${cue.notes ?? ""}`;
52326
52472
  }
52327
52473
  };
52328
52474
 
52475
+ // src/mpeg-ts/scte35.ts
52476
+ var SYNC_BYTE = 71;
52477
+ var SPLICE_INFO_TABLE_ID = 252;
52478
+ var CUEI_FORMAT_IDENTIFIER2 = 1129661769;
52479
+ var SCTE35_STREAM_TYPE2 = 134;
52480
+ var SPLICE_INSERT = 5;
52481
+ var PROGRAM_SPLICE_FLAG = 64;
52482
+ var SPLICE_IMMEDIATE_FLAG = 16;
52483
+ var TIME_SIGNAL = 6;
52484
+ var readU163 = (bytes2, offset) => bytes2[offset] << 8 | bytes2[offset + 1];
52485
+ var readU323 = (bytes2, offset) => (bytes2[offset] << 24 | bytes2[offset + 1] << 16 | bytes2[offset + 2] << 8 | bytes2[offset + 3]) >>> 0;
52486
+ var read33Bits = (bytes2, offset, lowBitsOfFirstByte) => {
52487
+ const high = bytes2[offset] & (1 << lowBitsOfFirstByte) - 1;
52488
+ return high * 2 ** 32 + readU323(bytes2, offset + 1);
52489
+ };
52490
+ var readScte35Sections = (bytes2) => {
52491
+ const sections = [];
52492
+ let pmtPid = null;
52493
+ let scte35Pid = null;
52494
+ const assembly = /* @__PURE__ */ new Map();
52495
+ let start = 0;
52496
+ while (start < bytes2.length && bytes2[start] !== SYNC_BYTE) {
52497
+ start++;
52498
+ }
52499
+ for (let offset = start; offset + TS_PACKET_SIZE <= bytes2.length; offset += TS_PACKET_SIZE) {
52500
+ if (bytes2[offset] !== SYNC_BYTE) {
52501
+ break;
52502
+ }
52503
+ const pid = readU163(bytes2, offset + 1) & 8191;
52504
+ const payloadUnitStart = (bytes2[offset + 1] & 64) !== 0;
52505
+ const hasAdaptation = (bytes2[offset + 3] & 32) !== 0;
52506
+ const hasPayload = (bytes2[offset + 3] & 16) !== 0;
52507
+ if (!hasPayload) {
52508
+ continue;
52509
+ }
52510
+ let payloadStart = offset + 4;
52511
+ if (hasAdaptation) {
52512
+ payloadStart += 1 + bytes2[payloadStart];
52513
+ }
52514
+ if (payloadStart >= offset + TS_PACKET_SIZE) {
52515
+ continue;
52516
+ }
52517
+ if (pid === PAT_PID) {
52518
+ pmtPid ??= readPmtPidFromPat(bytes2, payloadStart, offset + TS_PACKET_SIZE, payloadUnitStart);
52519
+ continue;
52520
+ }
52521
+ if (pmtPid !== null && pid === pmtPid) {
52522
+ scte35Pid ??= readScte35PidFromPmt(bytes2, payloadStart, offset + TS_PACKET_SIZE, payloadUnitStart);
52523
+ continue;
52524
+ }
52525
+ if (scte35Pid === null || pid !== scte35Pid) {
52526
+ continue;
52527
+ }
52528
+ collectSection(bytes2, payloadStart, offset + TS_PACKET_SIZE, payloadUnitStart, pid, assembly, sections);
52529
+ }
52530
+ return sections;
52531
+ };
52532
+ var sectionStart = (bytes2, payloadStart, payloadEnd) => {
52533
+ const pointerField = bytes2[payloadStart];
52534
+ const start = payloadStart + 1 + pointerField;
52535
+ return start < payloadEnd ? start : null;
52536
+ };
52537
+ var readPmtPidFromPat = (bytes2, payloadStart, payloadEnd, payloadUnitStart) => {
52538
+ if (!payloadUnitStart) {
52539
+ return null;
52540
+ }
52541
+ const start = sectionStart(bytes2, payloadStart, payloadEnd);
52542
+ if (start === null || start + 12 > payloadEnd) {
52543
+ return null;
52544
+ }
52545
+ for (let pos = start + 8; pos + 4 <= payloadEnd; pos += 4) {
52546
+ const programNumber = readU163(bytes2, pos);
52547
+ const pid = readU163(bytes2, pos + 2) & 8191;
52548
+ if (programNumber !== 0) {
52549
+ return pid;
52550
+ }
52551
+ }
52552
+ return null;
52553
+ };
52554
+ var readScte35PidFromPmt = (bytes2, payloadStart, payloadEnd, payloadUnitStart) => {
52555
+ if (!payloadUnitStart) {
52556
+ return null;
52557
+ }
52558
+ const start = sectionStart(bytes2, payloadStart, payloadEnd);
52559
+ if (start === null || start + 12 > payloadEnd) {
52560
+ return null;
52561
+ }
52562
+ const sectionLength = readU163(bytes2, start + 1) & 4095;
52563
+ const sectionEnd = Math.min(start + 3 + sectionLength, payloadEnd);
52564
+ const programInfoLength = readU163(bytes2, start + 10) & 4095;
52565
+ let pos = start + 12 + programInfoLength;
52566
+ while (pos + 5 <= sectionEnd - 4) {
52567
+ const streamType = bytes2[pos];
52568
+ const pid = readU163(bytes2, pos + 1) & 8191;
52569
+ const esInfoLength = readU163(bytes2, pos + 3) & 4095;
52570
+ const descriptorsEnd = Math.min(pos + 5 + esInfoLength, sectionEnd);
52571
+ if (streamType === SCTE35_STREAM_TYPE2) {
52572
+ for (let d = pos + 5; d + 2 <= descriptorsEnd; ) {
52573
+ const tag = bytes2[d];
52574
+ const length = bytes2[d + 1];
52575
+ if (tag === REGISTRATION_DESCRIPTOR_TAG && length >= 4 && d + 6 <= descriptorsEnd && readU323(bytes2, d + 2) === CUEI_FORMAT_IDENTIFIER2) {
52576
+ return pid;
52577
+ }
52578
+ d += 2 + length;
52579
+ }
52580
+ }
52581
+ pos = descriptorsEnd;
52582
+ }
52583
+ return null;
52584
+ };
52585
+ var collectSection = (bytes2, payloadStart, payloadEnd, payloadUnitStart, pid, assembly, out) => {
52586
+ let pos = payloadStart;
52587
+ if (payloadUnitStart) {
52588
+ const start = sectionStart(bytes2, payloadStart, payloadEnd);
52589
+ if (start === null) {
52590
+ return;
52591
+ }
52592
+ if (bytes2[start] !== SPLICE_INFO_TABLE_ID) {
52593
+ assembly.delete(pid);
52594
+ return;
52595
+ }
52596
+ if (start + 3 > payloadEnd) {
52597
+ return;
52598
+ }
52599
+ const sectionLength = readU163(bytes2, start + 1) & 4095;
52600
+ assembly.set(pid, { bytes: [], remaining: 3 + sectionLength });
52601
+ pos = start;
52602
+ }
52603
+ const pending = assembly.get(pid);
52604
+ if (!pending) {
52605
+ return;
52606
+ }
52607
+ const take = Math.min(pending.remaining, payloadEnd - pos);
52608
+ for (let i = 0; i < take; i++) {
52609
+ pending.bytes.push(bytes2[pos + i]);
52610
+ }
52611
+ pending.remaining -= take;
52612
+ if (pending.remaining === 0) {
52613
+ assembly.delete(pid);
52614
+ out.push(parseSpliceInfoSection(Uint8Array.from(pending.bytes)));
52615
+ }
52616
+ };
52617
+ var parseSpliceInfoSection = (section) => {
52618
+ if (section.length < 14) {
52619
+ throw new MalformedInputError("SCTE-35 section is too short to hold its header.");
52620
+ }
52621
+ if (section[0] !== SPLICE_INFO_TABLE_ID) {
52622
+ throw new MalformedInputError(
52623
+ `SCTE-35 section states table_id 0x${section[0].toString(16)}, expected 0xfc.`
52624
+ );
52625
+ }
52626
+ const ptsAdjustment = read33Bits(section, 4, 1);
52627
+ const encrypted = (section[4] & 128) !== 0;
52628
+ const commandLength = readU163(section, 11) & 4095;
52629
+ const commandType = section[13];
52630
+ let commandPos = 14;
52631
+ const wrap = (ticks) => ticks % TIMESTAMP_MODULUS / MPEG_TS_TIMESCALE;
52632
+ const noTime = { section, time: null, commandType, ptsAdjustment };
52633
+ if (encrypted || commandLength === 0) {
52634
+ return noTime;
52635
+ }
52636
+ if (commandType === TIME_SIGNAL) {
52637
+ if (commandPos >= section.length) {
52638
+ return noTime;
52639
+ }
52640
+ const timeSpecified2 = (section[commandPos] & 128) !== 0;
52641
+ if (!timeSpecified2 || commandPos + 5 > section.length) {
52642
+ return noTime;
52643
+ }
52644
+ return { section, time: wrap(ptsAdjustment + read33Bits(section, commandPos, 1)), commandType, ptsAdjustment };
52645
+ }
52646
+ if (commandType !== SPLICE_INSERT) {
52647
+ return noTime;
52648
+ }
52649
+ commandPos += 4;
52650
+ if (commandPos >= section.length) {
52651
+ return noTime;
52652
+ }
52653
+ if ((section[commandPos] & 128) !== 0) {
52654
+ return noTime;
52655
+ }
52656
+ commandPos += 1;
52657
+ if (commandPos >= section.length) {
52658
+ return noTime;
52659
+ }
52660
+ const flags = section[commandPos];
52661
+ const programSplice = (flags & PROGRAM_SPLICE_FLAG) !== 0;
52662
+ const spliceImmediate = (flags & SPLICE_IMMEDIATE_FLAG) !== 0;
52663
+ commandPos += 1;
52664
+ if (spliceImmediate || !programSplice) {
52665
+ return noTime;
52666
+ }
52667
+ if (commandPos >= section.length) {
52668
+ return noTime;
52669
+ }
52670
+ const timeSpecified = (section[commandPos] & 128) !== 0;
52671
+ if (!timeSpecified || commandPos + 5 > section.length) {
52672
+ return noTime;
52673
+ }
52674
+ return { section, time: wrap(ptsAdjustment + read33Bits(section, commandPos, 1)), commandType, ptsAdjustment };
52675
+ };
52676
+
52329
52677
  // src/segment-pipeline-muxer.ts
52330
52678
  var SUBTITLE_GROUP_ID = "subtitles";
52331
52679
  var segmentVideoMetadata = (videoTrack) => ({
@@ -52398,11 +52746,18 @@ ${cue.notes ?? ""}`;
52398
52746
  this.trackDatas = [];
52399
52747
  this.firstPacketTimestamp = null;
52400
52748
  this.isRelativeToUnixEpoch = false;
52749
+ /**
52750
+ * What each track settled on in the segment it first appeared in, so every later segment states the
52751
+ * same decode timeline. Filled per track rather than taken whole from the first segment: the tracks
52752
+ * of one segment settle as their own queues flush, so one of them can be known while another is not.
52753
+ */
52754
+ this.decodeAnchors = /* @__PURE__ */ new Map();
52401
52755
  this.numWrittenMasterPlaylists = 0;
52402
52756
  this.playlists = [];
52403
52757
  this.playlistDeclarations = [];
52404
52758
  this.subtitlePlaylists = [];
52405
52759
  this.subtitleBoundarySource = null;
52760
+ this.pendingScte35 = [];
52406
52761
  this.options = options;
52407
52762
  this.targetSegmentDuration = options.targetDuration ?? 2;
52408
52763
  this.singleFilePerPlaylist = options.singleFilePerPlaylist ?? false;
@@ -53255,6 +53610,9 @@ ${cue.notes ?? ""}`;
53255
53610
  await this.startInnerOutput(output, privTags);
53256
53611
  }
53257
53612
  const muxer = output._muxer;
53613
+ if (muxer instanceof IsobmffMuxer2) {
53614
+ muxer._seedDecodeAnchors(this.decodeAnchors);
53615
+ }
53258
53616
  const partDuration = muxer instanceof IsobmffMuxer2 && muxer.isFragmented ? this.options.partDuration : void 0;
53259
53617
  maxEndTimestamp = partDuration === void 0 ? await this.addSegmentPackets(
53260
53618
  videoTrack,
@@ -53273,11 +53631,26 @@ ${cue.notes ?? ""}`;
53273
53631
  partStarts,
53274
53632
  partEnds
53275
53633
  );
53634
+ const firstPacket = videoTrack?.packets[0] ?? audioTrack?.packets[0];
53635
+ if (firstPacket && Number.isFinite(maxEndTimestamp)) {
53636
+ this.flushScte35Into(
53637
+ muxer,
53638
+ this.toMediaTimestamp(firstPacket.timestamp),
53639
+ this.toMediaTimestamp(maxEndTimestamp)
53640
+ );
53641
+ }
53276
53642
  if (playlist.singleFile?.fragmentedIsobmffOutput) {
53277
53643
  segmentSize = await finalizeFragmentedIsobmffSegment(playlist);
53278
53644
  } else {
53279
53645
  await output.finalize();
53280
53646
  }
53647
+ if (muxer instanceof IsobmffMuxer2) {
53648
+ for (const [trackId, anchor] of muxer._decodeAnchors()) {
53649
+ if (!this.decodeAnchors.has(trackId)) {
53650
+ this.decodeAnchors.set(trackId, anchor);
53651
+ }
53652
+ }
53653
+ }
53281
53654
  } catch (e) {
53282
53655
  await output?.cancel();
53283
53656
  throw e;
@@ -53695,6 +54068,47 @@ ${cue.notes ?? ""}`;
53695
54068
  }
53696
54069
  return packet.clone({ timestamp: this.toMediaTimestamp(packet.timestamp) });
53697
54070
  }
54071
+ /**
54072
+ * Hands every queued SCTE-35 section whose splice point this segment covers to the segment's own
54073
+ * muxer, so the section travels in the segment a receiver reaches it by. A section stating no time -
54074
+ * a `splice_null` heartbeat, a cancelled insert, an immediate splice - goes into the first segment
54075
+ * written after it was handed over, which is what "now" means to a packager.
54076
+ */
54077
+ flushScte35Into(muxer, start, end) {
54078
+ if (this.pendingScte35.length === 0) {
54079
+ return;
54080
+ }
54081
+ if (!(muxer instanceof MpegTsMuxer)) {
54082
+ this.pendingScte35 = [];
54083
+ return;
54084
+ }
54085
+ const kept = [];
54086
+ for (const pending of this.pendingScte35) {
54087
+ if (pending.mediaTime !== null && pending.mediaTime >= end) {
54088
+ kept.push(pending);
54089
+ continue;
54090
+ }
54091
+ if (pending.mediaTime !== null && pending.mediaTime < start - (end - start)) {
54092
+ Logging._warn(
54093
+ `An SCTE-35 section states a splice at ${pending.mediaTime}s, which is more than one segment behind the segment being written (${start}s to ${end}s). It is written here anyway. Splice times are on the output's own media timeline, which mediaTimestampBase shifts - a time on the input's timeline lands early by exactly that.`
54094
+ );
54095
+ }
54096
+ muxer.writeScte35Section(pending.section);
54097
+ }
54098
+ this.pendingScte35 = kept;
54099
+ }
54100
+ /**
54101
+ * Queues a `splice_info_section` for the segment covering its splice point.
54102
+ *
54103
+ * A segmented output has no one transport stream to write into - it opens a muxer per segment - so
54104
+ * the section waits here until the segment its time falls in is written.
54105
+ *
54106
+ * @internal
54107
+ */
54108
+ writeScte35Section(section) {
54109
+ const parsed = parseSpliceInfoSection(section);
54110
+ this.pendingScte35.push({ section, mediaTime: parsed.time });
54111
+ }
53698
54112
  toMediaTimestamp(timestamp) {
53699
54113
  assert(this.firstPacketTimestamp !== null);
53700
54114
  return timestamp - this.firstPacketTimestamp + this.mediaTimestampBase;
@@ -53724,6 +54138,12 @@ ${cue.notes ?? ""}`;
53724
54138
  }
53725
54139
  await Promise.all(this.playlists.map((playlist) => playlist.done || playlist.subtitleBoundaries ? Promise.resolve() : this.advancePlaylist(playlist)));
53726
54140
  await Promise.all(this.subtitlePlaylists.map((playlist) => playlist.done ? Promise.resolve() : this.advanceSubtitlePlaylist(playlist)));
54141
+ if (this.pendingScte35.length > 0) {
54142
+ Logging._warn(
54143
+ `${this.pendingScte35.length} SCTE-35 section(s) were never written: their splice times (${this.pendingScte35.map((p) => p.mediaTime).join(", ")}) fall past the end of the media this output wrote. Splice times are on the output's own media timeline, which mediaTimestampBase shifts.`
54144
+ );
54145
+ this.pendingScte35 = [];
54146
+ }
53727
54147
  await this.broadcast("onFinalize");
53728
54148
  }
53729
54149
  };
@@ -54667,6 +55087,9 @@ ${cue.text}
54667
55087
  if (options.getInitPath !== void 0 && typeof options.getInitPath !== "function") {
54668
55088
  throw new TypeError("options.getInitPath, when provided, must be a function.");
54669
55089
  }
55090
+ if (options.programDateTime !== void 0 && options.programDateTime !== null && (!(options.programDateTime instanceof Date) || Number.isNaN(options.programDateTime.getTime()))) {
55091
+ throw new TypeError("options.programDateTime, when provided, must be a valid Date or null.");
55092
+ }
54670
55093
  if (options.onMaster !== void 0 && typeof options.onMaster !== "function") {
54671
55094
  throw new TypeError("options.onMaster, when provided, must be a function.");
54672
55095
  }
@@ -59973,7 +60396,7 @@ ${baseUrl}${p.getFileName()}`;
59973
60396
  mp4a: 28,
59974
60397
  enca: 28
59975
60398
  };
59976
- var readU323 = (bytes2, offset) => (bytes2[offset] << 24 | bytes2[offset + 1] << 16 | bytes2[offset + 2] << 8 | bytes2[offset + 3]) >>> 0;
60399
+ var readU324 = (bytes2, offset) => (bytes2[offset] << 24 | bytes2[offset + 1] << 16 | bytes2[offset + 2] << 8 | bytes2[offset + 3]) >>> 0;
59977
60400
  var writeU32 = (bytes2, offset, value) => {
59978
60401
  bytes2[offset] = value >>> 24 & 255;
59979
60402
  bytes2[offset + 1] = value >>> 16 & 255;
@@ -60006,7 +60429,7 @@ ${baseUrl}${p.getFileName()}`;
60006
60429
  var trafsOf = (moof2) => (moof2.children ?? []).filter((box2) => box2.type === "traf");
60007
60430
  var trunsOf = (traf2) => (traf2.children ?? []).filter((box2) => box2.type === "trun");
60008
60431
  var tfhdOf = (traf2) => (traf2.children ?? []).find((box2) => box2.type === "tfhd");
60009
- var trunDataOffset = (data) => readU323(data, TRUN_DATA_OFFSET_POSITION) | 0;
60432
+ var trunDataOffset = (data) => readU324(data, TRUN_DATA_OFFSET_POSITION) | 0;
60010
60433
  var defaultSampleSizeOf = (tfhd2) => {
60011
60434
  const flags = boxFlags(tfhd2);
60012
60435
  if (!(flags & TFHD_DEFAULT_SAMPLE_SIZE_PRESENT)) {
@@ -60022,7 +60445,7 @@ ${baseUrl}${p.getFileName()}`;
60022
60445
  if (flags & TFHD_DEFAULT_SAMPLE_DURATION_PRESENT) {
60023
60446
  offset += 4;
60024
60447
  }
60025
- return readU323(tfhd2, offset);
60448
+ return readU324(tfhd2, offset);
60026
60449
  };
60027
60450
  var trafDataSpan = (traf2) => {
60028
60451
  const tfhd2 = tfhdOf(traf2);
@@ -60039,14 +60462,14 @@ ${baseUrl}${p.getFileName()}`;
60039
60462
  cursor = trunDataOffset(data);
60040
60463
  }
60041
60464
  start = Math.min(start, cursor);
60042
- const count = readU323(data, 4);
60465
+ const count = readU324(data, 4);
60043
60466
  let offset = 8 + (flags & TRUN_DATA_OFFSET_PRESENT ? 4 : 0) + (flags & TRUN_FIRST_SAMPLE_FLAGS_PRESENT ? 4 : 0);
60044
60467
  for (let i = 0; i < count; i++) {
60045
60468
  if (flags & TRUN_SAMPLE_DURATION_PRESENT) {
60046
60469
  offset += 4;
60047
60470
  }
60048
60471
  if (flags & TRUN_SAMPLE_SIZE_PRESENT) {
60049
- cursor += readU323(data, offset);
60472
+ cursor += readU324(data, offset);
60050
60473
  offset += 4;
60051
60474
  } else {
60052
60475
  cursor += defaultSampleSize;
@@ -60080,7 +60503,7 @@ ${baseUrl}${p.getFileName()}`;
60080
60503
  base = previousEnd !== moofOffset && !fits(previousEnd) && fits(moofOffset) ? moofOffset : previousEnd;
60081
60504
  }
60082
60505
  resolved.push({
60083
- trackId: data === void 0 ? 0 : readU323(data, 4),
60506
+ trackId: data === void 0 ? 0 : readU324(data, 4),
60084
60507
  base,
60085
60508
  // Only a base that is the moof moves what its offsets point at when that moof grows
60086
60509
  followsMoof: base === moofOffset
@@ -60100,7 +60523,7 @@ ${baseUrl}${p.getFileName()}`;
60100
60523
  writeU32(
60101
60524
  trun2.data,
60102
60525
  TRUN_DATA_OFFSET_POSITION,
60103
- readU323(trun2.data, TRUN_DATA_OFFSET_POSITION) + delta
60526
+ readU324(trun2.data, TRUN_DATA_OFFSET_POSITION) + delta
60104
60527
  );
60105
60528
  }
60106
60529
  }
@@ -64403,7 +64826,7 @@ ${baseUrl}${p.getFileName()}`;
64403
64826
  var SENC_ENTRIES_OFFSET = 8 + 4 + 4;
64404
64827
  var parseTrun = (data, defaultSampleSize, defaultSampleDuration) => {
64405
64828
  const flags = boxFlags(data);
64406
- const sampleCount = readU323(data, 4);
64829
+ const sampleCount = readU324(data, 4);
64407
64830
  let offset = 8;
64408
64831
  let dataOffsetPos = -1;
64409
64832
  if (flags & 1) {
@@ -64418,13 +64841,13 @@ ${baseUrl}${p.getFileName()}`;
64418
64841
  for (let i = 0; i < sampleCount; i++) {
64419
64842
  let duration = defaultSampleDuration;
64420
64843
  if (flags & 256) {
64421
- duration = readU323(data, offset);
64844
+ duration = readU324(data, offset);
64422
64845
  offset += 4;
64423
64846
  }
64424
64847
  durations.push(duration);
64425
64848
  let size = defaultSampleSize;
64426
64849
  if (flags & 512) {
64427
- size = readU323(data, offset);
64850
+ size = readU324(data, offset);
64428
64851
  offset += 4;
64429
64852
  }
64430
64853
  sizes.push(size);
@@ -64476,8 +64899,8 @@ ${baseUrl}${p.getFileName()}`;
64476
64899
  var trexSampleDuration = (moov2, trackId) => {
64477
64900
  const mvex2 = (moov2.children ?? []).find((box2) => box2.type === "mvex");
64478
64901
  for (const trex2 of (mvex2?.children ?? []).filter((box2) => box2.type === "trex")) {
64479
- if (trex2.data !== void 0 && readU323(trex2.data, 4) === trackId) {
64480
- return readU323(trex2.data, 12);
64902
+ if (trex2.data !== void 0 && readU324(trex2.data, 4) === trackId) {
64903
+ return readU324(trex2.data, 12);
64481
64904
  }
64482
64905
  }
64483
64906
  return 0;
@@ -64495,7 +64918,7 @@ ${baseUrl}${p.getFileName()}`;
64495
64918
  if (tkhd2?.data === void 0 || sampleEntry === void 0) {
64496
64919
  continue;
64497
64920
  }
64498
- const trackId = readU323(tkhd2.data, tkhd2.data[0] === 1 ? 20 : 12);
64921
+ const trackId = readU324(tkhd2.data, tkhd2.data[0] === 1 ? 20 : 12);
64499
64922
  const defaultSampleDuration = trexSampleDuration(moov2, trackId);
64500
64923
  if (handlerType === "vide") {
64501
64924
  tracks.push({
@@ -64822,7 +65245,7 @@ ${baseUrl}${p.getFileName()}`;
64822
65245
  const referenceCount = view2.getUint16(offset + 2);
64823
65246
  offset += 4;
64824
65247
  for (let i = 0; i < referenceCount; i++) {
64825
- const raw = readU323(data, offset);
65248
+ const raw = readU324(data, offset);
64826
65249
  const next = cursor + (raw & 2147483647);
64827
65250
  if (next > endOfInput) {
64828
65251
  Logging._warn(
@@ -64916,7 +65339,7 @@ ${baseUrl}${p.getFileName()}`;
64916
65339
  };
64917
65340
  var findTrafByTrackId = (moof2, trackId) => (moof2.children ?? []).filter((b) => b.type === "traf").find((traf2) => {
64918
65341
  const tfhd2 = findBox([traf2], "tfhd");
64919
- return tfhd2?.data !== void 0 && readU323(tfhd2.data, 4) === trackId;
65342
+ return tfhd2?.data !== void 0 && readU324(tfhd2.data, 4) === trackId;
64920
65343
  });
64921
65344
  var defaultSampleSizeOf2 = (tfhd2) => {
64922
65345
  const flags = boxFlags(tfhd2.data);
@@ -64930,9 +65353,9 @@ ${baseUrl}${p.getFileName()}`;
64930
65353
  if (flags & 8) {
64931
65354
  offset += 4;
64932
65355
  }
64933
- return flags & 16 ? readU323(tfhd2.data, offset) : 0;
65356
+ return flags & 16 ? readU324(tfhd2.data, offset) : 0;
64934
65357
  };
64935
- var trunDataOffset2 = (trun2, position) => readU323(trun2.data, position) | 0;
65358
+ var trunDataOffset2 = (trun2, position) => readU324(trun2.data, position) | 0;
64936
65359
  var cmafEncryptedLayout = (init, segment, options) => {
64937
65360
  const scheme = options.scheme ?? "cbcs";
64938
65361
  const tracks = requireTracks(requireMoov(parseBoxes(init, 0, init.length)));
@@ -65048,7 +65471,7 @@ ${baseUrl}${p.getFileName()}`;
65048
65471
  for (const traf2 of (moof2.children ?? []).filter((b) => b.type === "traf")) {
65049
65472
  for (const trun2 of trunsOf(traf2)) {
65050
65473
  if (boxFlags(trun2.data) & TRUN_DATA_OFFSET_PRESENT) {
65051
- writeU32(trun2.data, TRUN_DATA_OFFSET_POSITION, readU323(trun2.data, TRUN_DATA_OFFSET_POSITION) + delta);
65474
+ writeU32(trun2.data, TRUN_DATA_OFFSET_POSITION, readU324(trun2.data, TRUN_DATA_OFFSET_POSITION) + delta);
65052
65475
  }
65053
65476
  }
65054
65477
  }
@@ -65070,9 +65493,9 @@ ${baseUrl}${p.getFileName()}`;
65070
65493
  var sampleSizes = (stbl2) => {
65071
65494
  const stsz2 = findBox([stbl2], "stsz");
65072
65495
  if (stsz2?.data !== void 0) {
65073
- const uniform = readU323(stsz2.data, 4);
65074
- const count2 = readU323(stsz2.data, 8);
65075
- return Array.from({ length: count2 }, (_, i) => uniform !== 0 ? uniform : readU323(stsz2.data, 12 + i * 4));
65496
+ const uniform = readU324(stsz2.data, 4);
65497
+ const count2 = readU324(stsz2.data, 8);
65498
+ return Array.from({ length: count2 }, (_, i) => uniform !== 0 ? uniform : readU324(stsz2.data, 12 + i * 4));
65076
65499
  }
65077
65500
  const stz2 = findBox([stbl2], "stz2");
65078
65501
  if (stz2?.data === void 0) {
@@ -65082,7 +65505,7 @@ ${baseUrl}${p.getFileName()}`;
65082
65505
  }
65083
65506
  const data = stz2.data;
65084
65507
  const fieldSize = data[7];
65085
- const count = readU323(data, 8);
65508
+ const count = readU324(data, 8);
65086
65509
  const view2 = new DataView(data.buffer, data.byteOffset, data.byteLength);
65087
65510
  if (fieldSize === 16) {
65088
65511
  return Array.from({ length: count }, (_, i) => view2.getUint16(12 + i * 2));
@@ -65107,12 +65530,12 @@ ${baseUrl}${p.getFileName()}`;
65107
65530
  "The sample table has no stsc, so its samples cannot be located in their chunks."
65108
65531
  );
65109
65532
  }
65110
- const entryCount = readU323(stsc2, 4);
65533
+ const entryCount = readU324(stsc2, 4);
65111
65534
  const counts = [];
65112
65535
  for (let i = 0; i < entryCount; i++) {
65113
- const firstChunk = readU323(stsc2, 8 + i * 12);
65114
- const perChunk = readU323(stsc2, 12 + i * 12);
65115
- const nextFirstChunk = i + 1 < entryCount ? readU323(stsc2, 8 + (i + 1) * 12) : chunkCount + 1;
65536
+ const firstChunk = readU324(stsc2, 8 + i * 12);
65537
+ const perChunk = readU324(stsc2, 12 + i * 12);
65538
+ const nextFirstChunk = i + 1 < entryCount ? readU324(stsc2, 8 + (i + 1) * 12) : chunkCount + 1;
65116
65539
  for (let chunk = firstChunk; chunk < nextFirstChunk; chunk++) {
65117
65540
  counts.push(perChunk);
65118
65541
  }
@@ -65130,13 +65553,13 @@ ${baseUrl}${p.getFileName()}`;
65130
65553
  }
65131
65554
  const wide = stco2 === null;
65132
65555
  const view2 = new DataView(box2.data.buffer, box2.data.byteOffset, box2.data.byteLength);
65133
- const count = readU323(box2.data, 4);
65556
+ const count = readU324(box2.data, 4);
65134
65557
  return {
65135
65558
  box: box2,
65136
65559
  wide,
65137
65560
  offsets: Array.from(
65138
65561
  { length: count },
65139
- (_, i) => wide ? Number(view2.getBigUint64(8 + i * 8)) : readU323(box2.data, 8 + i * 4)
65562
+ (_, i) => wide ? Number(view2.getBigUint64(8 + i * 8)) : readU324(box2.data, 8 + i * 4)
65140
65563
  )
65141
65564
  };
65142
65565
  };
@@ -67121,12 +67544,12 @@ ${baseUrl}${p.getFileName()}`;
67121
67544
  }
67122
67545
  const version = data[0];
67123
67546
  let position = 4 + (boxFlags(data) & 1 ? 8 : 0);
67124
- const entryCount = readU323(data, position);
67547
+ const entryCount = readU324(data, position);
67125
67548
  position += 4;
67126
67549
  const view2 = new DataView(data.buffer, data.byteOffset, data.byteLength);
67127
67550
  for (let i = 0; i < entryCount; i++) {
67128
67551
  if (version === 0) {
67129
- writeU32(data, position, readU323(data, position) + added);
67552
+ writeU32(data, position, readU324(data, position) + added);
67130
67553
  position += 4;
67131
67554
  } else {
67132
67555
  view2.setBigUint64(position, view2.getBigUint64(position) + BigInt(added));
@@ -67144,7 +67567,7 @@ ${baseUrl}${p.getFileName()}`;
67144
67567
  return;
67145
67568
  }
67146
67569
  const at = position + 2 + fragmentIndex * 12;
67147
- const word = readU323(bytes2, at);
67570
+ const word = readU324(bytes2, at);
67148
67571
  const grown = (word & 2147483647) + added;
67149
67572
  if (grown > 2147483647) {
67150
67573
  throw new InvalidUsageError("The grown fragment no longer fits a sidx referenced_size.");
@@ -67177,7 +67600,7 @@ ${baseUrl}${p.getFileName()}`;
67177
67600
  const children2 = parseBoxes(segment, box2.start + box2.headerSize, box2.start + box2.size);
67178
67601
  const found = children2.filter((child) => child.type === "traf").findIndex((traf3) => {
67179
67602
  const tfhd2 = tfhdOf(traf3);
67180
- return tfhd2?.data !== void 0 && readU323(tfhd2.data, 4) === trackId;
67603
+ return tfhd2?.data !== void 0 && readU324(tfhd2.data, 4) === trackId;
67181
67604
  });
67182
67605
  if (found !== -1) {
67183
67606
  moofStart = box2.start;
@@ -67195,7 +67618,7 @@ ${baseUrl}${p.getFileName()}`;
67195
67618
  const trafs = trafsOf(moof2);
67196
67619
  const traf2 = trafs.find((candidate) => {
67197
67620
  const tfhd2 = tfhdOf(candidate);
67198
- return tfhd2?.data !== void 0 && readU323(tfhd2.data, 4) === trackId;
67621
+ return tfhd2?.data !== void 0 && readU324(tfhd2.data, 4) === trackId;
67199
67622
  });
67200
67623
  const children = traf2.children ?? [];
67201
67624
  const lastTrun = children.map((box2) => box2.type).lastIndexOf("trun");
@@ -67206,7 +67629,7 @@ ${baseUrl}${p.getFileName()}`;
67206
67629
  const geometry = {
67207
67630
  moofOffset: moofStart,
67208
67631
  mdatDataStart: moofEnd + 8,
67209
- mdatLength: readU323(segment, moofEnd) - 8
67632
+ mdatLength: readU324(segment, moofEnd) - 8
67210
67633
  };
67211
67634
  const resolved = shiftTrunDataOffsets(moof2, added, geometry);
67212
67635
  for (let index = trafs.indexOf(traf2); index < trafs.length; index++) {
@@ -67228,208 +67651,6 @@ ${baseUrl}${p.getFileName()}`;
67228
67651
  return out;
67229
67652
  };
67230
67653
 
67231
- // src/mpeg-ts/scte35.ts
67232
- var SYNC_BYTE = 71;
67233
- var SPLICE_INFO_TABLE_ID = 252;
67234
- var CUEI_FORMAT_IDENTIFIER2 = 1129661769;
67235
- var SCTE35_STREAM_TYPE2 = 134;
67236
- var SPLICE_INSERT = 5;
67237
- var PROGRAM_SPLICE_FLAG = 64;
67238
- var SPLICE_IMMEDIATE_FLAG = 16;
67239
- var TIME_SIGNAL = 6;
67240
- var readU163 = (bytes2, offset) => bytes2[offset] << 8 | bytes2[offset + 1];
67241
- var readU324 = (bytes2, offset) => (bytes2[offset] << 24 | bytes2[offset + 1] << 16 | bytes2[offset + 2] << 8 | bytes2[offset + 3]) >>> 0;
67242
- var read33Bits = (bytes2, offset, lowBitsOfFirstByte) => {
67243
- const high = bytes2[offset] & (1 << lowBitsOfFirstByte) - 1;
67244
- return high * 2 ** 32 + readU324(bytes2, offset + 1);
67245
- };
67246
- var readScte35Sections = (bytes2) => {
67247
- const sections = [];
67248
- let pmtPid = null;
67249
- let scte35Pid = null;
67250
- const assembly = /* @__PURE__ */ new Map();
67251
- let start = 0;
67252
- while (start < bytes2.length && bytes2[start] !== SYNC_BYTE) {
67253
- start++;
67254
- }
67255
- for (let offset = start; offset + TS_PACKET_SIZE <= bytes2.length; offset += TS_PACKET_SIZE) {
67256
- if (bytes2[offset] !== SYNC_BYTE) {
67257
- break;
67258
- }
67259
- const pid = readU163(bytes2, offset + 1) & 8191;
67260
- const payloadUnitStart = (bytes2[offset + 1] & 64) !== 0;
67261
- const hasAdaptation = (bytes2[offset + 3] & 32) !== 0;
67262
- const hasPayload = (bytes2[offset + 3] & 16) !== 0;
67263
- if (!hasPayload) {
67264
- continue;
67265
- }
67266
- let payloadStart = offset + 4;
67267
- if (hasAdaptation) {
67268
- payloadStart += 1 + bytes2[payloadStart];
67269
- }
67270
- if (payloadStart >= offset + TS_PACKET_SIZE) {
67271
- continue;
67272
- }
67273
- if (pid === PAT_PID) {
67274
- pmtPid ??= readPmtPidFromPat(bytes2, payloadStart, offset + TS_PACKET_SIZE, payloadUnitStart);
67275
- continue;
67276
- }
67277
- if (pmtPid !== null && pid === pmtPid) {
67278
- scte35Pid ??= readScte35PidFromPmt(bytes2, payloadStart, offset + TS_PACKET_SIZE, payloadUnitStart);
67279
- continue;
67280
- }
67281
- if (scte35Pid === null || pid !== scte35Pid) {
67282
- continue;
67283
- }
67284
- collectSection(bytes2, payloadStart, offset + TS_PACKET_SIZE, payloadUnitStart, pid, assembly, sections);
67285
- }
67286
- return sections;
67287
- };
67288
- var sectionStart = (bytes2, payloadStart, payloadEnd) => {
67289
- const pointerField = bytes2[payloadStart];
67290
- const start = payloadStart + 1 + pointerField;
67291
- return start < payloadEnd ? start : null;
67292
- };
67293
- var readPmtPidFromPat = (bytes2, payloadStart, payloadEnd, payloadUnitStart) => {
67294
- if (!payloadUnitStart) {
67295
- return null;
67296
- }
67297
- const start = sectionStart(bytes2, payloadStart, payloadEnd);
67298
- if (start === null || start + 12 > payloadEnd) {
67299
- return null;
67300
- }
67301
- for (let pos = start + 8; pos + 4 <= payloadEnd; pos += 4) {
67302
- const programNumber = readU163(bytes2, pos);
67303
- const pid = readU163(bytes2, pos + 2) & 8191;
67304
- if (programNumber !== 0) {
67305
- return pid;
67306
- }
67307
- }
67308
- return null;
67309
- };
67310
- var readScte35PidFromPmt = (bytes2, payloadStart, payloadEnd, payloadUnitStart) => {
67311
- if (!payloadUnitStart) {
67312
- return null;
67313
- }
67314
- const start = sectionStart(bytes2, payloadStart, payloadEnd);
67315
- if (start === null || start + 12 > payloadEnd) {
67316
- return null;
67317
- }
67318
- const sectionLength = readU163(bytes2, start + 1) & 4095;
67319
- const sectionEnd = Math.min(start + 3 + sectionLength, payloadEnd);
67320
- const programInfoLength = readU163(bytes2, start + 10) & 4095;
67321
- let pos = start + 12 + programInfoLength;
67322
- while (pos + 5 <= sectionEnd - 4) {
67323
- const streamType = bytes2[pos];
67324
- const pid = readU163(bytes2, pos + 1) & 8191;
67325
- const esInfoLength = readU163(bytes2, pos + 3) & 4095;
67326
- const descriptorsEnd = Math.min(pos + 5 + esInfoLength, sectionEnd);
67327
- if (streamType === SCTE35_STREAM_TYPE2) {
67328
- for (let d = pos + 5; d + 2 <= descriptorsEnd; ) {
67329
- const tag = bytes2[d];
67330
- const length = bytes2[d + 1];
67331
- if (tag === REGISTRATION_DESCRIPTOR_TAG && length >= 4 && d + 6 <= descriptorsEnd && readU324(bytes2, d + 2) === CUEI_FORMAT_IDENTIFIER2) {
67332
- return pid;
67333
- }
67334
- d += 2 + length;
67335
- }
67336
- }
67337
- pos = descriptorsEnd;
67338
- }
67339
- return null;
67340
- };
67341
- var collectSection = (bytes2, payloadStart, payloadEnd, payloadUnitStart, pid, assembly, out) => {
67342
- let pos = payloadStart;
67343
- if (payloadUnitStart) {
67344
- const start = sectionStart(bytes2, payloadStart, payloadEnd);
67345
- if (start === null) {
67346
- return;
67347
- }
67348
- if (bytes2[start] !== SPLICE_INFO_TABLE_ID) {
67349
- assembly.delete(pid);
67350
- return;
67351
- }
67352
- if (start + 3 > payloadEnd) {
67353
- return;
67354
- }
67355
- const sectionLength = readU163(bytes2, start + 1) & 4095;
67356
- assembly.set(pid, { bytes: [], remaining: 3 + sectionLength });
67357
- pos = start;
67358
- }
67359
- const pending = assembly.get(pid);
67360
- if (!pending) {
67361
- return;
67362
- }
67363
- const take = Math.min(pending.remaining, payloadEnd - pos);
67364
- for (let i = 0; i < take; i++) {
67365
- pending.bytes.push(bytes2[pos + i]);
67366
- }
67367
- pending.remaining -= take;
67368
- if (pending.remaining === 0) {
67369
- assembly.delete(pid);
67370
- out.push(parseSpliceInfoSection(Uint8Array.from(pending.bytes)));
67371
- }
67372
- };
67373
- var parseSpliceInfoSection = (section) => {
67374
- if (section.length < 14) {
67375
- throw new MalformedInputError("SCTE-35 section is too short to hold its header.");
67376
- }
67377
- if (section[0] !== SPLICE_INFO_TABLE_ID) {
67378
- throw new MalformedInputError(
67379
- `SCTE-35 section states table_id 0x${section[0].toString(16)}, expected 0xfc.`
67380
- );
67381
- }
67382
- const ptsAdjustment = read33Bits(section, 4, 1);
67383
- const encrypted = (section[4] & 128) !== 0;
67384
- const commandLength = readU163(section, 11) & 4095;
67385
- const commandType = section[13];
67386
- let commandPos = 14;
67387
- const wrap = (ticks) => ticks % TIMESTAMP_MODULUS / MPEG_TS_TIMESCALE;
67388
- const noTime = { section, time: null, commandType, ptsAdjustment };
67389
- if (encrypted || commandLength === 0) {
67390
- return noTime;
67391
- }
67392
- if (commandType === TIME_SIGNAL) {
67393
- if (commandPos >= section.length) {
67394
- return noTime;
67395
- }
67396
- const timeSpecified2 = (section[commandPos] & 128) !== 0;
67397
- if (!timeSpecified2 || commandPos + 5 > section.length) {
67398
- return noTime;
67399
- }
67400
- return { section, time: wrap(ptsAdjustment + read33Bits(section, commandPos, 1)), commandType, ptsAdjustment };
67401
- }
67402
- if (commandType !== SPLICE_INSERT) {
67403
- return noTime;
67404
- }
67405
- commandPos += 4;
67406
- if (commandPos >= section.length) {
67407
- return noTime;
67408
- }
67409
- if ((section[commandPos] & 128) !== 0) {
67410
- return noTime;
67411
- }
67412
- commandPos += 1;
67413
- if (commandPos >= section.length) {
67414
- return noTime;
67415
- }
67416
- const flags = section[commandPos];
67417
- const programSplice = (flags & PROGRAM_SPLICE_FLAG) !== 0;
67418
- const spliceImmediate = (flags & SPLICE_IMMEDIATE_FLAG) !== 0;
67419
- commandPos += 1;
67420
- if (spliceImmediate || !programSplice) {
67421
- return noTime;
67422
- }
67423
- if (commandPos >= section.length) {
67424
- return noTime;
67425
- }
67426
- const timeSpecified = (section[commandPos] & 128) !== 0;
67427
- if (!timeSpecified || commandPos + 5 > section.length) {
67428
- return noTime;
67429
- }
67430
- return { section, time: wrap(ptsAdjustment + read33Bits(section, commandPos, 1)), commandType, ptsAdjustment };
67431
- };
67432
-
67433
67654
  // src/mpeg-ts/private-streams.ts
67434
67655
  var SYNC_BYTE2 = 71;
67435
67656
  var TELETEXT_DESCRIPTOR_TAG = 86;
@@ -67629,8 +67850,14 @@ ${baseUrl}${p.getFileName()}`;
67629
67850
  throw new InvalidUsageError("A splice_info_section is at least three bytes.");
67630
67851
  }
67631
67852
  const muxer = output._muxer;
67853
+ if (muxer instanceof SegmentPipelineMuxer) {
67854
+ muxer.writeScte35Section(section);
67855
+ return;
67856
+ }
67632
67857
  if (!(muxer instanceof MpegTsMuxer)) {
67633
- throw new InvalidUsageError("SCTE-35 sections can only be written to an MpegTsOutputFormat output.");
67858
+ throw new InvalidUsageError(
67859
+ "SCTE-35 sections can only be written to an MpegTsOutputFormat output, or to a segmented one whose segmentFormat is MpegTsOutputFormat."
67860
+ );
67634
67861
  }
67635
67862
  muxer.writeScte35Section(section);
67636
67863
  };