@torrent-tv/proxy 2.74.1 → 2.76.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.
Files changed (124) hide show
  1. package/CHANGELOG.md +1504 -1453
  2. package/CLAUDE.md +19 -6
  3. package/biome.json +182 -1
  4. package/docs/container-architecture.md +27 -6
  5. package/docs/encode-run-state.md +1 -1
  6. package/knip.json +14 -0
  7. package/package.json +1 -1
  8. package/routes/api/sources/warm/post.js +1 -1
  9. package/routes/api/transcode-sessions/post.js +185 -185
  10. package/routes/api/transcode-sessions/progress/get.js +5 -1
  11. package/routes/transcode/audio-file/get.js +11 -1
  12. package/routes/transcode/audio-warm/get.js +11 -1
  13. package/routes/transcode/session-file/get.js +1 -1
  14. package/routes/transcode/variant-file/get.js +10 -1
  15. package/scripts/render-run-graph.js +2 -2
  16. package/server.js +25 -0
  17. package/services/audio-inventory.js +9 -214
  18. package/services/container/AviContainer.js +266 -81
  19. package/services/container/Container.js +281 -80
  20. package/services/container/ContainerFactory.js +67 -0
  21. package/services/container/MatroskaContainer.js +327 -8
  22. package/services/container/Mp4Container.js +373 -27
  23. package/services/container/SubtitleFileContainer.js +0 -1
  24. package/services/controllers/SubtitleController.js +128 -128
  25. package/services/demand/index.js +7 -10
  26. package/services/download/registry.js +0 -14
  27. package/services/encode/CoverageMap.js +281 -0
  28. package/services/encode/EncodePlan.js +255 -0
  29. package/services/encode/EncodeRun.js +587 -0
  30. package/services/encode/Encoder.js +84 -0
  31. package/services/encode/NvencEncoder.js +45 -0
  32. package/services/encode/QsvEncoder.js +47 -0
  33. package/services/encode/SegmentDemand.js +0 -0
  34. package/services/encode/SegmentStore.js +529 -0
  35. package/services/encode/SoftwareEncoder.js +111 -0
  36. package/services/encode/V4l2m2mEncoder.js +53 -0
  37. package/services/encode/VaapiEncoder.js +53 -0
  38. package/services/encode/args.js +200 -0
  39. package/services/{encode-exit.js → encode/encode-exit.js} +17 -0
  40. package/services/encode/index.js +9 -0
  41. package/services/encode/run-command.js +647 -0
  42. package/services/hls-session-manager.js +11073 -10711
  43. package/services/hwaccel.js +1688 -1992
  44. package/services/orchestrators/EncodeOrchestrator.js +359 -0
  45. package/services/output/LiveOutputs.js +213 -0
  46. package/services/output/Output.js +94 -0
  47. package/services/output/OutputSpec.js +195 -0
  48. package/services/output/Timeline.js +220 -0
  49. package/services/output/index.js +1 -0
  50. package/services/output/ladder.js +26 -0
  51. package/services/playback-planner.js +806 -747
  52. package/services/produced-index.js +222 -300
  53. package/services/source/SourceFile.js +346 -0
  54. package/services/{sidecar-files.js → torrent/files.js} +107 -11
  55. package/services/torrent/naming.js +619 -0
  56. package/services/torrent-worker/client.js +10 -0
  57. package/services/torrent-worker/container-tracks.js +71 -43
  58. package/services/torrent-worker/pool-adapter.js +370 -333
  59. package/services/torrent-worker/protocol.js +7 -0
  60. package/services/torrent-worker/subtitle-cues.js +549 -549
  61. package/services/torrent-worker/worker.js +18 -0
  62. package/services/tracks/AudioTrack.js +131 -40
  63. package/services/tracks/TextSubtitleTrack.js +287 -287
  64. package/services/tracks/index.js +15 -14
  65. package/services/viewer/Viewer.js +145 -0
  66. package/services/viewer/Viewers.js +124 -0
  67. package/test/audio-inventory.test.js +176 -177
  68. package/test/auto-quality-step.test.js +508 -506
  69. package/test/behind-head-repair.test.js +17 -7
  70. package/test/coverage-map.test.js +153 -0
  71. package/test/cut-times-timeline.test.js +6 -5
  72. package/test/cuts-follow-published-grid.test.js +4 -4
  73. package/test/decode-cost.test.js +31 -12
  74. package/test/encode-exit.test.js +1 -1
  75. package/test/encode-orchestrator.test.js +196 -0
  76. package/test/encode-plan.test.js +245 -0
  77. package/test/encode-run-state.test.js +2 -2
  78. package/test/encode-run.test.js +168 -0
  79. package/test/encoder-kinds.test.js +122 -0
  80. package/test/held-request-width.test.js +9 -3
  81. package/test/helpers/encode-run.js +128 -0
  82. package/test/keyframe-index-accuracy.test.js +19 -12
  83. package/test/keyframes-belong-to-the-file.test.js +132 -0
  84. package/test/matroska-cues-track.test.js +192 -192
  85. package/test/mp4-composition-times.test.js +0 -0
  86. package/test/orchestrator-wired.test.js +164 -0
  87. package/test/output-shape.test.js +68 -0
  88. package/test/output-spec.test.js +157 -0
  89. package/test/produced-copy-choice.test.js +58 -92
  90. package/test/produced-index.test.js +142 -188
  91. package/test/quality-variants.test.js +1079 -1075
  92. package/test/run-graph-drift.test.js +1 -1
  93. package/test/run-intervals.test.js +329 -0
  94. package/test/run-position-follows-published-grid.test.js +4 -4
  95. package/test/seek-landing.test.js +8 -8
  96. package/test/seek-target-not-superseded.test.js +21 -9
  97. package/test/segment-demand.test.js +82 -0
  98. package/test/segment-serve-wiring.test.js +47 -52
  99. package/test/segment-store.test.js +187 -0
  100. package/test/segments-are-shared.test.js +175 -0
  101. package/test/sidecar-naming.test.js +142 -0
  102. package/test/source-file.test.js +133 -0
  103. package/test/stale-request-after-seek.test.js +18 -12
  104. package/test/subtitle-language.test.js +252 -252
  105. package/test/timeline.test.js +95 -0
  106. package/test/{sidecar-files.test.js → torrent-files.test.js} +44 -1
  107. package/test/torrent-naming.test.js +255 -0
  108. package/test/tracks-begin-together.test.js +44 -32
  109. package/test/two-viewers-one-picture.test.js +347 -0
  110. package/test/video-facts.test.js +102 -0
  111. package/test/viewer-outputs.test.js +273 -0
  112. package/test/viewer.test.js +91 -0
  113. package/utils/perf.js +1 -63
  114. package/services/container/index.js +0 -6
  115. package/services/container-index/avi.js +0 -167
  116. package/services/container-index/index.js +0 -118
  117. package/services/container-index/matroska.js +0 -336
  118. package/services/container-index/mp4.js +0 -358
  119. package/services/controllers/index.js +0 -2
  120. package/services/download/index.js +0 -8
  121. package/services/orchestrators/index.js +0 -2
  122. /package/services/{container-index → container}/ebml-reader.js +0 -0
  123. /package/services/{encode-run-state.js → encode/encode-run-state.js} +0 -0
  124. /package/services/{language-detect.js → tracks/language-detect.js} +0 -0
