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

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 (124) hide show
  1. package/dist/block-policy.d.ts +33 -3
  2. package/dist/block-policy.d.ts.map +1 -1
  3. package/dist/block-policy.js +60 -24
  4. package/dist/block-policy.js.map +3 -3
  5. package/dist/browser-runtime.js +1034 -126
  6. package/dist/browser-runtime.js.map +4 -4
  7. package/dist/carp-catalog.d.ts +40 -1
  8. package/dist/carp-catalog.d.ts.map +1 -1
  9. package/dist/carp-catalog.js +163 -10
  10. package/dist/carp-catalog.js.map +4 -4
  11. package/dist/catalog-fec-params.js +34 -0
  12. package/dist/catalog-fec-params.js.map +3 -3
  13. package/dist/catalog-network-source.js +34 -0
  14. package/dist/catalog-network-source.js.map +3 -3
  15. package/dist/datagram-leg-deadline.d.ts +138 -0
  16. package/dist/datagram-leg-deadline.d.ts.map +1 -0
  17. package/dist/datagram-leg-deadline.js +229 -0
  18. package/dist/datagram-leg-deadline.js.map +7 -0
  19. package/dist/delivery-catalog.js +68 -0
  20. package/dist/delivery-catalog.js.map +4 -4
  21. package/dist/delta-fallback-config.js +34 -0
  22. package/dist/delta-fallback-config.js.map +3 -3
  23. package/dist/fec-manager.d.ts +117 -13
  24. package/dist/fec-manager.d.ts.map +1 -1
  25. package/dist/fec-manager.js +358 -42
  26. package/dist/fec-manager.js.map +4 -4
  27. package/dist/fec-state.d.ts +17 -5
  28. package/dist/fec-state.d.ts.map +1 -1
  29. package/dist/fec-state.js +36 -0
  30. package/dist/fec-state.js.map +3 -3
  31. package/dist/fec-worker-setup.js +34 -0
  32. package/dist/fec-worker-setup.js.map +3 -3
  33. package/dist/held-layer-admission.js +34 -0
  34. package/dist/held-layer-admission.js.map +3 -3
  35. package/dist/index.d.ts +6 -4
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +14 -0
  38. package/dist/index.js.map +2 -2
  39. package/dist/iso-block-locator.d.ts +16 -0
  40. package/dist/iso-block-locator.d.ts.map +1 -1
  41. package/dist/iso-block-locator.js +62 -0
  42. package/dist/iso-block-locator.js.map +3 -3
  43. package/dist/mfu-reassembler.js +34 -0
  44. package/dist/mfu-reassembler.js.map +3 -3
  45. package/dist/mmtp-container-parser.d.ts +6 -3
  46. package/dist/mmtp-container-parser.d.ts.map +1 -1
  47. package/dist/mmtp-container-parser.js +34 -0
  48. package/dist/mmtp-container-parser.js.map +3 -3
  49. package/dist/mmtp-container-parser.no-modules.js +167 -13
  50. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  51. package/dist/mmtp-decode-pipeline.js +34 -0
  52. package/dist/mmtp-decode-pipeline.js.map +3 -3
  53. package/dist/mmtp-fec-session.js +34 -0
  54. package/dist/mmtp-fec-session.js.map +3 -3
  55. package/dist/mmtp-mode-selection.d.ts +48 -0
  56. package/dist/mmtp-mode-selection.d.ts.map +1 -0
  57. package/dist/mmtp-mode-selection.js +79 -0
  58. package/dist/mmtp-mode-selection.js.map +7 -0
  59. package/dist/mmtp-router.d.ts +66 -10
  60. package/dist/mmtp-router.d.ts.map +1 -1
  61. package/dist/mmtp-router.js +204 -30
  62. package/dist/mmtp-router.js.map +4 -4
  63. package/dist/multicast-auth-catalog.js +42 -2
  64. package/dist/multicast-auth-catalog.js.map +3 -3
  65. package/dist/multicast-input.js +42 -2
  66. package/dist/multicast-input.js.map +3 -3
  67. package/dist/multicast-manager.d.ts +95 -1
  68. package/dist/multicast-manager.d.ts.map +1 -1
  69. package/dist/multicast-manager.js +613 -92
  70. package/dist/multicast-manager.js.map +4 -4
  71. package/dist/multicast-transport.js +68 -0
  72. package/dist/multicast-transport.js.map +4 -4
  73. package/dist/multicast-unwrap.js +34 -0
  74. package/dist/multicast-unwrap.js.map +3 -3
  75. package/dist/no-modules.js +167 -13
  76. package/dist/no-modules.js.map +4 -4
  77. package/dist/phase14-catalog-override.d.ts +2 -1
  78. package/dist/phase14-catalog-override.d.ts.map +1 -1
  79. package/dist/phase14-catalog-override.js +69 -2
  80. package/dist/phase14-catalog-override.js.map +3 -3
  81. package/dist/repair-fetch.d.ts.map +1 -1
  82. package/dist/repair-fetch.js +50 -0
  83. package/dist/repair-fetch.js.map +3 -3
  84. package/dist/repair-layer-subscriptions.js +34 -0
  85. package/dist/repair-layer-subscriptions.js.map +3 -3
  86. package/dist/signaling.js +34 -0
  87. package/dist/signaling.js.map +3 -3
  88. package/dist/timestamp-codec.js +34 -0
  89. package/dist/timestamp-codec.js.map +3 -3
  90. package/dist/wasm-mfu-reassembler.js +34 -0
  91. package/dist/wasm-mfu-reassembler.js.map +3 -3
  92. package/dist/wasm-mmtp-container-parser.js +34 -0
  93. package/dist/wasm-mmtp-container-parser.js.map +3 -3
  94. package/package.json +5 -5
  95. package/schemas/catalog.schema.json +2 -2
  96. package/src/block-policy.ts +108 -31
  97. package/src/carp-catalog.ts +165 -5
  98. package/src/datagram-leg-deadline.ts +370 -0
  99. package/src/fec-manager.ts +345 -33
  100. package/src/fec-state.ts +21 -5
  101. package/src/index.ts +37 -0
  102. package/src/iso-block-locator.ts +34 -0
  103. package/src/mmtp-container-parser.ts +6 -4
  104. package/src/mmtp-mode-selection.ts +80 -0
  105. package/src/mmtp-router.ts +226 -45
  106. package/src/multicast-manager.ts +126 -0
  107. package/src/phase14-catalog-override.ts +14 -3
  108. package/src/repair-fetch.ts +23 -0
  109. package/test-vectors/catalog/README.md +36 -2
  110. package/test-vectors/catalog/mmtp-siblings/ambiguous-sibling.json +158 -0
  111. package/test-vectors/catalog/mmtp-siblings/ladder-bitrate-only.json +231 -0
  112. package/test-vectors/catalog/mmtp-siblings/ladder-four-rungs.json +405 -0
  113. package/test-vectors/catalog/mmtp-siblings/ladder-other-names.json +231 -0
  114. package/test-vectors/catalog/mmtp-siblings/mmtp-mode-unknown.json +143 -0
  115. package/test-vectors/catalog/mmtp-siblings/multicast-endpoint-lists-datagram-track.json +143 -0
  116. package/test-vectors/catalog/mmtp-siblings/no-sibling.json +79 -0
  117. package/test-vectors/catalog/mmtp-siblings/rendition-mismatch.json +143 -0
  118. package/test-vectors/catalog/mmtp-siblings/stream-delivery-mode.json +143 -0
  119. package/test-vectors/catalog/mmtp-siblings/timescale-mismatch.json +143 -0
  120. package/test-vectors/catalog/mmtp-siblings/valid-pair.json +142 -0
  121. package/test-vectors/catalog/section-codes.json +3 -3
  122. package/test-vectors/catalog-timing.json +742 -1
  123. package/test-vectors/catalog-validation.json +266 -7
  124. package/test-vectors/moq/mmtp-mode-selection.json +259 -0
