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

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.
@@ -46280,8 +46280,12 @@ ${cue.notes ?? ""}`;
46280
46280
  };
46281
46281
 
46282
46282
  // src/mpeg-ts/mpeg-ts-muxer.ts
46283
+ var EMPTY_PAYLOAD = new Uint8Array(0);
46283
46284
  var PAT_PID2 = 0;
46284
- var PMT_PID = 4096;
46285
+ var SDT_PID = 17;
46286
+ var NULL_PACKET_PID = 8191;
46287
+ var CBR_LEAD_ALLOWANCE = 0.5;
46288
+ var DEFAULT_PMT_PID = 4096;
46285
46289
  var SCTE35_STREAM_TYPE = 134;
46286
46290
  var FIRST_TRACK_PID = 256;
46287
46291
  var PRIVATE_STREAM_ID = 189;
@@ -46297,6 +46301,11 @@ ${cue.notes ?? ""}`;
46297
46301
  // offset leaking into the next shifts a programme that needed no shift at all.
46298
46302
  this.defaultTimestampOffset = null;
46299
46303
  this.trackDatas = [];
46304
+ /** Every 188-byte packet written so far, null packets included: the byte clock of a CBR stream. */
46305
+ this.packetsWritten = 0;
46306
+ /** The media's own span, so a constant-rate stream's byte clock starts and ends where it does. */
46307
+ this.firstWrittenTimestamp = null;
46308
+ this.lastWrittenEndTimestamp = 0;
46300
46309
  /**
46301
46310
  * Sections handed over before the tables went out. A track's data exists only once its first packet
46302
46311
  * is added, so writing the tables early would publish a PMT that names the cue stream and no media.
@@ -46307,8 +46316,12 @@ ${cue.notes ?? ""}`;
46307
46316
  this.privateStreamsStarted = /* @__PURE__ */ new Set();
46308
46317
  this.tablesWritten = false;
46309
46318
  this.continuityCounters = /* @__PURE__ */ new Map();
46310
- /** Stream time the tables were last written at, or null until the first write. */
46311
- this.lastTableTimestamp = null;
46319
+ /** Stream time each table was last written at, or null until its first write. */
46320
+ this.lastPatTimestamp = null;
46321
+ this.lastPmtTimestamp = null;
46322
+ this.lastSdtTimestamp = null;
46323
+ /** Stream time of the last PCR, however it was carried. */
46324
+ this.lastPcrTimestamp = null;
46312
46325
  this.packetBuffer = new Uint8Array(TS_PACKET_SIZE);
46313
46326
  this.packetView = toDataView(this.packetBuffer);
46314
46327
  this.allTracksKnown = promiseWithResolvers();
@@ -46320,7 +46333,18 @@ ${cue.notes ?? ""}`;
46320
46333
  this.offsetIsStated = format._options.timestampOffset !== void 0;
46321
46334
  this.scte35Pid = format._options.scte35Pid ?? null;
46322
46335
  this.tableRepeatInterval = format._options.tableRepeatInterval ?? 0.1;
46336
+ this.patInterval = format._options.patInterval ?? this.tableRepeatInterval;
46337
+ this.pmtInterval = format._options.pmtInterval ?? this.tableRepeatInterval;
46338
+ this.pcrInterval = format._options.pcrInterval ?? 0.04;
46339
+ this.sdtInterval = format._options.sdtInterval ?? 0.5;
46340
+ this.muxRate = format._options.muxRate ?? null;
46341
+ this.service = format._options.service ?? null;
46323
46342
  this.privateStreams = format._options.privateStreams ?? [];
46343
+ this.programNumber = format._options.programNumber ?? 1;
46344
+ this.transportStreamId = format._options.transportStreamId ?? 1;
46345
+ this.pmtPid = format._options.pmtPid ?? DEFAULT_PMT_PID;
46346
+ this.statedPcrPid = format._options.pcrPid ?? null;
46347
+ this.trackPids = format._options.trackPids ?? null;
46324
46348
  }
46325
46349
  /**
46326
46350
  * PIDs and stream ids follow the order tracks were added to the output, not the order their first
@@ -46331,7 +46355,8 @@ ${cue.notes ?? ""}`;
46331
46355
  return this.output.tracks.filter((x) => x.type === track.type).indexOf(track);