@@ -1,192 +1,192 @@
1
- /**
2
- * @file The container's table names a track, and only the picture's entries are
3
- * cut points.
4
- *
5
- * Measured 2026-08-18 on the two files the field sessions were recorded from:
6
- * `Minions.and.Monsters.1080p.mkv` carries 2778 cue entries for its video track
7
- * — one every 2.002 s — and 4669 more across four subtitle tracks;
8
- * `Moana.2 … MegaPeer.mkv` carries 1055 for video and 5007 across five subtitle
9
- * tracks. Read without the track, both sets went into the cut list together,
10
- * and ffmpeg — which can only cut a copied picture at a real keyframe at or
11
- * after the time it is asked for — moved every such cut forward to the next
12
- * keyframe. That is the whole of the disagreement the field reported: 2.002 s
13
- * on the first file, a median of 6.3 s and a worst case of 21 s on the second,
14
- * and never once negative.
15
- */
16
-
17
- import test from "node:test";
18
- import assert from "node:assert/strict";
19
- import { readMatroskaKeyframeTimes } from "../services/container-index/matroska.js";
20
-
21
- const ID_EBML = 0x1a45dfa3;
22
- const ID_SEGMENT = 0x18538067;
23
- const ID_SEEK_HEAD = 0x114d9b74;
24
- const ID_SEEK = 0x4dbb;
25
- const ID_SEEK_ID = 0x53ab;
26
- const ID_SEEK_POSITION = 0x53ac;
27
- const ID_INFO = 0x1549a966;
28
- const ID_TIMESTAMP_SCALE = 0x2ad7b1;
29
- const ID_TRACKS = 0x1654ae6b;
30
- const ID_TRACK_ENTRY = 0xae;
31
- const ID_TRACK_NUMBER = 0xd7;
32
- const ID_TRACK_TYPE = 0x83;
33
- const ID_CUES = 0x1c53bb6b;
34
- const ID_CUE_POINT = 0xbb;
35
- const ID_CUE_TIME = 0xb3;
36
- const ID_CUE_TRACK_POSITIONS = 0xb7;
37
- const ID_CUE_TRACK = 0xf7;
38
- const ID_CUE_CLUSTER_POSITION = 0xf1;
39
-
40
- /** An element id, as the bytes the specification gives it. */
41
- function idBytes(id) {
42
- const bytes = [];
43
- let rest = id;
44
- while (rest > 0) {
45
- bytes.unshift(rest & 0xff);
46
- rest = Math.floor(rest / 256);
47
- }
48
- return Buffer.from(bytes);
49
- }
50
-
51
- /** A size, as a four-byte EBML variable-length integer. */
52
- function sizeBytes(size) {
53
- const buffer = Buffer.alloc(4);
54
- buffer.writeUInt32BE(size, 0);
55
- buffer[0] |= 0x10;
56
- return buffer;
57
- }
58
-
59
- function element(id, payload) {
60
- return Buffer.concat([idBytes(id), sizeBytes(payload.length), payload]);
61
- }
62
-
63
- /** An unsigned value, in as few bytes as it needs. */
64
- function uintElement(id, value) {
65
- const bytes = [];
66
- let rest = value;
67
- do {
68
- bytes.unshift(rest & 0xff);
69
- rest = Math.floor(rest / 256);
70
- } while (rest > 0);
71
- return element(id, Buffer.from(bytes));
72
- }
73
-
74
- function cuePoint(timeMs, tracks) {
75
- return element(ID_CUE_POINT, Buffer.concat([
76
- uintElement(ID_CUE_TIME, timeMs),
77
- ...tracks.map((track) => element(ID_CUE_TRACK_POSITIONS, Buffer.concat([
78
- uintElement(ID_CUE_TRACK, track),
79
- uintElement(ID_CUE_CLUSTER_POSITION, 4096)
80
- ])))
81
- ]));
82
- }
83
-
84
- /**
85
- * A file with the shape the field files have: one picture, one set of subtitles,
86
- * and a table that indexes both.
87
- *
88
- * @param {{ withTracks?: boolean }} [options]
89
- * @returns {Buffer}
90
- */
91
- function buildFile({ withTracks = true, cueTrack = null } = {}) {
92
- const info = element(ID_INFO, uintElement(ID_TIMESTAMP_SCALE, 1_000_000));
93
- const tracks = element(ID_TRACKS, Buffer.concat([
94
- element(ID_TRACK_ENTRY, Buffer.concat([
95
- uintElement(ID_TRACK_NUMBER, 1),
96
- uintElement(ID_TRACK_TYPE, 1) // video
97
- ])),
98
- element(ID_TRACK_ENTRY, Buffer.concat([
99
- uintElement(ID_TRACK_NUMBER, 2),
100
- uintElement(ID_TRACK_TYPE, 17) // subtitles
101
- ]))
102
- ]));
103
- const forPicture = cueTrack ?? 1;
104
- const forSubtitles = cueTrack ?? 2;
105
- const cues = element(ID_CUES, Buffer.concat([
106
- cuePoint(0, [forPicture]),
107
- cuePoint(1070, [forSubtitles]),
108
- cuePoint(2002, [forPicture]),
109
- cuePoint(3141, [forSubtitles]),
110
- cuePoint(4004, [forPicture])
111
- ]));
112
-
113
- const seekEntry = (targetId, position) => element(ID_SEEK, Buffer.concat([
114
- element(ID_SEEK_ID, idBytes(targetId)),
115
- element(ID_SEEK_POSITION, (() => {
116
- const buffer = Buffer.alloc(4);
117
- buffer.writeUInt32BE(position, 0);
118
- return buffer;
119
- })())
120
- ]));
121
- // Positions are relative to the Segment's payload, so the SeekHead has to be
122
- // measured before they can be stated. Its own length does not change when the
123
- // placeholders become real values: every size and position here is written at
124
- // a fixed width.
125
- const draft = element(ID_SEEK_HEAD, Buffer.concat([
126
- seekEntry(ID_INFO, 0),
127
- ...(withTracks ? [seekEntry(ID_TRACKS, 0)] : []),
128
- seekEntry(ID_CUES, 0)
129
- ]));
130
- const infoAt = draft.length;
131
- const tracksAt = infoAt + info.length;
132
- const cuesAt = withTracks ? tracksAt + tracks.length : infoAt + info.length;
133
- const seekHead = element(ID_SEEK_HEAD, Buffer.concat([
134
- seekEntry(ID_INFO, infoAt),
135
- ...(withTracks ? [seekEntry(ID_TRACKS, tracksAt)] : []),
136
- seekEntry(ID_CUES, cuesAt)
137
- ]));
138
-
139
- const segmentPayload = Buffer.concat(
140
- withTracks ? [seekHead, info, tracks, cues] : [seekHead, info, cues]
141
- );
142
- return Buffer.concat([
143
- element(ID_EBML, Buffer.from([0x42, 0x86, 0x81, 0x01])),
144
- element(ID_SEGMENT, segmentPayload)
145
- ]);
146
- }
147
-
148
- function readerOver(file) {
149
- return async (start, end) => {
150
- const last = Math.min(end, file.length - 1);
151
- return start > last ? null : file.subarray(start, last + 1);
152
- };
153
- }
154
-
155
- test("only the picture's entries become cut times", async () => {
156
- const file = buildFile();
157
-
158
- const times = await readMatroskaKeyframeTimes(readerOver(file), file.length);
159
-
160
- assert.deepEqual(
161
- times.map((time) => Number(time.toFixed(3))),
162
- [0, 2.002, 4.004],
163
- "the subtitle entries at 1.070 and 3.141 are not keyframes, and a cut asked for there lands late"
164
- );
165
- });
166
-
167
- test("a table whose entries name no known track is still used", async () => {
168
- // The cue points reference track 9, which Tracks never declares. Filtering
169
- // leaves nothing — and returning nothing would put an even grid on a copied
170
- // picture, the failure this reader exists to prevent.
171
- const file = buildFile({ cueTrack: 9 });
172
-
173
- const times = await readMatroskaKeyframeTimes(readerOver(file), file.length);
174
-
175
- assert.deepEqual(
176
- times.map((time) => Number(time.toFixed(3))),
177
- [0, 1.07, 2.002, 3.141, 4.004],
178
- "an unrecognised table beats no table at all"
179
- );
180
- });
181
-
182
- test("a file whose tracks cannot be read keeps every entry", async () => {
183
- const file = buildFile({ withTracks: false });
184
-
185
- const times = await readMatroskaKeyframeTimes(readerOver(file), file.length);
186
-
187
- assert.deepEqual(
188
- times.map((time) => Number(time.toFixed(3))),
189
- [0, 1.07, 2.002, 3.141, 4.004],
190
- "with nothing to tell the tracks apart, the old behaviour is the only one available"
191
- );
192
- });
1
+ /**
2
+ * @file The container's table names a track, and only the picture's entries are
3
+ * cut points.
4
+ *
5
+ * Measured 2026-08-18 on the two files the field sessions were recorded from:
6
+ * `Minions.and.Monsters.1080p.mkv` carries 2778 cue entries for its video track
7
+ * — one every 2.002 s — and 4669 more across four subtitle tracks;
8
+ * `Moana.2 … MegaPeer.mkv` carries 1055 for video and 5007 across five subtitle
9
+ * tracks. Read without the track, both sets went into the cut list together,
10
+ * and ffmpeg — which can only cut a copied picture at a real keyframe at or
11
+ * after the time it is asked for — moved every such cut forward to the next
12
+ * keyframe. That is the whole of the disagreement the field reported: 2.002 s
13
+ * on the first file, a median of 6.3 s and a worst case of 21 s on the second,
14
+ * and never once negative.
15
+ */
16
+
17
+ import test from "node:test";
18
+ import assert from "node:assert/strict";
19
+ import { MatroskaContainer } from "../services/container/MatroskaContainer.js";
20
+
21
+ const ID_EBML = 0x1a45dfa3;
22
+ const ID_SEGMENT = 0x18538067;
23
+ const ID_SEEK_HEAD = 0x114d9b74;
24
+ const ID_SEEK = 0x4dbb;
25
+ const ID_SEEK_ID = 0x53ab;
26
+ const ID_SEEK_POSITION = 0x53ac;
27
+ const ID_INFO = 0x1549a966;
28
+ const ID_TIMESTAMP_SCALE = 0x2ad7b1;
29
+ const ID_TRACKS = 0x1654ae6b;
30
+ const ID_TRACK_ENTRY = 0xae;
31
+ const ID_TRACK_NUMBER = 0xd7;
32
+ const ID_TRACK_TYPE = 0x83;
33
+ const ID_CUES = 0x1c53bb6b;
34
+ const ID_CUE_POINT = 0xbb;
35
+ const ID_CUE_TIME = 0xb3;
36
+ const ID_CUE_TRACK_POSITIONS = 0xb7;
37
+ const ID_CUE_TRACK = 0xf7;
38
+ const ID_CUE_CLUSTER_POSITION = 0xf1;
39
+
40
+ /** An element id, as the bytes the specification gives it. */
41
+ function idBytes(id) {
42
+ const bytes = [];
43
+ let rest = id;
44
+ while (rest > 0) {
45
+ bytes.unshift(rest & 0xff);
46
+ rest = Math.floor(rest / 256);
47
+ }
48
+ return Buffer.from(bytes);
49
+ }
50
+
51
+ /** A size, as a four-byte EBML variable-length integer. */
52
+ function sizeBytes(size) {
53
+ const buffer = Buffer.alloc(4);
54
+ buffer.writeUInt32BE(size, 0);
55
+ buffer[0] |= 0x10;
56
+ return buffer;
57
+ }
58
+
59
+ function element(id, payload) {
60
+ return Buffer.concat([idBytes(id), sizeBytes(payload.length), payload]);
61
+ }
62
+
63
+ /** An unsigned value, in as few bytes as it needs. */
64
+ function uintElement(id, value) {
65
+ const bytes = [];
66
+ let rest = value;
67
+ do {
68
+ bytes.unshift(rest & 0xff);
69
+ rest = Math.floor(rest / 256);
70
+ } while (rest > 0);
71
+ return element(id, Buffer.from(bytes));
72
+ }
73
+
74
+ function cuePoint(timeMs, tracks) {
75
+ return element(ID_CUE_POINT, Buffer.concat([
76
+ uintElement(ID_CUE_TIME, timeMs),
77
+ ...tracks.map((track) => element(ID_CUE_TRACK_POSITIONS, Buffer.concat([
78
+ uintElement(ID_CUE_TRACK, track),
79
+ uintElement(ID_CUE_CLUSTER_POSITION, 4096)
80
+ ])))
81
+ ]));
82
+ }
83
+
84
+ /**
85
+ * A file with the shape the field files have: one picture, one set of subtitles,
86
+ * and a table that indexes both.
87
+ *
88
+ * @param {{ withTracks?: boolean }} [options]
89
+ * @returns {Buffer}
90
+ */
91
+ function buildFile({ withTracks = true, cueTrack = null } = {}) {
92
+ const info = element(ID_INFO, uintElement(ID_TIMESTAMP_SCALE, 1_000_000));
93
+ const tracks = element(ID_TRACKS, Buffer.concat([
94
+ element(ID_TRACK_ENTRY, Buffer.concat([
95
+ uintElement(ID_TRACK_NUMBER, 1),
96
+ uintElement(ID_TRACK_TYPE, 1) // video
97
+ ])),
98
+ element(ID_TRACK_ENTRY, Buffer.concat([
99
+ uintElement(ID_TRACK_NUMBER, 2),
100
+ uintElement(ID_TRACK_TYPE, 17) // subtitles
101
+ ]))
102
+ ]));
103
+ const forPicture = cueTrack ?? 1;
104
+ const forSubtitles = cueTrack ?? 2;
105
+ const cues = element(ID_CUES, Buffer.concat([
106
+ cuePoint(0, [forPicture]),
107
+ cuePoint(1070, [forSubtitles]),
108
+ cuePoint(2002, [forPicture]),
109
+ cuePoint(3141, [forSubtitles]),
110
+ cuePoint(4004, [forPicture])
111
+ ]));
112
+
113
+ const seekEntry = (targetId, position) => element(ID_SEEK, Buffer.concat([
114
+ element(ID_SEEK_ID, idBytes(targetId)),
115
+ element(ID_SEEK_POSITION, (() => {
116
+ const buffer = Buffer.alloc(4);
117
+ buffer.writeUInt32BE(position, 0);
118
+ return buffer;
119
+ })())
120
+ ]));
121
+ // Positions are relative to the Segment's payload, so the SeekHead has to be
122
+ // measured before they can be stated. Its own length does not change when the
123
+ // placeholders become real values: every size and position here is written at
124
+ // a fixed width.
125
+ const draft = element(ID_SEEK_HEAD, Buffer.concat([
126
+ seekEntry(ID_INFO, 0),
127
+ ...(withTracks ? [seekEntry(ID_TRACKS, 0)] : []),
128
+ seekEntry(ID_CUES, 0)
129
+ ]));
130
+ const infoAt = draft.length;
131
+ const tracksAt = infoAt + info.length;
132
+ const cuesAt = withTracks ? tracksAt + tracks.length : infoAt + info.length;
133
+ const seekHead = element(ID_SEEK_HEAD, Buffer.concat([
134
+ seekEntry(ID_INFO, infoAt),
135
+ ...(withTracks ? [seekEntry(ID_TRACKS, tracksAt)] : []),
136
+ seekEntry(ID_CUES, cuesAt)
137
+ ]));
138
+
139
+ const segmentPayload = Buffer.concat(
140
+ withTracks ? [seekHead, info, tracks, cues] : [seekHead, info, cues]
141
+ );
142
+ return Buffer.concat([
143
+ element(ID_EBML, Buffer.from([0x42, 0x86, 0x81, 0x01])),
144
+ element(ID_SEGMENT, segmentPayload)
145
+ ]);
146
+ }
147
+
148
+ function readerOver(file) {
149
+ return async (start, end) => {
150
+ const last = Math.min(end, file.length - 1);
151
+ return start > last ? null : file.subarray(start, last + 1);
152
+ };
153
+ }
154
+
155
+ test("only the picture's entries become cut times", async () => {
156
+ const file = buildFile();
157
+
158
+ const times = await MatroskaContainer.readKeyframeTimes(readerOver(file), file.length);
159
+
160
+ assert.deepEqual(
161
+ times.map((time) => Number(time.toFixed(3))),
162
+ [0, 2.002, 4.004],
163
+ "the subtitle entries at 1.070 and 3.141 are not keyframes, and a cut asked for there lands late"
164
+ );
165
+ });
166
+
167
+ test("a table whose entries name no known track is still used", async () => {
168
+ // The cue points reference track 9, which Tracks never declares. Filtering
169
+ // leaves nothing — and returning nothing would put an even grid on a copied
170
+ // picture, the failure this reader exists to prevent.
171
+ const file = buildFile({ cueTrack: 9 });
172
+
173
+ const times = await MatroskaContainer.readKeyframeTimes(readerOver(file), file.length);
174
+
175
+ assert.deepEqual(
176
+ times.map((time) => Number(time.toFixed(3))),
177
+ [0, 1.07, 2.002, 3.141, 4.004],
178
+ "an unrecognised table beats no table at all"
179
+ );
180
+ });
181
+
182
+ test("a file whose tracks cannot be read keeps every entry", async () => {
183
+ const file = buildFile({ withTracks: false });
184
+
185
+ const times = await MatroskaContainer.readKeyframeTimes(readerOver(file), file.length);
186
+
187
+ assert.deepEqual(
188
+ times.map((time) => Number(time.toFixed(3))),
189
+ [0, 1.07, 2.002, 3.141, 4.004],
190
+ "with nothing to tell the tracks apart, the old behaviour is the only one available"
191
+ );
192
+ });
Binary file
@@ -0,0 +1,164 @@
1
+ /**
2
+ * @file The plan is what decides the encoders, and it is asked of real sessions.
3
+ *
4
+ * Three rules written into the session manager become one here: where a run
5
+ * belongs, when it has been overtaken, and how many the machine affords. The
6
+ * point of wiring rather than duplicating is that the arithmetic has sixteen
7
+ * checks of its own and no ffmpeg behind it.
8
+ */
9
+
10
+ import test from "node:test";
11
+ import assert from "node:assert/strict";
12
+ import { SourceFile } from "../services/source/SourceFile.js";
13
+ import { Timeline } from "../services/output/Timeline.js";
14
+ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
15
+ import os from "node:os";
16
+ import path from "node:path";
17
+ import { HlsSessionManager } from "../services/hls-session-manager.js";
18
+ import { SegmentStore } from "../services/encode/SegmentStore.js";
19
+ import { fmp4Format } from "../services/segment-formats/fmp4.js";
20
+ import { viewerOf } from "../services/viewer/Viewer.js";
21
+ import { startRunOn } from "./helpers/encode-run.js";
22
+
23
+ const KEY = "torrent:abc:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
24
+
25
+ function managerWithAnOutput() {
26
+ const root = mkdtempSync(path.join(os.tmpdir(), "orchestrator-wired-"));
27
+ const store = new SegmentStore({ root });
28
+ const manager = new HlsSessionManager({
29
+ enabled: true,
30
+ ffmpegBin: "ffmpeg",
31
+ localBindHost: "127.0.0.1",
32
+ localPort: 9090,
33
+ segmentStore: store
34
+ });
35
+ const dirPath = store.directoryFor(KEY);
36
+ store.useFormat(KEY, fmp4Format);
37
+ return { manager, store, root, dirPath };
38
+ }
39
+
40
+ function sessionOn({ id, dirPath, encodeStartIndex = 0, runEndIndex = -1, speed = 0, running = true }) {
41
+ const session = {
42
+ id,
43
+ outputKey: KEY,
44
+ dirPath,
45
+ // Where this file is cut, held by the file. A fixture that stated it
46
+ // on the session was describing what production no longer does.
47
+ timeline: new Timeline({
48
+ boundaries: Array.from({ length: 1001 }, (_, index) => index * 4),
49
+ cutGrid: "uniform"
50
+ }),
51
+ state: "ready",
52
+ file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "video.mkv" }),
53
+ // An ordinary session reads its own file, and its sound is inside it. The
54
+ // three differ only for a soundtrack shipped as a file of its own.
55
+ get inputFile() { return this.file; },
56
+ get audioFile() { return this.file; },
57
+ segmentFormat: fmp4Format,
58
+ segmentCount: 1000,
59
+ recentSpeed: null,
60
+ consumers: new Set(),
61
+ viewers: new Map(),
62
+ runs: new Set(),
63
+ lastAccessedAt: Date.now()
64
+ };
65
+ if (running) {
66
+ const run = startRunOn(session, { from: encodeStartIndex, to: runEndIndex });
67
+ // A speed is a reading taken FROM a run, so it names the run it came from.
68
+ session.recentSpeed = speed > 0 ? { speed, at: Date.now(), run } : null;
69
+ run.noteSpeed(speed);
70
+ }
71
+ return session;
72
+ }
73
+
74
+ test("a session is handed to the plan as the run it is", (t) => {
75
+ const { manager, root, dirPath } = managerWithAnOutput();
76
+ t.after(() => rmSync(root, { recursive: true, force: true }));
77
+
78
+ const session = sessionOn({ id: "one", dirPath, encodeStartIndex: 10, runEndIndex: 40 });
79
+ manager.sessionsById.set(session.id, session);
80
+ viewerOf(session, "watching").head = { segment: 12, seconds: 48, at: Date.now() };
81
+
82
+ manager.runQualityBudgetOnce;
83
+ manager.planEncodersNow();
84
+
85
+ const runs = manager.encodeOrchestrator.runsOn(KEY);
86
+ assert.equal(runs.length, 1, "the session the browser already has is a run like any other");
87
+ assert.equal(runs[0], [...session.runs][0], "and it is the run the session actually holds");
88
+ assert.equal(runs[0].from, 10);
89
+ assert.equal(runs[0].to, 40);
90
+ });
91
+
92
+ test("what a viewer waits for reaches the plan without their name", (t) => {
93
+ const { manager, root, dirPath } = managerWithAnOutput();
94
+ t.after(() => rmSync(root, { recursive: true, force: true }));
95
+
96
+ const session = sessionOn({ id: "one", dirPath, encodeStartIndex: 0, runEndIndex: -1 });
97
+ manager.sessionsById.set(session.id, session);
98
+ viewerOf(session, "someone").head = { segment: 5, seconds: 20, at: Date.now() };
99
+
100
+ manager.planEncodersNow();
101
+
102
+ const wanted = manager.encodeOrchestrator.demand.windowsOn(KEY);
103
+ assert.equal(wanted.length, 1);
104
+ assert.equal(wanted[0].from, 5, "where they are");
105
+ assert.ok(wanted[0].to > 5, "and the cushion in front of them");
106
+ });
107
+
108
+ test("a viewer who has gone quiet stops being waited for", (t) => {
109
+ const { manager, root, dirPath } = managerWithAnOutput();
110
+ t.after(() => rmSync(root, { recursive: true, force: true }));
111
+
112
+ const session = sessionOn({ id: "one", dirPath });
113
+ manager.sessionsById.set(session.id, session);
114
+ viewerOf(session, "gone").head = { segment: 5, seconds: 20, at: Date.now() - 10 * 60 * 1000 };
115
+
116
+ manager.planEncodersNow();
117
+
118
+ assert.equal(manager.encodeOrchestrator.demand.windowsOn(KEY).length, 0);
119
+ });
120
+
121
+ test("how many encoders the machine affords is measured, not chosen", (t) => {
122
+ const { manager, root, dirPath } = managerWithAnOutput();
123
+ t.after(() => rmSync(root, { recursive: true, force: true }));
124
+
125
+ // Nothing measured yet: one is what it has.
126
+ const cold = sessionOn({ id: "cold", dirPath });
127
+ manager.sessionsById.set(cold.id, cold);
128
+ assert.equal(manager.maxRunsForOutput(KEY), 1);
129
+
130
+ // Fast, but what a second job costs on THIS machine has not been measured,
131
+ // and an unmeasured penalty of 1 is not a statement that it is free.
132
+ cold.recentSpeed = { speed: 7.12 };
133
+ assert.equal(manager.maxRunsForOutput(KEY), 1, "no measurement, no second encoder");
134
+
135
+ // Measured on the addon host 2026-09-03: at 854x480 one run made 7.12x and
136
+ // two made 4.20x and 4.16x, a penalty of 1.70x, and both stayed far above
137
+ // realtime.
138
+ manager.contentionPenalties = new Map([[1, 1.7]]);
139
+ assert.ok(manager.maxRunsForOutput(KEY) > 1, "measured, and a second fits");
140
+
141
+ // The same host at 1920x1080: one made 1.96x, two made 0.99x and 0.98x.
142
+ manager.contentionPenalties = new Map([[1, 1.98]]);
143
+ cold.recentSpeed = { speed: 1.96 };
144
+ assert.equal(manager.maxRunsForOutput(KEY), 1, "the machine is full at one");
145
+ });
146
+
147
+ test("segments already made are known to the plan, whoever made them", (t) => {
148
+ const { manager, store, root, dirPath } = managerWithAnOutput();
149
+ t.after(() => rmSync(root, { recursive: true, force: true }));
150
+
151
+ for (const index of [0, 1, 2, 3]) {
152
+ writeFileSync(path.join(dirPath, fmp4Format.segmentFileName(index)), Buffer.alloc(16, 1));
153
+ }
154
+ const session = sessionOn({ id: "one", dirPath });
155
+ manager.sessionsById.set(session.id, session);
156
+
157
+ manager.planEncodersNow();
158
+
159
+ const coverage = manager.encodeOrchestrator.coverageOf(KEY);
160
+ assert.equal(coverage.isReady(0), true);
161
+ assert.equal(coverage.isReady(2), true);
162
+ assert.equal(coverage.isReady(3), false, "the highest has no successor to prove it closed");
163
+ void store;
164
+ });
@@ -0,0 +1,68 @@
1
+ /**
2
+ * @file What an output is encoded AS is decided once, for the output.
3
+ *
4
+ * The realtime budget decides the box, the frame rate and the speed setting
5
+ * from what the machine could hold at that moment. Decided per SESSION, two
6
+ * sessions of one output made minutes apart could be given different pictures
7
+ * while claiming the same identity — and everything downstream assumes they
8
+ * cannot be, from a segment of one standing in for a segment of the other to
9
+ * the single RESOLUTION the master names for both.
10
+ */
11
+
12
+ import test from "node:test";
13
+ import assert from "node:assert/strict";
14
+ import { Output, Outputs } from "../services/output/Output.js";
15
+
16
+ const KEY = "torrent:abc:fmt=fmp4:grid=even@0:video-only:v=0/enc:1280x720:auto";
17
+
18
+ test("the second session of one output is given the shape the first was", () => {
19
+ const outputs = new Outputs();
20
+ const first = outputs.get(KEY, () => new Output({
21
+ encodeWidth: 1280,
22
+ encodeHeight: 720,
23
+ outputFps: 24,
24
+ softwarePreset: "veryfast",
25
+ applyTonemap: false
26
+ }));
27
+ // A minute later the machine is busier and the budget would answer 480p. It
28
+ // is not asked: the shape of this output was settled when it was first made.
29
+ const second = outputs.get(KEY, () => {
30
+ throw new Error("deciding twice is how two sessions of one output diverge");
31
+ });
32
+
33
+ assert.equal(second, first);
34
+ assert.equal(second.encodeHeight, 720);
35
+ });
36
+
37
+ test("two outputs are two shapes", () => {
38
+ const outputs = new Outputs();
39
+ const tall = outputs.get(KEY, () => new Output({ encodeWidth: 1280, encodeHeight: 720, outputFps: 24 }));
40
+ const short = outputs.get(`${KEY}:other`, () => new Output({
41
+ encodeWidth: 854,
42
+ encodeHeight: 480,
43
+ outputFps: 24
44
+ }));
45
+
46
+ assert.notEqual(short, tall);
47
+ assert.equal(short.encodeHeight, 480);
48
+ });
49
+
50
+ test("a copied picture has no box of its own, and says so with zeroes", () => {
51
+ // Zero means the source's own size, which is what a copy is by definition:
52
+ // no box asked for can change one byte of it.
53
+ const copied = new Output({ encodeWidth: 0, encodeHeight: 0, outputFps: 24 });
54
+
55
+ assert.equal(copied.encodeWidth, 0);
56
+ assert.equal(copied.encodeHeight, 0);
57
+ assert.equal(copied.softwarePreset, null, "and no speed setting, having no encoder");
58
+ });
59
+
60
+ test("a shape nobody holds is dropped", () => {
61
+ const outputs = new Outputs();
62
+ const kept = outputs.get(KEY, () => new Output({ encodeWidth: 1280, encodeHeight: 720, outputFps: 24 }));
63
+ outputs.get(`${KEY}:other`, () => new Output({ encodeWidth: 854, encodeHeight: 480, outputFps: 24 }));
64
+
65
+ assert.equal(outputs.size, 2);
66
+ assert.equal(outputs.forgetUnused(new Set([kept])), 1);
67
+ assert.equal(outputs.size, 1);
68
+ });