@torrent-tv/proxy 2.91.0 → 2.92.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/bin/cli.js +3 -2
- package/package.json +18 -2
- package/routes/api/playback-plan/post.js +21 -4
- package/routes/api/sources/files/get.js +20 -28
- package/routes/api/sources/post.js +4 -2
- package/routes/api/sources/stats/get.js +4 -11
- package/routes/api/sources/viewer/post.js +16 -0
- package/routes/api/sources/warm/post.js +6 -215
- package/routes/api/subtitles/get.js +22 -125
- package/routes/api/subtitles/post.js +26 -0
- package/routes/api/transcode-sessions/post.js +24 -2
- package/routes/media/get.js +80 -0
- package/routes/stream/get.js +26 -94
- package/routes/transcode/audio-warm/get.js +9 -21
- package/routes/transcode/variant-warm/get.js +8 -14
- package/server.js +410 -46
- package/services/encode/AdmittedInput.js +136 -0
- package/services/encode/CushionReport.js +1 -76
- package/services/encode/EncodeInputs.js +142 -0
- package/services/encode/EncodeOrchestrator.js +4 -1
- package/services/encode/EncodeRun.js +13 -4
- package/services/encode/EncodeRuns.js +132 -52
- package/services/encode/InputFailures.js +37 -0
- package/services/encode/MatroskaInput.js +158 -0
- package/services/encode/OutputOpening.js +16 -64
- package/services/encode/Renditions.js +1 -17
- package/services/encode/admitted-command.js +60 -0
- package/services/encode/annex-b-input.js +37 -0
- package/services/encode/bit-payload.js +29 -0
- package/services/encode/output/OutputCatalog.js +1 -1
- package/services/encode/output/Timeline.js +11 -0
- package/services/encode/pcm-normalization.js +30 -0
- package/services/encode/piece-completeness.js +54 -1
- package/services/encode/quality/HostLoad.js +19 -73
- package/services/encode/quality/QualityOffer.js +1 -1
- package/services/encode/run-command.js +1 -589
- package/services/encode/segment-formats/mp4-boxes.js +3 -1
- package/services/encode/segment-formats/presentation-segment.js +114 -0
- package/services/media/ContainerOrchestrator.js +118 -48
- package/services/media/KeyframeTables.js +16 -115
- package/services/media/MediaReadRequests.js +120 -0
- package/services/media/ProbeRequests.js +135 -0
- package/services/media/SegmentInputs.js +48 -0
- package/services/media/SourceFile.js +6 -0
- package/services/media/SubtitleOrchestrator.js +190 -4
- package/services/media/await-plan.js +47 -0
- package/services/media/container/AsfContainer.js +217 -0
- package/services/media/container/AviContainer.js +343 -34
- package/services/media/container/CadencedPictureOrder.js +45 -0
- package/services/media/container/Container.js +7 -3
- package/services/media/container/ContainerFactory.js +10 -3
- package/services/media/container/FfprobeContainer.js +222 -0
- package/services/media/container/MatroskaContainer.js +72 -46
- package/services/media/container/Mp4Container.js +472 -139
- package/services/media/container/MpegPsContainer.js +281 -0
- package/services/media/container/MpegTsContainer.js +277 -0
- package/services/media/container/PacketIndex.js +270 -0
- package/services/media/container/PacketRecords.js +237 -0
- package/services/media/container/RetainedBytes.js +39 -0
- package/services/media/container/RetainedReads.js +51 -0
- package/services/media/container/ac3-frame.js +46 -0
- package/services/media/container/asf-packets.js +153 -0
- package/services/media/container/avc-picture-order.js +50 -0
- package/services/media/container/avi-open-dml.js +47 -0
- package/services/media/container/dts-frame.js +51 -0
- package/services/media/container/ebml-stream.js +12 -1
- package/services/media/container/ffprobe-record.js +45 -0
- package/services/media/container/flv-packet-ranges.js +27 -0
- package/services/media/container/following-packet-time.js +37 -0
- package/services/media/container/h264-configuration.js +183 -0
- package/services/media/container/h264-elementary-index.js +245 -0
- package/services/media/container/hevc-configuration.js +165 -0
- package/services/media/container/hevc-elementary-index.js +132 -0
- package/services/media/container/hevc-picture-order.js +66 -0
- package/services/media/container/latm-frame.js +99 -0
- package/services/media/container/matroska-packets.js +207 -0
- package/services/media/container/memory-unavailable.js +8 -0
- package/services/media/container/mp3-packet-dependencies.js +41 -0
- package/services/media/container/mp4-fragments.js +148 -0
- package/services/media/container/mpeg-audio-frame.js +19 -0
- package/services/media/container/mpeg-audio-reservoir.js +27 -0
- package/services/media/container/mpeg-elementary-index.js +196 -0
- package/services/media/container/mpeg-sequence-reader.js +69 -0
- package/services/media/container/mpeg-ts-packets.js +93 -0
- package/services/media/container/mpeg-video-configuration.js +50 -0
- package/services/media/container/mpeg4-picture-timing.js +108 -0
- package/services/media/container/pes-header.js +34 -0
- package/services/media/playback-declarations.js +57 -0
- package/services/media/playback-planner.js +72 -230
- package/services/media/probe-packets.js +61 -0
- package/services/media/read-playback-declarations.js +18 -0
- package/services/media/tracks/AudioTrack.js +9 -0
- package/services/media/tracks/TextSubtitleTrack.js +4 -1
- package/services/server/OutputLifecycle.js +2 -0
- package/services/server/SegmentServing.js +40 -33
- package/services/server/ViewerRequests.js +22 -41
- package/services/server/controllers/PlaybackController.js +4 -0
- package/services/server/controllers/SubtitleController.js +8 -29
- package/services/server/transcode-session-files.js +58 -99
- package/services/server/wire-outputs.js +58 -15
- package/services/storage/IndexMemory.js +85 -0
- package/services/storage/MachineBudget.js +27 -3
- package/services/storage/allowance.js +8 -1
- package/services/storage/piece-store/piece-disk-store.js +18 -3
- package/services/storage/piece-store/piece-lru.js +8 -4
- package/services/storage/piece-store/shared-piece-store.js +26 -11
- package/services/storage/segment-store/SegmentStore.js +49 -18
- package/services/storage/wire.js +7 -0
- package/services/torrent/Contents.js +52 -20
- package/services/torrent/demand/DemandRegister.js +13 -4
- package/services/torrent/demand/Window.js +13 -2
- package/services/torrent/demand-progress.js +25 -0
- package/services/torrent/download/DeadlineScheduler.js +167 -0
- package/services/torrent/download/FutureDownload.js +166 -0
- package/services/torrent/download/SwarmSelection.js +30 -59
- package/services/torrent/download/registry.js +45 -5
- package/services/torrent/download/withdraw-claim.js +2 -0
- package/services/torrent/torrent-pool.js +39 -600
- package/services/torrent/worker/channel.js +8 -1
- package/services/torrent/worker/client.js +74 -72
- package/services/torrent/worker/files-in-use.js +9 -0
- package/services/torrent/worker/handle-state.js +12 -0
- package/services/torrent/worker/held-bytes.js +82 -99
- package/services/torrent/worker/held-file-bytes.js +17 -0
- package/services/torrent/worker/piece-reader.js +67 -683
- package/services/torrent/worker/pool-adapter.js +37 -123
- package/services/torrent/worker/protocol.js +6 -17
- package/services/torrent/worker/whole-file-stats.js +2 -0
- package/services/torrent/worker/worker.js +86 -156
- package/services/transport/data-channel-handler.js +56 -13
- package/services/viewer/DownloadMaps.js +168 -0
- package/services/viewer/NativeSourceMap.js +33 -0
- package/services/viewer/PriorityMap.js +49 -40
- package/services/viewer/PriorityOrchestrator.js +42 -13
- package/services/viewer/SourcePreparation.js +278 -0
- package/services/viewer/Viewer.js +11 -0
- package/services/viewer/Viewers.js +109 -2
- package/services/viewer/playback-readiness.js +50 -63
- package/services/viewer/segment-demands.js +37 -0
- package/services/viewer/urgent-output-ready.js +26 -0
- package/services/encode/run-inputs.js +0 -53
- package/services/media/keyframe-probe.js +0 -157
- package/services/media/media-info-probe.js +0 -93
- package/services/torrent/worker/background-fill.js +0 -213
- package/services/torrent/worker/resume-warm.js +0 -107
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Translate map demand to complete output intervals and selected source tracks. */
|
|
2
|
+
export function segmentDemands(output, fileIndex, zones) {
|
|
3
|
+
const tracks = [];
|
|
4
|
+
for (const type of ["video", "audio"]) {
|
|
5
|
+
const spec = output.spec?.[type];
|
|
6
|
+
if (spec?.fileIndex !== fileIndex) continue;
|
|
7
|
+
tracks.push({ type, index: type === "video" ? 0 : spec.trackIndex,
|
|
8
|
+
mode: type === "video" ? spec.encode ? "transcode" : "copy" : spec.transcode ? "transcode" : "copy" });
|
|
9
|
+
}
|
|
10
|
+
if (!tracks.length) return [];
|
|
11
|
+
const grid = output.timeline?.published ?? output.timeline?.boundaries;
|
|
12
|
+
if (!Array.isArray(grid)) return [];
|
|
13
|
+
const demands = [];
|
|
14
|
+
for (let index = 0; index + 1 < grid.length; index++) {
|
|
15
|
+
const from = grid[index], to = grid[index + 1];
|
|
16
|
+
if (!Number.isFinite(from) || !Number.isFinite(to) || !(to > from)) throw new Error("An output segment needs a valid interval.");
|
|
17
|
+
const overlapping = zones.filter(zone => zone.priority > 0 && zone.from < to && zone.to > from);
|
|
18
|
+
if (!overlapping.length) continue;
|
|
19
|
+
const owner = overlapping.reduce((first, zone) => (zone.deadlineAt ?? Infinity) < (first.deadlineAt ?? Infinity) ? zone : first);
|
|
20
|
+
demands.push({ index, from, to, tracks, owner,
|
|
21
|
+
priority: Math.max(...overlapping.map(zone => zone.priority)),
|
|
22
|
+
urgent: overlapping.some(zone => zone.urgent),
|
|
23
|
+
behind: overlapping.every(zone => zone.behind),
|
|
24
|
+
deferred: overlapping.every(zone => zone.deferred),
|
|
25
|
+
deadlineAt: Math.min(...overlapping.map(zone => Number.isFinite(zone.deadlineAt) ? zone.deadlineAt : Infinity)) });
|
|
26
|
+
}
|
|
27
|
+
return demands.sort((left, right) => left.deadlineAt - right.deadlineAt || right.priority - left.priority || left.index - right.index);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Source bytes must arrive before measured processing and delivery consume time. */
|
|
31
|
+
export function sourceDeadline(demand, { encodeSpeed, outputBytes, linkMbps }) {
|
|
32
|
+
const span = demand.to - demand.from;
|
|
33
|
+
const processing = Number.isFinite(encodeSpeed) && encodeSpeed > 0 ? span / encodeSpeed : 0;
|
|
34
|
+
const delivery = Number.isFinite(outputBytes) && outputBytes > 0 && Number.isFinite(linkMbps) && linkMbps > 0
|
|
35
|
+
? outputBytes * 8 / (linkMbps * 1_000_000) : 0;
|
|
36
|
+
return Number.isFinite(demand.deadlineAt) ? demand.deadlineAt - (processing + delivery) * 1000 : demand.deadlineAt;
|
|
37
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** All outputs still consumed by this viewer must cover the urgent interval. */
|
|
2
|
+
export function urgentOutputsReady({ sourceKey, fileIndex, durationSeconds, atSeconds, seconds,
|
|
3
|
+
outputs, consumed, segmentIndex, segmentStart, closed }) {
|
|
4
|
+
if (!Number.isFinite(durationSeconds) || !(durationSeconds > 0) ||
|
|
5
|
+
!Number.isFinite(atSeconds) || atSeconds < 0 ||
|
|
6
|
+
!Number.isFinite(seconds) || !(seconds > 0)) return false;
|
|
7
|
+
const target = Math.min(durationSeconds, atSeconds + seconds);
|
|
8
|
+
let found = false;
|
|
9
|
+
for (const output of outputs) {
|
|
10
|
+
if (output.file?.sourceKey !== sourceKey || output.file?.fileIndex !== fileIndex || !consumed(output)) continue;
|
|
11
|
+
found = true;
|
|
12
|
+
if (!output.timeline || !output.outputKey) return false;
|
|
13
|
+
let index = segmentIndex(output, atSeconds);
|
|
14
|
+
if (!Number.isSafeInteger(index) || index < 0) return false;
|
|
15
|
+
let start = segmentStart(output, index);
|
|
16
|
+
if (!Number.isFinite(start) || start > atSeconds) return false;
|
|
17
|
+
while (start < target) {
|
|
18
|
+
if (!closed(output.outputKey, index)) return false;
|
|
19
|
+
const next = segmentStart(output, index + 1);
|
|
20
|
+
if (!Number.isFinite(next) || !(next > start)) return false;
|
|
21
|
+
start = next;
|
|
22
|
+
index += 1;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return found;
|
|
26
|
+
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file The addresses an encoder reads its source from.
|
|
3
|
-
*
|
|
4
|
-
* Built per run from facts that belong elsewhere — which files the output
|
|
5
|
-
* reads (`OutputSpec`), where the stream route listens, which output the read
|
|
6
|
-
* feeds, and how wide its read window is — and kept nowhere.
|
|
7
|
-
*
|
|
8
|
-
* Two query parameters are what make the encoder's read what it is to the
|
|
9
|
-
* stream route: `reader=playback`, the one read that says where the viewer is,
|
|
10
|
-
* and `windowBytes`, how far ahead of itself that read asks the swarm for,
|
|
11
|
-
* sized in seconds of playback by whoever knows the file's byte rate.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/** The query parameter naming which run of an output a read feeds. */
|
|
15
|
-
const RUN_PARAMETER = "run";
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @param {object} params
|
|
19
|
-
* @param {{ fileIndex: number, streamUrl: (baseUrl: string, options?: { sessionId?: string }) => URL }} params.picture
|
|
20
|
-
* @param {{ fileIndex: number, streamUrl: (baseUrl: string, options?: { sessionId?: string }) => URL }} params.soundtrack
|
|
21
|
-
* The file the carried soundtrack lives in; the picture's own file for an
|
|
22
|
-
* embedded track.
|
|
23
|
-
* @param {"video-only" | "audio-only" | "muxed" | "empty"} params.carries
|
|
24
|
-
* @param {boolean} params.audioSeparate - The picture's sound travels as its own output.
|
|
25
|
-
* @param {string} params.sessionId - Which output this read feeds.
|
|
26
|
-
* @param {number} [params.runToken] - Which run of that output the read feeds,
|
|
27
|
-
* so the time its input waits is charged to that run and not to another one
|
|
28
|
-
* working on the same output.
|
|
29
|
-
* @param {number} params.readWindowBytes - Zero when no window was measured.
|
|
30
|
-
* @param {string} params.baseUrl
|
|
31
|
-
* @returns {{ inputFile: object, audioFile: object, inputUrl: string, audioInputUrl: string }}
|
|
32
|
-
*/
|
|
33
|
-
export function encoderInputs({ picture, soundtrack, carries, audioSeparate, sessionId, runToken, readWindowBytes, baseUrl }) {
|
|
34
|
-
// A soundtrack shipped as its own file is encoded FROM that file, so a
|
|
35
|
-
// rendition of it reads nothing else.
|
|
36
|
-
const inputFile = carries === "audio-only" && soundtrack !== picture ? soundtrack : picture;
|
|
37
|
-
const input = inputFile.streamUrl(baseUrl, { sessionId });
|
|
38
|
-
const forRun = (url) => {
|
|
39
|
-
if (Number.isInteger(runToken)) url.searchParams.set(RUN_PARAMETER, String(runToken));
|
|
40
|
-
return url;
|
|
41
|
-
};
|
|
42
|
-
forRun(input);
|
|
43
|
-
if (Number.isFinite(readWindowBytes) && readWindowBytes > 0) {
|
|
44
|
-
input.searchParams.set("windowBytes", String(readWindowBytes));
|
|
45
|
-
input.searchParams.set("reader", "playback");
|
|
46
|
-
}
|
|
47
|
-
// The second input exists only for a picture that carries its sound inside
|
|
48
|
-
// itself when that sound is in another file.
|
|
49
|
-
const audioInputUrl = carries !== "audio-only" && !audioSeparate && soundtrack !== inputFile
|
|
50
|
-
? forRun(soundtrack.streamUrl(baseUrl, { sessionId })).toString()
|
|
51
|
-
: "";
|
|
52
|
-
return { inputFile, audioFile: soundtrack, inputUrl: input.toString(), audioInputUrl };
|
|
53
|
-
}
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Where a file's keyframes are, found by DECODING it rather than by
|
|
3
|
-
* reading what its container states.
|
|
4
|
-
*
|
|
5
|
-
* The SECOND reader of one fact, and the weaker of the two: the container's own
|
|
6
|
-
* index is two point reads of 16 KB and names every keyframe, while this scans
|
|
7
|
-
* the media over a torrent-backed input and on a large file often does not
|
|
8
|
-
* finish — measured 2026-08-02, 77 keyframes in 45 s against all 570 in 0.8 s
|
|
9
|
-
* from the index. So it is never waited for and never displaces an index that
|
|
10
|
-
* answered; `KeyframeTable.learn` keeps the fuller answer whichever arrives
|
|
11
|
-
* second.
|
|
12
|
-
*
|
|
13
|
-
* It exists because some containers state no index at all, and a keyframe is
|
|
14
|
-
* still needed for something both encode branches need: choosing a SOURCE seek
|
|
15
|
-
* position ffmpeg can land on. `-ss` before `-i` trusts the container's
|
|
16
|
-
* on-the-fly seek, which for some containers (observed: AVI with VBR MP3 audio)
|
|
17
|
-
* can point at a position with no valid frame boundary at all — ffmpeg then
|
|
18
|
-
* fails outright ("Seek failed" / "Header missing"), not merely imprecisely.
|
|
19
|
-
*
|
|
20
|
-
* **Why it is here and not in `container/`.** It runs a process and reads the
|
|
21
|
-
* file over the address this proxy serves it at. A container is built from four
|
|
22
|
-
* functions and takes byte ranges; it spawns nothing and knows no URL. Both are
|
|
23
|
-
* readings of the same file, so both are this layer's — and they are separate
|
|
24
|
-
* files because one of them needs a machine and the other needs sixteen
|
|
25
|
-
* kilobytes.
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
import { spawn } from "node:child_process";
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The ffprobe that sits beside a given ffmpeg.
|
|
32
|
-
*
|
|
33
|
-
* @param {string} ffmpegBin
|
|
34
|
-
* @returns {string}
|
|
35
|
-
*/
|
|
36
|
-
export function ffprobeBinFor(ffmpegBin) {
|
|
37
|
-
if (typeof ffmpegBin !== "string" || ffmpegBin.length === 0) {
|
|
38
|
-
return "ffprobe";
|
|
39
|
-
}
|
|
40
|
-
if (/ffmpeg(\.exe)?$/i.test(ffmpegBin)) {
|
|
41
|
-
return ffmpegBin.replace(/ffmpeg(\.exe)?$/i, "ffprobe$1");
|
|
42
|
-
}
|
|
43
|
-
return "ffprobe";
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Where this file's keyframes are, found by DECODING it rather than by reading
|
|
48
|
-
* what its container states.
|
|
49
|
-
*
|
|
50
|
-
* The second reader of one fact, and the weaker of the two: the container's own
|
|
51
|
-
* index is two point reads of 16 KB and names every keyframe, while this scans
|
|
52
|
-
* the media over a torrent-backed input and on a large file often does not
|
|
53
|
-
* finish — measured 2026-08-02, 77 keyframes in 45 s against all 570 in 0.8 s
|
|
54
|
-
* from the index. So it is never waited for and never displaces an index that
|
|
55
|
-
* answered; `KeyframeTable.learn` keeps the fuller answer whichever arrives
|
|
56
|
-
* second.
|
|
57
|
-
*
|
|
58
|
-
* It exists because some containers state no index at all, and a keyframe is
|
|
59
|
-
* still needed for something both branches need: choosing a SOURCE seek
|
|
60
|
-
* position ffmpeg can land on. `-ss` before `-i` trusts the container's
|
|
61
|
-
* on-the-fly seek, which for some containers (observed: AVI with VBR MP3 audio)
|
|
62
|
-
* can point at a position with no valid frame boundary at all — ffmpeg then
|
|
63
|
-
* fails outright ("Seek failed" / "Header missing"), not merely imprecisely.
|
|
64
|
-
*
|
|
65
|
-
* It lives here, beside the codec probe, because it is the same kind of thing:
|
|
66
|
-
* a reading of the file made by running ffmpeg's own tools over the address
|
|
67
|
-
* this proxy serves it at. The container layer takes byte ranges and spawns
|
|
68
|
-
* nothing.
|
|
69
|
-
*
|
|
70
|
-
* @param {string} ffmpegBin
|
|
71
|
-
* @param {string | URL} inputUrl
|
|
72
|
-
* @param {number} [timeoutMs]
|
|
73
|
-
* @returns {Promise<number[] | null>} Sorted keyframe times, or null.
|
|
74
|
-
*/
|
|
75
|
-
export async function probeVideoKeyframeTimes(ffmpegBin, inputUrl, timeoutMs = 25_000) {
|
|
76
|
-
return new Promise((resolve) => {
|
|
77
|
-
let proc;
|
|
78
|
-
try {
|
|
79
|
-
proc = spawn(
|
|
80
|
-
ffprobeBinFor(ffmpegBin),
|
|
81
|
-
[
|
|
82
|
-
"-v", "error",
|
|
83
|
-
// `-skip_frame nokey` makes the decoder discard non-keyframes, so the
|
|
84
|
-
// probe reads only what it needs. Without it a full packet scan of a
|
|
85
|
-
// ~5 GB MKV cannot finish inside any sane budget over a torrent-backed
|
|
86
|
-
// input, the probe returns nothing, and the playlist falls back to a
|
|
87
|
-
// uniform grid — which on the COPY path is a lie: cuts land on the
|
|
88
|
-
// source's real keyframes, not on a 4 s ruler. The player then finds
|
|
89
|
-
// the declared times do not match the media, stops trusting the
|
|
90
|
-
// playlist and walks the file from segment #1 to locate the seek
|
|
91
|
-
// position by hand (field 2026-08-02: a seek to 1:30 produced requests
|
|
92
|
-
// #1, #2, #45, #86, #123 … #1187, taking minutes and never arriving).
|
|
93
|
-
"-skip_frame", "nokey",
|
|
94
|
-
"-select_streams", "v:0",
|
|
95
|
-
"-show_entries", "packet=pts_time,flags",
|
|
96
|
-
"-of", "csv=p=0",
|
|
97
|
-
String(inputUrl)
|
|
98
|
-
],
|
|
99
|
-
{ stdio: ["ignore", "pipe", "ignore"], windowsHide: true }
|
|
100
|
-
);
|
|
101
|
-
} catch {
|
|
102
|
-
resolve(null);
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
let stdout = "";
|
|
106
|
-
let settled = false;
|
|
107
|
-
const finish = (value) => {
|
|
108
|
-
if (settled) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
settled = true;
|
|
112
|
-
resolve(value);
|
|
113
|
-
};
|
|
114
|
-
const timer = setTimeout(() => {
|
|
115
|
-
try {
|
|
116
|
-
if (!proc.killed) {
|
|
117
|
-
proc.kill("SIGTERM");
|
|
118
|
-
}
|
|
119
|
-
} catch {
|
|
120
|
-
// ignore
|
|
121
|
-
}
|
|
122
|
-
finish(null);
|
|
123
|
-
}, timeoutMs);
|
|
124
|
-
proc.stdout.on("data", (chunk) => {
|
|
125
|
-
stdout += String(chunk);
|
|
126
|
-
});
|
|
127
|
-
proc.on("error", () => {
|
|
128
|
-
clearTimeout(timer);
|
|
129
|
-
finish(null);
|
|
130
|
-
});
|
|
131
|
-
proc.on("exit", (code) => {
|
|
132
|
-
clearTimeout(timer);
|
|
133
|
-
if (code !== 0) {
|
|
134
|
-
finish(null);
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
const times = [];
|
|
138
|
-
for (const line of stdout.split("\n")) {
|
|
139
|
-
// Each line: "<pts_time>,<flags>" e.g. "12.345000,K__"
|
|
140
|
-
const comma = line.indexOf(",");
|
|
141
|
-
if (comma < 0) {
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
const flags = line.slice(comma + 1);
|
|
145
|
-
if (!flags.includes("K")) {
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
const t = Number(line.slice(0, comma));
|
|
149
|
-
if (Number.isFinite(t)) {
|
|
150
|
-
times.push(t);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
times.sort((a, b) => a - b);
|
|
154
|
-
finish(times.length > 0 ? times : null);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
}
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file What a stream's own header says: duration, picture size, frame rate,
|
|
3
|
-
* start time, stream counts and HDR — from one short ffmpeg run that stops as
|
|
4
|
-
* soon as the header has been printed.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { spawn } from "node:child_process";
|
|
8
|
-
import {
|
|
9
|
-
parseFfmpegBitrateKbps,
|
|
10
|
-
parseFfmpegDurationSeconds,
|
|
11
|
-
parseFfmpegHdr,
|
|
12
|
-
parseFfmpegStartTimeSeconds,
|
|
13
|
-
parseFfmpegStreamCounts,
|
|
14
|
-
parseFfmpegVideoDimensions,
|
|
15
|
-
parseFfmpegVideoFps
|
|
16
|
-
} from "./ffmpeg-banner.js";
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Run a short ffmpeg probe to extract the total duration AND video resolution
|
|
20
|
-
* of a stream from the container header. Both are printed almost immediately
|
|
21
|
-
* (before any decoding), so this returns as soon as they are seen; an 8 s
|
|
22
|
-
* timeout guards the rest.
|
|
23
|
-
*
|
|
24
|
-
* @param {string} ffmpegBin - Path to the ffmpeg executable.
|
|
25
|
-
* @param {string | URL} inputUrl - URL of the stream to probe.
|
|
26
|
-
* @returns {Promise<{ durationSeconds: number | null, width: number | null, height: number | null, fps: number | null, startTime: number, isHdr: boolean }>}
|
|
27
|
-
*/
|
|
28
|
-
export async function probeInputMediaInfo(ffmpegBin, inputUrl) {
|
|
29
|
-
return new Promise((resolve) => {
|
|
30
|
-
const ffmpeg = spawn(ffmpegBin, ["-hide_banner", "-loglevel", "info", "-i", inputUrl, "-f", "null", "-"], {
|
|
31
|
-
stdio: ["ignore", "ignore", "pipe"],
|
|
32
|
-
windowsHide: true
|
|
33
|
-
});
|
|
34
|
-
let stderr = "";
|
|
35
|
-
let settled = false;
|
|
36
|
-
const finish = () => {
|
|
37
|
-
if (settled) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
settled = true;
|
|
41
|
-
const dims = parseFfmpegVideoDimensions(stderr);
|
|
42
|
-
resolve({
|
|
43
|
-
durationSeconds: parseFfmpegDurationSeconds(stderr),
|
|
44
|
-
bitrateKbps: parseFfmpegBitrateKbps(stderr),
|
|
45
|
-
width: dims.width,
|
|
46
|
-
height: dims.height,
|
|
47
|
-
fps: parseFfmpegVideoFps(stderr),
|
|
48
|
-
startTime: parseFfmpegStartTimeSeconds(stderr),
|
|
49
|
-
// Only ever read when a run fails, and read HERE because by then the
|
|
50
|
-
// banner is long gone: this probe is the one place the source says what
|
|
51
|
-
// it holds.
|
|
52
|
-
streamCounts: parseFfmpegStreamCounts(stderr),
|
|
53
|
-
isHdr: parseFfmpegHdr(stderr)
|
|
54
|
-
});
|
|
55
|
-
};
|
|
56
|
-
const timeoutId = setTimeout(() => {
|
|
57
|
-
if (!ffmpeg.killed) {
|
|
58
|
-
ffmpeg.kill("SIGTERM");
|
|
59
|
-
}
|
|
60
|
-
finish();
|
|
61
|
-
}, 8_000);
|
|
62
|
-
ffmpeg.stderr.on("data", (chunk) => {
|
|
63
|
-
stderr += String(chunk);
|
|
64
|
-
// The header ("Duration:" then the "Video: … WxH" stream line) is printed
|
|
65
|
-
// before any decoding. Bail as soon as both are present instead of letting
|
|
66
|
-
// `-f null -` decode the whole stream until the 8 s timeout.
|
|
67
|
-
//
|
|
68
|
-
// This probe asks about the PICTURE and nothing else now: a file with no
|
|
69
|
-
// video track is read by the container layer, which answers from 64 KB of
|
|
70
|
-
// header. It used to take an `expectVideo: false` for exactly that case,
|
|
71
|
-
// and the branch cost 8121 ms of every cold start (2026-09-03) because
|
|
72
|
-
// the exit still waited for a parsed DURATION, which a partly downloaded
|
|
73
|
-
// file prints as `N/A`.
|
|
74
|
-
const duration = parseFfmpegDurationSeconds(stderr);
|
|
75
|
-
const dims = parseFfmpegVideoDimensions(stderr);
|
|
76
|
-
if (duration != null && dims.width != null) {
|
|
77
|
-
clearTimeout(timeoutId);
|
|
78
|
-
if (!ffmpeg.killed) {
|
|
79
|
-
ffmpeg.kill("SIGTERM");
|
|
80
|
-
}
|
|
81
|
-
finish();
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
ffmpeg.on("error", () => {
|
|
85
|
-
clearTimeout(timeoutId);
|
|
86
|
-
finish();
|
|
87
|
-
});
|
|
88
|
-
ffmpeg.on("exit", () => {
|
|
89
|
-
clearTimeout(timeoutId);
|
|
90
|
-
finish();
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
}
|
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Fetch a whole file that nobody is playing yet, using only the room the viewer
|
|
3
|
-
* is not using.
|
|
4
|
-
*
|
|
5
|
-
* WHAT IT IS FOR. A release ships its dub and its subtitles as separate files,
|
|
6
|
-
* and a viewer who switches to one waits for the swarm to deliver its first
|
|
7
|
-
* pieces — 27.7 s in the field on 2026-08-31, which is longer than the switch is
|
|
8
|
-
* willing to wait. These files are small beside the picture (30 MB against 566
|
|
9
|
-
* MB, about a twentieth), so having them on disk before anyone asks turns every
|
|
10
|
-
* later switch into a local read.
|
|
11
|
-
*
|
|
12
|
-
* THE ORDERING, WHICH IS THE WHOLE DESIGN. What plays now comes first: the
|
|
13
|
-
* picture at the playhead, the soundtrack being heard, the subtitles being
|
|
14
|
-
* shown. The other soundtracks and subtitle files come next. Reading the film
|
|
15
|
-
* far ahead comes last. This module implements the middle tier, and it stays
|
|
16
|
-
* below the first by a condition that is measured rather than chosen: it fetches
|
|
17
|
-
* only while NO reader on the torrent is inside a wait. A reader blocked on a
|
|
18
|
-
* piece is the viewer's own reading starving, and that is exactly the moment
|
|
19
|
-
* this must not be asking the swarm for anything.
|
|
20
|
-
*
|
|
21
|
-
* WHY IT IS A READ AND NOT A SELECTION. `file.select()` claims every piece of a
|
|
22
|
-
* file at once. `#syncSelections` in `torrent/torrent-pool.js` records what that cost
|
|
23
|
-
* when it was done alongside the readers' own moving windows: a claim covering
|
|
24
|
-
* everything always outranked the window, and a seek to 89.1 % of a 4.7 GB film
|
|
25
|
-
* waited 93 s while the swarm fetched 2.47 GB in file order. So this walks the
|
|
26
|
-
* file a piece at a time through an ordinary bounded read, which claims what it
|
|
27
|
-
* is reading and gives it back.
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
import { logger } from "../../../utils/logger.js";
|
|
31
|
-
import { readersAreBlockedOn, stallsSeenOn } from "./piece-reader.js";
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* How long to stand aside after finding the viewer's own reading blocked.
|
|
35
|
-
*
|
|
36
|
-
* Not a measurement of anything: it is how often the question "is the viewer
|
|
37
|
-
* still starving?" is asked, and it is answered by the reader count, which is
|
|
38
|
-
* exact. Short enough that room is used soon after it appears, long enough that
|
|
39
|
-
* asking costs nothing.
|
|
40
|
-
*/
|
|
41
|
-
const STAND_ASIDE_MS = 1_000;
|
|
42
|
-
|
|
43
|
-
/** Files being filled, by `sourceKey:fileIndex`, so one runs per file. */
|
|
44
|
-
const running = new Map();
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* @param {number} ms
|
|
48
|
-
* @returns {Promise<void>}
|
|
49
|
-
*/
|
|
50
|
-
function pause(ms) {
|
|
51
|
-
return new Promise((resolve) => {
|
|
52
|
-
const timer = setTimeout(resolve, ms);
|
|
53
|
-
timer.unref?.();
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Read one byte range, letting the torrent fetch what is missing.
|
|
59
|
-
*
|
|
60
|
-
* @param {object} file
|
|
61
|
-
* @param {number} start
|
|
62
|
-
* @param {number} end - Inclusive.
|
|
63
|
-
* @returns {Promise<number>} Bytes read; 0 on failure.
|
|
64
|
-
*/
|
|
65
|
-
function readRange(file, start, end) {
|
|
66
|
-
return new Promise((resolve) => {
|
|
67
|
-
let stream;
|
|
68
|
-
try {
|
|
69
|
-
stream = file.createReadStream({ start, end });
|
|
70
|
-
} catch {
|
|
71
|
-
resolve(0);
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
let bytes = 0;
|
|
75
|
-
let settled = false;
|
|
76
|
-
const settle = (value) => {
|
|
77
|
-
if (settled) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
settled = true;
|
|
81
|
-
resolve(value);
|
|
82
|
-
};
|
|
83
|
-
stream.on("data", (chunk) => {
|
|
84
|
-
bytes += chunk.length;
|
|
85
|
-
});
|
|
86
|
-
stream.on("end", () => settle(bytes));
|
|
87
|
-
stream.on("error", () => {
|
|
88
|
-
stream.destroy?.();
|
|
89
|
-
settle(0);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Fetch a file whole, in the room the viewer leaves.
|
|
96
|
-
*
|
|
97
|
-
* Returns as soon as the work is under way; the caller is not waiting for it.
|
|
98
|
-
* One fill per file — a second request while one is running is ignored rather
|
|
99
|
-
* than doubling the reads.
|
|
100
|
-
*
|
|
101
|
-
* @param {object} torrent
|
|
102
|
-
* @param {number} fileIndex
|
|
103
|
-
* @param {string} sourceKey
|
|
104
|
-
* @param {{ chunkBytes?: number, isBlocked?: (infoHash: string) => boolean, stallsSeen?: (infoHash: string) => number }} [options]
|
|
105
|
-
* `isBlocked` answers "is the viewer's own reading starving right now"; it is
|
|
106
|
-
* the tier boundary, and it is a parameter so it can be exercised without a
|
|
107
|
-
* swarm.
|
|
108
|
-
* @returns {boolean} Whether a fill was started by this call.
|
|
109
|
-
*/
|
|
110
|
-
export function fillFileInBackground(torrent, fileIndex, sourceKey, options = {}) {
|
|
111
|
-
const key = `${sourceKey}:${fileIndex}`;
|
|
112
|
-
if (running.has(key)) {
|
|
113
|
-
return false;
|
|
114
|
-
}
|
|
115
|
-
const file = torrent?.files?.[fileIndex];
|
|
116
|
-
if (!file || !Number.isFinite(file.length) || file.length <= 0) {
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
// One piece at a time: the smallest unit the swarm actually delivers, so the
|
|
120
|
-
// gap between two checks of "is the viewer starving?" is as short as it can
|
|
121
|
-
// usefully be.
|
|
122
|
-
const chunkBytes = Number.isFinite(options.chunkBytes) && options.chunkBytes > 0
|
|
123
|
-
? options.chunkBytes
|
|
124
|
-
: (Number(torrent?.pieceLength) || 4 * 1024 * 1024);
|
|
125
|
-
const isBlocked = typeof options.isBlocked === "function" ? options.isBlocked : readersAreBlockedOn;
|
|
126
|
-
const stallsSeen = typeof options.stallsSeen === "function" ? options.stallsSeen : stallsSeenOn;
|
|
127
|
-
const work = fill(torrent, file, fileIndex, chunkBytes, isBlocked, stallsSeen).finally(() => {
|
|
128
|
-
running.delete(key);
|
|
129
|
-
});
|
|
130
|
-
running.set(key, work);
|
|
131
|
-
return true;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* @param {object} torrent
|
|
136
|
-
* @param {object} file
|
|
137
|
-
* @param {number} fileIndex
|
|
138
|
-
* @param {number} chunkBytes
|
|
139
|
-
* @param {(infoHash: string) => boolean} isBlocked
|
|
140
|
-
* @param {(infoHash: string) => number} stallsSeen
|
|
141
|
-
* @returns {Promise<void>}
|
|
142
|
-
*/
|
|
143
|
-
async function fill(torrent, file, fileIndex, chunkBytes, isBlocked, stallsSeen) {
|
|
144
|
-
const startedAt = Date.now();
|
|
145
|
-
const infoHash = String(torrent?.infoHash ?? "");
|
|
146
|
-
let read = 0;
|
|
147
|
-
let stoodAsideMs = 0;
|
|
148
|
-
// The stall count this fill last saw. A chunk is fetched only when it has not
|
|
149
|
-
// moved since the previous one.
|
|
150
|
-
let quietSince = stallsSeen(infoHash);
|
|
151
|
-
logger.info(
|
|
152
|
-
`background-fill: "${String(file.name).slice(0, 40)}" (${(file.length / 1e6).toFixed(1)}MB) will be ` +
|
|
153
|
-
"fetched whole while the viewer's own reading leaves room"
|
|
154
|
-
);
|
|
155
|
-
for (let start = 0; start < file.length; start += chunkBytes) {
|
|
156
|
-
// Stand aside while anything the viewer is watching is waiting on the
|
|
157
|
-
// swarm — AND for as long after it as it takes for a quiet stretch to
|
|
158
|
-
// pass. Pausing only DURING a stall is not enough: on a swarm delivering
|
|
159
|
-
// exactly what the film needs, this still takes bandwidth between stalls,
|
|
160
|
-
// and the stalls themselves are the proof there was none to spare. Field
|
|
161
|
-
// 2026-08-31, the case that forced this: 200-600 KB/s delivered against
|
|
162
|
-
// the 399 KB/s the film eats, one piece waited 101 s, and the picture
|
|
163
|
-
// stood still 145.6 s.
|
|
164
|
-
//
|
|
165
|
-
// "A quiet stretch" is measured, not chosen: the stall counter must not
|
|
166
|
-
// have moved while the previous chunk was being fetched. On a starving
|
|
167
|
-
// swarm it moves constantly and this stops altogether, which is the right
|
|
168
|
-
// answer — there is no spare room to use.
|
|
169
|
-
while (isBlocked(infoHash) || stallsSeen(infoHash) !== quietSince) {
|
|
170
|
-
stoodAsideMs += STAND_ASIDE_MS;
|
|
171
|
-
await pause(STAND_ASIDE_MS);
|
|
172
|
-
// Re-baselined after the pause, so a stretch that passes without a new
|
|
173
|
-
// stall lets the fill go on. Without this it could never resume.
|
|
174
|
-
quietSince = stallsSeen(infoHash);
|
|
175
|
-
}
|
|
176
|
-
// The torrent may have been destroyed under us — a viewer who left, the
|
|
177
|
-
// disk sweep, a restart. Reading a destroyed file throws, and there is
|
|
178
|
-
// nothing here worth an error.
|
|
179
|
-
if (!torrent?.files?.[fileIndex]) {
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
// Taken BEFORE the read, and deliberately not refreshed after it: a stall
|
|
183
|
-
// that happens while this chunk is in flight must still be visible to the
|
|
184
|
-
// next iteration. Refreshing afterwards erased exactly that evidence, which
|
|
185
|
-
// is the defect a test caught here.
|
|
186
|
-
quietSince = stallsSeen(infoHash);
|
|
187
|
-
const bytes = await readRange(file, start, Math.min(start + chunkBytes, file.length) - 1);
|
|
188
|
-
if (bytes === 0) {
|
|
189
|
-
logger.info(
|
|
190
|
-
`background-fill: "${String(file.name).slice(0, 40)}" stopped at ` +
|
|
191
|
-
`${(start / 1e6).toFixed(1)}MB — the read returned nothing`
|
|
192
|
-
);
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
read += bytes;
|
|
196
|
-
}
|
|
197
|
-
logger.info(
|
|
198
|
-
`background-fill: "${String(file.name).slice(0, 40)}" is on disk — ${(read / 1e6).toFixed(1)}MB in ` +
|
|
199
|
-
`${((Date.now() - startedAt) / 1000).toFixed(0)}s, of which ${(stoodAsideMs / 1000).toFixed(0)}s ` +
|
|
200
|
-
"was spent standing aside for the viewer; a switch to it will not wait for the swarm"
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Whether a file is being filled right now. For tests and for the log.
|
|
206
|
-
*
|
|
207
|
-
* @param {string} sourceKey
|
|
208
|
-
* @param {number} fileIndex
|
|
209
|
-
* @returns {boolean}
|
|
210
|
-
*/
|
|
211
|
-
export function fillIsRunning(sourceKey, fileIndex) {
|
|
212
|
-
return running.has(`${sourceKey}:${fileIndex}`);
|
|
213
|
-
}
|