BlueGit

timfa filemanager

Merge fix/subtitle-cache-miss-fallback

timfa Tim Falken · pushed by timfa

Authored 2026-05-27 · 5 months ago

Hash
0dfb4c3b3321f7b22e98cea84b9b81e7142c56dd
3 files changed +162 −2
.github/technical-reference.md +1 −0
@@ -207,6 +207,7 @@
207 207 - applyDefaultTextTrackPrefs() - Writes the default Video.js caption override settings (transparent background, uniform edge style) so all streamed players start from the same text-track baseline.
208 208 - bindTrackPrefs(player, prefs, savePrefs) - Restores subtitle/audio track selections by track name and persists later track changes; shared by the authenticated player and the share page so both resolve track matches the same way.
209 209 - bindImageSubtitleOverlay(player, videojs, prefs, session, opts) - Adds the shared IM button/menu/overlay for bitmap subtitle tracks, restores saved image-subtitle selections, fetches rendered frames through a caller-supplied request builder, keeps a separate rolling lookahead-prefetch worker alive across timeupdates (including gaps where no subtitle is currently visible), supports cache-only foreground playback with `currentFetchEnabled: false` (foreground uses cached frames while walker owns server fetches), supports startup warmup with `initialPrefetchSeconds` (default 60s) so early timeline subtitle frames are requested immediately on player load before playback advances, extends prefetch target to at least the latest buffered video end (not only fixed lookahead seconds), merges buffered-end signals from both Video.js/player APIs and underlying media buffered ranges so under-reported player values do not cap subtitle prefetch early, keeps the prefetch frontier pinned to at least current playback time so it chases buffered-ahead range instead of replaying stale lag after video/session changes, coalesces overlapping current-frame refreshes into a single in-flight fetch (only force-refresh paths abort/restart), applies per-request timeouts (`currentRequestTimeoutMs`, `prefetchRequestTimeoutMs`) so hung subtitle frame requests cannot stall updates indefinitely, paces prefetch polling (`prefetchRequestIntervalMs`) to reduce request bursts, logs current and prefetch subtitle request/send/receive/not-available timing to the browser console and optional batch sink, caches received subtitle PNG frames client-side for the lifetime of the player session (including prefetched frames) so rewind playback can render from local cache even after server-side purge, ensures force-seek aborts still clear in-flight current-request state so subtitle refreshes continue instead of deadlocking, updates seek-bar bracket markers only after responses resolve (no in-flight jump-back flicker), keeps both marker positions forward-only (never regressing to earlier times), allows the two bracket glyphs to overlap naturally when positions match, advances markers even when responses are 204/no-frame, and disables the track after the first hard failure.
210 - bindImageSubtitleOverlay(player, videojs, prefs, session, opts) - Cache-frame time matching now uses a small epsilon window around start/end timestamps to avoid boundary misses; cache-only mode can also perform a throttled direct current-frame fallback fetch on cache miss (`cacheMissFallbackEnabled`, `cacheMissFallbackIntervalMs`) so subtitles recover without waiting for another seek/timeupdate cycle.
210 211 - bindStreamAheadPrefetch(opts) - Starts a background HLS walker that repeatedly fetches the session playlist, discovers variant playlists and media segments, and prefetches those assets into the browser cache so playback can read already-fetched segments while the backend prunes old files; tracks cached-through timeline progress (primary from playlist segment durations, with filename-based segment-index fallback for `.ts/.m4s` naming and a section-ratio fallback from fetched segments/total discovered segments when absolute timing metadata is missing) and returns a controller with stop()/done()/getStatus()/subscribe().
211 212 - bindStreamAheadSeekMarker(player, streamAheadPrefetch, opts) - Renders a non-interactive `:` marker on the playback seek bar showing how far the stream-ahead walker has cached through the timeline; subscribes to walker progress updates, updates tooltip text with formatted cached-through time, and removes itself on detach.
212 213 - enableVideoStreamClientCache() - Registers a shared service worker (`/stream-cache-sw.js`) that caches HLS playlist/segment responses so already-downloaded stream assets remain playable on rewind after server-side segment pruning.
@@ -207,6 +207,7 @@
207- applyDefaultTextTrackPrefs() - Writes the default Video.js caption override settings (transparent background, uniform edge style) so all streamed players start from the same text-track baseline.
207- applyDefaultTextTrackPrefs() - Writes the default Video.js caption override settings (transparent background, uniform edge style) so all streamed players start from the same text-track baseline.
208- bindTrackPrefs(player, prefs, savePrefs) - Restores subtitle/audio track selections by track name and persists later track changes; shared by the authenticated player and the share page so both resolve track matches the same way.
208- bindTrackPrefs(player, prefs, savePrefs) - Restores subtitle/audio track selections by track name and persists later track changes; shared by the authenticated player and the share page so both resolve track matches the same way.
209- bindImageSubtitleOverlay(player, videojs, prefs, session, opts) - Adds the shared IM button/menu/overlay for bitmap subtitle tracks, restores saved image-subtitle selections, fetches rendered frames through a caller-supplied request builder, keeps a separate rolling lookahead-prefetch worker alive across timeupdates (including gaps where no subtitle is currently visible), supports cache-only foreground playback with `currentFetchEnabled: false` (foreground uses cached frames while walker owns server fetches), supports startup warmup with `initialPrefetchSeconds` (default 60s) so early timeline subtitle frames are requested immediately on player load before playback advances, extends prefetch target to at least the latest buffered video end (not only fixed lookahead seconds), merges buffered-end signals from both Video.js/player APIs and underlying media buffered ranges so under-reported player values do not cap subtitle prefetch early, keeps the prefetch frontier pinned to at least current playback time so it chases buffered-ahead range instead of replaying stale lag after video/session changes, coalesces overlapping current-frame refreshes into a single in-flight fetch (only force-refresh paths abort/restart), applies per-request timeouts (`currentRequestTimeoutMs`, `prefetchRequestTimeoutMs`) so hung subtitle frame requests cannot stall updates indefinitely, paces prefetch polling (`prefetchRequestIntervalMs`) to reduce request bursts, logs current and prefetch subtitle request/send/receive/not-available timing to the browser console and optional batch sink, caches received subtitle PNG frames client-side for the lifetime of the player session (including prefetched frames) so rewind playback can render from local cache even after server-side purge, ensures force-seek aborts still clear in-flight current-request state so subtitle refreshes continue instead of deadlocking, updates seek-bar bracket markers only after responses resolve (no in-flight jump-back flicker), keeps both marker positions forward-only (never regressing to earlier times), allows the two bracket glyphs to overlap naturally when positions match, advances markers even when responses are 204/no-frame, and disables the track after the first hard failure.
209- bindImageSubtitleOverlay(player, videojs, prefs, session, opts) - Adds the shared IM button/menu/overlay for bitmap subtitle tracks, restores saved image-subtitle selections, fetches rendered frames through a caller-supplied request builder, keeps a separate rolling lookahead-prefetch worker alive across timeupdates (including gaps where no subtitle is currently visible), supports cache-only foreground playback with `currentFetchEnabled: false` (foreground uses cached frames while walker owns server fetches), supports startup warmup with `initialPrefetchSeconds` (default 60s) so early timeline subtitle frames are requested immediately on player load before playback advances, extends prefetch target to at least the latest buffered video end (not only fixed lookahead seconds), merges buffered-end signals from both Video.js/player APIs and underlying media buffered ranges so under-reported player values do not cap subtitle prefetch early, keeps the prefetch frontier pinned to at least current playback time so it chases buffered-ahead range instead of replaying stale lag after video/session changes, coalesces overlapping current-frame refreshes into a single in-flight fetch (only force-refresh paths abort/restart), applies per-request timeouts (`currentRequestTimeoutMs`, `prefetchRequestTimeoutMs`) so hung subtitle frame requests cannot stall updates indefinitely, paces prefetch polling (`prefetchRequestIntervalMs`) to reduce request bursts, logs current and prefetch subtitle request/send/receive/not-available timing to the browser console and optional batch sink, caches received subtitle PNG frames client-side for the lifetime of the player session (including prefetched frames) so rewind playback can render from local cache even after server-side purge, ensures force-seek aborts still clear in-flight current-request state so subtitle refreshes continue instead of deadlocking, updates seek-bar bracket markers only after responses resolve (no in-flight jump-back flicker), keeps both marker positions forward-only (never regressing to earlier times), allows the two bracket glyphs to overlap naturally when positions match, advances markers even when responses are 204/no-frame, and disables the track after the first hard failure.
210- bindImageSubtitleOverlay(player, videojs, prefs, session, opts) - Cache-frame time matching now uses a small epsilon window around start/end timestamps to avoid boundary misses; cache-only mode can also perform a throttled direct current-frame fallback fetch on cache miss (`cacheMissFallbackEnabled`, `cacheMissFallbackIntervalMs`) so subtitles recover without waiting for another seek/timeupdate cycle.
210- bindStreamAheadPrefetch(opts) - Starts a background HLS walker that repeatedly fetches the session playlist, discovers variant playlists and media segments, and prefetches those assets into the browser cache so playback can read already-fetched segments while the backend prunes old files; tracks cached-through timeline progress (primary from playlist segment durations, with filename-based segment-index fallback for `.ts/.m4s` naming and a section-ratio fallback from fetched segments/total discovered segments when absolute timing metadata is missing) and returns a controller with stop()/done()/getStatus()/subscribe().
211- bindStreamAheadPrefetch(opts) - Starts a background HLS walker that repeatedly fetches the session playlist, discovers variant playlists and media segments, and prefetches those assets into the browser cache so playback can read already-fetched segments while the backend prunes old files; tracks cached-through timeline progress (primary from playlist segment durations, with filename-based segment-index fallback for `.ts/.m4s` naming and a section-ratio fallback from fetched segments/total discovered segments when absolute timing metadata is missing) and returns a controller with stop()/done()/getStatus()/subscribe().
211- bindStreamAheadSeekMarker(player, streamAheadPrefetch, opts) - Renders a non-interactive `:` marker on the playback seek bar showing how far the stream-ahead walker has cached through the timeline; subscribes to walker progress updates, updates tooltip text with formatted cached-through time, and removes itself on detach.
212- bindStreamAheadSeekMarker(player, streamAheadPrefetch, opts) - Renders a non-interactive `:` marker on the playback seek bar showing how far the stream-ahead walker has cached through the timeline; subscribes to walker progress updates, updates tooltip text with formatted cached-through time, and removes itself on detach.
212- enableVideoStreamClientCache() - Registers a shared service worker (`/stream-cache-sw.js`) that caches HLS playlist/segment responses so already-downloaded stream assets remain playable on rewind after server-side segment pruning.
213- enableVideoStreamClientCache() - Registers a shared service worker (`/stream-cache-sw.js`) that caches HLS playlist/segment responses so already-downloaded stream assets remain playable on rewind after server-side segment pruning.
public/shared-video-player.js +45 −2
@@ -14,6 +14,8 @@
14 14 const IMAGE_SUBTITLE_INITIAL_PREFETCH_SECONDS_DEFAULT = 60;
15 15 const IMAGE_SUBTITLE_INITIAL_PREFETCH_SECONDS_MAX = 120;
16 16 const IMAGE_SUBTITLE_PREFETCH_STEP_SECONDS = 0.5;
17 const IMAGE_SUBTITLE_TIME_MATCH_EPSILON_SECONDS = 0.05;
18 const IMAGE_SUBTITLE_CACHE_MISS_FALLBACK_INTERVAL_MS_DEFAULT = 2000;
17 19 const IMAGE_SUBTITLE_CURRENT_REQUEST_TIMEOUT_MS_DEFAULT = 4000;
18 20 const IMAGE_SUBTITLE_PREFETCH_REQUEST_TIMEOUT_MS_DEFAULT = 4000;
19 21 const IMAGE_SUBTITLE_PREFETCH_REQUEST_INTERVAL_MS_DEFAULT = 0;
@@ -303,6 +305,11 @@
303 305 const prefetchRequestIntervalMs = Number.isFinite(requestedPrefetchRequestIntervalMs)
304 306 ? Math.max(0, requestedPrefetchRequestIntervalMs)
305 307 : IMAGE_SUBTITLE_PREFETCH_REQUEST_INTERVAL_MS_DEFAULT;
308 const requestedCacheMissFallbackIntervalMs = Number(opts.cacheMissFallbackIntervalMs);
309 const cacheMissFallbackIntervalMs = Number.isFinite(requestedCacheMissFallbackIntervalMs)
310 ? Math.max(500, requestedCacheMissFallbackIntervalMs)
311 : IMAGE_SUBTITLE_CACHE_MISS_FALLBACK_INTERVAL_MS_DEFAULT;
312 const cacheMissFallbackEnabled = opts.cacheMissFallbackEnabled !== false;
306 313 const currentFetchEnabled = opts.currentFetchEnabled !== false;
307 314 const logToConsole = opts.logToConsole !== false;
308 315 if (!buildFrameRequest) return null;
@@ -337,6 +344,7 @@
337 344 let subtitleLogSequence = 0;
338 345 let subtitleLogQueue = [];
339 346 let subtitleLogFlushTimer = null;
347 let lastCacheMissFallbackAtMs = 0;
340 348 const cachedSubtitleFrames = new Map();
341 349
342 350 const flushSubtitleLogs = () =>
@@ -580,7 +588,9 @@
580 588 {
581 589 if (!frame || frame.streamIndex !== streamIndex) continue;
582 590 if (!Number.isFinite(frame.startTimeSeconds) || !Number.isFinite(frame.endTimeSeconds)) continue;
583 if (currentTime < frame.startTimeSeconds || currentTime >= frame.endTimeSeconds) continue;
591 const startsAt = frame.startTimeSeconds - IMAGE_SUBTITLE_TIME_MATCH_EPSILON_SECONDS;
592 const endsAt = frame.endTimeSeconds + IMAGE_SUBTITLE_TIME_MATCH_EPSILON_SECONDS;
593 if (currentTime < startsAt || currentTime >= endsAt) continue;
584 594 if (!best || frame.startTimeSeconds > best.startTimeSeconds) best = frame;
585 595 }
586 596 return best;
@@ -1021,11 +1031,29 @@
1021 1031 const blob = new Blob([payload], {
1022 1032 type: String(res.headers.get('Content-Type') || '').trim() || 'image/png',
1023 1033 });
1024 cacheSubtitleFrame({
1034 const cachedFrameEntry = cacheSubtitleFrame({
1025 1035 streamIndex: selectedStreamIndex,
1026 1036 blob,
1027 1037 metadata,
1028 1038 });
1039
1040 // When foreground fetches are disabled, current-time rendering is
1041 // cache-only and may otherwise wait for a later timeupdate tick.
1042 // Render immediately if this prefetched frame covers current time.
1043 if (cachedFrameEntry)
1044 {
1045 const nowTime = Number(player.currentTime());
1046 if (Number.isFinite(nowTime) && nowTime >= 0)
1047 {
1048 const startsAt = cachedFrameEntry.startTimeSeconds - IMAGE_SUBTITLE_TIME_MATCH_EPSILON_SECONDS;
1049 const endsAt = cachedFrameEntry.endTimeSeconds + IMAGE_SUBTITLE_TIME_MATCH_EPSILON_SECONDS;
1050 if (nowTime >= startsAt && nowTime < endsAt)
1051 {
1052 renderSubtitleFrame(cachedFrameEntry);
1053 }
1054 }
1055 }
1056
1029 1057 logSubtitleEvent('subtitle_png_received', {
1030 1058 ...responseMeta,
1031 1059 receivedAtMs,
@@ -1104,6 +1132,21 @@
1104 1132 if (!currentFetchEnabled)
1105 1133 {
1106 1134 hideOverlay();
1135 const nowMs = Date.now();
1136 const shouldFallbackFetch = cacheMissFallbackEnabled
1137 && nowMs - lastCacheMissFallbackAtMs >= cacheMissFallbackIntervalMs;
1138 if (shouldFallbackFetch)
1139 {
1140 lastCacheMissFallbackAtMs = nowMs;
1141 logSubtitleEvent('subtitle_cache_miss_fallback_fetch', {
1142 requestKind: 'current',
1143 requestedAtMs: nowMs,
1144 requestedVideoTimeSeconds: roundLogNumber(currentTime),
1145 });
1146 // Safety net for occasional cache key/window mismatches.
1147 // Uses normal current-frame path but throttled to avoid request spam.
1148 return refreshFrame(true);
1149 }
1107 1150 queueLookaheadPrefetch(currentTime, {
1108 1151 reset: force,
1109 1152 includeCurrentTime: true,
@@ -14,6 +14,8 @@
14 const IMAGE_SUBTITLE_INITIAL_PREFETCH_SECONDS_DEFAULT = 60;
14 const IMAGE_SUBTITLE_INITIAL_PREFETCH_SECONDS_DEFAULT = 60;
15 const IMAGE_SUBTITLE_INITIAL_PREFETCH_SECONDS_MAX = 120;
15 const IMAGE_SUBTITLE_INITIAL_PREFETCH_SECONDS_MAX = 120;
16 const IMAGE_SUBTITLE_PREFETCH_STEP_SECONDS = 0.5;
16 const IMAGE_SUBTITLE_PREFETCH_STEP_SECONDS = 0.5;
17 const IMAGE_SUBTITLE_TIME_MATCH_EPSILON_SECONDS = 0.05;
18 const IMAGE_SUBTITLE_CACHE_MISS_FALLBACK_INTERVAL_MS_DEFAULT = 2000;
17 const IMAGE_SUBTITLE_CURRENT_REQUEST_TIMEOUT_MS_DEFAULT = 4000;
19 const IMAGE_SUBTITLE_CURRENT_REQUEST_TIMEOUT_MS_DEFAULT = 4000;
18 const IMAGE_SUBTITLE_PREFETCH_REQUEST_TIMEOUT_MS_DEFAULT = 4000;
20 const IMAGE_SUBTITLE_PREFETCH_REQUEST_TIMEOUT_MS_DEFAULT = 4000;
19 const IMAGE_SUBTITLE_PREFETCH_REQUEST_INTERVAL_MS_DEFAULT = 0;
21 const IMAGE_SUBTITLE_PREFETCH_REQUEST_INTERVAL_MS_DEFAULT = 0;
@@ -303,6 +305,11 @@
303 const prefetchRequestIntervalMs = Number.isFinite(requestedPrefetchRequestIntervalMs)
305 const prefetchRequestIntervalMs = Number.isFinite(requestedPrefetchRequestIntervalMs)
304 ? Math.max(0, requestedPrefetchRequestIntervalMs)
306 ? Math.max(0, requestedPrefetchRequestIntervalMs)
305 : IMAGE_SUBTITLE_PREFETCH_REQUEST_INTERVAL_MS_DEFAULT;
307 : IMAGE_SUBTITLE_PREFETCH_REQUEST_INTERVAL_MS_DEFAULT;
308 const requestedCacheMissFallbackIntervalMs = Number(opts.cacheMissFallbackIntervalMs);
309 const cacheMissFallbackIntervalMs = Number.isFinite(requestedCacheMissFallbackIntervalMs)
310 ? Math.max(500, requestedCacheMissFallbackIntervalMs)
311 : IMAGE_SUBTITLE_CACHE_MISS_FALLBACK_INTERVAL_MS_DEFAULT;
312 const cacheMissFallbackEnabled = opts.cacheMissFallbackEnabled !== false;
306 const currentFetchEnabled = opts.currentFetchEnabled !== false;
313 const currentFetchEnabled = opts.currentFetchEnabled !== false;
307 const logToConsole = opts.logToConsole !== false;
314 const logToConsole = opts.logToConsole !== false;
308 if (!buildFrameRequest) return null;
315 if (!buildFrameRequest) return null;
@@ -337,6 +344,7 @@
337 let subtitleLogSequence = 0;
344 let subtitleLogSequence = 0;
338 let subtitleLogQueue = [];
345 let subtitleLogQueue = [];
339 let subtitleLogFlushTimer = null;
346 let subtitleLogFlushTimer = null;
347 let lastCacheMissFallbackAtMs = 0;
340 const cachedSubtitleFrames = new Map();
348 const cachedSubtitleFrames = new Map();
341
349
342 const flushSubtitleLogs = () =>
350 const flushSubtitleLogs = () =>
@@ -580,7 +588,9 @@
580 {
588 {
581 if (!frame || frame.streamIndex !== streamIndex) continue;
589 if (!frame || frame.streamIndex !== streamIndex) continue;
582 if (!Number.isFinite(frame.startTimeSeconds) || !Number.isFinite(frame.endTimeSeconds)) continue;
590 if (!Number.isFinite(frame.startTimeSeconds) || !Number.isFinite(frame.endTimeSeconds)) continue;
583 if (currentTime < frame.startTimeSeconds || currentTime >= frame.endTimeSeconds) continue;
591 const startsAt = frame.startTimeSeconds - IMAGE_SUBTITLE_TIME_MATCH_EPSILON_SECONDS;
592 const endsAt = frame.endTimeSeconds + IMAGE_SUBTITLE_TIME_MATCH_EPSILON_SECONDS;
593 if (currentTime < startsAt || currentTime >= endsAt) continue;
584 if (!best || frame.startTimeSeconds > best.startTimeSeconds) best = frame;
594 if (!best || frame.startTimeSeconds > best.startTimeSeconds) best = frame;
585 }
595 }
586 return best;
596 return best;
@@ -1021,11 +1031,29 @@
1021 const blob = new Blob([payload], {
1031 const blob = new Blob([payload], {
1022 type: String(res.headers.get('Content-Type') || '').trim() || 'image/png',
1032 type: String(res.headers.get('Content-Type') || '').trim() || 'image/png',
1023 });
1033 });
1024 cacheSubtitleFrame({
1034 const cachedFrameEntry = cacheSubtitleFrame({
1025 streamIndex: selectedStreamIndex,
1035 streamIndex: selectedStreamIndex,
1026 blob,
1036 blob,
1027 metadata,
1037 metadata,
1028 });
1038 });
1039
1040 // When foreground fetches are disabled, current-time rendering is
1041 // cache-only and may otherwise wait for a later timeupdate tick.
1042 // Render immediately if this prefetched frame covers current time.
1043 if (cachedFrameEntry)
1044 {
1045 const nowTime = Number(player.currentTime());
1046 if (Number.isFinite(nowTime) && nowTime >= 0)
1047 {
1048 const startsAt = cachedFrameEntry.startTimeSeconds - IMAGE_SUBTITLE_TIME_MATCH_EPSILON_SECONDS;
1049 const endsAt = cachedFrameEntry.endTimeSeconds + IMAGE_SUBTITLE_TIME_MATCH_EPSILON_SECONDS;
1050 if (nowTime >= startsAt && nowTime < endsAt)
1051 {
1052 renderSubtitleFrame(cachedFrameEntry);
1053 }
1054 }
1055 }
1056
1029 logSubtitleEvent('subtitle_png_received', {
1057 logSubtitleEvent('subtitle_png_received', {
1030 ...responseMeta,
1058 ...responseMeta,
1031 receivedAtMs,
1059 receivedAtMs,
@@ -1104,6 +1132,21 @@
1104 if (!currentFetchEnabled)
1132 if (!currentFetchEnabled)
1105 {
1133 {
1106 hideOverlay();
1134 hideOverlay();
1135 const nowMs = Date.now();
1136 const shouldFallbackFetch = cacheMissFallbackEnabled
1137 && nowMs - lastCacheMissFallbackAtMs >= cacheMissFallbackIntervalMs;
1138 if (shouldFallbackFetch)
1139 {
1140 lastCacheMissFallbackAtMs = nowMs;
1141 logSubtitleEvent('subtitle_cache_miss_fallback_fetch', {
1142 requestKind: 'current',
1143 requestedAtMs: nowMs,
1144 requestedVideoTimeSeconds: roundLogNumber(currentTime),
1145 });
1146 // Safety net for occasional cache key/window mismatches.
1147 // Uses normal current-frame path but throttled to avoid request spam.
1148 return refreshFrame(true);
1149 }
1107 queueLookaheadPrefetch(currentTime, {
1150 queueLookaheadPrefetch(currentTime, {
1108 reset: force,
1151 reset: force,
1109 includeCurrentTime: true,
1152 includeCurrentTime: true,
tests/shared-video-player.test.js +116 −0
@@ -414,6 +414,64 @@ describe('shared image subtitle prefetching', () =>
414 414 window.close();
415 415 });
416 416
417 test('renders a prefetched frame for the current paused time without waiting for another timeupdate', async () =>
418 {
419 const window = createWindow();
420 const helper = loadSharedVideoPlayer(window);
421 const { player, videojs } = buildPlayerHarness(window);
422
423 player.duration(120);
424 player.setCurrentTime(118.95);
425
426 window.fetch = jest.fn(async (url) =>
427 {
428 const parsed = new URL(url, 'http://localhost/');
429 const timeSeconds = Number(parsed.searchParams.get('timeSeconds'));
430 const headers = {
431 'Content-Type': 'image/png',
432 };
433
434 if (Math.abs(timeSeconds - 118.95) < 0.001)
435 {
436 headers['X-Subtitle-Frame-Start-Time'] = '118.90';
437 headers['X-Subtitle-Frame-End-Time'] = '119.00';
438 headers['X-Subtitle-Frame-Width'] = '640';
439 headers['X-Subtitle-Frame-Height'] = '80';
440 headers['X-Subtitle-Frame-Canvas-Width'] = '1920';
441 headers['X-Subtitle-Frame-Canvas-Height'] = '1080';
442 return new Response(new Uint8Array([137, 80, 78, 71]), { status: 200, headers });
443 }
444
445 return new Response(null, {
446 status: 204,
447 headers: { 'X-Subtitle-Frame-Status': 'none' },
448 });
449 });
450
451 helper.bindImageSubtitleOverlay(player, videojs, {}, {
452 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
453 }, {
454 currentFetchEnabled: false,
455 cacheMissFallbackEnabled: false,
456 lookaheadSeconds: 0,
457 initialPrefetchSeconds: 0,
458 buildFrameRequest: ({ currentTime, streamIndex }) => ({
459 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
460 }),
461 });
462
463 await flushAsyncWork(window, 10);
464
465 const overlay = player.el().querySelector('.video-subtitle-overlay');
466 const overlayImg = overlay && overlay.querySelector('img');
467 expect(overlay).toBeTruthy();
468 expect(overlayImg).toBeTruthy();
469 expect(overlay.style.display).toBe('block');
470 expect(overlayImg.getAttribute('src')).toBe('blob:subtitle');
471
472 window.close();
473 });
474
417 475 test('aborts in-flight prefetch requests when the overlay is detached', async () =>
418 476 {
419 477 const window = createWindow();
@@ -599,6 +657,7 @@ describe('shared image subtitle prefetching', () =>
599 657 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
600 658 }, {
601 659 currentFetchEnabled: false,
660 cacheMissFallbackEnabled: false,
602 661 lookaheadSeconds: 0,
603 662 sendSubtitleLogBatch: (entries) =>
604 663 {
@@ -623,6 +682,63 @@ describe('shared image subtitle prefetching', () =>
623 682 window.close();
624 683 });
625 684
685 test('cache-only mode falls back to throttled current fetch on cache miss', async () =>
686 {
687 const window = createWindow();
688 const helper = loadSharedVideoPlayer(window);
689 const { player, videojs } = buildPlayerHarness(window);
690
691 let requestCount = 0;
692 window.fetch = jest.fn(async () =>
693 {
694 requestCount += 1;
695 if (requestCount === 1)
696 {
697 return new Response(new Uint8Array([137, 80, 78, 71]), {
698 status: 200,
699 headers: {
700 'Content-Type': 'image/png',
701 'X-Subtitle-Frame-Start-Time': '2.00',
702 'X-Subtitle-Frame-End-Time': '2.50',
703 'X-Subtitle-Frame-Width': '600',
704 'X-Subtitle-Frame-Height': '72',
705 'X-Subtitle-Frame-Canvas-Width': '1920',
706 'X-Subtitle-Frame-Canvas-Height': '1080',
707 },
708 });
709 }
710
711 return new Response(null, {
712 status: 204,
713 headers: { 'X-Subtitle-Frame-Status': 'none' },
714 });
715 });
716
717 player.setCurrentTime(2.2);
718
719 helper.bindImageSubtitleOverlay(player, videojs, {}, {
720 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
721 }, {
722 currentFetchEnabled: false,
723 cacheMissFallbackIntervalMs: 2000,
724 lookaheadSeconds: 0,
725 initialPrefetchSeconds: 0,
726 buildFrameRequest: ({ currentTime, streamIndex }) => ({
727 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
728 }),
729 });
730
731 await flushAsyncWork(window, 10);
732
733 const overlay = player.el().querySelector('.video-subtitle-overlay');
734 const overlayImg = overlay && overlay.querySelector('img');
735 expect(window.fetch).toHaveBeenCalled();
736 expect(overlay.style.display).toBe('block');
737 expect(overlayImg.getAttribute('src')).toBe('blob:subtitle');
738
739 window.close();
740 });
741
626 742 test('cache-only mode performs initial warmup prefetch window on load', async () =>
627 743 {
628 744 const window = createWindow();
@@ -414,6 +414,64 @@ describe('shared image subtitle prefetching', () =>
414 window.close();
414 window.close();
415 });
415 });
416
416
417 test('renders a prefetched frame for the current paused time without waiting for another timeupdate', async () =>
418 {
419 const window = createWindow();
420 const helper = loadSharedVideoPlayer(window);
421 const { player, videojs } = buildPlayerHarness(window);
422
423 player.duration(120);
424 player.setCurrentTime(118.95);
425
426 window.fetch = jest.fn(async (url) =>
427 {
428 const parsed = new URL(url, 'http://localhost/');
429 const timeSeconds = Number(parsed.searchParams.get('timeSeconds'));
430 const headers = {
431 'Content-Type': 'image/png',
432 };
433
434 if (Math.abs(timeSeconds - 118.95) < 0.001)
435 {
436 headers['X-Subtitle-Frame-Start-Time'] = '118.90';
437 headers['X-Subtitle-Frame-End-Time'] = '119.00';
438 headers['X-Subtitle-Frame-Width'] = '640';
439 headers['X-Subtitle-Frame-Height'] = '80';
440 headers['X-Subtitle-Frame-Canvas-Width'] = '1920';
441 headers['X-Subtitle-Frame-Canvas-Height'] = '1080';
442 return new Response(new Uint8Array([137, 80, 78, 71]), { status: 200, headers });
443 }
444
445 return new Response(null, {
446 status: 204,
447 headers: { 'X-Subtitle-Frame-Status': 'none' },
448 });
449 });
450
451 helper.bindImageSubtitleOverlay(player, videojs, {}, {
452 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
453 }, {
454 currentFetchEnabled: false,
455 cacheMissFallbackEnabled: false,
456 lookaheadSeconds: 0,
457 initialPrefetchSeconds: 0,
458 buildFrameRequest: ({ currentTime, streamIndex }) => ({
459 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
460 }),
461 });
462
463 await flushAsyncWork(window, 10);
464
465 const overlay = player.el().querySelector('.video-subtitle-overlay');
466 const overlayImg = overlay && overlay.querySelector('img');
467 expect(overlay).toBeTruthy();
468 expect(overlayImg).toBeTruthy();
469 expect(overlay.style.display).toBe('block');
470 expect(overlayImg.getAttribute('src')).toBe('blob:subtitle');
471
472 window.close();
473 });
474
417 test('aborts in-flight prefetch requests when the overlay is detached', async () =>
475 test('aborts in-flight prefetch requests when the overlay is detached', async () =>
418 {
476 {
419 const window = createWindow();
477 const window = createWindow();
@@ -599,6 +657,7 @@ describe('shared image subtitle prefetching', () =>
599 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
657 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
600 }, {
658 }, {
601 currentFetchEnabled: false,
659 currentFetchEnabled: false,
660 cacheMissFallbackEnabled: false,
602 lookaheadSeconds: 0,
661 lookaheadSeconds: 0,
603 sendSubtitleLogBatch: (entries) =>
662 sendSubtitleLogBatch: (entries) =>
604 {
663 {
@@ -623,6 +682,63 @@ describe('shared image subtitle prefetching', () =>
623 window.close();
682 window.close();
624 });
683 });
625
684
685 test('cache-only mode falls back to throttled current fetch on cache miss', async () =>
686 {
687 const window = createWindow();
688 const helper = loadSharedVideoPlayer(window);
689 const { player, videojs } = buildPlayerHarness(window);
690
691 let requestCount = 0;
692 window.fetch = jest.fn(async () =>
693 {
694 requestCount += 1;
695 if (requestCount === 1)
696 {
697 return new Response(new Uint8Array([137, 80, 78, 71]), {
698 status: 200,
699 headers: {
700 'Content-Type': 'image/png',
701 'X-Subtitle-Frame-Start-Time': '2.00',
702 'X-Subtitle-Frame-End-Time': '2.50',
703 'X-Subtitle-Frame-Width': '600',
704 'X-Subtitle-Frame-Height': '72',
705 'X-Subtitle-Frame-Canvas-Width': '1920',
706 'X-Subtitle-Frame-Canvas-Height': '1080',
707 },
708 });
709 }
710
711 return new Response(null, {
712 status: 204,
713 headers: { 'X-Subtitle-Frame-Status': 'none' },
714 });
715 });
716
717 player.setCurrentTime(2.2);
718
719 helper.bindImageSubtitleOverlay(player, videojs, {}, {
720 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
721 }, {
722 currentFetchEnabled: false,
723 cacheMissFallbackIntervalMs: 2000,
724 lookaheadSeconds: 0,
725 initialPrefetchSeconds: 0,
726 buildFrameRequest: ({ currentTime, streamIndex }) => ({
727 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
728 }),
729 });
730
731 await flushAsyncWork(window, 10);
732
733 const overlay = player.el().querySelector('.video-subtitle-overlay');
734 const overlayImg = overlay && overlay.querySelector('img');
735 expect(window.fetch).toHaveBeenCalled();
736 expect(overlay.style.display).toBe('block');
737 expect(overlayImg.getAttribute('src')).toBe('blob:subtitle');
738
739 window.close();
740 });
741
626 test('cache-only mode performs initial warmup prefetch window on load', async () =>
742 test('cache-only mode performs initial warmup prefetch window on load', async () =>
627 {
743 {
628 const window = createWindow();
744 const window = createWindow();