mediabunny 1.55.4 → 1.55.6

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.
@@ -7206,10 +7206,9 @@ var Mediabunny = (() => {
7206
7206
  case "esds":
7207
7207
  {
7208
7208
  const track = this.currentTrack;
7209
- if (!track) {
7209
+ if (!track || track.info?.type !== "audio") {
7210
7210
  break;
7211
7211
  }
7212
- assert(track.info?.type === "audio");
7213
7212
  slice.skip(4);
7214
7213
  const tag = readU8(slice);
7215
7214
  assert(tag === 3);
@@ -17063,6 +17062,10 @@ var Mediabunny = (() => {
17063
17062
  _dispose() {
17064
17063
  }
17065
17064
  };
17065
+ var blobReaderRegistry = typeof FinalizationRegistry !== "undefined" ? new FinalizationRegistry((reader) => {
17066
+ void reader.cancel().catch(() => {
17067
+ });
17068
+ }) : null;
17066
17069
  var BlobSource = class extends Source {
17067
17070
  /**
17068
17071
  * Creates a new {@link BlobSource} backed by the specified
@@ -17090,6 +17093,15 @@ var Mediabunny = (() => {
17090
17093
  maxCacheSize: options.maxCacheSize ?? 8 * 2 ** 20,
17091
17094
  maxWorkerCount: 4,
17092
17095
  runWorker: this._runWorker.bind(this),
17096
+ onIdleWorkerRemoved: (worker) => {
17097
+ const reader = this._readers.get(worker);
17098
+ if (reader) {
17099
+ this._readers.delete(worker);
17100
+ blobReaderRegistry?.unregister(worker);
17101
+ void reader.cancel().catch(() => {
17102
+ });
17103
+ }
17104
+ },
17093
17105
  prefetchProfile: PREFETCH_PROFILES.fileSystem
17094
17106
  });
17095
17107
  this._orchestrator.fileSize = blob.size;
@@ -17110,6 +17122,7 @@ var Mediabunny = (() => {
17110
17122
  if ("stream" in this._blob && !isWebKit() && this._options.useStreamReader !== false) {
17111
17123
  const slice = this._blob.slice(worker.currentPos);
17112
17124
  reader = slice.stream().getReader();
17125
+ blobReaderRegistry?.register(worker, reader, worker);
17113
17126
  } else {
17114
17127
  reader = null;
17115
17128
  }
@@ -18149,6 +18162,7 @@ var Mediabunny = (() => {
18149
18162
  assert(oldestIndex !== null);
18150
18163
  assert(oldestWorker.pendingSlices.length === 0);
18151
18164
  this.workers.splice(oldestIndex, 1);
18165
+ this.options.onIdleWorkerRemoved?.(oldestWorker);
18152
18166
  } else {
18153
18167
  return null;
18154
18168
  }
@@ -18265,6 +18279,7 @@ var Mediabunny = (() => {
18265
18279
  // These should typically be equal when the worker's idle
18266
18280
  )) {
18267
18281
  this.workers.splice(i, 1);
18282
+ this.options.onIdleWorkerRemoved?.(otherWorker);
18268
18283
  i--;
18269
18284
  }
18270
18285
  }
@@ -18319,6 +18334,7 @@ var Mediabunny = (() => {
18319
18334
  assert(index !== -1);
18320
18335
  worker.running = false;
18321
18336
  this.workers.splice(index, 1);
18337
+ this.options.onIdleWorkerRemoved?.(worker);
18322
18338
  if (this.fileSize === null) {
18323
18339
  this.supplyFileSize(worker.currentPos);
18324
18340
  }
@@ -18399,6 +18415,9 @@ var Mediabunny = (() => {
18399
18415
  }
18400
18416
  worker.pendingSlices.length = 0;
18401
18417
  worker.aborted = true;
18418
+ if (!worker.running) {
18419
+ this.options.onIdleWorkerRemoved?.(worker);
18420
+ }
18402
18421
  }
18403
18422
  for (const queuedRead of this.queuedReads) {
18404
18423
  for (const slice of queuedRead.pendingSlices) {
@@ -21903,7 +21922,7 @@ var Mediabunny = (() => {
21903
21922
  }
21904
21923
  const { format, frameCount: optFrameCount, frameOffset: optFrameOffset } = options;
21905
21924
  let { planeIndex } = options;
21906
- const srcFormat = this.format;
21925
+ let srcFormat = this.format;
21907
21926
  const destFormat = format ?? this.format;
21908
21927
  if (!destFormat) throw new Error("Destination format not determined");
21909
21928
  const numFrames = this.numberOfFrames;
@@ -21943,75 +21962,113 @@ var Mediabunny = (() => {
21943
21962
  frameOffset,
21944
21963
  copyFrameCount
21945
21964
  );
21965
+ return;
21946
21966
  } else {
21947
- this._data.copyTo(destination, {
21948
- planeIndex,
21949
- frameOffset,
21950
- frameCount: copyFrameCount,
21951
- format: destFormat
21952
- });
21967
+ try {
21968
+ this._data.copyTo(destination, {
21969
+ planeIndex,
21970
+ frameOffset,
21971
+ frameCount: copyFrameCount,
21972
+ format: destFormat
21973
+ });
21974
+ return;
21975
+ } catch (error) {
21976
+ if (destFormat === "f32-planar") {
21977
+ throw error;
21978
+ }
21979
+ srcFormat = "f32-planar";
21980
+ }
21953
21981
  }
21954
- } else {
21955
- const readFn = getReadFunction(srcFormat);
21956
- const srcBytesPerSample = getBytesPerSample(srcFormat);
21957
- const srcIsPlanar = formatIsPlanar(srcFormat);
21958
- let uint8Data;
21959
- if (this._data instanceof AudioSampleResource) {
21960
- const getDataPlaneValidated = (index) => {
21961
- const result = this._data.getDataPlane(index);
21962
- if (!(result instanceof Uint8Array)) {
21963
- throw new TypeError("getDataPlane() must return a Uint8Array.");
21964
- }
21965
- const expectedSize = numFrames * srcBytesPerSample * (srcIsPlanar ? 1 : numChannels);
21966
- if (result.byteLength !== expectedSize) {
21967
- throw new TypeError(
21968
- `Data plane ${index} has invalid size. Expected exactly ${expectedSize} bytes, got ${result.byteLength} bytes.`
21969
- );
21982
+ }
21983
+ const readFn = getReadFunction(srcFormat);
21984
+ const srcBytesPerSample = getBytesPerSample(srcFormat);
21985
+ const srcIsPlanar = formatIsPlanar(srcFormat);
21986
+ let uint8Data;
21987
+ if (this._data instanceof AudioSampleResource) {
21988
+ const getDataPlaneValidated = (index) => {
21989
+ const result = this._data.getDataPlane(index);
21990
+ if (!(result instanceof Uint8Array)) {
21991
+ throw new TypeError("getDataPlane() must return a Uint8Array.");
21992
+ }
21993
+ const expectedSize = numFrames * srcBytesPerSample * (srcIsPlanar ? 1 : numChannels);
21994
+ if (result.byteLength !== expectedSize) {
21995
+ throw new TypeError(
21996
+ `Data plane ${index} has invalid size. Expected exactly ${expectedSize} bytes, got ${result.byteLength} bytes.`
21997
+ );
21998
+ }
21999
+ return result;
22000
+ };
22001
+ if (srcIsPlanar) {
22002
+ if (destIsPlanar) {
22003
+ uint8Data = getDataPlaneValidated(planeIndex);
22004
+ planeIndex = 0;
22005
+ } else {
22006
+ uint8Data = new Uint8Array(numFrames * srcBytesPerSample * numChannels);
22007
+ for (let ch = 0; ch < numChannels; ch++) {
22008
+ const planeData = getDataPlaneValidated(ch);
22009
+ uint8Data.set(planeData, ch * numFrames * srcBytesPerSample);
21970
22010
  }
21971
- return result;
21972
- };
21973
- if (srcIsPlanar) {
21974
- if (destIsPlanar) {
21975
- uint8Data = getDataPlaneValidated(planeIndex);
21976
- planeIndex = 0;
21977
- } else {
21978
- uint8Data = new Uint8Array(numFrames * srcBytesPerSample * numChannels);
21979
- for (let ch = 0; ch < numChannels; ch++) {
21980
- const planeData = getDataPlaneValidated(ch);
21981
- uint8Data.set(planeData, ch * numFrames * srcBytesPerSample);
22011
+ }
22012
+ } else {
22013
+ uint8Data = getDataPlaneValidated(0);
22014
+ }
22015
+ } else if (this._data instanceof Uint8Array) {
22016
+ uint8Data = this._data;
22017
+ } else {
22018
+ assert(srcFormat === "f32-planar");
22019
+ if (destIsPlanar) {
22020
+ uint8Data = new Uint8Array(this._data.allocationSize({
22021
+ format: "f32-planar",
22022
+ planeIndex
22023
+ }));
22024
+ this._data.copyTo(uint8Data, {
22025
+ format: "f32-planar",
22026
+ planeIndex
22027
+ });
22028
+ planeIndex = 0;
22029
+ } else {
22030
+ uint8Data = new Uint8Array(this._data.allocationSize({
22031
+ format: "f32-planar",
22032
+ planeIndex: 0
22033
+ }) * numChannels);
22034
+ for (let ch = 0; ch < numChannels; ch++) {
22035
+ this._data.copyTo(
22036
+ uint8Data.subarray(
22037
+ ch * numFrames * srcBytesPerSample,
22038
+ (ch + 1) * numFrames * srcBytesPerSample
22039
+ ),
22040
+ {
22041
+ format: "f32-planar",
22042
+ planeIndex: ch
21982
22043
  }
21983
- }
22044
+ );
22045
+ }
22046
+ }
22047
+ }
22048
+ const srcView = toDataView(uint8Data);
22049
+ for (let i = 0; i < copyFrameCount; i++) {
22050
+ if (destIsPlanar) {
22051
+ const destOffset = i * destBytesPerSample;
22052
+ let srcOffset;
22053
+ if (srcIsPlanar) {
22054
+ srcOffset = (planeIndex * numFrames + (i + frameOffset)) * srcBytesPerSample;
21984
22055
  } else {
21985
- uint8Data = getDataPlaneValidated(0);
22056
+ srcOffset = ((i + frameOffset) * numChannels + planeIndex) * srcBytesPerSample;
21986
22057
  }
22058
+ const normalized = readFn(srcView, srcOffset);
22059
+ writeFn(destView, destOffset, normalized);
21987
22060
  } else {
21988
- uint8Data = this._data;
21989
- }
21990
- const srcView = toDataView(uint8Data);
21991
- for (let i = 0; i < copyFrameCount; i++) {
21992
- if (destIsPlanar) {
21993
- const destOffset = i * destBytesPerSample;
22061
+ for (let ch = 0; ch < numChannels; ch++) {
22062
+ const destIndex = i * numChannels + ch;
22063
+ const destOffset = destIndex * destBytesPerSample;
21994
22064
  let srcOffset;
21995
22065
  if (srcIsPlanar) {
21996
- srcOffset = (planeIndex * numFrames + (i + frameOffset)) * srcBytesPerSample;
22066
+ srcOffset = (ch * numFrames + (i + frameOffset)) * srcBytesPerSample;
21997
22067
  } else {
21998
- srcOffset = ((i + frameOffset) * numChannels + planeIndex) * srcBytesPerSample;
22068
+ srcOffset = ((i + frameOffset) * numChannels + ch) * srcBytesPerSample;
21999
22069
  }
22000
22070
  const normalized = readFn(srcView, srcOffset);
22001
22071
  writeFn(destView, destOffset, normalized);
22002
- } else {
22003
- for (let ch = 0; ch < numChannels; ch++) {
22004
- const destIndex = i * numChannels + ch;
22005
- const destOffset = destIndex * destBytesPerSample;
22006
- let srcOffset;
22007
- if (srcIsPlanar) {
22008
- srcOffset = (ch * numFrames + (i + frameOffset)) * srcBytesPerSample;
22009
- } else {
22010
- srcOffset = ((i + frameOffset) * numChannels + ch) * srcBytesPerSample;
22011
- }
22012
- const normalized = readFn(srcView, srcOffset);
22013
- writeFn(destView, destOffset, normalized);
22014
- }
22015
22072
  }
22016
22073
  }
22017
22074
  }
@@ -25702,7 +25759,7 @@ var Mediabunny = (() => {
25702
25759
  if (!options || typeof options !== "object") {
25703
25760
  throw new TypeError("options must be an object.");
25704
25761
  }
25705
- if (options.targetPacketCount !== void 0 && (!Number.isFinite(options.targetPacketCount) || options.targetPacketCount < 0)) {
25762
+ if (options.targetPacketCount !== void 0 && (!isNumber(options.targetPacketCount) || options.targetPacketCount < 0)) {
25706
25763
  throw new TypeError("options.targetPacketCount must be a non-negative number.");
25707
25764
  }
25708
25765
  const timeResolution = await this.getTimeResolution();
@@ -37729,7 +37786,7 @@ ${cue.notes ?? ""}`;
37729
37786
  "options.fastStart, when provided, must be false, 'in-memory', 'reserve', or 'fragmented'."
37730
37787
  );
37731
37788
  }
37732
- if (options.minimumFragmentDuration !== void 0 && (!Number.isFinite(options.minimumFragmentDuration) || options.minimumFragmentDuration < 0)) {
37789
+ if (options.minimumFragmentDuration !== void 0 && (!isNumber(options.minimumFragmentDuration) || options.minimumFragmentDuration < 0)) {
37733
37790
  throw new TypeError("options.minimumFragmentDuration, when provided, must be a non-negative number.");
37734
37791
  }
37735
37792
  if (options.onFtyp !== void 0 && typeof options.onFtyp !== "function") {
@@ -37889,7 +37946,7 @@ ${cue.notes ?? ""}`;
37889
37946
  if (options.appendOnly !== void 0 && typeof options.appendOnly !== "boolean") {
37890
37947
  throw new TypeError("options.appendOnly, when provided, must be a boolean.");
37891
37948
  }
37892
- if (options.minimumClusterDuration !== void 0 && (!Number.isFinite(options.minimumClusterDuration) || options.minimumClusterDuration < 0)) {
37949
+ if (options.minimumClusterDuration !== void 0 && (!isNumber(options.minimumClusterDuration) || options.minimumClusterDuration < 0)) {
37893
37950
  throw new TypeError("options.minimumClusterDuration, when provided, must be a non-negative number.");
37894
37951
  }
37895
37952
  if (options.onEbmlHeader !== void 0 && typeof options.onEbmlHeader !== "function") {
@@ -38079,7 +38136,7 @@ ${cue.notes ?? ""}`;
38079
38136
  if (!options || typeof options !== "object") {
38080
38137
  throw new TypeError("options must be an object.");
38081
38138
  }
38082
- if (options.maximumPageDuration !== void 0 && (!Number.isFinite(options.maximumPageDuration) || options.maximumPageDuration <= 0)) {
38139
+ if (options.maximumPageDuration !== void 0 && (!isNumber(options.maximumPageDuration) || options.maximumPageDuration <= 0)) {
38083
38140
  throw new TypeError("options.maximumPageDuration, when provided, must be a positive number.");
38084
38141
  }
38085
38142
  if (options.onPage !== void 0 && typeof options.onPage !== "function") {