@torrent-tv/proxy 2.76.6 → 2.78.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.
@@ -1,164 +1,190 @@
1
- /**
2
- * @file The plan is what decides the encoders, and it is asked of real sessions.
3
- *
4
- * Three rules written into the session manager become one here: where a run
5
- * belongs, when it has been overtaken, and how many the machine affords. The
6
- * point of wiring rather than duplicating is that the arithmetic has sixteen
7
- * checks of its own and no ffmpeg behind it.
8
- */
9
-
10
- import test from "node:test";
11
- import assert from "node:assert/strict";
12
- import { SourceFile } from "../services/source/SourceFile.js";
13
- import { Timeline } from "../services/output/Timeline.js";
14
- import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
15
- import os from "node:os";
16
- import path from "node:path";
17
- import { HlsSessionManager } from "../services/hls-session-manager.js";
18
- import { SegmentStore } from "../services/encode/SegmentStore.js";
19
- import { fmp4Format } from "../services/segment-formats/fmp4.js";
20
- import { viewerOf } from "../services/viewer/Viewer.js";
21
- import { startRunOn } from "./helpers/encode-run.js";
22
-
23
- const KEY = "torrent:abc:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
24
-
25
- function managerWithAnOutput() {
26
- const root = mkdtempSync(path.join(os.tmpdir(), "orchestrator-wired-"));
27
- const store = new SegmentStore({ root });
28
- const manager = new HlsSessionManager({
29
- enabled: true,
30
- ffmpegBin: "ffmpeg",
31
- localBindHost: "127.0.0.1",
32
- localPort: 9090,
33
- segmentStore: store
34
- });
35
- const dirPath = store.directoryFor(KEY);
36
- store.useFormat(KEY, fmp4Format);
37
- return { manager, store, root, dirPath };
38
- }
39
-
40
- function sessionOn({ id, dirPath, encodeStartIndex = 0, runEndIndex = -1, speed = 0, running = true }) {
41
- const session = {
42
- id,
43
- outputKey: KEY,
44
- dirPath,
45
- // Where this file is cut, held by the file. A fixture that stated it
46
- // on the session was describing what production no longer does.
47
- timeline: new Timeline({
48
- boundaries: Array.from({ length: 1001 }, (_, index) => index * 4),
49
- cutGrid: "uniform"
50
- }),
51
- state: "ready",
52
- file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "video.mkv" }),
53
- // An ordinary session reads its own file, and its sound is inside it. The
54
- // three differ only for a soundtrack shipped as a file of its own.
55
- get inputFile() { return this.file; },
56
- get audioFile() { return this.file; },
57
- segmentFormat: fmp4Format,
58
- segmentCount: 1000,
59
- recentSpeed: null,
60
- consumers: new Set(),
61
- viewers: new Map(),
62
- runs: new Set(),
63
- lastAccessedAt: Date.now()
64
- };
65
- if (running) {
66
- const run = startRunOn(session, { from: encodeStartIndex, to: runEndIndex });
67
- // A speed is a reading taken FROM a run, so it names the run it came from.
68
- session.recentSpeed = speed > 0 ? { speed, at: Date.now(), run } : null;
69
- run.noteSpeed(speed);
70
- }
71
- return session;
72
- }
73
-
74
- test("a session is handed to the plan as the run it is", (t) => {
75
- const { manager, root, dirPath } = managerWithAnOutput();
76
- t.after(() => rmSync(root, { recursive: true, force: true }));
77
-
78
- const session = sessionOn({ id: "one", dirPath, encodeStartIndex: 10, runEndIndex: 40 });
79
- manager.sessionsById.set(session.id, session);
80
- viewerOf(session, "watching").head = { segment: 12, seconds: 48, at: Date.now() };
81
-
82
- manager.runQualityBudgetOnce;
83
- manager.planEncodersNow();
84
-
85
- const runs = manager.encodeOrchestrator.runsOn(KEY);
86
- assert.equal(runs.length, 1, "the session the browser already has is a run like any other");
87
- assert.equal(runs[0], [...session.runs][0], "and it is the run the session actually holds");
88
- assert.equal(runs[0].from, 10);
89
- assert.equal(runs[0].to, 40);
90
- });
91
-
92
- test("what a viewer waits for reaches the plan without their name", (t) => {
93
- const { manager, root, dirPath } = managerWithAnOutput();
94
- t.after(() => rmSync(root, { recursive: true, force: true }));
95
-
96
- const session = sessionOn({ id: "one", dirPath, encodeStartIndex: 0, runEndIndex: -1 });
97
- manager.sessionsById.set(session.id, session);
98
- viewerOf(session, "someone").head = { segment: 5, seconds: 20, at: Date.now() };
99
-
100
- manager.planEncodersNow();
101
-
102
- const wanted = manager.encodeOrchestrator.demand.windowsOn(KEY);
103
- assert.equal(wanted.length, 1);
104
- assert.equal(wanted[0].from, 5, "where they are");
105
- assert.ok(wanted[0].to > 5, "and the cushion in front of them");
106
- });
107
-
108
- test("a viewer who has gone quiet stops being waited for", (t) => {
109
- const { manager, root, dirPath } = managerWithAnOutput();
110
- t.after(() => rmSync(root, { recursive: true, force: true }));
111
-
112
- const session = sessionOn({ id: "one", dirPath });
113
- manager.sessionsById.set(session.id, session);
114
- viewerOf(session, "gone").head = { segment: 5, seconds: 20, at: Date.now() - 10 * 60 * 1000 };
115
-
116
- manager.planEncodersNow();
117
-
118
- assert.equal(manager.encodeOrchestrator.demand.windowsOn(KEY).length, 0);
119
- });
120
-
121
- test("how many encoders the machine affords is measured, not chosen", (t) => {
122
- const { manager, root, dirPath } = managerWithAnOutput();
123
- t.after(() => rmSync(root, { recursive: true, force: true }));
124
-
125
- // Nothing measured yet: one is what it has.
126
- const cold = sessionOn({ id: "cold", dirPath });
127
- manager.sessionsById.set(cold.id, cold);
128
- assert.equal(manager.maxRunsForOutput(KEY), 1);
129
-
130
- // Fast, but what a second job costs on THIS machine has not been measured,
131
- // and an unmeasured penalty of 1 is not a statement that it is free.
132
- cold.recentSpeed = { speed: 7.12 };
133
- assert.equal(manager.maxRunsForOutput(KEY), 1, "no measurement, no second encoder");
134
-
135
- // Measured on the addon host 2026-09-03: at 854x480 one run made 7.12x and
136
- // two made 4.20x and 4.16x, a penalty of 1.70x, and both stayed far above
137
- // realtime.
138
- manager.contentionPenalties = new Map([[1, 1.7]]);
139
- assert.ok(manager.maxRunsForOutput(KEY) > 1, "measured, and a second fits");
140
-
141
- // The same host at 1920x1080: one made 1.96x, two made 0.99x and 0.98x.
142
- manager.contentionPenalties = new Map([[1, 1.98]]);
143
- cold.recentSpeed = { speed: 1.96 };
144
- assert.equal(manager.maxRunsForOutput(KEY), 1, "the machine is full at one");
145
- });
146
-
147
- test("segments already made are known to the plan, whoever made them", (t) => {
148
- const { manager, store, root, dirPath } = managerWithAnOutput();
149
- t.after(() => rmSync(root, { recursive: true, force: true }));
150
-
151
- for (const index of [0, 1, 2, 3]) {
152
- writeFileSync(path.join(dirPath, fmp4Format.segmentFileName(index)), Buffer.alloc(16, 1));
153
- }
154
- const session = sessionOn({ id: "one", dirPath });
155
- manager.sessionsById.set(session.id, session);
156
-
157
- manager.planEncodersNow();
158
-
159
- const coverage = manager.encodeOrchestrator.coverageOf(KEY);
160
- assert.equal(coverage.isReady(0), true);
161
- assert.equal(coverage.isReady(2), true);
162
- assert.equal(coverage.isReady(3), false, "the highest has no successor to prove it closed");
163
- void store;
164
- });
1
+ /**
2
+ * @file The plan is what decides the encoders, and it is asked of real sessions.
3
+ *
4
+ * Three rules written into the session manager become one here: where a run
5
+ * belongs, when it has been overtaken, and how many the machine affords. The
6
+ * point of wiring rather than duplicating is that the arithmetic has sixteen
7
+ * checks of its own and no ffmpeg behind it.
8
+ */
9
+
10
+ import test from "node:test";
11
+ import assert from "node:assert/strict";
12
+ import { SourceFile } from "../services/source/SourceFile.js";
13
+ import { Timeline } from "../services/output/Timeline.js";
14
+ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
15
+ import os from "node:os";
16
+ import path from "node:path";
17
+ import { HlsSessionManager } from "../services/hls-session-manager.js";
18
+ import { SegmentStore } from "../services/encode/SegmentStore.js";
19
+ import { fmp4Format } from "../services/segment-formats/fmp4.js";
20
+ import { viewerOf } from "../services/viewer/Viewer.js";
21
+ import { startRunOn } from "./helpers/encode-run.js";
22
+
23
+ const KEY = "torrent:abc:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
24
+
25
+ function managerWithAnOutput() {
26
+ const root = mkdtempSync(path.join(os.tmpdir(), "orchestrator-wired-"));
27
+ const store = new SegmentStore({ root });
28
+ const manager = new HlsSessionManager({
29
+ enabled: true,
30
+ ffmpegBin: "ffmpeg",
31
+ localBindHost: "127.0.0.1",
32
+ localPort: 9090,
33
+ segmentStore: store
34
+ });
35
+ const dirPath = store.directoryFor(KEY);
36
+ store.useFormat(KEY, fmp4Format);
37
+ return { manager, store, root, dirPath };
38
+ }
39
+
40
+ function sessionOn({ id, dirPath, encodeStartIndex = 0, runEndIndex = -1, speed = 0, running = true }) {
41
+ const session = {
42
+ id,
43
+ outputKey: KEY,
44
+ dirPath,
45
+ // Where this file is cut, held by the file. A fixture that stated it
46
+ // on the session was describing what production no longer does.
47
+ timeline: new Timeline({
48
+ boundaries: Array.from({ length: 1001 }, (_, index) => index * 4),
49
+ cutGrid: "uniform"
50
+ }),
51
+ state: "ready",
52
+ file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "video.mkv" }),
53
+ // An ordinary session reads its own file, and its sound is inside it. The
54
+ // three differ only for a soundtrack shipped as a file of its own.
55
+ get inputFile() { return this.file; },
56
+ get audioFile() { return this.file; },
57
+ segmentFormat: fmp4Format,
58
+ segmentCount: 1000,
59
+ recentSpeed: null,
60
+ consumers: new Set(),
61
+ viewers: new Map(),
62
+ runs: new Set(),
63
+ lastAccessedAt: Date.now()
64
+ };
65
+ if (running) {
66
+ const run = startRunOn(session, { from: encodeStartIndex, to: runEndIndex });
67
+ // A speed is a reading taken FROM a run, so it names the run it came from.
68
+ session.recentSpeed = speed > 0 ? { speed, at: Date.now(), run } : null;
69
+ run.noteSpeed(speed);
70
+ }
71
+ return session;
72
+ }
73
+
74
+ test("a session is handed to the plan as the run it is", (t) => {
75
+ const { manager, root, dirPath } = managerWithAnOutput();
76
+ t.after(() => rmSync(root, { recursive: true, force: true }));
77
+
78
+ const session = sessionOn({ id: "one", dirPath, encodeStartIndex: 10, runEndIndex: 40 });
79
+ manager.sessionsById.set(session.id, session);
80
+ viewerOf(session, "watching").position = { segment: 12, seconds: 48, at: Date.now() };
81
+
82
+ manager.runQualityBudgetOnce;
83
+ manager.planEncodersNow();
84
+
85
+ const runs = manager.encodeOrchestrator.runsOn(KEY);
86
+ assert.equal(runs.length, 1, "the session the browser already has is a run like any other");
87
+ assert.equal(runs[0], [...session.runs][0], "and it is the run the session actually holds");
88
+ assert.equal(runs[0].from, 10);
89
+ assert.equal(runs[0].to, 40);
90
+ });
91
+
92
+ test("what a viewer waits for reaches the plan without their name", (t) => {
93
+ const { manager, root, dirPath } = managerWithAnOutput();
94
+ t.after(() => rmSync(root, { recursive: true, force: true }));
95
+
96
+ const session = sessionOn({ id: "one", dirPath, encodeStartIndex: 0, runEndIndex: -1 });
97
+ manager.sessionsById.set(session.id, session);
98
+ viewerOf(session, "someone").position = { segment: 5, seconds: 20, at: Date.now() };
99
+
100
+ manager.planEncodersNow();
101
+
102
+ const wanted = manager.encodeOrchestrator.demand.windowsOn(KEY);
103
+ assert.equal(wanted.length, 1);
104
+ assert.equal(wanted[0].from, 5, "where they are");
105
+ assert.ok(wanted[0].to > 5, "and the cushion in front of them");
106
+ });
107
+
108
+ test("a viewer nothing has been heard from at all stops being waited for", (t) => {
109
+ const { manager, root, dirPath } = managerWithAnOutput();
110
+ t.after(() => rmSync(root, { recursive: true, force: true }));
111
+
112
+ const session = sessionOn({ id: "one", dirPath });
113
+ manager.sessionsById.set(session.id, session);
114
+ const gone = viewerOf(session, "gone");
115
+ gone.position = { segment: 5, seconds: 20, at: Date.now() - 10 * 60 * 1000 };
116
+ // What decides it is when they were last HEARD FROM, not when they last
117
+ // asked for a segment: a viewer holding a full cushion asks for nothing for
118
+ // as long as it takes to drain, and is no less present for it.
119
+ gone.seen(Date.now() - 10 * 60 * 1000);
120
+
121
+ manager.planEncodersNow();
122
+
123
+ assert.equal(manager.encodeOrchestrator.demand.windowsOn(KEY).length, 0);
124
+ });
125
+
126
+ test("a viewer who has arrived and asked for nothing is waited for", (t) => {
127
+ const { manager, root, dirPath } = managerWithAnOutput();
128
+ t.after(() => rmSync(root, { recursive: true, force: true }));
129
+
130
+ const session = sessionOn({ id: "one", dirPath });
131
+ manager.sessionsById.set(session.id, session);
132
+ // Nothing places them here on purpose: this is a viewer known to the output
133
+ // and nothing more, which is what a viewer IS between arriving and asking
134
+ // for their first file. Answered from the position alone, this viewer read
135
+ // as absent, and on 2026-09-05 that stopped a soundtrack's encoder 1.25 s
136
+ // after it started, with nothing produced.
137
+ const fresh = manager.viewers.of(session, "fresh");
138
+ assert.equal(fresh.position, null, "nobody has placed them");
139
+
140
+ manager.planEncodersNow();
141
+
142
+ const wanted = manager.encodeOrchestrator.demand.windowsOn(KEY);
143
+ assert.equal(wanted.length, 1, "an output with a viewer on it is wanted");
144
+ assert.equal(wanted[0].from, 0, "and the beginning is where an unplaced viewer is");
145
+ });
146
+
147
+ test("how many encoders the machine affords is measured, not chosen", (t) => {
148
+ const { manager, root, dirPath } = managerWithAnOutput();
149
+ t.after(() => rmSync(root, { recursive: true, force: true }));
150
+
151
+ // Nothing measured yet: one is what it has.
152
+ const cold = sessionOn({ id: "cold", dirPath });
153
+ manager.sessionsById.set(cold.id, cold);
154
+ assert.equal(manager.maxRunsForOutput(KEY), 1);
155
+
156
+ // Fast, but what a second job costs on THIS machine has not been measured,
157
+ // and an unmeasured penalty of 1 is not a statement that it is free.
158
+ cold.recentSpeed = { speed: 7.12 };
159
+ assert.equal(manager.maxRunsForOutput(KEY), 1, "no measurement, no second encoder");
160
+
161
+ // Measured on the addon host 2026-09-03: at 854x480 one run made 7.12x and
162
+ // two made 4.20x and 4.16x, a penalty of 1.70x, and both stayed far above
163
+ // realtime.
164
+ manager.contentionPenalties = new Map([[1, 1.7]]);
165
+ assert.ok(manager.maxRunsForOutput(KEY) > 1, "measured, and a second fits");
166
+
167
+ // The same host at 1920x1080: one made 1.96x, two made 0.99x and 0.98x.
168
+ manager.contentionPenalties = new Map([[1, 1.98]]);
169
+ cold.recentSpeed = { speed: 1.96 };
170
+ assert.equal(manager.maxRunsForOutput(KEY), 1, "the machine is full at one");
171
+ });
172
+
173
+ test("segments already made are known to the plan, whoever made them", (t) => {
174
+ const { manager, store, root, dirPath } = managerWithAnOutput();
175
+ t.after(() => rmSync(root, { recursive: true, force: true }));
176
+
177
+ for (const index of [0, 1, 2, 3]) {
178
+ writeFileSync(path.join(dirPath, fmp4Format.segmentFileName(index)), Buffer.alloc(16, 1));
179
+ }
180
+ const session = sessionOn({ id: "one", dirPath });
181
+ manager.sessionsById.set(session.id, session);
182
+
183
+ manager.planEncodersNow();
184
+
185
+ const coverage = manager.encodeOrchestrator.coverageOf(KEY);
186
+ assert.equal(coverage.isReady(0), true);
187
+ assert.equal(coverage.isReady(2), true);
188
+ assert.equal(coverage.isReady(3), false, "the highest has no successor to prove it closed");
189
+ void store;
190
+ });
@@ -78,8 +78,8 @@ function fakeSession({ id, encodeHeight, dirPath, transcodeVideo = true }) {
78
78
  output: new Output({ encodeWidth: 0, encodeHeight, outputFps: 24, softwarePreset: null, applyTonemap: false }),
79
79
  encodeRunGeneration: 0,
80
80
  lastRestartAt: 0,
81
- seekFailureTarget: -1,
82
- seekFailureCount: 0,
81
+ failedStartAt: -1,
82
+ failedStartCount: 0,
83
83
  seekSettleTimer: null,
84
84
  seekTarget: null,
85
85
  waitEpoch: 0,