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