@blockcast/mmt-container 2.2.0-main.b4ce3986f2dd → 2.2.0-main.c1482ff09fa3

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 (67) hide show
  1. package/dist/browser-runtime.js +1300 -180
  2. package/dist/browser-runtime.js.map +3 -3
  3. package/dist/cmaf-block-assembler.js +92 -1
  4. package/dist/cmaf-block-assembler.js.map +2 -2
  5. package/dist/codec-utils.d.ts.map +1 -1
  6. package/dist/codec-utils.js +92 -1
  7. package/dist/codec-utils.js.map +2 -2
  8. package/dist/delivery-catalog.js +2 -1
  9. package/dist/delivery-catalog.js.map +2 -2
  10. package/dist/delta-fallback-config.js.map +1 -1
  11. package/dist/fec-manager.d.ts +74 -14
  12. package/dist/fec-manager.d.ts.map +1 -1
  13. package/dist/fec-manager.js +419 -61
  14. package/dist/fec-manager.js.map +2 -2
  15. package/dist/fec-worker-setup.js.map +1 -1
  16. package/dist/index.d.ts +2 -2
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js.map +2 -2
  19. package/dist/iso-block-locator.d.ts +48 -13
  20. package/dist/iso-block-locator.d.ts.map +1 -1
  21. package/dist/iso-block-locator.js +97 -25
  22. package/dist/iso-block-locator.js.map +2 -2
  23. package/dist/iso-bmff-timing.d.ts +10 -1
  24. package/dist/iso-bmff-timing.d.ts.map +1 -1
  25. package/dist/iso-bmff-timing.js +36 -2
  26. package/dist/iso-bmff-timing.js.map +2 -2
  27. package/dist/media-router.js.map +1 -1
  28. package/dist/mmtp-container-parser.d.ts +77 -10
  29. package/dist/mmtp-container-parser.d.ts.map +1 -1
  30. package/dist/mmtp-container-parser.js +295 -27
  31. package/dist/mmtp-container-parser.js.map +2 -2
  32. package/dist/mmtp-container-parser.no-modules.js +297 -30
  33. package/dist/mmtp-container-parser.no-modules.js.map +3 -3
  34. package/dist/mmtp-decode-pipeline.js +294 -27
  35. package/dist/mmtp-decode-pipeline.js.map +2 -2
  36. package/dist/mmtp-router.d.ts +34 -8
  37. package/dist/mmtp-router.d.ts.map +1 -1
  38. package/dist/mmtp-router.js +619 -75
  39. package/dist/mmtp-router.js.map +3 -3
  40. package/dist/multicast-manager.d.ts +26 -3
  41. package/dist/multicast-manager.d.ts.map +1 -1
  42. package/dist/multicast-manager.js +1120 -153
  43. package/dist/multicast-manager.js.map +3 -3
  44. package/dist/multicast-transport.js +4 -3
  45. package/dist/multicast-transport.js.map +2 -2
  46. package/dist/no-modules.js +294 -27
  47. package/dist/no-modules.js.map +3 -3
  48. package/dist/repair-fetch.d.ts +38 -8
  49. package/dist/repair-fetch.d.ts.map +1 -1
  50. package/dist/repair-fetch.js +286 -27
  51. package/dist/repair-fetch.js.map +3 -3
  52. package/dist/track-session.d.ts.map +1 -1
  53. package/dist/track-session.js +1 -0
  54. package/dist/track-session.js.map +2 -2
  55. package/dist/wasm-mmtp-container-parser.js +294 -27
  56. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  57. package/package.json +5 -5
  58. package/src/codec-utils.ts +121 -1
  59. package/src/fec-manager.ts +454 -35
  60. package/src/index.ts +3 -0
  61. package/src/iso-block-locator.ts +133 -31
  62. package/src/iso-bmff-timing.ts +40 -1
  63. package/src/mmtp-container-parser.ts +294 -29
  64. package/src/mmtp-router.ts +544 -55
  65. package/src/multicast-manager.ts +118 -14
  66. package/src/repair-fetch.ts +366 -50
  67. package/src/track-session.ts +3 -0
@@ -4650,9 +4650,10 @@ var BlockcastMmt = (() => {
4650
4650
  const sliceType = readH264SliceType(nal);
4651
4651
  return sliceType === 2 || sliceType === 4 || sliceType === 7 || sliceType === 9;
4652
4652
  }
4653
+ var H264_SLICE_TYPE_ESCAPED_BYTES = 32;
4653
4654
  function readH264SliceType(nal) {
4654
4655
  if (nal.length < 2 || (nal[0] & 31) !== 1) return null;
4655
- const rbsp = removeEmulationPrevention(nal.subarray(1));
4656
+ const rbsp = removeEmulationPrevention(nal.subarray(1, 1 + H264_SLICE_TYPE_ESCAPED_BYTES));
4656
4657
  const state = { bitOffset: 0 };
4657
4658
  if (readUnsignedExpGolomb(rbsp, state) === null) return null;
4658
4659
  return readUnsignedExpGolomb(rbsp, state);
@@ -4799,6 +4800,96 @@ var BlockcastMmt = (() => {
4799
4800
  ]))).toBe(false);
4800
4801
  });
4801
4802
  });
