@torrent-tv/proxy 2.80.18 → 2.81.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 (67) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/docs/encode-architecture.md +51 -2
  3. package/package.json +1 -1
  4. package/research/double-spawn-2026-09-10.md +171 -0
  5. package/services/disk/DiskSpace.js +146 -0
  6. package/services/disk/wire.js +60 -0
  7. package/services/encode/EncodeRun.js +37 -9
  8. package/services/encode/SegmentStore.js +284 -232
  9. package/services/encode/run-command.js +16 -1
  10. package/services/hls-session-manager.js +31 -128
  11. package/services/orchestrators/EncodeOrchestrator.js +52 -38
  12. package/services/piece-store/allowance.js +107 -0
  13. package/services/piece-store/piece-disk-store.js +365 -0
  14. package/services/piece-store/shared-piece-store.js +1549 -1535
  15. package/services/segment-formats/fmp4.js +54 -0
  16. package/services/segment-formats/mpegts.js +54 -0
  17. package/services/torrent-worker/client.js +32 -0
  18. package/services/torrent-worker/pool-adapter.js +15 -0
  19. package/services/torrent-worker/protocol.js +9 -0
  20. package/services/torrent-worker/worker.js +8 -1
  21. package/services/viewer/positions.js +48 -0
  22. package/test/audio-inventory.test.js +176 -176
  23. package/test/auto-quality-step.test.js +514 -514
  24. package/test/concurrent-cost.test.js +138 -138
  25. package/test/coverage-follows-the-disk.test.js +191 -187
  26. package/test/coverage-map.test.js +195 -195
  27. package/test/declared-tracks.test.js +35 -35
  28. package/test/disk-space.test.js +138 -0
  29. package/test/encode-orchestrator.test.js +0 -3
  30. package/test/encode-run.test.js +5 -12
  31. package/test/held-request-width.test.js +155 -155
  32. package/test/helpers/encode-run.js +2 -2
  33. package/test/matroska-blocks.test.js +0 -0
  34. package/test/matroska-cues-track.test.js +192 -192
  35. package/test/mp4-composition-times.test.js +0 -0
  36. package/test/mp4-subtitles.test.js +173 -173
  37. package/test/one-authority.test.js +281 -220
  38. package/test/orchestrator-wired.test.js +199 -195
  39. package/test/packet-witness-ring.test.js +236 -236
  40. package/test/packet-witness.test.js +148 -148
  41. package/test/piece-disk-store.test.js +267 -0
  42. package/test/piece-reader.test.js +4 -4
  43. package/test/piece-store-eviction.test.js +17 -17
  44. package/test/piece-store-reservations.test.js +20 -1
  45. package/test/piece-store-slow-disk.test.js +16 -1
  46. package/test/produced-copy-choice.test.js +258 -358
  47. package/test/read-window.test.js +6 -6
  48. package/test/run-intervals.test.js +100 -100
  49. package/test/seek-landing.test.js +109 -109
  50. package/test/segment-serve-wiring.test.js +8 -9
  51. package/test/segment-store-eviction.test.js +232 -0
  52. package/test/segment-store.test.js +238 -216
  53. package/test/segments-are-shared.test.js +1 -1
  54. package/test/shared-piece-store.test.js +12 -12
  55. package/test/sidecar-naming.test.js +142 -142
  56. package/test/subtitle-cue-framing.test.js +200 -200
  57. package/test/subtitle-cue-walk.test.js +369 -369
  58. package/test/subtitle-defaults.test.js +97 -97
  59. package/test/subtitle-track-numbering.test.js +370 -370
  60. package/test/tail-duplication.test.js +167 -167
  61. package/test/tracks-begin-together.test.js +195 -195
  62. package/test/two-viewers-one-picture.test.js +374 -374
  63. package/test/video-facts.test.js +102 -102
  64. package/test/wedge-certainty.test.js +131 -131
  65. package/services/encode/open-piece.js +0 -135
  66. package/services/piece-store/disk-tier.js +0 -151
  67. package/test/open-piece.test.js +0 -152
