@torrent-tv/proxy 2.80.4 → 2.80.6

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 (33) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/package.json +1 -1
  3. package/routes/api/delivery-sink/get.js +8 -4
  4. package/server.js +415 -403
  5. package/services/data-channel-handler.js +87 -25
  6. package/services/delivery-probe.js +38 -5
  7. package/services/encode/CoverageMap.js +77 -4
  8. package/services/encode/EncodePlan.js +1025 -358
  9. package/services/encode/EncodeRun.js +42 -22
  10. package/services/encode/SegmentDemand.js +0 -0
  11. package/services/encode/SegmentStore.js +55 -3
  12. package/services/encode/open-piece.js +47 -24
  13. package/services/encode/run-command.js +12 -2
  14. package/services/hls-session-manager.js +38 -158
  15. package/services/hwaccel.js +182 -54
  16. package/services/orchestrators/EncodeOrchestrator.js +123 -94
  17. package/services/output/LiveOutputs.js +233 -213
  18. package/services/output/Timeline.js +333 -256
  19. package/services/priority/PriorityMap.js +262 -108
  20. package/services/priority/PriorityOrchestrator.js +31 -6
  21. package/services/quality/EncodeCost.js +555 -500
  22. package/services/torrent-pool.js +9 -4
  23. package/test/encode-orchestrator.test.js +195 -65
  24. package/test/encode-plan-viewers.test.js +719 -0
  25. package/test/encode-plan.test.js +174 -81
  26. package/test/open-piece.test.js +152 -0
  27. package/test/output-speed.test.js +86 -0
  28. package/test/priority-map-download.test.js +25 -7
  29. package/test/priority-map.test.js +134 -83
  30. package/test/seek-landing.test.js +109 -76
  31. package/test/segment-demand.test.js +54 -56
  32. package/test/wedge-certainty.test.js +3 -3
  33. package/test/flushed-piece.test.js +0 -108
@@ -1,5 +1,15 @@
1
1
  /**
2
- * @file What viewers want of an output, stated once each and read as a union.
2
+ * @file What is wanted of one output, in its own segment numbers.
3
+ *
4
+ * The class held a window per viewer per band and merged them itself. That was
5
+ * the priority layer's work done a second time in the wrong place, and it
6
+ * carried the viewer's name as the key of a claim — against the rule that the
7
+ * encoding and the viewer are not connected at all. The behaviours those checks
8
+ * pinned (a viewer's own bands, two viewers as a union) are the priority map's
9
+ * and are checked there.
10
+ *
11
+ * What is left is a holder: one map per output, replaced whole, and an empty one
12
+ * meaning nobody is coming.
3
13
  */
4
14
 
5
15
  import test from "node:test";
@@ -7,76 +17,64 @@ import assert from "node:assert/strict";
7
17
  import { SegmentDemand } from "../services/encode/SegmentDemand.js";
8
18
 
9
19
  const PICTURE = "torrent:abc:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
10
- const SOUND = "torrent:abc:fmt=fmp4:grid=kf@0:audio-only:a=0/1/copy";
20
+ const SOUND = "torrent:abc:fmt=fmp4:grid=kf@0:audio-only:a=0/0/aac";
11
21
 
