@blockcast/mmt-container 2.2.0-main.ea9223dc11de → 2.2.0-main.ec45bee1b9cc

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 (132) hide show
  1. package/README.md +125 -2
  2. package/dist/__tests__/ad-creative-signer.d.ts +11 -0
  3. package/dist/__tests__/ad-creative-signer.d.ts.map +1 -0
  4. package/dist/__tests__/test-utils.d.ts +1 -0
  5. package/dist/__tests__/test-utils.d.ts.map +1 -1
  6. package/dist/ad-beacon.d.ts +39 -0
  7. package/dist/ad-beacon.d.ts.map +1 -0
  8. package/dist/ad-beacon.js +23 -0
  9. package/dist/ad-beacon.js.map +7 -0
  10. package/dist/ad-creative-record.d.ts +65 -0
  11. package/dist/ad-creative-record.d.ts.map +1 -0
  12. package/dist/ad-creative-record.js +2281 -0
  13. package/dist/ad-creative-record.js.map +7 -0
  14. package/dist/ad-creative-timeline.d.ts +103 -0
  15. package/dist/ad-creative-timeline.d.ts.map +1 -0
  16. package/dist/ad-creative-timeline.js +2550 -0
  17. package/dist/ad-creative-timeline.js.map +7 -0
  18. package/dist/block-policy.d.ts +128 -14
  19. package/dist/block-policy.d.ts.map +1 -1
  20. package/dist/block-policy.js +472 -27
  21. package/dist/block-policy.js.map +2 -2
  22. package/dist/browser-runtime.js +1039 -120
  23. package/dist/browser-runtime.js.map +4 -4
  24. package/dist/carp-catalog.d.ts +1 -0
  25. package/dist/carp-catalog.d.ts.map +1 -1
  26. package/dist/carp-catalog.js +34 -0
  27. package/dist/carp-catalog.js.map +4 -4
  28. package/dist/catalog-fec-params.js.map +1 -1
  29. package/dist/catalog-network-source.d.ts +1 -17
  30. package/dist/catalog-network-source.d.ts.map +1 -1
  31. package/dist/catalog-network-source.js +25 -19
  32. package/dist/catalog-network-source.js.map +3 -3
  33. package/dist/cmaf-block-assembler.d.ts +10 -2
  34. package/dist/cmaf-block-assembler.d.ts.map +1 -1
  35. package/dist/cmaf-block-assembler.js +30 -8
  36. package/dist/cmaf-block-assembler.js.map +3 -3
  37. package/dist/codec-utils.d.ts.map +1 -1
  38. package/dist/codec-utils.js +16 -3
  39. package/dist/codec-utils.js.map +2 -2
  40. package/dist/conceal-timeline.d.ts +24 -1
  41. package/dist/conceal-timeline.d.ts.map +1 -1
  42. package/dist/conceal-timeline.js +201 -4
  43. package/dist/conceal-timeline.js.map +3 -3
  44. package/dist/datagram-leg-deadline.d.ts +4 -0
  45. package/dist/datagram-leg-deadline.d.ts.map +1 -1
  46. package/dist/datagram-leg-deadline.js.map +2 -2
  47. package/dist/event-timeline-mmtp.d.ts +17 -0
  48. package/dist/event-timeline-mmtp.d.ts.map +1 -0
  49. package/dist/event-timeline-mmtp.js +3117 -0
  50. package/dist/event-timeline-mmtp.js.map +7 -0
  51. package/dist/fec-manager.d.ts +24 -3
  52. package/dist/fec-manager.d.ts.map +1 -1
  53. package/dist/fec-manager.js +531 -38
  54. package/dist/fec-manager.js.map +2 -2
  55. package/dist/feed-gate.d.ts +114 -0
  56. package/dist/feed-gate.d.ts.map +1 -0
  57. package/dist/feed-gate.js +175 -0
  58. package/dist/feed-gate.js.map +7 -0
  59. package/dist/index.d.ts +3 -2
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js +13 -1
  62. package/dist/index.js.map +2 -2
  63. package/dist/iso-bmff-timing.d.ts +16 -0
  64. package/dist/iso-bmff-timing.d.ts.map +1 -1
  65. package/dist/iso-bmff-timing.js +28 -0
  66. package/dist/iso-bmff-timing.js.map +2 -2
  67. package/dist/mfu-reassembler.d.ts +2 -0
  68. package/dist/mfu-reassembler.d.ts.map +1 -1
  69. package/dist/mfu-reassembler.js +4 -2
  70. package/dist/mfu-reassembler.js.map +2 -2
  71. package/dist/mmtp-container-parser.d.ts +12 -0
  72. package/dist/mmtp-container-parser.d.ts.map +1 -1
  73. package/dist/mmtp-container-parser.js +73 -3
  74. package/dist/mmtp-container-parser.js.map +2 -2
  75. package/dist/mmtp-container-parser.no-modules.js +103 -25
  76. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  77. package/dist/mmtp-decode-pipeline.js +73 -3
  78. package/dist/mmtp-decode-pipeline.js.map +2 -2
  79. package/dist/mmtp-router.d.ts +20 -2
  80. package/dist/mmtp-router.d.ts.map +1 -1
  81. package/dist/mmtp-router.js +150 -23
  82. package/dist/mmtp-router.js.map +3 -3
  83. package/dist/multicast-auth-catalog.js.map +1 -1
  84. package/dist/multicast-input.js.map +1 -1
  85. package/dist/multicast-manager.d.ts +23 -2
  86. package/dist/multicast-manager.d.ts.map +1 -1
  87. package/dist/multicast-manager.js +749 -87
  88. package/dist/multicast-manager.js.map +3 -3
  89. package/dist/multicast-transport.d.ts +8 -0
  90. package/dist/multicast-transport.d.ts.map +1 -1
  91. package/dist/multicast-transport.js +56 -23
  92. package/dist/multicast-transport.js.map +2 -2
  93. package/dist/no-modules.js +98 -22
  94. package/dist/no-modules.js.map +4 -4
  95. package/dist/repair-fetch.d.ts +15 -7
  96. package/dist/repair-fetch.d.ts.map +1 -1
  97. package/dist/repair-fetch.js +34 -7
  98. package/dist/repair-fetch.js.map +2 -2
  99. package/dist/wasm-mfu-reassembler.js.map +2 -2
  100. package/dist/wasm-mmtp-container-parser.js +73 -3
  101. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  102. package/package.json +17 -5
  103. package/schemas/catalog.schema.json +7 -2
  104. package/src/ad-beacon.ts +54 -0
  105. package/src/ad-creative-record.ts +276 -0
  106. package/src/ad-creative-timeline.ts +410 -0
  107. package/src/block-policy.ts +673 -42
  108. package/src/carp-catalog.ts +9 -0
  109. package/src/catalog-network-source.ts +3 -37
  110. package/src/cmaf-block-assembler.ts +17 -0
  111. package/src/codec-utils.ts +19 -3
  112. package/src/conceal-timeline.ts +60 -5
  113. package/src/datagram-leg-deadline.ts +4 -0
  114. package/src/event-timeline-mmtp.ts +55 -0
  115. package/src/fec-manager.ts +84 -12
  116. package/src/feed-gate.ts +295 -0
  117. package/src/index.ts +21 -0
  118. package/src/iso-bmff-timing.ts +33 -0
  119. package/src/mfu-reassembler.ts +4 -0
  120. package/src/mmtp-container-parser.ts +51 -5
  121. package/src/mmtp-router.ts +116 -4
  122. package/src/multicast-manager.ts +31 -0
  123. package/src/multicast-transport.ts +27 -2
  124. package/src/repair-fetch.ts +70 -19
  125. package/test-vectors/catalog/README.md +1 -0
  126. package/test-vectors/catalog/negative/event-timeline-empty-event-type.json +41 -0
  127. package/test-vectors/catalog/positive/event-timeline-ad-creative.json +41 -0
  128. package/test-vectors/catalog/positive/event-timeline-wallclock-no-depends.json +38 -0
  129. package/test-vectors/catalog/positive/multicast-event-timeline.json +81 -0
  130. package/test-vectors/catalog/section-codes.json +1 -0
  131. package/test-vectors/frame-needs-v1.json +295 -0
  132. package/test-vectors/repair-policy-v1.json +98 -18
