@torrent-tv/proxy 2.107.82 → 2.107.84
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 +45 -1
- package/package.json +1 -1
- package/server.js +3 -2
- package/services/media/ffprobe-selection.js +79 -0
- package/services/media/probe-packets.js +6 -14
package/bin/cli.js
CHANGED
|
@@ -16,11 +16,12 @@ import "./thread-pool.js";
|
|
|
16
16
|
import { Command } from "commander";
|
|
17
17
|
import { mkdirSync } from "node:fs";
|
|
18
18
|
import os from "node:os";
|
|
19
|
-
import { spawnSync } from "node:child_process";
|
|
19
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
20
20
|
import { createRequire } from "node:module";
|
|
21
21
|
import ffmpegStatic from "ffmpeg-static";
|
|
22
22
|
import nodeDataChannel from "node-datachannel";
|
|
23
23
|
import { startProxyServer } from "../server.js";
|
|
24
|
+
import { ffprobeCandidates, selectFfprobe } from "../services/media/ffprobe-selection.js";
|
|
24
25
|
import { registerClient } from "../services/transport/registry-api.js";
|
|
25
26
|
import { createTunnelClient } from "../services/transport/tunnel-client.js";
|
|
26
27
|
import { createWebRtcManager } from "../services/transport/webrtc-manager.js";
|
|
@@ -190,6 +191,44 @@ const memoryBytes =
|
|
|
190
191
|
const bundledFfmpegBin = typeof ffmpegStatic === "string" ? ffmpegStatic : "";
|
|
191
192
|
const ffmpegBin = options.ffmpegBin ? String(options.ffmpegBin) : bundledFfmpegBin || "ffmpeg";
|
|
192
193
|
|
|
194
|
+
/**
|
|
195
|
+
* The path of the ffprobe binary bundled by `@ffprobe-installer/ffprobe`, or null when
|
|
196
|
+
* the package cannot be loaded on this platform.
|
|
197
|
+
*
|
|
198
|
+
* @returns {string | null}
|
|
199
|
+
*/
|
|
200
|
+
function bundledFfprobeBin() {
|
|
201
|
+
try {
|
|
202
|
+
const { path } = require("@ffprobe-installer/ffprobe");
|
|
203
|
+
return typeof path === "string" && path !== "" ? path : null;
|
|
204
|
+
} catch {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// How long one run of a candidate ffprobe (`-version`, `-sections`) may take before it is
|
|
210
|
+
// killed and the candidate does not qualify. CHOSEN, not measured: both print at once.
|
|
211
|
+
const FFPROBE_RUN_LIMIT_MS = 10_000;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Run `binary args` and resolve what it printed; reject when it cannot be started.
|
|
215
|
+
*
|
|
216
|
+
* @param {string} binary
|
|
217
|
+
* @param {string[]} args
|
|
218
|
+
* @returns {Promise<{ status: number | null, stdout: string, stderr: string }>}
|
|
219
|
+
*/
|
|
220
|
+
function runProcess(binary, args) {
|
|
221
|
+
return new Promise((resolve, reject) => {
|
|
222
|
+
const child = spawn(binary, args, { stdio: ["ignore", "pipe", "pipe"], windowsHide: true, timeout: FFPROBE_RUN_LIMIT_MS });
|
|
223
|
+
let stdout = "";
|
|
224
|
+
let stderr = "";
|
|
225
|
+
child.stdout.setEncoding("utf8").on("data", (chunk) => { stdout += chunk; });
|
|
226
|
+
child.stderr.setEncoding("utf8").on("data", (chunk) => { stderr += chunk; });
|
|
227
|
+
child.once("error", reject);
|
|
228
|
+
child.once("close", (status) => resolve({ status, stdout, stderr }));
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
193
232
|
/**
|
|
194
233
|
* Verify that the ffmpeg binary is reachable and exits cleanly.
|
|
195
234
|
* Throws with a descriptive message when the check fails.
|
|
@@ -398,11 +437,16 @@ try {
|
|
|
398
437
|
if (transcodeAudio) {
|
|
399
438
|
assertFfmpegAvailability();
|
|
400
439
|
}
|
|
440
|
+
// Chosen once, here, by what each candidate can do; a host with none that qualifies
|
|
441
|
+
// stops with FfprobeUnavailableError (one logged line) instead of refusing every probe.
|
|
442
|
+
const ffprobe = await selectFfprobe({ candidates: ffprobeCandidates({ bundledBinary: bundledFfprobeBin() }), run: runProcess, logger });
|
|
443
|
+
logger.info(`ffprobe: using ${ffprobe.source} ${ffprobe.binary} (version ${ffprobe.version}), which names section stream_side_data`);
|
|
401
444
|
const started = await startProxyServer({
|
|
402
445
|
host: bindHost,
|
|
403
446
|
port: localPort,
|
|
404
447
|
transcodeAudio,
|
|
405
448
|
ffmpegBin,
|
|
449
|
+
ffprobeBin: ffprobe.binary,
|
|
406
450
|
memoryBytes,
|
|
407
451
|
segmentFormat: options.segmentFormat,
|
|
408
452
|
stateDir,
|
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -110,6 +110,7 @@ function buildPortCandidates(startPort, maxAttempts = 51) {
|
|
|
110
110
|
* @property {number} port - Preferred listen port.
|
|
111
111
|
* @property {boolean} transcodeAudio - Whether HLS audio transcoding is enabled.
|
|
112
112
|
* @property {string} ffmpegBin - Path to the ffmpeg executable.
|
|
113
|
+
* @property {string} ffprobeBin - Path to the ffprobe executable chosen at startup (`services/media/ffprobe-selection.js`).
|
|
113
114
|
* @property {number} [memoryBytes] - Per-torrent budget for pieces held in memory (undefined = store default).
|
|
114
115
|
* @property {string} [segmentFormat] - HLS output container: "fmp4" (default) or "mpegts".
|
|
115
116
|
* @property {string} [stateDir] - Where to keep what this host has measured about itself.
|
|
@@ -122,7 +123,7 @@ function buildPortCandidates(startPort, maxAttempts = 51) {
|
|
|
122
123
|
* @returns {Promise<{ app: import("fastify").FastifyInstance, port: number }>}
|
|
123
124
|
*/
|
|
124
125
|
export async function startProxyServer({
|
|
125
|
-
host, port, transcodeAudio, ffmpegBin, memoryBytes, segmentFormat, stateDir, onSubtitleCues, onChaptersChanged, diagnostics, budgetPolicy,
|
|
126
|
+
host, port, transcodeAudio, ffmpegBin, ffprobeBin, memoryBytes, segmentFormat, stateDir, onSubtitleCues, onChaptersChanged, diagnostics, budgetPolicy,
|
|
126
127
|
deliverySink = false,
|
|
127
128
|
// Whether a memory high-water earns a heap snapshot (`--heap-snapshots`).
|
|
128
129
|
heapSnapshots = false,
|
|
@@ -324,7 +325,7 @@ export async function startProxyServer({
|
|
|
324
325
|
},
|
|
325
326
|
packetMemory: (sourceKey, fileIndex) => indexMemory.forFile(sourceKey, fileIndex),
|
|
326
327
|
readProbe: request => probeReads.read(request),
|
|
327
|
-
probePackets,
|
|
328
|
+
probePackets: request => probePackets({ ...request, ffprobeBin }),
|
|
328
329
|
baseUrl: () => localBaseUrl,
|
|
329
330
|
observeWait: (sourceKey, event, fields) => waitTrace.source(sourceKey, event, fields),
|
|
330
331
|
reads: { revision: (...a) => mediaReads.revision(...a), memoryRevision: () => mediaReads.memoryRevision(),
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Which ffprobe binary the proxy probes with is decided once, at startup,
|
|
3
|
+
* by what the binary can do: its `-sections` output names the section
|
|
4
|
+
* `stream_side_data`, which the `streams` statement of `probe-packets.js` asks for.
|
|
5
|
+
* The bundled build of darwin-arm64 (4.4.1) has `stream_side_data_list` and no
|
|
6
|
+
* `stream_side_data`, and refuses every probe; a version number would need a table
|
|
7
|
+
* of releases, the section list is the fact.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** The section the `streams` statement asks for. */
|
|
11
|
+
const REQUIRED_SECTION = "stream_side_data";
|
|
12
|
+
|
|
13
|
+
/** Thrown when no candidate qualifies; its message is the one line `selectFfprobe` logged. */
|
|
14
|
+
export class FfprobeUnavailableError extends Error {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "FfprobeUnavailableError";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The candidates in order of preference: the bundled binary when its path is
|
|
23
|
+
* known, then `ffprobe` found on PATH.
|
|
24
|
+
*
|
|
25
|
+
* @param {{ bundledBinary?: string | null }} options
|
|
26
|
+
* @returns {{ source: "bundled" | "PATH", binary: string }[]}
|
|
27
|
+
*/
|
|
28
|
+
export function ffprobeCandidates({ bundledBinary }) {
|
|
29
|
+
const candidates = [];
|
|
30
|
+
if (typeof bundledBinary === "string" && bundledBinary !== "") candidates.push({ source: "bundled", binary: bundledBinary });
|
|
31
|
+
candidates.push({ source: "PATH", binary: "ffprobe" });
|
|
32
|
+
return candidates;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The word after `ffprobe version` on the first line, or "unknown". */
|
|
36
|
+
function versionOf(output) {
|
|
37
|
+
return /^ffprobe version (\S+)/.exec(String(output).split("\n", 1)[0])?.[1] ?? "unknown";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** True when a name in the `-sections` print, taken apart at whitespace and `/`, equals the section. */
|
|
41
|
+
function namesSection(output) {
|
|
42
|
+
return String(output).split(/[\s/]+/).includes(REQUIRED_SECTION);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Ask each candidate, in order, its version and its sections, each at most once, and
|
|
47
|
+
* choose the first that exits 0 from `-sections` and names `stream_side_data`. A later
|
|
48
|
+
* candidate is not run once one is chosen.
|
|
49
|
+
*
|
|
50
|
+
* @param {object} options
|
|
51
|
+
* @param {{ source: string, binary: string }[]} options.candidates
|
|
52
|
+
* @param {(binary: string, args: string[]) => Promise<{ status: number | null, stdout: string, stderr: string }>} options.run
|
|
53
|
+
* Rejects when the process cannot be started.
|
|
54
|
+
* @param {{ error: (line: string) => void }} options.logger
|
|
55
|
+
* @returns {Promise<{ source: string, binary: string, version: string }>}
|
|
56
|
+
* @throws {FfprobeUnavailableError} After one `error` line, when no candidate qualifies.
|
|
57
|
+
*/
|
|
58
|
+
export async function selectFfprobe({ candidates, run, logger }) {
|
|
59
|
+
const entries = [];
|
|
60
|
+
for (const { source, binary } of candidates) {
|
|
61
|
+
let version;
|
|
62
|
+
let sections;
|
|
63
|
+
try {
|
|
64
|
+
const versionRun = await run(binary, ["-version"]);
|
|
65
|
+
version = versionRun.status === 0 ? versionOf(versionRun.stdout) : "unknown";
|
|
66
|
+
sections = await run(binary, ["-hide_banner", "-sections"]);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
69
|
+
entries.push(`${source} ${binary} cannot be run (${reason}), so ${REQUIRED_SECTION} was not checked`);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (sections.status === 0 && namesSection(sections.stdout)) return { source, binary, version };
|
|
73
|
+
const exit = sections.status === 0 ? "" : `, -sections exited ${sections.status}`;
|
|
74
|
+
entries.push(`${source} ${binary} (version ${version}${exit}) lacks section ${REQUIRED_SECTION}`);
|
|
75
|
+
}
|
|
76
|
+
const line = `ffprobe: no usable binary: ${entries.length ? entries.join("; ") : "no candidate to try"}`;
|
|
77
|
+
logger.error(line);
|
|
78
|
+
throw new FfprobeUnavailableError(line);
|
|
79
|
+
}
|
|
@@ -1,20 +1,12 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
-
import { createRequire } from "node:module";
|
|
3
|
-
import { accessSync, constants } from "node:fs";
|
|
4
2
|
import { ffprobeRecord } from "./container/ffprobe-record.js";
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
catch { return "ffprobe"; }
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/** Read only the strict available-byte endpoint; missing reads abort through ProbeRequests. */
|
|
17
|
-
export function probePackets({ url, statement, signal, onRecord = () => {}, ffprobeBin = bundledProbe(), spawnProcess = spawn }) {
|
|
4
|
+
/**
|
|
5
|
+
* Read only the strict available-byte endpoint; missing reads abort through ProbeRequests.
|
|
6
|
+
* `ffprobeBin` is the binary chosen once at startup (`ffprobe-selection.js`); this function never chooses one.
|
|
7
|
+
*/
|
|
8
|
+
export function probePackets({ url, statement, signal, onRecord = () => {}, ffprobeBin, spawnProcess = spawn }) {
|
|
9
|
+
if (typeof ffprobeBin !== "string" || ffprobeBin === "") throw new TypeError("probePackets needs the ffprobe binary chosen at startup.");
|
|
18
10
|
if (!["streams", "packets"].includes(statement)) throw new TypeError("Unknown ffprobe statement.");
|
|
19
11
|
if (signal?.aborted) return Promise.reject(signal.reason ?? new Error("Probe cancelled."));
|
|
20
12
|
onRecord({ kind: "scan-start" });
|