@torrent-tv/proxy 2.73.1 → 2.74.1

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 (34) hide show
  1. package/CHANGELOG.md +1453 -1437
  2. package/CLAUDE.md +165 -160
  3. package/docs/container-architecture.md +192 -184
  4. package/package.json +1 -1
  5. package/routes/api/subtitles/get.js +205 -205
  6. package/services/container/Container.js +400 -135
  7. package/services/container/ContainerFactory.js +55 -31
  8. package/services/container/MatroskaContainer.js +1166 -516
  9. package/services/container/Mp4Container.js +898 -392
  10. package/services/container/SubtitleFileContainer.js +323 -261
  11. package/services/controllers/SubtitleController.js +128 -127
  12. package/services/delivery-probe.js +64 -6
  13. package/services/hls-session-manager.js +32 -35
  14. package/services/language-detect.js +174 -228
  15. package/services/playback-planner.js +747 -747
  16. package/services/produced-index.js +300 -0
  17. package/services/torrent-worker/subtitle-cues.js +549 -633
  18. package/services/tracks/TextSubtitleTrack.js +287 -47
  19. package/services/tracks/index.js +14 -14
  20. package/test/delivery-probe.test.js +67 -0
  21. package/test/matroska-blocks.test.js +0 -0
  22. package/test/mp4-subtitles.test.js +173 -127
  23. package/test/produced-index.test.js +188 -0
  24. package/test/subtitle-cue-framing.test.js +200 -202
  25. package/test/subtitle-cue-walk.test.js +369 -0
  26. package/test/subtitle-defaults.test.js +97 -97
  27. package/test/subtitle-language.test.js +252 -252
  28. package/test/subtitle-track-numbering.test.js +370 -370
  29. package/services/container-index/matroska-blocks.js +0 -202
  30. package/services/container-index/matroska-subtitles.js +0 -372
  31. package/services/container-index/mp4-subtitles.js +0 -404
  32. package/services/subtitle-convert.js +0 -144
  33. package/services/subtitle-defaults.js +0 -157
  34. package/services/tracks/subtitle-markup.js +0 -104