@@ -0,0 +1,370 @@
1
+ // When a player takes an "mfu-datagram" leg, and when it hands the rendition
2
+ // back to the "mfu" sibling (draft-ramadan-moq-mmt-02 4.3.1). Both follow from
3
+ // two values and no constant: the player's delay target and the FEC window
4
+ // the catalog signals for the datagram entry. One implementation for every
5
+ // player, with shared vectors in test-vectors/catalog-timing.json (group
6
+ // datagramLegDeadlines), so the three cannot drift.
7
+
8
+ import { fecSpanDeliveryWindowMs } from './track-session.js'
9
+
10
+ /** One FEC instance the receiver decodes on the "mfu-datagram" entry. */
11
+ export interface DatagramLegFecInstance {
12
+ /** `fec.interleaveDepthMs`: the whole send span of a block. */
13
+ readonly interleaveDepthMs?: number
14
+ /** `fec.reorderToleranceMs`. Absent adds nothing and is never synthesized. */
15
+ readonly reorderToleranceMs?: number
16
+ }
17
+
18
+ export interface DatagramLegDeadlineInput {
19
+ /**
20
+ * The instances decoded on the datagram entry, base instance first, a
21
+ * decoded keyframe overlay after it. Never the "mfu" sibling's: the two
22
+ * tracks have their own instances.
23
+ */
24
+ readonly fec: readonly DatagramLegFecInstance[]
25
+ /** The entry's group duration (`catalogGroupDurationMs`). */
26
+ readonly groupDurationMs: number | undefined
27
+ /** One media unit: 1000 / framerate for video, the codec frame for audio. Absent counts as 0. */
28
+ readonly mediaUnitMs?: number
29
+ /** The player's delay target for the kind: the value it hands libmmt as `presentationBudgetMs`. */
30
+ readonly delayTargetMs: number | undefined
31
+ /** The `mmtp-mode` override is "mfu-datagram". */
32
+ readonly forced?: boolean
33
+ }
34
+
35
+ export type DatagramLegDeadlineReason =
36
+ | 'ok'
37
+ /** The base instance signals no positive span, or a tolerance is negative: no window, so no deadline. Not taken, the forced mode included. */
38
+ | 'no-fec-span'
39
+ | 'no-group-duration'
40
+ | 'no-delay-target'
41
+ /** FEC could not rebuild a packet before it is presented. Not taken under `auto`; the forced mode runs it and still reports this. */
42
+ | 'fec-window-exceeds-delay-target'
43
+
44
+ /** The limits a `DatagramLegWatch` runs on, in whole ms (`confirmMs` as signaled). */
45
+ export interface DatagramLegLimits {
46
+ /** No source datagram since SUBSCRIBE_OK, on a join that starts at the next group: G + W. */
47
+ readonly joinDarkMs: number
48
+ /** No source datagram: max(D, W). The buffer is played out, and nothing on the leg can still arrive in time. */
49
+ readonly darkMs: number
50
+ /** No decodable frame while source datagrams arrive: max(D, G + u + W). One lost keyframe never ends the leg. */
51
+ readonly noFrameMs: number
52
+ /** Delay of the confirming timer turn: the base tolerance, else one media unit, else 0. */
53
+ readonly confirmMs: number
54
+ }
55
+
56
+ export interface DatagramLegDeadlines extends Partial<DatagramLegLimits> {
57
+ /** Whether the leg is taken. Decide this before the reliable reader is held off. */
58
+ readonly take: boolean
59
+ readonly reason: DatagramLegDeadlineReason
60
+ /** W: the largest `ceil(span + tolerance)` among the instances. */
61
+ readonly fecWindowMs?: number
62
+ }
63
+
64
+ const positive = (value: unknown): value is number => typeof value === 'number' && Number.isFinite(value) && value > 0
65
+
66
+ /** W, or undefined when the base instance has no positive span or a tolerance is negative. */
67
+ function fecWindow(fec: readonly DatagramLegFecInstance[] | undefined): number | undefined {
68
+ if (!Array.isArray(fec) || !positive(fec[0]?.interleaveDepthMs)) return undefined
69
+ let window = 0
70
+ for (const instance of fec) {
71
+ const tolerance = instance?.reorderToleranceMs
72
+ if (tolerance !== undefined && !(typeof tolerance === 'number' && Number.isFinite(tolerance) && tolerance >= 0)) {
73
+ return undefined
74
+ }
75
+ // An instance with no signaled span has no elapsed-time deadline of its own.
76
+ if (!positive(instance?.interleaveDepthMs)) continue
77
+ try {
78
+ window = Math.max(window, fecSpanDeliveryWindowMs(instance.interleaveDepthMs, tolerance))
79
+ } catch {
80
+ return undefined
81
+ }
82
+ }
83
+ return window
84
+ }
85
+
86
+ /**
87
+ * Whether an "mfu-datagram" leg is taken, and the limits it runs on. Pure and
88
+ * total: it never throws, and it never clamps. A window above the delay
89
+ * target is not taken (the forced mode runs it at `darkMs = W`), because the
90
+ * fix is the publisher's: declare the span it keeps.
91
+ */
92
+ export function computeDatagramLegDeadlines(input: DatagramLegDeadlineInput): DatagramLegDeadlines {
93
+ const window = fecWindow(input?.fec)
94
+ if (window === undefined) return { take: false, reason: 'no-fec-span' }
95
+ const group = input.groupDurationMs
96
+ if (!positive(group)) return { take: false, reason: 'no-group-duration', fecWindowMs: window }
97
+ const delay = input.delayTargetMs
98
+ if (!positive(delay)) return { take: false, reason: 'no-delay-target', fecWindowMs: window }
99
+ const unit = positive(input.mediaUnitMs) ? input.mediaUnitMs : 0
100
+ const tolerance = input.fec[0]?.reorderToleranceMs
101
+ const fits = window <= delay
102
+ return {
103
+ take: fits || input.forced === true,
104
+ reason: fits ? 'ok' : 'fec-window-exceeds-delay-target',
105
+ fecWindowMs: window,
106
+ joinDarkMs: Math.ceil(group + window),
107
+ darkMs: Math.ceil(Math.max(delay, window)),
108
+ noFrameMs: Math.ceil(Math.max(delay, group + unit + window)),
109
+ confirmMs: positive(tolerance) ? tolerance : unit,
110
+ }
111
+ }
112
+
113
+ /** Why a `DatagramLegWatch` ended the leg. */
114
+ export type DatagramLegVerdict =
115
+ /** No source datagram ever arrived on a join that starts at the next group. */
116
+ | 'join'
117
+ /** No source datagram for `darkMs`. */
118
+ | 'dark'
119
+ /** Source datagrams arrive and build no decodable frame for `noFrameMs`. */
120
+ | 'no-frame'
121
+
122
+ export interface DatagramLegWatchOptions {
123
+ /**
124
+ * The limits, read each time a clock is armed, so a delay target that moves
125
+ * is followed (call `refresh()` when it shrinks). A result without numbers
126
+ * keeps the last good ones; with none yet, the watch arms nothing until
127
+ * `refresh()`. `() => computeDatagramLegDeadlines(...)` fits.
128
+ */
129
+ readonly limits: () => Partial<DatagramLegLimits> | undefined
130
+ /** true: the subscription starts at the next group. false: the join brings the current group by a fill. */
131
+ readonly joinsAtNextGroup: boolean
132
+ /** Called once. The watch has stopped. */
133
+ readonly onVerdict: (verdict: DatagramLegVerdict, silentMs: number, limitMs: number) => void
134
+ /** The host's timers, where a player keeps its own set; tests. */
135
+ readonly timers?: {
136
+ setTimeout(handler: () => void, ms: number): unknown
137
+ clearTimeout(handle: unknown): void
138
+ }
139
+ readonly now?: () => number
140
+ }
141
+
142
+ interface WatchedClock {
143
+ readonly clock: 'dark' | 'frame'
144
+ readonly since: number
145
+ readonly limit: number
146
+ }
147
+
148
+ /**
149
+ * The two clocks of a running "mfu-datagram" leg.
150
+ *
151
+ * - **dark** counts time with no source datagram of the track. A repair
152
+ * datagram, a fill or FETCH object and a frame do not count.
153
+ * - **frame** counts time with no decodable frame, from the first source
154
+ * datagram on.
155
+ *
156
+ * Only time in which the player reads the track counts (`setReading`). A
157
+ * clock that reaches its limit is first suspected, `confirmMs` early, and the
158
+ * verdict is given in a new timer turn if nothing arrived meanwhile: datagrams
159
+ * that queued while the page did not run are read in between. Arrivals only
160
+ * stamp a time; no timer call per datagram.
161
+ */
162
+ export class DatagramLegWatch {
163
+ readonly #options: DatagramLegWatchOptions
164
+ readonly #setTimeout: (handler: () => void, ms: number) => unknown
165
+ readonly #clearTimeout: (handle: unknown) => void
166
+ readonly #now: () => number
167
+ #limits: DatagramLegLimits | undefined
168
+ #timer: unknown
169
+ #stopped = false
170
+ #pausedMs = 0
171
+ #pausedAt: number | undefined
172
+ #joinAt: number
173
+ #lastSourceAt: number | undefined
174
+ #frameClockAt: number | undefined
175
+ #chain = false
176
+ #suspect: { clock: 'dark' | 'frame'; since: number } | undefined
177
+
178
+ constructor(options: DatagramLegWatchOptions) {
179
+ this.#options = options
180
+ const timers = options.timers
181
+ this.#setTimeout = (handler, ms) => (timers ? timers.setTimeout(handler, ms) : setTimeout(handler, ms))
182
+ this.#clearTimeout = (handle) =>
183
+ timers ? timers.clearTimeout(handle) : clearTimeout(handle as ReturnType<typeof setTimeout>)
184
+ this.#now = options.now ?? (() => (typeof performance === 'undefined' ? Date.now() : performance.now()))
185
+ this.#joinAt = this.#clock()
186
+ this.#arm()
187
+ }
188
+
189
+ /** SUBSCRIBE_OK: the join clock starts again. */
190
+ subscribed(): void {
191
+ if (this.#stopped || this.#lastSourceAt !== undefined) return
192
+ this.#joinAt = this.#clock()
193
+ }
194
+
195
+ /** A source object of the track arrived as a datagram. Never call it for repair, fill or FETCH objects. */
196
+ source(): void {
197
+ if (this.#stopped) return
198
+ const at = this.#clock()
199
+ const first = this.#lastSourceAt === undefined
200
+ this.#lastSourceAt = at
201
+ this.#frameClockAt ??= at
202
+ // The first source datagram ends the join: the dark limit changes from
203
+ // joinDarkMs to darkMs, which can be the shorter one. Every later arrival
204
+ // only moves a limit out, so the armed timer stays.
205
+ if (first) this.#arm()
206
+ }
207
+
208
+ /**
209
+ * A frame left the leg. Without `meta` every frame counts (audio, or a sink
210
+ * that already counts decodable frames). With `meta` a frame counts when it
211
+ * is a keyframe, or stands behind one with no stall decision between.
212
+ */
213
+ frame(meta?: { readonly keyframe?: boolean; readonly gapBefore?: { readonly action?: string } }): void {
214
+ if (this.#stopped) return
215
+ if (meta !== undefined) {
216
+ if (meta.keyframe === true) this.#chain = true
217
+ else if (meta.gapBefore?.action === 'stall') this.#chain = false
218
+ if (!this.#chain) return
219
+ }
220
+ if (this.#frameClockAt !== undefined) this.#frameClockAt = this.#clock()
221
+ }
222
+
223
+ /** false while the player itself does not read the track: that time is not silence. */
224
+ setReading(reading: boolean): void {
225
+ if (this.#stopped) return
226
+ if (!reading) {
227
+ this.#pausedAt ??= this.#now()
228
+ return
229
+ }
230
+ if (this.#pausedAt === undefined) return
231
+ this.#pausedMs += this.#now() - this.#pausedAt
232
+ this.#pausedAt = undefined
233
+ if (this.#timer === undefined) this.#arm()
234
+ }
235
+
236
+ /**
237
+ * The limits moved: arm on the new ones now. Call it when the delay target
238
+ * shrinks, and when `limits()` first has numbers after the watch started
239
+ * without them: an unarmed watch stays unarmed through frames and through
240
+ * every source datagram but the first.
241
+ */
242
+ refresh(): void {
243
+ if (this.#stopped) return
244
+ this.#suspect = undefined
245
+ this.#arm()
246
+ }
247
+
248
+ stop(): void {
249
+ this.#stopped = true
250
+ this.#clear()
251
+ }
252
+
253
+ /** The leg's own time: it stands still while the player is not reading. */
254
+ #clock(): number {
255
+ return (this.#pausedAt ?? this.#now()) - this.#pausedMs
256
+ }
257
+
258
+ #clear(): void {
259
+ if (this.#timer === undefined) return
260
+ this.#clearTimeout(this.#timer)
261
+ this.#timer = undefined
262
+ }
263
+
264
+ #readLimits(): DatagramLegLimits | undefined {
265
+ const next = this.#options.limits()
266
+ const usable = (value: unknown): value is number => typeof value === 'number' && Number.isFinite(value) && value >= 0
267
+ if (next && usable(next.joinDarkMs) && usable(next.darkMs) && usable(next.noFrameMs)) {
268
+ this.#limits = {
269
+ joinDarkMs: next.joinDarkMs,
270
+ darkMs: next.darkMs,
271
+ noFrameMs: next.noFrameMs,
272
+ confirmMs: usable(next.confirmMs) ? next.confirmMs : 0,
273
+ }
274
+ }
275
+ return this.#limits
276
+ }
277
+
278
+ #clocks(limits: DatagramLegLimits): WatchedClock[] {
279
+ const joining = this.#lastSourceAt === undefined && this.#options.joinsAtNextGroup
280
+ const clocks: WatchedClock[] = [
281
+ {
282
+ clock: 'dark',
283
+ since: this.#lastSourceAt ?? this.#joinAt,
284
+ limit: joining ? limits.joinDarkMs : limits.darkMs,
285
+ },
286
+ ]
287
+ if (this.#frameClockAt !== undefined) {
288
+ clocks.push({ clock: 'frame', since: this.#frameClockAt, limit: limits.noFrameMs })
289
+ }
290
+ return clocks
291
+ }
292
+
293
+ #arm(): void {
294
+ this.#clear()
295
+ if (this.#stopped || this.#pausedAt !== undefined) return
296
+ const limits = this.#readLimits()
297
+ if (!limits) return
298
+ const at = this.#clock()
299
+ let wait = Number.POSITIVE_INFINITY
300
+ for (const clock of this.#clocks(limits)) {
301
+ wait = Math.min(wait, clock.since + clock.limit - limits.confirmMs - at)
302
+ }
303
+ this.#timer = this.#setTimeout(() => this.#suspectTurn(), Math.max(0, wait))
304
+ }
305
+
306
+ #suspectTurn(): void {
307
+ this.#timer = undefined
308
+ if (this.#stopped || this.#pausedAt !== undefined) return
309
+ const limits = this.#readLimits()
310
+ if (!limits) return
311
+ const at = this.#clock()
312
+ const reached = this.#clocks(limits).find((clock) => at >= clock.since + clock.limit - limits.confirmMs)
313
+ if (!reached) {
314
+ this.#arm()
315
+ return
316
+ }
317
+ this.#suspect = { clock: reached.clock, since: reached.since }
318
+ // Always a new timer turn, also when this one ran late: what queued while
319
+ // the page did not run is read before the verdict.
320
+ this.#timer = this.#setTimeout(() => this.#verdictTurn(), limits.confirmMs)
321
+ }
322
+
323
+ #verdictTurn(): void {
324
+ this.#timer = undefined
325
+ const suspect = this.#suspect
326
+ this.#suspect = undefined
327
+ if (this.#stopped || suspect === undefined || this.#pausedAt !== undefined) return
328
+ const limits = this.#readLimits()
329
+ if (!limits) return
330
+ const at = this.#clock()
331
+ const clock = this.#clocks(limits).find((candidate) => candidate.clock === suspect.clock)
332
+ if (!clock || clock.since !== suspect.since) {
333
+ this.#arm()
334
+ return
335
+ }
336
+ const silentMs = at - clock.since
337
+ if (silentMs < clock.limit) {
338
+ // Never early: a timer that fired ahead of its time waits out the rest.
339
+ this.#suspect = suspect
340
+ this.#timer = this.#setTimeout(() => this.#verdictTurn(), clock.limit - silentMs)
341
+ return
342
+ }
343
+ this.#stopped = true
344
+ const joining = this.#lastSourceAt === undefined && this.#options.joinsAtNextGroup
345
+ const verdict: DatagramLegVerdict = clock.clock === 'frame' ? 'no-frame' : joining ? 'join' : 'dark'
346
+ this.#options.onVerdict(verdict, silentMs, clock.limit)
347
+ }
348
+ }
349
+
350
+ const wholeCount = (value: unknown): number =>
351
+ typeof value === 'number' && Number.isFinite(value) && value >= 0 ? Math.floor(value) : 0
352
+
353
+ /**
354
+ * How long a player stays on the "mfu" sibling before it takes the
355
+ * "mfu-datagram" leg again, after the leg was handed back on a timer:
356
+ * `ceil(joinDarkMs x 2^n x (1 + rand))`. `n` counts the timed hand-backs in a
357
+ * row (0 after the first; the player resets it once a leg held the rendition
358
+ * longer than its last wait). `rand` in [0, 1) comes from the caller, so the
359
+ * function stays pure and its vectors exact.
360
+ *
361
+ * Never throws, and never waits less than `joinDarkMs`: an `n` that is not a
362
+ * finite number >= 0 counts as 0 (a fraction is floored), a `rand` outside
363
+ * [0, 1) counts as 0. A `joinDarkMs` that is not a finite number > 0 has no
364
+ * leg to return to: undefined. A very large `n` yields Infinity, never a wrap.
365
+ */
366
+ export function datagramLegRetryDelayMs(joinDarkMs: number, n: number, rand: number): number | undefined {
367
+ if (!positive(joinDarkMs)) return undefined
368
+ const jitter = typeof rand === 'number' && rand >= 0 && rand < 1 ? rand : 0
369
+ return Math.ceil(joinDarkMs * 2 ** wholeCount(n) * (1 + jitter))
370
+ }