@blockcast/mmt-container 2.2.0-main.efb4fa4521af → 2.2.0-main.f480a7ede8b1

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 (72) hide show
  1. package/README.md +29 -17
  2. package/dist/browser-runtime.js +446 -162
  3. package/dist/browser-runtime.js.map +3 -3
  4. package/dist/catalog-fec-params.d.ts +17 -1
  5. package/dist/catalog-fec-params.d.ts.map +1 -1
  6. package/dist/catalog-fec-params.js +15 -0
  7. package/dist/catalog-fec-params.js.map +2 -2
  8. package/dist/cmcd-producer.d.ts +60 -47
  9. package/dist/cmcd-producer.d.ts.map +1 -1
  10. package/dist/cmcd-producer.js +138 -109
  11. package/dist/cmcd-producer.js.map +2 -2
  12. package/dist/delivery-catalog.js +26 -16
  13. package/dist/delivery-catalog.js.map +2 -2
  14. package/dist/delta-fallback-config.js.map +1 -1
  15. package/dist/fec-manager.d.ts +3 -1
  16. package/dist/fec-manager.d.ts.map +1 -1
  17. package/dist/fec-manager.js +121 -5
  18. package/dist/fec-manager.js.map +2 -2
  19. package/dist/fec-worker-setup.d.ts +1 -1
  20. package/dist/fec-worker-setup.js +1 -1
  21. package/dist/fec-worker-setup.js.map +1 -1
  22. package/dist/index.d.ts +3 -3
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +5 -1
  25. package/dist/index.js.map +2 -2
  26. package/dist/iso-block-locator.d.ts +9 -0
  27. package/dist/iso-block-locator.d.ts.map +1 -1
  28. package/dist/iso-block-locator.js +17 -0
  29. package/dist/iso-block-locator.js.map +2 -2
  30. package/dist/iso-bmff-timing.d.ts +10 -1
  31. package/dist/iso-bmff-timing.d.ts.map +1 -1
  32. package/dist/iso-bmff-timing.js +36 -2
  33. package/dist/iso-bmff-timing.js.map +2 -2
  34. package/dist/media-router.js.map +1 -1
  35. package/dist/mmtp-container-parser.d.ts +37 -2
  36. package/dist/mmtp-container-parser.d.ts.map +1 -1
  37. package/dist/mmtp-container-parser.js +86 -9
  38. package/dist/mmtp-container-parser.js.map +2 -2
  39. package/dist/mmtp-container-parser.no-modules.js +89 -11
  40. package/dist/mmtp-container-parser.no-modules.js.map +2 -2
  41. package/dist/mmtp-decode-pipeline.js +85 -9
  42. package/dist/mmtp-decode-pipeline.js.map +2 -2
  43. package/dist/mmtp-router.d.ts +6 -0
  44. package/dist/mmtp-router.d.ts.map +1 -1
  45. package/dist/mmtp-router.js +148 -27
  46. package/dist/mmtp-router.js.map +2 -2
  47. package/dist/multicast-manager.js +269 -32
  48. package/dist/multicast-manager.js.map +2 -2
  49. package/dist/multicast-transport.js +26 -16
  50. package/dist/multicast-transport.js.map +2 -2
  51. package/dist/no-modules.js +85 -9
  52. package/dist/no-modules.js.map +2 -2
  53. package/dist/repair-layer-subscriptions.d.ts +6 -2
  54. package/dist/repair-layer-subscriptions.d.ts.map +1 -1
  55. package/dist/repair-layer-subscriptions.js +6 -3
  56. package/dist/repair-layer-subscriptions.js.map +2 -2
  57. package/dist/track-session.d.ts +1 -1
  58. package/dist/track-session.js.map +1 -1
  59. package/dist/wasm-mmtp-container-parser.js +85 -9
  60. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  61. package/package.json +8 -5
  62. package/src/catalog-fec-params.ts +36 -1
  63. package/src/cmcd-producer.ts +189 -179
  64. package/src/fec-manager.ts +130 -5
  65. package/src/fec-worker-setup.ts +2 -2
  66. package/src/index.ts +7 -2
  67. package/src/iso-block-locator.ts +19 -0
  68. package/src/iso-bmff-timing.ts +40 -1
  69. package/src/mmtp-container-parser.ts +96 -8
  70. package/src/mmtp-router.ts +68 -1
  71. package/src/repair-layer-subscriptions.ts +13 -5
  72. package/src/track-session.ts +1 -1
