@blockcast/mmt-container 2.2.0-main.7a01a10f6629 → 2.2.0-main.7e8a9d13c01c

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 (102) hide show
  1. package/README.md +29 -17
  2. package/dist/browser-runtime.js +1626 -334
  3. package/dist/browser-runtime.js.map +4 -4
  4. package/dist/carp-catalog.js.map +1 -1
  5. package/dist/catalog-fec-params.d.ts +17 -1
  6. package/dist/catalog-fec-params.d.ts.map +1 -1
  7. package/dist/catalog-fec-params.js +15 -0
  8. package/dist/catalog-fec-params.js.map +2 -2
  9. package/dist/catalog-network-source.js.map +1 -1
  10. package/dist/cmcd-producer.d.ts +60 -47
  11. package/dist/cmcd-producer.d.ts.map +1 -1
  12. package/dist/cmcd-producer.js +138 -109
  13. package/dist/cmcd-producer.js.map +2 -2
  14. package/dist/delivery-catalog.js +26 -16
  15. package/dist/delivery-catalog.js.map +2 -2
  16. package/dist/delta-fallback-config.js.map +1 -1
  17. package/dist/fec-manager.d.ts +70 -12
  18. package/dist/fec-manager.d.ts.map +1 -1
  19. package/dist/fec-manager.js +403 -58
  20. package/dist/fec-manager.js.map +2 -2
  21. package/dist/fec-state.js.map +1 -1
  22. package/dist/fec-worker-setup.d.ts +1 -1
  23. package/dist/fec-worker-setup.js +1 -1
  24. package/dist/fec-worker-setup.js.map +1 -1
  25. package/dist/held-layer-admission.js.map +1 -1
  26. package/dist/index.d.ts +6 -5
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +9 -1
  29. package/dist/index.js.map +2 -2
  30. package/dist/iso-block-locator.d.ts +48 -13
  31. package/dist/iso-block-locator.d.ts.map +1 -1
  32. package/dist/iso-block-locator.js +97 -25
  33. package/dist/iso-block-locator.js.map +2 -2
  34. package/dist/iso-bmff-timing.d.ts +10 -1
  35. package/dist/iso-bmff-timing.d.ts.map +1 -1
  36. package/dist/iso-bmff-timing.js +36 -2
  37. package/dist/iso-bmff-timing.js.map +2 -2
  38. package/dist/media-router.js.map +1 -1
  39. package/dist/mfu-reassembler.js +12 -19
  40. package/dist/mfu-reassembler.js.map +2 -2
  41. package/dist/mmtp-container-parser.d.ts +79 -10
  42. package/dist/mmtp-container-parser.d.ts.map +1 -1
  43. package/dist/mmtp-container-parser.js +219 -45
  44. package/dist/mmtp-container-parser.js.map +2 -2
  45. package/dist/mmtp-container-parser.no-modules.js +342 -63
  46. package/dist/mmtp-container-parser.no-modules.js.map +3 -3
  47. package/dist/mmtp-decode-pipeline.js +218 -45
  48. package/dist/mmtp-decode-pipeline.js.map +2 -2
  49. package/dist/mmtp-fec-session.d.ts +41 -3
  50. package/dist/mmtp-fec-session.d.ts.map +1 -1
  51. package/dist/mmtp-fec-session.js +118 -13
  52. package/dist/mmtp-fec-session.js.map +3 -3
  53. package/dist/mmtp-router.d.ts +34 -8
  54. package/dist/mmtp-router.d.ts.map +1 -1
  55. package/dist/mmtp-router.js +567 -98
  56. package/dist/mmtp-router.js.map +2 -2
  57. package/dist/multicast-auth-catalog.js.map +1 -1
  58. package/dist/multicast-input.js.map +1 -1
  59. package/dist/multicast-manager.d.ts +15 -3
  60. package/dist/multicast-manager.d.ts.map +1 -1
  61. package/dist/multicast-manager.js +993 -165
  62. package/dist/multicast-manager.js.map +2 -2
  63. package/dist/multicast-transport.js +28 -18
  64. package/dist/multicast-transport.js.map +2 -2
  65. package/dist/multicast-unwrap.js +12 -19
  66. package/dist/multicast-unwrap.js.map +2 -2
  67. package/dist/no-modules.js +336 -59
  68. package/dist/no-modules.js.map +3 -3
  69. package/dist/repair-fetch.d.ts +36 -8
  70. package/dist/repair-fetch.d.ts.map +1 -1
  71. package/dist/repair-fetch.js +285 -26
  72. package/dist/repair-fetch.js.map +3 -3
  73. package/dist/repair-layer-subscriptions.d.ts +82 -0
  74. package/dist/repair-layer-subscriptions.d.ts.map +1 -0
  75. package/dist/repair-layer-subscriptions.js +192 -0
  76. package/dist/repair-layer-subscriptions.js.map +7 -0
  77. package/dist/signaling.js.map +1 -1
  78. package/dist/timestamp-codec.js +12 -19
  79. package/dist/timestamp-codec.js.map +2 -2
  80. package/dist/track-session.d.ts +1 -1
  81. package/dist/track-session.d.ts.map +1 -1
  82. package/dist/track-session.js +1 -0
  83. package/dist/track-session.js.map +2 -2
  84. package/dist/wasm-mfu-reassembler.js.map +1 -1
  85. package/dist/wasm-mmtp-container-parser.js +218 -45
  86. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  87. package/package.json +8 -5
  88. package/schemas/catalog.schema.json +4 -0
  89. package/src/catalog-fec-params.ts +36 -1
  90. package/src/cmcd-producer.ts +189 -179
  91. package/src/fec-manager.ts +433 -32
  92. package/src/fec-worker-setup.ts +2 -2
  93. package/src/index.ts +20 -2
  94. package/src/iso-block-locator.ts +133 -31
  95. package/src/iso-bmff-timing.ts +40 -1
  96. package/src/mmtp-container-parser.ts +306 -29
  97. package/src/mmtp-fec-session.ts +115 -15
  98. package/src/mmtp-router.ts +548 -38
  99. package/src/multicast-manager.ts +47 -6
  100. package/src/repair-fetch.ts +363 -49
  101. package/src/repair-layer-subscriptions.ts +261 -0
  102. package/src/track-session.ts +4 -1
@@ -489,6 +489,8 @@ var PACKET_TYPE_SIGNALING = 2;
489
489
  var PACKET_TYPE_REPAIR = 3;
490
490
  var MMTP_SIGNALING_PACKET_ID = 0;
491
491
  var MMTP_PACKET_ID_MAX = 65535;
