@torrent-tv/proxy 2.80.9 → 2.80.11
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/CHANGELOG.md +1685 -1666
- package/package.json +1 -1
- package/services/data-channel-handler.js +2 -4
- package/services/encode/EncodePlan.js +0 -3
- package/services/encode/EncodeRun.js +14 -0
- package/services/encode/run-command.js +729 -707
- package/services/hls-session-manager.js +10168 -10494
- package/services/hwaccel.js +2 -2
- package/services/orchestrators/EncodeOrchestrator.js +4 -4
- package/services/output/LiveOutputs.js +49 -0
- package/services/priority/PriorityOrchestrator.js +278 -174
- package/services/quality/EncodeCost.js +555 -555
- package/test/auto-quality-step.test.js +514 -507
- package/test/contention.test.js +1 -1
- package/test/decode-cost-fit.test.js +57 -0
- package/test/decode-cost.test.js +13 -36
- package/test/encode-orchestrator.test.js +1 -2
- package/test/encode-plan-viewers.test.js +18 -19
- package/test/encode-plan.test.js +539 -539
- package/test/held-request-width.test.js +155 -154
- package/test/helpers/encode-run.js +136 -128
- package/test/keyframe-table-budget.test.js +7 -1
- package/test/one-authority.test.js +220 -105
- package/test/orchestrator-wired.test.js +16 -11
- package/test/piece-store-slow-disk.test.js +32 -2
- package/test/priority-map-per-output.test.js +211 -0
- package/test/produced-copy-choice.test.js +358 -327
- package/test/quality-variants.test.js +1222 -1209
- package/test/segment-serve-wiring.test.js +76 -20
- package/test/stale-request-after-seek.test.js +51 -77
- package/test/tracks-begin-together.test.js +195 -153
- package/test/tunnel-renewal.test.js +34 -5
- package/test/two-viewers-one-picture.test.js +374 -347
- package/test/viewer-outputs.test.js +18 -14
- package/test/behind-head-repair.test.js +0 -261
- package/test/decode-measurement.test.js +0 -83
- /package/services/{contention.js → encode/contention.js} +0 -0
|
@@ -1,1209 +1,1222 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Quality variants: the master playlist, and what happens when the viewer
|
|
3
|
-
* moves between rungs.
|
|
4
|
-
*
|
|
5
|
-
* What makes a mid-stream change of quality possible at all is that every
|
|
6
|
-
* variant is cut at the SAME times, so a segment produced by one encoder can be
|
|
7
|
-
* appended where another encoder's would have gone. The last test here pins
|
|
8
|
-
* that property at its source; the rest cover the wiring that a route reaches —
|
|
9
|
-
* a module test that imports a function directly cannot see a caller that never
|
|
10
|
-
* calls it (2.9.124).
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import test from "node:test";
|
|
14
|
-
import { fakeProcess as fakeEncoder, startRunOn } from "./helpers/encode-run.js";
|
|
15
|
-
import assert from "node:assert/strict";
|
|
16
|
-
import { SourceFile } from "../services/source/SourceFile.js";
|
|
17
|
-
import { Timeline } from "../services/output/Timeline.js";
|
|
18
|
-
import { mkdtemp, rm } from "node:fs/promises";
|
|
19
|
-
import os from "node:os";
|
|
20
|
-
import path from "node:path";
|
|
21
|
-
import {
|
|
22
|
-
costKindForSession,
|
|
23
|
-
HlsSessionManager
|
|
24
|
-
} from "../services/hls-session-manager.js";
|
|
25
|
-
import { fmp4Format } from "../services/segment-formats/fmp4.js";
|
|
26
|
-
import { computeCutGrid } from "../services/output/cut-grid.js";
|
|
27
|
-
import { buildRunCommand, nearestKeyframeAtOrBefore } from "../services/encode/run-command.js";
|
|
28
|
-
import { Output } from "../services/output/Output.js";
|
|
29
|
-
import { viewerOf } from "../services/viewer/Viewer.js";
|
|
30
|
-
|
|
31
|
-
const BASE_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
|
|
32
|
-
const VARIANT_ID = "11111111-2222-3333-4444-555555555555";
|
|
33
|
-
const SECOND_VARIANT_ID = "99999999-8888-7777-6666-555555555555";
|
|
34
|
-
const SEGMENT_SECONDS = 4;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* A session shaped like a live one, without the ffmpeg run behind it.
|
|
38
|
-
*
|
|
39
|
-
* @param {{ id: string, encodeHeight: number, dirPath: string, transcodeVideo?: boolean }} params
|
|
40
|
-
* @returns {object}
|
|
41
|
-
*/
|
|
42
|
-
function fakeSession({ id, encodeHeight, dirPath, transcodeVideo = true }) {
|
|
43
|
-
return {
|
|
44
|
-
id,
|
|
45
|
-
dirPath,
|
|
46
|
-
// Where this file is cut, held by the file. A fixture that stated it
|
|
47
|
-
// on the session was describing what production no longer does.
|
|
48
|
-
timeline: new Timeline({
|
|
49
|
-
boundaries: Array.from({ length: 101 }, (_, index) => index * SEGMENT_SECONDS),
|
|
50
|
-
cutGrid: "uniform"
|
|
51
|
-
}),
|
|
52
|
-
state: "ready",
|
|
53
|
-
// The file's own facts, which is where the size and what decoding it costs
|
|
54
|
-
// both come from: 1080p24 at 8 Mbit/s, the field file of 2026-08-17. A
|
|
55
|
-
// fixture that assigned a cost object instead was stating an answer the
|
|
56
|
-
// file now derives, so nothing it said reached the arithmetic.
|
|
57
|
-
file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "video.mkv" }).learn({
|
|
58
|
-
width: 1920,
|
|
59
|
-
height: 1080,
|
|
60
|
-
fps: 24,
|
|
61
|
-
bitrateKbps: 8000
|
|
62
|
-
}),
|
|
63
|
-
// An ordinary session reads its own file, and its sound is inside it. The
|
|
64
|
-
// three differ only for a soundtrack shipped as a file of its own.
|
|
65
|
-
get inputFile() { return this.file; },
|
|
66
|
-
get audioFile() { return this.file; },
|
|
67
|
-
startedAt: Date.now(),
|
|
68
|
-
createEntryMs: Date.now(),
|
|
69
|
-
lastAccessedAt: Date.now(),
|
|
70
|
-
ffmpeg: null,
|
|
71
|
-
lastError: "",
|
|
72
|
-
consumers: new Set(),
|
|
73
|
-
segmentFormat: fmp4Format,
|
|
74
|
-
transcodeVideo,
|
|
75
|
-
transcodeAudio: true,
|
|
76
|
-
audioTrackIndex: 0,
|
|
77
|
-
// The shape this output is encoded AS, decided once for the output rather
|
|
78
|
-
// than once per session.
|
|
79
|
-
output: new Output({ encodeWidth: 0, encodeHeight, outputFps: 24, softwarePreset: null, applyTonemap: false }),
|
|
80
|
-
encodeRunGeneration: 0,
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
"
|
|
308
|
-
);
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
//
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
base
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
);
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
);
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
assert.
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
variant.
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
//
|
|
393
|
-
//
|
|
394
|
-
//
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
variant.
|
|
427
|
-
|
|
428
|
-
variant.
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
//
|
|
433
|
-
//
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
);
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
);
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
);
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
);
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
);
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
assert.
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
//
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
//
|
|
646
|
-
//
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
//
|
|
724
|
-
//
|
|
725
|
-
//
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
base.file.
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
);
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
});
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
});
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
)
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
)
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
);
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
assert.equal(
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
"
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
//
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
//
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
//
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
const
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
const
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
const
|
|
1081
|
-
const
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
);
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
assert.equal(
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
manager
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
spawnedDirs.
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
const
|
|
1132
|
-
const
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
});
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
base
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
base
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @file Quality variants: the master playlist, and what happens when the viewer
|
|
3
|
+
* moves between rungs.
|
|
4
|
+
*
|
|
5
|
+
* What makes a mid-stream change of quality possible at all is that every
|
|
6
|
+
* variant is cut at the SAME times, so a segment produced by one encoder can be
|
|
7
|
+
* appended where another encoder's would have gone. The last test here pins
|
|
8
|
+
* that property at its source; the rest cover the wiring that a route reaches —
|
|
9
|
+
* a module test that imports a function directly cannot see a caller that never
|
|
10
|
+
* calls it (2.9.124).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import test from "node:test";
|
|
14
|
+
import { fakeProcess as fakeEncoder, startRunOn } from "./helpers/encode-run.js";
|
|
15
|
+
import assert from "node:assert/strict";
|
|
16
|
+
import { SourceFile } from "../services/source/SourceFile.js";
|
|
17
|
+
import { Timeline } from "../services/output/Timeline.js";
|
|
18
|
+
import { mkdtemp, rm } from "node:fs/promises";
|
|
19
|
+
import os from "node:os";
|
|
20
|
+
import path from "node:path";
|
|
21
|
+
import {
|
|
22
|
+
costKindForSession,
|
|
23
|
+
HlsSessionManager
|
|
24
|
+
} from "../services/hls-session-manager.js";
|
|
25
|
+
import { fmp4Format } from "../services/segment-formats/fmp4.js";
|
|
26
|
+
import { computeCutGrid } from "../services/output/cut-grid.js";
|
|
27
|
+
import { buildRunCommand, nearestKeyframeAtOrBefore } from "../services/encode/run-command.js";
|
|
28
|
+
import { Output } from "../services/output/Output.js";
|
|
29
|
+
import { viewerOf } from "../services/viewer/Viewer.js";
|
|
30
|
+
|
|
31
|
+
const BASE_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
|
|
32
|
+
const VARIANT_ID = "11111111-2222-3333-4444-555555555555";
|
|
33
|
+
const SECOND_VARIANT_ID = "99999999-8888-7777-6666-555555555555";
|
|
34
|
+
const SEGMENT_SECONDS = 4;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A session shaped like a live one, without the ffmpeg run behind it.
|
|
38
|
+
*
|
|
39
|
+
* @param {{ id: string, encodeHeight: number, dirPath: string, transcodeVideo?: boolean }} params
|
|
40
|
+
* @returns {object}
|
|
41
|
+
*/
|
|
42
|
+
function fakeSession({ id, encodeHeight, dirPath, transcodeVideo = true }) {
|
|
43
|
+
return {
|
|
44
|
+
id,
|
|
45
|
+
dirPath,
|
|
46
|
+
// Where this file is cut, held by the file. A fixture that stated it
|
|
47
|
+
// on the session was describing what production no longer does.
|
|
48
|
+
timeline: new Timeline({
|
|
49
|
+
boundaries: Array.from({ length: 101 }, (_, index) => index * SEGMENT_SECONDS),
|
|
50
|
+
cutGrid: "uniform"
|
|
51
|
+
}),
|
|
52
|
+
state: "ready",
|
|
53
|
+
// The file's own facts, which is where the size and what decoding it costs
|
|
54
|
+
// both come from: 1080p24 at 8 Mbit/s, the field file of 2026-08-17. A
|
|
55
|
+
// fixture that assigned a cost object instead was stating an answer the
|
|
56
|
+
// file now derives, so nothing it said reached the arithmetic.
|
|
57
|
+
file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "video.mkv" }).learn({
|
|
58
|
+
width: 1920,
|
|
59
|
+
height: 1080,
|
|
60
|
+
fps: 24,
|
|
61
|
+
bitrateKbps: 8000
|
|
62
|
+
}),
|
|
63
|
+
// An ordinary session reads its own file, and its sound is inside it. The
|
|
64
|
+
// three differ only for a soundtrack shipped as a file of its own.
|
|
65
|
+
get inputFile() { return this.file; },
|
|
66
|
+
get audioFile() { return this.file; },
|
|
67
|
+
startedAt: Date.now(),
|
|
68
|
+
createEntryMs: Date.now(),
|
|
69
|
+
lastAccessedAt: Date.now(),
|
|
70
|
+
ffmpeg: null,
|
|
71
|
+
lastError: "",
|
|
72
|
+
consumers: new Set(),
|
|
73
|
+
segmentFormat: fmp4Format,
|
|
74
|
+
transcodeVideo,
|
|
75
|
+
transcodeAudio: true,
|
|
76
|
+
audioTrackIndex: 0,
|
|
77
|
+
// The shape this output is encoded AS, decided once for the output rather
|
|
78
|
+
// than once per session.
|
|
79
|
+
output: new Output({ encodeWidth: 0, encodeHeight, outputFps: 24, softwarePreset: null, applyTonemap: false }),
|
|
80
|
+
encodeRunGeneration: 0,
|
|
81
|
+
failedStartAt: -1,
|
|
82
|
+
failedStartCount: 0,
|
|
83
|
+
waitEpoch: 0,
|
|
84
|
+
viewers: new Map(),
|
|
85
|
+
usesExplicitCuts: false,
|
|
86
|
+
useSyntheticPlaylist: true,
|
|
87
|
+
playlistText: "#EXTM3U\n",
|
|
88
|
+
segmentCount: 100,
|
|
89
|
+
progress: { state: "running", processedSeconds: 0, startPositionSeconds: 0, speed: "1.0x" }
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @returns {Promise<{ manager: HlsSessionManager, base: object, dirPath: string }>}
|
|
96
|
+
*/
|
|
97
|
+
async function managerWithBase() {
|
|
98
|
+
const dirPath = await mkdtemp(path.join(os.tmpdir(), "quality-variants-"));
|
|
99
|
+
const manager = new HlsSessionManager({
|
|
100
|
+
enabled: true,
|
|
101
|
+
ffmpegBin: "ffmpeg",
|
|
102
|
+
localBindHost: "127.0.0.1",
|
|
103
|
+
localPort: 9090
|
|
104
|
+
});
|
|
105
|
+
// 812p is what a viewport-sized budget actually produces — deliberately not a
|
|
106
|
+
// ladder rung, because that is the case the master has to carry.
|
|
107
|
+
const base = fakeSession({ id: BASE_ID, encodeHeight: 812, dirPath });
|
|
108
|
+
manager.sessionsById.set(BASE_ID, base);
|
|
109
|
+
return { manager, base, dirPath };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
test("the master offers every rung, the session's own height among them", async (t) => {
|
|
113
|
+
const { manager, dirPath } = await managerWithBase();
|
|
114
|
+
t.after(async () => {
|
|
115
|
+
await manager.disposeAll();
|
|
116
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const master = manager.buildMasterPlaylist(BASE_ID);
|
|
120
|
+
|
|
121
|
+
assert.ok(master, "a re-encoded 1080p source has rungs to choose between");
|
|
122
|
+
const heights = [...master.matchAll(/^v\/(\d+)\/index\.m3u8$/gm)].map((match) => Number(match[1]));
|
|
123
|
+
assert.deepEqual(
|
|
124
|
+
heights,
|
|
125
|
+
[1080, 812, 720, 540, 480, 360, 240],
|
|
126
|
+
"the source height, the height already being encoded, and the rungs below it, largest first"
|
|
127
|
+
);
|
|
128
|
+
assert.match(master, /^#EXT-X-VERSION:7$/m, "the version the segment format requires");
|
|
129
|
+
assert.match(master, /RESOLUTION=1280x720/, "each variant states the size it decodes to");
|
|
130
|
+
assert.ok(
|
|
131
|
+
!master.includes("2160"),
|
|
132
|
+
"a rung above the source would be upscaling — invented detail at a higher cost than the source"
|
|
133
|
+
);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("audio is published once for the file, and every rung points at it", async (t) => {
|
|
137
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
138
|
+
t.after(async () => {
|
|
139
|
+
await manager.disposeAll();
|
|
140
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
141
|
+
});
|
|
142
|
+
// The inventory the plan already probed — the same list the browser's audio
|
|
143
|
+
// menu is built from.
|
|
144
|
+
manager.getCachedAudioTracks = () => [
|
|
145
|
+
{ index: 0, language: "rus", title: "Дубляж", isDefault: true },
|
|
146
|
+
{ index: 1, language: "eng", title: "", isDefault: false }
|
|
147
|
+
];
|
|
148
|
+
// Settled at creation in production; set here directly, since this test
|
|
149
|
+
// builds its session by hand.
|
|
150
|
+
base.audioSeparate = true;
|
|
151
|
+
base.audioTrackIndex = 1;
|
|
152
|
+
|
|
153
|
+
const master = manager.buildMasterPlaylist(BASE_ID);
|
|
154
|
+
|
|
155
|
+
assert.match(
|
|
156
|
+
master,
|
|
157
|
+
/#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="aud",NAME="Дубляж",LANGUAGE="ru",AUTOSELECT=YES,DEFAULT=NO,URI="a\/0\/index\.m3u8"/,
|
|
158
|
+
"a track with a title is named by it"
|
|
159
|
+
);
|
|
160
|
+
assert.match(
|
|
161
|
+
master,
|
|
162
|
+
/#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="aud",NAME="eng",LANGUAGE="en",AUTOSELECT=YES,DEFAULT=YES,URI="a\/1\/index\.m3u8"/,
|
|
163
|
+
"the track the session was created with is the default one, and an untitled track is named by its language"
|
|
164
|
+
);
|
|
165
|
+
const streams = [...master.matchAll(/^#EXT-X-STREAM-INF:.*$/gm)].map((match) => match[0]);
|
|
166
|
+
assert.equal(streams.length, 7, "every rung");
|
|
167
|
+
assert.ok(
|
|
168
|
+
streams.every((line) => line.includes('AUDIO="aud"')),
|
|
169
|
+
"each rung plays with the shared audio rather than carrying its own"
|
|
170
|
+
);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test("a session that did not ask for renditions gets audio in its stream, as before", async (t) => {
|
|
174
|
+
const { manager, dirPath } = await managerWithBase();
|
|
175
|
+
t.after(async () => {
|
|
176
|
+
await manager.disposeAll();
|
|
177
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
178
|
+
});
|
|
179
|
+
manager.getCachedAudioTracks = () => [{ index: 0, language: "rus", title: "", isDefault: true }];
|
|
180
|
+
|
|
181
|
+
const master = manager.buildMasterPlaylist(BASE_ID);
|
|
182
|
+
|
|
183
|
+
assert.ok(!master.includes("EXT-X-MEDIA"), "a browser that does not know about renditions is not sent any");
|
|
184
|
+
assert.ok(!master.includes("AUDIO="), "and its rungs still carry their own audio");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("a copied video is offered variants when its cut grid is real", async (t) => {
|
|
188
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
189
|
+
t.after(async () => {
|
|
190
|
+
await manager.disposeAll();
|
|
191
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
192
|
+
});
|
|
193
|
+
// A copy is cut at the source's own keyframes — it has no other choice. A
|
|
194
|
+
// re-encoded rung CAN be cut there too, by being told those times, and then
|
|
195
|
+
// its segments cover the same spans and can stand in the copy's place.
|
|
196
|
+
base.transcodeVideo = false;
|
|
197
|
+
base.timeline = new Timeline({ boundaries: base.timeline?.boundaries ?? [], cutGrid: "keyframe" });
|
|
198
|
+
|
|
199
|
+
const master = manager.buildMasterPlaylist(BASE_ID);
|
|
200
|
+
|
|
201
|
+
assert.ok(master, "the obstacle was never the encoder, it was the cut points");
|
|
202
|
+
assert.match(master, /^v\/1080\/index\.m3u8$/m, "the copy itself is the top rung — no encoder, no cost");
|
|
203
|
+
assert.match(master, /^v\/540\/index\.m3u8$/m);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test("a copied video with no readable keyframe index is offered nothing", async (t) => {
|
|
207
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
208
|
+
t.after(async () => {
|
|
209
|
+
await manager.disposeAll();
|
|
210
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
211
|
+
});
|
|
212
|
+
// Its playlist claims an even grid that ffmpeg does not cut on. Aligning a
|
|
213
|
+
// rung to that is aligning it to a fiction.
|
|
214
|
+
base.transcodeVideo = false;
|
|
215
|
+
base.timeline = new Timeline({ boundaries: base.timeline?.boundaries ?? [], cutGrid: "uniform" });
|
|
216
|
+
|
|
217
|
+
assert.equal(manager.buildMasterPlaylist(BASE_ID), null);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test("the session's own height resolves to the session itself", async (t) => {
|
|
221
|
+
const { manager, dirPath } = await managerWithBase();
|
|
222
|
+
t.after(async () => {
|
|
223
|
+
await manager.disposeAll();
|
|
224
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
assert.deepEqual(
|
|
228
|
+
await manager.resolveVariantFile(BASE_ID, 812, "segment-00000.mp4"),
|
|
229
|
+
{ sessionId: BASE_ID },
|
|
230
|
+
"an encoder is already producing this height; making a second one would be a cold start for nothing"
|
|
231
|
+
);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test("a height the master does not offer is refused", async (t) => {
|
|
235
|
+
const { manager, dirPath } = await managerWithBase();
|
|
236
|
+
t.after(async () => {
|
|
237
|
+
await manager.disposeAll();
|
|
238
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
assert.deepEqual(
|
|
242
|
+
await manager.resolveVariantFile(BASE_ID, 999, "index.m3u8"),
|
|
243
|
+
{ sessionId: null },
|
|
244
|
+
"honouring an arbitrary height would let a client start encoder runs at will"
|
|
245
|
+
);
|
|
246
|
+
assert.deepEqual(
|
|
247
|
+
await manager.resolveVariantFile(BASE_ID, 540, "master.m3u8"),
|
|
248
|
+
{ sessionId: null },
|
|
249
|
+
"a master under a variant would describe variants of a variant"
|
|
250
|
+
);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("a variant's playlist is answered without starting an encoder for it", async (t) => {
|
|
254
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
255
|
+
t.after(async () => {
|
|
256
|
+
await manager.disposeAll();
|
|
257
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
const resolved = await manager.resolveVariantFile(BASE_ID, 540, "index.m3u8");
|
|
261
|
+
|
|
262
|
+
assert.deepEqual(
|
|
263
|
+
resolved,
|
|
264
|
+
{ sessionId: BASE_ID },
|
|
265
|
+
"every variant of a file has the same media playlist — that is what makes them interchangeable"
|
|
266
|
+
);
|
|
267
|
+
assert.equal(
|
|
268
|
+
base.file.stepHeights.size,
|
|
269
|
+
0,
|
|
270
|
+
"the player fetches a level's playlist to decide with, and may never switch to it"
|
|
271
|
+
);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test("a segment request hands the encoder to the variant the viewer moved to", async (t) => {
|
|
275
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
276
|
+
t.after(async () => {
|
|
277
|
+
await manager.disposeAll();
|
|
278
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
279
|
+
});
|
|
280
|
+
const variant = fakeSession({ id: VARIANT_ID, encodeHeight: 540, dirPath });
|
|
281
|
+
variant.variantHeight = 540;
|
|
282
|
+
// A step of the picture: made as one, same file, and a height of its own.
|
|
283
|
+
variant.isStep = true;
|
|
284
|
+
manager.sessionsById.set(VARIANT_ID, variant);
|
|
285
|
+
base.file.stepHeights.set(540, 540);
|
|
286
|
+
// The viewer is a hundred seconds in, and the base is the one encoding.
|
|
287
|
+
base.lastRequestedSegment = 25;
|
|
288
|
+
const encoder = fakeEncoder();
|
|
289
|
+
startRunOn(base, { process: encoder });
|
|
290
|
+
const served = await manager.resolveVariantFile(BASE_ID, 540, "segment-00025.mp4");
|
|
291
|
+
|
|
292
|
+
assert.equal(served.sessionId, VARIANT_ID, "the file must be served from the variant, not the base");
|
|
293
|
+
assert.equal(base.activeVariantId, VARIANT_ID, "the variant the viewer is watching is the active one");
|
|
294
|
+
assert.equal(
|
|
295
|
+
viewerOf(variant, "").positionSeconds(),
|
|
296
|
+
100,
|
|
297
|
+
"a segment request steers nothing, so where this person stands on the rung is stated outright — " +
|
|
298
|
+
"segment 25 of a four-second grid"
|
|
299
|
+
);
|
|
300
|
+
// AND THE RUNG THEY CAME OFF IS PRODUCING FOR NOBODY, which is one fact and
|
|
301
|
+
// not an act. It used to be stopped from this path by hand, and the plan —
|
|
302
|
+
// handed the whole film's priority map — started it again on the very next
|
|
303
|
+
// pass, which this viewer's own move had just triggered.
|
|
304
|
+
assert.equal(
|
|
305
|
+
manager.liveOutputs.watchedBy(base, viewerOf(base, "")),
|
|
306
|
+
false,
|
|
307
|
+
"the picture they stepped off is nobody's now"
|
|
308
|
+
);
|
|
309
|
+
assert.equal(
|
|
310
|
+
manager.liveOutputs.watchedBy(variant, viewerOf(variant, "")),
|
|
311
|
+
true,
|
|
312
|
+
"and the rung they moved to is theirs"
|
|
313
|
+
);
|
|
314
|
+
assert.deepEqual(encoder.signals, [], "stopping it is the plan's, from that fact, and not this path's");
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test("a rung is placed where the player asked it for, not where the other rung had read to", async (t) => {
|
|
318
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
319
|
+
t.after(async () => {
|
|
320
|
+
await manager.disposeAll();
|
|
321
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
322
|
+
});
|
|
323
|
+
const variant = fakeSession({ id: VARIANT_ID, encodeHeight: 540, dirPath });
|
|
324
|
+
variant.variantHeight = 540;
|
|
325
|
+
// A step of the picture: made as one, same file, and a height of its own.
|
|
326
|
+
variant.isStep = true;
|
|
327
|
+
manager.sessionsById.set(VARIANT_ID, variant);
|
|
328
|
+
base.file.stepHeights.set(540, 540);
|
|
329
|
+
startRunOn(base, { process: fakeEncoder() });
|
|
330
|
+
// The rung being left had read fourteen segments further than the picture had
|
|
331
|
+
// played — an encoder running at several times realtime fills the buffer far
|
|
332
|
+
// ahead. Measured 2026-08-11: 56 s of gap, and using the read head placed the
|
|
333
|
+
// new run past everything the player then asked for, which no request could
|
|
334
|
+
// ever be answered from.
|
|
335
|
+
base.lastRequestedSegment = 70;
|
|
336
|
+
base.furthestViewerSeconds = 280;
|
|
337
|
+
|
|
338
|
+
await manager.resolveVariantFile(BASE_ID, 540, "segment-00056.mp4");
|
|
339
|
+
|
|
340
|
+
assert.equal(
|
|
341
|
+
viewerOf(variant, "").positionSeconds(),
|
|
342
|
+
224,
|
|
343
|
+
"the segment the player asked this rung for is where this person is — 56 on a four-second grid, " +
|
|
344
|
+
"not the read head fourteen segments further on"
|
|
345
|
+
);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
test("warming a rung prepares it without taking the encoder from the one on screen", async (t) => {
|
|
349
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
350
|
+
t.after(async () => {
|
|
351
|
+
await manager.disposeAll();
|
|
352
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
353
|
+
});
|
|
354
|
+
const variant = fakeSession({ id: VARIANT_ID, encodeHeight: 540, dirPath });
|
|
355
|
+
variant.variantHeight = 540;
|
|
356
|
+
// A step of the picture: made as one, same file, and a height of its own.
|
|
357
|
+
variant.isStep = true;
|
|
358
|
+
manager.sessionsById.set(VARIANT_ID, variant);
|
|
359
|
+
base.file.stepHeights.set(540, 540);
|
|
360
|
+
const encoder = fakeEncoder();
|
|
361
|
+
startRunOn(base, { process: encoder });
|
|
362
|
+
const prepared = await manager.prepareVariant(BASE_ID, 540, 240);
|
|
363
|
+
|
|
364
|
+
assert.deepEqual(
|
|
365
|
+
prepared,
|
|
366
|
+
{ sessionId: VARIANT_ID, fileName: "segment-00060.mp4" },
|
|
367
|
+
"the caller is told which segment to wait for — 240 s on a four-second grid"
|
|
368
|
+
);
|
|
369
|
+
assert.equal(
|
|
370
|
+
viewerOf(variant, "").positionSeconds(),
|
|
371
|
+
240,
|
|
372
|
+
"the rung being warmed is told where this person is, which is what buys it an encoder"
|
|
373
|
+
);
|
|
374
|
+
assert.equal(base.activeVariantId, undefined, "nothing has switched yet");
|
|
375
|
+
assert.equal([...base.runs][0]?.process, encoder, "the picture on screen keeps its encoder until the player actually moves");
|
|
376
|
+
assert.deepEqual(encoder.signals, [], "stopping it here is what would put the spinner back");
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
test("a rung warmed at the playhead survives the switch that lands just ahead of it", async (t) => {
|
|
380
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
381
|
+
t.after(async () => {
|
|
382
|
+
await manager.disposeAll();
|
|
383
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
384
|
+
});
|
|
385
|
+
const variant = fakeSession({ id: VARIANT_ID, encodeHeight: 540, dirPath });
|
|
386
|
+
variant.variantHeight = 540;
|
|
387
|
+
// A step of the picture: made as one, same file, and a height of its own.
|
|
388
|
+
variant.isStep = true;
|
|
389
|
+
manager.sessionsById.set(VARIANT_ID, variant);
|
|
390
|
+
base.file.stepHeights.set(540, 540);
|
|
391
|
+
startRunOn(base, { process: fakeEncoder() });
|
|
392
|
+
// Warmed AT THE PLAYHEAD (240 s = segment #60), which is what the browser
|
|
393
|
+
// sends from server 0.10.0 onwards, and the run is alive and has produced a
|
|
394
|
+
// few segments past it.
|
|
395
|
+
await manager.prepareVariant(BASE_ID, 540, 240);
|
|
396
|
+
|
|
397
|
+
startRunOn(variant, { from: 59, process: fakeEncoder() });
|
|
398
|
+
variant.progress = { ...variant.progress, processedSeconds: 268 };
|
|
399
|
+
variant.seekTarget = null;
|
|
400
|
+
variant.seekSettleTimer = null;
|
|
401
|
+
|
|
402
|
+
// hls.js flushes from the fragment after the one holding
|
|
403
|
+
// `currentTime + fetchdelay`, so its first request for the new rung is the
|
|
404
|
+
// playhead plus up to one fragment — here #61 against a run that began at
|
|
405
|
+
// #59. Warming at the END OF THE BUFFER instead put the run tens of seconds
|
|
406
|
+
// AHEAD of this request, which the proxy then read as a seek backwards:
|
|
407
|
+
// measured 2026-08-14, that killed a run holding 21.8 s of encoded output.
|
|
408
|
+
await manager.resolveVariantFile(BASE_ID, 540, "segment-00061.mp4");
|
|
409
|
+
|
|
410
|
+
assert.equal(base.activeVariantId, VARIANT_ID, "the viewer has moved to this rung");
|
|
411
|
+
assert.equal(
|
|
412
|
+
variant.seekTarget,
|
|
413
|
+
null,
|
|
414
|
+
"the request is inside the warmed run, so nothing is repositioned and the warm-up is kept"
|
|
415
|
+
);
|
|
416
|
+
assert.equal([...variant.runs][0].from, 59, "the run still begins where it was warmed");
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
test("a rung warmed PAST the switch is repositioned, which is what warming late costs", async (t) => {
|
|
420
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
421
|
+
t.after(async () => {
|
|
422
|
+
await manager.disposeAll();
|
|
423
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
424
|
+
});
|
|
425
|
+
const variant = fakeSession({ id: VARIANT_ID, encodeHeight: 540, dirPath });
|
|
426
|
+
variant.variantHeight = 540;
|
|
427
|
+
// A step of the picture: made as one, same file, and a height of its own.
|
|
428
|
+
variant.isStep = true;
|
|
429
|
+
manager.sessionsById.set(VARIANT_ID, variant);
|
|
430
|
+
base.file.stepHeights.set(540, 540);
|
|
431
|
+
startRunOn(base, { process: fakeEncoder() });
|
|
432
|
+
// The same session, warmed where the BUFFER ended rather than where the
|
|
433
|
+
// picture was — 60 s further on, which is an ordinary cushion. This is what
|
|
434
|
+
// server 0.9.3 sent and 0.11.0 stopped sending.
|
|
435
|
+
await manager.prepareVariant(BASE_ID, 540, 300);
|
|
436
|
+
startRunOn(variant, { from: 74, process: fakeEncoder() });
|
|
437
|
+
variant.progress = { ...variant.progress, processedSeconds: 310 };
|
|
438
|
+
|
|
439
|
+
// hls.js still lands near the playhead, so the request is far BEHIND the
|
|
440
|
+
// warmed run: the proxy reads it as a seek backwards and starts again, and
|
|
441
|
+
// everything the warm-up produced is thrown away. Measured in the field
|
|
442
|
+
// 2026-08-14 as 21.8 s of encoded output destroyed by the act of using it.
|
|
443
|
+
await manager.resolveVariantFile(BASE_ID, 540, "segment-00061.mp4");
|
|
444
|
+
|
|
445
|
+
assert.equal(
|
|
446
|
+
viewerOf(variant, "").positionSeconds(),
|
|
447
|
+
244,
|
|
448
|
+
"where the player actually asked, not where the warm-up had run to"
|
|
449
|
+
);
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
test("the rung on screen fetching its own segments does not cancel a warm-up", async (t) => {
|
|
453
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
454
|
+
t.after(async () => {
|
|
455
|
+
await manager.disposeAll();
|
|
456
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
457
|
+
});
|
|
458
|
+
const variant = fakeSession({ id: VARIANT_ID, encodeHeight: 540, dirPath });
|
|
459
|
+
variant.variantHeight = 540;
|
|
460
|
+
// A step of the picture: made as one, same file, and a height of its own.
|
|
461
|
+
variant.isStep = true;
|
|
462
|
+
manager.sessionsById.set(VARIANT_ID, variant);
|
|
463
|
+
base.file.stepHeights.set(540, 540);
|
|
464
|
+
const warmedEncoder = fakeEncoder();
|
|
465
|
+
startRunOn(variant, { process: warmedEncoder });
|
|
466
|
+
startRunOn(base, { process: fakeEncoder() });
|
|
467
|
+
await manager.prepareVariant(BASE_ID, 540, 100);
|
|
468
|
+
|
|
469
|
+
// The viewer has not moved: the rung they are watching goes on asking for its
|
|
470
|
+
// own segments, every few seconds, for as long as they watch.
|
|
471
|
+
await manager.resolveVariantFile(BASE_ID, 812, "segment-00026.mp4");
|
|
472
|
+
await manager.resolveVariantFile(BASE_ID, 812, "segment-00027.mp4");
|
|
473
|
+
|
|
474
|
+
// Kept per viewer, and this one is the unnamed viewer of a transport that
|
|
475
|
+
// carries no consumer id.
|
|
476
|
+
assert.equal(
|
|
477
|
+
base.viewers.get("")?.warmingVariantId ?? null,
|
|
478
|
+
VARIANT_ID,
|
|
479
|
+
"the rung being prepared is still being prepared"
|
|
480
|
+
);
|
|
481
|
+
assert.deepEqual(
|
|
482
|
+
warmedEncoder.signals,
|
|
483
|
+
[],
|
|
484
|
+
"cancelling it here left the viewer waiting out the whole warm-up for a segment nobody was making"
|
|
485
|
+
);
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
test("warming the height the base itself serves still points it at the switch", async (t) => {
|
|
489
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
490
|
+
t.after(async () => {
|
|
491
|
+
await manager.disposeAll();
|
|
492
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
493
|
+
});
|
|
494
|
+
// The viewer is on another rung; the base is parked where they left it, with
|
|
495
|
+
// its encoder stopped. Warming its height must bring it back.
|
|
496
|
+
const variant = fakeSession({ id: VARIANT_ID, encodeHeight: 540, dirPath });
|
|
497
|
+
variant.variantHeight = 540;
|
|
498
|
+
manager.sessionsById.set(VARIANT_ID, variant);
|
|
499
|
+
base.file.stepHeights.set(540, 540);
|
|
500
|
+
base.activeVariantId = VARIANT_ID;
|
|
501
|
+
base.runs = new Set();
|
|
502
|
+
|
|
503
|
+
await manager.prepareVariant(BASE_ID, 812, 400);
|
|
504
|
+
|
|
505
|
+
// 400 s falls on the boundary between #99 and #100, and a run starts one
|
|
506
|
+
// segment back so the player has the preceding keyframe.
|
|
507
|
+
assert.equal(
|
|
508
|
+
viewerOf(base, "").positionSeconds(),
|
|
509
|
+
396,
|
|
510
|
+
"the base is parked where they left it, so warming its height says where they are like any other rung"
|
|
511
|
+
);
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
test("the viewer's position is kept current by the segments they ask for", async (t) => {
|
|
515
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
516
|
+
t.after(async () => {
|
|
517
|
+
await manager.disposeAll();
|
|
518
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
519
|
+
});
|
|
520
|
+
// A seek an hour ago is the only thing that ever wrote this field, and
|
|
521
|
+
// playback reports no position at all. Read as it stood, a quality change
|
|
522
|
+
// would place the new variant's encode run back at the seek — and since a
|
|
523
|
+
// segment request steers nothing, the segments the player then asks for would
|
|
524
|
+
// never be produced by anyone.
|
|
525
|
+
base.furthestViewerSeconds = 40;
|
|
526
|
+
|
|
527
|
+
await manager.getFileStream(BASE_ID, "segment-00090.mp4", { requestSeq: 1 });
|
|
528
|
+
|
|
529
|
+
assert.equal(
|
|
530
|
+
base.furthestViewerSeconds,
|
|
531
|
+
360,
|
|
532
|
+
"a request for segment #90 of a four-second grid says where the viewer is now"
|
|
533
|
+
);
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
test("a playlist or an init segment does not move the encoder", async (t) => {
|
|
537
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
538
|
+
t.after(async () => {
|
|
539
|
+
await manager.disposeAll();
|
|
540
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
541
|
+
});
|
|
542
|
+
const variant = fakeSession({ id: VARIANT_ID, encodeHeight: 540, dirPath });
|
|
543
|
+
manager.sessionsById.set(VARIANT_ID, variant);
|
|
544
|
+
base.file.stepHeights.set(540, 540);
|
|
545
|
+
startRunOn(base, { process: fakeEncoder() });
|
|
546
|
+
base.file.stepHeights.set(540, 540);
|
|
547
|
+
await manager.resolveVariantFile(BASE_ID, 540, "index.m3u8");
|
|
548
|
+
await manager.resolveVariantFile(BASE_ID, 540, "init.mp4");
|
|
549
|
+
|
|
550
|
+
assert.notEqual(
|
|
551
|
+
base.activeVariantId,
|
|
552
|
+
VARIANT_ID,
|
|
553
|
+
"hls.js fetches a level's playlist and init to decide with, and may never switch to it"
|
|
554
|
+
);
|
|
555
|
+
assert.ok([...base.runs][0]?.process, "the stream on screen must keep its encoder while the player is only looking");
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
test("the name of a variant is fixed, whatever its encode is later set to", async (t) => {
|
|
559
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
560
|
+
t.after(async () => {
|
|
561
|
+
await manager.disposeAll();
|
|
562
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
563
|
+
});
|
|
564
|
+
// The player fetched the master once and addresses this variant as 812p for
|
|
565
|
+
// the rest of the session, so the name must not follow the encode. Nothing in
|
|
566
|
+
// the proxy changes `encodeHeight` mid-session any more — a change of size is
|
|
567
|
+
// a change of variant now — but the name and the encode are still two
|
|
568
|
+
// different things, and the addressing depends on their staying so.
|
|
569
|
+
assert.equal(manager.liveOutputs.variantHeightOf(base), 812);
|
|
570
|
+
base.encodeHeight = 540;
|
|
571
|
+
|
|
572
|
+
assert.equal(
|
|
573
|
+
manager.liveOutputs.variantHeightOf(base),
|
|
574
|
+
812,
|
|
575
|
+
"the name stays; renaming it would leave the player addressing a variant nobody answers for"
|
|
576
|
+
);
|
|
577
|
+
assert.deepEqual(
|
|
578
|
+
await manager.resolveVariantFile(BASE_ID, 812, "segment-00000.mp4"),
|
|
579
|
+
{ sessionId: BASE_ID },
|
|
580
|
+
"a second session at a height the host has already failed to manage is the opposite of what a step is for"
|
|
581
|
+
);
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
test("the cut grid follows the grid asked for, not who produces the frames", () => {
|
|
585
|
+
// Why a segment from one encoder can stand where another's would have: both
|
|
586
|
+
// are cut at the same times. Which times is a property of the SESSION — the
|
|
587
|
+
// even grid, or the source's own keyframes — and it must not be re-derived
|
|
588
|
+
// from whether the video is copied, because a variant of a copied stream is
|
|
589
|
+
// re-encoded and still has to land on the copy's cuts.
|
|
590
|
+
const shape = { durationSeconds: 100, segDur: SEGMENT_SECONDS, startTime: 0 };
|
|
591
|
+
const keyframeTimes = [0, 3.1, 9.7, 14.2, 21.5, 40, 61.25];
|
|
592
|
+
|
|
593
|
+
const even = computeCutGrid({ ...shape, useKeyframeGrid: false, keyframeTimes });
|
|
594
|
+
assert.equal(even.boundaries[1], SEGMENT_SECONDS, "the even grid ignores the source's keyframes");
|
|
595
|
+
assert.equal(even.boundaries.at(-1), 100);
|
|
596
|
+
|
|
597
|
+
const source = computeCutGrid({ ...shape, useKeyframeGrid: true, keyframeTimes });
|
|
598
|
+
assert.deepEqual(
|
|
599
|
+
source.boundaries,
|
|
600
|
+
[0, 9.7, 14.2, 21.5, 40, 61.25, 100],
|
|
601
|
+
"the source's own keyframes, kept only where they are at least a segment apart"
|
|
602
|
+
);
|
|
603
|
+
|
|
604
|
+
// The one that matters: a copy and a re-encoded rung of it, given the same
|
|
605
|
+
// grid, produce the SAME table. Segment N then covers the same span in both,
|
|
606
|
+
// which is what lets one stand where the other would have.
|
|
607
|
+
assert.deepEqual(
|
|
608
|
+
computeCutGrid({ ...shape, useKeyframeGrid: true, keyframeTimes }).boundaries,
|
|
609
|
+
source.boundaries,
|
|
610
|
+
"a variant inherits the grid, so its boundaries are the same values"
|
|
611
|
+
);
|
|
612
|
+
// And with no index there is nothing to align to — the even grid, whoever asks.
|
|
613
|
+
assert.deepEqual(
|
|
614
|
+
computeCutGrid({ ...shape, useKeyframeGrid: true, keyframeTimes: null }).boundaries,
|
|
615
|
+
even.boundaries,
|
|
616
|
+
"no keyframes means no keyframe grid, however the caller asks"
|
|
617
|
+
);
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
test("a boundary carries the keyframe it is, exactly as the container stated it", () => {
|
|
621
|
+
// The fault that stopped a viewing on 2026-09-05. A boundary is kept on the
|
|
622
|
+
// player's clock, `keyframe - startTime`, rounded; the seek used to add the
|
|
623
|
+
// start time back and look the result up in the container's list by value.
|
|
624
|
+
// The round trip is lossy — 26.234 - 0.083 + 0.083 is 26.233999999999998 —
|
|
625
|
+
// and "the keyframe at or before that" is then the PREVIOUS one, a whole
|
|
626
|
+
// keyframe interval earlier.
|
|
627
|
+
const startTime = 0.083;
|
|
628
|
+
const keyframeTimes = [];
|
|
629
|
+
for (let index = 0; index < 60; index += 1) {
|
|
630
|
+
keyframeTimes.push(Number((startTime + index * 8.717).toFixed(3)));
|
|
631
|
+
}
|
|
632
|
+
const grid = computeCutGrid({
|
|
633
|
+
useKeyframeGrid: true, durationSeconds: 500, segDur: SEGMENT_SECONDS, keyframeTimes, startTime
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
assert.equal(grid.sourceTimes[0], startTime, "the first cut is the start of the file");
|
|
637
|
+
assert.equal(grid.boundaries.length, grid.sourceTimes.length, "an index must name both clocks");
|
|
638
|
+
for (let index = 1; index < grid.boundaries.length - 1; index += 1) {
|
|
639
|
+
assert.ok(
|
|
640
|
+
keyframeTimes.includes(grid.sourceTimes[index]),
|
|
641
|
+
`cut #${index} carries ${grid.sourceTimes[index]}, which the container never stated`
|
|
642
|
+
);
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// What the round trip does, so the reason this table exists cannot be
|
|
646
|
+
// mistaken for caution: at least one boundary fails to find itself.
|
|
647
|
+
const lost = grid.boundaries
|
|
648
|
+
.slice(1, -1)
|
|
649
|
+
.filter((published, at) => nearestKeyframeAtOrBefore(keyframeTimes, published + startTime)
|
|
650
|
+
!== grid.sourceTimes[at + 1]);
|
|
651
|
+
assert.ok(
|
|
652
|
+
lost.length > 0,
|
|
653
|
+
"the search agreed everywhere, so this fixture no longer reproduces the fault it was built for"
|
|
654
|
+
);
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
test("a run on the keyframe grid is given no trim to apply", () => {
|
|
658
|
+
// The second half of the same fault. That trim is an output-side `-ss`, and
|
|
659
|
+
// beside `-copyts` it moves the whole run backwards by its own amount:
|
|
660
|
+
// measured 2026-09-06 on a 5 s keyframe interval, a run landing at 15 s and
|
|
661
|
+
// asked to trim to the cut at 20 s produced its first file starting at 10 s.
|
|
662
|
+
// Every cut of the run inherits it while the numbering, fixed at spawn, does
|
|
663
|
+
// not — so the files are named for times they do not hold.
|
|
664
|
+
const startTime = 0.083;
|
|
665
|
+
const keyframeTimes = [];
|
|
666
|
+
for (let index = 0; index < 60; index += 1) {
|
|
667
|
+
keyframeTimes.push(Number((startTime + index * 8.717).toFixed(3)));
|
|
668
|
+
}
|
|
669
|
+
const grid = computeCutGrid({
|
|
670
|
+
useKeyframeGrid: true, durationSeconds: 500, segDur: SEGMENT_SECONDS, keyframeTimes, startTime
|
|
671
|
+
});
|
|
672
|
+
const timeline = new Timeline({
|
|
673
|
+
boundaries: grid.boundaries, sourceTimes: grid.sourceTimes, cutGrid: "keyframe"
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
for (let index = 1; index < grid.boundaries.length - 1; index += 1) {
|
|
677
|
+
const { args } = buildRunCommand({
|
|
678
|
+
file: { keyframeTimes },
|
|
679
|
+
inputFile: { startTime },
|
|
680
|
+
audioFile: { startTime },
|
|
681
|
+
inputUrl: "http://127.0.0.1/stream",
|
|
682
|
+
audioInputUrl: "",
|
|
683
|
+
outputDir: ".",
|
|
684
|
+
timeline,
|
|
685
|
+
segmentFormat: fmp4Format,
|
|
686
|
+
transcodeVideo: false,
|
|
687
|
+
transcodeAudio: true,
|
|
688
|
+
audioOnly: false,
|
|
689
|
+
audioSeparate: false,
|
|
690
|
+
audioSourceTrackIndex: 0,
|
|
691
|
+
rateCapKbps: 0,
|
|
692
|
+
startIndex: index,
|
|
693
|
+
endIndex: index,
|
|
694
|
+
videoEncoder: { name: "libx264" },
|
|
695
|
+
segmentDurationSec: SEGMENT_SECONDS
|
|
696
|
+
});
|
|
697
|
+
// Exactly one `-ss`, and it is the input seek: the trim is not there to be
|
|
698
|
+
// inherited by the cuts.
|
|
699
|
+
assert.equal(
|
|
700
|
+
args.filter((one) => one === "-ss").length,
|
|
701
|
+
1,
|
|
702
|
+
`run at #${index} was given a trim beside -copyts`
|
|
703
|
+
);
|
|
704
|
+
const seek = Number(args[args.indexOf("-ss") + 1]);
|
|
705
|
+
assert.ok(
|
|
706
|
+
Math.abs(seek - grid.sourceTimes[index]) < 0.2,
|
|
707
|
+
`run at #${index} seeks to ${seek}, not to its own keyframe ${grid.sourceTimes[index]}`
|
|
708
|
+
);
|
|
709
|
+
}
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
test("a rung served by copy stays offered while a re-encoded rung is on screen", async (t) => {
|
|
713
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
714
|
+
t.after(async () => {
|
|
715
|
+
await manager.disposeAll();
|
|
716
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
717
|
+
});
|
|
718
|
+
// The field case of 2026-08-15: a 1080p source served by COPY, the viewer on
|
|
719
|
+
// 240p, and a host too weak to re-encode anything above it.
|
|
720
|
+
base.transcodeVideo = false;
|
|
721
|
+
base.encodeHeight = 1080;
|
|
722
|
+
base.variantHeight = 1080;
|
|
723
|
+
// Enough to re-encode 240p (1.67x combined) and nowhere near enough for
|
|
724
|
+
// 720p (0.45x) — the field's own shape, where the rung the viewer picked was
|
|
725
|
+
// offered and everything between it and the copy was not.
|
|
726
|
+
manager.softwarePresetBenchmark = [{ preset: "ultrafast", pixelsPerSec: 12_000_000 }];
|
|
727
|
+
manager.decodeCostModel = { pixelTerm: 0.00793, bitrateTerm: 0, constantTerm: 0 };
|
|
728
|
+
// What decoding this source costs comes from the file's own facts, stated in
|
|
729
|
+
// the fixture: 49.766 Mpx/s at 8 Mbit/s.
|
|
730
|
+
assert.ok(base.file.decode, "the fixture must state enough for a decode cost to exist");
|
|
731
|
+
|
|
732
|
+
const watching = fakeSession({ id: VARIANT_ID, encodeHeight: 240, dirPath });
|
|
733
|
+
watching.variantHeight = 240;
|
|
734
|
+
watching.transcodeVideo = true;
|
|
735
|
+
// The rung knows the source as well as the base does, because it IS the same
|
|
736
|
+
// file. Without that it prices nothing at all — every height comes back
|
|
737
|
+
// "sustainable" for want of a measurement — and the assertion below would
|
|
738
|
+
// hold for the wrong reason.
|
|
739
|
+
watching.file = base.file;
|
|
740
|
+
// A step of the picture: same file, and made as a step.
|
|
741
|
+
watching.isStep = true;
|
|
742
|
+
manager.sessionsById.set(VARIANT_ID, watching);
|
|
743
|
+
base.file.stepHeights.set(240, 240);
|
|
744
|
+
base.activeVariantId = VARIANT_ID;
|
|
745
|
+
|
|
746
|
+
const offered = manager.offeredHeights(watching);
|
|
747
|
+
|
|
748
|
+
assert.ok(
|
|
749
|
+
offered.includes(1080),
|
|
750
|
+
"the height the source is COPIED at costs no encoder, so no measurement of this host can withdraw it"
|
|
751
|
+
);
|
|
752
|
+
assert.deepEqual(
|
|
753
|
+
offered,
|
|
754
|
+
manager.offeredHeights(base),
|
|
755
|
+
"one answer for the family: a rung asked while watching another must not disagree with the base"
|
|
756
|
+
);
|
|
757
|
+
});
|
|
758
|
+
|
|
759
|
+
test("a separately published audio track starts where the picture is, from the reported buffer", async (t) => {
|
|
760
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
761
|
+
t.after(async () => {
|
|
762
|
+
await manager.disposeAll();
|
|
763
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
764
|
+
});
|
|
765
|
+
base.audioSeparate = true;
|
|
766
|
+
// Served up to 140 s, and the browser says it holds 40 s ahead of the
|
|
767
|
+
// picture — so the viewer is at 100 s, and that, less a segment of margin,
|
|
768
|
+
// is where the track has to begin.
|
|
769
|
+
base.furthestViewerSeconds = 140;
|
|
770
|
+
viewerOf(base, "viewer").netReport = {
|
|
771
|
+
linkMbps: 20,
|
|
772
|
+
bufferedAheadSec: 40,
|
|
773
|
+
positionSeconds: null,
|
|
774
|
+
at: Date.now()
|
|
775
|
+
};
|
|
776
|
+
manager.getCachedAudioTracks = () => [
|
|
777
|
+
{ index: 0, language: "rus", title: "", isDefault: true },
|
|
778
|
+
{ index: 1, language: "eng", title: "", isDefault: false }
|
|
779
|
+
];
|
|
780
|
+
const created = [];
|
|
781
|
+
manager.createOrGetSession = async (params) => {
|
|
782
|
+
created.push(params);
|
|
783
|
+
const rendition = fakeSession({ id: VARIANT_ID, encodeHeight: 0, dirPath });
|
|
784
|
+
rendition.audioOnly = true;
|
|
785
|
+
return { sessionId: VARIANT_ID, session: rendition };
|
|
786
|
+
};
|
|
787
|
+
|
|
788
|
+
await manager.resolveAudioRenditionFile(BASE_ID, 1, "segment-00010.mp4");
|
|
789
|
+
|
|
790
|
+
assert.equal(created.length, 1, "the track's own session was made");
|
|
791
|
+
assert.equal(
|
|
792
|
+
created[0].startPositionSeconds,
|
|
793
|
+
96,
|
|
794
|
+
"140 s served, less the 40 s the player holds, less one segment of margin"
|
|
795
|
+
);
|
|
796
|
+
});
|
|
797
|
+
|
|
798
|
+
test("with two viewers the audio track starts at the EARLIEST picture, not the read head", async (t) => {
|
|
799
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
800
|
+
t.after(async () => {
|
|
801
|
+
await manager.disposeAll();
|
|
802
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
803
|
+
});
|
|
804
|
+
base.audioSeparate = true;
|
|
805
|
+
// A copied picture is one session shared by both of them. The read head is
|
|
806
|
+
// the furthest request of EITHER, so it belongs to the viewer in front.
|
|
807
|
+
base.furthestViewerSeconds = 140;
|
|
808
|
+
viewerOf(base, "ahead").netReport = {
|
|
809
|
+
linkMbps: 20,
|
|
810
|
+
bufferedAheadSec: 40,
|
|
811
|
+
positionSeconds: 100,
|
|
812
|
+
at: Date.now()
|
|
813
|
+
};
|
|
814
|
+
viewerOf(base, "behind").netReport = {
|
|
815
|
+
linkMbps: 20,
|
|
816
|
+
bufferedAheadSec: 8,
|
|
817
|
+
positionSeconds: 40,
|
|
818
|
+
at: Date.now()
|
|
819
|
+
};
|
|
820
|
+
manager.getCachedAudioTracks = () => [
|
|
821
|
+
{ index: 0, language: "rus", title: "", isDefault: true },
|
|
822
|
+
{ index: 1, language: "eng", title: "", isDefault: false }
|
|
823
|
+
];
|
|
824
|
+
const created = [];
|
|
825
|
+
manager.createOrGetSession = async (params) => {
|
|
826
|
+
created.push(params);
|
|
827
|
+
const rendition = fakeSession({ id: VARIANT_ID, encodeHeight: 0, dirPath });
|
|
828
|
+
rendition.audioOnly = true;
|
|
829
|
+
return { sessionId: VARIANT_ID, session: rendition };
|
|
830
|
+
};
|
|
831
|
+
|
|
832
|
+
await manager.resolveAudioRenditionFile(BASE_ID, 1, "segment-00010.mp4");
|
|
833
|
+
|
|
834
|
+
assert.equal(
|
|
835
|
+
created[0].startPositionSeconds,
|
|
836
|
+
36,
|
|
837
|
+
"the viewer at 40 s, less one segment of margin — a run starting at the leader " +
|
|
838
|
+
"has nothing to give the one behind them"
|
|
839
|
+
);
|
|
840
|
+
});
|
|
841
|
+
|
|
842
|
+
test("a position past the read head is clamped rather than acted on", async (t) => {
|
|
843
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
844
|
+
t.after(async () => {
|
|
845
|
+
await manager.disposeAll();
|
|
846
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
847
|
+
});
|
|
848
|
+
base.audioSeparate = true;
|
|
849
|
+
base.furthestViewerSeconds = 140;
|
|
850
|
+
// Reports and requests race; a position claiming to be past everything that
|
|
851
|
+
// has been asked for would start the run where no request can reach it.
|
|
852
|
+
viewerOf(base, "viewer").netReport = {
|
|
853
|
+
linkMbps: 20,
|
|
854
|
+
bufferedAheadSec: 40,
|
|
855
|
+
positionSeconds: 900,
|
|
856
|
+
at: Date.now()
|
|
857
|
+
};
|
|
858
|
+
manager.getCachedAudioTracks = () => [
|
|
859
|
+
{ index: 0, language: "rus", title: "", isDefault: true },
|
|
860
|
+
{ index: 1, language: "eng", title: "", isDefault: false }
|
|
861
|
+
];
|
|
862
|
+
const created = [];
|
|
863
|
+
manager.createOrGetSession = async (params) => {
|
|
864
|
+
created.push(params);
|
|
865
|
+
const rendition = fakeSession({ id: VARIANT_ID, encodeHeight: 0, dirPath });
|
|
866
|
+
rendition.audioOnly = true;
|
|
867
|
+
return { sessionId: VARIANT_ID, session: rendition };
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
await manager.resolveAudioRenditionFile(BASE_ID, 1, "segment-00010.mp4");
|
|
871
|
+
|
|
872
|
+
assert.equal(
|
|
873
|
+
created[0].startPositionSeconds,
|
|
874
|
+
136,
|
|
875
|
+
"clamped to the read head, less one segment of margin"
|
|
876
|
+
);
|
|
877
|
+
});
|
|
878
|
+
|
|
879
|
+
test("a stale buffer report is not used to place an audio track", async (t) => {
|
|
880
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
881
|
+
t.after(async () => {
|
|
882
|
+
await manager.disposeAll();
|
|
883
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
884
|
+
});
|
|
885
|
+
base.audioSeparate = true;
|
|
886
|
+
base.furthestViewerSeconds = 300;
|
|
887
|
+
// Sent a minute ago: the viewer may have seeked anywhere since, so neither
|
|
888
|
+
// the buffer nor the position in it says where they are now.
|
|
889
|
+
viewerOf(base, "viewer").netReport = {
|
|
890
|
+
linkMbps: 20,
|
|
891
|
+
bufferedAheadSec: 5,
|
|
892
|
+
positionSeconds: 250,
|
|
893
|
+
at: Date.now() - 60_000
|
|
894
|
+
};
|
|
895
|
+
manager.getCachedAudioTracks = () => [
|
|
896
|
+
{ index: 0, language: "rus", title: "", isDefault: true },
|
|
897
|
+
{ index: 1, language: "eng", title: "", isDefault: false }
|
|
898
|
+
];
|
|
899
|
+
const created = [];
|
|
900
|
+
manager.createOrGetSession = async (params) => {
|
|
901
|
+
created.push(params);
|
|
902
|
+
const rendition = fakeSession({ id: VARIANT_ID, encodeHeight: 0, dirPath });
|
|
903
|
+
rendition.audioOnly = true;
|
|
904
|
+
return { sessionId: VARIANT_ID, session: rendition };
|
|
905
|
+
};
|
|
906
|
+
|
|
907
|
+
await manager.resolveAudioRenditionFile(BASE_ID, 1, "segment-00010.mp4");
|
|
908
|
+
|
|
909
|
+
assert.equal(
|
|
910
|
+
created[0].startPositionSeconds,
|
|
911
|
+
176,
|
|
912
|
+
"the whole look-ahead is subtracted instead — it cannot leave the run ahead of the viewer"
|
|
913
|
+
);
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
test("an audio track is prepared at the position the switch will land on", async (t) => {
|
|
917
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
918
|
+
t.after(async () => {
|
|
919
|
+
await manager.disposeAll();
|
|
920
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
921
|
+
});
|
|
922
|
+
base.audioSeparate = true;
|
|
923
|
+
manager.getCachedAudioTracks = () => [
|
|
924
|
+
{ index: 0, language: "rus", title: "", isDefault: true },
|
|
925
|
+
{ index: 1, language: "eng", title: "", isDefault: false }
|
|
926
|
+
];
|
|
927
|
+
const rendition = fakeSession({ id: VARIANT_ID, encodeHeight: 0, dirPath });
|
|
928
|
+
rendition.audioOnly = true;
|
|
929
|
+
rendition.audioTrackIndex = 1;
|
|
930
|
+
rendition.transcodeAudio = true;
|
|
931
|
+
// A soundtrack of THIS picture: the same file, published on its own. Found by
|
|
932
|
+
// what it is — there is no list of ids to put it on.
|
|
933
|
+
rendition.file = base.file;
|
|
934
|
+
startRunOn(rendition, { process: fakeEncoder() });
|
|
935
|
+
manager.sessionsById.set(VARIANT_ID, rendition);
|
|
936
|
+
|
|
937
|
+
const prepared = await manager.prepareAudioTrack(BASE_ID, 1, 240);
|
|
938
|
+
|
|
939
|
+
assert.deepEqual(
|
|
940
|
+
prepared,
|
|
941
|
+
{ sessionId: VARIANT_ID, fileName: "segment-00060.mp4" },
|
|
942
|
+
"the caller is told which segment to wait for — 240 s on a four-second grid"
|
|
943
|
+
);
|
|
944
|
+
assert.equal(
|
|
945
|
+
viewerOf(rendition, "").positionSeconds(),
|
|
946
|
+
240,
|
|
947
|
+
"and the track is told where this person is, which is what the switch will land on"
|
|
948
|
+
);
|
|
949
|
+
});
|
|
950
|
+
|
|
951
|
+
test("a reading from a soundtrack is priced as one", () => {
|
|
952
|
+
// The fault this pins: an audio rendition reached no learner at all. One
|
|
953
|
+
// guard refused renditions a reading, and the only call that would have
|
|
954
|
+
// priced one sat behind a second guard its caller had already made — so a
|
|
955
|
+
// soundtrack ran for the whole film and was charged at nothing, which is the
|
|
956
|
+
// half of roadmap item 6 that was left owing.
|
|
957
|
+
assert.equal(costKindForSession({ audioOnly: true, transcodeVideo: false }), "audio");
|
|
958
|
+
assert.equal(
|
|
959
|
+
costKindForSession({ audioOnly: true, transcodeVideo: true }),
|
|
960
|
+
"audio",
|
|
961
|
+
"a rendition carries no picture, whatever the flag it inherited says"
|
|
962
|
+
);
|
|
963
|
+
assert.equal(costKindForSession({ transcodeVideo: true }), "decode");
|
|
964
|
+
assert.equal(costKindForSession({ transcodeVideo: false }), "copy");
|
|
965
|
+
});
|
|
966
|
+
|
|
967
|
+
test("a quality step being warmed is not refused by its own cost", async (t) => {
|
|
968
|
+
const dirPath = await mkdtemp(path.join(os.tmpdir(), "warm-cost-"));
|
|
969
|
+
// The addon host's own figures, so the arithmetic below is the field's and
|
|
970
|
+
// not an invention. Without a benchmark the check returns every height
|
|
971
|
+
// untouched and a test over it would pass while proving nothing.
|
|
972
|
+
const manager = new HlsSessionManager({
|
|
973
|
+
enabled: true,
|
|
974
|
+
ffmpegBin: "ffmpeg",
|
|
975
|
+
localBindHost: "127.0.0.1",
|
|
976
|
+
localPort: 9090,
|
|
977
|
+
softwarePresetBenchmark: [
|
|
978
|
+
{ preset: "fast", pixelsPerSec: 17.0e6 },
|
|
979
|
+
{ preset: "ultrafast", pixelsPerSec: 67.5e6 }
|
|
980
|
+
],
|
|
981
|
+
decodeCostModel: { pixelTerm: 0.007742, bitrateTerm: 0, constantTerm: 0 }
|
|
982
|
+
});
|
|
983
|
+
t.after(async () => {
|
|
984
|
+
await manager.disposeAll();
|
|
985
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
986
|
+
});
|
|
987
|
+
// A copied 1080p picture, and a 240p step warmed beside it for a switch —
|
|
988
|
+
// which is what every quality change does, two encoders on purpose.
|
|
989
|
+
const base = fakeSession({ id: BASE_ID, encodeHeight: 0, dirPath, transcodeVideo: false });
|
|
990
|
+
base.variantHeight = 1080;
|
|
991
|
+
// What this source costs to decode comes from the file's own facts, stated in
|
|
992
|
+
// the fixture: 1080p24 at 8 Mbit/s, the field file. Without them nothing can
|
|
993
|
+
// be priced and every height comes back offerable for want of a measurement,
|
|
994
|
+
// which would make the assertion hold for the wrong reason.
|
|
995
|
+
assert.ok(base.file.decode, "the fixture must state enough for a decode cost to exist");
|
|
996
|
+
const warming = fakeSession({ id: VARIANT_ID, encodeHeight: 240, dirPath });
|
|
997
|
+
warming.variantHeight = 240;
|
|
998
|
+
// Two sessions of ONE file share its facts, which is the whole point of the
|
|
999
|
+
// file being an object: a step warmed beside the picture is not a second file.
|
|
1000
|
+
warming.file = base.file;
|
|
1001
|
+
base.file.stepHeights.set(240, 240);
|
|
1002
|
+
// A step of the picture: same file, and a height of its own.
|
|
1003
|
+
// Running, and running well: it says of itself that it holds twice realtime,
|
|
1004
|
+
// i.e. half a second of work per second of video.
|
|
1005
|
+
startRunOn(warming, { process: fakeEncoder() });
|
|
1006
|
+
warming.lastAloneSpeed = 2;
|
|
1007
|
+
manager.sessionsById.set(BASE_ID, base);
|
|
1008
|
+
manager.sessionsById.set(VARIANT_ID, warming);
|
|
1009
|
+
|
|
1010
|
+
const offered = manager.offeredHeights(base);
|
|
1011
|
+
|
|
1012
|
+
assert.ok(
|
|
1013
|
+
offered.includes(240),
|
|
1014
|
+
"charged its own cost while being judged, the step the viewer just asked for is dropped from the " +
|
|
1015
|
+
"offer by the act of warming it — and its next segment 404s on a stream that is playing"
|
|
1016
|
+
);
|
|
1017
|
+
});
|
|
1018
|
+
|
|
1019
|
+
test("the master survives a live offer that has collapsed to one rung", async (t) => {
|
|
1020
|
+
// The field case of 2026-08-18, in the smallest form that reproduces it: a
|
|
1021
|
+
// host too slow for any re-encoded rung, and a swarm whose interruptions
|
|
1022
|
+
// demand far more than realtime. The live offer then holds only the height an
|
|
1023
|
+
// encoder is already producing — and until this test existed, that made
|
|
1024
|
+
// `buildMasterPlaylist` answer null and the route answer 404 to a session
|
|
1025
|
+
// that had just published the address.
|
|
1026
|
+
const dirPath = await mkdtemp(path.join(os.tmpdir(), "quality-variants-collapse-"));
|
|
1027
|
+
const manager = new HlsSessionManager({
|
|
1028
|
+
enabled: true,
|
|
1029
|
+
ffmpegBin: "ffmpeg",
|
|
1030
|
+
localBindHost: "127.0.0.1",
|
|
1031
|
+
localPort: 9090,
|
|
1032
|
+
// A megapixel a second: every rung below the source costs more than the
|
|
1033
|
+
// machine has.
|
|
1034
|
+
softwarePresetBenchmark: [{ preset: "veryfast", pixelsPerSec: 1_000_000 }],
|
|
1035
|
+
decodeCostModel: { pixelTerm: 0.01, bitrateTerm: 0, constantTerm: 0 }
|
|
1036
|
+
});
|
|
1037
|
+
const base = fakeSession({ id: BASE_ID, encodeHeight: 812, dirPath });
|
|
1038
|
+
// A thicker source than the fixture's, stated as the file's own bitrate
|
|
1039
|
+
// rather than as a cost object the file now derives.
|
|
1040
|
+
base.file.learn({ width: 1920, height: 1080, fps: 24, bitrateKbps: 10_000 });
|
|
1041
|
+
// What this file's own reader measured: a step must run at eight times
|
|
1042
|
+
// realtime to survive this swarm.
|
|
1043
|
+
base.supplyFigures = { requiredSpeed: 8 };
|
|
1044
|
+
manager.sessionsById.set(BASE_ID, base);
|
|
1045
|
+
t.after(async () => {
|
|
1046
|
+
await manager.disposeAll();
|
|
1047
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
1048
|
+
});
|
|
1049
|
+
|
|
1050
|
+
assert.deepEqual(
|
|
1051
|
+
manager.offeredHeights(base),
|
|
1052
|
+
[812],
|
|
1053
|
+
"the live judgement is unchanged: nothing but the running height is worth offering"
|
|
1054
|
+
);
|
|
1055
|
+
|
|
1056
|
+
const master = manager.buildMasterPlaylist(BASE_ID);
|
|
1057
|
+
|
|
1058
|
+
assert.ok(master, "the master is a published document, not a live figure");
|
|
1059
|
+
const heights = [...master.matchAll(/^v\/(\d+)\/index\.m3u8$/gm)].map((match) => Number(match[1]));
|
|
1060
|
+
assert.deepEqual(
|
|
1061
|
+
heights,
|
|
1062
|
+
[1080, 812, 720, 540, 480, 360, 240],
|
|
1063
|
+
"every rung that can be spliced onto this cut grid stays addressable"
|
|
1064
|
+
);
|
|
1065
|
+
});
|
|
1066
|
+
|
|
1067
|
+
test("two heights that clamp onto one picture share a single encoder", async (t) => {
|
|
1068
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
1069
|
+
const spawnedDirs = [];
|
|
1070
|
+
t.after(async () => {
|
|
1071
|
+
await manager.disposeAll();
|
|
1072
|
+
await Promise.all(spawnedDirs.map((dir) => rm(dir, { recursive: true, force: true })));
|
|
1073
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
1074
|
+
});
|
|
1075
|
+
|
|
1076
|
+
// A host that can hold exactly one rung. Whatever height is asked for, the
|
|
1077
|
+
// clamp inside createOrGetSession lands the encode on 240p — which is what a
|
|
1078
|
+
// CM4 did on 2026-08-28, turning a 360p and a 540p request into two more
|
|
1079
|
+
// ffmpeg processes making the same 426x240 picture as the 240p one.
|
|
1080
|
+
const madeIds = [VARIANT_ID, SECOND_VARIANT_ID];
|
|
1081
|
+
const created = [];
|
|
1082
|
+
manager.createOrGetSession = async (params) => {
|
|
1083
|
+
const id = madeIds[created.length];
|
|
1084
|
+
created.push(params);
|
|
1085
|
+
const variantDir = await mkdtemp(path.join(os.tmpdir(), "quality-variants-clamped-"));
|
|
1086
|
+
spawnedDirs.push(variantDir);
|
|
1087
|
+
const variant = fakeSession({ id, encodeHeight: 240, dirPath: variantDir });
|
|
1088
|
+
variant.consumers = new Set([params.consumerId]);
|
|
1089
|
+
manager.sessionsById.set(id, variant);
|
|
1090
|
+
return variant;
|
|
1091
|
+
};
|
|
1092
|
+
|
|
1093
|
+
const asked360 = await manager.resolveVariantSession(BASE_ID, 360);
|
|
1094
|
+
const asked540 = await manager.resolveVariantSession(BASE_ID, 540);
|
|
1095
|
+
|
|
1096
|
+
assert.equal(asked360.id, VARIANT_ID, "the first request made the encoder");
|
|
1097
|
+
assert.equal(
|
|
1098
|
+
asked540.id,
|
|
1099
|
+
VARIANT_ID,
|
|
1100
|
+
"and the second is served by it, because it produces the very same picture"
|
|
1101
|
+
);
|
|
1102
|
+
assert.equal(
|
|
1103
|
+
manager.sessionsById.has(SECOND_VARIANT_ID),
|
|
1104
|
+
false,
|
|
1105
|
+
"the duplicate was let go as soon as its size was known"
|
|
1106
|
+
);
|
|
1107
|
+
assert.equal(
|
|
1108
|
+
base.file.stepHeights.get(540),
|
|
1109
|
+
240,
|
|
1110
|
+
"540p is recorded as answered with the 240p the machine can hold"
|
|
1111
|
+
);
|
|
1112
|
+
|
|
1113
|
+
const askedAgain = await manager.resolveVariantSession(BASE_ID, 540);
|
|
1114
|
+
|
|
1115
|
+
assert.equal(askedAgain.id, VARIANT_ID);
|
|
1116
|
+
assert.equal(created.length, 2, "the third request created nothing at all");
|
|
1117
|
+
});
|
|
1118
|
+
|
|
1119
|
+
test("rungs that really do differ keep their own encoders", async (t) => {
|
|
1120
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
1121
|
+
const spawnedDirs = [];
|
|
1122
|
+
t.after(async () => {
|
|
1123
|
+
await manager.disposeAll();
|
|
1124
|
+
await Promise.all(spawnedDirs.map((dir) => rm(dir, { recursive: true, force: true })));
|
|
1125
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
1126
|
+
});
|
|
1127
|
+
|
|
1128
|
+
// A host with room: each request is encoded at the height it named, so
|
|
1129
|
+
// nothing may be merged. The sharing above must not become "one encoder for
|
|
1130
|
+
// every rung".
|
|
1131
|
+
const madeIds = [VARIANT_ID, SECOND_VARIANT_ID];
|
|
1132
|
+
const created = [];
|
|
1133
|
+
manager.createOrGetSession = async (params) => {
|
|
1134
|
+
const id = madeIds[created.length];
|
|
1135
|
+
created.push(params);
|
|
1136
|
+
const variantDir = await mkdtemp(path.join(os.tmpdir(), "quality-variants-distinct-"));
|
|
1137
|
+
spawnedDirs.push(variantDir);
|
|
1138
|
+
const variant = fakeSession({ id, encodeHeight: params.targetHeight, dirPath: variantDir });
|
|
1139
|
+
variant.consumers = new Set([params.consumerId]);
|
|
1140
|
+
manager.sessionsById.set(id, variant);
|
|
1141
|
+
return variant;
|
|
1142
|
+
};
|
|
1143
|
+
|
|
1144
|
+
const asked360 = await manager.resolveVariantSession(BASE_ID, 360);
|
|
1145
|
+
const asked540 = await manager.resolveVariantSession(BASE_ID, 540);
|
|
1146
|
+
|
|
1147
|
+
assert.equal(asked360.id, VARIANT_ID);
|
|
1148
|
+
assert.equal(asked540.id, SECOND_VARIANT_ID, "two different pictures, two encoders");
|
|
1149
|
+
assert.equal(base.file.stepHeights.get(360), 360);
|
|
1150
|
+
assert.equal(base.file.stepHeights.get(540), 540);
|
|
1151
|
+
});
|
|
1152
|
+
|
|
1153
|
+
test("a rung is never served from the COPY, whatever height the copy happens to be", async (t) => {
|
|
1154
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
1155
|
+
const spawnedDirs = [];
|
|
1156
|
+
t.after(async () => {
|
|
1157
|
+
await manager.disposeAll();
|
|
1158
|
+
await Promise.all(spawnedDirs.map((dir) => rm(dir, { recursive: true, force: true })));
|
|
1159
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
1160
|
+
});
|
|
1161
|
+
|
|
1162
|
+
// The base is a COPY at 240p — no encoder behind it, and it is the one rung
|
|
1163
|
+
// this host can always serve. Handing it to a request for a re-encoded 360p
|
|
1164
|
+
// would give away exactly that, and a viewer stranded on a rung the machine
|
|
1165
|
+
// cannot hold would have nowhere left to return to.
|
|
1166
|
+
base.transcodeVideo = false;
|
|
1167
|
+
base.encodeHeight = 240;
|
|
1168
|
+
base.variantHeight = 240;
|
|
1169
|
+
const created = [];
|
|
1170
|
+
manager.createOrGetSession = async (params) => {
|
|
1171
|
+
created.push(params);
|
|
1172
|
+
const variantDir = await mkdtemp(path.join(os.tmpdir(), "quality-variants-copy-"));
|
|
1173
|
+
spawnedDirs.push(variantDir);
|
|
1174
|
+
const variant = fakeSession({ id: VARIANT_ID, encodeHeight: 240, dirPath: variantDir });
|
|
1175
|
+
variant.consumers = new Set([params.consumerId]);
|
|
1176
|
+
manager.sessionsById.set(VARIANT_ID, variant);
|
|
1177
|
+
return variant;
|
|
1178
|
+
};
|
|
1179
|
+
|
|
1180
|
+
const asked = await manager.resolveVariantSession(BASE_ID, 360);
|
|
1181
|
+
|
|
1182
|
+
assert.equal(asked.id, VARIANT_ID, "the re-encoded rung is its own session, not the copy");
|
|
1183
|
+
});
|
|
1184
|
+
|
|
1185
|
+
test("a file opened at a position starts its sound THERE, not a look-ahead earlier", async (t) => {
|
|
1186
|
+
const { manager, base, dirPath } = await managerWithBase();
|
|
1187
|
+
t.after(async () => {
|
|
1188
|
+
await manager.disposeAll();
|
|
1189
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
1190
|
+
});
|
|
1191
|
+
base.audioSeparate = true;
|
|
1192
|
+
// The state at the instant a page is opened at a position: nothing seeked,
|
|
1193
|
+
// no segment served, no report from anybody. The read head is then not a
|
|
1194
|
+
// request edge — it is where the session was made — and a browser that has
|
|
1195
|
+
// just opened holds no buffer at all.
|
|
1196
|
+
base.furthestViewerSeconds = null;
|
|
1197
|
+
base.lastRequestedSegment = null;
|
|
1198
|
+
base.viewers.clear();
|
|
1199
|
+
base.progress.startPositionSeconds = 588;
|
|
1200
|
+
manager.getCachedAudioTracks = () => [
|
|
1201
|
+
{ index: 0, language: "rus", title: "", isDefault: true },
|
|
1202
|
+
{ index: 1, language: "eng", title: "", isDefault: false }
|
|
1203
|
+
];
|
|
1204
|
+
const created = [];
|
|
1205
|
+
manager.createOrGetSession = async (params) => {
|
|
1206
|
+
created.push(params);
|
|
1207
|
+
const rendition = fakeSession({ id: VARIANT_ID, encodeHeight: 0, dirPath });
|
|
1208
|
+
rendition.audioOnly = true;
|
|
1209
|
+
return { sessionId: VARIANT_ID, session: rendition };
|
|
1210
|
+
};
|
|
1211
|
+
|
|
1212
|
+
await manager.resolveAudioRenditionFile(BASE_ID, 1, "segment-00010.mp4");
|
|
1213
|
+
|
|
1214
|
+
// Field 2026-08-31: this answered 460 for a page opened at 588 — the whole
|
|
1215
|
+
// 120 s look-ahead subtracted from a buffer that did not exist — and the
|
|
1216
|
+
// segment the viewer needed took 38.8 s to appear against the picture's 8.4 s.
|
|
1217
|
+
assert.equal(
|
|
1218
|
+
created[0].startPositionSeconds,
|
|
1219
|
+
584,
|
|
1220
|
+
"where the viewer opened, less one segment of margin, and nothing else"
|
|
1221
|
+
);
|
|
1222
|
+
});
|