46332
46356
  }
46333
46357
  pidOf(track) {
46334
- return FIRST_TRACK_PID + this.output.tracks.indexOf(track);
46358
+ const index = this.output.tracks.indexOf(track);
46359
+ return this.trackPids?.[index] ?? FIRST_TRACK_PID + index;
46335
46360
  }
46336
46361
  /**
46337
46362
  * Keeps `trackDatas` in the output's own track order. The PMT lists streams in this order and the
@@ -46508,6 +46533,7 @@ ${cue.notes ?? ""}`;
46508
46533
  presentationTimestamp: packet.timestamp,
46509
46534
  sourceDecodeTimestamp: packet.decodeTimestamp,
46510
46535
  decodeTimestamp: null,
46536
+ duration: packet.duration,
46511
46537
  isKeyframe: packet.type === "key"
46512
46538
  });
46513
46539
  } finally {
@@ -46532,6 +46558,7 @@ ${cue.notes ?? ""}`;
46532
46558
  presentationTimestamp: packet.timestamp,
46533
46559
  sourceDecodeTimestamp: packet.decodeTimestamp,
46534
46560
  decodeTimestamp: null,
46561
+ duration: packet.duration,
46535
46562
  isKeyframe: packet.type === "key"
46536
46563
  });
46537
46564
  } finally {
@@ -46745,6 +46772,12 @@ ${cue.notes ?? ""}`;
46745
46772
  this.decideTimestampOffset();
46746
46773
  const queuedPacket = trackWithMinTimestamp.packetQueue.shift();
46747
46774
  this.repeatTablesIfDue(queuedPacket);
46775
+ this.writePcrIfDue(
46776
+ // The decode timestamp, which is the clock the track's own PCRs are stamped from and the
46777
+ // only one that rises monotonically in the order packets are written
46778
+ queuedPacket.decodeTimestamp ?? queuedPacket.presentationTimestamp,
46779
+ trackWithMinTimestamp.pid
46780
+ );
46748
46781
  this.drainPrivatePes(queuedPacket.presentationTimestamp);
46749
46782
  this.writePesPacket(trackWithMinTimestamp, queuedPacket);
46750
46783
  }
@@ -46756,40 +46789,134 @@ ${cue.notes ?? ""}`;
46756
46789
  }
46757
46790
  }
46758
46791
  /** PAT then PMT, on their own PIDs, each continuing its own continuity counter. */
