| 1 | 'use strict'; |
| 2 | |
| 3 | const fs = require('fs'); |
| 4 | const path = require('path'); |
| 5 | const { JSDOM } = require('jsdom'); |
| 6 | |
| 7 | function 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 | |
| 17 | function 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 | |
| 135 | function 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 | |
| 142 | describe('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('prefetches at least through the buffered video end', async () => |
| 231 | { |
| 232 | const window = createWindow(); |
| 233 | const helper = loadSharedVideoPlayer(window); |
| 234 | const { player, videojs } = buildPlayerHarness(window); |
| 235 | const requestedTimes = []; |
| 236 | |
| 237 | player.setBufferedEnd(3); |
| 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 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 254 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 255 | }), |
| 256 | }); |
| 257 | |
| 258 | await flushAsyncWork(window, 12); |
| 259 | |
| 260 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3])); |
| 261 | |
| 262 | window.close(); |
| 263 | }); |
| 264 | |
| 265 | test('prefetch uses furthest buffered end when player.bufferedEnd under-reports', async () => |
| 266 | { |
| 267 | const window = createWindow(); |
| 268 | const helper = loadSharedVideoPlayer(window); |
| 269 | const { player, videojs } = buildPlayerHarness(window); |
| 270 | const requestedTimes = []; |
| 271 | |
| 272 | player.setBufferedEnd(0); |
| 273 | player.tech = () => ({ |
| 274 | el: () => ({ |
| 275 | buffered: { |
| 276 | length: 1, |
| 277 | end: () => 6, |
| 278 | }, |
| 279 | }), |
| 280 | }); |
| 281 | |
| 282 | window.fetch = jest.fn(async (url) => |
| 283 | { |
| 284 | const parsed = new URL(url, 'http://localhost/'); |
| 285 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 286 | return new Response(null, { |
| 287 | status: 204, |
| 288 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 289 | }); |
| 290 | }); |
| 291 | |
| 292 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 293 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 294 | }, { |
| 295 | lookaheadSeconds: 0, |
| 296 | initialPrefetchSeconds: 0, |
| 297 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 298 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 299 | }), |
| 300 | }); |
| 301 | |
| 302 | await flushAsyncWork(window, 16); |
| 303 | |
| 304 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6])); |
| 305 | |
| 306 | window.close(); |
| 307 | }); |
| 308 | |
| 309 | test('prefetch frontier jumps to current playback time instead of replaying stale lag', async () => |
| 310 | { |
| 311 | const window = createWindow(); |
| 312 | const helper = loadSharedVideoPlayer(window); |
| 313 | const { player, videojs } = buildPlayerHarness(window); |
| 314 | const requestedTimes = []; |
| 315 | |
| 316 | player.setBufferedEnd(35); |
| 317 | |
| 318 | window.fetch = jest.fn(async (url) => |
| 319 | { |
| 320 | const parsed = new URL(url, 'http://localhost/'); |
| 321 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 322 | requestedTimes.push(timeSeconds); |
| 323 | return new Response(null, { |
| 324 | status: 204, |
| 325 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 326 | }); |
| 327 | }); |
| 328 | |
| 329 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 330 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 331 | }, { |
| 332 | lookaheadSeconds: 0, |
| 333 | initialPrefetchSeconds: 0, |
| 334 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 335 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 336 | }), |
| 337 | }); |
| 338 | |
| 339 | await flushAsyncWork(window, 8); |
| 340 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5])); |
| 341 | |
| 342 | requestedTimes.length = 0; |
| 343 | player.setCurrentTime(20); |
| 344 | player.setBufferedEnd(40); |
| 345 | player.trigger('timeupdate'); |
| 346 | await flushAsyncWork(window, 8); |
| 347 | |
| 348 | expect(requestedTimes).toContain(20); |
| 349 | const prefetchAfterJump = requestedTimes.filter((value) => value > 20); |
| 350 | expect(prefetchAfterJump.length).toBeGreaterThan(0); |
| 351 | expect(Math.min(...prefetchAfterJump)).toBeGreaterThanOrEqual(20.5); |
| 352 | |
| 353 | window.close(); |
| 354 | }); |
| 355 | |
| 356 | test('does not restart current subtitle request while one is in flight', async () => |
| 357 | { |
| 358 | const window = createWindow(); |
| 359 | const helper = loadSharedVideoPlayer(window); |
| 360 | const { player, videojs } = buildPlayerHarness(window); |
| 361 | const pendingResolvers = []; |
| 362 | const requestTimes = []; |
| 363 | |
| 364 | window.fetch = jest.fn((url, init = {}) => |
| 365 | { |
| 366 | const parsed = new URL(url, 'http://localhost/'); |
| 367 | requestTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 368 | |
| 369 | if (pendingResolvers.length === 0) |
| 370 | { |
| 371 | return new Promise((resolve) => |
| 372 | { |
| 373 | pendingResolvers.push(() => resolve(new Response(null, { |
| 374 | status: 204, |
| 375 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 376 | }))); |
| 377 | }); |
| 378 | } |
| 379 | |
| 380 | expect(init.signal && init.signal.aborted).toBe(false); |
| 381 | return Promise.resolve(new Response(null, { |
| 382 | status: 204, |
| 383 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 384 | })); |
| 385 | }); |
| 386 | |
| 387 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 388 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 389 | }, { |
| 390 | lookaheadSeconds: 0, |
| 391 | initialPrefetchSeconds: 0, |
| 392 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 393 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 394 | }), |
| 395 | }); |
| 396 | |
| 397 | await flushAsyncWork(window, 3); |
| 398 | expect(window.fetch).toHaveBeenCalledTimes(1); |
| 399 | |
| 400 | player.setCurrentTime(1.5); |
| 401 | player.trigger('timeupdate'); |
| 402 | player.setCurrentTime(2.25); |
| 403 | player.trigger('timeupdate'); |
| 404 | await flushAsyncWork(window, 2); |
| 405 | |
| 406 | expect(window.fetch).toHaveBeenCalledTimes(1); |
| 407 | |
| 408 | pendingResolvers[0](); |
| 409 | await flushAsyncWork(window, 5); |
| 410 | |
| 411 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 412 | expect(requestTimes).toEqual(expect.arrayContaining([0, 2.25])); |
| 413 | |
| 414 | window.close(); |
| 415 | }); |
| 416 | |
| 417 | test('aborts in-flight prefetch requests when the overlay is detached', async () => |
| 418 | { |
| 419 | const window = createWindow(); |
| 420 | const helper = loadSharedVideoPlayer(window); |
| 421 | const { player, videojs } = buildPlayerHarness(window); |
| 422 | const activeSignals = []; |
| 423 | |
| 424 | window.fetch = jest.fn((url, init = {}) => |
| 425 | { |
| 426 | const parsed = new URL(url, 'http://localhost/'); |
| 427 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 428 | |
| 429 | if (timeSeconds === 0) |
| 430 | { |
| 431 | return Promise.resolve(new Response(null, { |
| 432 | status: 204, |
| 433 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 434 | })); |
| 435 | } |
| 436 | |
| 437 | activeSignals.push(init.signal || null); |
| 438 | |
| 439 | return new Promise((resolve, reject) => |
| 440 | { |
| 441 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 442 | { |
| 443 | init.signal.addEventListener('abort', () => |
| 444 | { |
| 445 | reject(new DOMException('Aborted', 'AbortError')); |
| 446 | }, { once: true }); |
| 447 | } |
| 448 | }); |
| 449 | }); |
| 450 | |
| 451 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 452 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 453 | }, { |
| 454 | lookaheadSeconds: 1, |
| 455 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 456 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 457 | }), |
| 458 | }); |
| 459 | |
| 460 | await flushAsyncWork(window, 3); |
| 461 | expect(activeSignals.length).toBeGreaterThan(0); |
| 462 | |
| 463 | controller.detach(); |
| 464 | await flushAsyncWork(window, 2); |
| 465 | |
| 466 | expect(activeSignals.every(signal => signal && signal.aborted)).toBe(true); |
| 467 | |
| 468 | window.close(); |
| 469 | }); |
| 470 | |
| 471 | test('logs current subtitle request lifecycle events and flushes them on detach', async () => |
| 472 | { |
| 473 | const window = createWindow(); |
| 474 | const helper = loadSharedVideoPlayer(window); |
| 475 | const { player, videojs } = buildPlayerHarness(window); |
| 476 | const sentBatches = []; |
| 477 | |
| 478 | window.fetch = jest.fn(async () => |
| 479 | { |
| 480 | return new Response('png-data', { |
| 481 | status: 200, |
| 482 | headers: { |
| 483 | 'Content-Type': 'image/png', |
| 484 | 'X-Subtitle-Frame-Status': 'ok', |
| 485 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 486 | 'X-Subtitle-Response-Sent-At-Ms': '1012', |
| 487 | 'X-Subtitle-Frame-Start-Time': '1.5', |
| 488 | 'X-Subtitle-Frame-End-Time': '3.2', |
| 489 | 'X-Subtitle-Frame-Width': '320', |
| 490 | 'X-Subtitle-Frame-Height': '180', |
| 491 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 492 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 493 | }, |
| 494 | }); |
| 495 | }); |
| 496 | |
| 497 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 498 | sessionId: 'sess-1', |
| 499 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 500 | }, { |
| 501 | lookaheadSeconds: 0, |
| 502 | sendSubtitleLogBatch: (entries) => |
| 503 | { |
| 504 | sentBatches.push(entries); |
| 505 | return Promise.resolve(); |
| 506 | }, |
| 507 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 508 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 509 | }), |
| 510 | }); |
| 511 | |
| 512 | await flushAsyncWork(window, 8); |
| 513 | controller.detach(); |
| 514 | await flushAsyncWork(window, 4); |
| 515 | |
| 516 | const flattened = sentBatches.flat(); |
| 517 | expect(flattened.map((entry) => entry.eventType)).toEqual(expect.arrayContaining([ |
| 518 | 'subtitle_png_requested', |
| 519 | 'subtitle_png_sent', |
| 520 | 'subtitle_png_received', |
| 521 | ])); |
| 522 | |
| 523 | window.close(); |
| 524 | }); |
| 525 | |
| 526 | test('logs prefetch subtitle lifecycle events', async () => |
| 527 | { |
| 528 | const window = createWindow(); |
| 529 | const helper = loadSharedVideoPlayer(window); |
| 530 | const { player, videojs } = buildPlayerHarness(window); |
| 531 | const sentBatches = []; |
| 532 | |
| 533 | window.fetch = jest.fn(async () => |
| 534 | { |
| 535 | return new Response(null, { |
| 536 | status: 204, |
| 537 | headers: { |
| 538 | 'X-Subtitle-Frame-Status': 'none', |
| 539 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 540 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 541 | 'X-Subtitle-Frame-Start-Time': '2', |
| 542 | 'X-Subtitle-Frame-End-Time': '3', |
| 543 | }, |
| 544 | }); |
| 545 | }); |
| 546 | |
| 547 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 548 | sessionId: 'sess-1', |
| 549 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 550 | }, { |
| 551 | lookaheadSeconds: 1, |
| 552 | sendSubtitleLogBatch: (entries) => |
| 553 | { |
| 554 | sentBatches.push(entries); |
| 555 | return Promise.resolve(); |
| 556 | }, |
| 557 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 558 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 559 | }), |
| 560 | }); |
| 561 | |
| 562 | await flushAsyncWork(window, 8); |
| 563 | controller.detach(); |
| 564 | await flushAsyncWork(window, 4); |
| 565 | |
| 566 | const flattened = sentBatches.flat(); |
| 567 | const prefetchEvents = flattened.filter((entry) => entry.requestKind === 'prefetch').map((entry) => entry.eventType); |
| 568 | expect(prefetchEvents).toEqual(expect.arrayContaining([ |
| 569 | 'subtitle_png_requested', |
| 570 | 'subtitle_png_sent', |
| 571 | 'subtitle_png_not_available', |
| 572 | ])); |
| 573 | |
| 574 | window.close(); |
| 575 | }); |
| 576 | |
| 577 | test('cache-only current mode avoids direct current subtitle fetch requests', async () => |
| 578 | { |
| 579 | const window = createWindow(); |
| 580 | const helper = loadSharedVideoPlayer(window); |
| 581 | const { player, videojs } = buildPlayerHarness(window); |
| 582 | const sentBatches = []; |
| 583 | |
| 584 | player.setCurrentTime(8); |
| 585 | window.fetch = jest.fn(async () => |
| 586 | { |
| 587 | return new Response(null, { |
| 588 | status: 204, |
| 589 | headers: { |
| 590 | 'X-Subtitle-Frame-Status': 'none', |
| 591 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 592 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 593 | }, |
| 594 | }); |
| 595 | }); |
| 596 | |
| 597 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 598 | sessionId: 'sess-1', |
| 599 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 600 | }, { |
| 601 | currentFetchEnabled: false, |
| 602 | lookaheadSeconds: 0, |
| 603 | sendSubtitleLogBatch: (entries) => |
| 604 | { |
| 605 | sentBatches.push(entries); |
| 606 | return Promise.resolve(); |
| 607 | }, |
| 608 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 609 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 610 | }), |
| 611 | }); |
| 612 | |
| 613 | await flushAsyncWork(window, 8); |
| 614 | controller.detach(); |
| 615 | await flushAsyncWork(window, 4); |
| 616 | |
| 617 | const flattened = sentBatches.flat(); |
| 618 | expect(flattened.length).toBeGreaterThan(0); |
| 619 | const requestKinds = new Set(flattened.map((entry) => entry.requestKind).filter(Boolean)); |
| 620 | expect(requestKinds.has('prefetch')).toBe(true); |
| 621 | expect(requestKinds.has('current')).toBe(false); |
| 622 | |
| 623 | window.close(); |
| 624 | }); |
| 625 | |
| 626 | test('cache-only mode performs initial warmup prefetch window on load', async () => |
| 627 | { |
| 628 | const window = createWindow(); |
| 629 | const helper = loadSharedVideoPlayer(window); |
| 630 | const { player, videojs } = buildPlayerHarness(window); |
| 631 | const requestedTimes = []; |
| 632 | |
| 633 | window.fetch = jest.fn(async (url) => |
| 634 | { |
| 635 | const parsed = new URL(url, 'http://localhost/'); |
| 636 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 637 | return new Response(null, { |
| 638 | status: 204, |
| 639 | headers: { |
| 640 | 'X-Subtitle-Frame-Status': 'none', |
| 641 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 642 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 643 | }, |
| 644 | }); |
| 645 | }); |
| 646 | |
| 647 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 648 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 649 | }, { |
| 650 | currentFetchEnabled: false, |
| 651 | lookaheadSeconds: 0, |
| 652 | initialPrefetchSeconds: 2, |
| 653 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 654 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 655 | }), |
| 656 | }); |
| 657 | |
| 658 | await flushAsyncWork(window, 12); |
| 659 | |
| 660 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2])); |
| 661 | |
| 662 | controller.detach(); |
| 663 | await flushAsyncWork(window, 2); |
| 664 | window.close(); |
| 665 | }); |
| 666 | |
| 667 | test('renders requested and received subtitle markers on the seek bar', async () => |
| 668 | { |
| 669 | const window = createWindow(); |
| 670 | const helper = loadSharedVideoPlayer(window); |
| 671 | const { player, videojs } = buildPlayerHarness(window); |
| 672 | |
| 673 | window.fetch = jest.fn(async () => |
| 674 | { |
| 675 | return new Response('png-data', { |
| 676 | status: 200, |
| 677 | headers: { |
| 678 | 'Content-Type': 'image/png', |
| 679 | 'X-Subtitle-Frame-Status': 'ok', |
| 680 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 681 | 'X-Subtitle-Response-Sent-At-Ms': '1012', |
| 682 | 'X-Subtitle-Frame-Start-Time': '2.5', |
| 683 | 'X-Subtitle-Frame-End-Time': '3.5', |
| 684 | 'X-Subtitle-Frame-Width': '320', |
| 685 | 'X-Subtitle-Frame-Height': '180', |
| 686 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 687 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 688 | }, |
| 689 | }); |
| 690 | }); |
| 691 | |
| 692 | player.duration(10); |
| 693 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 694 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 695 | }, { |
| 696 | lookaheadSeconds: 0, |
| 697 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 698 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 699 | }), |
| 700 | }); |
| 701 | |
| 702 | await flushAsyncWork(window, 8); |
| 703 | |
| 704 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 705 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 706 | expect(requestedMarker).toBeTruthy(); |
| 707 | expect(receivedMarker).toBeTruthy(); |
| 708 | expect(requestedMarker.style.display).toBe('block'); |
| 709 | expect(receivedMarker.style.display).toBe('block'); |
| 710 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 711 | expect(receivedMarker.style.left).toBe('25.0000%'); |
| 712 | expect(requestedMarker.textContent).toBe(']'); |
| 713 | expect(receivedMarker.textContent).toBe('['); |
| 714 | |
| 715 | controller.detach(); |
| 716 | await new Promise((resolve) => window.setTimeout(resolve, 300)); |
| 717 | await flushAsyncWork(window, 2); |
| 718 | |
| 719 | window.close(); |
| 720 | }); |
| 721 | |
| 722 | test('advances both seek markers when subtitle request returns 204 without frame timing headers', async () => |
| 723 | { |
| 724 | const window = createWindow(); |
| 725 | const helper = loadSharedVideoPlayer(window); |
| 726 | const { player, videojs } = buildPlayerHarness(window); |
| 727 | |
| 728 | player.duration(10); |
| 729 | player.setCurrentTime(4); |
| 730 | window.fetch = jest.fn(async () => |
| 731 | { |
| 732 | return new Response(null, { |
| 733 | status: 204, |
| 734 | headers: { |
| 735 | 'X-Subtitle-Frame-Status': 'none', |
| 736 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 737 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 738 | }, |
| 739 | }); |
| 740 | }); |
| 741 | |
| 742 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 743 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 744 | }, { |
| 745 | lookaheadSeconds: 0, |
| 746 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 747 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 748 | }), |
| 749 | }); |
| 750 | |
| 751 | await flushAsyncWork(window, 8); |
| 752 | |
| 753 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 754 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 755 | expect(requestedMarker).toBeTruthy(); |
| 756 | expect(receivedMarker).toBeTruthy(); |
| 757 | expect(requestedMarker.style.display).toBe('block'); |
| 758 | expect(receivedMarker.style.display).toBe('block'); |
| 759 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 760 | expect(receivedMarker.style.left).toBe('100.0000%'); |
| 761 | expect(requestedMarker.textContent).toBe(']'); |
| 762 | expect(receivedMarker.textContent).toBe('['); |
| 763 | |
| 764 | controller.detach(); |
| 765 | await flushAsyncWork(window, 2); |
| 766 | window.close(); |
| 767 | }); |
| 768 | |
| 769 | test('keeps existing marker positions while the next request is still in flight', async () => |
| 770 | { |
| 771 | const window = createWindow(); |
| 772 | const helper = loadSharedVideoPlayer(window); |
| 773 | const { player, videojs } = buildPlayerHarness(window); |
| 774 | let pendingResolve = null; |
| 775 | |
| 776 | player.duration(10); |
| 777 | window.fetch = jest.fn((url) => |
| 778 | { |
| 779 | const parsed = new URL(url, 'http://localhost/'); |
| 780 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 781 | |
| 782 | if (timeSeconds === 0) |
| 783 | { |
| 784 | return Promise.resolve(new Response('png-data', { |
| 785 | status: 200, |
| 786 | headers: { |
| 787 | 'Content-Type': 'image/png', |
| 788 | 'X-Subtitle-Frame-Status': 'ok', |
| 789 | 'X-Subtitle-Frame-Start-Time': '2', |
| 790 | 'X-Subtitle-Frame-End-Time': '3', |
| 791 | 'X-Subtitle-Frame-Width': '320', |
| 792 | 'X-Subtitle-Frame-Height': '180', |
| 793 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 794 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 795 | }, |
| 796 | })); |
| 797 | } |
| 798 | |
| 799 | return new Promise((resolve) => |
| 800 | { |
| 801 | pendingResolve = resolve; |
| 802 | }); |
| 803 | }); |
| 804 | |
| 805 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 806 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 807 | }, { |
| 808 | lookaheadSeconds: 0, |
| 809 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 810 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 811 | }), |
| 812 | }); |
| 813 | |
| 814 | await flushAsyncWork(window, 8); |
| 815 | |
| 816 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 817 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 818 | expect(requestedMarker).toBeTruthy(); |
| 819 | expect(receivedMarker).toBeTruthy(); |
| 820 | expect(requestedMarker.style.left).toBe('0.0000%'); |
| 821 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 822 | |
| 823 | player.setCurrentTime(1.5); |
| 824 | player.trigger('timeupdate'); |
| 825 | await flushAsyncWork(window, 2); |
| 826 | |
| 827 | expect(requestedMarker.style.left).toBe('0.0000%'); |
| 828 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 829 | |
| 830 | pendingResolve(new Response(null, { |
| 831 | status: 204, |
| 832 | headers: { |
| 833 | 'X-Subtitle-Frame-Status': 'none', |
| 834 | }, |
| 835 | })); |
| 836 | |
| 837 | await flushAsyncWork(window, 8); |
| 838 | expect(requestedMarker.style.display).toBe('block'); |
| 839 | expect(receivedMarker.style.display).toBe('block'); |
| 840 | expect(requestedMarker.style.left).toBe('15.0000%'); |
| 841 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 842 | |
| 843 | controller.detach(); |
| 844 | await flushAsyncWork(window, 2); |
| 845 | window.close(); |
| 846 | }); |
| 847 | |
| 848 | test('keeps marker positions forward-only when later responses resolve with earlier times', async () => |
| 849 | { |
| 850 | const window = createWindow(); |
| 851 | const helper = loadSharedVideoPlayer(window); |
| 852 | const { player, videojs } = buildPlayerHarness(window); |
| 853 | const requestedTimes = []; |
| 854 | |
| 855 | player.duration(12); |
| 856 | window.fetch = jest.fn(async (url) => |
| 857 | { |
| 858 | const parsed = new URL(url, 'http://localhost/'); |
| 859 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 860 | requestedTimes.push(timeSeconds); |
| 861 | |
| 862 | if (requestedTimes.length === 1) |
| 863 | { |
| 864 | return new Response('png-data', { |
| 865 | status: 200, |
| 866 | headers: { |
| 867 | 'Content-Type': 'image/png', |
| 868 | 'X-Subtitle-Frame-Status': 'ok', |
| 869 | 'X-Subtitle-Frame-Start-Time': '4', |
| 870 | 'X-Subtitle-Frame-End-Time': '5', |
| 871 | 'X-Subtitle-Frame-Width': '320', |
| 872 | 'X-Subtitle-Frame-Height': '180', |
| 873 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 874 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 875 | }, |
| 876 | }); |
| 877 | } |
| 878 | |
| 879 | return new Response('png-data', { |
| 880 | status: 200, |
| 881 | headers: { |
| 882 | 'Content-Type': 'image/png', |
| 883 | 'X-Subtitle-Frame-Status': 'ok', |
| 884 | 'X-Subtitle-Frame-Start-Time': '2', |
| 885 | 'X-Subtitle-Frame-End-Time': '3', |
| 886 | 'X-Subtitle-Frame-Width': '320', |
| 887 | 'X-Subtitle-Frame-Height': '180', |
| 888 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 889 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 890 | }, |
| 891 | }); |
| 892 | }); |
| 893 | |
| 894 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 895 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 896 | }, { |
| 897 | lookaheadSeconds: 0, |
| 898 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 899 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 900 | }), |
| 901 | }); |
| 902 | |
| 903 | await flushAsyncWork(window, 8); |
| 904 | |
| 905 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 906 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 907 | expect(requestedMarker).toBeTruthy(); |
| 908 | expect(receivedMarker).toBeTruthy(); |
| 909 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 910 | expect(receivedMarker.style.left).toBe('33.3333%'); |
| 911 | |
| 912 | player.setCurrentTime(1.5); |
| 913 | player.trigger('timeupdate'); |
| 914 | await flushAsyncWork(window, 8); |
| 915 | |
| 916 | // Second response reports an older start time, but markers stay monotonic. |
| 917 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 918 | expect(receivedMarker.style.left).toBe('33.3333%'); |
| 919 | |
| 920 | controller.detach(); |
| 921 | await flushAsyncWork(window, 2); |
| 922 | window.close(); |
| 923 | }); |
| 924 | |
| 925 | test('continues current subtitle refreshes after a force-seek aborts the in-flight request', async () => |
| 926 | { |
| 927 | const window = createWindow(); |
| 928 | const helper = loadSharedVideoPlayer(window); |
| 929 | const { player, videojs } = buildPlayerHarness(window); |
| 930 | const requestedTimes = []; |
| 931 | |
| 932 | window.fetch = jest.fn((url, init = {}) => |
| 933 | { |
| 934 | const parsed = new URL(url, 'http://localhost/'); |
| 935 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 936 | requestedTimes.push(timeSeconds); |
| 937 | |
| 938 | if (requestedTimes.length === 1) |
| 939 | { |
| 940 | return new Promise((resolve, reject) => |
| 941 | { |
| 942 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 943 | { |
| 944 | init.signal.addEventListener('abort', () => |
| 945 | { |
| 946 | reject(new window.DOMException('Aborted', 'AbortError')); |
| 947 | }, { once: true }); |
| 948 | } |
| 949 | }); |
| 950 | } |
| 951 | |
| 952 | return Promise.resolve(new Response(null, { |
| 953 | status: 204, |
| 954 | headers: { |
| 955 | 'X-Subtitle-Frame-Status': 'none', |
| 956 | }, |
| 957 | })); |
| 958 | }); |
| 959 | |
| 960 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 961 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 962 | }, { |
| 963 | lookaheadSeconds: 0, |
| 964 | currentRequestTimeoutMs: 300, |
| 965 | logToConsole: false, |
| 966 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 967 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 968 | }), |
| 969 | }); |
| 970 | |
| 971 | await flushAsyncWork(window, 4); |
| 972 | const afterInitialLoadCalls = window.fetch.mock.calls.length; |
| 973 | expect(afterInitialLoadCalls).toBeGreaterThan(0); |
| 974 | |
| 975 | player.setCurrentTime(5); |
| 976 | player.trigger('seeking'); |
| 977 | await flushAsyncWork(window, 20); |
| 978 | |
| 979 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 980 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 5])); |
| 981 | |
| 982 | controller.detach(); |
| 983 | await flushAsyncWork(window, 2); |
| 984 | window.close(); |
| 985 | }); |
| 986 | |
| 987 | test('recovers when a current subtitle request hangs and times out', async () => |
| 988 | { |
| 989 | const window = createWindow(); |
| 990 | const helper = loadSharedVideoPlayer(window); |
| 991 | const { player, videojs } = buildPlayerHarness(window); |
| 992 | const requestedTimes = []; |
| 993 | |
| 994 | window.fetch = jest.fn((url, init = {}) => |
| 995 | { |
| 996 | const parsed = new URL(url, 'http://localhost/'); |
| 997 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 998 | requestedTimes.push(timeSeconds); |
| 999 | |
| 1000 | if (requestedTimes.length === 1) |
| 1001 | { |
| 1002 | return new Promise((resolve, reject) => |
| 1003 | { |
| 1004 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 1005 | { |
| 1006 | init.signal.addEventListener('abort', () => |
| 1007 | { |
| 1008 | reject(new window.DOMException('Aborted', 'AbortError')); |
| 1009 | }, { once: true }); |
| 1010 | } |
| 1011 | }); |
| 1012 | } |
| 1013 | |
| 1014 | return Promise.resolve(new Response(null, { |
| 1015 | status: 204, |
| 1016 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 1017 | })); |
| 1018 | }); |
| 1019 | |
| 1020 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1021 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1022 | }, { |
| 1023 | lookaheadSeconds: 0, |
| 1024 | currentRequestTimeoutMs: 250, |
| 1025 | logToConsole: false, |
| 1026 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1027 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1028 | }), |
| 1029 | }); |
| 1030 | |
| 1031 | await flushAsyncWork(window, 3); |
| 1032 | |
| 1033 | player.setCurrentTime(1.5); |
| 1034 | player.trigger('timeupdate'); |
| 1035 | |
| 1036 | await new Promise((resolve) => window.setTimeout(resolve, 320)); |
| 1037 | player.trigger('timeupdate'); |
| 1038 | await new Promise((resolve) => window.setTimeout(resolve, 220)); |
| 1039 | await flushAsyncWork(window, 20); |
| 1040 | |
| 1041 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 1042 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 1.5])); |
| 1043 | |
| 1044 | controller.detach(); |
| 1045 | await new Promise((resolve) => window.setTimeout(resolve, 200)); |
| 1046 | await flushAsyncWork(window, 20); |
| 1047 | window.close(); |
| 1048 | }); |
| 1049 | |
| 1050 | test('reuses cached subtitle frames after rewinding when server no longer has older frames', async () => |
| 1051 | { |
| 1052 | const window = createWindow(); |
| 1053 | const helper = loadSharedVideoPlayer(window); |
| 1054 | const { player, videojs } = buildPlayerHarness(window); |
| 1055 | |
| 1056 | player.duration(10); |
| 1057 | player.setCurrentTime(2); |
| 1058 | window.fetch = jest.fn(async (url) => |
| 1059 | { |
| 1060 | const parsed = new URL(url, 'http://localhost/'); |
| 1061 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1062 | |
| 1063 | if (Math.abs(timeSeconds - 2) < 1e-6) |
| 1064 | { |
| 1065 | return new Response('png-data', { |
| 1066 | status: 200, |
| 1067 | headers: { |
| 1068 | 'Content-Type': 'image/png', |
| 1069 | 'X-Subtitle-Frame-Status': 'ok', |
| 1070 | 'X-Subtitle-Frame-Start-Time': '2', |
| 1071 | 'X-Subtitle-Frame-End-Time': '3', |
| 1072 | 'X-Subtitle-Frame-Width': '320', |
| 1073 | 'X-Subtitle-Frame-Height': '180', |
| 1074 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1075 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1076 | }, |
| 1077 | }); |
| 1078 | } |
| 1079 | |
| 1080 | return new Response(null, { |
| 1081 | status: 204, |
| 1082 | headers: { |
| 1083 | 'X-Subtitle-Frame-Status': 'none', |
| 1084 | }, |
| 1085 | }); |
| 1086 | }); |
| 1087 | |
| 1088 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1089 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1090 | }, { |
| 1091 | lookaheadSeconds: 0, |
| 1092 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1093 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1094 | }), |
| 1095 | }); |
| 1096 | |
| 1097 | await flushAsyncWork(window, 8); |
| 1098 | const afterInitialLoadCalls = window.fetch.mock.calls.length; |
| 1099 | expect(afterInitialLoadCalls).toBeGreaterThan(0); |
| 1100 | |
| 1101 | player.setCurrentTime(4); |
| 1102 | player.trigger('timeupdate'); |
| 1103 | await flushAsyncWork(window, 8); |
| 1104 | const afterForwardSeekCalls = window.fetch.mock.calls.length; |
| 1105 | expect(afterForwardSeekCalls).toBeGreaterThanOrEqual(afterInitialLoadCalls); |
| 1106 | |
| 1107 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 1108 | expect(overlay).toBeTruthy(); |
| 1109 | expect(overlay.style.display).toBe('none'); |
| 1110 | |
| 1111 | player.setCurrentTime(2.5); |
| 1112 | player.trigger('timeupdate'); |
| 1113 | await flushAsyncWork(window, 8); |
| 1114 | |
| 1115 | expect(window.fetch.mock.calls.length).toBe(afterForwardSeekCalls); |
| 1116 | expect(overlay.style.display).toBe('block'); |
| 1117 | |
| 1118 | controller.detach(); |
| 1119 | await flushAsyncWork(window, 2); |
| 1120 | window.close(); |
| 1121 | }); |
| 1122 | |
| 1123 | test('registers stream cache service worker once when supported', async () => |
| 1124 | { |
| 1125 | const window = createWindow(); |
| 1126 | const helper = loadSharedVideoPlayer(window); |
| 1127 | const register = jest.fn(async () => ({})); |
| 1128 | window.navigator.serviceWorker = { register }; |
| 1129 | |
| 1130 | const first = await helper.enableVideoStreamClientCache(); |
| 1131 | const second = await helper.enableVideoStreamClientCache(); |
| 1132 | |
| 1133 | expect(first).toBe(true); |
| 1134 | expect(second).toBe(true); |
| 1135 | expect(register).toHaveBeenCalledTimes(1); |
| 1136 | expect(register).toHaveBeenCalledWith('/stream-cache-sw.js', { |
| 1137 | scope: '/', |
| 1138 | updateViaCache: 'none', |
| 1139 | }); |
| 1140 | |
| 1141 | window.close(); |
| 1142 | }); |
| 1143 | |
| 1144 | test('stream-ahead prefetch follows playlists and fetches segment assets', async () => |
| 1145 | { |
| 1146 | const window = createWindow(); |
| 1147 | const helper = loadSharedVideoPlayer(window); |
| 1148 | const requestedUrls = []; |
| 1149 | |
| 1150 | window.fetch = jest.fn(async (input) => |
| 1151 | { |
| 1152 | const url = String(input && input.url ? input.url : input); |
| 1153 | requestedUrls.push(url); |
| 1154 | |
| 1155 | if (url === 'http://localhost/master.m3u8') |
| 1156 | { |
| 1157 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1158 | status: 200, |
| 1159 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1160 | }); |
| 1161 | } |
| 1162 | |
| 1163 | if (url === 'http://localhost/stream_0.m3u8') |
| 1164 | { |
| 1165 | return new Response('#EXTM3U\n#EXTINF:4,\nseg0.ts\n#EXTINF:4,\nseg1.ts\n#EXT-X-ENDLIST\n', { |
| 1166 | status: 200, |
| 1167 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1168 | }); |
| 1169 | } |
| 1170 | |
| 1171 | if (url === 'http://localhost/seg0.ts' || url === 'http://localhost/seg1.ts') |
| 1172 | { |
| 1173 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1174 | status: 200, |
| 1175 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1176 | }); |
| 1177 | } |
| 1178 | |
| 1179 | return new Response(null, { status: 404 }); |
| 1180 | }); |
| 1181 | |
| 1182 | const controller = helper.bindStreamAheadPrefetch({ |
| 1183 | playlistUrl: 'http://localhost/master.m3u8', |
| 1184 | pollIntervalMs: 0, |
| 1185 | credentials: 'same-origin', |
| 1186 | }); |
| 1187 | |
| 1188 | expect(controller).toBeTruthy(); |
| 1189 | await controller.done(); |
| 1190 | |
| 1191 | expect(requestedUrls).toEqual(expect.arrayContaining([ |
| 1192 | 'http://localhost/master.m3u8', |
| 1193 | 'http://localhost/stream_0.m3u8', |
| 1194 | 'http://localhost/seg0.ts', |
| 1195 | 'http://localhost/seg1.ts', |
| 1196 | ])); |
| 1197 | |
| 1198 | controller.stop(); |
| 1199 | window.close(); |
| 1200 | }); |
| 1201 | |
| 1202 | test('stream-ahead prefetch reports cached-through progress for fetched segments', async () => |
| 1203 | { |
| 1204 | const window = createWindow(); |
| 1205 | const helper = loadSharedVideoPlayer(window); |
| 1206 | const progressSamples = []; |
| 1207 | |
| 1208 | window.fetch = jest.fn(async (input) => |
| 1209 | { |
| 1210 | const url = String(input && input.url ? input.url : input); |
| 1211 | |
| 1212 | if (url === 'http://localhost/master.m3u8') |
| 1213 | { |
| 1214 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1215 | status: 200, |
| 1216 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1217 | }); |
| 1218 | } |
| 1219 | |
| 1220 | if (url === 'http://localhost/stream_0.m3u8') |
| 1221 | { |
| 1222 | return new Response('#EXTM3U\n#EXTINF:4,\nseg0.ts\n#EXTINF:4,\nseg1.ts\n#EXT-X-ENDLIST\n', { |
| 1223 | status: 200, |
| 1224 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1225 | }); |
| 1226 | } |
| 1227 | |
| 1228 | if (url === 'http://localhost/seg0.ts' || url === 'http://localhost/seg1.ts') |
| 1229 | { |
| 1230 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1231 | status: 200, |
| 1232 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1233 | }); |
| 1234 | } |
| 1235 | |
| 1236 | return new Response(null, { status: 404 }); |
| 1237 | }); |
| 1238 | |
| 1239 | const controller = helper.bindStreamAheadPrefetch({ |
| 1240 | playlistUrl: 'http://localhost/master.m3u8', |
| 1241 | pollIntervalMs: 0, |
| 1242 | credentials: 'same-origin', |
| 1243 | }); |
| 1244 | |
| 1245 | expect(controller).toBeTruthy(); |
| 1246 | const unsubscribe = controller.subscribe((status) => |
| 1247 | { |
| 1248 | progressSamples.push(status && status.cachedThroughSeconds); |
| 1249 | }); |
| 1250 | |
| 1251 | await controller.done(); |
| 1252 | unsubscribe(); |
| 1253 | |
| 1254 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 1255 | |
| 1256 | controller.stop(); |
| 1257 | window.close(); |
| 1258 | }); |
| 1259 | |
| 1260 | test('stream-ahead prefetch infers cached-through progress from segment filenames', async () => |
| 1261 | { |
| 1262 | const window = createWindow(); |
| 1263 | const helper = loadSharedVideoPlayer(window); |
| 1264 | const progressSamples = []; |
| 1265 | |
| 1266 | window.fetch = jest.fn(async (input) => |
| 1267 | { |
| 1268 | const url = String(input && input.url ? input.url : input); |
| 1269 | |
| 1270 | if (url === 'http://localhost/master.m3u8') |
| 1271 | { |
| 1272 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1273 | status: 200, |
| 1274 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1275 | }); |
| 1276 | } |
| 1277 | |
| 1278 | if (url === 'http://localhost/stream_0.m3u8') |
| 1279 | { |
| 1280 | return new Response('#EXTM3U\nstream_0_segment_000000.ts\nstream_0_segment_000001.ts\n#EXT-X-ENDLIST\n', { |
| 1281 | status: 200, |
| 1282 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1283 | }); |
| 1284 | } |
| 1285 | |
| 1286 | if (url === 'http://localhost/stream_0_segment_000000.ts' || url === 'http://localhost/stream_0_segment_000001.ts') |
| 1287 | { |
| 1288 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1289 | status: 200, |
| 1290 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1291 | }); |
| 1292 | } |
| 1293 | |
| 1294 | return new Response(null, { status: 404 }); |
| 1295 | }); |
| 1296 | |
| 1297 | const controller = helper.bindStreamAheadPrefetch({ |
| 1298 | playlistUrl: 'http://localhost/master.m3u8', |
| 1299 | pollIntervalMs: 0, |
| 1300 | credentials: 'same-origin', |
| 1301 | }); |
| 1302 | |
| 1303 | expect(controller).toBeTruthy(); |
| 1304 | const unsubscribe = controller.subscribe((status) => |
| 1305 | { |
| 1306 | progressSamples.push(status && status.cachedThroughSeconds); |
| 1307 | }); |
| 1308 | |
| 1309 | await controller.done(); |
| 1310 | unsubscribe(); |
| 1311 | |
| 1312 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 1313 | |
| 1314 | controller.stop(); |
| 1315 | window.close(); |
| 1316 | }); |
| 1317 | |
| 1318 | test('stream-ahead prefetch infers cached-through progress for m4s segment filenames', async () => |
| 1319 | { |
| 1320 | const window = createWindow(); |
| 1321 | const helper = loadSharedVideoPlayer(window); |
| 1322 | const progressSamples = []; |
| 1323 | |
| 1324 | window.fetch = jest.fn(async (input) => |
| 1325 | { |
| 1326 | const url = String(input && input.url ? input.url : input); |
| 1327 | |
| 1328 | if (url === 'http://localhost/master.m3u8') |
| 1329 | { |
| 1330 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1331 | status: 200, |
| 1332 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1333 | }); |
| 1334 | } |
| 1335 | |
| 1336 | if (url === 'http://localhost/stream_0.m3u8') |
| 1337 | { |
| 1338 | return new Response('#EXTM3U\nstream_0_segment_000000.m4s\nstream_0_segment_000001.m4s\n#EXT-X-ENDLIST\n', { |
| 1339 | status: 200, |
| 1340 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1341 | }); |
| 1342 | } |
| 1343 | |
| 1344 | if (url === 'http://localhost/stream_0_segment_000000.m4s' || url === 'http://localhost/stream_0_segment_000001.m4s') |
| 1345 | { |
| 1346 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1347 | status: 200, |
| 1348 | headers: { 'Content-Type': 'video/mp4' }, |
| 1349 | }); |
| 1350 | } |
| 1351 | |
| 1352 | return new Response(null, { status: 404 }); |
| 1353 | }); |
| 1354 | |
| 1355 | const controller = helper.bindStreamAheadPrefetch({ |
| 1356 | playlistUrl: 'http://localhost/master.m3u8', |
| 1357 | pollIntervalMs: 0, |
| 1358 | credentials: 'same-origin', |
| 1359 | }); |
| 1360 | |
| 1361 | expect(controller).toBeTruthy(); |
| 1362 | const unsubscribe = controller.subscribe((status) => |
| 1363 | { |
| 1364 | progressSamples.push(status && status.cachedThroughSeconds); |
| 1365 | }); |
| 1366 | |
| 1367 | await controller.done(); |
| 1368 | unsubscribe(); |
| 1369 | |
| 1370 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 1371 | |
| 1372 | controller.stop(); |
| 1373 | window.close(); |
| 1374 | }); |
| 1375 | |
| 1376 | test('stream-ahead prefetch reports ratio progress from fetched sections when timing is unknown', async () => |
| 1377 | { |
| 1378 | const window = createWindow(); |
| 1379 | const helper = loadSharedVideoPlayer(window); |
| 1380 | const statusSamples = []; |
| 1381 | |
| 1382 | window.fetch = jest.fn(async (input) => |
| 1383 | { |
| 1384 | const url = String(input && input.url ? input.url : input); |
| 1385 | |
| 1386 | if (url === 'http://localhost/master.m3u8') |
| 1387 | { |
| 1388 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1389 | status: 200, |
| 1390 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1391 | }); |
| 1392 | } |
| 1393 | |
| 1394 | if (url === 'http://localhost/stream_0.m3u8') |
| 1395 | { |
| 1396 | return new Response('#EXTM3U\nchunk-alpha.bin\nchunk-beta.bin\n#EXT-X-ENDLIST\n', { |
| 1397 | status: 200, |
| 1398 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1399 | }); |
| 1400 | } |
| 1401 | |
| 1402 | if (url === 'http://localhost/chunk-alpha.bin' || url === 'http://localhost/chunk-beta.bin') |
| 1403 | { |
| 1404 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1405 | status: 200, |
| 1406 | headers: { 'Content-Type': 'application/octet-stream' }, |
| 1407 | }); |
| 1408 | } |
| 1409 | |
| 1410 | return new Response(null, { status: 404 }); |
| 1411 | }); |
| 1412 | |
| 1413 | const controller = helper.bindStreamAheadPrefetch({ |
| 1414 | playlistUrl: 'http://localhost/master.m3u8', |
| 1415 | pollIntervalMs: 0, |
| 1416 | credentials: 'same-origin', |
| 1417 | }); |
| 1418 | |
| 1419 | expect(controller).toBeTruthy(); |
| 1420 | const unsubscribe = controller.subscribe((status) => |
| 1421 | { |
| 1422 | statusSamples.push(status || {}); |
| 1423 | }); |
| 1424 | |
| 1425 | await controller.done(); |
| 1426 | unsubscribe(); |
| 1427 | |
| 1428 | const finalStatus = statusSamples[statusSamples.length - 1] || {}; |
| 1429 | expect(finalStatus.totalSegmentCount).toBe(2); |
| 1430 | expect(finalStatus.fetchedSegmentCount).toBe(2); |
| 1431 | expect(finalStatus.cachedThroughRatio).toBe(1); |
| 1432 | |
| 1433 | controller.stop(); |
| 1434 | window.close(); |
| 1435 | }); |
| 1436 | |
| 1437 | test('clears cached stream assets for a finished playlist session', async () => |
| 1438 | { |
| 1439 | const window = createWindow(); |
| 1440 | const helper = loadSharedVideoPlayer(window); |
| 1441 | const requests = [ |
| 1442 | new window.Request('http://localhost/api/files/video-stream/sess-a/master.m3u8'), |
| 1443 | new window.Request('http://localhost/api/files/video-stream/sess-a/stream_0_segment_000001.ts'), |
| 1444 | new window.Request('http://localhost/api/files/video-stream/sess-b/master.m3u8'), |
| 1445 | ]; |
| 1446 | const deleteCalls = []; |
| 1447 | const cache = { |
| 1448 | keys: jest.fn(async () => requests), |
| 1449 | delete: jest.fn(async (request) => |
| 1450 | { |
| 1451 | deleteCalls.push(request.url); |
| 1452 | return true; |
| 1453 | }), |
| 1454 | }; |
| 1455 | window.caches = { |
| 1456 | open: jest.fn(async () => cache), |
| 1457 | }; |
| 1458 | |
| 1459 | await helper.clearVideoStreamClientCache('http://localhost/api/files/video-stream/sess-a/master.m3u8'); |
| 1460 | |
| 1461 | expect(window.caches.open).toHaveBeenCalledWith('filemanager-stream-cache-v1'); |
| 1462 | expect(cache.keys).toHaveBeenCalledTimes(1); |
| 1463 | expect(deleteCalls).toEqual(expect.arrayContaining([ |
| 1464 | 'http://localhost/api/files/video-stream/sess-a/master.m3u8', |
| 1465 | 'http://localhost/api/files/video-stream/sess-a/stream_0_segment_000001.ts', |
| 1466 | ])); |
| 1467 | expect(deleteCalls).not.toContain('http://localhost/api/files/video-stream/sess-b/master.m3u8'); |
| 1468 | |
| 1469 | window.close(); |
| 1470 | }); |
| 1471 | }); |