@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.
- package/CHANGELOG.md +22 -0
- package/docs/encode-architecture.md +51 -2
- package/package.json +1 -1
- package/research/double-spawn-2026-09-10.md +171 -0
- package/services/disk/DiskSpace.js +146 -0
- package/services/disk/wire.js +60 -0
- package/services/encode/EncodeRun.js +37 -9
- package/services/encode/SegmentStore.js +284 -232
- package/services/encode/run-command.js +16 -1
- package/services/hls-session-manager.js +31 -128
- package/services/orchestrators/EncodeOrchestrator.js +52 -38
- package/services/piece-store/allowance.js +107 -0
- package/services/piece-store/piece-disk-store.js +365 -0
- package/services/piece-store/shared-piece-store.js +1549 -1535
- package/services/segment-formats/fmp4.js +54 -0
- package/services/segment-formats/mpegts.js +54 -0
- package/services/torrent-worker/client.js +32 -0
- package/services/torrent-worker/pool-adapter.js +15 -0
- package/services/torrent-worker/protocol.js +9 -0
- package/services/torrent-worker/worker.js +8 -1
- package/services/viewer/positions.js +48 -0
- package/test/audio-inventory.test.js +176 -176
- package/test/auto-quality-step.test.js +514 -514
- package/test/concurrent-cost.test.js +138 -138
- package/test/coverage-follows-the-disk.test.js +191 -187
- package/test/coverage-map.test.js +195 -195
- package/test/declared-tracks.test.js +35 -35
- package/test/disk-space.test.js +138 -0
- package/test/encode-orchestrator.test.js +0 -3
- package/test/encode-run.test.js +5 -12
- package/test/held-request-width.test.js +155 -155
- package/test/helpers/encode-run.js +2 -2
- package/test/matroska-blocks.test.js +0 -0
- package/test/matroska-cues-track.test.js +192 -192
- package/test/mp4-composition-times.test.js +0 -0
- package/test/mp4-subtitles.test.js +173 -173
- package/test/one-authority.test.js +281 -220
- package/test/orchestrator-wired.test.js +199 -195
- package/test/packet-witness-ring.test.js +236 -236
- package/test/packet-witness.test.js +148 -148
- package/test/piece-disk-store.test.js +267 -0
- package/test/piece-reader.test.js +4 -4
- package/test/piece-store-eviction.test.js +17 -17
- package/test/piece-store-reservations.test.js +20 -1
- package/test/piece-store-slow-disk.test.js +16 -1
- package/test/produced-copy-choice.test.js +258 -358
- package/test/read-window.test.js +6 -6
- package/test/run-intervals.test.js +100 -100
- package/test/seek-landing.test.js +109 -109
- package/test/segment-serve-wiring.test.js +8 -9
- package/test/segment-store-eviction.test.js +232 -0
- package/test/segment-store.test.js +238 -216
- package/test/segments-are-shared.test.js +1 -1
- package/test/shared-piece-store.test.js +12 -12
- package/test/sidecar-naming.test.js +142 -142
- package/test/subtitle-cue-framing.test.js +200 -200
- package/test/subtitle-cue-walk.test.js +369 -369
- package/test/subtitle-defaults.test.js +97 -97
- package/test/subtitle-track-numbering.test.js +370 -370
- package/test/tail-duplication.test.js +167 -167
- package/test/tracks-begin-together.test.js +195 -195
- package/test/two-viewers-one-picture.test.js +374 -374
- package/test/video-facts.test.js +102 -102
- package/test/wedge-certainty.test.js +131 -131
- package/services/encode/open-piece.js +0 -135
- package/services/piece-store/disk-tier.js +0 -151
- package/test/open-piece.test.js +0 -152
|
@@ -1,358 +1,258 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file A piece that cannot be played: waited for, or removed and made again.
|
|
3
|
-
*
|
|
4
|
-
* Field 2026-09-03. A run was suspended 548 ms after it started with
|
|
5
|
-
* `segment-00025.mp4` newly opened, so the file stayed at zero bytes. From then
|
|
6
|
-
* on the two halves of the proxy disagreed about it and neither could see the
|
|
7
|
-
* other's reason: the look-ahead counted the NAME, found the numbering unbroken
|
|
8
|
-
* through #83, called it `420s ahead of the viewer` and kept the encoder
|
|
9
|
-
* stopped; the serving path read the FILE, found it short of a track, and
|
|
10
|
-
* waited for a run that had produced nothing to finish it.
|
|
11
|
-
*
|
|
12
|
-
* The viewer's picture stood still for ten minutes on a transport measuring
|
|
13
|
-
* 3-9 ms round trip.
|
|
14
|
-
*
|
|
15
|
-
* The question of WHICH copy answers is gone with the directory-per-run scheme:
|
|
16
|
-
* there is one directory per output, runs are kept apart by their stretches,
|
|
17
|
-
* and one name is one file. What is left is the question that remains real —
|
|
18
|
-
* whether a file that cannot be played is being written now or was left by a
|
|
19
|
-
* run that has ended.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
import test from "node:test";
|
|
23
|
-
import assert from "node:assert/strict";
|
|
24
|
-
import { SourceFile } from "../services/source/SourceFile.js";
|
|
25
|
-
import { Timeline } from "../services/output/Timeline.js";
|
|
26
|
-
import { mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
|
|
27
|
-
import os from "node:os";
|
|
28
|
-
import path from "node:path";
|
|
29
|
-
import { HlsSessionManager, usableSegmentIndices } from "../services/hls-session-manager.js";
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
* @param {
|
|
42
|
-
* @
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
head.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
* @
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
body.writeUInt32BE(
|
|
59
|
-
body.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
* @param {number}
|
|
66
|
-
* @param {number}
|
|
67
|
-
* @
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
box("
|
|
77
|
-
box("
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
* @
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
tfdtBody.
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
* @
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
trak(
|
|
107
|
-
|
|
108
|
-
]));
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
box("
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
//
|
|
186
|
-
//
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
//
|
|
208
|
-
//
|
|
209
|
-
//
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
await rm(dirPath, { recursive: true, force: true });
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
assert.
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
[0, 1],
|
|
260
|
-
"a copy with bytes in it, which is what the serving path will find"
|
|
261
|
-
);
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
test("what the output holds is asked of the filesystem once per file", async (t) => {
|
|
265
|
-
const dirPath = await mkdtemp(path.join(os.tmpdir(), "usable-memo-"));
|
|
266
|
-
t.after(async () => {
|
|
267
|
-
await rm(dirPath, { recursive: true, force: true });
|
|
268
|
-
});
|
|
269
|
-
await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
|
|
270
|
-
const known = new Set();
|
|
271
|
-
|
|
272
|
-
usableSegmentIndices([dirPath], fmp4Format, known);
|
|
273
|
-
|
|
274
|
-
assert.deepEqual(
|
|
275
|
-
[...known],
|
|
276
|
-
[path.join(dirPath, "segment-00000.mp4")],
|
|
277
|
-
"a piece that has bytes never loses them, and this runs on the thread carrying the data channel"
|
|
278
|
-
);
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
test("a run killed with a piece open leaves nothing behind", async (t) => {
|
|
282
|
-
const dirPath = await mkdtemp(path.join(os.tmpdir(), "open-piece-"));
|
|
283
|
-
t.after(async () => {
|
|
284
|
-
await rm(dirPath, { recursive: true, force: true });
|
|
285
|
-
});
|
|
286
|
-
await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
|
|
287
|
-
await writeFile(path.join(dirPath, "segment-00001.mp4"), Buffer.alloc(0));
|
|
288
|
-
|
|
289
|
-
// WHAT THE RUN NAMED IS WHAT IT FINISHED. It named #0 on the ready channel and
|
|
290
|
-
// was killed with #1 open, so #1 goes and #0 stays. Asking the file instead
|
|
291
|
-
// cannot answer: a piece cut short still decodes, which is how 3.92 s of a
|
|
292
|
-
// declared 5.589 s reached a viewer on 2026-09-06.
|
|
293
|
-
assert.equal(
|
|
294
|
-
await discardOpenPiece(dirPath, fmp4Format, { from: 0, to: 1 }, null, "segment-00000.mp4"),
|
|
295
|
-
1
|
|
296
|
-
);
|
|
297
|
-
assert.deepEqual(
|
|
298
|
-
await readdir(dirPath),
|
|
299
|
-
["segment-00000.mp4"],
|
|
300
|
-
"only the piece that was open goes; everything the run named stays"
|
|
301
|
-
);
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
test("a run that named nothing can only have opened the first piece it was given", async (t) => {
|
|
305
|
-
const dirPath = await mkdtemp(path.join(os.tmpdir(), "open-piece-unnamed-"));
|
|
306
|
-
t.after(async () => {
|
|
307
|
-
await rm(dirPath, { recursive: true, force: true });
|
|
308
|
-
});
|
|
309
|
-
await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
|
|
310
|
-
await writeFile(path.join(dirPath, "segment-00001.mp4"), wholePiece(SEGMENT_SECONDS));
|
|
311
|
-
|
|
312
|
-
// THE CASE THIS CONTRACT WAS WRITTEN FOR. ffmpeg names a piece as it closes it
|
|
313
|
-
// and opens the next, so a run that named nothing had only its first open. It
|
|
314
|
-
// used to search the whole directory and take the highest number in it —
|
|
315
|
-
// every run of an output writes into that one directory, so what it took was
|
|
316
|
-
// a piece a LIVE run had just finished. Field 2026-09-06: the piece holding
|
|
317
|
-
// 2:47-2:57 went that way, its number is spent for good because names only
|
|
318
|
-
// grow, and the picture stood still for 647 s.
|
|
319
|
-
assert.equal(await discardOpenPiece(dirPath, fmp4Format, null, null), 0);
|
|
320
|
-
assert.deepEqual(
|
|
321
|
-
await readdir(dirPath),
|
|
322
|
-
["segment-00001.mp4"],
|
|
323
|
-
"the piece belonging to somebody else is not touched"
|
|
324
|
-
);
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
test("a run that finished its last piece keeps it", async (t) => {
|
|
328
|
-
const dirPath = await mkdtemp(path.join(os.tmpdir(), "open-piece-good-"));
|
|
329
|
-
t.after(async () => {
|
|
330
|
-
await rm(dirPath, { recursive: true, force: true });
|
|
331
|
-
});
|
|
332
|
-
await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
|
|
333
|
-
await writeFile(path.join(dirPath, "segment-00001.mp4"), wholePiece(SEGMENT_SECONDS));
|
|
334
|
-
|
|
335
|
-
assert.equal(
|
|
336
|
-
await discardOpenPiece(dirPath, fmp4Format, { from: 0, to: 1 }, () => true, "segment-00001.mp4"),
|
|
337
|
-
null,
|
|
338
|
-
"a stop between two cuts leaves good output; deleting it means encoding it twice"
|
|
339
|
-
);
|
|
340
|
-
assert.equal((await readdir(dirPath)).length, 2);
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
test("a last piece short of a track goes, even though it has bytes", async (t) => {
|
|
344
|
-
const dirPath = await mkdtemp(path.join(os.tmpdir(), "open-piece-half-"));
|
|
345
|
-
t.after(async () => {
|
|
346
|
-
await rm(dirPath, { recursive: true, force: true });
|
|
347
|
-
});
|
|
348
|
-
await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
|
|
349
|
-
await writeFile(path.join(dirPath, "segment-00001.mp4"), halfPiece(SEGMENT_SECONDS));
|
|
350
|
-
const init = fmp4Format.extractInit(wholePiece(0));
|
|
351
|
-
|
|
352
|
-
assert.equal(
|
|
353
|
-
await discardOpenPiece(dirPath, fmp4Format, { from: 0, to: 1 }, (raw) =>
|
|
354
|
-
fmp4Format.hasEveryTrack(fmp4Format.stripInit(raw), init), "segment-00001.mp4"),
|
|
355
|
-
1,
|
|
356
|
-
"a size above zero is not the same as a piece that can be played"
|
|
357
|
-
);
|
|
358
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* @file A piece that cannot be played: waited for, or removed and made again.
|
|
3
|
+
*
|
|
4
|
+
* Field 2026-09-03. A run was suspended 548 ms after it started with
|
|
5
|
+
* `segment-00025.mp4` newly opened, so the file stayed at zero bytes. From then
|
|
6
|
+
* on the two halves of the proxy disagreed about it and neither could see the
|
|
7
|
+
* other's reason: the look-ahead counted the NAME, found the numbering unbroken
|
|
8
|
+
* through #83, called it `420s ahead of the viewer` and kept the encoder
|
|
9
|
+
* stopped; the serving path read the FILE, found it short of a track, and
|
|
10
|
+
* waited for a run that had produced nothing to finish it.
|
|
11
|
+
*
|
|
12
|
+
* The viewer's picture stood still for ten minutes on a transport measuring
|
|
13
|
+
* 3-9 ms round trip.
|
|
14
|
+
*
|
|
15
|
+
* The question of WHICH copy answers is gone with the directory-per-run scheme:
|
|
16
|
+
* there is one directory per output, runs are kept apart by their stretches,
|
|
17
|
+
* and one name is one file. What is left is the question that remains real —
|
|
18
|
+
* whether a file that cannot be played is being written now or was left by a
|
|
19
|
+
* run that has ended.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import test from "node:test";
|
|
23
|
+
import assert from "node:assert/strict";
|
|
24
|
+
import { SourceFile } from "../services/source/SourceFile.js";
|
|
25
|
+
import { Timeline } from "../services/output/Timeline.js";
|
|
26
|
+
import { mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
|
|
27
|
+
import os from "node:os";
|
|
28
|
+
import path from "node:path";
|
|
29
|
+
import { HlsSessionManager, usableSegmentIndices } from "../services/hls-session-manager.js";
|
|
30
|
+
import { startRunOn } from "./helpers/encode-run.js";
|
|
31
|
+
import { fmp4Format } from "../services/segment-formats/fmp4.js";
|
|
32
|
+
|
|
33
|
+
const MOVIE_TIMESCALE = 1000;
|
|
34
|
+
const VIDEO_TIMESCALE = 90_000;
|
|
35
|
+
const AUDIO_TIMESCALE = 48_000;
|
|
36
|
+
const SEGMENT_SECONDS = 12.5;
|
|
37
|
+
const SESSION_ID = "aef21c88-a8d6-4a9a-8e7a-d0a9536351cf";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {string} type
|
|
41
|
+
* @param {Buffer} body
|
|
42
|
+
* @returns {Buffer}
|
|
43
|
+
*/
|
|
44
|
+
function box(type, body) {
|
|
45
|
+
const head = Buffer.alloc(8);
|
|
46
|
+
head.writeUInt32BE(8 + body.length, 0);
|
|
47
|
+
head.write(type, 4, "latin1");
|
|
48
|
+
return Buffer.concat([head, body]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @param {number} offsetSeconds
|
|
53
|
+
* @returns {Buffer}
|
|
54
|
+
*/
|
|
55
|
+
function emptyEdit(offsetSeconds) {
|
|
56
|
+
const body = Buffer.alloc(16);
|
|
57
|
+
body.writeUInt32BE(1, 4);
|
|
58
|
+
body.writeUInt32BE(Math.round(offsetSeconds * MOVIE_TIMESCALE), 8);
|
|
59
|
+
body.writeInt32BE(-1, 12);
|
|
60
|
+
return box("elst", body);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @param {number} trackId
|
|
65
|
+
* @param {number} timescale
|
|
66
|
+
* @param {number} offsetSeconds
|
|
67
|
+
* @returns {Buffer}
|
|
68
|
+
*/
|
|
69
|
+
function trak(trackId, timescale, offsetSeconds) {
|
|
70
|
+
const tkhdBody = Buffer.alloc(84);
|
|
71
|
+
tkhdBody.writeUInt32BE(trackId, 12);
|
|
72
|
+
const mdhdBody = Buffer.alloc(20);
|
|
73
|
+
mdhdBody.writeUInt32BE(timescale, 12);
|
|
74
|
+
return box("trak", Buffer.concat([
|
|
75
|
+
box("tkhd", tkhdBody),
|
|
76
|
+
box("edts", emptyEdit(offsetSeconds)),
|
|
77
|
+
box("mdia", box("mdhd", mdhdBody))
|
|
78
|
+
]));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @param {number} trackId
|
|
83
|
+
* @returns {Buffer}
|
|
84
|
+
*/
|
|
85
|
+
function traf(trackId) {
|
|
86
|
+
const tfhdBody = Buffer.alloc(8);
|
|
87
|
+
tfhdBody.writeUInt32BE(trackId, 4);
|
|
88
|
+
const tfdtBody = Buffer.alloc(12);
|
|
89
|
+
tfdtBody.writeUInt8(1, 0);
|
|
90
|
+
tfdtBody.writeBigUInt64BE(0n, 4);
|
|
91
|
+
return box("traf", Buffer.concat([box("tfhd", tfhdBody), box("tfdt", tfdtBody)]));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* A piece shaped like the `segment` muxer's output, carrying both tracks.
|
|
96
|
+
*
|
|
97
|
+
* @param {number} offsetSeconds
|
|
98
|
+
* @returns {Buffer}
|
|
99
|
+
*/
|
|
100
|
+
function wholePiece(offsetSeconds) {
|
|
101
|
+
const mvhdBody = Buffer.alloc(100);
|
|
102
|
+
mvhdBody.writeUInt32BE(MOVIE_TIMESCALE, 12);
|
|
103
|
+
const moov = box("moov", Buffer.concat([
|
|
104
|
+
box("mvhd", mvhdBody),
|
|
105
|
+
trak(1, VIDEO_TIMESCALE, offsetSeconds),
|
|
106
|
+
trak(2, AUDIO_TIMESCALE, offsetSeconds)
|
|
107
|
+
]));
|
|
108
|
+
const moof = box("moof", Buffer.concat([traf(1), traf(2)]));
|
|
109
|
+
return Buffer.concat([
|
|
110
|
+
box("ftyp", Buffer.alloc(16, 0)),
|
|
111
|
+
moov,
|
|
112
|
+
moof,
|
|
113
|
+
box("mdat", Buffer.alloc(64, 0x5a)),
|
|
114
|
+
box("mfra", Buffer.alloc(24, 0))
|
|
115
|
+
]);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* A session with the output directory every run writes into.
|
|
121
|
+
*
|
|
122
|
+
* @returns {Promise<{ manager: HlsSessionManager, session: object, dirPath: string }>}
|
|
123
|
+
*/
|
|
124
|
+
async function sessionOnOneDirectory() {
|
|
125
|
+
const dirPath = await mkdtemp(path.join(os.tmpdir(), "produced-copy-"));
|
|
126
|
+
const manager = new HlsSessionManager({
|
|
127
|
+
enabled: true,
|
|
128
|
+
ffmpegBin: "ffmpeg",
|
|
129
|
+
localBindHost: "127.0.0.1",
|
|
130
|
+
localPort: 9090
|
|
131
|
+
});
|
|
132
|
+
const session = {
|
|
133
|
+
id: SESSION_ID,
|
|
134
|
+
dirPath,
|
|
135
|
+
// Where this file is cut, held by the file. A fixture that stated it
|
|
136
|
+
// on the session was describing what production no longer does.
|
|
137
|
+
timeline: new Timeline({
|
|
138
|
+
boundaries: [0, SEGMENT_SECONDS, 25, 37.5, 50],
|
|
139
|
+
cutGrid: "uniform"
|
|
140
|
+
}),
|
|
141
|
+
state: "ready",
|
|
142
|
+
file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "Drifters - 04.mkv" }),
|
|
143
|
+
// An ordinary session reads its own file, and its sound is inside it. The
|
|
144
|
+
// three differ only for a soundtrack shipped as a file of its own.
|
|
145
|
+
get inputFile() { return this.file; },
|
|
146
|
+
get audioFile() { return this.file; },
|
|
147
|
+
startedAt: Date.now(),
|
|
148
|
+
createEntryMs: Date.now(),
|
|
149
|
+
lastAccessedAt: Date.now(),
|
|
150
|
+
runs: new Set(),
|
|
151
|
+
lastError: "",
|
|
152
|
+
consumers: new Set(),
|
|
153
|
+
viewers: new Map(),
|
|
154
|
+
segmentFormat: fmp4Format,
|
|
155
|
+
useSyntheticPlaylist: true,
|
|
156
|
+
playlistText: "#EXTM3U\n",
|
|
157
|
+
initBytes: fmp4Format.extractInit(wholePiece(0)),
|
|
158
|
+
firstSegmentLogged: false,
|
|
159
|
+
waitEpoch: 0
|
|
160
|
+
};
|
|
161
|
+
manager.sessionsById.set(SESSION_ID, session);
|
|
162
|
+
// A run exists and is alive — the state the field case was in, and the one in
|
|
163
|
+
// which the old test called every leftover "still being written".
|
|
164
|
+
startRunOn(session, { from: 0, usesExplicitCuts: true });
|
|
165
|
+
return { manager, session, dirPath };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
test("a leftover of a run that has ended is not served, and answering does not delete it", async (t) => {
|
|
171
|
+
const { manager, session, dirPath } = await sessionOnOneDirectory();
|
|
172
|
+
t.after(async () => {
|
|
173
|
+
await manager.disposeAll();
|
|
174
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
175
|
+
});
|
|
176
|
+
// Opened by a run that is gone. It has a name and no bytes, so there is
|
|
177
|
+
// nothing to serve.
|
|
178
|
+
session.runs = new Set();
|
|
179
|
+
await writeFile(path.join(dirPath, "segment-00001.mp4"), Buffer.alloc(0));
|
|
180
|
+
|
|
181
|
+
const result = await manager.getFileStream(SESSION_ID, "segment-00001.mp4", { requestSeq: 1 });
|
|
182
|
+
|
|
183
|
+
assert.equal(result.kind, "warming-up", "nothing servable exists yet, so the viewer waits");
|
|
184
|
+
// AND ANSWERING A REQUEST DELETES NOTHING. It used to: this test was written
|
|
185
|
+
// when the serving path cleared up after ended runs, and several runs of an
|
|
186
|
+
// output share one directory — so what a request removed could be the piece a
|
|
187
|
+
// LIVE run had just finished. Clearing up belongs to the ending of a run,
|
|
188
|
+
// which is the only thing that knows what it left open and what it named
|
|
189
|
+
// and it knows because the piece is under that run's own working name
|
|
190
|
+
// (`SegmentStore.clearUpAfter`, checked in `segment-store.test.js`). A file
|
|
191
|
+
// with no bytes is
|
|
192
|
+
// ignored by everything that reads the directory, so leaving it costs nothing
|
|
193
|
+
// and deleting it from here costs a segment.
|
|
194
|
+
const left = (await readdir(dirPath)).filter((name) => name === "segment-00001.mp4");
|
|
195
|
+
assert.deepEqual(left, ["segment-00001.mp4"]);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("the current run's own unfinished piece is waited for, never deleted", async (t) => {
|
|
199
|
+
const { manager, session, dirPath } = await sessionOnOneDirectory();
|
|
200
|
+
t.after(async () => {
|
|
201
|
+
await manager.disposeAll();
|
|
202
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
203
|
+
});
|
|
204
|
+
// The same file, in the directory of the run that is alive. It is being
|
|
205
|
+
// written right now. Deleting it is the 2026-08-06 incident: #225 was removed
|
|
206
|
+
// 14 s into the run producing it, which then wrote on into a file nobody
|
|
207
|
+
// could open, and the segment never appeared.
|
|
208
|
+
//
|
|
209
|
+
// The live run begins at #1, so #1 is its own first piece.
|
|
210
|
+
startRunOn(session, { from: 1, producing: false, usesExplicitCuts: true });
|
|
211
|
+
await writeFile(path.join(dirPath, "segment-00001.mp4"), Buffer.alloc(0));
|
|
212
|
+
|
|
213
|
+
const result = await manager.getFileStream(SESSION_ID, "segment-00001.mp4", { requestSeq: 1 });
|
|
214
|
+
|
|
215
|
+
assert.equal(result.kind, "warming-up");
|
|
216
|
+
const left = (await readdir(dirPath)).filter((name) => name === "segment-00001.mp4");
|
|
217
|
+
assert.deepEqual(left, ["segment-00001.mp4"], "the live run's own output must be left alone");
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test("the look-ahead does not count a file with nothing in it", async (t) => {
|
|
221
|
+
const dirPath = await mkdtemp(path.join(os.tmpdir(), "usable-indices-"));
|
|
222
|
+
t.after(async () => {
|
|
223
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
224
|
+
});
|
|
225
|
+
await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
|
|
226
|
+
await writeFile(path.join(dirPath, "segment-00001.mp4"), Buffer.alloc(0));
|
|
227
|
+
|
|
228
|
+
assert.deepEqual(
|
|
229
|
+
[...usableSegmentIndices([dirPath], fmp4Format, new Set())].sort((a, b) => a - b),
|
|
230
|
+
[0],
|
|
231
|
+
"an empty file bridged the hole and bought the encoder a suspension it had not earned"
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
// The same number, made properly: now it genuinely is ready.
|
|
235
|
+
await writeFile(path.join(dirPath, "segment-00001.mp4"), wholePiece(SEGMENT_SECONDS));
|
|
236
|
+
assert.deepEqual(
|
|
237
|
+
[...usableSegmentIndices([dirPath], fmp4Format, new Set())].sort((a, b) => a - b),
|
|
238
|
+
[0, 1],
|
|
239
|
+
"a copy with bytes in it, which is what the serving path will find"
|
|
240
|
+
);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("what the output holds is asked of the filesystem once per file", async (t) => {
|
|
244
|
+
const dirPath = await mkdtemp(path.join(os.tmpdir(), "usable-memo-"));
|
|
245
|
+
t.after(async () => {
|
|
246
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
247
|
+
});
|
|
248
|
+
await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
|
|
249
|
+
const known = new Set();
|
|
250
|
+
|
|
251
|
+
usableSegmentIndices([dirPath], fmp4Format, known);
|
|
252
|
+
|
|
253
|
+
assert.deepEqual(
|
|
254
|
+
[...known],
|
|
255
|
+
[path.join(dirPath, "segment-00000.mp4")],
|
|
256
|
+
"a piece that has bytes never loses them, and this runs on the thread carrying the data channel"
|
|
257
|
+
);
|
|
258
|
+
});
|