BlueGit

timfa filemanager

Files

filemanager / tests / shared-video-player.test.js

shared-video-player.test.js

Fix subtitle tail markers
timfa Tim Falken · pushed by timfa · 5 months ago
6a260501 History
shared-video-player.test.js 61.1 KiB Download Raw
1'use strict';
2
3const fs = require('fs');
4const path = require('path');
5const { JSDOM } = require('jsdom');
6
7function flushAsyncWork (window, rounds = 6)
8{
9 let chain = Promise.resolve();
10 for (let i = 0; i < rounds; i++)
11 {
12 chain = chain.then(() => new Promise((resolve) => window.setTimeout(resolve, 0)));
13 }
14 return chain;
15}
16
17function buildPlayerHarness (window)
18{
19 const registeredComponents = new Map();
20
21 class MockButton
22 {
23 constructor(player)
24 {
25 this._player = player;
26 this._el = null;
27 }
28
29 player ()
30 {
31 return this._player;
32 }
33
34 buildCSSClass ()
35 {
36 return 'vjs-control';
37 }
38
39 createEl ()
40 {
41 const el = window.document.createElement('button');
42 el.className = this.buildCSSClass();
43 return el;
44 }
45
46 el ()
47 {
48 if (!this._el) this._el = this.createEl();
49 return this._el;
50 }
51 }
52
53 registeredComponents.set('Button', MockButton);
54
55 const videojs = {
56 getComponent: (name) => registeredComponents.get(name),
57 registerComponent: (name, component) =>
58 {
59 registeredComponents.set(name, component);
60 },
61 };
62
63 const events = new Map();
64 const playerEl = window.document.createElement('div');
65 const progressControlEl = window.document.createElement('div');
66 progressControlEl.className = 'vjs-progress-control';
67 playerEl.appendChild(progressControlEl);
68 let currentTime = 0;
69 let durationSeconds = 10;
70 let bufferedEndSeconds = null;
71
72 const player = {
73 ready (cb)
74 {
75 if (typeof cb === 'function') cb();
76 },
77 on (type, fn)
78 {
79 if (!events.has(type)) events.set(type, new Set());
80 events.get(type).add(fn);
81 },
82 trigger (type)
83 {
84 const listeners = events.get(type);
85 if (!listeners) return;
86 for (const fn of Array.from(listeners)) fn();
87 },
88 currentTime ()
89 {
90 return currentTime;
91 },
92 setCurrentTime (next)
93 {
94 currentTime = Number(next);
95 },
96 duration (nextValue)
97 {
98 if (typeof nextValue === 'number')
99 {
100 durationSeconds = nextValue;
101 }
102 return durationSeconds;
103 },
104 bufferedEnd ()
105 {
106 return bufferedEndSeconds;
107 },
108 setBufferedEnd (next)
109 {
110 bufferedEndSeconds = Number.isFinite(next) ? Number(next) : null;
111 },
112 el ()
113 {
114 return playerEl;
115 },
116 getChild (name)
117 {
118 if (name !== 'controlBar') return null;
119 return {
120 addChild (componentName)
121 {
122 const Component = registeredComponents.get(componentName);
123 if (!Component) return null;
124 const instance = new Component(player);
125 playerEl.appendChild(instance.el());
126 return instance;
127 },
128 };
129 },
130 };
131
132 return { player, videojs };
133}
134
135function loadSharedVideoPlayer (window)
136{
137 const script = fs.readFileSync(path.join(__dirname, '../public/shared-video-player.js'), 'utf8');
138 window.eval(script);
139 return window.FilemanagerSharedVideoPlayer;
140}
141
142describe('shared image subtitle prefetching', () =>
143{
144 function createWindow ()
145 {
146 const dom = new JSDOM('<!doctype html><html><body></body></html>', {
147 url: 'http://localhost/',
148 runScripts: 'dangerously',
149 pretendToBeVisual: true,
150 });
151
152 dom.window.AbortController = AbortController;
153 dom.window.Response = Response;
154 dom.window.Headers = Headers;
155 dom.window.Request = Request;
156 dom.window.URL.createObjectURL = () => 'blob:subtitle';
157 dom.window.URL.revokeObjectURL = () => { };
158
159 return dom.window;
160 }
161
162 test('prefetches lookahead frames even when no subtitle is currently active', async () =>
163 {
164 const window = createWindow();
165 const helper = loadSharedVideoPlayer(window);
166 const { player, videojs } = buildPlayerHarness(window);
167 const requestedTimes = [];
168
169 window.fetch = jest.fn(async (url) =>
170 {
171 const parsed = new URL(url, 'http://localhost/');
172 requestedTimes.push(Number(parsed.searchParams.get('timeSeconds')));
173 return new Response(null, {
174 status: 204,
175 headers: { 'X-Subtitle-Frame-Status': 'none' },
176 });
177 });
178
179 helper.bindImageSubtitleOverlay(player, videojs, {}, {
180 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
181 }, {
182 lookaheadSeconds: 1,
183 buildFrameRequest: ({ currentTime, streamIndex }) => ({
184 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
185 }),
186 });
187
188 await flushAsyncWork(window);
189
190 expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1]));
191
192 window.close();
193 });
194
195 test('prefetches subtitle frames through known video duration even with short lookahead', async () =>
196 {
197 const window = createWindow();
198 const helper = loadSharedVideoPlayer(window);
199 const { player, videojs } = buildPlayerHarness(window);
200 const requestedTimes = [];
201
202 player.duration(3);
203
204 window.fetch = jest.fn(async (url) =>
205 {
206 const parsed = new URL(url, 'http://localhost/');
207 requestedTimes.push(Number(parsed.searchParams.get('timeSeconds')));
208 return new Response(null, {
209 status: 204,
210 headers: { 'X-Subtitle-Frame-Status': 'none' },
211 });
212 });
213
214 helper.bindImageSubtitleOverlay(player, videojs, {}, {
215 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
216 }, {
217 lookaheadSeconds: 0.5,
218 buildFrameRequest: ({ currentTime, streamIndex }) => ({
219 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
220 }),
221 });
222
223 await flushAsyncWork(window, 18);
224
225 expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3]));
226
227 window.close();
228 });
229
230 test('prefetch expands to video end when duration metadata appears later', async () =>
231 {
232 const window = createWindow();
233 const helper = loadSharedVideoPlayer(window);
234 const { player, videojs } = buildPlayerHarness(window);
235 const requestedTimes = [];
236
237 player.duration(Number.NaN);
238
239 window.fetch = jest.fn(async (url) =>
240 {
241 const parsed = new URL(url, 'http://localhost/');
242 requestedTimes.push(Number(parsed.searchParams.get('timeSeconds')));
243 return new Response(null, {
244 status: 204,
245 headers: { 'X-Subtitle-Frame-Status': 'none' },
246 });
247 });
248
249 helper.bindImageSubtitleOverlay(player, videojs, {}, {
250 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
251 }, {
252 lookaheadSeconds: 0.5,
253 initialPrefetchSeconds: 0,
254 buildFrameRequest: ({ currentTime, streamIndex }) => ({
255 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
256 }),
257 });
258
259 await flushAsyncWork(window, 6);
260 expect(requestedTimes).toContain(0);
261
262 requestedTimes.length = 0;
263 player.duration(4);
264 player.trigger('durationchange');
265 await flushAsyncWork(window, 14);
266
267 expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4]));
268
269 window.close();
270 });
271
272 test('prefetches at least through the buffered video end', async () =>
273 {
274 const window = createWindow();
275 const helper = loadSharedVideoPlayer(window);
276 const { player, videojs } = buildPlayerHarness(window);
277 const requestedTimes = [];
278
279 player.setBufferedEnd(3);
280
281 window.fetch = jest.fn(async (url) =>
282 {
283 const parsed = new URL(url, 'http://localhost/');
284 requestedTimes.push(Number(parsed.searchParams.get('timeSeconds')));
285 return new Response(null, {
286 status: 204,
287 headers: { 'X-Subtitle-Frame-Status': 'none' },
288 });
289 });
290
291 helper.bindImageSubtitleOverlay(player, videojs, {}, {
292 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
293 }, {
294 lookaheadSeconds: 0.5,
295 buildFrameRequest: ({ currentTime, streamIndex }) => ({
296 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
297 }),
298 });
299
300 await flushAsyncWork(window, 12);
301
302 expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3]));
303
304 window.close();
305 });
306
307 test('range prefetch clamps the tail window to the known video duration', async () =>
308 {
309 const window = createWindow();
310 const helper = loadSharedVideoPlayer(window);
311 const { player, videojs } = buildPlayerHarness(window);
312 const requestedRanges = [];
313
314 player.duration(10);
315 player.setCurrentTime(8.5);
316
317 window.fetch = jest.fn(async (url) =>
318 {
319 const parsed = new URL(url, 'http://localhost/');
320 requestedRanges.push({
321 start: Number(parsed.searchParams.get('startTimeSeconds')),
322 end: Number(parsed.searchParams.get('endTimeSeconds')),
323 });
324 return new Response(JSON.stringify({ status: 'ok', frames: [] }), {
325 status: 200,
326 headers: { 'Content-Type': 'application/json' },
327 });
328 });
329
330 helper.bindImageSubtitleOverlay(player, videojs, {}, {
331 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
332 }, {
333 lookaheadSeconds: 0,
334 initialPrefetchSeconds: 0,
335 currentFetchEnabled: false,
336 cacheMissFallbackEnabled: false,
337 prefetchAdaptiveStepEnabled: true,
338 prefetchStepSecondsMin: 3,
339 prefetchStepSecondsMax: 3,
340 buildFrameRequest: ({ currentTime, streamIndex }) => ({
341 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
342 }),
343 buildPrefetchRequest: ({ currentTime, endTime, streamIndex }) => ({
344 url: `/subtitle-range?streamIndex=${streamIndex}&startTimeSeconds=${currentTime}&endTimeSeconds=${endTime}`,
345 }),
346 });
347
348 player.trigger('timeupdate');
349 await flushAsyncWork(window, 10);
350
351 expect(requestedRanges.length).toBeGreaterThan(0);
352 expect(requestedRanges[0]).toEqual({ start: 8.5, end: 10 });
353 expect(requestedRanges.every((range) => range.end <= 10)).toBe(true);
354
355 window.close();
356 });
357
358 test('prefetch respects configured larger prefetch step size', async () =>
359 {
360 const window = createWindow();
361 const helper = loadSharedVideoPlayer(window);
362 const { player, videojs } = buildPlayerHarness(window);
363 const requestedTimes = [];
364
365 player.duration(6);
366
367 window.fetch = jest.fn(async (url) =>
368 {
369 const parsed = new URL(url, 'http://localhost/');
370 requestedTimes.push(Number(parsed.searchParams.get('timeSeconds')));
371 return new Response(null, {
372 status: 204,
373 headers: { 'X-Subtitle-Frame-Status': 'none' },
374 });
375 });
376
377 helper.bindImageSubtitleOverlay(player, videojs, {}, {
378 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
379 }, {
380 lookaheadSeconds: 0,
381 initialPrefetchSeconds: 0,
382 prefetchStepSeconds: 2,
383 buildFrameRequest: ({ currentTime, streamIndex }) => ({
384 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
385 }),
386 });
387
388 await flushAsyncWork(window, 14);
389
390 expect(requestedTimes).toEqual(expect.arrayContaining([0, 2, 4, 6]));
391
392 window.close();
393 });
394
395 test('adaptive prefetch step increases stride when cache lead grows', async () =>
396 {
397 const window = createWindow();
398 const helper = loadSharedVideoPlayer(window);
399 const { player, videojs } = buildPlayerHarness(window);
400 const requestedTimes = [];
401
402 player.duration(40);
403
404 window.fetch = jest.fn(async (url) =>
405 {
406 const parsed = new URL(url, 'http://localhost/');
407 requestedTimes.push(Number(parsed.searchParams.get('timeSeconds')));
408 return new Response(null, {
409 status: 204,
410 headers: { 'X-Subtitle-Frame-Status': 'none' },
411 });
412 });
413
414 helper.bindImageSubtitleOverlay(player, videojs, {}, {
415 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
416 }, {
417 lookaheadSeconds: 20,
418 initialPrefetchSeconds: 0,
419 prefetchAdaptiveStepEnabled: true,
420 prefetchStepSecondsMin: 1,
421 prefetchStepSecondsMax: 5,
422 buildFrameRequest: ({ currentTime, streamIndex }) => ({
423 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
424 }),
425 });
426
427 await flushAsyncWork(window, 20);
428
429 const strideSamples = [];
430 for (let i = 1; i < requestedTimes.length; i++)
431 {
432 const stride = requestedTimes[i] - requestedTimes[i - 1];
433 if (Number.isFinite(stride) && stride > 0) strideSamples.push(Number(stride.toFixed(3)));
434 }
435
436 expect(strideSamples.some((stride) => stride > 1.25)).toBe(true);
437
438 window.close();
439 });
440
441 test('prefetch can cache subtitle frames from a range request and render immediately', async () =>
442 {
443 const window = createWindow();
444 const helper = loadSharedVideoPlayer(window);
445 const { player, videojs } = buildPlayerHarness(window);
446 const requestedUrls = [];
447
448 player.duration(12);
449 player.setCurrentTime(2);
450
451 window.fetch = jest.fn(async (url) =>
452 {
453 const parsed = new URL(url, 'http://localhost/');
454 requestedUrls.push(parsed.pathname + parsed.search);
455
456 if (parsed.pathname === '/subtitle-range')
457 {
458 return new Response(JSON.stringify({
459 status: 'ok',
460 startTimeSeconds: Number(parsed.searchParams.get('startTimeSeconds')),
461 endTimeSeconds: Number(parsed.searchParams.get('endTimeSeconds')),
462 frames: [
463 {
464 left: 0,
465 top: 0,
466 width: 320,
467 height: 180,
468 canvasWidth: 640,
469 canvasHeight: 360,
470 startTimeSeconds: 2,
471 endTimeSeconds: 4,
472 contentType: 'image/png',
473 frameBase64: Buffer.from('png-data').toString('base64'),
474 },
475 ],
476 }), {
477 status: 200,
478 headers: { 'Content-Type': 'application/json' },
479 });
480 }
481
482 return new Response(null, {
483 status: 204,
484 headers: { 'X-Subtitle-Frame-Status': 'none' },
485 });
486 });
487
488 helper.bindImageSubtitleOverlay(player, videojs, {}, {
489 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
490 }, {
491 currentFetchEnabled: false,
492 cacheMissFallbackEnabled: false,
493 lookaheadSeconds: 0,
494 initialPrefetchSeconds: 0,
495 buildPrefetchRequest: ({ currentTime, endTime, streamIndex }) => ({
496 url: `/subtitle-range?streamIndex=${streamIndex}&startTimeSeconds=${currentTime}&endTimeSeconds=${endTime}`,
497 }),
498 buildFrameRequest: ({ currentTime, streamIndex }) => ({
499 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
500 }),
501 });
502
503 await flushAsyncWork(window, 10);
504
505 expect(requestedUrls.some((url) => url.startsWith('/subtitle-range'))).toBe(true);
506 expect(requestedUrls.some((url) => url.startsWith('/subtitle-frame'))).toBe(false);
507
508 const overlay = player.el().querySelector('.video-subtitle-overlay');
509 expect(overlay).toBeTruthy();
510 expect(overlay.style.display).toBe('block');
511
512 window.close();
513 });
514
515 test('prefetch uses furthest buffered end when player.bufferedEnd under-reports', async () =>
516 {
517 const window = createWindow();
518 const helper = loadSharedVideoPlayer(window);
519 const { player, videojs } = buildPlayerHarness(window);
520 const requestedTimes = [];
521
522 player.setBufferedEnd(0);
523 player.tech = () => ({
524 el: () => ({
525 buffered: {
526 length: 1,
527 end: () => 6,
528 },
529 }),
530 });
531
532 window.fetch = jest.fn(async (url) =>
533 {
534 const parsed = new URL(url, 'http://localhost/');
535 requestedTimes.push(Number(parsed.searchParams.get('timeSeconds')));
536 return new Response(null, {
537 status: 204,
538 headers: { 'X-Subtitle-Frame-Status': 'none' },
539 });
540 });
541
542 helper.bindImageSubtitleOverlay(player, videojs, {}, {
543 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
544 }, {
545 lookaheadSeconds: 0,
546 initialPrefetchSeconds: 0,
547 buildFrameRequest: ({ currentTime, streamIndex }) => ({
548 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
549 }),
550 });
551
552 await flushAsyncWork(window, 16);
553
554 expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6]));
555
556 window.close();
557 });
558
559 test('prefetch frontier jumps to current playback time instead of replaying stale lag', async () =>
560 {
561 const window = createWindow();
562 const helper = loadSharedVideoPlayer(window);
563 const { player, videojs } = buildPlayerHarness(window);
564 const requestedTimes = [];
565
566 player.duration(60);
567 player.setBufferedEnd(2);
568
569 window.fetch = jest.fn(async (url) =>
570 {
571 const parsed = new URL(url, 'http://localhost/');
572 const timeSeconds = Number(parsed.searchParams.get('timeSeconds'));
573 requestedTimes.push(timeSeconds);
574 return new Response(null, {
575 status: 204,
576 headers: { 'X-Subtitle-Frame-Status': 'none' },
577 });
578 });
579
580 helper.bindImageSubtitleOverlay(player, videojs, {}, {
581 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
582 }, {
583 lookaheadSeconds: 0,
584 initialPrefetchSeconds: 0,
585 buildFrameRequest: ({ currentTime, streamIndex }) => ({
586 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
587 }),
588 });
589
590 await flushAsyncWork(window, 8);
591 expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5]));
592
593 requestedTimes.length = 0;
594 player.setCurrentTime(20);
595 player.setBufferedEnd(40);
596 player.trigger('timeupdate');
597 await flushAsyncWork(window, 8);
598
599 expect(requestedTimes).toContain(20);
600 expect(requestedTimes.some((value) => value < 20)).toBe(false);
601 const prefetchAfterJump = requestedTimes.filter((value) => value > 20);
602 if (prefetchAfterJump.length)
603 {
604 expect(Math.min(...prefetchAfterJump)).toBeGreaterThanOrEqual(20.5);
605 }
606
607 window.close();
608 });
609
610 test('does not restart current subtitle request while one is in flight', async () =>
611 {
612 const window = createWindow();
613 const helper = loadSharedVideoPlayer(window);
614 const { player, videojs } = buildPlayerHarness(window);
615 const pendingResolvers = [];
616 const requestTimes = [];
617
618 window.fetch = jest.fn((url, init = {}) =>
619 {
620 const parsed = new URL(url, 'http://localhost/');
621 requestTimes.push(Number(parsed.searchParams.get('timeSeconds')));
622
623 if (pendingResolvers.length === 0)
624 {
625 return new Promise((resolve) =>
626 {
627 pendingResolvers.push(() => resolve(new Response(null, {
628 status: 204,
629 headers: { 'X-Subtitle-Frame-Status': 'none' },
630 })));
631 });
632 }
633
634 expect(init.signal && init.signal.aborted).toBe(false);
635 return Promise.resolve(new Response(null, {
636 status: 204,
637 headers: { 'X-Subtitle-Frame-Status': 'none' },
638 }));
639 });
640
641 helper.bindImageSubtitleOverlay(player, videojs, {}, {
642 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
643 }, {
644 lookaheadSeconds: 0,
645 initialPrefetchSeconds: 0,
646 buildFrameRequest: ({ currentTime, streamIndex }) => ({
647 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
648 }),
649 });
650
651 await flushAsyncWork(window, 3);
652 expect(window.fetch).toHaveBeenCalledTimes(1);
653
654 player.setCurrentTime(1.5);
655 player.trigger('timeupdate');
656 player.setCurrentTime(2.25);
657 player.trigger('timeupdate');
658 await flushAsyncWork(window, 2);
659
660 expect(window.fetch).toHaveBeenCalledTimes(1);
661
662 pendingResolvers[0]();
663 await flushAsyncWork(window, 5);
664
665 expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2);
666 expect(requestTimes).toEqual(expect.arrayContaining([0, 2.25]));
667
668 window.close();
669 });
670
671 test('renders a prefetched frame for the current paused time without waiting for another timeupdate', async () =>
672 {
673 const window = createWindow();
674 const helper = loadSharedVideoPlayer(window);
675 const { player, videojs } = buildPlayerHarness(window);
676
677 player.duration(120);
678 player.setCurrentTime(118.95);
679
680 window.fetch = jest.fn(async (url) =>
681 {
682 const parsed = new URL(url, 'http://localhost/');
683 const timeSeconds = Number(parsed.searchParams.get('timeSeconds'));
684 const headers = {
685 'Content-Type': 'image/png',
686 };
687
688 if (Math.abs(timeSeconds - 118.95) < 0.001)
689 {
690 headers['X-Subtitle-Frame-Start-Time'] = '118.90';
691 headers['X-Subtitle-Frame-End-Time'] = '119.00';
692 headers['X-Subtitle-Frame-Width'] = '640';
693 headers['X-Subtitle-Frame-Height'] = '80';
694 headers['X-Subtitle-Frame-Canvas-Width'] = '1920';
695 headers['X-Subtitle-Frame-Canvas-Height'] = '1080';
696 return new Response(new Uint8Array([137, 80, 78, 71]), { status: 200, headers });
697 }
698
699 return new Response(null, {
700 status: 204,
701 headers: { 'X-Subtitle-Frame-Status': 'none' },
702 });
703 });
704
705 helper.bindImageSubtitleOverlay(player, videojs, {}, {
706 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
707 }, {
708 currentFetchEnabled: false,
709 cacheMissFallbackEnabled: false,
710 lookaheadSeconds: 0,
711 initialPrefetchSeconds: 0,
712 buildFrameRequest: ({ currentTime, streamIndex }) => ({
713 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
714 }),
715 });
716
717 await flushAsyncWork(window, 10);
718
719 const overlay = player.el().querySelector('.video-subtitle-overlay');
720 const overlayImg = overlay && overlay.querySelector('img');
721 expect(overlay).toBeTruthy();
722 expect(overlayImg).toBeTruthy();
723 expect(overlay.style.display).toBe('block');
724 expect(overlayImg.getAttribute('src')).toBe('blob:subtitle');
725
726 window.close();
727 });
728
729 test('aborts in-flight prefetch requests when the overlay is detached', async () =>
730 {
731 const window = createWindow();
732 const helper = loadSharedVideoPlayer(window);
733 const { player, videojs } = buildPlayerHarness(window);
734 const activeSignals = [];
735
736 window.fetch = jest.fn((url, init = {}) =>
737 {
738 const parsed = new URL(url, 'http://localhost/');
739 const timeSeconds = Number(parsed.searchParams.get('timeSeconds'));
740
741 if (timeSeconds === 0)
742 {
743 return Promise.resolve(new Response(null, {
744 status: 204,
745 headers: { 'X-Subtitle-Frame-Status': 'none' },
746 }));
747 }
748
749 activeSignals.push(init.signal || null);
750
751 return new Promise((resolve, reject) =>
752 {
753 if (init.signal && typeof init.signal.addEventListener === 'function')
754 {
755 init.signal.addEventListener('abort', () =>
756 {
757 reject(new DOMException('Aborted', 'AbortError'));
758 }, { once: true });
759 }
760 });
761 });
762
763 const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, {
764 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
765 }, {
766 lookaheadSeconds: 1,
767 buildFrameRequest: ({ currentTime, streamIndex }) => ({
768 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
769 }),
770 });
771
772 await flushAsyncWork(window, 3);
773 expect(activeSignals.length).toBeGreaterThan(0);
774
775 controller.detach();
776 await flushAsyncWork(window, 2);
777
778 expect(activeSignals.every(signal => signal && signal.aborted)).toBe(true);
779
780 window.close();
781 });
782
783 test('logs current subtitle request lifecycle events and flushes them on detach', async () =>
784 {
785 const window = createWindow();
786 const helper = loadSharedVideoPlayer(window);
787 const { player, videojs } = buildPlayerHarness(window);
788 const sentBatches = [];
789
790 window.fetch = jest.fn(async () =>
791 {
792 return new Response('png-data', {
793 status: 200,
794 headers: {
795 'Content-Type': 'image/png',
796 'X-Subtitle-Frame-Status': 'ok',
797 'X-Subtitle-Request-Received-At-Ms': '1000',
798 'X-Subtitle-Response-Sent-At-Ms': '1012',
799 'X-Subtitle-Frame-Start-Time': '1.5',
800 'X-Subtitle-Frame-End-Time': '3.2',
801 'X-Subtitle-Frame-Width': '320',
802 'X-Subtitle-Frame-Height': '180',
803 'X-Subtitle-Frame-Canvas-Width': '640',
804 'X-Subtitle-Frame-Canvas-Height': '360',
805 },
806 });
807 });
808
809 const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, {
810 sessionId: 'sess-1',
811 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
812 }, {
813 lookaheadSeconds: 0,
814 sendSubtitleLogBatch: (entries) =>
815 {
816 sentBatches.push(entries);
817 return Promise.resolve();
818 },
819 buildFrameRequest: ({ currentTime, streamIndex }) => ({
820 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
821 }),
822 });
823
824 await flushAsyncWork(window, 8);
825 controller.detach();
826 await flushAsyncWork(window, 4);
827
828 const flattened = sentBatches.flat();
829 expect(flattened.map((entry) => entry.eventType)).toEqual(expect.arrayContaining([
830 'subtitle_png_requested',
831 'subtitle_png_sent',
832 'subtitle_png_received',
833 ]));
834
835 window.close();
836 });
837
838 test('logs prefetch subtitle lifecycle events', async () =>
839 {
840 const window = createWindow();
841 const helper = loadSharedVideoPlayer(window);
842 const { player, videojs } = buildPlayerHarness(window);
843 const sentBatches = [];
844
845 window.fetch = jest.fn(async () =>
846 {
847 return new Response(null, {
848 status: 204,
849 headers: {
850 'X-Subtitle-Frame-Status': 'none',
851 'X-Subtitle-Request-Received-At-Ms': '1000',
852 'X-Subtitle-Response-Sent-At-Ms': '1010',
853 'X-Subtitle-Frame-Start-Time': '2',
854 'X-Subtitle-Frame-End-Time': '3',
855 },
856 });
857 });
858
859 const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, {
860 sessionId: 'sess-1',
861 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
862 }, {
863 lookaheadSeconds: 1,
864 sendSubtitleLogBatch: (entries) =>
865 {
866 sentBatches.push(entries);
867 return Promise.resolve();
868 },
869 buildFrameRequest: ({ currentTime, streamIndex }) => ({
870 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
871 }),
872 });
873
874 await flushAsyncWork(window, 8);
875 controller.detach();
876 await flushAsyncWork(window, 4);
877
878 const flattened = sentBatches.flat();
879 const prefetchEvents = flattened.filter((entry) => entry.requestKind === 'prefetch').map((entry) => entry.eventType);
880 expect(prefetchEvents).toEqual(expect.arrayContaining([
881 'subtitle_png_requested',
882 'subtitle_png_sent',
883 'subtitle_png_not_available',
884 ]));
885
886 window.close();
887 });
888
889 test('cache-only current mode avoids direct current subtitle fetch requests', async () =>
890 {
891 const window = createWindow();
892 const helper = loadSharedVideoPlayer(window);
893 const { player, videojs } = buildPlayerHarness(window);
894 const sentBatches = [];
895
896 player.setCurrentTime(8);
897 window.fetch = jest.fn(async () =>
898 {
899 return new Response(null, {
900 status: 204,
901 headers: {
902 'X-Subtitle-Frame-Status': 'none',
903 'X-Subtitle-Request-Received-At-Ms': '1000',
904 'X-Subtitle-Response-Sent-At-Ms': '1010',
905 },
906 });
907 });
908
909 const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, {
910 sessionId: 'sess-1',
911 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
912 }, {
913 currentFetchEnabled: false,
914 cacheMissFallbackEnabled: false,
915 lookaheadSeconds: 0,
916 sendSubtitleLogBatch: (entries) =>
917 {
918 sentBatches.push(entries);
919 return Promise.resolve();
920 },
921 buildFrameRequest: ({ currentTime, streamIndex }) => ({
922 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
923 }),
924 });
925
926 await flushAsyncWork(window, 8);
927 controller.detach();
928 await flushAsyncWork(window, 4);
929
930 const flattened = sentBatches.flat();
931 expect(flattened.length).toBeGreaterThan(0);
932 const requestKinds = new Set(flattened.map((entry) => entry.requestKind).filter(Boolean));
933 expect(requestKinds.has('prefetch')).toBe(true);
934 expect(requestKinds.has('current')).toBe(false);
935
936 window.close();
937 });
938
939 test('cache-only mode falls back to throttled current fetch on cache miss', async () =>
940 {
941 const window = createWindow();
942 const helper = loadSharedVideoPlayer(window);
943 const { player, videojs } = buildPlayerHarness(window);
944
945 let requestCount = 0;
946 window.fetch = jest.fn(async () =>
947 {
948 requestCount += 1;
949 if (requestCount === 1)
950 {
951 return new Response(new Uint8Array([137, 80, 78, 71]), {
952 status: 200,
953 headers: {
954 'Content-Type': 'image/png',
955 'X-Subtitle-Frame-Start-Time': '2.00',
956 'X-Subtitle-Frame-End-Time': '2.50',
957 'X-Subtitle-Frame-Width': '600',
958 'X-Subtitle-Frame-Height': '72',
959 'X-Subtitle-Frame-Canvas-Width': '1920',
960 'X-Subtitle-Frame-Canvas-Height': '1080',
961 },
962 });
963 }
964
965 return new Response(null, {
966 status: 204,
967 headers: { 'X-Subtitle-Frame-Status': 'none' },
968 });
969 });
970
971 player.setCurrentTime(2.2);
972
973 helper.bindImageSubtitleOverlay(player, videojs, {}, {
974 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
975 }, {
976 currentFetchEnabled: false,
977 cacheMissFallbackIntervalMs: 2000,
978 lookaheadSeconds: 0,
979 initialPrefetchSeconds: 0,
980 buildFrameRequest: ({ currentTime, streamIndex }) => ({
981 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
982 }),
983 });
984
985 await flushAsyncWork(window, 10);
986
987 const overlay = player.el().querySelector('.video-subtitle-overlay');
988 const overlayImg = overlay && overlay.querySelector('img');
989 expect(window.fetch).toHaveBeenCalled();
990 expect(overlay.style.display).toBe('block');
991 expect(overlayImg.getAttribute('src')).toBe('blob:subtitle');
992
993 window.close();
994 });
995
996 test('cache-miss fallback does not reset prefetch progress on repeated triggers', async () =>
997 {
998 const window = createWindow();
999 const helper = loadSharedVideoPlayer(window);
1000 const { player, videojs } = buildPlayerHarness(window);
1001
1002 let nowMs = 1000;
1003 const realNow = window.Date.now;
1004 window.Date.now = () => nowMs;
1005
1006 let abortedCount = 0;
1007 window.fetch = jest.fn((url, init = {}) =>
1008 {
1009 return new Promise((resolve, reject) =>
1010 {
1011 const signal = init && init.signal;
1012 const onAbort = () =>
1013 {
1014 abortedCount += 1;
1015 const err = new Error('aborted');
1016 err.name = 'AbortError';
1017 reject(err);
1018 };
1019
1020 if (signal && typeof signal.addEventListener === 'function')
1021 {
1022 if (signal.aborted) return onAbort();
1023 signal.addEventListener('abort', onAbort, { once: true });
1024 }
1025
1026 window.setTimeout(() =>
1027 {
1028 if (signal && signal.aborted) return;
1029 resolve(new Response(null, {
1030 status: 204,
1031 headers: { 'X-Subtitle-Frame-Status': 'none' },
1032 }));
1033 }, 80);
1034 });
1035 });
1036
1037 const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, {
1038 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
1039 }, {
1040 currentFetchEnabled: false,
1041 cacheMissFallbackIntervalMs: 20,
1042 lookaheadSeconds: 2,
1043 initialPrefetchSeconds: 0,
1044 buildFrameRequest: ({ currentTime, streamIndex }) => ({
1045 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
1046 }),
1047 });
1048
1049 await flushAsyncWork(window, 4);
1050
1051 player.setCurrentTime(5);
1052 player.trigger('timeupdate');
1053 nowMs += 40;
1054 player.setCurrentTime(5.5);
1055 player.trigger('timeupdate');
1056 nowMs += 40;
1057 player.setCurrentTime(6);
1058 player.trigger('timeupdate');
1059
1060 await flushAsyncWork(window, 12);
1061 expect(abortedCount).toBe(0);
1062
1063 window.Date.now = realNow;
1064 controller.detach();
1065 window.close();
1066 });
1067
1068 test('cache-only mode performs initial warmup prefetch window on load', async () =>
1069 {
1070 const window = createWindow();
1071 const helper = loadSharedVideoPlayer(window);
1072 const { player, videojs } = buildPlayerHarness(window);
1073 const requestedTimes = [];
1074
1075 window.fetch = jest.fn(async (url) =>
1076 {
1077 const parsed = new URL(url, 'http://localhost/');
1078 requestedTimes.push(Number(parsed.searchParams.get('timeSeconds')));
1079 return new Response(null, {
1080 status: 204,
1081 headers: {
1082 'X-Subtitle-Frame-Status': 'none',
1083 'X-Subtitle-Request-Received-At-Ms': '1000',
1084 'X-Subtitle-Response-Sent-At-Ms': '1010',
1085 },
1086 });
1087 });
1088
1089 const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, {
1090 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
1091 }, {
1092 currentFetchEnabled: false,
1093 lookaheadSeconds: 0,
1094 initialPrefetchSeconds: 2,
1095 buildFrameRequest: ({ currentTime, streamIndex }) => ({
1096 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
1097 }),
1098 });
1099
1100 await flushAsyncWork(window, 12);
1101
1102 expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2]));
1103
1104 controller.detach();
1105 await flushAsyncWork(window, 2);
1106 window.close();
1107 });
1108
1109 test('renders requested and received subtitle markers on the seek bar', async () =>
1110 {
1111 const window = createWindow();
1112 const helper = loadSharedVideoPlayer(window);
1113 const { player, videojs } = buildPlayerHarness(window);
1114
1115 window.fetch = jest.fn(async () =>
1116 {
1117 return new Response('png-data', {
1118 status: 200,
1119 headers: {
1120 'Content-Type': 'image/png',
1121 'X-Subtitle-Frame-Status': 'ok',
1122 'X-Subtitle-Request-Received-At-Ms': '1000',
1123 'X-Subtitle-Response-Sent-At-Ms': '1012',
1124 'X-Subtitle-Frame-Start-Time': '2.5',
1125 'X-Subtitle-Frame-End-Time': '3.5',
1126 'X-Subtitle-Frame-Width': '320',
1127 'X-Subtitle-Frame-Height': '180',
1128 'X-Subtitle-Frame-Canvas-Width': '640',
1129 'X-Subtitle-Frame-Canvas-Height': '360',
1130 },
1131 });
1132 });
1133
1134 player.duration(10);
1135 const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, {
1136 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
1137 }, {
1138 lookaheadSeconds: 0,
1139 buildFrameRequest: ({ currentTime, streamIndex }) => ({
1140 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
1141 }),
1142 });
1143
1144 await flushAsyncWork(window, 8);
1145
1146 const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right');
1147 const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left');
1148 expect(requestedMarker).toBeTruthy();
1149 expect(receivedMarker).toBeTruthy();
1150 expect(requestedMarker.style.display).toBe('block');
1151 expect(receivedMarker.style.display).toBe('block');
1152 expect(requestedMarker.style.left).toBe('100.0000%');
1153 expect(receivedMarker.style.left).toBe('25.0000%');
1154 expect(requestedMarker.textContent).toBe(']');
1155 expect(receivedMarker.textContent).toBe('[');
1156
1157 controller.detach();
1158 await new Promise((resolve) => window.setTimeout(resolve, 300));
1159 await flushAsyncWork(window, 2);
1160
1161 window.close();
1162 });
1163
1164 test('advances both seek markers when subtitle request returns 204 without frame timing headers', async () =>
1165 {
1166 const window = createWindow();
1167 const helper = loadSharedVideoPlayer(window);
1168 const { player, videojs } = buildPlayerHarness(window);
1169
1170 player.duration(10);
1171 player.setCurrentTime(4);
1172 window.fetch = jest.fn(async () =>
1173 {
1174 return new Response(null, {
1175 status: 204,
1176 headers: {
1177 'X-Subtitle-Frame-Status': 'none',
1178 'X-Subtitle-Request-Received-At-Ms': '1000',
1179 'X-Subtitle-Response-Sent-At-Ms': '1010',
1180 },
1181 });
1182 });
1183
1184 const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, {
1185 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
1186 }, {
1187 lookaheadSeconds: 0,
1188 buildFrameRequest: ({ currentTime, streamIndex }) => ({
1189 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
1190 }),
1191 });
1192
1193 await flushAsyncWork(window, 8);
1194
1195 const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right');
1196 const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left');
1197 expect(requestedMarker).toBeTruthy();
1198 expect(receivedMarker).toBeTruthy();
1199 expect(requestedMarker.style.display).toBe('block');
1200 expect(receivedMarker.style.display).toBe('block');
1201 expect(requestedMarker.style.left).toBe('100.0000%');
1202 expect(receivedMarker.style.left).toBe('100.0000%');
1203 expect(requestedMarker.textContent).toBe(']');
1204 expect(receivedMarker.textContent).toBe('[');
1205
1206 controller.detach();
1207 await flushAsyncWork(window, 2);
1208 window.close();
1209 });
1210
1211 test('advances seek markers to the end of an empty prefetch tail window', async () =>
1212 {
1213 const window = createWindow();
1214 const helper = loadSharedVideoPlayer(window);
1215 const { player, videojs } = buildPlayerHarness(window);
1216
1217 player.duration(120);
1218 player.setCurrentTime(110);
1219
1220 window.fetch = jest.fn(async () =>
1221 {
1222 return new Response(null, {
1223 status: 204,
1224 headers: {
1225 'X-Subtitle-Frame-Status': 'none',
1226 'X-Subtitle-Request-Received-At-Ms': '1000',
1227 'X-Subtitle-Response-Sent-At-Ms': '1010',
1228 },
1229 });
1230 });
1231
1232 const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, {
1233 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
1234 }, {
1235 lookaheadSeconds: 0,
1236 initialPrefetchSeconds: 0,
1237 currentFetchEnabled: false,
1238 cacheMissFallbackEnabled: false,
1239 prefetchAdaptiveStepEnabled: true,
1240 prefetchStepSecondsMin: 10,
1241 prefetchStepSecondsMax: 10,
1242 buildFrameRequest: ({ currentTime, streamIndex }) => ({
1243 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
1244 }),
1245 buildPrefetchRequest: ({ currentTime, endTime, streamIndex }) => ({
1246 url: `/subtitle-range?streamIndex=${streamIndex}&startTimeSeconds=${currentTime}&endTimeSeconds=${endTime}`,
1247 }),
1248 });
1249
1250 player.trigger('timeupdate');
1251 await flushAsyncWork(window, 8);
1252
1253 const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right');
1254 const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left');
1255 expect(requestedMarker).toBeTruthy();
1256 expect(receivedMarker).toBeTruthy();
1257 expect(requestedMarker.style.left).toBe('100.0000%');
1258 expect(receivedMarker.style.left).toBe('100.0000%');
1259
1260 controller.detach();
1261 await flushAsyncWork(window, 2);
1262 window.close();
1263 });
1264
1265 test('keeps existing marker positions while the next request is still in flight', async () =>
1266 {
1267 const window = createWindow();
1268 const helper = loadSharedVideoPlayer(window);
1269 const { player, videojs } = buildPlayerHarness(window);
1270 let pendingResolve = null;
1271
1272 player.duration(10);
1273 window.fetch = jest.fn((url) =>
1274 {
1275 const parsed = new URL(url, 'http://localhost/');
1276 const timeSeconds = Number(parsed.searchParams.get('timeSeconds'));
1277
1278 if (timeSeconds === 0)
1279 {
1280 return Promise.resolve(new Response('png-data', {
1281 status: 200,
1282 headers: {
1283 'Content-Type': 'image/png',
1284 'X-Subtitle-Frame-Status': 'ok',
1285 'X-Subtitle-Frame-Start-Time': '2',
1286 'X-Subtitle-Frame-End-Time': '3',
1287 'X-Subtitle-Frame-Width': '320',
1288 'X-Subtitle-Frame-Height': '180',
1289 'X-Subtitle-Frame-Canvas-Width': '640',
1290 'X-Subtitle-Frame-Canvas-Height': '360',
1291 },
1292 }));
1293 }
1294
1295 return new Promise((resolve) =>
1296 {
1297 pendingResolve = resolve;
1298 });
1299 });
1300
1301 const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, {
1302 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
1303 }, {
1304 lookaheadSeconds: 0,
1305 buildFrameRequest: ({ currentTime, streamIndex }) => ({
1306 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
1307 }),
1308 });
1309
1310 await flushAsyncWork(window, 8);
1311
1312 const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right');
1313 const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left');
1314 expect(requestedMarker).toBeTruthy();
1315 expect(receivedMarker).toBeTruthy();
1316 expect(requestedMarker.style.left).toBe('0.0000%');
1317 expect(receivedMarker.style.left).toBe('20.0000%');
1318
1319 player.setCurrentTime(1.5);
1320 player.trigger('timeupdate');
1321 await flushAsyncWork(window, 2);
1322
1323 expect(requestedMarker.style.left).toBe('0.0000%');
1324 expect(receivedMarker.style.left).toBe('20.0000%');
1325
1326 pendingResolve(new Response(null, {
1327 status: 204,
1328 headers: {
1329 'X-Subtitle-Frame-Status': 'none',
1330 },
1331 }));
1332
1333 await flushAsyncWork(window, 8);
1334 expect(requestedMarker.style.display).toBe('block');
1335 expect(receivedMarker.style.display).toBe('block');
1336 expect(requestedMarker.style.left).toBe('15.0000%');
1337 expect(receivedMarker.style.left).toBe('20.0000%');
1338
1339 controller.detach();
1340 await flushAsyncWork(window, 2);
1341 window.close();
1342 });
1343
1344 test('keeps marker positions forward-only when later responses resolve with earlier times', async () =>
1345 {
1346 const window = createWindow();
1347 const helper = loadSharedVideoPlayer(window);
1348 const { player, videojs } = buildPlayerHarness(window);
1349 const requestedTimes = [];
1350
1351 player.duration(12);
1352 window.fetch = jest.fn(async (url) =>
1353 {
1354 const parsed = new URL(url, 'http://localhost/');
1355 const timeSeconds = Number(parsed.searchParams.get('timeSeconds'));
1356 requestedTimes.push(timeSeconds);
1357
1358 if (requestedTimes.length === 1)
1359 {
1360 return new Response('png-data', {
1361 status: 200,
1362 headers: {
1363 'Content-Type': 'image/png',
1364 'X-Subtitle-Frame-Status': 'ok',
1365 'X-Subtitle-Frame-Start-Time': '4',
1366 'X-Subtitle-Frame-End-Time': '5',
1367 'X-Subtitle-Frame-Width': '320',
1368 'X-Subtitle-Frame-Height': '180',
1369 'X-Subtitle-Frame-Canvas-Width': '640',
1370 'X-Subtitle-Frame-Canvas-Height': '360',
1371 },
1372 });
1373 }
1374
1375 return new Response('png-data', {
1376 status: 200,
1377 headers: {
1378 'Content-Type': 'image/png',
1379 'X-Subtitle-Frame-Status': 'ok',
1380 'X-Subtitle-Frame-Start-Time': '2',
1381 'X-Subtitle-Frame-End-Time': '3',
1382 'X-Subtitle-Frame-Width': '320',
1383 'X-Subtitle-Frame-Height': '180',
1384 'X-Subtitle-Frame-Canvas-Width': '640',
1385 'X-Subtitle-Frame-Canvas-Height': '360',
1386 },
1387 });
1388 });
1389
1390 const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, {
1391 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
1392 }, {
1393 lookaheadSeconds: 0,
1394 buildFrameRequest: ({ currentTime, streamIndex }) => ({
1395 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
1396 }),
1397 });
1398
1399 await flushAsyncWork(window, 8);
1400
1401 const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right');
1402 const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left');
1403 expect(requestedMarker).toBeTruthy();
1404 expect(receivedMarker).toBeTruthy();
1405 expect(requestedMarker.style.left).toBe('100.0000%');
1406 expect(receivedMarker.style.left).toBe('33.3333%');
1407
1408 player.setCurrentTime(1.5);
1409 player.trigger('timeupdate');
1410 await flushAsyncWork(window, 8);
1411
1412 // Second response reports an older start time, but markers stay monotonic.
1413 expect(requestedMarker.style.left).toBe('100.0000%');
1414 expect(receivedMarker.style.left).toBe('33.3333%');
1415
1416 controller.detach();
1417 await flushAsyncWork(window, 2);
1418 window.close();
1419 });
1420
1421 test('continues current subtitle refreshes after a force-seek aborts the in-flight request', async () =>
1422 {
1423 const window = createWindow();
1424 const helper = loadSharedVideoPlayer(window);
1425 const { player, videojs } = buildPlayerHarness(window);
1426 const requestedTimes = [];
1427
1428 window.fetch = jest.fn((url, init = {}) =>
1429 {
1430 const parsed = new URL(url, 'http://localhost/');
1431 const timeSeconds = Number(parsed.searchParams.get('timeSeconds'));
1432 requestedTimes.push(timeSeconds);
1433
1434 if (requestedTimes.length === 1)
1435 {
1436 return new Promise((resolve, reject) =>
1437 {
1438 if (init.signal && typeof init.signal.addEventListener === 'function')
1439 {
1440 init.signal.addEventListener('abort', () =>
1441 {
1442 reject(new window.DOMException('Aborted', 'AbortError'));
1443 }, { once: true });
1444 }
1445 });
1446 }
1447
1448 return Promise.resolve(new Response(null, {
1449 status: 204,
1450 headers: {
1451 'X-Subtitle-Frame-Status': 'none',
1452 },
1453 }));
1454 });
1455
1456 const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, {
1457 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
1458 }, {
1459 lookaheadSeconds: 0,
1460 currentRequestTimeoutMs: 300,
1461 logToConsole: false,
1462 buildFrameRequest: ({ currentTime, streamIndex }) => ({
1463 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
1464 }),
1465 });
1466
1467 await flushAsyncWork(window, 4);
1468 const afterInitialLoadCalls = window.fetch.mock.calls.length;
1469 expect(afterInitialLoadCalls).toBeGreaterThan(0);
1470
1471 player.setCurrentTime(5);
1472 player.trigger('seeking');
1473 await flushAsyncWork(window, 20);
1474
1475 expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2);
1476 expect(requestedTimes).toEqual(expect.arrayContaining([0, 5]));
1477
1478 controller.detach();
1479 await flushAsyncWork(window, 2);
1480 window.close();
1481 });
1482
1483 test('recovers when a current subtitle request hangs and times out', async () =>
1484 {
1485 const window = createWindow();
1486 const helper = loadSharedVideoPlayer(window);
1487 const { player, videojs } = buildPlayerHarness(window);
1488 const requestedTimes = [];
1489
1490 window.fetch = jest.fn((url, init = {}) =>
1491 {
1492 const parsed = new URL(url, 'http://localhost/');
1493 const timeSeconds = Number(parsed.searchParams.get('timeSeconds'));
1494 requestedTimes.push(timeSeconds);
1495
1496 if (requestedTimes.length === 1)
1497 {
1498 return new Promise((resolve, reject) =>
1499 {
1500 if (init.signal && typeof init.signal.addEventListener === 'function')
1501 {
1502 init.signal.addEventListener('abort', () =>
1503 {
1504 reject(new window.DOMException('Aborted', 'AbortError'));
1505 }, { once: true });
1506 }
1507 });
1508 }
1509
1510 return Promise.resolve(new Response(null, {
1511 status: 204,
1512 headers: { 'X-Subtitle-Frame-Status': 'none' },
1513 }));
1514 });
1515
1516 const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, {
1517 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
1518 }, {
1519 lookaheadSeconds: 0,
1520 currentRequestTimeoutMs: 250,
1521 logToConsole: false,
1522 buildFrameRequest: ({ currentTime, streamIndex }) => ({
1523 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
1524 }),
1525 });
1526
1527 await flushAsyncWork(window, 3);
1528
1529 player.setCurrentTime(1.5);
1530 player.trigger('timeupdate');
1531
1532 await new Promise((resolve) => window.setTimeout(resolve, 320));
1533 player.trigger('timeupdate');
1534 await new Promise((resolve) => window.setTimeout(resolve, 220));
1535 await flushAsyncWork(window, 20);
1536
1537 expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2);
1538 expect(requestedTimes).toEqual(expect.arrayContaining([0, 1.5]));
1539
1540 controller.detach();
1541 await new Promise((resolve) => window.setTimeout(resolve, 200));
1542 await flushAsyncWork(window, 20);
1543 window.close();
1544 });
1545
1546 test('reuses cached subtitle frames after rewinding when server no longer has older frames', async () =>
1547 {
1548 const window = createWindow();
1549 const helper = loadSharedVideoPlayer(window);
1550 const { player, videojs } = buildPlayerHarness(window);
1551
1552 player.duration(10);
1553 player.setCurrentTime(2);
1554 window.fetch = jest.fn(async (url) =>
1555 {
1556 const parsed = new URL(url, 'http://localhost/');
1557 const timeSeconds = Number(parsed.searchParams.get('timeSeconds'));
1558
1559 if (Math.abs(timeSeconds - 2) < 1e-6)
1560 {
1561 return new Response('png-data', {
1562 status: 200,
1563 headers: {
1564 'Content-Type': 'image/png',
1565 'X-Subtitle-Frame-Status': 'ok',
1566 'X-Subtitle-Frame-Start-Time': '2',
1567 'X-Subtitle-Frame-End-Time': '3',
1568 'X-Subtitle-Frame-Width': '320',
1569 'X-Subtitle-Frame-Height': '180',
1570 'X-Subtitle-Frame-Canvas-Width': '640',
1571 'X-Subtitle-Frame-Canvas-Height': '360',
1572 },
1573 });
1574 }
1575
1576 return new Response(null, {
1577 status: 204,
1578 headers: {
1579 'X-Subtitle-Frame-Status': 'none',
1580 },
1581 });
1582 });
1583
1584 const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, {
1585 imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }],
1586 }, {
1587 lookaheadSeconds: 0,
1588 buildFrameRequest: ({ currentTime, streamIndex }) => ({
1589 url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`,
1590 }),
1591 });
1592
1593 await flushAsyncWork(window, 8);
1594 const afterInitialLoadCalls = window.fetch.mock.calls.length;
1595 expect(afterInitialLoadCalls).toBeGreaterThan(0);
1596
1597 player.setCurrentTime(4);
1598 player.trigger('timeupdate');
1599 await flushAsyncWork(window, 8);
1600 const afterForwardSeekCalls = window.fetch.mock.calls.length;
1601 expect(afterForwardSeekCalls).toBeGreaterThanOrEqual(afterInitialLoadCalls);
1602
1603 const overlay = player.el().querySelector('.video-subtitle-overlay');
1604 expect(overlay).toBeTruthy();
1605 expect(overlay.style.display).toBe('none');
1606
1607 player.setCurrentTime(2.5);
1608 player.trigger('timeupdate');
1609 await flushAsyncWork(window, 8);
1610
1611 expect(window.fetch.mock.calls.length).toBe(afterForwardSeekCalls);
1612 expect(overlay.style.display).toBe('block');
1613
1614 controller.detach();
1615 await flushAsyncWork(window, 2);
1616 window.close();
1617 });
1618
1619 test('registers stream cache service worker once when supported', async () =>
1620 {
1621 const window = createWindow();
1622 const helper = loadSharedVideoPlayer(window);
1623 const register = jest.fn(async () => ({}));
1624 window.navigator.serviceWorker = { register };
1625
1626 const first = await helper.enableVideoStreamClientCache();
1627 const second = await helper.enableVideoStreamClientCache();
1628
1629 expect(first).toBe(true);
1630 expect(second).toBe(true);
1631 expect(register).toHaveBeenCalledTimes(1);
1632 expect(register).toHaveBeenCalledWith('/stream-cache-sw.js', {
1633 scope: '/',
1634 updateViaCache: 'none',
1635 });
1636
1637 window.close();
1638 });
1639
1640 test('stream-ahead prefetch follows playlists and fetches segment assets', async () =>
1641 {
1642 const window = createWindow();
1643 const helper = loadSharedVideoPlayer(window);
1644 const requestedUrls = [];
1645
1646 window.fetch = jest.fn(async (input) =>
1647 {
1648 const url = String(input && input.url ? input.url : input);
1649 requestedUrls.push(url);
1650
1651 if (url === 'http://localhost/master.m3u8')
1652 {
1653 return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', {
1654 status: 200,
1655 headers: { 'Content-Type': 'application/vnd.apple.mpegurl' },
1656 });
1657 }
1658
1659 if (url === 'http://localhost/stream_0.m3u8')
1660 {
1661 return new Response('#EXTM3U\n#EXTINF:4,\nseg0.ts\n#EXTINF:4,\nseg1.ts\n#EXT-X-ENDLIST\n', {
1662 status: 200,
1663 headers: { 'Content-Type': 'application/vnd.apple.mpegurl' },
1664 });
1665 }
1666
1667 if (url === 'http://localhost/seg0.ts' || url === 'http://localhost/seg1.ts')
1668 {
1669 return new Response(new Uint8Array([1, 2, 3, 4]), {
1670 status: 200,
1671 headers: { 'Content-Type': 'video/mp2t' },
1672 });
1673 }
1674
1675 return new Response(null, { status: 404 });
1676 });
1677
1678 const controller = helper.bindStreamAheadPrefetch({
1679 playlistUrl: 'http://localhost/master.m3u8',
1680 pollIntervalMs: 0,
1681 credentials: 'same-origin',
1682 });
1683
1684 expect(controller).toBeTruthy();
1685 await controller.done();
1686
1687 expect(requestedUrls).toEqual(expect.arrayContaining([
1688 'http://localhost/master.m3u8',
1689 'http://localhost/stream_0.m3u8',
1690 'http://localhost/seg0.ts',
1691 'http://localhost/seg1.ts',
1692 ]));
1693
1694 controller.stop();
1695 window.close();
1696 });
1697
1698 test('stream-ahead prefetch reports cached-through progress for fetched segments', async () =>
1699 {
1700 const window = createWindow();
1701 const helper = loadSharedVideoPlayer(window);
1702 const progressSamples = [];
1703
1704 window.fetch = jest.fn(async (input) =>
1705 {
1706 const url = String(input && input.url ? input.url : input);
1707
1708 if (url === 'http://localhost/master.m3u8')
1709 {
1710 return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', {
1711 status: 200,
1712 headers: { 'Content-Type': 'application/vnd.apple.mpegurl' },
1713 });
1714 }
1715
1716 if (url === 'http://localhost/stream_0.m3u8')
1717 {
1718 return new Response('#EXTM3U\n#EXTINF:4,\nseg0.ts\n#EXTINF:4,\nseg1.ts\n#EXT-X-ENDLIST\n', {
1719 status: 200,
1720 headers: { 'Content-Type': 'application/vnd.apple.mpegurl' },
1721 });
1722 }
1723
1724 if (url === 'http://localhost/seg0.ts' || url === 'http://localhost/seg1.ts')
1725 {
1726 return new Response(new Uint8Array([1, 2, 3, 4]), {
1727 status: 200,
1728 headers: { 'Content-Type': 'video/mp2t' },
1729 });
1730 }
1731
1732 return new Response(null, { status: 404 });
1733 });
1734
1735 const controller = helper.bindStreamAheadPrefetch({
1736 playlistUrl: 'http://localhost/master.m3u8',
1737 pollIntervalMs: 0,
1738 credentials: 'same-origin',
1739 });
1740
1741 expect(controller).toBeTruthy();
1742 const unsubscribe = controller.subscribe((status) =>
1743 {
1744 progressSamples.push(status && status.cachedThroughSeconds);
1745 });
1746
1747 await controller.done();
1748 unsubscribe();
1749
1750 expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true);
1751
1752 controller.stop();
1753 window.close();
1754 });
1755
1756 test('stream-ahead prefetch infers cached-through progress from segment filenames', async () =>
1757 {
1758 const window = createWindow();
1759 const helper = loadSharedVideoPlayer(window);
1760 const progressSamples = [];
1761
1762 window.fetch = jest.fn(async (input) =>
1763 {
1764 const url = String(input && input.url ? input.url : input);
1765
1766 if (url === 'http://localhost/master.m3u8')
1767 {
1768 return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', {
1769 status: 200,
1770 headers: { 'Content-Type': 'application/vnd.apple.mpegurl' },
1771 });
1772 }
1773
1774 if (url === 'http://localhost/stream_0.m3u8')
1775 {
1776 return new Response('#EXTM3U\nstream_0_segment_000000.ts\nstream_0_segment_000001.ts\n#EXT-X-ENDLIST\n', {
1777 status: 200,
1778 headers: { 'Content-Type': 'application/vnd.apple.mpegurl' },
1779 });
1780 }
1781
1782 if (url === 'http://localhost/stream_0_segment_000000.ts' || url === 'http://localhost/stream_0_segment_000001.ts')
1783 {
1784 return new Response(new Uint8Array([1, 2, 3, 4]), {
1785 status: 200,
1786 headers: { 'Content-Type': 'video/mp2t' },
1787 });
1788 }
1789
1790 return new Response(null, { status: 404 });
1791 });
1792
1793 const controller = helper.bindStreamAheadPrefetch({
1794 playlistUrl: 'http://localhost/master.m3u8',
1795 pollIntervalMs: 0,
1796 credentials: 'same-origin',
1797 });
1798
1799 expect(controller).toBeTruthy();
1800 const unsubscribe = controller.subscribe((status) =>
1801 {
1802 progressSamples.push(status && status.cachedThroughSeconds);
1803 });
1804
1805 await controller.done();
1806 unsubscribe();
1807
1808 expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true);
1809
1810 controller.stop();
1811 window.close();
1812 });
1813
1814 test('stream-ahead prefetch infers cached-through progress for m4s segment filenames', async () =>
1815 {
1816 const window = createWindow();
1817 const helper = loadSharedVideoPlayer(window);
1818 const progressSamples = [];
1819
1820 window.fetch = jest.fn(async (input) =>
1821 {
1822 const url = String(input && input.url ? input.url : input);
1823
1824 if (url === 'http://localhost/master.m3u8')
1825 {
1826 return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', {
1827 status: 200,
1828 headers: { 'Content-Type': 'application/vnd.apple.mpegurl' },
1829 });
1830 }
1831
1832 if (url === 'http://localhost/stream_0.m3u8')
1833 {
1834 return new Response('#EXTM3U\nstream_0_segment_000000.m4s\nstream_0_segment_000001.m4s\n#EXT-X-ENDLIST\n', {
1835 status: 200,
1836 headers: { 'Content-Type': 'application/vnd.apple.mpegurl' },
1837 });
1838 }
1839
1840 if (url === 'http://localhost/stream_0_segment_000000.m4s' || url === 'http://localhost/stream_0_segment_000001.m4s')
1841 {
1842 return new Response(new Uint8Array([1, 2, 3, 4]), {
1843 status: 200,
1844 headers: { 'Content-Type': 'video/mp4' },
1845 });
1846 }
1847
1848 return new Response(null, { status: 404 });
1849 });
1850
1851 const controller = helper.bindStreamAheadPrefetch({
1852 playlistUrl: 'http://localhost/master.m3u8',
1853 pollIntervalMs: 0,
1854 credentials: 'same-origin',
1855 });
1856
1857 expect(controller).toBeTruthy();
1858 const unsubscribe = controller.subscribe((status) =>
1859 {
1860 progressSamples.push(status && status.cachedThroughSeconds);
1861 });
1862
1863 await controller.done();
1864 unsubscribe();
1865
1866 expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true);
1867
1868 controller.stop();
1869 window.close();
1870 });
1871
1872 test('stream-ahead prefetch reports ratio progress from fetched sections when timing is unknown', async () =>
1873 {
1874 const window = createWindow();
1875 const helper = loadSharedVideoPlayer(window);
1876 const statusSamples = [];
1877
1878 window.fetch = jest.fn(async (input) =>
1879 {
1880 const url = String(input && input.url ? input.url : input);
1881
1882 if (url === 'http://localhost/master.m3u8')
1883 {
1884 return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', {
1885 status: 200,
1886 headers: { 'Content-Type': 'application/vnd.apple.mpegurl' },
1887 });
1888 }
1889
1890 if (url === 'http://localhost/stream_0.m3u8')
1891 {
1892 return new Response('#EXTM3U\nchunk-alpha.bin\nchunk-beta.bin\n#EXT-X-ENDLIST\n', {
1893 status: 200,
1894 headers: { 'Content-Type': 'application/vnd.apple.mpegurl' },
1895 });
1896 }
1897
1898 if (url === 'http://localhost/chunk-alpha.bin' || url === 'http://localhost/chunk-beta.bin')
1899 {
1900 return new Response(new Uint8Array([1, 2, 3, 4]), {
1901 status: 200,
1902 headers: { 'Content-Type': 'application/octet-stream' },
1903 });
1904 }
1905
1906 return new Response(null, { status: 404 });
1907 });
1908
1909 const controller = helper.bindStreamAheadPrefetch({
1910 playlistUrl: 'http://localhost/master.m3u8',
1911 pollIntervalMs: 0,
1912 credentials: 'same-origin',
1913 });
1914
1915 expect(controller).toBeTruthy();
1916 const unsubscribe = controller.subscribe((status) =>
1917 {
1918 statusSamples.push(status || {});
1919 });
1920
1921 await controller.done();
1922 unsubscribe();
1923
1924 const finalStatus = statusSamples[statusSamples.length - 1] || {};
1925 expect(finalStatus.totalSegmentCount).toBe(2);
1926 expect(finalStatus.fetchedSegmentCount).toBe(2);
1927 expect(finalStatus.cachedThroughRatio).toBe(1);
1928
1929 controller.stop();
1930 window.close();
1931 });
1932
1933 test('clears cached stream assets for a finished playlist session', async () =>
1934 {
1935 const window = createWindow();
1936 const helper = loadSharedVideoPlayer(window);
1937 const requests = [
1938 new window.Request('http://localhost/api/files/video-stream/sess-a/master.m3u8'),
1939 new window.Request('http://localhost/api/files/video-stream/sess-a/stream_0_segment_000001.ts'),
1940 new window.Request('http://localhost/api/files/video-stream/sess-b/master.m3u8'),
1941 ];
1942 const deleteCalls = [];
1943 const cache = {
1944 keys: jest.fn(async () => requests),
1945 delete: jest.fn(async (request) =>
1946 {
1947 deleteCalls.push(request.url);
1948 return true;
1949 }),
1950 };
1951 window.caches = {
1952 open: jest.fn(async () => cache),
1953 };
1954
1955 await helper.clearVideoStreamClientCache('http://localhost/api/files/video-stream/sess-a/master.m3u8');
1956
1957 expect(window.caches.open).toHaveBeenCalledWith('filemanager-stream-cache-v1');
1958 expect(cache.keys).toHaveBeenCalledTimes(1);
1959 expect(deleteCalls).toEqual(expect.arrayContaining([
1960 'http://localhost/api/files/video-stream/sess-a/master.m3u8',
1961 'http://localhost/api/files/video-stream/sess-a/stream_0_segment_000001.ts',
1962 ]));
1963 expect(deleteCalls).not.toContain('http://localhost/api/files/video-stream/sess-b/master.m3u8');
1964
1965 window.close();
1966 });
1967});