492
+ var FEC_CODE_REED_SOLOMON = 1;
493
+ var FEC_CODE_RAPTORQ = 3;
492
494
  function parseMMTPHeader(buffer) {
493
495
  if (buffer.length < MMTP_HEADER_SIZE) {
494
496
  throw new Error(`MMTP packet too small: ${buffer.length} bytes`);
@@ -655,30 +657,26 @@ function isEmptyOrExactLegacyTrailer(payload, start, end) {
655
657
  const authLength = view2.getUint16(2, false);
656
658
  return start + LEGACY_TRAILER_HEADER_SIZE + authLength === end;
657
659
  }
658
- function resolveDeclaredTrailers(payload, declaredEnd, fecType, options, rapFlag = false) {
660
+ function resolveDeclaredTrailers(payload, declaredEnd, fecType, options) {
659
661
  if (declaredEnd > payload.length)
660
662
  return null;
661
663
  if (options?.skipFecId) {
662
- return isEmptyOrExactLegacyTrailer(payload, declaredEnd, payload.length) ? { hasFecId: false, hasOverlayFecId: false } : null;
664
+ return isEmptyOrExactLegacyTrailer(payload, declaredEnd, payload.length) ? { hasFecId: false } : null;
663
665
  }
664
- const overlay = options?.overlayDeclared === true && rapFlag;
665
666
  if (fecType === 1) {
666
- const fecStart = payload.length - sourceFecTrailerSize(overlay);
667
+ const fecStart = payload.length - SOURCE_FEC_PAYLOAD_ID_SIZE;
667
668
  if (fecStart < declaredEnd || !isEmptyOrExactLegacyTrailer(payload, declaredEnd, fecStart)) {
668
669
  return null;
669
670
  }
670
- return { hasFecId: true, hasOverlayFecId: overlay };
671
+ return { hasFecId: true };
671
672
  }
672
- if (fecType !== 0 || overlay)
673
+ if (fecType !== 0)
673
674
  return null;
674
675
  if (isEmptyOrExactLegacyTrailer(payload, declaredEnd, payload.length)) {
675
- return { hasFecId: false, hasOverlayFecId: false };
676
+ return { hasFecId: false };
676
677
  }
677
678
  return null;
678
679
  }
679
- function sourceFecTrailerSize(overlay) {
680
- return overlay ? 2 * SOURCE_FEC_PAYLOAD_ID_SIZE : SOURCE_FEC_PAYLOAD_ID_SIZE;
681
- }
682
680
  function extractNALUnit(mmtpPacket) {
683
681
  const extraction = extractMFUPayload(mmtpPacket);
684
682
  if (!extraction)
@@ -737,11 +735,11 @@ function planMfuExtraction(mmtpPacket, options, parsedHeader) {
737
735
  if ("rejection" in layoutResult)
738
736
  return layoutResult;
739
737
  const { layout } = layoutResult;
740
- const trailerLayout = resolveDeclaredTrailers(payload, layout.dataEnd, header.fecType, options, header.rapFlag);
738
+ const trailerLayout = resolveDeclaredTrailers(payload, layout.dataEnd, header.fecType, options);
741
739
  if (!trailerLayout) {
742
740
  if (!options?.skipFecId && header.fecType === 1) {
743
741
  const observed = payload.length - layout.dataEnd;
744
- const expected = sourceFecTrailerSize(options?.overlayDeclared === true && header.rapFlag);
742
+ const expected = SOURCE_FEC_PAYLOAD_ID_SIZE;
745
743
  if (observed !== expected) {
746
744
  return {
747
745
  rejection: {
@@ -776,13 +774,10 @@ function extractPlannedMfu(mmtpPacket, result) {
776
774
  const mfuDuHeader = mfuDuHeaderPresent && mpu.timedFlag === 1 ? parseTimedMfuDuHeader(payload, MPU_HEADER_SIZE) : null;
777
775
  const itemId = mfuDuHeaderPresent && mpu.timedFlag === 0 ? new DataView(payload.buffer, payload.byteOffset + MPU_HEADER_SIZE, 4).getUint32(0, false) : null;
778
776
  const data = payload.subarray(mfuOffset, dataEnd);
779
- const trailerStart = mmtpPacket.length - sourceFecTrailerSize(trailerLayout.hasOverlayFecId);
780
- const ssIdAt = (offset) => ({
781
- ssId: new DataView(mmtpPacket.buffer, mmtpPacket.byteOffset + offset, 4).getUint32(0, false)
782
- });
783
- const fecPayloadId = trailerLayout.hasFecId ? ssIdAt(trailerStart) : null;
784
- const overlayFecPayloadId = trailerLayout.hasOverlayFecId ? ssIdAt(trailerStart + SOURCE_FEC_PAYLOAD_ID_SIZE) : null;
785
- return { header, mpu, fragmentType, data, mfuDuHeader, itemId, fecPayloadId, overlayFecPayloadId };
777
+ const fecPayloadId = trailerLayout.hasFecId ? {
778
+ ssId: new DataView(mmtpPacket.buffer, mmtpPacket.byteOffset + mmtpPacket.length - SOURCE_FEC_PAYLOAD_ID_SIZE, 4).getUint32(0, false)
779
+ } : null;
780
+ return { header, mpu, fragmentType, data, mfuDuHeader, itemId, fecPayloadId };
786
781
  }
787
782
  function extractTimestamp(mmtpPacket) {
788
783
  const { header } = unwrapMMTP(mmtpPacket);
@@ -856,6 +851,17 @@ function readRepairFecPayloadIdFields(mmtpPacket, parsedHeader) {
856
851
  dataOffset: off + REPAIR_FEC_PID_SIZE
857
852
  };
858
853
  }
854
+ function extractRepairFecPayloadId(mmtpPacket) {
855
+ const fields = readRepairFecPayloadIdFields(mmtpPacket);
856
+ if (!fields)
857
+ return null;
858
+ return {
859
+ ssStart: fields.ssStart,
860
+ rsbLength: fields.rsbLength,
861
+ rsId: fields.rsId,
862
+ ssbLength: fields.ssbLength
863
+ };
864
+ }
859
865
  function parseMoqRepairFrame(frame) {
860
866
  return moqRepairFrameFrom(frame, readRepairFecPayloadIdFields(frame));
861
867
  }
@@ -903,6 +909,50 @@ var NTP_SHORT_BITS = 32;
903
909
  var NTP_SHORT_MODULUS = 2 ** NTP_SHORT_BITS;
904
910
  var NTP_SHORT_HALF_RANGE = 2 ** (NTP_SHORT_BITS - 1);
905
911
 
912
+ // ../mmt-base/dist/signalling.js
913
+ var FEC_CODING_STRUCTURE_ONE_STAGE = 1;
914
+ var SSBG_MODE_1 = 1;
915
+ var RAPTORQ_MAX_SOURCE_SYMBOLS = 56403;
916
+ var REPAIR_SYMBOL_ID_MAX = 16777215;
917
+ var MIN_REPAIR_SYMBOL_SIZE = 14;
918
+ var REED_SOLOMON_MAX_BLOCK = 255;
919
+ function alFecRaptorQFlowConfig(flow) {
920
+ if (flow.fecCodingStructure !== FEC_CODING_STRUCTURE_ONE_STAGE || flow.repairFlows.length !== 1)
921
+ return null;
922
+ if (flow.ssbgMode !== SSBG_MODE_1 || flow.ffsrptsFlag !== 0 || flow.fecPayloadIdMode !== 0)
923
+ return null;
924
+ if (flow.packetIds.length !== 1)
925
+ return null;
926
+ const repair = flow.repairFlows[0];
927
+ const { fecCodeId, maximumK: k, maximumP: p } = repair;
928
+ const t = flow.repairSymbolSize;
929
+ if (t < MIN_REPAIR_SYMBOL_SIZE || k < 1 || p < 1 || k + p - 1 > REPAIR_SYMBOL_ID_MAX)
930
+ return null;
931
+ if (fecCodeId === FEC_CODE_RAPTORQ) {
932
+ if (t % 8 !== 0 || k > RAPTORQ_MAX_SOURCE_SYMBOLS)
933
+ return null;
934
+ } else if (fecCodeId === FEC_CODE_REED_SOLOMON) {
935
+ if (k + p > REED_SOLOMON_MAX_BLOCK)
936
+ return null;
937
+ } else {
938
+ return null;
939
+ }
940
+ if (repair.repairFlowId === MMTP_SIGNALING_PACKET_ID || repair.repairFlowId === flow.packetIds[0])
941
+ return null;
942
+ return {
943
+ sourcePacketIds: [...flow.packetIds],
944
+ repairFlowId: repair.repairFlowId,
945
+ fecCodeId,
946
+ ssbgMode: flow.ssbgMode,
947
+ fecPayloadIdMode: flow.fecPayloadIdMode,
948
+ repairSymbolSize: t,
949
+ maximumK: k,
950
+ maximumP: p,
951
+ protectionWindowTime: repair.protectionWindowTime,
952
+ protectionWindowSize: repair.protectionWindowSize
953
+ };
954
+ }
955
+
906
956
  // ../mmt-base/dist/serial-number.js
907
957
  var SERIAL32_SPAN = 4294967296;
908
958
  var SERIAL32_HALF = 2147483648;
@@ -7949,8 +7999,8 @@ var MulticastSubscriptionPool = class {
7949
7999
  record.claims--;
7950
8000
  return this.acquire(gateway, config, consumer);
7951
8001
  }
7952
- const token = Symbol(key);
7953
- entry.consumers.set(token, consumer);
8002
+ const token2 = Symbol(key);
8003
+ entry.consumers.set(token2, consumer);
7954
8004
  let released = false;
7955
8005
  return {
7956
8006
  key,
@@ -7958,7 +8008,7 @@ var MulticastSubscriptionPool = class {
7958
8008
  if (released)
7959
8009
  return;
7960
8010
  released = true;
7961
- entry.consumers.delete(token);
8011
+ entry.consumers.delete(token2);
7962
8012
  record.claims--;
7963
8013
  if (record.claims > 0)
7964
8014
  return;
@@ -8586,9 +8636,9 @@ async function openSubscription(rx, openedPromise, options, packetIds) {
8586
8636
  } catch (error) {
8587
8637
  throw new MulticastError("subscription-failed", "MulticastReceiver did not open", error);
8588
8638
  }
8589
- if (rx.integrityScheme !== INTEGRITY_SCHEME) {
8639
+ if (opened.integrityScheme !== INTEGRITY_SCHEME) {
8590
8640
  await rx.close();
8591
- throw unsupported(`integrity scheme ${String(rx.integrityScheme)} is not ${INTEGRITY_SCHEME}`);
8641
+ throw unsupported(`integrity scheme ${String(opened.integrityScheme)} is not ${INTEGRITY_SCHEME}`);
8592
8642
  }
8593
8643
  const events = new EventTarget();
8594
8644
  let state = "connected";
@@ -8707,14 +8757,15 @@ var MOQT_PUBLISHER_PRIORITY = Object.freeze({
8707
8757
  });
8708
8758
 
8709
8759
  // node_modules/@blockcast/mmt-transport/dist/amt-gateway.js
8760
+ var MAX_PENDING_PACKETS = 256;
8710
8761
  var AMTGatewayManager = class {
8711
8762
  subscriptions = /* @__PURE__ */ new Map();
8712
8763
  messageHandler;
8764
+ messageListenerRegistered = false;
8713
8765
  pendingPackets = /* @__PURE__ */ new Map();
8714
8766
  constructor() {
8715
8767
  this.messageHandler = (event) => {
8716
8768
  const data = event.data;
8717
- console.log(`[AMT] SW message received:`, data.type);
8718
8769
  if (data.type === "UDP_PACKET") {
8719
8770
  this.handlePacket(data);
8720
8771
  } else if (data.type === "SOCKET_ERROR") {
@@ -8723,12 +8774,22 @@ var AMTGatewayManager = class {
8723
8774
  console.log(`[AMT SW Debug] ${data.message}`);
8724
8775
  }
8725
8776
  };
8726
- if (typeof navigator !== "undefined" && navigator.serviceWorker) {
8727
- navigator.serviceWorker.addEventListener("message", this.messageHandler);
8728
- console.log("[AMT] Registered service worker message handler");
8729
- } else {
8730
- console.warn("[AMT] Service worker not available");
8777
+ }
8778
+ ensureMessageListener() {
8779
+ if (this.messageListenerRegistered || typeof navigator === "undefined" || !navigator.serviceWorker || !this.messageHandler) {
8780
+ return;
8781
+ }
8782
+ navigator.serviceWorker.addEventListener("message", this.messageHandler);
8783
+ this.messageListenerRegistered = true;
8784
+ console.log("[AMT] Registered service worker message handler");
8785
+ }
8786
+ removeMessageListener() {
8787
+ if (!this.messageListenerRegistered)
8788
+ return;
8789
+ if (this.messageHandler && typeof navigator !== "undefined" && navigator.serviceWorker) {
8790
+ navigator.serviceWorker.removeEventListener("message", this.messageHandler);
8731
8791
  }
8792
+ this.messageListenerRegistered = false;
8732
8793
  }
8733
8794
  /**
8734
8795
  * Subscribe via AMT tunnel
@@ -8738,6 +8799,7 @@ var AMTGatewayManager = class {
8738
8799
  if (config.protected) {
8739
8800
  throw new Error("AMTGatewayManager refuses a protected join: it reports no per-datagram sender (multicast-02 7.2.7)");
8740
8801
  }
8802
+ this.ensureMessageListener();
8741
8803
  const keyStr = this.getKeyString(key);
8742
8804
  if (this.subscriptions.has(keyStr)) {
8743
8805
  console.log(`[AMT] Reusing existing subscription to ${keyStr}`);
@@ -8773,6 +8835,8 @@ var AMTGatewayManager = class {
8773
8835
  this.subscriptions.delete(keyStr);
8774
8836
  this.pendingPackets.delete(subscription.subscriptionId);
8775
8837
  }
8838
+ if (this.subscriptions.size === 0)
8839
+ this.removeMessageListener();
8776
8840
  throw new Error(`Failed to subscribe via AMT: ${error instanceof Error ? error.message : String(error)}`);
8777
8841
  }
8778
8842
  }
@@ -8794,6 +8858,8 @@ var AMTGatewayManager = class {
8794
8858
  } finally {
8795
8859
  this.subscriptions.delete(handle);
8796
8860
  this.pendingPackets.delete(subscription.subscriptionId);
8861
+ if (this.subscriptions.size === 0)
8862
+ this.removeMessageListener();
8797
8863
  }
8798
8864
  }
8799
8865
  /**
@@ -8810,14 +8876,12 @@ var AMTGatewayManager = class {
8810
8876
  if (!subscriptionId)
8811
8877
  return;
8812
8878
  const subscription = this.subscriptions.get(subscriptionId);
8813
- if (!subscription) {
8814
- console.warn(`[AMT] Received packet for unknown subscription: ${subscriptionId}`);
8879
+ if (!subscription)
8815
8880
  return;
8816
- }
8817
8881
  if (!subscription.handshakeComplete) {
8818
8882
  const queue = this.pendingPackets.get(subscriptionId);
8819
8883
  const packetData2 = data;
8820
- if (queue) {
8884
+ if (queue && queue.length < MAX_PENDING_PACKETS) {
8821
8885
  queue.push({
8822
8886
  data: packetData2.data,
8823
8887
  remoteAddress: packetData2.remoteAddress,
@@ -8843,19 +8907,15 @@ var AMTGatewayManager = class {
8843
8907
  if (!subscriptionId)
8844
8908
  return;
8845
8909
  const subscription = this.subscriptions.get(subscriptionId);
8846
- if (!subscription) {
8847
- console.warn(`[AMT] Received error for unknown subscription: ${subscriptionId}`);
8910
+ if (!subscription)
8848
8911
  return;
8849
- }
8850
8912
  subscription.config.onError(new Error(error || "Unknown error"));
8851
8913
  }
8852
8914
  /**
8853
8915
  * Cleanup on destroy
8854
8916
  */
8855
8917
  destroy() {
8856
- if (this.messageHandler && typeof navigator !== "undefined" && navigator.serviceWorker) {
8857
- navigator.serviceWorker.removeEventListener("message", this.messageHandler);
8858
- }
8918
+ this.removeMessageListener();
8859
8919
  const handles = Array.from(this.subscriptions.keys());
8860
8920
  for (const handle of handles) {
8861
8921
  this.unsubscribe(handle).catch((err) => {
@@ -12386,6 +12446,7 @@ var TrackSession = class {
12386
12446
  this.#state = "idle";
12387
12447
  this.#subscription = null;
12388
12448
  this.#disarmFec();
12449
+ this.#unsuppress();
12389
12450
  }
12390
12451
  // -----------------------------------------------------------------------
12391
12452
  // Internal
@@ -12879,6 +12940,15 @@ var IsoBlockLocator = class _IsoBlockLocator {
12879
12940
  this.#heldEvicted = 0;
12880
12941
  /** Pending holes (see `deferIncompleteGaps`): unstated, sources still held. */
12881
12942
  this.#holes = [];
12943
+ /** Holes the horizon passed while pending: sources held until closed. */
12944
+ this.#expired = [];
12945
+ /** Of `#expired`, those `takeExpiredHoles` has not handed out yet. */
12946
+ this.#newlyExpired = [];
12947
+ /**
12948
+ * Spans closed from expired holes: behind the horizon, so kept only for
12949
+ * `locate` while their sources are fed, until the next retirement.
12950
+ */
12951
+ this.#closedBehind = [];
12882
12952
  /**
12883
12953
  * Keep a gap that is missing a source as a pending hole rather than a span,
12884
12954
  * so a repair FETCHed for it (draft-ramadan-moq-fec §7.5) can still state
@@ -12900,7 +12970,11 @@ var IsoBlockLocator = class _IsoBlockLocator {
12900
12970
  #retentionBlocks;
12901
12971
  #heldEvicted;
12902
12972
  #newest;
12973
+ #newestSource;
12903
12974
  #holes;
12975
+ #expired;
12976
+ #newlyExpired;
12977
+ #closedBehind;
12904
12978
  static #checkedRetention(retentionBlocks) {
12905
12979
  if (!Number.isSafeInteger(retentionBlocks) || retentionBlocks <= 0) {
12906
12980
  throw new Error(
@@ -12937,6 +13011,21 @@ var IsoBlockLocator = class _IsoBlockLocator {
12937
13011
  observe(position) {
12938
13012
  if (this.#newest === void 0 || serial32Precedes(this.#newest, position)) this.#newest = position;
12939
13013
  }
13014
+ /**
13015
+ * The newest source SS_ID noted with `noteSource` (RFC 1982 order). Unlike
13016
+ * `newest`, a repair never moves it: a repair can run ahead of its sources,
13017
+ * a source cannot run ahead of the sources sent before it except by
13018
+ * reordering.
13019
+ */
13020
+ get newestSource() {
13021
+ return this.#newestSource;
13022
+ }
13023
+ /** Note a received source's SS_ID; true when it is the newest one yet. */
13024
+ noteSource(ssId) {
13025
+ if (this.#newestSource !== void 0 && !serial32Precedes(this.#newestSource, ssId)) return false;
13026
+ this.#newestSource = ssId;
13027
+ return true;
13028
+ }
12940
13029
  /** Held sources evicted by `heldCapacity` (never by the horizon itself). */
12941
13030
  get heldEvicted() {
12942
13031
  return this.#heldEvicted;
@@ -12955,17 +13044,29 @@ var IsoBlockLocator = class _IsoBlockLocator {
12955
13044
  }
12956
13045
  /** The known block or span containing `ssId`, with its ESI. */
12957
13046
  locate(ssId) {
12958
- for (let i = this.#blocks.length - 1; i >= 0; i--) {
12959
- const block = this.#blocks[i];
12960
- if (isoBlockContains(block.start, block.length, ssId)) {
12961
- return { block, esi: serial32Distance(block.start, ssId) };
13047
+ for (const blocks of [this.#blocks, this.#closedBehind]) {
13048
+ for (let i = blocks.length - 1; i >= 0; i--) {
13049
+ const block = blocks[i];
13050
+ if (isoBlockContains(block.start, block.length, ssId)) {
13051
+ return { block, esi: serial32Distance(block.start, ssId) };
13052
+ }
12962
13053
  }
12963
13054
  }
12964
13055
  return null;
12965
13056
  }
12966
- /** Number of pending holes. */
13057
+ /** Number of holes not closed yet, pending or past the horizon. */
12967
13058
  get holeCount() {
12968
- return this.#holes.length;
13059
+ return this.#holes.length + this.#expired.length;
13060
+ }
13061
+ /**
13062
+ * The pending holes the horizon has passed since the last call, in the
13063
+ * order it passed them. Each is no longer pending: no repair states a block
13064
+ * in it (that repair is `stale`, as for a span the horizon retired) and the
13065
+ * release gate does not wait for it. Its owner must close it
13066
+ * (`closeHolesWithin`), which turns it into a span that is not kept here.
13067
+ */
13068
+ takeExpiredHoles() {
13069
+ return this.#newlyExpired.splice(0);
12969
13070
  }
12970
13071
  /** The stated block (never a span) ending exactly at `end`. */
12971
13072
  blockEndingAt(end) {
@@ -13021,9 +13122,11 @@ var IsoBlockLocator = class _IsoBlockLocator {
13021
13122
  /**
13022
13123
  * Apply one repair symbol's `(SS_Start, SSB_length)`. The first repair of a
13023
13124
  * block states its range and places every held source in it; later repair
13024
- * symbols of the same block change nothing. A range that contradicts a
13025
- * known block (a different length, or an overlap with another block or a
13026
- * span) fails closed: the repair symbol is dropped and no block changes.
13125
+ * symbols of the same block change nothing. A repair whose range is exactly
13126
+ * a repair-less span states that span's one block. A range that contradicts
13127
+ * a known block (a different length, or an overlap with another block or
13128
+ * with part of a span) fails closed: the repair symbol is dropped and no
13129
+ * block changes.
13027
13130
  * A newly stated block retires behind its SS_Start (see the class note).
13028
13131
  * A block so far past `newest` that stating it would retire the position
13029
13132
  * `newest` is refused as `ahead`. When `newest` is a placed block's SS_Start
@@ -13042,7 +13145,16 @@ var IsoBlockLocator = class _IsoBlockLocator {
13042
13145
  const known = this.blockAt(start);
13043
13146
  if (known !== void 0) {
13044
13147
  if (known.span) {
13045
- return { status: "overlap", reason: `repair block ${start}+${length} falls inside a repair-less span` };
13148
+ if (known.length === length) {
13149
+ const block2 = { start, length, span: false };
13150
+ this.#blocks[this.#blocks.indexOf(known)] = block2;
13151
+ return { status: "placed", block: block2, placed: [] };
13152
+ }
13153
+ return length < known.length ? {
13154
+ status: "overlap",
13155
+ reason: `repair block ${start}+${length} is only the first block of repair-less span ${known.start}+${known.length}`,
13156
+ ofSpan: known
13157
+ } : { status: "overlap", reason: `repair block ${start}+${length} runs past repair-less span ${known.start}+${known.length}` };
13046
13158
  }
13047
13159
  if (known.length !== length) {
13048
13160
  return {
@@ -13110,18 +13222,28 @@ var IsoBlockLocator = class _IsoBlockLocator {
13110
13222
  return span === void 0 ? { status: "placed", block, placed } : { status: "placed", block, placed, span };
13111
13223
  }
13112
13224
  /**
13113
- * Turn every pending hole inside `[start, start + length)` into a
13114
- * repair-less span, claiming its held sources: its range FETCH ended, so
13115
- * nothing more will state its blocks. Returns the spans, in SS_ID order.
13225
+ * Turn every hole inside `[start, start + length)` into a repair-less span,
13226
+ * claiming its held sources: its range FETCH ended, so nothing more will
13227
+ * state its blocks. A hole the horizon passed (`takeExpiredHoles`) leaves a
13228
+ * span that is not kept among the blocks, as the horizon would retire it at
13229
+ * once; only `locate` finds it, until the next retirement, so its sources
13230
+ * can be fed. Returns the spans, in SS_ID order.
13116
13231
  */
13117
13232
  closeHolesWithin(start, length) {
13118
13233
  const closed = [];
13119
- for (const hole of [...this.#holes]) {
13120
- if (!isoBlockContains(start, length, hole.start)) continue;
13121
- this.#holes.splice(this.#holes.indexOf(hole), 1);
13122
- const spanBlock = { start: hole.start, length: hole.length, span: true };
13123
- closed.push({ block: spanBlock, placed: this.#claim(spanBlock) });
13124
- this.#blocks.push(spanBlock);
13234
+ for (const holes of [this.#holes, this.#expired]) {
13235
+ for (const hole of [...holes]) {
13236
+ if (!isoBlockContains(start, length, hole.start)) continue;
13237
+ holes.splice(holes.indexOf(hole), 1);
13238
+ const spanBlock = { start: hole.start, length: hole.length, span: true };
13239
+ closed.push({ block: spanBlock, placed: this.#claim(spanBlock) });
13240
+ if (holes === this.#holes) {
13241
+ this.#blocks.push(spanBlock);
13242
+ } else {
13243
+ this.#closedBehind.push(spanBlock);
13244
+ if (this.#newlyExpired.includes(hole)) this.#newlyExpired.splice(this.#newlyExpired.indexOf(hole), 1);
13245
+ }
13246
+ }
13125
13247
  }
13126
13248
  return closed.sort((x, y) => serial32Distance(start, x.block.start) - serial32Distance(start, y.block.start));
13127
13249
  }
@@ -13135,15 +13257,17 @@ var IsoBlockLocator = class _IsoBlockLocator {
13135
13257
  return void 0;
13136
13258
  }
13137
13259
  /**
13138
- * Retire every block and span whose last SS_ID, and every held source,
13139
- * lies more than `retentionBlocks` blocks behind `position`, with the shared
13140
- * lower-bound metric (`isoBlocksBehindLowerBound`) every ISO interleave
13141
- * horizon uses. Returns the retired block starts.
13260
+ * Retire every block and span whose last SS_ID, and every held source
13261
+ * outside a hole, lies more than `retentionBlocks` blocks behind
13262
+ * `position`, with the shared lower-bound metric (`isoBlocksBehindLowerBound`)
13263
+ * every ISO interleave horizon uses; a pending hole so far behind expires
13264
+ * (`takeExpiredHoles`). Returns the retired block starts.
13142
13265
  */
13143
13266
  retireBehind(position) {
13144
13267
  this.observe(position);
13145
13268
  const horizon = this.#retentionBlocks;
13146
13269
  const retired = [];
13270
+ this.#closedBehind.length = 0;
13147
13271
  for (let i = this.#blocks.length - 1; i >= 0; i--) {
13148
13272
  const block = this.#blocks[i];
13149
13273
  const last = serial32Add(block.start, block.length - 1);
@@ -13155,10 +13279,15 @@ var IsoBlockLocator = class _IsoBlockLocator {
13155
13279
  for (let i = this.#holes.length - 1; i >= 0; i--) {
13156
13280
  const hole = this.#holes[i];
13157
13281
  const last = serial32Add(hole.start, hole.length - 1);
13158
- if (isoBlocksBehindLowerBound(last, position, this.#kMax) > horizon) this.#holes.splice(i, 1);
13282
+ if (isoBlocksBehindLowerBound(last, position, this.#kMax) <= horizon) continue;
13283
+ this.#holes.splice(i, 1);
13284
+ this.#expired.push(hole);
13285
+ this.#newlyExpired.push(hole);
13159
13286
  }
13160
13287
  for (const ssId of [...this.#held.keys()]) {
13161
- if (isoBlocksBehindLowerBound(ssId, position, this.#kMax) > horizon) this.#held.delete(ssId);
13288
+ if (isoBlocksBehindLowerBound(ssId, position, this.#kMax) > horizon && !this.#holes.some((hole) => isoBlockContains(hole.start, hole.length, ssId)) && !this.#expired.some((hole) => isoBlockContains(hole.start, hole.length, ssId))) {
13289
+ this.#held.delete(ssId);
13290
+ }
13162
13291
  }
13163
13292
  return retired;
13164
13293
  }
@@ -13166,9 +13295,13 @@ var IsoBlockLocator = class _IsoBlockLocator {
13166
13295
  reset() {
13167
13296
  this.#blocks.length = 0;
13168
13297
  this.#holes.length = 0;
13298
+ this.#expired.length = 0;
13299
+ this.#newlyExpired.length = 0;
13300
+ this.#closedBehind.length = 0;
13169
13301
  this.#held.clear();
13170
13302
  this.#coverageEnd = void 0;
13171
13303
  this.#newest = void 0;
13304
+ this.#newestSource = void 0;
13172
13305
  }
13173
13306
  #claim(block) {
13174
13307
  const placed = [];
@@ -13788,6 +13921,7 @@ var WORKER_ERROR_LOG_CAP = 5;
13788
13921
  var WORKER_ERROR_KIND_CAP = 32;
13789
13922
  var WORKER_ERROR_OVERFLOW_KIND = "other";
13790
13923
  var STARTUP_REPAIR_WAIT_SOURCE_BOUNDARIES = 2;
13924
+ var NOT_TAKEN = Object.freeze({ taken: false, recovered: null });
13791
13925
  function isMulticastSymbolDelivery(value) {
13792
13926
  return value === "datagram" || value === "ordered-relay";
13793
13927
  }
@@ -13881,10 +14015,18 @@ var FECManager = class _FECManager {
13881
14015
  // 'pending' forever, which would park every later frame of its stream in
13882
14016
  // the in-order release gate. Each entry owns its handle; the callback
13883
14017
  // checks it still owns the key, so a settled or reset key is never touched.
14018
+ /**
14019
+ * Per owed Worker cleanup: the bound on its acknowledgement, when its block
14020
+ * was due, and when its cleanup was asked for (see `onCleanupComplete`).
14021
+ */
13884
14022
  this.#pendingWorkerCleanupBounds = /* @__PURE__ */ new Map();
13885
14023
  // Source symbol count per track-scoped block — marks complete when K symbols arrive.
13886
14024
  this.#blockSourceCounts = /* @__PURE__ */ new Map();
13887
14025
  this.#blockSourceEsis = /* @__PURE__ */ new Map();
14026
+ // Source ESIs per block that another FEC instance rebuilt and fed in
14027
+ // (feedRebuiltSourceSymbol). Kept apart from #blockSourceEsis: they are
14028
+ // not network arrivals and never make a block clean.
14029
+ this.#blockRebuiltEsis = /* @__PURE__ */ new Map();
13888
14030
  // Authored SSB_length per block (ISO/IEC 23008-1 §C.5.3), learned from the
13889
14031
  // first repair symbol. The FFmpeg moq_mmt publisher closes a block every D
13890
14032
  // media units, usually before it holds K symbols; under iso-ssbg1-v1
@@ -13893,6 +14035,9 @@ var FECManager = class _FECManager {
13893
14035
  // SSB_length source arrivals and recoverable from SSB_length received
13894
14036
  // symbols; waiting for K fails every one of them.
13895
14037
  this.#blockAuthoredLengths = /* @__PURE__ */ new Map();
14038
+ // Keys whose authored length is a repair-less span's (#openIsoSpan), until
14039
+ // a repair states the block there or the span resolves.
14040
+ this.#isoSpanKeys = /* @__PURE__ */ new Set();
13896
14041
  this.#ssbLengthConflictCount = 0;
13897
14042
  this.#blockRepairRsIds = /* @__PURE__ */ new Map();
13898
14043
  // RSB_length per block (C.5.3 Repair FEC Payload ID): how many repair
@@ -14013,6 +14158,10 @@ var FECManager = class _FECManager {
14013
14158
  // ── Keyframe-protected repair policy (block-policy.ts) ──
14014
14159
  // One classifier per physical stream, built from that stream's geometry.
14015
14160
  this.#classifiers = /* @__PURE__ */ new Map();
14161
+ // A stream without catalog timing has no classifier, so the last RAP it
14162
+ // released stands in for one: "an MPU boundary bounds its RAPs" holds until
14163
+ // a second RAP is released inside one MPU (BLO-52085).
14164
+ this.#noSpanRaps = /* @__PURE__ */ new Map();
14016
14165
  // Geometry a stream-scoped configuration gave one stream (streamKey). ABR
14017
14166
  // rungs can share a packetId with a different framerate or GOP, so the
14018
14167
  // packet-level geometry is only the geometry of a packet whose streams
@@ -14086,7 +14235,10 @@ var FECManager = class _FECManager {
14086
14235
  // arrives via onBlockRecovered callback.
14087
14236
  this.onBlockStarted = null;
14088
14237
  this.onBlockComplete = null;
14089
- /** A block reached its fail bound. `blockClass` is its class at failure. */
14238
+ /**
14239
+ * A block reached its fail bound. `blockClass` is its class at failure.
14240
+ * `span` is set when what failed is a repair-less span starting at `sbn`.
14241
+ */
14090
14242
  this.onBlockFailed = null;
14091
14243
  /**
14092
14244
  * Fired once per stream when its Worker decoder stops being routable.
@@ -14128,6 +14280,8 @@ var FECManager = class _FECManager {
14128
14280
  * for it, which it must be.
14129
14281
  */
14130
14282
  this.#onUndelimitedLoss = null;
14283
+ /** Set with the loss handler: true takes an expired hole to close (`closeIsoHoles`). */
14284
+ this.#onIsoHoleExpired = null;
14131
14285
  /** A block every symbol of which vanished was created by SBN evidence. */
14132
14286
  this.onBlockVanished = null;
14133
14287
  /** A block reached a terminal state (completed, recovered, or failed). */
@@ -14170,7 +14324,9 @@ var FECManager = class _FECManager {
14170
14324
  #pendingWorkerCleanupBounds;
14171
14325
  #blockSourceCounts;
14172
14326
  #blockSourceEsis;
14327
+ #blockRebuiltEsis;
14173
14328
  #blockAuthoredLengths;
14329
+ #isoSpanKeys;
14174
14330
  #ssbLengthConflictCount;
14175
14331
  #blockRepairRsIds;
14176
14332
  #blockRsbLengths;
@@ -14212,6 +14368,7 @@ var FECManager = class _FECManager {
14212
14368
  #orderedFrontiers;
14213
14369
  #orderedPendingBlocks;
14214
14370
  #classifiers;
14371
+ #noSpanRaps;
14215
14372
  #streamGeometries;
14216
14373
  #failedBlocks;
14217
14374
  #resolvedBlockClasses;
@@ -14236,6 +14393,7 @@ var FECManager = class _FECManager {
14236
14393
  #decoderUnavailableEvents;
14237
14394
  #repairSymbolSizeMismatchCount;
14238
14395
  #onUndelimitedLoss;
14396
+ #onIsoHoleExpired;
14239
14397
  #encodeTrackName(catalogTrackName) {
14240
14398
  let encoded = this.#encodedTrackNames.get(catalogTrackName);
14241
14399
  if (encoded === void 0) {
@@ -14707,7 +14865,10 @@ var FECManager = class _FECManager {
14707
14865
  timer.handle = null;
14708
14866
  this.#requestRepair(sbn, packetId, catalogTrackName);
14709
14867
  };
14710
- timer.handle = setTimeout(onFire, Math.max(0, deadlineMs - performance.now()));
14868
+ timer.handle = setTimeout(
14869
+ onFire,
14870
+ this.#receiptAtMs === void 0 ? Math.max(0, deadlineMs - performance.now()) : this.#receiptTimerDelayMs(deadlineMs, 0)
14871
+ );
14711
14872
  this.#repairDeadlines.set(key, timer);
14712
14873
  }
14713
14874
  #requestRepair(sbn, packetId, catalogTrackName, settledByOrder = false) {
@@ -14851,8 +15012,14 @@ var FECManager = class _FECManager {
14851
15012
  * Vanished blocks are not enumerated here: their starts are unknown until a
14852
15013
  * repair states them. The SS_IDs between two stated blocks become one
14853
15014
  * repair-less span when the later block's repair arrives (placeIsoRepair).
15015
+ *
15016
+ * A `held` source arrived before its block was known and is fed only now
15017
+ * that a repair, a span or a closed hole placed it: how far its block start
15018
+ * then lies behind the frontier measures how long the receiver held it, not
15019
+ * where the stream was, so only its SS_ID and packet_sequence_number judge
15020
+ * it (BLO-52085).
14854
15021
  */
14855
- #noteSbnSeen(sbn, packetId, catalogTrackName, symbol, packetSequenceNumber, ssId = sbn) {
15022
+ #noteSbnSeen(sbn, packetId, catalogTrackName, symbol, packetSequenceNumber, ssId = sbn, held = false) {
14856
15023
  const streamKey = this.#streamKey(packetId, catalogTrackName);
14857
15024
  const now = performance.now();
14858
15025
  const previous = (symbol === "source" ? this.#sourceFrontiers : this.#highestSbn).get(streamKey);
@@ -14873,6 +15040,7 @@ var FECManager = class _FECManager {
14873
15040
  };
14874
15041
  const discontinuity = () => {
14875
15042
  this.#classifiers.get(streamKey)?.noteSourceDiscontinuity();
15043
+ this.#noSpanRaps.delete(streamKey);
14876
15044
  this.#highestSbn.set(streamKey, { sbn, atMs: now });
14877
15045
  this.#sourceFrontiers.set(streamKey, { sbn, atMs: now });
14878
15046
  if (psn === void 0) this.#sourceAnchors.delete(streamKey);
@@ -14890,7 +15058,7 @@ var FECManager = class _FECManager {
14890
15058
  return sent <= 2147483647 && serial32Distance(from.ssId, to.ssId) <= sent + config.k;
14891
15059
  };
14892
15060
  if (!this.#blockPrecedes(previous.sbn, sbn)) {
14893
- if (symbol === "source" && window2 !== null && this.#blockPrecedes(sbn, previous.sbn) && (this.#blocksBehind(sbn, previous.sbn, packetId) > this.#isoRetentionBlocks(packetId) || psn !== void 0 && anchor !== void 0 && serial32Precedes(ssId, anchor.ssId) && psn !== anchor.psn && !sentInOrder({ ssId, psn }, anchor))) {
15061
+ if (symbol === "source" && window2 !== null && this.#blockPrecedes(sbn, previous.sbn) && (!held && this.#blocksBehind(sbn, previous.sbn, packetId) > this.#isoRetentionBlocks(packetId) || psn !== void 0 && anchor !== void 0 && serial32Precedes(ssId, anchor.ssId) && psn !== anchor.psn && !sentInOrder({ ssId, psn }, anchor))) {
14894
15062
  discontinuity();
14895
15063
  return;
14896
15064
  }
@@ -15152,17 +15320,21 @@ var FECManager = class _FECManager {
15152
15320
  this.#blockWaitDeadlinesMs.delete(key);
15153
15321
  this.#repairHoldUntilMs.delete(key);
15154
15322
  this.#traceLifecycle("timeout-queued", sbn, packetId, catalogTrackName);
15155
- this.#finalizeExpiredBlock(sbn, packetId, catalogTrackName);
15323
+ this.#finalizeExpiredBlock(sbn, packetId, catalogTrackName, currentDeadlineMs ?? firedAtMs);
15156
15324
  };
15157
- timer.handle = setTimeout(onFire, Math.max(1, Math.ceil(deadlineMs - nowMs)));
15325
+ timer.handle = setTimeout(
15326
+ onFire,
15327
+ this.#receiptAtMs === void 0 ? Math.max(1, Math.ceil(deadlineMs - nowMs)) : this.#receiptTimerDelayMs(deadlineMs, 1)
15328
+ );
15158
15329
  this.#activeBlocks.set(key, timer);
15159
15330
  this.#indexBlockKey(key, sbn, packetId);
15160
15331
  }
15161
15332
  /**
15162
15333
  * Hand a block whose delivery is over to its decoder for finalization: the
15163
- * datagram deadline fired, or the ordered relay moved past it.
15334
+ * datagram deadline fired, or the ordered relay moved past it. `dueAtMs`
15335
+ * is when it was due: its deadline, or now.
15164
15336
  */
15165
- #finalizeExpiredBlock(sbn, packetId, catalogTrackName) {
15337
+ #finalizeExpiredBlock(sbn, packetId, catalogTrackName, dueAtMs = performance.now()) {
15166
15338
  const key = this.#blockKey(sbn, packetId, catalogTrackName);
15167
15339
  if (this.#materializedBlocks.has(key) && !this.#blockPathCounts.has(key)) {
15168
15340
  this.#failBlock(sbn, packetId, catalogTrackName);
@@ -15178,7 +15350,7 @@ var FECManager = class _FECManager {
15178
15350
  if (workerClient) {
15179
15351
  this.#pendingWorkerCleanups.add(key);
15180
15352
  this.#pendingWorkerCleanupClients.set(key, workerClient);
15181
- this.#boundOwedCleanup(key, packetId, workerClient);
15353
+ this.#boundOwedCleanup(key, packetId, workerClient, dueAtMs);
15182
15354
  workerClient.cleanup(sbn);
15183
15355
  } else {
15184
15356
  const decoder = this.#decoderForPacket(packetId, catalogTrackName)?.client;
@@ -15285,6 +15457,9 @@ var FECManager = class _FECManager {
15285
15457
  }
15286
15458
  if (policyChanged) {
15287
15459
  this.#bumpEpoch(packetId);
15460
+ for (const key of [...this.#noSpanRaps.keys()]) {
15461
+ if (this.#belongsToPacket(key, packetId)) this.#noSpanRaps.delete(key);
15462
+ }
15288
15463
  for (const [key, classifier] of [...this.#classifiers]) {
15289
15464
  if (!this.#belongsToPacket(key, packetId)) continue;
15290
15465
  const stream = this.#parseStreamKey(key);
@@ -15487,7 +15662,9 @@ var FECManager = class _FECManager {
15487
15662
  }
15488
15663
  resetMap(this.#blockSourceCounts);
15489
15664
  resetMap(this.#blockSourceEsis);
15665
+ resetMap(this.#blockRebuiltEsis);
15490
15666
  resetMap(this.#blockAuthoredLengths);
15667
+ resetSet(this.#isoSpanKeys);
15491
15668
  resetMap(this.#blockRepairRsIds);
15492
15669
  resetMap(this.#blockRsbLengths);
15493
15670
  resetMap(this.#heldLayerStates);
@@ -15520,6 +15697,7 @@ var FECManager = class _FECManager {
15520
15697
  resetMap(this.#isoLocators);
15521
15698
  resetMap(this.#sourceAnchors);
15522
15699
  this.#classifiers.delete(this.#streamKey(packetId, catalogTrackName));
15700
+ this.#noSpanRaps.delete(this.#streamKey(packetId, catalogTrackName));
15523
15701
  this.#bumpEpoch(packetId);
15524
15702
  for (const [sbn, keys] of this.#blockKeysBySbn) {
15525
15703
  for (const key of keys.keys()) {
@@ -15610,7 +15788,9 @@ var FECManager = class _FECManager {
15610
15788
  dropMap(this.#pendingWorkerCleanupClients);
15611
15789
  dropMap(this.#blockSourceCounts);
15612
15790
  dropMap(this.#blockSourceEsis);
15791
+ dropMap(this.#blockRebuiltEsis);
15613
15792
  dropMap(this.#blockAuthoredLengths);
15793
+ dropSet(this.#isoSpanKeys);
15614
15794
  dropMap(this.#blockRepairRsIds);
15615
15795
  dropMap(this.#blockRsbLengths);
15616
15796
  dropMap(this.#blockPathCounts);
@@ -15626,6 +15806,7 @@ var FECManager = class _FECManager {
15626
15806
  dropMap(this.#isoLocators);
15627
15807
  dropMap(this.#sourceAnchors);
15628
15808
  dropMap(this.#classifiers);
15809
+ dropMap(this.#noSpanRaps);
15629
15810
  dropMap(this.#streamGeometries);
15630
15811
  dropSet(this.#pendingWorkerCleanups);
15631
15812
  dropSet(this.#completedBlocks);
@@ -15771,6 +15952,9 @@ var FECManager = class _FECManager {
15771
15952
  this.#unindexBlockKey(key);
15772
15953
  }
15773
15954
  }
15955
+ if (isSourceSymbol && source === "multicast") {
15956
+ this.#noteMulticastSource(this.#isoLocatorFor(packetId, catalogTrackName), sbn + esi >>> 0, packetId, catalogTrackName);
15957
+ }
15774
15958
  return true;
15775
15959
  }
15776
15960
  #observeBlockCatalogTrackName(blockKey, sbn, packetId, catalogTrackName) {
@@ -15949,8 +16133,9 @@ var FECManager = class _FECManager {
15949
16133
  return true;
15950
16134
  }
15951
16135
  /**
15952
- * Drop a block's repair hold: its FETCH ended without delivering. The block
15953
- * falls back to its multicast wait, which may already have passed.
16136
+ * Drop a block's repair hold: its FETCH ended without delivering, or brought
16137
+ * back only a repair that cannot complete it. The block falls back to its
16138
+ * multicast wait, which may already have passed.
15954
16139
  */
15955
16140
  releaseRepairHold(sbn, packetId, catalogTrackName) {
15956
16141
  const key = this.#blockKey(sbn, packetId, catalogTrackName);
@@ -16088,6 +16273,8 @@ var FECManager = class _FECManager {
16088
16273
  const locator = this.#growingIsoLocatorFor(packetId, catalogTrackName);
16089
16274
  const evictedBefore = locator.heldEvicted;
16090
16275
  const held = locator.hold(ssId, source);
16276
+ this.#settleExpiredIsoHoles(locator, packetId, catalogTrackName);
16277
+ if (held && source.source === "multicast") this.#noteMulticastSource(locator, ssId, packetId, catalogTrackName);
16091
16278
  const evicted = locator.heldEvicted - evictedBefore;
16092
16279
  if (evicted > 0) {
16093
16280
  const before = this.#isoHeldEvictedCount;
@@ -16108,17 +16295,20 @@ var FECManager = class _FECManager {
16108
16295
  * contradicts a known block is refused (counted) and must be dropped.
16109
16296
  */
16110
16297
  placeIsoRepair(packetId, catalogTrackName, ssStart, ssbLength) {
16111
- const placement = this.#growingIsoLocatorFor(packetId, catalogTrackName).placeRepair(ssStart, ssbLength);
16298
+ const locator = this.#growingIsoLocatorFor(packetId, catalogTrackName);
16299
+ const placement = locator.placeRepair(ssStart, ssbLength);
16300
+ this.#settleExpiredIsoHoles(locator, packetId, catalogTrackName);
16112
16301
  if (placement.status !== "placed") {
16113
16302
  this.#isoRepairRefusedCount++;
16114
16303
  if (this.#isoRepairRefusedCount <= 5 || this.#isoRepairRefusedCount % 100 === 0) {
16115
16304
  console.warn(`[FECManager] packetId=${packetId} iso-ssbg1-v1 repair refused (${placement.status}): ${placement.reason}`);
16116
16305
  }
16306
+ if (placement.ofSpan !== void 0) this.releaseRepairHold(placement.ofSpan.start, packetId, catalogTrackName);
16117
16307
  return placement;
16118
16308
  }
16309
+ this.#isoSpanKeys.delete(this.#blockKey(placement.block.start, packetId, catalogTrackName));
16119
16310
  if (placement.span !== void 0) this.#openIsoSpan(placement.span, packetId, catalogTrackName);
16120
16311
  if (placement.hole !== void 0) {
16121
- const locator = this.#isoLocatorFor(packetId, catalogTrackName);
16122
16312
  const hole = placement.hole.block;
16123
16313
  const previous = locator.blockEndingAt(hole.start);
16124
16314
  const loss = {
@@ -16139,17 +16329,29 @@ var FECManager = class _FECManager {
16139
16329
  * `handler`, instead of a repair-less span; null restores spans. Set by the
16140
16330
  * range repair scheduler; applies to every stream from its next gap. The
16141
16331
  * handler and the deferral are one setting, so no hole is ever created with
16142
- * nobody to close it.
16332
+ * nobody to close it. `onExpired` is told of each pending hole the stream
16333
+ * runs past the locator's horizon (BLO-52085): returning true, it closes
16334
+ * the hole (`closeIsoHoles`), so the span it leaves still fails and is
16335
+ * reported; otherwise the hole is discarded with its sources.
16143
16336
  */
16144
- setUndelimitedLossHandler(handler) {
16337
+ setUndelimitedLossHandler(handler, onExpired = null) {
16145
16338
  this.#onUndelimitedLoss = handler;
16339
+ this.#onIsoHoleExpired = handler === null ? null : onExpired;
16146
16340
  this.#deferIsoGaps = handler !== null;
16147
16341
  for (const locator of this.#isoLocators.values()) locator.deferIncompleteGaps = this.#deferIsoGaps;
16148
16342
  }
16343
+ /** Hand each hole the locator's horizon passed to its owner, or discard it (setUndelimitedLossHandler). */
16344
+ #settleExpiredIsoHoles(locator, packetId, catalogTrackName) {
16345
+ for (const hole of locator.takeExpiredHoles()) {
16346
+ const expired = { packetId, catalogTrackName, holeStart: hole.start, holeLength: hole.length };
16347
+ if (this.#onIsoHoleExpired?.(expired) !== true) locator.closeHolesWithin(hole.start, hole.length);
16348
+ }
16349
+ }
16149
16350
  /**
16150
- * The range repair for a hole ended: every hole still pending inside
16151
- * `[start, start + length)` becomes a repair-less span. Returns those spans
16152
- * with the held sources they placed, for the caller to feed (MmtpRouter).
16351
+ * The range repair for a hole ended: every hole inside `[start, start +
16352
+ * length)`, pending or expired, becomes a repair-less span. Returns those
16353
+ * spans with the held sources they placed, for the caller to feed
16354
+ * (MmtpRouter).
16153
16355
  */
16154
16356
  closeIsoHoles(packetId, catalogTrackName, start, length) {
16155
16357
  const locator = this.#isoLocators.get(this.#streamKey(packetId, catalogTrackName));
@@ -16168,7 +16370,10 @@ var FECManager = class _FECManager {
16168
16370
  */
16169
16371
  #openIsoSpan(span, packetId, catalogTrackName) {
16170
16372
  const key = this.#blockKey(span.block.start, packetId, catalogTrackName);
16171
- if (!this.#blockAuthoredLengths.has(key)) this.#blockAuthoredLengths.set(key, span.block.length);
16373
+ if (!this.#blockAuthoredLengths.has(key)) {
16374
+ this.#blockAuthoredLengths.set(key, span.block.length);
16375
+ this.#isoSpanKeys.add(key);
16376
+ }
16172
16377
  if (span.placed.length === 0) this.#materializeBlock(span.block.start, packetId, catalogTrackName, true);
16173
16378
  }
16174
16379
  /**
@@ -16256,11 +16461,14 @@ var FECManager = class _FECManager {
16256
16461
  /**
16257
16462
  * Whether an MPU boundary still bounds this stream's RAPs (see
16258
16463
  * `BlockClassifier.mpuBoundsRaps`). Gap decisions drop MPU evidence once
16259
- * it does not. A stream with no classifier yet has shown nothing against it.
16464
+ * it does not. A stream with no classifier yet has shown nothing against it,
16465
+ * and neither has a stream without catalog timing until it releases two
16466
+ * RAPs inside one MPU.
16260
16467
  */
16261
16468
  mpuBoundsRapsFor(packetId, catalogTrackName) {
16262
- if (!this.#hasCatalogTimingForPacket(packetId)) return false;
16263
- return this.#classifiers.get(this.#streamKey(packetId, catalogTrackName))?.mpuBoundsRaps ?? true;
16469
+ const streamKey = this.#streamKey(packetId, catalogTrackName);
16470
+ if (!this.#hasCatalogTimingForPacket(packetId)) return this.#noSpanRaps.get(streamKey)?.inside !== true;
16471
+ return this.#classifiers.get(streamKey)?.mpuBoundsRaps ?? true;
16264
16472
  }
16265
16473
  /** A block's class now, or frozen at its resolution. */
16266
16474
  blockClassFor(sbn, packetId, catalogTrackName) {
@@ -16286,7 +16494,14 @@ var FECManager = class _FECManager {
16286
16494
  * invariant (consecutive MPUs exactly one keyframe interval apart).
16287
16495
  */
16288
16496
  noteReleasedRap(packetId, catalogTrackName, mpu, presentationTime, mediaTimescale) {
16289
- if (!this.#trackConfigs.has(packetId) || !this.#hasCatalogTimingForPacket(packetId)) return;
16497
+ if (!this.#trackConfigs.has(packetId)) return;
16498
+ if (!this.#hasCatalogTimingForPacket(packetId)) {
16499
+ const streamKey = this.#streamKey(packetId, catalogTrackName);
16500
+ const last = this.#noSpanRaps.get(streamKey);
16501
+ const inside = last?.inside === true || last?.mpu === mpu && last.presentationTime !== presentationTime;
16502
+ this.#noSpanRaps.set(streamKey, { mpu, presentationTime, inside });
16503
+ return;
16504
+ }
16290
16505
  this.#applyClassifierChanges(
16291
16506
  packetId,
16292
16507
  catalogTrackName,
@@ -16499,11 +16714,14 @@ var FECManager = class _FECManager {
16499
16714
  });
16500
16715
  client.onCleanupComplete((sbn) => {
16501
16716
  if (this.#releasedWorkerClients.has(client)) return;
16502
- const cleanupKeys = [...this.#pendingWorkerCleanups].filter((key) => {
16717
+ const owed = [...this.#pendingWorkerCleanups].filter((key) => this.#pendingWorkerCleanupClients.get(key) === client);
16718
+ const cleanupKeys = owed.filter((key) => {
16503
16719
  const block = this.#parseBlockKey(key);
16504
- return block.packetId === packetId && block.sbn === sbn && this.#pendingWorkerCleanupClients.get(key) === client;
16720
+ return block.packetId === packetId && block.sbn === sbn;
16505
16721
  });
16506
- for (const key of cleanupKeys) this.#settleOwedCleanup(key, "cleanup-ack");
16722
+ const askedAtMs = Math.max(...cleanupKeys.map((key) => this.#pendingWorkerCleanupBounds.get(key)?.askedAtMs ?? -Infinity));
16723
+ const due = owed.filter((key) => !cleanupKeys.includes(key) && (this.#pendingWorkerCleanupBounds.get(key)?.dueAtMs ?? Infinity) <= askedAtMs);
16724
+ for (const key of [...cleanupKeys, ...due]) this.#settleOwedCleanup(key, "cleanup-ack");
16507
16725
  });
16508
16726
  client.onError((msg, code) => {
16509
16727
  const requestedKind = code ?? msg;
@@ -16573,9 +16791,11 @@ var FECManager = class _FECManager {
16573
16791
  * fail deadline already passed, so failing it is the decision that deadline
16574
16792
  * made. A late ack then finds nothing owed and is a no-op.
16575
16793
  */
16576
- #boundOwedCleanup(key, packetId, client) {
16794
+ #boundOwedCleanup(key, packetId, client, dueAtMs) {
16577
16795
  this.#clearOwedCleanupBound(key);
16578
16796
  const bound = {
16797
+ dueAtMs,
16798
+ askedAtMs: performance.now(),
16579
16799
  handle: setTimeout(() => {
16580
16800
  if (this.#pendingWorkerCleanupBounds.get(key) !== bound) return;
16581
16801
  this.#pendingWorkerCleanupBounds.delete(key);
@@ -16673,7 +16893,15 @@ var FECManager = class _FECManager {
16673
16893
  this.#isoSourceOversizeCount++;
16674
16894
  return null;
16675
16895
  }
16676
- const sbn = located.block.start;
16896
+ return this.#feedDecoderSource(packetId, ssId, symbolData, catalogTrackName, located.block.start);
16897
+ }
16898
+ /**
16899
+ * Hand one placed source symbol to the stream's decoder: its Worker, which
16900
+ * answers through onFrame (null here), or its synchronous decoder, whose
16901
+ * recovered block is returned. `sbn` is the SS_Start of the block a repair
16902
+ * stated for `ssId`.
16903
+ */
16904
+ #feedDecoderSource(packetId, ssId, symbolData, catalogTrackName, sbn) {
16677
16905
  const blockKey = this.#blockKey(sbn, packetId, catalogTrackName);
16678
16906
  const syncBlockOwner = this.#syncDecoderBlockOwners.get(blockKey);
16679
16907
  const workerClient = this.#routableWorkerForPacket(packetId, catalogTrackName);
@@ -16702,6 +16930,74 @@ var FECManager = class _FECManager {
16702
16930
  }
16703
16931
  return recovered;
16704
16932
  }
16933
+ /**
16934
+ * Feed a source symbol of this stream that another FEC instance over the
16935
+ * same packets rebuilt: a keyframe overlay recovery whose base SS_ID the
16936
+ * caller determined (draft-ramadan-moq-mmt-02 7.1, 7.4.4; joint decoding,
16937
+ * BLO-52085). It joins its block's decoder as a received source would, so
16938
+ * it reduces the erasures the block still has to solve, but it is not a
16939
+ * network arrival: no transport accounting, no classification evidence,
16940
+ * no deadline refresh, and it never makes a block clean.
16941
+ *
16942
+ * Only a block a repair stated takes one (a repair-less span has no
16943
+ * decoder block). A block that is terminal, or already holds that source,
16944
+ * does not. The symbol is the exact protected packet, unpadded, as
16945
+ * feedSourceSymbol takes it.
16946
+ *
16947
+ * `recovered` is the block the synchronous decoder completed with it; the
16948
+ * caller routes it and completes the block, as for any recovery. When
16949
+ * every source of the block is then present, received or rebuilt, and no
16950
+ * decoder returned it (a Worker answers asynchronously, and reports such a
16951
+ * block as clean on its own side), the block is completed here as
16952
+ * recovered, with nothing left to route.
16953
+ */
16954
+ feedRebuiltSourceSymbol(packetId, ssId, symbolData, catalogTrackName) {
16955
+ if (!this.#hasBlockProfile(packetId)) return NOT_TAKEN;
16956
+ const located = this.#isoLocatorFor(packetId, catalogTrackName).locate(ssId);
16957
+ if (located === null || located.block.span) return NOT_TAKEN;
16958
+ if (!ssbg1FitsSymbol(symbolData.byteLength, this.#trackConfig(packetId).symbolSize)) return NOT_TAKEN;
16959
+ const sbn = located.block.start;
16960
+ const key = this.#blockKey(sbn, packetId, catalogTrackName);
16961
+ if (this.#completedBlocks.has(key) || this.#blockSourceEsis.get(key)?.has(located.esi)) return NOT_TAKEN;
16962
+ let rebuilt = this.#blockRebuiltEsis.get(key);
16963
+ if (rebuilt?.has(located.esi)) return NOT_TAKEN;
16964
+ if (rebuilt === void 0) {
16965
+ rebuilt = /* @__PURE__ */ new Set();
16966
+ this.#blockRebuiltEsis.set(key, rebuilt);
16967
+ }
16968
+ const config = this.#trackConfigs.get(packetId);
16969
+ const wasShort = config !== void 0 && this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config) !== void 0;
16970
+ rebuilt.add(located.esi);
16971
+ const recovered = this.#feedDecoderSource(packetId, ssId, symbolData, catalogTrackName, sbn);
16972
+ if (recovered === null && !this.#completeWhenSourcesPresent(key, sbn, located.block.length, packetId, catalogTrackName)) {
16973
+ if (wasShort && this.#activeBlocks.has(key) && this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config) === void 0) {
16974
+ this.#armBlockTimeout(sbn, packetId, catalogTrackName, this.#multicastWaitMs(config));
16975
+ }
16976
+ }
16977
+ return { taken: true, recovered };
16978
+ }
16979
+ /**
16980
+ * A block that holds every one of its `length` sources, received or
16981
+ * rebuilt by another instance, with at least one rebuilt, is recovered:
16982
+ * every packet of it was delivered, by its own arrival or by the instance
16983
+ * that rebuilt it, so nothing is routed. Returns whether it completed it.
16984
+ */
16985
+ #completeWhenSourcesPresent(key, sbn, length, packetId, catalogTrackName) {
16986
+ const rebuilt = this.#blockRebuiltEsis.get(key);
16987
+ if (rebuilt === void 0 || this.#completedBlocks.has(key)) return false;
16988
+ let present = rebuilt.size;
16989
+ for (const esi of this.#blockSourceEsis.get(key) ?? []) if (!rebuilt.has(esi)) present++;
16990
+ if (present < length) return false;
16991
+ this.#traceLifecycle("recovered", sbn, packetId, catalogTrackName);
16992
+ this.countRecovery(packetId, sbn, catalogTrackName);
16993
+ this.cancelBlockTimeout(sbn, packetId, catalogTrackName);
16994
+ if (catalogTrackName === void 0) {
16995
+ this.onBlockComplete?.(sbn, packetId);
16996
+ } else {
16997
+ this.onBlockComplete?.(sbn, packetId, catalogTrackName);
16998
+ }
16999
+ return true;
17000
+ }
16705
17001
  /**
16706
17002
  * fec-01 4.5: a reed-solomon repair packet whose SSB_length + RSB_length
16707
17003
  * exceeds 255, or whose RS_ID is not below RSB_length, is discarded with the
@@ -16904,8 +17200,75 @@ var FECManager = class _FECManager {
16904
17200
  this.#completeCleanBlock(blockKey, sbn, packetId, catalogTrackName);
16905
17201
  return null;
16906
17202
  }
17203
+ if (recovered === null) this.#shortenUnrecoverableWait(blockKey, sbn, packetId, catalogTrackName);
16907
17204
  return recovered;
16908
17205
  }
17206
+ /**
17207
+ * A first-arrival multicast source, held or placed. A datagram flow keeps
17208
+ * its sources in send order, so one that moves the stream's newest source
17209
+ * position past a block leaves the sources that block still misses
17210
+ * reordered behind it at best: it shortens the wait of every block it
17211
+ * passed that only a late source can complete. MoQ is left out: its streams
17212
+ * can overtake one another, so a MoQ source shows nothing about the sources
17213
+ * sent before it.
17214
+ */
17215
+ #noteMulticastSource(locator, ssId, packetId, catalogTrackName) {
17216
+ const previous = locator.newestSource;
17217
+ if (!locator.noteSource(ssId) || previous === void 0) return;
17218
+ for (const block of locator.blocksBetween(previous - 1 >>> 0, ssId)) {
17219
+ if (block.span || !serial32Precedes(serial32Add(block.start, block.length - 1), ssId)) continue;
17220
+ this.#shortenUnrecoverableWait(
17221
+ this.#blockKey(block.start, packetId, catalogTrackName),
17222
+ block.start,
17223
+ packetId,
17224
+ catalogTrackName
17225
+ );
17226
+ }
17227
+ }
17228
+ /**
17229
+ * The wait of a block that only a late source can complete (BLO-52085): a
17230
+ * repair stated it, its received sources plus every symbol its repair group
17231
+ * carries (RSB_length) are short of K' (the repair scheduler's
17232
+ * group-cannot-complete), and a later source of the stream has arrived, so
17233
+ * a source it misses is lost or reordered behind that one. The span, which
17234
+ * covers repair symbols paced after its sources, buys it nothing: it waits
17235
+ * the late-symbol allowance alone, the multicast wait less the span (the
17236
+ * catalog tolerance or the measured delivery gap). Undefined for any other
17237
+ * block.
17238
+ */
17239
+ #lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config) {
17240
+ const authoredLength = this.#blockAuthoredLengths.get(key);
17241
+ const rsbLength = this.#blockRsbLengths.get(key);
17242
+ if (authoredLength === void 0 || rsbLength === void 0) return void 0;
17243
+ let present = this.#blockSourceCounts.get(key) ?? 0;
17244
+ const received = this.#blockSourceEsis.get(key);
17245
+ for (const esi of this.#blockRebuiltEsis.get(key) ?? []) if (!received?.has(esi)) present++;
17246
+ if (present + rsbLength >= authoredLength) return void 0;
17247
+ const newestSource = this.#isoLocators.get(this.#streamKey(packetId, catalogTrackName))?.newestSource;
17248
+ if (newestSource === void 0 || !serial32Precedes(serial32Add(sbn, authoredLength - 1), newestSource)) {
17249
+ return void 0;
17250
+ }
17251
+ return this.#multicastWaitMs(config) - config.interleaveDepthMs;
17252
+ }
17253
+ /**
17254
+ * Bring an open block's wait in to its late-source wait, when it has one.
17255
+ * Never later than it is armed: only a source can complete such a block,
17256
+ * so a repair of its group (paced across the span) must not keep it open.
17257
+ */
17258
+ #shortenUnrecoverableWait(key, sbn, packetId, catalogTrackName) {
17259
+ if (!this.#activeBlocks.has(key)) return;
17260
+ const config = this.#trackConfigs.get(packetId);
17261
+ if (config === void 0) return;
17262
+ const waitMs = this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config);
17263
+ if (waitMs === void 0) return;
17264
+ const armedAtMs = this.#blockWaitDeadlinesMs.get(key);
17265
+ this.#armBlockTimeout(
17266
+ sbn,
17267
+ packetId,
17268
+ catalogTrackName,
17269
+ armedAtMs === void 0 ? waitMs : Math.min(waitMs, armedAtMs - this.#receiptNow())
17270
+ );
17271
+ }
16909
17272
  /**
16910
17273
  * Record a block's authored SSB_length from a repair symbol. Returns false,
16911
17274
  * leaving the block unchanged, when the symbol contradicts the block: a
@@ -17003,12 +17366,13 @@ var FECManager = class _FECManager {
17003
17366
  * the inactivity deadline on each symbol, records the symbol's in-band
17004
17367
  * classification evidence, and marks the block clean when all of its
17005
17368
  * authored source symbols arrived: the SSB_length a repair symbol declared,
17006
- * otherwise K.
17369
+ * otherwise K. `held` marks a source held unplaced since it arrived and
17370
+ * placed only now (see #noteSbnSeen).
17007
17371
  *
17008
17372
  * Throws for a packetId that was never configured, so a classified block
17009
17373
  * never reads the manager-global K or timeout.
17010
17374
  */
17011
- trackBlock(sbn, packetId, catalogTrackName, esi, evidence) {
17375
+ trackBlock(sbn, packetId, catalogTrackName, esi, evidence, held = false) {
17012
17376
  const trackConfig = this.#trackConfigs.get(packetId);
17013
17377
  if (trackConfig === void 0) {
17014
17378
  throw new Error(
@@ -17024,7 +17388,8 @@ var FECManager = class _FECManager {
17024
17388
  catalogTrackName,
17025
17389
  "source",
17026
17390
  evidence.packetSequenceNumber,
17027
- Number.isInteger(esi) ? sbn + esi >>> 0 : sbn
17391
+ Number.isInteger(esi) ? sbn + esi >>> 0 : sbn,
17392
+ held
17028
17393
  );
17029
17394
  const authoredLength = this.#blockAuthoredLengths.get(key) ?? k;
17030
17395
  if (Number.isInteger(esi) && esi >= authoredLength) {
@@ -17060,6 +17425,15 @@ var FECManager = class _FECManager {
17060
17425
  this.#completeCleanBlock(key, sbn, packetId, catalogTrackName);
17061
17426
  return;
17062
17427
  }
17428
+ if (this.#blockRebuiltEsis.has(key) && this.#completeWhenSourcesPresent(
17429
+ key,
17430
+ sbn,
17431
+ this.#isoLocatorFor(packetId, catalogTrackName).blockAt(sbn)?.length ?? authoredLength,
17432
+ packetId,
17433
+ catalogTrackName
17434
+ )) {
17435
+ return;
17436
+ }
17063
17437
  this.noteBlockActivity(sbn, packetId, catalogTrackName, true);
17064
17438
  }
17065
17439
  #completeCleanBlock(key, sbn, packetId, catalogTrackName) {
@@ -17068,7 +17442,9 @@ var FECManager = class _FECManager {
17068
17442
  this.cancelBlockTimeout(sbn, packetId, catalogTrackName);
17069
17443
  this.#blockSourceCounts.delete(key);
17070
17444
  this.#blockSourceEsis.delete(key);
17445
+ this.#blockRebuiltEsis.delete(key);
17071
17446
  this.#blockAuthoredLengths.delete(key);
17447
+ this.#isoSpanKeys.delete(key);
17072
17448
  this.#blockRepairRsIds.delete(key);
17073
17449
  this.#blockRsbLengths.delete(key);
17074
17450
  this.#markTerminal(key, sbn, packetId, true);
@@ -17099,6 +17475,19 @@ var FECManager = class _FECManager {
17099
17475
  #receiptNow() {
17100
17476
  return this.#receiptAtMs ?? performance.now();
17101
17477
  }
17478
+ /**
17479
+ * The whole-ms delay of a timer for `deadlineMs` armed inside a receipt
17480
+ * scope: counted from the receipt time, as the timer of a symbol fed when
17481
+ * it was received was, less the whole ms since. So it fires on the tick
17482
+ * that timer would have, never later than a delay counted from now, and
17483
+ * its handler still re-arms a timer that fires before its deadline.
17484
+ * Counted from now, a deadline set from the receipt time is rounded up
17485
+ * from another fraction of a ms (BLO-52085).
17486
+ */
17487
+ #receiptTimerDelayMs(deadlineMs, minMs) {
17488
+ const receivedAtMs = this.#receiptAtMs;
17489
+ return Math.max(minMs, Math.ceil(deadlineMs - receivedAtMs) - Math.ceil(performance.now() - receivedAtMs));
17490
+ }
17102
17491
  /**
17103
17492
  * A held packet admitted late arms from its (earlier) receipt time; that
17104
17493
  * never pulls in a deadline later activity already armed. Outside a
@@ -17134,10 +17523,20 @@ var FECManager = class _FECManager {
17134
17523
  this.#repairDeadlineMs(trackConfig)
17135
17524
  );
17136
17525
  }
17137
- this.#armBlockTimeout(sbn, packetId, catalogTrackName, timeoutMs);
17526
+ if (!sourceProgress && this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, trackConfig) !== void 0) {
17527
+ this.#shortenUnrecoverableWait(key, sbn, packetId, catalogTrackName);
17528
+ return;
17529
+ }
17530
+ this.#armBlockTimeout(
17531
+ sbn,
17532
+ packetId,
17533
+ catalogTrackName,
17534
+ this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, trackConfig) ?? timeoutMs
17535
+ );
17138
17536
  }
17139
17537
  #failBlock(sbn, packetId, catalogTrackName) {
17140
17538
  const key = this.#blockKey(sbn, packetId, catalogTrackName);
17539
+ const span = this.#isoSpanKeys.delete(key) ? { length: this.#blockAuthoredLengths.get(key) ?? 0, sources: this.#blockSourceCounts.get(key) ?? 0 } : void 0;
17141
17540
  this.#traceLifecycle("failed", sbn, packetId, catalogTrackName);
17142
17541
  this.#clearRepairDeadline(key);
17143
17542
  this.#clearBlockTimer(key);
@@ -17147,6 +17546,7 @@ var FECManager = class _FECManager {
17147
17546
  }
17148
17547
  this.#blockSourceCounts.delete(key);
17149
17548
  this.#blockSourceEsis.delete(key);
17549
+ this.#blockRebuiltEsis.delete(key);
17150
17550
  this.#blockAuthoredLengths.delete(key);
17151
17551
  this.#blockRepairRsIds.delete(key);
17152
17552
  this.#blockRsbLengths.delete(key);
@@ -17156,7 +17556,8 @@ var FECManager = class _FECManager {
17156
17556
  this.#markPathBlockComplete(sbn, packetId, catalogTrackName);
17157
17557
  this.#fecStats.blocksFailed++;
17158
17558
  this.#statsForPacket(packetId).blocksFailed++;
17159
- this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass);
17559
+ if (span === void 0) this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass);
17560
+ else this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass, span);
17160
17561
  this.onBlockResolved?.(sbn, packetId, catalogTrackName);
17161
17562
  }
17162
17563
  /**
@@ -17185,7 +17586,9 @@ var FECManager = class _FECManager {
17185
17586
  this.#retireBlockClass(key, block.sbn, block.packetId, block.catalogTrackName);
17186
17587
  this.#blockSourceCounts.delete(key);
17187
17588
  this.#blockSourceEsis.delete(key);
17589
+ this.#blockRebuiltEsis.delete(key);
17188
17590
  this.#blockAuthoredLengths.delete(key);
17591
+ this.#isoSpanKeys.delete(key);
17189
17592
  this.#blockRepairRsIds.delete(key);
17190
17593
  this.#blockRsbLengths.delete(key);
17191
17594
  this.#clearBlockTimer(key);
@@ -17276,7 +17679,9 @@ var FECManager = class _FECManager {
17276
17679
  this.#blockKeysBySbn.clear();
17277
17680
  this.#blockSourceCounts.clear();
17278
17681
  this.#blockSourceEsis.clear();
17682
+ this.#blockRebuiltEsis.clear();
17279
17683
  this.#blockAuthoredLengths.clear();
17684
+ this.#isoSpanKeys.clear();
17280
17685
  this.#blockRepairRsIds.clear();
17281
17686
  this.#blockRsbLengths.clear();
17282
17687
  this.#heldLayerStates.clear();
@@ -17301,6 +17706,7 @@ var FECManager = class _FECManager {
17301
17706
  this.#blockAnchorsMs.clear();
17302
17707
  this.#blockCatalogTrackNames.clear();
17303
17708
  this.#classifiers.clear();
17709
+ this.#noSpanRaps.clear();
17304
17710
  this.#streamGeometries.clear();
17305
17711
  this.#failedBlocks.clear();
17306
17712
  this.#resolvedBlockClasses.clear();
@@ -17831,9 +18237,21 @@ var IsoBmffTimingResolver = class {
17831
18237
  }
17832
18238
  #track = null;
17833
18239
  #fragments = /* @__PURE__ */ new Map();
18240
+ /** First sample of each pending fragment, kept in `#delivered` with its sample count. */
18241
+ #firstSamples = /* @__PURE__ */ new Map();
18242
+ /**
18243
+ * First sample and sample count of the newest fragments whose every slot was
18244
+ * cleared. Abandonment counts: `releaseSample` clears a slot too, which
18245
+ * matches the combiner, since it retires an abandoned sample as well.
18246
+ */
18247
+ #delivered = /* @__PURE__ */ new Map();
17834
18248
  #pendingSamples = 0;
17835
18249
  #evictedSamples = 0;
17836
18250
  #lastMovieFragmentRejection = null;
18251
+ /** True once an FT=0 has bound the track that FT=1 timing is read against. */
18252
+ get initialized() {
18253
+ return this.#track !== null;
18254
+ }
17837
18255
  get pendingSampleCount() {
17838
18256
  return this.#pendingSamples;
17839
18257
  }
@@ -17857,7 +18275,14 @@ var IsoBmffTimingResolver = class {
17857
18275
  this.#track = timing;
17858
18276
  return true;
17859
18277
  }
17860
- addMovieFragment(mpuSequenceNumber2, data, additionalSampleLimit = this.maxPendingSamples) {
18278
+ /**
18279
+ * `recovered` marks an FEC-recovered copy. A recovered block hands back
18280
+ * every FT=1 it holds, the received ones included, so a recovered copy of a
18281
+ * fragment whose samples are all done is a duplicate: admitting it again
18282
+ * would charge stale timing and could displace the live MPU's. A source
18283
+ * copy is still admitted again (a rendition switch re-arms timing that way).
18284
+ */
18285
+ addMovieFragment(mpuSequenceNumber2, data, additionalSampleLimit = this.maxPendingSamples, recovered = false) {
17861
18286
  this.#lastMovieFragmentRejection = null;
17862
18287
  if (!Number.isInteger(additionalSampleLimit) || additionalSampleLimit < 0) {
17863
18288
  this.#lastMovieFragmentRejection = "invalid";
@@ -17883,6 +18308,8 @@ var IsoBmffTimingResolver = class {
17883
18308
  (sample, index) => sample !== null && sameSample(sample, parsed.samples[index])
17884
18309
  );
17885
18310
  }
18311
+ const delivered = recovered ? this.#delivered.get(key) : void 0;
18312
+ if (delivered && delivered.sampleCount === parsed.samples.length && sameSample(delivered.first, parsed.samples[0])) return true;
17886
18313
  let freed = 0;
17887
18314
  while (this.#fragments.size > 0 && (this.#pendingSamples + parsed.samples.length > this.maxPendingSamples || parsed.samples.length - freed > additionalSampleLimit)) {
17888
18315
  freed += this.#evictOldestFragment();
@@ -17896,6 +18323,7 @@ var IsoBmffTimingResolver = class {
17896
18323
  return false;
17897
18324
  }
17898
18325
  this.#fragments.set(key, [...parsed.samples]);
18326
+ this.#firstSamples.set(key, parsed.samples[0]);
17899
18327
  this.#pendingSamples += parsed.samples.length;
17900
18328
  return true;
17901
18329
  }
@@ -17953,6 +18381,7 @@ var IsoBmffTimingResolver = class {
17953
18381
  let live = 0;
17954
18382
  for (const sample of samples) if (sample !== null) live++;
17955
18383
  this.#fragments.delete(oldest);
18384
+ this.#firstSamples.delete(oldest);
17956
18385
  this.#pendingSamples -= live;
17957
18386
  this.#evictedSamples += live;
17958
18387
  return live;
@@ -17968,12 +18397,23 @@ var IsoBmffTimingResolver = class {
17968
18397
  if (!samples || !samples[index]) return false;
17969
18398
  samples[index] = null;
17970
18399
  this.#pendingSamples--;
17971
- if (samples.every((value) => value === null)) this.#fragments.delete(key);
18400
+ if (samples.every((value) => value === null)) {
18401
+ this.#fragments.delete(key);
18402
+ const first = this.#firstSamples.get(key);
18403
+ this.#firstSamples.delete(key);
18404
+ this.#delivered.delete(key);
18405
+ this.#delivered.set(key, { first, sampleCount: samples.length });
18406
+ if (this.#delivered.size > this.maxPendingSamples) {
18407
+ this.#delivered.delete(this.#delivered.keys().next().value);
18408
+ }
18409
+ }
17972
18410
  return true;
17973
18411
  }
17974
18412
  reset() {
17975
18413
  this.#track = null;
17976
18414
  this.#fragments.clear();
18415
+ this.#firstSamples.clear();
18416
+ this.#delivered.clear();
17977
18417
  this.#pendingSamples = 0;
17978
18418
  }
17979
18419
  };
@@ -18143,6 +18583,9 @@ function authoredGap(before, after) {
18143
18583
  function ssIdAfter(a, b) {
18144
18584
  return (a - b | 0) > 0;
18145
18585
  }
18586
+ function holdsRebuilt(stream) {
18587
+ return stream.buf.some(({ meta }) => meta.fecSbnFirst !== void 0);
18588
+ }
18146
18589
  var FecReleaseSequencer = class {
18147
18590
  #streams = /* @__PURE__ */ new Map();
18148
18591
  #gate;
@@ -18205,6 +18648,7 @@ var FecReleaseSequencer = class {
18205
18648
  stream.lastSbn = void 0;
18206
18649
  stream.discontinuity = false;
18207
18650
  stream.rapSsId = void 0;
18651
+ stream.sourced = false;
18208
18652
  }
18209
18653
  if (stream.last !== null && compareFrameOrder(frame, stream.last) <= 0) {
18210
18654
  this.#recordStat("droppedStale");
@@ -18231,6 +18675,7 @@ var FecReleaseSequencer = class {
18231
18675
  if (compareFrameOrder(stream.buf[mid], frame) < 0) lo = mid + 1;
18232
18676
  else hi = mid;
18233
18677
  }
18678
+ if (stream.buf[lo]?.passed === true) frame.passed = true;
18234
18679
  stream.buf.splice(lo, 0, frame);
18235
18680
  this.#drainUnlessDeferred(stream);
18236
18681
  }
@@ -18266,6 +18711,41 @@ var FecReleaseSequencer = class {
18266
18711
  }
18267
18712
  return false;
18268
18713
  }
18714
+ /**
18715
+ * These streams' sink is being replaced by one that has seen no source
18716
+ * frame yet. First, from a stream that holds a frame the overlay rebuilt,
18717
+ * release to the sink being replaced the frames the unanchored path had
18718
+ * already released to it (#cursorPassed): there they waited in its queue,
18719
+ * which its replacement flushes. A stream that holds none is not drained:
18720
+ * it holds nothing the anchor kept back, and a drain would still force out
18721
+ * a frame held past the hold limit, which before the anchor waited for the
18722
+ * next push. Then treat the streams as unsourced until a source frame is
18723
+ * released.
18724
+ */
18725
+ unsource(packetId, catalogTrackName) {
18726
+ for (const stream of this.#streams.values()) {
18727
+ if (stream.packetId === packetId && (catalogTrackName === void 0 || stream.catalogTrackName === catalogTrackName)) {
18728
+ if (stream.draining === 0 && holdsRebuilt(stream)) {
18729
+ if (this.#deferDepth > 0) this.#deferredStreams.add(stream);
18730
+ else this.#drain(stream, true);
18731
+ }
18732
+ stream.sourced = false;
18733
+ }
18734
+ }
18735
+ }
18736
+ /**
18737
+ * Whether a stream of `packetId` holds a frame the keyframe overlay rebuilt
18738
+ * (FrameMeta.fecSbnFirst). Without `catalogTrackName`, every stream of the
18739
+ * packetId.
18740
+ */
18741
+ holdsRebuilt(packetId, catalogTrackName) {
18742
+ for (const stream of this.#streams.values()) {
18743
+ if (stream.packetId !== packetId) continue;
18744
+ if (catalogTrackName !== void 0 && stream.catalogTrackName !== catalogTrackName) continue;
18745
+ if (holdsRebuilt(stream)) return true;
18746
+ }
18747
+ return false;
18748
+ }
18269
18749
  /** Re-evaluate held gaps after FEC state changed. */
18270
18750
  poke(streamKey) {
18271
18751
  if (streamKey !== void 0) {
@@ -18357,16 +18837,32 @@ var FecReleaseSequencer = class {
18357
18837
  last: null,
18358
18838
  lastSbn: void 0,
18359
18839
  discontinuity: false,
18360
- rapSsId: void 0
18840
+ rapSsId: void 0,
18841
+ sourced: false,
18842
+ draining: 0
18361
18843
  };
18362
18844
  this.#streams.set(streamKey, stream);
18363
18845
  }
18364
18846
  return stream;
18365
18847
  }
18366
- #drain(stream) {
18848
+ /** `asUnanchored`: release overlay-rebuilt frames when unanchored did (#cursorPassed). */
18849
+ #drain(stream, asUnanchored = false) {
18850
+ stream.draining++;
18851
+ try {
18852
+ this.#drainLoop(stream, asUnanchored);
18853
+ } finally {
18854
+ stream.draining--;
18855
+ }
18856
+ }
18857
+ #drainLoop(stream, asUnanchored) {
18367
18858
  while (stream.buf.length > 0 && stream.last !== null) {
18368
18859
  const head = stream.buf[0];
18369
18860
  const last = stream.last;
18861
+ if (compareFrameOrder(head, last) <= 0) {
18862
+ stream.buf.shift();
18863
+ this.#release(stream, head, void 0);
18864
+ continue;
18865
+ }
18370
18866
  if (stream.discontinuity) {
18371
18867
  stream.discontinuity = false;
18372
18868
  stream.buf.shift();
@@ -18380,9 +18876,10 @@ var FecReleaseSequencer = class {
18380
18876
  }
18381
18877
  const headSbn = head.meta.fecSbn;
18382
18878
  const from = stream.lastSbn ?? headSbn;
18383
- const to = headSbn ?? stream.lastSbn;
18879
+ const to = head.meta.fecSbnFirst ?? headSbn ?? stream.lastSbn;
18384
18880
  let lost = [];
18385
18881
  const unanchored = from === void 0 && to === void 0;
18882
+ let unresolved = false;
18386
18883
  if (from !== void 0 && to !== void 0) {
18387
18884
  const resolved = this.#gate.resolvedLoss(
18388
18885
  stream.packetId,
@@ -18391,7 +18888,8 @@ var FecReleaseSequencer = class {
18391
18888
  Math.max(from, to),
18392
18889
  from
18393
18890
  );
18394
- if (resolved === null) {
18891
+ unresolved = resolved === null;
18892
+ if (unresolved && !this.#cursorPassed(stream, asUnanchored)) {
18395
18893
  const heldMs = performance.now() - head.arrivedAtMs;
18396
18894
  if (heldMs <= this.#gate.holdLimitMs(stream.packetId, stream.catalogTrackName)) return;
18397
18895
  this.#recordStat("forcedWindowFlushes");
@@ -18399,22 +18897,56 @@ var FecReleaseSequencer = class {
18399
18897
  this.#release(stream, head, this.#gate.decide(last, head, [], true));
18400
18898
  continue;
18401
18899
  }
18402
- lost = resolved;
18900
+ lost = resolved ?? [];
18403
18901
  }
18404
18902
  stream.buf.shift();
18405
18903
  this.#release(
18406
18904
  stream,
18407
18905
  head,
18408
- lost.length > 0 || unanchored || authoredGap(last.meta, head.meta) ? this.#gate.decide(last, head, lost, false) : void 0
18906
+ lost.length > 0 || unanchored || unresolved || authoredGap(last.meta, head.meta) ? this.#gate.decide(last, head, lost, false) : void 0
18409
18907
  );
18410
18908
  }
18411
18909
  }
18910
+ /**
18911
+ * An overlay-rebuilt frame (fecSbnFirst) waits no longer than the BLO-53342
18912
+ * source cursor holds it unanchored, and neither does a later frame behind
18913
+ * it. Unanchored, a frame that waits on no block (fecSbnFirst set, or no
18914
+ * fecSbn) left the gate once every frame ahead of it had: mark it `passed`,
18915
+ * like each frame that arrived in front of it since (stale there). The head
18916
+ * then goes, decided as it would be unanchored, once a frame the base path
18917
+ * delivered (a source frame, or one base FEC recovered) is buffered after a
18918
+ * marked one: the base path has passed that one, so a frame still missing
18919
+ * before it can only come from a repair. It also goes while no source frame
18920
+ * has been released, when the direct path shows a recovered frame at once.
18921
+ * And it goes at once while a staged switch decides on when frames arrive
18922
+ * (FecReleaseGate.releaseUnanchored), and when unsource releases into a
18923
+ * sink being replaced: unanchored, it had already reached the sink.
18924
+ * The scan also writes: the `passed` marks it sets stay on the frames,
18925
+ * and push marks a frame it inserts in front of a marked one from them.
18926
+ */
18927
+ #cursorPassed(stream, asUnanchored) {
18928
+ let at2 = -1;
18929
+ for (let i = 0; i < stream.buf.length; i++) {
18930
+ const frame = stream.buf[i];
18931
+ if (frame.meta.fecSbnFirst !== void 0 || frame.meta.fecSbn === void 0) {
18932
+ frame.passed = true;
18933
+ if (at2 < 0) at2 = i;
18934
+ } else if (frame.passed !== true) break;
18935
+ }
18936
+ if (at2 < 0) return false;
18937
+ return asUnanchored || !stream.sourced || this.#gate.releaseUnanchored?.(stream.packetId, stream.catalogTrackName) === true || stream.buf.some(({ meta }, i) => i > at2 && meta.fecSbnFirst === void 0 && (meta.recovered !== true || meta.fecSbn !== void 0));
18938
+ }
18412
18939
  #release(stream, frame, decision) {
18940
+ const stale = stream.last !== null && compareFrameOrder(frame, stream.last) <= 0;
18941
+ if (stale) this.#recordStat("droppedStale");
18413
18942
  if (decision) frame.meta.gapBefore = decision;
18414
18943
  stream.last = frame;
18415
- if (frame.meta.fecSbn !== void 0) stream.lastSbn = frame.meta.fecSbn;
18944
+ if (frame.meta.fecSbn !== void 0 && (frame.meta.fecSbnFirst === void 0 || stream.lastSbn !== void 0)) {
18945
+ stream.lastSbn = frame.meta.fecSbn;
18946
+ }
18947
+ if (frame.meta.recovered !== true) stream.sourced = true;
18416
18948
  this.#gate.onReleased?.(frame.data, frame.meta);
18417
- this.#emit(frame.data, frame.meta);
18949
+ if (!stale) this.#emit(frame.data, frame.meta);
18418
18950
  }
18419
18951
  };
18420
18952
  var FMPU_BOX_TYPES = /* @__PURE__ */ new Set(["ftyp", "moov", "moof"]);
@@ -18455,6 +18987,12 @@ var MmtpContainerParser = class _MmtpContainerParser {
18455
18987
  this.#reclaimedTimingSamples = 0;
18456
18988
  this.#evictedTimingSamples = 0;
18457
18989
  this.#rejectedMovieFragments = 0;
18990
+ this.#recoveredHeadsAwaitingTiming = {
18991
+ held: 0,
18992
+ expired: 0,
18993
+ discarded: 0,
18994
+ superseded: 0
18995
+ };
18458
18996
  this.#sequencers = /* @__PURE__ */ new Map();
18459
18997
  this.#fecGate = null;
18460
18998
  this.#fecSequencer = null;
@@ -18563,6 +19101,7 @@ var MmtpContainerParser = class _MmtpContainerParser {
18563
19101
  #reclaimedTimingSamples;
18564
19102
  #evictedTimingSamples;
18565
19103
  #rejectedMovieFragments;
19104
+ #recoveredHeadsAwaitingTiming;
18566
19105
  #sequencers;
18567
19106
  #fecGate;
18568
19107
  #fecSequencer;
@@ -18668,11 +19207,12 @@ var MmtpContainerParser = class _MmtpContainerParser {
18668
19207
  /**
18669
19208
  * Typed view of the ISO-BMFF timing-sample budget.
18670
19209
  *
18671
- * This is the acceptance signal for the budget ratchet: `pending` walking
18672
- * to `capacity` while `rejectedMovieFragments` climbs is the stream wedging,
18673
- * and it is observable here rather than only as media silently stopping.
18674
- * Per repo policy this is a producer-owned counter -- never infer budget
18675
- * state by parsing logs.
19210
+ * Each stream admits timing against its own window of `capacity` slots
19211
+ * (BLO-52085). A stream at capacity displaces its own oldest timing to
19212
+ * admit the moof in hand, and `evicted` counts the slots so displaced:
19213
+ * timing a stream declared and never delivered shows up there. `pending`
19214
+ * sums every stream, so it can exceed `capacity`. Per repo policy this is
19215
+ * a producer-owned counter -- never infer budget state by parsing logs.
18676
19216
  */
18677
19217
  get timingBudgetStats() {
18678
19218
  return {
@@ -18683,6 +19223,14 @@ var MmtpContainerParser = class _MmtpContainerParser {
18683
19223
  rejectedMovieFragments: this.#rejectedMovieFragments
18684
19224
  };
18685
19225
  }
19226
+ /** Recovered heads parked for FT=1 timing (BLO-53369). Survives dispose(). */
19227
+ get recoveredHeadsAwaitingTiming() {
19228
+ let pending = 0;
19229
+ for (const { awaitingTiming } of this.#timingStreams.values()) {
19230
+ for (const head of awaitingTiming) if (head.options?.recovered) pending++;
19231
+ }
19232
+ return { ...this.#recoveredHeadsAwaitingTiming, pending };
19233
+ }
18686
19234
  get reorderStats() {
18687
19235
  let bufferedFrames = this.#fecSequencer?.buffered ?? 0;
18688
19236
  for (const { sequencer } of this.#sequencers.values()) bufferedFrames += sequencer.buffered;
@@ -18724,6 +19272,14 @@ var MmtpContainerParser = class _MmtpContainerParser {
18724
19272
  fecReleaseRapAbove(packetId, catalogTrackName, ssId) {
18725
19273
  return this.#fecSequencer?.rapAbove(packetId, catalogTrackName, ssId) ?? false;
18726
19274
  }
19275
+ /** See FecReleaseSequencer.unsource. */
19276
+ unsourceFecRelease(packetId, catalogTrackName) {
19277
+ this.#fecSequencer?.unsource(packetId, catalogTrackName);
19278
+ }
19279
+ /** See FecReleaseSequencer.holdsRebuilt. */
19280
+ fecReleaseHoldsRebuilt(packetId, catalogTrackName) {
19281
+ return this.#fecSequencer?.holdsRebuilt(packetId, catalogTrackName) ?? false;
19282
+ }
18727
19283
  /** Re-evaluate held gaps after FEC block state changed. */
18728
19284
  pokeFecRelease(packetId, catalogTrackName) {
18729
19285
  if (!this.#fecSequencer) return;
@@ -18811,10 +19367,10 @@ var MmtpContainerParser = class _MmtpContainerParser {
18811
19367
  get reassemblerDataUnitsReturned() {
18812
19368
  return this.#reassemblerDataUnitsReturned;
18813
19369
  }
18814
- #countOutcome(token) {
18815
- const count = (this.#feedOutcomes.get(token) ?? 0) + 1;
18816
- this.#feedOutcomes.set(token, count);
18817
- this.#lastOutcome = token;
19370
+ #countOutcome(token2) {
19371
+ const count = (this.#feedOutcomes.get(token2) ?? 0) + 1;
19372
+ this.#feedOutcomes.set(token2, count);
19373
+ this.#lastOutcome = token2;
18818
19374
  this.#lastOutcomeCount = count;
18819
19375
  }
18820
19376
  #countDeferredOutcome(reason) {
@@ -18856,7 +19412,7 @@ var MmtpContainerParser = class _MmtpContainerParser {
18856
19412
  const now = Date.now();
18857
19413
  for (const { awaitingTiming } of this.#timingStreams.values()) {
18858
19414
  while (awaitingTiming.length > 0 && now - awaitingTiming[0].arrivedAt > timeoutMs) {
18859
- awaitingTiming.shift();
19415
+ this.#leaveAwaitingTiming(awaitingTiming.shift(), "expired");
18860
19416
  }
18861
19417
  }
18862
19418
  }
@@ -18974,11 +19530,16 @@ var MmtpContainerParser = class _MmtpContainerParser {
18974
19530
  if (mpu.fragmentationIndicator !== 0 || mpu.fragmentCounter !== 0)
18975
19531
  return this.#drop("fragment-unexpected-fragmentation");
18976
19532
  const resolver2 = this.#timingResolver(streamKey, info.packetId, false);
18977
- if (!resolver2) return this.#drop("fragment-no-resolver");
19533
+ if (!resolver2?.initialized) return this.#drop("fragment-no-resolver");
18978
19534
  const pendingBefore = resolver2.pendingSampleCount;
18979
19535
  const evictedBefore = resolver2.evictedSampleCount;
18980
- const remaining = this.#maxPendingTimingSamples - this.#pendingTimingSamples;
18981
- const admitted = resolver2.addMovieFragment(mpu.mpuSequenceNumber, data, remaining);
19536
+ const remaining = this.#maxPendingTimingSamples - resolver2.pendingSampleCount;
19537
+ const admitted = resolver2.addMovieFragment(
19538
+ mpu.mpuSequenceNumber,
19539
+ data,
19540
+ remaining,
19541
+ options?.recovered === true
19542
+ );
18982
19543
  this.#pendingTimingSamples += resolver2.pendingSampleCount - pendingBefore;
18983
19544
  this.#evictedTimingSamples += resolver2.evictedSampleCount - evictedBefore;
18984
19545
  if (!admitted) {
@@ -18998,7 +19559,7 @@ var MmtpContainerParser = class _MmtpContainerParser {
18998
19559
  };
18999
19560
  }
19000
19561
  if (fragmentType !== 2) return this.#drop("unknown-fragment-type");
19001
- const resolver = this.#timingResolver(streamKey, info.packetId, false);
19562
+ const resolver = this.#timingResolver(streamKey, info.packetId, true);
19002
19563
  if (!resolver) return this.#drop("mfu-no-resolver");
19003
19564
  if (!Number.isSafeInteger(data.byteLength) || data.byteLength <= 0)
19004
19565
  return this.#drop("mfu-empty-payload");
@@ -19019,6 +19580,7 @@ var MmtpContainerParser = class _MmtpContainerParser {
19019
19580
  mpuSequenceNumber: mpu.mpuSequenceNumber,
19020
19581
  movieFragmentSequenceNumber: mfuDuHeader.movieFragmentSequenceNumber,
19021
19582
  sampleNumber: mfuDuHeader.sampleNumber,
19583
+ sequenceNumber: info.sequenceNumber,
19022
19584
  arrivedAt: Date.now()
19023
19585
  });
19024
19586
  return this.#drop("mfu-sample-timing-missing");
@@ -19044,7 +19606,8 @@ var MmtpContainerParser = class _MmtpContainerParser {
19044
19606
  info,
19045
19607
  resolver,
19046
19608
  options?.recovered === true,
19047
- options?.fecSbn
19609
+ options?.fecSbn,
19610
+ options?.fecSbnFirst
19048
19611
  );
19049
19612
  if (deferred.record === void 0) this.#countDeferredOutcome(deferred.reason);
19050
19613
  }
@@ -19137,7 +19700,8 @@ var MmtpContainerParser = class _MmtpContainerParser {
19137
19700
  info,
19138
19701
  resolver,
19139
19702
  options?.recovered === true,
19140
- options?.fecSbn
19703
+ options?.fecSbn,
19704
+ options?.fecSbnFirst
19141
19705
  );
19142
19706
  if (outcome.record !== void 0) return outcome.record;
19143
19707
  return this.#drop(outcome.reason);
@@ -19157,7 +19721,7 @@ var MmtpContainerParser = class _MmtpContainerParser {
19157
19721
  * attribution exists to keep readable. Leaving the choice at the call site
19158
19722
  * is what keeps both true.
19159
19723
  */
19160
- #emitCombinedSample(combined, info, resolver, recovered, fecSbn) {
19724
+ #emitCombinedSample(combined, info, resolver, recovered, fecSbn, fecSbnFirst) {
19161
19725
  const timing = resolver.sampleTiming(
19162
19726
  combined.mpuSequenceNumber,
19163
19727
  combined.movieFragmentSequenceNumber,
@@ -19174,6 +19738,7 @@ var MmtpContainerParser = class _MmtpContainerParser {
19174
19738
  );
19175
19739
  if (!meta) return { reason: "combined-meta-build-failed" };
19176
19740
  if (fecSbn !== void 0) meta.fecSbn = fecSbn;
19741
+ if (fecSbnFirst !== void 0) meta.fecSbnFirst = fecSbnFirst;
19177
19742
  if (!resolver.consumeSample(
19178
19743
  combined.mpuSequenceNumber,
19179
19744
  combined.movieFragmentSequenceNumber,
@@ -19342,6 +19907,10 @@ var MmtpContainerParser = class _MmtpContainerParser {
19342
19907
  }
19343
19908
  this.#emitFrame(data, meta);
19344
19909
  }
19910
+ /** Whether FT=1 metadata already names this sample, without claiming it. */
19911
+ hasAuthoredTimingFor(packetId, mpuSequenceNumber2, movieFragmentSequenceNumber, sampleNumber, catalogTrackName) {
19912
+ return this.#timingResolver(this.#streamKey(packetId, catalogTrackName), packetId, false)?.sampleTiming(mpuSequenceNumber2, movieFragmentSequenceNumber, sampleNumber) != null;
19913
+ }
19345
19914
  /**
19346
19915
  * Claim and return the authored timing for a recovered sample. Recovery does
19347
19916
  * not pass through the ordinary `feed()` completion path, so it must retire
@@ -19403,6 +19972,9 @@ var MmtpContainerParser = class _MmtpContainerParser {
19403
19972
  for (const { sequencer } of this.#sequencers.values()) sequencer.discard();
19404
19973
  this.#sequencers.clear();
19405
19974
  this.#fecSequencer?.discard();
19975
+ for (const { awaitingTiming } of this.#timingStreams.values()) {
19976
+ for (const head of awaitingTiming) this.#leaveAwaitingTiming(head, "discarded");
19977
+ }
19406
19978
  this.#timingStreams.clear();
19407
19979
  this.#pendingTimingSamples = 0;
19408
19980
  this.#reliableMoqStartupGate.clear();
@@ -19421,10 +19993,23 @@ var MmtpContainerParser = class _MmtpContainerParser {
19421
19993
  return resolver;
19422
19994
  }
19423
19995
  #awaitTiming(streamKey, head) {
19424
- const awaitingTiming = this.#timingStreams.get(streamKey)?.awaitingTiming;
19425
- if (!awaitingTiming) return;
19996
+ const state = this.#timingStreams.get(streamKey);
19997
+ if (!state) return;
19998
+ if (!head.options?.recovered) {
19999
+ const kept = state.awaitingTiming.filter((queued) => !queued.options?.recovered || queued.sequenceNumber !== head.sequenceNumber);
20000
+ this.#recoveredHeadsAwaitingTiming.superseded += state.awaitingTiming.length - kept.length;
20001
+ state.awaitingTiming = kept;
20002
+ }
20003
+ const awaitingTiming = state.awaitingTiming;
19426
20004
  awaitingTiming.push(head);
19427
- if (awaitingTiming.length > MAX_AWAITING_TIMING_HEADS) awaitingTiming.shift();
20005
+ if (head.options?.recovered) this.#recoveredHeadsAwaitingTiming.held++;
20006
+ if (awaitingTiming.length > MAX_AWAITING_TIMING_HEADS) {
20007
+ this.#leaveAwaitingTiming(awaitingTiming.shift(), "expired");
20008
+ }
20009
+ }
20010
+ /** Count a recovered head leaving the queue other than by replay or its source copy. */
20011
+ #leaveAwaitingTiming(head, outcome) {
20012
+ if (head.options?.recovered) this.#recoveredHeadsAwaitingTiming[outcome]++;
19428
20013
  }
19429
20014
  /**
19430
20015
  * Feed again the heads whose timing the FT=1 just admitted declares. That
@@ -19452,6 +20037,7 @@ var MmtpContainerParser = class _MmtpContainerParser {
19452
20037
  const state = this.#timingStreams.get(streamKey);
19453
20038
  if (!state) return;
19454
20039
  this.#timingStreams.delete(streamKey);
20040
+ for (const head of state.awaitingTiming) this.#leaveAwaitingTiming(head, "discarded");
19455
20041
  this.#pendingTimingSamples -= state.resolver.pendingSampleCount;
19456
20042
  if (this.#pendingTimingSamples < 0) {
19457
20043
  throw new Error("[MmtpContainerParser] pending timing sample accounting underflow");
@@ -22658,7 +23244,18 @@ function keyframeOverlaySsId(header) {
22658
23244
  if (ext?.type !== KEYFRAME_OVERLAY_EXTENSION_TYPE || ext.data.length !== 4) return void 0;
22659
23245
  return new DataView(ext.data.buffer, ext.data.byteOffset, 4).getUint32(0);
22660
23246
  }
23247
+ function overlayBaseSsId(anchor, overlaySsId, restoredPsn, headerPsn) {
23248
+ if (anchor.baseSsId === void 0 || headerPsn !== restoredPsn) return void 0;
23249
+ return serial32Add(anchor.baseSsId, overlaySsId - anchor.overlaySsId | 0);
23250
+ }
23251
+ function unstampedBaseSsId(offset, unstampedOffset, headerPsn) {
23252
+ if (offset === void 0 || offset !== unstampedOffset) return void 0;
23253
+ return headerPsn + offset >>> 0;
23254
+ }
23255
+ var OVERLAY_ROUTED_LIMIT = 1024;
23256
+ var OVERLAY_PENDING_LIMIT = 256;
22661
23257
  var OVERLAY_OFFSET_MPUS = 16;
23258
+ var OVERLAY_REBUILT_LIMIT = 256;
22662
23259
  var OVERLAY_MOQ_HORIZON_BLOCKS = 64;
22663
23260
  function assertStreamIdentity(role, stream) {
22664
23261
  if (stream === void 0 || !Number.isInteger(stream.packetId) || stream.packetId <= MMTP_SIGNALING_PACKET_ID || typeof stream.catalogTrackName !== "string" || stream.catalogTrackName.length === 0) {
@@ -22769,6 +23366,7 @@ var MmtpRouter = class {
22769
23366
  #provenance = /* @__PURE__ */ new Map();
22770
23367
  #onFlowProtected = null;
22771
23368
  #unadmittedListedDrops = 0;
23369
+ #admittedSourceDatagrams = 0;
22772
23370
  #unlistedRepairSetAside = 0;
22773
23371
  #assistBlocks = /* @__PURE__ */ new Map();
22774
23372
  #assistFailures = /* @__PURE__ */ new Map();
@@ -22825,6 +23423,26 @@ var MmtpRouter = class {
22825
23423
  * sequence is its SS_ID minus this; see #routeProcessedFecSymbols.
22826
23424
  */
22827
23425
  #ssIdSeqOffsetByStream = /* @__PURE__ */ new Map();
23426
+ /**
23427
+ * Per stream, the SS_ID offset at which a base recovery last showed the
23428
+ * sender's own packet_sequence_number: a recovered packet's header psn
23429
+ * equal to its SS_ID minus that offset. While it equals the stream's
23430
+ * current offset, no hop re-stamps the stream, and a packet's protected psn
23431
+ * names its base SS_ID within unstampedBaseSsId's limits (joint decoding of
23432
+ * an MPU no member of which arrived).
23433
+ */
23434
+ #unstampedSsIdSeqOffsetByStream = /* @__PURE__ */ new Map();
23435
+ /**
23436
+ * The same, shown by the overlay alone, before any base recovery did: a
23437
+ * packet it rebuilt whose base copy carries its protected psn
23438
+ * (#feedOverlaySource), or whose protected psn passes overlayBaseSsId's
23439
+ * test at that offset (#routeOverlayRecovered). A base SS_ID placed by it
23440
+ * anchors the release gate only (BLO-52085 D3): fed to the base, it would
23441
+ * make the base drop a late base copy of a keyframe the overlay never
23442
+ * finished, and lift a block's late-source cut, which nothing before
23443
+ * this offset did.
23444
+ */
23445
+ #overlayUnstampedSsIdSeqOffsetByStream = /* @__PURE__ */ new Map();
22828
23446
  /**
22829
23447
  * Streams whose multicast leg delivered a RAP-flagged source packet that was
22830
23448
  * not parsed, typically because its overlap copy was parsed first
@@ -22894,7 +23512,9 @@ var MmtpRouter = class {
22894
23512
  recovered: 0,
22895
23513
  restarts: 0,
22896
23514
  unsequencedDropped: 0,
22897
- protectedDropped: 0
23515
+ protectedDropped: 0,
23516
+ baseFed: 0,
23517
+ baseUndetermined: 0
22898
23518
  };
22899
23519
  /** Callback fired when a delta-track FEC block completes or fails. */
22900
23520
  #onDeltaBlockCompleteCb = null;
@@ -23067,7 +23687,7 @@ var MmtpRouter = class {
23067
23687
  const cb = this.#getFrameCallback(packetId);
23068
23688
  if (isVideo && !assembler) {
23069
23689
  if (frameMeta.recovered) {
23070
- this.#routeDirectRecoveredVideo(packetId, data, frameMeta, cb);
23690
+ this.#routeDirectRecoveredVideo(packetId, data, frameMeta, cb, meta.fecSbn);
23071
23691
  } else {
23072
23692
  this.#routeDirectSourceVideo(packetId, data, frameMeta, cb);
23073
23693
  }
@@ -23100,13 +23720,18 @@ var MmtpRouter = class {
23100
23720
  this.#onDeltaBlockCompleteCb?.(packetId, false);
23101
23721
  }
23102
23722
  };
23103
- this.#fecManager.onBlockFailed = (sbn, packetId, catalogTrackName, blockClass) => {
23723
+ this.#fecManager.onBlockFailed = (sbn, packetId, catalogTrackName, blockClass, span) => {
23104
23724
  this.#mseAssemblerFor(packetId, catalogTrackName)?.failBlock(sbn);
23105
23725
  if (this.#isCatalogTrackUnambiguous(packetId, catalogTrackName) && this.#packetIdRoles.get(packetId) === "delta") {
23106
23726
  this.#onDeltaBlockCompleteCb?.(packetId, true);
23107
23727
  }
23108
- onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass);
23109
- this.#onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass);
23728
+ if (span === void 0) {
23729
+ onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass);
23730
+ this.#onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass);
23731
+ } else {
23732
+ onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass, span);
23733
+ this.#onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass, span);
23734
+ }
23110
23735
  };
23111
23736
  }
23112
23737
  }
@@ -23151,8 +23776,9 @@ var MmtpRouter = class {
23151
23776
  }
23152
23777
  /**
23153
23778
  * FEC-recovered source symbols whose reconstructed header named a
23154
- * packet_sequence_number other than the wire sequence their SS_ID maps to:
23155
- * a hop re-stamped the sequence after the sender protected the packet.
23779
+ * packet_sequence_number other than the wire sequence their SS_ID maps to,
23780
+ * or, rebuilt by the overlay, than their base copy carries: a hop
23781
+ * re-stamped the sequence after the sender protected the packet.
23156
23782
  */
23157
23783
  get restampedRecoveredSequenceCount() {
23158
23784
  return this.#restampedRecoveredSeqs;
@@ -23331,6 +23957,7 @@ var MmtpRouter = class {
23331
23957
  enableMseAssembler(packetId, config, catalogTrackName) {
23332
23958
  const assemblerKey = this.#mseAssemblerKey(packetId, catalogTrackName);
23333
23959
  const previous = this.#mseAssemblers.get(assemblerKey);
23960
+ this.#parser.unsourceFecRelease(packetId, catalogTrackName);
23334
23961
  if (previous) {
23335
23962
  previous.flush();
23336
23963
  previous.dispose();
@@ -23457,6 +24084,10 @@ var MmtpRouter = class {
23457
24084
  get keyframeOverlayStats() {
23458
24085
  return { ...this.#overlayStats };
23459
24086
  }
24087
+ /** Recovered FT=2 heads held for their FT=1, and how each left the wait (BLO-53369). */
24088
+ get recoveredHeadsAwaitingTiming() {
24089
+ return this.#parser.recoveredHeadsAwaitingTiming;
24090
+ }
23460
24091
  #isOverlayRepair(repairPacketId, repairTrackName) {
23461
24092
  return this.#overlayLegs.has(repairPacketId) || repairTrackName !== void 0 && this.#overlayRepairNames.has(repairTrackName);
23462
24093
  }
@@ -23477,7 +24108,8 @@ var MmtpRouter = class {
23477
24108
  fec,
23478
24109
  k,
23479
24110
  offsets: /* @__PURE__ */ new Map(),
23480
- moqBlocks: /* @__PURE__ */ new Set()
24111
+ moqBlocks: /* @__PURE__ */ new Set(),
24112
+ rebuilt: /* @__PURE__ */ new Map()
23481
24113
  };
23482
24114
  this.#overlays.set(repairPacketId, inst);
23483
24115
  return () => {
@@ -23507,12 +24139,18 @@ var MmtpRouter = class {
23507
24139
  */
23508
24140
  resetKeyframeOverlays(sourcePacketId, catalogTrackName) {
23509
24141
  for (const inst of this.#overlaysForSource(sourcePacketId, catalogTrackName)) this.#resetOverlay(inst);
24142
+ for (const [name, stream] of this.#streams.get(sourcePacketId) ?? []) {
24143
+ if (catalogTrackName !== void 0 && name !== catalogTrackName) continue;
24144
+ stream.overlayToBase.routed.clear();
24145
+ stream.overlayToBase.pending.clear();
24146
+ }
23510
24147
  }
23511
24148
  #resetOverlay(inst) {
23512
24149
  inst.fec.resetTrack(inst.repairPacketId, inst.streamName);
23513
24150
  inst.last = void 0;
23514
24151
  inst.offsets.clear();
23515
24152
  inst.moqBlocks.clear();
24153
+ inst.rebuilt.clear();
23516
24154
  }
23517
24155
  #overlaysForSource(sourcePacketId, catalogTrackName) {
23518
24156
  return [...this.#overlays.values()].filter(({ leg }) => leg.sourcePacketId === sourcePacketId && (catalogTrackName === void 0 || leg.sourceCatalogTrackName === void 0 || leg.sourceCatalogTrackName === catalogTrackName));
@@ -23563,18 +24201,33 @@ var MmtpRouter = class {
23563
24201
  * A source packet that carries a 0x8B01 overlay SS_ID is also a symbol of
23564
24202
  * every overlay installed for its source, at that SS_ID, with the bytes the
23565
24203
  * base protects: the packet minus its 4-byte SS_ID trailer, extension
23566
- * included (7.1, 7.4.3).
23567
- */
23568
- #feedOverlaySource(packetId, catalogTrackName, packet, header, sequenceNumber, rapFlag, source) {
24204
+ * included (7.1, 7.4.3). `baseSsId` is that trailer's SS_ID, when the base
24205
+ * FEC reads it.
24206
+ *
24207
+ * The base copy of a packet an overlay rebuilt with no base SS_ID shows
24208
+ * what a base recovery of it would (#routeProcessedFecSymbols, BLO-52085):
24209
+ * its wire packet_sequence_number equal to the protected one rebuilt shows
24210
+ * the stream unstamped at the SS_ID offset it states, for the release gate
24211
+ * (#overlayUnstampedSsIdSeqOffsetByStream); any other shows a hop that
24212
+ * re-stamps it, which is counted (restampedRecoveredSequenceCount).
24213
+ */
24214
+ #feedOverlaySource(packetId, catalogTrackName, packet, header, sequenceNumber, rapFlag, source, baseSsId) {
23569
24215
  if (this.#overlays.size === 0) return;
23570
24216
  const ssId = keyframeOverlaySsId(header);
23571
24217
  if (ssId === void 0) return;
24218
+ let rebuiltPsn;
23572
24219
  for (const inst of this.#overlaysForSource(packetId, catalogTrackName)) {
23573
24220
  this.#noteOverlaySsId(inst, ssId, sequenceNumber);
24221
+ rebuiltPsn ??= inst.rebuilt.get(ssId);
24222
+ inst.rebuilt.delete(ssId);
23574
24223
  const identity = MmtpContainerParser.sourcePacketIdentity(packet, header);
23575
24224
  if (identity !== null) {
23576
24225
  inst.offsets.delete(identity.mpuSequenceNumber);
23577
- inst.offsets.set(identity.mpuSequenceNumber, ssId - sequenceNumber >>> 0);
24226
+ inst.offsets.set(identity.mpuSequenceNumber, {
24227
+ psnOffset: ssId - sequenceNumber >>> 0,
24228
+ overlaySsId: ssId,
24229
+ ...baseSsId === void 0 ? {} : { baseSsId }
24230
+ });
23578
24231
  if (inst.offsets.size > OVERLAY_OFFSET_MPUS) inst.offsets.delete(inst.offsets.keys().next().value);
23579
24232
  }
23580
24233
  const { fec, repairPacketId: pid, streamName: name } = inst;
@@ -23596,6 +24249,14 @@ var MmtpRouter = class {
23596
24249
  const recovered = fec.feedSourceSymbol(pid, ssId, symbolData, name);
23597
24250
  if (recovered && !alreadyCompleted) this.#routeOverlayRecovered(inst, coords.sbn, recovered);
23598
24251
  }
24252
+ if (rebuiltPsn === void 0 || this.#classifyPacketId(packetId) === null) return;
24253
+ const streamKey = this.#mediaStreamKey(packetId, catalogTrackName);
24254
+ if (rebuiltPsn !== sequenceNumber) {
24255
+ this.#restampedRecoveredSeqs++;
24256
+ } else if (baseSsId !== void 0) {
24257
+ const offset = this.#ssIdSeqOffsetByStream.get(streamKey);
24258
+ if (offset === baseSsId - sequenceNumber >>> 0) this.#overlayUnstampedSsIdSeqOffsetByStream.set(streamKey, offset);
24259
+ }
23599
24260
  }
23600
24261
  #noteOverlayMoqBlock(inst, blockStart) {
23601
24262
  inst.moqBlocks.add(blockStart);
@@ -23629,11 +24290,16 @@ var MmtpRouter = class {
23629
24290
  }
23630
24291
  /**
23631
24292
  * Route an overlay-recovered block's packets as source media of the
23632
- * overlay's source (7.2: an overlay coordinate yields no base SS_ID). No
23633
- * base-SS_ID derivation runs: no release-gate anchor, no parse ledger, no
23634
- * base re-stamp, no block advance. Each packet's wire sequence is restored
23635
- * from its keyframe's own offset; without one, a stream the base never
23636
- * re-stamped keeps the header's sequence and any other drops the packet.
24293
+ * overlay's source (7.2: an overlay coordinate yields no base SS_ID). A
24294
+ * packet whose base SS_ID is determined independently (overlayBaseSsId,
24295
+ * unstampedBaseSsId) enters the release gate at that SS_ID, as a packet
24296
+ * base FEC recovered does, so it waits behind base frames still in flight,
24297
+ * though not for its own MPU's packets (FrameMeta.fecSbnFirst) and no
24298
+ * longer than unanchored; one whose base SS_ID is undetermined has no
24299
+ * release-gate anchor. No parse ledger, no base re-stamp, no block
24300
+ * advance. Each packet's wire sequence is restored from its keyframe's own
24301
+ * offset; without one, a stream the base never re-stamped keeps the
24302
+ * header's sequence and any other drops the packet.
23637
24303
  */
23638
24304
  #routeOverlayRecovered(inst, blockStart, recovered) {
23639
24305
  const { fec, repairPacketId: pid, streamName: name } = inst;
@@ -23653,22 +24319,139 @@ var MmtpRouter = class {
23653
24319
  const streamKey = this.#mediaStreamKey(sourcePacketId, sourceName);
23654
24320
  const restamped = this.#restampedStreams.has(streamKey);
23655
24321
  const restored = /* @__PURE__ */ new Set();
24322
+ const baseSsIds = /* @__PURE__ */ new Map();
24323
+ const gateOnly = /* @__PURE__ */ new Set();
23656
24324
  const symbols = result.symbols.flatMap((sym) => {
23657
- const offset = inst.offsets.get(sym.fragment.mpuSequenceNumber);
23658
- if (offset !== void 0) {
23659
- const packetSequenceNumber = serial32Add(blockStart, sym.index) - offset >>> 0;
24325
+ const overlaySsId = serial32Add(blockStart, sym.index);
24326
+ const anchor = inst.offsets.get(sym.fragment.mpuSequenceNumber);
24327
+ if (anchor !== void 0) {
24328
+ const packetSequenceNumber = overlaySsId - anchor.psnOffset >>> 0;
23660
24329
  restored.add(packetSequenceNumber);
23661
- return [{ ...sym, packetSequenceNumber }];
24330
+ const out = { ...sym, packetSequenceNumber };
24331
+ const baseSsId = overlayBaseSsId(anchor, overlaySsId, packetSequenceNumber, sym.packetSequenceNumber);
24332
+ if (baseSsId !== void 0) {
24333
+ baseSsIds.set(sym.index, baseSsId);
24334
+ const offset = this.#ssIdSeqOffsetByStream.get(streamKey);
24335
+ if (!this.#sourceSeqReceived(sourcePacketId, packetSequenceNumber, sourceName) && offset === baseSsId - packetSequenceNumber >>> 0) {
24336
+ this.#overlayUnstampedSsIdSeqOffsetByStream.set(streamKey, offset);
24337
+ }
24338
+ }
24339
+ return [out];
24340
+ }
24341
+ if (!restamped) {
24342
+ const offset = this.#ssIdSeqOffsetByStream.get(streamKey);
24343
+ const fedSsId = unstampedBaseSsId(offset, this.#unstampedSsIdSeqOffsetByStream.get(streamKey), sym.packetSequenceNumber);
24344
+ const baseSsId = fedSsId ?? unstampedBaseSsId(offset, this.#overlayUnstampedSsIdSeqOffsetByStream.get(streamKey), sym.packetSequenceNumber);
24345
+ if (baseSsId !== void 0) {
24346
+ baseSsIds.set(sym.index, baseSsId);
24347
+ if (fedSsId === void 0) gateOnly.add(sym.index);
24348
+ } else {
24349
+ inst.rebuilt.set(overlaySsId, sym.packetSequenceNumber);
24350
+ if (inst.rebuilt.size > OVERLAY_REBUILT_LIMIT) inst.rebuilt.delete(inst.rebuilt.keys().next().value);
24351
+ }
24352
+ return [sym];
23662
24353
  }
23663
- if (!restamped) return [sym];
23664
24354
  this.#overlayStats.unsequencedDropped++;
23665
24355
  return [];
23666
24356
  });
23667
24357
  this.#overlayStats.recovered++;
24358
+ const mpuFirst = /* @__PURE__ */ new Map();
24359
+ for (const sym of symbols) {
24360
+ const ssId = baseSsIds.get(sym.index);
24361
+ const mpu = sym.fragment.mpuSequenceNumber;
24362
+ const first = mpuFirst.get(mpu);
24363
+ if (ssId !== void 0 && (first === void 0 || serial32Precedes(ssId, first))) mpuFirst.set(mpu, ssId);
24364
+ }
24365
+ const firstSsIds = /* @__PURE__ */ new Map();
24366
+ for (const sym of symbols) {
24367
+ if (baseSsIds.has(sym.index)) firstSsIds.set(sym.index, mpuFirst.get(sym.fragment.mpuSequenceNumber));
24368
+ }
23668
24369
  this.#routeProcessedFecSymbols(blockStart, { ...result, symbols }, sourcePacketId, sourceName, {
23669
- deliveryProvenance: inst.moqBlocks.has(blockStart) ? "mixed" : "multicast"
24370
+ deliveryProvenance: inst.moqBlocks.has(blockStart) ? "mixed" : "multicast",
24371
+ baseSsIds,
24372
+ firstSsIds
23670
24373
  });
23671
24374
  this.#noteOverlayRecoveredRaps(sourcePacketId, sourceName, streamKey, symbols.filter((sym) => restored.has(sym.packetSequenceNumber)));
24375
+ this.#feedOverlayRecoveredToBase(inst, blockStart, symbols, gateOnly.size === 0 ? baseSsIds : new Map([...baseSsIds].filter(([index]) => !gateOnly.has(index))));
24376
+ }
24377
+ /**
24378
+ * Joint decoding (BLO-52085). A packet the overlay rebuilt is a source of
24379
+ * its base block too, once its base SS_ID is determined (overlayBaseSsId,
24380
+ * from a received member of its MPU, else unstampedBaseSsId at an offset a
24381
+ * base recovery showed; `baseSsIds` holds no other),
24382
+ * so it is fed to the base decoder as a rebuilt source
24383
+ * (FECManager.feedRebuiltSourceSymbol): the base block has one erasure
24384
+ * fewer to solve, and its repair can recover the block's other packets.
24385
+ * A base block no repair has stated yet takes it when one does
24386
+ * (#feedPendingRebuilt).
24387
+ *
24388
+ * The overlay route already handled each packet fed (delivered it, or
24389
+ * held an untimed head for its FT=1), so a base recovery routes none of
24390
+ * them again (#routeVerifiedFecSymbols): a second recovered copy of a
24391
+ * held head would wait in the parser for timing its first copy consumed.
24392
+ * Only an overlay block decoded from multicast symbols alone is fed: with
24393
+ * a MoQ symbol in it, the base block's recovery would depend on bytes
24394
+ * that crossed no multicast socket, which the provenance counted from the
24395
+ * block's own arrivals cannot show.
24396
+ */
24397
+ #feedOverlayRecoveredToBase(inst, blockStart, symbols, baseSsIds) {
24398
+ const fec = this.#fecManager;
24399
+ const owner = inst.leg.sourcePacketId;
24400
+ const name = inst.leg.sourceCatalogTrackName;
24401
+ if (!fec?.feedRebuiltSourceSymbol || inst.moqBlocks.has(blockStart)) return;
24402
+ if (this.#classifyPacketId(owner) === null || fec.isTrackConfigured?.(owner) === false) return;
24403
+ const { duplicates, overlayToBase: { routed } } = this.#stream(owner, name);
24404
+ const feeds = [];
24405
+ for (const sym of symbols) {
24406
+ if (duplicates.sourceSeqs.has(sym.packetSequenceNumber)) continue;
24407
+ const ssId = baseSsIds.get(sym.index);
24408
+ if (ssId === void 0) {
24409
+ this.#overlayStats.baseUndetermined++;
24410
+ continue;
24411
+ }
24412
+ routed.delete(ssId);
24413
+ routed.add(ssId);
24414
+ if (routed.size > OVERLAY_ROUTED_LIMIT) routed.delete(routed.values().next().value);
24415
+ feeds.push([ssId, sym.rawSymbol]);
24416
+ }
24417
+ for (const [ssId, symbolData] of feeds) {
24418
+ if (this.#classifyPacketId(owner) === null) return;
24419
+ this.#feedRebuiltToBase(owner, name, ssId, symbolData);
24420
+ }
24421
+ }
24422
+ /** Feed one rebuilt source to its base block, or keep it until a repair states that block. */
24423
+ #feedRebuiltToBase(owner, name, ssId, symbolData) {
24424
+ const fec = this.#fecManager;
24425
+ if (!fec?.feedRebuiltSourceSymbol) return;
24426
+ const coords = fec.locateIsoSource?.(owner, name, ssId);
24427
+ if (coords == null) {
24428
+ const pending = this.#stream(owner, name).overlayToBase.pending;
24429
+ pending.delete(ssId);
24430
+ pending.set(ssId, symbolData.slice());
24431
+ if (pending.size > OVERLAY_PENDING_LIMIT) pending.delete(pending.keys().next().value);
24432
+ return;
24433
+ }
24434
+ const fed = fec.feedRebuiltSourceSymbol(owner, ssId, symbolData, name);
24435
+ if (!fed.taken) return;
24436
+ this.#overlayStats.baseFed++;
24437
+ if (fed.recovered === null) return;
24438
+ const sbn = coords.sbn;
24439
+ this.#routeRecoveredBlock(sbn, fed.recovered, owner, name, () => {
24440
+ fec.cancelBlockTimeout(sbn, owner, name);
24441
+ fec.countRecovery(owner, sbn, name);
24442
+ fec.markBlockCompleted(sbn, owner, name);
24443
+ });
24444
+ }
24445
+ /** A repair stated `block`: the rebuilt sources that waited for it join it now. */
24446
+ #feedPendingRebuilt(owner, name, block) {
24447
+ const pending = this.#streams.get(owner)?.get(name)?.overlayToBase.pending;
24448
+ if (pending === void 0 || pending.size === 0) return;
24449
+ for (const [ssId, symbolData] of [...pending]) {
24450
+ if (this.#classifyPacketId(owner) === null) return;
24451
+ if (serial32Distance(block.start, ssId) >= block.length) continue;
24452
+ pending.delete(ssId);
24453
+ this.#feedRebuiltToBase(owner, name, ssId, symbolData);
24454
+ }
23672
24455
  }
23673
24456
  /** fec-multicast-00 §6.6: report each base block whose RAP MFU fragments
23674
24457
  * the overlay restored, when the keyframe's FT=0 and FT=1 are held too. A
@@ -23808,6 +24591,10 @@ var MmtpRouter = class {
23808
24591
  get unadmittedListedDropCount() {
23809
24592
  return this.#unadmittedListedDrops;
23810
24593
  }
24594
+ /** Source datagrams (multicast or MoQ) a protected flow's bc-provenance v2 receiver released: admitted at once or held, then admitted by a record. Repair symbols and user-agent-admitted datagrams are not counted. Counted at release, before this router's own track checks: a released datagram those drop, which only a track change between hold and release can cause, still counts, so this is not a count of routed media. */
24595
+ get admittedSourceDatagramCount() {
24596
+ return this.#admittedSourceDatagrams;
24597
+ }
23811
24598
  /** MoQ repair symbols of an unlisted track kept out of a governed FEC instance (7.2.7 step 2). */
23812
24599
  get unlistedRepairSetAsideCount() {
23813
24600
  return this.#unlistedRepairSetAside;
@@ -24294,6 +25081,8 @@ var MmtpRouter = class {
24294
25081
  )) return;
24295
25082
  fec.noteBlockActivity?.(blockStart, ownerPacketId, catalogTrackName);
24296
25083
  fec.noteRepairGroupLength?.(blockStart, ownerPacketId, repair.rsbLength, catalogTrackName);
25084
+ this.#feedPendingRebuilt(ownerPacketId, catalogTrackName, placement.block);
25085
+ if (this.#classifyPacketId(ownerPacketId) === null) return;
24297
25086
  const alreadyCompleted = fec.isBlockCompleted(blockStart, ownerPacketId, catalogTrackName);
24298
25087
  const recovered = fec.feedRepairSymbol(
24299
25088
  ownerPacketId,
@@ -24344,7 +25133,7 @@ var MmtpRouter = class {
24344
25133
  catalogTrackName
24345
25134
  )) continue;
24346
25135
  if (fec.isTrackConfigured?.(ownerPacketId) !== false) {
24347
- fec.trackBlock(blockStart, ownerPacketId, catalogTrackName, esi, payload.evidence);
25136
+ fec.trackBlock(blockStart, ownerPacketId, catalogTrackName, esi, payload.evidence, true);
24348
25137
  this.#requestFecPoke(ownerPacketId, catalogTrackName);
24349
25138
  }
24350
25139
  if (payload.oversized) continue;
@@ -24431,6 +25220,8 @@ var MmtpRouter = class {
24431
25220
  this.#droppedPreSyncVideo.delete(streamKey);
24432
25221
  this.#latestVideoSeqByStream.delete(streamKey);
24433
25222
  this.#ssIdSeqOffsetByStream.delete(streamKey);
25223
+ this.#unstampedSsIdSeqOffsetByStream.delete(streamKey);
25224
+ this.#overlayUnstampedSsIdSeqOffsetByStream.delete(streamKey);
24434
25225
  this.#multicastRapTwinByStream.delete(streamKey);
24435
25226
  this.#lastDirectSourceVideoTsByStream.delete(streamKey);
24436
25227
  this.#lastDirectEmittedVideoTsByStream.delete(streamKey);
@@ -24557,6 +25348,12 @@ var MmtpRouter = class {
24557
25348
  * the outgoing stream stops. The hold is bounded by the incoming stream's
24558
25349
  * own RAP cadence, so no timer is involved.
24559
25350
  *
25351
+ * While the switch is staged, the FEC release gate lets a keyframe the
25352
+ * overlay rebuilt reach it, on either side, when the keyframe did before
25353
+ * the gate anchored it (FecReleaseGate.releaseUnanchored): the switch
25354
+ * decides on what each side delivered, and the commit discards the gate's
25355
+ * frames of the outgoing stream.
25356
+ *
24560
25357
  * The commit is one synchronous transition inside the frame path: the
24561
25358
  * outgoing stream's already-admitted frames are released (its queued FEC
24562
25359
  * gap fills only up to the RAP, since recovery can queue a frame ahead of
@@ -24607,6 +25404,9 @@ var MmtpRouter = class {
24607
25404
  droppedIncomingFrames: 0
24608
25405
  };
24609
25406
  this.#staged = state;
25407
+ if (this.#parser.fecReleaseHoldsRebuilt(from.packetId, from.catalogTrackName)) {
25408
+ this.#requestFecPoke(from.packetId, from.catalogTrackName);
25409
+ }
24610
25410
  return {
24611
25411
  cancel: () => {
24612
25412
  if (this.#staged !== state) return false;
@@ -24689,7 +25489,17 @@ var MmtpRouter = class {
24689
25489
  */
24690
25490
  #routeParsedFrame(data, meta) {
24691
25491
  if (this.#classifyPacketId(meta.packetId) === null) return;
24692
- if (this.#staged !== null && this.#interceptSwitchFrame(data, meta)) return;
25492
+ if (this.#staged !== null) {
25493
+ let consumed = false;
25494
+ this.#fecPokeDepth++;
25495
+ try {
25496
+ consumed = this.#interceptSwitchFrame(data, meta);
25497
+ } finally {
25498
+ this.#fecPokeDepth--;
25499
+ this.#flushPendingFecPokes();
25500
+ }
25501
+ if (consumed) return;
25502
+ }
24693
25503
  this.#deliverParsedFrame(data, meta);
24694
25504
  }
24695
25505
  #commitStreamSwitch(state, rap, reason) {
@@ -24841,12 +25651,16 @@ var MmtpRouter = class {
24841
25651
  this.#parser.deferFecRelease(work);
24842
25652
  } finally {
24843
25653
  this.#fecPokeDepth--;
24844
- if (this.#fecPokeDepth === 0 && this.#pendingFecPokes.size > 0 && !this.#disposed) {
24845
- const pokes = [...this.#pendingFecPokes.values()];
24846
- this.#pendingFecPokes.clear();
24847
- for (const poke of pokes) {
24848
- this.#parser.pokeFecRelease(poke.packetId, poke.catalogTrackName);
24849
- }
25654
+ this.#flushPendingFecPokes();
25655
+ }
25656
+ }
25657
+ /** Run the pokes requested while #fecPokeDepth was raised, once it is 0. */
25658
+ #flushPendingFecPokes() {
25659
+ if (this.#fecPokeDepth === 0 && this.#pendingFecPokes.size > 0 && !this.#disposed) {
25660
+ const pokes = [...this.#pendingFecPokes.values()];
25661
+ this.#pendingFecPokes.clear();
25662
+ for (const poke of pokes) {
25663
+ this.#parser.pokeFecRelease(poke.packetId, poke.catalogTrackName);
24850
25664
  }
24851
25665
  }
24852
25666
  }
@@ -24924,6 +25738,14 @@ var MmtpRouter = class {
24924
25738
  onRap: (packetId, catalogTrackName, ssId) => {
24925
25739
  for (const observer of this.#fecRapObservers) observer(packetId, catalogTrackName, ssId);
24926
25740
  },
25741
+ // A staged switch commits on when each side's frames reach it
25742
+ // (#interceptSwitchFrame): both sides' overlay keyframes reach it when
25743
+ // they did unanchored.
25744
+ releaseUnanchored: (packetId, catalogTrackName) => {
25745
+ const staged = this.#staged;
25746
+ const stream = { packetId, ...catalogTrackName === void 0 ? {} : { catalogTrackName } };
25747
+ return staged !== null && (isStream(stream, staged.from) || isStream(stream, staged.to));
25748
+ },
24927
25749
  onReleased: (data, meta) => {
24928
25750
  if (meta.trackType !== "video" || meta.mpuSequenceNumber === void 0) return;
24929
25751
  const timing = meta.authoredTiming;
@@ -25065,6 +25887,7 @@ var MmtpRouter = class {
25065
25887
  const parsed = this.#parser.parsePacket(packet);
25066
25888
  if (!parsed) return;
25067
25889
  const info = parsed.info;
25890
+ if (manifestAdmitted && !info.isRepair && ingress?.admittedBy !== USER_AGENT_VERIFIED) this.#admittedSourceDatagrams++;
25068
25891
  if (source === "multicast" && !manifestAdmitted) {
25069
25892
  this.#fecManager?.noteMulticastArrival?.(info.packetId, info.sequenceNumber);
25070
25893
  }
@@ -25346,7 +26169,8 @@ var MmtpRouter = class {
25346
26169
  parsed.header,
25347
26170
  info.sequenceNumber,
25348
26171
  info.rapFlag,
25349
- source
26172
+ source,
26173
+ fecPayloadId?.ssId
25350
26174
  );
25351
26175
  }
25352
26176
  }
@@ -25372,7 +26196,8 @@ var MmtpRouter = class {
25372
26196
  source: parserOptions?.source,
25373
26197
  deliveryProvenance: parserOptions?.deliveryProvenance,
25374
26198
  catalogTrackName: parserOptions?.catalogTrackName,
25375
- ...parserOptions?.fecSbn === void 0 ? {} : { fecSbn: parserOptions.fecSbn }
26199
+ ...parserOptions?.fecSbn === void 0 ? {} : { fecSbn: parserOptions.fecSbn },
26200
+ ...parserOptions?.fecSbnFirst === void 0 ? {} : { fecSbnFirst: parserOptions.fecSbnFirst }
25376
26201
  };
25377
26202
  if (parsed) this.#parser.feedParsed(packet, parsed, feedOptions);
25378
26203
  else this.#parser.feed(packet, feedOptions);
@@ -25496,7 +26321,8 @@ var MmtpRouter = class {
25496
26321
  if (stream === void 0) {
25497
26322
  stream = {
25498
26323
  key: JSON.stringify([packetId, catalogTrackName ?? null]),
25499
- duplicates: new StreamDuplicateLedgers(() => this.duplicateHorizonFor(packetId))
26324
+ duplicates: new StreamDuplicateLedgers(() => this.duplicateHorizonFor(packetId)),
26325
+ overlayToBase: { routed: /* @__PURE__ */ new Set(), pending: /* @__PURE__ */ new Map() }
25500
26326
  };
25501
26327
  byName.set(catalogTrackName, stream);
25502
26328
  }
@@ -25611,8 +26437,16 @@ var MmtpRouter = class {
25611
26437
  this.#drainDirectRecoveredVideo(streamKey, packetId, (frame) => frame.timestamp <= meta.timestamp, cb);
25612
26438
  this.#emitDirectVideo(packetId, data, meta, cb);
25613
26439
  }
25614
- #routeDirectRecoveredVideo(packetId, data, meta, cb) {
26440
+ #routeDirectRecoveredVideo(packetId, data, meta, cb, fecSbn) {
25615
26441
  const streamKey = this.#mediaStreamKey(packetId, meta.catalogTrackName);
26442
+ if (this.#isFecTrack(packetId)) {
26443
+ const high = this.#deliveredVideoHighWaterByStream.get(streamKey);
26444
+ if (fecSbn !== void 0 || high !== void 0 && authoredAdjacent(high, meta)) {
26445
+ this.#drainDirectRecoveredVideo(streamKey, packetId, (frame) => frame.timestamp <= meta.timestamp, cb);
26446
+ this.#emitDirectVideo(packetId, data, meta, cb);
26447
+ return;
26448
+ }
26449
+ }
25616
26450
  this.#enqueueDirectRecoveredVideo(packetId, data, meta);
25617
26451
  const lastSourceTs = this.#lastDirectSourceVideoTsByStream.get(streamKey);
25618
26452
  this.#drainDirectRecoveredVideo(
@@ -25766,6 +26600,13 @@ var MmtpRouter = class {
25766
26600
  * frames by epoch only if pre-protection input ever matters.
25767
26601
  */
25768
26602
  #routeVerifiedFecSymbols(blockId, result, ownerPacketId, catalogTrackName, complete, assisted = false) {
26603
+ const overlayRouted = ownerPacketId == null ? void 0 : this.#streams.get(ownerPacketId)?.get(catalogTrackName)?.overlayToBase.routed;
26604
+ if (overlayRouted !== void 0 && overlayRouted.size > 0) {
26605
+ result = {
26606
+ ...result,
26607
+ symbols: result.symbols.filter((sym) => !overlayRouted.has(this.#fecAnchorForRecovered(blockId, sym.index)))
26608
+ };
26609
+ }
25769
26610
  const entry = ownerPacketId == null ? void 0 : this.#provenanceFor(ownerPacketId, catalogTrackName);
25770
26611
  const receiver = entry === void 0 || entry === "ambiguous" ? entry : entry.receiver;
25771
26612
  const userAgent = !assisted && entry !== void 0 && entry !== "ambiguous" && entry.protected === true && entry.userAgent === true;
@@ -25822,7 +26663,12 @@ var MmtpRouter = class {
25822
26663
  #routeProcessedFecSymbols(blockId, result, ownerPacketId, catalogTrackName, overlay) {
25823
26664
  if (ownerPacketId == null) return;
25824
26665
  if (this.#classifyPacketId(ownerPacketId) === null) return;
25825
- const fecSbnOf = (index) => overlay ? {} : { fecSbn: this.#fecAnchorForRecovered(blockId, index) };
26666
+ const fecSbnOf = (index) => {
26667
+ if (!overlay) return { fecSbn: this.#fecAnchorForRecovered(blockId, index) };
26668
+ const fecSbn = overlay.baseSsIds.get(index);
26669
+ const fecSbnFirst = overlay.firstSsIds.get(index);
26670
+ return fecSbn === void 0 || fecSbnFirst === void 0 ? {} : { fecSbn, fecSbnFirst };
26671
+ };
25826
26672
  const streamKey = this.#mediaStreamKey(ownerPacketId, catalogTrackName);
25827
26673
  const ssIdSeqOffset = overlay ? void 0 : this.#ssIdSeqOffsetByStream.get(streamKey);
25828
26674
  if (ssIdSeqOffset !== void 0) {
@@ -25830,7 +26676,10 @@ var MmtpRouter = class {
25830
26676
  ...result,
25831
26677
  symbols: result.symbols.map((sym) => {
25832
26678
  const wire = this.#fecAnchorForRecovered(blockId, sym.index) - ssIdSeqOffset >>> 0;
25833
- if (wire === sym.packetSequenceNumber) return sym;
26679
+ if (wire === sym.packetSequenceNumber) {
26680
+ this.#unstampedSsIdSeqOffsetByStream.set(streamKey, ssIdSeqOffset);
26681
+ return sym;
26682
+ }
25834
26683
  this.#restampedRecoveredSeqs++;
25835
26684
  this.#restampedStreams.add(streamKey);
25836
26685
  return { ...sym, packetSequenceNumber: wire };
@@ -25866,6 +26715,29 @@ var MmtpRouter = class {
25866
26715
  }
25867
26716
  );
25868
26717
  }
26718
+ const untimedHead = (sym) => sym.fragmentType === 2 && sym.fragment.fragmentationIndicator === 1 && sym.fragment.movieFragmentSequenceNumber !== void 0 && sym.fragment.sampleNumber !== void 0 && !this.#parser.hasAuthoredTimingFor(
26719
+ ownerPacketId,
26720
+ sym.fragment.mpuSequenceNumber,
26721
+ sym.fragment.movieFragmentSequenceNumber,
26722
+ sym.fragment.sampleNumber,
26723
+ catalogTrackName
26724
+ );
26725
+ for (const sym of result.symbols) {
26726
+ if (!untimedHead(sym) || this.#sourceSeqReceived(ownerPacketId, sym.packetSequenceNumber, catalogTrackName)) continue;
26727
+ this.#feedViaParser(
26728
+ sym.rawSymbol,
26729
+ { packetId: ownerPacketId, sequenceNumber: sym.packetSequenceNumber },
26730
+ void 0,
26731
+ {
26732
+ skipFecPayloadId: true,
26733
+ recovered: true,
26734
+ deliveryProvenance,
26735
+ ...fecSbnOf(sym.index),
26736
+ ...catalogTrackName === void 0 ? {} : { catalogTrackName }
26737
+ }
26738
+ );
26739
+ }
26740
+ result = { ...result, symbols: result.symbols.filter((sym) => !untimedHead(sym)) };
25869
26741
  const gated = this.#isFecTrack(ownerPacketId);
25870
26742
  const assembler = this.#mseAssemblerFor(ownerPacketId, catalogTrackName);
25871
26743
  if (assembler) {
@@ -27592,6 +28464,12 @@ function isNonNegativeSafeInteger(value) {
27592
28464
  function isSymbolCounts(value) {
27593
28465
  return isRecord(value) && hasExactKeys(value, ["multicast", "moq"]) && isNonNegativeSafeInteger(value["multicast"]) && isNonNegativeSafeInteger(value["moq"]);
27594
28466
  }
28467
+ function isSpanFailure(value) {
28468
+ if (!isRecord(value) || !hasExactKeys(value, ["length", "sources"])) return false;
28469
+ const length = value["length"];
28470
+ const sources = value["sources"];
28471
+ return Number.isSafeInteger(length) && length > 0 && isNonNegativeSafeInteger(sources) && sources < length;
28472
+ }
27595
28473
  function isNullableNonNegative(value) {
27596
28474
  return value === null || typeof value === "number" && Number.isFinite(value) && value >= 0;
27597
28475
  }
@@ -27611,7 +28489,10 @@ function isRepairLifecycleEvent(value) {
27611
28489
  case "recovered":
27612
28490
  return hasExactKeys(value, ["version", "type", "block", "blockClass", "via", "symbols"]) && isRepairBlockIdentity(block) && BLOCK_CLASSES.has(value["blockClass"]) && (value["via"] === "inband" || value["via"] === "quic") && isSymbolCounts(value["symbols"]);
27613
28491
  case "failed":
27614
- return hasExactKeys(value, ["version", "type", "block", "blockClass", "symbols"]) && isRepairBlockIdentity(block) && BLOCK_CLASSES.has(value["blockClass"]) && isSymbolCounts(value["symbols"]);
28492
+ return hasExactKeys(
28493
+ value,
28494
+ "span" in value ? ["version", "type", "block", "blockClass", "symbols", "span"] : ["version", "type", "block", "blockClass", "symbols"]
28495
+ ) && isRepairBlockIdentity(block) && BLOCK_CLASSES.has(value["blockClass"]) && isSymbolCounts(value["symbols"]) && (!("span" in value) || isSpanFailure(value["span"]));
27615
28496
  case "repair-preempted":
27616
28497
  case "repair-deferred":
27617
28498
  return hasExactKeys(value, ["version", "type", "block"]) && isRepairBlockIdentity(block);
@@ -27877,7 +28758,7 @@ var MulticastManager = class {
27877
28758
  onPublisherRestart: (packetId, catalogTrackName) => {
27878
28759
  this.#resetStreamOutsideRouter(packetId, catalogTrackName);
27879
28760
  },
27880
- onBlockFailed: (sbn, packetId, catalogTrackName, blockClass) => {
28761
+ onBlockFailed: (sbn, packetId, catalogTrackName, blockClass, span) => {
27881
28762
  const pathCount = this.#fec.blockPathCount(
27882
28763
  sbn,
27883
28764
  packetId,
@@ -27895,7 +28776,8 @@ var MulticastManager = class {
27895
28776
  symbols: {
27896
28777
  multicast: pathCount?.multicastSymbols ?? 0,
27897
28778
  moq: pathCount?.moqSymbols ?? 0
27898
- }
28779
+ },
28780
+ ...span === void 0 ? {} : { span }
27899
28781
  });
27900
28782
  },
27901
28783
  onFrameGap: (decision) => {
@@ -28678,6 +29560,7 @@ var MulticastManager = class {
28678
29560
  mode: endpoint.mode,
28679
29561
  amtRelay: endpoint.amtRelay,
28680
29562
  amtRelayPort: endpoint.amtRelayPort ?? (endpoint.amtRelay?.trim() ? DEFAULT_AMT_RELAY_PORT : void 0),
29563
+ amtDiscovery: endpoint.amtDiscovery,
28681
29564
  packetIds: endpoint.packetIds,
28682
29565
  tracks: endpoint.tracks.map(({ name, packetId }) => ({ name, packetId })),
28683
29566
  // A protected repair group's records ride in band on its port (7.2.3).
@@ -29274,10 +30157,12 @@ var MulticastManager = class {
29274
30157
  * Register the range repair scheduler (draft-ramadan-moq-fec §7.5): from now
29275
30158
  * on an iso-ssbg1-v1 gap missing a source and every repair is kept as a
29276
30159
  * pending hole and reported here, and `closeIsoHoles` must be called for it
29277
- * once its range FETCH ends (or none is sent).
30160
+ * once its range FETCH ends (or none is sent). `onExpired` is told of a
30161
+ * pending hole the stream runs past the locator's horizon: returning true,
30162
+ * the caller closes it (`closeIsoHoles`); otherwise it is discarded.
29278
30163
  */
29279
- onUndelimitedLoss(cb) {
29280
- this.#fec.setUndelimitedLossHandler(cb);
30164
+ onUndelimitedLoss(cb, onExpired) {
30165
+ this.#fec.setUndelimitedLossHandler(cb, onExpired ?? null);
29281
30166
  }
29282
30167
  /**
29283
30168
  * Report each stream whose FEC fails closed at runtime (fec-01 6.3.9), e.g.
@@ -29513,7 +30398,8 @@ var MulticastManager = class {
29513
30398
  "stream" in event ? { ...event, stream: Object.freeze({ ...event.stream }) } : {
29514
30399
  ...event,
29515
30400
  block: Object.freeze({ ...event.block }),
29516
- ..."symbols" in event ? { symbols: Object.freeze({ ...event.symbols }) } : {}
30401
+ ..."symbols" in event ? { symbols: Object.freeze({ ...event.symbols }) } : {},
30402
+ ..."span" in event && event.span !== void 0 ? { span: Object.freeze({ ...event.span }) } : {}
29517
30403
  }
29518
30404
  );
29519
30405
  if (this.#logRepairLifecycle) {
@@ -29626,7 +30512,7 @@ async function setupFecWorkerForTrack(opts) {
29626
30512
  blockProfile,
29627
30513
  k: opts.fecK,
29628
30514
  // P as this receiver joined it, not the catalog total: the worker snapshot
29629
- // and CMCD `x-bc-fec-p` are read from this. Identity on a single layer.
30515
+ // and CMCD `com.blockcast-fp` are read from this. Identity on a single layer.
29630
30516
  repairCount: opts.joinedRepairCount ?? opts.fecRepairCount,
29631
30517
  symbolSize: opts.fecSymbolSize,
29632
30518
  interleaveDepth: interleaveDepthBlocks,
@@ -30168,24 +31054,24 @@ var MediaRouter = class {
30168
31054
  if (!this.#multicastVideoKeyed) return;
30169
31055
  if (this.#requireAudio && !this.#multicastAudioReceived) return;
30170
31056
  if (this.#requireAudio && !this.#moqAudioWarmed) return;
30171
- const token = ++this.#sourceSwitchToken;
31057
+ const token2 = ++this.#sourceSwitchToken;
30172
31058
  const maybeReady = this.#onSourceSwitch?.("multicast");
30173
31059
  if (!maybeReady || typeof maybeReady.then !== "function") {
30174
- this.#commitMulticastActivation(token);
31060
+ this.#commitMulticastActivation(token2);
30175
31061
  return;
30176
31062
  }
30177
31063
  const ready = Promise.resolve(maybeReady);
30178
- this.#pendingMulticastToken = token;
31064
+ this.#pendingMulticastToken = token2;
30179
31065
  this.#sourceSwitchReady = ready;
30180
31066
  void ready.then(
30181
- () => this.#commitMulticastActivation(token),
30182
- (err) => this.#rejectMulticastActivation(token, err)
31067
+ () => this.#commitMulticastActivation(token2),
31068
+ (err) => this.#rejectMulticastActivation(token2, err)
30183
31069
  ).finally(() => {
30184
31070
  if (this.#sourceSwitchReady === ready) this.#sourceSwitchReady = null;
30185
31071
  });
30186
31072
  }
30187
- #commitMulticastActivation(token) {
30188
- if (token !== this.#sourceSwitchToken) return;
31073
+ #commitMulticastActivation(token2) {
31074
+ if (token2 !== this.#sourceSwitchToken) return;
30189
31075
  this.#active = "multicast";
30190
31076
  this.#suppressed = true;
30191
31077
  this.#pendingMulticastToken = null;
@@ -30196,9 +31082,9 @@ var MediaRouter = class {
30196
31082
  if (video) this.#onVideoFrame?.(video.data, video.meta);
30197
31083
  if (audio) this.#onAudioFrame?.(audio.data, audio.meta);
30198
31084
  }
30199
- #rejectMulticastActivation(token, err) {
30200
- if (token !== this.#sourceSwitchToken) return;
30201
- if (this.#pendingMulticastToken === token) this.#pendingMulticastToken = null;
31085
+ #rejectMulticastActivation(token2, err) {
31086
+ if (token2 !== this.#sourceSwitchToken) return;
31087
+ if (this.#pendingMulticastToken === token2) this.#pendingMulticastToken = null;
30202
31088
  this.#pendingMulticastVideo = null;
30203
31089
  this.#pendingMulticastAudio = null;
30204
31090
  console.warn("[MediaRouter] onSourceSwitch(multicast) failed", err);
@@ -31167,63 +32053,65 @@ var FecStarvationDetector = class {
31167
32053
 
31168
32054
  // src/cmcd-producer.ts
31169
32055
  var PLAY_MODES = ["full", "base-only", "single-track"];
31170
- function sanitize(value) {
31171
- return value.replace(/[,\r\n]/g, "");
31172
- }
31173
- function buildFecParams(stats) {
31174
- const params = {};
31175
- const cfg = stats.config;
31176
- params["x-bc-fec-algo"] = cfg.algorithm;
31177
- params["x-bc-fec-k"] = cfg.k;
31178
- params["x-bc-fec-p"] = cfg.repairCount;
31179
- params["x-bc-fec-recovery"] = parseFloat((stats.recoveryRate / 100).toFixed(4));
31180
- return params;
31181
- }
31182
- function buildTransportParams(snap) {
31183
- if (!snap) return {};
31184
- const params = {};
31185
- params["x-bc-transport"] = snap.transport;
31186
- if (snap.multicastGroup != null) {
31187
- params["x-bc-multicast-group"] = snap.multicastGroup;
31188
- }
31189
- if (snap.multicastSource != null) {
31190
- params["x-bc-multicast-source"] = snap.multicastSource;
31191
- }
31192
- if (snap.amtRelay != null) {
31193
- params["x-bc-amt-relay"] = snap.amtRelay;
31194
- }
31195
- return params;
31196
- }
31197
- function formatQueryString(params) {
31198
- return Object.keys(params).sort().map((key) => {
31199
- const v = params[key];
31200
- if (typeof v === "string") return `${key}=${sanitize(v)}`;
31201
- return `${key}=${v}`;
32056
+ var DEFAULT_INTERVAL_MS = 3e4;
32057
+ var CUSTOM_PREFIX = "com.blockcast-";
32058
+ var MAX_CUSTOM_VALUE = 64;
32059
+ var TRANSPORT_TOKEN = {
32060
+ native: "ssm",
32061
+ amt: "amt",
32062
+ moq: "moq"
32063
+ };
32064
+ function sfString(value) {
32065
+ return `"${value.replace(/[^\x20-\x7e]/g, "").replace(/[\\"]/g, "\\$&")}"`;
32066
+ }
32067
+ function encodeCmcdRecord(record) {
32068
+ return Object.keys(record).sort().map((key) => {
32069
+ const value = record[key];
32070
+ if (typeof value === "number") return `${key}=${value}`;
32071
+ if (typeof value === "string") return `${key}=${sfString(value)}`;
32072
+ if (Array.isArray(value)) return `${key}=(${value.map(sfString).join(" ")})`;
32073
+ return `${key}=${value.token}`;
31202
32074
  }).join(",");
31203
32075
  }
32076
+ var token = (value) => ({ token: value });
32077
+ function putCustom(record, name, value) {
32078
+ if (value === void 0) return;
32079
+ if (typeof value === "number") value = String(value);
32080
+ if (typeof value === "string" && value.length > MAX_CUSTOM_VALUE) return;
32081
+ record[CUSTOM_PREFIX + name] = value;
32082
+ }
31204
32083
  var CmcdProducer = class {
31205
- #onCmcdParams;
31206
32084
  #onCmcdPost;
32085
+ #intervalMs;
32086
+ #sessionId;
32087
+ #now;
32088
+ #sn = 0;
32089
+ #lastHeartbeat = null;
31207
32090
  #transport;
31208
32091
  #lastStats;
31209
- // Phase 14 state — Plan 03 Task 2 (D-12, D-15).
31210
32092
  #playMode = null;
31211
32093
  #deltaFailRate = null;
31212
32094
  #switchMiscount = 0;
31213
32095
  #manifestStats = null;
31214
32096
  #fecFailClosed = null;
32097
+ /** Video-block resolutions from the repair lifecycle; null until the player feeds one. */
32098
+ #lifecycle = null;
31215
32099
  constructor(options) {
31216
- this.#onCmcdParams = options.onCmcdParams;
31217
32100
  this.#onCmcdPost = options.onCmcdPost;
32101
+ this.#intervalMs = options.intervalMs ?? DEFAULT_INTERVAL_MS;
32102
+ if (!Number.isFinite(this.#intervalMs) || this.#intervalMs <= 0) {
32103
+ throw new Error(`[CmcdProducer] invalid intervalMs "${String(options.intervalMs)}"; expected a positive number`);
32104
+ }
32105
+ this.#sessionId = options.sessionId ?? globalThis.crypto.randomUUID();
32106
+ this.#now = options.now ?? Date.now;
31218
32107
  }
31219
32108
  /** Update the stored transport state. Called by player wiring layer. */
31220
32109
  updateTransport(snapshot) {
31221
32110
  this.#transport = snapshot;
31222
32111
  }
31223
32112
  /**
31224
- * Set the current play-mode. Emitted as `x-bc-play-mode` on subsequent
31225
- * snapshots. Throws on any value outside {'full','base-only','single-track'}
31226
- * (CLAUDE.md: error on bad init).
32113
+ * Set the current play-mode, reported as `com.blockcast-pm`. Throws on any
32114
+ * value outside {'full','base-only','single-track'} (CLAUDE.md: error on bad init).
31227
32115
  */
31228
32116
  updatePlayMode(mode) {
31229
32117
  if (typeof mode !== "string" || !PLAY_MODES.includes(mode)) {
@@ -31234,9 +32122,8 @@ var CmcdProducer = class {
31234
32122
  this.#playMode = mode;
31235
32123
  }
31236
32124
  /**
31237
- * Set the delta-track FEC block-failure rate (0..1 inclusive). Emitted as
31238
- * `x-bc-delta-fail-rate` on snapshots when play-mode is 'full' or 'base-only'.
31239
- * Omitted on single-track streams.
32125
+ * Set the delta-track FEC block-failure rate (0..1 inclusive), reported as
32126
+ * `com.blockcast-dfr` when play-mode is 'full' or 'base-only'.
31240
32127
  */
31241
32128
  updateDeltaFailRate(rate) {
31242
32129
  if (typeof rate !== "number" || !Number.isFinite(rate) || rate < 0 || rate > 1) {
@@ -31246,112 +32133,139 @@ var CmcdProducer = class {
31246
32133
  }
31247
32134
  this.#deltaFailRate = rate;
31248
32135
  }
31249
- /**
31250
- * Bump the cumulative switching-set misalignment counter. Emitted as
31251
- * `x-bc-switch-miscount` on snapshots when the counter is > 0.
31252
- */
32136
+ /** Bump the cumulative switching-set misalignment counter (`com.blockcast-sm`). */
31253
32137
  incrementSwitchMiscount() {
31254
32138
  this.#switchMiscount += 1;
31255
32139
  }
31256
32140
  /**
31257
- * Record that FEC failed closed at runtime (fec-01 6.3.9). Every later
31258
- * snapshot carries `x-bc-fec-fail-closed` (the reason code) and
31259
- * `x-bc-fec-fail-layer` (the held layer i whose total B_i was undercut), so a
31260
- * stream playing without FEC is told apart from one with no loss.
32141
+ * Record that FEC failed closed at runtime (fec-01 6.3.9). Reported at once
32142
+ * as `e=e` with the reason code in `ec`, and on every later counter report
32143
+ * as `com.blockcast-ffc` / `com.blockcast-ffl` (the held layer whose total
32144
+ * B_i was undercut), so a stream playing without FEC is told apart from one
32145
+ * with no loss.
31261
32146
  */
31262
32147
  updateFecFailClosed(reason) {
32148
+ const changed = this.#fecFailClosed?.code !== reason.code || this.#fecFailClosed?.layer !== reason.layer;
31263
32149
  this.#fecFailClosed = reason;
32150
+ if (changed) this.#post([{ ...this.#base("e"), ec: [reason.code] }]);
31264
32151
  }
31265
32152
  updateManifestVerification(stats) {
31266
32153
  this.#manifestStats = stats;
31267
32154
  }
31268
32155
  /**
31269
- * One-shot emit of `x-bc-switch-drift-us` for a single misalignment event.
31270
- * Does NOT mutate snapshot state — subsequent onSnapshot() calls will not
31271
- * include the drift key until the next emitSwitchDrift(). Mirrors the
31272
- * existing onTransition() pattern.
32156
+ * Count a video block's resolution from the repair lifecycle: recovered in
32157
+ * band (`com.blockcast-fbr`), recovered by repair FETCH (`-fbq`), or failed
32158
+ * as a keyframe-class block (`-fbfk`). Audio blocks are not counted, matching
32159
+ * the video-track `-fbf`. Until the player feeds this, the three are absent.
31273
32160
  */
32161
+ onRepairLifecycle(event) {
32162
+ this.#lifecycle ??= { inband: 0, fetch: 0, failedKeyframe: 0 };
32163
+ if (event.type === "recovered" && event.blockClass !== "audio") {
32164
+ if (event.via === "inband") this.#lifecycle.inband++;
32165
+ else this.#lifecycle.fetch++;
32166
+ } else if (event.type === "failed" && event.blockClass === "keyframe") {
32167
+ this.#lifecycle.failedKeyframe++;
32168
+ }
32169
+ }
32170
+ /** One-shot custom event `com.blockcast-switch-drift` for a single misalignment. */
31274
32171
  emitSwitchDrift(driftUs) {
31275
32172
  if (typeof driftUs !== "number" || !Number.isFinite(driftUs) || driftUs < 0) {
31276
32173
  throw new Error(
31277
32174
  `[CmcdProducer] invalid drift-us "${String(driftUs)}"; expected a non-negative finite number`
31278
32175
  );
31279
32176
  }
31280
- const params = {};
31281
- if (this.#lastStats) Object.assign(params, buildFecParams(this.#lastStats));
31282
- Object.assign(params, buildTransportParams(this.#transport));
31283
- if (this.#switchMiscount > 0) params["x-bc-switch-miscount"] = this.#switchMiscount;
31284
- params["x-bc-switch-drift-us"] = driftUs;
31285
- this.#emit(params);
32177
+ const record = { ...this.#base("ce"), cen: CUSTOM_PREFIX + "switch-drift" };
32178
+ putCustom(record, "sdu", Math.round(driftUs));
32179
+ this.#post([record]);
31286
32180
  }
31287
- /**
31288
- * Process a FecTrackStats snapshot. Finds the video track, builds
31289
- * x-bc-* vendor keys, and fires both delivery callbacks.
31290
- */
32181
+ /** Take a FecTrackStats snapshot (first video track) and send the heartbeat when due. */
31291
32182
  onSnapshot(data) {
31292
32183
  const videoTrack = data.tracks.find((t) => t.config.trackType === "video");
31293
32184
  if (!videoTrack) return;
31294
32185
  this.#lastStats = videoTrack;
31295
- const params = {
31296
- ...buildFecParams(videoTrack),
31297
- ...buildTransportParams(this.#transport)
31298
- };
31299
- if (this.#playMode !== null) {
31300
- params["x-bc-play-mode"] = this.#playMode;
31301
- }
31302
- if (this.#fecFailClosed) {
31303
- params["x-bc-fec-fail-closed"] = this.#fecFailClosed.code;
31304
- params["x-bc-fec-fail-layer"] = this.#fecFailClosed.layer;
31305
- }
31306
- if (this.#playMode !== null && this.#playMode !== "single-track" && this.#deltaFailRate !== null) {
31307
- params["x-bc-delta-fail-rate"] = parseFloat(this.#deltaFailRate.toFixed(4));
31308
- }
31309
- if (this.#switchMiscount > 0) {
31310
- params["x-bc-switch-miscount"] = this.#switchMiscount;
31311
- }
31312
- if (this.#manifestStats) {
31313
- params["x-bc-manifest-verified"] = this.#manifestStats.verified > 0;
31314
- params["x-bc-manifest-failures"] = Object.values(this.#manifestStats.failures).reduce((total, count) => total + count, 0);
31315
- }
31316
- this.#emit(params);
32186
+ this.#heartbeat();
31317
32187
  }
31318
32188
  /**
31319
- * Emit transport state without inventing FEC metrics. Cadence is owned by
31320
- * the caller because only the transport integration knows when state changed.
32189
+ * Take transport state for receivers that do not own FEC stats, and send the
32190
+ * heartbeat when due.
31321
32191
  */
31322
32192
  onTransportSnapshot(data) {
31323
32193
  if (!Number.isFinite(data.timestampMs) || data.timestampMs < 0) {
31324
32194
  throw new Error("[CmcdProducer] transport snapshot timestamp must be a non-negative finite number");
31325
32195
  }
31326
32196
  if (data.playMode !== void 0) this.updatePlayMode(data.playMode);
31327
- if (data.switchMiscount !== void 0 && (!Number.isSafeInteger(data.switchMiscount) || data.switchMiscount < 0)) {
31328
- throw new Error("[CmcdProducer] transport snapshot switch-miscount must be a non-negative safe integer");
32197
+ if (data.switchMiscount !== void 0) {
32198
+ if (!Number.isSafeInteger(data.switchMiscount) || data.switchMiscount < 0) {
32199
+ throw new Error("[CmcdProducer] transport snapshot switch-miscount must be a non-negative safe integer");
32200
+ }
32201
+ this.#switchMiscount = data.switchMiscount;
31329
32202
  }
31330
32203
  this.#transport = data.transport;
31331
- const params = buildTransportParams(data.transport);
31332
- if (this.#playMode !== null) params["x-bc-play-mode"] = this.#playMode;
31333
- const switchMiscount = data.switchMiscount ?? this.#switchMiscount;
31334
- if (switchMiscount > 0) params["x-bc-switch-miscount"] = switchMiscount;
31335
- this.#emit(params);
32204
+ this.#heartbeat();
31336
32205
  }
31337
- /**
31338
- * Signal a transport transition event. Fires an immediate out-of-band
31339
- * emit with x-bc-ct set. The subsequent onSnapshot() will NOT include
31340
- * x-bc-ct (one-shot).
31341
- */
32206
+ /** Report a transport transition at once, as custom event `com.blockcast-ct`. */
31342
32207
  onTransition(event) {
31343
- const params = {};
31344
- if (this.#lastStats) {
31345
- Object.assign(params, buildFecParams(this.#lastStats));
32208
+ const record = { ...this.#base("ce"), cen: CUSTOM_PREFIX + "ct" };
32209
+ putCustom(record, "ct", token(event));
32210
+ this.#putTransport(record);
32211
+ this.#post([record]);
32212
+ }
32213
+ #base(event) {
32214
+ return { e: token(event), sid: this.#sessionId, sn: this.#sn++, ts: Math.floor(this.#now()), v: 2 };
32215
+ }
32216
+ #heartbeat() {
32217
+ const now = this.#now();
32218
+ if (this.#lastHeartbeat !== null && now - this.#lastHeartbeat < this.#intervalMs) return;
32219
+ this.#lastHeartbeat = now;
32220
+ this.#post([this.#base("t"), this.#counters()]);
32221
+ }
32222
+ #putTransport(record) {
32223
+ const snap = this.#transport;
32224
+ if (!snap) return;
32225
+ putCustom(record, "tr", token(TRANSPORT_TOKEN[snap.transport]));
32226
+ if (snap.multicastGroup != null && snap.multicastSource != null) {
32227
+ putCustom(record, "sg", `${snap.multicastGroup}@${snap.multicastSource}`);
32228
+ }
32229
+ putCustom(record, "ar", snap.amtRelay);
32230
+ }
32231
+ /** The `e=ce` counter report. */
32232
+ #counters() {
32233
+ const record = { ...this.#base("ce"), cen: CUSTOM_PREFIX + "fec" };
32234
+ const stats = this.#lastStats;
32235
+ if (stats) {
32236
+ putCustom(record, "fa", stats.config.algorithm);
32237
+ putCustom(record, "fk", stats.config.k);
32238
+ putCustom(record, "fp", stats.config.repairCount);
32239
+ putCustom(record, "pr", stats.sourceSymbols);
32240
+ putCustom(record, "pl", stats.sourcePacketsMissing);
32241
+ putCustom(record, "pu", stats.sourcePacketsUnrecovered);
32242
+ putCustom(record, "fbf", stats.blocksFailed);
32243
+ }
32244
+ if (this.#lifecycle) {
32245
+ putCustom(record, "fbr", this.#lifecycle.inband);
32246
+ putCustom(record, "fbq", this.#lifecycle.fetch);
32247
+ putCustom(record, "fbfk", this.#lifecycle.failedKeyframe);
32248
+ }
32249
+ this.#putTransport(record);
32250
+ if (this.#playMode !== null) {
32251
+ putCustom(record, "pm", token(this.#playMode));
32252
+ if (this.#playMode !== "single-track" && this.#deltaFailRate !== null) {
32253
+ putCustom(record, "dfr", this.#deltaFailRate.toFixed(3));
32254
+ }
31346
32255
  }
31347
- Object.assign(params, buildTransportParams(this.#transport));
31348
- params["x-bc-ct"] = event;
31349
- this.#emit(params);
32256
+ if (this.#fecFailClosed) {
32257
+ putCustom(record, "ffc", token(this.#fecFailClosed.code));
32258
+ putCustom(record, "ffl", this.#fecFailClosed.layer);
32259
+ }
32260
+ if (this.#switchMiscount > 0) putCustom(record, "sm", this.#switchMiscount);
32261
+ if (this.#manifestStats) {
32262
+ putCustom(record, "mv", this.#manifestStats.verified);
32263
+ putCustom(record, "mf", Object.values(this.#manifestStats.failures).reduce((a, b) => a + b, 0));
32264
+ }
32265
+ return record;
31350
32266
  }
31351
- /** Fire both delivery callbacks with the given params. */
31352
- #emit(params) {
31353
- this.#onCmcdParams?.(params);
31354
- this.#onCmcdPost?.(formatQueryString(params));
32267
+ #post(records) {
32268
+ this.#onCmcdPost?.(records.map(encodeCmcdRecord).join("\n"));
31355
32269
  }
31356
32270
  };
31357
32271
 
@@ -31447,18 +32361,31 @@ function resolveRepairLayers(catalog, source) {
31447
32361
  totalBaseRepairSymbols: binding.totalBaseRepairSymbols
31448
32362
  };
31449
32363
  }
32364
+ function valueAt(catalog, pointer) {
32365
+ let node = catalog;
32366
+ for (const segment of pointer.split("/").slice(1)) {
32367
+ if (typeof node !== "object" || node === null) return void 0;
32368
+ node = node[segment.replace(/~1/g, "/").replace(/~0/g, "~")];
32369
+ }
32370
+ return node;
32371
+ }
32372
+ function catalogTrackDeliveryMode(catalog, source) {
32373
+ const binding = bindingOf(catalog, source);
32374
+ const entry = binding ? valueAt(catalog, binding.pointer) : void 0;
32375
+ return entry?.deliveryMode === "stream" ? "stream" : "datagram";
32376
+ }
31450
32377
  function moqtPriority2(value) {
31451
32378
  return Number.isSafeInteger(value) && value >= 0 && value <= 255 ? value : null;
31452
32379
  }
31453
32380
  function catalogRepairTarget(catalog, source) {
31454
32381
  const binding = bindingOf(catalog, source);
31455
32382
  if (binding?.status !== "ok" || !(binding.base.interleaveDepthMs ?? 0)) return null;
31456
- const layerZero = binding.layers[0];
32383
+ const layerZero2 = binding.layers[0];
31457
32384
  const m = /^\/tracks\/(\d+)$/.exec(binding.pointer);
31458
32385
  const tracks = catalog.tracks;
31459
32386
  const entry = m && Array.isArray(tracks) ? tracks[Number(m[1])] : void 0;
31460
32387
  const sourcePriority = entry ? moqtPriority2(entry["priority"]) : null;
31461
- return { repairTrack: layerZero.repairTrackName, sourcePriority, repairPriority: layerZero.priority };
32388
+ return { repairTrack: layerZero2.repairTrackName, sourcePriority, repairPriority: layerZero2.priority };
31462
32389
  }
31463
32390
  function numericField(value) {
31464
32391
  const n = typeof value === "number" ? value : typeof value === "string" ? Number(value) : Number.NaN;
@@ -31490,6 +32417,7 @@ function fecParams(catalog, track, fps, sampleRate) {
31490
32417
  repairTrackName: base.repairTrack,
31491
32418
  repairLayers: repair.baseLayers,
31492
32419
  repairOverlays: repair.overlays,
32420
+ deliveryMode: catalogTrackDeliveryMode(catalog, track),
31493
32421
  ...mediaRate
31494
32422
  };
31495
32423
  }
@@ -31540,7 +32468,14 @@ function resolveCatalogTrackFec(catalog, tracks) {
31540
32468
  var PATH_BITS = { moq: 1, multicast: 2 };
31541
32469
  var MIXED_PATHS = PATH_BITS.moq | PATH_BITS.multicast;
31542
32470
  var MmtpFecSession = class {
32471
+ /** P_0 = B_0, the held total every repair packet is checked against. */
32472
+ get repairSymbols() {
32473
+ return this.#repairSymbols;
32474
+ }
31543
32475
  #decoder;
32476
+ #repairSymbols;
32477
+ /** The message entry a catalog-less session was built from; undefined under a catalog. */
32478
+ #message;
31544
32479
  /** Blocks a repair has defined and that are not settled yet, by SS_Start. */
31545
32480
  #blocks = /* @__PURE__ */ new Map();
31546
32481
  /** SS_Starts of settled blocks, so a later repair for one is refused. */
@@ -31578,6 +32513,24 @@ var MmtpFecSession = class {
31578
32513
  * `interleaveDepthMs` or `reorderToleranceMs`. None is defaulted.
31579
32514
  */
31580
32515
  constructor(config) {
32516
+ if (config && "alFecFlow" in config) {
32517
+ const message = alFecRaptorQFlowConfig(config.alFecFlow);
32518
+ if (!message) {
32519
+ throw new Error("MmtpFecSession: the AL-FEC entry is outside draft-ramadan-moq-mmt-02 8.3 (iso-ssbg1-v1)");
32520
+ }
32521
+ this.#message = message;
32522
+ this.blockProfile = parseFecBlockProfile("iso-ssbg1-v1", "alFecFlow");
32523
+ this.algorithm = message.fecCodeId === FEC_CODE_RAPTORQ ? "raptorq" : "reed-solomon";
32524
+ this.sourceSymbols = message.maximumK;
32525
+ this.#repairSymbols = message.maximumP;
32526
+ this.symbolSize = message.repairSymbolSize;
32527
+ this.retentionBlocks = requirePositiveInteger(config.retentionBlocks, "retentionBlocks");
32528
+ this.#repairPacketId = message.repairFlowId;
32529
+ this.#heldLayers = layerZero(message.maximumP);
32530
+ this.#anchorHorizon = heldLayerAnchorHorizon(this.retentionBlocks, this.sourceSymbols);
32531
+ this.#decoder = buildDecoder(config.Decoder, this);
32532
+ return;
32533
+ }
31581
32534
  const fec = config?.fec;
31582
32535
  if (!fec || typeof fec !== "object") {
31583
32536
  throw new Error("MmtpFecSession: the catalog fec instance is required");
@@ -31588,7 +32541,7 @@ var MmtpFecSession = class {
31588
32541
  }
31589
32542
  this.algorithm = fec.scheme;
31590
32543
  this.sourceSymbols = requirePositiveInteger(fec.sourceSymbols, "fec.sourceSymbols");
31591
- this.repairSymbols = requirePositiveInteger(fec.repairSymbols, "fec.repairSymbols");
32544
+ this.#repairSymbols = requirePositiveInteger(fec.repairSymbols, "fec.repairSymbols");
31592
32545
  this.symbolSize = requirePositiveInteger(fec.symbolSize, "fec.symbolSize");
31593
32546
  const interleaveDepthMs = requireNumber(fec.interleaveDepthMs, "fec.interleaveDepthMs");
31594
32547
  const reorderToleranceMs = requireNumber(fec.reorderToleranceMs, "fec.reorderToleranceMs");
@@ -31608,17 +32561,30 @@ var MmtpFecSession = class {
31608
32561
  config.heldLayers
31609
32562
  );
31610
32563
  this.#anchorHorizon = heldLayerAnchorHorizon(this.retentionBlocks, this.sourceSymbols);
31611
- if (typeof config.Decoder !== "function") {
31612
- throw new Error("MmtpFecSession: a decoder constructor (wasm_bindgen.MmtFecDecoder) is required");
31613
- }
31614
- const decoder = new config.Decoder(this.retentionBlocks, this.blockProfile, this.algorithm);
31615
- try {
31616
- decoder.configure_params(this.sourceSymbols, this.symbolSize);
31617
- } catch (error) {
31618
- decoder.free();
31619
- throw error;
31620
- }
31621
- this.#decoder = decoder;
32564
+ this.#decoder = buildDecoder(config.Decoder, this);
32565
+ }
32566
+ /**
32567
+ * A later version of the AL-FEC message, catalog-less sessions only. A
32568
+ * maximum_p_for_repair_flow above the held total raises it (`raised`): the
32569
+ * anchored check restarts at the new total, so a block generated under the
32570
+ * previous one stays valid before the first block carrying the new total
32571
+ * (fec-core-00 7.3.2, mmt-02 8.3). A field at or below it changes nothing
32572
+ * (`unchanged`): no publisher lowers it, and the check catches a wire that
32573
+ * does. An entry with another asset, repair flow, code point, K or T, or
32574
+ * one outside 8.3, is `refused`: that is a new source encoding, which needs
32575
+ * a new session. A latched fail-closed stays latched either way.
32576
+ */
32577
+ updateAlFecFlow(flow) {
32578
+ const held = this.#message;
32579
+ if (!held) throw new Error("MmtpFecSession: updateAlFecFlow() needs a session built from an AL-FEC entry");
32580
+ const next = alFecRaptorQFlowConfig(flow);
32581
+ if (!next || next.sourcePacketIds[0] !== held.sourcePacketIds[0] || next.repairFlowId !== held.repairFlowId || next.fecCodeId !== held.fecCodeId || next.maximumK !== held.maximumK || next.repairSymbolSize !== held.repairSymbolSize) {
32582
+ return "refused";
32583
+ }
32584
+ if (next.maximumP <= this.#repairSymbols) return "unchanged";
32585
+ this.#repairSymbols = next.maximumP;
32586
+ this.#heldLayers = layerZero(next.maximumP);
32587
+ return "raised";
31622
32588
  }
31623
32589
  /** True once release() ran. */
31624
32590
  get released() {
@@ -31713,7 +32679,7 @@ var MmtpFecSession = class {
31713
32679
  }
31714
32680
  if (admission.reject) {
31715
32681
  const verdict2 = this.#heldLayers.failsClosed(
31716
- repairTrackName,
32682
+ this.#trackFor(repairTrackName),
31717
32683
  frame.ssStart,
31718
32684
  frame.rsbLength,
31719
32685
  this.#anchorHorizon,
@@ -31728,7 +32694,7 @@ var MmtpFecSession = class {
31728
32694
  this.#repairPacketId ??= frame.packetId;
31729
32695
  if (frame.ssbLength > this.sourceSymbols) return this.#drop("repair:ssb-length");
31730
32696
  const verdict = this.#heldLayers.admit(
31731
- repairTrackName,
32697
+ this.#trackFor(repairTrackName),
31732
32698
  frame.ssStart,
31733
32699
  frame.rsbLength,
31734
32700
  this.#anchorHorizon,
@@ -31787,6 +32753,10 @@ var MmtpFecSession = class {
31787
32753
  decoder.free();
31788
32754
  return true;
31789
32755
  }
32756
+ /** Catalog-less: layer 0 has no own-track clause (mmt-02 8.3), so no track name is judged. */
32757
+ #trackFor(repairTrackName) {
32758
+ return this.#message ? void 0 : repairTrackName;
32759
+ }
31790
32760
  #drop(reason) {
31791
32761
  this.#stats.dropped[reason] = (this.#stats.dropped[reason] ?? 0) + 1;
31792
32762
  return [];
@@ -31908,6 +32878,22 @@ var MmtpFecSession = class {
31908
32878
  }
31909
32879
  }
31910
32880
  };
32881
+ function layerZero(total) {
32882
+ return new HeldLayerAdmission([{ layer: 0, repairTrackName: "", rsIdOffset: 0, repairSymbols: total }]);
32883
+ }
32884
+ function buildDecoder(Decoder, session) {
32885
+ if (typeof Decoder !== "function") {
32886
+ throw new Error("MmtpFecSession: a decoder constructor (wasm_bindgen.MmtFecDecoder) is required");
32887
+ }
32888
+ const decoder = new Decoder(session.retentionBlocks, session.blockProfile, session.algorithm);
32889
+ try {
32890
+ decoder.configure_params(session.sourceSymbols, session.symbolSize);
32891
+ } catch (error) {
32892
+ decoder.free();
32893
+ throw error;
32894
+ }
32895
+ return decoder;
32896
+ }
31911
32897
  function requirePositiveInteger(value, path) {
31912
32898
  if (typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0) {
31913
32899
  throw new Error(`MmtpFecSession: ${path} must be a positive integer (got ${String(value)})`);
@@ -31921,8 +32907,171 @@ function requireNumber(value, path) {
31921
32907
  return value;
31922
32908
  }
31923
32909
 
32910
+ // src/repair-layer-subscriptions.ts
32911
+ var SERIAL32_HALF2 = 2147483648;
32912
+ var RepairLayerSubscriptions = class {
32913
+ #opts;
32914
+ #windowPackets;
32915
+ #margin;
32916
+ #dropAfterWindows;
32917
+ #source;
32918
+ #layerCount = 0;
32919
+ #subs = /* @__PURE__ */ new Map();
32920
+ #disposed = false;
32921
+ #lastSeq;
32922
+ #expected = 0;
32923
+ #lost = 0;
32924
+ #wantFewer = 0;
32925
+ #lossRate = null;
32926
+ #repairPackets = 0;
32927
+ #repairBytes = 0;
32928
+ #staleDropped = 0;
32929
+ constructor(opts) {
32930
+ this.#opts = opts;
32931
+ this.#source = opts.source;
32932
+ this.#windowPackets = opts.windowPackets ?? 500;
32933
+ this.#margin = opts.margin ?? 2;
32934
+ this.#dropAfterWindows = opts.dropAfterWindows ?? 3;
32935
+ }
32936
+ get layerCount() {
32937
+ return this.#layerCount;
32938
+ }
32939
+ /**
32940
+ * One source packet's MMTP `packet_sequence_number`, in arrival order.
32941
+ * The sequence is per `packet_id`: pass the packets of ONE packet_id only,
32942
+ * or interleaved sequences read as loss.
32943
+ */
32944
+ noteSourceSequence(seq) {
32945
+ if (this.#disposed || this.#delivery() === "stream") return;
32946
+ seq >>>= 0;
32947
+ if (this.#lastSeq === void 0) {
32948
+ this.#lastSeq = seq;
32949
+ return;
32950
+ }
32951
+ const d = serial32Distance(this.#lastSeq, seq);
32952
+ if (d === 0) return;
32953
+ const back = d >= SERIAL32_HALF2 ? 2 ** 32 - d : 0;
32954
+ if (d > this.#windowPackets && (back === 0 || back > this.#windowPackets)) {
32955
+ this.#lastSeq = seq;
32956
+ this.#expected = 0;
32957
+ this.#lost = 0;
32958
+ return;
32959
+ }
32960
+ if (back === 0) {
32961
+ this.#expected += d;
32962
+ this.#lost += d - 1;
32963
+ this.#lastSeq = seq;
32964
+ } else if (this.#lost > 0) {
32965
+ this.#lost -= 1;
32966
+ }
32967
+ if (this.#expected >= this.#windowPackets) this.#closeWindow();
32968
+ }
32969
+ /** ABR switch R -> R': close R's tracks, carry the layer count to R'. */
32970
+ switchSource(next) {
32971
+ if (this.#disposed) return;
32972
+ this.#closeAll();
32973
+ this.#source = next;
32974
+ this.#lastSeq = void 0;
32975
+ this.#expected = 0;
32976
+ this.#lost = 0;
32977
+ this.#wantFewer = 0;
32978
+ this.#layerCount = this.#delivery() === "stream" ? 0 : Math.min(this.#layerCount, this.#layers().length);
32979
+ this.#reconcile();
32980
+ }
32981
+ snapshot() {
32982
+ return {
32983
+ delivery: this.#delivery(),
32984
+ layerCount: this.#layerCount,
32985
+ subscribedTracks: [...this.#subs.keys()],
32986
+ repairPackets: this.#repairPackets,
32987
+ repairBytes: this.#repairBytes,
32988
+ staleDropped: this.#staleDropped,
32989
+ lossRate: this.#lossRate
32990
+ };
32991
+ }
32992
+ dispose() {
32993
+ if (this.#disposed) return;
32994
+ this.#closeAll();
32995
+ this.#disposed = true;
32996
+ }
32997
+ #delivery() {
32998
+ return this.#source.deliveryMode === "stream" ? "stream" : "datagram";
32999
+ }
33000
+ #layers() {
33001
+ return this.#source.repairLayers ?? [];
33002
+ }
33003
+ #closeWindow() {
33004
+ const rate = this.#lost / this.#expected;
33005
+ this.#lossRate = rate;
33006
+ this.#expected = 0;
33007
+ this.#lost = 0;
33008
+ const target = this.#targetLayers(rate);
33009
+ if (target > this.#layerCount) {
33010
+ this.#wantFewer = 0;
33011
+ this.#layerCount = target;
33012
+ } else if (target < this.#layerCount) {
33013
+ this.#wantFewer += 1;
33014
+ if (this.#wantFewer >= this.#dropAfterWindows) {
33015
+ this.#wantFewer = 0;
33016
+ this.#layerCount -= 1;
33017
+ }
33018
+ } else {
33019
+ this.#wantFewer = 0;
33020
+ }
33021
+ this.#reconcile();
33022
+ }
33023
+ /** Fewest base layers whose summed P covers margin x the measured loss of K. */
33024
+ #targetLayers(rate) {
33025
+ if (rate <= 0) return 0;
33026
+ const needed = Math.ceil(rate * this.#source.fecK * this.#margin);
33027
+ const layers = this.#layers();
33028
+ let held = 0;
33029
+ for (let n = 0; n < layers.length; n++) {
33030
+ held += layers[n].repairSymbols;
33031
+ if (held >= needed) return n + 1;
33032
+ }
33033
+ return layers.length;
33034
+ }
33035
+ /** Stream delivery never leaves layer count 0, so it never subscribes. */
33036
+ #reconcile() {
33037
+ const want = /* @__PURE__ */ new Map();
33038
+ for (const layer of this.#layers().slice(0, this.#layerCount)) {
33039
+ want.set(layer.repairTrackName, layer.priority);
33040
+ }
33041
+ const overlay = this.#source.repairOverlays?.[0];
33042
+ if (this.#layerCount > 0 && overlay) want.set(overlay.repairTrackName, overlay.priority);
33043
+ for (const [name, entry] of this.#subs) {
33044
+ if (!want.has(name)) {
33045
+ entry.sub.close();
33046
+ this.#subs.delete(name);
33047
+ }
33048
+ }
33049
+ for (const [name, priority] of want) {
33050
+ if (this.#subs.has(name)) continue;
33051
+ const entry = {};
33052
+ entry.sub = this.#opts.subscribe(name, priority, (packet) => this.#onPacket(name, entry, packet));
33053
+ this.#subs.set(name, entry);
33054
+ }
33055
+ }
33056
+ /** Fenced on the entry itself, so a closed subscription is stale even if its track is re-added. */
33057
+ #onPacket(name, entry, packet) {
33058
+ if (this.#disposed || this.#subs.get(name) !== entry) {
33059
+ this.#staleDropped += 1;
33060
+ return;
33061
+ }
33062
+ this.#repairPackets += 1;
33063
+ this.#repairBytes += packet.byteLength;
33064
+ this.#opts.feed(packet, name);
33065
+ }
33066
+ #closeAll() {
33067
+ for (const entry of this.#subs.values()) entry.sub.close();
33068
+ this.#subs.clear();
33069
+ }
33070
+ };
33071
+
31924
33072
  // src/repair-fetch.ts
31925
33073
  var MOQT_PRIORITY_MAX = 255;
33074
+ var WALK_LEAD_MS = 3;
31926
33075
  var SOURCE_MEDIA_PRIORITY_MAX = REPAIR_PRIORITY_MIN - 1;
31927
33076
  function isMoqtPriority(value) {
31928
33077
  return Number.isSafeInteger(value) && value >= 0 && value <= MOQT_PRIORITY_MAX;
@@ -31973,8 +33122,17 @@ function wireRepairFetch(mc, opts) {
31973
33122
  mc.noteRepairScheduling(type, entry.sbn, entry.packetId, entry.catalogTrackName);
31974
33123
  };
31975
33124
  const clearEntry = (entry) => {
31976
- if (entry.range) mc.closeIsoHoles(entry.packetId, entry.catalogTrackName, entry.sbn, entry.range.holeLength);
31977
- else clearRepair(entry.sbn, entry.packetId, entry.catalogTrackName);
33125
+ const { range } = entry;
33126
+ if (range) {
33127
+ atReport(range, () => mc.closeIsoHoles(entry.packetId, entry.catalogTrackName, entry.sbn, range.holeLength));
33128
+ } else {
33129
+ clearRepair(entry.sbn, entry.packetId, entry.catalogTrackName);
33130
+ }
33131
+ };
33132
+ const atReport = (range, work) => {
33133
+ const fec = mc.fecManager;
33134
+ if (range.groups !== null || fec.withReceiptTime === void 0) work();
33135
+ else fec.withReceiptTime(range.reportedAt, work);
31978
33136
  };
31979
33137
  const releaseHold = (entry) => {
31980
33138
  if (entry.range) return;
@@ -32041,21 +33199,42 @@ function wireRepairFetch(mc, opts) {
32041
33199
  const admit = (entry, now) => {
32042
33200
  const budgetMs = entry.deadlineAt - now;
32043
33201
  const rttMs = readRtt();
32044
- const reason = !entry.range && !groupCanComplete(entry.sbn, entry.packetId, entry.catalogTrackName) ? "group-cannot-complete" : rttMs === null ? "rtt-unmeasured" : rttMs < budgetMs ? null : "rtt-exceeds-budget";
33202
+ const reason = refusal(entry, now, rttMs);
32045
33203
  if (reason === null) return rttMs;
32046
33204
  skip(entry, reason, rttMs, budgetMs);
32047
33205
  return null;
32048
33206
  };
33207
+ const refusal = (entry, now, rttMs) => !entry.range && !groupCanComplete(entry.sbn, entry.packetId, entry.catalogTrackName) ? "group-cannot-complete" : rttMs === null ? "rtt-unmeasured" : rttMs < entry.deadlineAt - now ? null : "rtt-exceeds-budget";
32049
33208
  const retire = (entry) => {
32050
33209
  if (entries.get(entry.key) === entry) entries.delete(entry.key);
32051
33210
  if (entry.expiry !== void 0) clearTimeout(entry.expiry);
32052
33211
  };
32053
- const outstandingBelow = (priority) => {
33212
+ const holdsBack = (entry, priority, videoWalks = true) => {
33213
+ if (entry.priority >= priority) return false;
33214
+ const { walk } = entry;
33215
+ if (walk === void 0) return true;
33216
+ if (entry.priority !== 0 || !videoWalks) return false;
33217
+ return entry.running === null || walk.group !== null && !walk.group.controller.signal.aborted;
33218
+ };
33219
+ const outstandingBelow = (priority, videoWalks = true) => {
32054
33220
  for (const entry of entries.values()) {
32055
- if (entry.priority < priority) return true;
33221
+ if (holdsBack(entry, priority, videoWalks)) return true;
32056
33222
  }
32057
33223
  return false;
32058
33224
  };
33225
+ const yieldVideoWalks = (queued) => {
33226
+ const now = performance.now();
33227
+ const rttMs = readRtt();
33228
+ const due = queued.some((entry) => entry.priority !== 0 && entry.walk === void 0 && !outstandingBelow(entry.priority, false) && refusal(entry, now, rttMs) === null);
33229
+ if (!due) return;
33230
+ for (const entry of entries.values()) {
33231
+ const walk = entry.walk;
33232
+ if (entry.priority !== 0 || walk === void 0 || walk.group === null) continue;
33233
+ if (walk.group.controller.signal.aborted) continue;
33234
+ walk.yielded = true;
33235
+ walk.group.controller.abort();
33236
+ }
33237
+ };
32059
33238
  const preemptBestEffort = () => {
32060
33239
  for (const entry of entries.values()) {
32061
33240
  if (entry.priority === 0 || !entry.running) continue;
@@ -32084,7 +33263,8 @@ function wireRepairFetch(mc, opts) {
32084
33263
  }
32085
33264
  const rttMs = admit(entry, now);
32086
33265
  if (rttMs === null) return;
32087
- if (entry.priority === 0) preemptBestEffort();
33266
+ if (entry.priority === 0 && entry.walk === void 0) preemptBestEffort();
33267
+ if (entry.range === void 0) leaveWalkedGroup(entry);
32088
33268
  if (counted) entry.attempts++;
32089
33269
  mc.noteRepairFetchSent?.(entry.priority, entry.sbn, entry.packetId, entry.catalogTrackName, {
32090
33270
  rttMs,
@@ -32106,6 +33286,14 @@ function wireRepairFetch(mc, opts) {
32106
33286
  }
32107
33287
  void execute(entry, run);
32108
33288
  };
33289
+ const leaveWalkedGroup = (block) => {
33290
+ for (const entry of entries.values()) {
33291
+ const group = entry.walk?.group;
33292
+ if (group?.sbn === block.sbn && entry.packetId === block.packetId && entry.catalogTrackName === block.catalogTrackName) {
33293
+ group.controller.abort();
33294
+ }
33295
+ }
33296
+ };
32109
33297
  const dispatch = () => {
32110
33298
  if (disposed) return;
32111
33299
  const now = performance.now();
@@ -32130,6 +33318,7 @@ function wireRepairFetch(mc, opts) {
32130
33318
  for (const entry of queued) {
32131
33319
  if (entry.priority === 0) start(entry);
32132
33320
  }
33321
+ yieldVideoWalks(queued);
32133
33322
  if (outstandingBelow(1)) return;
32134
33323
  for (const entry of queued) {
32135
33324
  if (entry.priority === 1) start(entry);
@@ -32156,6 +33345,16 @@ function wireRepairFetch(mc, opts) {
32156
33345
  }
32157
33346
  return counts.sources + counts.repairGroupLength >= counts.authoredLength;
32158
33347
  };
33348
+ const groupServed = (sbn, packetId, catalogTrackName) => {
33349
+ if (mc.fecManager.blockSymbolCounts === void 0) return false;
33350
+ const counts = symbolCounts(sbn, packetId, catalogTrackName);
33351
+ if (counts === void 0 || counts.authoredLength === void 0) return true;
33352
+ return counts.sources + counts.repairs >= counts.authoredLength || !groupCanComplete(sbn, packetId, catalogTrackName);
33353
+ };
33354
+ const statedEnd = (sbn, packetId, catalogTrackName) => {
33355
+ const length = symbolCounts(sbn, packetId, catalogTrackName)?.authoredLength;
33356
+ return length === void 0 ? void 0 : serial32Add(sbn, length);
33357
+ };
32159
33358
  const finish = (entry, run, fed, error) => {
32160
33359
  if (run.stopped !== null || entry.running !== run || entries.get(entry.key) !== entry) {
32161
33360
  return;
@@ -32200,27 +33399,81 @@ function wireRepairFetch(mc, opts) {
32200
33399
  signal: run.controller.signal,
32201
33400
  subscriberPriority: entry.subscriberPriority
32202
33401
  };
33402
+ const { range } = entry;
33403
+ const feed = (packet) => {
33404
+ if (entry.catalogTrackName === void 0) {
33405
+ mc.processRepairFromMoQ(packet, entry.packetId);
33406
+ } else {
33407
+ mc.processRepairFromMoQ(packet, entry.packetId, entry.catalogTrackName);
33408
+ }
33409
+ };
32203
33410
  try {
32204
- const packets = entry.range ? opts.rangeTransport({
33411
+ const packets = range === void 0 ? opts.transport({ ...common, sbn: entry.sbn }) : entry.walk !== void 0 ? walkHole(entry, entry.walk, range, common) : opts.rangeTransport({
32205
33412
  ...common,
32206
- startGroup: entry.range.startGroup,
32207
- endGroup: entry.range.endGroup,
32208
- lostSsId: entry.range.lostSsId
32209
- }) : opts.transport({ ...common, sbn: entry.sbn });
33413
+ startGroup: range.groups.startGroup,
33414
+ endGroup: range.groups.endGroup,
33415
+ lostSsId: range.lostSsId
33416
+ });
32210
33417
  for await (const packet of packets) {
32211
33418
  if (run.stopped !== null || entry.running !== run) break;
32212
33419
  fed++;
32213
- if (entry.catalogTrackName === void 0) {
32214
- mc.processRepairFromMoQ(packet, entry.packetId);
32215
- } else {
32216
- mc.processRepairFromMoQ(packet, entry.packetId, entry.catalogTrackName);
32217
- }
33420
+ if (range === void 0) feed(packet);
33421
+ else atReport(range, () => feed(packet));
32218
33422
  }
32219
33423
  finish(entry, run, fed, void 0);
32220
33424
  } catch (error) {
32221
33425
  finish(entry, run, fed, error ?? new Error("repair transport rejected"));
32222
33426
  }
32223
33427
  };
33428
+ async function* walkHole(entry, walk, hole, common) {
33429
+ const lostAt = serial32Distance(entry.sbn, hole.lostSsId);
33430
+ for (let first = true; !common.signal.aborted; first = false) {
33431
+ if (!first && entry.priority !== 0 && outstandingBelow(1)) {
33432
+ stop(entry, "preempted");
33433
+ note("repair-preempted", entry);
33434
+ return;
33435
+ }
33436
+ if (!first && entry.priority === 0) {
33437
+ dispatch();
33438
+ if (common.signal.aborted) return;
33439
+ }
33440
+ const sbn = walk.next;
33441
+ let next;
33442
+ if (entries.get(requestKey(sbn, entry.packetId, entry.catalogTrackName))?.running == null) {
33443
+ const rsIds = /* @__PURE__ */ new Set();
33444
+ const group = new AbortController();
33445
+ const cancel = () => group.abort();
33446
+ common.signal.addEventListener("abort", cancel, { once: true });
33447
+ walk.group = { sbn, controller: group };
33448
+ try {
33449
+ for await (const packet of opts.transport({ ...common, sbn, signal: group.signal })) {
33450
+ yield packet;
33451
+ const id = extractRepairFecPayloadId(packet);
33452
+ if (id === null || id.ssStart !== sbn || id.ssbLength === 0) continue;
33453
+ next ??= serial32Add(sbn, id.ssbLength);
33454
+ rsIds.add(id.rsId);
33455
+ if (rsIds.size >= id.rsbLength || groupServed(sbn, entry.packetId, entry.catalogTrackName)) {
33456
+ group.abort();
33457
+ break;
33458
+ }
33459
+ }
33460
+ } catch (error) {
33461
+ if (!group.signal.aborted || common.signal.aborted) throw error;
33462
+ } finally {
33463
+ common.signal.removeEventListener("abort", cancel);
33464
+ walk.group = null;
33465
+ }
33466
+ if (walk.yielded) {
33467
+ walk.yielded = false;
33468
+ continue;
33469
+ }
33470
+ }
33471
+ next ??= statedEnd(sbn, entry.packetId, entry.catalogTrackName);
33472
+ if (next === void 0) return;
33473
+ if (serial32Distance(entry.sbn, next) > lostAt) return;
33474
+ walk.next = next;
33475
+ }
33476
+ }
32224
33477
  const removeResetObserver = mc.addTrackResetObserver((packetId, catalogTrackName) => {
32225
33478
  for (const entry of [...entries.values()]) {
32226
33479
  if (entry.packetId !== packetId || entry.catalogTrackName !== catalogTrackName) continue;
@@ -32416,7 +33669,37 @@ function wireRepairFetch(mc, opts) {
32416
33669
  throw error;
32417
33670
  }
32418
33671
  };
32419
- if (opts.rangeTransport !== void 0) {
33672
+ const trackKind = (packetId) => {
33673
+ try {
33674
+ return mc.fecManager.trackKindFor?.(packetId);
33675
+ } catch {
33676
+ return void 0;
33677
+ }
33678
+ };
33679
+ const walkDeadlineAt = (packetId, now, deadlineAt) => {
33680
+ let repairDeadlineMs;
33681
+ try {
33682
+ repairDeadlineMs = mc.fecManager.repairDeadlineMsForPacket?.(packetId);
33683
+ } catch {
33684
+ repairDeadlineMs = void 0;
33685
+ }
33686
+ if (repairDeadlineMs === void 0 || !Number.isFinite(repairDeadlineMs)) return deadlineAt;
33687
+ return Math.min(deadlineAt, now + repairDeadlineMs - WALK_LEAD_MS);
33688
+ };
33689
+ const onHoleExpired = (hole) => {
33690
+ if (disposed) return false;
33691
+ const entry = [...entries.values()].find((candidate) => candidate.walk !== void 0 && candidate.packetId === hole.packetId && candidate.catalogTrackName === hole.catalogTrackName && serial32Distance(candidate.sbn, hole.holeStart) < candidate.range.holeLength);
33692
+ if (entry === void 0) return false;
33693
+ queueMicrotask(() => {
33694
+ if (entries.get(entry.key) !== entry) return;
33695
+ retire(entry);
33696
+ stop(entry, "expired");
33697
+ clearEntry(entry);
33698
+ dispatch();
33699
+ });
33700
+ return true;
33701
+ };
33702
+ if (mc.onUndelimitedLoss !== void 0 && mc.closeIsoHoles !== void 0) {
32420
33703
  mc.onUndelimitedLoss((loss) => {
32421
33704
  const { packetId, catalogTrackName, holeStart, holeLength } = loss;
32422
33705
  const close = () => mc.closeIsoHoles(packetId, catalogTrackName, holeStart, holeLength);
@@ -32424,9 +33707,11 @@ function wireRepairFetch(mc, opts) {
32424
33707
  const blockProfile = blockProfileOf(packetId);
32425
33708
  const target = opts.resolveTarget(packetId, catalogTrackName);
32426
33709
  if (!target) return close();
32427
- const priority = repairPriorityForClass("unknown");
32428
- const range = rangeFor(loss, target.repairTrack);
32429
- if (range === null) {
33710
+ const walked = opts.rangeTransport === void 0;
33711
+ const holeClass = walked && trackKind(packetId) === "audio" ? "audio" : "unknown";
33712
+ const priority = repairPriorityForClass(holeClass);
33713
+ const groups = walked ? null : rangeFor(loss, target.repairTrack);
33714
+ if (!walked && groups === null) {
32430
33715
  close();
32431
33716
  mc.noteRepairSkipped("range-no-anchor", { rttMs: readRtt(), budgetMs: 0, priority }, holeStart, packetId, catalogTrackName);
32432
33717
  return;
@@ -32437,8 +33722,10 @@ function wireRepairFetch(mc, opts) {
32437
33722
  packetId,
32438
33723
  ...catalogTrackName === void 0 ? {} : { catalogTrackName }
32439
33724
  };
32440
- const wire = wirePriority(identity, "unknown");
32441
- const deadline = presentationDeadlineAt(holeStart, packetId, catalogTrackName, performance.now(), "unknown");
33725
+ const wire = wirePriority(identity, holeClass);
33726
+ if (wire === null) return close();
33727
+ const now = performance.now();
33728
+ const deadline = presentationDeadlineAt(holeStart, packetId, catalogTrackName, now, holeClass);
32442
33729
  if (deadline === null) {
32443
33730
  close();
32444
33731
  mc.noteRepairSkipped(
@@ -32450,6 +33737,7 @@ function wireRepairFetch(mc, opts) {
32450
33737
  );
32451
33738
  return;
32452
33739
  }
33740
+ const deadlineAt = walked ? walkDeadlineAt(packetId, now, deadline.deadlineAt) : deadline.deadlineAt;
32453
33741
  const entry = {
32454
33742
  key: JSON.stringify(["range", packetId, catalogTrackName ?? null, holeStart]),
32455
33743
  ...identity,
@@ -32457,15 +33745,16 @@ function wireRepairFetch(mc, opts) {
32457
33745
  blockProfile,
32458
33746
  priority,
32459
33747
  subscriberPriority: wire.subscriberPriority,
32460
- deadlineAt: deadline.deadlineAt,
32461
- playoutDeadlineAt: deadline.deadlineAt - deadline.extensionMs,
33748
+ deadlineAt,
33749
+ playoutDeadlineAt: deadlineAt - deadline.extensionMs,
32462
33750
  anchorAt: deadline.anchorAt,
32463
33751
  delayMs: deadline.delayMs,
32464
33752
  bound: deadline.bound,
32465
33753
  order: enqueueOrder++,
32466
33754
  attempts: 0,
32467
33755
  running: null,
32468
- range: { ...range, lostSsId: loss.lostSsId, holeLength }
33756
+ range: { lostSsId: loss.lostSsId, holeLength, groups, reportedAt: now },
33757
+ ...walked ? { walk: { next: holeStart, group: null, yielded: false } } : {}
32469
33758
  };
32470
33759
  const previous = entries.get(entry.key);
32471
33760
  if (previous) {
@@ -32475,7 +33764,7 @@ function wireRepairFetch(mc, opts) {
32475
33764
  entries.set(entry.key, entry);
32476
33765
  entry.expiry = setTimeout(dispatch, Math.max(0, entry.deadlineAt - performance.now()) + 1);
32477
33766
  dispatch();
32478
- });
33767
+ }, onHoleExpired);
32479
33768
  }
32480
33769
  const removeRapObserver = mc.addRapCompleteObserver?.((packetId) => {
32481
33770
  queueMicrotask(() => {
@@ -32566,6 +33855,7 @@ export {
32566
33855
  REPAIR_LIFECYCLE_LOG_PREFIX,
32567
33856
  REPAIR_PRIORITY_MAX,
32568
33857
  REPAIR_PRIORITY_MIN,
33858
+ RepairLayerSubscriptions,
32569
33859
  SOURCE_MEDIA_PRIORITY_MAX,
32570
33860
  SampleCombineError,
32571
33861
  SampleCombiner,
@@ -32590,6 +33880,7 @@ export {
32590
33880
  catalogGroupDurationMs,
32591
33881
  catalogKeyframeCadenceMs,
32592
33882
  catalogRepairTarget,
33883
+ catalogTrackDeliveryMode,
32593
33884
  catalogTrackFecUnavailable,
32594
33885
  classifyH264Keyframe,
32595
33886
  classifyHevcKeyframe,
@@ -32610,6 +33901,7 @@ export {
32610
33901
  detectH264Keyframe,
32611
33902
  detectHevcKeyframe,
32612
33903
  driftCheck,
33904
+ encodeCmcdRecord,
32613
33905
  encodeDataSegment,
32614
33906
  encodeFecSourceInfo,
32615
33907
  encodeLocTimestamp,