@torrent-tv/proxy 2.80.9 → 2.80.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/CHANGELOG.md +1685 -1666
  2. package/package.json +1 -1
  3. package/services/data-channel-handler.js +2 -4
  4. package/services/encode/EncodePlan.js +0 -3
  5. package/services/encode/EncodeRun.js +14 -0
  6. package/services/encode/run-command.js +729 -707
  7. package/services/hls-session-manager.js +10168 -10494
  8. package/services/hwaccel.js +2 -2
  9. package/services/orchestrators/EncodeOrchestrator.js +4 -4
  10. package/services/output/LiveOutputs.js +49 -0
  11. package/services/priority/PriorityOrchestrator.js +278 -174
  12. package/services/quality/EncodeCost.js +555 -555
  13. package/test/auto-quality-step.test.js +514 -507
  14. package/test/contention.test.js +1 -1
  15. package/test/decode-cost-fit.test.js +57 -0
  16. package/test/decode-cost.test.js +13 -36
  17. package/test/encode-orchestrator.test.js +1 -2
  18. package/test/encode-plan-viewers.test.js +18 -19
  19. package/test/encode-plan.test.js +539 -539
  20. package/test/held-request-width.test.js +155 -154
  21. package/test/helpers/encode-run.js +136 -128
  22. package/test/keyframe-table-budget.test.js +7 -1
  23. package/test/one-authority.test.js +220 -105
  24. package/test/orchestrator-wired.test.js +16 -11
  25. package/test/piece-store-slow-disk.test.js +32 -2
  26. package/test/priority-map-per-output.test.js +211 -0
  27. package/test/produced-copy-choice.test.js +358 -327
  28. package/test/quality-variants.test.js +1222 -1209
  29. package/test/segment-serve-wiring.test.js +76 -20
  30. package/test/stale-request-after-seek.test.js +51 -77
  31. package/test/tracks-begin-together.test.js +195 -153
  32. package/test/tunnel-renewal.test.js +34 -5
  33. package/test/two-viewers-one-picture.test.js +374 -347
  34. package/test/viewer-outputs.test.js +18 -14
  35. package/test/behind-head-repair.test.js +0 -261
  36. package/test/decode-measurement.test.js +0 -83
  37. /package/services/{contention.js → encode/contention.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
- const session = manager.sessionsById.get(SESSION_ID);
105
-
106
- // Both are watching the same copied picture, so both are this one session.
107
- // One is at segment #25, the other far ahead at #150 — inside the fixture's
108
- // own 200-segment timeline, since a position past the end of the grid is
109
- // clamped to it and would prove nothing about the width.
110
- manager.requestSeek(SESSION_ID, 100, "behind");
111
- manager.requestSeek(SESSION_ID, 600, "ahead");
112
-
113
- // The shared field now holds the leader's position, which is what the
114
- // encoder is steered by and what every earlier release judged BOTH of them
115
- // against.
116
- assert.equal(session.furthestViewerSeconds, 600);
117
-
118
- assert.equal(
119
- manager.requestStillWanted(SESSION_ID, segment(26), "behind"),
120
- true,
121
- "the segment the viewer behind is waiting for is still theirs to wait for"
122
- );
123
- assert.equal(
124
- manager.requestStillWanted(SESSION_ID, segment(26), "ahead"),
125
- false,
126
- "and for the one in front it is a place they have left"
127
- );
128
- });
129
-
130
- test("a viewer's own head moves with their seek", async (t) => {
131
- const { manager, dirPath } = await managerWithSession();
132
- t.after(async () => {
133
- await rm(dirPath, { recursive: true, force: true });
134
- });
135
-
136
- // Their last request was at #25; they jump to 600 s, which is #150. The
137
- // segment at the target must be wanted refusing it there is the freeze of
138
- // 2026-08-18.
139
- manager.requestSeek(SESSION_ID, 600, "viewer");
140
- assert.equal(manager.requestStillWanted(SESSION_ID, segment(150), "viewer"), true);
141
- assert.equal(manager.requestStillWanted(SESSION_ID, segment(25), "viewer"), false);
142
- });
143
-
144
- test("a viewer nobody can name is judged against the one shared position", async (t) => {
145
- const { manager, dirPath } = await managerWithSession();
146
- t.after(async () => {
147
- await rm(dirPath, { recursive: true, force: true });
148
- });
149
-
150
- // A plain HTTP transport builds its own URLs and carries no id. The old
151
- // behaviour is what remains, which for a single viewer is the same thing.
152
- assert.equal(manager.requestStillWanted(SESSION_ID, segment(26), ""), true);
153
- assert.equal(manager.requestStillWanted(SESSION_ID, segment(24), ""), false);
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
- } = options;
104
- const run = new EncodeRun({
105
- address: session.outputKey ?? session.id ?? "output",
106
- encoder: { name: "libx264", kind: "software" },
107
- from,
108
- to,
109
- buildArgs: () => [],
110
- spawn: () => child,
111
- logger: silentLogger,
112
- lastSegmentIndex: () => lastSegmentIndex,
113
- usesExplicitCuts
114
- });
115
- run.start("a test asked for it");
116
- if (producing) {
117
- // What moves a run out of starting is its first segment, in the product as
118
- // here — so a run that is producing has made one, and its head stands one
119
- // past where it began. `from` is still where it started, which is what a
120
- // test asserting a run's position asks for.
121
- run.noteProduced(from);
122
- }
123
- if (!(session.runs instanceof Set)) {
124
- session.runs = new Set();
125
- }
126
- session.runs.add(run);
127
- return run;
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
+ }
@@ -79,7 +79,13 @@ test("the read goes on after the budget, so the next session of the file gets th
79
79
  assert.equal(first.arrived, false);
80
80
 
81
81
  answerLate({ times: [0, 5, 10], tolerance: 0, format: "matroska" });
82
- await new Promise((resolve) => setTimeout(resolve, 10));
82
+ // Every microtask this resolution queues, and not a chosen ten milliseconds.
83
+ // `setImmediate` runs after the whole microtask queue of this turn, so the
84
+ // answer has been taken in by the time it fires — where the ten milliseconds
85
+ // were a guess about how long that takes on whatever machine is running.
86
+ // Asking the reader again instead would have counted as another read, which
87
+ // is the very thing the last assertion here is about.
88
+ await new Promise((resolve) => { setImmediate(resolve); });
83
89
 
84
90
  const second = await sessions.readKeyframeTableWithin(FILE);
85
91
  assert.deepEqual(second.times, [0, 5, 10], "the late answer was kept, not thrown away");