@torrent-tv/proxy 2.52.0 → 2.54.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.
- package/CHANGELOG.md +20 -0
- package/package.json +2 -2
- package/services/hls-session-manager.js +697 -169
- package/services/hwaccel.js +49 -6
- package/services/segment-formats/fmp4.js +320 -300
- package/services/segment-formats/mp4-boxes.js +59 -0
- package/test/auto-quality-step.test.js +442 -0
- package/test/quality-variants.test.js +6 -5
|
@@ -245,3 +245,62 @@ export function readSelfContainedStartSeconds(piece) {
|
|
|
245
245
|
});
|
|
246
246
|
return startSeconds;
|
|
247
247
|
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Sample entry types that describe a picture. Anything else in an `stsd` is a
|
|
251
|
+
* soundtrack or a text track, whose "size" means nothing here.
|
|
252
|
+
*
|
|
253
|
+
* @type {ReadonlySet<string>}
|
|
254
|
+
*/
|
|
255
|
+
const VISUAL_SAMPLE_ENTRIES = new Set(["avc1", "avc3", "hvc1", "hev1", "hvc2", "av01", "vp08", "vp09", "mp4v"]);
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* The picture size an init segment describes, in pixels, or null when it
|
|
259
|
+
* describes no picture.
|
|
260
|
+
*
|
|
261
|
+
* Read from the visual sample entry rather than taken from our own record of
|
|
262
|
+
* what the encoder was told, because those two disagreeing IS the fault this
|
|
263
|
+
* exists to name: the init segment is fetched once, by `#EXT-X-MAP`, and then
|
|
264
|
+
* every fragment of the session is decoded against it. A run that encodes
|
|
265
|
+
* another size produces fragments the decoder cannot read — measured
|
|
266
|
+
* 2026-08-21, a browser went on reporting 1280x720 for three and a half
|
|
267
|
+
* minutes after the encoder had left for 960x540, over a band of macroblock
|
|
268
|
+
* garbage.
|
|
269
|
+
*
|
|
270
|
+
* The layout is ISO/IEC 14496-12 `SampleEntry` (6 reserved bytes + 2 bytes of
|
|
271
|
+
* data_reference_index) followed by `VisualSampleEntry`'s 16 bytes of
|
|
272
|
+
* pre_defined/reserved, then width and height as 16-bit integers.
|
|
273
|
+
*
|
|
274
|
+
* @param {Buffer} initSegment
|
|
275
|
+
* @returns {{ width: number, height: number } | null}
|
|
276
|
+
*/
|
|
277
|
+
export function readVideoSampleSize(initSegment) {
|
|
278
|
+
if (!initSegment || initSegment.length === 0) {
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
/** @type {{ width: number, height: number } | null} */
|
|
282
|
+
let found = null;
|
|
283
|
+
walkBoxes(initSegment, (type, bodyStart, bodyEnd) => {
|
|
284
|
+
if (type !== "stsd" || found !== null) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
// Full box: version + flags, then the entry count.
|
|
288
|
+
let offset = bodyStart + 8;
|
|
289
|
+
while (offset + 8 <= bodyEnd && found === null) {
|
|
290
|
+
const size = initSegment.readUInt32BE(offset);
|
|
291
|
+
const entryType = initSegment.toString("latin1", offset + 4, offset + 8);
|
|
292
|
+
if (size < 8 || offset + size > bodyEnd) {
|
|
293
|
+
return; // malformed — say nothing rather than read out of bounds
|
|
294
|
+
}
|
|
295
|
+
if (VISUAL_SAMPLE_ENTRIES.has(entryType) && offset + 36 <= bodyEnd) {
|
|
296
|
+
const width = initSegment.readUInt16BE(offset + 32);
|
|
297
|
+
const height = initSegment.readUInt16BE(offset + 34);
|
|
298
|
+
if (width > 0 && height > 0) {
|
|
299
|
+
found = { width, height };
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
offset += size;
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
return found;
|
|
306
|
+
}
|
|
@@ -0,0 +1,442 @@
|
|
|
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 assert from "node:assert/strict";
|
|
21
|
+
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
22
|
+
import os from "node:os";
|
|
23
|
+
import path from "node:path";
|
|
24
|
+
import { HlsSessionManager } from "../services/hls-session-manager.js";
|
|
25
|
+
import { fmp4Format } from "../services/segment-formats/fmp4.js";
|
|
26
|
+
import { softwareDescriptor, maxrateKbpsFor, nominalKbpsForHeight } from "../services/hwaccel.js";
|
|
27
|
+
import { readVideoSampleSize } from "../services/segment-formats/mp4-boxes.js";
|
|
28
|
+
|
|
29
|
+
const BASE_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
|
|
30
|
+
const SEGMENT_SECONDS = 4;
|
|
31
|
+
|
|
32
|
+
/** A child process that is alive as far as the budget is concerned. */
|
|
33
|
+
function fakeEncoder() {
|
|
34
|
+
return {
|
|
35
|
+
pid: 4321,
|
|
36
|
+
exitCode: null,
|
|
37
|
+
signalCode: null,
|
|
38
|
+
kill() {},
|
|
39
|
+
once(event, handler) {
|
|
40
|
+
if (event === "exit") {
|
|
41
|
+
handler();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A session shaped like a live one, encoding 720p of a 1080p source.
|
|
49
|
+
*
|
|
50
|
+
* @param {{ dirPath: string, transcodeVideo?: boolean, cutGrid?: string }} params
|
|
51
|
+
* @returns {object}
|
|
52
|
+
*/
|
|
53
|
+
function fakeSession({ dirPath, transcodeVideo = true, cutGrid = "keyframe" }) {
|
|
54
|
+
return {
|
|
55
|
+
id: BASE_ID,
|
|
56
|
+
// A copy can only be cut where the source already has a keyframe, so this
|
|
57
|
+
// is what decides whether it publishes variants at all.
|
|
58
|
+
cutGrid,
|
|
59
|
+
dirPath,
|
|
60
|
+
state: "ready",
|
|
61
|
+
fileName: "video.mkv",
|
|
62
|
+
startedAt: Date.now(),
|
|
63
|
+
lastAccessedAt: Date.now(),
|
|
64
|
+
ffmpeg: fakeEncoder(),
|
|
65
|
+
runState: "running",
|
|
66
|
+
runSerial: 1,
|
|
67
|
+
lastError: "",
|
|
68
|
+
consumers: new Set(),
|
|
69
|
+
segmentFormat: fmp4Format,
|
|
70
|
+
transcodeVideo,
|
|
71
|
+
transcodeAudio: true,
|
|
72
|
+
audioOnly: false,
|
|
73
|
+
audioTrackIndex: 0,
|
|
74
|
+
sourceKey: "source-1",
|
|
75
|
+
fileIndex: 0,
|
|
76
|
+
sourceWidth: 1920,
|
|
77
|
+
sourceHeight: 1080,
|
|
78
|
+
encodeWidth: transcodeVideo ? 1280 : 0,
|
|
79
|
+
encodeHeight: transcodeVideo ? 720 : 0,
|
|
80
|
+
outputFps: 24,
|
|
81
|
+
encodeRunGeneration: 0,
|
|
82
|
+
encodeStartIndex: 0,
|
|
83
|
+
budgetSlowSince: 0,
|
|
84
|
+
budgetUpSince: 0,
|
|
85
|
+
budgetLastActionAt: 0,
|
|
86
|
+
qualityAsk: null,
|
|
87
|
+
initSizeSaid: "",
|
|
88
|
+
recentSpeed: null,
|
|
89
|
+
rateCapKbps: null,
|
|
90
|
+
netReport: null,
|
|
91
|
+
linkSlowSince: 0,
|
|
92
|
+
lastAloneSpeed: null,
|
|
93
|
+
durationSeconds: 400,
|
|
94
|
+
totalDurationSeconds: 400,
|
|
95
|
+
usesExplicitCuts: false,
|
|
96
|
+
useSyntheticPlaylist: true,
|
|
97
|
+
playlistText: "#EXTM3U\n",
|
|
98
|
+
segmentBoundaries: Array.from({ length: 101 }, (_, index) => index * SEGMENT_SECONDS),
|
|
99
|
+
segmentCount: 100,
|
|
100
|
+
progress: { state: "running", processedSeconds: 40, startPositionSeconds: 0, speed: "1.0x" }
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @param {{ transcodeVideo?: boolean, cutGrid?: string }} [options]
|
|
106
|
+
* @returns {Promise<{ manager: HlsSessionManager, session: object, dirPath: string, restarts: number[] }>}
|
|
107
|
+
*/
|
|
108
|
+
async function managerWithSession({ transcodeVideo = true, cutGrid = "keyframe" } = {}) {
|
|
109
|
+
const dirPath = await mkdtemp(path.join(os.tmpdir(), "auto-quality-"));
|
|
110
|
+
const manager = new HlsSessionManager({
|
|
111
|
+
enabled: true,
|
|
112
|
+
ffmpegBin: "ffmpeg",
|
|
113
|
+
localBindHost: "127.0.0.1",
|
|
114
|
+
localPort: 9090
|
|
115
|
+
});
|
|
116
|
+
// A software host: the budget's own precondition.
|
|
117
|
+
manager.videoEncoder = { kind: "software", name: "libx264", inputArgs: [] };
|
|
118
|
+
// A fully-downloaded file, so nothing here is ever read as download-bound —
|
|
119
|
+
// the distinction is tested elsewhere and would only obscure these.
|
|
120
|
+
manager.getSourceStats = async () => ({
|
|
121
|
+
downloadSpeed: 10e6,
|
|
122
|
+
fileProgress: 1,
|
|
123
|
+
fileLength: 4e9
|
|
124
|
+
});
|
|
125
|
+
const session = fakeSession({ dirPath, transcodeVideo, cutGrid });
|
|
126
|
+
manager.sessionsById.set(BASE_ID, session);
|
|
127
|
+
return { manager, session, dirPath };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Produced segments of a known size, so the observed stream bitrate the link
|
|
132
|
+
* check compares against is a real reading of real files.
|
|
133
|
+
*
|
|
134
|
+
* @param {object} session
|
|
135
|
+
* @param {number} bytesEach
|
|
136
|
+
* @returns {Promise<void>}
|
|
137
|
+
*/
|
|
138
|
+
async function produceSegments(session, bytesEach) {
|
|
139
|
+
// Where a run really writes: the manager reads produced files out of the
|
|
140
|
+
// `run-N` directories, not out of the session directory itself.
|
|
141
|
+
const runDir = path.join(session.dirPath, `run-${session.runSerial}`);
|
|
142
|
+
await mkdir(runDir, { recursive: true });
|
|
143
|
+
session.runDirPath = runDir;
|
|
144
|
+
for (let index = 0; index < 4; index += 1) {
|
|
145
|
+
await writeFile(
|
|
146
|
+
path.join(runDir, session.segmentFormat.segmentFileName(index)),
|
|
147
|
+
Buffer.alloc(bytesEach)
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
test("a picture that cannot be kept up with is asked for as another VARIANT, and its size is left alone", async (t) => {
|
|
153
|
+
const { manager, session, dirPath } = await managerWithSession();
|
|
154
|
+
t.after(async () => {
|
|
155
|
+
await manager.disposeAll();
|
|
156
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
const sizeBefore = `${session.encodeWidth}x${session.encodeHeight}`;
|
|
160
|
+
// Sustained sub-realtime, read as a slope: the run has been slow since well
|
|
161
|
+
// before the window, and the reading is from this very run.
|
|
162
|
+
session.budgetSlowSince = Date.now() - 60_000;
|
|
163
|
+
session.recentSpeed = { speed: 0.7, at: Date.now(), runSerial: session.runSerial };
|
|
164
|
+
|
|
165
|
+
await manager.runQualityBudgetOnce();
|
|
166
|
+
|
|
167
|
+
assert.equal(
|
|
168
|
+
`${session.encodeWidth}x${session.encodeHeight}`,
|
|
169
|
+
sizeBefore,
|
|
170
|
+
"the size the init segment describes must survive the step — that is the whole fault"
|
|
171
|
+
);
|
|
172
|
+
assert.ok(session.qualityAsk, "the step is a request to the player to move variant");
|
|
173
|
+
assert.ok(
|
|
174
|
+
session.qualityAsk.height < 720,
|
|
175
|
+
`a step DOWN, and 720p was on screen (asked for ${session.qualityAsk?.height}p)`
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("the request reaches the browser in the progress report, and stops once the viewer is there", async (t) => {
|
|
180
|
+
const { manager, session, dirPath } = await managerWithSession();
|
|
181
|
+
t.after(async () => {
|
|
182
|
+
await manager.disposeAll();
|
|
183
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
session.qualityAsk = { height: 480, at: Date.now(), reason: "measured" };
|
|
187
|
+
const asked = await manager.getSessionProgress(BASE_ID);
|
|
188
|
+
assert.equal(asked.requestedHeight, 480, "the request travels with every progress report");
|
|
189
|
+
|
|
190
|
+
// The player moved: the variant it is now watching IS the height asked for.
|
|
191
|
+
session.variantHeight = 480;
|
|
192
|
+
const answered = await manager.getSessionProgress(BASE_ID);
|
|
193
|
+
assert.equal(answered.requestedHeight, 0, "a request the viewer has answered is not repeated");
|
|
194
|
+
assert.equal(session.qualityAsk, null, "and it is let go of, not merely hidden");
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test("a request the player never follows runs out instead of being repeated for the whole film", async (t) => {
|
|
198
|
+
const { manager, session, dirPath } = await managerWithSession();
|
|
199
|
+
t.after(async () => {
|
|
200
|
+
await manager.disposeAll();
|
|
201
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
// A viewer on a manual pick ignores every request by design, and so does a
|
|
205
|
+
// stream with no variants. Neither is an error; both look the same from here.
|
|
206
|
+
session.qualityAsk = { height: 480, at: Date.now() - 120_000, reason: "measured" };
|
|
207
|
+
|
|
208
|
+
const progress = await manager.getSessionProgress(BASE_ID);
|
|
209
|
+
|
|
210
|
+
assert.equal(progress.requestedHeight, 0);
|
|
211
|
+
assert.equal(session.qualityAsk, null, "said once and let go");
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("a COPIED picture is never asked to slow its encoder, because it has none", async (t) => {
|
|
215
|
+
const { manager, session, dirPath } = await managerWithSession({ transcodeVideo: false });
|
|
216
|
+
t.after(async () => {
|
|
217
|
+
await manager.disposeAll();
|
|
218
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// Whatever this reading says, a copy has no encoder to make cheaper: moving
|
|
222
|
+
// the viewer to a RE-ENCODED rung costs the machine more, not less.
|
|
223
|
+
session.budgetSlowSince = Date.now() - 60_000;
|
|
224
|
+
session.recentSpeed = { speed: 0.4, at: Date.now(), runSerial: session.runSerial };
|
|
225
|
+
|
|
226
|
+
await manager.runQualityBudgetOnce();
|
|
227
|
+
|
|
228
|
+
assert.equal(session.qualityAsk, null, "the copy path's lever is the viewer's link, not the CPU");
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test("a measured link becomes the encoder's own bitrate ceiling, and nothing else moves", () => {
|
|
232
|
+
// The one lever that reduces what is sent without touching the picture:
|
|
233
|
+
// -maxrate/-bufsize and CRF do not appear in the SPS, so the init segment
|
|
234
|
+
// already in the player's hands goes on describing every fragment.
|
|
235
|
+
const uncapped = softwareDescriptor().buildVideoArgs({
|
|
236
|
+
targetWidth: 1280,
|
|
237
|
+
targetHeight: 720,
|
|
238
|
+
segmentDurationSec: 4,
|
|
239
|
+
fps: 24
|
|
240
|
+
});
|
|
241
|
+
const capped = softwareDescriptor().buildVideoArgs({
|
|
242
|
+
targetWidth: 1280,
|
|
243
|
+
targetHeight: 720,
|
|
244
|
+
segmentDurationSec: 4,
|
|
245
|
+
fps: 24,
|
|
246
|
+
nominalKbps: 1200
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
assert.equal(
|
|
250
|
+
uncapped[uncapped.indexOf("-maxrate") + 1],
|
|
251
|
+
`${maxrateKbpsFor(nominalKbpsForHeight(720))}k`,
|
|
252
|
+
"with nothing measured the rung's own nominal rate stands"
|
|
253
|
+
);
|
|
254
|
+
assert.equal(capped[capped.indexOf("-maxrate") + 1], `${maxrateKbpsFor(1200)}k`);
|
|
255
|
+
// Everything that decides the SIZE must be identical in the two.
|
|
256
|
+
assert.deepEqual(
|
|
257
|
+
uncapped.slice(0, uncapped.indexOf("-maxrate")),
|
|
258
|
+
capped.slice(0, capped.indexOf("-maxrate")),
|
|
259
|
+
"the scale filter, the codec and the preset are untouched by a rate cap"
|
|
260
|
+
);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test("the size an init segment describes is read from the init, not assumed", () => {
|
|
264
|
+
// A minimal moov/trak/mdia/minf/stbl/stsd with one avc1 entry. Built here
|
|
265
|
+
// rather than taken from a fixture so the offsets under test are the ones
|
|
266
|
+
// ISO/IEC 14496-12 states, and a fixture cannot quietly encode a mistake.
|
|
267
|
+
const avc1 = Buffer.alloc(8 + 8 + 16 + 4);
|
|
268
|
+
avc1.writeUInt32BE(avc1.length, 0);
|
|
269
|
+
avc1.write("avc1", 4, "latin1");
|
|
270
|
+
avc1.writeUInt16BE(960, 32);
|
|
271
|
+
avc1.writeUInt16BE(540, 34);
|
|
272
|
+
|
|
273
|
+
const stsd = Buffer.concat([Buffer.alloc(8 + 8), avc1]);
|
|
274
|
+
stsd.writeUInt32BE(stsd.length, 0);
|
|
275
|
+
stsd.write("stsd", 4, "latin1");
|
|
276
|
+
stsd.writeUInt32BE(1, 12); // entry_count
|
|
277
|
+
|
|
278
|
+
const wrap = (type, payload) => {
|
|
279
|
+
const box = Buffer.alloc(8 + payload.length);
|
|
280
|
+
box.writeUInt32BE(box.length, 0);
|
|
281
|
+
box.write(type, 4, "latin1");
|
|
282
|
+
payload.copy(box, 8);
|
|
283
|
+
return box;
|
|
284
|
+
};
|
|
285
|
+
const init = wrap("moov", wrap("trak", wrap("mdia", wrap("minf", wrap("stbl", stsd)))));
|
|
286
|
+
|
|
287
|
+
assert.deepEqual(readVideoSampleSize(init), { width: 960, height: 540 });
|
|
288
|
+
assert.equal(readVideoSampleSize(Buffer.alloc(0)), null);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test("a COPIED picture too thick for the viewer's link is asked for as a smaller VARIANT", async (t) => {
|
|
292
|
+
const { manager, session, dirPath } = await managerWithSession({ transcodeVideo: false });
|
|
293
|
+
t.after(async () => {
|
|
294
|
+
await manager.disposeAll();
|
|
295
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
// Four seconds of segment at 2 MB is ~4 Mbit/s of stream. The viewer reports
|
|
299
|
+
// a link that cannot carry it and a buffer that is running dry.
|
|
300
|
+
await produceSegments(session, 2_000_000);
|
|
301
|
+
session.netReport = { linkMbps: 1.0, bufferedAheadSec: 1.5, at: Date.now() };
|
|
302
|
+
session.linkSlowSince = Date.now() - 60_000;
|
|
303
|
+
|
|
304
|
+
await manager.runQualityBudgetOnce();
|
|
305
|
+
|
|
306
|
+
assert.ok(
|
|
307
|
+
session.qualityAsk,
|
|
308
|
+
"a copy has no encoder to bound, so the only way to send fewer bits is another rendering of the film"
|
|
309
|
+
);
|
|
310
|
+
assert.ok(session.qualityAsk.height < 1080, `a step down (asked for ${session.qualityAsk?.height}p)`);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
test("the way BACK UP exists, and a bitrate cap is lifted before the picture is enlarged", async (t) => {
|
|
314
|
+
const { manager, session, dirPath } = await managerWithSession();
|
|
315
|
+
t.after(async () => {
|
|
316
|
+
await manager.disposeAll();
|
|
317
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
// The viewer is on 480p, the machine has been ahead of realtime for longer
|
|
321
|
+
// than the up window, and nothing is capping the bitrate.
|
|
322
|
+
session.variantHeight = 480;
|
|
323
|
+
session.encodeWidth = 854;
|
|
324
|
+
session.encodeHeight = 480;
|
|
325
|
+
session.recentSpeed = { speed: 2.4, at: Date.now(), runSerial: session.runSerial };
|
|
326
|
+
session.budgetUpSince = Date.now() - 120_000;
|
|
327
|
+
|
|
328
|
+
await manager.runQualityBudgetOnce();
|
|
329
|
+
|
|
330
|
+
assert.ok(session.qualityAsk, "for most of this project's life there was no step up at all");
|
|
331
|
+
assert.equal(
|
|
332
|
+
session.qualityAsk.height,
|
|
333
|
+
540,
|
|
334
|
+
"one rung at a time: the lowest height above the one on screen, never above the source"
|
|
335
|
+
);
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
test("a capped picture gets its own bitrate back before it is asked to grow", async (t) => {
|
|
339
|
+
const { manager, session, dirPath } = await managerWithSession();
|
|
340
|
+
t.after(async () => {
|
|
341
|
+
await manager.disposeAll();
|
|
342
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
session.variantHeight = 480;
|
|
346
|
+
session.encodeWidth = 854;
|
|
347
|
+
session.encodeHeight = 480;
|
|
348
|
+
session.rateCapKbps = 700;
|
|
349
|
+
session.recentSpeed = { speed: 2.4, at: Date.now(), runSerial: session.runSerial };
|
|
350
|
+
session.budgetUpSince = Date.now() - 120_000;
|
|
351
|
+
// A restart is what lifting the cap costs, and spawning ffmpeg is not this
|
|
352
|
+
// test's business — the session is left with no encoder to replace, which is
|
|
353
|
+
// the same path a run that has already ended takes.
|
|
354
|
+
session.ffmpeg = fakeEncoder();
|
|
355
|
+
|
|
356
|
+
await manager.runQualityBudgetOnce().catch(() => undefined);
|
|
357
|
+
|
|
358
|
+
assert.equal(session.rateCapKbps, null, "the cap goes first: it is cheaper than enlarging the picture");
|
|
359
|
+
assert.equal(
|
|
360
|
+
session.qualityAsk,
|
|
361
|
+
null,
|
|
362
|
+
"and the height is left for a second unbroken window, so the two do not move at once"
|
|
363
|
+
);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
test("a stream that publishes no variants is left alone, and said so once", async (t) => {
|
|
367
|
+
const { manager, session, dirPath } = await managerWithSession({
|
|
368
|
+
transcodeVideo: false,
|
|
369
|
+
// A copy whose keyframe index could not be read falls back to an even grid
|
|
370
|
+
// ffmpeg does not cut on. Nothing can be aligned to that, so there is no
|
|
371
|
+
// master and no variant to move to.
|
|
372
|
+
cutGrid: "even"
|
|
373
|
+
});
|
|
374
|
+
t.after(async () => {
|
|
375
|
+
await manager.disposeAll();
|
|
376
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
await produceSegments(session, 2_000_000);
|
|
380
|
+
session.netReport = { linkMbps: 1.0, bufferedAheadSec: 1.5, at: Date.now() };
|
|
381
|
+
session.linkSlowSince = Date.now() - 60_000;
|
|
382
|
+
|
|
383
|
+
await manager.runQualityBudgetOnce();
|
|
384
|
+
|
|
385
|
+
assert.equal(session.qualityAsk, null, "asking a player with no variants to change variant is nothing");
|
|
386
|
+
assert.equal(session.saidNoVariants, true, "and the reason is stated once, not once per window");
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
test("a height this machine has been MEASURED failing at is not what the way back up offers", async (t) => {
|
|
390
|
+
const { manager, session, dirPath } = await managerWithSession();
|
|
391
|
+
t.after(async () => {
|
|
392
|
+
await manager.disposeAll();
|
|
393
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
// The base ran 720p at half realtime and the viewer was stepped down to 480p.
|
|
397
|
+
// The base's own height used to be exempt from every refusal — it was the
|
|
398
|
+
// rung on screen, back when a step changed the encode inside it — so the way
|
|
399
|
+
// back up would have asked for 720p again, failed again, and stepped down
|
|
400
|
+
// again, about every hundred seconds for the length of the film.
|
|
401
|
+
manager.softwarePresetBenchmark = [{ preset: "ultrafast", pixelsPerSec: 1e6 }];
|
|
402
|
+
session.lastAloneSpeed = 0.5;
|
|
403
|
+
session.variantHeight = 720;
|
|
404
|
+
|
|
405
|
+
const offered = manager.offeredHeights(session);
|
|
406
|
+
|
|
407
|
+
assert.ok(!offered.includes(720) || manager.variantHeightOf(session) === 720);
|
|
408
|
+
// Now on the 480p variant: 720p has a reading of its own and must be gone.
|
|
409
|
+
session.variantHeight = 480;
|
|
410
|
+
session.encodeHeight = 480;
|
|
411
|
+
session.encodeWidth = 854;
|
|
412
|
+
assert.ok(
|
|
413
|
+
!manager.offeredHeights(session).includes(720),
|
|
414
|
+
"a rung measured below realtime is withdrawn once the viewer has left it"
|
|
415
|
+
);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
test("a cap is not lifted because there is no higher rung to compare against", async (t) => {
|
|
419
|
+
const { manager, session, dirPath } = await managerWithSession();
|
|
420
|
+
t.after(async () => {
|
|
421
|
+
await manager.disposeAll();
|
|
422
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
// At the top offered height, so there is no NEXT rung — and the question of
|
|
426
|
+
// whether to lift the cap is about THIS one. Deciding it on "nothing to step
|
|
427
|
+
// to, so yes" took the cap off a link measured at a fifth of what the picture
|
|
428
|
+
// needs, after which #checkLinkBudget put it straight back: two ffmpeg
|
|
429
|
+
// restarts a minute and a half, on exactly the thin cellular viewer the cap
|
|
430
|
+
// exists for.
|
|
431
|
+
session.variantHeight = 1080;
|
|
432
|
+
session.encodeWidth = 1920;
|
|
433
|
+
session.encodeHeight = 1080;
|
|
434
|
+
session.rateCapKbps = 700;
|
|
435
|
+
session.recentSpeed = { speed: 2.4, at: Date.now(), runSerial: session.runSerial };
|
|
436
|
+
session.budgetUpSince = Date.now() - 120_000;
|
|
437
|
+
session.netReport = { linkMbps: 1.0, bufferedAheadSec: 30, at: Date.now() };
|
|
438
|
+
|
|
439
|
+
await manager.runQualityBudgetOnce();
|
|
440
|
+
|
|
441
|
+
assert.equal(session.rateCapKbps, 700, "the link still cannot carry this picture uncapped");
|
|
442
|
+
});
|
|
@@ -543,16 +543,17 @@ test("a playlist or an init segment does not move the encoder", async (t) => {
|
|
|
543
543
|
assert.ok(base.ffmpeg, "the stream on screen must keep its encoder while the player is only looking");
|
|
544
544
|
});
|
|
545
545
|
|
|
546
|
-
test("
|
|
546
|
+
test("the name of a variant is fixed, whatever its encode is later set to", async (t) => {
|
|
547
547
|
const { manager, base, dirPath } = await managerWithBase();
|
|
548
548
|
t.after(async () => {
|
|
549
549
|
await manager.disposeAll();
|
|
550
550
|
await rm(dirPath, { recursive: true, force: true });
|
|
551
551
|
});
|
|
552
552
|
// The player fetched the master once and addresses this variant as 812p for
|
|
553
|
-
// the rest of the session
|
|
554
|
-
//
|
|
555
|
-
//
|
|
553
|
+
// the rest of the session, so the name must not follow the encode. Nothing in
|
|
554
|
+
// the proxy changes `encodeHeight` mid-session any more — a change of size is
|
|
555
|
+
// a change of variant now — but the name and the encode are still two
|
|
556
|
+
// different things, and the addressing depends on their staying so.
|
|
556
557
|
assert.equal(manager.variantHeightOf(base), 812);
|
|
557
558
|
base.encodeHeight = 540;
|
|
558
559
|
|
|
@@ -564,7 +565,7 @@ test("a downshift does not rename the variant the viewer is watching", async (t)
|
|
|
564
565
|
assert.deepEqual(
|
|
565
566
|
await manager.resolveVariantFile(BASE_ID, 812, "segment-00000.mp4"),
|
|
566
567
|
{ sessionId: BASE_ID },
|
|
567
|
-
"a second session at
|
|
568
|
+
"a second session at a height the host has already failed to manage is the opposite of what a step is for"
|
|
568
569
|
);
|
|
569
570
|
});
|
|
570
571
|
|