BlueGit

timfa filemanager

Files

filemanager / server / video-stream.js

video-stream.js

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