@torrent-tv/proxy 2.80.17 → 2.80.19

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.
@@ -290,12 +290,12 @@ test("a run's FIRST segment is served once the encoder has passed it, without wa
290
290
  // successor and nothing is producing one. Waiting for that successor is what
291
291
  // held #317 for 46 s and then answered 404 to a browser that had given up.
292
292
  await rm(path.join(dirPath, "segment-00001.mp4"));
293
- // WHAT PROVES IT INSTEAD: the encoder named it. ffmpeg writes the name of each
294
- // piece on a channel of its own as it closes it, so a run's own first piece is
295
- // proven the moment it is finished and the absence of a next one says nothing.
296
- // This used to be inferred from the encoder's reported position instead, which
297
- // is a different question where it has read to, not what it has closed.
298
- manager.segmentStore.markClosed(OUTPUT_KEY, 0);
293
+ // WHAT PROVES IT INSTEAD: its own NAME. A piece is called something else while
294
+ // it is being written and takes its served name when its writer says it is
295
+ // closed, so a run's first piece is proven the moment it is finished and the
296
+ // absence of a next one says nothing. This used to be inferred from the
297
+ // encoder's reported position — a different question, where it has read to
298
+ // rather than what it has closed.
299
299
 
300
300
  const result = await manager.getFileStream(SESSION_ID, "segment-00000.mp4", { requestSeq: 1 });
301
301
 
@@ -345,10 +345,9 @@ test("serving a run's own segment moves the run out of STARTING", async (t) => {
345
345
  // ITS OWN PIECE, AND FILM STILL TO MAKE BEHIND IT. A run standing on material
346
346
  // that already exists is moved forward — correctly — so a fixture that wants a
347
347
  // live run has to put it where the work is. #1 is what it is making; #2..#4
348
- // are unmade, so it is wanted; and the encoder has named #1 on its ready
349
- // channel, which is what makes the piece servable at all.
348
+ // are unmade, so it is wanted; and #1 stands under its served name, which is
349
+ // what makes the piece servable at all.
350
350
  const run = startRunOn(session, { from: 1, producing: false, usesExplicitCuts: true, speedX: 2 });
351
- manager.segmentStore.markClosed(OUTPUT_KEY, 1);
352
351
  assert.equal(run.state, ENCODE_RUN_STATE.STARTING);
353
352
  assert.deepEqual(run.produced, [], "it has made nothing yet");
354
353
 
@@ -1,216 +1,238 @@
1
- /**
2
- * @file One store of segments, addressed by what they are, and what it makes of
3
- * what a killed process left behind.
4
- */
5
-
6
- import test from "node:test";
7
- import assert from "node:assert/strict";
8
- import { mkdtempSync, mkdirSync, readdirSync, rmSync, writeFileSync } from "node:fs";
9
- import os from "node:os";
10
- import path from "node:path";
11
- import { directoryNameFor, SegmentStore } from "../services/encode/SegmentStore.js";
12
- import { fmp4Format } from "../services/segment-formats/fmp4.js";
13
-
14
- /**
15
- * @returns {{ store: SegmentStore, root: string, lines: string[] }}
16
- */
17
- function storeInATempRoot() {
18
- const root = mkdtempSync(path.join(os.tmpdir(), "segment-store-"));
19
- const lines = [];
20
- const store = new SegmentStore({
21
- root,
22
- logger: { info: (line) => lines.push(line), warn: (line) => lines.push(line) }
23
- });
24
- return { store, root, lines };
25
- }
26
-
27
- /**
28
- * @param {string} dir
29
- * @param {number} index
30
- * @param {number} bytes
31
- */
32
- function writeSegment(dir, index, bytes = 16) {
33
- writeFileSync(path.join(dir, fmp4Format.segmentFileName(index)), Buffer.alloc(bytes));
34
- }
35
-
36
- const KEY = "torrent:abc:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
37
-
38
- test("two viewers of one output are given the same directory", (t) => {
39
- const { store, root } = storeInATempRoot();
40
- t.after(() => rmSync(root, { recursive: true, force: true }));
41
-
42
- // Which viewer asked never enters it — the address is what the segments are.
43
- assert.equal(store.directoryFor(KEY), store.directoryFor(KEY));
44
- assert.notEqual(store.directoryFor(KEY), store.directoryFor(`${KEY}:other`));
45
- });
46
-
47
- test("the directory says what it holds, so a later process can tell", (t) => {
48
- const { store, root } = storeInATempRoot();
49
- t.after(() => rmSync(root, { recursive: true, force: true }));
50
-
51
- const dir = store.directoryFor(KEY);
52
- const names = readdirSync(dir);
53
- assert.deepEqual(names, ["key.txt"]);
54
- });
55
-
56
- test("a segment is proven closed by the existence of the next one", (t) => {
57
- const { store, root } = storeInATempRoot();
58
- t.after(() => rmSync(root, { recursive: true, force: true }));
59
-
60
- const dir = store.directoryFor(KEY);
61
- store.useFormat(KEY, fmp4Format);
62
- writeSegment(dir, 0);
63
- writeSegment(dir, 1);
64
- writeSegment(dir, 2);
65
-
66
- // The `hls` muxer carries no channel to report on and renames into place when
67
- // it closes a piece, so on that branch existence IS the proof; the same
68
- // answers for the pieces a previous life of this process left behind.
69
- assert.deepEqual(store.provenNumbers(KEY), [0, 1]);
70
- assert.equal(store.unprovenNumber(KEY), 2);
71
- });
72
-
73
- test("a file of no bytes is not a segment, whatever it is called", (t) => {
74
- const { store, root } = storeInATempRoot();
75
- t.after(() => rmSync(root, { recursive: true, force: true }));
76
-
77
- const dir = store.directoryFor(KEY);
78
- store.useFormat(KEY, fmp4Format);
79
- writeSegment(dir, 0);
80
- writeSegment(dir, 1);
81
- writeSegment(dir, 2, 0);
82
-
83
- // What a run killed the moment after opening its next piece leaves behind.
84
- // Counted as a segment once, it closed the only hole in the numbering and
85
- // convinced the look-ahead the encoder had produced it.
86
- assert.equal(store.pathOf(KEY, 2), null);
87
- assert.deepEqual(store.provenNumbers(KEY), [0]);
88
- });
89
-
90
- test("a run's own stretch bounds what it unproves", (t) => {
91
- const { store, root } = storeInATempRoot();
92
- t.after(() => rmSync(root, { recursive: true, force: true }));
93
-
94
- store.useFormat(KEY, fmp4Format);
95
- store.directoryFor(KEY);
96
- for (let index = 0; index <= 5; index += 1) {
97
- store.markClosed(KEY, index);
98
- }
99
-
100
- // A run of #0..#0 rewrites #0 and nothing else. Unbounded, this forgot the
101
- // whole film beyond it — and with readiness a projection of what is proven,
102
- // that is an output declaring itself unmade whenever an encoder starts near
103
- // the beginning, which is a fresh encoder for every segment of it.
104
- //
105
- // Asked with no files on the disk, so the successor rule cannot answer for
106
- // the statements and only the statements are under test.
107
- store.forgetClosed(KEY, 0, 0);
108
- assert.equal(store.isClosed(KEY, 0), false, "the one number it will rewrite");
109
- assert.equal(store.isClosed(KEY, 1), true, "and nothing beyond its stretch");
110
- assert.equal(store.isClosed(KEY, 5), true);
111
-
112
- store.forgetClosed(KEY, 3);
113
- assert.equal(store.isClosed(KEY, 2), true);
114
- assert.equal(store.isClosed(KEY, 3), false, "no end given means to the end of the film");
115
- assert.equal(store.isClosed(KEY, 5), false);
116
- });
117
-
118
- test("a directory that has not moved is not read again", (t) => {
119
- const { store, root } = storeInATempRoot();
120
- t.after(() => rmSync(root, { recursive: true, force: true }));
121
-
122
- const dir = store.directoryFor(KEY);
123
- store.useFormat(KEY, fmp4Format);
124
- writeSegment(dir, 0);
125
- writeSegment(dir, 1);
126
-
127
- const first = store.refresh(KEY);
128
- const second = store.refresh(KEY);
129
- assert.equal(first, second, "the same reading is handed back, not a fresh listing");
130
- });
131
-
132
- test("what a killed process left is found, named and counted", (t) => {
133
- const { store, root, lines } = storeInATempRoot();
134
- t.after(() => rmSync(root, { recursive: true, force: true }));
135
-
136
- const dir = path.join(root, directoryNameFor(KEY));
137
- mkdirSync(dir, { recursive: true });
138
- writeFileSync(path.join(dir, "key.txt"), `${KEY}\n`);
139
- writeSegment(dir, 0);
140
- writeSegment(dir, 1);
141
- const orphan = path.join(root, "0123456789abcdef");
142
- mkdirSync(orphan, { recursive: true });
143
- writeSegment(orphan, 0);
144
-
145
- const swept = store.sweep();
146
- assert.equal(swept.directories, 2);
147
- assert.equal(swept.unidentified, 1, "the one with no key file cannot be matched to a request");
148
- assert.equal(swept.segments, 3);
149
- assert.match(lines.join("\n"), /startup sweep/);
150
- assert.match(lines.join("\n"), /ended\s+without anything recording why/);
151
- });
152
-
153
- test("what survived a kill is taken back, minus the piece nothing proves", (t) => {
154
- const { store, root } = storeInATempRoot();
155
- t.after(() => rmSync(root, { recursive: true, force: true }));
156
-
157
- const dir = path.join(root, directoryNameFor(KEY));
158
- mkdirSync(dir, { recursive: true });
159
- writeFileSync(path.join(dir, "key.txt"), `${KEY}\n`);
160
- for (let index = 0; index <= 5; index += 1) {
161
- writeSegment(dir, index);
162
- }
163
-
164
- const taken = store.adoptWhatSurvived(() => fmp4Format);
165
-
166
- assert.equal(taken.adopted, 1);
167
- assert.equal(taken.unprovenRemoved, 1, "the highest number was being written when the run died");
168
- // Five kept rather than six thrown away: a copied segment's bytes depend only
169
- // on the source, and re-encoding them costs a machine already short of it.
170
- assert.deepEqual(store.provenNumbers(KEY), [0, 1, 2, 3, 4]);
171
- assert.equal(store.pathOf(KEY, 5), null);
172
- });
173
-
174
- test("a directory that cannot name itself is thrown away rather than served", (t) => {
175
- const { store, root } = storeInATempRoot();
176
- t.after(() => rmSync(root, { recursive: true, force: true }));
177
-
178
- const orphan = path.join(root, "0123456789abcdef");
179
- mkdirSync(orphan, { recursive: true });
180
- writeSegment(orphan, 0);
181
-
182
- const taken = store.adoptWhatSurvived(() => fmp4Format);
183
-
184
- assert.equal(taken.adopted, 0);
185
- assert.equal(taken.dropped, 1);
186
- assert.equal(readdirSync(root).length, 0);
187
- });
188
-
189
- test("an output this proxy can no longer serve is thrown away too", (t) => {
190
- const { store, root } = storeInATempRoot();
191
- t.after(() => rmSync(root, { recursive: true, force: true }));
192
-
193
- const dir = path.join(root, directoryNameFor(KEY));
194
- mkdirSync(dir, { recursive: true });
195
- writeFileSync(path.join(dir, "key.txt"), `${KEY}\n`);
196
- writeSegment(dir, 0);
197
-
198
- const taken = store.adoptWhatSurvived(() => null);
199
-
200
- assert.equal(taken.dropped, 1);
201
- assert.equal(readdirSync(root).length, 0);
202
- });
203
-
204
- test("what the store weighs is reported, and dropping one output frees it", (t) => {
205
- const { store, root } = storeInATempRoot();
206
- t.after(() => rmSync(root, { recursive: true, force: true }));
207
-
208
- const dir = store.directoryFor(KEY);
209
- store.useFormat(KEY, fmp4Format);
210
- writeSegment(dir, 0, 1000);
211
- writeSegment(dir, 1, 1000);
212
-
213
- assert.equal(store.stats().bytes, 2000);
214
- store.drop(KEY, "nobody is watching it");
215
- assert.equal(store.stats().outputs, 0);
216
- });
1
+ /**
2
+ * @file One store of segments, addressed by what they are, and what it makes of
3
+ * what a killed process left behind.
4
+ */
5
+
6
+ import test from "node:test";
7
+ import assert from "node:assert/strict";
8
+ import { mkdtempSync, mkdirSync, readdirSync, rmSync, writeFileSync } from "node:fs";
9
+ import os from "node:os";
10
+ import path from "node:path";
11
+ import { directoryNameFor, SegmentStore } from "../services/encode/SegmentStore.js";
12
+ import { fmp4Format } from "../services/segment-formats/fmp4.js";
13
+
14
+ /**
15
+ * @returns {{ store: SegmentStore, root: string, lines: string[] }}
16
+ */
17
+ function storeInATempRoot() {
18
+ const root = mkdtempSync(path.join(os.tmpdir(), "segment-store-"));
19
+ const lines = [];
20
+ const store = new SegmentStore({
21
+ root,
22
+ logger: { info: (line) => lines.push(line), warn: (line) => lines.push(line) }
23
+ });
24
+ return { store, root, lines };
25
+ }
26
+
27
+ /**
28
+ * @param {string} dir
29
+ * @param {number} index
30
+ * @param {number} bytes
31
+ */
32
+ function writeSegment(dir, index, bytes = 16) {
33
+ writeFileSync(path.join(dir, fmp4Format.segmentFileName(index)), Buffer.alloc(bytes));
34
+ }
35
+
36
+ const KEY = "torrent:abc:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
37
+
38
+ test("two viewers of one output are given the same directory", (t) => {
39
+ const { store, root } = storeInATempRoot();
40
+ t.after(() => rmSync(root, { recursive: true, force: true }));
41
+
42
+ // Which viewer asked never enters it — the address is what the segments are.
43
+ assert.equal(store.directoryFor(KEY), store.directoryFor(KEY));
44
+ assert.notEqual(store.directoryFor(KEY), store.directoryFor(`${KEY}:other`));
45
+ });
46
+
47
+ test("the directory says what it holds, so a later process can tell", (t) => {
48
+ const { store, root } = storeInATempRoot();
49
+ t.after(() => rmSync(root, { recursive: true, force: true }));
50
+
51
+ const dir = store.directoryFor(KEY);
52
+ const names = readdirSync(dir);
53
+ assert.deepEqual(names, ["key.txt"]);
54
+ });
55
+
56
+ test("a piece under its served name is finished, and the last one too", (t) => {
57
+ const { store, root } = storeInATempRoot();
58
+ t.after(() => rmSync(root, { recursive: true, force: true }));
59
+
60
+ const dir = store.directoryFor(KEY);
61
+ store.useFormat(KEY, fmp4Format);
62
+ writeSegment(dir, 0);
63
+ writeSegment(dir, 1);
64
+ writeSegment(dir, 2);
65
+
66
+ // It was "the NEXT number exists", which left the last piece of every run
67
+ // unprovable for ever and the moment two runs share an output declared a
68
+ // half-written file finished because somebody else had written the one after
69
+ // it. The name is the proof now, so all three count.
70
+ assert.deepEqual(store.provenNumbers(KEY), [0, 1, 2]);
71
+ });
72
+
73
+ test("a piece still being written is not a piece, and its name says so", (t) => {
74
+ const { store, root } = storeInATempRoot();
75
+ t.after(() => rmSync(root, { recursive: true, force: true }));
76
+
77
+ const dir = store.directoryFor(KEY);
78
+ store.useFormat(KEY, fmp4Format);
79
+ writeSegment(dir, 0);
80
+ // What ffmpeg is writing into right now, tagged with the run that opened it.
81
+ writeFileSync(path.join(dir, "making-0-00001.mp4"), Buffer.alloc(64));
82
+
83
+ assert.deepEqual(store.provenNumbers(KEY), [0]);
84
+ assert.equal(store.isClosed(KEY, 1), false);
85
+ assert.equal(store.pathOf(KEY, 1), null);
86
+
87
+ // And closing it is one rename, after which it is servable.
88
+ assert.equal(store.publish(KEY, "making-0-00001.mp4", fmp4Format), "segment-00001.mp4");
89
+ assert.equal(store.isClosed(KEY, 1), true);
90
+ });
91
+
92
+ test("a file of no bytes is not a segment, whatever it is called", (t) => {
93
+ const { store, root } = storeInATempRoot();
94
+ t.after(() => rmSync(root, { recursive: true, force: true }));
95
+
96
+ const dir = store.directoryFor(KEY);
97
+ store.useFormat(KEY, fmp4Format);
98
+ writeSegment(dir, 0);
99
+ writeSegment(dir, 1);
100
+ writeSegment(dir, 2, 0);
101
+
102
+ // A file of no bytes under a served name cannot arise from this proxy any
103
+ // more a piece takes that name only on being closed — but a killed process
104
+ // can leave one, and taking it for a segment once closed the only hole in the
105
+ // numbering and convinced the look-ahead the encoder had produced it.
106
+ assert.equal(store.pathOf(KEY, 2), null);
107
+ assert.deepEqual(store.provenNumbers(KEY), [0, 1]);
108
+ });
109
+
110
+ test("clearing up after one run leaves every other run's work alone", (t) => {
111
+ const { store, root } = storeInATempRoot();
112
+ t.after(() => rmSync(root, { recursive: true, force: true }));
113
+
114
+ const dir = store.directoryFor(KEY);
115
+ store.useFormat(KEY, fmp4Format);
116
+ // Two live runs, each with a piece open, plus finished pieces of both.
117
+ writeSegment(dir, 0);
118
+ writeSegment(dir, 100);
119
+ writeFileSync(path.join(dir, "making-0-00001.mp4"), Buffer.alloc(64));
120
+ writeFileSync(path.join(dir, "making-100-00101.mp4"), Buffer.alloc(64));
121
+
122
+ // The run that began at #0 ends. Its own unfinished piece goes and nothing
123
+ // else does — which the old rule could not manage: it took the highest SERVED
124
+ // name inside the ended run's stretch and judged its bytes, so with the
125
+ // naming rule above it would have removed #0, a piece that run had closed.
126
+ assert.equal(store.clearUpAfter(KEY, 0), 1);
127
+ assert.deepEqual(store.provenNumbers(KEY), [0, 100]);
128
+ assert.equal(
129
+ readdirSync(dir).includes("making-100-00101.mp4"),
130
+ true,
131
+ "the other run is still writing its own"
132
+ );
133
+ });
134
+
135
+ test("a directory that has not moved is not read again", (t) => {
136
+ const { store, root } = storeInATempRoot();
137
+ t.after(() => rmSync(root, { recursive: true, force: true }));
138
+
139
+ const dir = store.directoryFor(KEY);
140
+ store.useFormat(KEY, fmp4Format);
141
+ writeSegment(dir, 0);
142
+ writeSegment(dir, 1);
143
+
144
+ const first = store.refresh(KEY);
145
+ const second = store.refresh(KEY);
146
+ assert.equal(first, second, "the same reading is handed back, not a fresh listing");
147
+ });
148
+
149
+ test("what a killed process left is found, named and counted", (t) => {
150
+ const { store, root, lines } = storeInATempRoot();
151
+ t.after(() => rmSync(root, { recursive: true, force: true }));
152
+
153
+ const dir = path.join(root, directoryNameFor(KEY));
154
+ mkdirSync(dir, { recursive: true });
155
+ writeFileSync(path.join(dir, "key.txt"), `${KEY}\n`);
156
+ writeSegment(dir, 0);
157
+ writeSegment(dir, 1);
158
+ const orphan = path.join(root, "0123456789abcdef");
159
+ mkdirSync(orphan, { recursive: true });
160
+ writeSegment(orphan, 0);
161
+
162
+ const swept = store.sweep();
163
+ assert.equal(swept.directories, 2);
164
+ assert.equal(swept.unidentified, 1, "the one with no key file cannot be matched to a request");
165
+ assert.equal(swept.segments, 3);
166
+ assert.match(lines.join("\n"), /startup sweep/);
167
+ assert.match(lines.join("\n"), /ended\s+without anything recording why/);
168
+ });
169
+
170
+ test("what survived a kill is taken back whole, minus what was still being written", (t) => {
171
+ const { store, root } = storeInATempRoot();
172
+ t.after(() => rmSync(root, { recursive: true, force: true }));
173
+
174
+ const dir = path.join(root, directoryNameFor(KEY));
175
+ mkdirSync(dir, { recursive: true });
176
+ writeFileSync(path.join(dir, "key.txt"), `${KEY}
177
+ `);
178
+ for (let index = 0; index <= 5; index += 1) {
179
+ writeSegment(dir, index);
180
+ }
181
+ // The piece the kernel interrupted. It never had a served name, so it cannot
182
+ // be mistaken for one — which is what the old rule could only guess at, by
183
+ // always throwing the highest number away.
184
+ writeFileSync(path.join(dir, "making-0-00006.mp4"), Buffer.alloc(64));
185
+
186
+ const taken = store.adoptWhatSurvived(() => fmp4Format);
187
+
188
+ assert.equal(taken.adopted, 1);
189
+ assert.equal(taken.unprovenRemoved, 1, "the one under a working name");
190
+ // All six kept rather than five: the highest served name used to be thrown
191
+ // away because nothing could prove it, and on the copy branch that is a piece
192
+ // whose bytes depend only on the source and cost a short machine to remake.
193
+ assert.deepEqual(store.provenNumbers(KEY), [0, 1, 2, 3, 4, 5]);
194
+ });
195
+
196
+ test("a directory that cannot name itself is thrown away rather than served", (t) => {
197
+ const { store, root } = storeInATempRoot();
198
+ t.after(() => rmSync(root, { recursive: true, force: true }));
199
+
200
+ const orphan = path.join(root, "0123456789abcdef");
201
+ mkdirSync(orphan, { recursive: true });
202
+ writeSegment(orphan, 0);
203
+
204
+ const taken = store.adoptWhatSurvived(() => fmp4Format);
205
+
206
+ assert.equal(taken.adopted, 0);
207
+ assert.equal(taken.dropped, 1);
208
+ assert.equal(readdirSync(root).length, 0);
209
+ });
210
+
211
+ test("an output this proxy can no longer serve is thrown away too", (t) => {
212
+ const { store, root } = storeInATempRoot();
213
+ t.after(() => rmSync(root, { recursive: true, force: true }));
214
+
215
+ const dir = path.join(root, directoryNameFor(KEY));
216
+ mkdirSync(dir, { recursive: true });
217
+ writeFileSync(path.join(dir, "key.txt"), `${KEY}\n`);
218
+ writeSegment(dir, 0);
219
+
220
+ const taken = store.adoptWhatSurvived(() => null);
221
+
222
+ assert.equal(taken.dropped, 1);
223
+ assert.equal(readdirSync(root).length, 0);
224
+ });
225
+
226
+ test("what the store weighs is reported, and dropping one output frees it", (t) => {
227
+ const { store, root } = storeInATempRoot();
228
+ t.after(() => rmSync(root, { recursive: true, force: true }));
229
+
230
+ const dir = store.directoryFor(KEY);
231
+ store.useFormat(KEY, fmp4Format);
232
+ writeSegment(dir, 0, 1000);
233
+ writeSegment(dir, 1, 1000);
234
+
235
+ assert.equal(store.stats().bytes, 2000);
236
+ store.drop(KEY, "nobody is watching it");
237
+ assert.equal(store.stats().outputs, 0);
238
+ });
@@ -0,0 +1,139 @@
1
+ /**
2
+ * @file Where a second encoder joins a stretch, and whether it is worth having.
3
+ *
4
+ * Both halves are arithmetic and neither was asked before. The stretch used to
5
+ * be halved, unconditionally:
6
+ *
7
+ * widestFrom = from + Math.floor(Math.min(room, to - from + 1) / 2)
8
+ *
9
+ * Halving IS the answer when both encoders are fresh and owe the same. It is not
10
+ * when one of them is already partway through a piece, and it never asked the
11
+ * other question at all — whether two encoders under this host's measured
12
+ * contention beat one at full speed.
13
+ *
14
+ * The derivation. A stretch of unmade film runs from `from` to `to`; whoever is
15
+ * already on it stands at `from` and owes `w` before the piece under it exists;
16
+ * a fresh one placed at `x` owes `d` — its start and then a whole piece — and
17
+ * both then work at the rate two encoders leave each other:
18
+ *
19
+ * the one there closes [from, x-1]: w + (x - 1 - from) / r rises with x
20
+ * the fresh one closes [x, to]: d + (to - x) / r falls with x
21
+ *
22
+ * x* = (from + to + 1) / 2 + (d - w) * r / 2
23
+ *
24
+ * The shift is `firstByteWait / (2 * perPiece)` pieces, so it depends on how
25
+ * long a piece takes here: 0.24 of a piece at 4.45x on a 4.2 s grid, but 0.96 at
26
+ * 8.9x and 2.14 at 20x — a whole segment and more on the copy branch, where a
27
+ * piece is short and a start is not. Measuring only the re-encode rate
28
+ * understated it.
29
+ */
30
+
31
+ import test from "node:test";
32
+ import assert from "node:assert/strict";
33
+ import { planEncoders } from "../services/encode/EncodePlan.js";
34
+ import { CoverageMap } from "../services/encode/CoverageMap.js";
35
+
36
+ const SEGMENT_SECONDS = 4.2;
37
+ const LAST = 199;
38
+
39
+ /**
40
+ * One encoder on a long stretch nobody has made, and room for more.
41
+ *
42
+ * @param {{ speedX: number, penalty: number, head?: number, ageMs?: number,
43
+ * maxRuns?: number }} params
44
+ * @returns {{ actions: object[], run: object }}
45
+ */
46
+ function planFor({ speedX, penalty, head = 0, ageMs = 60_000, maxRuns = 3, runs = null }) {
47
+ const coverage = new CoverageMap({ segmentCount: LAST + 1 });
48
+ const run = { from: 0, to: -1, head, speedX, isAlive: true, startedAt: 1_000_000 };
49
+ const live = runs ?? [run];
50
+ for (const one of live) {
51
+ coverage.claim(one, one.from, one.to);
52
+ }
53
+ return {
54
+ run,
55
+ actions: planEncoders({
56
+ coverage,
57
+ // One zone over the whole film, due now, so nothing but the arithmetic of
58
+ // sharing it decides anything.
59
+ windows: [{ from: 0, to: LAST, priority: 100, withinSeconds: 0, behind: false }],
60
+ runs: live,
61
+ maxRuns,
62
+ segmentSeconds: SEGMENT_SECONDS,
63
+ speedX,
64
+ killCostSec: 0.04,
65
+ firstByteWaitSec: 0.9,
66
+ moveCostSec: 0.94,
67
+ refetchSecPerFilmSecond: 0,
68
+ // Measured on the addon host: 1.70 at 854x480, 1.98 at 1920x1080. Raised
69
+ // to the power of how many others there are, so a third costs again.
70
+ contentionPenaltyFor: (others) => (others <= 0 ? 1 : penalty ** others),
71
+ now: 1_000_000 + ageMs
72
+ })
73
+ };
74
+ }
75
+
76
+ test("one encoder that reaches everything in time is left to do it alone", () => {
77
+ // A zone whose deadline GROWS with distance — which is what the map states,
78
+ // one segment at a time — is comfortably served by one encoder at 4.45x: #199
79
+ // is due in 835 s and arrives in 189 s. So there is no position to fill and
80
+ // nothing to split, whatever the budget says.
81
+ //
82
+ // Measured rather than assumed: this is what the plan answers, and the test
83
+ // was written expecting two encoders before it was run.
84
+ const { actions } = planFor({ speedX: 4.45, penalty: 1, maxRuns: 3 });
85
+
86
+ assert.deepEqual(
87
+ actions.map((one) => `${one.type} #${one.from}..#${one.to}`),
88
+ ["keep #0..#-1"],
89
+ "one encoder, its road to the end of the film"
90
+ );
91
+ });
92
+
93
+ test("a stretch nobody is on is split at the middle, and the halves bound each other", () => {
94
+ // Both encoders are fresh, so both owe the same and `d - w` is zero: the
95
+ // meeting point is the midpoint, which is what halving always said. What is
96
+ // new is that each one's road ENDS where the next begins, so neither writes
97
+ // into the other's names.
98
+ const actions = planFor({ speedX: 4.45, penalty: 1, maxRuns: 2, runs: [] }).actions;
99
+
100
+ assert.deepEqual(
101
+ actions.map((one) => `${one.type} #${one.from}..#${one.to}`),
102
+ ["start #0..#100", "start #101..#199"],
103
+ "0..199 halved at 100"
104
+ );
105
+ });
106
+
107
+ test("a third encoder halves the widest half that is left", () => {
108
+ const actions = planFor({ speedX: 4.45, penalty: 1, maxRuns: 3, runs: [] }).actions;
109
+
110
+ assert.deepEqual(
111
+ actions.map((one) => `${one.type} #${one.from}..#${one.to}`),
112
+ ["start #0..#50", "start #51..#100", "start #101..#199"],
113
+ "the first half is halved again, greedily, widest first"
114
+ );
115
+ });
116
+
117
+ test("a stretch of one piece is not split at all", () => {
118
+ const coverage = new CoverageMap({ segmentCount: 2 });
119
+ const actions = planEncoders({
120
+ coverage,
121
+ windows: [{ from: 0, to: 0, priority: 100, withinSeconds: 0, behind: false }],
122
+ runs: [],
123
+ maxRuns: 3,
124
+ segmentSeconds: SEGMENT_SECONDS,
125
+ speedX: 8.9,
126
+ killCostSec: 0.04,
127
+ firstByteWaitSec: 0.9,
128
+ moveCostSec: 0.94,
129
+ refetchSecPerFilmSecond: 0,
130
+ contentionPenaltyFor: () => 1,
131
+ now: 1_000_000
132
+ });
133
+
134
+ assert.equal(
135
+ actions.filter((one) => one.type === "start").length,
136
+ 1,
137
+ "there is nothing to share"
138
+ );
139
+ });