@blockcast/mmt-container 0.4.0 → 0.4.1-main.22841c1f0a2d

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 (220) hide show
  1. package/README.md +37 -21
  2. package/dist/align.d.ts +24 -18
  3. package/dist/align.d.ts.map +1 -1
  4. package/dist/align.js +23 -4
  5. package/dist/align.js.map +2 -2
  6. package/dist/browser-runtime.js +2037 -881
  7. package/dist/browser-runtime.js.map +4 -4
  8. package/dist/carp-catalog.d.ts +76 -39
  9. package/dist/carp-catalog.d.ts.map +1 -1
  10. package/dist/carp-catalog.js +321 -205
  11. package/dist/carp-catalog.js.map +3 -3
  12. package/dist/catalog-fec-params.d.ts +12 -4
  13. package/dist/catalog-fec-params.d.ts.map +1 -1
  14. package/dist/catalog-fec-params.js +40 -17
  15. package/dist/catalog-fec-params.js.map +3 -3
  16. package/dist/catalog-network-source.d.ts +28 -7
  17. package/dist/catalog-network-source.d.ts.map +1 -1
  18. package/dist/catalog-network-source.js +21 -1
  19. package/dist/catalog-network-source.js.map +2 -2
  20. package/dist/catalog-validator.d.ts +4 -3
  21. package/dist/catalog-validator.d.ts.map +1 -1
  22. package/dist/catalog-validator.js +0 -29
  23. package/dist/catalog-validator.js.map +3 -3
  24. package/dist/cmcd-producer.d.ts +13 -1
  25. package/dist/cmcd-producer.d.ts.map +1 -1
  26. package/dist/cmcd-producer.js +14 -0
  27. package/dist/cmcd-producer.js.map +2 -2
  28. package/dist/delivery-catalog.js +98 -234
  29. package/dist/delivery-catalog.js.map +4 -4
  30. package/dist/fec-manager.d.ts +108 -1
  31. package/dist/fec-manager.d.ts.map +1 -1
  32. package/dist/fec-manager.js +452 -66
  33. package/dist/fec-manager.js.map +4 -4
  34. package/dist/fec-state.d.ts +2 -1
  35. package/dist/fec-state.d.ts.map +1 -1
  36. package/dist/fec-state.js +18 -3
  37. package/dist/fec-state.js.map +3 -3
  38. package/dist/fec-worker-setup.d.ts +10 -0
  39. package/dist/fec-worker-setup.d.ts.map +1 -1
  40. package/dist/fec-worker-setup.js +15 -4
  41. package/dist/fec-worker-setup.js.map +2 -2
  42. package/dist/held-layer-admission.d.ts +58 -0
  43. package/dist/held-layer-admission.d.ts.map +1 -0
  44. package/dist/held-layer-admission.js +105 -0
  45. package/dist/held-layer-admission.js.map +7 -0
  46. package/dist/index.d.ts +5 -5
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +8 -8
  49. package/dist/index.js.map +2 -2
  50. package/dist/iso-block-locator.d.ts +39 -1
  51. package/dist/iso-block-locator.d.ts.map +1 -1
  52. package/dist/iso-block-locator.js +109 -17
  53. package/dist/iso-block-locator.js.map +3 -3
  54. package/dist/mfu-reassembler.js +21 -13
  55. package/dist/mfu-reassembler.js.map +2 -2
  56. package/dist/mmtp-container-parser.d.ts.map +1 -1
  57. package/dist/mmtp-container-parser.js +93 -22
  58. package/dist/mmtp-container-parser.js.map +2 -2
  59. package/dist/mmtp-container-parser.no-modules.js +3677 -1213
  60. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  61. package/dist/mmtp-decode-pipeline.js +93 -22
  62. package/dist/mmtp-decode-pipeline.js.map +2 -2
  63. package/dist/mmtp-fec-session.d.ts +192 -0
  64. package/dist/mmtp-fec-session.d.ts.map +1 -0
  65. package/dist/mmtp-fec-session.js +640 -0
  66. package/dist/mmtp-fec-session.js.map +7 -0
  67. package/dist/mmtp-router.d.ts +69 -32
  68. package/dist/mmtp-router.d.ts.map +1 -1
  69. package/dist/mmtp-router.js +296 -192
  70. package/dist/mmtp-router.js.map +2 -2
  71. package/dist/multicast-auth-catalog.d.ts +86 -92
  72. package/dist/multicast-auth-catalog.d.ts.map +1 -1
  73. package/dist/multicast-auth-catalog.js +313 -124
  74. package/dist/multicast-auth-catalog.js.map +3 -3
  75. package/dist/multicast-input.d.ts +31 -8
  76. package/dist/multicast-input.d.ts.map +1 -1
  77. package/dist/multicast-input.js +299 -35
  78. package/dist/multicast-input.js.map +3 -3
  79. package/dist/multicast-manager.d.ts +97 -11
  80. package/dist/multicast-manager.d.ts.map +1 -1
  81. package/dist/multicast-manager.js +1022 -527
  82. package/dist/multicast-manager.js.map +4 -4
  83. package/dist/multicast-transport.d.ts +12 -2
  84. package/dist/multicast-transport.d.ts.map +1 -1
  85. package/dist/multicast-transport.js +108 -238
  86. package/dist/multicast-transport.js.map +4 -4
  87. package/dist/multicast-unwrap.js +21 -13
  88. package/dist/multicast-unwrap.js.map +2 -2
  89. package/dist/no-modules.d.ts +5 -3
  90. package/dist/no-modules.d.ts.map +1 -1
  91. package/dist/no-modules.js +3625 -1194
  92. package/dist/no-modules.js.map +4 -4
  93. package/dist/phase14-catalog-override.d.ts +1 -1
  94. package/dist/phase14-catalog-override.d.ts.map +1 -1
  95. package/dist/phase14-catalog-override.js +2 -2
  96. package/dist/phase14-catalog-override.js.map +2 -2
  97. package/dist/repair-fetch.d.ts +81 -6
  98. package/dist/repair-fetch.d.ts.map +1 -1
  99. package/dist/repair-fetch.js +156 -31
  100. package/dist/repair-fetch.js.map +3 -3
  101. package/dist/repair-layers.d.ts +2 -0
  102. package/dist/repair-layers.d.ts.map +1 -1
  103. package/dist/repair-layers.js +19 -2
  104. package/dist/repair-layers.js.map +2 -2
  105. package/dist/sample-combiner.d.ts +7 -1
  106. package/dist/sample-combiner.d.ts.map +1 -1
  107. package/dist/sample-combiner.js +14 -2
  108. package/dist/sample-combiner.js.map +2 -2
  109. package/dist/signaling.d.ts +2 -2
  110. package/dist/signaling.d.ts.map +1 -1
  111. package/dist/signaling.js +3 -3
  112. package/dist/signaling.js.map +2 -2
  113. package/dist/switching-set-alignment.d.ts +1 -1
  114. package/dist/switching-set-alignment.js.map +1 -1
  115. package/dist/timestamp-codec.js +21 -13
  116. package/dist/timestamp-codec.js.map +2 -2
  117. package/dist/wasm-mfu-reassembler.js.map +1 -1
  118. package/dist/wasm-mmtp-container-parser.js +93 -22
  119. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  120. package/package.json +6 -18
  121. package/schemas/catalog.schema.json +133 -38
  122. package/src/align.ts +52 -21
  123. package/src/carp-catalog.ts +188 -68
  124. package/src/catalog-fec-params.ts +28 -3
  125. package/src/catalog-network-source.ts +49 -7
  126. package/src/catalog-validator.ts +4 -11
  127. package/src/cmcd-producer.ts +23 -1
  128. package/src/fec-manager.ts +366 -63
  129. package/src/fec-state.ts +11 -4
  130. package/src/fec-worker-setup.ts +30 -2
  131. package/src/held-layer-admission.ts +161 -0
  132. package/src/index.ts +32 -19
  133. package/src/iso-block-locator.ts +110 -7
  134. package/src/mmtp-container-parser.ts +75 -7
  135. package/src/mmtp-fec-session.ts +606 -0
  136. package/src/mmtp-router.ts +301 -240
  137. package/src/multicast-auth-catalog.ts +134 -238
  138. package/src/multicast-input.ts +148 -124
  139. package/src/multicast-manager.ts +271 -40
  140. package/src/multicast-transport.ts +17 -3
  141. package/src/no-modules.ts +8 -7
  142. package/src/phase14-catalog-override.ts +3 -3
  143. package/src/repair-fetch.ts +283 -39
  144. package/src/repair-layers.ts +22 -2
  145. package/src/sample-combiner.ts +15 -2
  146. package/src/signaling.ts +2 -2
  147. package/src/switching-set-alignment.ts +1 -1
  148. package/test-vectors/align.json +108 -32
  149. package/test-vectors/bc-provenance-v2.json +1526 -0
  150. package/test-vectors/catalog/README.md +71 -4
  151. package/test-vectors/catalog/negative/fec-block-profile-missing.json +1 -1
  152. package/test-vectors/catalog/negative/fec-block-profile-retired.json +1 -1
  153. package/test-vectors/catalog/negative/fec-block-profile-unknown.json +1 -1
  154. package/test-vectors/catalog/negative/fec-iso-symbol-size-below-floor.json +28 -0
  155. package/test-vectors/catalog/negative/fec-reed-solomon-iso-symbol-size-13.json +28 -0
  156. package/test-vectors/catalog/negative/fec-reed-solomon-k-plus-p-over-255.json +39 -0
  157. package/test-vectors/catalog/negative/fec-reed-solomon-overlay-over-255.json +40 -0
  158. package/test-vectors/catalog/negative/fec-repair-overlay-source-symbols-zero.json +43 -0
  159. package/test-vectors/catalog/negative/fec-repair-symbols-zero.json +28 -0
  160. package/test-vectors/catalog/negative/fec-source-symbols-zero.json +28 -0
  161. package/test-vectors/catalog/negative/fec-symbol-size-zero.json +28 -0
  162. package/test-vectors/catalog/negative/init-data-inline-without-data.json +22 -0
  163. package/test-vectors/catalog/negative/mmtp-mode-mpu.json +15 -0
  164. package/test-vectors/catalog/negative/multicast-amt-dns-without-relay.json +37 -0
  165. package/test-vectors/catalog/negative/multicast-amt-driad-with-relay.json +38 -0
  166. package/test-vectors/catalog/negative/multicast-auth-asm-missing-sender-address.json +77 -0
  167. package/test-vectors/catalog/negative/multicast-auth-duplicate-first-epoch.json +56 -0
  168. package/test-vectors/catalog/negative/multicast-auth-duplicate-kid.json +56 -0
  169. package/test-vectors/catalog/negative/multicast-auth-endpoint-auth-without-object-auth.json +38 -0
  170. package/test-vectors/catalog/negative/multicast-auth-endpoint-missing-auth.json +49 -0
  171. package/test-vectors/catalog/negative/multicast-auth-endpoint-missing-bandwidth.json +55 -0
  172. package/test-vectors/catalog/negative/multicast-auth-manifest-port-equals-port.json +56 -0
  173. package/test-vectors/catalog/negative/multicast-auth-manifest-timer-out-of-range.json +56 -0
  174. package/test-vectors/catalog/negative/multicast-auth-max-auth-delay-above-max.json +56 -0
  175. package/test-vectors/catalog/negative/multicast-auth-max-auth-delay-below-min.json +56 -0
  176. package/test-vectors/catalog/negative/multicast-auth-root-dotted-key.json +55 -0
  177. package/test-vectors/catalog/negative/multicast-auth-sender-address-on-ssm.json +57 -0
  178. package/test-vectors/catalog/negative/multicast-auth-version-1.json +56 -0
  179. package/test-vectors/catalog/negative/multicast-auth-version-3.json +56 -0
  180. package/test-vectors/catalog/negative/multicast-auth-version-missing.json +55 -0
  181. package/test-vectors/catalog/negative/multicast-endpoint-foreign-namespace-track.json +44 -0
  182. package/test-vectors/catalog/negative/multicast-endpoint-protocol-amt.json +38 -0
  183. package/test-vectors/catalog/negative/multicast-endpoint-protocol-atsc3.json +38 -0
  184. package/test-vectors/catalog/negative/presentation-anchor-unsafe.json +16 -0
  185. package/test-vectors/catalog/positive/fec-algorithm-unimplemented.json +29 -0
  186. package/test-vectors/catalog/positive/fec-iso-ssbg1.json +1 -1
  187. package/test-vectors/catalog/positive/fec-multicast.json +1 -1
  188. package/test-vectors/catalog/positive/fec-reed-solomon-layered.json +50 -0
  189. package/test-vectors/catalog/positive/mmt01-12-1-init-ref-anchor.json +29 -0
  190. package/test-vectors/catalog/positive/multicast-amt-discovery-omitted.json +37 -0
  191. package/test-vectors/catalog/positive/multicast-amt-dns-relay.json +38 -0
  192. package/test-vectors/catalog/positive/multicast-amt-manual-relay.json +3 -3
  193. package/test-vectors/catalog/{negative/multicast-endpoint-missing-protocol-and-source.json → positive/multicast-asm-unprotected.json} +10 -5
  194. package/test-vectors/catalog/positive/multicast-auth-asm-sender.json +78 -0
  195. package/test-vectors/catalog/positive/multicast-auth-rotation.json +39 -38
  196. package/test-vectors/catalog/positive/multicast-draft01-4-1-example.json +81 -0
  197. package/test-vectors/catalog/positive/multicast-draft01-7-2-6-example.json +70 -0
  198. package/test-vectors/catalog/positive/presentation-anchor-negative.json +27 -0
  199. package/test-vectors/catalog/section-codes.json +31 -1
  200. package/test-vectors/catalog-multicast-auth-v1.json +38 -20
  201. package/test-vectors/catalog-validation.json +101 -5
  202. package/test-vectors/catalog-validator-public-api.mjs +14 -9
  203. package/test-vectors/generate-bc-provenance-v2.mjs +252 -0
  204. package/dist/fec-codec.d.ts +0 -32
  205. package/dist/fec-codec.d.ts.map +0 -1
  206. package/dist/fec-codec.js +0 -47
  207. package/dist/fec-codec.js.map +0 -7
  208. package/dist/manifest-auth-plan.d.ts +0 -30
  209. package/dist/manifest-auth-plan.d.ts.map +0 -1
  210. package/dist/manifest-auth-plan.js +0 -1266
  211. package/dist/manifest-auth-plan.js.map +0 -7
  212. package/dist/source-auth.d.ts +0 -2
  213. package/dist/source-auth.d.ts.map +0 -1
  214. package/dist/source-auth.js +0 -1209
  215. package/dist/source-auth.js.map +0 -7
  216. package/src/fec-codec.ts +0 -61
  217. package/src/manifest-auth-plan.ts +0 -107
  218. package/src/source-auth.ts +0 -1
  219. package/test-vectors/generate-source-auth-fixture.mjs +0 -114
  220. package/test-vectors/source-auth-v1.json +0 -61
