@torrent-tv/proxy 2.80.9 → 2.80.10
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 +9 -0
- package/package.json +51 -50
- package/services/data-channel-handler.js +2 -4
- package/services/encode/EncodePlan.js +0 -3
- package/services/encode/EncodeRun.js +14 -0
- package/services/hls-session-manager.js +37 -143
- package/services/hwaccel.js +2 -2
- package/services/orchestrators/EncodeOrchestrator.js +726 -726
- package/services/quality/EncodeCost.js +555 -555
- package/test/auto-quality-step.test.js +514 -507
- package/test/contention.test.js +1 -1
- package/test/encode-orchestrator.test.js +1 -2
- package/test/encode-plan-viewers.test.js +18 -19
- package/test/encode-plan.test.js +539 -539
- package/test/held-request-width.test.js +155 -154
- package/test/helpers/encode-run.js +136 -128
- package/test/orchestrator-wired.test.js +16 -11
- package/test/produced-copy-choice.test.js +358 -327
- package/test/segment-serve-wiring.test.js +76 -20
- package/test/stale-request-after-seek.test.js +51 -77
- package/test/tracks-begin-together.test.js +176 -153
- package/test/viewer-outputs.test.js +278 -273
- package/test/behind-head-repair.test.js +0 -261
- /package/services/{contention.js → encode/contention.js} +0 -0
- /package/{test/decode-cost.test.js → test-measured/decode-cost.measured.js} +0 -0
- /package/{test/decode-measurement.test.js → test-measured/decode-measurement.measured.js} +0 -0
|
@@ -1,154 +1,155 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file How far ahead of the viewer a held segment request may sit.
|
|
3
|
-
*
|
|
4
|
-
* A request is released when the viewer has moved away from it. "Moved away"
|
|
5
|
-
* used to mean more than `MAX_LOOKAHEAD_SEGMENTS` — eight segments, which
|
|
6
|
-
* happened to match a browser holding thirty seconds and would refuse three
|
|
7
|
-
* quarters of the requests of one holding the whole cushion. The width is the
|
|
8
|
-
* encoder's own look-ahead now, which is the same figure the browser sizes its
|
|
9
|
-
* buffer from. Roadmap item 4, step 2.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import test from "node:test";
|
|
13
|
-
import assert from "node:assert/strict";
|
|
14
|
-
import { Timeline } from "../services/output/Timeline.js";
|
|
15
|
-
import { mkdtemp, rm } from "node:fs/promises";
|
|
16
|
-
import os from "node:os";
|
|
17
|
-
import path from "node:path";
|
|
18
|
-
import { HlsSessionManager } from "../services/hls-session-manager.js";
|
|
19
|
-
import { fmp4Format } from "../services/segment-formats/fmp4.js";
|
|
20
|
-
|
|
21
|
-
const SESSION_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
|
|
22
|
-
const SEGMENT_SECONDS = 4;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @returns {Promise<{ manager: HlsSessionManager, dirPath: string }>}
|
|
26
|
-
*/
|
|
27
|
-
async function managerWithSession() {
|
|
28
|
-
const dirPath = await mkdtemp(path.join(os.tmpdir(), "held-request-"));
|
|
29
|
-
const manager = new HlsSessionManager({
|
|
30
|
-
enabled: true,
|
|
31
|
-
ffmpegBin: "ffmpeg",
|
|
32
|
-
localBindHost: "127.0.0.1",
|
|
33
|
-
localPort: 9090
|
|
34
|
-
});
|
|
35
|
-
manager.sessionsById.set(SESSION_ID, {
|
|
36
|
-
id: SESSION_ID,
|
|
37
|
-
dirPath,
|
|
38
|
-
// Where this file is cut, held by the file. A fixture that stated it
|
|
39
|
-
// on the session was describing what production no longer does.
|
|
40
|
-
timeline: new Timeline({
|
|
41
|
-
boundaries: Array.from({ length: 201 }, (_, index) => index * SEGMENT_SECONDS),
|
|
42
|
-
cutGrid: "uniform"
|
|
43
|
-
}),
|
|
44
|
-
state: "ready",
|
|
45
|
-
segmentFormat: fmp4Format,
|
|
46
|
-
segmentCount: 200,
|
|
47
|
-
useSyntheticPlaylist: true,
|
|
48
|
-
// The viewer is at segment #25.
|
|
49
|
-
furthestViewerSeconds: 100
|
|
50
|
-
});
|
|
51
|
-
return { manager, dirPath };
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* @param {number} index
|
|
56
|
-
* @returns {string}
|
|
57
|
-
*/
|
|
58
|
-
function segment(index) {
|
|
59
|
-
return `segment-${String(index).padStart(5, "0")}.mp4`;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
test("the width is the encoder's own look-ahead, in segments", async (t) => {
|
|
63
|
-
const { manager, dirPath } = await managerWithSession();
|
|
64
|
-
t.after(async () => {
|
|
65
|
-
await rm(dirPath, { recursive: true, force: true });
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
const width = Math.ceil(manager.lookaheadSeconds / manager.segmentDurationSec);
|
|
69
|
-
assert.equal(width, 30, "120 s of look-ahead over 4 s segments");
|
|
70
|
-
assert.equal(manager.requestStillWanted(SESSION_ID, segment(25 + width)), true, "the far edge");
|
|
71
|
-
assert.equal(
|
|
72
|
-
manager.requestStillWanted(SESSION_ID, segment(25 + width + 1)),
|
|
73
|
-
false,
|
|
74
|
-
"past everything the encoder is allowed to have produced"
|
|
75
|
-
);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test("a request the old eight-segment width would have refused is kept", async (t) => {
|
|
79
|
-
const { manager, dirPath } = await managerWithSession();
|
|
80
|
-
t.after(async () => {
|
|
81
|
-
await rm(dirPath, { recursive: true, force: true });
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
// #34 is nine segments ahead of the viewer — inside a 120 s cushion and
|
|
85
|
-
// outside the eight the width used to be. This is the request a browser
|
|
86
|
-
// holding the whole cushion makes constantly.
|
|
87
|
-
assert.equal(manager.requestStillWanted(SESSION_ID, segment(34)), true);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
test("a segment behind the viewer is still released", async (t) => {
|
|
91
|
-
const { manager, dirPath } = await managerWithSession();
|
|
92
|
-
t.after(async () => {
|
|
93
|
-
await rm(dirPath, { recursive: true, force: true });
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
assert.equal(manager.requestStillWanted(SESSION_ID, segment(24)), false);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test("a seek by one viewer does not release the request held for another", async (t) => {
|
|
100
|
-
const { manager, dirPath } = await managerWithSession();
|
|
101
|
-
t.after(async () => {
|
|
102
|
-
await rm(dirPath, { recursive: true, force: true });
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
//
|
|
107
|
-
//
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
//
|
|
114
|
-
//
|
|
115
|
-
//
|
|
116
|
-
assert.equal(
|
|
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
|
-
assert.equal(manager.requestStillWanted(SESSION_ID, segment(
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
//
|
|
152
|
-
|
|
153
|
-
assert.equal(manager.requestStillWanted(SESSION_ID, segment(
|
|
154
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @file How far ahead of the viewer a held segment request may sit.
|
|
3
|
+
*
|
|
4
|
+
* A request is released when the viewer has moved away from it. "Moved away"
|
|
5
|
+
* used to mean more than `MAX_LOOKAHEAD_SEGMENTS` — eight segments, which
|
|
6
|
+
* happened to match a browser holding thirty seconds and would refuse three
|
|
7
|
+
* quarters of the requests of one holding the whole cushion. The width is the
|
|
8
|
+
* encoder's own look-ahead now, which is the same figure the browser sizes its
|
|
9
|
+
* buffer from. Roadmap item 4, step 2.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import test from "node:test";
|
|
13
|
+
import assert from "node:assert/strict";
|
|
14
|
+
import { Timeline } from "../services/output/Timeline.js";
|
|
15
|
+
import { mkdtemp, rm } from "node:fs/promises";
|
|
16
|
+
import os from "node:os";
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
import { HlsSessionManager } from "../services/hls-session-manager.js";
|
|
19
|
+
import { fmp4Format } from "../services/segment-formats/fmp4.js";
|
|
20
|
+
|
|
21
|
+
const SESSION_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
|
|
22
|
+
const SEGMENT_SECONDS = 4;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @returns {Promise<{ manager: HlsSessionManager, dirPath: string }>}
|
|
26
|
+
*/
|
|
27
|
+
async function managerWithSession() {
|
|
28
|
+
const dirPath = await mkdtemp(path.join(os.tmpdir(), "held-request-"));
|
|
29
|
+
const manager = new HlsSessionManager({
|
|
30
|
+
enabled: true,
|
|
31
|
+
ffmpegBin: "ffmpeg",
|
|
32
|
+
localBindHost: "127.0.0.1",
|
|
33
|
+
localPort: 9090
|
|
34
|
+
});
|
|
35
|
+
manager.sessionsById.set(SESSION_ID, {
|
|
36
|
+
id: SESSION_ID,
|
|
37
|
+
dirPath,
|
|
38
|
+
// Where this file is cut, held by the file. A fixture that stated it
|
|
39
|
+
// on the session was describing what production no longer does.
|
|
40
|
+
timeline: new Timeline({
|
|
41
|
+
boundaries: Array.from({ length: 201 }, (_, index) => index * SEGMENT_SECONDS),
|
|
42
|
+
cutGrid: "uniform"
|
|
43
|
+
}),
|
|
44
|
+
state: "ready",
|
|
45
|
+
segmentFormat: fmp4Format,
|
|
46
|
+
segmentCount: 200,
|
|
47
|
+
useSyntheticPlaylist: true,
|
|
48
|
+
// The viewer is at segment #25.
|
|
49
|
+
furthestViewerSeconds: 100
|
|
50
|
+
});
|
|
51
|
+
return { manager, dirPath };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @param {number} index
|
|
56
|
+
* @returns {string}
|
|
57
|
+
*/
|
|
58
|
+
function segment(index) {
|
|
59
|
+
return `segment-${String(index).padStart(5, "0")}.mp4`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
test("the width is the encoder's own look-ahead, in segments", async (t) => {
|
|
63
|
+
const { manager, dirPath } = await managerWithSession();
|
|
64
|
+
t.after(async () => {
|
|
65
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const width = Math.ceil(manager.lookaheadSeconds / manager.segmentDurationSec);
|
|
69
|
+
assert.equal(width, 30, "120 s of look-ahead over 4 s segments");
|
|
70
|
+
assert.equal(manager.requestStillWanted(SESSION_ID, segment(25 + width)), true, "the far edge");
|
|
71
|
+
assert.equal(
|
|
72
|
+
manager.requestStillWanted(SESSION_ID, segment(25 + width + 1)),
|
|
73
|
+
false,
|
|
74
|
+
"past everything the encoder is allowed to have produced"
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("a request the old eight-segment width would have refused is kept", async (t) => {
|
|
79
|
+
const { manager, dirPath } = await managerWithSession();
|
|
80
|
+
t.after(async () => {
|
|
81
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// #34 is nine segments ahead of the viewer — inside a 120 s cushion and
|
|
85
|
+
// outside the eight the width used to be. This is the request a browser
|
|
86
|
+
// holding the whole cushion makes constantly.
|
|
87
|
+
assert.equal(manager.requestStillWanted(SESSION_ID, segment(34)), true);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("a segment behind the viewer is still released", async (t) => {
|
|
91
|
+
const { manager, dirPath } = await managerWithSession();
|
|
92
|
+
t.after(async () => {
|
|
93
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
assert.equal(manager.requestStillWanted(SESSION_ID, segment(24)), false);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("a seek by one viewer does not release the request held for another", async (t) => {
|
|
100
|
+
const { manager, dirPath } = await managerWithSession();
|
|
101
|
+
t.after(async () => {
|
|
102
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
103
|
+
});
|
|
104
|
+
// Both are watching the same copied picture, so both are this one session.
|
|
105
|
+
// One is at segment #25, the other far ahead at #150 — inside the fixture's
|
|
106
|
+
// own 200-segment timeline, since a position past the end of the grid is
|
|
107
|
+
// clamped to it and would prove nothing about the width.
|
|
108
|
+
manager.requestSeek(SESSION_ID, 100, "behind");
|
|
109
|
+
manager.requestSeek(SESSION_ID, 600, "ahead");
|
|
110
|
+
|
|
111
|
+
// A SEEK DOES ONE THING: it puts the viewer where they now are. It used to
|
|
112
|
+
// write that position into five places, this session's shared field among
|
|
113
|
+
// them, and every held request was then judged against whoever moved last —
|
|
114
|
+
// which is exactly what this test exists to refuse. Each viewer's own
|
|
115
|
+
// position is what decides, and the two below are asked separately.
|
|
116
|
+
assert.equal(manager.viewers.get("behind").positionSeconds(), 100);
|
|
117
|
+
assert.equal(manager.viewers.get("ahead").positionSeconds(), 600);
|
|
118
|
+
|
|
119
|
+
assert.equal(
|
|
120
|
+
manager.requestStillWanted(SESSION_ID, segment(26), "behind"),
|
|
121
|
+
true,
|
|
122
|
+
"the segment the viewer behind is waiting for is still theirs to wait for"
|
|
123
|
+
);
|
|
124
|
+
assert.equal(
|
|
125
|
+
manager.requestStillWanted(SESSION_ID, segment(26), "ahead"),
|
|
126
|
+
false,
|
|
127
|
+
"and for the one in front it is a place they have left"
|
|
128
|
+
);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("a viewer's own head moves with their seek", async (t) => {
|
|
132
|
+
const { manager, dirPath } = await managerWithSession();
|
|
133
|
+
t.after(async () => {
|
|
134
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Their last request was at #25; they jump to 600 s, which is #150. The
|
|
138
|
+
// segment at the target must be wanted — refusing it there is the freeze of
|
|
139
|
+
// 2026-08-18.
|
|
140
|
+
manager.requestSeek(SESSION_ID, 600, "viewer");
|
|
141
|
+
assert.equal(manager.requestStillWanted(SESSION_ID, segment(150), "viewer"), true);
|
|
142
|
+
assert.equal(manager.requestStillWanted(SESSION_ID, segment(25), "viewer"), false);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test("a viewer nobody can name is judged against the one shared position", async (t) => {
|
|
146
|
+
const { manager, dirPath } = await managerWithSession();
|
|
147
|
+
t.after(async () => {
|
|
148
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// A plain HTTP transport builds its own URLs and carries no id. The old
|
|
152
|
+
// behaviour is what remains, which for a single viewer is the same thing.
|
|
153
|
+
assert.equal(manager.requestStillWanted(SESSION_ID, segment(26), ""), true);
|
|
154
|
+
assert.equal(manager.requestStillWanted(SESSION_ID, segment(24), ""), false);
|
|
155
|
+
});
|
|
@@ -1,128 +1,136 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file A real `EncodeRun` over a process that is not real.
|
|
3
|
-
*
|
|
4
|
-
* Tests used to set a session's process fields by hand — `session.ffmpeg`,
|
|
5
|
-
* `session.runState`, `session.encodeStartIndex` — which is how a test can pass
|
|
6
|
-
* over code that no longer works: the fields were the thing under test as much
|
|
7
|
-
* as the behaviour was. A run is an object now, so a test builds the object the
|
|
8
|
-
* product builds and injects only the one thing a test may not have, which is a
|
|
9
|
-
* child process.
|
|
10
|
-
*
|
|
11
|
-
* The pid is deliberately absent unless a test asks for one. `pause` and
|
|
12
|
-
* `resume` send signals by pid, and a made-up number is somebody else's process
|
|
13
|
-
* on the machine running the tests.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { EncodeRun } from "../../services/encode/EncodeRun.js";
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* A child process that records what was done to it.
|
|
20
|
-
*
|
|
21
|
-
* @param {object} [options]
|
|
22
|
-
* @param {number | null} [options.pid] - Only where a test genuinely exercises
|
|
23
|
-
* suspend or resume, and then it must be this process's own.
|
|
24
|
-
* @returns {object}
|
|
25
|
-
*/
|
|
26
|
-
export function fakeProcess({ pid = null, exitsWhenKilled = true } = {}) {
|
|
27
|
-
/** @type {Map<string, (...args: unknown[]) => void>} */
|
|
28
|
-
const listeners = new Map();
|
|
29
|
-
return {
|
|
30
|
-
pid,
|
|
31
|
-
killed: false,
|
|
32
|
-
exitCode: null,
|
|
33
|
-
signalCode: null,
|
|
34
|
-
/** Every signal it was sent, in order. */
|
|
35
|
-
signals: [],
|
|
36
|
-
stdout: { on() {} },
|
|
37
|
-
stderr: { on() {} },
|
|
38
|
-
on(event, handler) {
|
|
39
|
-
const kept = listeners.get(event) ?? [];
|
|
40
|
-
kept.push(handler);
|
|
41
|
-
listeners.set(event, kept);
|
|
42
|
-
return this;
|
|
43
|
-
},
|
|
44
|
-
once(event, handler) {
|
|
45
|
-
return this.on(event, handler);
|
|
46
|
-
},
|
|
47
|
-
kill(signal = "SIGTERM") {
|
|
48
|
-
this.signals.push(signal);
|
|
49
|
-
this.killed = true;
|
|
50
|
-
// A real process answers a signal by exiting, and it does so on a later
|
|
51
|
-
// turn. A fake that never exits makes every disposal wait out the grace
|
|
52
|
-
// period, which is two seconds a test spends proving nothing.
|
|
53
|
-
if (exitsWhenKilled && this.exitCode === null && this.signalCode === null) {
|
|
54
|
-
queueMicrotask(() => this.exit(null, signal));
|
|
55
|
-
}
|
|
56
|
-
return true;
|
|
57
|
-
},
|
|
58
|
-
/**
|
|
59
|
-
* Report an exit, as the real thing would.
|
|
60
|
-
*
|
|
61
|
-
* @param {number | null} code
|
|
62
|
-
* @param {string | null} [signal]
|
|
63
|
-
*/
|
|
64
|
-
exit(code, signal = null) {
|
|
65
|
-
this.exitCode = code;
|
|
66
|
-
this.signalCode = signal;
|
|
67
|
-
for (const handler of listeners.get("exit") ?? []) {
|
|
68
|
-
handler(code, signal);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** A logger that says nothing, for tests that are not about the log. */
|
|
75
|
-
export const silentLogger = { info() {}, warn() {}, error() {} };
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Add a run to a session, started, over a fake process.
|
|
79
|
-
*
|
|
80
|
-
* A session holds a SET of runs — as many as the machine affords — so a test
|
|
81
|
-
* that wants two heads on one output calls this twice.
|
|
82
|
-
*
|
|
83
|
-
* @param {object} session - The session under test.
|
|
84
|
-
* @param {object} [options]
|
|
85
|
-
* @param {number} [options.from] - First segment number it is making.
|
|
86
|
-
* @param {number} [options.to] - Last, inclusive; below `from` means no end.
|
|
87
|
-
* @param {object} [options.process] - The process it should own.
|
|
88
|
-
* @param {boolean} [options.producing] - Whether it has already made its first
|
|
89
|
-
* segment, which is what moves it out of starting.
|
|
90
|
-
* @param {number | null} [options.lastSegmentIndex] - The film's last number,
|
|
91
|
-
* for telling a finished file from an input that dried up.
|
|
92
|
-
* @param {boolean} [options.usesExplicitCuts]
|
|
93
|
-
* @returns {import("../../services/encode/EncodeRun.js").EncodeRun}
|
|
94
|
-
*/
|
|
95
|
-
export function startRunOn(session, options = {}) {
|
|
96
|
-
const {
|
|
97
|
-
from = 0,
|
|
98
|
-
to = -1,
|
|
99
|
-
process: child = fakeProcess(),
|
|
100
|
-
producing = true,
|
|
101
|
-
lastSegmentIndex = null,
|
|
102
|
-
usesExplicitCuts = false
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @file A real `EncodeRun` over a process that is not real.
|
|
3
|
+
*
|
|
4
|
+
* Tests used to set a session's process fields by hand — `session.ffmpeg`,
|
|
5
|
+
* `session.runState`, `session.encodeStartIndex` — which is how a test can pass
|
|
6
|
+
* over code that no longer works: the fields were the thing under test as much
|
|
7
|
+
* as the behaviour was. A run is an object now, so a test builds the object the
|
|
8
|
+
* product builds and injects only the one thing a test may not have, which is a
|
|
9
|
+
* child process.
|
|
10
|
+
*
|
|
11
|
+
* The pid is deliberately absent unless a test asks for one. `pause` and
|
|
12
|
+
* `resume` send signals by pid, and a made-up number is somebody else's process
|
|
13
|
+
* on the machine running the tests.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { EncodeRun } from "../../services/encode/EncodeRun.js";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A child process that records what was done to it.
|
|
20
|
+
*
|
|
21
|
+
* @param {object} [options]
|
|
22
|
+
* @param {number | null} [options.pid] - Only where a test genuinely exercises
|
|
23
|
+
* suspend or resume, and then it must be this process's own.
|
|
24
|
+
* @returns {object}
|
|
25
|
+
*/
|
|
26
|
+
export function fakeProcess({ pid = null, exitsWhenKilled = true } = {}) {
|
|
27
|
+
/** @type {Map<string, (...args: unknown[]) => void>} */
|
|
28
|
+
const listeners = new Map();
|
|
29
|
+
return {
|
|
30
|
+
pid,
|
|
31
|
+
killed: false,
|
|
32
|
+
exitCode: null,
|
|
33
|
+
signalCode: null,
|
|
34
|
+
/** Every signal it was sent, in order. */
|
|
35
|
+
signals: [],
|
|
36
|
+
stdout: { on() {} },
|
|
37
|
+
stderr: { on() {} },
|
|
38
|
+
on(event, handler) {
|
|
39
|
+
const kept = listeners.get(event) ?? [];
|
|
40
|
+
kept.push(handler);
|
|
41
|
+
listeners.set(event, kept);
|
|
42
|
+
return this;
|
|
43
|
+
},
|
|
44
|
+
once(event, handler) {
|
|
45
|
+
return this.on(event, handler);
|
|
46
|
+
},
|
|
47
|
+
kill(signal = "SIGTERM") {
|
|
48
|
+
this.signals.push(signal);
|
|
49
|
+
this.killed = true;
|
|
50
|
+
// A real process answers a signal by exiting, and it does so on a later
|
|
51
|
+
// turn. A fake that never exits makes every disposal wait out the grace
|
|
52
|
+
// period, which is two seconds a test spends proving nothing.
|
|
53
|
+
if (exitsWhenKilled && this.exitCode === null && this.signalCode === null) {
|
|
54
|
+
queueMicrotask(() => this.exit(null, signal));
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
57
|
+
},
|
|
58
|
+
/**
|
|
59
|
+
* Report an exit, as the real thing would.
|
|
60
|
+
*
|
|
61
|
+
* @param {number | null} code
|
|
62
|
+
* @param {string | null} [signal]
|
|
63
|
+
*/
|
|
64
|
+
exit(code, signal = null) {
|
|
65
|
+
this.exitCode = code;
|
|
66
|
+
this.signalCode = signal;
|
|
67
|
+
for (const handler of listeners.get("exit") ?? []) {
|
|
68
|
+
handler(code, signal);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A logger that says nothing, for tests that are not about the log. */
|
|
75
|
+
export const silentLogger = { info() {}, warn() {}, error() {} };
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Add a run to a session, started, over a fake process.
|
|
79
|
+
*
|
|
80
|
+
* A session holds a SET of runs — as many as the machine affords — so a test
|
|
81
|
+
* that wants two heads on one output calls this twice.
|
|
82
|
+
*
|
|
83
|
+
* @param {object} session - The session under test.
|
|
84
|
+
* @param {object} [options]
|
|
85
|
+
* @param {number} [options.from] - First segment number it is making.
|
|
86
|
+
* @param {number} [options.to] - Last, inclusive; below `from` means no end.
|
|
87
|
+
* @param {object} [options.process] - The process it should own.
|
|
88
|
+
* @param {boolean} [options.producing] - Whether it has already made its first
|
|
89
|
+
* segment, which is what moves it out of starting.
|
|
90
|
+
* @param {number | null} [options.lastSegmentIndex] - The film's last number,
|
|
91
|
+
* for telling a finished file from an input that dried up.
|
|
92
|
+
* @param {boolean} [options.usesExplicitCuts]
|
|
93
|
+
* @returns {import("../../services/encode/EncodeRun.js").EncodeRun}
|
|
94
|
+
*/
|
|
95
|
+
export function startRunOn(session, options = {}) {
|
|
96
|
+
const {
|
|
97
|
+
from = 0,
|
|
98
|
+
to = -1,
|
|
99
|
+
process: child = fakeProcess(),
|
|
100
|
+
producing = true,
|
|
101
|
+
lastSegmentIndex = null,
|
|
102
|
+
usesExplicitCuts = false,
|
|
103
|
+
// WHAT FFMPEG HAS REPORTED IT IS RUNNING AT. It says so on its progress
|
|
104
|
+
// channel from the first seconds, before any piece closes — so a run can be
|
|
105
|
+
// starting and already have a speed, and every real one does. Left at zero,
|
|
106
|
+
// the plan has no speed to compute an arrival from, every arrangement of
|
|
107
|
+
// encoders is equally hopeless, they all tie, and the encoder is taken away
|
|
108
|
+
// for changing nothing.
|
|
109
|
+
speedX = 0
|
|
110
|
+
} = options;
|
|
111
|
+
const run = new EncodeRun({
|
|
112
|
+
address: session.outputKey ?? session.id ?? "output",
|
|
113
|
+
encoder: { name: "libx264", kind: "software" },
|
|
114
|
+
from,
|
|
115
|
+
to,
|
|
116
|
+
buildArgs: () => [],
|
|
117
|
+
spawn: () => child,
|
|
118
|
+
logger: silentLogger,
|
|
119
|
+
lastSegmentIndex: () => lastSegmentIndex,
|
|
120
|
+
usesExplicitCuts
|
|
121
|
+
});
|
|
122
|
+
run.start("a test asked for it");
|
|
123
|
+
run.noteSpeed(speedX);
|
|
124
|
+
if (producing) {
|
|
125
|
+
// What moves a run out of starting is its first segment, in the product as
|
|
126
|
+
// here — so a run that is producing has made one, and its head stands one
|
|
127
|
+
// past where it began. `from` is still where it started, which is what a
|
|
128
|
+
// test asserting a run's position asks for.
|
|
129
|
+
run.noteProduced(from);
|
|
130
|
+
}
|
|
131
|
+
if (!(session.runs instanceof Set)) {
|
|
132
|
+
session.runs = new Set();
|
|
133
|
+
}
|
|
134
|
+
session.runs.add(run);
|
|
135
|
+
return run;
|
|
136
|
+
}
|
|
@@ -49,7 +49,7 @@ function sessionOn({ id, dirPath, encodeStartIndex = 0, runEndIndex = -1, speed
|
|
|
49
49
|
cutGrid: "uniform"
|
|
50
50
|
}),
|
|
51
51
|
state: "ready",
|
|
52
|
-
file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "video.mkv" }),
|
|
52
|
+
file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "video.mkv" }).learn({ durationSeconds: 4000 }),
|
|
53
53
|
// An ordinary session reads its own file, and its sound is inside it. The
|
|
54
54
|
// three differ only for a soundtrack shipped as a file of its own.
|
|
55
55
|
get inputFile() { return this.file; },
|
|
@@ -99,10 +99,14 @@ test("what a viewer waits for reaches the plan without their name", (t) => {
|
|
|
99
99
|
|
|
100
100
|
manager.planEncodersNow();
|
|
101
101
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
102
|
+
// THE MAP IS BANDS NOW, not one window per viewer: what a viewer is stopped
|
|
103
|
+
// on, then what is in front of them band by band, then the rest of the track.
|
|
104
|
+
// The check is the same question asked of that shape — where the most urgent
|
|
105
|
+
// band begins, and that there is film in front of it.
|
|
106
|
+
const wanted = manager.encodeOrchestrator.demand.mapOn(KEY);
|
|
107
|
+
const first = [...wanted].sort((left, right) => right.priority - left.priority)[0];
|
|
108
|
+
assert.equal(first.from, 5, "where they are");
|
|
109
|
+
assert.ok(wanted.some((zone) => zone.to > 5), "and the cushion in front of them");
|
|
106
110
|
});
|
|
107
111
|
|
|
108
112
|
test("a viewer nothing has been heard from at all stops being waited for", (t) => {
|
|
@@ -120,7 +124,7 @@ test("a viewer nothing has been heard from at all stops being waited for", (t) =
|
|
|
120
124
|
|
|
121
125
|
manager.planEncodersNow();
|
|
122
126
|
|
|
123
|
-
assert.equal(manager.encodeOrchestrator.demand.
|
|
127
|
+
assert.equal(manager.encodeOrchestrator.demand.mapOn(KEY).length, 0);
|
|
124
128
|
});
|
|
125
129
|
|
|
126
130
|
test("a viewer who has arrived and asked for nothing is waited for", (t) => {
|
|
@@ -139,9 +143,10 @@ test("a viewer who has arrived and asked for nothing is waited for", (t) => {
|
|
|
139
143
|
|
|
140
144
|
manager.planEncodersNow();
|
|
141
145
|
|
|
142
|
-
const wanted = manager.encodeOrchestrator.demand.
|
|
143
|
-
assert.
|
|
144
|
-
|
|
146
|
+
const wanted = manager.encodeOrchestrator.demand.mapOn(KEY);
|
|
147
|
+
assert.ok(wanted.length > 0, "an output with a viewer on it is wanted");
|
|
148
|
+
const first = [...wanted].sort((left, right) => right.priority - left.priority)[0];
|
|
149
|
+
assert.equal(first.from, 0, "and the beginning is where an unplaced viewer is");
|
|
145
150
|
});
|
|
146
151
|
|
|
147
152
|
test("how many encoders the machine affords is measured, not chosen", (t) => {
|
|
@@ -155,7 +160,7 @@ test("how many encoders the machine affords is measured, not chosen", (t) => {
|
|
|
155
160
|
|
|
156
161
|
// Fast, but what a second job costs on THIS machine has not been measured,
|
|
157
162
|
// and an unmeasured penalty of 1 is not a statement that it is free.
|
|
158
|
-
cold.
|
|
163
|
+
cold.lastAloneSpeed = 7.12;
|
|
159
164
|
assert.equal(manager.maxRunsForOutput(KEY), 1, "no measurement, no second encoder");
|
|
160
165
|
|
|
161
166
|
// Measured on the addon host 2026-09-03: at 854x480 one run made 7.12x and
|
|
@@ -166,7 +171,7 @@ test("how many encoders the machine affords is measured, not chosen", (t) => {
|
|
|
166
171
|
|
|
167
172
|
// The same host at 1920x1080: one made 1.96x, two made 0.99x and 0.98x.
|
|
168
173
|
manager.contentionPenalties = new Map([[1, 1.98]]);
|
|
169
|
-
cold.
|
|
174
|
+
cold.lastAloneSpeed = 1.96;
|
|
170
175
|
assert.equal(manager.maxRunsForOutput(KEY), 1, "the machine is full at one");
|
|
171
176
|
});
|
|
172
177
|
|