@blockcast/mmt-container 0.4.1-main.a37bc86bdad2 → 0.4.1-main.e1b4700e8078

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.
@@ -15441,6 +15441,18 @@ var FECManager = class _FECManager {
15441
15441
  blockAnchorAtMs(sbn, packetId, catalogTrackName) {
15442
15442
  return this.#blockAnchorsMs.get(this.#blockKey(sbn, packetId, catalogTrackName));
15443
15443
  }
15444
+ /**
15445
+ * How long ago (ms) the oldest open block of the stream, one with an FEC
15446
+ * deadline, took its first accepted symbol; undefined with none open.
15447
+ */
15448
+ oldestOpenBlockAgeMs(packetId, catalogTrackName) {
15449
+ let oldest = Infinity;
15450
+ for (const key of this.#activeBlocks.keys()) {
15451
+ if (!this.#belongsToStream(key, packetId, catalogTrackName)) continue;
15452
+ oldest = Math.min(oldest, this.#blockAnchorsMs.get(key) ?? Infinity);
15453
+ }
15454
+ return oldest === Infinity ? void 0 : performance.now() - oldest;
15455
+ }
15444
15456
  /** Absolute fail deadline of a still-open block, or undefined. */
15445
15457
  blockDeadlineAtMs(sbn, packetId, catalogTrackName) {
15446
15458
  return this.#activeBlockDeadlinesMs.get(this.#blockKey(sbn, packetId, catalogTrackName));
@@ -19415,8 +19427,13 @@ var MmtpRouter = class {
19415
19427
  #fecMappersByPacketId = /* @__PURE__ */ new Map();
19416
19428
  /** Protected flows: one 7.2.7 receiver each, and the ingress its datagrams arrive with. */
19417
19429
  #provenance = /* @__PURE__ */ new Map();
19430
+ #onFlowProtected = null;
19431
+ #unadmittedListedDrops = 0;
19418
19432
  /** The ingress a held datagram arrived with; the receiver returns the same bytes on release. */
19419
19433
  #heldIngress = /* @__PURE__ */ new WeakMap();
19434
+ #unicastRepair = false;
19435
+ /** How to route a held unicast (MoQ) datagram once a record admits it. */
19436
+ #heldReplay = /* @__PURE__ */ new WeakMap();
19420
19437
  /** The flow whose receiver admitted a stream's source datagrams, by packetId then catalog track name: recovered packets answer to it. */
19421
19438
  #provenanceStreamFlow = /* @__PURE__ */ new Map();
19422
19439
  #unregisteredFlowDrops = 0;
@@ -20098,6 +20115,25 @@ var MmtpRouter = class {
20098
20115
  registerProvenanceFlow(ingress, receiver) {
20099
20116
  this.#provenance.get(ingress.flow)?.receiver.release();
20100
20117
  this.#provenance.set(ingress.flow, { receiver, ingress });
20118
+ receiver.keepPendingAgeMs = () => this.#oldestOpenBlockAgeMs(ingress.flow);
20119
+ }
20120
+ /**
20121
+ * Declare that this receiver can request unicast repair (a repair FETCH
20122
+ * scheduler is wired): a protected flow then keeps the pending entries of
20123
+ * its open blocks for the repair to consume (7.2.7 step 2).
20124
+ */
20125
+ setUnicastRepair(enabled) {
20126
+ this.#unicastRepair = enabled;
20127
+ }
20128
+ #oldestOpenBlockAgeMs(flow) {
20129
+ const entry = this.#provenance.get(flow);
20130
+ if (!this.#unicastRepair || !entry) return void 0;
20131
+ let age;
20132
+ for (const { name, packetId } of entry.ingress.tracks) {
20133
+ const a = name ? this.#fecManager?.oldestOpenBlockAgeMs?.(packetId, name) : void 0;
20134
+ if (a !== void 0 && (age === void 0 || a > age)) age = a;
20135
+ }
20136
+ return age;
20101
20137
  }
20102
20138
  /**
20103
20139
  * Replace a registered flow's ingress (a rendition switch staged, committed
@@ -20105,7 +20141,88 @@ var MmtpRouter = class {
20105
20141
  */
20106
20142
  updateProvenanceFlowIngress(ingress) {
20107
20143
  const entry = this.#provenance.get(ingress.flow);
20108
- if (entry) this.#provenance.set(ingress.flow, { receiver: entry.receiver, ingress });
20144
+ if (!entry) return;
20145
+ this.#provenance.set(ingress.flow, { ...entry, ingress });
20146
+ if (entry.protected) {
20147
+ for (const { name, packetId } of ingress.tracks) {
20148
+ if (name && !entry.ingress.tracks.some((t) => t.name === name && t.packetId === packetId)) {
20149
+ this.#fecManager?.resetTrack?.(packetId, name);
20150
+ }
20151
+ }
20152
+ }
20153
+ }
20154
+ /**
20155
+ * Called once per protection of a flow: at the admission that makes this
20156
+ * router's consumers protected under it (7.2.7), after their unadmitted
20157
+ * holdings of its listed tracks are discarded. Again after an unprotect.
20158
+ */
20159
+ onProvenanceFlowProtected(cb) {
20160
+ this.#onFlowProtected = cb;
20161
+ }
20162
+ /**
20163
+ * The receiver ceased to hold the flow's endpoint on failover (7.2.7): drop
20164
+ * its state and hold queues (no expiry counted) and un-protect its tracks,
20165
+ * which then take MoQ again. The registration stays, so multicast resuming
20166
+ * protects the flow anew at its next admission.
20167
+ */
20168
+ unprotectProvenanceFlow(flow) {
20169
+ const entry = this.#provenance.get(flow);
20170
+ if (!entry) return;
20171
+ entry.receiver.release();
20172
+ entry.protected = false;
20173
+ }
20174
+ /** MoQ packets of a track a protected flow lists, dropped on its unicast path: duplicate, overflow, or listed by more than one flow. */
20175
+ get unadmittedListedDropCount() {
20176
+ return this.#unadmittedListedDrops;
20177
+ }
20178
+ /**
20179
+ * The admission that makes this router protected under `flow` (7.2.7):
20180
+ * its FEC instances discard what they hold of the flow's listed tracks so
20181
+ * no unadmitted symbol stands for an admitted one, then notify. Before it
20182
+ * nothing of the flow was admitted, so every such symbol is unadmitted.
20183
+ * The playback parse ledger is kept: it records MoQ copies already played,
20184
+ * not holdings, and clearing it re-emits that media (audio twice).
20185
+ * The FEC reset is the whole 7.2.7 discard here: no keyframe-overlay
20186
+ * instance exists to hold anything (repair-layers.ts is the catalog
20187
+ * contract only, no decoder), and packet_id 0 is never a listed track
20188
+ * (`ingressHasPacketId`), so nothing of it is held for the flow.
20189
+ */
20190
+ #noteFlowAdmitted(flow) {
20191
+ const entry = this.#provenance.get(flow);
20192
+ if (!entry || entry.protected) return;
20193
+ entry.protected = true;
20194
+ for (const { name, packetId } of entry.ingress.tracks) {
20195
+ if (name) this.#fecManager?.resetTrack?.(packetId, name);
20196
+ }
20197
+ this.#onFlowProtected?.(flow);
20198
+ }
20199
+ /**
20200
+ * Admit a MoQ datagram of `packetId` on the unicast path of the protected
20201
+ * flow that lists it (7.2.7). `unlisted`: no protected flow lists it, so it
20202
+ * is read as before. A held datagram is routed by `replay` when a record
20203
+ * admits it; one more than one protected flow lists is dropped. So is an
20204
+ * overlap copy: it never enters FEC accounting, so admitting it would leave
20205
+ * its multicast twin a duplicate and the block one symbol short.
20206
+ */
20207
+ #admitUnicast(packetId, packet, replay, overlapCopy = false) {
20208
+ let receiver;
20209
+ for (const entry of this.#provenance.values()) {
20210
+ if (!entry.protected || !ingressHasPacketId(entry.ingress, packetId)) continue;
20211
+ if (receiver || overlapCopy) {
20212
+ this.#unadmittedListedDrops++;
20213
+ return "dropped";
20214
+ }
20215
+ receiver = entry.receiver;
20216
+ }
20217
+ if (!receiver) return "unlisted";
20218
+ const { verdict } = receiver.onDatagram(packet, "unicast");
20219
+ if (verdict === "admitted") return "admitted";
20220
+ if (verdict === "held") {
20221
+ this.#heldReplay.set(packet, replay);
20222
+ return "held";
20223
+ }
20224
+ this.#unadmittedListedDrops++;
20225
+ return "dropped";
20109
20226
  }
20110
20227
  /**
20111
20228
  * Feed one bc-provenance record of `flow`: a datagram from `manifestPort`
@@ -20117,9 +20234,12 @@ var MmtpRouter = class {
20117
20234
  if (!entry || this.#disposed) return "unregistered";
20118
20235
  const result = entry.receiver.onRecord(record, path);
20119
20236
  if (result.admitted.length > 0) {
20237
+ this.#noteFlowAdmitted(flow);
20120
20238
  this.#withDeferredFecPokes(() => {
20121
20239
  for (const datagram of result.admitted) {
20122
- this.#processMMTP(datagram, "multicast", true, this.#heldIngress.get(datagram) ?? entry.ingress, false, true);
20240
+ const replay = this.#heldReplay.get(datagram);
20241
+ if (replay) replay();
20242
+ else this.#processMMTP(datagram, "multicast", true, this.#heldIngress.get(datagram) ?? entry.ingress, false, true);
20123
20243
  }
20124
20244
  });
20125
20245
  }
@@ -20206,7 +20326,7 @@ var MmtpRouter = class {
20206
20326
  this.#feedRepairPacket(packet, ownerPacketIdHint, catalogTrackName, repairTrackName);
20207
20327
  });
20208
20328
  }
20209
- #feedRepairPacket(packet, ownerPacketIdHint, catalogTrackName, repairTrackName) {
20329
+ #feedRepairPacket(packet, ownerPacketIdHint, catalogTrackName, repairTrackName, admitted = false) {
20210
20330
  let header;
20211
20331
  try {
20212
20332
  header = parseMMTPHeader(packet);
@@ -20223,6 +20343,10 @@ var MmtpRouter = class {
20223
20343
  `[MmtpRouter] Dropping MoQ repair packet pid=${repairPacketId}: owner hint is required`
20224
20344
  );
20225
20345
  }
20346
+ if (ownerPacketId != null && !admitted) {
20347
+ const admit = this.#admitUnicast(ownerPacketId, packet, () => this.#feedRepairPacket(packet, ownerPacketIdHint, catalogTrackName, repairTrackName, true));
20348
+ if (admit === "held" || admit === "dropped") return;
20349
+ }
20226
20350
  if (ownerPacketId != null && this.#fecManager) {
20227
20351
  this.#moqRepairPidCounts[repairPacketId] = (this.#moqRepairPidCounts[repairPacketId] || 0) + 1;
20228
20352
  this.#moqRepairOwnerCounts[ownerPacketId] = (this.#moqRepairOwnerCounts[ownerPacketId] || 0) + 1;
@@ -21112,10 +21236,19 @@ var MmtpRouter = class {
21112
21236
  return;
21113
21237
  }
21114
21238
  const { verdict } = protectedFlow.receiver.onDatagram(packet, "multicast");
21115
- if (verdict === "admitted") this.#processMMTP(packet, source, true, ingress, overlapCopy);
21116
- else if (verdict === "held") this.#heldIngress.set(packet, ingress);
21239
+ if (verdict === "admitted") {
21240
+ this.#noteFlowAdmitted(ingress.flow);
21241
+ this.#processMMTP(packet, source, true, ingress, overlapCopy);
21242
+ } else if (verdict === "held") this.#heldIngress.set(packet, ingress);
21117
21243
  return;
21118
21244
  }
21245
+ if (source === "moq" && !manifestAdmitted) {
21246
+ const admit = this.#admitUnicast(info.packetId, packet, () => this.#processMMTP(packet, source, true, ingress, overlapCopy, true), overlapCopy);
21247
+ if (admit !== "unlisted") {
21248
+ if (admit === "admitted") this.#processMMTP(packet, source, true, ingress, overlapCopy);
21249
+ return;
21250
+ }
21251
+ }
21119
21252
  if (info.isRepair) {
21120
21253
  const repair = parseMoqRepairFrameWithHeader(packet, parsed.header);
21121
21254
  if (!repair) return;
@@ -25562,6 +25695,14 @@ var BcProvenanceV2Receiver = class {
25562
25695
  widenMs;
25563
25696
  /** Datagrams that left a hold queue at expiry, per path: the loss signal for failover. */
25564
25697
  expired = { multicast: 0, unicast: 0 };
25698
+ /**
25699
+ * Unicast repair (Section 7.2.7 step 2): on a receiver that can request it, how
25700
+ * long ago (ms) the first packet admitted into the oldest open block with an FEC
25701
+ * deadline was admitted; undefined with none open. A pending entry still live
25702
+ * then is kept instead of expiring, and is released with the block, under the
25703
+ * open-block bound of [MOQ-FEC] Section 9.
25704
+ */
25705
+ keepPendingAgeMs = null;
25565
25706
  cfg;
25566
25707
  now;
25567
25708
  verifySig;
@@ -25922,10 +26063,17 @@ var BcProvenanceV2Receiver = class {
25922
26063
  return false;
25923
26064
  });
25924
26065
  }
26066
+ let keepFrom = null;
25925
26067
  for (const [d, list] of this.pending) {
25926
26068
  const live = list.filter((p) => {
25927
26069
  if (p.expiry > now)
25928
26070
  return true;
26071
+ if (keepFrom === null) {
26072
+ const age = this.keepPendingAgeMs?.();
26073
+ keepFrom = age === void 0 ? void 0 : now - age;
26074
+ }
26075
+ if (keepFrom !== void 0 && keepFrom !== null && p.expiry > keepFrom)
26076
+ return true;
25929
26077
  p.pair.pending = void 0;
25930
26078
  return false;
25931
26079
  });
@@ -26672,6 +26820,8 @@ var MulticastManager = class {
26672
26820
  #transport;
26673
26821
  /** The protected flow of the current join and its manifestPort subscription. */
26674
26822
  #provenanceJoin = null;
26823
+ #onFailback = null;
26824
+ #failbackHooked = false;
26675
26825
  /** Live joinRepairEndpoints() releases → the repair owners they registered,
26676
26826
  * re-applied after configureMultiTrack() clears catalog-derived owners. */
26677
26827
  #repairReleases = /* @__PURE__ */ new Map();
@@ -26863,6 +27013,10 @@ var MulticastManager = class {
26863
27013
  get router() {
26864
27014
  return this.#router;
26865
27015
  }
27016
+ /** Repair FETCH wiring (wireRepairFetch): this manager can request unicast repair. */
27017
+ setUnicastRepair(enabled) {
27018
+ this.#router.setUnicastRepair(enabled);
27019
+ }
26866
27020
  get transport() {
26867
27021
  return this.#transport;
26868
27022
  }
@@ -27273,8 +27427,35 @@ var MulticastManager = class {
27273
27427
  onTakeover(cb) {
27274
27428
  this.#transport.onTakeover(cb);
27275
27429
  }
27430
+ /** Failback un-protects the join's flow (7.2.7 failover), so the fallback's MoQ packets are read. */
27276
27431
  onFailback(cb) {
27277
- this.#transport.onFailback(cb);
27432
+ this.#onFailback = cb;
27433
+ this.#hookFailback();
27434
+ }
27435
+ /**
27436
+ * Install the manager's own failback listener on the media transport, which
27437
+ * arms its silence watchdog: a protected join un-protects on failover even
27438
+ * when no player listens, else its tracks' MoQ copies stay dropped.
27439
+ */
27440
+ #hookFailback() {
27441
+ if (this.#failbackHooked) return;
27442
+ this.#failbackHooked = true;
27443
+ this.#transport.onFailback((reason) => {
27444
+ const joined = this.#provenanceJoin;
27445
+ if (joined !== null) this.#router.unprotectProvenanceFlow(joined.flow);
27446
+ this.#onFailback?.(reason);
27447
+ });
27448
+ }
27449
+ /**
27450
+ * A protected join's handoff (draft -01 7.2.7), before `onTakeover`: its
27451
+ * first admitted datagram. MoQ copies of the flow's tracks the router still
27452
+ * held unadmitted are discarded, and later ones are dropped. Fires again
27453
+ * after a failback once multicast resumes.
27454
+ */
27455
+ onProtected(cb) {
27456
+ this.#router.onProvenanceFlowProtected(cb && ((flow) => {
27457
+ if (this.#provenanceJoin?.flow === flow) cb();
27458
+ }));
27278
27459
  }
27279
27460
  /** See MulticastTransport.onDataPlaneLive: the join's first accepted packet. */
27280
27461
  onDataPlaneLive(cb) {
@@ -27375,6 +27556,7 @@ var MulticastManager = class {
27375
27556
  manifestTimerMs: flow.manifestTimerMs
27376
27557
  });
27377
27558
  this.#router.registerProvenanceFlow(ingress, receiver);
27559
+ this.#hookFailback();
27378
27560
  const manifest = new MulticastTransport((record) => {
27379
27561
  if (this.#provenanceJoin?.manifest === manifest) {
27380
27562
  this.#router.feedProvenanceRecord(ingress.flow, record, "multicast");
@@ -30454,6 +30636,7 @@ function wireRepairFetch(mc, opts) {
30454
30636
  if (opts.rangeTransport !== void 0 && (typeof opts.repairEpochReference !== "function" || !mc.onUndelimitedLoss || !mc.closeIsoHoles)) {
30455
30637
  throw new Error("[RepairFetch] rangeTransport needs repairEpochReference and a host that reports undelimited losses");
30456
30638
  }
30639
+ mc.setUnicastRepair?.(true);
30457
30640
  const entries = /* @__PURE__ */ new Map();
30458
30641
  const warnedNoTarget = /* @__PURE__ */ new Set();
30459
30642
  let disposed = false;
@@ -30940,6 +31123,7 @@ function wireRepairFetch(mc, opts) {
30940
31123
  }
30941
31124
  return () => {
30942
31125
  disposed = true;
31126
+ mc.setUnicastRepair?.(false);
30943
31127
  removeResetObserver();
30944
31128
  removeLifecycleObserver();
30945
31129
  for (const entry of [...entries.values()]) {