@@ -1,514 +1,514 @@
1
- /**
2
- * @file The automatic quality step: what the proxy does when this machine, or
3
- * the viewer's link, cannot carry the picture it is producing.
4
- *
5
- * The rule these tests exist to pin is one sentence long: THE SIZE OF THE
6
- * PICTURE IS NEVER REWRITTEN UNDERNEATH A RUNNING SESSION. The fMP4 init
7
- * segment is fetched once, by `#EXT-X-MAP`, and `avc1` keeps SPS and PPS in it
8
- * rather than in the fragments — so a run that changes the size produces
9
- * fragments the decoder cannot read, silently, with no layer reporting an
10
- * error. Measured 2026-08-21 on two files: one browser reported
11
- * `size=1280x720` for three and a half minutes over macroblock garbage, the
12
- * other errored on the first mismatched fragment and sat at `size=0x0`.
13
- *
14
- * A change of resolution is a change of VARIANT. So the proxy ASKS, the request
15
- * travels in every progress report, and the browser — where the viewer's own
16
- * choice lives — decides whether to follow it.
17
- */
18
-
19
- import test from "node:test";
20
- import { fakeProcess as fakeEncoder, startRunOn } from "./helpers/encode-run.js";
21
- import assert from "node:assert/strict";
22
- import { SourceFile } from "../services/source/SourceFile.js";
23
- import { Timeline } from "../services/output/Timeline.js";
24
- import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
25
- import os from "node:os";
26
- import path from "node:path";
27
- import { HlsSessionManager } from "../services/hls-session-manager.js";
28
- import { Output } from "../services/output/Output.js";
29
- import { viewerOf } from "../services/viewer/Viewer.js";
30
- import { fmp4Format } from "../services/segment-formats/fmp4.js";
31
- import { softwareDescriptor, maxrateKbpsFor, nominalKbpsForHeight } from "../services/hwaccel.js";
32
- import { readVideoSampleSize } from "../services/segment-formats/mp4-boxes.js";
33
-
34
- const BASE_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
35
- const SEGMENT_SECONDS = 4;
36
-
37
-
38
- /**
39
- * A session shaped like a live one, encoding 720p of a 1080p source.
40
- *
41
- * @param {{ dirPath: string, transcodeVideo?: boolean, cutGrid?: string }} params
42
- * `cutGrid` goes into the file's cut table; left out, it is what production
43
- * builds for this branch.
44
- * @returns {object}
45
- */
46
- function fakeSession({ dirPath, transcodeVideo = true, cutGrid = transcodeVideo ? "uniform" : "keyframe" }) {
47
- return {
48
- id: BASE_ID,
49
- dirPath,
50
- // Where this file is cut, held by the file. A fixture that stated it
51
- // on the session was describing what production no longer does.
52
- //
53
- // The grid travels in here and nowhere else: a copy can only be cut where
54
- // the source already has a keyframe, so this is what decides whether the
55
- // stream publishes variants at all, and `#publishesVariants` reads it off
56
- // the table. The default is what production builds — a keyframe grid only
57
- // where one was read, which is the copy.
58
- timeline: new Timeline({
59
- boundaries: Array.from({ length: 101 }, (_, index) => index * SEGMENT_SECONDS),
60
- cutGrid
61
- }),
62
- state: "ready",
63
- file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "video.mkv" }).learn({ width: 1920, height: 1080, durationSeconds: 400 }),
64
- // An ordinary session reads its own file, and its sound is inside it. The
65
- // three differ only for a soundtrack shipped as a file of its own.
66
- get inputFile() { return this.file; },
67
- get audioFile() { return this.file; },
68
- startedAt: Date.now(),
69
- lastAccessedAt: Date.now(),
70
- runs: new Set(),
71
- runState: "running",
72
- runSerial: 1,
73
- lastError: "",
74
- consumers: new Set(),
75
- segmentFormat: fmp4Format,
76
- transcodeVideo,
77
- transcodeAudio: true,
78
- audioOnly: false,
79
- audioTrackIndex: 0,
80
- // The shape this output is encoded AS, decided once for the output.
81
- output: new Output({
82
- encodeWidth: transcodeVideo ? 1280 : 0,
83
- encodeHeight: transcodeVideo ? 720 : 0,
84
- outputFps: 24,
85
- softwarePreset: null,
86
- applyTonemap: false
87
- }),
88
- encodeRunGeneration: 0,
89
- budgetSlowSince: 0,
90
- budgetUpSince: 0,
91
- budgetLastActionAt: 0,
92
- qualityAsk: null,
93
- initSizeSaid: "",
94
- recentSpeed: null,
95
- rateCapKbps: null,
96
- viewers: new Map(),
97
- linkSlowSince: 0,
98
- lastAloneSpeed: null,
99
- usesExplicitCuts: false,
100
- useSyntheticPlaylist: true,
101
- playlistText: "#EXTM3U\n",
102
- progress: { state: "running", processedSeconds: 40, startPositionSeconds: 0, speed: "1.0x" }
103
- };
104
- }
105
-
106
- /**
107
- * @param {{ transcodeVideo?: boolean, cutGrid?: string }} [options]
108
- * @returns {Promise<{ manager: HlsSessionManager, session: object, dirPath: string, restarts: number[] }>}
109
- */
110
- async function managerWithSession({ transcodeVideo = true, cutGrid } = {}) {
111
- const dirPath = await mkdtemp(path.join(os.tmpdir(), "auto-quality-"));
112
- const manager = new HlsSessionManager({
113
- enabled: true,
114
- ffmpegBin: "ffmpeg",
115
- localBindHost: "127.0.0.1",
116
- localPort: 9090
117
- });
118
- // A software host: the budget's own precondition.
119
- manager.videoEncoder = { kind: "software", name: "libx264", inputArgs: [] };
120
- // A fully-downloaded file, so nothing here is ever read as download-bound —
121
- // the distinction is tested elsewhere and would only obscure these.
122
- manager.getSourceStats = async () => ({
123
- downloadSpeed: 10e6,
124
- fileProgress: 1,
125
- fileLength: 4e9
126
- });
127
- const session = fakeSession({ dirPath, transcodeVideo, cutGrid });
128
- manager.sessionsById.set(BASE_ID, session);
129
- startRunOn(session, { process: fakeEncoder() });
130
- return { manager, session, dirPath };
131
- }
132
-
133
- /**
134
- * Produced segments of a known size, so the observed stream bitrate the link
135
- * check compares against is a real reading of real files.
136
- *
137
- * @param {object} session
138
- * @param {number} bytesEach
139
- * @returns {Promise<void>}
140
- */
141
- async function produceSegments(session, bytesEach) {
142
- // Where the run in force writes.
143
- const runDir = session.dirPath;
144
- await mkdir(runDir, { recursive: true });
145
- for (let index = 0; index < 4; index += 1) {
146
- await writeFile(
147
- path.join(runDir, session.segmentFormat.segmentFileName(index)),
148
- Buffer.alloc(bytesEach)
149
- );
150
- }
151
- }
152
-
153
- test("a picture that cannot be kept up with is asked for as another VARIANT, and its size is left alone", async (t) => {
154
- const { manager, session, dirPath } = await managerWithSession();
155
- t.after(async () => {
156
- await manager.disposeAll();
157
- await rm(dirPath, { recursive: true, force: true });
158
- });
159
-
160
- const sizeBefore = `${session.encodeWidth}x${session.encodeHeight}`;
161
- // Sustained sub-realtime, read as a slope: the run has been slow since well
162
- // before the window, and the reading is from this very run.
163
- session.budgetSlowSince = Date.now() - 60_000;
164
- session.recentSpeed = { speed: 0.7, at: Date.now(), run: [...session.runs][0] };
165
-
166
- await manager.runQualityBudgetOnce();
167
-
168
- assert.equal(
169
- `${session.encodeWidth}x${session.encodeHeight}`,
170
- sizeBefore,
171
- "the size the init segment describes must survive the step — that is the whole fault"
172
- );
173
- assert.ok(session.qualityAsk, "the step is a request to the player to move variant");
174
- assert.ok(
175
- session.qualityAsk.height < 720,
176
- `a step DOWN, and 720p was on screen (asked for ${session.qualityAsk?.height}p)`
177
- );
178
- });
179
-
180
- test("the request reaches the browser in the progress report, and stops once the viewer is there", async (t) => {
181
- const { manager, session, dirPath } = await managerWithSession();
182
- t.after(async () => {
183
- await manager.disposeAll();
184
- await rm(dirPath, { recursive: true, force: true });
185
- });
186
-
187
- session.qualityAsk = { height: 480, at: Date.now(), reason: "measured" };
188
- const asked = await manager.getSessionProgress(BASE_ID);
189
- assert.equal(asked.requestedHeight, 480, "the request travels with every progress report");
190
-
191
- // The player moved: the variant it is now watching IS the height asked for.
192
- session.variantHeight = 480;
193
- const answered = await manager.getSessionProgress(BASE_ID);
194
- assert.equal(answered.requestedHeight, 0, "a request the viewer has answered is not repeated");
195
- assert.equal(session.qualityAsk, null, "and it is let go of, not merely hidden");
196
- });
197
-
198
- test("a request the player never follows runs out instead of being repeated for the whole film", async (t) => {
199
- const { manager, session, dirPath } = await managerWithSession();
200
- t.after(async () => {
201
- await manager.disposeAll();
202
- await rm(dirPath, { recursive: true, force: true });
203
- });
204
-
205
- // A viewer on a manual pick ignores every request by design, and so does a
206
- // stream with no variants. Neither is an error; both look the same from here.
207
- session.qualityAsk = { height: 480, at: Date.now() - 120_000, reason: "measured" };
208
-
209
- const progress = await manager.getSessionProgress(BASE_ID);
210
-
211
- assert.equal(progress.requestedHeight, 0);
212
- assert.equal(session.qualityAsk, null, "said once and let go");
213
- });
214
-
215
- test("a COPIED picture is never asked to slow its encoder, because it has none", async (t) => {
216
- const { manager, session, dirPath } = await managerWithSession({ transcodeVideo: false });
217
- t.after(async () => {
218
- await manager.disposeAll();
219
- await rm(dirPath, { recursive: true, force: true });
220
- });
221
-
222
- // Whatever this reading says, a copy has no encoder to make cheaper: moving
223
- // the viewer to a RE-ENCODED rung costs the machine more, not less.
224
- session.budgetSlowSince = Date.now() - 60_000;
225
- session.recentSpeed = { speed: 0.4, at: Date.now(), run: [...session.runs][0] };
226
-
227
- await manager.runQualityBudgetOnce();
228
-
229
- assert.equal(session.qualityAsk, null, "the copy path's lever is the viewer's link, not the CPU");
230
- });
231
-
232
- test("a measured link becomes the encoder's own bitrate ceiling, and nothing else moves", () => {
233
- // The one lever that reduces what is sent without touching the picture:
234
- // -maxrate/-bufsize and CRF do not appear in the SPS, so the init segment
235
- // already in the player's hands goes on describing every fragment.
236
- const uncapped = softwareDescriptor().buildVideoArgs({
237
- targetWidth: 1280,
238
- targetHeight: 720,
239
- segmentDurationSec: 4,
240
- fps: 24
241
- });
242
- const capped = softwareDescriptor().buildVideoArgs({
243
- targetWidth: 1280,
244
- targetHeight: 720,
245
- segmentDurationSec: 4,
246
- fps: 24,
247
- nominalKbps: 1200
248
- });
249
-
250
- assert.equal(
251
- uncapped[uncapped.indexOf("-maxrate") + 1],
252
- `${maxrateKbpsFor(nominalKbpsForHeight(720))}k`,
253
- "with nothing measured the rung's own nominal rate stands"
254
- );
255
- assert.equal(capped[capped.indexOf("-maxrate") + 1], `${maxrateKbpsFor(1200)}k`);
256
- // Everything that decides the SIZE must be identical in the two.
257
- assert.deepEqual(
258
- uncapped.slice(0, uncapped.indexOf("-maxrate")),
259
- capped.slice(0, capped.indexOf("-maxrate")),
260
- "the scale filter, the codec and the preset are untouched by a rate cap"
261
- );
262
- });
263
-
264
- test("the size an init segment describes is read from the init, not assumed", () => {
265
- // A minimal moov/trak/mdia/minf/stbl/stsd with one avc1 entry. Built here
266
- // rather than taken from a fixture so the offsets under test are the ones
267
- // ISO/IEC 14496-12 states, and a fixture cannot quietly encode a mistake.
268
- const avc1 = Buffer.alloc(8 + 8 + 16 + 4);
269
- avc1.writeUInt32BE(avc1.length, 0);
270
- avc1.write("avc1", 4, "latin1");
271
- avc1.writeUInt16BE(960, 32);
272
- avc1.writeUInt16BE(540, 34);
273
-
274
- const stsd = Buffer.concat([Buffer.alloc(8 + 8), avc1]);
275
- stsd.writeUInt32BE(stsd.length, 0);
276
- stsd.write("stsd", 4, "latin1");
277
- stsd.writeUInt32BE(1, 12); // entry_count
278
-
279
- const wrap = (type, payload) => {
280
- const box = Buffer.alloc(8 + payload.length);
281
- box.writeUInt32BE(box.length, 0);
282
- box.write(type, 4, "latin1");
283
- payload.copy(box, 8);
284
- return box;
285
- };
286
- const init = wrap("moov", wrap("trak", wrap("mdia", wrap("minf", wrap("stbl", stsd)))));
287
-
288
- assert.deepEqual(readVideoSampleSize(init), { width: 960, height: 540 });
289
- assert.equal(readVideoSampleSize(Buffer.alloc(0)), null);
290
- });
291
-
292
- test("a COPIED picture too thick for the viewer's link is asked for as a smaller VARIANT", async (t) => {
293
- const { manager, session, dirPath } = await managerWithSession({ transcodeVideo: false });
294
- t.after(async () => {
295
- await manager.disposeAll();
296
- await rm(dirPath, { recursive: true, force: true });
297
- });
298
-
299
- // Four seconds of segment at 2 MB is ~4 Mbit/s of stream. The viewer reports
300
- // a link that cannot carry it and a buffer that is running dry.
301
- await produceSegments(session, 2_000_000);
302
- viewerOf(session, "viewer").netReport = { linkMbps: 1.0, bufferedAheadSec: 1.5, positionSeconds: null, at: Date.now() };
303
- session.linkSlowSince = Date.now() - 60_000;
304
-
305
- await manager.runQualityBudgetOnce();
306
-
307
- assert.ok(
308
- session.qualityAsk,
309
- "a copy has no encoder to bound, so the only way to send fewer bits is another rendering of the film"
310
- );
311
- assert.ok(session.qualityAsk.height < 1080, `a step down (asked for ${session.qualityAsk?.height}p)`);
312
- });
313
-
314
- test("with two viewers the budget acts on the WORST link, not on whoever reported last", async (t) => {
315
- const { manager, session, dirPath } = await managerWithSession({ transcodeVideo: false });
316
- t.after(async () => {
317
- await manager.disposeAll();
318
- await rm(dirPath, { recursive: true, force: true });
319
- });
320
-
321
- await produceSegments(session, 2_000_000);
322
- // One viewer is comfortable and reported LAST, which under a single field was
323
- // the whole of what the budget saw. The other cannot carry the stream and is
324
- // running dry.
325
- viewerOf(session, "thin").netReport = {
326
- linkMbps: 1.0,
327
- bufferedAheadSec: 1.5,
328
- positionSeconds: 40,
329
- at: Date.now() - 1_000
330
- };
331
- viewerOf(session, "fat").netReport = {
332
- linkMbps: 80,
333
- bufferedAheadSec: 60,
334
- positionSeconds: 40,
335
- at: Date.now()
336
- };
337
- session.linkSlowSince = Date.now() - 60_000;
338
-
339
- await manager.runQualityBudgetOnce();
340
-
341
- assert.ok(
342
- session.qualityAsk,
343
- "the viewer who cannot keep up decides, whichever of them reported most recently"
344
- );
345
- });
346
-
347
- test("a report from a viewer who has left stops counting", async (t) => {
348
- const { manager, session, dirPath } = await managerWithSession({ transcodeVideo: false });
349
- t.after(async () => {
350
- await manager.disposeAll();
351
- await rm(dirPath, { recursive: true, force: true });
352
- });
353
-
354
- await produceSegments(session, 2_000_000);
355
- // A reading describes a link at a moment, and one this old cannot decide for
356
- // the viewers still here. ONLY THE READING EXPIRES: whether the person is
357
- // still watching is a different question with its own answer — their
358
- // connection — and answering both from this one place is what stopped a
359
- // soundtrack's encoder on 2026-09-05, so silence no longer removes anybody.
360
- viewerOf(session, "gone").netReport = {
361
- linkMbps: 1.0,
362
- bufferedAheadSec: 1.5,
363
- positionSeconds: 40,
364
- at: Date.now() - 120_000
365
- };
366
- manager.recordNetReport(session.id, {
367
- linkMbps: 80,
368
- bufferedAheadSec: 60,
369
- consumerId: "here",
370
- positionSeconds: 40
371
- });
372
- session.linkSlowSince = Date.now() - 60_000;
373
-
374
- await manager.runQualityBudgetOnce();
375
-
376
- assert.equal(session.viewers.size, 2, "the viewer is still known — silence is not leaving");
377
- assert.equal(
378
- viewerOf(session, "gone").netReport,
379
- null,
380
- "but their reading has expired, so it decides nothing"
381
- );
382
- assert.equal(session.qualityAsk, null, "the viewer who is here can carry the picture");
383
- });
384
-
385
- test("the way BACK UP exists, and a bitrate cap is lifted before the picture is enlarged", async (t) => {
386
- const { manager, session, dirPath } = await managerWithSession();
387
- t.after(async () => {
388
- await manager.disposeAll();
389
- await rm(dirPath, { recursive: true, force: true });
390
- });
391
-
392
- // The viewer is on 480p, the machine has been ahead of realtime for longer
393
- // than the up window, and nothing is capping the bitrate.
394
- session.variantHeight = 480;
395
- session.encodeWidth = 854;
396
- session.encodeHeight = 480;
397
- session.recentSpeed = { speed: 2.4, at: Date.now(), run: [...session.runs][0] };
398
- session.budgetUpSince = Date.now() - 120_000;
399
-
400
- await manager.runQualityBudgetOnce();
401
-
402
- assert.ok(session.qualityAsk, "for most of this project's life there was no step up at all");
403
- assert.equal(
404
- session.qualityAsk.height,
405
- 540,
406
- "one rung at a time: the lowest height above the one on screen, never above the source"
407
- );
408
- });
409
-
410
- test("a capped picture gets its own bitrate back before it is asked to grow", async (t) => {
411
- const { manager, session, dirPath } = await managerWithSession();
412
- t.after(async () => {
413
- await manager.disposeAll();
414
- await rm(dirPath, { recursive: true, force: true });
415
- });
416
-
417
- session.variantHeight = 480;
418
- session.encodeWidth = 854;
419
- session.encodeHeight = 480;
420
- session.rateCapKbps = 700;
421
- session.recentSpeed = { speed: 2.4, at: Date.now(), run: [...session.runs][0] };
422
- session.budgetUpSince = Date.now() - 120_000;
423
- // A restart is what lifting the cap costs, and spawning ffmpeg is not this
424
- // test's business. The run the fixture gave the session is the one the
425
- // reading above came from, and replacing it here would make that reading
426
- // belong to a run that is gone — which is exactly what the comparison is for.
427
-
428
- await manager.runQualityBudgetOnce().catch(() => undefined);
429
-
430
- assert.equal(session.rateCapKbps, null, "the cap goes first: it is cheaper than enlarging the picture");
431
- assert.equal(
432
- session.qualityAsk,
433
- null,
434
- "and the height is left for a second unbroken window, so the two do not move at once"
435
- );
436
- });
437
-
438
- test("a stream that publishes no variants is left alone, and said so once", async (t) => {
439
- const { manager, session, dirPath } = await managerWithSession({
440
- transcodeVideo: false,
441
- // A copy whose keyframe index could not be read falls back to an even grid
442
- // ffmpeg does not cut on. Nothing can be aligned to that, so there is no
443
- // master and no variant to move to.
444
- cutGrid: "even"
445
- });
446
- t.after(async () => {
447
- await manager.disposeAll();
448
- await rm(dirPath, { recursive: true, force: true });
449
- });
450
-
451
- await produceSegments(session, 2_000_000);
452
- viewerOf(session, "viewer").netReport = { linkMbps: 1.0, bufferedAheadSec: 1.5, positionSeconds: null, at: Date.now() };
453
- session.linkSlowSince = Date.now() - 60_000;
454
-
455
- await manager.runQualityBudgetOnce();
456
-
457
- assert.equal(session.qualityAsk, null, "asking a player with no variants to change variant is nothing");
458
- assert.equal(session.saidNoVariants, true, "and the reason is stated once, not once per window");
459
- });
460
-
461
- test("a height this machine has been MEASURED failing at is not what the way back up offers", async (t) => {
462
- const { manager, session, dirPath } = await managerWithSession();
463
- t.after(async () => {
464
- await manager.disposeAll();
465
- await rm(dirPath, { recursive: true, force: true });
466
- });
467
-
468
- // The base ran 720p at half realtime and the viewer was stepped down to 480p.
469
- // The base's own height used to be exempt from every refusal — it was the
470
- // rung on screen, back when a step changed the encode inside it — so the way
471
- // back up would have asked for 720p again, failed again, and stepped down
472
- // again, about every hundred seconds for the length of the film.
473
- manager.softwarePresetBenchmark = [{ preset: "ultrafast", pixelsPerSec: 1e6 }];
474
- session.lastAloneSpeed = 0.5;
475
- session.variantHeight = 720;
476
-
477
- const offered = manager.offeredHeights(session);
478
-
479
- assert.ok(!offered.includes(720) || manager.liveOutputs.variantHeightOf(session) === 720);
480
- // Now on the 480p variant: 720p has a reading of its own and must be gone.
481
- session.variantHeight = 480;
482
- session.encodeHeight = 480;
483
- session.encodeWidth = 854;
484
- assert.ok(
485
- !manager.offeredHeights(session).includes(720),
486
- "a rung measured below realtime is withdrawn once the viewer has left it"
487
- );
488
- });
489
-
490
- test("a cap is not lifted because there is no higher rung to compare against", async (t) => {
491
- const { manager, session, dirPath } = await managerWithSession();
492
- t.after(async () => {
493
- await manager.disposeAll();
494
- await rm(dirPath, { recursive: true, force: true });
495
- });
496
-
497
- // At the top offered height, so there is no NEXT rung — and the question of
498
- // whether to lift the cap is about THIS one. Deciding it on "nothing to step
499
- // to, so yes" took the cap off a link measured at a fifth of what the picture
500
- // needs, after which #checkLinkBudget put it straight back: two ffmpeg
501
- // restarts a minute and a half, on exactly the thin cellular viewer the cap
502
- // exists for.
503
- session.variantHeight = 1080;
504
- session.encodeWidth = 1920;
505
- session.encodeHeight = 1080;
506
- session.rateCapKbps = 700;
507
- session.recentSpeed = { speed: 2.4, at: Date.now(), run: [...session.runs][0] };
508
- session.budgetUpSince = Date.now() - 120_000;
509
- viewerOf(session, "viewer").netReport = { linkMbps: 1.0, bufferedAheadSec: 30, positionSeconds: null, at: Date.now() };
510
-
511
- await manager.runQualityBudgetOnce();
512
-
513
- assert.equal(session.rateCapKbps, 700, "the link still cannot carry this picture uncapped");
514
- });
1
+ /**
2
+ * @file The automatic quality step: what the proxy does when this machine, or
3
+ * the viewer's link, cannot carry the picture it is producing.
4
+ *
5
+ * The rule these tests exist to pin is one sentence long: THE SIZE OF THE
6
+ * PICTURE IS NEVER REWRITTEN UNDERNEATH A RUNNING SESSION. The fMP4 init
7
+ * segment is fetched once, by `#EXT-X-MAP`, and `avc1` keeps SPS and PPS in it
8
+ * rather than in the fragments — so a run that changes the size produces
9
+ * fragments the decoder cannot read, silently, with no layer reporting an
10
+ * error. Measured 2026-08-21 on two files: one browser reported
11
+ * `size=1280x720` for three and a half minutes over macroblock garbage, the
12
+ * other errored on the first mismatched fragment and sat at `size=0x0`.
13
+ *
14
+ * A change of resolution is a change of VARIANT. So the proxy ASKS, the request
15
+ * travels in every progress report, and the browser — where the viewer's own
16
+ * choice lives — decides whether to follow it.
17
+ */
18
+
19
+ import test from "node:test";
20
+ import { fakeProcess as fakeEncoder, startRunOn } from "./helpers/encode-run.js";
21
+ import assert from "node:assert/strict";
22
+ import { SourceFile } from "../services/source/SourceFile.js";
23
+ import { Timeline } from "../services/output/Timeline.js";
24
+ import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
25
+ import os from "node:os";
26
+ import path from "node:path";
27
+ import { HlsSessionManager } from "../services/hls-session-manager.js";
28
+ import { Output } from "../services/output/Output.js";
29
+ import { viewerOf } from "../services/viewer/Viewer.js";
30
+ import { fmp4Format } from "../services/segment-formats/fmp4.js";
31
+ import { softwareDescriptor, maxrateKbpsFor, nominalKbpsForHeight } from "../services/hwaccel.js";
32
+ import { readVideoSampleSize } from "../services/segment-formats/mp4-boxes.js";
33
+
34
+ const BASE_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
35
+ const SEGMENT_SECONDS = 4;
36
+
37
+
38
+ /**
39
+ * A session shaped like a live one, encoding 720p of a 1080p source.
40
+ *
41
+ * @param {{ dirPath: string, transcodeVideo?: boolean, cutGrid?: string }} params
42
+ * `cutGrid` goes into the file's cut table; left out, it is what production
43
+ * builds for this branch.
44
+ * @returns {object}
45
+ */
46
+ function fakeSession({ dirPath, transcodeVideo = true, cutGrid = transcodeVideo ? "uniform" : "keyframe" }) {
47
+ return {
48
+ id: BASE_ID,
49
+ dirPath,
50
+ // Where this file is cut, held by the file. A fixture that stated it
51
+ // on the session was describing what production no longer does.
52
+ //
53
+ // The grid travels in here and nowhere else: a copy can only be cut where
54
+ // the source already has a keyframe, so this is what decides whether the
55
+ // stream publishes variants at all, and `#publishesVariants` reads it off
56
+ // the table. The default is what production builds — a keyframe grid only
57
+ // where one was read, which is the copy.
58
+ timeline: new Timeline({
59
+ boundaries: Array.from({ length: 101 }, (_, index) => index * SEGMENT_SECONDS),
60
+ cutGrid
61
+ }),
62
+ state: "ready",
63
+ file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "video.mkv" }).learn({ width: 1920, height: 1080, durationSeconds: 400 }),
64
+ // An ordinary session reads its own file, and its sound is inside it. The
65
+ // three differ only for a soundtrack shipped as a file of its own.
66
+ get inputFile() { return this.file; },
67
+ get audioFile() { return this.file; },
68
+ startedAt: Date.now(),
69
+ lastAccessedAt: Date.now(),
70
+ runs: new Set(),
71
+ runState: "running",
72
+ runSerial: 1,
73
+ lastError: "",
74
+ consumers: new Set(),
75
+ segmentFormat: fmp4Format,
76
+ transcodeVideo,
77
+ transcodeAudio: true,
78
+ audioOnly: false,
79
+ audioTrackIndex: 0,
80
+ // The shape this output is encoded AS, decided once for the output.
81
+ output: new Output({
82
+ encodeWidth: transcodeVideo ? 1280 : 0,
83
+ encodeHeight: transcodeVideo ? 720 : 0,
84
+ outputFps: 24,
85
+ softwarePreset: null,
86
+ applyTonemap: false
87
+ }),
88
+ encodeRunGeneration: 0,
89
+ budgetSlowSince: 0,
90
+ budgetUpSince: 0,
91
+ budgetLastActionAt: 0,
92
+ qualityAsk: null,
93
+ initSizeSaid: "",
94
+ recentSpeed: null,
95
+ rateCapKbps: null,
96
+ viewers: new Map(),
97
+ linkSlowSince: 0,
98
+ lastAloneSpeed: null,
99
+ usesExplicitCuts: false,
100
+ useSyntheticPlaylist: true,
101
+ playlistText: "#EXTM3U\n",
102
+ progress: { state: "running", processedSeconds: 40, startPositionSeconds: 0, speed: "1.0x" }
103
+ };
104
+ }
105
+
106
+ /**
107
+ * @param {{ transcodeVideo?: boolean, cutGrid?: string }} [options]
108
+ * @returns {Promise<{ manager: HlsSessionManager, session: object, dirPath: string, restarts: number[] }>}
109
+ */
110
+ async function managerWithSession({ transcodeVideo = true, cutGrid } = {}) {
111
+ const dirPath = await mkdtemp(path.join(os.tmpdir(), "auto-quality-"));
112
+ const manager = new HlsSessionManager({
113
+ enabled: true,
114
+ ffmpegBin: "ffmpeg",
115
+ localBindHost: "127.0.0.1",
116
+ localPort: 9090
117
+ });
118
+ // A software host: the budget's own precondition.
119
+ manager.videoEncoder = { kind: "software", name: "libx264", inputArgs: [] };
120
+ // A fully-downloaded file, so nothing here is ever read as download-bound —
121
+ // the distinction is tested elsewhere and would only obscure these.
122
+ manager.getSourceStats = async () => ({
123
+ downloadSpeed: 10e6,
124
+ fileProgress: 1,
125
+ fileLength: 4e9
126
+ });
127
+ const session = fakeSession({ dirPath, transcodeVideo, cutGrid });
128
+ manager.sessionsById.set(BASE_ID, session);
129
+ startRunOn(session, { process: fakeEncoder() });
130
+ return { manager, session, dirPath };
131
+ }
132
+
133
+ /**
134
+ * Produced segments of a known size, so the observed stream bitrate the link
135
+ * check compares against is a real reading of real files.
136
+ *
137
+ * @param {object} session
138
+ * @param {number} bytesEach
139
+ * @returns {Promise<void>}
140
+ */
141
+ async function produceSegments(session, bytesEach) {
142
+ // Where the run in force writes.
143
+ const runDir = session.dirPath;
144
+ await mkdir(runDir, { recursive: true });
145
+ for (let index = 0; index < 4; index += 1) {
146
+ await writeFile(
147
+ path.join(runDir, session.segmentFormat.segmentFileName(index)),
148
+ Buffer.alloc(bytesEach)
149
+ );
150
+ }
151
+ }
152
+
153
+ test("a picture that cannot be kept up with is asked for as another VARIANT, and its size is left alone", async (t) => {
154
+ const { manager, session, dirPath } = await managerWithSession();
155
+ t.after(async () => {
156
+ await manager.disposeAll();
157
+ await rm(dirPath, { recursive: true, force: true });
158
+ });
159
+
160
+ const sizeBefore = `${session.encodeWidth}x${session.encodeHeight}`;
161
+ // Sustained sub-realtime, read as a slope: the run has been slow since well
162
+ // before the window, and the reading is from this very run.
163
+ session.budgetSlowSince = Date.now() - 60_000;
164
+ session.recentSpeed = { speed: 0.7, at: Date.now(), run: [...session.runs][0] };
165
+
166
+ await manager.runQualityBudgetOnce();
167
+
168
+ assert.equal(
169
+ `${session.encodeWidth}x${session.encodeHeight}`,
170
+ sizeBefore,
171
+ "the size the init segment describes must survive the step — that is the whole fault"
172
+ );
173
+ assert.ok(session.qualityAsk, "the step is a request to the player to move variant");
174
+ assert.ok(
175
+ session.qualityAsk.height < 720,
176
+ `a step DOWN, and 720p was on screen (asked for ${session.qualityAsk?.height}p)`
177
+ );
178
+ });
179
+
180
+ test("the request reaches the browser in the progress report, and stops once the viewer is there", async (t) => {
181
+ const { manager, session, dirPath } = await managerWithSession();
182
+ t.after(async () => {
183
+ await manager.disposeAll();
184
+ await rm(dirPath, { recursive: true, force: true });
185
+ });
186
+
187
+ session.qualityAsk = { height: 480, at: Date.now(), reason: "measured" };
188
+ const asked = await manager.getSessionProgress(BASE_ID);
189
+ assert.equal(asked.requestedHeight, 480, "the request travels with every progress report");
190
+
191
+ // The player moved: the variant it is now watching IS the height asked for.
192
+ session.variantHeight = 480;
193
+ const answered = await manager.getSessionProgress(BASE_ID);
194
+ assert.equal(answered.requestedHeight, 0, "a request the viewer has answered is not repeated");
195
+ assert.equal(session.qualityAsk, null, "and it is let go of, not merely hidden");
196
+ });
197
+
198
+ test("a request the player never follows runs out instead of being repeated for the whole film", async (t) => {
199
+ const { manager, session, dirPath } = await managerWithSession();
200
+ t.after(async () => {
201
+ await manager.disposeAll();
202
+ await rm(dirPath, { recursive: true, force: true });
203
+ });
204
+
205
+ // A viewer on a manual pick ignores every request by design, and so does a
206
+ // stream with no variants. Neither is an error; both look the same from here.
207
+ session.qualityAsk = { height: 480, at: Date.now() - 120_000, reason: "measured" };
208
+
209
+ const progress = await manager.getSessionProgress(BASE_ID);
210
+
211
+ assert.equal(progress.requestedHeight, 0);
212
+ assert.equal(session.qualityAsk, null, "said once and let go");
213
+ });
214
+
215
+ test("a COPIED picture is never asked to slow its encoder, because it has none", async (t) => {
216
+ const { manager, session, dirPath } = await managerWithSession({ transcodeVideo: false });
217
+ t.after(async () => {
218
+ await manager.disposeAll();
219
+ await rm(dirPath, { recursive: true, force: true });
220
+ });
221
+
222
+ // Whatever this reading says, a copy has no encoder to make cheaper: moving
223
+ // the viewer to a RE-ENCODED rung costs the machine more, not less.
224
+ session.budgetSlowSince = Date.now() - 60_000;
225
+ session.recentSpeed = { speed: 0.4, at: Date.now(), run: [...session.runs][0] };
226
+
227
+ await manager.runQualityBudgetOnce();
228
+
229
+ assert.equal(session.qualityAsk, null, "the copy path's lever is the viewer's link, not the CPU");
230
+ });
231
+
232
+ test("a measured link becomes the encoder's own bitrate ceiling, and nothing else moves", () => {
233
+ // The one lever that reduces what is sent without touching the picture:
234
+ // -maxrate/-bufsize and CRF do not appear in the SPS, so the init segment
235
+ // already in the player's hands goes on describing every fragment.
236
+ const uncapped = softwareDescriptor().buildVideoArgs({
237
+ targetWidth: 1280,
238
+ targetHeight: 720,
239
+ segmentDurationSec: 4,
240
+ fps: 24
241
+ });
242
+ const capped = softwareDescriptor().buildVideoArgs({
243
+ targetWidth: 1280,
244
+ targetHeight: 720,
245
+ segmentDurationSec: 4,
246
+ fps: 24,
247
+ nominalKbps: 1200
248
+ });
249
+
250
+ assert.equal(
251
+ uncapped[uncapped.indexOf("-maxrate") + 1],
252
+ `${maxrateKbpsFor(nominalKbpsForHeight(720))}k`,
253
+ "with nothing measured the rung's own nominal rate stands"
254
+ );
255
+ assert.equal(capped[capped.indexOf("-maxrate") + 1], `${maxrateKbpsFor(1200)}k`);
256
+ // Everything that decides the SIZE must be identical in the two.
257
+ assert.deepEqual(
258
+ uncapped.slice(0, uncapped.indexOf("-maxrate")),
259
+ capped.slice(0, capped.indexOf("-maxrate")),
260
+ "the scale filter, the codec and the preset are untouched by a rate cap"
261
+ );
262
+ });
263
+
264
+ test("the size an init segment describes is read from the init, not assumed", () => {
265
+ // A minimal moov/trak/mdia/minf/stbl/stsd with one avc1 entry. Built here
266
+ // rather than taken from a fixture so the offsets under test are the ones
267
+ // ISO/IEC 14496-12 states, and a fixture cannot quietly encode a mistake.
268
+ const avc1 = Buffer.alloc(8 + 8 + 16 + 4);
269
+ avc1.writeUInt32BE(avc1.length, 0);
270
+ avc1.write("avc1", 4, "latin1");
271
+ avc1.writeUInt16BE(960, 32);
272
+ avc1.writeUInt16BE(540, 34);
273
+
274
+ const stsd = Buffer.concat([Buffer.alloc(8 + 8), avc1]);
275
+ stsd.writeUInt32BE(stsd.length, 0);
276
+ stsd.write("stsd", 4, "latin1");
277
+ stsd.writeUInt32BE(1, 12); // entry_count
278
+
279
+ const wrap = (type, payload) => {
280
+ const box = Buffer.alloc(8 + payload.length);
281
+ box.writeUInt32BE(box.length, 0);
282
+ box.write(type, 4, "latin1");
283
+ payload.copy(box, 8);
284
+ return box;
285
+ };
286
+ const init = wrap("moov", wrap("trak", wrap("mdia", wrap("minf", wrap("stbl", stsd)))));
287
+
288
+ assert.deepEqual(readVideoSampleSize(init), { width: 960, height: 540 });
289
+ assert.equal(readVideoSampleSize(Buffer.alloc(0)), null);
290
+ });
291
+
292
+ test("a COPIED picture too thick for the viewer's link is asked for as a smaller VARIANT", async (t) => {
293
+ const { manager, session, dirPath } = await managerWithSession({ transcodeVideo: false });
294
+ t.after(async () => {
295
+ await manager.disposeAll();
296
+ await rm(dirPath, { recursive: true, force: true });
297
+ });
298
+
299
+ // Four seconds of segment at 2 MB is ~4 Mbit/s of stream. The viewer reports
300
+ // a link that cannot carry it and a buffer that is running dry.
301
+ await produceSegments(session, 2_000_000);
302
+ viewerOf(session, "viewer").netReport = { linkMbps: 1.0, bufferedAheadSec: 1.5, positionSeconds: null, at: Date.now() };
303
+ session.linkSlowSince = Date.now() - 60_000;
304
+
305
+ await manager.runQualityBudgetOnce();
306
+
307
+ assert.ok(
308
+ session.qualityAsk,
309
+ "a copy has no encoder to bound, so the only way to send fewer bits is another rendering of the film"
310
+ );
311
+ assert.ok(session.qualityAsk.height < 1080, `a step down (asked for ${session.qualityAsk?.height}p)`);
312
+ });
313
+
314
+ test("with two viewers the budget acts on the WORST link, not on whoever reported last", async (t) => {
315
+ const { manager, session, dirPath } = await managerWithSession({ transcodeVideo: false });
316
+ t.after(async () => {
317
+ await manager.disposeAll();
318
+ await rm(dirPath, { recursive: true, force: true });
319
+ });
320
+
321
+ await produceSegments(session, 2_000_000);
322
+ // One viewer is comfortable and reported LAST, which under a single field was
323
+ // the whole of what the budget saw. The other cannot carry the stream and is
324
+ // running dry.
325
+ viewerOf(session, "thin").netReport = {
326
+ linkMbps: 1.0,
327
+ bufferedAheadSec: 1.5,
328
+ positionSeconds: 40,
329
+ at: Date.now() - 1_000
330
+ };
331
+ viewerOf(session, "fat").netReport = {
332
+ linkMbps: 80,
333
+ bufferedAheadSec: 60,
334
+ positionSeconds: 40,
335
+ at: Date.now()
336
+ };
337
+ session.linkSlowSince = Date.now() - 60_000;
338
+
339
+ await manager.runQualityBudgetOnce();
340
+
341
+ assert.ok(
342
+ session.qualityAsk,
343
+ "the viewer who cannot keep up decides, whichever of them reported most recently"
344
+ );
345
+ });
346
+
347
+ test("a report from a viewer who has left stops counting", async (t) => {
348
+ const { manager, session, dirPath } = await managerWithSession({ transcodeVideo: false });
349
+ t.after(async () => {
350
+ await manager.disposeAll();
351
+ await rm(dirPath, { recursive: true, force: true });
352
+ });
353
+
354
+ await produceSegments(session, 2_000_000);
355
+ // A reading describes a link at a moment, and one this old cannot decide for
356
+ // the viewers still here. ONLY THE READING EXPIRES: whether the person is
357
+ // still watching is a different question with its own answer — their
358
+ // connection — and answering both from this one place is what stopped a
359
+ // soundtrack's encoder on 2026-09-05, so silence no longer removes anybody.
360
+ viewerOf(session, "gone").netReport = {
361
+ linkMbps: 1.0,
362
+ bufferedAheadSec: 1.5,
363
+ positionSeconds: 40,
364
+ at: Date.now() - 120_000
365
+ };
366
+ manager.recordNetReport(session.id, {
367
+ linkMbps: 80,
368
+ bufferedAheadSec: 60,
369
+ consumerId: "here",
370
+ positionSeconds: 40
371
+ });
372
+ session.linkSlowSince = Date.now() - 60_000;
373
+
374
+ await manager.runQualityBudgetOnce();
375
+
376
+ assert.equal(session.viewers.size, 2, "the viewer is still known — silence is not leaving");
377
+ assert.equal(
378
+ viewerOf(session, "gone").netReport,
379
+ null,
380
+ "but their reading has expired, so it decides nothing"
381
+ );
382
+ assert.equal(session.qualityAsk, null, "the viewer who is here can carry the picture");
383
+ });
384
+
385
+ test("the way BACK UP exists, and a bitrate cap is lifted before the picture is enlarged", async (t) => {
386
+ const { manager, session, dirPath } = await managerWithSession();
387
+ t.after(async () => {
388
+ await manager.disposeAll();
389
+ await rm(dirPath, { recursive: true, force: true });
390
+ });
391
+
392
+ // The viewer is on 480p, the machine has been ahead of realtime for longer
393
+ // than the up window, and nothing is capping the bitrate.
394
+ session.variantHeight = 480;
395
+ session.encodeWidth = 854;
396
+ session.encodeHeight = 480;
397
+ session.recentSpeed = { speed: 2.4, at: Date.now(), run: [...session.runs][0] };
398
+ session.budgetUpSince = Date.now() - 120_000;
399
+
400
+ await manager.runQualityBudgetOnce();
401
+
402
+ assert.ok(session.qualityAsk, "for most of this project's life there was no step up at all");
403
+ assert.equal(
404
+ session.qualityAsk.height,
405
+ 540,
406
+ "one rung at a time: the lowest height above the one on screen, never above the source"
407
+ );
408
+ });
409
+
410
+ test("a capped picture gets its own bitrate back before it is asked to grow", async (t) => {
411
+ const { manager, session, dirPath } = await managerWithSession();
412
+ t.after(async () => {
413
+ await manager.disposeAll();
414
+ await rm(dirPath, { recursive: true, force: true });
415
+ });
416
+
417
+ session.variantHeight = 480;
418
+ session.encodeWidth = 854;
419
+ session.encodeHeight = 480;
420
+ session.rateCapKbps = 700;
421
+ session.recentSpeed = { speed: 2.4, at: Date.now(), run: [...session.runs][0] };
422
+ session.budgetUpSince = Date.now() - 120_000;
423
+ // A restart is what lifting the cap costs, and spawning ffmpeg is not this
424
+ // test's business. The run the fixture gave the session is the one the
425
+ // reading above came from, and replacing it here would make that reading
426
+ // belong to a run that is gone — which is exactly what the comparison is for.
427
+
428
+ await manager.runQualityBudgetOnce().catch(() => undefined);
429
+
430
+ assert.equal(session.rateCapKbps, null, "the cap goes first: it is cheaper than enlarging the picture");
431
+ assert.equal(
432
+ session.qualityAsk,
433
+ null,
434
+ "and the height is left for a second unbroken window, so the two do not move at once"
435
+ );
436
+ });
437
+
438
+ test("a stream that publishes no variants is left alone, and said so once", async (t) => {
439
+ const { manager, session, dirPath } = await managerWithSession({
440
+ transcodeVideo: false,
441
+ // A copy whose keyframe index could not be read falls back to an even grid
442
+ // ffmpeg does not cut on. Nothing can be aligned to that, so there is no
443
+ // master and no variant to move to.
444
+ cutGrid: "even"
445
+ });
446
+ t.after(async () => {
447
+ await manager.disposeAll();
448
+ await rm(dirPath, { recursive: true, force: true });
449
+ });
450
+
451
+ await produceSegments(session, 2_000_000);
452
+ viewerOf(session, "viewer").netReport = { linkMbps: 1.0, bufferedAheadSec: 1.5, positionSeconds: null, at: Date.now() };
453
+ session.linkSlowSince = Date.now() - 60_000;
454
+
455
+ await manager.runQualityBudgetOnce();
456
+
457
+ assert.equal(session.qualityAsk, null, "asking a player with no variants to change variant is nothing");
458
+ assert.equal(session.saidNoVariants, true, "and the reason is stated once, not once per window");
459
+ });
460
+
461
+ test("a height this machine has been MEASURED failing at is not what the way back up offers", async (t) => {
462
+ const { manager, session, dirPath } = await managerWithSession();
463
+ t.after(async () => {
464
+ await manager.disposeAll();
465
+ await rm(dirPath, { recursive: true, force: true });
466
+ });
467
+
468
+ // The base ran 720p at half realtime and the viewer was stepped down to 480p.
469
+ // The base's own height used to be exempt from every refusal — it was the
470
+ // rung on screen, back when a step changed the encode inside it — so the way
471
+ // back up would have asked for 720p again, failed again, and stepped down
472
+ // again, about every hundred seconds for the length of the film.
473
+ manager.softwarePresetBenchmark = [{ preset: "ultrafast", pixelsPerSec: 1e6 }];
474
+ session.lastAloneSpeed = 0.5;
475
+ session.variantHeight = 720;
476
+
477
+ const offered = manager.offeredHeights(session);
478
+
479
+ assert.ok(!offered.includes(720) || manager.liveOutputs.variantHeightOf(session) === 720);
480
+ // Now on the 480p variant: 720p has a reading of its own and must be gone.
481
+ session.variantHeight = 480;
482
+ session.encodeHeight = 480;
483
+ session.encodeWidth = 854;
484
+ assert.ok(
485
+ !manager.offeredHeights(session).includes(720),
486
+ "a rung measured below realtime is withdrawn once the viewer has left it"
487
+ );
488
+ });
489
+
490
+ test("a cap is not lifted because there is no higher rung to compare against", async (t) => {
491
+ const { manager, session, dirPath } = await managerWithSession();
492
+ t.after(async () => {
493
+ await manager.disposeAll();
494
+ await rm(dirPath, { recursive: true, force: true });
495
+ });
496
+
497
+ // At the top offered height, so there is no NEXT rung — and the question of
498
+ // whether to lift the cap is about THIS one. Deciding it on "nothing to step
499
+ // to, so yes" took the cap off a link measured at a fifth of what the picture
500
+ // needs, after which #checkLinkBudget put it straight back: two ffmpeg
501
+ // restarts a minute and a half, on exactly the thin cellular viewer the cap
502
+ // exists for.
503
+ session.variantHeight = 1080;
504
+ session.encodeWidth = 1920;
505
+ session.encodeHeight = 1080;
506
+ session.rateCapKbps = 700;
507
+ session.recentSpeed = { speed: 2.4, at: Date.now(), run: [...session.runs][0] };
508
+ session.budgetUpSince = Date.now() - 120_000;
509
+ viewerOf(session, "viewer").netReport = { linkMbps: 1.0, bufferedAheadSec: 30, positionSeconds: null, at: Date.now() };
510
+
511
+ await manager.runQualityBudgetOnce();
512
+
513
+ assert.equal(session.rateCapKbps, 700, "the link still cannot carry this picture uncapped");
514
+ });