@@ -67,10 +67,8 @@ import {
67
67
  type DuplicateHorizon,
68
68
  } from "./duplicate-ledger.js";
69
69
  import type {
70
- ManifestAuthMode,
71
- ManifestObjectIdentity,
72
- ManifestTrackContext,
73
- ManifestVerifier,
70
+ BcProvenanceV2Receiver,
71
+ BcpV2RecordPath,
74
72
  } from "@blockcast/mmt-manifest-verify";
75
73
 
76
74
  // ─── Constants ─────────────────────────────────────────────────────
@@ -177,6 +175,12 @@ export type RoutedFrameMeta = {
177
175
 
178
176
  export interface MmtpIngressContext {
179
177
  tracks: readonly Readonly<{ name: string; packetId: number }>[];
178
+ /**
179
+ * The bc-provenance v2 flow (draft-ramadan-moq-multicast-01 7.2.7) that
180
+ * delivered the packet, when its endpoint is protected: the key a receiver
181
+ * was registered under with `registerProvenanceFlow`.
182
+ */
183
+ flow?: string;
180
184
  }
181
185
 
182
186
  /** One physical stream: an MMTP packet ID and the catalog track it carries. */
@@ -435,10 +439,11 @@ export type RoutedPacketMeta = {
435
439
  /**
436
440
  * The MMTP header RAP flag: the packet starts a random access point. On a
437
441
  * `recovered` packet it is read from the FEC-reconstructed header, which is
438
- * byte-exact, so it is the flag the publisher sent. The router's own gap
439
- * decisions deliberately ignore it for recovered frames (`#isRapForGap`
440
- * judges those by bitstream only); a host using it as a tier-switch commit
441
- * point should decide explicitly whether a recovered RAP qualifies.
442
+ * byte-exact outside packet_sequence_number, so it is the flag the
443
+ * publisher sent. The router's own gap decisions deliberately ignore it for
444
+ * recovered frames (`#isRapForGap` judges those by bitstream only); a host
445
+ * using it as a tier-switch commit point should decide explicitly whether a
446
+ * recovered RAP qualifies.
442
447
  */
443
448
  rap: boolean;
444
449
  /** True when FEC reconstructed this source packet. */
@@ -560,6 +565,27 @@ export interface FECManagerLike {
560
565
  /** A block's RSB_length, from one of its repair symbols (optional). */
561
566
  noteRepairGroupLength?(sbn: number, packetId: number, rsbLength: number, catalogTrackName?: string): void;
562
567
 
568
+ /** fec-01 4.5: refuse a repair header whose block length the track's scheme
569
+ * forbids, before it touches block state (optional). */
570
+ admitRepairBlockLength?(packetId: number, ssbLength: number, rsbLength: number, rsId: number): boolean;
571
+ admitRepairLayerTotal?(
572
+ packetId: number,
573
+ catalogTrackName: string | undefined,
574
+ repairTrackName: string | undefined,
575
+ ssStart: number,
576
+ rsbLength: number,
577
+ ): boolean;
578
+
579
+ /** Validate a repair symbol's size before it touches block state; counts a
580
+ * mismatch on `repairSymbolSizeMismatches` (optional). */
581
+ admitRepairSymbolSize?(
582
+ packetId: number,
583
+ ssStart: number,
584
+ ssbLength: number,
585
+ rsId: number,
586
+ byteLength: number,
587
+ ): boolean;
588
+
563
589
  /** Feed a repair symbol to the FEC decoder for a given track.
564
590
  * Returns recovered block bytes if recovery completes, else null. */
565
591
  feedRepairSymbol(
@@ -695,6 +721,13 @@ export interface FECManagerLike {
695
721
  ssStart: number,
696
722
  ssbLength: number,
697
723
  ): IsoRepairPlacement<IsoHeldSource>;
724
+ /** iso-ssbg1-v1: turn the pending holes in a range into spans, with the sources they place. */
725
+ closeIsoHoles?(
726
+ packetId: number,
727
+ catalogTrackName: string | undefined,
728
+ start: number,
729
+ length: number,
730
+ ): readonly { readonly block: { readonly start: number }; readonly placed: readonly IsoPlacedSource<IsoHeldSource>[] }[];
698
731
  /** iso-ssbg1-v1: resolution of the SS_IDs strictly between two released frames. */
699
732
  isoSpanResolution?(
700
733
  packetId: number,
@@ -775,26 +808,6 @@ export interface FECManagerLike {
775
808
 
776
809
  // ─── MmtpRouter ─────────────────────────────────────────────────────
777
810
 
778
- export interface ManifestIdentityResolutionInput {
779
- packetId: number;
780
- sbn: number;
781
- esi: number;
782
- /**
783
- * The packet's SS_ID and its MPU's mpu_sequence_number. Under
784
- * iso-ssbg1-v1 blocks vary in length, so a packet's media group comes from
785
- * its own MPU header, never from SBN arithmetic (BLO-35597 D2a).
786
- */
787
- ssId?: number;
788
- mpuSequenceNumber?: number;
789
- catalogTrackName?: string;
790
- recovered: boolean;
791
- sourceSymbolFormat: "full-mmtp-packet" | "fec-padded-recovery";
792
- }
793
-
794
- export type ManifestIdentityResolver = (
795
- input: ManifestIdentityResolutionInput,
796
- ) => ManifestObjectIdentity | null;
797
-
798
811
  /**
799
812
  * Classification evidence carried by one first-arrival network source
800
813
  * packet: the MMTP header R bit and the MPU / data-unit identity it states
@@ -831,26 +844,17 @@ export class MmtpRouter {
831
844
  readonly #fecManager: FECManagerLike | null;
832
845
  #defaultFecMapper: FecBlockMapper | null;
833
846
  #fecMappersByPacketId = new Map<number, FecBlockMapper>();
834
- readonly #manifestVerifier: ManifestVerifier | null;
835
- readonly #ownsManifestVerifier: boolean;
836
- readonly #resolveManifestIdentity:
837
- | ((
838
- packetId: number,
839
- sbn: number,
840
- esi: number,
841
- catalogTrackName?: string,
842
- ) => ManifestObjectIdentity | null)
843
- | null;
844
- readonly #resolveManifestIdentityForRoute: ManifestIdentityResolver | null;
845
- #manifestRecoveryQueues = new Map<
847
+ /** Protected flows: one 7.2.7 receiver each, and the ingress its datagrams arrive with. */
848
+ readonly #provenance = new Map<
846
849
  string,
847
- Array<{
848
- blockId: number;
849
- result: ProcessFecBlockResult;
850
- ownerPacketId: number;
851
- catalogTrackName?: string;
852
- }>
850
+ { receiver: BcProvenanceV2Receiver; ingress: MmtpIngressContext }
853
851
  >();
852
+ /** The ingress a held datagram arrived with; the receiver returns the same bytes on release. */
853
+ readonly #heldIngress = new WeakMap<Uint8Array, MmtpIngressContext>();
854
+ /** The flow whose receiver admitted a stream's source datagrams, by packetId then catalog track name: recovered packets answer to it. */
855
+ readonly #provenanceStreamFlow = new Map<number, Map<string | undefined, string>>();
856
+ #unregisteredFlowDrops = 0;
857
+ #ambiguousRecoveredDrops = 0;
854
858
 
855
859
  // Temp state set by #processMMTP before calling parser.feed(),
856
860
  // read by the parser's onFrame callback for CMAF assembly + dedup.
@@ -896,6 +900,18 @@ export class MmtpRouter {
896
900
  // MSE recovery routes through CmafBlockAssembler and can reorder by block; the
897
901
  // direct callback path has to preserve decode order before frames leave libmmt.
898
902
  #latestVideoSeqByStream = new Map<string, number>();
903
+ /**
904
+ * Per stream, SS_ID - packet_sequence_number (mod 2^32) of its latest
905
+ * first-arrival FEC source packet. A recovered source symbol's wire
906
+ * sequence is its SS_ID minus this; see #routeProcessedFecSymbols.
907
+ */
908
+ #ssIdSeqOffsetByStream = new Map<string, number>();
909
+ /**
910
+ * Streams whose multicast leg delivered a RAP-flagged source packet that was
911
+ * not parsed, typically because its overlap copy was parsed first
912
+ * (BLO-49179). The next video keyframe emitted on the stream consumes it.
913
+ */
914
+ #multicastRapTwinByStream = new Set<string>();
899
915
  #lastDirectSourceVideoTsByStream = new Map<string, number>();
900
916
  #lastDirectEmittedVideoTsByStream = new Map<string, number>();
901
917
  #pendingDirectRecoveredVideoByStream = new Map<string, Map<string, PendingRecoveredVideoFrame>>();
@@ -988,11 +1004,14 @@ export class MmtpRouter {
988
1004
  */
989
1005
  #onPublisherRestart: ((packetId: number, catalogTrackName: string) => void) | null;
990
1006
  #publisherRestarts = 0;
1007
+ #restampedRecoveredSeqs = 0;
991
1008
  /** Inits delivered with no onTrackInit handler for their packetId (MOQT-01). */
992
1009
  #unmappedInitDeliveries = 0;
993
1010
  // FEC Payload IDs left unread because their (owner) packetId had no
994
1011
  // configured fec.blockProfile (see #noteUnreadableFecPayloadId).
995
1012
  #fecPayloadIdsWithoutProfile = 0;
1013
+ // Repair packets discarded by the fec-01 4.5 block-length rule.
1014
+ #repairBlockLengthDiscards = 0;
996
1015
 
997
1016
  /** The staged in-place stream switch, if any (stageStreamSwitch). */
998
1017
  #staged: StreamSwitchState | null = null;
@@ -1012,16 +1031,6 @@ export class MmtpRouter {
1012
1031
  * Pass a reader when its inputs are measured later; see
1013
1032
  * setKeyframeLostTimeoutMs. */
1014
1033
  keyframeLostTimeoutMs?: number | (() => number);
1015
- manifestVerifier?: ManifestVerifier;
1016
- /** Existing behavior is router ownership; shared browser plans opt into caller ownership. */
1017
- manifestVerifierOwnership?: "caller" | "router";
1018
- resolveManifestIdentity?: (
1019
- packetId: number,
1020
- sbn: number,
1021
- esi: number,
1022
- catalogTrackName?: string,
1023
- ) => ManifestObjectIdentity | null;
1024
- resolveManifestIdentityForRoute?: ManifestIdentityResolver;
1025
1034
  /** Internal terminal-failure sink owned by the composing transport. */
1026
1035
  onBlockFailed?: (
1027
1036
  sbn: number,
@@ -1044,10 +1053,6 @@ export class MmtpRouter {
1044
1053
  this.#fecManager = config?.fecManager ?? null;
1045
1054
  this.#onFrameGap = config?.onFrameGap ?? null;
1046
1055
  this.#defaultFecMapper = config?.fecMapper ?? null;
1047
- this.#manifestVerifier = config?.manifestVerifier ?? null;
1048
- this.#ownsManifestVerifier = config?.manifestVerifierOwnership !== "caller";
1049
- this.#resolveManifestIdentity = config?.resolveManifestIdentity ?? null;
1050
- this.#resolveManifestIdentityForRoute = config?.resolveManifestIdentityForRoute ?? null;
1051
1056
 
1052
1057
  // Create MMTP parser — handles MFU extraction, reassembly, keyframe detection.
1053
1058
  // Its MFUReassembler is the single reassembler for BOTH the source path
@@ -1119,6 +1124,19 @@ export class MmtpRouter {
1119
1124
  : { catalogTrackName: meta.catalogTrackName }),
1120
1125
  };
1121
1126
  if (meta.source) frameMeta.source = meta.source;
1127
+ // During make-before-break the overlap copy usually wins the parse ledger,
1128
+ // so almost every frame is head-attributed to "moq" and multicast takeover
1129
+ // waited on a rare multicast-won keyframe (BLO-49179: 34 s, 38 s, or past
1130
+ // the 30 s window). A RAP whose multicast twin the ledger dropped is
1131
+ // multicast delivering keyframes, so the next keyframe routes as multicast:
1132
+ // this one if the twin beat its tail, else at most one GOP later.
1133
+ // deliveryProvenance is unchanged and stays 'mixed'.
1134
+ if (isVideo && frameMeta.keyframe) {
1135
+ const twinSeen = this.#multicastRapTwinByStream.delete(
1136
+ this.#mediaStreamKey(packetId, frameMeta.catalogTrackName),
1137
+ );
1138
+ if (twinSeen && frameMeta.source === "moq") frameMeta.source = "multicast";
1139
+ }
1122
1140
  // Forward whenever present, not when truthy: 'unknown' is a verdict the
1123
1141
  // parser reached, not a value to drop.
1124
1142
  if (meta.deliveryProvenance !== undefined)
@@ -1364,6 +1382,15 @@ export class MmtpRouter {
1364
1382
  return this.#publisherRestarts;
1365
1383
  }
1366
1384
 
1385
+ /**
1386
+ * FEC-recovered source symbols whose reconstructed header named a
1387
+ * packet_sequence_number other than the wire sequence their SS_ID maps to:
1388
+ * a hop re-stamped the sequence after the sender protected the packet.
1389
+ */
1390
+ get restampedRecoveredSequenceCount(): number {
1391
+ return this.#restampedRecoveredSeqs;
1392
+ }
1393
+
1367
1394
  /**
1368
1395
  * Packets whose Source or Repair FEC Payload ID was left unread because
1369
1396
  * their (owner) packetId had no configured `fec.blockProfile`: FEC not
@@ -1376,6 +1403,15 @@ export class MmtpRouter {
1376
1403
  return this.#fecPayloadIdsWithoutProfile;
1377
1404
  }
1378
1405
 
1406
+ /**
1407
+ * Repair packets discarded at admission by the fec-01 4.5 rule (RS_ID not
1408
+ * below RSB_length, or a reed-solomon SSB_length + RSB_length above 255),
1409
+ * before they touched any block state.
1410
+ */
1411
+ get repairBlockLengthDiscards(): number {
1412
+ return this.#repairBlockLengthDiscards;
1413
+ }
1414
+
1379
1415
  get reassembler(): MfuReassemblerBackend {
1380
1416
  return this.#parser.reassembler;
1381
1417
  }
@@ -1747,32 +1783,59 @@ export class MmtpRouter {
1747
1783
  });
1748
1784
  }
1749
1785
 
1750
- registerManifestTrack(context: ManifestTrackContext): void {
1751
- if (!this.#manifestVerifier) throw new Error("manifest verifier is not configured");
1752
- this.#manifestVerifier.registerTrack(context);
1786
+ /**
1787
+ * Protect a flow (draft-ramadan-moq-multicast-01 7.2.7): every multicast
1788
+ * datagram whose ingress names `ingress.flow`, source and repair alike, is
1789
+ * admitted by `receiver` before anything reads it, and an unadmitted one is
1790
+ * held or dropped. The caller owns the receiver's configuration; the router
1791
+ * releases it on `releaseProvenanceFlow` and on dispose.
1792
+ */
1793
+ registerProvenanceFlow(
1794
+ ingress: MmtpIngressContext & { flow: string },
1795
+ receiver: BcProvenanceV2Receiver,
1796
+ ): void {
1797
+ this.#provenance.get(ingress.flow)?.receiver.release();
1798
+ this.#provenance.set(ingress.flow, { receiver, ingress });
1753
1799
  }
1754
1800
 
1755
- feedManifestFrame(trackName: string, frame: Uint8Array): void {
1756
- this.#manifestVerifier?.ingestManifestFrame(trackName, frame);
1801
+ /**
1802
+ * Feed one bc-provenance record of `flow`: a datagram from `manifestPort`
1803
+ * (`multicast`) or an object of the manifest track (`mirror`). Held
1804
+ * datagrams the record admits are routed before this returns.
1805
+ */
1806
+ feedProvenanceRecord(flow: string, record: Uint8Array, path: BcpV2RecordPath): string {
1807
+ const entry = this.#provenance.get(flow);
1808
+ if (!entry || this.#disposed) return "unregistered";
1809
+ const result = entry.receiver.onRecord(record, path);
1810
+ if (result.admitted.length > 0) {
1811
+ this.#withDeferredFecPokes(() => {
1812
+ for (const datagram of result.admitted) {
1813
+ this.#processMMTP(datagram, "multicast", true, this.#heldIngress.get(datagram) ?? entry.ingress, false, true);
1814
+ }
1815
+ });
1816
+ }
1817
+ return result.verdict;
1757
1818
  }
1758
1819
 
1759
- setManifestAuthMode(mode: ManifestAuthMode): void {
1760
- this.#manifestVerifier?.setMode(mode);
1761
- if (mode !== "enforce") {
1762
- for (const queue of this.#manifestRecoveryQueues.values()) {
1763
- for (const item of queue) {
1764
- this.#routeProcessedFecSymbols(
1765
- item.blockId,
1766
- item.result,
1767
- item.ownerPacketId,
1768
- item.catalogTrackName,
1769
- );
1770
- }
1771
- }
1772
- this.#manifestRecoveryQueues.clear();
1820
+ /** The receiver ceased to hold the flow: its hold queues are dropped, not expired. */
1821
+ releaseProvenanceFlow(flow: string): void {
1822
+ this.#provenance.get(flow)?.receiver.release();
1823
+ this.#provenance.delete(flow);
1824
+ for (const byName of this.#provenanceStreamFlow.values()) {
1825
+ for (const [name, bound] of byName) if (bound === flow) byName.delete(name);
1773
1826
  }
1774
1827
  }
1775
1828
 
1829
+ /** Multicast datagrams dropped because their ingress names a flow with no registered receiver. */
1830
+ get unregisteredFlowDropCount(): number {
1831
+ return this.#unregisteredFlowDrops;
1832
+ }
1833
+
1834
+ /** Recovered packets dropped because more than one protected flow could own their stream. */
1835
+ get ambiguousRecoveredDropCount(): number {
1836
+ return this.#ambiguousRecoveredDrops;
1837
+ }
1838
+
1776
1839
  /**
1777
1840
  * Main entry point: feed a raw MMTP packet for processing.
1778
1841
  *
@@ -1790,12 +1853,11 @@ export class MmtpRouter {
1790
1853
  feedPacket(
1791
1854
  packet: Uint8Array,
1792
1855
  source: "multicast" | "moq",
1793
- manifestIdentity?: ManifestObjectIdentity,
1794
1856
  ingress?: MmtpIngressContext,
1795
1857
  ): void {
1796
1858
  if (this.#disposed) return;
1797
1859
  this.#withDeferredFecPokes(() => {
1798
- this.#processMMTP(packet, source, manifestIdentity, false, ingress);
1860
+ this.#processMMTP(packet, source, false, ingress);
1799
1861
  });
1800
1862
  }
1801
1863
 
@@ -1831,7 +1893,7 @@ export class MmtpRouter {
1831
1893
  feedOverlapCopy(packet: Uint8Array, ingress?: MmtpIngressContext): void {
1832
1894
  if (this.#disposed) return;
1833
1895
  this.#withDeferredFecPokes(() => {
1834
- this.#processMMTP(packet, "moq", undefined, false, ingress, true);
1896
+ this.#processMMTP(packet, "moq", false, ingress, true);
1835
1897
  });
1836
1898
  }
1837
1899
 
@@ -1843,10 +1905,11 @@ export class MmtpRouter {
1843
1905
  packet: Uint8Array,
1844
1906
  ownerPacketIdHint?: number,
1845
1907
  catalogTrackName?: string,
1908
+ repairTrackName?: string,
1846
1909
  ): void {
1847
1910
  if (this.#disposed) return;
1848
1911
  this.#withDeferredFecPokes(() => {
1849
- this.#feedRepairPacket(packet, ownerPacketIdHint, catalogTrackName);
1912
+ this.#feedRepairPacket(packet, ownerPacketIdHint, catalogTrackName, repairTrackName);
1850
1913
  });
1851
1914
  }
1852
1915
 
@@ -1854,6 +1917,7 @@ export class MmtpRouter {
1854
1917
  packet: Uint8Array,
1855
1918
  ownerPacketIdHint: number | undefined,
1856
1919
  catalogTrackName: string | undefined,
1920
+ repairTrackName: string | undefined,
1857
1921
  ): void {
1858
1922
  // One header parse per packet, as on the multicast repair branch: mmt-fec's
1859
1923
  // parseRepairPacket() parsed it once more before this parse (BLO-36599).
@@ -1901,7 +1965,7 @@ export class MmtpRouter {
1901
1965
  this.#noteUnreadableFecPayloadId(ownerPacketId, "repair");
1902
1966
  return;
1903
1967
  }
1904
- this.#handleIsoRepair(ownerPacketId, catalogTrackName, parsed, "moq");
1968
+ this.#handleIsoRepair(ownerPacketId, catalogTrackName, parsed, "moq", repairTrackName);
1905
1969
  }
1906
1970
  }
1907
1971
 
@@ -2007,6 +2071,7 @@ export class MmtpRouter {
2007
2071
  catalogTrackName: string | undefined,
2008
2072
  repair: { ssStart: number; ssbLength: number; rsbLength: number; rsId: number; symbolData: Uint8Array },
2009
2073
  source: "multicast" | "moq",
2074
+ repairTrackName?: string,
2010
2075
  ): void {
2011
2076
  const fec = this.#fecManager;
2012
2077
  if (!fec?.placeIsoRepair) {
@@ -2014,6 +2079,33 @@ export class MmtpRouter {
2014
2079
  `[MmtpRouter] packetId=${ownerPacketId} is iso-ssbg1-v1 but its FEC manager cannot place ISO blocks`,
2015
2080
  );
2016
2081
  }
2082
+ // fec-01 4.5: a reed-solomon block-length violation is discarded with the
2083
+ // block state unchanged, so it runs before any other admission.
2084
+ if (
2085
+ fec.admitRepairBlockLength &&
2086
+ !fec.admitRepairBlockLength(ownerPacketId, repair.ssbLength, repair.rsbLength, repair.rsId)
2087
+ ) {
2088
+ this.#repairBlockLengthDiscards++;
2089
+ return;
2090
+ }
2091
+ // A repair symbol that is not exactly T bytes is invalid: refuse it before
2092
+ // it can place a block or be swallowed by dedup, so it is always counted
2093
+ // and never reaches the decoder (BLO-48306).
2094
+ if (
2095
+ fec.admitRepairSymbolSize &&
2096
+ !fec.admitRepairSymbolSize(
2097
+ ownerPacketId,
2098
+ repair.ssStart,
2099
+ repair.ssbLength,
2100
+ repair.rsId,
2101
+ repair.symbolData.byteLength,
2102
+ )
2103
+ ) return;
2104
+ // fec-01 6.3.9: a held layer's total below its anchor fails the track's FEC closed.
2105
+ if (
2106
+ fec.admitRepairLayerTotal &&
2107
+ !fec.admitRepairLayerTotal(ownerPacketId, catalogTrackName, repairTrackName, repair.ssStart, repair.rsbLength)
2108
+ ) return;
2017
2109
  const placement = fec.placeIsoRepair(ownerPacketId, catalogTrackName, repair.ssStart, repair.ssbLength);
2018
2110
  if (placement.status !== "placed") return;
2019
2111
  const blockStart = placement.block.start;
@@ -2049,6 +2141,23 @@ export class MmtpRouter {
2049
2141
  }
2050
2142
  }
2051
2143
 
2144
+ /**
2145
+ * The range repair FETCH for an undelimited hole ended (draft-ramadan-moq-fec
2146
+ * §7.5): what no repair stated becomes repair-less spans, whose held sources
2147
+ * are fed exactly as a span a repair closed.
2148
+ */
2149
+ closeIsoHoles(
2150
+ ownerPacketId: number,
2151
+ catalogTrackName: string | undefined,
2152
+ start: number,
2153
+ length: number,
2154
+ ): void {
2155
+ const spans = this.#fecManager?.closeIsoHoles?.(ownerPacketId, catalogTrackName, start, length) ?? [];
2156
+ for (const span of spans) {
2157
+ this.#replayIsoSources(ownerPacketId, catalogTrackName, span.block.start, span.placed);
2158
+ }
2159
+ }
2160
+
2052
2161
  /**
2053
2162
  * Feed iso-ssbg1-v1 sources a repair just placed, in SS_ID order, exactly as
2054
2163
  * a first-arrival source is fed once its block is known: transport
@@ -2180,13 +2289,14 @@ export class MmtpRouter {
2180
2289
  this.#trackSynced.delete(streamKey);
2181
2290
  this.#droppedPreSyncVideo.delete(streamKey);
2182
2291
  this.#latestVideoSeqByStream.delete(streamKey);
2292
+ this.#ssIdSeqOffsetByStream.delete(streamKey);
2293
+ this.#multicastRapTwinByStream.delete(streamKey);
2183
2294
  this.#lastDirectSourceVideoTsByStream.delete(streamKey);
2184
2295
  this.#lastDirectEmittedVideoTsByStream.delete(streamKey);
2185
2296
  this.#pendingDirectRecoveredVideoByStream.delete(streamKey);
2186
2297
  this.#lastDirectSourceAudioCursorByStream.delete(streamKey);
2187
2298
  this.#lastDirectEmittedAudioCursorByStream.delete(streamKey);
2188
2299
  this.#pendingDirectRecoveredAudioByStream.delete(streamKey);
2189
- this.#manifestRecoveryQueues.delete(streamKey);
2190
2300
  this.#deliveredVideoHighWaterByStream.delete(streamKey);
2191
2301
  this.#stream(packetId, catalogTrackName).duplicates.clear();
2192
2302
  const assemblersToReset = new Set<CmafBlockAssembler>();
@@ -2238,7 +2348,6 @@ export class MmtpRouter {
2238
2348
  this.#lastDirectSourceAudioCursorByStream.delete(key);
2239
2349
  this.#lastDirectEmittedAudioCursorByStream.delete(key);
2240
2350
  this.#pendingDirectRecoveredAudioByStream.delete(key);
2241
- this.#manifestRecoveryQueues.delete(key);
2242
2351
  this.#trackInitDelivered.delete(key);
2243
2352
  this.#parserDiagnostics.delete(key);
2244
2353
  this.#pendingFecPokes.delete(key);
@@ -2534,8 +2643,9 @@ export class MmtpRouter {
2534
2643
  this.#parserDiagnostics.clear();
2535
2644
  this.#catalogTrackNamesByPacketId.clear();
2536
2645
  this.#repairOwnersByPacketId.clear();
2537
- if (this.#ownsManifestVerifier) this.#manifestVerifier?.dispose();
2538
- this.#manifestRecoveryQueues.clear();
2646
+ for (const { receiver } of this.#provenance.values()) receiver.release();
2647
+ this.#provenance.clear();
2648
+ this.#provenanceStreamFlow.clear();
2539
2649
  for (const assembler of this.#mseAssemblers.values()) assembler.dispose();
2540
2650
  this.#mseAssemblers.clear();
2541
2651
  }
@@ -2889,7 +2999,6 @@ export class MmtpRouter {
2889
2999
  #processMMTP(
2890
3000
  packet: Uint8Array,
2891
3001
  source: "multicast" | "moq",
2892
- manifestIdentity?: ManifestObjectIdentity,
2893
3002
  manifestAdmitted = false,
2894
3003
  ingress?: MmtpIngressContext,
2895
3004
  overlapCopy = false,
@@ -2938,59 +3047,28 @@ export class MmtpRouter {
2938
3047
  this.#classifyPacketId(info.packetId) !== null &&
2939
3048
  this.#configuredBlockProfile(info.packetId) === undefined;
2940
3049
  if (fecUnreadable && !manifestAdmitted) this.#noteUnreadableFecPayloadId(info.packetId, "source");
3050
+ if (source === "multicast" && manifestAdmitted && !info.isRepair && ingress?.flow !== undefined) {
3051
+ let byName = this.#provenanceStreamFlow.get(info.packetId);
3052
+ if (!byName) this.#provenanceStreamFlow.set(info.packetId, (byName = new Map()));
3053
+ byName.set(catalogTrackNameForPacket(ingress, info.packetId), ingress.flow);
3054
+ }
2941
3055
 
2942
- if (!info.isRepair && this.#manifestVerifier && !manifestAdmitted) {
2943
- let identity = manifestIdentity;
2944
- if (!identity && info.fecPayloadId && !fecUnreadable && (this.#resolveManifestIdentityForRoute || this.#resolveManifestIdentity)) {
2945
- // A source resolves with its block coordinates (SS_Start, ESI), the
2946
- // pair a recovered symbol of the same block resolves with. SS_ID alone
2947
- // locates no block (C.5.3): until a repair states this source's block
2948
- // it has none, its identity stays absent, and the verifier's
2949
- // missing-identity policy below applies.
2950
- const catalogTrackName = catalogTrackNameForPacket(ingress, info.packetId);
2951
- const coordinates = this.#locateFecSource(info.packetId, catalogTrackName, info.fecPayloadId.ssId);
2952
- identity = coordinates === null ? undefined : (this.#resolveManifestIdentityForRoute
2953
- ? this.#resolveManifestIdentityForRoute({
2954
- packetId: info.packetId,
2955
- sbn: coordinates.sbn,
2956
- esi: coordinates.esi,
2957
- ...(catalogTrackName === undefined ? {} : { catalogTrackName }),
2958
- recovered: false,
2959
- sourceSymbolFormat: "full-mmtp-packet",
2960
- })
2961
- : this.#resolveManifestIdentity!(
2962
- info.packetId,
2963
- coordinates.sbn,
2964
- coordinates.esi,
2965
- catalogTrackName,
2966
- )) ?? undefined;
2967
- }
2968
- if (identity) {
2969
- identity = {
2970
- ...identity,
2971
- packetId: info.packetId,
2972
- ...(info.fecPayloadId ? { ssId: info.fecPayloadId.ssId } : {}),
2973
- };
2974
- const authenticated = info.fecPayloadId ? packet.subarray(0, packet.length - 4) : packet;
2975
- // A `stall` admission quarantines the packet and releases it later,
2976
- // after the stream has moved on; a synchronous release happens inside
2977
- // admitObject, before this flag is set.
2978
- let quarantined = false;
2979
- const admission = this.#manifestVerifier.admitObject(identity, authenticated, () => {
2980
- if (!this.#disposed) {
2981
- this.#processMMTP(packet, source, identity, true, ingress, overlapCopy, quarantined);
2982
- }
2983
- });
2984
- quarantined = admission.action === "stall";
2985
- return;
2986
- }
2987
- if (
2988
- this.#manifestVerifier.config.mode === "enforce" &&
2989
- this.#manifestVerifier.config.onMissingManifest === "stall"
2990
- ) {
2991
- console.warn(`[MmtpRouter] manifest identity absent; stalling packetId=${info.packetId}`);
3056
+ // A protected flow admits every datagram, repair included, before it is
3057
+ // read (7.2.7): an unadmitted repair symbol never reaches the decoder.
3058
+ if (source === "multicast" && !manifestAdmitted && ingress?.flow !== undefined) {
3059
+ const protectedFlow = this.#provenance.get(ingress.flow);
3060
+ // Named but not registered (before registration, after release): the
3061
+ // flow is protected, so nothing admits this datagram. Fail closed.
3062
+ if (!protectedFlow) {
3063
+ this.#unregisteredFlowDrops++;
3064
+ debugRouter(`[MmtpRouter] Dropping multicast packet pid=${info.packetId}: flow ${ingress.flow} has no receiver`);
2992
3065
  return;
2993
3066
  }
3067
+ // Held: released by a record (feedProvenanceRecord) or expired. Duplicate / overflow: dropped.
3068
+ const { verdict } = protectedFlow.receiver.onDatagram(packet, "multicast");
3069
+ if (verdict === "admitted") this.#processMMTP(packet, source, true, ingress, overlapCopy);
3070
+ else if (verdict === "held") this.#heldIngress.set(packet, ingress!);
3071
+ return;
2994
3072
  }
2995
3073
 
2996
3074
  if (info.isRepair) {
@@ -3079,8 +3157,8 @@ export class MmtpRouter {
3079
3157
  // Start a fresh epoch for the stream before this packet is looked up.
3080
3158
  // The reset needs the catalog identity, so a stream without one is not
3081
3159
  // checked; an overlap copy is MoQ bytes and says nothing about the
3082
- // multicast publisher, and neither does a packet the manifest verifier
3083
- // quarantined: it predates the horizon because it waited, not because the
3160
+ // multicast publisher, and neither does a packet a provenance hold queue
3161
+ // released: it predates the horizon because it waited, not because the
3084
3162
  // publisher restarted.
3085
3163
  if (
3086
3164
  source === "multicast" &&
@@ -3205,6 +3283,13 @@ export class MmtpRouter {
3205
3283
  parseSourceSymbol = !duplicates.parsedSymbols.has(info.fecPayloadId.ssId);
3206
3284
  }
3207
3285
 
3286
+ if (fecPayloadId && !overlapCopy) {
3287
+ this.#ssIdSeqOffsetByStream.set(
3288
+ this.#mediaStreamKey(info.packetId, catalogTrackName),
3289
+ (fecPayloadId.ssId - info.sequenceNumber) >>> 0,
3290
+ );
3291
+ }
3292
+
3208
3293
  // Parse only the first copy of an FEC source symbol. A late direct copy
3209
3294
  // of a worker-recovered fragment has the same SBN/ESI and fragment
3210
3295
  // counter; feeding it again can make the MFU buffer non-contiguous even
@@ -3231,6 +3316,11 @@ export class MmtpRouter {
3231
3316
  // An FEC source frame is anchored by its own SS_ID (C.5.2).
3232
3317
  ...(fecSource && fecPayloadId ? { fecSbn: fecPayloadId.ssId } : {}),
3233
3318
  }, parsed);
3319
+ } else if (source === "multicast" && info.rapFlag) {
3320
+ // Usually the twin of a RAP its overlap copy already parsed.
3321
+ this.#multicastRapTwinByStream.add(
3322
+ this.#mediaStreamKey(info.packetId, catalogTrackName),
3323
+ );
3234
3324
  }
3235
3325
  // The frame this packet completed can commit an in-place switch that
3236
3326
  // retires this very packet ID. Its FEC state is gone then, and feeding
@@ -3959,115 +4049,63 @@ export class MmtpRouter {
3959
4049
  this.#routeVerifiedFecSymbols(blockId, result, ownerPacketId, catalogTrackName);
3960
4050
  }
3961
4051
 
4052
+ /**
4053
+ * A recovered source packet of a protected flow is admitted on its own
4054
+ * digest: its bytes with the SS_ID it was sent with (SS_Start + ESI)
4055
+ * appended are the datagram the record listed (7.2.7). One that no record
4056
+ * lists is dropped; holding it would let it out later as a multicast arrival.
4057
+ */
3962
4058
  #routeVerifiedFecSymbols(
3963
4059
  blockId: number,
3964
4060
  result: ProcessFecBlockResult,
3965
4061
  ownerPacketId?: number,
3966
4062
  catalogTrackName?: string,
3967
4063
  ): void {
3968
- if (!this.#manifestVerifier || ownerPacketId == null) {
3969
- this.#routeProcessedFecSymbols(blockId, result, ownerPacketId, catalogTrackName);
3970
- return;
3971
- }
3972
- if (!this.#resolveManifestIdentity && !this.#resolveManifestIdentityForRoute) {
3973
- console.warn(`[MmtpRouter] manifest identity resolver unavailable; recovered media is fail-open for packetId=${ownerPacketId} block=${blockId}`);
3974
- this.#routeProcessedFecSymbols(blockId, result, ownerPacketId, catalogTrackName);
3975
- return;
4064
+ const receiver = ownerPacketId == null ? undefined : this.#provenanceFor(ownerPacketId, catalogTrackName);
4065
+ if (receiver === "ambiguous") {
4066
+ this.#ambiguousRecoveredDrops += result.symbols.length;
4067
+ result = { ...result, symbols: [] };
4068
+ } else if (receiver) {
4069
+ result = {
4070
+ ...result,
4071
+ symbols: result.symbols.filter((sym) => {
4072
+ const wire = new Uint8Array(sym.rawSymbol.byteLength + 4);
4073
+ wire.set(sym.rawSymbol);
4074
+ new DataView(wire.buffer).setUint32(sym.rawSymbol.byteLength, this.#fecAnchorForRecovered(blockId, sym.index));
4075
+ return receiver.admitRecovered(wire);
4076
+ }),
4077
+ };
3976
4078
  }
3977
- const queueKey = this.#mediaStreamKey(ownerPacketId, catalogTrackName);
3978
- const queue = this.#manifestRecoveryQueues.get(queueKey) ?? [];
3979
- queue.push({ blockId, result, ownerPacketId, catalogTrackName });
3980
- this.#manifestRecoveryQueues.set(queueKey, queue);
3981
- if (queue.length > 1) return;
3982
- const drain = (): void => {
3983
- const next = queue[0];
3984
- if (!next) {
3985
- this.#manifestRecoveryQueues.delete(queueKey);
3986
- return;
3987
- }
3988
- this.#admitRecoveredResult(
3989
- next.blockId,
3990
- next.result,
3991
- next.ownerPacketId,
3992
- next.catalogTrackName,
3993
- () => {
3994
- queue.shift();
3995
- drain();
3996
- },
3997
- );
3998
- };
3999
- drain();
4079
+ this.#routeProcessedFecSymbols(blockId, result, ownerPacketId, catalogTrackName);
4000
4080
  }
4001
4081
 
4002
- #admitRecoveredResult(
4003
- blockId: number,
4004
- result: ProcessFecBlockResult,
4005
- ownerPacketId: number,
4082
+ /**
4083
+ * The receiver a recovered packet of `packetId` answers to: the flow that
4084
+ * admitted the stream's source datagrams. Before any was admitted, the one
4085
+ * flow whose registration lists the stream; more than one (a repair flow
4086
+ * also lists its owners' source tracks) cannot be told apart, so none.
4087
+ */
4088
+ #provenanceFor(
4089
+ packetId: number,
4006
4090
  catalogTrackName: string | undefined,
4007
- done: () => void,
4008
- ): void {
4009
- const verifier = this.#manifestVerifier;
4010
- const resolveIdentity = this.#resolveManifestIdentity;
4011
- const resolveIdentityForRoute = this.#resolveManifestIdentityForRoute;
4012
- if (!verifier) {
4013
- this.#routeProcessedFecSymbols(blockId, result, ownerPacketId, catalogTrackName);
4014
- done();
4015
- return;
4016
- }
4017
- if (!resolveIdentity && !resolveIdentityForRoute) throw new Error("manifest identity resolver disappeared during admission");
4018
- const symbols = result.symbols;
4019
- const admitNext = (index: number): void => {
4020
- const symbol = symbols[index];
4021
- if (!symbol) {
4022
- done();
4023
- return;
4024
- }
4025
- const identity = resolveIdentityForRoute
4026
- ? resolveIdentityForRoute({
4027
- packetId: ownerPacketId,
4028
- sbn: blockId,
4029
- esi: symbol.index,
4030
- ...(catalogTrackName === undefined ? {} : { catalogTrackName }),
4031
- recovered: true,
4032
- sourceSymbolFormat: "fec-padded-recovery",
4033
- })
4034
- : resolveIdentity!(ownerPacketId, blockId, symbol.index, catalogTrackName);
4035
- if (!identity) {
4036
- console.warn(`[MmtpRouter] manifest identity unresolved; recovered symbol is fail-open for packetId=${ownerPacketId} block=${blockId} esi=${symbol.index}`);
4037
- this.#routeProcessedFecSymbols(
4038
- blockId,
4039
- { ...result, symbols: [symbol] },
4040
- ownerPacketId,
4041
- catalogTrackName,
4042
- );
4043
- admitNext(index + 1);
4044
- return;
4091
+ ): BcProvenanceV2Receiver | "ambiguous" | undefined {
4092
+ // A caller that names no track matches every name the packet id was admitted under.
4093
+ const byName = this.#provenanceStreamFlow.get(packetId);
4094
+ const flows = new Set(
4095
+ catalogTrackName === undefined || !byName
4096
+ ? byName?.values()
4097
+ : byName.has(catalogTrackName) ? [byName.get(catalogTrackName)!] : [],
4098
+ );
4099
+ if (flows.size > 1) return "ambiguous";
4100
+ for (const bound of flows) return this.#provenance.get(bound)?.receiver ?? "ambiguous";
4101
+ let match: BcProvenanceV2Receiver | undefined;
4102
+ for (const { receiver, ingress } of this.#provenance.values()) {
4103
+ if (ingress.tracks.some((t) => t.packetId === packetId && (catalogTrackName === undefined || t.name === catalogTrackName))) {
4104
+ if (match) return "ambiguous";
4105
+ match = receiver;
4045
4106
  }
4046
- const admission = verifier.admitObject(
4047
- {
4048
- ...identity,
4049
- packetId: ownerPacketId,
4050
- },
4051
- symbol.rawSymbol,
4052
- () => {
4053
- if (!this.#disposed) {
4054
- this.#routeProcessedFecSymbols(
4055
- blockId,
4056
- { ...result, symbols: [symbol] },
4057
- ownerPacketId,
4058
- catalogTrackName,
4059
- );
4060
- }
4061
- admitNext(index + 1);
4062
- },
4063
- // A stalled symbol that is later discarded instead of played must still
4064
- // advance this loop; enforce mode never calls the release callback for
4065
- // it (BLO-30095).
4066
- () => admitNext(index + 1),
4067
- );
4068
- if (admission.action === "drop") admitNext(index + 1);
4069
- };
4070
- admitNext(0);
4107
+ }
4108
+ return match;
4071
4109
  }
4072
4110
 
4073
4111
  #routeProcessedFecSymbols(
@@ -4080,6 +4118,28 @@ export class MmtpRouter {
4080
4118
  // which track this block belongs to — resolves per-track, then legacy fallback.
4081
4119
  if (ownerPacketId == null) return; // no track association
4082
4120
  if (this.#classifyPacketId(ownerPacketId) === null) return; // unknown track
4121
+ // A recovered symbol's header names the sequence the sender protected. A
4122
+ // hop that re-stamps packet_sequence_number after protection (cast's
4123
+ // publisher-rotation bridge) leaves the decode mixing every received
4124
+ // source's re-stamped bytes into it. The reassembler addresses a
4125
+ // fragmented data unit by sequence + fragment counter, and the source
4126
+ // dedup reads the same number, so that fragment would park under a
4127
+ // foreign address until its data unit times out. Its SS_ID still names
4128
+ // its wire sequence, through the offset every received source states.
4129
+ const ssIdSeqOffset = this.#ssIdSeqOffsetByStream.get(
4130
+ this.#mediaStreamKey(ownerPacketId, catalogTrackName),
4131
+ );
4132
+ if (ssIdSeqOffset !== undefined) {
4133
+ result = {
4134
+ ...result,
4135
+ symbols: result.symbols.map((sym) => {
4136
+ const wire = (this.#fecAnchorForRecovered(blockId, sym.index) - ssIdSeqOffset) >>> 0;
4137
+ if (wire === sym.packetSequenceNumber) return sym;
4138
+ this.#restampedRecoveredSeqs++;
4139
+ return { ...sym, packetSequenceNumber: wire };
4140
+ }),
4141
+ };
4142
+ }
4083
4143
  this.#stream(ownerPacketId, catalogTrackName).duplicates.advance(blockId);
4084
4144
 
4085
4145
  // A recovered symbol is a function of EVERY symbol that entered its block,
@@ -4100,7 +4160,8 @@ export class MmtpRouter {
4100
4160
  ) ?? 'unknown';
4101
4161
 
4102
4162
  for (const sym of result.symbols) {
4103
- // The block processor read these from the recovered packet's own header.
4163
+ // The block processor read these from the recovered packet's own
4164
+ // header; the sequence is the wire one derived above.
4104
4165
  this.#routeTrackPacket(sym.rawSymbol, ownerPacketId, sym.packetSequenceNumber, {
4105
4166
  mmtpTimestamp: sym.fragment.timestamp,
4106
4167
  rap: sym.fragment.rapFlag,