46759
- writeTableSections() {
46760
- this.writePsiSection(PAT_PID2, PAT_SECTION);
46761
- this.writePsiSection(PMT_PID, buildPmt(this.trackDatas, this.scte35Pid, this.privateStreams));
46792
+ writeTableSections(pat = true, pmt = true, sdt = this.service !== null) {
46793
+ if (pat) {
46794
+ this.writePsiSection(PAT_PID2, buildPat(this.transportStreamId, this.programNumber, this.pmtPid));
46795
+ }
46796
+ if (pmt) {
46797
+ this.writePsiSection(this.pmtPid, buildPmt(
46798
+ this.trackDatas,
46799
+ this.scte35Pid,
46800
+ this.privateStreams,
46801
+ this.programNumber,
46802
+ this.pcrPid
46803
+ ));
46804
+ }
46805
+ if (sdt && this.service !== null) {
46806
+ this.writePsiSection(SDT_PID, buildSdt(this.transportStreamId, this.programNumber, this.service));
46807
+ }
46808
+ }
46809
+ /**
46810
+ * Whether a table whose period is `interval` is due at `now`, having last gone out at `last`. A key
46811
+ * frame pulls it forward by up to half the period: that is where a receiver joining mid-stream
46812
+ * begins, so the table is worth most immediately before it.
46813
+ */
46814
+ tableIsDue(interval, last2, now, isKeyframe) {
46815
+ if (!Number.isFinite(interval)) {
46816
+ return false;
46817
+ }
46818
+ if (last2 === null) {
46819
+ return false;
46820
+ }
46821
+ const elapsed = now - last2;
46822
+ return elapsed >= interval || isKeyframe && elapsed >= interval / 2;
46762
46823
  }
46763
46824
  /**
46764
46825
  * Repeats the tables once the period has elapsed on the stream's own timeline, or a little early to
46765
46826
  * get in front of a key frame, which is where a receiver joining mid-stream begins.
46766
46827
  */
46767
46828
  repeatTablesIfDue(packet) {
46768
- if (!Number.isFinite(this.tableRepeatInterval)) {
46829
+ const now = packet.presentationTimestamp;
46830
+ this.lastPatTimestamp ??= now;
46831
+ this.lastPmtTimestamp ??= now;
46832
+ this.lastSdtTimestamp ??= now;
46833
+ const pat = this.tableIsDue(this.patInterval, this.lastPatTimestamp, now, packet.isKeyframe);
46834
+ const pmt = this.tableIsDue(this.pmtInterval, this.lastPmtTimestamp, now, packet.isKeyframe);
46835
+ const sdt = this.service !== null && this.tableIsDue(this.sdtInterval, this.lastSdtTimestamp, now, packet.isKeyframe);
46836
+ if (!pat && !pmt && !sdt) {
46769
46837
  return;
46770
46838
  }
46771
- if (this.lastTableTimestamp === null) {
46772
- this.lastTableTimestamp = packet.presentationTimestamp;
46839
+ this.padTo(now);
46840
+ this.writeTableSections(pat, pmt, sdt);
46841
+ if (pat) {
46842
+ this.lastPatTimestamp = now;
46843
+ }
46844
+ if (pmt) {
46845
+ this.lastPmtTimestamp = now;
46846
+ }
46847
+ if (sdt) {
46848
+ this.lastSdtTimestamp = now;
46849
+ }
46850
+ }
46851
+ /** The clock the next packet of the PCR track states, or null while it has none queued. */
46852
+ nextPcrTrackTimestamp() {
46853
+ const trackData = this.trackDatas.find((x) => x.pid === this.pcrPid);
46854
+ const head = trackData?.packetQueue[0];
46855
+ if (!head) {
46856
+ return null;
46857
+ }
46858
+ return head.decodeTimestamp ?? head.presentationTimestamp;
46859
+ }
46860
+ /**
46861
+ * Fills the gap to the PCR track's next packet with PCR-only packets, which is what keeps the
46862
+ * spacing where that track has gone quiet or where the PCR PID names no track at all.
46863
+ *
46864
+ * Stamped at one interval past the last PCR rather than from `now`: `now` is the clock of whichever
46865
+ * track the interleave reached, and only the PCR track's own is the one the PCRs are on. Audio
46866
+ * states no decode timeline, so taking its presentation time would put a PCR ahead of a video
46867
+ * decode time still to be written - and a PCR that goes backwards is worse than one that is late.
46868
+ */
46869
+ writePcrIfDue(now, pidAboutToCarryPcr) {
46870
+ if (this.pcrPid === null || pidAboutToCarryPcr === this.pcrPid) {
46773
46871
  return;
46774
46872
  }
46775
- const elapsed = packet.presentationTimestamp - this.lastTableTimestamp;
46776
- const earlyForKeyFrame = packet.isKeyframe && elapsed >= this.tableRepeatInterval / 2;
46777
- if (elapsed < this.tableRepeatInterval && !earlyForKeyFrame) {
46873
+ if (this.lastPcrTimestamp === null && this.trackDatas.some((x) => x.pid === this.pcrPid)) {
46778
46874
  return;
46779
46875
  }
46780
- this.writeTableSections();
46781
- this.lastTableTimestamp = packet.presentationTimestamp;
46876
+ let last2 = this.lastPcrTimestamp ?? now - this.pcrInterval;
46877
+ const until = this.nextPcrTrackTimestamp() ?? now;
46878
+ const field = this.adaptationFieldBuffer;
46879
+ while (last2 + this.pcrInterval < until) {
46880
+ const timestamp = last2 + this.pcrInterval;
46881
+ this.padTo(timestamp);
46882
+ field.fill(255);
46883
+ field[0] = 183;
46884
+ field[1] = 16;
46885
+ if (this.muxRate !== null) {
46886
+ writePcr27Into(field, 2, this.byteClock27MHz());
46887
+ } else {
46888
+ writePcrInto(field, 2, secondsToMpegTsTimestamp(timestamp + this.timestampOffset));
46889
+ }
46890
+ this.writeTsPacket(this.pcrPid, false, field, EMPTY_PAYLOAD);
46891
+ last2 = timestamp;
46892
+ this.lastPcrTimestamp = timestamp;
46893
+ }
46782
46894
  }
46783
46895
  writeTables() {
46784
46896
  assert(!this.tablesWritten);
46785
- for (const stream of this.privateStreams) {
46786
- if (stream.pid === PMT_PID || this.trackDatas.some((x) => x.pid === stream.pid)) {
46897
+ const used = /* @__PURE__ */ new Map([[PAT_PID2, "the PAT"], [this.pmtPid, "the PMT"]]);
46898
+ if (this.service !== null) {
46899
+ used.set(SDT_PID, "the SDT");
46900
+ }
46901
+ const claim = (pid, what) => {
46902
+ const owner = used.get(pid);
46903
+ if (owner !== void 0) {
46787
46904
  throw new InvalidUsageError(
46788
- `Private stream PID ${stream.pid} collides with a PID this output already uses.`
46905
+ `${what} uses PID ${pid}, which this output already uses for ${owner}.`
46789
46906
  );
46790
46907
  }
46908
+ used.set(pid, what);
46909
+ };
46910
+ for (const trackData of this.trackDatas) {
46911
+ claim(trackData.pid, `Track ${this.output.tracks.indexOf(trackData.track) + 1}`);
46912
+ }
46913
+ for (const stream of this.privateStreams) {
46914
+ claim(stream.pid, "A private stream");
46791
46915
  }
46792
- this.pcrPid = pcrTrackOf(this.trackDatas)?.pid ?? null;
46916
+ if (this.scte35Pid !== null) {
46917
+ claim(this.scte35Pid, "The SCTE-35 cue stream");
46918
+ }
46919
+ this.pcrPid = this.statedPcrPid ?? pcrTrackOf(this.trackDatas)?.pid ?? null;
46793
46920
  this.writeTableSections();
46794
46921
  this.tablesWritten = true;
46795
46922
  this.flushScte35Sections();
@@ -46921,12 +47048,22 @@ ${cue.notes ?? ""}`;
46921
47048
  secondsToMpegTsTimestamp(queuedPacket.decodeTimestamp + this.timestampOffset)
46922
47049
  );
46923
47050
  }
47051
+ const carriesPcr = trackData.pid === this.pcrPid;
47052
+ const pcrTimestamp = queuedPacket.decodeTimestamp ?? queuedPacket.presentationTimestamp;
47053
+ this.firstWrittenTimestamp ??= queuedPacket.presentationTimestamp;
47054
+ this.lastWrittenEndTimestamp = Math.max(
47055
+ this.lastWrittenEndTimestamp,
47056
+ queuedPacket.presentationTimestamp + queuedPacket.duration
47057
+ );
47058
+ this.padTo(pcrTimestamp);
46924
47059
  this.writePesTsPackets(trackData.pid, pesHeaderBuffer, queuedPacket.data, {
46925
47060
  isKeyframe: queuedPacket.isKeyframe,
46926
47061
  discontinuity: !trackData.firstPacketWritten,
46927
- // One per access unit stays well inside the 0.1 s the spec allows, with no clock of its own
46928
- pcr: trackData.pid === this.pcrPid ? secondsToMpegTsTimestamp(queuedPacket.decodeTimestamp + this.timestampOffset) : null
47062
+ pcr: carriesPcr ? secondsToMpegTsTimestamp(pcrTimestamp + this.timestampOffset) : null
46929
47063
  });
47064
+ if (carriesPcr) {
47065
+ this.lastPcrTimestamp = pcrTimestamp;
47066
+ }
46930
47067
  trackData.firstPacketWritten = true;
46931
47068
  }
46932
47069
  /** Splits one PES packet across 188-byte TS packets, with the adaptation field the first needs. */
@@ -46959,7 +47096,11 @@ ${cue.notes ?? ""}`;
46959
47096
  buf[1] = Number(discontinuityIndicator) << 7 | Number(randomAccessIndicator) << 6 | Number(writePcr) << 4;
46960
47097
  let stuffingStart = 2;
46961
47098
  if (writePcr) {
46962
- writePcrInto(buf, 2, flags.pcr);
47099
+ if (this.muxRate !== null) {
47100
+ writePcr27Into(buf, 2, this.byteClock27MHz());
47101
+ } else {
47102
+ writePcrInto(buf, 2, flags.pcr);
47103
+ }
46963
47104
  stuffingStart = 8;
46964
47105
  }
46965
47106
  buf.fill(255, stuffingStart, adaptationFieldSize);
@@ -46984,6 +47125,37 @@ ${cue.notes ?? ""}`;
46984
47125
  isFirstTsPacket = false;
46985
47126
  }
46986
47127
  }
47128
+ /** The 27 MHz clock the stream's own bit rate puts the next packet at. */
47129
+ byteClock27MHz() {
47130
+ assert(this.muxRate !== null);
47131
+ return Math.round(this.packetsWritten * TS_PACKET_SIZE * 8 * 27e6 / this.muxRate);
47132
+ }
47133
+ /**
47134
+ * Fills with null packets until the byte clock reaches `streamTime`, which is what makes the stream
47135
+ * constant bit rate. Nothing to do when no rate was asked for.
47136
+ */
47137
+ padTo(streamTime) {
47138
+ if (this.muxRate === null) {
47139
+ return;
47140
+ }
47141
+ const elapsed = streamTime - (this.firstWrittenTimestamp ?? streamTime);
47142
+ const target = Math.floor(elapsed * this.muxRate / (TS_PACKET_SIZE * 8));
47143
+ const allowance = CBR_LEAD_ALLOWANCE * this.muxRate / (TS_PACKET_SIZE * 8);
47144
+ if (this.packetsWritten > target + allowance) {
47145
+ const needed = this.packetsWritten * TS_PACKET_SIZE * 8 / elapsed;
47146
+ throw new InvalidUsageError(
47147
+ `muxRate is ${this.muxRate} bits per second, but by ${streamTime.toFixed(3)}s this output has written ${this.packetsWritten} packets, which needs ${Math.round(needed)}. Raise muxRate or lower the media bitrate.`
47148
+ );
47149
+ }
47150
+ while (this.packetsWritten < target) {
47151
+ this.writeNullPacket();
47152
+ }
47153
+ }
47154
+ /** A null packet: PID 0x1fff with a payload of no meaning, which is what a CBR stream pads with. */
47155
+ writeNullPacket() {
47156
+ this.payloadBuffer.fill(255);
47157
+ this.writeTsPacket(NULL_PACKET_PID, false, null, this.payloadBuffer);
47158
+ }
46987
47159
  writeTsPacket(pid, pusi, adaptationField, payload) {
46988
47160
  const cc = this.continuityCounters.get(pid) ?? 0;
46989
47161
  const hasPayload = payload.length > 0;
@@ -47006,6 +47178,7 @@ ${cue.notes ?? ""}`;
47006
47178
  }
47007
47179
  const startPos = this.writer.getPos();
47008
47180
  this.writer.write(this.packetBuffer);
47181
+ this.packetsWritten++;
47009
47182
  if (this.format._options.onPacket) {
47010
47183
  this.format._options.onPacket(this.packetBuffer.slice(), startPos);
47011
47184
  }
@@ -47034,6 +47207,8 @@ ${cue.notes ?? ""}`;
47034
47207
  await this.flushTimestampQueue(trackData, false);
47035
47208
  }
47036
47209
  await this.interleavePackets(true);
47210
+ this.padTo(this.lastWrittenEndTimestamp);
47211
+ await this.writer.flush();
47037
47212
  release();
47038
47213
  }
47039
47214
  };
@@ -47054,19 +47229,67 @@ ${cue.notes ?? ""}`;
47054
47229
  }
47055
47230
  return crc;
47056
47231
  };
47057
- var PAT_SECTION = new Uint8Array(16);
47058
- {
47059
- const view2 = toDataView(PAT_SECTION);
47060
- PAT_SECTION[0] = 0;
47232
+ var dvbText = (text) => {
47233
+ const utf8 = textEncoder.encode(text);
47234
+ if (utf8.every((byte) => byte >= 32 && byte < 128)) {
47235
+ return utf8;
47236
+ }
47237
+ const result = new Uint8Array(utf8.length + 1);
47238
+ result[0] = 21;
47239
+ result.set(utf8, 1);
47240
+ return result;
47241
+ };
47242
+ var buildSdt = (transportStreamId, programNumber, service) => {
47243
+ const provider = dvbText(service.provider);
47244
+ const name = dvbText(service.name);
47245
+ if (provider.length > 255 || name.length > 255) {
47246
+ throw new InvalidUsageError(
47247
+ "A DVB service_descriptor holds at most 255 bytes of provider name and 255 of service name."
47248
+ );
47249
+ }
47250
+ const descriptorLength = 1 + 1 + provider.length + 1 + name.length;
47251
+ const sectionLength = 8 + 5 + 2 + descriptorLength + 4;
47252
+ const section = new Uint8Array(3 + sectionLength - 4);
47253
+ const view2 = toDataView(section);
47254
+ section[0] = 66;
47255
+ view2.setUint16(1, 61440 | sectionLength & 4095, false);
47256
+ view2.setUint16(3, transportStreamId, false);
47257
+ section[5] = 193;
47258
+ section[6] = 0;
47259
+ section[7] = 0;
47260
+ view2.setUint16(8, service.originalNetworkId ?? 1, false);
47261
+ section[10] = 255;
47262
+ view2.setUint16(11, programNumber, false);
47263
+ section[13] = 252;
47264
+ view2.setUint16(14, 32768 | 2 + descriptorLength, false);
47265
+ section[16] = 72;
47266
+ section[17] = descriptorLength;
47267
+ section[18] = service.type ?? 1;
47268
+ section[19] = provider.length;
47269
+ section.set(provider, 20);
47270
+ let offset = 20 + provider.length;
47271
+ section[offset++] = name.length;
47272
+ section.set(name, offset);
47273
+ const crc = computeMpegTsCrc32(section);
47274
+ const result = new Uint8Array(section.length + 4);
47275
+ result.set(section, 0);
47276
+ toDataView(result).setUint32(section.length, crc, false);
47277
+ return result;
47278
+ };
47279
+ var buildPat = (transportStreamId, programNumber, pmtPid) => {
47280
+ const section = new Uint8Array(16);
47281
+ const view2 = toDataView(section);
47282
+ section[0] = 0;
47061
47283
  view2.setUint16(1, 45069, false);
47062
- view2.setUint16(3, 1, false);
47063
- PAT_SECTION[5] = 193;
47064
- PAT_SECTION[6] = 0;
47065
- PAT_SECTION[7] = 0;
47066
- view2.setUint16(8, 1, false);
47067
- view2.setUint16(10, 57344 | PMT_PID & 8191, false);
47068
- view2.setUint32(12, computeMpegTsCrc32(PAT_SECTION.subarray(0, 12)), false);
47069
- }
47284
+ view2.setUint16(3, transportStreamId, false);
47285
+ section[5] = 193;
47286
+ section[6] = 0;
47287
+ section[7] = 0;
47288
+ view2.setUint16(8, programNumber, false);
47289
+ view2.setUint16(10, 57344 | pmtPid & 8191, false);
47290
+ view2.setUint32(12, computeMpegTsCrc32(section.subarray(0, 12)), false);
47291
+ return section;
47292
+ };
47070
47293
  var sampleAesDescriptorsFor = (trackData) => {
47071
47294
  if (trackData.streamType === 219 /* SAMPLE_AES_AVC */) {
47072
47295
  return [buildPrivateDataIndicatorDescriptor("zavc")];
@@ -47090,14 +47313,17 @@ ${cue.notes ?? ""}`;
47090
47313
  bitstream.writeBits(15, timestamp % 2 ** 15);
47091
47314
  bitstream.writeBits(1, 1);
47092
47315
  };
47093
- var writePcrInto = (buf, offset, base90kHz) => {
47094
- buf[offset] = Math.floor(base90kHz / 2 ** 25) & 255;
47095
- buf[offset + 1] = Math.floor(base90kHz / 2 ** 17) & 255;
47096
- buf[offset + 2] = Math.floor(base90kHz / 2 ** 9) & 255;
47097
- buf[offset + 3] = Math.floor(base90kHz / 2) & 255;
47098
- buf[offset + 4] = base90kHz % 2 << 7 | 126;
47099
- buf[offset + 5] = 0;
47100
- };
47316
+ var writePcr27Into = (buf, offset, ticks27MHz) => {
47317
+ const base = Math.floor(ticks27MHz / 300);
47318
+ const extension = ticks27MHz - base * 300;
47319
+ buf[offset] = Math.floor(base / 2 ** 25) & 255;
47320
+ buf[offset + 1] = Math.floor(base / 2 ** 17) & 255;
47321
+ buf[offset + 2] = Math.floor(base / 2 ** 9) & 255;
47322
+ buf[offset + 3] = Math.floor(base / 2) & 255;
47323
+ buf[offset + 4] = base % 2 << 7 | 126 | extension >> 8 & 1;
47324
+ buf[offset + 5] = extension & 255;
47325
+ };
47326
+ var writePcrInto = (buf, offset, base90kHz) => writePcr27Into(buf, offset, base90kHz * 300);
47101
47327
  var languageDescriptor = (languageCode) => new Uint8Array([
47102
47328
  LANGUAGE_DESCRIPTOR_TAG,
47103
47329
  4,
@@ -47123,7 +47349,7 @@ ${cue.notes ?? ""}`;
47123
47349
  return descriptors;
47124
47350
  };
47125
47351
  var descriptorBytesOf = (stream) => stream.descriptors.reduce((sum, descriptor) => sum + descriptor.byteLength, 0);
47126
- var buildPmt = (trackDatas, scte35Pid, privateStreams) => {
47352
+ var buildPmt = (trackDatas, scte35Pid, privateStreams, programNumber, pcrPid) => {
47127
47353
  const esDescriptors = /* @__PURE__ */ new Map();
47128
47354
  let totalEsBytes = 0;
47129
47355
  for (const trackData of trackDatas) {
@@ -47143,12 +47369,11 @@ ${cue.notes ?? ""}`;
47143
47369
  const view2 = toDataView(section);
47144
47370
  section[0] = 2;
47145
47371
  view2.setUint16(1, 45056 | sectionLength & 4095, false);
47146
- view2.setUint16(3, 1, false);
47372
+ view2.setUint16(3, programNumber, false);
47147
47373
  section[5] = 193;
47148
47374
  section[6] = 0;
47149
47375
  section[7] = 0;
47150
- const pcrTrack = pcrTrackOf(trackDatas);
47151
- view2.setUint16(8, 57344 | (pcrTrack?.pid ?? 8191) & 8191, false);
47376
+ view2.setUint16(8, 57344 | (pcrPid ?? 8191) & 8191, false);
47152
47377
  view2.setUint16(10, 61440, false);
47153
47378
  let offset = 12;
47154
47379
  for (const trackData of trackDatas) {
@@ -54197,6 +54422,68 @@ ${cue.text}
54197
54422
  if (options.scte35Pid !== void 0 && (!Number.isInteger(options.scte35Pid) || options.scte35Pid < 16 || options.scte35Pid > 8190)) {
54198
54423
  throw new TypeError("options.scte35Pid, when provided, must be an integer between 0x0010 and 0x1ffe.");
54199
54424
  }
54425
+ for (const name of ["pmtPid", "pcrPid"]) {
54426
+ const value = options[name];
54427
+ if (value !== void 0 && (!Number.isInteger(value) || value < 16 || value > 8190)) {
54428
+ throw new TypeError(`options.${name}, when provided, must be an integer between 0x0010 and 0x1ffe.`);
54429
+ }
54430
+ }
54431
+ if (options.trackPids !== void 0) {
54432
+ if (!Array.isArray(options.trackPids)) {
54433
+ throw new TypeError("options.trackPids, when provided, must be an array of PIDs.");
54434
+ }
54435
+ for (const pid of options.trackPids) {
54436
+ if (typeof pid !== "number" || !Number.isInteger(pid) || pid < 16 || pid > 8190) {
54437
+ throw new TypeError("options.trackPids must hold integers between 0x0010 and 0x1ffe.");
54438
+ }
54439
+ }
54440
+ if (new Set(options.trackPids).size !== options.trackPids.length) {
54441
+ throw new TypeError("options.trackPids must not repeat a PID.");
54442
+ }
54443
+ }
54444
+ for (const name of ["programNumber", "transportStreamId"]) {
54445
+ const value = options[name];
54446
+ if (value !== void 0 && (!Number.isInteger(value) || value < 1 || value > 65535)) {
54447
+ throw new TypeError(`options.${name}, when provided, must be an integer between 1 and 65535.`);
54448
+ }
54449
+ }
54450
+ if (options.service !== void 0) {
54451
+ const service = options.service;
54452
+ if (!service || typeof service !== "object") {
54453
+ throw new TypeError("options.service, when provided, must be an object.");
54454
+ }
54455
+ if (typeof service.name !== "string" || service.name.length === 0) {
54456
+ throw new TypeError("options.service.name must be a non-empty string.");
54457
+ }
54458
+ if (typeof service.provider !== "string" || service.provider.length === 0) {
54459
+ throw new TypeError("options.service.provider must be a non-empty string.");
54460
+ }
54461
+ if (service.type !== void 0 && (!Number.isInteger(service.type) || service.type < 0 || service.type > 255)) {
54462
+ throw new TypeError("options.service.type, when provided, must be an integer between 0 and 255.");
54463
+ }
54464
+ if (service.originalNetworkId !== void 0 && (!Number.isInteger(service.originalNetworkId) || service.originalNetworkId < 0 || service.originalNetworkId > 65535)) {
54465
+ throw new TypeError(
54466
+ "options.service.originalNetworkId, when provided, must be an integer between 0 and 65535."
54467
+ );
54468
+ }
54469
+ }
54470
+ if (options.muxRate !== void 0 && (!Number.isFinite(options.muxRate) || options.muxRate <= 0)) {
54471
+ throw new TypeError("options.muxRate, when provided, must be a positive, finite number.");
54472
+ }
54473
+ if (options.sdtInterval !== void 0 && !(options.sdtInterval > 0)) {
54474
+ throw new TypeError("options.sdtInterval, when provided, must be a positive number.");
54475
+ }
54476
+ for (const name of ["patInterval", "pmtInterval", "pcrInterval"]) {
54477
+ const value = options[name];
54478
+ if (value !== void 0 && !(value > 0)) {
54479
+ throw new TypeError(`options.${name}, when provided, must be a positive number.`);
54480
+ }
54481
+ }
54482
+ if (options.pcrInterval !== void 0 && options.pcrInterval > 0.1) {
54483
+ throw new TypeError(
54484
+ "options.pcrInterval must be at most 0.1 seconds, which is the maximum 13818-1 2.7.2 allows."
54485
+ );
54486
+ }
54200
54487
  if (options.tableRepeatInterval !== void 0 && !(options.tableRepeatInterval > 0)) {
54201
54488
  throw new TypeError("options.tableRepeatInterval, when provided, must be a positive number.");
54202
54489
  }