@@ -1,747 +1,747 @@
1
- /**
2
- * @file Playback planner service.
3
- *
4
- * Determines whether a torrent file can be served directly or requires
5
- * HLS audio transcoding by probing the stream codecs with ffmpeg.
6
- * Results are cached indefinitely (keyed by source + file index).
7
- */
8
-
9
- import { spawn } from "node:child_process";
10
- import { logger } from "../utils/logger.js";
11
- import { mergeContainerSubtitleFlags } from "./subtitle-defaults.js";
12
- import { buildAudioInventory, mergeContainerAudioFlags } from "./audio-inventory.js";
13
- import { countVideoFiles, matchSidecarFiles } from "./sidecar-files.js";
14
- import {
15
- parseFfmpegDurationSeconds,
16
- parseFfmpegStartTimeSeconds,
17
- parseFfmpegVideoDimensions,
18
- parseFfmpegBitDepth,
19
- parseFfmpegBitrateKbps,
20
- parseFfmpegVideoFps,
21
- parseFfmpegHdr
22
- } from "./ffmpeg-banner.js";
23
-
24
- /** Audio codecs that browsers can decode natively without transcoding. */
25
- const DIRECT_AUDIO_CODECS = new Set(["aac", "mp3", "opus", "vorbis", "flac"]);
26
-
27
- // Once the plan probe succeeds, warm the START of the file body so the
28
- // transcode session's ffmpeg reads hit downloaded data instead of paying
29
- // piece latency at encode time (the edge prefetch only covers head+tail for
30
- // the codec probe). ~16 MB ≈ the first segments of typical media.
31
- const BODY_PREFETCH_BYTES = 16 * 1024 * 1024;
32
-
33
- /**
34
- * How long the plan waits for a file's own header before offering its
35
- * soundtrack without what that header would have said.
36
- *
37
- * Not a measurement, and nothing is derived from it: it is the point past which
38
- * holding the viewer costs more than the language and flags being waited for —
39
- * which the folder name supplies anyway, from the torrent's file list, at no
40
- * cost. The reading itself carries on in the worker and is kept there.
41
- */
42
- const SIDECAR_HEADER_WAIT_MS = 3_000;
43
-
44
- /** Subtitle codecs that can be converted to WebVTT (text-based). */
45
- const TEXT_SUBTITLE_CODECS = new Set(["subrip", "srt", "ass", "ssa", "webvtt", "vtt", "mov_text", "text"]);
46
-
47
- /**
48
- * Parse every stream from the ffmpeg `-i` banner: type, codec, language tag,
49
- * default disposition and (when present) the stream's `title` metadata line.
50
- *
51
- * @param {string} ffmpegOutput
52
- * @returns {Array<{ streamIndex: number, type: string, codec: string, language: string, title: string, isDefault: boolean }>}
53
- */
54
- function parseStreams(ffmpegOutput) {
55
- // Only the Input section: ffmpeg prints Stream lines for the null OUTPUT
56
- // too (wrapped_avframe / pcm_s16le), which would duplicate every track.
57
- const inputSection = ffmpegOutput.split(/^(?:Output #|Stream mapping:)/m)[0] ?? ffmpegOutput;
58
- const lines = inputSection.split(/\r?\n/);
59
- const streams = [];
60
- let current = null;
61
- for (const line of lines) {
62
- const streamMatch = line.match(
63
- /^\s*Stream #0:(\d+)(?:\[[^\]]*\])?(?:\(([A-Za-z0-9]{2,3})\))?: (Audio|Video|Subtitle): ([A-Za-z0-9_]+)/
64
- );
65
- if (streamMatch) {
66
- current = {
67
- streamIndex: Number(streamMatch[1]),
68
- type: streamMatch[3].toLowerCase(),
69
- codec: String(streamMatch[4]).toLowerCase(),
70
- language: (streamMatch[2] ?? "").toLowerCase(),
71
- title: "",
72
- isDefault: /\(default\)/.test(line)
73
- };
74
- streams.push(current);
75
- continue;
76
- }
77
- if (current) {
78
- const titleMatch = line.match(/^\s+title\s*:\s*(.+)$/);
79
- if (titleMatch && current.title.length === 0) {
80
- current.title = titleMatch[1].trim();
81
- continue;
82
- }
83
- // A new top-level section (non-indented line) ends the stream's block.
84
- if (!/^\s/.test(line)) {
85
- current = null;
86
- }
87
- }
88
- }
89
- return streams;
90
- }
91
-
92
- /**
93
- * Parse audio and video codec names from ffmpeg stderr output.
94
- *
95
- * @param {string} ffmpegOutput
96
- * @returns {{ audioCodec: string, videoCodec: string }}
97
- */
98
- function parseStreamCodecs(ffmpegOutput) {
99
- const audioMatch = ffmpegOutput.match(/Audio:\s*([A-Za-z0-9_]+)/i);
100
- const videoMatch = ffmpegOutput.match(/Video:\s*([A-Za-z0-9_]+)/i);
101
- // Coded resolution from the video Stream line ("Video: h264 …, 1280x720, …").
102
- // The first WxH is the coded size (any trailing "[SAR …]" is ignored).
103
- const videoLineMatch = ffmpegOutput.match(/Video:[^\n]*/i);
104
- let videoWidth = 0;
105
- let videoHeight = 0;
106
- if (videoLineMatch) {
107
- const dim = videoLineMatch[0].match(/\b(\d{2,5})x(\d{2,5})\b/);
108
- if (dim) {
109
- videoWidth = Number(dim[1]);
110
- videoHeight = Number(dim[2]);
111
- }
112
- }
113
- const containerMatch = ffmpegOutput.match(/Input #0,\s*([^,]+(?:,[^,]+)*?),\s*from/i);
114
- const durationMatch = ffmpegOutput.match(/Duration:\s*(\d+):(\d+):(\d+(?:\.\d+)?)/i);
115
- let durationSeconds = 0;
116
- if (durationMatch) {
117
- const value =
118
- Number(durationMatch[1]) * 3600 + Number(durationMatch[2]) * 60 + Number(durationMatch[3]);
119
- durationSeconds = Number.isFinite(value) ? value : 0;
120
- }
121
- const streams = parseStreams(ffmpegOutput);
122
- const audioTracks = streams
123
- .filter((s) => s.type === "audio")
124
- .map((s, i) => ({
125
- // Type-relative index — what ffmpeg's `-map 0:a:N` selects.
126
- index: i,
127
- streamIndex: s.streamIndex,
128
- codec: s.codec,
129
- language: s.language,
130
- title: s.title,
131
- isDefault: s.isDefault
132
- }));
133
- const subtitleTracks = streams
134
- .filter((s) => s.type === "subtitle")
135
- .map((s, i) => ({
136
- // Type-relative index — what ffmpeg's `-map 0:s:N` selects.
137
- index: i,
138
- streamIndex: s.streamIndex,
139
- codec: s.codec,
140
- language: s.language,
141
- title: s.title,
142
- isDefault: s.isDefault,
143
- // Image-based subtitles (PGS/VobSub) cannot become WebVTT.
144
- textBased: TEXT_SUBTITLE_CODECS.has(s.codec)
145
- }));
146
- return {
147
- audioCodec: audioMatch ? String(audioMatch[1]).toLowerCase() : "",
148
- videoCodec: videoMatch ? String(videoMatch[1]).toLowerCase() : "",
149
- container: containerMatch ? String(containerMatch[1]).trim().toLowerCase() : "",
150
- durationSeconds,
151
- videoWidth,
152
- videoHeight,
153
- audioTracks,
154
- subtitleTracks
155
- };
156
- }
157
-
158
- /**
159
- * Run a brief ffmpeg probe to identify the audio and video codecs of a stream.
160
- * Times out after `timeoutMs` and returns empty strings on failure.
161
- *
162
- * @param {object} options
163
- * @param {string} options.ffmpegBin
164
- * @param {string} options.inputUrl
165
- * @param {string} [options.userAgent=""]
166
- * @param {number} [options.timeoutMs=8000]
167
- * @returns {Promise<{ audioCodec: string, videoCodec: string, container: string, durationSeconds: number, videoWidth: number, videoHeight: number, audioTracks: object[], subtitleTracks: object[], stderr: string }>}
168
- * Parsed banner fields plus the raw `stderr`, so the caller can derive the
169
- * full media info (fps/startTime/HDR) without a second ffmpeg scan.
170
- */
171
- function probeStreamCodecs({ ffmpegBin, inputUrl, userAgent = "", timeoutMs = 8_000 }) {
172
- return new Promise((resolve) => {
173
- const args = ["-hide_banner", "-loglevel", "info"];
174
- if (typeof userAgent === "string" && userAgent.trim().length > 0) {
175
- args.push("-user_agent", userAgent.trim());
176
- }
177
- // Decode a tiny slice of all streams (no per-stream -map, so video-only
178
- // files probe correctly too). The ffmpeg banner that precedes decoding
179
- // gives us audio/video codecs, the container format and the duration in a
180
- // single pass.
181
- args.push("-i", inputUrl, "-t", "0.1", "-f", "null", "-");
182
-
183
- const ffmpeg = spawn(ffmpegBin, args, {
184
- stdio: ["ignore", "ignore", "pipe"],
185
- windowsHide: true
186
- });
187
- let stderr = "";
188
- let settled = false;
189
-
190
- const finish = (codecs) => {
191
- if (settled) {
192
- return;
193
- }
194
- settled = true;
195
- resolve(codecs);
196
- };
197
-
198
- const timeoutId = setTimeout(() => {
199
- if (!ffmpeg.killed) {
200
- ffmpeg.kill("SIGTERM");
201
- }
202
- finish({ ...parseStreamCodecs(stderr), stderr });
203
- }, timeoutMs);
204
-
205
- ffmpeg.stderr.on("data", (chunk) => {
206
- stderr += String(chunk);
207
- });
208
-
209
- ffmpeg.on("error", () => {
210
- clearTimeout(timeoutId);
211
- finish({ audioCodec: "", videoCodec: "", stderr: "" });
212
- });
213
-
214
- ffmpeg.on("exit", () => {
215
- clearTimeout(timeoutId);
216
- finish({ ...parseStreamCodecs(stderr), stderr });
217
- });
218
- });
219
- }
220
-
221
- /**
222
- * Resolve after a given number of milliseconds.
223
- *
224
- * @param {number} ms
225
- * @returns {Promise<void>}
226
- */
227
- function delay(ms) {
228
- return new Promise((resolve) => {
229
- setTimeout(resolve, ms);
230
- });
231
- }
232
-
233
- /**
234
- * Build the direct stream URL for a source file served by the local proxy.
235
- *
236
- * @param {string} localBaseUrl - e.g. "http://127.0.0.1:9090"
237
- * @param {string} sourceKey
238
- * @param {number} fileIndex
239
- * @returns {string}
240
- */
241
- function buildDirectUrl(localBaseUrl, sourceKey, fileIndex) {
242
- const directUrl = new URL("/stream", `${localBaseUrl}/`);
243
- directUrl.searchParams.set("sourceKey", sourceKey);
244
- directUrl.searchParams.set("fileIndex", String(fileIndex));
245
- return directUrl.toString();
246
- }
247
-
248
- /**
249
- * @typedef {Object} PlaybackPlan
250
- * @property {"direct" | "hls"} mode
251
- * @property {string} directUrl
252
- * @property {string} reason - Human-readable explanation of the chosen mode.
253
- * @property {string} audioCodec
254
- * @property {string} videoCodec
255
- * @property {string} container - Demuxer/container name(s) reported by ffmpeg.
256
- * @property {number} durationSeconds - Total media duration in seconds (0 if unknown).
257
- * @property {number} videoWidth - Source coded width (0 if unknown).
258
- * @property {number} videoHeight - Source coded height (0 if unknown).
259
- */
260
-
261
- /**
262
- * @typedef {Object} PlaybackPlannerOptions
263
- * @property {string} ffmpegBin
264
- * @property {boolean} transcodeAudioEnabled
265
- * @property {string} localBaseUrl
266
- * @property {ReturnType<import("../store/source-registry.js").createSourceRegistry>} sourceRegistry
267
- * @property {import("./torrent-pool.js").TorrentPool} torrentPool
268
- */
269
-
270
- /**
271
- * Create a playback planner that decides the optimal streaming mode for
272
- * a torrent file. Plans are cached per (sourceKey, fileIndex) pair.
273
- *
274
- * @param {PlaybackPlannerOptions} options
275
- * @returns {{ getPlan: (params: { sourceKey: string, fileIndex: number, userAgent?: string }) => Promise<PlaybackPlan> }}
276
- */
277
- export function createPlaybackPlanner({
278
- ffmpegBin,
279
- transcodeAudioEnabled,
280
- localBaseUrl,
281
- sourceRegistry,
282
- torrentPool,
283
- // Optional. Reports what this host typically takes to produce a session's
284
- // first segment. The browser needs it for the gap between "the file is
285
- // downloaded" and "a segment exists": until now it assumed the pipeline
286
- // merely keeps up with realtime, and showed 15 s where 3.8 s were left.
287
- expectedFirstSegmentMs,
288
- expectedSessionCreateMs,
289
- // Optional. Called once the file's edges are downloaded, so the keyframe
290
- // index — which reads the same tail of the file — is fetched alongside the
291
- // codec probe instead of after it. Late-bound to the HLS session manager,
292
- // which owns the cache both of them share.
293
- warmKeyframeIndex,
294
- // Optional. The heights this host could actually serve this source at, for
295
- // both playback branches, so the quality menu is right from the moment the
296
- // file is opened rather than from the moment an encoder exists.
297
- predictOfferedHeights
298
- }) {
299
- /** @type {Map<string, PlaybackPlan>} */
300
- const cache = new Map();
301
- /**
302
- * Full media info parsed from the SAME probe that produced the plan, cached
303
- * under the same key so a transcode session can reuse it instead of running
304
- * a second ffmpeg scan. Only set when the plan is cached (codecs detected).
305
- * @type {Map<string, { durationSeconds: number | null, width: number | null, height: number | null, fps: number | null, startTime: number, isHdr: boolean }>}
306
- */
307
- const mediaInfoCache = new Map();
308
-
309
- /**
310
- * Attach what this host currently measures itself taking to create a session
311
- * and to produce a first segment.
312
- *
313
- * Read at RESPONSE time, deliberately. Both are medians of sessions that have
314
- * already finished on this host, so at the moment a plan is BUILT the very
315
- * first file opened after a restart has none and gets `null` — and the plan
316
- * is then cached, so that file kept answering `null` for the life of the
317
- * process however many sessions ran afterwards. Measured 2026-08-05: a fresh
318
- * 2.9.103 answered `null` for both, then produced the session in 6 ms and the
319
- * first segment in 21 479 ms. The figures existed; the plan could not carry
320
- * them, and the browser's estimate fell back to its own guess in exactly the
321
- * cold-start case the feature was built for.
322
- *
323
- * @param {PlaybackPlan} plan
324
- * @returns {PlaybackPlan}
325
- */
326
- /**
327
- * The probe's subtitle tracks, with `FlagDefault` read from the container
328
- * instead of inferred from ffmpeg's banner.
329
- *
330
- * Best-effort by construction: a container that cannot be read this way, or a
331
- * reading that does not line up with the probe, leaves the tracks as they
332
- * were with `declaresDefault: false` — which the browser reads as "the file
333
- * has no opinion", and then nothing is shown unasked.
334
- *
335
- * @param {object} torrent
336
- * @param {number} fileIndex
337
- * @param {object[]} subtitleTracks
338
- * @returns {Promise<object[]>}
339
- */
340
- async function withContainerDefaults(torrent, fileIndex, subtitleTracks) {
341
- if (subtitleTracks.length === 0 || typeof torrentPool?.getDeclaredSubtitleTracks !== "function") {
342
- return subtitleTracks.map((track) => ({ ...track, declaresDefault: false }));
343
- }
344
- let declared = [];
345
- try {
346
- declared = await torrentPool.getDeclaredSubtitleTracks(torrent, fileIndex);
347
- } catch (error) {
348
- logger.info(`subtitle defaults: the container could not be read (${error?.message ?? error})`);
349
- }
350
- const merged = mergeContainerSubtitleFlags(subtitleTracks, declared);
351
- logger.info(
352
- merged.aligned
353
- ? "subtitle defaults: the container wrote FlagDefault on " +
354
- `${merged.tracks.filter((track) => track.declaresDefault).length} of ${merged.tracks.length} ` +
355
- `subtitle tracks, marking ${merged.tracks.filter((track) => track.declaresDefault && track.isDefault).length}`
356
- : `subtitle defaults: using the probe's own flags — ${merged.reason}`
357
- );
358
- return merged.tracks;
359
- }
360
-
361
- /**
362
- * Every soundtrack this file can be watched with, as one numbered list: its
363
- * own tracks and the ones shipped as separate files beside it.
364
- *
365
- * Built here, in the plan, because the plan is what the viewer's menu is drawn
366
- * from — so the offer is complete the moment a file is opened, with nothing
367
- * arriving late and nothing measured while the viewer waits. It is also what
368
- * the master playlist's rendition group is built from, so the number in the
369
- * menu and the number in the `a/<n>/` address are the same number by
370
- * construction rather than by agreement.
371
- *
372
- * @param {object} torrent
373
- * @param {number} fileIndex
374
- * @param {object[]} bannerAudioTracks - The probe's own audio streams.
375
- * @returns {Promise<import("./audio-inventory.js").AudioInventoryEntry[]>}
376
- */
377
- async function buildInventory(torrent, fileIndex, bannerAudioTracks) {
378
- const banner = Array.isArray(bannerAudioTracks) ? bannerAudioTracks : [];
379
- /**
380
- * Read a file's declared audio tracks, or give up quickly.
381
- *
382
- * The plan is on the path to the first frame, and reading a sidecar's header
383
- * waits on the swarm: that file has usually had nothing downloaded when this
384
- * runs, and a header that never arrives would hold the plan — and the
385
- * viewer — for the whole of the read's own patience. What a timeout costs is
386
- * small and deliberate: the track is still offered, still numbered and still
387
- * playable, only without the language and flags its own header would have
388
- * given. The language the viewer actually sees is read from the FOLDER the
389
- * release put it in, which is in the torrent's file list and needs no bytes
390
- * at all.
391
- *
392
- * @param {number} wantedFileIndex
393
- * @param {string} label
394
- * @returns {Promise<object[]>}
395
- */
396
- const declaredAudioOf = async (wantedFileIndex, label) => {
397
- if (typeof torrentPool?.getDeclaredAudioTracks !== "function") {
398
- return [];
399
- }
400
- let timer = null;
401
- try {
402
- return await Promise.race([
403
- torrentPool.getDeclaredAudioTracks(torrent, wantedFileIndex),
404
- new Promise((resolve) => {
405
- timer = setTimeout(() => resolve(null), SIDECAR_HEADER_WAIT_MS);
406
- timer.unref?.();
407
- })
408
- ]).then((tracks) => {
409
- if (tracks === null) {
410
- logger.info(
411
- `audio tracks: "${label}" did not answer within ` +
412
- `${SIDECAR_HEADER_WAIT_MS / 1000}s — offered without what its header would say`
413
- );
414
- return [];
415
- }
416
- return Array.isArray(tracks) ? tracks : [];
417
- });
418
- } catch (error) {
419
- logger.info(`audio tracks: "${label}" could not be read (${error?.message ?? error})`);
420
- return [];
421
- } finally {
422
- if (timer !== null) {
423
- clearTimeout(timer);
424
- }
425
- }
426
- };
427
- // The picture's own tracks: ffmpeg numbers them, the container declares what
428
- // they are. Both readings, lined up and checked — see `audio-inventory.js`.
429
- let embedded = banner.map((track) => ({ ...track, declaresDefault: false }));
430
- if (banner.length > 0) {
431
- // The picture's head is already downloaded — the codec probe just read it
432
- // — so this is a parse and not a wait, but it is bounded like the rest.
433
- const declared = await declaredAudioOf(fileIndex, "the picture");
434
- const merged = mergeContainerAudioFlags(banner, declared);
435
- embedded = merged.tracks;
436
- logger.info(
437
- merged.aligned
438
- ? `audio tracks: the container describes all ${merged.tracks.length}` +
439
- `${merged.tracks.some((track) => track.isCommentary) ? ", one of them commentary" : ""}` +
440
- `${merged.tracks.some((track) => track.isVisualImpaired) ? ", one of them described" : ""}`
441
- : `audio tracks: using the probe's own fields — ${merged.reason}`
442
- );
443
- }
444
-
445
- const sidecarFiles = matchSidecarFiles({
446
- files: torrent?.files ?? [],
447
- videoIndex: fileIndex,
448
- torrentName: typeof torrent?.name === "string" ? torrent.name : "",
449
- videoCount: countVideoFiles(torrent?.files ?? [])
450
- });
451
- // All of them at once. They are separate files with separate headers, and
452
- // read one after another the waits add up on the path to the first frame.
453
- const sidecars = await Promise.all(
454
- sidecarFiles.audio.map(async (file) => ({
455
- file,
456
- // A bare elementary stream — `.ac3`, `.dts`, `.mp3` — has no table to
457
- // read, so nothing is asked of the swarm for it at all.
458
- tracks: file.declaresTracks ? await declaredAudioOf(file.fileIndex, file.name) : []
459
- }))
460
- );
461
- const inventory = buildAudioInventory({ embedded, videoFileIndex: fileIndex, sidecars });
462
- if (sidecars.length > 0) {
463
- logger.info(
464
- `audio tracks: ${sidecars.length} file(s) beside the picture carry sound — ` +
465
- inventory
466
- .filter((entry) => entry.kind === "sidecar")
467
- .map((entry) =>
468
- `a:${entry.index}=${entry.folders.join("/") || "."}/${entry.fileName}` +
469
- `#${entry.sourceTrackIndex}${entry.codec ? `(${entry.codec})` : ""}`
470
- )
471
- .join(" ")
472
- );
473
- }
474
- return inventory;
475
- }
476
-
477
- function withHostTimings(plan) {
478
- const withOffer = {
479
- ...plan,
480
- expectedFirstSegmentMs: expectedFirstSegmentMs?.() ?? null,
481
- expectedSessionCreateMs: expectedSessionCreateMs?.() ?? null,
482
- // Answered here for the same reason as the two above: a plan is cached for
483
- // the life of the process, and what this host will serve a file at is not.
484
- // It starts as a prediction from the startup benchmarks and is replaced by
485
- // what an encoder running on this very source turns out to cost — frozen
486
- // into the cache, every later open of the file would hand the browser the
487
- // first guess again and undo that. This is the 2.9.106 defect exactly.
488
- offeredHeights: plan.mediaInfoForOffer
489
- ? (predictOfferedHeights?.(plan.mediaInfoForOffer) ?? null)
490
- : null,
491
- mediaInfoForOffer: undefined
492
- };
493
- // Refused rather than served badly. Both lists empty means this machine
494
- // cannot sustain this file at ANY height — not even by copying the picture,
495
- // which costs no encoder at all — so a session made here would produce a
496
- // slideshow and take the swarm and the processor from whoever is already
497
- // watching. Field 2026-08-28: five sessions on one file put every rung at
498
- // 0.04x of realtime and the viewer watched one before the process was
499
- // killed. The viewer is told why, which is a different thing from a spinner
500
- // that never ends.
501
- const offer = withOffer.offeredHeights;
502
- if (offer && offer.copy.length === 0 && offer.transcode.length === 0) {
503
- withOffer.cannotServe =
504
- "This proxy cannot keep up with this file at any quality right now.";
505
- // The description travels with the refusal, and only with it. It is what
506
- // lets the browser ask the rest of the pool the same question without
507
- // anybody else adding the torrent, fetching a byte or running ffmpeg —
508
- // the expensive half of finding out what this file IS has been paid here,
509
- // once. Everyone else answers by arithmetic against their own startup
510
- // benchmarks.
511
- withOffer.mediaInfoForOffer = plan.mediaInfoForOffer;
512
- }
513
- return withOffer;
514
- }
515
-
516
- return {
517
- /**
518
- * Media info the planner already probed for this file, or `null`. Lets the
519
- * HLS session manager skip its own duplicate `probeInputMediaInfo` scan.
520
- *
521
- * @param {{ sourceKey: string, fileIndex: number }} params
522
- * @returns {{ durationSeconds: number | null, width: number | null, height: number | null, fps: number | null, startTime: number, isHdr: boolean } | null}
523
- */
524
- /**
525
- * The audio tracks this file was probed to have, or an empty list. The
526
- * master playlist publishes one rendition per track, and the inventory is
527
- * already here — probing again for it would be a second scan of a file the
528
- * proxy is in the middle of serving.
529
- *
530
- * @param {{ sourceKey: string, fileIndex: number }} params
531
- * @returns {object[]}
532
- */
533
- getCachedAudioTracks({ sourceKey, fileIndex }) {
534
- const plan = cache.get(`${sourceKey}:${fileIndex}`);
535
- return Array.isArray(plan?.audioTracks) ? plan.audioTracks : [];
536
- },
537
-
538
- getCachedMediaInfo({ sourceKey, fileIndex }) {
539
- return mediaInfoCache.get(`${sourceKey}:${fileIndex}`) ?? null;
540
- },
541
-
542
- /**
543
- * Return the playback plan for the given source file.
544
- * Throws with `error.code === "SOURCE_NOT_FOUND"` or `"FILE_NOT_FOUND"`
545
- * when the source or file cannot be located.
546
- *
547
- * When the file header has not downloaded yet (cold torrent, peers still
548
- * connecting) the codec probe cannot succeed. Rather than block the HTTP
549
- * response until it can, the planner prioritises the header, probes for at
550
- * most `maxWaitMs`, and if still undetectable returns a plan flagged
551
- * `pending: true` (NOT cached). The caller polls again — each call keeps the
552
- * header prioritised and downloading — until a real plan comes back. This
553
- * avoids a single long request racing the transport's request timeout.
554
- *
555
- * @param {object} params
556
- * @param {string} params.sourceKey
557
- * @param {number} params.fileIndex
558
- * @param {string} [params.userAgent=""]
559
- * @param {number} [params.maxWaitMs=60000] - Max time to wait for the header within ONE call.
560
- * @returns {Promise<PlaybackPlan & { pending?: boolean }>}
561
- */
562
- async getPlan({ sourceKey, fileIndex, userAgent = "", maxWaitMs = 60_000 }) {
563
- const cacheKey = `${sourceKey}:${fileIndex}`;
564
- const cached = cache.get(cacheKey);
565
- if (cached) {
566
- return withHostTimings(cached);
567
- }
568
- // Where the time before playback goes. `cold-start` already breaks down
569
- // everything from the transcode-session request onwards, but the plan
570
- // runs BEFORE that and was a single opaque wait: a field session spent
571
- // 5.7 s here on a torrent already in the store, with the codec probe
572
- // cached, and nothing said which part of it was slow.
573
- const planEntryMs = Date.now();
574
- let torrentReadyMs = 0;
575
- let edgesReadyMs = 0;
576
-
577
- const sourceRecord = sourceRegistry.get(sourceKey);
578
- if (!sourceRecord) {
579
- const error = new Error("Source key was not found.");
580
- error.code = "SOURCE_NOT_FOUND";
581
- throw error;
582
- }
583
-
584
- const torrent = await torrentPool.getTorrent(sourceRecord.sourceType, sourceRecord.source);
585
- torrentReadyMs = Date.now() - planEntryMs;
586
- const file = torrent.files[fileIndex];
587
- if (!file) {
588
- const error = new Error("File index was not found in torrent.");
589
- error.code = "FILE_NOT_FOUND";
590
- throw error;
591
- }
592
-
593
- const directUrl = buildDirectUrl(localBaseUrl, sourceKey, fileIndex);
594
- if (!transcodeAudioEnabled) {
595
- const plan = {
596
- mode: "direct",
597
- directUrl,
598
- reason: "transcode-disabled",
599
- audioCodec: "",
600
- videoCodec: "",
601
- container: "",
602
- durationSeconds: 0,
603
- videoWidth: 0,
604
- videoHeight: 0,
605
- audioTracks: [],
606
- subtitleTracks: []
607
- };
608
- cache.set(cacheKey, plan);
609
- return withHostTimings(plan);
610
- }
611
-
612
- // Pre-fetch file edges (head + tail), then probe — retrying while the
613
- // file header is still downloading. In a multi-file torrent the pieces
614
- // for a given file arrive unevenly, so the first probe can return empty
615
- // codecs. A transient empty probe must NOT be cached: otherwise the wrong
616
- // plan (file treated as directly playable) sticks permanently for this
617
- // file, and an unsupported codec like xvid gets copied → black video.
618
- await torrentPool.prefetchFileEdges(torrent, fileIndex);
619
- edgesReadyMs = Date.now() - planEntryMs;
620
- // The keyframe index reads the tail of the file, which the probe has just
621
- // waited for as well. Started here it overlaps the probe instead of
622
- // following the whole plan — worth 311-430 ms of the time before the
623
- // first segment. Fire and forget: the session reads it itself if this has
624
- // not finished, and both share one cache entry.
625
- warmKeyframeIndex?.({
626
- sourceKey,
627
- fileIndex,
628
- inputUrl: new URL(directUrl),
629
- logName: file.name
630
- });
631
- let probe = await probeStreamCodecs({ ffmpegBin, inputUrl: directUrl, userAgent });
632
- const probeDeadline = Date.now() + Math.max(0, maxWaitMs);
633
- let attempt = 0;
634
- while (
635
- probe.audioCodec.length === 0 &&
636
- probe.videoCodec.length === 0 &&
637
- Date.now() < probeDeadline
638
- ) {
639
- attempt += 1;
640
- await delay(Math.min(3_000, 500 + attempt * 250));
641
- await torrentPool.prefetchFileEdges(torrent, fileIndex);
642
- probe = await probeStreamCodecs({ ffmpegBin, inputUrl: directUrl, userAgent });
643
- }
644
- const { audioCodec, videoCodec, container, durationSeconds, videoWidth, videoHeight, audioTracks, subtitleTracks } = probe;
645
- const codecsDetected = audioCodec.length > 0 || videoCodec.length > 0;
646
- logger.info(
647
- `plan ${sourceKey.slice(0, 8)}:${fileIndex} torrent-ready=${torrentReadyMs}ms ` +
648
- `file-edges=${edgesReadyMs - torrentReadyMs}ms probe=${Date.now() - planEntryMs - edgesReadyMs}ms ` +
649
- `total=${Date.now() - planEntryMs}ms attempts=${attempt + 1} ` +
650
- `${codecsDetected ? `${videoCodec || "-"}/${audioCodec || "-"}` : "codecs NOT detected (will be polled again)"}`
651
- );
652
-
653
- // `mode` is advisory only (audio-codec based). The browser makes the
654
- // authoritative decision independently per stream via canPlayType /
655
- // mediaCapabilities, transcoding only what it cannot play.
656
- const requiresTranscode = audioCodec.length > 0 && !DIRECT_AUDIO_CODECS.has(audioCodec);
657
- const plan = {
658
- mode: requiresTranscode ? "hls" : "direct",
659
- directUrl,
660
- reason: requiresTranscode ? "audio-codec-transcode-required" : "audio-codec-supported",
661
- audioCodec,
662
- videoCodec,
663
- container,
664
- durationSeconds,
665
- // Source coded resolution — drives the browser's manual quality menu
666
- // (list of forced resolutions <= source). 0 when unknown.
667
- videoWidth,
668
- videoHeight,
669
- // Full track inventory for the browser's audio/subtitle menus. The audio
670
- // half spans the picture's own tracks AND the soundtracks shipped as
671
- // files beside it, under one numbering — see `buildInventory`.
672
- audioTracks: await buildInventory(torrent, fileIndex, audioTracks ?? []),
673
- subtitleTracks: await withContainerDefaults(torrent, fileIndex, subtitleTracks ?? []),
674
- // Both host timings are filled in by `withHostTimings` on the way out,
675
- // never here: read at build time they would be frozen into the cached
676
- // plan, which is the bug fixed in 2.9.106.
677
- expectedFirstSegmentMs: null,
678
- expectedSessionCreateMs: null,
679
- offeredHeights: null,
680
- // What the offer is computed FROM, kept on the cached plan so the offer
681
- // itself can be recomputed on every response. The figures are the
682
- // probe's own and never change for a file; the answer derived from them
683
- // does, as the host learns what this source costs. Stripped on the way
684
- // out — it is not part of the plan the browser is given.
685
- mediaInfoForOffer: {
686
- width: videoWidth,
687
- height: videoHeight,
688
- fps: parseFfmpegVideoFps(probe.stderr),
689
- bitrateKbps: parseFfmpegBitrateKbps(probe.stderr),
690
- // Which family of the decode measurement prices this source. A video
691
- // that has to be re-encoded is one the browser could not play, so it
692
- // is usually NOT H.264, and H.264 constants are wrong for it.
693
- codec: videoCodec,
694
- bitDepth: parseFfmpegBitDepth(probe.stderr),
695
- // Which file this is, so the offer can be answered from what an
696
- // encoder has already learned about THIS source rather than from the
697
- // startup clips — the same correction a live session applies.
698
- sourceKey,
699
- fileIndex
700
- }
701
- };
702
- // Only cache a plan whose codecs were actually detected. An empty probe is
703
- // a "header not downloaded yet" signal, not a valid result — caching it
704
- // would permanently mis-plan the file. In that case flag the plan
705
- // `pending` so the caller polls again (the header keeps downloading,
706
- // prioritised by the prefetch above).
707
- if (codecsDetected) {
708
- cache.set(cacheKey, plan);
709
- // Cache the full media info from THIS probe's banner (same helpers the
710
- // session manager uses) so createSession can skip its own probe.
711
- const dims = parseFfmpegVideoDimensions(probe.stderr);
712
- mediaInfoCache.set(cacheKey, {
713
- // The codecs, because the session manager asks this cache which
714
- // tracks the output will carry — and they were never stored here. It
715
- // read `videoCodec`/`audioCodec` off an object that has only ever had
716
- // dimensions and duration, got `undefined` for both, and declared
717
- // `{video: false, audio: false}` for EVERY session since the check was
718
- // written. Measured 2026-08-11: `declared tracks video=false
719
- // audio=false`, which left the browser unable to tell "this file has
720
- // no video" from "the video was lost on the way", and left the init
721
- // guard expecting zero tracks and therefore accepting any header.
722
- videoCodec: plan.videoCodec,
723
- audioCodec: plan.audioCodec,
724
- durationSeconds: parseFfmpegDurationSeconds(probe.stderr),
725
- width: dims.width,
726
- height: dims.height,
727
- bitrateKbps: parseFfmpegBitrateKbps(probe.stderr),
728
- fps: parseFfmpegVideoFps(probe.stderr),
729
- startTime: parseFfmpegStartTimeSeconds(probe.stderr),
730
- isHdr: parseFfmpegHdr(probe.stderr),
731
- bitDepth: parseFfmpegBitDepth(probe.stderr)
732
- });
733
- // Warm the file-body start for the transcode session that follows.
734
- // Fire-and-forget: never delays the plan response.
735
- void torrentPool
736
- .prefetchFileEdges(torrent, fileIndex, {
737
- headBytes: BODY_PREFETCH_BYTES,
738
- tailBytes: 0,
739
- timeoutMs: 60_000
740
- })
741
- .catch(() => {});
742
- return withHostTimings(plan);
743
- }
744
- return withHostTimings({ ...plan, pending: true });
745
- }
746
- };
747
- }
1
+ /**
2
+ * @file Playback planner service.
3
+ *
4
+ * Determines whether a torrent file can be served directly or requires
5
+ * HLS audio transcoding by probing the stream codecs with ffmpeg.
6
+ * Results are cached indefinitely (keyed by source + file index).
7
+ */
8
+
9
+ import { spawn } from "node:child_process";
10
+ import { logger } from "../utils/logger.js";
11
+ import { Container } from "./container/Container.js";
12
+ import { buildAudioInventory, mergeContainerAudioFlags } from "./audio-inventory.js";
13
+ import { countVideoFiles, matchSidecarFiles } from "./sidecar-files.js";
14
+ import {
15
+ parseFfmpegDurationSeconds,
16
+ parseFfmpegStartTimeSeconds,
17
+ parseFfmpegVideoDimensions,
18
+ parseFfmpegBitDepth,
19
+ parseFfmpegBitrateKbps,
20
+ parseFfmpegVideoFps,
21
+ parseFfmpegHdr
22
+ } from "./ffmpeg-banner.js";
23
+
24
+ /** Audio codecs that browsers can decode natively without transcoding. */
25
+ const DIRECT_AUDIO_CODECS = new Set(["aac", "mp3", "opus", "vorbis", "flac"]);
26
+
27
+ // Once the plan probe succeeds, warm the START of the file body so the
28
+ // transcode session's ffmpeg reads hit downloaded data instead of paying
29
+ // piece latency at encode time (the edge prefetch only covers head+tail for
30
+ // the codec probe). ~16 MB ≈ the first segments of typical media.
31
+ const BODY_PREFETCH_BYTES = 16 * 1024 * 1024;
32
+
33
+ /**
34
+ * How long the plan waits for a file's own header before offering its
35
+ * soundtrack without what that header would have said.
36
+ *
37
+ * Not a measurement, and nothing is derived from it: it is the point past which
38
+ * holding the viewer costs more than the language and flags being waited for —
39
+ * which the folder name supplies anyway, from the torrent's file list, at no
40
+ * cost. The reading itself carries on in the worker and is kept there.
41
+ */
42
+ const SIDECAR_HEADER_WAIT_MS = 3_000;
43
+
44
+ /** Subtitle codecs that can be converted to WebVTT (text-based). */
45
+ const TEXT_SUBTITLE_CODECS = new Set(["subrip", "srt", "ass", "ssa", "webvtt", "vtt", "mov_text", "text"]);
46
+
47
+ /**
48
+ * Parse every stream from the ffmpeg `-i` banner: type, codec, language tag,
49
+ * default disposition and (when present) the stream's `title` metadata line.
50
+ *
51
+ * @param {string} ffmpegOutput
52
+ * @returns {Array<{ streamIndex: number, type: string, codec: string, language: string, title: string, isDefault: boolean }>}
53
+ */
54
+ function parseStreams(ffmpegOutput) {
55
+ // Only the Input section: ffmpeg prints Stream lines for the null OUTPUT
56
+ // too (wrapped_avframe / pcm_s16le), which would duplicate every track.
57
+ const inputSection = ffmpegOutput.split(/^(?:Output #|Stream mapping:)/m)[0] ?? ffmpegOutput;
58
+ const lines = inputSection.split(/\r?\n/);
59
+ const streams = [];
60
+ let current = null;
61
+ for (const line of lines) {
62
+ const streamMatch = line.match(
63
+ /^\s*Stream #0:(\d+)(?:\[[^\]]*\])?(?:\(([A-Za-z0-9]{2,3})\))?: (Audio|Video|Subtitle): ([A-Za-z0-9_]+)/
64
+ );
65
+ if (streamMatch) {
66
+ current = {
67
+ streamIndex: Number(streamMatch[1]),
68
+ type: streamMatch[3].toLowerCase(),
69
+ codec: String(streamMatch[4]).toLowerCase(),
70
+ language: (streamMatch[2] ?? "").toLowerCase(),
71
+ title: "",
72
+ isDefault: /\(default\)/.test(line)
73
+ };
74
+ streams.push(current);
75
+ continue;
76
+ }
77
+ if (current) {
78
+ const titleMatch = line.match(/^\s+title\s*:\s*(.+)$/);
79
+ if (titleMatch && current.title.length === 0) {
80
+ current.title = titleMatch[1].trim();
81
+ continue;
82
+ }
83
+ // A new top-level section (non-indented line) ends the stream's block.
84
+ if (!/^\s/.test(line)) {
85
+ current = null;
86
+ }
87
+ }
88
+ }
89
+ return streams;
90
+ }
91
+
92
+ /**
93
+ * Parse audio and video codec names from ffmpeg stderr output.
94
+ *
95
+ * @param {string} ffmpegOutput
96
+ * @returns {{ audioCodec: string, videoCodec: string }}
97
+ */
98
+ function parseStreamCodecs(ffmpegOutput) {
99
+ const audioMatch = ffmpegOutput.match(/Audio:\s*([A-Za-z0-9_]+)/i);
100
+ const videoMatch = ffmpegOutput.match(/Video:\s*([A-Za-z0-9_]+)/i);
101
+ // Coded resolution from the video Stream line ("Video: h264 …, 1280x720, …").
102
+ // The first WxH is the coded size (any trailing "[SAR …]" is ignored).
103
+ const videoLineMatch = ffmpegOutput.match(/Video:[^\n]*/i);
104
+ let videoWidth = 0;
105
+ let videoHeight = 0;
106
+ if (videoLineMatch) {
107
+ const dim = videoLineMatch[0].match(/\b(\d{2,5})x(\d{2,5})\b/);
108
+ if (dim) {
109
+ videoWidth = Number(dim[1]);
110
+ videoHeight = Number(dim[2]);
111
+ }
112
+ }
113
+ const containerMatch = ffmpegOutput.match(/Input #0,\s*([^,]+(?:,[^,]+)*?),\s*from/i);
114
+ const durationMatch = ffmpegOutput.match(/Duration:\s*(\d+):(\d+):(\d+(?:\.\d+)?)/i);
115
+ let durationSeconds = 0;
116
+ if (durationMatch) {
117
+ const value =
118
+ Number(durationMatch[1]) * 3600 + Number(durationMatch[2]) * 60 + Number(durationMatch[3]);
119
+ durationSeconds = Number.isFinite(value) ? value : 0;
120
+ }
121
+ const streams = parseStreams(ffmpegOutput);
122
+ const audioTracks = streams
123
+ .filter((s) => s.type === "audio")
124
+ .map((s, i) => ({
125
+ // Type-relative index — what ffmpeg's `-map 0:a:N` selects.
126
+ index: i,
127
+ streamIndex: s.streamIndex,
128
+ codec: s.codec,
129
+ language: s.language,
130
+ title: s.title,
131
+ isDefault: s.isDefault
132
+ }));
133
+ const subtitleTracks = streams
134
+ .filter((s) => s.type === "subtitle")
135
+ .map((s, i) => ({
136
+ // Type-relative index — what ffmpeg's `-map 0:s:N` selects.
137
+ index: i,
138
+ streamIndex: s.streamIndex,
139
+ codec: s.codec,
140
+ language: s.language,
141
+ title: s.title,
142
+ isDefault: s.isDefault,
143
+ // Image-based subtitles (PGS/VobSub) cannot become WebVTT.
144
+ textBased: TEXT_SUBTITLE_CODECS.has(s.codec)
145
+ }));
146
+ return {
147
+ audioCodec: audioMatch ? String(audioMatch[1]).toLowerCase() : "",
148
+ videoCodec: videoMatch ? String(videoMatch[1]).toLowerCase() : "",
149
+ container: containerMatch ? String(containerMatch[1]).trim().toLowerCase() : "",
150
+ durationSeconds,
151
+ videoWidth,
152
+ videoHeight,
153
+ audioTracks,
154
+ subtitleTracks
155
+ };
156
+ }
157
+
158
+ /**
159
+ * Run a brief ffmpeg probe to identify the audio and video codecs of a stream.
160
+ * Times out after `timeoutMs` and returns empty strings on failure.
161
+ *
162
+ * @param {object} options
163
+ * @param {string} options.ffmpegBin
164
+ * @param {string} options.inputUrl
165
+ * @param {string} [options.userAgent=""]
166
+ * @param {number} [options.timeoutMs=8000]
167
+ * @returns {Promise<{ audioCodec: string, videoCodec: string, container: string, durationSeconds: number, videoWidth: number, videoHeight: number, audioTracks: object[], subtitleTracks: object[], stderr: string }>}
168
+ * Parsed banner fields plus the raw `stderr`, so the caller can derive the
169
+ * full media info (fps/startTime/HDR) without a second ffmpeg scan.
170
+ */
171
+ function probeStreamCodecs({ ffmpegBin, inputUrl, userAgent = "", timeoutMs = 8_000 }) {
172
+ return new Promise((resolve) => {
173
+ const args = ["-hide_banner", "-loglevel", "info"];
174
+ if (typeof userAgent === "string" && userAgent.trim().length > 0) {
175
+ args.push("-user_agent", userAgent.trim());
176
+ }
177
+ // Decode a tiny slice of all streams (no per-stream -map, so video-only
178
+ // files probe correctly too). The ffmpeg banner that precedes decoding
179
+ // gives us audio/video codecs, the container format and the duration in a
180
+ // single pass.
181
+ args.push("-i", inputUrl, "-t", "0.1", "-f", "null", "-");
182
+
183
+ const ffmpeg = spawn(ffmpegBin, args, {
184
+ stdio: ["ignore", "ignore", "pipe"],
185
+ windowsHide: true
186
+ });
187
+ let stderr = "";
188
+ let settled = false;
189
+
190
+ const finish = (codecs) => {
191
+ if (settled) {
192
+ return;
193
+ }
194
+ settled = true;
195
+ resolve(codecs);
196
+ };
197
+
198
+ const timeoutId = setTimeout(() => {
199
+ if (!ffmpeg.killed) {
200
+ ffmpeg.kill("SIGTERM");
201
+ }
202
+ finish({ ...parseStreamCodecs(stderr), stderr });
203
+ }, timeoutMs);
204
+
205
+ ffmpeg.stderr.on("data", (chunk) => {
206
+ stderr += String(chunk);
207
+ });
208
+
209
+ ffmpeg.on("error", () => {
210
+ clearTimeout(timeoutId);
211
+ finish({ audioCodec: "", videoCodec: "", stderr: "" });
212
+ });
213
+
214
+ ffmpeg.on("exit", () => {
215
+ clearTimeout(timeoutId);
216
+ finish({ ...parseStreamCodecs(stderr), stderr });
217
+ });
218
+ });
219
+ }
220
+
221
+ /**
222
+ * Resolve after a given number of milliseconds.
223
+ *
224
+ * @param {number} ms
225
+ * @returns {Promise<void>}
226
+ */
227
+ function delay(ms) {
228
+ return new Promise((resolve) => {
229
+ setTimeout(resolve, ms);
230
+ });
231
+ }
232
+
233
+ /**
234
+ * Build the direct stream URL for a source file served by the local proxy.
235
+ *
236
+ * @param {string} localBaseUrl - e.g. "http://127.0.0.1:9090"
237
+ * @param {string} sourceKey
238
+ * @param {number} fileIndex
239
+ * @returns {string}
240
+ */
241
+ function buildDirectUrl(localBaseUrl, sourceKey, fileIndex) {
242
+ const directUrl = new URL("/stream", `${localBaseUrl}/`);
243
+ directUrl.searchParams.set("sourceKey", sourceKey);
244
+ directUrl.searchParams.set("fileIndex", String(fileIndex));
245
+ return directUrl.toString();
246
+ }
247
+
248
+ /**
249
+ * @typedef {Object} PlaybackPlan
250
+ * @property {"direct" | "hls"} mode
251
+ * @property {string} directUrl
252
+ * @property {string} reason - Human-readable explanation of the chosen mode.
253
+ * @property {string} audioCodec
254
+ * @property {string} videoCodec
255
+ * @property {string} container - Demuxer/container name(s) reported by ffmpeg.
256
+ * @property {number} durationSeconds - Total media duration in seconds (0 if unknown).
257
+ * @property {number} videoWidth - Source coded width (0 if unknown).
258
+ * @property {number} videoHeight - Source coded height (0 if unknown).
259
+ */
260
+
261
+ /**
262
+ * @typedef {Object} PlaybackPlannerOptions
263
+ * @property {string} ffmpegBin
264
+ * @property {boolean} transcodeAudioEnabled
265
+ * @property {string} localBaseUrl
266
+ * @property {ReturnType<import("../store/source-registry.js").createSourceRegistry>} sourceRegistry
267
+ * @property {import("./torrent-pool.js").TorrentPool} torrentPool
268
+ */
269
+
270
+ /**
271
+ * Create a playback planner that decides the optimal streaming mode for
272
+ * a torrent file. Plans are cached per (sourceKey, fileIndex) pair.
273
+ *
274
+ * @param {PlaybackPlannerOptions} options
275
+ * @returns {{ getPlan: (params: { sourceKey: string, fileIndex: number, userAgent?: string }) => Promise<PlaybackPlan> }}
276
+ */
277
+ export function createPlaybackPlanner({
278
+ ffmpegBin,
279
+ transcodeAudioEnabled,
280
+ localBaseUrl,
281
+ sourceRegistry,
282
+ torrentPool,
283
+ // Optional. Reports what this host typically takes to produce a session's
284
+ // first segment. The browser needs it for the gap between "the file is
285
+ // downloaded" and "a segment exists": until now it assumed the pipeline
286
+ // merely keeps up with realtime, and showed 15 s where 3.8 s were left.
287
+ expectedFirstSegmentMs,
288
+ expectedSessionCreateMs,
289
+ // Optional. Called once the file's edges are downloaded, so the keyframe
290
+ // index — which reads the same tail of the file — is fetched alongside the
291
+ // codec probe instead of after it. Late-bound to the HLS session manager,
292
+ // which owns the cache both of them share.
293
+ warmKeyframeIndex,
294
+ // Optional. The heights this host could actually serve this source at, for
295
+ // both playback branches, so the quality menu is right from the moment the
296
+ // file is opened rather than from the moment an encoder exists.
297
+ predictOfferedHeights
298
+ }) {
299
+ /** @type {Map<string, PlaybackPlan>} */
300
+ const cache = new Map();
301
+ /**
302
+ * Full media info parsed from the SAME probe that produced the plan, cached
303
+ * under the same key so a transcode session can reuse it instead of running
304
+ * a second ffmpeg scan. Only set when the plan is cached (codecs detected).
305
+ * @type {Map<string, { durationSeconds: number | null, width: number | null, height: number | null, fps: number | null, startTime: number, isHdr: boolean }>}
306
+ */
307
+ const mediaInfoCache = new Map();
308
+
309
+ /**
310
+ * Attach what this host currently measures itself taking to create a session
311
+ * and to produce a first segment.
312
+ *
313
+ * Read at RESPONSE time, deliberately. Both are medians of sessions that have
314
+ * already finished on this host, so at the moment a plan is BUILT the very
315
+ * first file opened after a restart has none and gets `null` — and the plan
316
+ * is then cached, so that file kept answering `null` for the life of the
317
+ * process however many sessions ran afterwards. Measured 2026-08-05: a fresh
318
+ * 2.9.103 answered `null` for both, then produced the session in 6 ms and the
319
+ * first segment in 21 479 ms. The figures existed; the plan could not carry
320
+ * them, and the browser's estimate fell back to its own guess in exactly the
321
+ * cold-start case the feature was built for.
322
+ *
323
+ * @param {PlaybackPlan} plan
324
+ * @returns {PlaybackPlan}
325
+ */
326
+ /**
327
+ * The probe's subtitle tracks, with `FlagDefault` read from the container
328
+ * instead of inferred from ffmpeg's banner.
329
+ *
330
+ * Best-effort by construction: a container that cannot be read this way, or a
331
+ * reading that does not line up with the probe, leaves the tracks as they
332
+ * were with `declaresDefault: false` — which the browser reads as "the file
333
+ * has no opinion", and then nothing is shown unasked.
334
+ *
335
+ * @param {object} torrent
336
+ * @param {number} fileIndex
337
+ * @param {object[]} subtitleTracks
338
+ * @returns {Promise<object[]>}
339
+ */
340
+ async function withContainerDefaults(torrent, fileIndex, subtitleTracks) {
341
+ if (subtitleTracks.length === 0 || typeof torrentPool?.getDeclaredSubtitleTracks !== "function") {
342
+ return subtitleTracks.map((track) => ({ ...track, declaresDefault: false }));
343
+ }
344
+ let declared = [];
345
+ try {
346
+ declared = await torrentPool.getDeclaredSubtitleTracks(torrent, fileIndex);
347
+ } catch (error) {
348
+ logger.info(`subtitle defaults: the container could not be read (${error?.message ?? error})`);
349
+ }
350
+ const merged = Container.mergeSubtitleFlags(subtitleTracks, declared);
351
+ logger.info(
352
+ merged.aligned
353
+ ? "subtitle defaults: the container wrote FlagDefault on " +
354
+ `${merged.tracks.filter((track) => track.declaresDefault).length} of ${merged.tracks.length} ` +
355
+ `subtitle tracks, marking ${merged.tracks.filter((track) => track.declaresDefault && track.isDefault).length}`
356
+ : `subtitle defaults: using the probe's own flags — ${merged.reason}`
357
+ );
358
+ return merged.tracks;
359
+ }
360
+
361
+ /**
362
+ * Every soundtrack this file can be watched with, as one numbered list: its
363
+ * own tracks and the ones shipped as separate files beside it.
364
+ *
365
+ * Built here, in the plan, because the plan is what the viewer's menu is drawn
366
+ * from — so the offer is complete the moment a file is opened, with nothing
367
+ * arriving late and nothing measured while the viewer waits. It is also what
368
+ * the master playlist's rendition group is built from, so the number in the
369
+ * menu and the number in the `a/<n>/` address are the same number by
370
+ * construction rather than by agreement.
371
+ *
372
+ * @param {object} torrent
373
+ * @param {number} fileIndex
374
+ * @param {object[]} bannerAudioTracks - The probe's own audio streams.
375
+ * @returns {Promise<import("./audio-inventory.js").AudioInventoryEntry[]>}
376
+ */
377
+ async function buildInventory(torrent, fileIndex, bannerAudioTracks) {
378
+ const banner = Array.isArray(bannerAudioTracks) ? bannerAudioTracks : [];
379
+ /**
380
+ * Read a file's declared audio tracks, or give up quickly.
381
+ *
382
+ * The plan is on the path to the first frame, and reading a sidecar's header
383
+ * waits on the swarm: that file has usually had nothing downloaded when this
384
+ * runs, and a header that never arrives would hold the plan — and the
385
+ * viewer — for the whole of the read's own patience. What a timeout costs is
386
+ * small and deliberate: the track is still offered, still numbered and still
387
+ * playable, only without the language and flags its own header would have
388
+ * given. The language the viewer actually sees is read from the FOLDER the
389
+ * release put it in, which is in the torrent's file list and needs no bytes
390
+ * at all.
391
+ *
392
+ * @param {number} wantedFileIndex
393
+ * @param {string} label
394
+ * @returns {Promise<object[]>}
395
+ */
396
+ const declaredAudioOf = async (wantedFileIndex, label) => {
397
+ if (typeof torrentPool?.getDeclaredAudioTracks !== "function") {
398
+ return [];
399
+ }
400
+ let timer = null;
401
+ try {
402
+ return await Promise.race([
403
+ torrentPool.getDeclaredAudioTracks(torrent, wantedFileIndex),
404
+ new Promise((resolve) => {
405
+ timer = setTimeout(() => resolve(null), SIDECAR_HEADER_WAIT_MS);
406
+ timer.unref?.();
407
+ })
408
+ ]).then((tracks) => {
409
+ if (tracks === null) {
410
+ logger.info(
411
+ `audio tracks: "${label}" did not answer within ` +
412
+ `${SIDECAR_HEADER_WAIT_MS / 1000}s — offered without what its header would say`
413
+ );
414
+ return [];
415
+ }
416
+ return Array.isArray(tracks) ? tracks : [];
417
+ });
418
+ } catch (error) {
419
+ logger.info(`audio tracks: "${label}" could not be read (${error?.message ?? error})`);
420
+ return [];
421
+ } finally {
422
+ if (timer !== null) {
423
+ clearTimeout(timer);
424
+ }
425
+ }
426
+ };
427
+ // The picture's own tracks: ffmpeg numbers them, the container declares what
428
+ // they are. Both readings, lined up and checked — see `audio-inventory.js`.
429
+ let embedded = banner.map((track) => ({ ...track, declaresDefault: false }));
430
+ if (banner.length > 0) {
431
+ // The picture's head is already downloaded — the codec probe just read it
432
+ // — so this is a parse and not a wait, but it is bounded like the rest.
433
+ const declared = await declaredAudioOf(fileIndex, "the picture");
434
+ const merged = mergeContainerAudioFlags(banner, declared);
435
+ embedded = merged.tracks;
436
+ logger.info(
437
+ merged.aligned
438
+ ? `audio tracks: the container describes all ${merged.tracks.length}` +
439
+ `${merged.tracks.some((track) => track.isCommentary) ? ", one of them commentary" : ""}` +
440
+ `${merged.tracks.some((track) => track.isVisualImpaired) ? ", one of them described" : ""}`
441
+ : `audio tracks: using the probe's own fields — ${merged.reason}`
442
+ );
443
+ }
444
+
445
+ const sidecarFiles = matchSidecarFiles({
446
+ files: torrent?.files ?? [],
447
+ videoIndex: fileIndex,
448
+ torrentName: typeof torrent?.name === "string" ? torrent.name : "",
449
+ videoCount: countVideoFiles(torrent?.files ?? [])
450
+ });
451
+ // All of them at once. They are separate files with separate headers, and
452
+ // read one after another the waits add up on the path to the first frame.
453
+ const sidecars = await Promise.all(
454
+ sidecarFiles.audio.map(async (file) => ({
455
+ file,
456
+ // A bare elementary stream — `.ac3`, `.dts`, `.mp3` — has no table to
457
+ // read, so nothing is asked of the swarm for it at all.
458
+ tracks: file.declaresTracks ? await declaredAudioOf(file.fileIndex, file.name) : []
459
+ }))
460
+ );
461
+ const inventory = buildAudioInventory({ embedded, videoFileIndex: fileIndex, sidecars });
462
+ if (sidecars.length > 0) {
463
+ logger.info(
464
+ `audio tracks: ${sidecars.length} file(s) beside the picture carry sound — ` +
465
+ inventory
466
+ .filter((entry) => entry.kind === "sidecar")
467
+ .map((entry) =>
468
+ `a:${entry.index}=${entry.folders.join("/") || "."}/${entry.fileName}` +
469
+ `#${entry.sourceTrackIndex}${entry.codec ? `(${entry.codec})` : ""}`
470
+ )
471
+ .join(" ")
472
+ );
473
+ }
474
+ return inventory;
475
+ }
476
+
477
+ function withHostTimings(plan) {
478
+ const withOffer = {
479
+ ...plan,
480
+ expectedFirstSegmentMs: expectedFirstSegmentMs?.() ?? null,
481
+ expectedSessionCreateMs: expectedSessionCreateMs?.() ?? null,
482
+ // Answered here for the same reason as the two above: a plan is cached for
483
+ // the life of the process, and what this host will serve a file at is not.
484
+ // It starts as a prediction from the startup benchmarks and is replaced by
485
+ // what an encoder running on this very source turns out to cost — frozen
486
+ // into the cache, every later open of the file would hand the browser the
487
+ // first guess again and undo that. This is the 2.9.106 defect exactly.
488
+ offeredHeights: plan.mediaInfoForOffer
489
+ ? (predictOfferedHeights?.(plan.mediaInfoForOffer) ?? null)
490
+ : null,
491
+ mediaInfoForOffer: undefined
492
+ };
493
+ // Refused rather than served badly. Both lists empty means this machine
494
+ // cannot sustain this file at ANY height — not even by copying the picture,
495
+ // which costs no encoder at all — so a session made here would produce a
496
+ // slideshow and take the swarm and the processor from whoever is already
497
+ // watching. Field 2026-08-28: five sessions on one file put every rung at
498
+ // 0.04x of realtime and the viewer watched one before the process was
499
+ // killed. The viewer is told why, which is a different thing from a spinner
500
+ // that never ends.
501
+ const offer = withOffer.offeredHeights;
502
+ if (offer && offer.copy.length === 0 && offer.transcode.length === 0) {
503
+ withOffer.cannotServe =
504
+ "This proxy cannot keep up with this file at any quality right now.";
505
+ // The description travels with the refusal, and only with it. It is what
506
+ // lets the browser ask the rest of the pool the same question without
507
+ // anybody else adding the torrent, fetching a byte or running ffmpeg —
508
+ // the expensive half of finding out what this file IS has been paid here,
509
+ // once. Everyone else answers by arithmetic against their own startup
510
+ // benchmarks.
511
+ withOffer.mediaInfoForOffer = plan.mediaInfoForOffer;
512
+ }
513
+ return withOffer;
514
+ }
515
+
516
+ return {
517
+ /**
518
+ * Media info the planner already probed for this file, or `null`. Lets the
519
+ * HLS session manager skip its own duplicate `probeInputMediaInfo` scan.
520
+ *
521
+ * @param {{ sourceKey: string, fileIndex: number }} params
522
+ * @returns {{ durationSeconds: number | null, width: number | null, height: number | null, fps: number | null, startTime: number, isHdr: boolean } | null}
523
+ */
524
+ /**
525
+ * The audio tracks this file was probed to have, or an empty list. The
526
+ * master playlist publishes one rendition per track, and the inventory is
527
+ * already here — probing again for it would be a second scan of a file the
528
+ * proxy is in the middle of serving.
529
+ *
530
+ * @param {{ sourceKey: string, fileIndex: number }} params
531
+ * @returns {object[]}
532
+ */
533
+ getCachedAudioTracks({ sourceKey, fileIndex }) {
534
+ const plan = cache.get(`${sourceKey}:${fileIndex}`);
535
+ return Array.isArray(plan?.audioTracks) ? plan.audioTracks : [];
536
+ },
537
+
538
+ getCachedMediaInfo({ sourceKey, fileIndex }) {
539
+ return mediaInfoCache.get(`${sourceKey}:${fileIndex}`) ?? null;
540
+ },
541
+
542
+ /**
543
+ * Return the playback plan for the given source file.
544
+ * Throws with `error.code === "SOURCE_NOT_FOUND"` or `"FILE_NOT_FOUND"`
545
+ * when the source or file cannot be located.
546
+ *
547
+ * When the file header has not downloaded yet (cold torrent, peers still
548
+ * connecting) the codec probe cannot succeed. Rather than block the HTTP
549
+ * response until it can, the planner prioritises the header, probes for at
550
+ * most `maxWaitMs`, and if still undetectable returns a plan flagged
551
+ * `pending: true` (NOT cached). The caller polls again — each call keeps the
552
+ * header prioritised and downloading — until a real plan comes back. This
553
+ * avoids a single long request racing the transport's request timeout.
554
+ *
555
+ * @param {object} params
556
+ * @param {string} params.sourceKey
557
+ * @param {number} params.fileIndex
558
+ * @param {string} [params.userAgent=""]
559
+ * @param {number} [params.maxWaitMs=60000] - Max time to wait for the header within ONE call.
560
+ * @returns {Promise<PlaybackPlan & { pending?: boolean }>}
561
+ */
562
+ async getPlan({ sourceKey, fileIndex, userAgent = "", maxWaitMs = 60_000 }) {
563
+ const cacheKey = `${sourceKey}:${fileIndex}`;
564
+ const cached = cache.get(cacheKey);
565
+ if (cached) {
566
+ return withHostTimings(cached);
567
+ }
568
+ // Where the time before playback goes. `cold-start` already breaks down
569
+ // everything from the transcode-session request onwards, but the plan
570
+ // runs BEFORE that and was a single opaque wait: a field session spent
571
+ // 5.7 s here on a torrent already in the store, with the codec probe
572
+ // cached, and nothing said which part of it was slow.
573
+ const planEntryMs = Date.now();
574
+ let torrentReadyMs = 0;
575
+ let edgesReadyMs = 0;
576
+
577
+ const sourceRecord = sourceRegistry.get(sourceKey);
578
+ if (!sourceRecord) {
579
+ const error = new Error("Source key was not found.");
580
+ error.code = "SOURCE_NOT_FOUND";
581
+ throw error;
582
+ }
583
+
584
+ const torrent = await torrentPool.getTorrent(sourceRecord.sourceType, sourceRecord.source);
585
+ torrentReadyMs = Date.now() - planEntryMs;
586
+ const file = torrent.files[fileIndex];
587
+ if (!file) {
588
+ const error = new Error("File index was not found in torrent.");
589
+ error.code = "FILE_NOT_FOUND";
590
+ throw error;
591
+ }
592
+
593
+ const directUrl = buildDirectUrl(localBaseUrl, sourceKey, fileIndex);
594
+ if (!transcodeAudioEnabled) {
595
+ const plan = {
596
+ mode: "direct",
597
+ directUrl,
598
+ reason: "transcode-disabled",
599
+ audioCodec: "",
600
+ videoCodec: "",
601
+ container: "",
602
+ durationSeconds: 0,
603
+ videoWidth: 0,
604
+ videoHeight: 0,
605
+ audioTracks: [],
606
+ subtitleTracks: []
607
+ };
608
+ cache.set(cacheKey, plan);
609
+ return withHostTimings(plan);
610
+ }
611
+
612
+ // Pre-fetch file edges (head + tail), then probe — retrying while the
613
+ // file header is still downloading. In a multi-file torrent the pieces
614
+ // for a given file arrive unevenly, so the first probe can return empty
615
+ // codecs. A transient empty probe must NOT be cached: otherwise the wrong
616
+ // plan (file treated as directly playable) sticks permanently for this
617
+ // file, and an unsupported codec like xvid gets copied → black video.
618
+ await torrentPool.prefetchFileEdges(torrent, fileIndex);
619
+ edgesReadyMs = Date.now() - planEntryMs;
620
+ // The keyframe index reads the tail of the file, which the probe has just
621
+ // waited for as well. Started here it overlaps the probe instead of
622
+ // following the whole plan — worth 311-430 ms of the time before the
623
+ // first segment. Fire and forget: the session reads it itself if this has
624
+ // not finished, and both share one cache entry.
625
+ warmKeyframeIndex?.({
626
+ sourceKey,
627
+ fileIndex,
628
+ inputUrl: new URL(directUrl),
629
+ logName: file.name
630
+ });
631
+ let probe = await probeStreamCodecs({ ffmpegBin, inputUrl: directUrl, userAgent });
632
+ const probeDeadline = Date.now() + Math.max(0, maxWaitMs);
633
+ let attempt = 0;
634
+ while (
635
+ probe.audioCodec.length === 0 &&
636
+ probe.videoCodec.length === 0 &&
637
+ Date.now() < probeDeadline
638
+ ) {
639
+ attempt += 1;
640
+ await delay(Math.min(3_000, 500 + attempt * 250));
641
+ await torrentPool.prefetchFileEdges(torrent, fileIndex);
642
+ probe = await probeStreamCodecs({ ffmpegBin, inputUrl: directUrl, userAgent });
643
+ }
644
+ const { audioCodec, videoCodec, container, durationSeconds, videoWidth, videoHeight, audioTracks, subtitleTracks } = probe;
645
+ const codecsDetected = audioCodec.length > 0 || videoCodec.length > 0;
646
+ logger.info(
647
+ `plan ${sourceKey.slice(0, 8)}:${fileIndex} torrent-ready=${torrentReadyMs}ms ` +
648
+ `file-edges=${edgesReadyMs - torrentReadyMs}ms probe=${Date.now() - planEntryMs - edgesReadyMs}ms ` +
649
+ `total=${Date.now() - planEntryMs}ms attempts=${attempt + 1} ` +
650
+ `${codecsDetected ? `${videoCodec || "-"}/${audioCodec || "-"}` : "codecs NOT detected (will be polled again)"}`
651
+ );
652
+
653
+ // `mode` is advisory only (audio-codec based). The browser makes the
654
+ // authoritative decision independently per stream via canPlayType /
655
+ // mediaCapabilities, transcoding only what it cannot play.
656
+ const requiresTranscode = audioCodec.length > 0 && !DIRECT_AUDIO_CODECS.has(audioCodec);
657
+ const plan = {
658
+ mode: requiresTranscode ? "hls" : "direct",
659
+ directUrl,
660
+ reason: requiresTranscode ? "audio-codec-transcode-required" : "audio-codec-supported",
661
+ audioCodec,
662
+ videoCodec,
663
+ container,
664
+ durationSeconds,
665
+ // Source coded resolution — drives the browser's manual quality menu
666
+ // (list of forced resolutions <= source). 0 when unknown.
667
+ videoWidth,
668
+ videoHeight,
669
+ // Full track inventory for the browser's audio/subtitle menus. The audio
670
+ // half spans the picture's own tracks AND the soundtracks shipped as
671
+ // files beside it, under one numbering — see `buildInventory`.
672
+ audioTracks: await buildInventory(torrent, fileIndex, audioTracks ?? []),
673
+ subtitleTracks: await withContainerDefaults(torrent, fileIndex, subtitleTracks ?? []),
674
+ // Both host timings are filled in by `withHostTimings` on the way out,
675
+ // never here: read at build time they would be frozen into the cached
676
+ // plan, which is the bug fixed in 2.9.106.
677
+ expectedFirstSegmentMs: null,
678
+ expectedSessionCreateMs: null,
679
+ offeredHeights: null,
680
+ // What the offer is computed FROM, kept on the cached plan so the offer
681
+ // itself can be recomputed on every response. The figures are the
682
+ // probe's own and never change for a file; the answer derived from them
683
+ // does, as the host learns what this source costs. Stripped on the way
684
+ // out — it is not part of the plan the browser is given.
685
+ mediaInfoForOffer: {
686
+ width: videoWidth,
687
+ height: videoHeight,
688
+ fps: parseFfmpegVideoFps(probe.stderr),
689
+ bitrateKbps: parseFfmpegBitrateKbps(probe.stderr),
690
+ // Which family of the decode measurement prices this source. A video
691
+ // that has to be re-encoded is one the browser could not play, so it
692
+ // is usually NOT H.264, and H.264 constants are wrong for it.
693
+ codec: videoCodec,
694
+ bitDepth: parseFfmpegBitDepth(probe.stderr),
695
+ // Which file this is, so the offer can be answered from what an
696
+ // encoder has already learned about THIS source rather than from the
697
+ // startup clips — the same correction a live session applies.
698
+ sourceKey,
699
+ fileIndex
700
+ }
701
+ };
702
+ // Only cache a plan whose codecs were actually detected. An empty probe is
703
+ // a "header not downloaded yet" signal, not a valid result — caching it
704
+ // would permanently mis-plan the file. In that case flag the plan
705
+ // `pending` so the caller polls again (the header keeps downloading,
706
+ // prioritised by the prefetch above).
707
+ if (codecsDetected) {
708
+ cache.set(cacheKey, plan);
709
+ // Cache the full media info from THIS probe's banner (same helpers the
710
+ // session manager uses) so createSession can skip its own probe.
711
+ const dims = parseFfmpegVideoDimensions(probe.stderr);
712
+ mediaInfoCache.set(cacheKey, {
713
+ // The codecs, because the session manager asks this cache which
714
+ // tracks the output will carry — and they were never stored here. It
715
+ // read `videoCodec`/`audioCodec` off an object that has only ever had
716
+ // dimensions and duration, got `undefined` for both, and declared
717
+ // `{video: false, audio: false}` for EVERY session since the check was
718
+ // written. Measured 2026-08-11: `declared tracks video=false
719
+ // audio=false`, which left the browser unable to tell "this file has
720
+ // no video" from "the video was lost on the way", and left the init
721
+ // guard expecting zero tracks and therefore accepting any header.
722
+ videoCodec: plan.videoCodec,
723
+ audioCodec: plan.audioCodec,
724
+ durationSeconds: parseFfmpegDurationSeconds(probe.stderr),
725
+ width: dims.width,
726
+ height: dims.height,
727
+ bitrateKbps: parseFfmpegBitrateKbps(probe.stderr),
728
+ fps: parseFfmpegVideoFps(probe.stderr),
729
+ startTime: parseFfmpegStartTimeSeconds(probe.stderr),
730
+ isHdr: parseFfmpegHdr(probe.stderr),
731
+ bitDepth: parseFfmpegBitDepth(probe.stderr)
732
+ });
733
+ // Warm the file-body start for the transcode session that follows.
734
+ // Fire-and-forget: never delays the plan response.
735
+ void torrentPool
736
+ .prefetchFileEdges(torrent, fileIndex, {
737
+ headBytes: BODY_PREFETCH_BYTES,
738
+ tailBytes: 0,
739
+ timeoutMs: 60_000
740
+ })
741
+ .catch(() => {});
742
+ return withHostTimings(plan);
743
+ }
744
+ return withHostTimings({ ...plan, pending: true });
745
+ }
746
+ };
747
+ }