@torrent-tv/proxy 2.88.5 → 2.89.0

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 (50) hide show
  1. package/AGENTS.md +21 -12
  2. package/CHANGELOG.md +18 -0
  3. package/assets/calibration/audio/NOTICE.md +19 -19
  4. package/assets/calibration/audio/manifest.json +98 -98
  5. package/docs/container-architecture.md +77 -22
  6. package/docs/playback-start-forecast.md +56 -56
  7. package/main2.cpuprofile +1 -0
  8. package/package.json +1 -1
  9. package/scripts/build-audio-samples.mjs +31 -31
  10. package/server.js +42 -27
  11. package/services/encode/EncodeRuns.js +3 -1
  12. package/services/encode/OutputOpening.js +5 -4
  13. package/services/encode/audio-calibration.js +67 -67
  14. package/services/encode/audio-work-rate.js +15 -15
  15. package/services/encode/output/index.js +4 -0
  16. package/services/encode/run-command.js +2 -1
  17. package/services/media/ContainerOrchestrator.js +99 -46
  18. package/services/media/KeyframeTables.js +61 -21
  19. package/services/media/SubtitleCues.js +255 -183
  20. package/services/media/container/AviContainer.js +5 -3
  21. package/services/media/container/Container.js +66 -43
  22. package/services/media/container/ContainerFactory.js +8 -26
  23. package/services/media/container/MatroskaContainer.js +719 -1182
  24. package/services/media/container/Mp4Container.js +83 -73
  25. package/services/media/container/ebml-stream.js +174 -0
  26. package/services/media/container/matroska-clusters.js +1165 -0
  27. package/services/media/container/unavailable.js +77 -0
  28. package/services/media/tracks/ContainerTrack.js +9 -0
  29. package/services/media/tracks/TextSubtitleTrack.js +9 -1
  30. package/services/server/ViewerRequests.js +3 -2
  31. package/services/server/playback-readiness.js +516 -516
  32. package/services/torrent/worker/client.js +6 -2
  33. package/services/torrent/worker/held-bytes.js +84 -8
  34. package/services/torrent/worker/worker.js +10 -2
  35. package/services/transport/data-channel-handler.js +7 -3
  36. package/services/viewer/PriorityMap.js +31 -0
  37. package/test/audio-calibration.test.js +23 -23
  38. package/test/coalesce.test.js +39 -0
  39. package/test/held-read-ownership.test.js +77 -0
  40. package/test/helpers/held-file.js +50 -2
  41. package/test/helpers/matroska-file.js +296 -0
  42. package/test/keyframe-table-budget.test.js +21 -0
  43. package/test/matroska-cluster-discovery.test.js +323 -0
  44. package/test/reads-not-yet-arrived.test.js +218 -0
  45. package/test/segment-store.test.js +14 -0
  46. package/test/subtitle-cue-walk.test.js +16 -1
  47. package/test/subtitle-plan-waits.test.js +125 -0
  48. package/test/subtitle-track-numbering.test.js +8 -3
  49. package/test/viewer-starts-on-map.test.js +27 -0
  50. package/utils/coalesce.js +41 -0