@@ -22,6 +22,7 @@ import {
22
22
  isoBlockContains,
23
23
  isoBlocksBehindLowerBound,
24
24
  parseFecBlockProfile,
25
+ serial32Add,
25
26
  serial32Distance,
26
27
  serial32Precedes,
27
28
  ssbg1FitsSymbol,
@@ -644,6 +645,10 @@ export class FECManager {
644
645
  // ── Keyframe-protected repair policy (block-policy.ts) ──
645
646
  // One classifier per physical stream, built from that stream's geometry.
646
647
  #classifiers = new Map<string, BlockClassifier>()
648
+ // A stream without catalog timing has no classifier, so the last RAP it
649
+ // released stands in for one: "an MPU boundary bounds its RAPs" holds until
650
+ // a second RAP is released inside one MPU (BLO-52085).
651
+ #noSpanRaps = new Map<string, { mpu: number; presentationTime: bigint; inside: boolean }>()
647
652
  // Geometry a stream-scoped configuration gave one stream (streamKey). ABR
648
653
  // rungs can share a packetId with a different framerate or GOP, so the
649
654
  // packet-level geometry is only the geometry of a packet whose streams
@@ -1706,6 +1711,7 @@ export class FECManager {
1706
1711
  // The classifier compares authored order with SBN order only within
1707
1712
  // one continuous SBN sequence.
1708
1713
  this.#classifiers.get(streamKey)?.noteSourceDiscontinuity()
1714
+ this.#noSpanRaps.delete(streamKey)
1709
1715
  this.#highestSbn.set(streamKey, { sbn, atMs: now })
1710
1716
  this.#sourceFrontiers.set(streamKey, { sbn, atMs: now })
1711
1717
  if (psn === undefined) this.#sourceAnchors.delete(streamKey)
@@ -2289,6 +2295,9 @@ export class FECManager {
2289
2295
  // sibling's change, but the first stream-scoped geometry on a packet
2290
2296
  // does change the siblings that have none (stream-geometry-absent).
2291
2297
  this.#bumpEpoch(packetId)
2298
+ for (const key of [...this.#noSpanRaps.keys()]) {
2299
+ if (this.#belongsToPacket(key, packetId)) this.#noSpanRaps.delete(key)
2300
+ }
2292
2301
  for (const [key, classifier] of [...this.#classifiers]) {
2293
2302
  if (!this.#belongsToPacket(key, packetId)) continue
2294
2303
  const stream = this.#parseStreamKey(key)
@@ -2565,6 +2574,7 @@ export class FECManager {
2565
2574
  resetMap(this.#isoLocators)
2566
2575
  resetMap(this.#sourceAnchors)
2567
2576
  this.#classifiers.delete(this.#streamKey(packetId, catalogTrackName))
2577
+ this.#noSpanRaps.delete(this.#streamKey(packetId, catalogTrackName))
2568
2578
  this.#bumpEpoch(packetId)
2569
2579
  for (const [sbn, keys] of this.#blockKeysBySbn) {
2570
2580
  for (const key of keys.keys()) {
@@ -2679,6 +2689,7 @@ export class FECManager {
2679
2689
  dropMap(this.#isoLocators)
2680
2690
  dropMap(this.#sourceAnchors)
2681
2691
  dropMap(this.#classifiers)
2692
+ dropMap(this.#noSpanRaps)
2682
2693
  dropMap(this.#streamGeometries)
2683
2694
  dropSet(this.#pendingWorkerCleanups)
2684
2695
  dropSet(this.#completedBlocks)
@@ -2881,6 +2892,10 @@ export class FECManager {
2881
2892
  }
2882
2893
  }
2883
2894
 
2895
+ if (isSourceSymbol && source === 'multicast') {
2896
+ this.#noteMulticastSource(this.#isoLocatorFor(packetId, catalogTrackName), (sbn + esi) >>> 0, packetId, catalogTrackName)
2897
+ }
2898
+
2884
2899
  return true
2885
2900
  }
2886
2901
 
@@ -3253,6 +3268,7 @@ export class FECManager {
3253
3268
  const locator = this.#growingIsoLocatorFor(packetId, catalogTrackName)
3254
3269
  const evictedBefore = locator.heldEvicted
3255
3270
  const held = locator.hold(ssId, source)
3271
+ if (held && source.source === 'multicast') this.#noteMulticastSource(locator, ssId, packetId, catalogTrackName)
3256
3272
  const evicted = locator.heldEvicted - evictedBefore
3257
3273
  if (evicted > 0) {
3258
3274
  const before = this.#isoHeldEvictedCount
@@ -3470,11 +3486,14 @@ export class FECManager {
3470
3486
  /**
3471
3487
  * Whether an MPU boundary still bounds this stream's RAPs (see
3472
3488
  * `BlockClassifier.mpuBoundsRaps`). Gap decisions drop MPU evidence once
3473
- * it does not. A stream with no classifier yet has shown nothing against it.
3489
+ * it does not. A stream with no classifier yet has shown nothing against it,
3490
+ * and neither has a stream without catalog timing until it releases two
3491
+ * RAPs inside one MPU.
3474
3492
  */
3475
3493
  mpuBoundsRapsFor(packetId: number, catalogTrackName?: string): boolean {
3476
- if (!this.#hasCatalogTimingForPacket(packetId)) return false
3477
- return this.#classifiers.get(this.#streamKey(packetId, catalogTrackName))?.mpuBoundsRaps ?? true
3494
+ const streamKey = this.#streamKey(packetId, catalogTrackName)
3495
+ if (!this.#hasCatalogTimingForPacket(packetId)) return this.#noSpanRaps.get(streamKey)?.inside !== true
3496
+ return this.#classifiers.get(streamKey)?.mpuBoundsRaps ?? true
3478
3497
  }
3479
3498
 
3480
3499
  /** A block's class now, or frozen at its resolution. */
@@ -3510,7 +3529,15 @@ export class FECManager {
3510
3529
  presentationTime: bigint,
3511
3530
  mediaTimescale: number,
3512
3531
  ): void {
3513
- if (!this.#trackConfigs.has(packetId) || !this.#hasCatalogTimingForPacket(packetId)) return
3532
+ if (!this.#trackConfigs.has(packetId)) return
3533
+ if (!this.#hasCatalogTimingForPacket(packetId)) {
3534
+ const streamKey = this.#streamKey(packetId, catalogTrackName)
3535
+ const last = this.#noSpanRaps.get(streamKey)
3536
+ const inside = last?.inside === true ||
3537
+ (last?.mpu === mpu && last.presentationTime !== presentationTime)
3538
+ this.#noSpanRaps.set(streamKey, { mpu, presentationTime, inside })
3539
+ return
3540
+ }
3514
3541
  this.#applyClassifierChanges(
3515
3542
  packetId,
3516
3543
  catalogTrackName,
@@ -4343,9 +4370,95 @@ export class FECManager {
4343
4370
  this.#completeCleanBlock(blockKey, sbn, packetId, catalogTrackName)
4344
4371
  return null
4345
4372
  }
4373
+ // Its K' is known now: a block this repair shows short of it waits less.
4374
+ if (recovered === null) this.#shortenUnrecoverableWait(blockKey, sbn, packetId, catalogTrackName)
4346
4375
  return recovered
4347
4376
  }
4348
4377
 
4378
+ /**
4379
+ * A first-arrival multicast source, held or placed. A datagram flow keeps
4380
+ * its sources in send order, so one that moves the stream's newest source
4381
+ * position past a block leaves the sources that block still misses
4382
+ * reordered behind it at best: it shortens the wait of every block it
4383
+ * passed that only a late source can complete. MoQ is left out: its streams
4384
+ * can overtake one another, so a MoQ source shows nothing about the sources
4385
+ * sent before it.
4386
+ */
4387
+ #noteMulticastSource(
4388
+ locator: IsoBlockLocator<IsoHeldSource>,
4389
+ ssId: number,
4390
+ packetId: number,
4391
+ catalogTrackName: string | undefined,
4392
+ ): void {
4393
+ const previous = locator.newestSource
4394
+ if (!locator.noteSource(ssId) || previous === undefined) return
4395
+ // The blocks whose last SS_ID lies in [previous, ssId): this source
4396
+ // passed them and the previous newest one had not.
4397
+ for (const block of locator.blocksBetween((previous - 1) >>> 0, ssId)) {
4398
+ if (block.span || !serial32Precedes(serial32Add(block.start, block.length - 1), ssId)) continue
4399
+ this.#shortenUnrecoverableWait(
4400
+ this.#blockKey(block.start, packetId, catalogTrackName),
4401
+ block.start,
4402
+ packetId,
4403
+ catalogTrackName,
4404
+ )
4405
+ }
4406
+ }
4407
+
4408
+ /**
4409
+ * The wait of a block that only a late source can complete (BLO-52085): a
4410
+ * repair stated it, its received sources plus every symbol its repair group
4411
+ * carries (RSB_length) are short of K' (the repair scheduler's
4412
+ * group-cannot-complete), and a later source of the stream has arrived, so
4413
+ * a source it misses is lost or reordered behind that one. The span, which
4414
+ * covers repair symbols paced after its sources, buys it nothing: it waits
4415
+ * the late-symbol allowance alone, the multicast wait less the span (the
4416
+ * catalog tolerance or the measured delivery gap). Undefined for any other
4417
+ * block.
4418
+ */
4419
+ #lateSourceWaitMs(
4420
+ key: string,
4421
+ sbn: number,
4422
+ packetId: number,
4423
+ catalogTrackName: string | undefined,
4424
+ config: ResolvedFECTrackConfig,
4425
+ ): number | undefined {
4426
+ const authoredLength = this.#blockAuthoredLengths.get(key)
4427
+ const rsbLength = this.#blockRsbLengths.get(key)
4428
+ if (authoredLength === undefined || rsbLength === undefined) return undefined
4429
+ if ((this.#blockSourceCounts.get(key) ?? 0) + rsbLength >= authoredLength) return undefined
4430
+ const newestSource = this.#isoLocators.get(this.#streamKey(packetId, catalogTrackName))?.newestSource
4431
+ if (newestSource === undefined || !serial32Precedes(serial32Add(sbn, authoredLength - 1), newestSource)) {
4432
+ return undefined
4433
+ }
4434
+ return this.#multicastWaitMs(config) - config.interleaveDepthMs
4435
+ }
4436
+
4437
+ /**
4438
+ * Bring an open block's wait in to its late-source wait, when it has one.
4439
+ * Never later than it is armed: only a source can complete such a block,
4440
+ * so a repair of its group (paced across the span) must not keep it open.
4441
+ */
4442
+ #shortenUnrecoverableWait(
4443
+ key: string,
4444
+ sbn: number,
4445
+ packetId: number,
4446
+ catalogTrackName: string | undefined,
4447
+ ): void {
4448
+ if (!this.#activeBlocks.has(key)) return
4449
+ const config = this.#trackConfigs.get(packetId)
4450
+ if (config === undefined) return
4451
+ const waitMs = this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config)
4452
+ if (waitMs === undefined) return
4453
+ const armedAtMs = this.#blockWaitDeadlinesMs.get(key)
4454
+ this.#armBlockTimeout(
4455
+ sbn,
4456
+ packetId,
4457
+ catalogTrackName,
4458
+ armedAtMs === undefined ? waitMs : Math.min(waitMs, armedAtMs - this.#receiptNow()),
4459
+ )
4460
+ }
4461
+
4349
4462
  /**
4350
4463
  * Record a block's authored SSB_length from a repair symbol. Returns false,
4351
4464
  * leaving the block unchanged, when the symbol contradicts the block: a
@@ -4693,7 +4806,18 @@ export class FECManager {
4693
4806
  )
4694
4807
  }
4695
4808
 
4696
- this.#armBlockTimeout(sbn, packetId, catalogTrackName, timeoutMs)
4809
+ // A block only a late source can complete waits the late-symbol
4810
+ // allowance from its own sources' activity; nothing else moves it later.
4811
+ if (!sourceProgress && this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, trackConfig) !== undefined) {
4812
+ this.#shortenUnrecoverableWait(key, sbn, packetId, catalogTrackName)
4813
+ return
4814
+ }
4815
+ this.#armBlockTimeout(
4816
+ sbn,
4817
+ packetId,
4818
+ catalogTrackName,
4819
+ this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, trackConfig) ?? timeoutMs,
4820
+ )
4697
4821
  }
4698
4822
 
4699
4823
  #failBlock(
@@ -4883,6 +5007,7 @@ export class FECManager {
4883
5007
  this.#blockAnchorsMs.clear()
4884
5008
  this.#blockCatalogTrackNames.clear()
4885
5009
  this.#classifiers.clear()
5010
+ this.#noSpanRaps.clear()
4886
5011
  this.#streamGeometries.clear()
4887
5012
  this.#failedBlocks.clear()
4888
5013
  this.#resolvedBlockClasses.clear()
@@ -64,7 +64,7 @@ export interface FecWorkerSetupOptions {
64
64
  /**
65
65
  * Repair symbols per block this receiver actually joined: the sum of
66
66
  * `repairSymbols` over the joined BASE layers (draft-ramadan-moq-fec 6.3).
67
- * Reported as P by the worker snapshot and CMCD `x-bc-fec-p`, because the
67
+ * Reported as P by the worker snapshot and CMCD `com.blockcast-fp`, because the
68
68
  * catalog total is not what this receiver joined. Falls back to
69
69
  * `fecRepairCount`, which is the same value on a single-layer track.
70
70
  */
@@ -252,7 +252,7 @@ export async function setupFecWorkerForTrack(
252
252
  blockProfile,
253
253
  k: opts.fecK,
254
254
  // P as this receiver joined it, not the catalog total: the worker snapshot
255
- // and CMCD `x-bc-fec-p` are read from this. Identity on a single layer.
255
+ // and CMCD `com.blockcast-fp` are read from this. Identity on a single layer.
256
256
  repairCount: opts.joinedRepairCount ?? opts.fecRepairCount,
257
257
  symbolSize: opts.fecSymbolSize,
258
258
  interleaveDepth: interleaveDepthBlocks,
package/src/index.ts CHANGED
@@ -299,6 +299,7 @@ export {
299
299
  type MmtpFeedOutcomeKey,
300
300
  type TrackInfo,
301
301
  type MmtpReorderStats,
302
+ type MmtpRecoveredHeadTimingStats,
302
303
  type MmtpReorderFrameDiagnostics,
303
304
  type MmtpReorderStreamDiagnostics,
304
305
  type FrameMeta as ParserFrameMeta,
@@ -526,10 +527,13 @@ export {
526
527
  type FecStarvationDetectorConfig,
527
528
  } from './fec-starvation-detector.js'
528
529
 
529
- // CMCD Producer (x-bc-* vendor key telemetry, BLO-1044)
530
+ // CMCD v2 Event-mode producer (CTA-5004-B, com.blockcast-* custom keys)
530
531
  export {
531
532
  CmcdProducer,
532
- type CmcdParams,
533
+ encodeCmcdRecord,
534
+ type CmcdRecord,
535
+ type CmcdToken,
536
+ type CmcdValue,
533
537
  type CmcdProducerOptions,
534
538
  type SnapshotData,
535
539
  type TransportSnapshot,
@@ -546,6 +550,7 @@ export {
546
550
  type TrackFecParams,
547
551
  type TrackFecUnavailable,
548
552
  catalogTrackFecUnavailable,
553
+ catalogTrackDeliveryMode,
549
554
  catalogGroupDurationMs,
550
555
  catalogKeyframeCadenceMs,
551
556
  catalogFecMediaUnitsPerSecond,
@@ -85,6 +85,7 @@ export class IsoBlockLocator<T> {
85
85
  #retentionBlocks: number
86
86
  #heldEvicted = 0
87
87
  #newest: number | undefined
88
+ #newestSource: number | undefined
88
89
  /** Pending holes (see `deferIncompleteGaps`): unstated, sources still held. */
89
90
  readonly #holes: IsoBlockRange[] = []
90
91
  /**
@@ -152,6 +153,23 @@ export class IsoBlockLocator<T> {
152
153
  if (this.#newest === undefined || serial32Precedes(this.#newest, position)) this.#newest = position
153
154
  }
154
155
 
156
+ /**
157
+ * The newest source SS_ID noted with `noteSource` (RFC 1982 order). Unlike
158
+ * `newest`, a repair never moves it: a repair can run ahead of its sources,
159
+ * a source cannot run ahead of the sources sent before it except by
160
+ * reordering.
161
+ */
162
+ get newestSource(): number | undefined {
163
+ return this.#newestSource
164
+ }
165
+
166
+ /** Note a received source's SS_ID; true when it is the newest one yet. */
167
+ noteSource(ssId: number): boolean {
168
+ if (this.#newestSource !== undefined && !serial32Precedes(this.#newestSource, ssId)) return false
169
+ this.#newestSource = ssId
170
+ return true
171
+ }
172
+
155
173
  /** Held sources evicted by `heldCapacity` (never by the horizon itself). */
156
174
  get heldEvicted(): number {
157
175
  return this.#heldEvicted
@@ -412,6 +430,7 @@ export class IsoBlockLocator<T> {
412
430
  this.#held.clear()
413
431
  this.#coverageEnd = undefined
414
432
  this.#newest = undefined
433
+ this.#newestSource = undefined
415
434
  }
416
435
 
417
436
  #claim(block: IsoBlockRange): IsoPlacedSource<T>[] {
@@ -581,6 +581,14 @@ type PendingMovieFragment = Array<IsoBmffSampleTiming | null>
581
581
  export class IsoBmffTimingResolver {
582
582
  #track: IsoBmffTrackTiming | null = null
583
583
  #fragments = new Map<string, PendingMovieFragment>()
584
+ /** First sample of each pending fragment, kept in `#delivered` with its sample count. */
585
+ #firstSamples = new Map<string, IsoBmffSampleTiming>()
586
+ /**
587
+ * First sample and sample count of the newest fragments whose every slot was
588
+ * cleared. Abandonment counts: `releaseSample` clears a slot too, which
589
+ * matches the combiner, since it retires an abandoned sample as well.
590
+ */
591
+ #delivered = new Map<string, { first: IsoBmffSampleTiming, sampleCount: number }>()
584
592
  #pendingSamples = 0
585
593
  #evictedSamples = 0
586
594
  #lastMovieFragmentRejection: IsoBmffMovieFragmentRejection | null = null
@@ -591,6 +599,11 @@ export class IsoBmffTimingResolver {
591
599
  }
592
600
  }
593
601
 
602
+ /** True once an FT=0 has bound the track that FT=1 timing is read against. */
603
+ get initialized(): boolean {
604
+ return this.#track !== null
605
+ }
606
+
594
607
  get pendingSampleCount(): number {
595
608
  return this.#pendingSamples
596
609
  }
@@ -618,10 +631,18 @@ export class IsoBmffTimingResolver {
618
631
  return true
619
632
  }
620
633
 
634
+ /**
635
+ * `recovered` marks an FEC-recovered copy. A recovered block hands back
636
+ * every FT=1 it holds, the received ones included, so a recovered copy of a
637
+ * fragment whose samples are all done is a duplicate: admitting it again
638
+ * would charge stale timing and could displace the live MPU's. A source
639
+ * copy is still admitted again (a rendition switch re-arms timing that way).
640
+ */
621
641
  addMovieFragment(
622
642
  mpuSequenceNumber: number,
623
643
  data: Uint8Array,
624
644
  additionalSampleLimit = this.maxPendingSamples,
645
+ recovered = false,
625
646
  ): boolean {
626
647
  this.#lastMovieFragmentRejection = null
627
648
  if (!Number.isInteger(additionalSampleLimit) || additionalSampleLimit < 0) {
@@ -648,6 +669,9 @@ export class IsoBmffTimingResolver {
648
669
  (sample, index) => sample !== null && sameSample(sample, parsed.samples[index]!),
649
670
  )
650
671
  }
672
+ const delivered = recovered ? this.#delivered.get(key) : undefined
673
+ if (delivered && delivered.sampleCount === parsed.samples.length &&
674
+ sameSample(delivered.first, parsed.samples[0]!)) return true
651
675
  // Sliding window, not a one-way ratchet. `consumeSample` and
652
676
  // `releaseSample` between them cover every sample some component observed
653
677
  // -- but a sample whose data units never arrive at all is observed by
@@ -683,6 +707,7 @@ export class IsoBmffTimingResolver {
683
707
  return false
684
708
  }
685
709
  this.#fragments.set(key, [...parsed.samples])
710
+ this.#firstSamples.set(key, parsed.samples[0]!)
686
711
  this.#pendingSamples += parsed.samples.length
687
712
  return true
688
713
  }
@@ -758,6 +783,7 @@ export class IsoBmffTimingResolver {
758
783
  let live = 0
759
784
  for (const sample of samples) if (sample !== null) live++
760
785
  this.#fragments.delete(oldest)
786
+ this.#firstSamples.delete(oldest)
761
787
  this.#pendingSamples -= live
762
788
  this.#evictedSamples += live
763
789
  return live
@@ -779,13 +805,26 @@ export class IsoBmffTimingResolver {
779
805
  if (!samples || !samples[index]) return false
780
806
  samples[index] = null
781
807
  this.#pendingSamples--
782
- if (samples.every(value => value === null)) this.#fragments.delete(key)
808
+ if (samples.every(value => value === null)) {
809
+ this.#fragments.delete(key)
810
+ const first = this.#firstSamples.get(key)!
811
+ this.#firstSamples.delete(key)
812
+ this.#delivered.delete(key)
813
+ this.#delivered.set(key, { first, sampleCount: samples.length })
814
+ // ponytail: the dedupe memory is as wide as the timing budget, in fragments;
815
+ // a recovered copy older than that is admitted again, as before.
816
+ if (this.#delivered.size > this.maxPendingSamples) {
817
+ this.#delivered.delete(this.#delivered.keys().next().value!)
818
+ }
819
+ }
783
820
  return true
784
821
  }
785
822
 
786
823
  reset(): void {
787
824
  this.#track = null
788
825
  this.#fragments.clear()
826
+ this.#firstSamples.clear()
827
+ this.#delivered.clear()
789
828
  this.#pendingSamples = 0
790
829
  }
791
830
  }
@@ -365,10 +365,19 @@ export type MmtpFeedDropReason =
365
365
  /** No timing resolver, or it refused the initialization segment. */
366
366
  | 'init-timing-refused'
367
367
  | 'fragment-unexpected-fragmentation'
368
+ /** FT=1 before an FT=0 has bound the stream's timing track. */
368
369
  | 'fragment-no-resolver'
369
370
  /** addMovieFragment refused — includes the pending-timing-sample budget. */
370
371
  | 'fragment-timing-refused'
371
372
  | 'unknown-fragment-type'
373
+ /** Stable code, no longer produced: an FT=2 with no timing (before the
374
+ * stream's first FT=0, or after setTrack dropped it) now waits for its
375
+ * timing like one whose FT=1 has not arrived. A head counts as
376
+ * `drop:mfu-sample-timing-missing` (parked) and a FI=2/FI=3 continuation
377
+ * as the `pending` record. A continuation that completes a head the
378
+ * reassembler buffered before the timing was dropped counts as
379
+ * `drop:assembled-timing-missing`. A panel keyed on this code reads zero:
380
+ * a re-attribution, not lost coverage. Re-key on both drop codes. */
372
381
  | 'mfu-no-resolver'
373
382
  | 'mfu-empty-payload'
374
383
  | 'mfu-du-header-missing'
@@ -679,7 +688,10 @@ class FrameSequencer {
679
688
  // ── FecReleaseSequencer ─────────────────────────────────────────────
680
689
 
681
690
  /** Authored decode-time adjacency: the next frame starts where this one ends. */
682
- function authoredAdjacent(before: FrameMeta, after: FrameMeta): boolean {
691
+ export function authoredAdjacent(
692
+ before: Pick<FrameMeta, 'authoredTiming'>,
693
+ after: Pick<FrameMeta, 'authoredTiming'>,
694
+ ): boolean {
683
695
  const a = before.authoredTiming
684
696
  const b = after.authoredTiming
685
697
  if (!a || !b || a.mediaTimescale !== b.mediaTimescale) return false
@@ -1089,9 +1101,30 @@ interface AwaitingTimingHead {
1089
1101
  mpuSequenceNumber: number
1090
1102
  movieFragmentSequenceNumber: number
1091
1103
  sampleNumber: number
1104
+ /** MMTP packet sequence number; a recovered copy carries its source's. */
1105
+ sequenceNumber: number
1092
1106
  arrivedAt: number
1093
1107
  }
1094
1108
 
1109
+ /** FEC-recovered heads that went through the awaiting-timing queue. Every
1110
+ * count but `pending` is cumulative. A held head is pending or has left the
1111
+ * queue exactly once, so `held - expired - discarded - superseded - pending`
1112
+ * is the number fed again once their FT=1 landed. That feed can still refuse
1113
+ * one (size, offset, dispose from a frame callback); the refusal is counted in
1114
+ * `feedStats.deferredOutcomes`, not here. */
1115
+ export interface MmtpRecoveredHeadTimingStats {
1116
+ /** Held because their sample's FT=1 had not arrived yet. */
1117
+ held: number
1118
+ /** Left without timing: the reassembler-timeout deadline, or pushed out by the queue's capacity. */
1119
+ expired: number
1120
+ /** Dropped with their stream (track reconfigure, reset or retire) or by dispose(). */
1121
+ discarded: number
1122
+ /** Gave way to their arriving source copy, which takes their place in the queue. */
1123
+ superseded: number
1124
+ /** In the queue now; current occupancy, not cumulative. */
1125
+ pending: number
1126
+ }
1127
+
1095
1128
  const MAX_AWAITING_TIMING_HEADS = 8
1096
1129
 
1097
1130
  // ── MmtpContainerParser ─────────────────────────────────────────────
@@ -1129,6 +1162,9 @@ export class MmtpContainerParser {
1129
1162
  #reclaimedTimingSamples = 0
1130
1163
  #evictedTimingSamples = 0
1131
1164
  #rejectedMovieFragments = 0
1165
+ #recoveredHeadsAwaitingTiming: Omit<MmtpRecoveredHeadTimingStats, 'pending'> = {
1166
+ held: 0, expired: 0, discarded: 0, superseded: 0,
1167
+ }
1132
1168
  #sequencers = new Map<string, { packetId: number; sequencer: FrameSequencer }>()
1133
1169
  #fecGate: FecReleaseGate | null = null
1134
1170
  #fecSequencer: FecReleaseSequencer | null = null
@@ -1346,6 +1382,15 @@ export class MmtpContainerParser {
1346
1382
  }
1347
1383
  }
1348
1384
 
1385
+ /** Recovered heads parked for FT=1 timing (BLO-53369). Survives dispose(). */
1386
+ get recoveredHeadsAwaitingTiming(): Readonly<MmtpRecoveredHeadTimingStats> {
1387
+ let pending = 0
1388
+ for (const { awaitingTiming } of this.#timingStreams.values()) {
1389
+ for (const head of awaitingTiming) if (head.options?.recovered) pending++
1390
+ }
1391
+ return { ...this.#recoveredHeadsAwaitingTiming, pending }
1392
+ }
1393
+
1349
1394
  get reorderStats(): Readonly<MmtpReorderStats> {
1350
1395
  let bufferedFrames = this.#fecSequencer?.buffered ?? 0
1351
1396
  for (const { sequencer } of this.#sequencers.values()) bufferedFrames += sequencer.buffered
@@ -1541,7 +1586,7 @@ export class MmtpContainerParser {
1541
1586
  const now = Date.now()
1542
1587
  for (const { awaitingTiming } of this.#timingStreams.values()) {
1543
1588
  while (awaitingTiming.length > 0 && now - awaitingTiming[0]!.arrivedAt > timeoutMs) {
1544
- awaitingTiming.shift()
1589
+ this.#leaveAwaitingTiming(awaitingTiming.shift()!, 'expired')
1545
1590
  }
1546
1591
  }
1547
1592
  }
@@ -1711,11 +1756,16 @@ export class MmtpContainerParser {
1711
1756
  if (mpu.fragmentationIndicator !== 0 || mpu.fragmentCounter !== 0)
1712
1757
  return this.#drop('fragment-unexpected-fragmentation')
1713
1758
  const resolver = this.#timingResolver(streamKey, info.packetId, false)
1714
- if (!resolver) return this.#drop('fragment-no-resolver')
1759
+ if (!resolver?.initialized) return this.#drop('fragment-no-resolver')
1715
1760
  const pendingBefore = resolver.pendingSampleCount
1716
1761
  const evictedBefore = resolver.evictedSampleCount
1717
1762
  const remaining = this.#maxPendingTimingSamples - this.#pendingTimingSamples
1718
- const admitted = resolver.addMovieFragment(mpu.mpuSequenceNumber, data, remaining)
1763
+ const admitted = resolver.addMovieFragment(
1764
+ mpu.mpuSequenceNumber,
1765
+ data,
1766
+ remaining,
1767
+ options?.recovered === true,
1768
+ )
1719
1769
  // Sync on BOTH paths: the resolver can displace stale timing and still
1720
1770
  // refuse the fragment in hand, and a global count that missed those
1721
1771
  // freed slots would keep refusing on a budget that is no longer full.
@@ -1739,7 +1789,11 @@ export class MmtpContainerParser {
1739
1789
  }
1740
1790
  if (fragmentType !== 2) return this.#drop('unknown-fragment-type')
1741
1791
 
1742
- const resolver = this.#timingResolver(streamKey, info.packetId, false)
1792
+ // An FT=2 before the stream's first FT=0 (a join, a track reset) is taken
1793
+ // like one whose FT=1 has not arrived: a head waits for its timing, a
1794
+ // continuation parks in the reassembler, and a later or FEC-recovered
1795
+ // FT=0/FT=1 can still time them.
1796
+ const resolver = this.#timingResolver(streamKey, info.packetId, true)
1743
1797
  if (!resolver) return this.#drop('mfu-no-resolver')
1744
1798
  if (!Number.isSafeInteger(data.byteLength) || data.byteLength <= 0)
1745
1799
  return this.#drop('mfu-empty-payload')
@@ -1761,6 +1815,7 @@ export class MmtpContainerParser {
1761
1815
  mpuSequenceNumber: mpu.mpuSequenceNumber,
1762
1816
  movieFragmentSequenceNumber: mfuDuHeader.movieFragmentSequenceNumber,
1763
1817
  sampleNumber: mfuDuHeader.sampleNumber,
1818
+ sequenceNumber: info.sequenceNumber,
1764
1819
  arrivedAt: Date.now(),
1765
1820
  })
1766
1821
  return this.#drop('mfu-sample-timing-missing')
@@ -2146,6 +2201,18 @@ export class MmtpContainerParser {
2146
2201
  this.#emitFrame(data, meta)
2147
2202
  }
2148
2203
 
2204
+ /** Whether FT=1 metadata already names this sample, without claiming it. */
2205
+ hasAuthoredTimingFor(
2206
+ packetId: number,
2207
+ mpuSequenceNumber: number,
2208
+ movieFragmentSequenceNumber: number,
2209
+ sampleNumber: number,
2210
+ catalogTrackName?: string,
2211
+ ): boolean {
2212
+ return this.#timingResolver(this.#streamKey(packetId, catalogTrackName), packetId, false)
2213
+ ?.sampleTiming(mpuSequenceNumber, movieFragmentSequenceNumber, sampleNumber) != null
2214
+ }
2215
+
2149
2216
  /**
2150
2217
  * Claim and return the authored timing for a recovered sample. Recovery does
2151
2218
  * not pass through the ordinary `feed()` completion path, so it must retire
@@ -2216,6 +2283,9 @@ export class MmtpContainerParser {
2216
2283
  for (const { sequencer } of this.#sequencers.values()) sequencer.discard()
2217
2284
  this.#sequencers.clear()
2218
2285
  this.#fecSequencer?.discard()
2286
+ for (const { awaitingTiming } of this.#timingStreams.values()) {
2287
+ for (const head of awaitingTiming) this.#leaveAwaitingTiming(head, 'discarded')
2288
+ }
2219
2289
  this.#timingStreams.clear()
2220
2290
  this.#pendingTimingSamples = 0
2221
2291
  this.#reliableMoqStartupGate.clear()
@@ -2241,10 +2311,27 @@ export class MmtpContainerParser {
2241
2311
  }
2242
2312
 
2243
2313
  #awaitTiming(streamKey: string, head: AwaitingTimingHead): void {
2244
- const awaitingTiming = this.#timingStreams.get(streamKey)?.awaitingTiming
2245
- if (!awaitingTiming) return
2314
+ const state = this.#timingStreams.get(streamKey)
2315
+ if (!state) return
2316
+ // The source copy of a held recovered head takes its place, so the frame
2317
+ // is delivered with source provenance once FT=1 lands.
2318
+ if (!head.options?.recovered) {
2319
+ const kept = state.awaitingTiming.filter(queued =>
2320
+ !queued.options?.recovered || queued.sequenceNumber !== head.sequenceNumber)
2321
+ this.#recoveredHeadsAwaitingTiming.superseded += state.awaitingTiming.length - kept.length
2322
+ state.awaitingTiming = kept
2323
+ }
2324
+ const awaitingTiming = state.awaitingTiming
2246
2325
  awaitingTiming.push(head)
2247
- if (awaitingTiming.length > MAX_AWAITING_TIMING_HEADS) awaitingTiming.shift()
2326
+ if (head.options?.recovered) this.#recoveredHeadsAwaitingTiming.held++
2327
+ if (awaitingTiming.length > MAX_AWAITING_TIMING_HEADS) {
2328
+ this.#leaveAwaitingTiming(awaitingTiming.shift()!, 'expired')
2329
+ }
2330
+ }
2331
+
2332
+ /** Count a recovered head leaving the queue other than by replay or its source copy. */
2333
+ #leaveAwaitingTiming(head: AwaitingTimingHead, outcome: 'expired' | 'discarded'): void {
2334
+ if (head.options?.recovered) this.#recoveredHeadsAwaitingTiming[outcome]++
2248
2335
  }
2249
2336
 
2250
2337
  /**
@@ -2274,6 +2361,7 @@ export class MmtpContainerParser {
2274
2361
  const state = this.#timingStreams.get(streamKey)
2275
2362
  if (!state) return
2276
2363
  this.#timingStreams.delete(streamKey)
2364
+ for (const head of state.awaitingTiming) this.#leaveAwaitingTiming(head, 'discarded')
2277
2365
  this.#pendingTimingSamples -= state.resolver.pendingSampleCount
2278
2366
  if (this.#pendingTimingSamples < 0) {
2279
2367
  throw new Error('[MmtpContainerParser] pending timing sample accounting underflow')