@@ -0,0 +1,295 @@
1
+ /**
2
+ * The video feed gate: one loss rule for every codec.
3
+ *
4
+ * Each video frame states, in the `priority` byte of its MFU data-unit
5
+ * header, the one earlier frame it needs (its anchor). The rule:
6
+ *
7
+ * Feed a frame only if its anchor was fed since the last keyframe.
8
+ * A frame that states nothing needs every frame since the keyframe.
9
+ * A decoder known to conceal a missing reference is fed everything.
10
+ *
11
+ * The gate answers only inside a GOP whose keyframe carried 254, and only
12
+ * for a decoder that does not conceal. Everywhere else it answers `unread`
13
+ * and the sink runs its own stall and hold: a stream whose keyframes carry
14
+ * 255 takes no other path.
15
+ *
16
+ * - {@link decodeFrameNeeds} is the value map of the byte.
17
+ * - {@link decoderTolerance} is the one codec table.
18
+ * - {@link VideoFeedGate} is the rule. It keeps no timer and no deadline.
19
+ */
20
+
21
+ // ─── The value map ──────────────────────────────────────────────────
22
+
23
+ /** `priority` on a keyframe whose producer follows the map up to the next keyframe. */
24
+ export const FRAME_NEEDS_KEYFRAME = 254
25
+
26
+ /** What one `priority` byte states. */
27
+ export interface FrameStatement {
28
+ /** On a keyframe: every frame up to the next keyframe follows the map. */
29
+ readonly map: boolean
30
+ /** The anchor is this many frames back in decode order, 1 to 62. 0: nothing stated. */
31
+ readonly distance: number
32
+ /** False only when the frame states that no later frame needs it. */
33
+ readonly mayBeNeeded: boolean
34
+ }
35
+
36
+ const NOTHING_STATED: FrameStatement = Object.freeze({ map: false, distance: 0, mayBeNeeded: true })
37
+ const MAP_KEYFRAME: FrameStatement = Object.freeze({ map: true, distance: 0, mayBeNeeded: true })
38
+
39
+ /**
40
+ * Read one `priority` byte. On a keyframe only 254 states anything. On any
41
+ * other frame bit 6 is "a later frame may need me" and bits 5 to 0 are the
42
+ * distance back to the anchor; 0, 63, 64, 127 and every value above 127
43
+ * state nothing.
44
+ */
45
+ export function decodeFrameNeeds(code: number, keyframe: boolean): FrameStatement {
46
+ if (keyframe) return code === FRAME_NEEDS_KEYFRAME ? MAP_KEYFRAME : NOTHING_STATED
47
+ if (!Number.isInteger(code) || code < 1 || code > 126 || code === 63 || code === 64) {
48
+ return NOTHING_STATED
49
+ }
50
+ return { map: false, distance: code & 0x3f, mayBeNeeded: code > 64 }
51
+ }
52
+
53
+ // ─── The decoder table ──────────────────────────────────────────────
54
+
55
+ /**
56
+ * `conceals`: the decoder draws through a missing reference and recovers by
57
+ * itself, so it is fed everything. `none`: it errors, closes or draws a wrong
58
+ * picture, so a frame whose reference is missing is never fed.
59
+ */
60
+ export type DecoderTolerance = 'conceals' | 'none'
61
+
62
+ /**
63
+ * The only place a codec name decides loss handling. A page cannot ask which
64
+ * decoder it got, so a codec conceals only when every decoder known for it
65
+ * does: today that is H.264. Any other codec, known or not, gets `none`.
66
+ */
67
+ export function decoderTolerance(codec: string): DecoderTolerance {
68
+ const lower = codec.toLowerCase()
69
+ return lower.startsWith('avc1') || lower.startsWith('avc3') ? 'conceals' : 'none'
70
+ }
71
+
72
+ // ─── The gate ───────────────────────────────────────────────────────
73
+
74
+ /** One frame's statement as the router read it. Opaque to a sink: forward it like `gapBefore`. */
75
+ export interface FrameNeeds {
76
+ /** MPU_sequence_number of the frame. */
77
+ readonly mpu: number
78
+ /** The frame's place in decode order: its movie_fragment_sequence_number. */
79
+ readonly position: number
80
+ /** The `priority` byte. */
81
+ readonly code: number
82
+ }
83
+
84
+ /** `unread`: the gate states nothing about this frame, run your own code. */
85
+ export type GateAnswer = 'unread' | 'feed' | 'withhold'
86
+
87
+ export interface GateFrame {
88
+ /** Codec-aware RAP check on this frame's bitstream. */
89
+ readonly keyframe: boolean
90
+ readonly needs?: FrameNeeds
91
+ }
92
+
93
+ export interface VideoFeedGateOptions {
94
+ readonly tolerance: DecoderTolerance
95
+ }
96
+
97
+ export interface VideoFeedGateStats {
98
+ /** Frames fed after a missing frame of their GOP. */
99
+ fedAcrossGap: number
100
+ /** Frames the gate answered `withhold` for. A late or repeated frame is not counted. */
101
+ withheld: number
102
+ /** Decoder failures after a frame was fed across a gap. Must stay 0. */
103
+ selectiveFeedFailures: number
104
+ /** Statements that reached before their keyframe. Must stay 0. */
105
+ statementViolations: number
106
+ }
107
+
108
+ /**
109
+ * Per-decoder feed state. A sink asks {@link admit} about every frame that
110
+ * is not a keyframe, after its own stall test; reports with {@link fed} each
111
+ * frame it hands to its decoder or buffer, keyframes included; and calls
112
+ * {@link requireKeyframe} where it stalls, resets its decoder or empties its
113
+ * queue. "Fed" is what the sink reports, never what the gate allowed: a frame
114
+ * the sink drops for a reason of its own is not recorded, so every frame
115
+ * anchored on it is withheld. An answer rests on the reports made before it,
116
+ * so a sink reports one frame before it asks about the next.
117
+ */
118
+ export class VideoFeedGate {
119
+ readonly #inert: boolean
120
+ /** A statement was false, or a decoder failed on a selective feed: this track is read no more. */
121
+ #off = false
122
+ /** `hold`: reading stopped inside a GOP in which the gate had answered for a hole. */
123
+ #mode: 'unread' | 'reading' | 'hold' = 'unread'
124
+ #keyMpu = 0
125
+ #keyPosition = 0
126
+ /** Slot `position & 63` holds the position fed there: the last 64 positions. */
127
+ readonly #fedAt = new Float64Array(64).fill(-1)
128
+ /** Frames fed since the keyframe. */
129
+ #fedCount = 0
130
+ /** Place after the keyframe of the last frame fed. */
131
+ #top = 0
132
+ /** Place after the keyframe of the last frame the gate allowed. */
133
+ #allowed = 0
134
+ #withheldInGop = false
135
+ #fedAcrossGap = false
136
+ /** A decoder may report a failure after the next keyframe went in. */
137
+ #fedAcrossGapBefore = false
138
+ #lastWithheld: FrameNeeds | undefined
139
+ readonly #stats: VideoFeedGateStats = {
140
+ fedAcrossGap: 0,
141
+ withheld: 0,
142
+ selectiveFeedFailures: 0,
143
+ statementViolations: 0,
144
+ }
145
+
146
+ constructor(options: VideoFeedGateOptions) {
147
+ // Anything but an explicit `none` leaves the sink on its own code.
148
+ this.#inert = options.tolerance !== 'none'
149
+ }
150
+
151
+ /** True while the gate answers from the statements of the current GOP. */
152
+ get reading(): boolean {
153
+ return this.#mode === 'reading'
154
+ }
155
+
156
+ get stats(): Readonly<VideoFeedGateStats> {
157
+ return { ...this.#stats }
158
+ }
159
+
160
+ /**
161
+ * May this frame be fed? Records no frame as fed, so a sink may ask twice
162
+ * about one frame. `unread` for a keyframe, a concealing decoder, a GOP
163
+ * whose keyframe did not carry 254, and a track switched off. A late or
164
+ * repeated frame is refused with `withhold` and not counted.
165
+ */
166
+ admit(frame: GateFrame): GateAnswer {
167
+ if (frame.keyframe) return 'unread'
168
+ const needs = frame.needs
169
+ if (this.#mode === 'reading') {
170
+ if (needs === undefined || needs.code > 128) {
171
+ // No position, or a keyframe's byte on a frame the sink takes for none: this GOP cannot be read.
172
+ this.#stopReading()
173
+ } else {
174
+ const offset = this.#offset(needs)
175
+ // A late or repeated frame is no hole in the GOP.
176
+ if (offset < 0) return 'withhold'
177
+ if (offset === 0) return this.#withhold(needs)
178
+ const { distance } = decodeFrameNeeds(needs.code, false)
179
+ if (distance > offset) {
180
+ // An anchor is never earlier than the keyframe: this sender does not follow the map.
181
+ this.#stats.statementViolations++
182
+ this.#off = true
183
+ this.#stopReading()
184
+ } else {
185
+ const whole = this.#fedCount === offset - 1
186
+ const anchor = (needs.position - distance) >>> 0
187
+ const anchorFed = distance === 0
188
+ ? whole
189
+ : distance === offset || this.#fedAt[anchor & 63] === anchor
190
+ if (!anchorFed) return this.#withhold(needs)
191
+ this.#allowed = Math.max(this.#allowed, offset)
192
+ return 'feed'
193
+ }
194
+ }
195
+ }
196
+ return this.#mode === 'hold' ? this.#withhold(needs) : 'unread'
197
+ }
198
+
199
+ /** The sink handed this frame to its decoder or buffer. */
200
+ fed(frame: GateFrame): void {
201
+ const needs = frame.needs
202
+ if (frame.keyframe) {
203
+ this.#fedAcrossGapBefore = this.#fedAcrossGap
204
+ this.#fedAcrossGap = false
205
+ this.#clear()
206
+ if (
207
+ this.#inert || this.#off || needs === undefined ||
208
+ !decodeFrameNeeds(needs.code, true).map
209
+ ) return
210
+ this.#mode = 'reading'
211
+ this.#keyMpu = needs.mpu
212
+ this.#keyPosition = needs.position >>> 0
213
+ return
214
+ }
215
+ if (this.#mode !== 'reading') return
216
+ if (needs === undefined || needs.code > 128) {
217
+ this.#stopReading()
218
+ return
219
+ }
220
+ const offset = this.#offset(needs)
221
+ if (offset <= 0) return
222
+ if (this.#fedCount !== offset - 1) {
223
+ this.#fedAcrossGap = true
224
+ this.#stats.fedAcrossGap++
225
+ }
226
+ const position = needs.position >>> 0
227
+ this.#fedAt[position & 63] = position
228
+ this.#fedCount++
229
+ this.#top = offset
230
+ }
231
+
232
+ /** The sink stalled, reset its decoder or emptied its queue: nothing is read before the next keyframe. */
233
+ requireKeyframe(): void {
234
+ this.#clear()
235
+ }
236
+
237
+ /**
238
+ * The decoder raised an error or closed. Returns true when a frame had been
239
+ * fed across a gap: the decoder could not take the selective feed, so no
240
+ * statement is read on this track again. Either way the reading of this GOP
241
+ * stops, and the rest of a GOP with a hole the gate answered for is withheld.
242
+ */
243
+ onDecoderFailure(): boolean {
244
+ const selective = this.#fedAcrossGap || this.#fedAcrossGapBefore
245
+ this.#fedAcrossGap = false
246
+ this.#fedAcrossGapBefore = false
247
+ if (selective) {
248
+ this.#stats.selectiveFeedFailures++
249
+ this.#off = true
250
+ }
251
+ if (this.#mode === 'reading') this.#stopReading()
252
+ if (this.#mode !== 'hold') this.#clear()
253
+ return selective
254
+ }
255
+
256
+ #clear(): void {
257
+ this.#mode = 'unread'
258
+ this.#fedAt.fill(-1)
259
+ this.#fedCount = 0
260
+ this.#top = 0
261
+ this.#allowed = 0
262
+ this.#withheldInGop = false
263
+ }
264
+
265
+ /** The frame's place after the keyframe. 0: another MPU. -1: late or repeated, not after the last frame fed. */
266
+ #offset(needs: FrameNeeds): number {
267
+ // Positions may restart per MPU, so a stale bit of another MPU must never look like a fed anchor.
268
+ if (needs.mpu !== this.#keyMpu) return 0
269
+ const offset = (needs.position - this.#keyPosition) >>> 0
270
+ return offset > this.#top && offset < 0x8000_0000 ? offset : -1
271
+ }
272
+
273
+ /**
274
+ * The sink's own hold never saw a hole the gate answered for: a frame it
275
+ * withheld, or one it allowed that was not fed. The gate then holds the
276
+ * rest of that GOP itself.
277
+ */
278
+ #stopReading(): void {
279
+ this.#mode = this.#withheldInGop || this.#fedCount < this.#allowed ? 'hold' : 'unread'
280
+ }
281
+
282
+ #withhold(needs: FrameNeeds | undefined): GateAnswer {
283
+ this.#withheldInGop = true
284
+ const last = this.#lastWithheld
285
+ // A sink may ask twice about one frame: it is counted once.
286
+ if (
287
+ needs === undefined || last === undefined ||
288
+ needs.mpu !== last.mpu || needs.position !== last.position
289
+ ) {
290
+ this.#stats.withheld++
291
+ }
292
+ this.#lastWithheld = needs
293
+ return 'withhold'
294
+ }
295
+ }
package/src/index.ts CHANGED
@@ -145,6 +145,10 @@ export {
145
145
  mmtpTrackIssues,
146
146
  type ArchiveConfig,
147
147
  archiveOrigin,
148
+ catalogEventTimelineTracks,
149
+ type EventTimelineTrack,
150
+ type EventTimelineTrackIssue,
151
+ type EventTimelineTrackIssueCode,
148
152
  } from './carp-catalog.js'
149
153
 
150
154
  // Strict v2 multicast delivery catalog (DASH/HLS/MMTP routes), moved from
@@ -394,6 +398,7 @@ export {
394
398
  type ClassifierChange,
395
399
  type ClassifierMode,
396
400
  type ClassifierReason,
401
+ type MpuRuleOffReason,
397
402
  type DecideGapInput,
398
403
  type FrameGapAction,
399
404
  type FrameGapDecision,
@@ -422,6 +427,22 @@ export {
422
427
  type VideoConcealMode,
423
428
  } from './conceal-timeline.js'
424
429
 
430
+ // The video feed gate: per-frame dependency statements, the decoder table,
431
+ // and the one loss rule every player calls.
432
+ export {
433
+ FRAME_NEEDS_KEYFRAME,
434
+ VideoFeedGate,
435
+ decodeFrameNeeds,
436
+ decoderTolerance,
437
+ type DecoderTolerance,
438
+ type FrameNeeds,
439
+ type FrameStatement,
440
+ type GateAnswer,
441
+ type GateFrame,
442
+ type VideoFeedGateOptions,
443
+ type VideoFeedGateStats,
444
+ } from './feed-gate.js'
445
+
425
446
  // FEC Worker Setup (shared fec-setup helper)
426
447
  export {
427
448
  setupFecWorkerForTrack,
@@ -592,6 +592,7 @@ export class IsoBmffTimingResolver {
592
592
  #pendingSamples = 0
593
593
  #evictedSamples = 0
594
594
  #lastMovieFragmentRejection: IsoBmffMovieFragmentRejection | null = null
595
+ #lastMovieFragmentDecodeSpan: { first: bigint, last: bigint, timescale: number } | null = null
595
596
 
596
597
  constructor(readonly maxPendingSamples: number) {
597
598
  if (!Number.isInteger(maxPendingSamples) || maxPendingSamples <= 0) {
@@ -623,6 +624,16 @@ export class IsoBmffTimingResolver {
623
624
  return this.#lastMovieFragmentRejection
624
625
  }
625
626
 
627
+ /**
628
+ * Decode times of the first and the last sample of the movie fragment that
629
+ * the most recent `addMovieFragment` parsed, or null when it parsed none. A
630
+ * `trun` lists samples in decode order, so these bound the fragment. It is
631
+ * set whether or not the fragment was then admitted: the caller knows that.
632
+ */
633
+ get lastMovieFragmentDecodeSpan(): { first: bigint, last: bigint, timescale: number } | null {
634
+ return this.#lastMovieFragmentDecodeSpan
635
+ }
636
+
626
637
  addInitialization(data: Uint8Array): boolean {
627
638
  const timing = parseIsoBmffInitializationTiming(data)
628
639
  if (!timing) return false
@@ -645,6 +656,7 @@ export class IsoBmffTimingResolver {
645
656
  recovered = false,
646
657
  ): boolean {
647
658
  this.#lastMovieFragmentRejection = null
659
+ this.#lastMovieFragmentDecodeSpan = null
648
660
  if (!Number.isInteger(additionalSampleLimit) || additionalSampleLimit < 0) {
649
661
  this.#lastMovieFragmentRejection = 'invalid'
650
662
  return false
@@ -662,6 +674,15 @@ export class IsoBmffTimingResolver {
662
674
  this.#lastMovieFragmentRejection = 'invalid'
663
675
  return false
664
676
  }
677
+ const firstSample = parsed.samples[0]
678
+ const lastSample = parsed.samples[parsed.samples.length - 1]
679
+ if (firstSample && lastSample) {
680
+ this.#lastMovieFragmentDecodeSpan = {
681
+ first: firstSample.decodeTime,
682
+ last: lastSample.decodeTime,
683
+ timescale: firstSample.timescale,
684
+ }
685
+ }
665
686
  const key = fragmentKey(mpuSequenceNumber, parsed.movieFragmentSequenceNumber)
666
687
  const existing = this.#fragments.get(key)
667
688
  if (existing) {
@@ -727,6 +748,18 @@ export class IsoBmffTimingResolver {
727
748
  return samples?.[sampleNumber - 1] ?? null
728
749
  }
729
750
 
751
+ /**
752
+ * True when FT=1 declared exactly one sample for this movie fragment,
753
+ * pending or delivered. False once the fragment is forgotten.
754
+ */
755
+ movieFragmentHoldsOneSample(
756
+ mpuSequenceNumber: number,
757
+ movieFragmentSequenceNumber: number,
758
+ ): boolean {
759
+ const key = fragmentKey(mpuSequenceNumber, movieFragmentSequenceNumber)
760
+ return (this.#fragments.get(key)?.length ?? this.#delivered.get(key)?.sampleCount) === 1
761
+ }
762
+
730
763
  consumeSample(
731
764
  mpuSequenceNumber: number,
732
765
  movieFragmentSequenceNumber: number,
@@ -179,6 +179,8 @@ export interface ReassembledMFU {
179
179
  deliveryProvenance: MmtpDeliveryProvenance
180
180
  /** Catalog identity from the accepted FI=1 head fragment, or FI=0 frame. */
181
181
  catalogTrackName?: string
182
+ /** Non-timed MFU Item_ID of the head fragment (continuations inherit it). */
183
+ itemId?: number
182
184
  }
183
185
 
184
186
  /**
@@ -712,6 +714,7 @@ export class MFUReassembler {
712
714
  source: fragment.source,
713
715
  deliveryProvenance: deliveryProvenanceOf([fragment]),
714
716
  catalogTrackName: fragment.catalogTrackName,
717
+ itemId: fragment.itemId,
715
718
  }
716
719
  }
717
720
 
@@ -1251,6 +1254,7 @@ export class MFUReassembler {
1251
1254
  source: fragments[0]!.source,
1252
1255
  deliveryProvenance: deliveryProvenanceOf(fragments),
1253
1256
  catalogTrackName: fragments[0]!.catalogTrackName,
1257
+ itemId: fragments[0]!.itemId,
1254
1258
  }
1255
1259
  }
1256
1260
 
@@ -25,6 +25,7 @@ import {
25
25
  } from './sample-combiner.js'
26
26
  import {
27
27
  classifyH264Keyframe,
28
+ isCompleteAvccSample,
28
29
  isHevcIrapNalUnitType,
29
30
  mmtpTimestampToMicros,
30
31
  normalizeMmtpInitSegment,
@@ -201,6 +202,8 @@ export interface SourcePacketIdentity {
201
202
  movieFragmentSequenceNumber?: number
202
203
  /** DU-header offset: the data unit's authored byte position in its sample. */
203
204
  offset?: number
205
+ /** DU-header dep_counter, when this packet carries a DU header. */
206
+ dependencyCounter?: number
204
207
  }
205
208
 
206
209
  /**
@@ -504,6 +507,15 @@ export interface MmtpParserFeedStats {
504
507
 
505
508
  type FrameCallback = (data: Uint8Array, meta: FrameMeta) => void
506
509
  type InitCallback = (data: Uint8Array, packetId: number, catalogTrackName?: string) => void
510
+ /** Decode times of the first and last sample that one admitted FT=1 times, in `mediaTimescale` ticks. */
511
+ type MovieFragmentTimingCallback = (
512
+ packetId: number,
513
+ catalogTrackName: string | undefined,
514
+ mpuSequenceNumber: number,
515
+ first: bigint,
516
+ last: bigint,
517
+ mediaTimescale: number,
518
+ ) => void
507
519
 
508
520
  /**
509
521
  * Result of one combined-sample delivery attempt: either the record to return
@@ -1387,6 +1399,7 @@ export class MmtpContainerParser {
1387
1399
  #reassemblyPolicy: MfuReassemblyPolicy | null = null
1388
1400
  #frameCb: FrameCallback | null = null
1389
1401
  #initCb: InitCallback | null = null
1402
+ #movieFragmentTimingCb: MovieFragmentTimingCallback | null = null
1390
1403
  #sourceSymbolCb: SourceSymbolCallback | null = null
1391
1404
  #trackFrameCbs = new Map<number, FrameCallback>()
1392
1405
  #reorderStats: Omit<MmtpReorderStats, 'bufferedFrames'> = {
@@ -1706,6 +1719,7 @@ export class MmtpContainerParser {
1706
1719
  identity.sampleNumber = extraction.mfuDuHeader.sampleNumber
1707
1720
  identity.movieFragmentSequenceNumber = extraction.mfuDuHeader.movieFragmentSequenceNumber
1708
1721
  identity.offset = extraction.mfuDuHeader.offset
1722
+ identity.dependencyCounter = extraction.mfuDuHeader.dependencyCounter
1709
1723
  }
1710
1724
  return identity
1711
1725
  } catch {
@@ -2008,6 +2022,17 @@ export class MmtpContainerParser {
2008
2022
  }
2009
2023
  return this.#drop('fragment-timing-refused')
2010
2024
  }
2025
+ const span = resolver.lastMovieFragmentDecodeSpan
2026
+ if (span) {
2027
+ this.#movieFragmentTimingCb?.(
2028
+ info.packetId,
2029
+ options?.catalogTrackName,
2030
+ mpu.mpuSequenceNumber,
2031
+ span.first,
2032
+ span.last,
2033
+ span.timescale,
2034
+ )
2035
+ }
2011
2036
  this.#replayAwaitingTiming(streamKey, resolver)
2012
2037
  return {
2013
2038
  kind: 'fragment',
@@ -2277,6 +2302,15 @@ export class MmtpContainerParser {
2277
2302
  this.#initCb = cb
2278
2303
  }
2279
2304
 
2305
+ /**
2306
+ * Register a callback for the decode times of each movie fragment (FT=1)
2307
+ * the parser admits, arrived or FEC-recovered: what the receiver can
2308
+ * determine of its samples' decode times, whether or not they arrive.
2309
+ */
2310
+ onMovieFragmentTiming(cb: MovieFragmentTimingCallback): void {
2311
+ this.#movieFragmentTimingCb = cb
2312
+ }
2313
+
2280
2314
  onFrame(cb: FrameCallback): void {
2281
2315
  this.#frameCb = cb
2282
2316
  }
@@ -2448,6 +2482,17 @@ export class MmtpContainerParser {
2448
2482
  ?.sampleTiming(mpuSequenceNumber, movieFragmentSequenceNumber, sampleNumber) != null
2449
2483
  }
2450
2484
 
2485
+ /** Whether FT=1 declared exactly one sample for this movie fragment. False when that is not known. */
2486
+ movieFragmentHoldsOneSample(
2487
+ packetId: number,
2488
+ mpuSequenceNumber: number,
2489
+ movieFragmentSequenceNumber: number,
2490
+ catalogTrackName?: string,
2491
+ ): boolean {
2492
+ return this.#timingResolver(this.#streamKey(packetId, catalogTrackName), packetId, false)
2493
+ ?.movieFragmentHoldsOneSample(mpuSequenceNumber, movieFragmentSequenceNumber) === true
2494
+ }
2495
+
2451
2496
  /**
2452
2497
  * Claim and return the authored timing for a recovered sample. Recovery does
2453
2498
  * not pass through the ordinary `feed()` completion path, so it must retire
@@ -2527,6 +2572,7 @@ export class MmtpContainerParser {
2527
2572
  this.#reassembler.reset()
2528
2573
  this.#frameCb = null
2529
2574
  this.#initCb = null
2575
+ this.#movieFragmentTimingCb = null
2530
2576
  this.#trackFrameCbs.clear()
2531
2577
  }
2532
2578
 
@@ -2749,7 +2795,8 @@ export class MmtpContainerParser {
2749
2795
  const isHevc = c.startsWith('hev1') || c.startsWith('hvc1')
2750
2796
  if (isHevc) {
2751
2797
  if (data.length < 5) return undefined
2752
- if (startsWithAnnexB(data)) {
2798
+ // A 4-byte NAL length of 256..511 is 00 00 01 xx, the bytes of a start code.
2799
+ if (startsWithAnnexB(data) && !isCompleteAvccSample(data)) {
2753
2800
  return MmtpContainerParser.#scanAnnexBForKeyframe(data, true)
2754
2801
  }
2755
2802
  return MmtpContainerParser.#scanLengthPrefixedHevcForKeyframe(data)
@@ -2773,10 +2820,9 @@ export class MmtpContainerParser {
2773
2820
  data[offset + 3]!
2774
2821
  offset += 4
2775
2822
  if (nalLen <= 0) return sawNal ? false : undefined
2776
- // The router's sync probe may see only the first fragment of a large
2777
- // length-prefixed NAL. The length can exceed this packet, but the NAL
2778
- // header is still enough to classify IRAP (H.265 Table 7-1, 16..=23)
2779
- // and open the sync gate.
2823
+ // The buffer may hold only the first fragment of a large length-prefixed
2824
+ // NAL. The length then exceeds it, but the NAL header is still enough to
2825
+ // classify IRAP (H.265 Table 7-1, 16..=23).
2780
2826
  if (offset >= data.length) return sawNal ? false : undefined
2781
2827
  sawNal = true
2782
2828
  const hevcType = (data[offset]! >> 1) & 0x3f