@torrent-tv/proxy 2.22.0 → 2.24.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.
- package/CHANGELOG.md +20 -0
- package/docs/encode-run-state.md +96 -0
- package/package.json +2 -1
- package/scripts/render-run-graph.js +173 -0
- package/services/encode-exit.js +80 -0
- package/services/encode-run-state.js +488 -0
- package/services/hls-session-manager.js +627 -85
- package/test/concurrent-cost.test.js +138 -82
- package/test/encode-exit.test.js +93 -0
- package/test/encode-run-state.test.js +314 -0
- package/test/quality-variants.test.js +67 -1
- package/test/run-graph-drift.test.js +37 -0
- package/test/segment-serve-wiring.test.js +46 -0
|
@@ -1,82 +1,138 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file A rung is judged against the machine it will actually have.
|
|
3
|
-
*
|
|
4
|
-
* The field case of 2026-08-15, in arithmetic: a copy of the picture took about
|
|
5
|
-
* 0.125 s of work per second of video (7.9-8.0x measured), a 240p rung needed
|
|
6
|
-
* about 1.05, and the two together are more than the one second per second the
|
|
7
|
-
* machine has. The budget priced the copy at nothing, offered the rung, and the
|
|
8
|
-
* viewer watched it fail.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import assert from "node:assert/strict";
|
|
12
|
-
import test from "node:test";
|
|
13
|
-
|
|
14
|
-
import { canSustainOutput } from "../services/hwaccel.js";
|
|
15
|
-
|
|
16
|
-
// A host that encodes 640x360 at 24 fps about eleven times over, and decodes
|
|
17
|
-
// 1080p24 at 2.6x — the addon host's own figures.
|
|
18
|
-
const BENCHMARK = [
|
|
19
|
-
{ preset: "fast", pixelsPerSec: 17.0e6 },
|
|
20
|
-
{ preset: "ultrafast", pixelsPerSec: 67.5e6 }
|
|
21
|
-
];
|
|
22
|
-
const DECODE_MODEL = { pixelTerm: 0.007742, bitrateTerm: 0, constantTerm: 0 };
|
|
23
|
-
const SOURCE = { megapixelsPerSecond: (1920 * 1080 * 24) / 1e6, megabitsPerSecond: 8 };
|
|
24
|
-
const RUNG_240P = 426 * 240 * 24;
|
|
25
|
-
|
|
26
|
-
test("a rung is judged on the machine it will have, not on an empty one", () => {
|
|
27
|
-
const alone = canSustainOutput({
|
|
28
|
-
benchmark: BENCHMARK,
|
|
29
|
-
decodeModel: DECODE_MODEL,
|
|
30
|
-
source: SOURCE,
|
|
31
|
-
outputPixelsPerSec: RUNG_240P
|
|
32
|
-
});
|
|
33
|
-
const beside = canSustainOutput({
|
|
34
|
-
benchmark: BENCHMARK,
|
|
35
|
-
decodeModel: DECODE_MODEL,
|
|
36
|
-
source: SOURCE,
|
|
37
|
-
outputPixelsPerSec: RUNG_240P,
|
|
38
|
-
// The picture is being copied beside it, at the cost the field measured.
|
|
39
|
-
concurrentCostSec: 0.125
|
|
40
|
-
});
|
|
41
|
-
assert.ok(alone.speed !== null && beside.speed !== null);
|
|
42
|
-
assert.ok(beside.speed < alone.speed, "sharing the machine cannot make a rung faster");
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test("nothing else running leaves the answer exactly as it was", () => {
|
|
46
|
-
const withZero = canSustainOutput({
|
|
47
|
-
benchmark: BENCHMARK,
|
|
48
|
-
decodeModel: DECODE_MODEL,
|
|
49
|
-
source: SOURCE,
|
|
50
|
-
outputPixelsPerSec: RUNG_240P,
|
|
51
|
-
concurrentCostSec: 0
|
|
52
|
-
});
|
|
53
|
-
const without = canSustainOutput({
|
|
54
|
-
benchmark: BENCHMARK,
|
|
55
|
-
decodeModel: DECODE_MODEL,
|
|
56
|
-
source: SOURCE,
|
|
57
|
-
outputPixelsPerSec: RUNG_240P
|
|
58
|
-
});
|
|
59
|
-
assert.equal(withZero.speed, without.speed);
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test("a host with nothing measured still refuses nothing", () => {
|
|
63
|
-
const answer = canSustainOutput({
|
|
64
|
-
benchmark: [],
|
|
65
|
-
outputPixelsPerSec: RUNG_240P,
|
|
66
|
-
concurrentCostSec: 0.125
|
|
67
|
-
});
|
|
68
|
-
assert.equal(answer.sustainable, true);
|
|
69
|
-
assert.equal(answer.speed, null);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
test("a cost heavy enough to fill the machine puts a rung below realtime", () => {
|
|
73
|
-
const answer = canSustainOutput({
|
|
74
|
-
benchmark: BENCHMARK,
|
|
75
|
-
decodeModel: DECODE_MODEL,
|
|
76
|
-
source: SOURCE,
|
|
77
|
-
outputPixelsPerSec: RUNG_240P,
|
|
78
|
-
concurrentCostSec: 0.9
|
|
79
|
-
});
|
|
80
|
-
assert.ok(answer.speed !== null && answer.speed < 1, "0.9 of the machine spent elsewhere leaves less than realtime");
|
|
81
|
-
assert.equal(answer.sustainable, false);
|
|
82
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* @file A rung is judged against the machine it will actually have.
|
|
3
|
+
*
|
|
4
|
+
* The field case of 2026-08-15, in arithmetic: a copy of the picture took about
|
|
5
|
+
* 0.125 s of work per second of video (7.9-8.0x measured), a 240p rung needed
|
|
6
|
+
* about 1.05, and the two together are more than the one second per second the
|
|
7
|
+
* machine has. The budget priced the copy at nothing, offered the rung, and the
|
|
8
|
+
* viewer watched it fail.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import assert from "node:assert/strict";
|
|
12
|
+
import test from "node:test";
|
|
13
|
+
|
|
14
|
+
import { canSustainOutput } from "../services/hwaccel.js";
|
|
15
|
+
|
|
16
|
+
// A host that encodes 640x360 at 24 fps about eleven times over, and decodes
|
|
17
|
+
// 1080p24 at 2.6x — the addon host's own figures.
|
|
18
|
+
const BENCHMARK = [
|
|
19
|
+
{ preset: "fast", pixelsPerSec: 17.0e6 },
|
|
20
|
+
{ preset: "ultrafast", pixelsPerSec: 67.5e6 }
|
|
21
|
+
];
|
|
22
|
+
const DECODE_MODEL = { pixelTerm: 0.007742, bitrateTerm: 0, constantTerm: 0 };
|
|
23
|
+
const SOURCE = { megapixelsPerSecond: (1920 * 1080 * 24) / 1e6, megabitsPerSecond: 8 };
|
|
24
|
+
const RUNG_240P = 426 * 240 * 24;
|
|
25
|
+
|
|
26
|
+
test("a rung is judged on the machine it will have, not on an empty one", () => {
|
|
27
|
+
const alone = canSustainOutput({
|
|
28
|
+
benchmark: BENCHMARK,
|
|
29
|
+
decodeModel: DECODE_MODEL,
|
|
30
|
+
source: SOURCE,
|
|
31
|
+
outputPixelsPerSec: RUNG_240P
|
|
32
|
+
});
|
|
33
|
+
const beside = canSustainOutput({
|
|
34
|
+
benchmark: BENCHMARK,
|
|
35
|
+
decodeModel: DECODE_MODEL,
|
|
36
|
+
source: SOURCE,
|
|
37
|
+
outputPixelsPerSec: RUNG_240P,
|
|
38
|
+
// The picture is being copied beside it, at the cost the field measured.
|
|
39
|
+
concurrentCostSec: 0.125
|
|
40
|
+
});
|
|
41
|
+
assert.ok(alone.speed !== null && beside.speed !== null);
|
|
42
|
+
assert.ok(beside.speed < alone.speed, "sharing the machine cannot make a rung faster");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("nothing else running leaves the answer exactly as it was", () => {
|
|
46
|
+
const withZero = canSustainOutput({
|
|
47
|
+
benchmark: BENCHMARK,
|
|
48
|
+
decodeModel: DECODE_MODEL,
|
|
49
|
+
source: SOURCE,
|
|
50
|
+
outputPixelsPerSec: RUNG_240P,
|
|
51
|
+
concurrentCostSec: 0
|
|
52
|
+
});
|
|
53
|
+
const without = canSustainOutput({
|
|
54
|
+
benchmark: BENCHMARK,
|
|
55
|
+
decodeModel: DECODE_MODEL,
|
|
56
|
+
source: SOURCE,
|
|
57
|
+
outputPixelsPerSec: RUNG_240P
|
|
58
|
+
});
|
|
59
|
+
assert.equal(withZero.speed, without.speed);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("a host with nothing measured still refuses nothing", () => {
|
|
63
|
+
const answer = canSustainOutput({
|
|
64
|
+
benchmark: [],
|
|
65
|
+
outputPixelsPerSec: RUNG_240P,
|
|
66
|
+
concurrentCostSec: 0.125
|
|
67
|
+
});
|
|
68
|
+
assert.equal(answer.sustainable, true);
|
|
69
|
+
assert.equal(answer.speed, null);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("a cost heavy enough to fill the machine puts a rung below realtime", () => {
|
|
73
|
+
const answer = canSustainOutput({
|
|
74
|
+
benchmark: BENCHMARK,
|
|
75
|
+
decodeModel: DECODE_MODEL,
|
|
76
|
+
source: SOURCE,
|
|
77
|
+
outputPixelsPerSec: RUNG_240P,
|
|
78
|
+
concurrentCostSec: 0.9
|
|
79
|
+
});
|
|
80
|
+
assert.ok(answer.speed !== null && answer.speed < 1, "0.9 of the machine spent elsewhere leaves less than realtime");
|
|
81
|
+
assert.equal(answer.sustainable, false);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("a soundtrack encoder is not free once it has been measured", () => {
|
|
85
|
+
// The second half of roadmap item 6. An audio rendition runs for as long as
|
|
86
|
+
// the picture does — it is a second encoder by construction, not an extra —
|
|
87
|
+
// and the budget counted it at nothing. Small beside a picture, and small is
|
|
88
|
+
// not zero: on a host where a rung needs almost the whole machine, this is
|
|
89
|
+
// the difference between offering it and refusing it.
|
|
90
|
+
const withoutAudio = canSustainOutput({
|
|
91
|
+
benchmark: BENCHMARK,
|
|
92
|
+
decodeModel: DECODE_MODEL,
|
|
93
|
+
source: SOURCE,
|
|
94
|
+
outputPixelsPerSec: RUNG_240P,
|
|
95
|
+
concurrentCostSec: 0.125
|
|
96
|
+
});
|
|
97
|
+
const withAudio = canSustainOutput({
|
|
98
|
+
benchmark: BENCHMARK,
|
|
99
|
+
decodeModel: DECODE_MODEL,
|
|
100
|
+
source: SOURCE,
|
|
101
|
+
outputPixelsPerSec: RUNG_240P,
|
|
102
|
+
// The copy, plus a soundtrack measured at about twenty times realtime.
|
|
103
|
+
concurrentCostSec: 0.125 + 0.05
|
|
104
|
+
});
|
|
105
|
+
assert.ok(
|
|
106
|
+
withAudio.speed < withoutAudio.speed,
|
|
107
|
+
"charging for the soundtrack must lower what the machine has left for the picture"
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("two pictures encoding at once are charged as two", () => {
|
|
112
|
+
// The warm-up that makes a quality switch seamless runs two encoders on
|
|
113
|
+
// purpose. Measured 2026-08-15: the new rung ran at 0.504x for its first four
|
|
114
|
+
// seconds, against 0.90-0.99x once it had the machine to itself and 1.58x
|
|
115
|
+
// predicted for an idle one. Priced as though it were alone, the budget
|
|
116
|
+
// describes a machine that does not exist at the moment the viewer switches.
|
|
117
|
+
const alone = canSustainOutput({
|
|
118
|
+
benchmark: BENCHMARK,
|
|
119
|
+
decodeModel: DECODE_MODEL,
|
|
120
|
+
source: SOURCE,
|
|
121
|
+
outputPixelsPerSec: RUNG_240P,
|
|
122
|
+
concurrentCostSec: 0
|
|
123
|
+
});
|
|
124
|
+
const besideAnother = canSustainOutput({
|
|
125
|
+
benchmark: BENCHMARK,
|
|
126
|
+
decodeModel: DECODE_MODEL,
|
|
127
|
+
source: SOURCE,
|
|
128
|
+
outputPixelsPerSec: RUNG_240P,
|
|
129
|
+
// Another rung of the same family, already running at about realtime.
|
|
130
|
+
concurrentCostSec: 1 / 1.05
|
|
131
|
+
});
|
|
132
|
+
assert.ok(alone.sustainable, "the rung must be offerable on an idle machine, or the case is not the one meant");
|
|
133
|
+
assert.equal(
|
|
134
|
+
besideAnother.sustainable,
|
|
135
|
+
false,
|
|
136
|
+
"a machine already spending a second per second of video has nothing left for a second picture"
|
|
137
|
+
);
|
|
138
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The four things an ffmpeg exit can mean, by their field cases.
|
|
3
|
+
*
|
|
4
|
+
* Each test names the release the classification cost. None of them needs a
|
|
5
|
+
* process: what went wrong in every case was the reading of the exit, not the
|
|
6
|
+
* handling of it.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import assert from "node:assert/strict";
|
|
10
|
+
import test from "node:test";
|
|
11
|
+
|
|
12
|
+
import { ENCODE_EXIT, classifyEncodeExit } from "../services/encode-exit.js";
|
|
13
|
+
|
|
14
|
+
test("the predecessor a seek kills says nothing about the session", () => {
|
|
15
|
+
// The handler decides "is this exit mine" by comparing against the session's
|
|
16
|
+
// current process — and during a restart that field still names the process
|
|
17
|
+
// being killed, because the replacement is spawned afterwards. So a SIGKILLed
|
|
18
|
+
// predecessor passed the check and was read as the session's own run dying:
|
|
19
|
+
// a spurious "failed" that a segment request landing in that window is
|
|
20
|
+
// answered 500 for, and on a hardware host a permanent downgrade to libx264.
|
|
21
|
+
assert.equal(
|
|
22
|
+
classifyEncodeExit({ superseded: true, code: null, inputUnavailable: false }),
|
|
23
|
+
ENCODE_EXIT.IGNORED
|
|
24
|
+
);
|
|
25
|
+
// Including when it looks like a clean finish.
|
|
26
|
+
assert.equal(classifyEncodeExit({ superseded: true, code: 0 }), ENCODE_EXIT.IGNORED);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("exit 0 short of the last segment is a failure, not a finished file", () => {
|
|
30
|
+
// 2.9.104. A run that had made 188 segments of 624 reported itself complete,
|
|
31
|
+
// the player consumed what was on disk and froze on the first segment nobody
|
|
32
|
+
// was making.
|
|
33
|
+
assert.equal(
|
|
34
|
+
classifyEncodeExit({ code: 0, producedThrough: 188, lastSegmentIndex: 623 }),
|
|
35
|
+
ENCODE_EXIT.SHORT
|
|
36
|
+
);
|
|
37
|
+
assert.equal(
|
|
38
|
+
classifyEncodeExit({ code: 0, producedThrough: 623, lastSegmentIndex: 623 }),
|
|
39
|
+
ENCODE_EXIT.COMPLETE
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("exit 0 is complete when there is nothing to check it against", () => {
|
|
44
|
+
// No published last segment, or nothing readable on disk: the claim cannot be
|
|
45
|
+
// contradicted, so it stands. Guessing "short" here would fail every session
|
|
46
|
+
// whose directory could not be read.
|
|
47
|
+
assert.equal(classifyEncodeExit({ code: 0, producedThrough: null, lastSegmentIndex: 623 }), ENCODE_EXIT.COMPLETE);
|
|
48
|
+
assert.equal(classifyEncodeExit({ code: 0, producedThrough: 12, lastSegmentIndex: null }), ENCODE_EXIT.COMPLETE);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("data that went away is recoverable, and is not an encoder fault", () => {
|
|
52
|
+
// Field 2026-08-06: a torrent evicted mid-seek took the film with it, the run
|
|
53
|
+
// died on `File 0 not found`, and the session answered 500 from then on while
|
|
54
|
+
// the swarm was there and the data would have come back in seconds.
|
|
55
|
+
assert.equal(
|
|
56
|
+
classifyEncodeExit({ code: 1, inputUnavailable: true }),
|
|
57
|
+
ENCODE_EXIT.INPUT_LOST
|
|
58
|
+
);
|
|
59
|
+
// And the distinction is what keeps a working hardware encoder: the caller
|
|
60
|
+
// downgrades to software on FAILED, so an input that vanished must not
|
|
61
|
+
// arrive there.
|
|
62
|
+
assert.notEqual(classifyEncodeExit({ code: 1, inputUnavailable: true }), ENCODE_EXIT.FAILED);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("anything else is a failure of this target", () => {
|
|
66
|
+
assert.equal(classifyEncodeExit({ code: 1, inputUnavailable: false }), ENCODE_EXIT.FAILED);
|
|
67
|
+
assert.equal(classifyEncodeExit({ code: null, inputUnavailable: false }), ENCODE_EXIT.FAILED);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("every combination answers, and no input produces undefined", () => {
|
|
71
|
+
for (const superseded of [true, false]) {
|
|
72
|
+
for (const code of [0, 1, null]) {
|
|
73
|
+
for (const producedThrough of [null, 0, 10]) {
|
|
74
|
+
for (const lastSegmentIndex of [null, 0, 10]) {
|
|
75
|
+
for (const inputUnavailable of [true, false]) {
|
|
76
|
+
const answer = classifyEncodeExit({
|
|
77
|
+
superseded,
|
|
78
|
+
code,
|
|
79
|
+
producedThrough,
|
|
80
|
+
lastSegmentIndex,
|
|
81
|
+
inputUnavailable
|
|
82
|
+
});
|
|
83
|
+
assert.ok(
|
|
84
|
+
Object.values(ENCODE_EXIT).includes(answer),
|
|
85
|
+
`answered ${String(answer)} for ${JSON.stringify({ superseded, code, producedThrough, lastSegmentIndex, inputUnavailable })}`
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
assert.ok(Object.values(ENCODE_EXIT).includes(classifyEncodeExit()));
|
|
93
|
+
});
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The encoder-run table, exercised as a graph.
|
|
3
|
+
*
|
|
4
|
+
* These are properties of the specification, not of any run: no ffmpeg, no
|
|
5
|
+
* torrent, no filesystem, no clock. What they buy is the thing five field
|
|
6
|
+
* failures in a row were — an empty cell — being visible before a release
|
|
7
|
+
* rather than after one.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import assert from "node:assert/strict";
|
|
11
|
+
import test from "node:test";
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
ABSENT_EDGE_INVARIANTS,
|
|
15
|
+
ENCODE_RUN_EVENT,
|
|
16
|
+
ENCODE_RUN_STATE,
|
|
17
|
+
ENCODE_RUN_SUPERSTATE,
|
|
18
|
+
INITIAL_RUN_STATE,
|
|
19
|
+
answerForMissingSegment,
|
|
20
|
+
declaredContainment,
|
|
21
|
+
declaredEdges,
|
|
22
|
+
isInputBeingRead,
|
|
23
|
+
isWithin,
|
|
24
|
+
mayRestart,
|
|
25
|
+
nextState,
|
|
26
|
+
processCanBeSignalled,
|
|
27
|
+
wireState
|
|
28
|
+
} from "../services/encode-run-state.js";
|
|
29
|
+
|
|
30
|
+
const ALL_STATES = Object.values(ENCODE_RUN_STATE);
|
|
31
|
+
const ALL_EVENTS = Object.values(ENCODE_RUN_EVENT);
|
|
32
|
+
|
|
33
|
+
// ------------------------------------------------------------ graph discipline
|
|
34
|
+
|
|
35
|
+
test("the relation is deterministic", () => {
|
|
36
|
+
for (const state of ALL_STATES) {
|
|
37
|
+
for (const event of ALL_EVENTS) {
|
|
38
|
+
assert.equal(nextState(state, event), nextState(state, event));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("every pair is answered, and nothing throws", () => {
|
|
44
|
+
for (const state of ALL_STATES) {
|
|
45
|
+
for (const event of ALL_EVENTS) {
|
|
46
|
+
const target = nextState(state, event);
|
|
47
|
+
assert.ok(
|
|
48
|
+
target === null || ALL_STATES.includes(target),
|
|
49
|
+
`${state} + ${event} answered ${String(target)}, which is neither a state nor "ignored"`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// Nonsense in, "ignored" out — never an exception. The machine this pattern
|
|
54
|
+
// replaces threw from inside an event handler, which left the caller's work
|
|
55
|
+
// half applied.
|
|
56
|
+
assert.equal(nextState("NOT_A_STATE", ENCODE_RUN_EVENT.SPAWNED), null);
|
|
57
|
+
assert.equal(nextState(ENCODE_RUN_STATE.IDLE, "NOT_AN_EVENT"), null);
|
|
58
|
+
assert.equal(nextState(undefined, undefined), null);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("one target per state and event — no pair is declared twice", () => {
|
|
62
|
+
const seen = new Set();
|
|
63
|
+
for (const edge of declaredEdges()) {
|
|
64
|
+
const pair = `${edge.from}+${edge.event}`;
|
|
65
|
+
assert.ok(!seen.has(pair), `${pair} is declared more than once`);
|
|
66
|
+
seen.add(pair);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("the table cannot be edited through what it hands out", () => {
|
|
71
|
+
const [first] = declaredEdges();
|
|
72
|
+
const mutated = declaredEdges();
|
|
73
|
+
mutated[0].to = "TAMPERED";
|
|
74
|
+
assert.equal(declaredEdges()[0].to, first.to);
|
|
75
|
+
const containment = declaredContainment();
|
|
76
|
+
containment[0].parent = "TAMPERED";
|
|
77
|
+
assert.notEqual(declaredContainment()[0].parent, "TAMPERED");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("every state is reachable from where a run begins", () => {
|
|
81
|
+
const seen = new Set([INITIAL_RUN_STATE]);
|
|
82
|
+
const queue = [INITIAL_RUN_STATE];
|
|
83
|
+
while (queue.length > 0) {
|
|
84
|
+
const state = queue.shift();
|
|
85
|
+
for (const event of ALL_EVENTS) {
|
|
86
|
+
const target = nextState(state, event);
|
|
87
|
+
if (target && !seen.has(target)) {
|
|
88
|
+
seen.add(target);
|
|
89
|
+
queue.push(target);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
assert.deepEqual(
|
|
94
|
+
ALL_STATES.filter((state) => !seen.has(state)),
|
|
95
|
+
[],
|
|
96
|
+
"a state no sequence of events can enter is either dead code or a missing edge"
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("no state is a dead end — every one can start a run again", () => {
|
|
101
|
+
for (const state of ALL_STATES) {
|
|
102
|
+
assert.equal(
|
|
103
|
+
nextState(state, ENCODE_RUN_EVENT.SPAWNED),
|
|
104
|
+
ENCODE_RUN_STATE.STARTING,
|
|
105
|
+
`${state} must be able to spawn a run: a session that cannot restart answers 500 for ever, ` +
|
|
106
|
+
"which is what the roadmap calls the empty cell"
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("the two edges that lead back to their own state are deliberate", () => {
|
|
112
|
+
const selfEdges = [];
|
|
113
|
+
for (const state of ALL_STATES) {
|
|
114
|
+
for (const event of ALL_EVENTS) {
|
|
115
|
+
if (nextState(state, event) === state) {
|
|
116
|
+
selfEdges.push(`${state} + ${event}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
assert.deepEqual(
|
|
121
|
+
selfEdges.sort(),
|
|
122
|
+
[
|
|
123
|
+
// A new run spawned while one was starting: the state is right already,
|
|
124
|
+
// and nothing in the caller re-runs on entry — the spawn has happened.
|
|
125
|
+
`${ENCODE_RUN_STATE.STARTING} + ${ENCODE_RUN_EVENT.SPAWNED}`,
|
|
126
|
+
// Suspending what is suspended. The caller returns early before signalling,
|
|
127
|
+
// so this is the answer to an event that cannot actually be raised twice.
|
|
128
|
+
`${ENCODE_RUN_STATE.SUSPENDED} + ${ENCODE_RUN_EVENT.SUSPEND_ORDERED}`
|
|
129
|
+
].sort(),
|
|
130
|
+
"an edge that leads back to its own state must be argued for, not acquired"
|
|
131
|
+
);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("the table stays small enough to hold in the head", () => {
|
|
135
|
+
assert.ok(
|
|
136
|
+
declaredEdges().length <= 12,
|
|
137
|
+
`${declaredEdges().length} declared edges — a near-complete digraph asserts nothing`
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// ------------------------------------------------------- what must not happen
|
|
142
|
+
|
|
143
|
+
test("the absent edges are absent", () => {
|
|
144
|
+
for (const invariant of ABSENT_EDGE_INVARIANTS) {
|
|
145
|
+
assert.notEqual(
|
|
146
|
+
nextState(invariant.from, invariant.event),
|
|
147
|
+
invariant.mustNotReach,
|
|
148
|
+
`${invariant.from} + ${invariant.event} reached ${invariant.mustNotReach}: ${invariant.because}`
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("an event that means nothing here is ignored, not obeyed", () => {
|
|
154
|
+
assert.equal(nextState(ENCODE_RUN_STATE.IDLE, ENCODE_RUN_EVENT.FIRST_SEGMENT), null);
|
|
155
|
+
assert.equal(nextState(ENCODE_RUN_STATE.IDLE, ENCODE_RUN_EVENT.EXITED_COMPLETE), null);
|
|
156
|
+
assert.equal(nextState(ENCODE_RUN_STATE.STOPPED, ENCODE_RUN_EVENT.RESUME_ORDERED), null);
|
|
157
|
+
assert.equal(nextState(ENCODE_RUN_STATE.ENDED_COMPLETE, ENCODE_RUN_EVENT.SUSPEND_ORDERED), null);
|
|
158
|
+
assert.equal(nextState(ENCODE_RUN_STATE.PRODUCING, ENCODE_RUN_EVENT.RETRY_DUE), null);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// ------------------------------------------------------------------ hierarchy
|
|
162
|
+
|
|
163
|
+
test("an edge on a superstate reaches every state inside it", () => {
|
|
164
|
+
const alive = ALL_STATES.filter((state) => isWithin(state, ENCODE_RUN_SUPERSTATE.ALIVE));
|
|
165
|
+
assert.deepEqual(
|
|
166
|
+
alive.sort(),
|
|
167
|
+
[ENCODE_RUN_STATE.PRODUCING, ENCODE_RUN_STATE.STARTING, ENCODE_RUN_STATE.SUSPENDED].sort()
|
|
168
|
+
);
|
|
169
|
+
for (const state of alive) {
|
|
170
|
+
assert.equal(nextState(state, ENCODE_RUN_EVENT.EXITED_FAILED), ENCODE_RUN_STATE.ENDED_FAILED);
|
|
171
|
+
assert.equal(nextState(state, ENCODE_RUN_EVENT.EXITED_INPUT_LOST), ENCODE_RUN_STATE.RETRY_WAIT);
|
|
172
|
+
assert.equal(nextState(state, ENCODE_RUN_EVENT.STOP_ORDERED), ENCODE_RUN_STATE.STOPPED);
|
|
173
|
+
}
|
|
174
|
+
const working = ALL_STATES.filter((state) => isWithin(state, ENCODE_RUN_SUPERSTATE.WORKING));
|
|
175
|
+
assert.deepEqual(working.sort(), [...alive, ENCODE_RUN_STATE.RETRY_WAIT].sort());
|
|
176
|
+
for (const state of ALL_STATES) {
|
|
177
|
+
assert.ok(isWithin(state, ENCODE_RUN_SUPERSTATE.RUN), `${state} must sit inside RUN`);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("a state's own edge wins over the one it inherits", () => {
|
|
182
|
+
// STARTING inherits SPAWNED from RUN and declares FIRST_SEGMENT itself; the
|
|
183
|
+
// walk must stop at the first hit rather than continue up the chain.
|
|
184
|
+
assert.equal(
|
|
185
|
+
nextState(ENCODE_RUN_STATE.STARTING, ENCODE_RUN_EVENT.FIRST_SEGMENT),
|
|
186
|
+
ENCODE_RUN_STATE.PRODUCING
|
|
187
|
+
);
|
|
188
|
+
assert.equal(
|
|
189
|
+
nextState(ENCODE_RUN_STATE.SUSPENDED, ENCODE_RUN_EVENT.RESUME_ORDERED),
|
|
190
|
+
ENCODE_RUN_STATE.PRODUCING
|
|
191
|
+
);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// -------------------------------------------------------------------- outputs
|
|
195
|
+
|
|
196
|
+
test("only a live run reads its input", () => {
|
|
197
|
+
assert.equal(isInputBeingRead(ENCODE_RUN_STATE.STARTING), true);
|
|
198
|
+
assert.equal(isInputBeingRead(ENCODE_RUN_STATE.PRODUCING), true);
|
|
199
|
+
assert.equal(
|
|
200
|
+
isInputBeingRead(ENCODE_RUN_STATE.SUSPENDED),
|
|
201
|
+
false,
|
|
202
|
+
"a suspended encoder reads nothing — the reader's window is then satisfied, WebTorrent drops " +
|
|
203
|
+
"the selection, and the download dies with the swarm open (eleven minutes, 2026-08-05)"
|
|
204
|
+
);
|
|
205
|
+
for (const state of [
|
|
206
|
+
ENCODE_RUN_STATE.IDLE,
|
|
207
|
+
ENCODE_RUN_STATE.RETRY_WAIT,
|
|
208
|
+
ENCODE_RUN_STATE.STOPPED,
|
|
209
|
+
ENCODE_RUN_STATE.ENDED_COMPLETE,
|
|
210
|
+
ENCODE_RUN_STATE.ENDED_FAILED
|
|
211
|
+
]) {
|
|
212
|
+
assert.equal(isInputBeingRead(state), false, `${state} has no process to read anything`);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("a signal may be sent only where a process exists", () => {
|
|
217
|
+
for (const state of ALL_STATES) {
|
|
218
|
+
assert.equal(
|
|
219
|
+
processCanBeSignalled(state),
|
|
220
|
+
isWithin(state, ENCODE_RUN_SUPERSTATE.ALIVE),
|
|
221
|
+
`${state} answered the wrong thing about whether it holds a process`
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
test("a missing segment is held everywhere except a terminal failure", () => {
|
|
227
|
+
for (const state of ALL_STATES) {
|
|
228
|
+
assert.equal(
|
|
229
|
+
answerForMissingSegment(state),
|
|
230
|
+
state === ENCODE_RUN_STATE.ENDED_FAILED ? "fail" : "hold",
|
|
231
|
+
`${state} must give one answer for a missing segment, not one per call site`
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test("the wire state is a function of the run state alone", () => {
|
|
237
|
+
assert.equal(wireState(ENCODE_RUN_STATE.IDLE), "starting");
|
|
238
|
+
assert.equal(wireState(ENCODE_RUN_STATE.RETRY_WAIT), "starting");
|
|
239
|
+
assert.equal(wireState(ENCODE_RUN_STATE.STARTING), "running");
|
|
240
|
+
assert.equal(wireState(ENCODE_RUN_STATE.PRODUCING), "running");
|
|
241
|
+
assert.equal(wireState(ENCODE_RUN_STATE.SUSPENDED), "running");
|
|
242
|
+
assert.equal(wireState(ENCODE_RUN_STATE.STOPPED), "running");
|
|
243
|
+
assert.equal(wireState(ENCODE_RUN_STATE.ENDED_COMPLETE), "ready");
|
|
244
|
+
assert.equal(wireState(ENCODE_RUN_STATE.ENDED_FAILED), "failed");
|
|
245
|
+
for (const state of ALL_STATES) {
|
|
246
|
+
assert.ok(
|
|
247
|
+
["starting", "running", "ready", "failed"].includes(wireState(state)),
|
|
248
|
+
`${state} produced a wire state the browser has never been told about`
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("only a finished file refuses to be repositioned", () => {
|
|
254
|
+
for (const state of ALL_STATES) {
|
|
255
|
+
assert.equal(mayRestart(state), state !== ENCODE_RUN_STATE.ENDED_COMPLETE);
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// ------------------------------------------------- the failures, by their name
|
|
260
|
+
|
|
261
|
+
test("the sawtooth of 2.9.93 is not expressible", () => {
|
|
262
|
+
// Any segment request released a suspended encoder, and the run drifted from
|
|
263
|
+
// 155 s to 922 s ahead of the viewer in three minutes. Resuming is an output
|
|
264
|
+
// of how far ahead the run is, computed by the caller — never an edge hung on
|
|
265
|
+
// a request arriving.
|
|
266
|
+
assert.equal(nextState(ENCODE_RUN_STATE.SUSPENDED, ENCODE_RUN_EVENT.FIRST_SEGMENT), null);
|
|
267
|
+
assert.equal(
|
|
268
|
+
nextState(ENCODE_RUN_STATE.SUSPENDED, ENCODE_RUN_EVENT.RESUME_ORDERED),
|
|
269
|
+
ENCODE_RUN_STATE.PRODUCING
|
|
270
|
+
);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test("a dead run cannot be mistaken for one that is covering the seek", () => {
|
|
274
|
+
// `session.ffmpeg` pointed at a corpse, so every later seek was waved through
|
|
275
|
+
// as "already covered by the running encode" and the session answered 500 for
|
|
276
|
+
// as long as the viewer kept trying.
|
|
277
|
+
assert.equal(processCanBeSignalled(ENCODE_RUN_STATE.ENDED_FAILED), false);
|
|
278
|
+
assert.equal(isInputBeingRead(ENCODE_RUN_STATE.ENDED_FAILED), false);
|
|
279
|
+
assert.equal(
|
|
280
|
+
nextState(ENCODE_RUN_STATE.ENDED_FAILED, ENCODE_RUN_EVENT.SPAWNED),
|
|
281
|
+
ENCODE_RUN_STATE.STARTING,
|
|
282
|
+
"and the way out of it is a spawn — the edge whose absence is the empty cell"
|
|
283
|
+
);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
test("a rung the viewer left is not the same as a run that has not started", () => {
|
|
287
|
+
// Both are "no process" in the field set this replaces, and they call for
|
|
288
|
+
// opposite answers: one must not be revived by its own held requests, the
|
|
289
|
+
// other is waiting to be spawned.
|
|
290
|
+
assert.notEqual(ENCODE_RUN_STATE.STOPPED, ENCODE_RUN_STATE.IDLE);
|
|
291
|
+
assert.equal(nextState(ENCODE_RUN_STATE.STOPPED, ENCODE_RUN_EVENT.RESUME_ORDERED), null);
|
|
292
|
+
assert.equal(nextState(ENCODE_RUN_STATE.IDLE, ENCODE_RUN_EVENT.SPAWNED), ENCODE_RUN_STATE.STARTING);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
test("an input that dried up is not a finished file", () => {
|
|
296
|
+
// ffmpeg exits 0 for both, and the difference had to be recovered by
|
|
297
|
+
// comparing what was produced against the published playlist (2.9.104).
|
|
298
|
+
assert.equal(
|
|
299
|
+
nextState(ENCODE_RUN_STATE.PRODUCING, ENCODE_RUN_EVENT.EXITED_SHORT),
|
|
300
|
+
ENCODE_RUN_STATE.ENDED_FAILED
|
|
301
|
+
);
|
|
302
|
+
assert.equal(
|
|
303
|
+
nextState(ENCODE_RUN_STATE.PRODUCING, ENCODE_RUN_EVENT.EXITED_COMPLETE),
|
|
304
|
+
ENCODE_RUN_STATE.ENDED_COMPLETE
|
|
305
|
+
);
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test("losing the input holds the viewer's requests instead of failing them", () => {
|
|
309
|
+
const afterLoss = nextState(ENCODE_RUN_STATE.PRODUCING, ENCODE_RUN_EVENT.EXITED_INPUT_LOST);
|
|
310
|
+
assert.equal(afterLoss, ENCODE_RUN_STATE.RETRY_WAIT);
|
|
311
|
+
assert.equal(answerForMissingSegment(afterLoss), "hold");
|
|
312
|
+
assert.equal(wireState(afterLoss), "starting");
|
|
313
|
+
assert.equal(nextState(afterLoss, ENCODE_RUN_EVENT.RETRY_DUE), ENCODE_RUN_STATE.IDLE);
|
|
314
|
+
});
|