@ahmedrowaihi/mediabunny 1.65.2-beta.699 → 1.65.2-beta.701

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.
@@ -18270,6 +18270,7 @@ var Mediabunny = (() => {
18270
18270
  var TS_PACKET_SIZE = 188;
18271
18271
  var PAT_PID = 0;
18272
18272
  var REGISTRATION_DESCRIPTOR_TAG = 5;
18273
+ var LANGUAGE_DESCRIPTOR_TAG = 10;
18273
18274
  var fourCc = (code) => [...code].map((character) => character.charCodeAt(0));
18274
18275
  var buildPrivateDataIndicatorDescriptor = (code) => new Uint8Array([15, 4, ...fourCc(code)]);
18275
18276
  var buildAudioSetupDescriptor = (audioType, setupData) => {
@@ -18457,11 +18458,21 @@ var Mediabunny = (() => {
18457
18458
  let hasEac3Descriptor = false;
18458
18459
  let hasDtsDescriptor = false;
18459
18460
  let hasCueiDescriptor = false;
18461
+ let languageCode = UNDETERMINED_LANGUAGE;
18460
18462
  while (bitstream.pos < esInfoEndPos) {
18461
18463
  const descriptorTag = bitstream.readBits(8);
18462
18464
  const descriptorLength = bitstream.readBits(8);
18463
18465
  const descriptorEndPos = bitstream.pos + 8 * descriptorLength;
18464
- if (descriptorTag === 106) {
18466
+ if (descriptorTag === LANGUAGE_DESCRIPTOR_TAG && descriptorLength >= 4) {
18467
+ const characters = [
18468
+ bitstream.readBits(8),
18469
+ bitstream.readBits(8),
18470
+ bitstream.readBits(8)
18471
+ ];
18472
+ if (characters.every((code) => code >= 97 && code <= 122)) {
18473
+ languageCode = String.fromCharCode(...characters);
18474
+ }
18475
+ } else if (descriptorTag === 106) {
18465
18476
  hasAc3Descriptor = true;
18466
18477
  } else if (descriptorTag === 122 || descriptorTag === 204) {
18467
18478
  hasEac3Descriptor = true;
@@ -18589,6 +18600,7 @@ var Mediabunny = (() => {
18589
18600
  initialized: false,
18590
18601
  firstSection: null,
18591
18602
  canBeTrustedWithKeyPackets: false,
18603
+ languageCode,
18592
18604
  info,
18593
18605
  referencePesPackets: []
18594
18606
  });
@@ -19148,7 +19160,7 @@ var Mediabunny = (() => {
19148
19160
  return null;
19149
19161
  }
19150
19162
  getLanguageCode() {
19151
- return UNDETERMINED_LANGUAGE;
19163
+ return this.elementaryStream.languageCode;
19152
19164
  }
19153
19165
  getDisposition() {
19154
19166
  return {
@@ -43305,7 +43317,11 @@ var Mediabunny = (() => {
43305
43317
  assert(trackData.lastSample);
43306
43318
  const timescaleUnits = intoTimescale(sample.decodeTimestamp, trackData.timescale, false);
43307
43319
  const delta = Math.round(timescaleUnits - trackData.lastTimescaleUnits);
43308
- assert(delta >= 0);
43320
+ if (delta < 0) {
43321
+ throw new Error(
43322
+ `Decode timestamps must not go backwards, but a packet presenting at ${sample.timestamp}s states a decode timestamp of ${sample.decodeTimestamp}s, which is before the previous packet's ${trackData.lastTimescaleUnits / trackData.timescale}s. Packets that state a decode timestamp must be added in decode order.`
43323
+ );
43324
+ }
43309
43325
  trackData.lastTimescaleUnits += delta;
43310
43326
  trackData.lastSample.timescaleUnitsToNextSample = delta;
43311
43327
  if (!this.isFragmented) {
@@ -45848,8 +45864,6 @@ ${cue.notes ?? ""}`;
45848
45864
  this.packetBuffer = new Uint8Array(TS_PACKET_SIZE);
45849
45865
  this.packetView = toDataView(this.packetBuffer);
45850
45866
  this.allTracksKnown = promiseWithResolvers();
45851
- this.videoTrackIndex = 0;
45852
- this.audioTrackIndex = 0;
45853
45867
  this.adaptationFieldBuffer = new Uint8Array(184);
45854
45868
  this.pcrPid = null;
45855
45869
  this.payloadBuffer = new Uint8Array(184);
@@ -45860,6 +45874,31 @@ ${cue.notes ?? ""}`;
45860
45874
  this.tableRepeatInterval = format._options.tableRepeatInterval ?? 0.1;
45861
45875
  this.privateStreams = format._options.privateStreams ?? [];
45862
45876
  }
45877
+ /**
45878
+ * PIDs and stream ids follow the order tracks were added to the output, not the order their first
45879
+ * packets arrive - two runs of the same job interleave differently, and that used to renumber the
45880
+ * streams. The index is of this track among the output's tracks of its own kind.
45881
+ */
45882
+ trackIndexOf(track) {
45883
+ return this.output.tracks.filter((x) => x.type === track.type).indexOf(track);
45884
+ }
45885
+ pidOf(track) {
45886
+ return FIRST_TRACK_PID + this.output.tracks.indexOf(track);
45887
+ }
45888
+ /**
45889
+ * Keeps `trackDatas` in the output's own track order. The PMT lists streams in this order and the
45890
+ * tables are written from it, so appending on arrival left the same job writing different bytes.
45891
+ */
45892
+ addTrackData(trackData) {
45893
+ const index = this.trackDatas.findIndex(
45894
+ (existing) => this.output.tracks.indexOf(existing.track) > this.output.tracks.indexOf(trackData.track)
45895
+ );
45896
+ if (index === -1) {
45897
+ this.trackDatas.push(trackData);
45898
+ } else {
45899
+ this.trackDatas.splice(index, 0, trackData);
45900
+ }
45901
+ }
45863
45902
  get maxInterleaveDelta() {
45864
45903
  return this.format._options.maxInterleaveDelta ?? DEFAULT_MAX_INTERLEAVE_DELTA;
45865
45904
  }
@@ -45888,8 +45927,8 @@ ${cue.notes ?? ""}`;
45888
45927
  );
45889
45928
  }
45890
45929
  const streamType = codec === "avc" ? encryption ? 219 /* SAMPLE_AES_AVC */ : 27 /* AVC */ : codec === "mpeg2" ? 2 /* MPEG2_VIDEO */ : 36 /* HEVC */;
45891
- const pid = FIRST_TRACK_PID + this.trackDatas.length;
45892
- const streamId = VIDEO_STREAM_ID_BASE + this.videoTrackIndex++;
45930
+ const pid = this.pidOf(track);
45931
+ const streamId = VIDEO_STREAM_ID_BASE + this.trackIndexOf(track);
45893
45932
  const newTrackData = {
45894
45933
  track,
45895
45934
  pid,
@@ -45908,7 +45947,7 @@ ${cue.notes ?? ""}`;
45908
45947
  firstPacketWritten: false,
45909
45948
  closed: false
45910
45949
  };
45911
- this.trackDatas.push(newTrackData);
45950
+ this.addTrackData(newTrackData);
45912
45951
  if (this.allTracksAreKnown()) {
45913
45952
  this.allTracksKnown.resolve();
45914
45953
  }
@@ -45937,7 +45976,7 @@ ${cue.notes ?? ""}`;
45937
45976
  case "aac":
45938
45977
  {
45939
45978
  streamType = audioEncryption ? 207 /* SAMPLE_AES_AAC */ : 15 /* AAC */;
45940
- streamId = AUDIO_STREAM_ID_BASE + this.audioTrackIndex++;
45979
+ streamId = AUDIO_STREAM_ID_BASE + this.trackIndexOf(track);
45941
45980
  }
45942
45981
  ;
45943
45982
  break;
@@ -45947,7 +45986,7 @@ ${cue.notes ?? ""}`;
45947
45986
  case "mp2":
45948
45987
  {
45949
45988
  streamType = 3 /* MP3_MPEG1 */;
45950
- streamId = AUDIO_STREAM_ID_BASE + this.audioTrackIndex++;
45989
+ streamId = AUDIO_STREAM_ID_BASE + this.trackIndexOf(track);
45951
45990
  }
45952
45991
  ;
45953
45992
  break;
@@ -45973,7 +46012,7 @@ ${cue.notes ?? ""}`;
45973
46012
  ;
45974
46013
  break;
45975
46014
  }
45976
- const pid = FIRST_TRACK_PID + this.trackDatas.length;
46015
+ const pid = this.pidOf(track);
45977
46016
  const newTrackData = {
45978
46017
  track,
45979
46018
  pid,
@@ -45997,7 +46036,7 @@ ${cue.notes ?? ""}`;
45997
46036
  "SAMPLE-AES needs this AAC track's AudioSpecificConfig for the PMT audio setup descriptor, but its decoder config states none."
45998
46037
  );
45999
46038
  }
46000
- this.trackDatas.push(newTrackData);
46039
+ this.addTrackData(newTrackData);
46001
46040
  if (this.allTracksAreKnown()) {
46002
46041
  this.allTracksKnown.resolve();
46003
46042
  }
@@ -46611,20 +46650,39 @@ ${cue.notes ?? ""}`;
46611
46650
  buf[offset + 4] = base90kHz % 2 << 7 | 126;
46612
46651
  buf[offset + 5] = 0;
46613
46652
  };
46653
+ var languageDescriptor = (languageCode) => new Uint8Array([
46654
+ LANGUAGE_DESCRIPTOR_TAG,
46655
+ 4,
46656
+ languageCode.charCodeAt(0),
46657
+ languageCode.charCodeAt(1),
46658
+ languageCode.charCodeAt(2),
46659
+ 0
46660
+ // audio_type: undefined, which is what a track that only states a language means
46661
+ ]);
46662
+ var esDescriptorsOf = (trackData) => {
46663
+ const descriptors = sampleAesDescriptorsFor(trackData);
46664
+ if (descriptors.length === 0) {
46665
+ if (trackData.streamType === 129 /* AC3_SYSTEM_A */) {
46666
+ descriptors.push(AC3_REGISTRATION_DESCRIPTOR);
46667
+ } else if (trackData.streamType === 135 /* EAC3_SYSTEM_A */) {
46668
+ descriptors.push(EAC3_REGISTRATION_DESCRIPTOR);
46669
+ }
46670
+ }
46671
+ const languageCode = trackData.track.metadata.languageCode;
46672
+ if (languageCode !== void 0 && languageCode !== UNDETERMINED_LANGUAGE) {
46673
+ descriptors.push(languageDescriptor(languageCode));
46674
+ }
46675
+ return descriptors;
46676
+ };
46614
46677
  var descriptorBytesOf = (stream) => stream.descriptors.reduce((sum, descriptor) => sum + descriptor.byteLength, 0);
46615
46678
  var buildPmt = (trackDatas, scte35Pid, privateStreams) => {
46616
- const sampleAesDescriptors = /* @__PURE__ */ new Map();
46679
+ const esDescriptors = /* @__PURE__ */ new Map();
46617
46680
  let totalEsBytes = 0;
46618
46681
  for (const trackData of trackDatas) {
46619
46682
  totalEsBytes += 5;
46620
- const descriptors = sampleAesDescriptorsFor(trackData);
46621
- sampleAesDescriptors.set(trackData, descriptors);
46683
+ const descriptors = esDescriptorsOf(trackData);
46684
+ esDescriptors.set(trackData, descriptors);
46622
46685
  totalEsBytes += descriptors.reduce((sum, descriptor) => sum + descriptor.length, 0);
46623
- if (trackData.streamType === 129 /* AC3_SYSTEM_A */) {
46624
- totalEsBytes += AC3_REGISTRATION_DESCRIPTOR.length;
46625
- } else if (trackData.streamType === 135 /* EAC3_SYSTEM_A */) {
46626
- totalEsBytes += EAC3_REGISTRATION_DESCRIPTOR.length;
46627
- }
46628
46686
  }
46629
46687
  for (const stream of privateStreams) {
46630
46688
  totalEsBytes += 5 + descriptorBytesOf(stream);
@@ -46649,28 +46707,13 @@ ${cue.notes ?? ""}`;
46649
46707
  section[offset++] = trackData.streamType;
46650
46708
  view2.setUint16(offset, 57344 | trackData.pid & 8191, false);
46651
46709
  offset += 2;
46652
- const descriptors = sampleAesDescriptors.get(trackData);
46653
- if (descriptors.length > 0) {
46654
- const length = descriptors.reduce((sum, descriptor) => sum + descriptor.length, 0);
46655
- view2.setUint16(offset, 61440 | length, false);
46656
- offset += 2;
46657
- for (const descriptor of descriptors) {
46658
- section.set(descriptor, offset);
46659
- offset += descriptor.length;
46660
- }
46661
- } else if (trackData.streamType === 129 /* AC3_SYSTEM_A */) {
46662
- view2.setUint16(offset, 61440 | AC3_REGISTRATION_DESCRIPTOR.length, false);
46663
- offset += 2;
46664
- section.set(AC3_REGISTRATION_DESCRIPTOR, offset);
46665
- offset += AC3_REGISTRATION_DESCRIPTOR.length;
46666
- } else if (trackData.streamType === 135 /* EAC3_SYSTEM_A */) {
46667
- view2.setUint16(offset, 61440 | EAC3_REGISTRATION_DESCRIPTOR.length, false);
46668
- offset += 2;
46669
- section.set(EAC3_REGISTRATION_DESCRIPTOR, offset);
46670
- offset += EAC3_REGISTRATION_DESCRIPTOR.length;
46671
- } else {
46672
- view2.setUint16(offset, 61440, false);
46673
- offset += 2;
46710
+ const descriptors = esDescriptors.get(trackData);
46711
+ const length = descriptors.reduce((sum, descriptor) => sum + descriptor.length, 0);
46712
+ view2.setUint16(offset, 61440 | length, false);
46713
+ offset += 2;
46714
+ for (const descriptor of descriptors) {
46715
+ section.set(descriptor, offset);
46716
+ offset += descriptor.length;
46674
46717
  }
46675
46718
  }
46676
46719
  for (const stream of privateStreams) {