@torrent-tv/proxy 2.75.0 → 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.
- package/CHANGELOG.md +1504 -1461
- package/CLAUDE.md +11 -1
- package/biome.json +182 -1
- package/docs/container-architecture.md +2 -1
- package/docs/encode-run-state.md +1 -1
- package/knip.json +14 -0
- package/package.json +1 -1
- package/routes/api/sources/warm/post.js +1 -1
- package/routes/api/transcode-sessions/post.js +185 -185
- package/routes/api/transcode-sessions/progress/get.js +5 -1
- package/routes/transcode/audio-file/get.js +11 -1
- package/routes/transcode/audio-warm/get.js +11 -1
- package/routes/transcode/session-file/get.js +1 -1
- package/routes/transcode/variant-file/get.js +10 -1
- package/scripts/render-run-graph.js +2 -2
- package/server.js +25 -0
- package/services/audio-inventory.js +206 -201
- package/services/container/AviContainer.js +1 -1
- package/services/container/Container.js +33 -1
- package/services/container/MatroskaContainer.js +1 -1
- package/services/container/Mp4Container.js +1 -1
- package/services/container/SubtitleFileContainer.js +0 -1
- package/services/controllers/SubtitleController.js +128 -128
- package/services/demand/index.js +7 -10
- package/services/download/registry.js +0 -14
- package/services/encode/CoverageMap.js +281 -0
- package/services/encode/EncodePlan.js +255 -0
- package/services/encode/EncodeRun.js +587 -0
- package/services/encode/Encoder.js +84 -0
- package/services/encode/NvencEncoder.js +45 -0
- package/services/encode/QsvEncoder.js +47 -0
- package/services/encode/SegmentDemand.js +0 -0
- package/services/encode/SegmentStore.js +529 -0
- package/services/encode/SoftwareEncoder.js +111 -0
- package/services/encode/V4l2m2mEncoder.js +53 -0
- package/services/encode/VaapiEncoder.js +53 -0
- package/services/encode/args.js +200 -0
- package/services/{encode-exit.js → encode/encode-exit.js} +17 -0
- package/services/encode/index.js +9 -0
- package/services/encode/run-command.js +647 -0
- package/services/hls-session-manager.js +11073 -10711
- package/services/hwaccel.js +1688 -1992
- package/services/orchestrators/EncodeOrchestrator.js +359 -0
- package/services/output/LiveOutputs.js +213 -0
- package/services/output/Output.js +94 -0
- package/services/output/OutputSpec.js +195 -0
- package/services/output/Timeline.js +220 -0
- package/services/output/index.js +1 -0
- package/services/output/ladder.js +26 -0
- package/services/playback-planner.js +806 -775
- package/services/produced-index.js +222 -300
- package/services/source/SourceFile.js +346 -0
- package/services/{sidecar-files.js → torrent/files.js} +107 -11
- package/services/torrent/naming.js +619 -0
- package/services/torrent-worker/client.js +10 -0
- package/services/torrent-worker/container-tracks.js +71 -43
- package/services/torrent-worker/pool-adapter.js +18 -0
- package/services/torrent-worker/protocol.js +7 -0
- package/services/torrent-worker/subtitle-cues.js +549 -549
- package/services/torrent-worker/worker.js +18 -0
- package/services/tracks/TextSubtitleTrack.js +287 -287
- package/services/tracks/index.js +15 -14
- package/services/viewer/Viewer.js +145 -0
- package/services/viewer/Viewers.js +124 -0
- package/test/auto-quality-step.test.js +508 -506
- package/test/behind-head-repair.test.js +17 -7
- package/test/coverage-map.test.js +153 -0
- package/test/cut-times-timeline.test.js +6 -5
- package/test/cuts-follow-published-grid.test.js +4 -4
- package/test/decode-cost.test.js +31 -12
- package/test/encode-exit.test.js +1 -1
- package/test/encode-orchestrator.test.js +196 -0
- package/test/encode-plan.test.js +245 -0
- package/test/encode-run-state.test.js +2 -2
- package/test/encode-run.test.js +168 -0
- package/test/encoder-kinds.test.js +122 -0
- package/test/held-request-width.test.js +9 -3
- package/test/helpers/encode-run.js +128 -0
- package/test/keyframe-index-accuracy.test.js +19 -12
- package/test/keyframes-belong-to-the-file.test.js +132 -0
- package/test/orchestrator-wired.test.js +164 -0
- package/test/output-shape.test.js +68 -0
- package/test/output-spec.test.js +157 -0
- package/test/produced-copy-choice.test.js +58 -92
- package/test/produced-index.test.js +142 -188
- package/test/quality-variants.test.js +1079 -1075
- package/test/run-graph-drift.test.js +1 -1
- package/test/run-intervals.test.js +329 -0
- package/test/run-position-follows-published-grid.test.js +4 -4
- package/test/seek-landing.test.js +8 -8
- package/test/seek-target-not-superseded.test.js +21 -9
- package/test/segment-demand.test.js +82 -0
- package/test/segment-serve-wiring.test.js +47 -52
- package/test/segment-store.test.js +187 -0
- package/test/segments-are-shared.test.js +175 -0
- package/test/sidecar-naming.test.js +142 -0
- package/test/source-file.test.js +133 -0
- package/test/stale-request-after-seek.test.js +18 -12
- package/test/subtitle-language.test.js +252 -252
- package/test/timeline.test.js +95 -0
- package/test/{sidecar-files.test.js → torrent-files.test.js} +44 -1
- package/test/torrent-naming.test.js +255 -0
- package/test/tracks-begin-together.test.js +44 -32
- package/test/two-viewers-one-picture.test.js +347 -0
- package/test/viewer-outputs.test.js +273 -0
- package/test/viewer.test.js +91 -0
- package/utils/perf.js +1 -63
- package/services/container/index.js +0 -6
- package/services/controllers/index.js +0 -2
- package/services/download/index.js +0 -8
- package/services/orchestrators/index.js +0 -2
- /package/services/{encode-run-state.js → encode/encode-run-state.js} +0 -0
- /package/services/{language-detect.js → tracks/language-detect.js} +0 -0
|
@@ -1,128 +1,128 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Subtitle controller — interface layer over SubtitleOrchestrator.
|
|
3
|
-
*
|
|
4
|
-
* Routes (HTTP or data-channel) call this, not the domain module directly.
|
|
5
|
-
* Handles external files vs embedded tracks branching, header setting, and
|
|
6
|
-
* cursor/covered-cluster bookkeeping. Domain work (cluster walk, conversion,
|
|
7
|
-
* language detection) stays in orchestrator/domain.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { subtitleOrchestrator } from "../orchestrators/SubtitleOrchestrator.js";
|
|
11
|
-
import { SubtitleFileContainer } from "../container/SubtitleFileContainer.js";
|
|
12
|
-
import { TextSubtitleTrack } from "../tracks/TextSubtitleTrack.js";
|
|
13
|
-
import { detectLanguage } from "../language-detect.js";
|
|
14
|
-
|
|
15
|
-
const EXTERNAL_MAX_BYTES = 8 * 1024 * 1024;
|
|
16
|
-
|
|
17
|
-
function readFileFully(file, maxBytes) {
|
|
18
|
-
return new Promise((resolve, reject) => {
|
|
19
|
-
const stream = file.createReadStream();
|
|
20
|
-
const chunks = [];
|
|
21
|
-
let total = 0;
|
|
22
|
-
stream.on("data", (chunk) => {
|
|
23
|
-
total += chunk.length;
|
|
24
|
-
if (total > maxBytes) { stream.destroy(); reject(new Error("subtitle file exceeds size cap")); return; }
|
|
25
|
-
chunks.push(chunk);
|
|
26
|
-
});
|
|
27
|
-
stream.on("end", () => resolve(Buffer.concat(chunks)));
|
|
28
|
-
stream.on("error", reject);
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export class SubtitleController {
|
|
33
|
-
constructor({ sourceRegistry, torrentPool }) {
|
|
34
|
-
this.sourceRegistry = sourceRegistry;
|
|
35
|
-
this.torrentPool = torrentPool;
|
|
36
|
-
this.orchestrator = subtitleOrchestrator;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Serve external subtitle file or embedded track.
|
|
41
|
-
* Returns { vtt, language, headers } or { error, status }.
|
|
42
|
-
*/
|
|
43
|
-
async getSubtitle({ sourceKey, fileIndex, trackIndex, since, after }) {
|
|
44
|
-
const rec = this.sourceRegistry.get(sourceKey);
|
|
45
|
-
if (!rec) return { error: "Source key was not found.", status: 404 };
|
|
46
|
-
const torrent = await this.torrentPool.getTorrent(rec.sourceType, rec.source);
|
|
47
|
-
const file = torrent.files[fileIndex];
|
|
48
|
-
if (!file) return { error: "File index was not found in torrent.", status: 404 };
|
|
49
|
-
|
|
50
|
-
const hasTrack = trackIndex !== undefined && trackIndex !== "" && Number.isFinite(Number(trackIndex));
|
|
51
|
-
if (!hasTrack) {
|
|
52
|
-
const name = file.name ?? "";
|
|
53
|
-
const ext = name.slice(name.lastIndexOf(".")).toLowerCase();
|
|
54
|
-
const release = this.torrentPool.acquireFile(torrent, fileIndex);
|
|
55
|
-
try {
|
|
56
|
-
const bytes = await readFileFully(file, EXTERNAL_MAX_BYTES);
|
|
57
|
-
const text = SubtitleFileContainer.decodeBytes(bytes);
|
|
58
|
-
const vtt = SubtitleFileContainer.toVtt(text, ext);
|
|
59
|
-
if (!vtt) return { error: `Unsupported subtitle format: ${ext}`, status: 422 };
|
|
60
|
-
// The language is read from the CONVERTED document, not from the file.
|
|
61
|
-
// The conversion has already dropped everything that is not the words —
|
|
62
|
-
// and on an ASS file that is half of it, in Latin letters, which is what
|
|
63
|
-
// made a Russian track answer `en` (field 2026-09-01, and the whole of
|
|
64
|
-
// `research/subtitle-language-ass-markup-2026-09-01.md`).
|
|
65
|
-
return { vtt, language: TextSubtitleTrack.detectLanguageFromVtt(vtt), headers: {} };
|
|
66
|
-
} catch (e) {
|
|
67
|
-
return { error: `Could not read subtitle file: ${e?.message ?? e}`, status: 502 };
|
|
68
|
-
} finally {
|
|
69
|
-
release();
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const idx = Number(trackIndex);
|
|
74
|
-
if (!Number.isInteger(idx) || idx < 0) return { error: "trackIndex must be a non-negative integer.", status: 400 };
|
|
75
|
-
|
|
76
|
-
// Resolve via orchestrator (domain: cluster walk or MP4 sample ranges)
|
|
77
|
-
const tracks = await this.orchestrator.getTracks(torrent, fileIndex, sourceKey);
|
|
78
|
-
const track = Array.isArray(tracks) ? tracks.find((c) => c.declaredIndex === idx) ?? null : null;
|
|
79
|
-
// Also try domain's declaredIndex-agnostic lookup via getCues path — keep compat with existing subtitle-cues declaredIndex
|
|
80
|
-
let held = null;
|
|
81
|
-
try {
|
|
82
|
-
// Need trackNumber for domain call — find via declared workspace
|
|
83
|
-
const domainTracks = await this.orchestrator.getDeclaredTracks(torrent, fileIndex, sourceKey);
|
|
84
|
-
// If not found, fall back to direct cuesHeldFor via trackNumber from tracks list
|
|
85
|
-
const target = track ?? domainTracks.find((t) => t.declaredIndex === idx) ?? null;
|
|
86
|
-
const trackNumber = target?.trackNumber ?? track?.trackNumber;
|
|
87
|
-
if (trackNumber != null) {
|
|
88
|
-
held = await this.orchestrator.getCues(this.torrentPool, torrent, fileIndex, sourceKey, trackNumber);
|
|
89
|
-
}
|
|
90
|
-
} catch {}
|
|
91
|
-
if (held && Array.isArray(held.cues)) {
|
|
92
|
-
const cursor = held.cues.reduce((h, c) => Math.max(h, Number(c.seq) || 0), 0);
|
|
93
|
-
const fresh = Number.isInteger(since) ? held.cues.filter((c) => (Number(c.seq) || 0) > since)
|
|
94
|
-
: Number.isFinite(after) ? held.cues.filter((c) => c.startSeconds > after) : held.cues;
|
|
95
|
-
const codecId = held.track?.codecId ?? track?.codecId ?? "";
|
|
96
|
-
const vtt = TextSubtitleTrack.cuesToVtt(fresh, codecId);
|
|
97
|
-
// Two things this reads, and each of them was wrong before 2.68.1.
|
|
98
|
-
//
|
|
99
|
-
// It reads the cues through `finalizeCues`, so what reaches the detector
|
|
100
|
-
// is the words and not ASS's `{\…}` override groups, which are Latin on a
|
|
101
|
-
// Russian track. (The dialogue row's own fields are gone earlier now, in
|
|
102
|
-
// the container that framed them — before 2.72.1 they were not gone at
|
|
103
|
-
// all, and the detector was reading them too.)
|
|
104
|
-
//
|
|
105
|
-
// And it reads EVERY cue held so far, not the `fresh` subset that is
|
|
106
|
-
// being sent. A re-subscription after a reconnect asks only for what this
|
|
107
|
-
// page missed, which can be three lines, and three lines are not a sample
|
|
108
|
-
// of a language.
|
|
109
|
-
const language = detectLanguage(
|
|
110
|
-
TextSubtitleTrack.finalizeCues(held.cues, codecId).map((cue) => cue.text).join("\n")
|
|
111
|
-
);
|
|
112
|
-
return {
|
|
113
|
-
vtt,
|
|
114
|
-
language,
|
|
115
|
-
headers: {
|
|
116
|
-
"X-Subtitle-Covered-Clusters": String(held.coveredClusters ?? 0),
|
|
117
|
-
"X-Subtitle-Indexed-Clusters": String(held.indexedClusters ?? 0),
|
|
118
|
-
"X-Subtitle-Cursor": String(cursor)
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
return { pending: true, status: 202 };
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
async warm(torrent, fileIndex, sourceKey) {
|
|
126
|
-
return this.orchestrator.warm(torrent, fileIndex, sourceKey);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @file Subtitle controller — interface layer over SubtitleOrchestrator.
|
|
3
|
+
*
|
|
4
|
+
* Routes (HTTP or data-channel) call this, not the domain module directly.
|
|
5
|
+
* Handles external files vs embedded tracks branching, header setting, and
|
|
6
|
+
* cursor/covered-cluster bookkeeping. Domain work (cluster walk, conversion,
|
|
7
|
+
* language detection) stays in orchestrator/domain.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { subtitleOrchestrator } from "../orchestrators/SubtitleOrchestrator.js";
|
|
11
|
+
import { SubtitleFileContainer } from "../container/SubtitleFileContainer.js";
|
|
12
|
+
import { TextSubtitleTrack } from "../tracks/TextSubtitleTrack.js";
|
|
13
|
+
import { detectLanguage } from "../tracks/language-detect.js";
|
|
14
|
+
|
|
15
|
+
const EXTERNAL_MAX_BYTES = 8 * 1024 * 1024;
|
|
16
|
+
|
|
17
|
+
function readFileFully(file, maxBytes) {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
const stream = file.createReadStream();
|
|
20
|
+
const chunks = [];
|
|
21
|
+
let total = 0;
|
|
22
|
+
stream.on("data", (chunk) => {
|
|
23
|
+
total += chunk.length;
|
|
24
|
+
if (total > maxBytes) { stream.destroy(); reject(new Error("subtitle file exceeds size cap")); return; }
|
|
25
|
+
chunks.push(chunk);
|
|
26
|
+
});
|
|
27
|
+
stream.on("end", () => resolve(Buffer.concat(chunks)));
|
|
28
|
+
stream.on("error", reject);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class SubtitleController {
|
|
33
|
+
constructor({ sourceRegistry, torrentPool }) {
|
|
34
|
+
this.sourceRegistry = sourceRegistry;
|
|
35
|
+
this.torrentPool = torrentPool;
|
|
36
|
+
this.orchestrator = subtitleOrchestrator;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Serve external subtitle file or embedded track.
|
|
41
|
+
* Returns { vtt, language, headers } or { error, status }.
|
|
42
|
+
*/
|
|
43
|
+
async getSubtitle({ sourceKey, fileIndex, trackIndex, since, after }) {
|
|
44
|
+
const rec = this.sourceRegistry.get(sourceKey);
|
|
45
|
+
if (!rec) return { error: "Source key was not found.", status: 404 };
|
|
46
|
+
const torrent = await this.torrentPool.getTorrent(rec.sourceType, rec.source);
|
|
47
|
+
const file = torrent.files[fileIndex];
|
|
48
|
+
if (!file) return { error: "File index was not found in torrent.", status: 404 };
|
|
49
|
+
|
|
50
|
+
const hasTrack = trackIndex !== undefined && trackIndex !== "" && Number.isFinite(Number(trackIndex));
|
|
51
|
+
if (!hasTrack) {
|
|
52
|
+
const name = file.name ?? "";
|
|
53
|
+
const ext = name.slice(name.lastIndexOf(".")).toLowerCase();
|
|
54
|
+
const release = this.torrentPool.acquireFile(torrent, fileIndex);
|
|
55
|
+
try {
|
|
56
|
+
const bytes = await readFileFully(file, EXTERNAL_MAX_BYTES);
|
|
57
|
+
const text = SubtitleFileContainer.decodeBytes(bytes);
|
|
58
|
+
const vtt = SubtitleFileContainer.toVtt(text, ext);
|
|
59
|
+
if (!vtt) return { error: `Unsupported subtitle format: ${ext}`, status: 422 };
|
|
60
|
+
// The language is read from the CONVERTED document, not from the file.
|
|
61
|
+
// The conversion has already dropped everything that is not the words —
|
|
62
|
+
// and on an ASS file that is half of it, in Latin letters, which is what
|
|
63
|
+
// made a Russian track answer `en` (field 2026-09-01, and the whole of
|
|
64
|
+
// `research/subtitle-language-ass-markup-2026-09-01.md`).
|
|
65
|
+
return { vtt, language: TextSubtitleTrack.detectLanguageFromVtt(vtt), headers: {} };
|
|
66
|
+
} catch (e) {
|
|
67
|
+
return { error: `Could not read subtitle file: ${e?.message ?? e}`, status: 502 };
|
|
68
|
+
} finally {
|
|
69
|
+
release();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const idx = Number(trackIndex);
|
|
74
|
+
if (!Number.isInteger(idx) || idx < 0) return { error: "trackIndex must be a non-negative integer.", status: 400 };
|
|
75
|
+
|
|
76
|
+
// Resolve via orchestrator (domain: cluster walk or MP4 sample ranges)
|
|
77
|
+
const tracks = await this.orchestrator.getTracks(torrent, fileIndex, sourceKey);
|
|
78
|
+
const track = Array.isArray(tracks) ? tracks.find((c) => c.declaredIndex === idx) ?? null : null;
|
|
79
|
+
// Also try domain's declaredIndex-agnostic lookup via getCues path — keep compat with existing subtitle-cues declaredIndex
|
|
80
|
+
let held = null;
|
|
81
|
+
try {
|
|
82
|
+
// Need trackNumber for domain call — find via declared workspace
|
|
83
|
+
const domainTracks = await this.orchestrator.getDeclaredTracks(torrent, fileIndex, sourceKey);
|
|
84
|
+
// If not found, fall back to direct cuesHeldFor via trackNumber from tracks list
|
|
85
|
+
const target = track ?? domainTracks.find((t) => t.declaredIndex === idx) ?? null;
|
|
86
|
+
const trackNumber = target?.trackNumber ?? track?.trackNumber;
|
|
87
|
+
if (trackNumber != null) {
|
|
88
|
+
held = await this.orchestrator.getCues(this.torrentPool, torrent, fileIndex, sourceKey, trackNumber);
|
|
89
|
+
}
|
|
90
|
+
} catch {}
|
|
91
|
+
if (held && Array.isArray(held.cues)) {
|
|
92
|
+
const cursor = held.cues.reduce((h, c) => Math.max(h, Number(c.seq) || 0), 0);
|
|
93
|
+
const fresh = Number.isInteger(since) ? held.cues.filter((c) => (Number(c.seq) || 0) > since)
|
|
94
|
+
: Number.isFinite(after) ? held.cues.filter((c) => c.startSeconds > after) : held.cues;
|
|
95
|
+
const codecId = held.track?.codecId ?? track?.codecId ?? "";
|
|
96
|
+
const vtt = TextSubtitleTrack.cuesToVtt(fresh, codecId);
|
|
97
|
+
// Two things this reads, and each of them was wrong before 2.68.1.
|
|
98
|
+
//
|
|
99
|
+
// It reads the cues through `finalizeCues`, so what reaches the detector
|
|
100
|
+
// is the words and not ASS's `{\…}` override groups, which are Latin on a
|
|
101
|
+
// Russian track. (The dialogue row's own fields are gone earlier now, in
|
|
102
|
+
// the container that framed them — before 2.72.1 they were not gone at
|
|
103
|
+
// all, and the detector was reading them too.)
|
|
104
|
+
//
|
|
105
|
+
// And it reads EVERY cue held so far, not the `fresh` subset that is
|
|
106
|
+
// being sent. A re-subscription after a reconnect asks only for what this
|
|
107
|
+
// page missed, which can be three lines, and three lines are not a sample
|
|
108
|
+
// of a language.
|
|
109
|
+
const language = detectLanguage(
|
|
110
|
+
TextSubtitleTrack.finalizeCues(held.cues, codecId).map((cue) => cue.text).join("\n")
|
|
111
|
+
);
|
|
112
|
+
return {
|
|
113
|
+
vtt,
|
|
114
|
+
language,
|
|
115
|
+
headers: {
|
|
116
|
+
"X-Subtitle-Covered-Clusters": String(held.coveredClusters ?? 0),
|
|
117
|
+
"X-Subtitle-Indexed-Clusters": String(held.indexedClusters ?? 0),
|
|
118
|
+
"X-Subtitle-Cursor": String(cursor)
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
return { pending: true, status: 202 };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async warm(torrent, fileIndex, sourceKey) {
|
|
126
|
+
return this.orchestrator.warm(torrent, fileIndex, sourceKey);
|
|
127
|
+
}
|
|
128
|
+
}
|
package/services/demand/index.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
+
// Only what is imported through this file. `unionOf`, `mayDisplaceSlowPeer`,
|
|
2
|
+
// `URGENCY_ORDER`, `nearestFirst`, `piecesNeededFor` and `piecesWithin` were
|
|
3
|
+
// re-exported here and taken by nobody; each is still where it is declared, and
|
|
4
|
+
// still used there.
|
|
1
5
|
export { DemandRegister } from "./DemandRegister.js";
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
|
|
5
|
-
mayDisplaceSlowPeer,
|
|
6
|
-
selectionPriority,
|
|
7
|
-
Urgency,
|
|
8
|
-
URGENCY_ORDER,
|
|
9
|
-
urgencyName
|
|
10
|
-
} from "./Urgency.js";
|
|
11
|
-
export { bytesOf, nearestFirst, piecesNeededFor, piecesOf, piecesWithin } from "./pieces.js";
|
|
6
|
+
export { Window } from "./Window.js";
|
|
7
|
+
export { isConditional, selectionPriority, Urgency, urgencyName } from "./Urgency.js";
|
|
8
|
+
export { bytesOf, piecesOf } from "./pieces.js";
|
|
@@ -80,17 +80,3 @@ export function reconcileAll() {
|
|
|
80
80
|
}
|
|
81
81
|
return { torrents: entries.length, speculativeAllowed, stated, withdrawn };
|
|
82
82
|
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Whether anybody, on any torrent, is still waiting for something urgent.
|
|
86
|
-
*
|
|
87
|
-
* @returns {boolean}
|
|
88
|
-
*/
|
|
89
|
-
export function anythingUrgentIsMissing() {
|
|
90
|
-
return [...live].some((entry) => entry.selection.hasUrgentMissing());
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/** Every live register and selection, for the periodic reconcile. */
|
|
94
|
-
export function liveDemand() {
|
|
95
|
-
return [...live];
|
|
96
|
-
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file What has been made of one output, what is being made right now, and by
|
|
3
|
+
* whom.
|
|
4
|
+
*
|
|
5
|
+
* One map per set of output parameters — never per session, never per viewer.
|
|
6
|
+
* Every segment number is in exactly one of three states:
|
|
7
|
+
*
|
|
8
|
+
* 1. **ready** — a file that is closed and can be served to anybody;
|
|
9
|
+
* 2. **being made** — claimed by a named live run, which has been given that
|
|
10
|
+
* stretch and is working forward through it;
|
|
11
|
+
* 3. **free** — nobody has made it and nobody is making it.
|
|
12
|
+
*
|
|
13
|
+
* Two questions are asked of it constantly and both have to be cheap, because
|
|
14
|
+
* one of them is on the path that answers a viewer:
|
|
15
|
+
*
|
|
16
|
+
* 1. is segment N ready — a set lookup;
|
|
17
|
+
* 2. where is the first gap at or after N — a walk over numbers, never over the
|
|
18
|
+
* disk. The walk it replaces listed every run directory of a session on the
|
|
19
|
+
* thread carrying the data channel, 1350 files for a 90-minute film, on
|
|
20
|
+
* every segment request.
|
|
21
|
+
*
|
|
22
|
+
* **Claims are intervals, not heads.** A run says which stretch it was given,
|
|
23
|
+
* so two runs on one output cannot be sent to the same numbers: the gap finder
|
|
24
|
+
* skips what another run will reach. A run that only announced its current
|
|
25
|
+
* position would leave the question "will anybody make #400" unanswerable
|
|
26
|
+
* without guessing at its speed.
|
|
27
|
+
*
|
|
28
|
+
* **Nothing here touches a disk, a process or a clock**, so every decision it
|
|
29
|
+
* makes can be exercised with numbers alone.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/** @typedef {"ready" | "making" | "free"} SegmentState */
|
|
33
|
+
|
|
34
|
+
export class CoverageMap {
|
|
35
|
+
/** Numbers whose file is closed and servable. @type {Set<number>} */
|
|
36
|
+
#ready = new Set();
|
|
37
|
+
|
|
38
|
+
/** Run id → the stretch it was given, both ends inclusive. @type {Map<string, {from: number, to: number}>} */
|
|
39
|
+
#claims = new Map();
|
|
40
|
+
|
|
41
|
+
/** How many segments this output has in total. @type {number} */
|
|
42
|
+
#segmentCount;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {object} [params]
|
|
46
|
+
* @param {number} [params.segmentCount=0] - The length of the output, in
|
|
47
|
+
* segments. Zero means it is not known yet, and then a gap search has to be
|
|
48
|
+
* given its own bound by the caller.
|
|
49
|
+
*/
|
|
50
|
+
constructor({ segmentCount = 0 } = {}) {
|
|
51
|
+
this.#segmentCount = Number.isInteger(segmentCount) && segmentCount > 0 ? segmentCount : 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The length of the output, once the playlist is known.
|
|
56
|
+
*
|
|
57
|
+
* @param {number} count
|
|
58
|
+
*/
|
|
59
|
+
setSegmentCount(count) {
|
|
60
|
+
if (Number.isInteger(count) && count > 0) {
|
|
61
|
+
this.#segmentCount = count;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** @returns {number} */
|
|
66
|
+
get segmentCount() {
|
|
67
|
+
return this.#segmentCount;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Record that a segment is closed and can be served.
|
|
72
|
+
*
|
|
73
|
+
* Idempotent, and deliberately independent of who made it: a segment made by
|
|
74
|
+
* a run that has since died is as good as one made by a run still going, and
|
|
75
|
+
* a segment left by a previous life of this process is as good as either.
|
|
76
|
+
*
|
|
77
|
+
* @param {number} index
|
|
78
|
+
*/
|
|
79
|
+
markReady(index) {
|
|
80
|
+
if (Number.isInteger(index) && index >= 0) {
|
|
81
|
+
this.#ready.add(index);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @param {Iterable<number>} indexes
|
|
87
|
+
*/
|
|
88
|
+
markReadyAll(indexes) {
|
|
89
|
+
for (const index of indexes) {
|
|
90
|
+
this.markReady(index);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Forget a segment: its file has gone, or was never closed.
|
|
96
|
+
*
|
|
97
|
+
* @param {number} index
|
|
98
|
+
*/
|
|
99
|
+
markGone(index) {
|
|
100
|
+
this.#ready.delete(index);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* A run has been given a stretch to fill.
|
|
105
|
+
*
|
|
106
|
+
* Replaces whatever that run claimed before, because a run has one stretch at
|
|
107
|
+
* a time: moved forward past ready material, it states the new one.
|
|
108
|
+
*
|
|
109
|
+
* @param {object} run - The run itself. A run has no name and needs none:
|
|
110
|
+
* what identifies it here is that it IS itself, and what identifies it in a
|
|
111
|
+
* log line is the stretch it was given, which no other live run of this
|
|
112
|
+
* output can hold.
|
|
113
|
+
* @param {number} from - First segment number, inclusive.
|
|
114
|
+
* @param {number} to - Last segment number, inclusive. May be
|
|
115
|
+
* `Number.POSITIVE_INFINITY` for a run with no end yet, which is what every
|
|
116
|
+
* run was before ends existed.
|
|
117
|
+
*/
|
|
118
|
+
claim(run, from, to) {
|
|
119
|
+
if (!run || !Number.isInteger(from) || from < 0) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const end = Number.isFinite(to) ? Math.max(from, Math.trunc(to)) : Number.POSITIVE_INFINITY;
|
|
123
|
+
this.#claims.set(run, { from, to: end });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A run has ended. Whatever it did not finish goes back to free.
|
|
128
|
+
*
|
|
129
|
+
* Nothing is un-marked: what it DID finish stays ready, because a closed file
|
|
130
|
+
* is closed whoever made it and whatever became of them afterwards.
|
|
131
|
+
*
|
|
132
|
+
* @param {object} run
|
|
133
|
+
*/
|
|
134
|
+
release(run) {
|
|
135
|
+
this.#claims.delete(run);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* @param {number} index
|
|
140
|
+
* @returns {boolean}
|
|
141
|
+
*/
|
|
142
|
+
isReady(index) {
|
|
143
|
+
return this.#ready.has(index);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The run that was given this number, if any.
|
|
148
|
+
*
|
|
149
|
+
* @param {number} index
|
|
150
|
+
* @returns {object | null}
|
|
151
|
+
*/
|
|
152
|
+
makerOf(index) {
|
|
153
|
+
for (const [run, span] of this.#claims) {
|
|
154
|
+
if (index >= span.from && index <= span.to) {
|
|
155
|
+
return run;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* @param {number} index
|
|
163
|
+
* @returns {SegmentState}
|
|
164
|
+
*/
|
|
165
|
+
stateOf(index) {
|
|
166
|
+
if (this.#ready.has(index)) {
|
|
167
|
+
return "ready";
|
|
168
|
+
}
|
|
169
|
+
return this.makerOf(index) === null ? "free" : "making";
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The first number at or after `index` that nobody has made and nobody is
|
|
174
|
+
* making — where a new run belongs.
|
|
175
|
+
*
|
|
176
|
+
* @param {number} index
|
|
177
|
+
* @param {number} [bound] - Search no further than this number, inclusive.
|
|
178
|
+
* Defaults to the last segment of the output; required while the length is
|
|
179
|
+
* unknown.
|
|
180
|
+
* @param {object} [exceptRun] - The run asking. Its own claim does not make
|
|
181
|
+
* a number taken as far as it is concerned: a run looking for where to move
|
|
182
|
+
* would otherwise be blocked by the very stretch it is trying to leave, and
|
|
183
|
+
* a run that had claimed the rest of the film could never move at all.
|
|
184
|
+
* @returns {number | null} Null when there is no gap in range, which is what
|
|
185
|
+
* "everything ahead is already covered" looks like.
|
|
186
|
+
*/
|
|
187
|
+
firstGapFrom(index, bound = undefined, exceptRun = null) {
|
|
188
|
+
const start = Number.isInteger(index) && index > 0 ? index : 0;
|
|
189
|
+
const last = Number.isInteger(bound) ? bound : this.#segmentCount - 1;
|
|
190
|
+
if (!Number.isInteger(last) || last < start) {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
for (let at = start; at <= last; at += 1) {
|
|
194
|
+
if (this.#ready.has(at)) {
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
const maker = this.makerOf(at);
|
|
198
|
+
if (maker === null || maker === exceptRun) {
|
|
199
|
+
return at;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* How many numbers from `index` onwards are already covered — ready, or
|
|
207
|
+
* claimed by a run other than `exceptRun`.
|
|
208
|
+
*
|
|
209
|
+
* This is what prices a decision: a run that has arrived at covered material
|
|
210
|
+
* either drives through it, paying its own encode time for every one of these
|
|
211
|
+
* numbers, or is moved to the gap beyond them, paying one restart. Both terms
|
|
212
|
+
* are measured elsewhere; this is the length.
|
|
213
|
+
*
|
|
214
|
+
* @param {number} index
|
|
215
|
+
* @param {object} [exceptRun] - The run asking. Its own claim does not
|
|
216
|
+
* count as somebody else's coverage.
|
|
217
|
+
* @returns {number}
|
|
218
|
+
*/
|
|
219
|
+
coveredRunFrom(index, exceptRun = null) {
|
|
220
|
+
const start = Number.isInteger(index) && index > 0 ? index : 0;
|
|
221
|
+
const last = this.#segmentCount > 0 ? this.#segmentCount - 1 : Number.MAX_SAFE_INTEGER;
|
|
222
|
+
let at = start;
|
|
223
|
+
while (at <= last) {
|
|
224
|
+
if (this.#ready.has(at)) {
|
|
225
|
+
at += 1;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
const maker = this.makerOf(at);
|
|
229
|
+
if (maker !== null && maker !== exceptRun) {
|
|
230
|
+
at += 1;
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
return at - start;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* How many numbers from `index` onwards are free — nobody has made them and
|
|
240
|
+
* nobody is making them.
|
|
241
|
+
*
|
|
242
|
+
* This is what gives a run its END. A run handed the whole rest of the film
|
|
243
|
+
* would drive straight through the next stretch somebody else is making; a
|
|
244
|
+
* run handed exactly the free stretch stops where the covered material
|
|
245
|
+
* begins, which is also where it would have been moved to anyway.
|
|
246
|
+
*
|
|
247
|
+
* @param {number} index
|
|
248
|
+
* @param {object} [exceptRun] - The run asking, whose own claim does not
|
|
249
|
+
* make a number unfree for it.
|
|
250
|
+
* @returns {number} Zero when `index` itself is not free.
|
|
251
|
+
*/
|
|
252
|
+
freeRunFrom(index, exceptRun = null) {
|
|
253
|
+
const start = Number.isInteger(index) && index > 0 ? index : 0;
|
|
254
|
+
const last = this.#segmentCount > 0 ? this.#segmentCount - 1 : Number.MAX_SAFE_INTEGER;
|
|
255
|
+
let at = start;
|
|
256
|
+
while (at <= last) {
|
|
257
|
+
if (this.#ready.has(at)) {
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
const maker = this.makerOf(at);
|
|
261
|
+
if (maker !== null && maker !== exceptRun) {
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
at += 1;
|
|
265
|
+
}
|
|
266
|
+
return at - start;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* What this map holds, for a log line.
|
|
271
|
+
*
|
|
272
|
+
* @returns {{ ready: number, claims: number, segmentCount: number }}
|
|
273
|
+
*/
|
|
274
|
+
stats() {
|
|
275
|
+
return {
|
|
276
|
+
ready: this.#ready.size,
|
|
277
|
+
claims: this.#claims.size,
|
|
278
|
+
segmentCount: this.#segmentCount
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
}
|