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