mediabunny 1.43.1 → 1.44.1

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 (40) hide show
  1. package/dist/bundles/mediabunny.cjs +43 -46
  2. package/dist/bundles/mediabunny.min.cjs +5 -5
  3. package/dist/bundles/mediabunny.min.mjs +5 -5
  4. package/dist/bundles/mediabunny.mjs +43 -46
  5. package/dist/bundles/mediabunny.node.cjs +43 -46
  6. package/dist/mediabunny.d.ts +6 -0
  7. package/dist/modules/src/adts/adts-muxer.js +1 -1
  8. package/dist/modules/src/flac/flac-muxer.js +1 -1
  9. package/dist/modules/src/hls/hls-muxer.d.ts.map +1 -1
  10. package/dist/modules/src/hls/hls-muxer.js +8 -10
  11. package/dist/modules/src/isobmff/isobmff-muxer.d.ts.map +1 -1
  12. package/dist/modules/src/isobmff/isobmff-muxer.js +5 -4
  13. package/dist/modules/src/matroska/matroska-muxer.d.ts.map +1 -1
  14. package/dist/modules/src/matroska/matroska-muxer.js +7 -6
  15. package/dist/modules/src/media-source.d.ts.map +1 -1
  16. package/dist/modules/src/media-source.js +8 -17
  17. package/dist/modules/src/mp3/mp3-muxer.js +1 -1
  18. package/dist/modules/src/mpeg-ts/mpeg-ts-muxer.js +4 -4
  19. package/dist/modules/src/muxer.d.ts +1 -1
  20. package/dist/modules/src/muxer.d.ts.map +1 -1
  21. package/dist/modules/src/muxer.js +1 -3
  22. package/dist/modules/src/ogg/ogg-muxer.js +1 -1
  23. package/dist/modules/src/source.d.ts +6 -0
  24. package/dist/modules/src/source.d.ts.map +1 -1
  25. package/dist/modules/src/source.js +5 -1
  26. package/dist/modules/src/tsconfig.tsbuildinfo +1 -1
  27. package/dist/modules/src/wave/wave-muxer.js +1 -1
  28. package/package.json +1 -1
  29. package/src/adts/adts-muxer.ts +1 -1
  30. package/src/flac/flac-muxer.ts +1 -1
  31. package/src/hls/hls-muxer.ts +8 -12
  32. package/src/isobmff/isobmff-muxer.ts +6 -4
  33. package/src/matroska/matroska-muxer.ts +8 -6
  34. package/src/media-source.ts +8 -17
  35. package/src/mp3/mp3-muxer.ts +1 -1
  36. package/src/mpeg-ts/mpeg-ts-muxer.ts +4 -4
  37. package/src/muxer.ts +1 -5
  38. package/src/ogg/ogg-muxer.ts +1 -1
  39. package/src/source.ts +14 -1
  40. package/src/wave/wave-muxer.ts +1 -1
