@torrent-tv/proxy 2.36.0 → 2.36.2
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 +10 -0
- package/bin/cli.js +5 -0
- package/package.json +1 -1
- package/scripts/read-container-index.mjs +108 -0
- package/services/container-index/matroska.js +142 -6
- package/services/hls-session-manager.js +48 -5
- package/services/thread-pool.js +30 -0
- package/test/decode-cost.test.js +25 -11
- package/test/matroska-cues-track.test.js +192 -0
- package/test/quality-variants.test.js +46 -0
- package/test/thread-pool.test.js +53 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
## 2.36.2
|
|
2
|
+
|
|
3
|
+
- **Fix**: A live session no longer answers 404 to the master playlist it has just published. The browser is handed `master.m3u8` when the session is created, and which rungs are worth OFFERING is recomputed every five seconds — so on 2026-08-18 a five-rung offer became a one-rung offer **192 ms** after creation (the session's own encoder started, charging the contention penalty of 2.35.0, and the first supply reading raised the bar of 2.36.0 from 1.00x to 1.06x), `buildMasterPlaylist` returned null for having fewer than two rungs, and the master answered 404. hls.js treats that as fatal and unrecoverable, so nothing played at all. The master now lists what CAN be spliced onto this session's cut grid — a fact about the source, settled once — while the live judgement stays where it belongs, in `offeredHeights` and in every progress report, which is what the viewer's menu already follows. The variant routes honour the published set too, so a quality switch can no longer meet a 404 on a rung the master named.
|
|
4
|
+
- **Fix**: Peer discovery no longer starves behind name resolution. Node resolves host names on the libuv thread pool, which holds four threads by default; a torrent announces to every tracker in its file at once, so four names resolve and the rest queue — and a tracker that no longer exists holds its thread for the resolver's full ten-second timeout while every announce behind it blows its own fifteen-second deadline. Measured inside the addon container: the ten trackers of one film took **7.58 s** to resolve as a burst and **27-42 ms** each with a larger pool. That film has 517 seeders on a tracker that answers in 50 ms, and it spent eleven minutes with **zero peers** while four other torrents in the same process were fine — they were the ones whose live trackers happened to fall in the first four. The pool is now stated before anything can create it (`services/thread-pool.js`, imported first by the entry point), and a deployment that states its own size is left alone.
|
|
5
|
+
|
|
6
|
+
## 2.36.1
|
|
7
|
+
|
|
8
|
+
- **Fix**: The cut list of a copied picture is built from the picture's own keyframes, and no longer from every entry in the container's table. A Matroska CuePoint belongs to the track named inside it, and RFC 9559 leaves the muxer free to index whichever tracks it likes — both field files index their subtitles as well. Measured over the swarm on 2026-08-18, reading only the head and the table: `Minions.and.Monsters.1080p.mkv` has **2778 video entries, one every 2.002 s, and 4669 more across four subtitle tracks**; `Moana.2.2024.720p.BluRay … MegaPeer.mkv` has **1055 video entries and 5007 across five**. Read without the track, the extra times entered the cut list as though they were keyframes; ffmpeg can only cut a copied picture at a real keyframe at or after the time it is given, so each such cut landed at the next one instead — which is exactly the disagreement the field measured, and why it was always positive: 2.002 s on the first file (its own keyframe spacing), a median of 6.3 s and a worst case of 21 s on the second. The reader now takes the first video track's number from Tracks — already inside the head it fetches, with one short extra read only for a file that keeps Tracks elsewhere — and keeps the entries of that track. Nothing else about the two-read approach changes, and a session costs nothing more. With the fix the same two files read 2778 and 1055 times, all of them keyframes. When the filter leaves NOTHING — a table whose entries name a track number Tracks never declares — the unfiltered table is used rather than no table: that case is this reader failing to recognise the file, and answering with nothing would put an even grid on a copied picture, which is the failure it exists to prevent.
|
|
9
|
+
- **Chore**: `scripts/read-container-index.mjs` reads the index of any `.torrent` over the swarm — two short ranged reads, in memory, no file written — so a claim about what a container says can be checked against a real film in seconds. Written after the measurement above was made by hand three times.
|
|
10
|
+
|
|
1
11
|
## 2.36.0
|
|
2
12
|
|
|
3
13
|
- **New**: The torrent is charged for the megabytes it is measured to be moving, and the price it is charged at no longer contains work that is not the torrent's. Two faults, both visible in one field log from the addon host (2026-08-18): the same session reported **145.4 ms of CPU per MB over 8.7 MB and 23.1 ms per MB over 54 MB**, a sixfold disagreement that followed the size of the interval rather than anything about the torrent — because a process with nothing to do still runs its timers, its tunnel and its session sweeps, and that draw does not shrink when fewer megabytes move. A minimum-megabytes threshold stood against exactly this and did not hold, because a chosen number was standing in for a measured one. The draw is now measured directly, in the intervals where nothing encodes and not one byte moves, and subtracted before the rest is called the torrent's (`services/torrent-cost.js`). What the threshold was reaching for is arrived at from the readings instead: the draw's own readings disagree by a measured amount, that disagreement is worth `scatter × elapsed` seconds over an interval, and a remainder smaller than it measures the wobble in the subtraction rather than the torrent — so a small interval fails on the same arithmetic that lets a large one through, with no size chosen anywhere. The second fault: the price was then charged against the file's own byte rate — what the viewer consumes — so a fully downloaded file moving nothing still paid, and a file being fetched ahead of the viewer, which is how every session starts, paid too little. It is charged against the rate the torrent is measured to be moving, sampled every five seconds per watched torrent and divided among the files of it being read, so two episodes of one pack do not each pay for the whole download.
|
package/bin/cli.js
CHANGED
|
@@ -8,6 +8,11 @@
|
|
|
8
8
|
* automatically on reconnect so the server's in-memory store stays consistent.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
// FIRST, and it must stay first: it sets how many blocking calls this process
|
|
12
|
+
// can have in flight, and a module's imports are evaluated before its body, so
|
|
13
|
+
// anything imported above it would get the default pool. See the file itself
|
|
14
|
+
// for the measurement that made it necessary.
|
|
15
|
+
import "../services/thread-pool.js";
|
|
11
16
|
import { Command } from "commander";
|
|
12
17
|
import crypto from "node:crypto";
|
|
13
18
|
import { spawnSync } from "node:child_process";
|
package/package.json
CHANGED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file What this proxy would cut a file at, read from a real torrent.
|
|
3
|
+
*
|
|
4
|
+
* The keyframe readers take two short ranged reads, so the index of a 7 GB film
|
|
5
|
+
* can be examined over the swarm in seconds without downloading it. Run against
|
|
6
|
+
* a `.torrent` file to see what the container's own table says, per track, and
|
|
7
|
+
* what the reader hands to the cut list.
|
|
8
|
+
*
|
|
9
|
+
* node scripts/read-container-index.mjs path/to/film.torrent [more.torrent …]
|
|
10
|
+
*
|
|
11
|
+
* Developer tool: nothing here runs in a session, and no viewer is involved.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import fs from "node:fs";
|
|
15
|
+
import path from "node:path";
|
|
16
|
+
import MemoryChunkStore from "memory-chunk-store";
|
|
17
|
+
import WebTorrent from "webtorrent";
|
|
18
|
+
import { readMatroskaKeyframeTimes } from "../services/container-index/matroska.js";
|
|
19
|
+
import { readMp4KeyframeTimes } from "../services/container-index/mp4.js";
|
|
20
|
+
import { readAviKeyframeTimes } from "../services/container-index/avi.js";
|
|
21
|
+
|
|
22
|
+
const torrents = process.argv.slice(2);
|
|
23
|
+
if (torrents.length === 0) {
|
|
24
|
+
console.error("usage: node scripts/read-container-index.mjs <file.torrent> [...]");
|
|
25
|
+
process.exit(1);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Collect one byte range of a torrent file.
|
|
30
|
+
*
|
|
31
|
+
* @param {import("webtorrent").TorrentFile} file
|
|
32
|
+
* @param {number} start
|
|
33
|
+
* @param {number} end - Inclusive.
|
|
34
|
+
* @returns {Promise<Buffer>}
|
|
35
|
+
*/
|
|
36
|
+
function readRangeOf(file, start, end) {
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
const chunks = [];
|
|
39
|
+
const stream = file.createReadStream({ start, end });
|
|
40
|
+
stream.on("data", (chunk) => chunks.push(chunk));
|
|
41
|
+
stream.on("end", () => resolve(Buffer.concat(chunks)));
|
|
42
|
+
stream.on("error", reject);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Say what a set of times looks like: how many, where it starts, how far apart.
|
|
48
|
+
*
|
|
49
|
+
* @param {string} label
|
|
50
|
+
* @param {number[] | null} times
|
|
51
|
+
* @returns {void}
|
|
52
|
+
*/
|
|
53
|
+
function describe(label, times) {
|
|
54
|
+
if (!times || times.length === 0) {
|
|
55
|
+
console.log(`${label}: no index`);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const gaps = times.slice(1).map((time, index) => time - times[index]).sort((left, right) => left - right);
|
|
59
|
+
const median = gaps.length > 0 ? gaps[Math.floor(gaps.length / 2)] : 0;
|
|
60
|
+
console.log(
|
|
61
|
+
`${label}: ${times.length} times, first ${times.slice(0, 4).map((time) => time.toFixed(3)).join(" ")}, ` +
|
|
62
|
+
`median gap ${median.toFixed(3)}s, last ${times[times.length - 1].toFixed(3)}`
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
for (const torrentPath of torrents) {
|
|
67
|
+
// uTP is off: binding its socket needs a permission this does not have on
|
|
68
|
+
// every developer machine, and TCP peers are enough to read an index.
|
|
69
|
+
const client = new WebTorrent({ utp: false });
|
|
70
|
+
console.log(`\n=== ${path.basename(torrentPath)} ===`);
|
|
71
|
+
await new Promise((resolve) => {
|
|
72
|
+
// In memory, deliberately. The file store creates the whole file on disk at
|
|
73
|
+
// its full length before a byte of it is wanted — reading the index of four
|
|
74
|
+
// films that way filled 34 GB and then failed with the disk full. Only two
|
|
75
|
+
// short ranges are ever read here, so they can simply be held.
|
|
76
|
+
client.add(fs.readFileSync(torrentPath), { store: MemoryChunkStore }, async (torrent) => {
|
|
77
|
+
try {
|
|
78
|
+
// Nothing is wanted up front; the ranged reads below select what they need.
|
|
79
|
+
torrent.deselect(0, torrent.pieces.length - 1, 0);
|
|
80
|
+
const file = torrent.files
|
|
81
|
+
.filter((candidate) => /\.(mkv|mp4|avi)$/i.test(candidate.name))
|
|
82
|
+
.sort((left, right) => right.length - left.length)[0];
|
|
83
|
+
if (!file) {
|
|
84
|
+
console.log("no video file in this torrent");
|
|
85
|
+
return resolve();
|
|
86
|
+
}
|
|
87
|
+
console.log(`file: ${file.name} (${(file.length / 1e9).toFixed(2)} GB)`);
|
|
88
|
+
const readRange = async (start, end) => {
|
|
89
|
+
const last = Math.min(end, file.length - 1);
|
|
90
|
+
return start > last ? null : readRangeOf(file, start, last);
|
|
91
|
+
};
|
|
92
|
+
if (/\.mkv$/i.test(file.name)) {
|
|
93
|
+
describe("matroska", await readMatroskaKeyframeTimes(readRange, file.length));
|
|
94
|
+
} else if (/\.mp4$/i.test(file.name)) {
|
|
95
|
+
describe("mp4", await readMp4KeyframeTimes(readRange, file.length));
|
|
96
|
+
} else {
|
|
97
|
+
describe("avi", await readAviKeyframeTimes(readRange, file.length));
|
|
98
|
+
}
|
|
99
|
+
} catch (error) {
|
|
100
|
+
console.log(`failed: ${error?.message ?? error}`);
|
|
101
|
+
} finally {
|
|
102
|
+
resolve();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
await new Promise((done) => client.destroy(done));
|
|
107
|
+
}
|
|
108
|
+
process.exit(0);
|
|
@@ -28,6 +28,14 @@ const ID_TIMESTAMP_SCALE = 0x2ad7b1;
|
|
|
28
28
|
const ID_CUES = 0x1c53bb6b;
|
|
29
29
|
const ID_CUE_POINT = 0xbb;
|
|
30
30
|
const ID_CUE_TIME = 0xb3;
|
|
31
|
+
const ID_CUE_TRACK_POSITIONS = 0xb7;
|
|
32
|
+
const ID_CUE_TRACK = 0xf7;
|
|
33
|
+
const ID_TRACKS = 0x1654ae6b;
|
|
34
|
+
const ID_TRACK_ENTRY = 0xae;
|
|
35
|
+
const ID_TRACK_NUMBER = 0xd7;
|
|
36
|
+
const ID_TRACK_TYPE = 0x83;
|
|
37
|
+
// TrackType 1 is video; 2 is audio, 17 subtitles, and the rest are rarer still.
|
|
38
|
+
const TRACK_TYPE_VIDEO = 1;
|
|
31
39
|
|
|
32
40
|
// How much of the file start to read. Must cover the EBML header, the SeekHead
|
|
33
41
|
// and Info; 64 KB is generous for every real muxer (the file measured needed
|
|
@@ -36,6 +44,10 @@ const HEAD_BYTES = 64 * 1024;
|
|
|
36
44
|
// Cap on the Cues read. A two-hour film indexes to tens of KB; anything beyond
|
|
37
45
|
// this is not a normal index and not worth pulling over a torrent.
|
|
38
46
|
const MAX_CUES_BYTES = 8 * 1024 * 1024;
|
|
47
|
+
// Cap on a Tracks read, for the rare file whose Tracks element sits outside the
|
|
48
|
+
// head window. Track entries are small, so a file with dozens of them still
|
|
49
|
+
// fits well inside this.
|
|
50
|
+
const MAX_TRACKS_BYTES = 1024 * 1024;
|
|
39
51
|
// Matroska's default timestamp scale (nanoseconds per tick) when Info omits it.
|
|
40
52
|
const DEFAULT_TIMESTAMP_SCALE = 1_000_000;
|
|
41
53
|
|
|
@@ -121,13 +133,64 @@ function readTimestampScale(head, segmentDataOffset) {
|
|
|
121
133
|
}
|
|
122
134
|
|
|
123
135
|
/**
|
|
124
|
-
*
|
|
136
|
+
* The number of the first video track, from a Tracks payload.
|
|
137
|
+
*
|
|
138
|
+
* The FIRST one, because that is the track ffmpeg is told to copy (`0:v:0`).
|
|
139
|
+
*
|
|
140
|
+
* @param {Buffer} buffer
|
|
141
|
+
* @param {{ dataOffset: number, size: number }} tracks
|
|
142
|
+
* @returns {number | null}
|
|
143
|
+
*/
|
|
144
|
+
function readVideoTrackNumber(buffer, tracks) {
|
|
145
|
+
const tracksEnd = Math.min(buffer.length, tracks.dataOffset + tracks.size);
|
|
146
|
+
for (const entry of iterateElements(buffer, tracks.dataOffset, tracksEnd)) {
|
|
147
|
+
if (entry.id !== ID_TRACK_ENTRY) {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const entryEnd = Math.min(tracksEnd, entry.dataOffset + entry.size);
|
|
151
|
+
let number = null;
|
|
152
|
+
let type = null;
|
|
153
|
+
for (const field of iterateElements(buffer, entry.dataOffset, entryEnd)) {
|
|
154
|
+
if (field.id === ID_TRACK_NUMBER) {
|
|
155
|
+
number = readUint(buffer, field.dataOffset, field.size);
|
|
156
|
+
} else if (field.id === ID_TRACK_TYPE) {
|
|
157
|
+
type = readUint(buffer, field.dataOffset, field.size);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (number !== null && type === TRACK_TYPE_VIDEO) {
|
|
161
|
+
return number;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Cue times (seconds, ascending) of ONE track, from a Cues payload.
|
|
169
|
+
*
|
|
170
|
+
* The track is the whole point, and leaving it out is what this reader got
|
|
171
|
+
* wrong until 2026-08-18. A CuePoint belongs to the track named inside its
|
|
172
|
+
* CueTrackPositions, and a muxer indexes whatever tracks it likes: RFC 9559
|
|
173
|
+
* says each keyframe of a video track SHOULD be referenced, and that the Cues
|
|
174
|
+
* Element "can be used to index every single timestamp of every Block or they
|
|
175
|
+
* can be indexed selectively". Both field files index their SUBTITLE tracks as
|
|
176
|
+
* well — `Minions.and.Monsters.1080p.mkv` has 2778 video entries every 2.002 s
|
|
177
|
+
* plus 4669 across four subtitle tracks; `Moana.2 … MegaPeer.mkv` has 1055
|
|
178
|
+
* video entries plus 5007 across five.
|
|
179
|
+
*
|
|
180
|
+
* Read without the track, those extra times enter the cut list as though they
|
|
181
|
+
* were keyframes. ffmpeg can only cut a COPIED picture at a real keyframe at or
|
|
182
|
+
* after the time it is given, so every cut asked for at one of them lands late
|
|
183
|
+
* — which is exactly what the field measured: on the first file every deviation
|
|
184
|
+
* was 2.002 s, that file's own keyframe spacing, and on the second the median
|
|
185
|
+
* was 6.3 s with a worst case of 21 s. Never once negative.
|
|
125
186
|
*
|
|
126
187
|
* @param {Buffer} cues
|
|
127
188
|
* @param {number} timestampScale - Nanoseconds per tick.
|
|
189
|
+
* @param {number | null} trackNumber - Null keeps every entry, which is right
|
|
190
|
+
* only for a file that indexes one track.
|
|
128
191
|
* @returns {number[]}
|
|
129
192
|
*/
|
|
130
|
-
function readCueTimes(cues, timestampScale) {
|
|
193
|
+
function readCueTimes(cues, timestampScale, trackNumber) {
|
|
131
194
|
const times = [];
|
|
132
195
|
const secondsPerTick = timestampScale / 1e9;
|
|
133
196
|
for (const point of iterateElements(cues)) {
|
|
@@ -135,11 +198,26 @@ function readCueTimes(cues, timestampScale) {
|
|
|
135
198
|
continue;
|
|
136
199
|
}
|
|
137
200
|
const pointEnd = Math.min(cues.length, point.dataOffset + point.size);
|
|
201
|
+
let time = null;
|
|
202
|
+
let belongsToTrack = trackNumber === null;
|
|
138
203
|
for (const field of iterateElements(cues, point.dataOffset, pointEnd)) {
|
|
139
204
|
if (field.id === ID_CUE_TIME) {
|
|
140
|
-
|
|
141
|
-
|
|
205
|
+
time = readUint(cues, field.dataOffset, field.size) * secondsPerTick;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (field.id !== ID_CUE_TRACK_POSITIONS || belongsToTrack) {
|
|
209
|
+
continue;
|
|
142
210
|
}
|
|
211
|
+
const positionsEnd = Math.min(pointEnd, field.dataOffset + field.size);
|
|
212
|
+
for (const inner of iterateElements(cues, field.dataOffset, positionsEnd)) {
|
|
213
|
+
if (inner.id === ID_CUE_TRACK && readUint(cues, inner.dataOffset, inner.size) === trackNumber) {
|
|
214
|
+
belongsToTrack = true;
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (time !== null && belongsToTrack) {
|
|
220
|
+
times.push(time);
|
|
143
221
|
}
|
|
144
222
|
}
|
|
145
223
|
times.sort((left, right) => left - right);
|
|
@@ -195,6 +273,64 @@ export async function readMatroskaKeyframeTimes(readRange, fileSize) {
|
|
|
195
273
|
const payloadEnd = Math.min(cuesChunk.length, cuesElement.dataOffset + cuesElement.size);
|
|
196
274
|
const payload = cuesChunk.subarray(cuesElement.dataOffset, payloadEnd);
|
|
197
275
|
|
|
198
|
-
|
|
199
|
-
|
|
276
|
+
// Whose entries to keep. Tracks sits near the head and is normally inside the
|
|
277
|
+
// bytes already fetched; when it is not, SeekHead says where it is and one
|
|
278
|
+
// more short read gets it. Nothing is fetched twice and nothing is scanned.
|
|
279
|
+
const videoTrack = await readVideoTrack(readRange, head, seekHead, fileSize);
|
|
280
|
+
const timestampScale = readTimestampScale(head, seekHead.segmentDataOffset);
|
|
281
|
+
const times = readCueTimes(payload, timestampScale, videoTrack);
|
|
282
|
+
if (times.length > 0) {
|
|
283
|
+
return times;
|
|
284
|
+
}
|
|
285
|
+
if (videoTrack === null) {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
// The filter left nothing, and that is not an answer about the file: a table
|
|
289
|
+
// exists, and this reader simply failed to recognise which of its entries
|
|
290
|
+
// belong to the picture — a track numbered one way in Tracks and another in
|
|
291
|
+
// the cue points, or an entry with no CueTrack at all. Returning null here
|
|
292
|
+
// would put an EVEN grid on a copied picture, which is the failure this
|
|
293
|
+
// module exists to prevent, so the unfiltered table is used instead: less
|
|
294
|
+
// exact than the picture's own keyframes, better than a grid that has nothing
|
|
295
|
+
// to do with the file.
|
|
296
|
+
const unfiltered = readCueTimes(payload, timestampScale, null);
|
|
297
|
+
return unfiltered.length > 0 ? unfiltered : null;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* The video track's number — from the head when it is there, and from one extra
|
|
302
|
+
* short read when it is not.
|
|
303
|
+
*
|
|
304
|
+
* @param {(start: number, end: number) => Promise<Buffer | null>} readRange
|
|
305
|
+
* @param {Buffer} head
|
|
306
|
+
* @param {{ segmentDataOffset: number, entries: Map<number, number> }} seekHead
|
|
307
|
+
* @param {number} fileSize
|
|
308
|
+
* @returns {Promise<number | null>} Null when Tracks cannot be read at all, and
|
|
309
|
+
* then every cue entry is kept — right for a file that indexes only its
|
|
310
|
+
* picture, wrong for one that does not, and nothing here can tell them apart.
|
|
311
|
+
* Refusing the index instead would put an even grid on a copied picture,
|
|
312
|
+
* which is the failure this reader exists to prevent.
|
|
313
|
+
*/
|
|
314
|
+
async function readVideoTrack(readRange, head, seekHead, fileSize) {
|
|
315
|
+
const inHead = findElement(head, ID_TRACKS, [], seekHead.segmentDataOffset);
|
|
316
|
+
if (inHead && inHead.dataOffset + inHead.size <= head.length) {
|
|
317
|
+
return readVideoTrackNumber(head, inHead);
|
|
318
|
+
}
|
|
319
|
+
const relative = seekHead.entries.get(ID_TRACKS);
|
|
320
|
+
if (relative === undefined) {
|
|
321
|
+
return null;
|
|
322
|
+
}
|
|
323
|
+
const offset = seekHead.segmentDataOffset + relative;
|
|
324
|
+
if (!Number.isFinite(offset) || offset <= 0 || offset >= fileSize) {
|
|
325
|
+
return null;
|
|
326
|
+
}
|
|
327
|
+
const chunk = await readRange(offset, Math.min(fileSize - 1, offset + MAX_TRACKS_BYTES));
|
|
328
|
+
if (!chunk || chunk.length === 0) {
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
const element = [...iterateElements(chunk, 0, chunk.length)][0];
|
|
332
|
+
if (!element || element.id !== ID_TRACKS) {
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
return readVideoTrackNumber(chunk, { dataOffset: element.dataOffset, size: element.size });
|
|
200
336
|
}
|
|
@@ -2065,7 +2065,7 @@ export class HlsSessionManager {
|
|
|
2065
2065
|
// flip the arrangement under a stream that is playing.
|
|
2066
2066
|
session.audioSeparate = inheritedAudioSeparate === null
|
|
2067
2067
|
? audioRenditions === true &&
|
|
2068
|
-
this.#
|
|
2068
|
+
this.#splicableHeights(session).length >= 2 &&
|
|
2069
2069
|
this.#audioRenditionsOf(session).length > 0
|
|
2070
2070
|
: inheritedAudioSeparate === true;
|
|
2071
2071
|
|
|
@@ -5379,6 +5379,42 @@ export class HlsSessionManager {
|
|
|
5379
5379
|
return session.variantHeight;
|
|
5380
5380
|
}
|
|
5381
5381
|
|
|
5382
|
+
/**
|
|
5383
|
+
* The heights this file's variants CAN be spliced at — a fact about the
|
|
5384
|
+
* source and the cut grid, settled once and never moved.
|
|
5385
|
+
*
|
|
5386
|
+
* Separate from {@link #variantHeights}, which answers a different question:
|
|
5387
|
+
* which of them are worth OFFERING to the viewer right now, on a machine
|
|
5388
|
+
* whose load moves every five seconds. Both were the same list until
|
|
5389
|
+
* 2026-08-18, and that is what broke playback outright: the browser is told
|
|
5390
|
+
* at session creation that a master playlist exists, and 192 ms later — after
|
|
5391
|
+
* the session's own encoder had started and the first supply reading had
|
|
5392
|
+
* arrived — the live list had fallen from five rungs to one, `buildMaster
|
|
5393
|
+
* Playlist` returned null for having fewer than two, and the master answered
|
|
5394
|
+
* 404 to the very session that had just published it. hls.js treats that as
|
|
5395
|
+
* fatal and unrecoverable, so nothing played at all (session `4ef731d8`,
|
|
5396
|
+
* "Moana (2016).mkv", 17:43:01).
|
|
5397
|
+
*
|
|
5398
|
+
* A live figure may decide what to offer. It may not decide whether a
|
|
5399
|
+
* published document exists.
|
|
5400
|
+
*
|
|
5401
|
+
* @param {HlsSession} session
|
|
5402
|
+
* @returns {number[]} Largest first.
|
|
5403
|
+
*/
|
|
5404
|
+
#splicableHeights(session) {
|
|
5405
|
+
const owner = this.#baseOf(session);
|
|
5406
|
+
if (Array.isArray(owner.splicableHeights)) {
|
|
5407
|
+
return owner.splicableHeights;
|
|
5408
|
+
}
|
|
5409
|
+
const heights = new Set(variantHeightsFor(Number(owner.sourceHeight) || 0));
|
|
5410
|
+
const own = this.variantHeightOf(owner);
|
|
5411
|
+
if (own > 0) {
|
|
5412
|
+
heights.add(own);
|
|
5413
|
+
}
|
|
5414
|
+
owner.splicableHeights = [...heights].sort((left, right) => right - left);
|
|
5415
|
+
return owner.splicableHeights;
|
|
5416
|
+
}
|
|
5417
|
+
|
|
5382
5418
|
/**
|
|
5383
5419
|
* The heights this session's file is offered at, largest first.
|
|
5384
5420
|
*
|
|
@@ -6540,8 +6576,11 @@ export class HlsSessionManager {
|
|
|
6540
6576
|
return null;
|
|
6541
6577
|
}
|
|
6542
6578
|
// Only the heights the master offers. Anything else is a made-up request,
|
|
6543
|
-
// and honouring it would let a client start encoder runs at will.
|
|
6544
|
-
|
|
6579
|
+
// and honouring it would let a client start encoder runs at will. The
|
|
6580
|
+
// MASTER's list, not the live one: a rung is published for the session's
|
|
6581
|
+
// whole life, and refusing what we published is how a quality switch became
|
|
6582
|
+
// a 404 storm across every level.
|
|
6583
|
+
if (!this.#splicableHeights(base).includes(height)) {
|
|
6545
6584
|
return null;
|
|
6546
6585
|
}
|
|
6547
6586
|
if (height === this.variantHeightOf(base)) {
|
|
@@ -6690,7 +6729,7 @@ export class HlsSessionManager {
|
|
|
6690
6729
|
if (!isPlaylist && !isInit && !isSegment) {
|
|
6691
6730
|
return { sessionId: null };
|
|
6692
6731
|
}
|
|
6693
|
-
if (!this.#
|
|
6732
|
+
if (!this.#splicableHeights(base).includes(height)) {
|
|
6694
6733
|
return { sessionId: null };
|
|
6695
6734
|
}
|
|
6696
6735
|
// Answered from the base, and no encoder is started for it. Every variant of
|
|
@@ -6945,7 +6984,11 @@ export class HlsSessionManager {
|
|
|
6945
6984
|
return null;
|
|
6946
6985
|
}
|
|
6947
6986
|
const sourceHeight = Number(session.sourceHeight) || 0;
|
|
6948
|
-
|
|
6987
|
+
// What CAN be spliced, not what is worth offering this second. The live
|
|
6988
|
+
// judgement travels in `offeredHeights` and in every progress report, which
|
|
6989
|
+
// is what the viewer's menu follows; letting it decide the master's
|
|
6990
|
+
// existence made a live session answer 404 to its own published address.
|
|
6991
|
+
const rungs = this.#splicableHeights(session);
|
|
6949
6992
|
if (rungs.length < 2) {
|
|
6950
6993
|
return null;
|
|
6951
6994
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file How many blocking calls this process can have in flight — set before
|
|
3
|
+
* anything makes one.
|
|
4
|
+
*
|
|
5
|
+
* Node resolves host names with `getaddrinfo`, which runs on the libuv thread
|
|
6
|
+
* pool, and that pool holds FOUR threads by default. A torrent announces to
|
|
7
|
+
* every tracker in its file at once — ten or thirteen of them — so four names
|
|
8
|
+
* are resolved and the rest queue; a tracker that no longer exists holds its
|
|
9
|
+
* thread for the resolver's full ten-second timeout, and every announce behind
|
|
10
|
+
* it blows its own fifteen-second deadline.
|
|
11
|
+
*
|
|
12
|
+
* Measured inside the addon container on 2026-08-18, resolving the ten trackers
|
|
13
|
+
* of one film: as a burst on the default pool, two names answered in 30-40 ms
|
|
14
|
+
* and the other seven took **7.58 s**; with a larger pool every live name
|
|
15
|
+
* answered in **27-42 ms**. The film itself had 517 seeders on a tracker that
|
|
16
|
+
* answers in 50 ms, and spent eleven minutes with zero peers, because every
|
|
17
|
+
* announce — UDP and HTTP alike — timed out waiting for a name.
|
|
18
|
+
*
|
|
19
|
+
* Sized to hold several torrents' announce lists at once, since the same pool
|
|
20
|
+
* also serves this process's file reads. Idle threads cost memory and nothing
|
|
21
|
+
* else, and a deployment that states its own size is left alone.
|
|
22
|
+
*
|
|
23
|
+
* Imported FIRST by the entry point, because a module's imports are evaluated
|
|
24
|
+
* before its body: written as a statement in `cli.js` this would run after
|
|
25
|
+
* every other import had already had its chance to create the pool.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
if (!process.env.UV_THREADPOOL_SIZE) {
|
|
29
|
+
process.env.UV_THREADPOOL_SIZE = "64";
|
|
30
|
+
}
|
package/test/decode-cost.test.js
CHANGED
|
@@ -288,7 +288,7 @@ test("the source's decode figures come off the probe, or not at all", () => {
|
|
|
288
288
|
assert.equal(sourceDecodeCharacteristics(null), null);
|
|
289
289
|
});
|
|
290
290
|
|
|
291
|
-
test("the
|
|
291
|
+
test("the OFFER drops the rungs the host cannot hold, and the master keeps addressing them", async (t) => {
|
|
292
292
|
const dirPath = await mkdtemp(path.join(os.tmpdir(), "decode-cost-"));
|
|
293
293
|
const manager = new HlsSessionManager({
|
|
294
294
|
enabled: true,
|
|
@@ -339,15 +339,22 @@ test("the master playlist drops the rungs the host cannot hold", async (t) => {
|
|
|
339
339
|
};
|
|
340
340
|
manager.sessionsById.set(session.id, session);
|
|
341
341
|
|
|
342
|
-
assert.equal(
|
|
343
|
-
manager.buildMasterPlaylist(session.id),
|
|
344
|
-
null,
|
|
345
|
-
"every rung under the copy runs below realtime here, so there is nothing to switch to"
|
|
346
|
-
);
|
|
347
342
|
assert.deepEqual(
|
|
348
343
|
manager.offeredHeights(session),
|
|
349
344
|
[1080],
|
|
350
|
-
"
|
|
345
|
+
"every rung under the copy runs below realtime here, so there is nothing to switch to"
|
|
346
|
+
);
|
|
347
|
+
// The master is NOT that answer. It says which rungs can be spliced onto this
|
|
348
|
+
// cut grid, which is a fact about the file, and it has to hold still for the
|
|
349
|
+
// session's life: the browser is handed its address at creation, and a live
|
|
350
|
+
// figure that withdrew it left a session answering 404 to itself (field
|
|
351
|
+
// 2026-08-18, "Moana (2016).mkv" — nothing played at all).
|
|
352
|
+
const weakMaster = manager.buildMasterPlaylist(session.id);
|
|
353
|
+
assert.ok(weakMaster, "published once, whatever the host is managing this second");
|
|
354
|
+
assert.deepEqual(
|
|
355
|
+
[...weakMaster.matchAll(/^v\/(\d+)\/index\.m3u8$/gm)].map((match) => Number(match[1])),
|
|
356
|
+
[1080, 720, 540, 480, 360, 240],
|
|
357
|
+
"the ladder of the source, addressable — the menu the viewer sees is offeredHeights"
|
|
351
358
|
);
|
|
352
359
|
|
|
353
360
|
// A host with a little more encoder keeps the rungs it can actually hold. A
|
|
@@ -355,11 +362,18 @@ test("the master playlist drops the rungs the host cannot hold", async (t) => {
|
|
|
355
362
|
manager.softwarePresetBenchmark = [{ preset: "ultrafast", pixelsPerSec: 12e6 }];
|
|
356
363
|
const stronger = { ...session, id: "dddddddd-eeee-ffff-0000-111111111111", offeredHeightsCache: undefined };
|
|
357
364
|
manager.sessionsById.set(stronger.id, stronger);
|
|
365
|
+
assert.deepEqual(
|
|
366
|
+
manager.offeredHeights(stronger),
|
|
367
|
+
[1080, 360, 240],
|
|
368
|
+
"nothing is known about this swarm, so the bar is realtime"
|
|
369
|
+
);
|
|
358
370
|
const master = manager.buildMasterPlaylist(stronger.id);
|
|
359
|
-
assert.ok(master, "1080p copied plus
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
371
|
+
assert.ok(master, "1080p copied plus every rung that can be spliced beside it");
|
|
372
|
+
assert.deepEqual(
|
|
373
|
+
[...master.matchAll(/^v\/(\d+)\/index\.m3u8$/gm)].map((match) => Number(match[1])),
|
|
374
|
+
[1080, 720, 540, 480, 360, 240],
|
|
375
|
+
"the same published set as before: what the host manages is the offer's business, not the document's"
|
|
376
|
+
);
|
|
363
377
|
|
|
364
378
|
// The same host, once the reader has measured what this file's supply
|
|
365
379
|
// demands: waits arriving as they did on the field torrent of 2026-08-17 ask
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The container's table names a track, and only the picture's entries are
|
|
3
|
+
* cut points.
|
|
4
|
+
*
|
|
5
|
+
* Measured 2026-08-18 on the two files the field sessions were recorded from:
|
|
6
|
+
* `Minions.and.Monsters.1080p.mkv` carries 2778 cue entries for its video track
|
|
7
|
+
* — one every 2.002 s — and 4669 more across four subtitle tracks;
|
|
8
|
+
* `Moana.2 … MegaPeer.mkv` carries 1055 for video and 5007 across five subtitle
|
|
9
|
+
* tracks. Read without the track, both sets went into the cut list together,
|
|
10
|
+
* and ffmpeg — which can only cut a copied picture at a real keyframe at or
|
|
11
|
+
* after the time it is asked for — moved every such cut forward to the next
|
|
12
|
+
* keyframe. That is the whole of the disagreement the field reported: 2.002 s
|
|
13
|
+
* on the first file, a median of 6.3 s and a worst case of 21 s on the second,
|
|
14
|
+
* and never once negative.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import test from "node:test";
|
|
18
|
+
import assert from "node:assert/strict";
|
|
19
|
+
import { readMatroskaKeyframeTimes } from "../services/container-index/matroska.js";
|
|
20
|
+
|
|
21
|
+
const ID_EBML = 0x1a45dfa3;
|
|
22
|
+
const ID_SEGMENT = 0x18538067;
|
|
23
|
+
const ID_SEEK_HEAD = 0x114d9b74;
|
|
24
|
+
const ID_SEEK = 0x4dbb;
|
|
25
|
+
const ID_SEEK_ID = 0x53ab;
|
|
26
|
+
const ID_SEEK_POSITION = 0x53ac;
|
|
27
|
+
const ID_INFO = 0x1549a966;
|
|
28
|
+
const ID_TIMESTAMP_SCALE = 0x2ad7b1;
|
|
29
|
+
const ID_TRACKS = 0x1654ae6b;
|
|
30
|
+
const ID_TRACK_ENTRY = 0xae;
|
|
31
|
+
const ID_TRACK_NUMBER = 0xd7;
|
|
32
|
+
const ID_TRACK_TYPE = 0x83;
|
|
33
|
+
const ID_CUES = 0x1c53bb6b;
|
|
34
|
+
const ID_CUE_POINT = 0xbb;
|
|
35
|
+
const ID_CUE_TIME = 0xb3;
|
|
36
|
+
const ID_CUE_TRACK_POSITIONS = 0xb7;
|
|
37
|
+
const ID_CUE_TRACK = 0xf7;
|
|
38
|
+
const ID_CUE_CLUSTER_POSITION = 0xf1;
|
|
39
|
+
|
|
40
|
+
/** An element id, as the bytes the specification gives it. */
|
|
41
|
+
function idBytes(id) {
|
|
42
|
+
const bytes = [];
|
|
43
|
+
let rest = id;
|
|
44
|
+
while (rest > 0) {
|
|
45
|
+
bytes.unshift(rest & 0xff);
|
|
46
|
+
rest = Math.floor(rest / 256);
|
|
47
|
+
}
|
|
48
|
+
return Buffer.from(bytes);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A size, as a four-byte EBML variable-length integer. */
|
|
52
|
+
function sizeBytes(size) {
|
|
53
|
+
const buffer = Buffer.alloc(4);
|
|
54
|
+
buffer.writeUInt32BE(size, 0);
|
|
55
|
+
buffer[0] |= 0x10;
|
|
56
|
+
return buffer;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function element(id, payload) {
|
|
60
|
+
return Buffer.concat([idBytes(id), sizeBytes(payload.length), payload]);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** An unsigned value, in as few bytes as it needs. */
|
|
64
|
+
function uintElement(id, value) {
|
|
65
|
+
const bytes = [];
|
|
66
|
+
let rest = value;
|
|
67
|
+
do {
|
|
68
|
+
bytes.unshift(rest & 0xff);
|
|
69
|
+
rest = Math.floor(rest / 256);
|
|
70
|
+
} while (rest > 0);
|
|
71
|
+
return element(id, Buffer.from(bytes));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function cuePoint(timeMs, tracks) {
|
|
75
|
+
return element(ID_CUE_POINT, Buffer.concat([
|
|
76
|
+
uintElement(ID_CUE_TIME, timeMs),
|
|
77
|
+
...tracks.map((track) => element(ID_CUE_TRACK_POSITIONS, Buffer.concat([
|
|
78
|
+
uintElement(ID_CUE_TRACK, track),
|
|
79
|
+
uintElement(ID_CUE_CLUSTER_POSITION, 4096)
|
|
80
|
+
])))
|
|
81
|
+
]));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A file with the shape the field files have: one picture, one set of subtitles,
|
|
86
|
+
* and a table that indexes both.
|
|
87
|
+
*
|
|
88
|
+
* @param {{ withTracks?: boolean }} [options]
|
|
89
|
+
* @returns {Buffer}
|
|
90
|
+
*/
|
|
91
|
+
function buildFile({ withTracks = true, cueTrack = null } = {}) {
|
|
92
|
+
const info = element(ID_INFO, uintElement(ID_TIMESTAMP_SCALE, 1_000_000));
|
|
93
|
+
const tracks = element(ID_TRACKS, Buffer.concat([
|
|
94
|
+
element(ID_TRACK_ENTRY, Buffer.concat([
|
|
95
|
+
uintElement(ID_TRACK_NUMBER, 1),
|
|
96
|
+
uintElement(ID_TRACK_TYPE, 1) // video
|
|
97
|
+
])),
|
|
98
|
+
element(ID_TRACK_ENTRY, Buffer.concat([
|
|
99
|
+
uintElement(ID_TRACK_NUMBER, 2),
|
|
100
|
+
uintElement(ID_TRACK_TYPE, 17) // subtitles
|
|
101
|
+
]))
|
|
102
|
+
]));
|
|
103
|
+
const forPicture = cueTrack ?? 1;
|
|
104
|
+
const forSubtitles = cueTrack ?? 2;
|
|
105
|
+
const cues = element(ID_CUES, Buffer.concat([
|
|
106
|
+
cuePoint(0, [forPicture]),
|
|
107
|
+
cuePoint(1070, [forSubtitles]),
|
|
108
|
+
cuePoint(2002, [forPicture]),
|
|
109
|
+
cuePoint(3141, [forSubtitles]),
|
|
110
|
+
cuePoint(4004, [forPicture])
|
|
111
|
+
]));
|
|
112
|
+
|
|
113
|
+
const seekEntry = (targetId, position) => element(ID_SEEK, Buffer.concat([
|
|
114
|
+
element(ID_SEEK_ID, idBytes(targetId)),
|
|
115
|
+
element(ID_SEEK_POSITION, (() => {
|
|
116
|
+
const buffer = Buffer.alloc(4);
|
|
117
|
+
buffer.writeUInt32BE(position, 0);
|
|
118
|
+
return buffer;
|
|
119
|
+
})())
|
|
120
|
+
]));
|
|
121
|
+
// Positions are relative to the Segment's payload, so the SeekHead has to be
|
|
122
|
+
// measured before they can be stated. Its own length does not change when the
|
|
123
|
+
// placeholders become real values: every size and position here is written at
|
|
124
|
+
// a fixed width.
|
|
125
|
+
const draft = element(ID_SEEK_HEAD, Buffer.concat([
|
|
126
|
+
seekEntry(ID_INFO, 0),
|
|
127
|
+
...(withTracks ? [seekEntry(ID_TRACKS, 0)] : []),
|
|
128
|
+
seekEntry(ID_CUES, 0)
|
|
129
|
+
]));
|
|
130
|
+
const infoAt = draft.length;
|
|
131
|
+
const tracksAt = infoAt + info.length;
|
|
132
|
+
const cuesAt = withTracks ? tracksAt + tracks.length : infoAt + info.length;
|
|
133
|
+
const seekHead = element(ID_SEEK_HEAD, Buffer.concat([
|
|
134
|
+
seekEntry(ID_INFO, infoAt),
|
|
135
|
+
...(withTracks ? [seekEntry(ID_TRACKS, tracksAt)] : []),
|
|
136
|
+
seekEntry(ID_CUES, cuesAt)
|
|
137
|
+
]));
|
|
138
|
+
|
|
139
|
+
const segmentPayload = Buffer.concat(
|
|
140
|
+
withTracks ? [seekHead, info, tracks, cues] : [seekHead, info, cues]
|
|
141
|
+
);
|
|
142
|
+
return Buffer.concat([
|
|
143
|
+
element(ID_EBML, Buffer.from([0x42, 0x86, 0x81, 0x01])),
|
|
144
|
+
element(ID_SEGMENT, segmentPayload)
|
|
145
|
+
]);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function readerOver(file) {
|
|
149
|
+
return async (start, end) => {
|
|
150
|
+
const last = Math.min(end, file.length - 1);
|
|
151
|
+
return start > last ? null : file.subarray(start, last + 1);
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
test("only the picture's entries become cut times", async () => {
|
|
156
|
+
const file = buildFile();
|
|
157
|
+
|
|
158
|
+
const times = await readMatroskaKeyframeTimes(readerOver(file), file.length);
|
|
159
|
+
|
|
160
|
+
assert.deepEqual(
|
|
161
|
+
times.map((time) => Number(time.toFixed(3))),
|
|
162
|
+
[0, 2.002, 4.004],
|
|
163
|
+
"the subtitle entries at 1.070 and 3.141 are not keyframes, and a cut asked for there lands late"
|
|
164
|
+
);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("a table whose entries name no known track is still used", async () => {
|
|
168
|
+
// The cue points reference track 9, which Tracks never declares. Filtering
|
|
169
|
+
// leaves nothing — and returning nothing would put an even grid on a copied
|
|
170
|
+
// picture, the failure this reader exists to prevent.
|
|
171
|
+
const file = buildFile({ cueTrack: 9 });
|
|
172
|
+
|
|
173
|
+
const times = await readMatroskaKeyframeTimes(readerOver(file), file.length);
|
|
174
|
+
|
|
175
|
+
assert.deepEqual(
|
|
176
|
+
times.map((time) => Number(time.toFixed(3))),
|
|
177
|
+
[0, 1.07, 2.002, 3.141, 4.004],
|
|
178
|
+
"an unrecognised table beats no table at all"
|
|
179
|
+
);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("a file whose tracks cannot be read keeps every entry", async () => {
|
|
183
|
+
const file = buildFile({ withTracks: false });
|
|
184
|
+
|
|
185
|
+
const times = await readMatroskaKeyframeTimes(readerOver(file), file.length);
|
|
186
|
+
|
|
187
|
+
assert.deepEqual(
|
|
188
|
+
times.map((time) => Number(time.toFixed(3))),
|
|
189
|
+
[0, 1.07, 2.002, 3.141, 4.004],
|
|
190
|
+
"with nothing to tell the tracks apart, the old behaviour is the only one available"
|
|
191
|
+
);
|
|
192
|
+
});
|
|
@@ -813,3 +813,49 @@ test("a quality step being warmed is not refused by its own cost", async (t) =>
|
|
|
813
813
|
"offer by the act of warming it — and its next segment 404s on a stream that is playing"
|
|
814
814
|
);
|
|
815
815
|
});
|
|
816
|
+
|
|
817
|
+
test("the master survives a live offer that has collapsed to one rung", async (t) => {
|
|
818
|
+
// The field case of 2026-08-18, in the smallest form that reproduces it: a
|
|
819
|
+
// host too slow for any re-encoded rung, and a swarm whose interruptions
|
|
820
|
+
// demand far more than realtime. The live offer then holds only the height an
|
|
821
|
+
// encoder is already producing — and until this test existed, that made
|
|
822
|
+
// `buildMasterPlaylist` answer null and the route answer 404 to a session
|
|
823
|
+
// that had just published the address.
|
|
824
|
+
const dirPath = await mkdtemp(path.join(os.tmpdir(), "quality-variants-collapse-"));
|
|
825
|
+
const manager = new HlsSessionManager({
|
|
826
|
+
enabled: true,
|
|
827
|
+
ffmpegBin: "ffmpeg",
|
|
828
|
+
localBindHost: "127.0.0.1",
|
|
829
|
+
localPort: 9090,
|
|
830
|
+
// A megapixel a second: every rung below the source costs more than the
|
|
831
|
+
// machine has.
|
|
832
|
+
softwarePresetBenchmark: [{ preset: "veryfast", pixelsPerSec: 1_000_000 }],
|
|
833
|
+
decodeCostModel: { pixelTerm: 0.01, bitrateTerm: 0, constantTerm: 0 }
|
|
834
|
+
});
|
|
835
|
+
const base = fakeSession({ id: BASE_ID, encodeHeight: 812, dirPath });
|
|
836
|
+
base.sourceDecode = { megapixelsPerSecond: 50, megabitsPerSecond: 10 };
|
|
837
|
+
// What this file's own reader measured: a step must run at eight times
|
|
838
|
+
// realtime to survive this swarm.
|
|
839
|
+
base.supplyFigures = { requiredSpeed: 8 };
|
|
840
|
+
manager.sessionsById.set(BASE_ID, base);
|
|
841
|
+
t.after(async () => {
|
|
842
|
+
await manager.disposeAll();
|
|
843
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
844
|
+
});
|
|
845
|
+
|
|
846
|
+
assert.deepEqual(
|
|
847
|
+
manager.offeredHeights(base),
|
|
848
|
+
[812],
|
|
849
|
+
"the live judgement is unchanged: nothing but the running height is worth offering"
|
|
850
|
+
);
|
|
851
|
+
|
|
852
|
+
const master = manager.buildMasterPlaylist(BASE_ID);
|
|
853
|
+
|
|
854
|
+
assert.ok(master, "the master is a published document, not a live figure");
|
|
855
|
+
const heights = [...master.matchAll(/^v\/(\d+)\/index\.m3u8$/gm)].map((match) => Number(match[1]));
|
|
856
|
+
assert.deepEqual(
|
|
857
|
+
heights,
|
|
858
|
+
[1080, 812, 720, 540, 480, 360, 240],
|
|
859
|
+
"every rung that can be spliced onto this cut grid stays addressable"
|
|
860
|
+
);
|
|
861
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The blocking-call pool is stated before anything can create it.
|
|
3
|
+
*
|
|
4
|
+
* Field 2026-08-18: a film with 517 seeders spent eleven minutes with zero
|
|
5
|
+
* peers on the addon host. Every announce timed out — UDP and HTTP alike — and
|
|
6
|
+
* the cause was not the network: resolving that torrent's ten tracker names as
|
|
7
|
+
* a burst took 7.58 s on the default four-thread pool, against 27-42 ms each
|
|
8
|
+
* when the pool was larger. A dead tracker holds a thread for the resolver's
|
|
9
|
+
* whole timeout, and every announce queued behind it misses its own deadline.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import test from "node:test";
|
|
13
|
+
import assert from "node:assert/strict";
|
|
14
|
+
import { readFile } from "node:fs/promises";
|
|
15
|
+
import path from "node:path";
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
|
|
18
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
19
|
+
|
|
20
|
+
test("the pool is set, and left alone when the deployment states its own", async () => {
|
|
21
|
+
const previous = process.env.UV_THREADPOOL_SIZE;
|
|
22
|
+
try {
|
|
23
|
+
process.env.UV_THREADPOOL_SIZE = "";
|
|
24
|
+
await import(`../services/thread-pool.js?first=${Date.now()}`);
|
|
25
|
+
assert.equal(
|
|
26
|
+
Number(process.env.UV_THREADPOOL_SIZE) >= 16,
|
|
27
|
+
true,
|
|
28
|
+
"enough threads for a torrent's whole announce list to resolve at once"
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
process.env.UV_THREADPOOL_SIZE = "8";
|
|
32
|
+
await import(`../services/thread-pool.js?stated=${Date.now()}`);
|
|
33
|
+
assert.equal(process.env.UV_THREADPOOL_SIZE, "8", "a stated size is the deployment's to choose");
|
|
34
|
+
} finally {
|
|
35
|
+
if (previous === undefined) {
|
|
36
|
+
delete process.env.UV_THREADPOOL_SIZE;
|
|
37
|
+
} else {
|
|
38
|
+
process.env.UV_THREADPOOL_SIZE = previous;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("the entry point imports it before anything that could create the pool", async () => {
|
|
44
|
+
const cli = await readFile(path.join(here, "..", "bin", "cli.js"), "utf8");
|
|
45
|
+
const imports = [...cli.matchAll(/^import\s.*?from\s+["'](.+?)["'];|^import\s+["'](.+?)["'];/gm)]
|
|
46
|
+
.map((match) => match[1] ?? match[2]);
|
|
47
|
+
|
|
48
|
+
assert.equal(
|
|
49
|
+
imports[0],
|
|
50
|
+
"../services/thread-pool.js",
|
|
51
|
+
"a module's imports run before its body, so this cannot be a statement in cli.js"
|
|
52
|
+
);
|
|
53
|
+
});
|