@@ -1,516 +1,516 @@
1
- /**
2
- * Predict the minimum delay before playback can run to the end without
3
- * exhausting the continuous media available for the required tracks.
4
- *
5
- * This module contains the playback decision's only model. Its inputs are
6
- * measurements and physical limits: source bytes and download rate, encoded
7
- * media and encoder rate, segment sizes and client-link rate, existing
8
- * continuous media, and measured supply interruptions. Each service integral
9
- * extends its latest measured rate until the next observation. The forecast
10
- * is conditional on these measured services continuing. Browser storage
11
- * capacity is not an admission condition. There are no fitted weights or
12
- * rate multipliers, and no search over candidate start times.
13
- */
14
-
15
- /**
16
- * The mean rate predicted over a horizon by the integral of the measured rate.
17
- * Polling does not change the last measured service rate.
18
- *
19
- * @param {Array<{ at: number, value: number }>} readings
20
- * @param {number} now
21
- * @param {number} horizonSeconds
22
- * @returns {number | null}
23
- */
24
- export function forecastRate(readings, now = Date.now(), horizonSeconds = 0) {
25
- const curve = rateCurve(readings, now);
26
- if (!curve) {
27
- return null;
28
- }
29
- const horizon = Number.isFinite(horizonSeconds) ? Math.max(0, horizonSeconds) : 0;
30
- return horizon > 0 ? curve.workBy(horizon) / horizon : curve.rateAt(0);
31
- }
32
-
33
- /**
34
- * Measurements over the output's existing look-ahead horizon. The oldest
35
- * boundary sample is retained until another sample replaces it, so a repeated
36
- * report never changes the history or invents a new measurement.
37
- */
38
- export class RateTrend {
39
- #readings = [];
40
- #horizonMs;
41
-
42
- constructor(horizonSeconds = Number.POSITIVE_INFINITY) {
43
- this.#horizonMs = Math.max(0, horizonSeconds) * 1000;
44
- }
45
-
46
- add(at, value) {
47
- if (!Number.isFinite(at) || !Number.isFinite(value) || value < 0 ||
48
- at <= (this.#readings.at(-1)?.at ?? Number.NEGATIVE_INFINITY)) {
49
- return false;
50
- }
51
- this.#readings.push({ at, value });
52
- const cutoff = at - this.#horizonMs;
53
- while (this.#readings.length > 1 && this.#readings[1].at <= cutoff) {
54
- this.#readings.shift();
55
- }
56
- return true;
57
- }
58
-
59
- snapshot() {
60
- if (this.#readings.length === 0) {
61
- return null;
62
- }
63
- return summarizeRate(this.#readings);
64
- }
65
- }
66
-
67
- /**
68
- * @param {object} input
69
- * @param {number} input.positionSeconds
70
- * @param {number} input.durationSeconds
71
- * @param {number} input.bufferedAheadSeconds
72
- * @param {number} input.bufferLimitSeconds
73
- * @param {number} input.reserveSeconds - Measured source-stall coverage, in media seconds.
74
- * @param {number} input.lookaheadSeconds
75
- * @param {number} input.now
76
- * @param {boolean} [input.requiredAudio]
77
- * @param {Array<{ id: string, serviceId?: string, complete: boolean, bytesPerMediaSecond: number,
78
- * readings: Array<{ at: number, value: number }> }>} input.sources
79
- * @param {Array<{ id: string, sourceIds: string[], processedSeconds: number,
80
- * bitsPerMediaSecond: number, readings: Array<{ at: number, value: number }>,
81
- * segments: Array<{ index: number, startSeconds: number, endSeconds: number }>,
82
- * readySegmentIndices: number[], segmentSizesBytes: Map<number, number> | object }>} input.tracks
83
- * @param {Array<{ at: number, value: number }>} input.linkReadings - Bits/s.
84
- * @returns {object}
85
- */
86
- export function predictPlaybackReadiness(input = {}) {
87
- const now = Number.isFinite(input.now) ? input.now : Date.now();
88
- const tracks = Array.isArray(input.tracks) ? input.tracks : [];
89
- const mediaOrigin = Math.max(0, ...tracks.map((track) =>
90
- track.segments?.find(({ index }) => index === 0)?.mediaRanges?.[0]?.start ?? 0));
91
- const position = Math.max(mediaOrigin, finiteNonNegative(input.positionSeconds));
92
- const duration = finiteNonNegative(input.durationSeconds);
93
- const remaining = Math.max(0, duration - position);
94
- const measuredRanges = tracks.map((track) => Array.isArray(track.clientRanges) ?
95
- normalizedRanges(track.clientRanges) : null);
96
- const buffered = measuredRanges.length > 0 && measuredRanges.every(Boolean) ?
97
- Math.max(0, Math.min(...measuredRanges.map((ranges) => contiguousEnd(ranges, position) - position))) :
98
- finiteNonNegative(input.bufferedAheadSeconds);
99
- const reserve = Math.min(remaining, finiteNonNegative(input.reserveSeconds));
100
- const sources = new Map((Array.isArray(input.sources) ? input.sources : [])
101
- .filter((source) => typeof source?.id === "string")
102
- .map((source) => [source.id, source]));
103
- if (!(duration > 0) || tracks.length === 0) {
104
- return result(false, null, buffered, reserve, null, "incomplete-state", 0);
105
- }
106
- const requiredTracks = input.requiredAudio === true ? 2 : 1;
107
- if (tracks.length < requiredTracks) {
108
- return result(false, null, buffered, reserve, null, "separate-audio-not-observed", 0);
109
- }
110
- if (remaining <= buffered) {
111
- return result(true, 0, buffered, reserve, 0, "client-buffer-covers-end", 0);
112
- }
113
- if (tracks.some((track) => !Array.isArray(track.segments) || track.segments.length === 0)) {
114
- return result(false, null, buffered, reserve, null, "timeline-unavailable", 0);
115
- }
116
-
117
- const trackState = tracks.map((track) => ({
118
- track,
119
- clientRanges: Array.isArray(track.clientRanges) ? normalizedRanges(track.clientRanges) :
120
- [{ start: position, end: position + buffered }],
121
- ready: new Set(Array.isArray(track.readySegmentIndices) ? track.readySegmentIndices : []),
122
- curve: rateCurve(track.readings, now),
123
- bitsPerMediaSecond: averageBitsPerMediaSecond(track, track.segments,
124
- new Set(Array.isArray(track.readySegmentIndices) ? track.readySegmentIndices : [])),
125
- segments: predictedMediaSegments(track.segments)
126
- .filter((segment) => Number.isInteger(segment?.index) &&
127
- Number.isFinite(segment?.startSeconds) && Number.isFinite(segment?.endSeconds) &&
128
- segment.endSeconds > segment.startSeconds && segment.endSeconds > position)
129
- .sort((left, right) => left.startSeconds - right.startSeconds || left.index - right.index)
130
- }));
131
- if (trackState.some(({ segments }) => segments.length === 0)) {
132
- return result(false, null, buffered, reserve, null, "timeline-unavailable", 0);
133
- }
134
- const sourceState = new Map();
135
- const downloadServices = new Map();
136
- for (const [id, source] of sources) {
137
- const serviceId = typeof source.serviceId === "string" ? source.serviceId : id;
138
- const service = downloadServices.get(serviceId);
139
- if (!service || (!service.curve && source.complete !== true)) {
140
- downloadServices.set(serviceId, {
141
- curve: source.complete === true ? null : rateCurve(source.readings, now)
142
- });
143
- }
144
- sourceState.set(id, {
145
- source,
146
- serviceId
147
- });
148
- }
149
- const link = rateCurve(input.linkReadings, now);
150
- let unknownReason = null;
151
- if (!link) {
152
- return result(false, null, buffered, reserve, null, "link-rate-unavailable", 0);
153
- }
154
-
155
- const preparedSegments = trackState.reduce((sum, { ready, segments }) =>
156
- sum + segments.filter((segment) => ready.has(segment.index)).length, 0);
157
- const jobs = [];
158
-
159
- for (let trackIndex = 0; trackIndex < trackState.length; trackIndex += 1) {
160
- const state = trackState[trackIndex];
161
- const { track, segments } = state;
162
- const measuredBitsPerMediaSecond = state.bitsPerMediaSecond;
163
- for (const segment of segments) {
164
- if (coveredSeconds(state.clientRanges, Math.max(position, segment.startSeconds), segment.endSeconds) >=
165
- segment.endSeconds - Math.max(position, segment.startSeconds) - Number.EPSILON * duration) {
166
- continue;
167
- }
168
- const exactBytes = sizeOf(track.segmentSizesBytes, segment.index);
169
- const segmentSeconds = segment.endSeconds - segment.startSeconds;
170
- // HLS fetches the complete file when any required part is absent.
171
- const sizeBits = exactBytes > 0
172
- ? exactBytes * 8
173
- : measuredBitsPerMediaSecond * segmentSeconds;
174
- if (!(sizeBits > 0)) {
175
- unknownReason ??= "segment-size-unavailable";
176
- }
177
-
178
- const produced = state.ready.has(segment.index);
179
- // Attribute unfinished work to the actual run interval. A global
180
- // position from another run must not erase a missing earlier segment.
181
- const workStart = segment.startSeconds;
182
- const remainingProcessing = uncoveredIntervals({ start: workStart, end: segment.endSeconds },
183
- normalizedRanges(Array.isArray(track.processingRanges) ? track.processingRanges : []));
184
- const encodeWork = produced ? 0 : remainingProcessing.reduce((sum, part) => sum + part.end - part.start, 0);
185
-
186
- const sourceIntervals = new Map();
187
- if (!produced && encodeWork > 0) {
188
- for (const sourceId of Array.isArray(track.sourceIds) ? new Set(track.sourceIds) : []) {
189
- const source = sourceState.get(sourceId);
190
- if (!source) {
191
- unknownReason ??= "source-measurement-unavailable";
192
- } else if (source.source.complete !== true) {
193
- const bytesPerMediaSecond = Number(source.source.bytesPerMediaSecond);
194
- if (!(bytesPerMediaSecond > 0)) {
195
- unknownReason ??= "download-rate-unavailable";
196
- } else {
197
- sourceIntervals.set(sourceId, remainingProcessing);
198
- }
199
- }
200
- }
201
- }
202
- if (!produced && encodeWork > 0 && !state.curve) {
203
- unknownReason ??= "encode-rate-unavailable";
204
- }
205
- jobs.push({
206
- trackIndex,
207
- track,
208
- segment,
209
- ahead: Math.max(0, segment.startSeconds - position),
210
- sizeBits,
211
- encodeWork,
212
- remainingProcessing,
213
- sourceIntervals,
214
- produced
215
- });
216
- }
217
- }
218
-
219
- if (unknownReason) {
220
- return result(false, null, buffered, reserve, null, unknownReason, preparedSegments);
221
- }
222
- for (const { track, curve } of trackState) {
223
- if (jobs.some((job) => job.track === track && !job.produced) && !curve) {
224
- return result(false, null, buffered, reserve, null, "encode-rate-unavailable", preparedSegments);
225
- }
226
- }
227
-
228
- jobs.sort((left, right) => left.segment.startSeconds - right.segment.startSeconds ||
229
- left.segment.endSeconds - right.segment.endSeconds || left.trackIndex - right.trackIndex);
230
-
231
- // Each resource has one service integral and one queue. Production is
232
- // scheduled in media order, independently of browser storage capacity.
233
- // The earliest legal start is the supremum of completion lateness against
234
- // presentation deadlines, not a search over guessed delay candidates.
235
- const sourceFinish = new Map([...downloadServices.keys()].map((id) => [id, 0]));
236
- const sourceMediaService = new Map();
237
- for (const [id, { source }] of sourceState) {
238
- const density = Number(source.bytesPerMediaSecond);
239
- if (density > 0 && Array.isArray(source.residence)) {
240
- // Initial time-to-byte mapping uses measured file-average density.
241
- // Held bytes are not downloaded again. Reading costs are already
242
- // included in effective processing speed, rather than charged twice.
243
- sourceMediaService.set(id, source.residence.filter(({ location, start, end }) =>
244
- location !== "missing" && Number.isFinite(start) && Number.isFinite(end) && end > start)
245
- .map(({ start, end }) => ({ start: start / density, end: end / density, begin: 0, finish: 0 })));
246
- }
247
- }
248
- const trackFinish = trackState.map(({ track }) => finiteNonNegative(track.startupRemainingSeconds));
249
- let transferFinish = 0;
250
- const completions = [];
251
- const productions = [];
252
- for (const job of jobs) {
253
- const { track, trackIndex, segment, sizeBits, encodeWork, sourceIntervals, remainingProcessing } = job;
254
- const sourceArrivals = [];
255
- for (const [sourceId, interval] of [...sourceIntervals].flatMap(([id, ranges]) =>
256
- ranges.map((range) => [id, range]))) {
257
- const source = sourceState.get(sourceId);
258
- const served = sourceMediaService.get(sourceId) ?? [];
259
- for (const part of uncoveredIntervals(interval, served)) {
260
- const supply = downloadServices.get(source.serviceId)?.curve;
261
- if (!supply) return result(false, null, buffered, reserve, null, "download-rate-unavailable", preparedSegments);
262
- const begin = sourceFinish.get(source.serviceId) ?? 0;
263
- const end = supply.finish(
264
- (part.end - part.start) * Number(source.source.bytesPerMediaSecond), begin);
265
- sourceFinish.set(source.serviceId, end);
266
- served.push({ ...part, begin, finish: end });
267
- }
268
- sourceMediaService.set(sourceId, served);
269
- sourceArrivals.push(...served.filter((part) => part.end > interval.start && part.start < interval.end)
270
- .flatMap((part) => [Math.max(part.start, interval.start), Math.min(part.end, interval.end)]
271
- .map((media) => ({ media, at: media === part.start ? part.begin : part.begin +
272
- (part.finish - part.begin) * (media - part.start) / (part.end - part.start) }))));
273
- }
274
- // Max-plus composition of fluid arrival and processing. Between measured
275
- // boundaries both are linear, so their supremum occurs at an endpoint.
276
- // Reused input keeps its original arrival times; it is neither downloaded
277
- // twice nor charged the queue start of a later request.
278
- const processing = trackState[trackIndex].curve;
279
- const producedAt = job.produced || encodeWork === 0 ? 0 : Math.max(
280
- processing.finish(encodeWork, trackFinish[trackIndex]),
281
- ...sourceArrivals.map(({ media, at }) => processing.finish(
282
- remainingProcessing.reduce((sum, part) => sum + Math.max(0, part.end - Math.max(part.start, media)), 0), at)));
283
- trackFinish[trackIndex] = Math.max(trackFinish[trackIndex], producedAt);
284
- productions.push({ at: producedAt, trackIndex, segment, requiresSource: sourceArrivals.some(({ at }) => at > 0) });
285
- const deliveryAt = link.finish(sizeBits, Math.max(transferFinish, producedAt));
286
- transferFinish = deliveryAt;
287
- completions.push({ at: deliveryAt + finiteNonNegative(track.appendSeconds), trackIndex, segment });
288
- }
289
-
290
- const constraints = [];
291
- for (let trackIndex = 0; trackIndex < trackState.length; trackIndex += 1) {
292
- const state = trackState[trackIndex];
293
- const delivered = state.clientRanges.map((range) => ({ ...range }));
294
- const prepared = state.clientRanges.map((range) => ({ ...range }));
295
- const trackEnd = Math.min(duration, state.segments.at(-1).mediaRanges?.at(-1)?.end ?? duration);
296
- for (const completion of completions.filter((item) => item.trackIndex === trackIndex)) {
297
- const before = contiguousEnd(delivered, position);
298
- const ranges = completion.segment.mediaRanges ?? [{
299
- start: completion.segment.startSeconds, end: completion.segment.endSeconds
300
- }];
301
- for (const range of ranges) addRange(delivered, range.start, range.end, range.joinEnd);
302
- const after = contiguousEnd(delivered, position);
303
- constraints.push(completion.at - Math.max(0, Math.min(before, trackEnd) - position));
304
- if (!(after > before) && ranges.some(({ start }) => start > before)) {
305
- // A timestamp hole cannot be repaired by waiting longer. Keep it
306
- // distinct from an unavailable rate or a slow but finite service.
307
- return result(false, null, buffered, reserve, null, 'media-continuity-unavailable', preparedSegments);
308
- }
309
- }
310
- for (const production of productions.filter((item) => item.trackIndex === trackIndex)) {
311
- const before = contiguousEnd(prepared, position);
312
- const ranges = production.segment.mediaRanges ?? [{
313
- start: production.segment.startSeconds, end: production.segment.endSeconds
314
- }];
315
- for (const range of ranges) addRange(prepared, range.start, range.end, range.joinEnd);
316
- // Supply interruption coverage is stock held by the proxy as well as
317
- // the browser. It is not a browser buffer-size admission rule.
318
- constraints.push(production.at - Math.max(0, before - position -
319
- Math.min(reserve, Math.max(0, trackEnd - before)) * Number(production.requiresSource)));
320
- }
321
- }
322
- const delay = Math.max(0, ...constraints);
323
- const originReady = trackState.every(({ clientRanges }) => contiguousEnd(clientRanges, position) > position);
324
- const ready = delay === 0 && originReady;
325
- const forecast = result(ready, Number.isFinite(delay) ? delay : null, buffered, reserve,
326
- Math.max(0, reserve - buffered), ready ? 'trajectory-safe-now' :
327
- Number.isFinite(delay) ? 'minimum-safe-delay' : 'service-not-advancing', preparedSegments);
328
- return { ...forecast, measuredAt: now, predictedStartAt: Number.isFinite(delay) ? now + delay * 1000 : null,
329
- preparedUntilSeconds: Math.min(duration, ...trackState.map((state, index) => {
330
- const ranges = state.clientRanges.map((range) => ({ ...range }));
331
- for (const item of productions.filter((item) => item.trackIndex === index && item.at <= delay)) {
332
- for (const range of item.segment.mediaRanges ?? [{ start: item.segment.startSeconds, end: item.segment.endSeconds }]) {
333
- addRange(ranges, range.start, range.end, range.joinEnd);
334
- }
335
- }
336
- return contiguousEnd(ranges, position);
337
- })) };
338
-
339
- }
340
-
341
- function uncoveredIntervals(interval, served) {
342
- let remaining = [{ ...interval }];
343
- for (const held of served) {
344
- remaining = remaining.flatMap((part) => held.end <= part.start || held.start >= part.end ? [part] : [
345
- { start: part.start, end: Math.min(part.end, held.start) },
346
- { start: Math.max(part.start, held.end), end: part.end }
347
- ].filter(({ start, end }) => end > start));
348
- }
349
- return remaining;
350
- }
351
-
352
- /** Predict only unknown cuts in the same clock as the measured track. */
353
- function predictedMediaSegments(segments) {
354
- const anchors = segments.filter(({ mediaRanges }) => mediaRanges?.length > 0);
355
- return segments.map((segment) => {
356
- if (segment.mediaRanges !== undefined && segment.mediaRanges !== null || anchors.length === 0) return segment;
357
- const previous = anchors.findLast(({ index }) => index < segment.index);
358
- const next = anchors.find(({ index }) => index > segment.index);
359
- const nominalLeft = previous?.endSeconds ?? next.startSeconds;
360
- const actualLeft = previous?.mediaRanges.at(-1).end ?? next.mediaRanges[0].start;
361
- const nominalRight = next?.startSeconds ?? nominalLeft;
362
- const actualRight = next?.mediaRanges[0].start ?? actualLeft;
363
- const scale = nominalRight > nominalLeft ? (actualRight - actualLeft) / (nominalRight - nominalLeft) : 1;
364
- return { ...segment, mediaRanges: [{
365
- start: actualLeft + (segment.startSeconds - nominalLeft) * scale,
366
- end: actualLeft + (segment.endSeconds - nominalLeft) * scale
367
- }] };
368
- });
369
- }
370
-
371
- function rateCurve(readings, now) {
372
- if (readings && Number.isFinite(readings.count) && readings.count > 0 &&
373
- Number.isFinite(readings.lastAt) && Number.isFinite(readings.lastValue) &&
374
- Number.isFinite(readings.meanValue) && Number.isFinite(readings.spanSeconds)) {
375
- return rateCurveFrom(readings, now);
376
- }
377
- const ordered = (Array.isArray(readings) ? readings : [])
378
- .filter((reading) => Number.isFinite(reading?.at) && Number.isFinite(reading?.value) && reading.value >= 0)
379
- .filter((reading) => reading.at <= now)
380
- .sort((left, right) => left.at - right.at)
381
- .filter((reading, index, all) => index === 0 || reading.at !== all[index - 1].at);
382
- if (ordered.length === 0) {
383
- return null;
384
- }
385
-
386
- return rateCurveFrom(summarizeRate(ordered), now);
387
- }
388
-
389
- function summarizeRate(readings) {
390
- const latest = readings.at(-1);
391
- return {
392
- count: readings.length,
393
- lastAt: latest.at,
394
- lastValue: latest.value,
395
- meanValue: readings.reduce((sum, reading) => sum + reading.value, 0) / readings.length,
396
- spanSeconds: (latest.at - readings[0].at) / 1000
397
- };
398
- }
399
-
400
- function rateCurveFrom({ lastValue }) {
401
- // Conditional projection of the current measured service. No exponential
402
- // relaxation, smoothing time, slope extrapolation or guessed recovery.
403
- // A new measurement changes the integral; polling does not change its rate.
404
- const rate = Math.max(0, lastValue);
405
- const workBy = (seconds) => rate * Math.max(0, seconds);
406
- return {
407
- rateAt: () => rate,
408
- workBy,
409
- finish: (work, startAt) => {
410
- const start = Math.max(0, startAt);
411
- return start + (work > 0 ? work / rate : 0);
412
- }
413
- };
414
- }
415
-
416
- function averageBitsPerMediaSecond(track, segments, ready) {
417
- let bits = 0;
418
- let mediaSeconds = 0;
419
- for (const segment of segments) {
420
- if (!ready.has(segment.index)) {
421
- continue;
422
- }
423
- const bytes = sizeOf(track.segmentSizesBytes, segment.index);
424
- const seconds = segment.endSeconds - segment.startSeconds;
425
- if (bytes > 0 && seconds > 0) {
426
- bits += bytes * 8;
427
- mediaSeconds += seconds;
428
- }
429
- }
430
- if (mediaSeconds > 0) {
431
- return bits / mediaSeconds;
432
- }
433
- return finiteNonNegative(track.bitsPerMediaSecond);
434
- }
435
-
436
- function addRange(ranges, start, end, joinEnd) {
437
- const merged = [];
438
- let next = { start, end, ...(Number.isFinite(joinEnd) ? { joinEnd } : {}) };
439
- let inserted = false;
440
- for (const range of ranges) {
441
- // The declared coded-frame join boundary connects two ranges only after
442
- // both have arrived. It never extends an isolated range's playable end.
443
- if (Math.max(range.end, range.joinEnd ?? range.end) < next.start) {
444
- merged.push(range);
445
- } else if (Math.max(next.end, next.joinEnd ?? next.end) < range.start) {
446
- if (!inserted) {
447
- merged.push(next);
448
- inserted = true;
449
- }
450
- merged.push(range);
451
- } else {
452
- next = { start: Math.min(next.start, range.start), end: Math.max(next.end, range.end),
453
- ...(next.joinEnd !== undefined || range.joinEnd !== undefined ? {
454
- joinEnd: Math.max(next.joinEnd ?? next.end, range.joinEnd ?? range.end)
455
- } : {}) };
456
- }
457
- }
458
- if (!inserted) {
459
- merged.push(next);
460
- }
461
- ranges.splice(0, ranges.length, ...merged);
462
- }
463
-
464
- function normalizedRanges(ranges) {
465
- const result = [];
466
- for (const range of ranges) {
467
- if (Number.isFinite(range?.start) && Number.isFinite(range?.end) && range.end > range.start) {
468
- addRange(result, range.start, range.end);
469
- }
470
- }
471
- return result;
472
- }
473
-
474
- function coveredSeconds(ranges, start, end) {
475
- return ranges.reduce((total, range) => total + Math.max(0,
476
- Math.min(end, range.end) - Math.max(start, range.start)), 0);
477
- }
478
-
479
- function contiguousEnd(ranges, position) {
480
- for (const range of ranges) {
481
- if (range.start <= position && position <= range.end) {
482
- return range.end;
483
- }
484
- if (range.start > position) {
485
- break;
486
- }
487
- }
488
- return position;
489
- }
490
-
491
- function sizeOf(sizes, index) {
492
- if (sizes instanceof Map) {
493
- return finiteNonNegative(sizes.get(index));
494
- }
495
- return finiteNonNegative(sizes?.[index]);
496
- }
497
-
498
- function finiteNonNegative(value) {
499
- const number = Number(value);
500
- return Number.isFinite(number) && number >= 0 ? number : 0;
501
- }
502
-
503
- function result(ready, delaySeconds, bufferedSeconds, reserveSeconds, neededSeconds, reason, preparedSegments,
504
- bufferedAtStartSeconds = null) {
505
- return {
506
- version: 1,
507
- ready,
508
- delaySeconds,
509
- bufferedSeconds,
510
- reserveSeconds,
511
- neededSeconds,
512
- reason,
513
- preparedSegments,
514
- bufferedAtStartSeconds
515
- };
516
- }
1
+ /**
2
+ * Predict the minimum delay before playback can run to the end without
3
+ * exhausting the continuous media available for the required tracks.
4
+ *
5
+ * This module contains the playback decision's only model. Its inputs are
6
+ * measurements and physical limits: source bytes and download rate, encoded
7
+ * media and encoder rate, segment sizes and client-link rate, existing
8
+ * continuous media, and measured supply interruptions. Each service integral
9
+ * extends its latest measured rate until the next observation. The forecast
10
+ * is conditional on these measured services continuing. Browser storage
11
+ * capacity is not an admission condition. There are no fitted weights or
12
+ * rate multipliers, and no search over candidate start times.
13
+ */
14
+
15
+ /**
16
+ * The mean rate predicted over a horizon by the integral of the measured rate.
17
+ * Polling does not change the last measured service rate.
18
+ *
19
+ * @param {Array<{ at: number, value: number }>} readings
20
+ * @param {number} now
21
+ * @param {number} horizonSeconds
22
+ * @returns {number | null}
23
+ */
24
+ export function forecastRate(readings, now = Date.now(), horizonSeconds = 0) {
25
+ const curve = rateCurve(readings, now);
26
+ if (!curve) {
27
+ return null;
28
+ }
29
+ const horizon = Number.isFinite(horizonSeconds) ? Math.max(0, horizonSeconds) : 0;
30
+ return horizon > 0 ? curve.workBy(horizon) / horizon : curve.rateAt(0);
31
+ }
32
+
33
+ /**
34
+ * Measurements over the output's existing look-ahead horizon. The oldest
35
+ * boundary sample is retained until another sample replaces it, so a repeated
36
+ * report never changes the history or invents a new measurement.
37
+ */
38
+ export class RateTrend {
39
+ #readings = [];
40
+ #horizonMs;
41
+
42
+ constructor(horizonSeconds = Number.POSITIVE_INFINITY) {
43
+ this.#horizonMs = Math.max(0, horizonSeconds) * 1000;
44
+ }
45
+
46
+ add(at, value) {
47
+ if (!Number.isFinite(at) || !Number.isFinite(value) || value < 0 ||
48
+ at <= (this.#readings.at(-1)?.at ?? Number.NEGATIVE_INFINITY)) {
49
+ return false;
50
+ }
51
+ this.#readings.push({ at, value });
52
+ const cutoff = at - this.#horizonMs;
53
+ while (this.#readings.length > 1 && this.#readings[1].at <= cutoff) {
54
+ this.#readings.shift();
55
+ }
56
+ return true;
57
+ }
58
+
59
+ snapshot() {
60
+ if (this.#readings.length === 0) {
61
+ return null;
62
+ }
63
+ return summarizeRate(this.#readings);
64
+ }
65
+ }
66
+
67
+ /**
68
+ * @param {object} input
69
+ * @param {number} input.positionSeconds
70
+ * @param {number} input.durationSeconds
71
+ * @param {number} input.bufferedAheadSeconds
72
+ * @param {number} input.bufferLimitSeconds
73
+ * @param {number} input.reserveSeconds - Measured source-stall coverage, in media seconds.
74
+ * @param {number} input.lookaheadSeconds
75
+ * @param {number} input.now
76
+ * @param {boolean} [input.requiredAudio]
77
+ * @param {Array<{ id: string, serviceId?: string, complete: boolean, bytesPerMediaSecond: number,
78
+ * readings: Array<{ at: number, value: number }> }>} input.sources
79
+ * @param {Array<{ id: string, sourceIds: string[], processedSeconds: number,
80
+ * bitsPerMediaSecond: number, readings: Array<{ at: number, value: number }>,
81
+ * segments: Array<{ index: number, startSeconds: number, endSeconds: number }>,
82
+ * readySegmentIndices: number[], segmentSizesBytes: Map<number, number> | object }>} input.tracks
83
+ * @param {Array<{ at: number, value: number }>} input.linkReadings - Bits/s.
84
+ * @returns {object}
85
+ */
86
+ export function predictPlaybackReadiness(input = {}) {
87
+ const now = Number.isFinite(input.now) ? input.now : Date.now();
88
+ const tracks = Array.isArray(input.tracks) ? input.tracks : [];
89
+ const mediaOrigin = Math.max(0, ...tracks.map((track) =>
90
+ track.segments?.find(({ index }) => index === 0)?.mediaRanges?.[0]?.start ?? 0));
91
+ const position = Math.max(mediaOrigin, finiteNonNegative(input.positionSeconds));
92
+ const duration = finiteNonNegative(input.durationSeconds);
93
+ const remaining = Math.max(0, duration - position);
94
+ const measuredRanges = tracks.map((track) => Array.isArray(track.clientRanges) ?
95
+ normalizedRanges(track.clientRanges) : null);
96
+ const buffered = measuredRanges.length > 0 && measuredRanges.every(Boolean) ?
97
+ Math.max(0, Math.min(...measuredRanges.map((ranges) => contiguousEnd(ranges, position) - position))) :
98
+ finiteNonNegative(input.bufferedAheadSeconds);
99
+ const reserve = Math.min(remaining, finiteNonNegative(input.reserveSeconds));
100
+ const sources = new Map((Array.isArray(input.sources) ? input.sources : [])
101
+ .filter((source) => typeof source?.id === "string")
102
+ .map((source) => [source.id, source]));
103
+ if (!(duration > 0) || tracks.length === 0) {
104
+ return result(false, null, buffered, reserve, null, "incomplete-state", 0);
105
+ }
106
+ const requiredTracks = input.requiredAudio === true ? 2 : 1;
107
+ if (tracks.length < requiredTracks) {
108
+ return result(false, null, buffered, reserve, null, "separate-audio-not-observed", 0);
109
+ }
110
+ if (remaining <= buffered) {
111
+ return result(true, 0, buffered, reserve, 0, "client-buffer-covers-end", 0);
112
+ }
113
+ if (tracks.some((track) => !Array.isArray(track.segments) || track.segments.length === 0)) {
114
+ return result(false, null, buffered, reserve, null, "timeline-unavailable", 0);
115
+ }
116
+
117
+ const trackState = tracks.map((track) => ({
118
+ track,
119
+ clientRanges: Array.isArray(track.clientRanges) ? normalizedRanges(track.clientRanges) :
120
+ [{ start: position, end: position + buffered }],
121
+ ready: new Set(Array.isArray(track.readySegmentIndices) ? track.readySegmentIndices : []),
122
+ curve: rateCurve(track.readings, now),
123
+ bitsPerMediaSecond: averageBitsPerMediaSecond(track, track.segments,
124
+ new Set(Array.isArray(track.readySegmentIndices) ? track.readySegmentIndices : [])),
125
+ segments: predictedMediaSegments(track.segments)
126
+ .filter((segment) => Number.isInteger(segment?.index) &&
127
+ Number.isFinite(segment?.startSeconds) && Number.isFinite(segment?.endSeconds) &&
128
+ segment.endSeconds > segment.startSeconds && segment.endSeconds > position)
129
+ .sort((left, right) => left.startSeconds - right.startSeconds || left.index - right.index)
130
+ }));
131
+ if (trackState.some(({ segments }) => segments.length === 0)) {
132
+ return result(false, null, buffered, reserve, null, "timeline-unavailable", 0);
133
+ }
134
+ const sourceState = new Map();
135
+ const downloadServices = new Map();
136
+ for (const [id, source] of sources) {
137
+ const serviceId = typeof source.serviceId === "string" ? source.serviceId : id;
138
+ const service = downloadServices.get(serviceId);
139
+ if (!service || (!service.curve && source.complete !== true)) {
140
+ downloadServices.set(serviceId, {
141
+ curve: source.complete === true ? null : rateCurve(source.readings, now)
142
+ });
143
+ }
144
+ sourceState.set(id, {
145
+ source,
146
+ serviceId
147
+ });
148
+ }
149
+ const link = rateCurve(input.linkReadings, now);
150
+ let unknownReason = null;
151
+ if (!link) {
152
+ return result(false, null, buffered, reserve, null, "link-rate-unavailable", 0);
153
+ }
154
+
155
+ const preparedSegments = trackState.reduce((sum, { ready, segments }) =>
156
+ sum + segments.filter((segment) => ready.has(segment.index)).length, 0);
157
+ const jobs = [];
158
+
159
+ for (let trackIndex = 0; trackIndex < trackState.length; trackIndex += 1) {
160
+ const state = trackState[trackIndex];
161
+ const { track, segments } = state;
162
+ const measuredBitsPerMediaSecond = state.bitsPerMediaSecond;
163
+ for (const segment of segments) {
164
+ if (coveredSeconds(state.clientRanges, Math.max(position, segment.startSeconds), segment.endSeconds) >=
165
+ segment.endSeconds - Math.max(position, segment.startSeconds) - Number.EPSILON * duration) {
166
+ continue;
167
+ }
168
+ const exactBytes = sizeOf(track.segmentSizesBytes, segment.index);
169
+ const segmentSeconds = segment.endSeconds - segment.startSeconds;
170
+ // HLS fetches the complete file when any required part is absent.
171
+ const sizeBits = exactBytes > 0
172
+ ? exactBytes * 8
173
+ : measuredBitsPerMediaSecond * segmentSeconds;
174
+ if (!(sizeBits > 0)) {
175
+ unknownReason ??= "segment-size-unavailable";
176
+ }
177
+
178
+ const produced = state.ready.has(segment.index);
179
+ // Attribute unfinished work to the actual run interval. A global
180
+ // position from another run must not erase a missing earlier segment.
181
+ const workStart = segment.startSeconds;
182
+ const remainingProcessing = uncoveredIntervals({ start: workStart, end: segment.endSeconds },
183
+ normalizedRanges(Array.isArray(track.processingRanges) ? track.processingRanges : []));
184
+ const encodeWork = produced ? 0 : remainingProcessing.reduce((sum, part) => sum + part.end - part.start, 0);
185
+
186
+ const sourceIntervals = new Map();
187
+ if (!produced && encodeWork > 0) {
188
+ for (const sourceId of Array.isArray(track.sourceIds) ? new Set(track.sourceIds) : []) {
189
+ const source = sourceState.get(sourceId);
190
+ if (!source) {
191
+ unknownReason ??= "source-measurement-unavailable";
192
+ } else if (source.source.complete !== true) {
193
+ const bytesPerMediaSecond = Number(source.source.bytesPerMediaSecond);
194
+ if (!(bytesPerMediaSecond > 0)) {
195
+ unknownReason ??= "download-rate-unavailable";
196
+ } else {
197
+ sourceIntervals.set(sourceId, remainingProcessing);
198
+ }
199
+ }
200
+ }
201
+ }
202
+ if (!produced && encodeWork > 0 && !state.curve) {
203
+ unknownReason ??= "encode-rate-unavailable";
204
+ }
205
+ jobs.push({
206
+ trackIndex,
207
+ track,
208
+ segment,
209
+ ahead: Math.max(0, segment.startSeconds - position),
210
+ sizeBits,
211
+ encodeWork,
212
+ remainingProcessing,
213
+ sourceIntervals,
214
+ produced
215
+ });
216
+ }
217
+ }
218
+
219
+ if (unknownReason) {
220
+ return result(false, null, buffered, reserve, null, unknownReason, preparedSegments);
221
+ }
222
+ for (const { track, curve } of trackState) {
223
+ if (jobs.some((job) => job.track === track && !job.produced) && !curve) {
224
+ return result(false, null, buffered, reserve, null, "encode-rate-unavailable", preparedSegments);
225
+ }
226
+ }
227
+
228
+ jobs.sort((left, right) => left.segment.startSeconds - right.segment.startSeconds ||
229
+ left.segment.endSeconds - right.segment.endSeconds || left.trackIndex - right.trackIndex);
230
+
231
+ // Each resource has one service integral and one queue. Production is
232
+ // scheduled in media order, independently of browser storage capacity.
233
+ // The earliest legal start is the supremum of completion lateness against
234
+ // presentation deadlines, not a search over guessed delay candidates.
235
+ const sourceFinish = new Map([...downloadServices.keys()].map((id) => [id, 0]));
236
+ const sourceMediaService = new Map();
237
+ for (const [id, { source }] of sourceState) {
238
+ const density = Number(source.bytesPerMediaSecond);
239
+ if (density > 0 && Array.isArray(source.residence)) {
240
+ // Initial time-to-byte mapping uses measured file-average density.
241
+ // Held bytes are not downloaded again. Reading costs are already
242
+ // included in effective processing speed, rather than charged twice.
243
+ sourceMediaService.set(id, source.residence.filter(({ location, start, end }) =>
244
+ location !== "missing" && Number.isFinite(start) && Number.isFinite(end) && end > start)
245
+ .map(({ start, end }) => ({ start: start / density, end: end / density, begin: 0, finish: 0 })));
246
+ }
247
+ }
248
+ const trackFinish = trackState.map(({ track }) => finiteNonNegative(track.startupRemainingSeconds));
249
+ let transferFinish = 0;
250
+ const completions = [];
251
+ const productions = [];
252
+ for (const job of jobs) {
253
+ const { track, trackIndex, segment, sizeBits, encodeWork, sourceIntervals, remainingProcessing } = job;
254
+ const sourceArrivals = [];
255
+ for (const [sourceId, interval] of [...sourceIntervals].flatMap(([id, ranges]) =>
256
+ ranges.map((range) => [id, range]))) {
257
+ const source = sourceState.get(sourceId);
258
+ const served = sourceMediaService.get(sourceId) ?? [];
259
+ for (const part of uncoveredIntervals(interval, served)) {
260
+ const supply = downloadServices.get(source.serviceId)?.curve;
261
+ if (!supply) return result(false, null, buffered, reserve, null, "download-rate-unavailable", preparedSegments);
262
+ const begin = sourceFinish.get(source.serviceId) ?? 0;
263
+ const end = supply.finish(
264
+ (part.end - part.start) * Number(source.source.bytesPerMediaSecond), begin);
265
+ sourceFinish.set(source.serviceId, end);
266
+ served.push({ ...part, begin, finish: end });
267
+ }
268
+ sourceMediaService.set(sourceId, served);
269
+ sourceArrivals.push(...served.filter((part) => part.end > interval.start && part.start < interval.end)
270
+ .flatMap((part) => [Math.max(part.start, interval.start), Math.min(part.end, interval.end)]
271
+ .map((media) => ({ media, at: media === part.start ? part.begin : part.begin +
272
+ (part.finish - part.begin) * (media - part.start) / (part.end - part.start) }))));
273
+ }
274
+ // Max-plus composition of fluid arrival and processing. Between measured
275
+ // boundaries both are linear, so their supremum occurs at an endpoint.
276
+ // Reused input keeps its original arrival times; it is neither downloaded
277
+ // twice nor charged the queue start of a later request.
278
+ const processing = trackState[trackIndex].curve;
279
+ const producedAt = job.produced || encodeWork === 0 ? 0 : Math.max(
280
+ processing.finish(encodeWork, trackFinish[trackIndex]),
281
+ ...sourceArrivals.map(({ media, at }) => processing.finish(
282
+ remainingProcessing.reduce((sum, part) => sum + Math.max(0, part.end - Math.max(part.start, media)), 0), at)));
283
+ trackFinish[trackIndex] = Math.max(trackFinish[trackIndex], producedAt);
284
+ productions.push({ at: producedAt, trackIndex, segment, requiresSource: sourceArrivals.some(({ at }) => at > 0) });
285
+ const deliveryAt = link.finish(sizeBits, Math.max(transferFinish, producedAt));
286
+ transferFinish = deliveryAt;
287
+ completions.push({ at: deliveryAt + finiteNonNegative(track.appendSeconds), trackIndex, segment });
288
+ }
289
+
290
+ const constraints = [];
291
+ for (let trackIndex = 0; trackIndex < trackState.length; trackIndex += 1) {
292
+ const state = trackState[trackIndex];
293
+ const delivered = state.clientRanges.map((range) => ({ ...range }));
294
+ const prepared = state.clientRanges.map((range) => ({ ...range }));
295
+ const trackEnd = Math.min(duration, state.segments.at(-1).mediaRanges?.at(-1)?.end ?? duration);
296
+ for (const completion of completions.filter((item) => item.trackIndex === trackIndex)) {
297
+ const before = contiguousEnd(delivered, position);
298
+ const ranges = completion.segment.mediaRanges ?? [{
299
+ start: completion.segment.startSeconds, end: completion.segment.endSeconds
300
+ }];
301
+ for (const range of ranges) addRange(delivered, range.start, range.end, range.joinEnd);
302
+ const after = contiguousEnd(delivered, position);
303
+ constraints.push(completion.at - Math.max(0, Math.min(before, trackEnd) - position));
304
+ if (!(after > before) && ranges.some(({ start }) => start > before)) {
305
+ // A timestamp hole cannot be repaired by waiting longer. Keep it
306
+ // distinct from an unavailable rate or a slow but finite service.
307
+ return result(false, null, buffered, reserve, null, 'media-continuity-unavailable', preparedSegments);
308
+ }
309
+ }
310
+ for (const production of productions.filter((item) => item.trackIndex === trackIndex)) {
311
+ const before = contiguousEnd(prepared, position);
312
+ const ranges = production.segment.mediaRanges ?? [{
313
+ start: production.segment.startSeconds, end: production.segment.endSeconds
314
+ }];
315
+ for (const range of ranges) addRange(prepared, range.start, range.end, range.joinEnd);
316
+ // Supply interruption coverage is stock held by the proxy as well as
317
+ // the browser. It is not a browser buffer-size admission rule.
318
+ constraints.push(production.at - Math.max(0, before - position -
319
+ Math.min(reserve, Math.max(0, trackEnd - before)) * Number(production.requiresSource)));
320
+ }
321
+ }
322
+ const delay = Math.max(0, ...constraints);
323
+ const originReady = trackState.every(({ clientRanges }) => contiguousEnd(clientRanges, position) > position);
324
+ const ready = delay === 0 && originReady;
325
+ const forecast = result(ready, Number.isFinite(delay) ? delay : null, buffered, reserve,
326
+ Math.max(0, reserve - buffered), ready ? 'trajectory-safe-now' :
327
+ Number.isFinite(delay) ? 'minimum-safe-delay' : 'service-not-advancing', preparedSegments);
328
+ return { ...forecast, measuredAt: now, predictedStartAt: Number.isFinite(delay) ? now + delay * 1000 : null,
329
+ preparedUntilSeconds: Math.min(duration, ...trackState.map((state, index) => {
330
+ const ranges = state.clientRanges.map((range) => ({ ...range }));
331
+ for (const item of productions.filter((item) => item.trackIndex === index && item.at <= delay)) {
332
+ for (const range of item.segment.mediaRanges ?? [{ start: item.segment.startSeconds, end: item.segment.endSeconds }]) {
333
+ addRange(ranges, range.start, range.end, range.joinEnd);
334
+ }
335
+ }
336
+ return contiguousEnd(ranges, position);
337
+ })) };
338
+
339
+ }
340
+
341
+ function uncoveredIntervals(interval, served) {
342
+ let remaining = [{ ...interval }];
343
+ for (const held of served) {
344
+ remaining = remaining.flatMap((part) => held.end <= part.start || held.start >= part.end ? [part] : [
345
+ { start: part.start, end: Math.min(part.end, held.start) },
346
+ { start: Math.max(part.start, held.end), end: part.end }
347
+ ].filter(({ start, end }) => end > start));
348
+ }
349
+ return remaining;
350
+ }
351
+
352
+ /** Predict only unknown cuts in the same clock as the measured track. */
353
+ function predictedMediaSegments(segments) {
354
+ const anchors = segments.filter(({ mediaRanges }) => mediaRanges?.length > 0);
355
+ return segments.map((segment) => {
356
+ if (segment.mediaRanges !== undefined && segment.mediaRanges !== null || anchors.length === 0) return segment;
357
+ const previous = anchors.findLast(({ index }) => index < segment.index);
358
+ const next = anchors.find(({ index }) => index > segment.index);
359
+ const nominalLeft = previous?.endSeconds ?? next.startSeconds;
360
+ const actualLeft = previous?.mediaRanges.at(-1).end ?? next.mediaRanges[0].start;
361
+ const nominalRight = next?.startSeconds ?? nominalLeft;
362
+ const actualRight = next?.mediaRanges[0].start ?? actualLeft;
363
+ const scale = nominalRight > nominalLeft ? (actualRight - actualLeft) / (nominalRight - nominalLeft) : 1;
364
+ return { ...segment, mediaRanges: [{
365
+ start: actualLeft + (segment.startSeconds - nominalLeft) * scale,
366
+ end: actualLeft + (segment.endSeconds - nominalLeft) * scale
367
+ }] };
368
+ });
369
+ }
370
+
371
+ function rateCurve(readings, now) {
372
+ if (readings && Number.isFinite(readings.count) && readings.count > 0 &&
373
+ Number.isFinite(readings.lastAt) && Number.isFinite(readings.lastValue) &&
374
+ Number.isFinite(readings.meanValue) && Number.isFinite(readings.spanSeconds)) {
375
+ return rateCurveFrom(readings, now);
376
+ }
377
+ const ordered = (Array.isArray(readings) ? readings : [])
378
+ .filter((reading) => Number.isFinite(reading?.at) && Number.isFinite(reading?.value) && reading.value >= 0)
379
+ .filter((reading) => reading.at <= now)
380
+ .sort((left, right) => left.at - right.at)
381
+ .filter((reading, index, all) => index === 0 || reading.at !== all[index - 1].at);
382
+ if (ordered.length === 0) {
383
+ return null;
384
+ }
385
+
386
+ return rateCurveFrom(summarizeRate(ordered), now);
387
+ }
388
+
389
+ function summarizeRate(readings) {
390
+ const latest = readings.at(-1);
391
+ return {
392
+ count: readings.length,
393
+ lastAt: latest.at,
394
+ lastValue: latest.value,
395
+ meanValue: readings.reduce((sum, reading) => sum + reading.value, 0) / readings.length,
396
+ spanSeconds: (latest.at - readings[0].at) / 1000
397
+ };
398
+ }
399
+
400
+ function rateCurveFrom({ lastValue }) {
401
+ // Conditional projection of the current measured service. No exponential
402
+ // relaxation, smoothing time, slope extrapolation or guessed recovery.
403
+ // A new measurement changes the integral; polling does not change its rate.
404
+ const rate = Math.max(0, lastValue);
405
+ const workBy = (seconds) => rate * Math.max(0, seconds);
406
+ return {
407
+ rateAt: () => rate,
408
+ workBy,
409
+ finish: (work, startAt) => {
410
+ const start = Math.max(0, startAt);
411
+ return start + (work > 0 ? work / rate : 0);
412
+ }
413
+ };
414
+ }
415
+
416
+ function averageBitsPerMediaSecond(track, segments, ready) {
417
+ let bits = 0;
418
+ let mediaSeconds = 0;
419
+ for (const segment of segments) {
420
+ if (!ready.has(segment.index)) {
421
+ continue;
422
+ }
423
+ const bytes = sizeOf(track.segmentSizesBytes, segment.index);
424
+ const seconds = segment.endSeconds - segment.startSeconds;
425
+ if (bytes > 0 && seconds > 0) {
426
+ bits += bytes * 8;
427
+ mediaSeconds += seconds;
428
+ }
429
+ }
430
+ if (mediaSeconds > 0) {
431
+ return bits / mediaSeconds;
432
+ }
433
+ return finiteNonNegative(track.bitsPerMediaSecond);
434
+ }
435
+
436
+ function addRange(ranges, start, end, joinEnd) {
437
+ const merged = [];
438
+ let next = { start, end, ...(Number.isFinite(joinEnd) ? { joinEnd } : {}) };
439
+ let inserted = false;
440
+ for (const range of ranges) {
441
+ // The declared coded-frame join boundary connects two ranges only after
442
+ // both have arrived. It never extends an isolated range's playable end.
443
+ if (Math.max(range.end, range.joinEnd ?? range.end) < next.start) {
444
+ merged.push(range);
445
+ } else if (Math.max(next.end, next.joinEnd ?? next.end) < range.start) {
446
+ if (!inserted) {
447
+ merged.push(next);
448
+ inserted = true;
449
+ }
450
+ merged.push(range);
451
+ } else {
452
+ next = { start: Math.min(next.start, range.start), end: Math.max(next.end, range.end),
453
+ ...(next.joinEnd !== undefined || range.joinEnd !== undefined ? {
454
+ joinEnd: Math.max(next.joinEnd ?? next.end, range.joinEnd ?? range.end)
455
+ } : {}) };
456
+ }
457
+ }
458
+ if (!inserted) {
459
+ merged.push(next);
460
+ }
461
+ ranges.splice(0, ranges.length, ...merged);
462
+ }
463
+
464
+ function normalizedRanges(ranges) {
465
+ const result = [];
466
+ for (const range of ranges) {
467
+ if (Number.isFinite(range?.start) && Number.isFinite(range?.end) && range.end > range.start) {
468
+ addRange(result, range.start, range.end);
469
+ }
470
+ }
471
+ return result;
472
+ }
473
+
474
+ function coveredSeconds(ranges, start, end) {
475
+ return ranges.reduce((total, range) => total + Math.max(0,
476
+ Math.min(end, range.end) - Math.max(start, range.start)), 0);
477
+ }
478
+
479
+ function contiguousEnd(ranges, position) {
480
+ for (const range of ranges) {
481
+ if (range.start <= position && position <= range.end) {
482
+ return range.end;
483
+ }
484
+ if (range.start > position) {
485
+ break;
486
+ }
487
+ }
488
+ return position;
489
+ }
490
+
491
+ function sizeOf(sizes, index) {
492
+ if (sizes instanceof Map) {
493
+ return finiteNonNegative(sizes.get(index));
494
+ }
495
+ return finiteNonNegative(sizes?.[index]);
496
+ }
497
+
498
+ function finiteNonNegative(value) {
499
+ const number = Number(value);
500
+ return Number.isFinite(number) && number >= 0 ? number : 0;
501
+ }
502
+
503
+ function result(ready, delaySeconds, bufferedSeconds, reserveSeconds, neededSeconds, reason, preparedSegments,
504
+ bufferedAtStartSeconds = null) {
505
+ return {
506
+ version: 1,
507
+ ready,
508
+ delaySeconds,
509
+ bufferedSeconds,
510
+ reserveSeconds,
511
+ neededSeconds,
512
+ reason,
513
+ preparedSegments,
514
+ bufferedAtStartSeconds
515
+ };
516
+ }