4803
+ describe("readH264SliceType", () => {
4804
+ const wholeNalSliceType = (nal) => {
4805
+ if (nal.length < 2 || (nal[0] & 31) !== 1) return null;
4806
+ const rbsp = removeEmulationPrevention(nal.subarray(1));
4807
+ const state = { bitOffset: 0 };
4808
+ if (readUnsignedExpGolomb(rbsp, state) === null) return null;
4809
+ return readUnsignedExpGolomb(rbsp, state);
4810
+ };
4811
+ const hex2 = (text2) => Uint8Array.from(text2.match(/../g), (byte) => parseInt(byte, 16));
4812
+ const ue = (value) => {
4813
+ const code = (value + 1).toString(2);
4814
+ return "0".repeat(code.length - 1) + code;
4815
+ };
4816
+ const sliceNal = (bits) => {
4817
+ const rbsp = `${bits}1`.padEnd(Math.ceil((bits.length + 1) / 8) * 8, "0").match(/.{8}/g);
4818
+ const nal = [1];
4819
+ let zeros = 0;
4820
+ for (const byte of rbsp.map((bits8) => parseInt(bits8, 2))) {
4821
+ if (zeros >= 2 && byte <= 3) {
4822
+ nal.push(3);
4823
+ zeros = 0;
4824
+ }
4825
+ nal.push(byte);
4826
+ zeros = byte === 0 ? zeros + 1 : 0;
4827
+ }
4828
+ return Uint8Array.from(nal);
4829
+ };
4830
+ const x264Slices = [
4831
+ ["64x48 P", hex2("419a231885bfd53888f63ef5b1e1e39b9ff2eabf03fd3f4d7d4317ffcbf75176"), 5, false],
4832
+ ["64x48 B", hex2("419e414212fffeeade283707cb70f7"), 6, false],
4833
+ ["64x48 non-IDR I", hex2(
4834
+ "4128899037f384db2be0cfe48aa23558781dd429f7db8e8a6ae44f7ca2333ba1029477da6ddfeac61843c5680cbf66247469d64549b0f5f7ca6acc529f5cf032b35d8dedc6b2412ece24f42f80b593fc0f3ff47a08c84799ff2ada18ac39a527e23eb34c8167fd"
4835
+ ), 7, true],
4836
+ ["1280x720 B, escape at byte 10", hex2(
4837
+ "01006e27a81109ff000003016b662ea5ea46000f57880f3fb0d3f03ff7dac0546fc9966a5f6d86cb795db178882f6e19b75d92170917306190f262fd58268702db2eff86a9651e48"
4838
+ ), 6, false],
4839
+ ["1280x720 P, escape at byte 15", hex2(
4840
+ "41006e26a8cd1329e1bffea784000003002b5ef096f0320bd99f00037f22327e886223b7996bb04acd35ad9a0385f02c1801eb6226bc94993d1880172ffbb23db74729569b6f3934cba0cba17b7c29ce0e216080cb7b18e5876df59e85302da9cba203fe6769178a2e02eeaa54a6f87568cbaa019a3442eae19c8a7adc8cd02441bee573c5408b6552abd2d9d3"
4841
+ ), 5, false],
4842
+ ["1280x720 B, escape at byte 22", hex2("01006e27da9109ff01a4c1dbc27ed915a3172ec80000030352"), 6, false],
4843
+ ["7680x4320 IDR, first_mb_in_slice 97440", hex2(
4844
+ "650000be5088843a118a000218f1c00040f63800087949c9c9c9c9c9c9c9c9c9c9c9c9c9c9c9c9c9"
4845
+ ), null, true],
4846
+ ["7680x4320 P, first_mb_in_slice 32640", hex2(
4847
+ "410003fc09a20a806ff0ae0085a611cc7ad5f2020e25900ba4c3398016b57e100011021220200020"
4848
+ ), 5, false],
4849
+ ["7680x4320 P, first_mb_in_slice 97440", hex2(
4850
+ "410000be509a20a8078f5a83cc40d800783b017f5c6d7e4883fb4801a9c569245bd04135f7c00806"
4851
+ ), 5, false]
4852
+ ];
4853
+ const syntheticSlices = [
4854
+ ["an escape inside first_mb_in_slice", sliceNal(ue(2 ** 24 - 1) + ue(2)), 2],
4855
+ ["an escape between the fields", sliceNal(ue(2 ** 21 - 1) + ue(7)), 7],
4856
+ ["an escape inside slice_type", sliceNal(ue(0) + ue(2 ** 22 - 1)), 2 ** 22 - 1],
4857
+ ["the longest fields, five escapes", sliceNal(ue(2 ** 30 - 1) + ue(2 ** 30 - 1)), 2 ** 30 - 1],
4858
+ ["the largest values", sliceNal(ue(2 ** 31 - 2) + ue(2 ** 31 - 2)), 2 ** 31 - 2],
4859
+ ["31 leading zeros in first_mb_in_slice", sliceNal(`${"0".repeat(31)}1`), null],
4860
+ ["31 leading zeros in slice_type", sliceNal(`1${"0".repeat(31)}1`), null],
4861
+ ["escapes running past the slice header", sliceNal(ue(0) + ue(5) + "0".repeat(400)), 5]
4862
+ ];
4863
+ test("reads the slice_type of real and synthetic slices", () => {
4864
+ for (const [name, nal, sliceType] of [...x264Slices, ...syntheticSlices]) {
4865
+ expect(readH264SliceType(nal), name).toBe(sliceType);
4866
+ }
4867
+ });
4868
+ test("matches the whole-NAL unescape at every truncation", () => {
4869
+ for (const [name, nal] of [...x264Slices, ...syntheticSlices]) {
4870
+ for (let end = 0; end <= nal.length; end++) {
4871
+ const cut = nal.subarray(0, end);
4872
+ expect(readH264SliceType(cut), `${name}, first ${end} bytes`).toBe(wholeNalSliceType(cut));
4873
+ }
4874
+ }
4875
+ });
4876
+ test("matches the whole-NAL unescape on zero-heavy NAL units", () => {
4877
+ const bytes = [0, 0, 0, 3, 1, 2, 128, 255];
4878
+ let seed = 1;
4879
+ const next = () => (seed = Math.imul(seed, 1103515245) + 12345 >>> 0) >>> 16;
4880
+ for (let i = 0; i < 5e3; i++) {
4881
+ const nal = new Uint8Array(2 + next() % 63);
4882
+ nal[0] = 65;
4883
+ for (let j = 1; j < nal.length; j++) nal[j] = bytes[next() % bytes.length];
4884
+ expect(readH264SliceType(nal), Array.from(nal).join()).toBe(wholeNalSliceType(nal));
4885
+ }
4886
+ });
4887
+ test("classifies the x264 slices as keyframes or not", () => {
4888
+ for (const [name, nal, , keyframe] of x264Slices) {
4889
+ expect(classifyH264Keyframe(rawH264NalToAvcc(nal), { allowPartialAvcc: true }), name).toBe(keyframe);
4890
+ }
4891
+ });
4892
+ });
4802
4893
  describe("avccToAnnexB", () => {
4803
4894
  test("converts with SPS/PPS on keyframe", () => {
4804
4895
  const avcc = new Uint8Array([0, 0, 0, 3, 101, 170, 187]);
@@ -5633,13 +5724,21 @@ var BlockcastMmt = (() => {
5633
5724
  function sameSample(a, b) {
5634
5725
  return a.decodeTime === b.decodeTime && a.presentationTime === b.presentationTime && a.duration === b.duration && a.timescale === b.timescale && a.size === b.size && a.bytePosition === b.bytePosition;
5635
5726
  }
5636
- var _track, _fragments, _pendingSamples, _evictedSamples, _lastMovieFragmentRejection, _IsoBmffTimingResolver_instances, evictOldestFragment_fn, clearSlot_fn;
5727
+ var _track, _fragments, _firstSamples, _delivered, _pendingSamples, _evictedSamples, _lastMovieFragmentRejection, _IsoBmffTimingResolver_instances, evictOldestFragment_fn, clearSlot_fn;
5637
5728
  var IsoBmffTimingResolver = class {
5638
5729
  constructor(maxPendingSamples) {
5639
5730
  this.maxPendingSamples = maxPendingSamples;
5640
5731
  __privateAdd(this, _IsoBmffTimingResolver_instances);
5641
5732
  __privateAdd(this, _track, null);
5642
5733
  __privateAdd(this, _fragments, /* @__PURE__ */ new Map());
5734
+ /** First sample of each pending fragment, kept in `#delivered` with its sample count. */
5735
+ __privateAdd(this, _firstSamples, /* @__PURE__ */ new Map());
5736
+ /**
5737
+ * First sample and sample count of the newest fragments whose every slot was
5738
+ * cleared. Abandonment counts: `releaseSample` clears a slot too, which
5739
+ * matches the combiner, since it retires an abandoned sample as well.
5740
+ */
5741
+ __privateAdd(this, _delivered, /* @__PURE__ */ new Map());
5643
5742
  __privateAdd(this, _pendingSamples, 0);
5644
5743
  __privateAdd(this, _evictedSamples, 0);
5645
5744
  __privateAdd(this, _lastMovieFragmentRejection, null);
@@ -5647,6 +5746,10 @@ var BlockcastMmt = (() => {
5647
5746
  throw new Error("[IsoBmffTimingResolver] maxPendingSamples must be a positive integer");
5648
5747
  }
5649
5748
  }
5749
+ /** True once an FT=0 has bound the track that FT=1 timing is read against. */
5750
+ get initialized() {
5751
+ return __privateGet(this, _track) !== null;
5752
+ }
5650
5753
  get pendingSampleCount() {
5651
5754
  return __privateGet(this, _pendingSamples);
5652
5755
  }
@@ -5670,7 +5773,14 @@ var BlockcastMmt = (() => {
5670
5773
  __privateSet(this, _track, timing);
5671
5774
  return true;
5672
5775
  }
5673
- addMovieFragment(mpuSequenceNumber, data, additionalSampleLimit = this.maxPendingSamples) {
5776
+ /**
5777
+ * `recovered` marks an FEC-recovered copy. A recovered block hands back
5778
+ * every FT=1 it holds, the received ones included, so a recovered copy of a
5779
+ * fragment whose samples are all done is a duplicate: admitting it again
5780
+ * would charge stale timing and could displace the live MPU's. A source
5781
+ * copy is still admitted again (a rendition switch re-arms timing that way).
5782
+ */
5783
+ addMovieFragment(mpuSequenceNumber, data, additionalSampleLimit = this.maxPendingSamples, recovered = false) {
5674
5784
  __privateSet(this, _lastMovieFragmentRejection, null);
5675
5785
  if (!Number.isInteger(additionalSampleLimit) || additionalSampleLimit < 0) {
5676
5786
  __privateSet(this, _lastMovieFragmentRejection, "invalid");
@@ -5696,6 +5806,8 @@ var BlockcastMmt = (() => {
5696
5806
  (sample, index) => sample !== null && sameSample(sample, parsed.samples[index])
5697
5807
  );
5698
5808
  }
5809
+ const delivered = recovered ? __privateGet(this, _delivered).get(key) : void 0;
5810
+ if (delivered && delivered.sampleCount === parsed.samples.length && sameSample(delivered.first, parsed.samples[0])) return true;
5699
5811
  let freed = 0;
5700
5812
  while (__privateGet(this, _fragments).size > 0 && (__privateGet(this, _pendingSamples) + parsed.samples.length > this.maxPendingSamples || parsed.samples.length - freed > additionalSampleLimit)) {
5701
5813
  freed += __privateMethod(this, _IsoBmffTimingResolver_instances, evictOldestFragment_fn).call(this);
@@ -5709,6 +5821,7 @@ var BlockcastMmt = (() => {
5709
5821
  return false;
5710
5822
  }
5711
5823
  __privateGet(this, _fragments).set(key, [...parsed.samples]);
5824
+ __privateGet(this, _firstSamples).set(key, parsed.samples[0]);
5712
5825
  __privateSet(this, _pendingSamples, __privateGet(this, _pendingSamples) + parsed.samples.length);
5713
5826
  return true;
5714
5827
  }
@@ -5755,11 +5868,15 @@ var BlockcastMmt = (() => {
5755
5868
  reset() {
5756
5869
  __privateSet(this, _track, null);
5757
5870
  __privateGet(this, _fragments).clear();
5871
+ __privateGet(this, _firstSamples).clear();
5872
+ __privateGet(this, _delivered).clear();
5758
5873
  __privateSet(this, _pendingSamples, 0);
5759
5874
  }
5760
5875
  };
5761
5876
  _track = new WeakMap();
5762
5877
  _fragments = new WeakMap();
5878
+ _firstSamples = new WeakMap();
5879
+ _delivered = new WeakMap();
5763
5880
  _pendingSamples = new WeakMap();
5764
5881
  _evictedSamples = new WeakMap();
5765
5882
  _lastMovieFragmentRejection = new WeakMap();
@@ -5778,6 +5895,7 @@ var BlockcastMmt = (() => {
5778
5895
  let live = 0;
5779
5896
  for (const sample of samples) if (sample !== null) live++;
5780
5897
  __privateGet(this, _fragments).delete(oldest);
5898
+ __privateGet(this, _firstSamples).delete(oldest);
5781
5899
  __privateSet(this, _pendingSamples, __privateGet(this, _pendingSamples) - live);
5782
5900
  __privateSet(this, _evictedSamples, __privateGet(this, _evictedSamples) + live);
5783
5901
  return live;
@@ -5793,7 +5911,16 @@ var BlockcastMmt = (() => {
5793
5911
  if (!samples || !samples[index]) return false;
5794
5912
  samples[index] = null;
5795
5913
  __privateWrapper(this, _pendingSamples)._--;
5796
- if (samples.every((value) => value === null)) __privateGet(this, _fragments).delete(key);
5914
+ if (samples.every((value) => value === null)) {
5915
+ __privateGet(this, _fragments).delete(key);
5916
+ const first = __privateGet(this, _firstSamples).get(key);
5917
+ __privateGet(this, _firstSamples).delete(key);
5918
+ __privateGet(this, _delivered).delete(key);
5919
+ __privateGet(this, _delivered).set(key, { first, sampleCount: samples.length });
5920
+ if (__privateGet(this, _delivered).size > this.maxPendingSamples) {
5921
+ __privateGet(this, _delivered).delete(__privateGet(this, _delivered).keys().next().value);
5922
+ }
5923
+ }
5797
5924
  return true;
5798
5925
  };
5799
5926
  function fragmentKey(mpuSequenceNumber, movieFragmentSequenceNumber) {
@@ -5969,7 +6096,10 @@ var BlockcastMmt = (() => {
5969
6096
  function ssIdAfter(a, b) {
5970
6097
  return (a - b | 0) > 0;
5971
6098
  }
5972
- var _streams2, _gate, _emit2, _recordStat2, _deferDepth, _deferredStreams, _FecReleaseSequencer_instances, drainUnlessDeferred_fn, streamFor_fn2, drain_fn, release_fn;
6099
+ function holdsRebuilt(stream) {
6100
+ return stream.buf.some(({ meta }) => meta.fecSbnFirst !== void 0);
6101
+ }
6102
+ var _streams2, _gate, _emit2, _recordStat2, _deferDepth, _deferredStreams, _FecReleaseSequencer_instances, drainUnlessDeferred_fn, streamFor_fn2, drain_fn, drainLoop_fn, cursorPassed_fn, release_fn;
5973
6103
  var FecReleaseSequencer = class {
5974
6104
  constructor(gate, emit, recordStat) {
5975
6105
  __privateAdd(this, _FecReleaseSequencer_instances);
@@ -6029,6 +6159,7 @@ var BlockcastMmt = (() => {
6029
6159
  stream.lastSbn = void 0;
6030
6160
  stream.discontinuity = false;
6031
6161
  stream.rapSsId = void 0;
6162
+ stream.sourced = false;
6032
6163
  }
6033
6164
  if (stream.last !== null && compareFrameOrder(frame, stream.last) <= 0) {
6034
6165
  __privateGet(this, _recordStat2).call(this, "droppedStale");
@@ -6055,6 +6186,7 @@ var BlockcastMmt = (() => {
6055
6186
  if (compareFrameOrder(stream.buf[mid], frame) < 0) lo = mid + 1;
6056
6187
  else hi = mid;
6057
6188
  }
6189
+ if (stream.buf[lo]?.passed === true) frame.passed = true;
6058
6190
  stream.buf.splice(lo, 0, frame);
6059
6191
  __privateMethod(this, _FecReleaseSequencer_instances, drainUnlessDeferred_fn).call(this, stream);
6060
6192
  }
@@ -6090,6 +6222,41 @@ var BlockcastMmt = (() => {
6090
6222
  }
6091
6223
  return false;
6092
6224
  }
6225
+ /**
6226
+ * These streams' sink is being replaced by one that has seen no source
6227
+ * frame yet. First, from a stream that holds a frame the overlay rebuilt,
6228
+ * release to the sink being replaced the frames the unanchored path had
6229
+ * already released to it (#cursorPassed): there they waited in its queue,
6230
+ * which its replacement flushes. A stream that holds none is not drained:
6231
+ * it holds nothing the anchor kept back, and a drain would still force out
6232
+ * a frame held past the hold limit, which before the anchor waited for the
6233
+ * next push. Then treat the streams as unsourced until a source frame is
6234
+ * released.
6235
+ */
6236
+ unsource(packetId, catalogTrackName) {
6237
+ for (const stream of __privateGet(this, _streams2).values()) {
6238
+ if (stream.packetId === packetId && (catalogTrackName === void 0 || stream.catalogTrackName === catalogTrackName)) {
6239
+ if (stream.draining === 0 && holdsRebuilt(stream)) {
6240
+ if (__privateGet(this, _deferDepth) > 0) __privateGet(this, _deferredStreams).add(stream);
6241
+ else __privateMethod(this, _FecReleaseSequencer_instances, drain_fn).call(this, stream, true);
6242
+ }
6243
+ stream.sourced = false;
6244
+ }
6245
+ }
6246
+ }
6247
+ /**
6248
+ * Whether a stream of `packetId` holds a frame the keyframe overlay rebuilt
6249
+ * (FrameMeta.fecSbnFirst). Without `catalogTrackName`, every stream of the
6250
+ * packetId.
6251
+ */
6252
+ holdsRebuilt(packetId, catalogTrackName) {
6253
+ for (const stream of __privateGet(this, _streams2).values()) {
6254
+ if (stream.packetId !== packetId) continue;
6255
+ if (catalogTrackName !== void 0 && stream.catalogTrackName !== catalogTrackName) continue;
6256
+ if (holdsRebuilt(stream)) return true;
6257
+ }
6258
+ return false;
6259
+ }
6093
6260
  /** Re-evaluate held gaps after FEC state changed. */
6094
6261
  poke(streamKey) {
6095
6262
  if (streamKey !== void 0) {
@@ -6193,16 +6360,32 @@ var BlockcastMmt = (() => {
6193
6360
  last: null,
6194
6361
  lastSbn: void 0,
6195
6362
  discontinuity: false,
6196
- rapSsId: void 0
6363
+ rapSsId: void 0,
6364
+ sourced: false,
6365
+ draining: 0
6197
6366
  };
6198
6367
  __privateGet(this, _streams2).set(streamKey, stream);
6199
6368
  }
6200
6369
  return stream;
6201
6370
  };
6202
- drain_fn = function(stream) {
6371
+ /** `asUnanchored`: release overlay-rebuilt frames when unanchored did (#cursorPassed). */
6372
+ drain_fn = function(stream, asUnanchored = false) {
6373
+ stream.draining++;
6374
+ try {
6375
+ __privateMethod(this, _FecReleaseSequencer_instances, drainLoop_fn).call(this, stream, asUnanchored);
6376
+ } finally {
6377
+ stream.draining--;
6378
+ }
6379
+ };
6380
+ drainLoop_fn = function(stream, asUnanchored) {
6203
6381
  while (stream.buf.length > 0 && stream.last !== null) {
6204
6382
  const head = stream.buf[0];
6205
6383
  const last = stream.last;
6384
+ if (compareFrameOrder(head, last) <= 0) {
6385
+ stream.buf.shift();
6386
+ __privateMethod(this, _FecReleaseSequencer_instances, release_fn).call(this, stream, head, void 0);
6387
+ continue;
6388
+ }
6206
6389
  if (stream.discontinuity) {
6207
6390
  stream.discontinuity = false;
6208
6391
  stream.buf.shift();
@@ -6216,9 +6399,10 @@ var BlockcastMmt = (() => {
6216
6399
  }
6217
6400
  const headSbn = head.meta.fecSbn;
6218
6401
  const from = stream.lastSbn ?? headSbn;
6219
- const to = headSbn ?? stream.lastSbn;
6402
+ const to = head.meta.fecSbnFirst ?? headSbn ?? stream.lastSbn;
6220
6403
  let lost = [];
6221
6404
  const unanchored = from === void 0 && to === void 0;
6405
+ let unresolved = false;
6222
6406
  if (from !== void 0 && to !== void 0) {
6223
6407
  const resolved = __privateGet(this, _gate).resolvedLoss(
6224
6408
  stream.packetId,
@@ -6227,7 +6411,8 @@ var BlockcastMmt = (() => {
6227
6411
  Math.max(from, to),
6228
6412
  from
6229
6413
  );
6230
- if (resolved === null) {
6414
+ unresolved = resolved === null;
6415
+ if (unresolved && !__privateMethod(this, _FecReleaseSequencer_instances, cursorPassed_fn).call(this, stream, asUnanchored)) {
6231
6416
  const heldMs = performance.now() - head.arrivedAtMs;
6232
6417
  if (heldMs <= __privateGet(this, _gate).holdLimitMs(stream.packetId, stream.catalogTrackName)) return;
6233
6418
  __privateGet(this, _recordStat2).call(this, "forcedWindowFlushes");
@@ -6235,18 +6420,52 @@ var BlockcastMmt = (() => {
6235
6420
  __privateMethod(this, _FecReleaseSequencer_instances, release_fn).call(this, stream, head, __privateGet(this, _gate).decide(last, head, [], true));
6236
6421
  continue;
6237
6422
  }
6238
- lost = resolved;
6423
+ lost = resolved ?? [];
6239
6424
  }
6240
6425
  stream.buf.shift();
6241
- __privateMethod(this, _FecReleaseSequencer_instances, release_fn).call(this, stream, head, lost.length > 0 || unanchored || authoredGap(last.meta, head.meta) ? __privateGet(this, _gate).decide(last, head, lost, false) : void 0);
6426
+ __privateMethod(this, _FecReleaseSequencer_instances, release_fn).call(this, stream, head, lost.length > 0 || unanchored || unresolved || authoredGap(last.meta, head.meta) ? __privateGet(this, _gate).decide(last, head, lost, false) : void 0);
6242
6427
  }
6243
6428
  };
6429
+ /**
6430
+ * An overlay-rebuilt frame (fecSbnFirst) waits no longer than the BLO-53342
6431
+ * source cursor holds it unanchored, and neither does a later frame behind
6432
+ * it. Unanchored, a frame that waits on no block (fecSbnFirst set, or no
6433
+ * fecSbn) left the gate once every frame ahead of it had: mark it `passed`,
6434
+ * like each frame that arrived in front of it since (stale there). The head
6435
+ * then goes, decided as it would be unanchored, once a frame the base path
6436
+ * delivered (a source frame, or one base FEC recovered) is buffered after a
6437
+ * marked one: the base path has passed that one, so a frame still missing
6438
+ * before it can only come from a repair. It also goes while no source frame
6439
+ * has been released, when the direct path shows a recovered frame at once.
6440
+ * And it goes at once while a staged switch decides on when frames arrive
6441
+ * (FecReleaseGate.releaseUnanchored), and when unsource releases into a
6442
+ * sink being replaced: unanchored, it had already reached the sink.
6443
+ * The scan also writes: the `passed` marks it sets stay on the frames,
6444
+ * and push marks a frame it inserts in front of a marked one from them.
6445
+ */
6446
+ cursorPassed_fn = function(stream, asUnanchored) {
6447
+ let at = -1;
6448
+ for (let i = 0; i < stream.buf.length; i++) {
6449
+ const frame = stream.buf[i];
6450
+ if (frame.meta.fecSbnFirst !== void 0 || frame.meta.fecSbn === void 0) {
6451
+ frame.passed = true;
6452
+ if (at < 0) at = i;
6453
+ } else if (frame.passed !== true) break;
6454
+ }
6455
+ if (at < 0) return false;
6456
+ return asUnanchored || !stream.sourced || __privateGet(this, _gate).releaseUnanchored?.(stream.packetId, stream.catalogTrackName) === true || stream.buf.some(({ meta }, i) => i > at && meta.fecSbnFirst === void 0 && (meta.recovered !== true || meta.fecSbn !== void 0));
6457
+ };
6244
6458
  release_fn = function(stream, frame, decision) {
6459
+ const stale = stream.last !== null && compareFrameOrder(frame, stream.last) <= 0;
6460
+ if (stale) __privateGet(this, _recordStat2).call(this, "droppedStale");
6245
6461
  if (decision) frame.meta.gapBefore = decision;
6246
6462
  stream.last = frame;
6247
- if (frame.meta.fecSbn !== void 0) stream.lastSbn = frame.meta.fecSbn;
6463
+ if (frame.meta.fecSbn !== void 0 && (frame.meta.fecSbnFirst === void 0 || stream.lastSbn !== void 0)) {
6464
+ stream.lastSbn = frame.meta.fecSbn;
6465
+ }
6466
+ if (frame.meta.recovered !== true) stream.sourced = true;
6248
6467
  __privateGet(this, _gate).onReleased?.(frame.data, frame.meta);
6249
- __privateGet(this, _emit2).call(this, frame.data, frame.meta);
6468
+ if (!stale) __privateGet(this, _emit2).call(this, frame.data, frame.meta);
6250
6469
  };
6251
6470
  var FMPU_BOX_TYPES = /* @__PURE__ */ new Set(["ftyp", "moov", "moof"]);
6252
6471
  function isFmp4Box(data) {
@@ -6255,7 +6474,7 @@ var BlockcastMmt = (() => {
6255
6474
  return FMPU_BOX_TYPES.has(type);
6256
6475
  }
6257
6476
  var MAX_AWAITING_TIMING_HEADS = 8;
6258
- var _tracks, _defaultTrack, _reassembler, _sampleCombiners, _sampleCombinerOptions, _retiredSampleCombinerStats, _timingStreams, _maxPendingTimingSamples, _pendingTimingSamples, _reclaimedTimingSamples, _evictedTimingSamples, _rejectedMovieFragments, _sequencers, _fecGate, _fecSequencer, _reorderWindow, _reassemblyPolicy, _frameCb, _initCb, _sourceSymbolCb, _trackFrameCbs, _reorderStats, _reliableMoqStartupGate, _disposed, _replayingAwaitingTiming, _fedObjects, _reassemblerFragmentsFed, _reassemblerDataUnitsReturned, _feedOutcomes, _deferredFeedOutcomes, _lastOutcome, _lastOutcomeCount, _MmtpContainerParser_instances, streamKey_fn, sampleCombinerFor_fn, releaseTimingSlot_fn, dropSampleCombiner_fn, countOutcome_fn, countDeferredOutcome_fn, drop_fn, feedCounted_fn, feed_fn, emitCombinedSample_fn, passReliableMoqStartupGate_fn, timingResolver_fn, awaitTiming_fn, replayAwaitingTiming_fn, dropTimingStream_fn, validateReassemblyPolicy_fn, buildFrameMeta_fn, sequencerFor_fn, flushSequencers_fn, emitFrame_fn2, _MmtpContainerParser_static, scanForKeyframe_fn, scanLengthPrefixedHevcForKeyframe_fn, scanAnnexBForKeyframe_fn;
6477
+ var _tracks, _defaultTrack, _reassembler, _sampleCombiners, _sampleCombinerOptions, _retiredSampleCombinerStats, _timingStreams, _maxPendingTimingSamples, _pendingTimingSamples, _reclaimedTimingSamples, _evictedTimingSamples, _rejectedMovieFragments, _recoveredHeadsAwaitingTiming, _sequencers, _fecGate, _fecSequencer, _reorderWindow, _reassemblyPolicy, _frameCb, _initCb, _sourceSymbolCb, _trackFrameCbs, _reorderStats, _reliableMoqStartupGate, _disposed, _replayingAwaitingTiming, _fedObjects, _reassemblerFragmentsFed, _reassemblerDataUnitsReturned, _feedOutcomes, _deferredFeedOutcomes, _lastOutcome, _lastOutcomeCount, _MmtpContainerParser_instances, streamKey_fn, sampleCombinerFor_fn, releaseTimingSlot_fn, dropSampleCombiner_fn, countOutcome_fn, countDeferredOutcome_fn, drop_fn, feedCounted_fn, feed_fn, emitCombinedSample_fn, passReliableMoqStartupGate_fn, timingResolver_fn, awaitTiming_fn, leaveAwaitingTiming_fn, replayAwaitingTiming_fn, dropTimingStream_fn, validateReassemblyPolicy_fn, buildFrameMeta_fn, sequencerFor_fn, flushSequencers_fn, emitFrame_fn2, _MmtpContainerParser_static, scanForKeyframe_fn, scanLengthPrefixedHevcForKeyframe_fn, scanAnnexBForKeyframe_fn;
6259
6478
  var _MmtpContainerParser = class _MmtpContainerParser {
6260
6479
  constructor(config) {
6261
6480
  __privateAdd(this, _MmtpContainerParser_instances);
@@ -6291,6 +6510,12 @@ var BlockcastMmt = (() => {
6291
6510
  __privateAdd(this, _reclaimedTimingSamples, 0);
6292
6511
  __privateAdd(this, _evictedTimingSamples, 0);
6293
6512
  __privateAdd(this, _rejectedMovieFragments, 0);
6513
+ __privateAdd(this, _recoveredHeadsAwaitingTiming, {
6514
+ held: 0,
6515
+ expired: 0,
6516
+ discarded: 0,
6517
+ superseded: 0
6518
+ });
6294
6519
  __privateAdd(this, _sequencers, /* @__PURE__ */ new Map());
6295
6520
  __privateAdd(this, _fecGate, null);
6296
6521
  __privateAdd(this, _fecSequencer, null);
@@ -6418,11 +6643,12 @@ var BlockcastMmt = (() => {
6418
6643
  /**
6419
6644
  * Typed view of the ISO-BMFF timing-sample budget.
6420
6645
  *
6421
- * This is the acceptance signal for the budget ratchet: `pending` walking
6422
- * to `capacity` while `rejectedMovieFragments` climbs is the stream wedging,
6423
- * and it is observable here rather than only as media silently stopping.
6424
- * Per repo policy this is a producer-owned counter -- never infer budget
6425
- * state by parsing logs.
6646
+ * Each stream admits timing against its own window of `capacity` slots
6647
+ * (BLO-52085). A stream at capacity displaces its own oldest timing to
6648
+ * admit the moof in hand, and `evicted` counts the slots so displaced:
6649
+ * timing a stream declared and never delivered shows up there. `pending`
6650
+ * sums every stream, so it can exceed `capacity`. Per repo policy this is
6651
+ * a producer-owned counter -- never infer budget state by parsing logs.
6426
6652
  */
6427
6653
  get timingBudgetStats() {
6428
6654
  return {
@@ -6433,6 +6659,14 @@ var BlockcastMmt = (() => {
6433
6659
  rejectedMovieFragments: __privateGet(this, _rejectedMovieFragments)
6434
6660
  };
6435
6661
  }
6662
+ /** Recovered heads parked for FT=1 timing (BLO-53369). Survives dispose(). */
6663
+ get recoveredHeadsAwaitingTiming() {
6664
+ let pending = 0;
6665
+ for (const { awaitingTiming } of __privateGet(this, _timingStreams).values()) {
6666
+ for (const head of awaitingTiming) if (head.options?.recovered) pending++;
6667
+ }
6668
+ return { ...__privateGet(this, _recoveredHeadsAwaitingTiming), pending };
6669
+ }
6436
6670
  get reorderStats() {
6437
6671
  let bufferedFrames = __privateGet(this, _fecSequencer)?.buffered ?? 0;
6438
6672
  for (const { sequencer } of __privateGet(this, _sequencers).values()) bufferedFrames += sequencer.buffered;
@@ -6474,6 +6708,14 @@ var BlockcastMmt = (() => {
6474
6708
  fecReleaseRapAbove(packetId, catalogTrackName, ssId) {
6475
6709
  return __privateGet(this, _fecSequencer)?.rapAbove(packetId, catalogTrackName, ssId) ?? false;
6476
6710
  }
6711
+ /** See FecReleaseSequencer.unsource. */
6712
+ unsourceFecRelease(packetId, catalogTrackName) {
6713
+ __privateGet(this, _fecSequencer)?.unsource(packetId, catalogTrackName);
6714
+ }
6715
+ /** See FecReleaseSequencer.holdsRebuilt. */
6716
+ fecReleaseHoldsRebuilt(packetId, catalogTrackName) {
6717
+ return __privateGet(this, _fecSequencer)?.holdsRebuilt(packetId, catalogTrackName) ?? false;
6718
+ }
6477
6719
  /** Re-evaluate held gaps after FEC block state changed. */
6478
6720
  pokeFecRelease(packetId, catalogTrackName) {
6479
6721
  if (!__privateGet(this, _fecSequencer)) return;
@@ -6584,7 +6826,7 @@ var BlockcastMmt = (() => {
6584
6826
  const now = Date.now();
6585
6827
  for (const { awaitingTiming } of __privateGet(this, _timingStreams).values()) {
6586
6828
  while (awaitingTiming.length > 0 && now - awaitingTiming[0].arrivedAt > timeoutMs) {
6587
- awaitingTiming.shift();
6829
+ __privateMethod(this, _MmtpContainerParser_instances, leaveAwaitingTiming_fn).call(this, awaitingTiming.shift(), "expired");
6588
6830
  }
6589
6831
  }
6590
6832
  }
@@ -6837,6 +7079,9 @@ var BlockcastMmt = (() => {
6837
7079
  for (const { sequencer } of __privateGet(this, _sequencers).values()) sequencer.discard();
6838
7080
  __privateGet(this, _sequencers).clear();
6839
7081
  __privateGet(this, _fecSequencer)?.discard();
7082
+ for (const { awaitingTiming } of __privateGet(this, _timingStreams).values()) {
7083
+ for (const head of awaitingTiming) __privateMethod(this, _MmtpContainerParser_instances, leaveAwaitingTiming_fn).call(this, head, "discarded");
7084
+ }
6840
7085
  __privateGet(this, _timingStreams).clear();
6841
7086
  __privateSet(this, _pendingTimingSamples, 0);
6842
7087
  __privateGet(this, _reliableMoqStartupGate).clear();
@@ -6865,6 +7110,7 @@ var BlockcastMmt = (() => {
6865
7110
  _reclaimedTimingSamples = new WeakMap();
6866
7111
  _evictedTimingSamples = new WeakMap();
6867
7112
  _rejectedMovieFragments = new WeakMap();
7113
+ _recoveredHeadsAwaitingTiming = new WeakMap();
6868
7114
  _sequencers = new WeakMap();
6869
7115
  _fecGate = new WeakMap();
6870
7116
  _fecSequencer = new WeakMap();
@@ -7023,11 +7269,16 @@ var BlockcastMmt = (() => {
7023
7269
  if (mpu.fragmentationIndicator !== 0 || mpu.fragmentCounter !== 0)
7024
7270
  return __privateMethod(this, _MmtpContainerParser_instances, drop_fn).call(this, "fragment-unexpected-fragmentation");
7025
7271
  const resolver2 = __privateMethod(this, _MmtpContainerParser_instances, timingResolver_fn).call(this, streamKey, info.packetId, false);
7026
- if (!resolver2) return __privateMethod(this, _MmtpContainerParser_instances, drop_fn).call(this, "fragment-no-resolver");
7272
+ if (!resolver2?.initialized) return __privateMethod(this, _MmtpContainerParser_instances, drop_fn).call(this, "fragment-no-resolver");
7027
7273
  const pendingBefore = resolver2.pendingSampleCount;
7028
7274
  const evictedBefore = resolver2.evictedSampleCount;
7029
- const remaining = __privateGet(this, _maxPendingTimingSamples) - __privateGet(this, _pendingTimingSamples);
7030
- const admitted = resolver2.addMovieFragment(mpu.mpuSequenceNumber, data, remaining);
7275
+ const remaining = __privateGet(this, _maxPendingTimingSamples) - resolver2.pendingSampleCount;
7276
+ const admitted = resolver2.addMovieFragment(
7277
+ mpu.mpuSequenceNumber,
7278
+ data,
7279
+ remaining,
7280
+ options?.recovered === true
7281
+ );
7031
7282
  __privateSet(this, _pendingTimingSamples, __privateGet(this, _pendingTimingSamples) + (resolver2.pendingSampleCount - pendingBefore));
7032
7283
  __privateSet(this, _evictedTimingSamples, __privateGet(this, _evictedTimingSamples) + (resolver2.evictedSampleCount - evictedBefore));
7033
7284
  if (!admitted) {
@@ -7047,7 +7298,7 @@ var BlockcastMmt = (() => {
7047
7298
  };
7048
7299
  }
7049
7300
  if (fragmentType !== 2) return __privateMethod(this, _MmtpContainerParser_instances, drop_fn).call(this, "unknown-fragment-type");
7050
- const resolver = __privateMethod(this, _MmtpContainerParser_instances, timingResolver_fn).call(this, streamKey, info.packetId, false);
7301
+ const resolver = __privateMethod(this, _MmtpContainerParser_instances, timingResolver_fn).call(this, streamKey, info.packetId, true);
7051
7302
  if (!resolver) return __privateMethod(this, _MmtpContainerParser_instances, drop_fn).call(this, "mfu-no-resolver");
7052
7303
  if (!Number.isSafeInteger(data.byteLength) || data.byteLength <= 0)
7053
7304
  return __privateMethod(this, _MmtpContainerParser_instances, drop_fn).call(this, "mfu-empty-payload");
@@ -7068,6 +7319,7 @@ var BlockcastMmt = (() => {
7068
7319
  mpuSequenceNumber: mpu.mpuSequenceNumber,
7069
7320
  movieFragmentSequenceNumber: mfuDuHeader.movieFragmentSequenceNumber,
7070
7321
  sampleNumber: mfuDuHeader.sampleNumber,
7322
+ sequenceNumber: info.sequenceNumber,
7071
7323
  arrivedAt: Date.now()
7072
7324
  });
7073
7325
  return __privateMethod(this, _MmtpContainerParser_instances, drop_fn).call(this, "mfu-sample-timing-missing");
@@ -7088,7 +7340,7 @@ var BlockcastMmt = (() => {
7088
7340
  headTiming.size
7089
7341
  );
7090
7342
  if (readyOnRegister) {
7091
- const deferred = __privateMethod(this, _MmtpContainerParser_instances, emitCombinedSample_fn).call(this, readyOnRegister, info, resolver, options?.recovered === true, options?.fecSbn);
7343
+ const deferred = __privateMethod(this, _MmtpContainerParser_instances, emitCombinedSample_fn).call(this, readyOnRegister, info, resolver, options?.recovered === true, options?.fecSbn, options?.fecSbnFirst);
7092
7344
  if (deferred.record === void 0) __privateMethod(this, _MmtpContainerParser_instances, countDeferredOutcome_fn).call(this, deferred.reason);
7093
7345
  }
7094
7346
  }
@@ -7175,7 +7427,7 @@ var BlockcastMmt = (() => {
7175
7427
  fragmentCounter: mpu.fragmentCounter
7176
7428
  };
7177
7429
  }
7178
- const outcome = __privateMethod(this, _MmtpContainerParser_instances, emitCombinedSample_fn).call(this, combined, info, resolver, options?.recovered === true, options?.fecSbn);
7430
+ const outcome = __privateMethod(this, _MmtpContainerParser_instances, emitCombinedSample_fn).call(this, combined, info, resolver, options?.recovered === true, options?.fecSbn, options?.fecSbnFirst);
7179
7431
  if (outcome.record !== void 0) return outcome.record;
7180
7432
  return __privateMethod(this, _MmtpContainerParser_instances, drop_fn).call(this, outcome.reason);
7181
7433
  };
@@ -7194,7 +7446,7 @@ var BlockcastMmt = (() => {
7194
7446
  * attribution exists to keep readable. Leaving the choice at the call site
7195
7447
  * is what keeps both true.
7196
7448
  */
7197
- emitCombinedSample_fn = function(combined, info, resolver, recovered, fecSbn) {
7449
+ emitCombinedSample_fn = function(combined, info, resolver, recovered, fecSbn, fecSbnFirst) {
7198
7450
  const timing = resolver.sampleTiming(
7199
7451
  combined.mpuSequenceNumber,
7200
7452
  combined.movieFragmentSequenceNumber,
@@ -7205,6 +7457,7 @@ var BlockcastMmt = (() => {
7205
7457
  const meta = __privateMethod(this, _MmtpContainerParser_instances, buildFrameMeta_fn).call(this, combined, info.packetId, info.sequenceNumber, timing, recovered);
7206
7458
  if (!meta) return { reason: "combined-meta-build-failed" };
7207
7459
  if (fecSbn !== void 0) meta.fecSbn = fecSbn;
7460
+ if (fecSbnFirst !== void 0) meta.fecSbnFirst = fecSbnFirst;
7208
7461
  if (!resolver.consumeSample(
7209
7462
  combined.mpuSequenceNumber,
7210
7463
  combined.movieFragmentSequenceNumber,
@@ -7249,10 +7502,23 @@ var BlockcastMmt = (() => {
7249
7502
  return resolver;
7250
7503
  };
7251
7504
  awaitTiming_fn = function(streamKey, head) {
7252
- const awaitingTiming = __privateGet(this, _timingStreams).get(streamKey)?.awaitingTiming;
7253
- if (!awaitingTiming) return;
7505
+ const state = __privateGet(this, _timingStreams).get(streamKey);
7506
+ if (!state) return;
7507
+ if (!head.options?.recovered) {
7508
+ const kept = state.awaitingTiming.filter((queued) => !queued.options?.recovered || queued.sequenceNumber !== head.sequenceNumber);
7509
+ __privateGet(this, _recoveredHeadsAwaitingTiming).superseded += state.awaitingTiming.length - kept.length;
7510
+ state.awaitingTiming = kept;
7511
+ }
7512
+ const awaitingTiming = state.awaitingTiming;
7254
7513
  awaitingTiming.push(head);
7255
- if (awaitingTiming.length > MAX_AWAITING_TIMING_HEADS) awaitingTiming.shift();
7514
+ if (head.options?.recovered) __privateGet(this, _recoveredHeadsAwaitingTiming).held++;
7515
+ if (awaitingTiming.length > MAX_AWAITING_TIMING_HEADS) {
7516
+ __privateMethod(this, _MmtpContainerParser_instances, leaveAwaitingTiming_fn).call(this, awaitingTiming.shift(), "expired");
7517
+ }
7518
+ };
7519
+ /** Count a recovered head leaving the queue other than by replay or its source copy. */
7520
+ leaveAwaitingTiming_fn = function(head, outcome) {
7521
+ if (head.options?.recovered) __privateGet(this, _recoveredHeadsAwaitingTiming)[outcome]++;
7256
7522
  };
7257
7523
  /**
7258
7524
  * Feed again the heads whose timing the FT=1 just admitted declares. That
@@ -7280,6 +7546,7 @@ var BlockcastMmt = (() => {
7280
7546
  const state = __privateGet(this, _timingStreams).get(streamKey);
7281
7547
  if (!state) return;
7282
7548
  __privateGet(this, _timingStreams).delete(streamKey);
7549
+ for (const head of state.awaitingTiming) __privateMethod(this, _MmtpContainerParser_instances, leaveAwaitingTiming_fn).call(this, head, "discarded");
7283
7550
  __privateSet(this, _pendingTimingSamples, __privateGet(this, _pendingTimingSamples) - state.resolver.pendingSampleCount);
7284
7551
  if (__privateGet(this, _pendingTimingSamples) < 0) {
7285
7552
  throw new Error("[MmtpContainerParser] pending timing sample accounting underflow");