@torrent-tv/proxy 2.80.5 → 2.80.7
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 +36 -0
- package/package.json +1 -1
- package/routes/api/delivery-sink/get.js +8 -4
- package/server.js +415 -403
- package/services/data-channel-handler.js +87 -25
- package/services/delivery-probe.js +38 -5
- package/services/encode/CoverageMap.js +77 -4
- package/services/encode/EncodePlan.js +1025 -358
- package/services/encode/EncodeRun.js +19 -1
- package/services/encode/SegmentDemand.js +0 -0
- package/services/encode/SegmentStore.js +53 -1
- package/services/encode/open-piece.js +22 -1
- package/services/encode/run-command.js +12 -2
- package/services/hls-session-manager.js +38 -158
- package/services/hwaccel.js +182 -54
- package/services/orchestrators/EncodeOrchestrator.js +118 -89
- package/services/output/LiveOutputs.js +233 -213
- package/services/output/Timeline.js +333 -256
- package/services/piece-store/shared-piece-store.js +13 -9
- package/services/priority/PriorityMap.js +262 -108
- package/services/priority/PriorityOrchestrator.js +31 -6
- package/services/quality/EncodeCost.js +555 -500
- package/services/torrent-pool.js +9 -4
- package/test/encode-orchestrator.test.js +195 -65
- package/test/encode-plan-viewers.test.js +719 -0
- package/test/encode-plan.test.js +174 -81
- package/test/open-piece.test.js +40 -0
- package/test/output-speed.test.js +86 -0
- package/test/piece-store-eviction.test.js +26 -0
- package/test/priority-map-download.test.js +25 -7
- package/test/priority-map.test.js +134 -83
- package/test/seek-landing.test.js +109 -76
- package/test/segment-demand.test.js +54 -56
- package/test/wedge-certainty.test.js +3 -3
|
@@ -0,0 +1,719 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The placement model under one, two and three viewers, each of them
|
|
3
|
+
* playing, seeking and paused.
|
|
4
|
+
*
|
|
5
|
+
* THE MODEL, restated so a failure here can be read against it:
|
|
6
|
+
*
|
|
7
|
+
* - each segment number carries a deadline `D(x)`, the seconds until somebody
|
|
8
|
+
* needs it. A viewer moving forward covers a second of film in a second, so
|
|
9
|
+
* `D(x)` is the distance to them. `Infinity` where nobody is coming;
|
|
10
|
+
* - an encoder placed at `a` delivers `a + j` at `(j + 1) / r`, `r` segments per
|
|
11
|
+
* second. The same expression answers "when would the one already placed get
|
|
12
|
+
* here";
|
|
13
|
+
* - a number is late when it arrives after its deadline. Placement is first-fit
|
|
14
|
+
* left to right: give it to an encoder that arrives in time, else open one
|
|
15
|
+
* exactly there.
|
|
16
|
+
*
|
|
17
|
+
* Three behaviours of the map follow from that and are checked here rather than
|
|
18
|
+
* assumed. A peak MOVES FORWARD on its own, because the deadline is a distance
|
|
19
|
+
* and the distance shrinks as the viewer watches. It JUMPS on a seek. It
|
|
20
|
+
* FLATTENS on a pause, because a viewer who is not moving has no time by which
|
|
21
|
+
* anything must exist.
|
|
22
|
+
*
|
|
23
|
+
* Nothing here spawns ffmpeg, touches a disk or reads a clock. That is the layer
|
|
24
|
+
* check, made executable: this layer is exercised with plain values alone.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import test from "node:test";
|
|
28
|
+
import assert from "node:assert/strict";
|
|
29
|
+
import { EventEmitter } from "node:events";
|
|
30
|
+
import { EncodeRun } from "../services/encode/EncodeRun.js";
|
|
31
|
+
import { SoftwareEncoder } from "../services/encode/SoftwareEncoder.js";
|
|
32
|
+
import { EncodeOrchestrator } from "../services/orchestrators/EncodeOrchestrator.js";
|
|
33
|
+
import { mapForViewer, mergeMaps, runsOf } from "../services/priority/PriorityMap.js";
|
|
34
|
+
import { contentionPenalty, penaltiesFrom } from "../services/contention.js";
|
|
35
|
+
|
|
36
|
+
// WHAT A SECOND ENCODER COSTS THE FIRST — measured, never a formula.
|
|
37
|
+
//
|
|
38
|
+
// Addon host, 2026-09-03: 854x480 through libx264 `ultrafast` ran at 7.12x with
|
|
39
|
+
// the machine to itself, and at 4.20x and 4.16x when two ran at once. The
|
|
40
|
+
// penalty is read off that reading by the SAME two functions production uses,
|
|
41
|
+
// so nothing here invents a shape: beyond what was measured the reading is held
|
|
42
|
+
// rather than extrapolated.
|
|
43
|
+
const MEASURED_PENALTIES = penaltiesFrom(7.12, [{ others: 1, speed: 4.18 }]);
|
|
44
|
+
const penaltyFor = (others) => contentionPenalty(others, MEASURED_PENALTIES).penalty;
|
|
45
|
+
|
|
46
|
+
// What a start and a stop cost, measured on the same host: a spawn with its
|
|
47
|
+
// input open is 0.12 s there.
|
|
48
|
+
const RUN_COSTS = { killCostSec: 0, firstByteWaitSec: 0.12 };
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
const PICTURE = "torrent:abc:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
|
|
52
|
+
const SEGMENT_SECONDS = 4;
|
|
53
|
+
const FILM_SECONDS = 4000;
|
|
54
|
+
const SEGMENTS = FILM_SECONDS / SEGMENT_SECONDS;
|
|
55
|
+
|
|
56
|
+
class FakeProcess extends EventEmitter {
|
|
57
|
+
constructor() {
|
|
58
|
+
super();
|
|
59
|
+
this.pid = 1;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
kill(signal) {
|
|
63
|
+
this.emit("exit", null, signal);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** @param {{ maxRuns?: number }} [options] */
|
|
68
|
+
function orchestrator({ maxRuns = 3 } = {}) {
|
|
69
|
+
/** @type {EncodeOrchestrator} */
|
|
70
|
+
let made;
|
|
71
|
+
const build = ({ address, from, to }) => new EncodeRun({
|
|
72
|
+
address,
|
|
73
|
+
encoder: new SoftwareEncoder(),
|
|
74
|
+
from,
|
|
75
|
+
to,
|
|
76
|
+
buildArgs: () => ["-i", "in", "out"],
|
|
77
|
+
spawn: () => new FakeProcess(),
|
|
78
|
+
logger: { info() {}, warn() {} },
|
|
79
|
+
now: () => 1000,
|
|
80
|
+
onEnded: (ended) => made.noteEnded(ended)
|
|
81
|
+
});
|
|
82
|
+
made = new EncodeOrchestrator({
|
|
83
|
+
maxRunsFor: () => maxRuns,
|
|
84
|
+
segmentSeconds: SEGMENT_SECONDS,
|
|
85
|
+
...RUN_COSTS,
|
|
86
|
+
// A swarm with room for several encoders. The figure is the measured cost
|
|
87
|
+
// of fetching a second of film again, in seconds of swarm time; at 0.25 one
|
|
88
|
+
// encoder at 6x already takes one and a half swarms, so the budget would be
|
|
89
|
+
// one process and the placement would have nothing to place — which is a
|
|
90
|
+
// real limit, checked in its own test below, and not the subject of these.
|
|
91
|
+
refetchSecPerFilmSecond: () => 0.02,
|
|
92
|
+
// What the startup benchmark says this host encodes at. It exists before
|
|
93
|
+
// any viewer, so the plan is never without a speed.
|
|
94
|
+
startingSpeedFor: () => 2,
|
|
95
|
+
// What a second encoder costs the first, measured on the addon host. The
|
|
96
|
+
// orchestrator reads the table with the same pure function production uses.
|
|
97
|
+
// Without it an extra process is free and the score always wants more of
|
|
98
|
+
// them — and fewer encoders can genuinely finish sooner.
|
|
99
|
+
contentionPenalties: MEASURED_PENALTIES,
|
|
100
|
+
now: () => 1000,
|
|
101
|
+
logger: { info() {}, warn() {} },
|
|
102
|
+
makeRun: build
|
|
103
|
+
});
|
|
104
|
+
made.setSegmentCount(PICTURE, SEGMENTS);
|
|
105
|
+
/** Who is watching, by name. The map is rebuilt from all of them on change. */
|
|
106
|
+
const watching = new Map();
|
|
107
|
+
const watches = (who, { atSeconds, playing = true }) => {
|
|
108
|
+
watching.set(who, { atSeconds, playing });
|
|
109
|
+
stateMap(made, watching);
|
|
110
|
+
};
|
|
111
|
+
const leaves = (who) => {
|
|
112
|
+
watching.delete(who);
|
|
113
|
+
stateMap(made, watching);
|
|
114
|
+
};
|
|
115
|
+
return { made, build, watches, leaves };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Everybody watching, as ONE map, the way the product builds it.
|
|
120
|
+
*
|
|
121
|
+
* A viewer's own map is in seconds of film; the maps of all of them are merged
|
|
122
|
+
* into one, and only then converted into an output's own numbering. Doing it
|
|
123
|
+
* here rather than stating windows by hand is the point: the pause and the seek
|
|
124
|
+
* are the MAP's behaviour, and a test that stated windows directly would be
|
|
125
|
+
* checking its own arithmetic.
|
|
126
|
+
*
|
|
127
|
+
* @param {EncodeOrchestrator} made
|
|
128
|
+
* @param {Map<string, { atSeconds: number, playing: boolean }>} watching
|
|
129
|
+
*/
|
|
130
|
+
function stateMap(made, watching) {
|
|
131
|
+
const map = mergeMaps(
|
|
132
|
+
[...watching.values()].map((viewer) => mapForViewer({
|
|
133
|
+
atSeconds: viewer.atSeconds,
|
|
134
|
+
durationSeconds: FILM_SECONDS,
|
|
135
|
+
allowanceSeconds: 8,
|
|
136
|
+
playing: viewer.playing
|
|
137
|
+
}))
|
|
138
|
+
);
|
|
139
|
+
// Seconds of film into this output's own piece numbers. The product does it
|
|
140
|
+
// with the cut table, which knows where the pieces fall; here the pieces are a
|
|
141
|
+
// uniform grid, so it is a division — and doing it at all is the point, since
|
|
142
|
+
// the map is per FILM and every consumer numbers differently.
|
|
143
|
+
made.notePriorityMap(PICTURE, runsOf(map).map((zone) => ({
|
|
144
|
+
from: Math.floor(zone.from / SEGMENT_SECONDS),
|
|
145
|
+
to: Math.max(
|
|
146
|
+
Math.floor(zone.from / SEGMENT_SECONDS),
|
|
147
|
+
Math.ceil(zone.to / SEGMENT_SECONDS) - 1
|
|
148
|
+
),
|
|
149
|
+
priority: zone.priority,
|
|
150
|
+
withinSeconds: zone.withinSeconds,
|
|
151
|
+
behind: zone.behind
|
|
152
|
+
})));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* How many encoders are working the stretch around one number.
|
|
157
|
+
*
|
|
158
|
+
* The count that matters for a viewer is not how many exist — spare capacity is
|
|
159
|
+
* meant to be spent finishing the film, so a machine that holds three should be
|
|
160
|
+
* holding three — but how many are crowded onto the place they are standing.
|
|
161
|
+
*
|
|
162
|
+
* @param {EncodeOrchestrator} made
|
|
163
|
+
* @param {number} segment
|
|
164
|
+
*/
|
|
165
|
+
function onTheStretchOf(made, segment) {
|
|
166
|
+
return made.runsOn(PICTURE).filter((run) => {
|
|
167
|
+
const to = run.to < run.from ? Number.POSITIVE_INFINITY : run.to;
|
|
168
|
+
return run.from <= segment && segment <= to;
|
|
169
|
+
}).length;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** @param {EncodeOrchestrator} made */
|
|
173
|
+
function placements(made) {
|
|
174
|
+
return made.runsOn(PICTURE).map((run) => run.from).sort((left, right) => left - right);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Every live encoder's stretch, so that "they never share a number" can be
|
|
179
|
+
* asserted as the consequence it is rather than trusted.
|
|
180
|
+
*
|
|
181
|
+
* @param {EncodeOrchestrator} made
|
|
182
|
+
*/
|
|
183
|
+
function assertNoOverlap(made) {
|
|
184
|
+
const spans = made.runsOn(PICTURE)
|
|
185
|
+
.map((run) => [run.from, run.to < run.from ? Number.POSITIVE_INFINITY : run.to])
|
|
186
|
+
.sort((left, right) => left[0] - right[0]);
|
|
187
|
+
for (let index = 0; index < spans.length - 1; index += 1) {
|
|
188
|
+
assert.ok(
|
|
189
|
+
spans[index][1] < spans[index + 1][0],
|
|
190
|
+
`#${spans[index][0]}..#${spans[index][1]} overlaps #${spans[index + 1][0]}`
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ---------------------------------------------------------------- one viewer
|
|
196
|
+
|
|
197
|
+
test("one viewer playing: one encoder, exactly where they are", () => {
|
|
198
|
+
const { made, watches, leaves } = orchestrator();
|
|
199
|
+
watches("one", { atSeconds: 400 });
|
|
200
|
+
made.reconcile();
|
|
201
|
+
assert.deepEqual(placements(made), [100], "at their own position, 400s / 4s");
|
|
202
|
+
assertNoOverlap(made);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test("one viewer playing: the encoder keeping up buys no second one", () => {
|
|
206
|
+
// The peak moves forward on its own as they watch, and an encoder that stays
|
|
207
|
+
// in front of it is never late. This is the case that must NOT spend a
|
|
208
|
+
// process, and the one the old head-as-a-barrier rule got wrong.
|
|
209
|
+
const { made, watches, leaves } = orchestrator();
|
|
210
|
+
watches("one", { atSeconds: 400 });
|
|
211
|
+
made.reconcile();
|
|
212
|
+
const [run] = made.runsOn(PICTURE);
|
|
213
|
+
run.noteSpeed(6);
|
|
214
|
+
for (let index = 100; index < 130; index += 1) {
|
|
215
|
+
made.noteProduced(PICTURE, index);
|
|
216
|
+
}
|
|
217
|
+
watches("one", { atSeconds: 480 });
|
|
218
|
+
made.reconcile();
|
|
219
|
+
// Asked of the first number nobody has, not of the one they are standing on:
|
|
220
|
+
// that one is already made, and nothing needs to be covering it.
|
|
221
|
+
assert.equal(onTheStretchOf(made, 130), 1, "one encoder on the film in front of them");
|
|
222
|
+
assertNoOverlap(made);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test("one viewer seeking far ahead: an encoder is placed there", () => {
|
|
226
|
+
const { made, watches, leaves } = orchestrator();
|
|
227
|
+
watches("one", { atSeconds: 400 });
|
|
228
|
+
made.reconcile();
|
|
229
|
+
made.runsOn(PICTURE)[0].noteSpeed(6);
|
|
230
|
+
made.noteProduced(PICTURE, 100);
|
|
231
|
+
|
|
232
|
+
// The peak JUMPS. Nothing about the old place is wanted now.
|
|
233
|
+
watches("one", { atSeconds: 3000 });
|
|
234
|
+
made.reconcile();
|
|
235
|
+
|
|
236
|
+
assert.ok(placements(made).includes(750), "an encoder where they landed, 3000s / 4s");
|
|
237
|
+
assertNoOverlap(made);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
test("one viewer paused: nothing is late, so no encoder is added", () => {
|
|
241
|
+
// A paused viewer states the whole film at one undifferentiated rank and no
|
|
242
|
+
// time by which any of it must exist. The encoder already working goes on
|
|
243
|
+
// encoding the track; nothing justifies a second process.
|
|
244
|
+
const { made, watches, leaves } = orchestrator();
|
|
245
|
+
watches("one", { atSeconds: 400 });
|
|
246
|
+
made.reconcile();
|
|
247
|
+
made.runsOn(PICTURE)[0].noteSpeed(1);
|
|
248
|
+
made.noteProduced(PICTURE, 100);
|
|
249
|
+
|
|
250
|
+
watches("one", { atSeconds: 404, playing: false });
|
|
251
|
+
made.reconcile();
|
|
252
|
+
|
|
253
|
+
assert.equal(onTheStretchOf(made, 101), 1, "one encoder where they are standing");
|
|
254
|
+
assertNoOverlap(made);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
test("one viewer paused states no deadline anywhere", () => {
|
|
258
|
+
// The map's own answer, checked directly, because every placement decision
|
|
259
|
+
// below rests on it.
|
|
260
|
+
const map = mapForViewer({
|
|
261
|
+
atSeconds: 400,
|
|
262
|
+
durationSeconds: FILM_SECONDS,
|
|
263
|
+
allowanceSeconds: 8,
|
|
264
|
+
playing: false
|
|
265
|
+
});
|
|
266
|
+
assert.ok(map.durationSeconds > 0, "a paused viewer still wants the film");
|
|
267
|
+
for (let second = 0; second < map.durationSeconds; second += 1) {
|
|
268
|
+
assert.equal(map.secondsUntilPlayed[second], Number.POSITIVE_INFINITY,
|
|
269
|
+
"but no second of it has a time by which it must exist");
|
|
270
|
+
}
|
|
271
|
+
// Their POSITION survives the pause, and with it the rule that what is in
|
|
272
|
+
// front of them is made before what is behind.
|
|
273
|
+
assert.equal(map.behind[399], 1, "what they have watched is behind them");
|
|
274
|
+
assert.equal(map.behind[400], 0, "and what they have not is in front");
|
|
275
|
+
assert.ok(map.priority[400] > map.priority[399], "which is what the numbers say");
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// --------------------------------------------------------------- two viewers
|
|
279
|
+
|
|
280
|
+
test("two viewers close together share one encoder", () => {
|
|
281
|
+
// Film both of them want is made once. This is what merging the map is for,
|
|
282
|
+
// and it must survive the deadline being carried alongside the rank.
|
|
283
|
+
const { made, watches, leaves } = orchestrator();
|
|
284
|
+
watches("one", { atSeconds: 400 });
|
|
285
|
+
watches("two", { atSeconds: 408 });
|
|
286
|
+
made.reconcile();
|
|
287
|
+
// One encoder for the pair — asked of the film they are both about to watch.
|
|
288
|
+
// The machine may well be running others further on: what it has spare goes to
|
|
289
|
+
// finishing the file, and that is a different question from crowding.
|
|
290
|
+
assert.equal(onTheStretchOf(made, 100), 1, "one encoder for the pair");
|
|
291
|
+
assert.equal(onTheStretchOf(made, 102), 1, "and one for the film just in front of them");
|
|
292
|
+
assertNoOverlap(made);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
test("two viewers far apart get an encoder each", () => {
|
|
296
|
+
const { made, watches, leaves } = orchestrator();
|
|
297
|
+
watches("one", { atSeconds: 400 });
|
|
298
|
+
made.reconcile();
|
|
299
|
+
made.runsOn(PICTURE)[0].noteSpeed(6);
|
|
300
|
+
made.noteProduced(PICTURE, 100);
|
|
301
|
+
|
|
302
|
+
watches("two", { atSeconds: 3000 });
|
|
303
|
+
made.reconcile();
|
|
304
|
+
|
|
305
|
+
assert.ok(placements(made).includes(750), "the far one is served where they stand");
|
|
306
|
+
assert.equal(onTheStretchOf(made, 750), 1, "and by one encoder, not a crowd");
|
|
307
|
+
assertNoOverlap(made);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
test("two viewers: one seeking does not take the other's encoder", () => {
|
|
311
|
+
const { made, watches, leaves } = orchestrator();
|
|
312
|
+
watches("one", { atSeconds: 400 });
|
|
313
|
+
made.reconcile();
|
|
314
|
+
// A speed has to be measured before a second encoder can be justified: how
|
|
315
|
+
// long one takes to reach a number is the whole comparison. Real life
|
|
316
|
+
// measures it from the first run; a test that stated both viewers before any
|
|
317
|
+
// run existed would be asking the plan to buy a process on no evidence.
|
|
318
|
+
made.runsOn(PICTURE)[0].noteSpeed(6);
|
|
319
|
+
made.noteProduced(PICTURE, 100);
|
|
320
|
+
watches("two", { atSeconds: 3000 });
|
|
321
|
+
made.reconcile();
|
|
322
|
+
for (const run of made.runsOn(PICTURE)) {
|
|
323
|
+
run.noteSpeed(6);
|
|
324
|
+
}
|
|
325
|
+
assert.ok(made.runsOn(PICTURE).length >= 2, "one each to begin with");
|
|
326
|
+
|
|
327
|
+
// The far one seeks somewhere else entirely.
|
|
328
|
+
watches("two", { atSeconds: 2000 });
|
|
329
|
+
made.reconcile();
|
|
330
|
+
|
|
331
|
+
// Asked of the coverage, not of a run's first number: a run whose road was
|
|
332
|
+
// shortened begins again at its own head, so "is this viewer served" is a
|
|
333
|
+
// question about the map and never about where a process happens to start.
|
|
334
|
+
const coverage = made.coverageOf(PICTURE);
|
|
335
|
+
assert.equal(coverage.stateOf(105), "making", "the one who did not move is still served");
|
|
336
|
+
assert.ok(placements(made).includes(500), "and the one who moved is served where they landed");
|
|
337
|
+
assertNoOverlap(made);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
test("two viewers: one pausing leaves the other served", () => {
|
|
341
|
+
const { made, watches, leaves } = orchestrator();
|
|
342
|
+
watches("one", { atSeconds: 400 });
|
|
343
|
+
watches("two", { atSeconds: 3000 });
|
|
344
|
+
made.reconcile();
|
|
345
|
+
for (const run of made.runsOn(PICTURE)) {
|
|
346
|
+
run.noteSpeed(1);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
watches("two", { atSeconds: 3000, playing: false });
|
|
350
|
+
made.reconcile();
|
|
351
|
+
|
|
352
|
+
assert.equal(made.coverageOf(PICTURE).stateOf(105), "making",
|
|
353
|
+
"the one still watching keeps their encoder");
|
|
354
|
+
assertNoOverlap(made);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
test("two viewers: the one who leaves takes nothing from the one who stays", () => {
|
|
358
|
+
const { made, watches, leaves } = orchestrator();
|
|
359
|
+
watches("one", { atSeconds: 400 });
|
|
360
|
+
watches("two", { atSeconds: 3000 });
|
|
361
|
+
made.reconcile();
|
|
362
|
+
for (const run of made.runsOn(PICTURE)) {
|
|
363
|
+
run.noteSpeed(1);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
leaves("two");
|
|
367
|
+
made.reconcile();
|
|
368
|
+
|
|
369
|
+
assert.equal(made.coverageOf(PICTURE).stateOf(105), "making",
|
|
370
|
+
"the one who stayed is still served");
|
|
371
|
+
assertNoOverlap(made);
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
// ------------------------------------------------------------- three viewers
|
|
375
|
+
|
|
376
|
+
test("three viewers far apart get an encoder each when the machine affords it", () => {
|
|
377
|
+
const { made, watches, leaves } = orchestrator({ maxRuns: 3 });
|
|
378
|
+
watches("one", { atSeconds: 400 });
|
|
379
|
+
watches("two", { atSeconds: 2000 });
|
|
380
|
+
watches("three", { atSeconds: 3600 });
|
|
381
|
+
made.reconcile();
|
|
382
|
+
for (const run of made.runsOn(PICTURE)) {
|
|
383
|
+
run.noteSpeed(6);
|
|
384
|
+
}
|
|
385
|
+
made.reconcile();
|
|
386
|
+
|
|
387
|
+
const where = placements(made);
|
|
388
|
+
for (const at of [100, 500, 900]) {
|
|
389
|
+
assert.ok(where.includes(at), `somebody is making what the viewer at #${at} needs`);
|
|
390
|
+
assert.equal(onTheStretchOf(made, at), 1, `and one encoder there, not a crowd`);
|
|
391
|
+
}
|
|
392
|
+
assertNoOverlap(made);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
test("three viewers, a machine that affords two: the budget binds, not the map", () => {
|
|
396
|
+
// The number of encoders is the smaller of what the map asks and what the
|
|
397
|
+
// machine can hold. Which two are served follows from the order the work is
|
|
398
|
+
// taken in; what must not happen is a third process on a host that cannot
|
|
399
|
+
// hold it, or two processes writing one number.
|
|
400
|
+
const { made, watches, leaves } = orchestrator({ maxRuns: 2 });
|
|
401
|
+
watches("one", { atSeconds: 400 });
|
|
402
|
+
watches("two", { atSeconds: 2000 });
|
|
403
|
+
watches("three", { atSeconds: 3600 });
|
|
404
|
+
made.reconcile();
|
|
405
|
+
for (const run of made.runsOn(PICTURE)) {
|
|
406
|
+
run.noteSpeed(1);
|
|
407
|
+
}
|
|
408
|
+
made.reconcile();
|
|
409
|
+
|
|
410
|
+
assert.ok(made.runsOn(PICTURE).length <= 2, "never more than the machine affords");
|
|
411
|
+
assert.ok(made.runsOn(PICTURE).length >= 1, "and never nothing while people wait");
|
|
412
|
+
assertNoOverlap(made);
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
test("three viewers: one seeks onto another, and the two of them share", () => {
|
|
416
|
+
const { made, watches, leaves } = orchestrator({ maxRuns: 3 });
|
|
417
|
+
watches("one", { atSeconds: 400 });
|
|
418
|
+
made.reconcile();
|
|
419
|
+
made.runsOn(PICTURE)[0].noteSpeed(6);
|
|
420
|
+
made.noteProduced(PICTURE, 100);
|
|
421
|
+
watches("two", { atSeconds: 2000 });
|
|
422
|
+
watches("three", { atSeconds: 3600 });
|
|
423
|
+
made.reconcile();
|
|
424
|
+
for (const run of made.runsOn(PICTURE)) {
|
|
425
|
+
run.noteSpeed(6);
|
|
426
|
+
}
|
|
427
|
+
made.reconcile();
|
|
428
|
+
assert.equal(made.runsOn(PICTURE).length, 3);
|
|
429
|
+
|
|
430
|
+
// The third joins the second. Two peaks where there were three.
|
|
431
|
+
watches("three", { atSeconds: 2008 });
|
|
432
|
+
made.reconcile();
|
|
433
|
+
|
|
434
|
+
assert.ok(made.runsOn(PICTURE).length <= 3, "no process is added by people converging");
|
|
435
|
+
assertNoOverlap(made);
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
test("three viewers: all paused, and no encoder is added for any of them", () => {
|
|
439
|
+
const { made, watches, leaves } = orchestrator({ maxRuns: 3 });
|
|
440
|
+
watches("one", { atSeconds: 400 });
|
|
441
|
+
made.reconcile();
|
|
442
|
+
made.runsOn(PICTURE)[0].noteSpeed(1);
|
|
443
|
+
const before = made.runsOn(PICTURE).length;
|
|
444
|
+
|
|
445
|
+
watches("one", { atSeconds: 400, playing: false });
|
|
446
|
+
watches("two", { atSeconds: 2000, playing: false });
|
|
447
|
+
watches("three", { atSeconds: 3600, playing: false });
|
|
448
|
+
made.reconcile();
|
|
449
|
+
|
|
450
|
+
// Nobody is coming anywhere, so nothing can be late — and the machine does not
|
|
451
|
+
// therefore fall idle: what it has spare goes on finishing the file, which is
|
|
452
|
+
// what makes a seek back into a made part start playing at once.
|
|
453
|
+
assert.ok(made.runsOn(PICTURE).length >= before, "the file goes on being finished");
|
|
454
|
+
assertNoOverlap(made);
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
test("the plan is a function of the state: the same state twice gives the same answer", () => {
|
|
458
|
+
// What stops a moving map from becoming a thrash: the decision is arithmetic
|
|
459
|
+
// over the state, so a pass that finds nothing changed changes nothing.
|
|
460
|
+
const { made, watches, leaves } = orchestrator({ maxRuns: 3 });
|
|
461
|
+
watches("one", { atSeconds: 400 });
|
|
462
|
+
watches("two", { atSeconds: 2000 });
|
|
463
|
+
made.reconcile();
|
|
464
|
+
for (const run of made.runsOn(PICTURE)) {
|
|
465
|
+
run.noteSpeed(1);
|
|
466
|
+
}
|
|
467
|
+
made.reconcile();
|
|
468
|
+
const first = placements(made);
|
|
469
|
+
made.reconcile();
|
|
470
|
+
made.reconcile();
|
|
471
|
+
assert.deepEqual(placements(made), first, "three more passes moved nothing");
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
// ---------------------------------------------------------------- the edge
|
|
475
|
+
|
|
476
|
+
test("exactly realtime arrives exactly on time, and no second encoder is bought", () => {
|
|
477
|
+
// The knife edge, and it lands on "in time" by construction: the deadline of
|
|
478
|
+
// a number is the distance to it, and an encoder at 1.0x covers that distance
|
|
479
|
+
// in exactly that time. So the arithmetic says nothing is late and nothing is
|
|
480
|
+
// bought. Worth pinning, because the formula this model replaced said
|
|
481
|
+
// something quite different — `speed / (1 - speed)` divides by zero here and
|
|
482
|
+
// claims the encoder stays ahead FOR EVER, which is the same answer arrived
|
|
483
|
+
// at by nonsense.
|
|
484
|
+
const { made, watches, leaves } = orchestrator({ maxRuns: 3 });
|
|
485
|
+
watches("one", { atSeconds: 400 });
|
|
486
|
+
made.reconcile();
|
|
487
|
+
made.runsOn(PICTURE)[0].noteSpeed(1);
|
|
488
|
+
made.noteProduced(PICTURE, 100);
|
|
489
|
+
made.reconcile();
|
|
490
|
+
|
|
491
|
+
assert.equal(onTheStretchOf(made, 150), 1,
|
|
492
|
+
"just in time everywhere, so one encoder on the viewer's stretch is enough");
|
|
493
|
+
assertNoOverlap(made);
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
test("below realtime, whether a second encoder helps is arithmetic, and here it does not", () => {
|
|
497
|
+
// The side of the edge that matters, and the answer is not the one this check
|
|
498
|
+
// asserted when it was written. An encoder at half realtime loses ground on
|
|
499
|
+
// the viewer every second, so everything ahead of it is late — and a second
|
|
500
|
+
// process does not fix that, because on this host each of two runs at 1.70x
|
|
501
|
+
// slower than one. The near film gets worse by more than the far film gets
|
|
502
|
+
// better, so the total seconds of waiting go UP, and the score refuses.
|
|
503
|
+
//
|
|
504
|
+
// What this case actually needs is the answer the user gave for it: a machine
|
|
505
|
+
// that cannot serve its viewers without stopping the picture says so, and the
|
|
506
|
+
// viewer is moved to another proxy. That is not built. Adding processes is not
|
|
507
|
+
// a substitute for it and the model correctly declines to pretend otherwise.
|
|
508
|
+
const { made, watches, leaves } = orchestrator({ maxRuns: 3 });
|
|
509
|
+
watches("one", { atSeconds: 400 });
|
|
510
|
+
made.reconcile();
|
|
511
|
+
made.runsOn(PICTURE)[0].noteSpeed(0.5);
|
|
512
|
+
made.noteProduced(PICTURE, 100);
|
|
513
|
+
made.reconcile();
|
|
514
|
+
|
|
515
|
+
assert.equal(made.runsOn(PICTURE).length, 1,
|
|
516
|
+
"one encoder, because two of them would leave the viewer waiting longer");
|
|
517
|
+
assert.equal(made.coverageOf(PICTURE).stateOf(101), "making",
|
|
518
|
+
"and it is on the film in front of them");
|
|
519
|
+
assertNoOverlap(made);
|
|
520
|
+
void leaves;
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
test("two viewers arriving together on a cold output get one encoder, then are measured", () => {
|
|
524
|
+
// Nothing has measured how fast this machine encodes, so how long one encoder
|
|
525
|
+
// would take to reach the second viewer is not a known quantity. Buying a
|
|
526
|
+
// process on that is buying it on no evidence, which is refused; one is placed
|
|
527
|
+
// and the speed it reports is what justifies the next.
|
|
528
|
+
const { made, watches, leaves } = orchestrator({ maxRuns: 3 });
|
|
529
|
+
watches("one", { atSeconds: 400 });
|
|
530
|
+
watches("two", { atSeconds: 3000 });
|
|
531
|
+
made.reconcile();
|
|
532
|
+
assert.ok(made.runsOn(PICTURE).length >= 1, "at least one, until something is measured");
|
|
533
|
+
assert.equal(onTheStretchOf(made, 100), 1, "and not two on the same place");
|
|
534
|
+
|
|
535
|
+
made.runsOn(PICTURE)[0].noteSpeed(6);
|
|
536
|
+
made.noteProduced(PICTURE, 100);
|
|
537
|
+
made.reconcile();
|
|
538
|
+
assert.ok(placements(made).includes(750), "and now the far one is served too");
|
|
539
|
+
assertNoOverlap(made);
|
|
540
|
+
});
|
|
541
|
+
|
|
542
|
+
test("an encoder comfortably faster than realtime is left to do the whole stretch", () => {
|
|
543
|
+
const { made, watches, leaves } = orchestrator({ maxRuns: 3 });
|
|
544
|
+
watches("one", { atSeconds: 400 });
|
|
545
|
+
made.reconcile();
|
|
546
|
+
made.runsOn(PICTURE)[0].noteSpeed(6);
|
|
547
|
+
made.noteProduced(PICTURE, 100);
|
|
548
|
+
made.reconcile();
|
|
549
|
+
|
|
550
|
+
assert.equal(onTheStretchOf(made, 150), 1, "one encoder on the stretch they are watching");
|
|
551
|
+
assertNoOverlap(made);
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
test("a swarm that feeds one encoder moves it to whoever is late, rather than serving nobody", () => {
|
|
555
|
+
// The budget binds. The one encoder there is stands where nothing is due —
|
|
556
|
+
// the viewer left — so it is moved to the soonest number that IS due. Without
|
|
557
|
+
// this the viewer who seeked was served by nobody at all: the run kept its
|
|
558
|
+
// road because nothing covered what lay in front of IT.
|
|
559
|
+
const { made, watches, leaves } = orchestrator({ maxRuns: 3 });
|
|
560
|
+
watches("one", { atSeconds: 400 });
|
|
561
|
+
made.reconcile();
|
|
562
|
+
const [run] = made.runsOn(PICTURE);
|
|
563
|
+
// 6x against a swarm charging 0.25 s per second of film: one encoder already
|
|
564
|
+
// takes one and a half of what is delivered, so the budget is one.
|
|
565
|
+
run.noteSpeed(6);
|
|
566
|
+
made.refetchSecPerFilmSecond = () => 0.25;
|
|
567
|
+
made.noteProduced(PICTURE, 100);
|
|
568
|
+
|
|
569
|
+
watches("one", { atSeconds: 3000 });
|
|
570
|
+
made.reconcile();
|
|
571
|
+
|
|
572
|
+
assert.equal(made.runsOn(PICTURE).length, 1, "still one, because that is what the swarm feeds");
|
|
573
|
+
assert.deepEqual(placements(made), [750], "and it is where the viewer now stands");
|
|
574
|
+
assertNoOverlap(made);
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
// ------------------------------------------------------- seeking BACKWARD
|
|
578
|
+
|
|
579
|
+
test("one viewer seeking back into film that exists is served from it, with no encoder", () => {
|
|
580
|
+
// The case the whole layer was built for. Everything behind them has been
|
|
581
|
+
// made, so nothing is late anywhere they are going, and the score says the
|
|
582
|
+
// cheapest arrangement is the one that changes nothing. A restart here is the
|
|
583
|
+
// 647-second stall of 2026-09-06 in miniature.
|
|
584
|
+
const { made, watches } = orchestrator();
|
|
585
|
+
watches("one", { atSeconds: 400 });
|
|
586
|
+
made.reconcile();
|
|
587
|
+
made.runsOn(PICTURE)[0].noteSpeed(6);
|
|
588
|
+
for (let index = 100; index <= 160; index += 1) {
|
|
589
|
+
made.noteProduced(PICTURE, index);
|
|
590
|
+
}
|
|
591
|
+
const before = made.runsOn(PICTURE);
|
|
592
|
+
|
|
593
|
+
watches("one", { atSeconds: 440 });
|
|
594
|
+
made.reconcile();
|
|
595
|
+
|
|
596
|
+
// Not "the count is unchanged": with nothing late anywhere, the machine is
|
|
597
|
+
// free to spend what it has spare on finishing the file, and that is what the
|
|
598
|
+
// user asked for. What must not happen is a process put on film that already
|
|
599
|
+
// exists, or the run they were behind restarted — which is the 647-second
|
|
600
|
+
// stall of 2026-09-06 in miniature.
|
|
601
|
+
for (const was of before) {
|
|
602
|
+
assert.ok(made.runsOn(PICTURE).includes(was),
|
|
603
|
+
"the encoder they were behind is not restarted");
|
|
604
|
+
}
|
|
605
|
+
for (const run_ of made.runsOn(PICTURE)) {
|
|
606
|
+
// Where it is WORKING, not where it was created: the surviving run was
|
|
607
|
+
// started at #100 and has since produced up to #161.
|
|
608
|
+
assert.equal(made.coverageOf(PICTURE).isReady(run_.head), false,
|
|
609
|
+
`no process is bought for film that exists (#${run_.head})`);
|
|
610
|
+
}
|
|
611
|
+
assertNoOverlap(made);
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
test("one viewer seeking back into film nobody has gets an encoder there", () => {
|
|
615
|
+
// Behind them is not the same as made. Where the film was never encoded, going
|
|
616
|
+
// back is exactly as bare as going forward, and the arithmetic is the same one.
|
|
617
|
+
const { made, watches } = orchestrator();
|
|
618
|
+
watches("one", { atSeconds: 3000 });
|
|
619
|
+
made.reconcile();
|
|
620
|
+
made.runsOn(PICTURE)[0].noteSpeed(6);
|
|
621
|
+
made.noteProduced(PICTURE, 750);
|
|
622
|
+
|
|
623
|
+
watches("one", { atSeconds: 400 });
|
|
624
|
+
made.reconcile();
|
|
625
|
+
|
|
626
|
+
const coverage = made.coverageOf(PICTURE);
|
|
627
|
+
assert.equal(coverage.stateOf(100), "making", "somebody is making where they landed");
|
|
628
|
+
assertNoOverlap(made);
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
test("two viewers: one seeks back onto film the other already had made", () => {
|
|
632
|
+
const { made, watches } = orchestrator({ maxRuns: 3 });
|
|
633
|
+
watches("one", { atSeconds: 400 });
|
|
634
|
+
made.reconcile();
|
|
635
|
+
made.runsOn(PICTURE)[0].noteSpeed(6);
|
|
636
|
+
for (let index = 100; index <= 200; index += 1) {
|
|
637
|
+
made.noteProduced(PICTURE, index);
|
|
638
|
+
}
|
|
639
|
+
watches("two", { atSeconds: 3000 });
|
|
640
|
+
made.reconcile();
|
|
641
|
+
for (const run of made.runsOn(PICTURE)) {
|
|
642
|
+
run.noteSpeed(6);
|
|
643
|
+
}
|
|
644
|
+
const before = made.runsOn(PICTURE).length;
|
|
645
|
+
|
|
646
|
+
// The far one comes back to where the first one has already been.
|
|
647
|
+
watches("two", { atSeconds: 500 });
|
|
648
|
+
made.reconcile();
|
|
649
|
+
|
|
650
|
+
assert.ok(made.runsOn(PICTURE).length <= before,
|
|
651
|
+
"coming back onto made film buys nobody an encoder");
|
|
652
|
+
assertNoOverlap(made);
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
test("three viewers: one forward, one back, one paused", () => {
|
|
656
|
+
// All three motions at once, which is the state a real proxy is in most of the
|
|
657
|
+
// time. What must hold is what always must: never two encoders on one number,
|
|
658
|
+
// never more than the machine affords, and somebody making what the moving
|
|
659
|
+
// viewers are about to need.
|
|
660
|
+
const { made, watches } = orchestrator({ maxRuns: 3 });
|
|
661
|
+
watches("one", { atSeconds: 400 });
|
|
662
|
+
watches("two", { atSeconds: 2000 });
|
|
663
|
+
watches("three", { atSeconds: 3600 });
|
|
664
|
+
made.reconcile();
|
|
665
|
+
for (const run of made.runsOn(PICTURE)) {
|
|
666
|
+
run.noteSpeed(6);
|
|
667
|
+
}
|
|
668
|
+
made.reconcile();
|
|
669
|
+
|
|
670
|
+
watches("one", { atSeconds: 800 });
|
|
671
|
+
watches("two", { atSeconds: 1200 });
|
|
672
|
+
watches("three", { atSeconds: 3600, playing: false });
|
|
673
|
+
made.reconcile();
|
|
674
|
+
|
|
675
|
+
assert.ok(made.runsOn(PICTURE).length <= 3, "never more than the machine holds");
|
|
676
|
+
assertNoOverlap(made);
|
|
677
|
+
const coverage = made.coverageOf(PICTURE);
|
|
678
|
+
assert.equal(coverage.stateOf(200), "making", "the one who went forward is served");
|
|
679
|
+
assert.equal(coverage.stateOf(300), "making", "and so is the one who came back");
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
test("a viewer scrubbing back and forth does not accumulate encoders", () => {
|
|
683
|
+
// A person dragging the time bar states a new position every few hundred
|
|
684
|
+
// milliseconds, and each of those is a state the plan answers. What must hold
|
|
685
|
+
// is that the answers do not pile up: an encoder bought for a place the viewer
|
|
686
|
+
// passed through is not still running when they have gone back.
|
|
687
|
+
//
|
|
688
|
+
// Not that the arrangement is identical to the one before the scrub — the
|
|
689
|
+
// model does not promise that and nothing here should claim it. What it
|
|
690
|
+
// promises is that no arrangement costs more than it is worth.
|
|
691
|
+
const { made, watches } = orchestrator({ maxRuns: 3 });
|
|
692
|
+
watches("one", { atSeconds: 400 });
|
|
693
|
+
made.reconcile();
|
|
694
|
+
made.runsOn(PICTURE)[0].noteSpeed(6);
|
|
695
|
+
made.noteProduced(PICTURE, 100);
|
|
696
|
+
made.reconcile();
|
|
697
|
+
const settled = placements(made);
|
|
698
|
+
|
|
699
|
+
watches("one", { atSeconds: 2000 });
|
|
700
|
+
made.reconcile();
|
|
701
|
+
for (const run of made.runsOn(PICTURE)) {
|
|
702
|
+
run.noteSpeed(6);
|
|
703
|
+
}
|
|
704
|
+
watches("one", { atSeconds: 404 });
|
|
705
|
+
made.reconcile();
|
|
706
|
+
watches("one", { atSeconds: 400 });
|
|
707
|
+
made.reconcile();
|
|
708
|
+
made.reconcile();
|
|
709
|
+
|
|
710
|
+
// Not "no more encoders" — the machine is allowed to spend what it has spare on
|
|
711
|
+
// finishing the file, and after a scrub there is more of the file it has seen.
|
|
712
|
+
// What must not happen is two of them on one stretch, or more than the machine
|
|
713
|
+
// holds, or the viewer left unserved.
|
|
714
|
+
assert.ok(made.runsOn(PICTURE).length <= 3, "never more than the machine holds");
|
|
715
|
+
assert.equal(onTheStretchOf(made, 105), 1, "one encoder on the film in front of them");
|
|
716
|
+
assert.equal(made.coverageOf(PICTURE).stateOf(105), "making", "and the viewer is served");
|
|
717
|
+
assertNoOverlap(made);
|
|
718
|
+
void settled;
|
|
719
|
+
});
|