BlueGit

timfa filemanager

Files

filemanager / server / image-subtitles.js

image-subtitles.js

Reduce subtitle buffer stalls
timfa Tim Falken · pushed by timfa · 5 months ago
f21c3881 History
image-subtitles.js 24.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 { Readable } = require('stream');
9const sharp = require('sharp');
10
11const SUBTITLE_CACHE_DIR = path.join(os.tmpdir(), 'filemanager-video-subtitles');
12const EVENT_CACHE_TTL_MS = toPositiveInt(process.env.IMAGE_SUBTITLE_EVENT_CACHE_TTL_MS, 5 * 60 * 1000);
13const FRAME_CACHE_TTL_MS = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_TTL_MS, 2 * 60 * 1000);
14const MAX_EVENT_CACHE_ITEMS = toCacheItemLimit(process.env.IMAGE_SUBTITLE_EVENT_CACHE_MAX_ITEMS, Infinity);
15const MAX_FRAME_CACHE_ITEMS = toCacheItemLimit(process.env.IMAGE_SUBTITLE_FRAME_CACHE_MAX_ITEMS, Infinity);
16// Removed byte size cap for event cache to prevent thrashing
17const MAX_EVENT_CACHE_BYTES = toPositiveInt(process.env.IMAGE_SUBTITLE_EVENT_CACHE_MAX_BYTES, 50 * 1024 * 1024);
18const MIN_EVENT_WINDOW_SECONDS = 30; // Always keep at least this much for seeking
19const MAX_FRAME_CACHE_BYTES = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_MAX_BYTES, 48 * 1024 * 1024);
20const IMAGE_SUBTITLE_RENDER_SCALE = toBoundedNumber(process.env.IMAGE_SUBTITLE_RENDER_SCALE, 0.75, 0.1, 1);
21const PGS_TIMEBASE = 90000;
22const IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS = toPositiveInt(process.env.IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS, 5);
23
24const eventCache = createCacheState(MAX_EVENT_CACHE_ITEMS, MAX_EVENT_CACHE_BYTES);
25const frameCache = createCacheState(MAX_FRAME_CACHE_ITEMS, MAX_FRAME_CACHE_BYTES);
26const subtitleDebugStats = createSubtitleDebugStats();
27
28let parseDisplaySets = null;
29
30function createDurationStats ()
31{
32 return {
33 count: 0,
34 totalMs: 0,
35 maxMs: 0,
36 lastMs: 0,
37 };
38}
39
40function createSubtitleDebugStats ()
41{
42 return {
43 eventCacheHits: 0,
44 eventCacheMisses: 0,
45 eventCachePendingHits: 0,
46 eventCacheBuilds: 0,
47 eventCacheBuildErrors: 0,
48 frameCacheHits: 0,
49 frameCacheMisses: 0,
50 frameRenders: 0,
51 eventBuildDurationMs: createDurationStats(),
52 frameRenderDurationMs: createDurationStats(),
53 frameRequestDurationMs: createDurationStats(),
54 };
55}
56
57function recordDuration (stats, startedAt)
58{
59 if (!stats || !Number.isFinite(startedAt)) return 0;
60
61 const elapsedMs = Math.max(0, Date.now() - startedAt);
62 stats.count += 1;
63 stats.totalMs += elapsedMs;
64 stats.lastMs = elapsedMs;
65 if (elapsedMs > stats.maxMs) stats.maxMs = elapsedMs;
66 return elapsedMs;
67}
68
69function averageDurationMs (stats)
70{
71 if (!stats || !stats.count) return 0;
72 return Number((stats.totalMs / stats.count).toFixed(1));
73}
74
75function debugLimitValue (value)
76{
77 return Number.isFinite(value) ? value : null;
78}
79
80function ensureImageDataGlobal ()
81{
82 if (typeof globalThis.ImageData === 'function') return;
83
84 class ImageDataPolyfill
85 {
86 constructor(data, width, height)
87 {
88 this.data = data;
89 this.width = width;
90 this.height = height;
91 }
92 }
93
94 globalThis.ImageData = ImageDataPolyfill;
95}
96
97function loadPgsParser ()
98{
99 if (parseDisplaySets) return parseDisplaySets;
100 ensureImageDataGlobal();
101 ({ parseDisplaySets } = require('pgs-parser'));
102 return parseDisplaySets;
103}
104
105function ffmpegPath ()
106{
107 return process.env.FFMPEG_PATH || 'ffmpeg';
108}
109
110function ensureSubtitleCacheDir ()
111{
112 fs.mkdirSync(SUBTITLE_CACHE_DIR, { recursive: true });
113}
114
115function toPositiveInt (value, fallback)
116{
117 const number = Number.parseInt(String(value || ''), 10);
118 if (Number.isInteger(number) && number > 0) return number;
119 return fallback;
120}
121
122function toCacheItemLimit (value, fallback)
123{
124 const raw = String(value || '').trim().toLowerCase();
125 if (!raw) return fallback;
126 if (raw === '0' || raw === 'none' || raw === 'infinity' || raw === 'inf' || raw === 'unbounded' || raw === 'unlimited')
127 {
128 return Infinity;
129 }
130
131 const number = Number.parseInt(raw, 10);
132 if (Number.isInteger(number) && number > 0) return number;
133 return fallback;
134}
135
136function toBoundedNumber (value, fallback, min, max)
137{
138 const numeric = Number.parseFloat(String(value || ''));
139 if (!Number.isFinite(numeric)) return fallback;
140 if (numeric < min || numeric > max) return fallback;
141 return numeric;
142}
143
144function hashSubtitleKey (filePath, streamIndex, stat)
145{
146 return crypto
147 .createHash('sha1')
148 .update(`${filePath}|${streamIndex}|${stat.size}|${Math.floor(stat.mtimeMs)}`)
149 .digest('hex');
150}
151
152function createCacheState (maxItems, maxBytes)
153{
154 return {
155 map: new Map(),
156 totalBytes: 0,
157 maxItems,
158 maxBytes,
159 };
160}
161
162function normaliseFilePath (filePath)
163{
164 return path.resolve(String(filePath || '').trim());
165}
166
167function cachePrefixForFile (filePath)
168{
169 const normalised = String(filePath || '').trim();
170 if (!normalised) return '';
171 return `${normaliseFilePath(normalised)}|`;
172}
173
174function eventIdentityKey (filePath, streamIndex, stat)
175{
176 const normalisedPath = normaliseFilePath(filePath);
177 const hash = hashSubtitleKey(normalisedPath, streamIndex, stat);
178 return `${normalisedPath}|${streamIndex}|${hash}`;
179}
180
181function eventCacheFileKey (identityKey)
182{
183 const hash = crypto.createHash('sha1').update(String(identityKey || '')).digest('hex');
184 return `event-${hash}`;
185}
186
187function toValidCacheSize (value)
188{
189 const number = Number(value);
190 if (!Number.isFinite(number) || number <= 0) return 0;
191 return Math.floor(number);
192}
193
194function estimateEventCacheBytes (value)
195{
196 if (!value) return 0;
197 if (typeof value.then === 'function') return 1024;
198 if (!Array.isArray(value.events)) return 1024;
199
200 let bytes = 1024;
201 for (const event of value.events)
202 {
203 if (event && Buffer.isBuffer(event.imagePng)) bytes += event.imagePng.length;
204 bytes += 160;
205 }
206 return bytes;
207}
208
209function estimateFrameCacheBytes (value)
210{
211 if (!value) return 0;
212 if (Buffer.isBuffer(value)) return value.length;
213 if (value && Buffer.isBuffer(value.frame)) return value.frame.length + 96;
214 return 1024;
215}
216
217function deleteCachedValue (cache, key)
218{
219 const entry = cache.map.get(key);
220 if (!entry) return false;
221
222 cache.map.delete(key);
223 cache.totalBytes = Math.max(0, cache.totalBytes - toValidCacheSize(entry.sizeBytes));
224 return true;
225}
226
227function touchCachedEntry (cache, key, ttlMs)
228{
229 const entry = cache.map.get(key);
230 if (!entry) return;
231 entry.expiresAt = Date.now() + ttlMs;
232 cache.map.delete(key);
233 cache.map.set(key, entry);
234}
235
236function getCachedValue (cache, key)
237{
238 const now = Date.now();
239 const entry = cache.map.get(key);
240 if (!entry) return null;
241
242 if (entry.expiresAt <= now)
243 {
244 deleteCachedValue(cache, key);
245 return null;
246 }
247
248 cache.map.delete(key);
249 cache.map.set(key, entry);
250 return entry.value;
251}
252
253function setCachedValue (cache, key, value, ttlMs, estimateBytes)
254{
255 const now = Date.now();
256 deleteCachedValue(cache, key);
257
258 const sizeBytes = toValidCacheSize(typeof estimateBytes === 'function' ? estimateBytes(value) : 0);
259
260 cache.map.set(key, {
261 value,
262 expiresAt: now + ttlMs,
263 sizeBytes,
264 });
265 cache.totalBytes += sizeBytes;
266
267 while (cache.map.size > cache.maxItems || cache.maxBytes !== Infinity && cache.totalBytes > cache.maxBytes)
268 {
269 const oldestKey = cache.map.keys().next().value;
270 deleteCachedValue(cache, oldestKey);
271 }
272}
273
274function runCommand (command, args)
275{
276 return new Promise((resolve, reject) =>
277 {
278 const child = spawn(command, args, { stdio: ['ignore', 'ignore', 'pipe'] });
279 let stderr = '';
280
281 child.stderr.on('data', (chunk) =>
282 {
283 stderr += chunk.toString();
284 if (stderr.length > 16000) stderr = stderr.slice(-16000);
285 });
286
287 child.on('error', reject);
288 child.on('close', (code) =>
289 {
290 if (code === 0)
291 {
292 resolve();
293 return;
294 }
295 reject(new Error(stderr.trim() || `ffmpeg exited with code ${code}`));
296 });
297 });
298}
299
300async function extractSupFile (sourcePath, streamIndex, cacheKey)
301{
302 ensureSubtitleCacheDir();
303 const supPath = path.join(SUBTITLE_CACHE_DIR, `${cacheKey}.sup`);
304
305 if (fs.existsSync(supPath) && fs.statSync(supPath).size > 0)
306 {
307 return supPath;
308 }
309
310 await runCommand(ffmpegPath(), [
311 '-hide_banner',
312 '-loglevel', 'error',
313 '-nostdin',
314 '-y',
315 '-i', sourcePath,
316 '-map', `0:s:${streamIndex}`,
317 '-c:s', 'copy',
318 '-f', 'sup',
319 supPath,
320 ]);
321
322 return supPath;
323}
324
325function toSeconds (pgsTimestamp)
326{
327 const numeric = Number(pgsTimestamp);
328 if (!Number.isFinite(numeric) || numeric < 0) return 0;
329 return numeric / PGS_TIMEBASE;
330}
331
332function parseDisplaySetTimestamp (displaySet)
333{
334 if (displaySet && displaySet.presentationCompositionSegment && displaySet.presentationCompositionSegment.header)
335 {
336 return toSeconds(displaySet.presentationCompositionSegment.header.presentationTimestamp);
337 }
338 if (displaySet && displaySet.endDefinitionSegment && displaySet.endDefinitionSegment.header)
339 {
340 return toSeconds(displaySet.endDefinitionSegment.header.presentationTimestamp);
341 }
342 return 0;
343}
344
345async function parseSupEvents (supPath)
346{
347 const parserFactory = loadPgsParser();
348 if (typeof Readable.toWeb !== 'function')
349 {
350 throw new Error('Node.js stream bridge is unavailable for subtitle parsing.');
351 }
352
353 const subtitleStream = Readable.toWeb(fs.createReadStream(supPath));
354 const parserStream = subtitleStream.pipeThrough(parserFactory());
355 const reader = parserStream.getReader();
356
357 const events = [];
358 let currentEvent = null;
359 let scratchBuffer = null;
360
361 while (true)
362 {
363 const { done, value: displaySet } = await reader.read();
364 if (done) break;
365
366 const timestampSeconds = parseDisplaySetTimestamp(displaySet);
367
368 if (currentEvent && timestampSeconds > currentEvent.startTimeSeconds)
369 {
370 currentEvent.endTimeSeconds = timestampSeconds;
371 events.push(currentEvent);
372 currentEvent = null;
373 }
374
375 if (!displaySet || !Array.isArray(displaySet.objectDefinitionSegments) || displaySet.objectDefinitionSegments.length === 0)
376 {
377 continue;
378 }
379
380 const pcs = displaySet.presentationCompositionSegment;
381 if (!pcs) continue;
382
383 const neededPixels = Number(pcs.width || 0) * Number(pcs.height || 0) * 4;
384 if (!scratchBuffer || !Number.isInteger(neededPixels) || neededPixels <= 0 || scratchBuffer.length < neededPixels)
385 {
386 scratchBuffer = neededPixels > 0 ? new Uint8ClampedArray(neededPixels) : null;
387 }
388
389 let imageData;
390 try
391 {
392 imageData = displaySet.imageData(scratchBuffer || undefined);
393 } catch
394 {
395 continue;
396 }
397
398 if (!imageData || !imageData.data || !Number.isInteger(imageData.width) || !Number.isInteger(imageData.height))
399 {
400 continue;
401 }
402
403 const rawPixels = Buffer.from(imageData.data);
404 const imagePng = await sharp(rawPixels, {
405 raw: {
406 width: imageData.width,
407 height: imageData.height,
408 channels: 4,
409 },
410 }).png().toBuffer();
411
412 currentEvent = {
413 startTimeSeconds: timestampSeconds,
414 endTimeSeconds: Number.POSITIVE_INFINITY,
415 screenWidth: toPositiveInt(pcs.width, imageData.width),
416 screenHeight: toPositiveInt(pcs.height, imageData.height),
417 left: toPositiveInt(pcs.objectHorizontalPosition, 0),
418 top: toPositiveInt(pcs.objectVerticalPosition, 0),
419 imageWidth: imageData.width,
420 imageHeight: imageData.height,
421 imagePng,
422 };
423 }
424
425 if (currentEvent)
426 {
427 events.push(currentEvent);
428 }
429
430 return events;
431}
432
433async function buildEventCacheEntry (filePath, streamIndex, identityKey)
434{
435 const subtitleFileCacheKey = eventCacheFileKey(identityKey);
436 const supPath = await extractSupFile(filePath, streamIndex, subtitleFileCacheKey);
437 const events = await parseSupEvents(supPath);
438 return {
439 cacheKey: identityKey,
440 events,
441 };
442}
443
444async function getEventCacheEntry (filePath, streamIndex)
445{
446 const normalisedPath = normaliseFilePath(filePath);
447 const stat = await fs.promises.stat(normalisedPath);
448 const identityKey = eventIdentityKey(normalisedPath, streamIndex, stat);
449
450 const cached = getCachedValue(eventCache, identityKey);
451 if (cached)
452 {
453 if (typeof cached.then === 'function') subtitleDebugStats.eventCachePendingHits += 1;
454 else subtitleDebugStats.eventCacheHits += 1;
455 touchCachedEntry(eventCache, identityKey, EVENT_CACHE_TTL_MS);
456 return cached;
457 }
458
459 subtitleDebugStats.eventCacheMisses += 1;
460 subtitleDebugStats.eventCacheBuilds += 1;
461 const buildStartedAt = Date.now();
462 const pending = buildEventCacheEntry(normalisedPath, streamIndex, identityKey);
463 setCachedValue(eventCache, identityKey, pending, EVENT_CACHE_TTL_MS, estimateEventCacheBytes);
464
465 try
466 {
467 const resolved = await pending;
468 recordDuration(subtitleDebugStats.eventBuildDurationMs, buildStartedAt);
469 setCachedValue(eventCache, identityKey, resolved, EVENT_CACHE_TTL_MS, estimateEventCacheBytes);
470 return resolved;
471 } catch (error)
472 {
473 subtitleDebugStats.eventCacheBuildErrors += 1;
474 deleteCachedValue(eventCache, identityKey);
475 throw error;
476 }
477}
478
479function findActiveEventIndex (events, timeSeconds)
480{
481 let left = 0;
482 let right = events.length - 1;
483 let candidate = -1;
484
485 while (left <= right)
486 {
487 const middle = Math.floor((left + right) / 2);
488 const event = events[middle];
489
490 if (event.startTimeSeconds <= timeSeconds)
491 {
492 candidate = middle;
493 left = middle + 1;
494 } else
495 {
496 right = middle - 1;
497 }
498 }
499
500 if (candidate < 0) return -1;
501
502 const active = events[candidate];
503 if (timeSeconds >= active.endTimeSeconds) return -1;
504 return candidate;
505}
506
507async function renderEventFrame (event, canvasWidth, canvasHeight)
508{
509 const sourceWidth = Math.max(1, toPositiveInt(event.screenWidth, canvasWidth));
510 const sourceHeight = Math.max(1, toPositiveInt(event.screenHeight, canvasHeight));
511
512 const scaleX = canvasWidth / sourceWidth;
513 const scaleY = canvasHeight / sourceHeight;
514
515 const targetWidth = Math.max(1, Math.round(event.imageWidth * scaleX));
516 const targetHeight = Math.max(1, Math.round(event.imageHeight * scaleY));
517 const left = Math.round(event.left * scaleX);
518 const top = Math.round(event.top * scaleY);
519 const right = left + targetWidth;
520 const bottom = top + targetHeight;
521
522 const visibleLeft = Math.max(0, left);
523 const visibleTop = Math.max(0, top);
524 const visibleRight = Math.min(canvasWidth, right);
525 const visibleBottom = Math.min(canvasHeight, bottom);
526 const visibleWidth = Math.max(0, visibleRight - visibleLeft);
527 const visibleHeight = Math.max(0, visibleBottom - visibleTop);
528
529 if (visibleWidth <= 0 || visibleHeight <= 0)
530 {
531 return null;
532 }
533
534 let imageBuffer = (targetWidth === event.imageWidth && targetHeight === event.imageHeight)
535 ? event.imagePng
536 : await sharp(event.imagePng).resize({ width: targetWidth, height: targetHeight, fit: 'fill' }).png().toBuffer();
537
538 if (visibleWidth < targetWidth || visibleHeight < targetHeight)
539 {
540 imageBuffer = await sharp(imageBuffer)
541 .extract({
542 left: Math.max(0, visibleLeft - left),
543 top: Math.max(0, visibleTop - top),
544 width: visibleWidth,
545 height: visibleHeight,
546 })
547 .png()
548 .toBuffer();
549 }
550
551 return {
552 frame: imageBuffer,
553 left: visibleLeft,
554 top: visibleTop,
555 width: visibleWidth,
556 height: visibleHeight,
557 canvasWidth,
558 canvasHeight,
559 };
560}
561
562function parseRenderOptions (options)
563{
564 const input = options || {};
565 const filePath = String(input.filePath ?? '').trim();
566 const streamIndex = Number.parseInt(String(input.streamIndex ?? ''), 10);
567 const timeSeconds = Number.parseFloat(String(input.timeSeconds ?? ''), 10);
568 const requestedWidth = Math.max(1, toPositiveInt(input.canvasWidth ?? '', 1280));
569 const requestedHeight = Math.max(1, toPositiveInt(input.canvasHeight ?? '', 720));
570 const scale = Math.min(1, 1280 / requestedWidth, 720 / requestedHeight) * IMAGE_SUBTITLE_RENDER_SCALE;
571 const canvasWidth = Math.max(1, Math.round(requestedWidth * scale));
572 const canvasHeight = Math.max(1, Math.round(requestedHeight * scale));
573
574 return {
575 filePath,
576 streamIndex,
577 timeSeconds,
578 canvasWidth,
579 canvasHeight,
580 isValid: !!(filePath && Number.isInteger(streamIndex) && streamIndex >= 0 && Number.isFinite(timeSeconds) && timeSeconds >= 0),
581 };
582}
583
584async function renderImageSubtitleFrame (options)
585{
586 const requestStartedAt = Date.now();
587 const parsed = parseRenderOptions(options);
588 const { filePath, streamIndex, timeSeconds, canvasWidth, canvasHeight } = parsed;
589
590 if (!parsed.isValid)
591 {
592 recordDuration(subtitleDebugStats.frameRequestDurationMs, requestStartedAt);
593 return null;
594 }
595
596 const entry = await getEventCacheEntry(filePath, streamIndex);
597 if (!entry || !Array.isArray(entry.events) || entry.events.length === 0)
598 {
599 recordDuration(subtitleDebugStats.frameRequestDurationMs, requestStartedAt);
600 return null;
601 }
602
603 const eventIndex = findActiveEventIndex(entry.events, timeSeconds);
604 if (eventIndex < 0)
605 {
606 recordDuration(subtitleDebugStats.frameRequestDurationMs, requestStartedAt);
607 return null;
608 }
609
610 const frameKey = `${entry.cacheKey}|${eventIndex}|${canvasWidth}x${canvasHeight}`;
611 const cachedFrame = getCachedValue(frameCache, frameKey);
612 if (cachedFrame)
613 {
614 subtitleDebugStats.frameCacheHits += 1;
615 recordDuration(subtitleDebugStats.frameRequestDurationMs, requestStartedAt);
616 return cachedFrame;
617 }
618
619 subtitleDebugStats.frameCacheMisses += 1;
620 subtitleDebugStats.frameRenders += 1;
621 const renderStartedAt = Date.now();
622 const frameData = await renderEventFrame(entry.events[eventIndex], canvasWidth, canvasHeight);
623 if (!frameData)
624 {
625 recordDuration(subtitleDebugStats.frameRenderDurationMs, renderStartedAt);
626 recordDuration(subtitleDebugStats.frameRequestDurationMs, requestStartedAt);
627 return null;
628 }
629 recordDuration(subtitleDebugStats.frameRenderDurationMs, renderStartedAt);
630
631 const event = entry.events[eventIndex];
632 const cacheValue = {
633 frame: frameData.frame,
634 left: frameData.left,
635 top: frameData.top,
636 width: frameData.width,
637 height: frameData.height,
638 canvasWidth: frameData.canvasWidth,
639 canvasHeight: frameData.canvasHeight,
640 startTimeSeconds: event.startTimeSeconds,
641 endTimeSeconds: event.endTimeSeconds,
642 };
643 setCachedValue(
644 frameCache,
645 frameKey,
646 cacheValue,
647 FRAME_CACHE_TTL_MS,
648 estimateFrameCacheBytes,
649 );
650 recordDuration(subtitleDebugStats.frameRequestDurationMs, requestStartedAt);
651 return cacheValue;
652}
653
654function deleteSupFileForKey (identityKey)
655{
656 const cacheKey = eventCacheFileKey(identityKey);
657 const supPath = path.join(SUBTITLE_CACHE_DIR, `${cacheKey}.sup`);
658 fs.unlink(supPath, () =>
659 {
660 // Best-effort; file may not exist if extraction never completed.
661 });
662}
663
664function purgeSubtitleCacheDir ()
665{
666 try
667 {
668 fs.rmSync(SUBTITLE_CACHE_DIR, { recursive: true, force: true });
669 } catch
670 {
671 // Best-effort; directory may not exist.
672 }
673}
674
675function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
676{
677 const prefix = cachePrefixForFile(filePath);
678 if (!prefix || !Number.isFinite(slowestViewerTimeSeconds)) return;
679
680 // Prune frameCache as before
681 const keysToDelete = [];
682 for (const key of frameCache.map.keys())
683 {
684 if (!key.startsWith(prefix)) continue;
685 const entry = frameCache.map.get(key);
686 if (!entry) continue;
687 const eventEntry = entry.value || {};
688 const { startTimeSeconds, endTimeSeconds } = eventEntry;
689 if (!Number.isFinite(startTimeSeconds) || !Number.isFinite(endTimeSeconds)) continue;
690 const duration = Math.max(0, endTimeSeconds - startTimeSeconds);
691 const dropThreshold = endTimeSeconds + duration + IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS;
692 if (slowestViewerTimeSeconds >= dropThreshold)
693 {
694 keysToDelete.push(key);
695 }
696 }
697 for (const key of keysToDelete)
698 {
699 deleteCachedValue(frameCache, key);
700 }
701
702 // Smart eventCache pruning: only prune if over cap
703 let totalBytes = eventCache.totalBytes;
704 if (totalBytes <= MAX_EVENT_CACHE_BYTES) return;
705 // For each entry, prune events that end before (slowestViewerTimeSeconds - MIN_EVENT_WINDOW_SECONDS)
706 for (const [key, entry] of eventCache.map.entries())
707 {
708 if (!key.startsWith(prefix)) continue;
709 const value = entry.value;
710 if (!value || !Array.isArray(value.events)) continue;
711 let events = value.events;
712 // Find first event that ends after the retention threshold
713 const minKeepTime = Math.max(0, slowestViewerTimeSeconds - MIN_EVENT_WINDOW_SECONDS);
714 let pruneIdx = 0;
715 while (pruneIdx < events.length && events[pruneIdx].endTimeSeconds < minKeepTime)
716 {
717 pruneIdx++;
718 }
719 if (pruneIdx > 0)
720 {
721 // Remove pruned events and update memory usage
722 const removed = events.splice(0, pruneIdx);
723 for (const ev of removed)
724 {
725 if (ev && Buffer.isBuffer(ev.imagePng))
726 {
727 entry.sizeBytes -= ev.imagePng.length + 160;
728 eventCache.totalBytes -= ev.imagePng.length + 160;
729 }
730 }
731 }
732 // If all events are pruned, remove the entry
733 if (events.length === 0)
734 {
735 deleteSupFileForKey(key);
736 deleteCachedValue(eventCache, key);
737 }
738 // Stop pruning if under cap
739 if (eventCache.totalBytes <= MAX_EVENT_CACHE_BYTES) break;
740 }
741}
742
743function clearCachesForFile (filePath)
744{
745 const prefix = cachePrefixForFile(filePath);
746 if (!prefix) return;
747
748 const eventKeysToDelete = [];
749 const frameKeysToDelete = [];
750
751 for (const key of eventCache.map.keys())
752 {
753 if (key.startsWith(prefix)) eventKeysToDelete.push(key);
754 }
755
756 for (const key of frameCache.map.keys())
757 {
758 if (key.startsWith(prefix)) frameKeysToDelete.push(key);
759 }
760
761 for (const key of eventKeysToDelete)
762 {
763 deleteSupFileForKey(key);
764 deleteCachedValue(eventCache, key);
765 }
766
767 for (const key of frameKeysToDelete)
768 {
769 deleteCachedValue(frameCache, key);
770 }
771}
772
773function filePathFromCacheKey (key)
774{
775 const raw = String(key || '');
776 const divider = raw.indexOf('|');
777 if (divider < 0) return '';
778 return raw.slice(0, divider);
779}
780
781function getImageSubtitleDebugSnapshot ()
782{
783 const byFile = {};
784
785 for (const [key, entry] of eventCache.map.entries())
786 {
787 const filePath = filePathFromCacheKey(key);
788 if (!filePath) continue;
789
790 const row = byFile[filePath] || {
791 filePath,
792 eventEntries: 0,
793 eventBytes: 0,
794 eventCount: 0,
795 eventImageBytes: 0,
796 frameEntries: 0,
797 frameBytes: 0,
798 };
799
800 row.eventEntries += 1;
801 row.eventBytes += Number(entry && entry.sizeBytes) || 0;
802
803 const value = entry && entry.value;
804 if (value && Array.isArray(value.events))
805 {
806 row.eventCount += value.events.length;
807 for (const event of value.events)
808 {
809 if (event && Buffer.isBuffer(event.imagePng))
810 {
811 row.eventImageBytes += event.imagePng.length;
812 }
813 }
814 }
815
816 byFile[filePath] = row;
817 }
818
819 for (const [key, entry] of frameCache.map.entries())
820 {
821 const filePath = filePathFromCacheKey(key);
822 if (!filePath) continue;
823
824 const row = byFile[filePath] || {
825 filePath,
826 eventEntries: 0,
827 eventBytes: 0,
828 eventCount: 0,
829 eventImageBytes: 0,
830 frameEntries: 0,
831 frameBytes: 0,
832 };
833
834 row.frameEntries += 1;
835 row.frameBytes += Number(entry && entry.sizeBytes) || 0;
836
837 byFile[filePath] = row;
838 }
839
840 return {
841 eventCache: {
842 entries: eventCache.map.size,
843 bytes: eventCache.totalBytes,
844 maxItems: debugLimitValue(eventCache.maxItems),
845 maxBytes: eventCache.maxBytes,
846 },
847 frameCache: {
848 entries: frameCache.map.size,
849 bytes: frameCache.totalBytes,
850 maxItems: debugLimitValue(frameCache.maxItems),
851 maxBytes: frameCache.maxBytes,
852 },
853 metrics: {
854 eventCacheHits: subtitleDebugStats.eventCacheHits,
855 eventCacheMisses: subtitleDebugStats.eventCacheMisses,
856 eventCachePendingHits: subtitleDebugStats.eventCachePendingHits,
857 eventCacheBuilds: subtitleDebugStats.eventCacheBuilds,
858 eventCacheBuildErrors: subtitleDebugStats.eventCacheBuildErrors,
859 frameCacheHits: subtitleDebugStats.frameCacheHits,
860 frameCacheMisses: subtitleDebugStats.frameCacheMisses,
861 frameRenders: subtitleDebugStats.frameRenders,
862 eventBuildDurationMs: {
863 last: subtitleDebugStats.eventBuildDurationMs.lastMs,
864 max: subtitleDebugStats.eventBuildDurationMs.maxMs,
865 average: averageDurationMs(subtitleDebugStats.eventBuildDurationMs),
866 },
867 frameRenderDurationMs: {
868 last: subtitleDebugStats.frameRenderDurationMs.lastMs,
869 max: subtitleDebugStats.frameRenderDurationMs.maxMs,
870 average: averageDurationMs(subtitleDebugStats.frameRenderDurationMs),
871 },
872 frameRequestDurationMs: {
873 last: subtitleDebugStats.frameRequestDurationMs.lastMs,
874 max: subtitleDebugStats.frameRequestDurationMs.maxMs,
875 average: averageDurationMs(subtitleDebugStats.frameRequestDurationMs),
876 },
877 },
878 byFile,
879 };
880}
881
882module.exports = {
883 parseRenderOptions,
884 renderImageSubtitleFrame,
885 clearCachesForFile,
886 pruneOldImageSubtitleCaches,
887 purgeSubtitleCacheDir,
888 getImageSubtitleDebugSnapshot,
889};