@blockcast/mmt-container 2.2.0-main.cc080e5a56f2 → 2.2.0-main.cc3fe0684403
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.
- package/README.md +5 -2
- package/dist/block-policy.d.ts +88 -16
- package/dist/block-policy.d.ts.map +1 -1
- package/dist/block-policy.js +164 -56
- package/dist/block-policy.js.map +3 -3
- package/dist/browser-runtime.js +1485 -172
- package/dist/browser-runtime.js.map +4 -4
- package/dist/carp-catalog.d.ts +40 -1
- package/dist/carp-catalog.d.ts.map +1 -1
- package/dist/carp-catalog.js +163 -10
- package/dist/carp-catalog.js.map +4 -4
- package/dist/catalog-fec-params.d.ts +1 -1
- package/dist/catalog-fec-params.d.ts.map +1 -1
- package/dist/catalog-fec-params.js +48 -1
- package/dist/catalog-fec-params.js.map +3 -3
- package/dist/catalog-network-source.js +34 -0
- package/dist/catalog-network-source.js.map +3 -3
- package/dist/cmaf-block-assembler.js +92 -1
- package/dist/cmaf-block-assembler.js.map +2 -2
- package/dist/codec-utils.d.ts.map +1 -1
- package/dist/codec-utils.js +92 -1
- package/dist/codec-utils.js.map +2 -2
- package/dist/datagram-leg-deadline.d.ts +118 -0
- package/dist/datagram-leg-deadline.d.ts.map +1 -0
- package/dist/datagram-leg-deadline.js +217 -0
- package/dist/datagram-leg-deadline.js.map +7 -0
- package/dist/delivery-catalog.js +68 -0
- package/dist/delivery-catalog.js.map +4 -4
- package/dist/delta-fallback-config.js +39 -1
- package/dist/delta-fallback-config.js.map +4 -4
- package/dist/fec-manager.d.ts +155 -10
- package/dist/fec-manager.d.ts.map +1 -1
- package/dist/fec-manager.js +596 -81
- package/dist/fec-manager.js.map +4 -4
- package/dist/fec-state.d.ts +17 -5
- package/dist/fec-state.d.ts.map +1 -1
- package/dist/fec-state.js +41 -1
- package/dist/fec-state.js.map +3 -3
- package/dist/fec-worker-setup.js +34 -0
- package/dist/fec-worker-setup.js.map +3 -3
- package/dist/held-layer-admission.js +34 -0
- package/dist/held-layer-admission.js.map +3 -3
- package/dist/index.d.ts +7 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -1
- package/dist/index.js.map +2 -2
- package/dist/iso-block-locator.d.ts +16 -0
- package/dist/iso-block-locator.d.ts.map +1 -1
- package/dist/iso-block-locator.js +62 -0
- package/dist/iso-block-locator.js.map +3 -3
- package/dist/mfu-reassembler.js +34 -0
- package/dist/mfu-reassembler.js.map +3 -3
- package/dist/mmtp-container-parser.d.ts +30 -2
- package/dist/mmtp-container-parser.d.ts.map +1 -1
- package/dist/mmtp-container-parser.js +140 -2
- package/dist/mmtp-container-parser.js.map +3 -3
- package/dist/mmtp-container-parser.no-modules.js +293 -18
- package/dist/mmtp-container-parser.no-modules.js.map +4 -4
- package/dist/mmtp-decode-pipeline.js +140 -2
- package/dist/mmtp-decode-pipeline.js.map +3 -3
- package/dist/mmtp-fec-session.js +34 -0
- package/dist/mmtp-fec-session.js.map +3 -3
- package/dist/mmtp-mode-selection.d.ts +48 -0
- package/dist/mmtp-mode-selection.d.ts.map +1 -0
- package/dist/mmtp-mode-selection.js +79 -0
- package/dist/mmtp-mode-selection.js.map +7 -0
- package/dist/mmtp-router.d.ts +66 -8
- package/dist/mmtp-router.d.ts.map +1 -1
- package/dist/mmtp-router.js +348 -27
- package/dist/mmtp-router.js.map +4 -4
- package/dist/multicast-auth-catalog.js +42 -2
- package/dist/multicast-auth-catalog.js.map +3 -3
- package/dist/multicast-input.js +42 -2
- package/dist/multicast-input.js.map +3 -3
- package/dist/multicast-manager.d.ts +108 -1
- package/dist/multicast-manager.d.ts.map +1 -1
- package/dist/multicast-manager.js +1067 -138
- package/dist/multicast-manager.js.map +4 -4
- package/dist/multicast-transport.js +68 -0
- package/dist/multicast-transport.js.map +4 -4
- package/dist/multicast-unwrap.js +34 -0
- package/dist/multicast-unwrap.js.map +3 -3
- package/dist/no-modules.d.ts +1 -0
- package/dist/no-modules.d.ts.map +1 -1
- package/dist/no-modules.js +295 -19
- package/dist/no-modules.js.map +4 -4
- package/dist/phase14-catalog-override.d.ts +2 -1
- package/dist/phase14-catalog-override.d.ts.map +1 -1
- package/dist/phase14-catalog-override.js +69 -2
- package/dist/phase14-catalog-override.js.map +3 -3
- package/dist/repair-fetch.d.ts.map +1 -1
- package/dist/repair-fetch.js +50 -0
- package/dist/repair-fetch.js.map +3 -3
- package/dist/repair-layer-subscriptions.js +34 -0
- package/dist/repair-layer-subscriptions.js.map +3 -3
- package/dist/signaling.js +34 -0
- package/dist/signaling.js.map +3 -3
- package/dist/timestamp-codec.js +34 -0
- package/dist/timestamp-codec.js.map +3 -3
- package/dist/wasm-mfu-reassembler.js +34 -0
- package/dist/wasm-mfu-reassembler.js.map +3 -3
- package/dist/wasm-mmtp-container-parser.js +140 -2
- package/dist/wasm-mmtp-container-parser.js.map +3 -3
- package/package.json +5 -5
- package/schemas/catalog.schema.json +2 -2
- package/src/block-policy.ts +324 -83
- package/src/carp-catalog.ts +165 -5
- package/src/catalog-fec-params.ts +1 -0
- package/src/codec-utils.ts +121 -1
- package/src/datagram-leg-deadline.ts +342 -0
- package/src/fec-manager.ts +534 -41
- package/src/fec-state.ts +21 -5
- package/src/index.ts +38 -0
- package/src/iso-block-locator.ts +34 -0
- package/src/mmtp-container-parser.ts +57 -11
- package/src/mmtp-mode-selection.ts +80 -0
- package/src/mmtp-router.ts +287 -42
- package/src/multicast-manager.ts +218 -10
- package/src/no-modules.ts +3 -0
- package/src/phase14-catalog-override.ts +14 -3
- package/src/repair-fetch.ts +23 -0
- package/test-vectors/catalog/README.md +36 -2
- package/test-vectors/catalog/mmtp-siblings/ambiguous-sibling.json +158 -0
- package/test-vectors/catalog/mmtp-siblings/ladder-bitrate-only.json +231 -0
- package/test-vectors/catalog/mmtp-siblings/ladder-four-rungs.json +405 -0
- package/test-vectors/catalog/mmtp-siblings/ladder-other-names.json +231 -0
- package/test-vectors/catalog/mmtp-siblings/mmtp-mode-unknown.json +143 -0
- package/test-vectors/catalog/mmtp-siblings/multicast-endpoint-lists-datagram-track.json +143 -0
- package/test-vectors/catalog/mmtp-siblings/no-sibling.json +79 -0
- package/test-vectors/catalog/mmtp-siblings/rendition-mismatch.json +143 -0
- package/test-vectors/catalog/mmtp-siblings/stream-delivery-mode.json +143 -0
- package/test-vectors/catalog/mmtp-siblings/timescale-mismatch.json +143 -0
- package/test-vectors/catalog/mmtp-siblings/valid-pair.json +142 -0
- package/test-vectors/catalog/section-codes.json +3 -3
- package/test-vectors/catalog-timing.json +559 -1
- package/test-vectors/catalog-validation.json +266 -7
- package/test-vectors/moq/mmtp-mode-selection.json +259 -0
|
@@ -0,0 +1,342 @@
|
|
|
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. `() => computeDatagramLegDeadlines(...)` fits.
|
|
127
|
+
*/
|
|
128
|
+
readonly limits: () => Partial<DatagramLegLimits> | undefined
|
|
129
|
+
/** true: the subscription starts at the next group. false: the join brings the current group by a fill. */
|
|
130
|
+
readonly joinsAtNextGroup: boolean
|
|
131
|
+
/** Called once. The watch has stopped. */
|
|
132
|
+
readonly onVerdict: (verdict: DatagramLegVerdict, silentMs: number, limitMs: number) => void
|
|
133
|
+
/** The host's timers, where a player keeps its own set; tests. */
|
|
134
|
+
readonly timers?: {
|
|
135
|
+
setTimeout(handler: () => void, ms: number): unknown
|
|
136
|
+
clearTimeout(handle: unknown): void
|
|
137
|
+
}
|
|
138
|
+
readonly now?: () => number
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
interface WatchedClock {
|
|
142
|
+
readonly clock: 'dark' | 'frame'
|
|
143
|
+
readonly since: number
|
|
144
|
+
readonly limit: number
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The two clocks of a running "mfu-datagram" leg.
|
|
149
|
+
*
|
|
150
|
+
* - **dark** counts time with no source datagram of the track. A repair
|
|
151
|
+
* datagram, a fill or FETCH object and a frame do not count.
|
|
152
|
+
* - **frame** counts time with no decodable frame, from the first source
|
|
153
|
+
* datagram on.
|
|
154
|
+
*
|
|
155
|
+
* Only time in which the player reads the track counts (`setReading`). A
|
|
156
|
+
* clock that reaches its limit is first suspected, `confirmMs` early, and the
|
|
157
|
+
* verdict is given in a new timer turn if nothing arrived meanwhile: datagrams
|
|
158
|
+
* that queued while the page did not run are read in between. Arrivals only
|
|
159
|
+
* stamp a time; no timer call per datagram.
|
|
160
|
+
*/
|
|
161
|
+
export class DatagramLegWatch {
|
|
162
|
+
readonly #options: DatagramLegWatchOptions
|
|
163
|
+
readonly #setTimeout: (handler: () => void, ms: number) => unknown
|
|
164
|
+
readonly #clearTimeout: (handle: unknown) => void
|
|
165
|
+
readonly #now: () => number
|
|
166
|
+
#limits: DatagramLegLimits | undefined
|
|
167
|
+
#timer: unknown
|
|
168
|
+
#stopped = false
|
|
169
|
+
#pausedMs = 0
|
|
170
|
+
#pausedAt: number | undefined
|
|
171
|
+
#joinAt: number
|
|
172
|
+
#lastSourceAt: number | undefined
|
|
173
|
+
#frameClockAt: number | undefined
|
|
174
|
+
#chain = false
|
|
175
|
+
#suspect: { clock: 'dark' | 'frame'; since: number } | undefined
|
|
176
|
+
|
|
177
|
+
constructor(options: DatagramLegWatchOptions) {
|
|
178
|
+
this.#options = options
|
|
179
|
+
const timers = options.timers
|
|
180
|
+
this.#setTimeout = (handler, ms) => (timers ? timers.setTimeout(handler, ms) : setTimeout(handler, ms))
|
|
181
|
+
this.#clearTimeout = (handle) =>
|
|
182
|
+
timers ? timers.clearTimeout(handle) : clearTimeout(handle as ReturnType<typeof setTimeout>)
|
|
183
|
+
this.#now = options.now ?? (() => (typeof performance === 'undefined' ? Date.now() : performance.now()))
|
|
184
|
+
this.#joinAt = this.#clock()
|
|
185
|
+
this.#arm()
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** SUBSCRIBE_OK: the join clock starts again. */
|
|
189
|
+
subscribed(): void {
|
|
190
|
+
if (this.#stopped || this.#lastSourceAt !== undefined) return
|
|
191
|
+
this.#joinAt = this.#clock()
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** A source object of the track arrived as a datagram. Never call it for repair, fill or FETCH objects. */
|
|
195
|
+
source(): void {
|
|
196
|
+
if (this.#stopped) return
|
|
197
|
+
const at = this.#clock()
|
|
198
|
+
const first = this.#lastSourceAt === undefined
|
|
199
|
+
this.#lastSourceAt = at
|
|
200
|
+
this.#frameClockAt ??= at
|
|
201
|
+
// The first source datagram ends the join: the dark limit changes from
|
|
202
|
+
// joinDarkMs to darkMs, which can be the shorter one. Every later arrival
|
|
203
|
+
// only moves a limit out, so the armed timer stays.
|
|
204
|
+
if (first) this.#arm()
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* A frame left the leg. Without `meta` every frame counts (audio, or a sink
|
|
209
|
+
* that already counts decodable frames). With `meta` a frame counts when it
|
|
210
|
+
* is a keyframe, or stands behind one with no stall decision between.
|
|
211
|
+
*/
|
|
212
|
+
frame(meta?: { readonly keyframe?: boolean; readonly gapBefore?: { readonly action?: string } }): void {
|
|
213
|
+
if (this.#stopped) return
|
|
214
|
+
if (meta !== undefined) {
|
|
215
|
+
if (meta.keyframe === true) this.#chain = true
|
|
216
|
+
else if (meta.gapBefore?.action === 'stall') this.#chain = false
|
|
217
|
+
if (!this.#chain) return
|
|
218
|
+
}
|
|
219
|
+
if (this.#frameClockAt !== undefined) this.#frameClockAt = this.#clock()
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** false while the player itself does not read the track: that time is not silence. */
|
|
223
|
+
setReading(reading: boolean): void {
|
|
224
|
+
if (this.#stopped) return
|
|
225
|
+
if (!reading) {
|
|
226
|
+
this.#pausedAt ??= this.#now()
|
|
227
|
+
return
|
|
228
|
+
}
|
|
229
|
+
if (this.#pausedAt === undefined) return
|
|
230
|
+
this.#pausedMs += this.#now() - this.#pausedAt
|
|
231
|
+
this.#pausedAt = undefined
|
|
232
|
+
if (this.#timer === undefined) this.#arm()
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** The limits moved (the delay target shrank): arm on the new ones now. */
|
|
236
|
+
refresh(): void {
|
|
237
|
+
if (this.#stopped) return
|
|
238
|
+
this.#suspect = undefined
|
|
239
|
+
this.#arm()
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
stop(): void {
|
|
243
|
+
this.#stopped = true
|
|
244
|
+
this.#clear()
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** The leg's own time: it stands still while the player is not reading. */
|
|
248
|
+
#clock(): number {
|
|
249
|
+
return (this.#pausedAt ?? this.#now()) - this.#pausedMs
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
#clear(): void {
|
|
253
|
+
if (this.#timer === undefined) return
|
|
254
|
+
this.#clearTimeout(this.#timer)
|
|
255
|
+
this.#timer = undefined
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
#readLimits(): DatagramLegLimits | undefined {
|
|
259
|
+
const next = this.#options.limits()
|
|
260
|
+
const usable = (value: unknown): value is number => typeof value === 'number' && Number.isFinite(value) && value >= 0
|
|
261
|
+
if (next && usable(next.joinDarkMs) && usable(next.darkMs) && usable(next.noFrameMs)) {
|
|
262
|
+
this.#limits = {
|
|
263
|
+
joinDarkMs: next.joinDarkMs,
|
|
264
|
+
darkMs: next.darkMs,
|
|
265
|
+
noFrameMs: next.noFrameMs,
|
|
266
|
+
confirmMs: usable(next.confirmMs) ? next.confirmMs : 0,
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return this.#limits
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
#clocks(limits: DatagramLegLimits): WatchedClock[] {
|
|
273
|
+
const joining = this.#lastSourceAt === undefined && this.#options.joinsAtNextGroup
|
|
274
|
+
const clocks: WatchedClock[] = [
|
|
275
|
+
{
|
|
276
|
+
clock: 'dark',
|
|
277
|
+
since: this.#lastSourceAt ?? this.#joinAt,
|
|
278
|
+
limit: joining ? limits.joinDarkMs : limits.darkMs,
|
|
279
|
+
},
|
|
280
|
+
]
|
|
281
|
+
if (this.#frameClockAt !== undefined) {
|
|
282
|
+
clocks.push({ clock: 'frame', since: this.#frameClockAt, limit: limits.noFrameMs })
|
|
283
|
+
}
|
|
284
|
+
return clocks
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
#arm(): void {
|
|
288
|
+
this.#clear()
|
|
289
|
+
if (this.#stopped || this.#pausedAt !== undefined) return
|
|
290
|
+
const limits = this.#readLimits()
|
|
291
|
+
if (!limits) return
|
|
292
|
+
const at = this.#clock()
|
|
293
|
+
let wait = Number.POSITIVE_INFINITY
|
|
294
|
+
for (const clock of this.#clocks(limits)) {
|
|
295
|
+
wait = Math.min(wait, clock.since + clock.limit - limits.confirmMs - at)
|
|
296
|
+
}
|
|
297
|
+
this.#timer = this.#setTimeout(() => this.#suspectTurn(), Math.max(0, wait))
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
#suspectTurn(): void {
|
|
301
|
+
this.#timer = undefined
|
|
302
|
+
if (this.#stopped || this.#pausedAt !== undefined) return
|
|
303
|
+
const limits = this.#readLimits()
|
|
304
|
+
if (!limits) return
|
|
305
|
+
const at = this.#clock()
|
|
306
|
+
const reached = this.#clocks(limits).find((clock) => at >= clock.since + clock.limit - limits.confirmMs)
|
|
307
|
+
if (!reached) {
|
|
308
|
+
this.#arm()
|
|
309
|
+
return
|
|
310
|
+
}
|
|
311
|
+
this.#suspect = { clock: reached.clock, since: reached.since }
|
|
312
|
+
// Always a new timer turn, also when this one ran late: what queued while
|
|
313
|
+
// the page did not run is read before the verdict.
|
|
314
|
+
this.#timer = this.#setTimeout(() => this.#verdictTurn(), limits.confirmMs)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
#verdictTurn(): void {
|
|
318
|
+
this.#timer = undefined
|
|
319
|
+
const suspect = this.#suspect
|
|
320
|
+
this.#suspect = undefined
|
|
321
|
+
if (this.#stopped || suspect === undefined || this.#pausedAt !== undefined) return
|
|
322
|
+
const limits = this.#readLimits()
|
|
323
|
+
if (!limits) return
|
|
324
|
+
const at = this.#clock()
|
|
325
|
+
const clock = this.#clocks(limits).find((candidate) => candidate.clock === suspect.clock)
|
|
326
|
+
if (!clock || clock.since !== suspect.since) {
|
|
327
|
+
this.#arm()
|
|
328
|
+
return
|
|
329
|
+
}
|
|
330
|
+
const silentMs = at - clock.since
|
|
331
|
+
if (silentMs < clock.limit) {
|
|
332
|
+
// Never early: a timer that fired ahead of its time waits out the rest.
|
|
333
|
+
this.#suspect = suspect
|
|
334
|
+
this.#timer = this.#setTimeout(() => this.#verdictTurn(), clock.limit - silentMs)
|
|
335
|
+
return
|
|
336
|
+
}
|
|
337
|
+
this.#stopped = true
|
|
338
|
+
const joining = this.#lastSourceAt === undefined && this.#options.joinsAtNextGroup
|
|
339
|
+
const verdict: DatagramLegVerdict = clock.clock === 'frame' ? 'no-frame' : joining ? 'join' : 'dark'
|
|
340
|
+
this.#options.onVerdict(verdict, silentMs, clock.limit)
|
|
341
|
+
}
|
|
342
|
+
}
|