@ahmedrowaihi/mediabunny 1.65.2-beta.587 → 1.65.2-beta.597

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.
@@ -45350,6 +45350,8 @@ ${cue.notes ?? ""}`;
45350
45350
  this.pendingScte35Sections = [];
45351
45351
  this.tablesWritten = false;
45352
45352
  this.continuityCounters = /* @__PURE__ */ new Map();
45353
+ /** Stream time the tables were last written at, or null until the first write. */
45354
+ this.lastTableTimestamp = null;
45353
45355
  this.packetBuffer = new Uint8Array(TS_PACKET_SIZE);
45354
45356
  this.packetView = toDataView(this.packetBuffer);
45355
45357
  this.allTracksKnown = promiseWithResolvers();
@@ -45362,6 +45364,7 @@ ${cue.notes ?? ""}`;
45362
45364
  this.timestampOffset = format._options.timestampOffset ?? 0;
45363
45365
  this.offsetIsStated = format._options.timestampOffset !== void 0;
45364
45366
  this.scte35Pid = format._options.scte35Pid ?? null;
45367
+ this.tableRepeatInterval = format._options.tableRepeatInterval ?? 0.1;
45365
45368
  }
45366
45369
  get maxInterleaveDelta() {
45367
45370
  return this.format._options.maxInterleaveDelta ?? DEFAULT_MAX_INTERLEAVE_DELTA;
@@ -45752,17 +45755,43 @@ ${cue.notes ?? ""}`;
45752
45755
  }
45753
45756
  this.decideTimestampOffset();
45754
45757
  const queuedPacket = trackWithMinTimestamp.packetQueue.shift();
45758
+ this.repeatTablesIfDue(queuedPacket);
45755
45759
  this.writePesPacket(trackWithMinTimestamp, queuedPacket);
45756
45760
  }
45757
45761
  if (!isFinalCall) {
45758
45762
  await this.writer.flush();
45759
45763
  }
45760
45764
  }
45765
+ /** PAT then PMT, on their own PIDs, each continuing its own continuity counter. */
45766
+ writeTableSections() {
45767
+ this.writePsiSection(PAT_PID2, PAT_SECTION);
45768
+ this.writePsiSection(PMT_PID, buildPmt(this.trackDatas, this.scte35Pid));
45769
+ }
45770
+ /**
45771
+ * Repeats the tables once the period has elapsed on the stream's own timeline, or a little early to
45772
+ * get in front of a key frame, which is where a receiver joining mid-stream begins.
45773
+ */
45774
+ repeatTablesIfDue(packet) {
45775
+ if (!Number.isFinite(this.tableRepeatInterval)) {
45776
+ return;
45777
+ }
45778
+ if (this.lastTableTimestamp === null) {
45779
+ this.lastTableTimestamp = packet.presentationTimestamp;
45780
+ return;
45781
+ }
45782
+ const elapsed = packet.presentationTimestamp - this.lastTableTimestamp;
45783
+ const earlyForKeyFrame = packet.isKeyframe && elapsed >= this.tableRepeatInterval / 2;
45784
+ if (elapsed < this.tableRepeatInterval && !earlyForKeyFrame) {
45785
+ return;
45786
+ }
45787
+ this.writePsiSection(PAT_PID2, PAT_SECTION);
45788
+ this.writePsiSection(PMT_PID, buildPmt(this.trackDatas, this.scte35Pid));
45789
+ this.lastTableTimestamp = packet.presentationTimestamp;
45790
+ }
45761
45791
  writeTables() {
45762
45792
  assert(!this.tablesWritten);
45763
- this.writePsiSection(PAT_PID2, PAT_SECTION);
45764
45793
  this.pcrPid = pcrTrackOf(this.trackDatas)?.pid ?? null;
45765
- this.writePsiSection(PMT_PID, buildPmt(this.trackDatas, this.scte35Pid));
45794
+ this.writeTableSections();
45766
45795
  this.tablesWritten = true;
45767
45796
  this.flushScte35Sections();
45768
45797
  }
@@ -53087,6 +53116,9 @@ ${cue.text}
53087
53116
  if (options.scte35Pid !== void 0 && (!Number.isInteger(options.scte35Pid) || options.scte35Pid < 16 || options.scte35Pid > 8190)) {
53088
53117
  throw new TypeError("options.scte35Pid, when provided, must be an integer between 0x0010 and 0x1ffe.");
53089
53118
  }
53119
+ if (options.tableRepeatInterval !== void 0 && !(options.tableRepeatInterval > 0)) {
53120
+ throw new TypeError("options.tableRepeatInterval, when provided, must be a positive number.");
53121
+ }
53090
53122
  if (options.maxInterleaveDelta !== void 0 && !(options.maxInterleaveDelta > 0)) {
53091
53123
  throw new TypeError("options.maxInterleaveDelta, when provided, must be a positive number.");
53092
53124
  }
@@ -66087,18 +66119,29 @@ ${baseUrl}${p.getFileName()}`;
66087
66119
  var readPrivatePesPayloads = (bytes2, pid) => {
66088
66120
  const out = [];
66089
66121
  let current = null;
66090
- const finish = () => {
66091
- if (current !== null && current.chunks.length > 0) {
66092
- out.push({ pid, time: current.time, data: new Uint8Array(current.chunks) });
66122
+ const finish = (atUnitStart) => {
66123
+ if (current === null) {
66124
+ current = null;
66125
+ return;
66093
66126
  }
66127
+ const { time, chunks, expected } = current;
66094
66128
  current = null;
66129
+ if (expected === null) {
66130
+ if (atUnitStart && chunks.length > 0) {
66131
+ out.push({ pid, time, data: new Uint8Array(chunks) });
66132
+ }
66133
+ return;
66134
+ }
66135
+ if (chunks.length >= expected) {
66136
+ out.push({ pid, time, data: new Uint8Array(chunks.slice(0, expected)) });
66137
+ }
66095
66138
  };
66096
66139
  for (const packet of iteratePackets(bytes2)) {
66097
66140
  if (packet.pid !== pid) {
66098
66141
  continue;
66099
66142
  }
66100
66143
  if (packet.payloadUnitStart) {
66101
- finish();
66144
+ finish(true);
66102
66145
  const at = packet.payloadStart;
66103
66146
  if (at + 9 > packet.payloadEnd || bytes2[at] !== 0 || bytes2[at + 1] !== 0 || bytes2[at + 2] !== 1) {
66104
66147
  continue;
@@ -66108,9 +66151,14 @@ ${baseUrl}${p.getFileName()}`;
66108
66151
  if (payloadStart > packet.payloadEnd) {
66109
66152
  continue;
66110
66153
  }
66154
+ const packetLength = bytes2[at + 4] << 8 | bytes2[at + 5];
66155
+ const expected = packetLength === 0 ? null : packetLength - 3 - headerLength;
66156
+ if (expected !== null && expected < 0) {
66157
+ continue;
66158
+ }
66111
66159
  const ptsPresent = (bytes2[at + 7] & 128) !== 0;
66112
66160
  const time = ptsPresent && at + 14 <= packet.payloadEnd ? readPts(bytes2, at + 9) % TIMESTAMP_MODULUS / MPEG_TS_TIMESCALE : null;
66113
- current = { time, chunks: [] };
66161
+ current = { time, chunks: [], expected };
66114
66162
  for (let i = payloadStart; i < packet.payloadEnd; i++) {
66115
66163
  current.chunks.push(bytes2[i]);
66116
66164
  }
@@ -66120,7 +66168,7 @@ ${baseUrl}${p.getFileName()}`;
66120
66168
  }
66121
66169
  }
66122
66170
  }
66123
- finish();
66171
+ finish(false);
66124
66172
  return out;
66125
66173
  };
66126
66174