BlueGit

timfa filemanager

Files

filemanager / server / video-stream.js

video-stream.js

Reduce subtitle buffer stalls
timfa Tim Falken · pushed by timfa · 5 months ago
f21c3881 History
video-stream.js 33.5 KiB Download Raw
1'use strict';
2
3const { spawn } = require('child_process');
4const crypto = require('crypto');
5const fs = require('fs');
6const os = require('os');
7const path = require('path');
8const { clearCachesForFile, pruneOldImageSubtitleCaches, purgeSubtitleCacheDir } = require('./image-subtitles');
9
10const STREAM_ROOT_DIR = path.join(os.tmpdir(), 'filemanager-video-streams');
11const STREAM_SESSION_TTL_MS = 30 * 60 * 1000;
12const STREAM_READY_TIMEOUT_MS = 20 * 1000;
13const STREAM_IDLE_STOP_MS = 5 * 60 * 1000;
14const ESTIMATED_SEGMENT_BYTES = 1024 * 1024;
15const ESTIMATED_SEGMENT_DURATION_SECONDS = 6;
16const STREAM_PRUNE_DELETE_BATCH_SIZE = parsePruneDeleteBatchSize(process.env.VIDEO_STREAM_PRUNE_DELETE_BATCH_SIZE);
17const MAX_STREAM_SEGMENT_RETENTION_COUNT = 360;
18const STREAM_SEGMENT_RETENTION_COUNT = parseSegmentRetentionCount(process.env.VIDEO_STREAM_SEGMENT_RETENTION_COUNT);
19const IMAGE_BASED_SUBTITLE_CODECS = new Set([
20 'hdmv_pgs_subtitle',
21 'pgssub',
22 'dvd_subtitle',
23 'dvdsub',
24 'dvb_subtitle',
25 'xsub',
26]);
27const sessions = new Map();
28const leases = new Map();
29const reusableSessionIdBySource = new Map();
30
31// Remove any .sup subtitle files left over from a previous server run.
32purgeSubtitleCacheDir();
33
34function purgeStreamRootDir ()
35{
36 try
37 {
38 fs.rmSync(STREAM_ROOT_DIR, { recursive: true, force: true });
39 } catch
40 {
41 // Best-effort cleanup.
42 }
43}
44
45function cleanupIdleStreamArtifacts ()
46{
47 if (sessions.size > 0) return;
48 if (leases.size > 0) return;
49 purgeStreamRootDir();
50}
51
52// Remove stale stream output folders left by previous runs.
53purgeStreamRootDir();
54
55// Periodically sweep stream artifacts when no streams are active.
56const streamRootCleanupTimer = setInterval(cleanupIdleStreamArtifacts, STREAM_IDLE_STOP_MS);
57if (streamRootCleanupTimer && typeof streamRootCleanupTimer.unref === 'function')
58{
59 streamRootCleanupTimer.unref();
60}
61
62function parseSegmentRetentionCount (value)
63{
64 const parsed = Number.parseInt(String(value || ''), 10);
65 if (Number.isInteger(parsed) && parsed >= 30)
66 {
67 return clampNumber(parsed, 30, MAX_STREAM_SEGMENT_RETENTION_COUNT);
68 }
69 return defaultSegmentRetentionCount();
70}
71
72function parsePruneDeleteBatchSize (value)
73{
74 const parsed = Number.parseInt(String(value || ''), 10);
75 if (Number.isInteger(parsed) && parsed >= 1)
76 {
77 return clampNumber(parsed, 1, 128);
78 }
79 return 16;
80}
81
82function readRaspberryPiModel ()
83{
84 try
85 {
86 return fs.readFileSync('/proc/device-tree/model', 'utf8').replace(/\u0000/g, '').trim();
87 } catch
88 {
89 return '';
90 }
91}
92
93function clampNumber (value, min, max)
94{
95 return Math.min(max, Math.max(min, value));
96}
97
98function currentCpuPressureFactor (cpuCount)
99{
100 if (typeof os.loadavg !== 'function') return 1;
101 if (!Number.isFinite(cpuCount) || cpuCount <= 0) return 1;
102
103 const [load1] = os.loadavg();
104 if (!Number.isFinite(load1) || load1 < 0) return 1;
105
106 const saturation = load1 / cpuCount;
107 if (saturation >= 1.5) return 0.72;
108 if (saturation >= 1.2) return 0.8;
109 if (saturation >= 1.0) return 0.88;
110 if (saturation >= 0.8) return 0.94;
111 return 1;
112}
113
114function defaultSegmentRetentionCount ()
115{
116 const totalMemBytes = Number(os.totalmem()) || 0;
117 const freeMemBytes = Number(os.freemem()) || 0;
118 const cpuCount = Math.max(1, ((os.cpus() || []).length || 1));
119 const arch = String(os.arch() || '').toLowerCase();
120 const piModel = readRaspberryPiModel().toLowerCase();
121 const isPi5 = piModel.includes('raspberry pi 5');
122 const isArm = arch === 'arm' || arch === 'arm64' || arch.startsWith('armv');
123 const cpuPressure = currentCpuPressureFactor(cpuCount);
124
125 const freeMemRatio = totalMemBytes > 0 ? (freeMemBytes / totalMemBytes) : 1;
126 const memoryPressure = freeMemRatio < 0.12 ? 0.72 : freeMemRatio < 0.2 ? 0.84 : freeMemRatio < 0.3 ? 0.92 : 1;
127
128 if (isPi5)
129 {
130 const memoryBudgetBytes = Math.floor(totalMemBytes * 0.03);
131 const byMemory = Math.floor(memoryBudgetBytes / ESTIMATED_SEGMENT_BYTES);
132 const byCpu = 120 + ((cpuCount - 2) * 20);
133 const combined = Math.min(byMemory, byCpu);
134 return clampNumber(Math.floor(combined * cpuPressure * memoryPressure), 100, 220);
135 }
136
137 if (isArm)
138 {
139 const memoryBudgetBytes = Math.floor(totalMemBytes * 0.025);
140 const byMemory = Math.floor(memoryBudgetBytes / ESTIMATED_SEGMENT_BYTES);
141 const byCpu = 90 + ((cpuCount - 2) * 14);
142 const combined = Math.min(byMemory, byCpu);
143 return clampNumber(Math.floor(combined * cpuPressure * memoryPressure), 80, 180);
144 }
145
146 const memoryBudgetBytes = Math.floor(totalMemBytes * 0.05);
147 const byMemory = Math.floor(memoryBudgetBytes / ESTIMATED_SEGMENT_BYTES);
148 const byCpu = 140 + ((cpuCount - 4) * 16);
149 const combined = Math.min(byMemory, byCpu);
150 return clampNumber(Math.floor(combined * cpuPressure * memoryPressure), 120, 320);
151}
152
153function hasInitialHlsAssets (outputDir)
154{
155 try
156 {
157 const entries = fs.readdirSync(outputDir, { withFileTypes: true });
158 return entries.some((entry) =>
159 {
160 if (!entry.isFile()) return false;
161 if (entry.name === 'master.m3u8') return true;
162 if (/^stream_.+\.m3u8(?:\.tmp)?$/i.test(entry.name)) return true;
163 if (/^stream_.+_segment_\d+\.ts(?:\.tmp)?$/i.test(entry.name)) return true;
164 return false;
165 });
166 } catch
167 {
168 return false;
169 }
170}
171
172function makeHttpError (status, message)
173{
174 const err = new Error(message);
175 err.status = status;
176 return err;
177}
178
179function ensureStreamRootDir ()
180{
181 fs.mkdirSync(STREAM_ROOT_DIR, { recursive: true });
182}
183
184function ffmpegPath ()
185{
186 return String(process.env.FFMPEG_PATH || '').trim() || 'ffmpeg';
187}
188
189function ffprobePath ()
190{
191 return String(process.env.FFPROBE_PATH || '').trim() || 'ffprobe';
192}
193
194function pruneExpiredSessions ()
195{
196 const now = Date.now();
197
198 for (const [leaseId, lease] of leases)
199 {
200 if (lease.expiresAt > now) continue;
201 releaseVideoStreamSession(leaseId);
202 }
203
204 for (const [sessionId, session] of sessions)
205 {
206 if (session.expiresAt > now) continue;
207 destroySession(sessionId);
208 }
209}
210
211function refreshSession (session)
212{
213 session.expiresAt = Date.now() + STREAM_SESSION_TTL_MS;
214}
215
216function refreshLease (lease)
217{
218 lease.expiresAt = Date.now() + STREAM_SESSION_TTL_MS;
219}
220
221function destroySession (sessionId)
222{
223 const session = sessions.get(sessionId);
224 if (!session) return false;
225
226 sessions.delete(sessionId);
227
228 if (reusableSessionIdBySource.get(session.sourcePath) === session.id)
229 {
230 reusableSessionIdBySource.delete(session.sourcePath);
231 }
232
233 if (session.leaseIds)
234 {
235 for (const leaseId of session.leaseIds)
236 {
237 leases.delete(leaseId);
238 }
239 session.leaseIds.clear();
240 }
241
242 if (session.idleTimer)
243 {
244 clearTimeout(session.idleTimer);
245 session.idleTimer = null;
246 }
247
248 try
249 {
250 if (session.process) session.process.kill('SIGTERM');
251 } catch
252 {
253 // Ignore process cleanup failures.
254 }
255
256 fs.rmSync(session.outputDir, { recursive: true, force: true });
257
258 // Only clear file-wide subtitle caches when no other active stream session
259 // for the same source file remains.
260 if (!hasActiveSessionForSourcePath(session.sourcePath))
261 {
262 clearCachesForFile(session.sourcePath);
263 }
264
265 return true;
266}
267
268function pruneImageSubtitleCachesForSession (session)
269{
270 if (!session || !session.sourcePath) return;
271
272 const slowestViewerTimeSeconds = slowestViewerTimeSecondsForSourcePath(session.sourcePath);
273 if (!Number.isFinite(slowestViewerTimeSeconds)) return;
274 pruneOldImageSubtitleCaches(session.sourcePath, slowestViewerTimeSeconds);
275}
276
277function hasActiveSessionForSourcePath (sourcePath)
278{
279 const wantedPath = String(sourcePath || '');
280 if (!wantedPath) return false;
281
282 for (const session of sessions.values())
283 {
284 if (session && session.sourcePath === wantedPath) return true;
285 }
286
287 return false;
288}
289
290function slowestViewerTimeSecondsForSourcePath (sourcePath)
291{
292 const wantedPath = String(sourcePath || '');
293 if (!wantedPath) return null;
294
295 let slowestIndex = null;
296 let hasActiveViewers = false;
297
298 for (const session of sessions.values())
299 {
300 if (!session || session.sourcePath !== wantedPath) continue;
301 if (!session.leaseIds || session.leaseIds.size === 0) continue;
302
303 hasActiveViewers = true;
304 const sessionSlowestIndex = slowestLeaseSegmentIndex(session);
305 if (!Number.isInteger(sessionSlowestIndex)) return null;
306 if (slowestIndex === null || sessionSlowestIndex < slowestIndex)
307 {
308 slowestIndex = sessionSlowestIndex;
309 }
310 }
311
312 if (!hasActiveViewers || slowestIndex === null) return null;
313 return slowestIndex * ESTIMATED_SEGMENT_DURATION_SECONDS;
314}
315
316function getSessionForLease (leaseId)
317{
318 const lease = leases.get(leaseId);
319 if (!lease) return null;
320
321 const session = sessions.get(lease.sessionId);
322 if (!session)
323 {
324 leases.delete(leaseId);
325 return null;
326 }
327
328 return { lease, session };
329}
330
331function createLeaseForSession (session)
332{
333 const leaseId = crypto.randomBytes(18).toString('hex');
334 const lease = {
335 id: leaseId,
336 sessionId: session.id,
337 expiresAt: Date.now() + STREAM_SESSION_TTL_MS,
338 lastActivityAt: Date.now(),
339 lastSegmentIndex: null,
340 };
341 leases.set(leaseId, lease);
342 session.leaseIds.add(leaseId);
343 return lease;
344}
345
346function touchSessionActivity (session, lease)
347{
348 refreshSession(session);
349 session.lastActivityAt = Date.now();
350
351 if (lease)
352 {
353 refreshLease(lease);
354 lease.lastActivityAt = Date.now();
355 }
356}
357
358function clearIdleStopTimer (session)
359{
360 if (!session.idleTimer) return;
361 clearTimeout(session.idleTimer);
362 session.idleTimer = null;
363}
364
365function scheduleIdleStop (session)
366{
367 clearIdleStopTimer(session);
368 if (session.activeRequests > 0 || (session.leaseIds && session.leaseIds.size > 0)) return;
369
370 session.idleTimer = setTimeout(() =>
371 {
372 const activeSession = sessions.get(session.id);
373 if (!activeSession) return;
374 if (activeSession.activeRequests > 0 || (activeSession.leaseIds && activeSession.leaseIds.size > 0)) return;
375 if (Date.now() - activeSession.lastActivityAt < STREAM_IDLE_STOP_MS)
376 {
377 scheduleIdleStop(activeSession);
378 return;
379 }
380 destroySession(activeSession.id);
381 }, STREAM_IDLE_STOP_MS);
382}
383
384function startAssetRequest (session, lease)
385{
386 session.activeRequests += 1;
387 touchSessionActivity(session, lease);
388 clearIdleStopTimer(session);
389}
390
391function finishAssetRequest (session, lease)
392{
393 if (session.activeRequests > 0) session.activeRequests -= 1;
394 touchSessionActivity(session, lease);
395 if (session.activeRequests === 0 && session.leaseIds.size === 0) scheduleIdleStop(session);
396}
397
398function parseSegmentIndexFromAssetPath (assetPath)
399{
400 const baseName = path.basename(String(assetPath || ''));
401 const match = baseName.match(/^stream_.+_segment_(\d+)\.ts$/i);
402 if (!match) return null;
403
404 const segmentIndex = Number.parseInt(match[1], 10);
405 return Number.isInteger(segmentIndex) ? segmentIndex : null;
406}
407
408function runSegmentPrune (session)
409{
410 if (session.segmentPruneRunning) return;
411 if (session.segmentPrunePendingCutoff <= session.segmentPrunedUntil) return;
412
413 const cutoff = session.segmentPrunePendingCutoff;
414 const pruneStartedAt = Date.now();
415 session.segmentPruneRunning = true;
416
417 fs.promises.readdir(session.outputDir)
418 .then((entries) =>
419 {
420 const removals = [];
421 for (const entryName of entries)
422 {
423 const match = String(entryName).match(/^stream_.+_segment_(\d+)\.ts$/i);
424 if (!match) continue;
425 const segmentIndex = Number.parseInt(match[1], 10);
426 if (!Number.isInteger(segmentIndex)) continue;
427 if (segmentIndex > cutoff) continue;
428
429 removals.push(path.join(session.outputDir, entryName));
430 }
431 return deleteFilesInBatches(removals, STREAM_PRUNE_DELETE_BATCH_SIZE);
432 })
433 .then((deletedCount) =>
434 {
435 const completedAt = Date.now();
436 session.segmentPrunedUntil = Math.max(session.segmentPrunedUntil, cutoff);
437 session.lastSegmentPrune = {
438 cutoff,
439 deletedSegments: deletedCount,
440 durationMs: Math.max(0, completedAt - pruneStartedAt),
441 completedAt,
442 };
443 pruneImageSubtitleCachesForSession(session);
444 if (session.segmentPrunedUntil >= 0 && reusableSessionIdBySource.get(session.sourcePath) === session.id)
445 {
446 reusableSessionIdBySource.delete(session.sourcePath);
447 }
448 })
449 .catch(() =>
450 {
451 // Best effort cleanup; keep streaming even if pruning fails.
452 })
453 .finally(() =>
454 {
455 session.segmentPruneRunning = false;
456 if (session.segmentPrunePendingCutoff > session.segmentPrunedUntil)
457 {
458 runSegmentPrune(session);
459 }
460 });
461}
462
463async function deleteFilesInBatches (pathsToDelete, batchSize)
464{
465 const paths = Array.isArray(pathsToDelete) ? pathsToDelete : [];
466 if (!paths.length) return 0;
467
468 let deletedCount = 0;
469 for (let index = 0; index < paths.length; index += batchSize)
470 {
471 const batch = paths.slice(index, index + batchSize);
472 await Promise.all(batch.map((targetPath) =>
473 fs.promises.unlink(targetPath)
474 .then(() =>
475 {
476 deletedCount += 1;
477 })
478 .catch(() =>
479 {
480 // Ignore races while ffmpeg rotates temp files and request handlers read segments.
481 })
482 ));
483 }
484
485 return deletedCount;
486}
487
488function slowestLeaseSegmentIndex (session)
489{
490 if (!session.leaseIds.size) return null;
491
492 let minIndex = null;
493 for (const leaseId of session.leaseIds)
494 {
495 const lease = leases.get(leaseId);
496 if (!lease) continue;
497 if (!Number.isInteger(lease.lastSegmentIndex)) return null;
498 if (minIndex === null || lease.lastSegmentIndex < minIndex) minIndex = lease.lastSegmentIndex;
499 }
500
501 return minIndex;
502}
503
504function scheduleSegmentPrune (session, lease, segmentIndex)
505{
506 if (!Number.isInteger(segmentIndex) || segmentIndex < 0) return;
507
508 lease.lastSegmentIndex = segmentIndex;
509 const slowestIndex = slowestLeaseSegmentIndex(session);
510 if (!Number.isInteger(slowestIndex)) return;
511
512 const pruneCutoff = slowestIndex - STREAM_SEGMENT_RETENTION_COUNT;
513 if (pruneCutoff <= session.segmentPrunedUntil) return;
514
515 session.segmentPrunePendingCutoff = Math.max(session.segmentPrunePendingCutoff, pruneCutoff);
516 runSegmentPrune(session);
517}
518
519function runCommand (command, args)
520{
521 return new Promise((resolve, reject) =>
522 {
523 const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'] });
524 let stdout = '';
525 let stderr = '';
526
527 child.stdout.on('data', (chunk) =>
528 {
529 stdout += chunk.toString();
530 });
531
532 child.stderr.on('data', (chunk) =>
533 {
534 stderr += chunk.toString();
535 });
536
537 child.on('error', (err) =>
538 {
539 if (err && err.code === 'ENOENT')
540 {
541 reject(makeHttpError(503, `${command} is not installed or not available in PATH.`));
542 return;
543 }
544 reject(err);
545 });
546
547 child.on('close', (code) =>
548 {
549 if (code === 0)
550 {
551 resolve({ stdout, stderr });
552 return;
553 }
554 reject(makeHttpError(500, stderr.trim() || `${command} exited with code ${code}.`));
555 });
556 });
557}
558
559async function probeInput (sourcePath)
560{
561 const { stdout } = await runCommand(ffprobePath(), [
562 '-v', 'error',
563 '-print_format', 'json',
564 '-show_streams',
565 '-show_format',
566 sourcePath,
567 ]);
568
569 let payload;
570 try
571 {
572 payload = JSON.parse(stdout || '{}');
573 } catch
574 {
575 throw makeHttpError(500, 'ffprobe returned invalid stream metadata.');
576 }
577
578 const typedIndexes = { video: 0, audio: 0, subtitle: 0 };
579 const streams = Array.isArray(payload.streams) ? payload.streams.map((stream) =>
580 {
581 const codecType = String(stream.codec_type || '').toLowerCase();
582 const relativeIndex = Object.prototype.hasOwnProperty.call(typedIndexes, codecType)
583 ? typedIndexes[codecType]++
584 : null;
585 return {
586 ...stream,
587 codecType,
588 relativeIndex,
589 tags: stream.tags || {},
590 disposition: stream.disposition || {},
591 };
592 }) : [];
593
594 return {
595 raw: payload,
596 durationSeconds: inputDurationSeconds(payload, streams),
597 video: streams.filter((stream) => stream.codecType === 'video'),
598 audio: streams.filter((stream) => stream.codecType === 'audio'),
599 subtitles: streams.filter((stream) => stream.codecType === 'subtitle'),
600 };
601}
602
603function parsePositiveNumber (value)
604{
605 const number = Number(value);
606 return Number.isFinite(number) && number > 0 ? number : null;
607}
608
609function parseDurationTagSeconds (value)
610{
611 const raw = String(value || '').trim();
612 if (!raw) return null;
613
614 const numeric = parsePositiveNumber(raw);
615 if (numeric) return numeric;
616
617 const match = raw.match(/^(\d+):([0-5]?\d):([0-5]?\d(?:\.\d+)?)$/);
618 if (!match) return null;
619
620 const hours = Number(match[1]);
621 const minutes = Number(match[2]);
622 const seconds = Number(match[3]);
623 if (!Number.isFinite(hours) || !Number.isFinite(minutes) || !Number.isFinite(seconds)) return null;
624 return hours * 3600 + minutes * 60 + seconds;
625}
626
627function inputDurationSeconds (payload, streams)
628{
629 const formatDuration = parsePositiveNumber(payload && payload.format && payload.format.duration);
630 if (formatDuration) return formatDuration;
631
632 for (const stream of streams)
633 {
634 const duration = parsePositiveNumber(stream.duration);
635 if (duration) return duration;
636 }
637
638 const formatTags = payload && payload.format && payload.format.tags;
639 const formatTagDuration = formatTags && parseDurationTagSeconds(formatTags.duration || formatTags.DURATION);
640 if (formatTagDuration) return formatTagDuration;
641
642 for (const stream of streams)
643 {
644 const tagDuration = parseDurationTagSeconds(stream.tags.duration || stream.tags.DURATION);
645 if (tagDuration) return tagDuration;
646 }
647
648 return null;
649}
650
651function sanitiseTrackValue (value, fallback)
652{
653 const cleaned = String(value || '').trim().replace(/[^A-Za-z0-9._-]+/g, '-').replace(/^-+|-+$/g, '');
654 return cleaned || fallback;
655}
656
657function trackLanguage (stream)
658{
659 return sanitiseTrackValue(stream.tags.language || stream.tags.LANGUAGE || 'und', 'und').slice(0, 8);
660}
661
662function trackLabel (stream, fallback)
663{
664 const title = String(stream.tags.title || stream.tags.handler_name || '').trim();
665 if (title) return title;
666 const language = trackLanguage(stream);
667 if (language && language !== 'und') return `${fallback} (${language})`;
668 return fallback;
669}
670
671function findMatchingSidecarSrtPath (sourcePath)
672{
673 const absoluteSourcePath = path.resolve(String(sourcePath || ''));
674 const dirPath = path.dirname(absoluteSourcePath);
675 const baseName = path.parse(absoluteSourcePath).name;
676 const exactPath = path.join(dirPath, `${baseName}.srt`);
677
678 try
679 {
680 const exactStat = fs.statSync(exactPath);
681 if (exactStat.isFile()) return exactPath;
682 } catch
683 {
684 // Fall back to case-insensitive directory scan.
685 }
686
687 const wantedName = `${baseName}.srt`.toLowerCase();
688
689 try
690 {
691 const entries = fs.readdirSync(dirPath, { withFileTypes: true });
692 for (const entry of entries)
693 {
694 if (!entry.isFile()) continue;
695 if (entry.name.toLowerCase() !== wantedName) continue;
696 return path.join(dirPath, entry.name);
697 }
698 } catch
699 {
700 return null;
701 }
702
703 return null;
704}
705
706async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
707{
708 const extractedTracks = [];
709 const imageBasedTracks = [];
710 let skippedCount = 0;
711 let trackNumber = 0;
712
713 for (const stream of subtitleStreams)
714 {
715 const codecName = String(stream.codec_name || '').toLowerCase();
716
717 // Image-based subtitle formats will be shown as DOM overlays via frame extraction.
718 if (IMAGE_BASED_SUBTITLE_CODECS.has(codecName))
719 {
720 imageBasedTracks.push({
721 label: trackLabel(stream, `Subtitle ${stream.relativeIndex + 1}`),
722 language: trackLanguage(stream),
723 isDefault: !!stream.disposition.default,
724 streamIndex: stream.relativeIndex,
725 });
726 continue;
727 }
728
729 const fileName = `subtitle-${trackNumber}.vtt`;
730 const targetPath = path.join(outputDir, fileName);
731
732 try
733 {
734 await runCommand(ffmpegPath(), [
735 '-hide_banner',
736 '-loglevel', 'error',
737 '-nostdin',
738 '-y',
739 '-i', sourcePath,
740 '-map', `0:s:${stream.relativeIndex}`,
741 '-c:s', 'webvtt',
742 targetPath,
743 ]);
744 } catch
745 {
746 skippedCount += 1;
747 continue;
748 }
749
750 extractedTracks.push({
751 fileName,
752 label: trackLabel(stream, `Subtitle ${trackNumber + 1}`),
753 language: trackLanguage(stream),
754 isDefault: !!stream.disposition.default,
755 });
756 trackNumber += 1;
757 }
758
759 const sidecarSrtPath = findMatchingSidecarSrtPath(sourcePath);
760 const hasSidecarSrt = !!sidecarSrtPath;
761 if (sidecarSrtPath)
762 {
763 const fileName = `subtitle-${trackNumber}.vtt`;
764 const targetPath = path.join(outputDir, fileName);
765 try
766 {
767 await runCommand(ffmpegPath(), [
768 '-hide_banner',
769 '-loglevel', 'error',
770 '-nostdin',
771 '-y',
772 '-i', sidecarSrtPath,
773 '-c:s', 'webvtt',
774 targetPath,
775 ]);
776
777 extractedTracks.push({
778 fileName,
779 label: `Subtitle ${trackNumber + 1} (external)`,
780 language: 'und',
781 isDefault: extractedTracks.length === 0 && imageBasedTracks.length === 0,
782 });
783 trackNumber += 1;
784 } catch
785 {
786 skippedCount += 1;
787 }
788 }
789
790 return {
791 tracks: extractedTracks,
792 imageBasedTracks,
793 hasSidecarSrt,
794 skippedCount,
795 };
796}
797
798function buildHlsArgs (sourcePath, outputDir, streamInfo)
799{
800 const args = [
801 '-hide_banner',
802 '-loglevel', 'error',
803 '-nostdin',
804 '-y',
805 '-i', sourcePath,
806 '-map', '0:v:0',
807 ];
808
809 for (const audioStream of streamInfo.audio)
810 {
811 args.push('-map', `0:a:${audioStream.relativeIndex}`);
812 }
813
814 args.push(
815 '-c:v', 'libx264',
816 '-preset', 'veryfast',
817 '-crf', '23',
818 '-pix_fmt', 'yuv420p',
819 '-g', '48',
820 '-sc_threshold', '0',
821 );
822
823 if (streamInfo.audio.length)
824 {
825 args.push('-c:a', 'aac', '-b:a', '160k', '-ac', '2');
826 } else
827 {
828 args.push('-an');
829 }
830
831 const variantMap = [];
832 if (streamInfo.audio.length)
833 {
834 variantMap.push('v:0,agroup:audio,name:video');
835 streamInfo.audio.forEach((audioStream, index) =>
836 {
837 let entry = `a:${index},agroup:audio,name:${sanitiseTrackValue(trackLabel(audioStream, `Audio ${index + 1}`), `audio-${index + 1}`)},language:${trackLanguage(audioStream)}`;
838 if (audioStream.disposition.default) entry += ',default:yes';
839 variantMap.push(entry);
840 });
841 } else
842 {
843 variantMap.push('v:0,name:video');
844 }
845
846 args.push(
847 '-f', 'hls',
848 '-hls_time', '4',
849 '-hls_list_size', '0',
850 '-hls_flags', 'independent_segments+temp_file',
851 '-master_pl_name', 'master.m3u8',
852 '-hls_segment_filename', path.join(outputDir, 'stream_%v_segment_%06d.ts'),
853 '-var_stream_map', variantMap.join(' '),
854 path.join(outputDir, 'stream_%v.m3u8'),
855 );
856
857 return args;
858}
859
860function startHlsTranscode (session, streamInfo)
861{
862 const args = buildHlsArgs(session.sourcePath, session.outputDir, streamInfo);
863 const child = spawn(ffmpegPath(), args, { stdio: ['ignore', 'ignore', 'pipe'] });
864 let stderr = '';
865
866 child.stderr.on('data', (chunk) =>
867 {
868 stderr += chunk.toString();
869 if (stderr.length > 16000) stderr = stderr.slice(-16000);
870 });
871
872 child.on('error', (err) =>
873 {
874 if (err && err.code === 'ENOENT')
875 {
876 session.error = makeHttpError(503, 'ffmpeg is not installed or not available in PATH.');
877 return;
878 }
879 session.error = err;
880 });
881
882 child.on('close', (code) =>
883 {
884 session.process = null;
885 if (code === 0 || session.error) return;
886 session.error = makeHttpError(500, stderr.trim() || `ffmpeg exited with code ${code}.`);
887 });
888
889 session.process = child;
890}
891
892function waitForFile (targetPath, session, timeoutMs = STREAM_READY_TIMEOUT_MS)
893{
894 const startedAt = Date.now();
895
896 return new Promise((resolve, reject) =>
897 {
898 const check = () =>
899 {
900 if (fs.existsSync(targetPath))
901 {
902 resolve();
903 return;
904 }
905
906 if (session.error)
907 {
908 reject(session.error);
909 return;
910 }
911
912 if (Date.now() - startedAt >= timeoutMs)
913 {
914 reject(makeHttpError(504, 'Timed out while preparing the video stream.'));
915 return;
916 }
917
918 setTimeout(check, 120);
919 };
920
921 check();
922 });
923}
924
925function waitForStreamStart (session, timeoutMs = STREAM_READY_TIMEOUT_MS)
926{
927 const startedAt = Date.now();
928
929 return new Promise((resolve, reject) =>
930 {
931 const check = () =>
932 {
933 if (fs.existsSync(path.join(session.outputDir, 'master.m3u8')) || hasInitialHlsAssets(session.outputDir))
934 {
935 resolve();
936 return;
937 }
938
939 if (session.error)
940 {
941 reject(session.error);
942 return;
943 }
944
945 if (!session.process)
946 {
947 reject(makeHttpError(500, 'Video stream preparation stopped unexpectedly.'));
948 return;
949 }
950
951 if (Date.now() - startedAt >= timeoutMs)
952 {
953 reject(makeHttpError(504, 'Timed out while preparing the video stream.'));
954 return;
955 }
956
957 setTimeout(check, 120);
958 };
959
960 check();
961 });
962}
963
964async function initialiseSession (session)
965{
966 const streamInfo = await probeInput(session.sourcePath);
967 if (!streamInfo.video.length)
968 {
969 throw makeHttpError(400, 'File does not contain a video stream.');
970 }
971
972 const primaryVideoStream = streamInfo.video[0] || {};
973 session.videoWidth = parsePositiveNumber(primaryVideoStream.width);
974 session.videoHeight = parsePositiveNumber(primaryVideoStream.height);
975
976 const subtitleResult = await extractSubtitleTracks(session.sourcePath, session.outputDir, streamInfo.subtitles);
977 session.subtitleTracks = subtitleResult.tracks;
978 session.imageSubtitleTracks = subtitleResult.imageBasedTracks;
979 session.subtitleSummary = {
980 total: streamInfo.subtitles.length + (subtitleResult.hasSidecarSrt ? 1 : 0),
981 converted: subtitleResult.tracks.length,
982 imageBased: subtitleResult.imageBasedTracks.length,
983 skipped: subtitleResult.skippedCount,
984 };
985 startHlsTranscode(session, streamInfo);
986 session.durationSeconds = streamInfo.durationSeconds;
987 await waitForStreamStart(session);
988}
989
990async function createVideoStreamSession (sourcePath)
991{
992 pruneExpiredSessions();
993 ensureStreamRootDir();
994
995 const sourceKey = String(sourcePath || '');
996 let session = null;
997
998 const reusableSessionId = reusableSessionIdBySource.get(sourceKey);
999 if (reusableSessionId)
1000 {
1001 const reusable = sessions.get(reusableSessionId);
1002 if (reusable && !reusable.error && reusable.segmentPrunedUntil < 0)
1003 {
1004 session = reusable;
1005 } else
1006 {
1007 reusableSessionIdBySource.delete(sourceKey);
1008 }
1009 }
1010
1011 if (!session)
1012 {
1013 const sessionId = crypto.randomBytes(18).toString('hex');
1014 const outputDir = path.join(STREAM_ROOT_DIR, sessionId);
1015 fs.mkdirSync(outputDir, { recursive: true });
1016
1017 session = {
1018 id: sessionId,
1019 sourcePath: String(sourcePath || ''),
1020 sourceKey: sourceKey,
1021 outputDir,
1022 expiresAt: Date.now() + STREAM_SESSION_TTL_MS,
1023 process: null,
1024 error: null,
1025 subtitleTracks: [],
1026 imageSubtitleTracks: [],
1027 subtitleSummary: { total: 0, converted: 0, imageBased: 0, skipped: 0 },
1028 readyPromise: null,
1029 activeRequests: 0,
1030 lastActivityAt: Date.now(),
1031 idleTimer: null,
1032 segmentPruneRunning: false,
1033 segmentPrunePendingCutoff: -1,
1034 segmentPrunedUntil: -1,
1035 lastSegmentPrune: null,
1036 leaseIds: new Set(),
1037 };
1038
1039 session.readyPromise = initialiseSession(session).catch((err) =>
1040 {
1041 session.error = err;
1042 throw err;
1043 });
1044
1045 sessions.set(session.id, session);
1046 reusableSessionIdBySource.set(sourceKey, session.id);
1047 }
1048
1049 if (!session.leaseIds) session.leaseIds = new Set();
1050 const lease = createLeaseForSession(session);
1051 touchSessionActivity(session, lease);
1052
1053 try
1054 {
1055 await session.readyPromise;
1056 } catch (err)
1057 {
1058 releaseVideoStreamSession(lease.id);
1059 throw err;
1060 }
1061
1062 return {
1063 sessionId: lease.id,
1064 durationSeconds: session.durationSeconds,
1065 videoWidth: session.videoWidth || null,
1066 videoHeight: session.videoHeight || null,
1067 subtitleTracks: session.subtitleTracks.map((track) => ({ ...track })),
1068 imageSubtitleTracks: (session.imageSubtitleTracks || []).map((track) => ({ ...track })),
1069 subtitleSummary: {
1070 total: Number(session.subtitleSummary.total || 0),
1071 converted: Number(session.subtitleSummary.converted || 0),
1072 imageBased: Number(session.subtitleSummary.imageBased || 0),
1073 skipped: Number(session.subtitleSummary.skipped || 0),
1074 },
1075 };
1076}
1077
1078function contentTypeForAsset (assetPath)
1079{
1080 const ext = path.extname(assetPath).toLowerCase();
1081 if (ext === '.m3u8') return 'application/vnd.apple.mpegurl';
1082 if (ext === '.ts') return 'video/mp2t';
1083 if (ext === '.vtt') return 'text/vtt; charset=utf-8';
1084 if (ext === '.aac') return 'audio/aac';
1085 if (ext === '.mp4' || ext === '.m4s') return 'video/mp4';
1086 return 'application/octet-stream';
1087}
1088
1089async function sendVideoStreamAsset (sessionId, assetPath, req, res)
1090{
1091 pruneExpiredSessions();
1092 const active = getSessionForLease(sessionId);
1093 if (!active) throw makeHttpError(404, 'Video stream session not found.');
1094
1095 const { session, lease } = active;
1096
1097 touchSessionActivity(session, lease);
1098 await session.readyPromise;
1099
1100 const relativeAssetPath = String(assetPath || 'master.m3u8').replace(/^[/\\]+/, '');
1101 const resolvedPath = path.resolve(session.outputDir, relativeAssetPath);
1102 const resolvedRoot = path.resolve(session.outputDir);
1103 if (resolvedPath !== resolvedRoot && !resolvedPath.startsWith(resolvedRoot + path.sep))
1104 {
1105 throw makeHttpError(404, 'Stream asset not found.');
1106 }
1107
1108 const segmentIndex = parseSegmentIndexFromAssetPath(relativeAssetPath);
1109 if (segmentIndex !== null) scheduleSegmentPrune(session, lease, segmentIndex);
1110
1111 await waitForFile(resolvedPath, session);
1112 res.set('Cache-Control', 'private, no-store');
1113 res.type(contentTypeForAsset(resolvedPath));
1114
1115 startAssetRequest(session, lease);
1116
1117 let settled = false;
1118 const finish = () =>
1119 {
1120 if (settled) return;
1121 settled = true;
1122 finishAssetRequest(session, lease);
1123 };
1124
1125 req.on('aborted', finish);
1126 res.on('close', finish);
1127 res.on('finish', finish);
1128 res.on('error', finish);
1129
1130 await new Promise((resolve, reject) =>
1131 {
1132 res.sendFile(resolvedPath, (err) =>
1133 {
1134 finish();
1135 if (!err)
1136 {
1137 resolve();
1138 return;
1139 }
1140
1141 if (err.code === 'ECONNABORTED' || err.message === 'Request aborted')
1142 {
1143 resolve();
1144 return;
1145 }
1146
1147 reject(err);
1148 });
1149 });
1150}
1151
1152function releaseVideoStreamSession (sessionId)
1153{
1154 const leaseId = String(sessionId || '');
1155 const active = getSessionForLease(leaseId);
1156 if (!active)
1157 {
1158 leases.delete(leaseId);
1159 return false;
1160 }
1161
1162 const { session } = active;
1163 leases.delete(leaseId);
1164 session.leaseIds.delete(leaseId);
1165
1166 if (session.leaseIds.size === 0 && session.activeRequests === 0)
1167 {
1168 return destroySession(session.id);
1169 }
1170
1171 return true;
1172}
1173
1174function collectOutputDirStats (outputDir)
1175{
1176 const stats = {
1177 exists: false,
1178 totalBytes: 0,
1179 totalFiles: 0,
1180 segmentFiles: 0,
1181 segmentBytes: 0,
1182 playlistFiles: 0,
1183 subtitleFiles: 0,
1184 };
1185
1186 if (!outputDir || !fs.existsSync(outputDir)) return stats;
1187 stats.exists = true;
1188
1189 let entries = [];
1190 try
1191 {
1192 entries = fs.readdirSync(outputDir, { withFileTypes: true });
1193 } catch
1194 {
1195 return stats;
1196 }
1197
1198 for (const entry of entries)
1199 {
1200 if (!entry.isFile()) continue;
1201 const name = entry.name;
1202 const fullPath = path.join(outputDir, name);
1203 let size = 0;
1204 try
1205 {
1206 const fileStat = fs.statSync(fullPath);
1207 size = Number(fileStat.size) || 0;
1208 } catch
1209 {
1210 size = 0;
1211 }
1212
1213 stats.totalFiles += 1;
1214 stats.totalBytes += size;
1215
1216 if (/\.ts$/i.test(name))
1217 {
1218 stats.segmentFiles += 1;
1219 stats.segmentBytes += size;
1220 continue;
1221 }
1222
1223 if (/\.m3u8$/i.test(name))
1224 {
1225 stats.playlistFiles += 1;
1226 continue;
1227 }
1228
1229 if (/\.(sup|vtt)$/i.test(name))
1230 {
1231 stats.subtitleFiles += 1;
1232 }
1233 }
1234
1235 return stats;
1236}
1237
1238function getVideoStreamDebugSnapshot ()
1239{
1240 const activeStreams = [];
1241
1242 for (const [sessionId, session] of sessions.entries())
1243 {
1244 activeStreams.push({
1245 sessionId,
1246 sourcePath: session.sourcePath,
1247 outputDir: session.outputDir,
1248 viewers: session.leaseIds ? session.leaseIds.size : 0,
1249 activeRequests: Number(session.activeRequests || 0),
1250 expiresAt: Number(session.expiresAt || 0),
1251 segmentRetentionCount: STREAM_SEGMENT_RETENTION_COUNT,
1252 segmentPruneDeleteBatchSize: STREAM_PRUNE_DELETE_BATCH_SIZE,
1253 segmentPrunePendingCutoff: Number(session.segmentPrunePendingCutoff || 0),
1254 segmentPrunedUntil: Number(session.segmentPrunedUntil || 0),
1255 slowestViewerSegmentIndex: slowestLeaseSegmentIndex(session) ?? -1,
1256 lastSegmentPrune: session.lastSegmentPrune || null,
1257 output: collectOutputDirStats(session.outputDir),
1258 });
1259 }
1260
1261 const knownSessionDirs = new Set(activeStreams.map((stream) => path.resolve(stream.outputDir || '')));
1262 const orphanedOutputDirs = [];
1263 try
1264 {
1265 const entries = fs.readdirSync(STREAM_ROOT_DIR, { withFileTypes: true });
1266 for (const entry of entries)
1267 {
1268 if (!entry.isDirectory()) continue;
1269 const dirPath = path.resolve(path.join(STREAM_ROOT_DIR, entry.name));
1270 if (knownSessionDirs.has(dirPath)) continue;
1271 orphanedOutputDirs.push({
1272 outputDir: dirPath,
1273 output: collectOutputDirStats(dirPath),
1274 });
1275 }
1276 } catch
1277 {
1278 // Stream root may not exist yet.
1279 }
1280
1281 return {
1282 streamRootDir: STREAM_ROOT_DIR,
1283 streamSegmentRetentionCount: STREAM_SEGMENT_RETENTION_COUNT,
1284 streamSegmentRetentionMax: MAX_STREAM_SEGMENT_RETENTION_COUNT,
1285 streamPruneDeleteBatchSize: STREAM_PRUNE_DELETE_BATCH_SIZE,
1286 activeSessionCount: sessions.size,
1287 activeLeaseCount: leases.size,
1288 activeStreams,
1289 orphanedOutputDirs,
1290 };
1291}
1292
1293module.exports = {
1294 STREAM_ROOT_DIR,
1295 STREAM_SESSION_TTL_MS,
1296 STREAM_IDLE_STOP_MS,
1297 STREAM_SEGMENT_RETENTION_COUNT,
1298 createVideoStreamSession,
1299 sendVideoStreamAsset,
1300 releaseVideoStreamSession,
1301 getVideoStreamDebugSnapshot,
1302 cleanupIdleStreamArtifacts,
1303};