12
- test("a viewer restating a window replaces it rather than adding to it", () => {
13
- // A player restates its window every few seconds. Accumulated, the demand
14
- // would grow to the whole film within a minute.
22
+ test("an output nothing has been said about wants nothing", () => {
15
23
  const demand = new SegmentDemand();
16
- demand.state({ claimant: "one", address: PICTURE, from: 0, to: 10, statedAt: 1 });
17
- demand.state({ claimant: "one", address: PICTURE, from: 30, to: 40, statedAt: 2 });
18
- assert.deepEqual(demand.spanOn(PICTURE), { from: 30, to: 40 });
19
- assert.equal(demand.stats().windows, 1);
24
+ assert.deepEqual(demand.mapOn(PICTURE), []);
25
+ assert.deepEqual(demand.addresses(), []);
20
26
  });
21
27
 
22
- test("one viewer states a window per output, and both stand", () => {
28
+ test("a map replaces whatever was wanted before, rather than adding to it", () => {
29
+ // The map is a statement of what is wanted NOW, built fresh each time from
30
+ // where the viewers are. Accumulating them would keep serving people who have
31
+ // moved or gone.
23
32
  const demand = new SegmentDemand();
24
- demand.state({ claimant: "one", address: PICTURE, from: 0, to: 10, statedAt: 1 });
25
- demand.state({ claimant: "one", address: SOUND, from: 0, to: 10, statedAt: 1 });
26
- assert.equal(demand.stats().windows, 2);
27
- assert.deepEqual(demand.addresses().sort(), [SOUND, PICTURE].sort());
28
- });
29
-
30
- test("two viewers of one output are a union, not a sum", () => {
31
- // Two viewers seconds apart want mostly the same segments; counted twice, a
32
- // stretch would look twice as wanted as it is.
33
- const demand = new SegmentDemand();
34
- demand.state({ claimant: "one", address: PICTURE, from: 10, to: 14, statedAt: 1 });
35
- demand.state({ claimant: "two", address: PICTURE, from: 12, to: 16, statedAt: 1 });
36
- assert.deepEqual(demand.wantedOn(PICTURE), [10, 11, 12, 13, 14, 15, 16]);
33
+ demand.state(PICTURE, [{ from: 100, to: 130, priority: 32, withinSeconds: 0 }]);
34
+ demand.state(PICTURE, [{ from: 500, to: 530, priority: 32, withinSeconds: 0 }]);
35
+ assert.deepEqual(demand.mapOn(PICTURE), [{ from: 500, to: 530, priority: 32, withinSeconds: 0 }]);
37
36
  });
38
37
 
39
- test("two viewers far apart make a span that covers the ground between them", () => {
40
- // Not so it is all made so that a search for a gap considers all of it.
38
+ test("an empty map is a statement, and it is kept as one", () => {
39
+ // It says nobody is coming anywhere in this output, which is what stops the
40
+ // encoders on it. Dropped instead of stored, the output would look like one
41
+ // nothing had ever been said about, and the last map with people in it would
42
+ // stand as current.
41
43
  const demand = new SegmentDemand();
42
- demand.state({ claimant: "one", address: PICTURE, from: 0, to: 5, statedAt: 1 });
43
- demand.state({ claimant: "two", address: PICTURE, from: 900, to: 905, statedAt: 1 });
44
- assert.deepEqual(demand.spanOn(PICTURE), { from: 0, to: 905 });
44
+ demand.state(PICTURE, [{ from: 100, to: 130, priority: 32, withinSeconds: 0 }]);
45
+ demand.state(PICTURE, []);
46
+ assert.deepEqual(demand.mapOn(PICTURE), []);
47
+ assert.deepEqual(demand.addresses(), [PICTURE], "the output is still one that has been spoken about");
45
48
  });
46
49
 
47
- test("a viewer who leaves takes every window they stated", () => {
50
+ test("each output holds its own map", () => {
51
+ // Two outputs of one film are cut independently — 454 pieces against 401 on
52
+ // the field file — so the same second is a different number in each, and one
53
+ // map cannot serve both.
48
54
  const demand = new SegmentDemand();
49
- demand.state({ claimant: "one", address: PICTURE, from: 0, to: 5, statedAt: 1 });
50
- demand.state({ claimant: "one", address: SOUND, from: 0, to: 5, statedAt: 1 });
51
- demand.state({ claimant: "two", address: PICTURE, from: 0, to: 5, statedAt: 1 });
52
- assert.equal(demand.forget("one"), 2);
53
- assert.equal(demand.stats().claimants, 1);
54
- assert.equal(demand.addresses().length, 1);
55
- });
56
-
57
- test("expiry is carried out here and decided elsewhere", () => {
58
- // The register has no clock on purpose: the rule that says how stale is too
59
- // stale lives with whoever measures it, and this stays exercisable with
60
- // numbers alone.
61
- const demand = new SegmentDemand();
62
- demand.state({ claimant: "one", address: PICTURE, from: 0, to: 5, statedAt: 1000 });
63
- demand.state({ claimant: "two", address: PICTURE, from: 0, to: 5, statedAt: 9000 });
64
- const dropped = demand.forgetStatedBefore(5000);
65
- assert.equal(dropped.length, 1);
66
- assert.equal(dropped[0].claimant, "one");
67
- assert.equal(demand.stats().windows, 1);
55
+ demand.state(PICTURE, [{ from: 100, to: 130, priority: 32, withinSeconds: 0 }]);
56
+ demand.state(SOUND, [{ from: 88, to: 115, priority: 32, withinSeconds: 0 }]);
57
+ assert.equal(demand.mapOn(PICTURE)[0].from, 100);
58
+ assert.equal(demand.mapOn(SOUND)[0].from, 88);
59
+ assert.deepEqual(demand.addresses().sort(), [SOUND, PICTURE].sort());
68
60
  });
69
61
 
70
- test("a window that is not a window is refused rather than stored", () => {
62
+ test("an output can be forgotten entirely", () => {
71
63
  const demand = new SegmentDemand();
72
- assert.equal(demand.state({ claimant: "one", address: PICTURE, from: 5, to: 1, statedAt: 1 }), null);
73
- assert.equal(demand.state({ claimant: "", address: PICTURE, from: 0, to: 1, statedAt: 1 }), null);
74
- assert.equal(demand.state({ claimant: "one", address: "", from: 0, to: 1, statedAt: 1 }), null);
75
- assert.equal(demand.stats().windows, 0);
64
+ demand.state(PICTURE, [{ from: 100, to: 130, priority: 32, withinSeconds: 0 }]);
65
+ demand.forget(PICTURE);
66
+ assert.deepEqual(demand.addresses(), []);
76
67
  });
77
68
 
78
- test("nothing wanted on an output answers null rather than an empty span", () => {
69
+ test("nothing about a viewer can be stated, because nothing about one is held", () => {
70
+ // The check that the rule holds by construction: there is no name to pass and
71
+ // no way to ask about one.
79
72
  const demand = new SegmentDemand();
80
- assert.equal(demand.spanOn(PICTURE), null);
81
- assert.deepEqual(demand.wantedOn(PICTURE), []);
73
+ assert.equal(typeof (/** @type {any} */ (demand).want), "undefined");
74
+ assert.equal(typeof (/** @type {any} */ (demand).windowsOn), "undefined");
75
+ assert.equal(
76
+ demand.state.length,
77
+ 2,
78
+ "an address and a map, and nothing else"
79
+ );
82
80
  });
@@ -65,14 +65,14 @@ test("a tiny queue still waits for one round of probes", () => {
65
65
  assert.equal(verdict.needMs, PROBE_INTERVAL_MS);
66
66
  });
67
67
 
68
- test("nothing queued is not a wedge however long the counter has been still", () => {
68
+ test("nothing queued is still a wedge when the counter has been flat long enough — the channel queue was 0 on the real wedge while usrsctp held 399 MB", () => {
69
69
  const verdict = wedgeIsCertain({
70
70
  queuedBytes: 0,
71
71
  bytesPerSecond: 16 * MEGABYTE,
72
72
  flatForMs: 600_000
73
73
  });
74
- assert.equal(verdict.certain, false);
75
- assert.equal(verdict.needMs, null);
74
+ assert.equal(verdict.certain, true);
75
+ assert.equal(verdict.needMs, PROBE_INTERVAL_MS);
76
76
  });
77
77
 
78
78
  test("with no rate measured the answer is that nothing can be said", () => {
@@ -1,108 +0,0 @@
1
- /**
2
- * @file The piece an encoder writes out on its way to being stopped.
3
- *
4
- * `-segment_list pipe:3` was taken as proof that a piece is whole: ffmpeg names
5
- * a file when it closes it, so a named file is closed. True of the FILE and
6
- * false of the SPAN. On SIGTERM ffmpeg writes out the piece it had open and
7
- * names it like any other, so the result is a valid, decodable piece holding
8
- * film only up to the instant of the stop — under a name whose playlist entry
9
- * promises the whole span.
10
- *
11
- * Field 2026-09-06, one session, both tracks: `segment-00010.mp4` held 3.92 s
12
- * of its declared 5.589 s (96 frames), and the picture jumped 1.5 s at 1:02;
13
- * the soundtrack's own stopped run left the same shape at 17.5 s, 2.8 s wide.
14
- * Judging such a file by whether it decodes answers yes, which is how both
15
- * reached the viewer.
16
- */
17
-
18
- import test from "node:test";
19
- import assert from "node:assert/strict";
20
- import { mkdtemp, rm, writeFile, readdir } from "node:fs/promises";
21
- import os from "node:os";
22
- import path from "node:path";
23
- import { discardOpenPiece } from "../services/encode/open-piece.js";
24
-
25
- const format = {
26
- isSegmentFileName: (name) => /^segment-\d{5}\.mp4$/.test(name),
27
- segmentIndexFromName: (name) => Number(name.slice(8, 13))
28
- };
29
-
30
- /** A run directory holding pieces 0..last, every one of them readable. */
31
- async function runDirectory(last) {
32
- const dir = await mkdtemp(path.join(os.tmpdir(), "flushed-piece-"));
33
- for (let index = 0; index <= last; index += 1) {
34
- await writeFile(path.join(dir, `segment-${String(index).padStart(5, "0")}.mp4`), Buffer.alloc(64, 7));
35
- }
36
- return dir;
37
- }
38
-
39
- test("the piece named on the way out goes, though it reads perfectly", async () => {
40
- const dir = await runDirectory(10);
41
- try {
42
- const removed = await discardOpenPiece(
43
- dir,
44
- format,
45
- { from: 0, to: 535 },
46
- // Everything decodes. This is the field case exactly: the short piece is
47
- // a valid fMP4 and nothing about its contents betrays it.
48
- () => true,
49
- "segment-00010.mp4"
50
- );
51
- assert.equal(removed, 10, "the flushed piece was kept because it decodes");
52
- const left = await readdir(dir);
53
- assert.ok(!left.includes("segment-00010.mp4"));
54
- assert.ok(left.includes("segment-00009.mp4"), "a piece closed before the stop was taken too");
55
- } finally {
56
- await rm(dir, { recursive: true, force: true });
57
- }
58
- });
59
-
60
- test("a run that named nothing on the way out loses no readable piece", async () => {
61
- // A run that reached the end of its stretch closed its last file properly.
62
- // Removing it would mean encoding it a second time for nothing.
63
- const dir = await runDirectory(10);
64
- try {
65
- const removed = await discardOpenPiece(dir, format, { from: 0, to: 535 }, () => true, null);
66
- assert.equal(removed, null);
67
- assert.ok((await readdir(dir)).includes("segment-00010.mp4"));
68
- } finally {
69
- await rm(dir, { recursive: true, force: true });
70
- }
71
- });
72
-
73
- test("a piece that does not read still goes, named or not", async () => {
74
- const dir = await runDirectory(10);
75
- try {
76
- const removed = await discardOpenPiece(dir, format, { from: 0, to: 535 }, () => false, null);
77
- assert.equal(removed, 10, "an unreadable last piece survived");
78
- } finally {
79
- await rm(dir, { recursive: true, force: true });
80
- }
81
- });
82
-
83
- test("only inside the stretch the ended run was given", async () => {
84
- // Several runs write into one directory, kept apart by their intervals. The
85
- // highest file overall may belong to a run that is still going.
86
- const dir = await runDirectory(20);
87
- try {
88
- const removed = await discardOpenPiece(
89
- dir, format, { from: 0, to: 10 }, () => true, "segment-00010.mp4"
90
- );
91
- assert.equal(removed, 10);
92
- assert.ok((await readdir(dir)).includes("segment-00020.mp4"), "another run's piece was taken");
93
- } finally {
94
- await rm(dir, { recursive: true, force: true });
95
- }
96
- });
97
-
98
- test("a name from another run's stretch takes nothing", async () => {
99
- const dir = await runDirectory(20);
100
- try {
101
- const removed = await discardOpenPiece(
102
- dir, format, { from: 0, to: 10 }, () => true, "segment-00020.mp4"
103
- );
104
- assert.equal(removed, null, "a name outside the stretch removed a piece anyway");
105
- } finally {
106
- await rm(dir, { recursive: true, force: true });
107
- }
108
- });