@torrent-tv/proxy 2.83.8 → 2.85.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 +23 -0
- package/CLAUDE.md +5 -3
- package/docs/encode-architecture.md +26 -1
- package/docs/logs.md +97 -51
- package/package.json +1 -1
- package/routes/api/link-probe/get.js +51 -0
- package/routes/api/transcode-sessions/net-report/post.js +84 -64
- package/routes/api/transcode-sessions/post.js +139 -137
- package/routes/transcode/session-file/get.js +268 -240
- package/server.js +3 -0
- package/services/data-channel-handler.js +12 -0
- package/services/delivery-probe.js +842 -828
- package/services/hls-session-manager.js +9759 -10006
- package/services/output/OutputSpec.js +202 -195
- package/services/priority/PriorityMap.js +390 -362
- package/services/priority/PriorityOrchestrator.js +320 -318
- package/services/viewer/Viewer.js +179 -83
- package/services/viewer/positions.js +120 -48
- package/services/viewer/report-intake.js +63 -0
- package/test/held-request-width.test.js +8 -5
- package/test/link-probe.test.js +56 -0
- package/test/orchestrator-wired.test.js +14 -8
- package/test/output-spec.test.js +157 -157
- package/test/plan-is-steady.test.js +225 -0
- package/test/priority-map-emptied.test.js +205 -207
- package/test/priority-map-per-output.test.js +209 -211
- package/test/quality-variants.test.js +1167 -1222
- package/test/seek-target-not-superseded.test.js +14 -12
- package/test/stale-request-after-seek.test.js +2 -3
- package/test/viewer-states.test.js +151 -0
- package/test/viewer.test.js +136 -108
- package/test/viewer-position-on-open.test.js +0 -92
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The route that hands a browser bytes to time its link with.
|
|
3
|
+
*
|
|
4
|
+
* Only this end's half. How big an ask should be is derived on the page, from
|
|
5
|
+
* what its last ask measured, and is checked where that function lives — a copy
|
|
6
|
+
* of it here would be two owners of one rule, which is the fault this
|
|
7
|
+
* repository keeps recording.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import assert from "node:assert/strict";
|
|
11
|
+
import test from "node:test";
|
|
12
|
+
|
|
13
|
+
import { handleApiLinkProbeGet, MAX_LINK_PROBE_BYTES } from "../routes/api/link-probe/get.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A reply that records what it was given.
|
|
17
|
+
*
|
|
18
|
+
* @returns {{ code: (n: number) => object, header: () => object, send: (b: unknown) => object, status: number | null, body: unknown }}
|
|
19
|
+
*/
|
|
20
|
+
function fakeReply() {
|
|
21
|
+
const reply = {
|
|
22
|
+
status: /** @type {number | null} */ (null),
|
|
23
|
+
body: /** @type {unknown} */ (null),
|
|
24
|
+
code(n) {
|
|
25
|
+
reply.status = n;
|
|
26
|
+
return reply;
|
|
27
|
+
},
|
|
28
|
+
header() {
|
|
29
|
+
return reply;
|
|
30
|
+
},
|
|
31
|
+
send(body) {
|
|
32
|
+
reply.body = body;
|
|
33
|
+
return reply;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
return reply;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
test("the route answers exactly what was asked for, up to its own ceiling", async () => {
|
|
42
|
+
const small = fakeReply();
|
|
43
|
+
await handleApiLinkProbeGet({ query: { bytes: "65536" } }, small);
|
|
44
|
+
assert.equal(small.status, 200);
|
|
45
|
+
assert.equal(/** @type {Buffer} */ (small.body).length, 65536);
|
|
46
|
+
|
|
47
|
+
const huge = fakeReply();
|
|
48
|
+
await handleApiLinkProbeGet({ query: { bytes: "999999999" } }, huge);
|
|
49
|
+
assert.equal(/** @type {Buffer} */ (huge.body).length, MAX_LINK_PROBE_BYTES);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("the route refuses an ask that is not a size", async () => {
|
|
53
|
+
const reply = fakeReply();
|
|
54
|
+
await handleApiLinkProbeGet({ query: { bytes: "none" } }, reply);
|
|
55
|
+
assert.equal(reply.status, 400);
|
|
56
|
+
});
|
|
@@ -109,21 +109,27 @@ test("what a viewer waits for reaches the plan without their name", (t) => {
|
|
|
109
109
|
assert.ok(wanted.some((zone) => zone.to > 5), "and the cushion in front of them");
|
|
110
110
|
});
|
|
111
111
|
|
|
112
|
-
test("a viewer
|
|
112
|
+
test("a viewer who has gone stops being waited for, and silence alone never counts", (t) => {
|
|
113
113
|
const { manager, root, dirPath } = managerWithAnOutput();
|
|
114
114
|
t.after(() => rmSync(root, { recursive: true, force: true }));
|
|
115
115
|
|
|
116
116
|
const session = sessionOn({ id: "one", dirPath });
|
|
117
117
|
manager.sessionsById.set(session.id, session);
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
118
|
+
const person = viewerOf(session, "gone");
|
|
119
|
+
person.moveTo(20, Date.now() - 10 * 60 * 1000);
|
|
120
|
+
person.playing = false;
|
|
121
|
+
person.seen(Date.now() - 10 * 60 * 1000);
|
|
122
|
+
|
|
123
|
+
// TEN MINUTES OF SILENCE IS NOT LEAVING. A paused viewer, a hidden tab whose
|
|
124
|
+
// timers the browser has throttled, and one holding a full cushion are all
|
|
125
|
+
// silent and all still watching, so what they want is still wanted.
|
|
125
126
|
manager.planEncodersNow();
|
|
127
|
+
assert.ok(manager.encodeOrchestrator.demand.mapOn(KEY).length > 0, "still watching");
|
|
126
128
|
|
|
129
|
+
// Something SAYS they are gone — the browser released the session, or their
|
|
130
|
+
// connection closed. That is the only way out.
|
|
131
|
+
manager.viewers.leaves(session, "gone");
|
|
132
|
+
manager.planEncodersNow();
|
|
127
133
|
assert.equal(manager.encodeOrchestrator.demand.mapOn(KEY).length, 0);
|
|
128
134
|
});
|
|
129
135
|
|
package/test/output-spec.test.js
CHANGED
|
@@ -1,157 +1,157 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import test from "node:test";
|
|
3
|
-
import { AudioOutput, CutGrid, OutputSpec, VideoOutput } from "../services/output/OutputSpec.js";
|
|
4
|
-
|
|
5
|
-
const TORRENT = "torrent:11f0929918e2b5aa2e5b71ecdbe5c0f1a4bbf7d1";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The picture of a file, copied, cut at that file's own keyframes.
|
|
9
|
-
*
|
|
10
|
-
* @param {object} [over]
|
|
11
|
-
* @returns {OutputSpec}
|
|
12
|
-
*/
|
|
13
|
-
function copiedPicture(over = {}) {
|
|
14
|
-
return new OutputSpec({
|
|
15
|
-
sourceKey: TORRENT,
|
|
16
|
-
segmentFormatId: "fmp4",
|
|
17
|
-
grid: new CutGrid({ kind: "keyframe", fileIndex: 0 }),
|
|
18
|
-
video: new VideoOutput({ fileIndex: 0, encode: null }),
|
|
19
|
-
audio: null,
|
|
20
|
-
...over
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
test("two viewers who chose different soundtracks share one copied picture", () => {
|
|
25
|
-
// The measurement of 2026-09-03: both sessions described the same thing word
|
|
26
|
-
// for word and answered `segment-00000.mp4` with the same 4141899 bytes. The
|
|
27
|
-
// soundtrack they picked is not a property of a picture that carries no
|
|
28
|
-
// sound, so it cannot tell the two apart.
|
|
29
|
-
assert.equal(copiedPicture().toKey(), copiedPicture().toKey());
|
|
30
|
-
assert.equal(copiedPicture().carries, "video-only");
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("the viewer's viewport does not fork a picture that is copied", () => {
|
|
34
|
-
// A copy is the source's own size whatever box was asked for, so there is
|
|
35
|
-
// nowhere for a target to appear in the key.
|
|
36
|
-
const key = copiedPicture().toKey();
|
|
37
|
-
assert.ok(!key.includes("x"), `a copied picture states no box: ${key}`);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test("two heights of one picture are two outputs", () => {
|
|
41
|
-
const at720 = copiedPicture({
|
|
42
|
-
video: new VideoOutput({ fileIndex: 0, encode: { width: 1280, height: 720,
|
|
43
|
-
});
|
|
44
|
-
const at480 = copiedPicture({
|
|
45
|
-
video: new VideoOutput({ fileIndex: 0, encode: { width: 854, height: 480,
|
|
46
|
-
});
|
|
47
|
-
assert.notEqual(at720.toKey(), at480.toKey());
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test("a height
|
|
51
|
-
const forced = copiedPicture({
|
|
52
|
-
video: new VideoOutput({ fileIndex: 0, encode: { width: 1280, height: 720,
|
|
53
|
-
});
|
|
54
|
-
const chosen = copiedPicture({
|
|
55
|
-
video: new VideoOutput({ fileIndex: 0, encode: { width: 1280, height: 720,
|
|
56
|
-
});
|
|
57
|
-
assert.notEqual(forced.toKey(), chosen.toKey());
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test("a soundtrack is named by the file it lives in and the track inside it", () => {
|
|
61
|
-
// Not by the flat number the browser sends: that number spans the picture's
|
|
62
|
-
// own tracks and the files beside it, so it means different things for
|
|
63
|
-
// different pictures of one torrent.
|
|
64
|
-
const dub = new OutputSpec({
|
|
65
|
-
sourceKey: TORRENT,
|
|
66
|
-
segmentFormatId: "fmp4",
|
|
67
|
-
grid: new CutGrid({ kind: "keyframe", fileIndex: 0 }),
|
|
68
|
-
audio: new AudioOutput({ fileIndex: 7, trackIndex: 0, transcode: true })
|
|
69
|
-
});
|
|
70
|
-
assert.equal(dub.carries, "audio-only");
|
|
71
|
-
assert.ok(dub.toKey().includes("a=7/0/aac"), dub.toKey());
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test("one soundtrack cut for two different pictures is two outputs", () => {
|
|
75
|
-
// The grid of a soundtrack is the picture's, so a rendition made for episode
|
|
76
|
-
// one cannot stand in for one made for episode two even when the dub is the
|
|
77
|
-
// same file.
|
|
78
|
-
const forFirst = new OutputSpec({
|
|
79
|
-
sourceKey: TORRENT,
|
|
80
|
-
segmentFormatId: "fmp4",
|
|
81
|
-
grid: new CutGrid({ kind: "keyframe", fileIndex: 0 }),
|
|
82
|
-
audio: new AudioOutput({ fileIndex: 7, trackIndex: 0, transcode: true })
|
|
83
|
-
});
|
|
84
|
-
const forSecond = new OutputSpec({
|
|
85
|
-
sourceKey: TORRENT,
|
|
86
|
-
segmentFormatId: "fmp4",
|
|
87
|
-
grid: new CutGrid({ kind: "keyframe", fileIndex: 1 }),
|
|
88
|
-
audio: new AudioOutput({ fileIndex: 7, trackIndex: 0, transcode: true })
|
|
89
|
-
});
|
|
90
|
-
assert.notEqual(forFirst.toKey(), forSecond.toKey());
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
test("a copied soundtrack and a re-encoded one are two outputs", () => {
|
|
94
|
-
const copied = new OutputSpec({
|
|
95
|
-
sourceKey: TORRENT,
|
|
96
|
-
segmentFormatId: "fmp4",
|
|
97
|
-
grid: new CutGrid({ kind: "uniform", fileIndex: 0 }),
|
|
98
|
-
audio: new AudioOutput({ fileIndex: 0, trackIndex: 1, transcode: false })
|
|
99
|
-
});
|
|
100
|
-
const encoded = new OutputSpec({
|
|
101
|
-
sourceKey: TORRENT,
|
|
102
|
-
segmentFormatId: "fmp4",
|
|
103
|
-
grid: new CutGrid({ kind: "uniform", fileIndex: 0 }),
|
|
104
|
-
audio: new AudioOutput({ fileIndex: 0, trackIndex: 1, transcode: true })
|
|
105
|
-
});
|
|
106
|
-
assert.notEqual(copied.toKey(), encoded.toKey());
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
test("a browser without rendition groups gets an output carrying both tracks", () => {
|
|
110
|
-
const muxed = new OutputSpec({
|
|
111
|
-
sourceKey: TORRENT,
|
|
112
|
-
segmentFormatId: "mpegts",
|
|
113
|
-
grid: new CutGrid({ kind: "keyframe", fileIndex: 0 }),
|
|
114
|
-
video: new VideoOutput({ fileIndex: 0, encode: null }),
|
|
115
|
-
audio: new AudioOutput({ fileIndex: 0, trackIndex: 2, transcode: true })
|
|
116
|
-
});
|
|
117
|
-
assert.equal(muxed.carries, "muxed");
|
|
118
|
-
// And there the soundtrack DOES tell two of them apart, because the output
|
|
119
|
-
// really carries it.
|
|
120
|
-
const other = new OutputSpec({
|
|
121
|
-
sourceKey: TORRENT,
|
|
122
|
-
segmentFormatId: "mpegts",
|
|
123
|
-
grid: new CutGrid({ kind: "keyframe", fileIndex: 0 }),
|
|
124
|
-
video: new VideoOutput({ fileIndex: 0, encode: null }),
|
|
125
|
-
audio: new AudioOutput({ fileIndex: 0, trackIndex: 3, transcode: true })
|
|
126
|
-
});
|
|
127
|
-
assert.notEqual(muxed.toKey(), other.toKey());
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
test("the picture, its soundtrack and the two of them muxed are three outputs", () => {
|
|
131
|
-
const video = copiedPicture();
|
|
132
|
-
const audio = new OutputSpec({
|
|
133
|
-
sourceKey: TORRENT,
|
|
134
|
-
segmentFormatId: "fmp4",
|
|
135
|
-
grid: new CutGrid({ kind: "keyframe", fileIndex: 0 }),
|
|
136
|
-
audio: new AudioOutput({ fileIndex: 0, trackIndex: 0, transcode: true })
|
|
137
|
-
});
|
|
138
|
-
const muxed = copiedPicture({
|
|
139
|
-
audio: new AudioOutput({ fileIndex: 0, trackIndex: 0, transcode: true })
|
|
140
|
-
});
|
|
141
|
-
assert.equal(new Set([video.toKey(), audio.toKey(), muxed.toKey()]).size, 3);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
test("two containers of the same tracks are two outputs", () => {
|
|
145
|
-
assert.notEqual(copiedPicture().toKey(), copiedPicture({ segmentFormatId: "mpegts" }).toKey());
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
test("the same tracks of two different torrents are two outputs", () => {
|
|
149
|
-
assert.notEqual(copiedPicture().toKey(), copiedPicture({ sourceKey: "torrent:0000" }).toKey());
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
test("a picture cut at keyframes and the same picture cut on the even grid are two outputs", () => {
|
|
153
|
-
assert.notEqual(
|
|
154
|
-
copiedPicture().toKey(),
|
|
155
|
-
copiedPicture({ grid: new CutGrid({ kind: "uniform", fileIndex: 0 }) }).toKey()
|
|
156
|
-
);
|
|
157
|
-
});
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { AudioOutput, CutGrid, OutputSpec, VideoOutput } from "../services/output/OutputSpec.js";
|
|
4
|
+
|
|
5
|
+
const TORRENT = "torrent:11f0929918e2b5aa2e5b71ecdbe5c0f1a4bbf7d1";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The picture of a file, copied, cut at that file's own keyframes.
|
|
9
|
+
*
|
|
10
|
+
* @param {object} [over]
|
|
11
|
+
* @returns {OutputSpec}
|
|
12
|
+
*/
|
|
13
|
+
function copiedPicture(over = {}) {
|
|
14
|
+
return new OutputSpec({
|
|
15
|
+
sourceKey: TORRENT,
|
|
16
|
+
segmentFormatId: "fmp4",
|
|
17
|
+
grid: new CutGrid({ kind: "keyframe", fileIndex: 0 }),
|
|
18
|
+
video: new VideoOutput({ fileIndex: 0, encode: null }),
|
|
19
|
+
audio: null,
|
|
20
|
+
...over
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
test("two viewers who chose different soundtracks share one copied picture", () => {
|
|
25
|
+
// The measurement of 2026-09-03: both sessions described the same thing word
|
|
26
|
+
// for word and answered `segment-00000.mp4` with the same 4141899 bytes. The
|
|
27
|
+
// soundtrack they picked is not a property of a picture that carries no
|
|
28
|
+
// sound, so it cannot tell the two apart.
|
|
29
|
+
assert.equal(copiedPicture().toKey(), copiedPicture().toKey());
|
|
30
|
+
assert.equal(copiedPicture().carries, "video-only");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("the viewer's viewport does not fork a picture that is copied", () => {
|
|
34
|
+
// A copy is the source's own size whatever box was asked for, so there is
|
|
35
|
+
// nowhere for a target to appear in the key.
|
|
36
|
+
const key = copiedPicture().toKey();
|
|
37
|
+
assert.ok(!key.includes("x"), `a copied picture states no box: ${key}`);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("two heights of one picture are two outputs", () => {
|
|
41
|
+
const at720 = copiedPicture({
|
|
42
|
+
video: new VideoOutput({ fileIndex: 0, encode: { width: 1280, height: 720, exactSize: true } })
|
|
43
|
+
});
|
|
44
|
+
const at480 = copiedPicture({
|
|
45
|
+
video: new VideoOutput({ fileIndex: 0, encode: { width: 854, height: 480, exactSize: true } })
|
|
46
|
+
});
|
|
47
|
+
assert.notEqual(at720.toKey(), at480.toKey());
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("a height produced exactly is not the same output as one the budget may move", () => {
|
|
51
|
+
const forced = copiedPicture({
|
|
52
|
+
video: new VideoOutput({ fileIndex: 0, encode: { width: 1280, height: 720, exactSize: true } })
|
|
53
|
+
});
|
|
54
|
+
const chosen = copiedPicture({
|
|
55
|
+
video: new VideoOutput({ fileIndex: 0, encode: { width: 1280, height: 720, exactSize: false } })
|
|
56
|
+
});
|
|
57
|
+
assert.notEqual(forced.toKey(), chosen.toKey());
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("a soundtrack is named by the file it lives in and the track inside it", () => {
|
|
61
|
+
// Not by the flat number the browser sends: that number spans the picture's
|
|
62
|
+
// own tracks and the files beside it, so it means different things for
|
|
63
|
+
// different pictures of one torrent.
|
|
64
|
+
const dub = new OutputSpec({
|
|
65
|
+
sourceKey: TORRENT,
|
|
66
|
+
segmentFormatId: "fmp4",
|
|
67
|
+
grid: new CutGrid({ kind: "keyframe", fileIndex: 0 }),
|
|
68
|
+
audio: new AudioOutput({ fileIndex: 7, trackIndex: 0, transcode: true })
|
|
69
|
+
});
|
|
70
|
+
assert.equal(dub.carries, "audio-only");
|
|
71
|
+
assert.ok(dub.toKey().includes("a=7/0/aac"), dub.toKey());
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("one soundtrack cut for two different pictures is two outputs", () => {
|
|
75
|
+
// The grid of a soundtrack is the picture's, so a rendition made for episode
|
|
76
|
+
// one cannot stand in for one made for episode two even when the dub is the
|
|
77
|
+
// same file.
|
|
78
|
+
const forFirst = new OutputSpec({
|
|
79
|
+
sourceKey: TORRENT,
|
|
80
|
+
segmentFormatId: "fmp4",
|
|
81
|
+
grid: new CutGrid({ kind: "keyframe", fileIndex: 0 }),
|
|
82
|
+
audio: new AudioOutput({ fileIndex: 7, trackIndex: 0, transcode: true })
|
|
83
|
+
});
|
|
84
|
+
const forSecond = new OutputSpec({
|
|
85
|
+
sourceKey: TORRENT,
|
|
86
|
+
segmentFormatId: "fmp4",
|
|
87
|
+
grid: new CutGrid({ kind: "keyframe", fileIndex: 1 }),
|
|
88
|
+
audio: new AudioOutput({ fileIndex: 7, trackIndex: 0, transcode: true })
|
|
89
|
+
});
|
|
90
|
+
assert.notEqual(forFirst.toKey(), forSecond.toKey());
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("a copied soundtrack and a re-encoded one are two outputs", () => {
|
|
94
|
+
const copied = new OutputSpec({
|
|
95
|
+
sourceKey: TORRENT,
|
|
96
|
+
segmentFormatId: "fmp4",
|
|
97
|
+
grid: new CutGrid({ kind: "uniform", fileIndex: 0 }),
|
|
98
|
+
audio: new AudioOutput({ fileIndex: 0, trackIndex: 1, transcode: false })
|
|
99
|
+
});
|
|
100
|
+
const encoded = new OutputSpec({
|
|
101
|
+
sourceKey: TORRENT,
|
|
102
|
+
segmentFormatId: "fmp4",
|
|
103
|
+
grid: new CutGrid({ kind: "uniform", fileIndex: 0 }),
|
|
104
|
+
audio: new AudioOutput({ fileIndex: 0, trackIndex: 1, transcode: true })
|
|
105
|
+
});
|
|
106
|
+
assert.notEqual(copied.toKey(), encoded.toKey());
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("a browser without rendition groups gets an output carrying both tracks", () => {
|
|
110
|
+
const muxed = new OutputSpec({
|
|
111
|
+
sourceKey: TORRENT,
|
|
112
|
+
segmentFormatId: "mpegts",
|
|
113
|
+
grid: new CutGrid({ kind: "keyframe", fileIndex: 0 }),
|
|
114
|
+
video: new VideoOutput({ fileIndex: 0, encode: null }),
|
|
115
|
+
audio: new AudioOutput({ fileIndex: 0, trackIndex: 2, transcode: true })
|
|
116
|
+
});
|
|
117
|
+
assert.equal(muxed.carries, "muxed");
|
|
118
|
+
// And there the soundtrack DOES tell two of them apart, because the output
|
|
119
|
+
// really carries it.
|
|
120
|
+
const other = new OutputSpec({
|
|
121
|
+
sourceKey: TORRENT,
|
|
122
|
+
segmentFormatId: "mpegts",
|
|
123
|
+
grid: new CutGrid({ kind: "keyframe", fileIndex: 0 }),
|
|
124
|
+
video: new VideoOutput({ fileIndex: 0, encode: null }),
|
|
125
|
+
audio: new AudioOutput({ fileIndex: 0, trackIndex: 3, transcode: true })
|
|
126
|
+
});
|
|
127
|
+
assert.notEqual(muxed.toKey(), other.toKey());
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("the picture, its soundtrack and the two of them muxed are three outputs", () => {
|
|
131
|
+
const video = copiedPicture();
|
|
132
|
+
const audio = new OutputSpec({
|
|
133
|
+
sourceKey: TORRENT,
|
|
134
|
+
segmentFormatId: "fmp4",
|
|
135
|
+
grid: new CutGrid({ kind: "keyframe", fileIndex: 0 }),
|
|
136
|
+
audio: new AudioOutput({ fileIndex: 0, trackIndex: 0, transcode: true })
|
|
137
|
+
});
|
|
138
|
+
const muxed = copiedPicture({
|
|
139
|
+
audio: new AudioOutput({ fileIndex: 0, trackIndex: 0, transcode: true })
|
|
140
|
+
});
|
|
141
|
+
assert.equal(new Set([video.toKey(), audio.toKey(), muxed.toKey()]).size, 3);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("two containers of the same tracks are two outputs", () => {
|
|
145
|
+
assert.notEqual(copiedPicture().toKey(), copiedPicture({ segmentFormatId: "mpegts" }).toKey());
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("the same tracks of two different torrents are two outputs", () => {
|
|
149
|
+
assert.notEqual(copiedPicture().toKey(), copiedPicture({ sourceKey: "torrent:0000" }).toKey());
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("a picture cut at keyframes and the same picture cut on the even grid are two outputs", () => {
|
|
153
|
+
assert.notEqual(
|
|
154
|
+
copiedPicture().toKey(),
|
|
155
|
+
copiedPicture({ grid: new CutGrid({ kind: "uniform", fileIndex: 0 }) }).toKey()
|
|
156
|
+
);
|
|
157
|
+
});
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The placement must not move while the facts do not.
|
|
3
|
+
*
|
|
4
|
+
* This is the check the field failure of 2026-09-13 had no equivalent of. Two
|
|
5
|
+
* browsers on one torrent: 77 encoder starts and 141 stops in six minutes
|
|
6
|
+
* against ONE normal end, both viewers frozen — one at 170.8 s with
|
|
7
|
+
* `bufferedAhead=0.1s`, the other at 1673.6 s with an empty buffer — while the
|
|
8
|
+
* swarm delivered 3 MB/s from 129 peers. Nothing was produced because no run
|
|
9
|
+
* lived long enough to close a piece.
|
|
10
|
+
*
|
|
11
|
+
* The plan itself was not at fault and was measured not to be: given a settled
|
|
12
|
+
* map it returns the same arrangement however often it is asked. What moved was
|
|
13
|
+
* its INPUT. A viewer's position had two writers — a seek wrote one field, a
|
|
14
|
+
* segment request wrote another, and the reading preferred whichever was set —
|
|
15
|
+
* so the priority map jumped backwards and forwards by one or two segments
|
|
16
|
+
* several times a second:
|
|
17
|
+
*
|
|
18
|
+
* 19:58:00.283 p100:#9..#10 p100:#160..#162
|
|
19
|
+
* 19:58:00.496 p100:#18..#20 p100:#161..#163
|
|
20
|
+
* 19:58:00.574 p100:#16..#18 p100:#161..#163
|
|
21
|
+
* 19:58:00.696 p100:#16..#18 p100:#160..#162
|
|
22
|
+
* 19:58:01.231 p100:#18..#20 p100:#160..#162
|
|
23
|
+
*
|
|
24
|
+
* So the property to hold is about the whole chain and not about one function:
|
|
25
|
+
* what a viewer states must produce a map that only moves the way film moves,
|
|
26
|
+
* and a plan asked repeatedly against unchanged facts must answer identically.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import test from "node:test";
|
|
30
|
+
import assert from "node:assert/strict";
|
|
31
|
+
import { CoverageMap } from "../services/encode/CoverageMap.js";
|
|
32
|
+
import { planEncoders } from "../services/encode/EncodePlan.js";
|
|
33
|
+
import { contentionPenalty, penaltiesFrom } from "../services/encode/contention.js";
|
|
34
|
+
import { mapForViewer, mergeMaps, runsOf } from "../services/priority/PriorityMap.js";
|
|
35
|
+
import { Viewer } from "../services/viewer/Viewer.js";
|
|
36
|
+
|
|
37
|
+
// Measured on the addon host 2026-09-03: 7.12x alone, 4.18x with a second.
|
|
38
|
+
const PENALTIES = penaltiesFrom(7.12, [{ others: 1, speed: 4.18 }]);
|
|
39
|
+
const penaltyFor = (others) => contentionPenalty(others, PENALTIES).penalty;
|
|
40
|
+
|
|
41
|
+
const HOST = {
|
|
42
|
+
contentionPenaltyFor: penaltyFor,
|
|
43
|
+
speedX: 2,
|
|
44
|
+
segmentSeconds: 8,
|
|
45
|
+
killCostSec: 0.5,
|
|
46
|
+
firstByteWaitSec: 1,
|
|
47
|
+
refetchSecPerFilmSecond: 0.25
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const DURATION = 2896;
|
|
51
|
+
const ALLOWANCE = 30;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The map two viewers produce, exactly as `PriorityOrchestrator` builds it.
|
|
55
|
+
*
|
|
56
|
+
* @param {Viewer[]} viewers
|
|
57
|
+
* @param {number} now
|
|
58
|
+
* @returns {{ from: number, to: number, priority: number }[]}
|
|
59
|
+
*/
|
|
60
|
+
function zonesFor(viewers, now) {
|
|
61
|
+
const merged = mergeMaps(
|
|
62
|
+
viewers.map((viewer) =>
|
|
63
|
+
mapForViewer({
|
|
64
|
+
atSeconds: viewer.positionSeconds(now) ?? 0,
|
|
65
|
+
durationSeconds: DURATION,
|
|
66
|
+
allowanceSeconds: ALLOWANCE,
|
|
67
|
+
playing: viewer.wantsFilmNow()
|
|
68
|
+
})
|
|
69
|
+
)
|
|
70
|
+
);
|
|
71
|
+
return runsOf(merged).map((zone) => ({
|
|
72
|
+
from: Math.floor(zone.from / HOST.segmentSeconds),
|
|
73
|
+
to: Math.floor((zone.to - 1) / HOST.segmentSeconds),
|
|
74
|
+
priority: zone.priority
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
test("a viewer's position only ever moves the way film moves", () => {
|
|
79
|
+
const viewer = new Viewer("one", 1_000_000);
|
|
80
|
+
// Stated the way a page states it: where they are, what they hold, and that
|
|
81
|
+
// the picture is advancing. The cushion is what bounds the extrapolation, so
|
|
82
|
+
// a viewer who has stated none cannot move at all.
|
|
83
|
+
viewer.report(
|
|
84
|
+
{ bufferedAheadSec: 120, positionSeconds: 170.8, playing: true, waiting: false },
|
|
85
|
+
1_000_000
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
let previous = viewer.positionSeconds(1_000_000);
|
|
89
|
+
for (let tick = 1; tick <= 200; tick += 1) {
|
|
90
|
+
const now = 1_000_000 + tick * 50;
|
|
91
|
+
const seconds = viewer.positionSeconds(now);
|
|
92
|
+
assert.ok(seconds >= previous, `position went backwards at tick ${tick}: ${seconds} < ${previous}`);
|
|
93
|
+
previous = seconds;
|
|
94
|
+
}
|
|
95
|
+
// Fifty ticks of 50 ms is ten seconds of wall clock, and a playing viewer
|
|
96
|
+
// covers ten seconds of film in it.
|
|
97
|
+
assert.ok(Math.abs(previous - (170.8 + 10)) < 0.001, `expected 180.8s, got ${previous}`);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("a segment request does not move a viewer, and a stopped picture does not either", () => {
|
|
101
|
+
const viewer = new Viewer("one", 1_000_000);
|
|
102
|
+
viewer.report(
|
|
103
|
+
{ bufferedAheadSec: 120, positionSeconds: 170.8, playing: false, waiting: false },
|
|
104
|
+
1_000_000
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
// Whatever else happens, only a statement from the viewer moves them. There
|
|
108
|
+
// is no method here a request could call: `seen` is presence and nothing more.
|
|
109
|
+
viewer.seen(1_000_500);
|
|
110
|
+
viewer.seen(1_001_000);
|
|
111
|
+
assert.equal(viewer.positionSeconds(1_010_000), 170.8);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("the priority map of two viewers is the same whenever it is asked", () => {
|
|
115
|
+
const at = 1_000_000;
|
|
116
|
+
const one = new Viewer("one", at);
|
|
117
|
+
one.report({ bufferedAheadSec: 120, positionSeconds: 170.8, playing: false, waiting: false }, at);
|
|
118
|
+
const two = new Viewer("two", at);
|
|
119
|
+
two.report({ bufferedAheadSec: 120, positionSeconds: 1673.6, playing: false, waiting: false }, at);
|
|
120
|
+
|
|
121
|
+
const first = JSON.stringify(zonesFor([one, two], at));
|
|
122
|
+
for (let tick = 1; tick <= 40; tick += 1) {
|
|
123
|
+
// Nothing changes: both pictures are stopped, so no time passes for them.
|
|
124
|
+
assert.equal(JSON.stringify(zonesFor([one, two], at + tick * 100)), first, `map moved at tick ${tick}`);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("two viewers far apart settle on one arrangement and stay on it", () => {
|
|
129
|
+
const at = 1_000_000;
|
|
130
|
+
const one = new Viewer("one", at);
|
|
131
|
+
one.report({ bufferedAheadSec: 120, positionSeconds: 170.8, playing: false, waiting: false }, at);
|
|
132
|
+
const two = new Viewer("two", at);
|
|
133
|
+
two.report({ bufferedAheadSec: 120, positionSeconds: 1673.6, playing: false, waiting: false }, at);
|
|
134
|
+
|
|
135
|
+
const coverage = new CoverageMap({ segmentCount: 362 });
|
|
136
|
+
/** @type {{ from: number, to: number, head: number, speedX: number, startedAt: number }[]} */
|
|
137
|
+
let live = [];
|
|
138
|
+
let started = 0;
|
|
139
|
+
let stopped = 0;
|
|
140
|
+
|
|
141
|
+
for (let tick = 0; tick < 60; tick += 1) {
|
|
142
|
+
const now = at + tick * 100;
|
|
143
|
+
const actions = planEncoders({
|
|
144
|
+
coverage,
|
|
145
|
+
windows: zonesFor([one, two], now),
|
|
146
|
+
runs: live,
|
|
147
|
+
maxRuns: 2,
|
|
148
|
+
now,
|
|
149
|
+
...HOST
|
|
150
|
+
});
|
|
151
|
+
for (const action of actions) {
|
|
152
|
+
if (action.type === "start") {
|
|
153
|
+
live.push({ from: action.from, to: action.to, head: action.from, speedX: 2, startedAt: now });
|
|
154
|
+
started += 1;
|
|
155
|
+
} else if (action.type === "stop") {
|
|
156
|
+
live = live.filter((run) => run !== action.run);
|
|
157
|
+
stopped += 1;
|
|
158
|
+
} else if (action.type === "move") {
|
|
159
|
+
live = live.filter((run) => run !== action.run);
|
|
160
|
+
live.push({ from: action.from, to: action.to, head: action.from, speedX: 2, startedAt: now });
|
|
161
|
+
started += 1;
|
|
162
|
+
stopped += 1;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// One encoder per viewer, placed once. Anything above this is the plan
|
|
168
|
+
// arguing with itself: in the field the same six minutes cost 77 and 141.
|
|
169
|
+
assert.equal(started, 2, `expected two starts, got ${started}`);
|
|
170
|
+
assert.equal(stopped, 0, `expected no stops, got ${stopped}`);
|
|
171
|
+
assert.equal(live.length, 2);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("an encoder keeping ahead of a playing viewer is left alone", () => {
|
|
175
|
+
const at = 1_000_000;
|
|
176
|
+
const one = new Viewer("one", at);
|
|
177
|
+
one.report({ bufferedAheadSec: 120, positionSeconds: 0, playing: true, waiting: false }, at);
|
|
178
|
+
|
|
179
|
+
const coverage = new CoverageMap({ segmentCount: 362 });
|
|
180
|
+
const ready = new Set();
|
|
181
|
+
/** @type {{ from: number, to: number, head: number, speedX: number, startedAt: number, made: number }[]} */
|
|
182
|
+
let live = [];
|
|
183
|
+
let moves = 0;
|
|
184
|
+
let starts = 0;
|
|
185
|
+
|
|
186
|
+
for (let tick = 0; tick < 120; tick += 1) {
|
|
187
|
+
const now = at + tick * 1000;
|
|
188
|
+
const actions = planEncoders({
|
|
189
|
+
coverage,
|
|
190
|
+
windows: zonesFor([one], now),
|
|
191
|
+
runs: live,
|
|
192
|
+
maxRuns: 1,
|
|
193
|
+
now,
|
|
194
|
+
...HOST
|
|
195
|
+
});
|
|
196
|
+
for (const action of actions) {
|
|
197
|
+
if (action.type === "start") {
|
|
198
|
+
live.push({ from: action.from, to: action.to, head: action.from, speedX: 2, startedAt: now, made: 0 });
|
|
199
|
+
starts += 1;
|
|
200
|
+
} else if (action.type === "move") {
|
|
201
|
+
live = live.filter((run) => run !== action.run);
|
|
202
|
+
live.push({ from: action.from, to: action.to, head: action.from, speedX: 2, startedAt: now, made: 0 });
|
|
203
|
+
moves += 1;
|
|
204
|
+
} else if (action.type === "stop") {
|
|
205
|
+
live = live.filter((run) => run !== action.run);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
// The encoder works: at 2x, one 8-second piece every four seconds of clock.
|
|
209
|
+
for (const run of live) {
|
|
210
|
+
run.made += 1000 * 2;
|
|
211
|
+
while (run.made >= HOST.segmentSeconds * 1000 && run.head <= run.to) {
|
|
212
|
+
run.made -= HOST.segmentSeconds * 1000;
|
|
213
|
+
ready.add(run.head);
|
|
214
|
+
run.head += 1;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
coverage.setReady(ready);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// One encoder, placed once, running twice as fast as the viewer plays: it
|
|
221
|
+
// stays in front of them for the whole two minutes, so there is nothing to
|
|
222
|
+
// decide again.
|
|
223
|
+
assert.equal(starts, 1, `expected one start, got ${starts}`);
|
|
224
|
+
assert.equal(moves, 0, `expected no moves, got ${moves}`);
|
|
225
|
+
});
|