@@ -15825,10 +15825,14 @@ var Mediabunny = (() => {
15825
15825
  if (options.maxCacheSize !== void 0 && (!isNumber(options.maxCacheSize) || options.maxCacheSize < 0)) {
15826
15826
  throw new TypeError("options.maxCacheSize, when provided, must be a non-negative number.");
15827
15827
  }
15828
+ if (options.useStreamReader !== void 0 && typeof options.useStreamReader !== "boolean") {
15829
+ throw new TypeError("options.useStreamReader, when provided, must be a boolean.");
15830
+ }
15828
15831
  super();
15829
15832
  /** @internal */
15830
15833
  this._readers = /* @__PURE__ */ new WeakMap();
15831
15834
  this._blob = blob;
15835
+ this._options = options;
15832
15836
  this._orchestrator = new ReadOrchestrator({
15833
15837
  maxCacheSize: options.maxCacheSize ?? 8 * 2 ** 20,
15834
15838
  maxWorkerCount: 4,
@@ -15850,7 +15854,7 @@ var Mediabunny = (() => {
15850
15854
  assert(worker.strictTarget);
15851
15855
  let reader = this._readers.get(worker);
15852
15856
  if (reader === void 0) {
15853
- if ("stream" in this._blob && !isWebKit()) {
15857
+ if ("stream" in this._blob && !isWebKit() && this._options.useStreamReader !== false) {
15854
15858
  const slice = this._blob.slice(worker.currentPos);
15855
15859
  reader = slice.stream().getReader();
15856
15860
  } else {
@@ -24886,8 +24890,7 @@ var Mediabunny = (() => {
24886
24890
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
24887
24891
  onTrackClose(track) {
24888
24892
  }
24889
- validateAndNormalizeTimestamp(track, timestampInSeconds, isKeyPacket) {
24890
- timestampInSeconds += track.source._timestampOffset;
24893
+ validateTimestamp(track, timestampInSeconds, isKeyPacket) {
24891
24894
  if (timestampInSeconds < 0) {
24892
24895
  throw new Error(`Timestamps must be non-negative (got ${timestampInSeconds}s).`);
24893
24896
  }
@@ -24912,7 +24915,6 @@ var Mediabunny = (() => {
24912
24915
  }
24913
24916
  timestampInfo.maxTimestamp = Math.max(timestampInfo.maxTimestamp, timestampInSeconds);
24914
24917
  }
24915
- return timestampInSeconds;
24916
24918
  }
24917
24919
  };
24918
24920
 
@@ -24943,7 +24945,7 @@ var Mediabunny = (() => {
24943
24945
  async addEncodedAudioPacket(track, packet, meta) {
24944
24946
  const release = await this.mutex.acquire();
24945
24947
  try {
24946
- this.validateAndNormalizeTimestamp(track, packet.timestamp, packet.type === "key");
24948
+ this.validateTimestamp(track, packet.timestamp, packet.type === "key");
24947
24949
  if (this.inputIsAdts === null) {
24948
24950
  validateAudioChunkMetadata(meta);
24949
24951
  const description = meta?.decoderConfig?.description;
@@ -25108,7 +25110,7 @@ var Mediabunny = (() => {
25108
25110
  async addEncodedAudioPacket(track, packet, meta) {
25109
25111
  const release = await this.mutex.acquire();
25110
25112
  try {
25111
- this.validateAndNormalizeTimestamp(
25113
+ this.validateTimestamp(
25112
25114
  track,
25113
25115
  packet.timestamp,
25114
25116
  packet.type === "key"
@@ -27969,7 +27971,7 @@ var Mediabunny = (() => {
27969
27971
  }
27970
27972
  packetData = concatNalUnitsInLengthPrefixed(nalUnits, 4);
27971
27973
  }
27972
- const timestamp = this.validateAndNormalizeTimestamp(
27974
+ this.validateTimestamp(
27973
27975
  trackData.track,
27974
27976
  packet.timestamp,
27975
27977
  packet.type === "key"
@@ -27977,7 +27979,7 @@ var Mediabunny = (() => {
27977
27979
  const internalSample = this.createSampleForTrack(
27978
27980
  trackData,
27979
27981
  packetData,
27980
- timestamp,
27982
+ packet.timestamp,
27981
27983
  packet.duration,
27982
27984
  packet.type
27983
27985
  );
@@ -27999,11 +28001,12 @@ var Mediabunny = (() => {
27999
28001
  const headerLength = adtsFrame.crcCheck === null ? MIN_ADTS_FRAME_HEADER_SIZE : MAX_ADTS_FRAME_HEADER_SIZE;
28000
28002
  packetData = packetData.subarray(headerLength);
28001
28003
  }
28002
- let timestamp = this.validateAndNormalizeTimestamp(
28004
+ this.validateTimestamp(
28003
28005
  trackData.track,
28004
28006
  packet.timestamp,
28005
28007
  packet.type === "key"
28006
28008
  );
28009
+ let timestamp = packet.timestamp;
28007
28010
  let duration = packet.duration;
28008
28011
  if (trackData.info.requiresPcmTransformation) {
28009
28012
  const pcmInfo = parsePcmCodec(
@@ -28062,7 +28065,7 @@ var Mediabunny = (() => {
28062
28065
  const release = await this.mutex.acquire();
28063
28066
  try {
28064
28067
  const trackData = this.getSubtitleTrackData(track, meta);
28065
- this.validateAndNormalizeTimestamp(trackData.track, cue.timestamp, true);
28068
+ this.validateTimestamp(trackData.track, cue.timestamp, true);
28066
28069
  if (track.source._codec === "webvtt") {
28067
28070
  trackData.cueQueue.push(cue);
28068
28071
  await this.processWebVTTCues(trackData, cue.timestamp);
@@ -29292,7 +29295,8 @@ var Mediabunny = (() => {
29292
29295
  try {
29293
29296
  const trackData = this.getVideoTrackData(track, packet, meta);
29294
29297
  const isKeyFrame = packet.type === "key";
29295
- let timestamp = this.validateAndNormalizeTimestamp(trackData.track, packet.timestamp, isKeyFrame);
29298
+ this.validateTimestamp(trackData.track, packet.timestamp, isKeyFrame);
29299
+ let timestamp = packet.timestamp;
29296
29300
  let duration = packet.duration;
29297
29301
  if (track.metadata.frameRate !== void 0) {
29298
29302
  timestamp = roundToDivisor(timestamp, track.metadata.frameRate);
@@ -29321,8 +29325,8 @@ var Mediabunny = (() => {
29321
29325
  packetData = packetData.subarray(headerLength);
29322
29326
  }
29323
29327
  const isKeyFrame = packet.type === "key";
29324
- const timestamp = this.validateAndNormalizeTimestamp(trackData.track, packet.timestamp, isKeyFrame);
29325
- const audioChunk = this.createInternalChunk(packetData, timestamp, packet.duration, packet.type);
29328
+ this.validateTimestamp(trackData.track, packet.timestamp, isKeyFrame);
29329
+ const audioChunk = this.createInternalChunk(packetData, packet.timestamp, packet.duration, packet.type);
29326
29330
  trackData.chunkQueue.push(audioChunk);
29327
29331
  await this.interleaveChunks();
29328
29332
  } finally {
@@ -29333,9 +29337,9 @@ var Mediabunny = (() => {
29333
29337
  const release = await this.mutex.acquire();
29334
29338
  try {
29335
29339
  const trackData = this.getSubtitleTrackData(track, meta);
29336
- const timestamp = this.validateAndNormalizeTimestamp(trackData.track, cue.timestamp, true);
29340
+ this.validateTimestamp(trackData.track, cue.timestamp, true);
29337
29341
  let bodyText = cue.text;
29338
- const timestampMs = Math.round(timestamp * 1e3);
29342
+ const timestampMs = Math.round(cue.timestamp * 1e3);
29339
29343
  inlineTimestampRegex.lastIndex = 0;
29340
29344
  bodyText = bodyText.replace(inlineTimestampRegex, (match) => {
29341
29345
  const time = parseSubtitleTimestamp(match.slice(1, -1));
@@ -29348,7 +29352,7 @@ ${cue.identifier ?? ""}
29348
29352
  ${cue.notes ?? ""}`;
29349
29353
  const subtitleChunk = this.createInternalChunk(
29350
29354
  body,
29351
- timestamp,
29355
+ cue.timestamp,
29352
29356
  cue.duration,
29353
29357
  "key",
29354
29358
  additions.trim() ? textEncoder.encode(additions) : null
@@ -29742,7 +29746,7 @@ ${cue.notes ?? ""}`;
29742
29746
  this.mp3Writer.writeXingFrame(this.xingFrameData);
29743
29747
  this.frameCount++;
29744
29748
  }
29745
- this.validateAndNormalizeTimestamp(track, packet.timestamp, packet.type === "key");
29749
+ this.validateTimestamp(track, packet.timestamp, packet.type === "key");
29746
29750
  if (writeXingHeader) {
29747
29751
  this.framePositions.push(this.writer.getPos());
29748
29752
  }
@@ -29949,7 +29953,7 @@ ${cue.notes ?? ""}`;
29949
29953
  const release = await this.mutex.acquire();
29950
29954
  try {
29951
29955
  const trackData = this.getTrackData(track, meta);
29952
- this.validateAndNormalizeTimestamp(trackData.track, packet.timestamp, packet.type === "key");
29956
+ this.validateTimestamp(trackData.track, packet.timestamp, packet.type === "key");
29953
29957
  const currentTimestampInSamples = trackData.currentTimestampInSamples;
29954
29958
  const { durationInSamples, vorbisBlockSize } = extractSampleMetadata(
29955
29959
  packet.data,
@@ -30272,7 +30276,7 @@ ${cue.notes ?? ""}`;
30272
30276
  const release = await this.mutex.acquire();
30273
30277
  try {
30274
30278
  const trackData = this.getVideoTrackData(track, meta);
30275
- const timestamp = this.validateAndNormalizeTimestamp(
30279
+ this.validateTimestamp(
30276
30280
  trackData.track,
30277
30281
  packet.timestamp,
30278
30282
  packet.type === "key"
@@ -30283,7 +30287,7 @@ ${cue.notes ?? ""}`;
30283
30287
  }
30284
30288
  trackData.timestampProcessingQueue.push({
30285
30289
  data: preparedData,
30286
- presentationTimestamp: timestamp,
30290
+ presentationTimestamp: packet.timestamp,
30287
30291
  decodeTimestamp: null,
30288
30292
  isKeyframe: packet.type === "key"
30289
30293
  });
@@ -30295,7 +30299,7 @@ ${cue.notes ?? ""}`;
30295
30299
  const release = await this.mutex.acquire();
30296
30300
  try {
30297
30301
  const trackData = this.getAudioTrackData(track, meta);
30298
- const timestamp = this.validateAndNormalizeTimestamp(
30302
+ this.validateTimestamp(
30299
30303
  trackData.track,
30300
30304
  packet.timestamp,
30301
30305
  packet.type === "key"
@@ -30306,7 +30310,7 @@ ${cue.notes ?? ""}`;
30306
30310
  }
30307
30311
  trackData.timestampProcessingQueue.push({
30308
30312
  data: preparedData,
30309
- presentationTimestamp: timestamp,
30313
+ presentationTimestamp: packet.timestamp,
30310
30314
  decodeTimestamp: null,
30311
30315
  isKeyframe: packet.type === "key"
30312
30316
  });
@@ -30779,7 +30783,7 @@ ${cue.notes ?? ""}`;
30779
30783
  this.sampleRate = meta.decoderConfig.sampleRate;
30780
30784
  this.headerWritten = true;
30781
30785
  }
30782
- this.validateAndNormalizeTimestamp(track, packet.timestamp, packet.type === "key");
30786
+ this.validateTimestamp(track, packet.timestamp, packet.type === "key");
30783
30787
  if (!this.isRf64 && this.writer.getPos() + packet.data.byteLength >= 2 ** 32) {
30784
30788
  throw new Error(
30785
30789
  "Adding more audio data would exceed the maximum RIFF size of 4 GiB. To write larger files, use RF64 by setting `large: true` in the WavOutputFormatOptions."
@@ -31205,11 +31209,6 @@ ${cue.notes ?? ""}`;
31205
31209
  this._closingPromise = null;
31206
31210
  /** @internal */
31207
31211
  this._closed = false;
31208
- /**
31209
- * @internal
31210
- * A time offset in seconds that is added to all timestamps generated by this source.
31211
- */
31212
- this._timestampOffset = 0;
31213
31212
  }
31214
31213
  /** @internal */
31215
31214
  _ensureValidAdd() {
@@ -32208,7 +32207,7 @@ ${cue.notes ?? ""}`;
32208
32207
  let frameCount = 0;
32209
32208
  let errored = false;
32210
32209
  let lastSampleTimestamp = null;
32211
- let pauseOffset = 0;
32210
+ let timestampOffset = 0;
32212
32211
  const tick = () => {
32213
32212
  assert(frameRate !== null);
32214
32213
  if (!this._lastVideoFrame) {
@@ -32258,7 +32257,7 @@ ${cue.notes ?? ""}`;
32258
32257
  if (frameSeen) {
32259
32258
  if (lastSampleTimestamp !== null && this._options.timestampBase !== "unix") {
32260
32259
  const timeDelta = currentTimestamp - lastSampleTimestamp;
32261
- pauseOffset -= timeDelta;
32260
+ timestampOffset -= timeDelta;
32262
32261
  }
32263
32262
  lastSampleTimestamp = currentTimestamp;
32264
32263
  }
@@ -32282,7 +32281,7 @@ ${cue.notes ?? ""}`;
32282
32281
  target = now / 1e3 - output._firstMediaStreamTimestamp;
32283
32282
  }
32284
32283
  }
32285
- this._timestampOffset = target - firstVideoFrameTimestamp;
32284
+ timestampOffset = target - firstVideoFrameTimestamp;
32286
32285
  }
32287
32286
  lastSampleTimestamp = currentTimestamp;
32288
32287
  if (this._encoder.getQueueSize() >= 8) {
@@ -32290,7 +32289,7 @@ ${cue.notes ?? ""}`;
32290
32289
  return;
32291
32290
  }
32292
32291
  const sample = new VideoSample(videoFrame, {
32293
- timestamp: currentTimestamp + pauseOffset
32292
+ timestamp: currentTimestamp + timestampOffset
32294
32293
  });
32295
32294
  void this._encoder.add(sample, true).catch((error) => {
32296
32295
  errored = true;
@@ -33018,7 +33017,7 @@ ${cue.notes ?? ""}`;
33018
33017
  let firstAudioDataTimestamp = null;
33019
33018
  let errored = false;
33020
33019
  let lastSampleTimestamp = null;
33021
- let pauseOffset = 0;
33020
+ let timestampOffset = 0;
33022
33021
  const onAudioSample = (audioSample) => {
33023
33022
  if (errored) {
33024
33023
  audioSample.close();
@@ -33030,7 +33029,7 @@ ${cue.notes ?? ""}`;
33030
33029
  if (dataSeen) {
33031
33030
  if (lastSampleTimestamp !== null && this._options.timestampBase !== "unix") {
33032
33031
  const timeDelta = currentTimestamp - lastSampleTimestamp;
33033
- pauseOffset -= timeDelta;
33032
+ timestampOffset -= timeDelta;
33034
33033
  }
33035
33034
  lastSampleTimestamp = currentTimestamp;
33036
33035
  }
@@ -33054,14 +33053,14 @@ ${cue.notes ?? ""}`;
33054
33053
  target = performance.now() / 1e3 - output._firstMediaStreamTimestamp;
33055
33054
  }
33056
33055
  }
33057
- this._timestampOffset = target - firstAudioDataTimestamp;
33056
+ timestampOffset = target - firstAudioDataTimestamp;
33058
33057
  }
33059
33058
  lastSampleTimestamp = currentTimestamp;
33060
33059
  if (this._encoder.getQueueSize() >= 8) {
33061
33060
  audioSample.close();
33062
33061
  return;
33063
33062
  }
33064
- audioSample.setTimestamp(currentTimestamp + pauseOffset);
33063
+ audioSample.setTimestamp(currentTimestamp + timestampOffset);
33065
33064
  void this._encoder.add(audioSample, true).catch((error) => {
33066
33065
  errored = true;
33067
33066
  this._abortController?.abort();
@@ -33663,15 +33662,14 @@ ${cue.notes ?? ""}`;
33663
33662
  const playlist = trackData.playlist;
33664
33663
  const release = await playlist.mutex.acquire();
33665
33664
  try {
33666
- const timestamp = this.validateAndNormalizeTimestamp(track, packet.timestamp, packet.type === "key");
33667
- const adjustedPacket = packet.clone({ timestamp });
33668
- trackData.packets.push(adjustedPacket);
33665
+ this.validateTimestamp(track, packet.timestamp, packet.type === "key");
33666
+ trackData.packets.push(packet);
33669
33667
  if (playlist.currentSegmentStartTimestamp === null) {
33670
- playlist.currentSegmentStartTimestamp = adjustedPacket.timestamp;
33668
+ playlist.currentSegmentStartTimestamp = packet.timestamp;
33671
33669
  } else if (!playlist.currentSegmentStartTimestampIsFixed) {
33672
33670
  playlist.currentSegmentStartTimestamp = Math.min(
33673
33671
  playlist.currentSegmentStartTimestamp,
33674
- adjustedPacket.timestamp
33672
+ packet.timestamp
33675
33673
  );
33676
33674
  }
33677
33675
  await this.advancePlaylist(playlist);
@@ -33684,15 +33682,14 @@ ${cue.notes ?? ""}`;
33684
33682
  const playlist = trackData.playlist;
33685
33683
  const release = await playlist.mutex.acquire();
33686
33684
  try {
33687
- const timestamp = this.validateAndNormalizeTimestamp(track, packet.timestamp, packet.type === "key");
33688
- const adjustedPacket = packet.clone({ timestamp });
33689
- trackData.packets.push(adjustedPacket);
33685
+ this.validateTimestamp(track, packet.timestamp, packet.type === "key");
33686
+ trackData.packets.push(packet);
33690
33687
  if (playlist.currentSegmentStartTimestamp === null) {
33691
- playlist.currentSegmentStartTimestamp = adjustedPacket.timestamp;
33688
+ playlist.currentSegmentStartTimestamp = packet.timestamp;
33692
33689
  } else if (!playlist.currentSegmentStartTimestampIsFixed) {
33693
33690
  playlist.currentSegmentStartTimestamp = Math.min(
33694
33691
  playlist.currentSegmentStartTimestamp,
33695
- adjustedPacket.timestamp
33692
+ packet.timestamp
33696
33693
  );
33697
33694
  }
33698
33695
  await this.advancePlaylist(playlist);