| 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('range prefetch clamps the tail window to the known video duration', async () => |
| 308 | { |
| 309 | const window = createWindow(); |
| 310 | const helper = loadSharedVideoPlayer(window); |
| 311 | const { player, videojs } = buildPlayerHarness(window); |
| 312 | const requestedRanges = []; |
| 313 | |
| 314 | player.duration(10); |
| 315 | player.setCurrentTime(8.5); |
| 316 | |
| 317 | window.fetch = jest.fn(async (url) => |
| 318 | { |
| 319 | const parsed = new URL(url, 'http://localhost/'); |
| 320 | requestedRanges.push({ |
| 321 | start: Number(parsed.searchParams.get('startTimeSeconds')), |
| 322 | end: Number(parsed.searchParams.get('endTimeSeconds')), |
| 323 | }); |
| 324 | return new Response(JSON.stringify({ status: 'ok', frames: [] }), { |
| 325 | status: 200, |
| 326 | headers: { 'Content-Type': 'application/json' }, |
| 327 | }); |
| 328 | }); |
| 329 | |
| 330 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 331 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 332 | }, { |
| 333 | lookaheadSeconds: 0, |
| 334 | initialPrefetchSeconds: 0, |
| 335 | currentFetchEnabled: false, |
| 336 | cacheMissFallbackEnabled: false, |
| 337 | prefetchAdaptiveStepEnabled: true, |
| 338 | prefetchStepSecondsMin: 3, |
| 339 | prefetchStepSecondsMax: 3, |
| 340 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 341 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 342 | }), |
| 343 | buildPrefetchRequest: ({ currentTime, endTime, streamIndex }) => ({ |
| 344 | url: `/subtitle-range?streamIndex=${streamIndex}&startTimeSeconds=${currentTime}&endTimeSeconds=${endTime}`, |
| 345 | }), |
| 346 | }); |
| 347 | |
| 348 | player.trigger('timeupdate'); |
| 349 | await flushAsyncWork(window, 10); |
| 350 | |
| 351 | expect(requestedRanges.length).toBeGreaterThan(0); |
| 352 | expect(requestedRanges[0]).toEqual({ start: 8.5, end: 10 }); |
| 353 | expect(requestedRanges.every((range) => range.end <= 10)).toBe(true); |
| 354 | |
| 355 | window.close(); |
| 356 | }); |
| 357 | |
| 358 | test('prefetch respects configured larger prefetch step size', async () => |
| 359 | { |
| 360 | const window = createWindow(); |
| 361 | const helper = loadSharedVideoPlayer(window); |
| 362 | const { player, videojs } = buildPlayerHarness(window); |
| 363 | const requestedTimes = []; |
| 364 | |
| 365 | player.duration(6); |
| 366 | |
| 367 | window.fetch = jest.fn(async (url) => |
| 368 | { |
| 369 | const parsed = new URL(url, 'http://localhost/'); |
| 370 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 371 | return new Response(null, { |
| 372 | status: 204, |
| 373 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 374 | }); |
| 375 | }); |
| 376 | |
| 377 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 378 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 379 | }, { |
| 380 | lookaheadSeconds: 0, |
| 381 | initialPrefetchSeconds: 0, |
| 382 | prefetchStepSeconds: 2, |
| 383 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 384 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 385 | }), |
| 386 | }); |
| 387 | |
| 388 | await flushAsyncWork(window, 14); |
| 389 | |
| 390 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 2, 4, 6])); |
| 391 | |
| 392 | window.close(); |
| 393 | }); |
| 394 | |
| 395 | test('adaptive prefetch step increases stride when cache lead grows', async () => |
| 396 | { |
| 397 | const window = createWindow(); |
| 398 | const helper = loadSharedVideoPlayer(window); |
| 399 | const { player, videojs } = buildPlayerHarness(window); |
| 400 | const requestedTimes = []; |
| 401 | |
| 402 | player.duration(40); |
| 403 | |
| 404 | window.fetch = jest.fn(async (url) => |
| 405 | { |
| 406 | const parsed = new URL(url, 'http://localhost/'); |
| 407 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 408 | return new Response(null, { |
| 409 | status: 204, |
| 410 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 411 | }); |
| 412 | }); |
| 413 | |
| 414 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 415 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 416 | }, { |
| 417 | lookaheadSeconds: 20, |
| 418 | initialPrefetchSeconds: 0, |
| 419 | prefetchAdaptiveStepEnabled: true, |
| 420 | prefetchStepSecondsMin: 1, |
| 421 | prefetchStepSecondsMax: 5, |
| 422 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 423 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 424 | }), |
| 425 | }); |
| 426 | |
| 427 | await flushAsyncWork(window, 20); |
| 428 | |
| 429 | const strideSamples = []; |
| 430 | for (let i = 1; i < requestedTimes.length; i++) |
| 431 | { |
| 432 | const stride = requestedTimes[i] - requestedTimes[i - 1]; |
| 433 | if (Number.isFinite(stride) && stride > 0) strideSamples.push(Number(stride.toFixed(3))); |
| 434 | } |
| 435 | |
| 436 | expect(strideSamples.some((stride) => stride > 1.25)).toBe(true); |
| 437 | |
| 438 | window.close(); |
| 439 | }); |
| 440 | |
| 441 | test('prefetch can cache subtitle frames from a range request and render immediately', async () => |
| 442 | { |
| 443 | const window = createWindow(); |
| 444 | const helper = loadSharedVideoPlayer(window); |
| 445 | const { player, videojs } = buildPlayerHarness(window); |
| 446 | const requestedUrls = []; |
| 447 | |
| 448 | player.duration(12); |
| 449 | player.setCurrentTime(2); |
| 450 | |
| 451 | window.fetch = jest.fn(async (url) => |
| 452 | { |
| 453 | const parsed = new URL(url, 'http://localhost/'); |
| 454 | requestedUrls.push(parsed.pathname + parsed.search); |
| 455 | |
| 456 | if (parsed.pathname === '/subtitle-range') |
| 457 | { |
| 458 | return new Response(JSON.stringify({ |
| 459 | status: 'ok', |
| 460 | startTimeSeconds: Number(parsed.searchParams.get('startTimeSeconds')), |
| 461 | endTimeSeconds: Number(parsed.searchParams.get('endTimeSeconds')), |
| 462 | frames: [ |
| 463 | { |
| 464 | left: 0, |
| 465 | top: 0, |
| 466 | width: 320, |
| 467 | height: 180, |
| 468 | canvasWidth: 640, |
| 469 | canvasHeight: 360, |
| 470 | startTimeSeconds: 2, |
| 471 | endTimeSeconds: 4, |
| 472 | contentType: 'image/png', |
| 473 | frameBase64: Buffer.from('png-data').toString('base64'), |
| 474 | }, |
| 475 | ], |
| 476 | }), { |
| 477 | status: 200, |
| 478 | headers: { 'Content-Type': 'application/json' }, |
| 479 | }); |
| 480 | } |
| 481 | |
| 482 | return new Response(null, { |
| 483 | status: 204, |
| 484 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 485 | }); |
| 486 | }); |
| 487 | |
| 488 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 489 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 490 | }, { |
| 491 | currentFetchEnabled: false, |
| 492 | cacheMissFallbackEnabled: false, |
| 493 | lookaheadSeconds: 0, |
| 494 | initialPrefetchSeconds: 0, |
| 495 | buildPrefetchRequest: ({ currentTime, endTime, streamIndex }) => ({ |
| 496 | url: `/subtitle-range?streamIndex=${streamIndex}&startTimeSeconds=${currentTime}&endTimeSeconds=${endTime}`, |
| 497 | }), |
| 498 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 499 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 500 | }), |
| 501 | }); |
| 502 | |
| 503 | await flushAsyncWork(window, 10); |
| 504 | |
| 505 | expect(requestedUrls.some((url) => url.startsWith('/subtitle-range'))).toBe(true); |
| 506 | expect(requestedUrls.some((url) => url.startsWith('/subtitle-frame'))).toBe(false); |
| 507 | |
| 508 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 509 | expect(overlay).toBeTruthy(); |
| 510 | expect(overlay.style.display).toBe('block'); |
| 511 | |
| 512 | window.close(); |
| 513 | }); |
| 514 | |
| 515 | test('prefetch uses furthest buffered end when player.bufferedEnd under-reports', async () => |
| 516 | { |
| 517 | const window = createWindow(); |
| 518 | const helper = loadSharedVideoPlayer(window); |
| 519 | const { player, videojs } = buildPlayerHarness(window); |
| 520 | const requestedTimes = []; |
| 521 | |
| 522 | player.setBufferedEnd(0); |
| 523 | player.tech = () => ({ |
| 524 | el: () => ({ |
| 525 | buffered: { |
| 526 | length: 1, |
| 527 | end: () => 6, |
| 528 | }, |
| 529 | }), |
| 530 | }); |
| 531 | |
| 532 | window.fetch = jest.fn(async (url) => |
| 533 | { |
| 534 | const parsed = new URL(url, 'http://localhost/'); |
| 535 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 536 | return new Response(null, { |
| 537 | status: 204, |
| 538 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 539 | }); |
| 540 | }); |
| 541 | |
| 542 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 543 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 544 | }, { |
| 545 | lookaheadSeconds: 0, |
| 546 | initialPrefetchSeconds: 0, |
| 547 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 548 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 549 | }), |
| 550 | }); |
| 551 | |
| 552 | await flushAsyncWork(window, 16); |
| 553 | |
| 554 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6])); |
| 555 | |
| 556 | window.close(); |
| 557 | }); |
| 558 | |
| 559 | test('prefetch frontier jumps to current playback time instead of replaying stale lag', async () => |
| 560 | { |
| 561 | const window = createWindow(); |
| 562 | const helper = loadSharedVideoPlayer(window); |
| 563 | const { player, videojs } = buildPlayerHarness(window); |
| 564 | const requestedTimes = []; |
| 565 | |
| 566 | player.duration(60); |
| 567 | player.setBufferedEnd(2); |
| 568 | |
| 569 | window.fetch = jest.fn(async (url) => |
| 570 | { |
| 571 | const parsed = new URL(url, 'http://localhost/'); |
| 572 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 573 | requestedTimes.push(timeSeconds); |
| 574 | return new Response(null, { |
| 575 | status: 204, |
| 576 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 577 | }); |
| 578 | }); |
| 579 | |
| 580 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 581 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 582 | }, { |
| 583 | lookaheadSeconds: 0, |
| 584 | initialPrefetchSeconds: 0, |
| 585 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 586 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 587 | }), |
| 588 | }); |
| 589 | |
| 590 | await flushAsyncWork(window, 8); |
| 591 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5])); |
| 592 | |
| 593 | requestedTimes.length = 0; |
| 594 | player.setCurrentTime(20); |
| 595 | player.setBufferedEnd(40); |
| 596 | player.trigger('timeupdate'); |
| 597 | await flushAsyncWork(window, 8); |
| 598 | |
| 599 | expect(requestedTimes).toContain(20); |
| 600 | expect(requestedTimes.some((value) => value < 20)).toBe(false); |
| 601 | const prefetchAfterJump = requestedTimes.filter((value) => value > 20); |
| 602 | if (prefetchAfterJump.length) |
| 603 | { |
| 604 | expect(Math.min(...prefetchAfterJump)).toBeGreaterThanOrEqual(20.5); |
| 605 | } |
| 606 | |
| 607 | window.close(); |
| 608 | }); |
| 609 | |
| 610 | test('does not restart current subtitle request while one is in flight', async () => |
| 611 | { |
| 612 | const window = createWindow(); |
| 613 | const helper = loadSharedVideoPlayer(window); |
| 614 | const { player, videojs } = buildPlayerHarness(window); |
| 615 | const pendingResolvers = []; |
| 616 | const requestTimes = []; |
| 617 | |
| 618 | window.fetch = jest.fn((url, init = {}) => |
| 619 | { |
| 620 | const parsed = new URL(url, 'http://localhost/'); |
| 621 | requestTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 622 | |
| 623 | if (pendingResolvers.length === 0) |
| 624 | { |
| 625 | return new Promise((resolve) => |
| 626 | { |
| 627 | pendingResolvers.push(() => resolve(new Response(null, { |
| 628 | status: 204, |
| 629 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 630 | }))); |
| 631 | }); |
| 632 | } |
| 633 | |
| 634 | expect(init.signal && init.signal.aborted).toBe(false); |
| 635 | return Promise.resolve(new Response(null, { |
| 636 | status: 204, |
| 637 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 638 | })); |
| 639 | }); |
| 640 | |
| 641 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 642 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 643 | }, { |
| 644 | lookaheadSeconds: 0, |
| 645 | initialPrefetchSeconds: 0, |
| 646 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 647 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 648 | }), |
| 649 | }); |
| 650 | |
| 651 | await flushAsyncWork(window, 3); |
| 652 | expect(window.fetch).toHaveBeenCalledTimes(1); |
| 653 | |
| 654 | player.setCurrentTime(1.5); |
| 655 | player.trigger('timeupdate'); |
| 656 | player.setCurrentTime(2.25); |
| 657 | player.trigger('timeupdate'); |
| 658 | await flushAsyncWork(window, 2); |
| 659 | |
| 660 | expect(window.fetch).toHaveBeenCalledTimes(1); |
| 661 | |
| 662 | pendingResolvers[0](); |
| 663 | await flushAsyncWork(window, 5); |
| 664 | |
| 665 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 666 | expect(requestTimes).toEqual(expect.arrayContaining([0, 2.25])); |
| 667 | |
| 668 | window.close(); |
| 669 | }); |
| 670 | |
| 671 | test('renders a prefetched frame for the current paused time without waiting for another timeupdate', async () => |
| 672 | { |
| 673 | const window = createWindow(); |
| 674 | const helper = loadSharedVideoPlayer(window); |
| 675 | const { player, videojs } = buildPlayerHarness(window); |
| 676 | |
| 677 | player.duration(120); |
| 678 | player.setCurrentTime(118.95); |
| 679 | |
| 680 | window.fetch = jest.fn(async (url) => |
| 681 | { |
| 682 | const parsed = new URL(url, 'http://localhost/'); |
| 683 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 684 | const headers = { |
| 685 | 'Content-Type': 'image/png', |
| 686 | }; |
| 687 | |
| 688 | if (Math.abs(timeSeconds - 118.95) < 0.001) |
| 689 | { |
| 690 | headers['X-Subtitle-Frame-Start-Time'] = '118.90'; |
| 691 | headers['X-Subtitle-Frame-End-Time'] = '119.00'; |
| 692 | headers['X-Subtitle-Frame-Width'] = '640'; |
| 693 | headers['X-Subtitle-Frame-Height'] = '80'; |
| 694 | headers['X-Subtitle-Frame-Canvas-Width'] = '1920'; |
| 695 | headers['X-Subtitle-Frame-Canvas-Height'] = '1080'; |
| 696 | return new Response(new Uint8Array([137, 80, 78, 71]), { status: 200, headers }); |
| 697 | } |
| 698 | |
| 699 | return new Response(null, { |
| 700 | status: 204, |
| 701 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 702 | }); |
| 703 | }); |
| 704 | |
| 705 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 706 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 707 | }, { |
| 708 | currentFetchEnabled: false, |
| 709 | cacheMissFallbackEnabled: false, |
| 710 | lookaheadSeconds: 0, |
| 711 | initialPrefetchSeconds: 0, |
| 712 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 713 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 714 | }), |
| 715 | }); |
| 716 | |
| 717 | await flushAsyncWork(window, 10); |
| 718 | |
| 719 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 720 | const overlayImg = overlay && overlay.querySelector('img'); |
| 721 | expect(overlay).toBeTruthy(); |
| 722 | expect(overlayImg).toBeTruthy(); |
| 723 | expect(overlay.style.display).toBe('block'); |
| 724 | expect(overlayImg.getAttribute('src')).toBe('blob:subtitle'); |
| 725 | |
| 726 | window.close(); |
| 727 | }); |
| 728 | |
| 729 | test('syncs caption visibility from cache between sparse timeupdate events', async () => |
| 730 | { |
| 731 | const window = createWindow(); |
| 732 | const helper = loadSharedVideoPlayer(window); |
| 733 | const { player, videojs } = buildPlayerHarness(window); |
| 734 | |
| 735 | // JSDOM raf cadence can be inconsistent; force timeout fallback for stable checks. |
| 736 | window.requestAnimationFrame = undefined; |
| 737 | window.cancelAnimationFrame = undefined; |
| 738 | |
| 739 | player.duration(20); |
| 740 | |
| 741 | window.fetch = jest.fn(async () => |
| 742 | { |
| 743 | return new Response(new Uint8Array([137, 80, 78, 71]), { |
| 744 | status: 200, |
| 745 | headers: { |
| 746 | 'Content-Type': 'image/png', |
| 747 | 'X-Subtitle-Frame-Start-Time': '1', |
| 748 | 'X-Subtitle-Frame-End-Time': '2', |
| 749 | 'X-Subtitle-Frame-Width': '640', |
| 750 | 'X-Subtitle-Frame-Height': '80', |
| 751 | 'X-Subtitle-Frame-Canvas-Width': '1920', |
| 752 | 'X-Subtitle-Frame-Canvas-Height': '1080', |
| 753 | }, |
| 754 | }); |
| 755 | }); |
| 756 | |
| 757 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 758 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 759 | }, { |
| 760 | lookaheadSeconds: 0, |
| 761 | initialPrefetchSeconds: 0, |
| 762 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 763 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 764 | }), |
| 765 | }); |
| 766 | |
| 767 | await flushAsyncWork(window, 8); |
| 768 | |
| 769 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 770 | expect(overlay).toBeTruthy(); |
| 771 | |
| 772 | player.setCurrentTime(1.2); |
| 773 | player.trigger('play'); |
| 774 | await new Promise((resolve) => window.setTimeout(resolve, 50)); |
| 775 | await flushAsyncWork(window, 2); |
| 776 | expect(overlay.style.display).toBe('block'); |
| 777 | |
| 778 | player.setCurrentTime(2.2); |
| 779 | await new Promise((resolve) => window.setTimeout(resolve, 50)); |
| 780 | await flushAsyncWork(window, 2); |
| 781 | expect(overlay.style.display).toBe('none'); |
| 782 | |
| 783 | controller.detach(); |
| 784 | await flushAsyncWork(window, 2); |
| 785 | window.close(); |
| 786 | }); |
| 787 | |
| 788 | test('uses video-frame media clock for precise cached caption timing', async () => |
| 789 | { |
| 790 | const window = createWindow(); |
| 791 | const helper = loadSharedVideoPlayer(window); |
| 792 | const { player, videojs } = buildPlayerHarness(window); |
| 793 | |
| 794 | let frameCallback = null; |
| 795 | let frameCallbackId = 0; |
| 796 | const videoEl = window.document.createElement('video'); |
| 797 | videoEl.requestVideoFrameCallback = jest.fn((cb) => |
| 798 | { |
| 799 | frameCallback = cb; |
| 800 | frameCallbackId += 1; |
| 801 | return frameCallbackId; |
| 802 | }); |
| 803 | videoEl.cancelVideoFrameCallback = jest.fn(); |
| 804 | player.el().appendChild(videoEl); |
| 805 | |
| 806 | window.fetch = jest.fn(async () => |
| 807 | { |
| 808 | return new Response(new Uint8Array([137, 80, 78, 71]), { |
| 809 | status: 200, |
| 810 | headers: { |
| 811 | 'Content-Type': 'image/png', |
| 812 | 'X-Subtitle-Frame-Start-Time': '1', |
| 813 | 'X-Subtitle-Frame-End-Time': '2', |
| 814 | 'X-Subtitle-Frame-Width': '640', |
| 815 | 'X-Subtitle-Frame-Height': '80', |
| 816 | 'X-Subtitle-Frame-Canvas-Width': '1920', |
| 817 | 'X-Subtitle-Frame-Canvas-Height': '1080', |
| 818 | }, |
| 819 | }); |
| 820 | }); |
| 821 | |
| 822 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 823 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 824 | }, { |
| 825 | lookaheadSeconds: 0, |
| 826 | initialPrefetchSeconds: 0, |
| 827 | currentFetchEnabled: false, |
| 828 | cacheMissFallbackEnabled: false, |
| 829 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 830 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 831 | }), |
| 832 | }); |
| 833 | |
| 834 | await flushAsyncWork(window, 8); |
| 835 | |
| 836 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 837 | expect(overlay).toBeTruthy(); |
| 838 | expect(videoEl.requestVideoFrameCallback).toHaveBeenCalled(); |
| 839 | expect(typeof frameCallback).toBe('function'); |
| 840 | |
| 841 | player.setCurrentTime(9); |
| 842 | player.trigger('play'); |
| 843 | |
| 844 | frameCallback(1000, { mediaTime: 1.5 }); |
| 845 | await flushAsyncWork(window, 3); |
| 846 | expect(overlay.style.display).toBe('block'); |
| 847 | |
| 848 | const nextFrameCallback = frameCallback; |
| 849 | nextFrameCallback(1016, { mediaTime: 2.2 }); |
| 850 | await flushAsyncWork(window, 3); |
| 851 | expect(overlay.style.display).toBe('none'); |
| 852 | |
| 853 | controller.detach(); |
| 854 | await flushAsyncWork(window, 2); |
| 855 | window.close(); |
| 856 | }); |
| 857 | |
| 858 | test('keeps tightly spaced captions renderable across near-boundary samples', async () => |
| 859 | { |
| 860 | const window = createWindow(); |
| 861 | const helper = loadSharedVideoPlayer(window); |
| 862 | const { player, videojs } = buildPlayerHarness(window); |
| 863 | |
| 864 | let objectUrlSeq = 0; |
| 865 | window.URL.createObjectURL = () => `blob:subtitle-${++objectUrlSeq}`; |
| 866 | |
| 867 | window.fetch = jest.fn(async (url) => |
| 868 | { |
| 869 | const parsed = new URL(url, 'http://localhost/'); |
| 870 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 871 | const headers = { |
| 872 | 'Content-Type': 'image/png', |
| 873 | 'X-Subtitle-Frame-Width': '640', |
| 874 | 'X-Subtitle-Frame-Height': '80', |
| 875 | 'X-Subtitle-Frame-Canvas-Width': '1920', |
| 876 | 'X-Subtitle-Frame-Canvas-Height': '1080', |
| 877 | }; |
| 878 | |
| 879 | if (Math.abs(timeSeconds - 8.351) < 0.02) |
| 880 | { |
| 881 | headers['X-Subtitle-Frame-Start-Time'] = '8.351'; |
| 882 | headers['X-Subtitle-Frame-End-Time'] = '8.410'; |
| 883 | } else |
| 884 | { |
| 885 | return new Response(null, { |
| 886 | status: 204, |
| 887 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 888 | }); |
| 889 | } |
| 890 | |
| 891 | return new Response(new Uint8Array([137, 80, 78, 71]), { |
| 892 | status: 200, |
| 893 | headers, |
| 894 | }); |
| 895 | }); |
| 896 | |
| 897 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 898 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 899 | }, { |
| 900 | currentFetchEnabled: false, |
| 901 | cacheMissFallbackEnabled: false, |
| 902 | lookaheadSeconds: 0, |
| 903 | initialPrefetchSeconds: 0, |
| 904 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 905 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 906 | }), |
| 907 | }); |
| 908 | |
| 909 | await flushAsyncWork(window, 8); |
| 910 | |
| 911 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 912 | const overlayImg = overlay && overlay.querySelector('img'); |
| 913 | expect(overlay).toBeTruthy(); |
| 914 | |
| 915 | player.setCurrentTime(8.344); |
| 916 | player.trigger('play'); |
| 917 | await new Promise((resolve) => window.setTimeout(resolve, 50)); |
| 918 | await flushAsyncWork(window, 2); |
| 919 | |
| 920 | expect(overlay.style.display).toBe('block'); |
| 921 | expect(overlayImg.getAttribute('src')).toBe('blob:subtitle-1'); |
| 922 | |
| 923 | window.close(); |
| 924 | }); |
| 925 | |
| 926 | test('aborts in-flight prefetch requests when the overlay is detached', async () => |
| 927 | { |
| 928 | const window = createWindow(); |
| 929 | const helper = loadSharedVideoPlayer(window); |
| 930 | const { player, videojs } = buildPlayerHarness(window); |
| 931 | const activeSignals = []; |
| 932 | |
| 933 | window.fetch = jest.fn((url, init = {}) => |
| 934 | { |
| 935 | const parsed = new URL(url, 'http://localhost/'); |
| 936 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 937 | |
| 938 | if (timeSeconds === 0) |
| 939 | { |
| 940 | return Promise.resolve(new Response(null, { |
| 941 | status: 204, |
| 942 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 943 | })); |
| 944 | } |
| 945 | |
| 946 | activeSignals.push(init.signal || null); |
| 947 | |
| 948 | return new Promise((resolve, reject) => |
| 949 | { |
| 950 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 951 | { |
| 952 | init.signal.addEventListener('abort', () => |
| 953 | { |
| 954 | reject(new DOMException('Aborted', 'AbortError')); |
| 955 | }, { once: true }); |
| 956 | } |
| 957 | }); |
| 958 | }); |
| 959 | |
| 960 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 961 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 962 | }, { |
| 963 | lookaheadSeconds: 1, |
| 964 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 965 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 966 | }), |
| 967 | }); |
| 968 | |
| 969 | await flushAsyncWork(window, 3); |
| 970 | expect(activeSignals.length).toBeGreaterThan(0); |
| 971 | |
| 972 | controller.detach(); |
| 973 | await flushAsyncWork(window, 2); |
| 974 | |
| 975 | expect(activeSignals.every(signal => signal && signal.aborted)).toBe(true); |
| 976 | |
| 977 | window.close(); |
| 978 | }); |
| 979 | |
| 980 | test('logs current subtitle request lifecycle events and flushes them on detach', async () => |
| 981 | { |
| 982 | const window = createWindow(); |
| 983 | const helper = loadSharedVideoPlayer(window); |
| 984 | const { player, videojs } = buildPlayerHarness(window); |
| 985 | const sentBatches = []; |
| 986 | |
| 987 | window.fetch = jest.fn(async () => |
| 988 | { |
| 989 | return new Response('png-data', { |
| 990 | status: 200, |
| 991 | headers: { |
| 992 | 'Content-Type': 'image/png', |
| 993 | 'X-Subtitle-Frame-Status': 'ok', |
| 994 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 995 | 'X-Subtitle-Response-Sent-At-Ms': '1012', |
| 996 | 'X-Subtitle-Frame-Start-Time': '1.5', |
| 997 | 'X-Subtitle-Frame-End-Time': '3.2', |
| 998 | 'X-Subtitle-Frame-Width': '320', |
| 999 | 'X-Subtitle-Frame-Height': '180', |
| 1000 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1001 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1002 | }, |
| 1003 | }); |
| 1004 | }); |
| 1005 | |
| 1006 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1007 | sessionId: 'sess-1', |
| 1008 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1009 | }, { |
| 1010 | lookaheadSeconds: 0, |
| 1011 | sendSubtitleLogBatch: (entries) => |
| 1012 | { |
| 1013 | sentBatches.push(entries); |
| 1014 | return Promise.resolve(); |
| 1015 | }, |
| 1016 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1017 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1018 | }), |
| 1019 | }); |
| 1020 | |
| 1021 | await flushAsyncWork(window, 8); |
| 1022 | controller.detach(); |
| 1023 | await flushAsyncWork(window, 4); |
| 1024 | |
| 1025 | const flattened = sentBatches.flat(); |
| 1026 | expect(flattened.map((entry) => entry.eventType)).toEqual(expect.arrayContaining([ |
| 1027 | 'subtitle_png_requested', |
| 1028 | 'subtitle_png_sent', |
| 1029 | 'subtitle_png_received', |
| 1030 | ])); |
| 1031 | |
| 1032 | window.close(); |
| 1033 | }); |
| 1034 | |
| 1035 | test('logs prefetch subtitle lifecycle events', async () => |
| 1036 | { |
| 1037 | const window = createWindow(); |
| 1038 | const helper = loadSharedVideoPlayer(window); |
| 1039 | const { player, videojs } = buildPlayerHarness(window); |
| 1040 | const sentBatches = []; |
| 1041 | |
| 1042 | window.fetch = jest.fn(async () => |
| 1043 | { |
| 1044 | return new Response(null, { |
| 1045 | status: 204, |
| 1046 | headers: { |
| 1047 | 'X-Subtitle-Frame-Status': 'none', |
| 1048 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 1049 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 1050 | 'X-Subtitle-Frame-Start-Time': '2', |
| 1051 | 'X-Subtitle-Frame-End-Time': '3', |
| 1052 | }, |
| 1053 | }); |
| 1054 | }); |
| 1055 | |
| 1056 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1057 | sessionId: 'sess-1', |
| 1058 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1059 | }, { |
| 1060 | lookaheadSeconds: 1, |
| 1061 | sendSubtitleLogBatch: (entries) => |
| 1062 | { |
| 1063 | sentBatches.push(entries); |
| 1064 | return Promise.resolve(); |
| 1065 | }, |
| 1066 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1067 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1068 | }), |
| 1069 | }); |
| 1070 | |
| 1071 | await flushAsyncWork(window, 8); |
| 1072 | controller.detach(); |
| 1073 | await flushAsyncWork(window, 4); |
| 1074 | |
| 1075 | const flattened = sentBatches.flat(); |
| 1076 | const prefetchEvents = flattened.filter((entry) => entry.requestKind === 'prefetch').map((entry) => entry.eventType); |
| 1077 | expect(prefetchEvents).toEqual(expect.arrayContaining([ |
| 1078 | 'subtitle_png_requested', |
| 1079 | 'subtitle_png_sent', |
| 1080 | 'subtitle_png_not_available', |
| 1081 | ])); |
| 1082 | |
| 1083 | window.close(); |
| 1084 | }); |
| 1085 | |
| 1086 | test('cache-only current mode avoids direct current subtitle fetch requests', async () => |
| 1087 | { |
| 1088 | const window = createWindow(); |
| 1089 | const helper = loadSharedVideoPlayer(window); |
| 1090 | const { player, videojs } = buildPlayerHarness(window); |
| 1091 | const sentBatches = []; |
| 1092 | |
| 1093 | player.setCurrentTime(8); |
| 1094 | window.fetch = jest.fn(async () => |
| 1095 | { |
| 1096 | return new Response(null, { |
| 1097 | status: 204, |
| 1098 | headers: { |
| 1099 | 'X-Subtitle-Frame-Status': 'none', |
| 1100 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 1101 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 1102 | }, |
| 1103 | }); |
| 1104 | }); |
| 1105 | |
| 1106 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1107 | sessionId: 'sess-1', |
| 1108 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1109 | }, { |
| 1110 | currentFetchEnabled: false, |
| 1111 | cacheMissFallbackEnabled: false, |
| 1112 | lookaheadSeconds: 0, |
| 1113 | sendSubtitleLogBatch: (entries) => |
| 1114 | { |
| 1115 | sentBatches.push(entries); |
| 1116 | return Promise.resolve(); |
| 1117 | }, |
| 1118 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1119 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1120 | }), |
| 1121 | }); |
| 1122 | |
| 1123 | await flushAsyncWork(window, 8); |
| 1124 | controller.detach(); |
| 1125 | await flushAsyncWork(window, 4); |
| 1126 | |
| 1127 | const flattened = sentBatches.flat(); |
| 1128 | expect(flattened.length).toBeGreaterThan(0); |
| 1129 | const requestKinds = new Set(flattened.map((entry) => entry.requestKind).filter(Boolean)); |
| 1130 | expect(requestKinds.has('prefetch')).toBe(true); |
| 1131 | expect(requestKinds.has('current')).toBe(false); |
| 1132 | |
| 1133 | window.close(); |
| 1134 | }); |
| 1135 | |
| 1136 | test('cache-only mode falls back to throttled current fetch on cache miss', async () => |
| 1137 | { |
| 1138 | const window = createWindow(); |
| 1139 | const helper = loadSharedVideoPlayer(window); |
| 1140 | const { player, videojs } = buildPlayerHarness(window); |
| 1141 | |
| 1142 | let requestCount = 0; |
| 1143 | window.fetch = jest.fn(async () => |
| 1144 | { |
| 1145 | requestCount += 1; |
| 1146 | if (requestCount === 1) |
| 1147 | { |
| 1148 | return new Response(new Uint8Array([137, 80, 78, 71]), { |
| 1149 | status: 200, |
| 1150 | headers: { |
| 1151 | 'Content-Type': 'image/png', |
| 1152 | 'X-Subtitle-Frame-Start-Time': '2.00', |
| 1153 | 'X-Subtitle-Frame-End-Time': '2.50', |
| 1154 | 'X-Subtitle-Frame-Width': '600', |
| 1155 | 'X-Subtitle-Frame-Height': '72', |
| 1156 | 'X-Subtitle-Frame-Canvas-Width': '1920', |
| 1157 | 'X-Subtitle-Frame-Canvas-Height': '1080', |
| 1158 | }, |
| 1159 | }); |
| 1160 | } |
| 1161 | |
| 1162 | return new Response(null, { |
| 1163 | status: 204, |
| 1164 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 1165 | }); |
| 1166 | }); |
| 1167 | |
| 1168 | player.setCurrentTime(2.2); |
| 1169 | |
| 1170 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1171 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1172 | }, { |
| 1173 | currentFetchEnabled: false, |
| 1174 | cacheMissFallbackIntervalMs: 2000, |
| 1175 | lookaheadSeconds: 0, |
| 1176 | initialPrefetchSeconds: 0, |
| 1177 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1178 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1179 | }), |
| 1180 | }); |
| 1181 | |
| 1182 | await flushAsyncWork(window, 10); |
| 1183 | |
| 1184 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 1185 | const overlayImg = overlay && overlay.querySelector('img'); |
| 1186 | expect(window.fetch).toHaveBeenCalled(); |
| 1187 | expect(overlay.style.display).toBe('block'); |
| 1188 | expect(overlayImg.getAttribute('src')).toBe('blob:subtitle'); |
| 1189 | |
| 1190 | window.close(); |
| 1191 | }); |
| 1192 | |
| 1193 | test('cache-miss fallback does not reset prefetch progress on repeated triggers', async () => |
| 1194 | { |
| 1195 | const window = createWindow(); |
| 1196 | const helper = loadSharedVideoPlayer(window); |
| 1197 | const { player, videojs } = buildPlayerHarness(window); |
| 1198 | |
| 1199 | let nowMs = 1000; |
| 1200 | const realNow = window.Date.now; |
| 1201 | window.Date.now = () => nowMs; |
| 1202 | |
| 1203 | let abortedCount = 0; |
| 1204 | window.fetch = jest.fn((url, init = {}) => |
| 1205 | { |
| 1206 | return new Promise((resolve, reject) => |
| 1207 | { |
| 1208 | const signal = init && init.signal; |
| 1209 | const onAbort = () => |
| 1210 | { |
| 1211 | abortedCount += 1; |
| 1212 | const err = new Error('aborted'); |
| 1213 | err.name = 'AbortError'; |
| 1214 | reject(err); |
| 1215 | }; |
| 1216 | |
| 1217 | if (signal && typeof signal.addEventListener === 'function') |
| 1218 | { |
| 1219 | if (signal.aborted) return onAbort(); |
| 1220 | signal.addEventListener('abort', onAbort, { once: true }); |
| 1221 | } |
| 1222 | |
| 1223 | window.setTimeout(() => |
| 1224 | { |
| 1225 | if (signal && signal.aborted) return; |
| 1226 | resolve(new Response(null, { |
| 1227 | status: 204, |
| 1228 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 1229 | })); |
| 1230 | }, 80); |
| 1231 | }); |
| 1232 | }); |
| 1233 | |
| 1234 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1235 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1236 | }, { |
| 1237 | currentFetchEnabled: false, |
| 1238 | cacheMissFallbackIntervalMs: 20, |
| 1239 | lookaheadSeconds: 2, |
| 1240 | initialPrefetchSeconds: 0, |
| 1241 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1242 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1243 | }), |
| 1244 | }); |
| 1245 | |
| 1246 | await flushAsyncWork(window, 4); |
| 1247 | |
| 1248 | player.setCurrentTime(5); |
| 1249 | player.trigger('timeupdate'); |
| 1250 | nowMs += 40; |
| 1251 | player.setCurrentTime(5.5); |
| 1252 | player.trigger('timeupdate'); |
| 1253 | nowMs += 40; |
| 1254 | player.setCurrentTime(6); |
| 1255 | player.trigger('timeupdate'); |
| 1256 | |
| 1257 | await flushAsyncWork(window, 12); |
| 1258 | expect(abortedCount).toBe(0); |
| 1259 | |
| 1260 | window.Date.now = realNow; |
| 1261 | controller.detach(); |
| 1262 | window.close(); |
| 1263 | }); |
| 1264 | |
| 1265 | test('cache-only mode performs initial warmup prefetch window on load', async () => |
| 1266 | { |
| 1267 | const window = createWindow(); |
| 1268 | const helper = loadSharedVideoPlayer(window); |
| 1269 | const { player, videojs } = buildPlayerHarness(window); |
| 1270 | const requestedTimes = []; |
| 1271 | |
| 1272 | window.fetch = jest.fn(async (url) => |
| 1273 | { |
| 1274 | const parsed = new URL(url, 'http://localhost/'); |
| 1275 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 1276 | return new Response(null, { |
| 1277 | status: 204, |
| 1278 | headers: { |
| 1279 | 'X-Subtitle-Frame-Status': 'none', |
| 1280 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 1281 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 1282 | }, |
| 1283 | }); |
| 1284 | }); |
| 1285 | |
| 1286 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1287 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1288 | }, { |
| 1289 | currentFetchEnabled: false, |
| 1290 | lookaheadSeconds: 0, |
| 1291 | initialPrefetchSeconds: 2, |
| 1292 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1293 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1294 | }), |
| 1295 | }); |
| 1296 | |
| 1297 | await flushAsyncWork(window, 12); |
| 1298 | |
| 1299 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2])); |
| 1300 | |
| 1301 | controller.detach(); |
| 1302 | await flushAsyncWork(window, 2); |
| 1303 | window.close(); |
| 1304 | }); |
| 1305 | |
| 1306 | test('renders requested and received subtitle markers on the seek bar', async () => |
| 1307 | { |
| 1308 | const window = createWindow(); |
| 1309 | const helper = loadSharedVideoPlayer(window); |
| 1310 | const { player, videojs } = buildPlayerHarness(window); |
| 1311 | |
| 1312 | window.fetch = jest.fn(async () => |
| 1313 | { |
| 1314 | return new Response('png-data', { |
| 1315 | status: 200, |
| 1316 | headers: { |
| 1317 | 'Content-Type': 'image/png', |
| 1318 | 'X-Subtitle-Frame-Status': 'ok', |
| 1319 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 1320 | 'X-Subtitle-Response-Sent-At-Ms': '1012', |
| 1321 | 'X-Subtitle-Frame-Start-Time': '2.5', |
| 1322 | 'X-Subtitle-Frame-End-Time': '3.5', |
| 1323 | 'X-Subtitle-Frame-Width': '320', |
| 1324 | 'X-Subtitle-Frame-Height': '180', |
| 1325 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1326 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1327 | }, |
| 1328 | }); |
| 1329 | }); |
| 1330 | |
| 1331 | player.duration(10); |
| 1332 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1333 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1334 | }, { |
| 1335 | lookaheadSeconds: 0, |
| 1336 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1337 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1338 | }), |
| 1339 | }); |
| 1340 | |
| 1341 | await flushAsyncWork(window, 8); |
| 1342 | |
| 1343 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 1344 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 1345 | expect(requestedMarker).toBeTruthy(); |
| 1346 | expect(receivedMarker).toBeTruthy(); |
| 1347 | expect(requestedMarker.style.display).toBe('block'); |
| 1348 | expect(receivedMarker.style.display).toBe('block'); |
| 1349 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 1350 | expect(receivedMarker.style.left).toBe('25.0000%'); |
| 1351 | expect(requestedMarker.textContent).toBe(']'); |
| 1352 | expect(receivedMarker.textContent).toBe('['); |
| 1353 | |
| 1354 | controller.detach(); |
| 1355 | await new Promise((resolve) => window.setTimeout(resolve, 300)); |
| 1356 | await flushAsyncWork(window, 2); |
| 1357 | |
| 1358 | window.close(); |
| 1359 | }); |
| 1360 | |
| 1361 | test('advances both seek markers when subtitle request returns 204 without frame timing headers', async () => |
| 1362 | { |
| 1363 | const window = createWindow(); |
| 1364 | const helper = loadSharedVideoPlayer(window); |
| 1365 | const { player, videojs } = buildPlayerHarness(window); |
| 1366 | |
| 1367 | player.duration(10); |
| 1368 | player.setCurrentTime(4); |
| 1369 | window.fetch = jest.fn(async () => |
| 1370 | { |
| 1371 | return new Response(null, { |
| 1372 | status: 204, |
| 1373 | headers: { |
| 1374 | 'X-Subtitle-Frame-Status': 'none', |
| 1375 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 1376 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 1377 | }, |
| 1378 | }); |
| 1379 | }); |
| 1380 | |
| 1381 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1382 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1383 | }, { |
| 1384 | lookaheadSeconds: 0, |
| 1385 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1386 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1387 | }), |
| 1388 | }); |
| 1389 | |
| 1390 | await flushAsyncWork(window, 8); |
| 1391 | |
| 1392 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 1393 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 1394 | expect(requestedMarker).toBeTruthy(); |
| 1395 | expect(receivedMarker).toBeTruthy(); |
| 1396 | expect(requestedMarker.style.display).toBe('block'); |
| 1397 | expect(receivedMarker.style.display).toBe('block'); |
| 1398 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 1399 | expect(receivedMarker.style.left).toBe('100.0000%'); |
| 1400 | expect(requestedMarker.textContent).toBe(']'); |
| 1401 | expect(receivedMarker.textContent).toBe('['); |
| 1402 | |
| 1403 | controller.detach(); |
| 1404 | await flushAsyncWork(window, 2); |
| 1405 | window.close(); |
| 1406 | }); |
| 1407 | |
| 1408 | test('advances seek markers to the end of an empty prefetch tail window', async () => |
| 1409 | { |
| 1410 | const window = createWindow(); |
| 1411 | const helper = loadSharedVideoPlayer(window); |
| 1412 | const { player, videojs } = buildPlayerHarness(window); |
| 1413 | |
| 1414 | player.duration(120); |
| 1415 | player.setCurrentTime(110); |
| 1416 | |
| 1417 | window.fetch = jest.fn(async () => |
| 1418 | { |
| 1419 | return new Response(null, { |
| 1420 | status: 204, |
| 1421 | headers: { |
| 1422 | 'X-Subtitle-Frame-Status': 'none', |
| 1423 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 1424 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 1425 | }, |
| 1426 | }); |
| 1427 | }); |
| 1428 | |
| 1429 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1430 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1431 | }, { |
| 1432 | lookaheadSeconds: 0, |
| 1433 | initialPrefetchSeconds: 0, |
| 1434 | currentFetchEnabled: false, |
| 1435 | cacheMissFallbackEnabled: false, |
| 1436 | prefetchAdaptiveStepEnabled: true, |
| 1437 | prefetchStepSecondsMin: 10, |
| 1438 | prefetchStepSecondsMax: 10, |
| 1439 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1440 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1441 | }), |
| 1442 | buildPrefetchRequest: ({ currentTime, endTime, streamIndex }) => ({ |
| 1443 | url: `/subtitle-range?streamIndex=${streamIndex}&startTimeSeconds=${currentTime}&endTimeSeconds=${endTime}`, |
| 1444 | }), |
| 1445 | }); |
| 1446 | |
| 1447 | player.trigger('timeupdate'); |
| 1448 | await flushAsyncWork(window, 8); |
| 1449 | |
| 1450 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 1451 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 1452 | expect(requestedMarker).toBeTruthy(); |
| 1453 | expect(receivedMarker).toBeTruthy(); |
| 1454 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 1455 | expect(receivedMarker.style.left).toBe('100.0000%'); |
| 1456 | |
| 1457 | controller.detach(); |
| 1458 | await flushAsyncWork(window, 2); |
| 1459 | window.close(); |
| 1460 | }); |
| 1461 | |
| 1462 | test('keeps existing marker positions while the next request is still in flight', async () => |
| 1463 | { |
| 1464 | const window = createWindow(); |
| 1465 | const helper = loadSharedVideoPlayer(window); |
| 1466 | const { player, videojs } = buildPlayerHarness(window); |
| 1467 | let pendingResolve = null; |
| 1468 | |
| 1469 | player.duration(10); |
| 1470 | window.fetch = jest.fn((url) => |
| 1471 | { |
| 1472 | const parsed = new URL(url, 'http://localhost/'); |
| 1473 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1474 | |
| 1475 | if (timeSeconds === 0) |
| 1476 | { |
| 1477 | return Promise.resolve(new Response('png-data', { |
| 1478 | status: 200, |
| 1479 | headers: { |
| 1480 | 'Content-Type': 'image/png', |
| 1481 | 'X-Subtitle-Frame-Status': 'ok', |
| 1482 | 'X-Subtitle-Frame-Start-Time': '2', |
| 1483 | 'X-Subtitle-Frame-End-Time': '3', |
| 1484 | 'X-Subtitle-Frame-Width': '320', |
| 1485 | 'X-Subtitle-Frame-Height': '180', |
| 1486 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1487 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1488 | }, |
| 1489 | })); |
| 1490 | } |
| 1491 | |
| 1492 | return new Promise((resolve) => |
| 1493 | { |
| 1494 | pendingResolve = resolve; |
| 1495 | }); |
| 1496 | }); |
| 1497 | |
| 1498 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1499 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1500 | }, { |
| 1501 | lookaheadSeconds: 0, |
| 1502 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1503 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1504 | }), |
| 1505 | }); |
| 1506 | |
| 1507 | await flushAsyncWork(window, 8); |
| 1508 | |
| 1509 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 1510 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 1511 | expect(requestedMarker).toBeTruthy(); |
| 1512 | expect(receivedMarker).toBeTruthy(); |
| 1513 | expect(requestedMarker.style.left).toBe('0.0000%'); |
| 1514 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 1515 | |
| 1516 | player.setCurrentTime(1.5); |
| 1517 | player.trigger('timeupdate'); |
| 1518 | await flushAsyncWork(window, 2); |
| 1519 | |
| 1520 | expect(requestedMarker.style.left).toBe('0.0000%'); |
| 1521 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 1522 | |
| 1523 | pendingResolve(new Response(null, { |
| 1524 | status: 204, |
| 1525 | headers: { |
| 1526 | 'X-Subtitle-Frame-Status': 'none', |
| 1527 | }, |
| 1528 | })); |
| 1529 | |
| 1530 | await flushAsyncWork(window, 8); |
| 1531 | expect(requestedMarker.style.display).toBe('block'); |
| 1532 | expect(receivedMarker.style.display).toBe('block'); |
| 1533 | expect(requestedMarker.style.left).toBe('15.0000%'); |
| 1534 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 1535 | |
| 1536 | controller.detach(); |
| 1537 | await flushAsyncWork(window, 2); |
| 1538 | window.close(); |
| 1539 | }); |
| 1540 | |
| 1541 | test('keeps marker positions forward-only when later responses resolve with earlier times', async () => |
| 1542 | { |
| 1543 | const window = createWindow(); |
| 1544 | const helper = loadSharedVideoPlayer(window); |
| 1545 | const { player, videojs } = buildPlayerHarness(window); |
| 1546 | const requestedTimes = []; |
| 1547 | |
| 1548 | player.duration(12); |
| 1549 | window.fetch = jest.fn(async (url) => |
| 1550 | { |
| 1551 | const parsed = new URL(url, 'http://localhost/'); |
| 1552 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1553 | requestedTimes.push(timeSeconds); |
| 1554 | |
| 1555 | if (requestedTimes.length === 1) |
| 1556 | { |
| 1557 | return new Response('png-data', { |
| 1558 | status: 200, |
| 1559 | headers: { |
| 1560 | 'Content-Type': 'image/png', |
| 1561 | 'X-Subtitle-Frame-Status': 'ok', |
| 1562 | 'X-Subtitle-Frame-Start-Time': '4', |
| 1563 | 'X-Subtitle-Frame-End-Time': '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 | return new Response('png-data', { |
| 1573 | status: 200, |
| 1574 | headers: { |
| 1575 | 'Content-Type': 'image/png', |
| 1576 | 'X-Subtitle-Frame-Status': 'ok', |
| 1577 | 'X-Subtitle-Frame-Start-Time': '2', |
| 1578 | 'X-Subtitle-Frame-End-Time': '3', |
| 1579 | 'X-Subtitle-Frame-Width': '320', |
| 1580 | 'X-Subtitle-Frame-Height': '180', |
| 1581 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1582 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1583 | }, |
| 1584 | }); |
| 1585 | }); |
| 1586 | |
| 1587 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1588 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1589 | }, { |
| 1590 | lookaheadSeconds: 0, |
| 1591 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1592 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1593 | }), |
| 1594 | }); |
| 1595 | |
| 1596 | await flushAsyncWork(window, 8); |
| 1597 | |
| 1598 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 1599 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 1600 | expect(requestedMarker).toBeTruthy(); |
| 1601 | expect(receivedMarker).toBeTruthy(); |
| 1602 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 1603 | expect(receivedMarker.style.left).toBe('33.3333%'); |
| 1604 | |
| 1605 | player.setCurrentTime(1.5); |
| 1606 | player.trigger('timeupdate'); |
| 1607 | await flushAsyncWork(window, 8); |
| 1608 | |
| 1609 | // Second response reports an older start time, but markers stay monotonic. |
| 1610 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 1611 | expect(receivedMarker.style.left).toBe('33.3333%'); |
| 1612 | |
| 1613 | controller.detach(); |
| 1614 | await flushAsyncWork(window, 2); |
| 1615 | window.close(); |
| 1616 | }); |
| 1617 | |
| 1618 | test('continues current subtitle refreshes after a force-seek aborts the in-flight request', async () => |
| 1619 | { |
| 1620 | const window = createWindow(); |
| 1621 | const helper = loadSharedVideoPlayer(window); |
| 1622 | const { player, videojs } = buildPlayerHarness(window); |
| 1623 | const requestedTimes = []; |
| 1624 | |
| 1625 | window.fetch = jest.fn((url, init = {}) => |
| 1626 | { |
| 1627 | const parsed = new URL(url, 'http://localhost/'); |
| 1628 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1629 | requestedTimes.push(timeSeconds); |
| 1630 | |
| 1631 | if (requestedTimes.length === 1) |
| 1632 | { |
| 1633 | return new Promise((resolve, reject) => |
| 1634 | { |
| 1635 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 1636 | { |
| 1637 | init.signal.addEventListener('abort', () => |
| 1638 | { |
| 1639 | reject(new window.DOMException('Aborted', 'AbortError')); |
| 1640 | }, { once: true }); |
| 1641 | } |
| 1642 | }); |
| 1643 | } |
| 1644 | |
| 1645 | return Promise.resolve(new Response(null, { |
| 1646 | status: 204, |
| 1647 | headers: { |
| 1648 | 'X-Subtitle-Frame-Status': 'none', |
| 1649 | }, |
| 1650 | })); |
| 1651 | }); |
| 1652 | |
| 1653 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1654 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1655 | }, { |
| 1656 | lookaheadSeconds: 0, |
| 1657 | currentRequestTimeoutMs: 300, |
| 1658 | logToConsole: false, |
| 1659 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1660 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1661 | }), |
| 1662 | }); |
| 1663 | |
| 1664 | await flushAsyncWork(window, 4); |
| 1665 | const afterInitialLoadCalls = window.fetch.mock.calls.length; |
| 1666 | expect(afterInitialLoadCalls).toBeGreaterThan(0); |
| 1667 | |
| 1668 | player.setCurrentTime(5); |
| 1669 | player.trigger('seeking'); |
| 1670 | await flushAsyncWork(window, 20); |
| 1671 | |
| 1672 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 1673 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 5])); |
| 1674 | |
| 1675 | controller.detach(); |
| 1676 | await flushAsyncWork(window, 2); |
| 1677 | window.close(); |
| 1678 | }); |
| 1679 | |
| 1680 | test('recovers when a current subtitle request hangs and times out', async () => |
| 1681 | { |
| 1682 | const window = createWindow(); |
| 1683 | const helper = loadSharedVideoPlayer(window); |
| 1684 | const { player, videojs } = buildPlayerHarness(window); |
| 1685 | const requestedTimes = []; |
| 1686 | |
| 1687 | window.fetch = jest.fn((url, init = {}) => |
| 1688 | { |
| 1689 | const parsed = new URL(url, 'http://localhost/'); |
| 1690 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1691 | requestedTimes.push(timeSeconds); |
| 1692 | |
| 1693 | if (requestedTimes.length === 1) |
| 1694 | { |
| 1695 | return new Promise((resolve, reject) => |
| 1696 | { |
| 1697 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 1698 | { |
| 1699 | init.signal.addEventListener('abort', () => |
| 1700 | { |
| 1701 | reject(new window.DOMException('Aborted', 'AbortError')); |
| 1702 | }, { once: true }); |
| 1703 | } |
| 1704 | }); |
| 1705 | } |
| 1706 | |
| 1707 | return Promise.resolve(new Response(null, { |
| 1708 | status: 204, |
| 1709 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 1710 | })); |
| 1711 | }); |
| 1712 | |
| 1713 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1714 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1715 | }, { |
| 1716 | lookaheadSeconds: 0, |
| 1717 | currentRequestTimeoutMs: 250, |
| 1718 | logToConsole: false, |
| 1719 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1720 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1721 | }), |
| 1722 | }); |
| 1723 | |
| 1724 | await flushAsyncWork(window, 3); |
| 1725 | |
| 1726 | player.setCurrentTime(1.5); |
| 1727 | player.trigger('timeupdate'); |
| 1728 | |
| 1729 | await new Promise((resolve) => window.setTimeout(resolve, 320)); |
| 1730 | player.trigger('timeupdate'); |
| 1731 | await new Promise((resolve) => window.setTimeout(resolve, 220)); |
| 1732 | await flushAsyncWork(window, 20); |
| 1733 | |
| 1734 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 1735 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 1.5])); |
| 1736 | |
| 1737 | controller.detach(); |
| 1738 | await new Promise((resolve) => window.setTimeout(resolve, 200)); |
| 1739 | await flushAsyncWork(window, 20); |
| 1740 | window.close(); |
| 1741 | }); |
| 1742 | |
| 1743 | test('reuses cached subtitle frames after rewinding when server no longer has older frames', async () => |
| 1744 | { |
| 1745 | const window = createWindow(); |
| 1746 | const helper = loadSharedVideoPlayer(window); |
| 1747 | const { player, videojs } = buildPlayerHarness(window); |
| 1748 | |
| 1749 | player.duration(10); |
| 1750 | player.setCurrentTime(2); |
| 1751 | window.fetch = jest.fn(async (url) => |
| 1752 | { |
| 1753 | const parsed = new URL(url, 'http://localhost/'); |
| 1754 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1755 | |
| 1756 | if (Math.abs(timeSeconds - 2) < 1e-6) |
| 1757 | { |
| 1758 | return new Response('png-data', { |
| 1759 | status: 200, |
| 1760 | headers: { |
| 1761 | 'Content-Type': 'image/png', |
| 1762 | 'X-Subtitle-Frame-Status': 'ok', |
| 1763 | 'X-Subtitle-Frame-Start-Time': '2', |
| 1764 | 'X-Subtitle-Frame-End-Time': '3', |
| 1765 | 'X-Subtitle-Frame-Width': '320', |
| 1766 | 'X-Subtitle-Frame-Height': '180', |
| 1767 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1768 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1769 | }, |
| 1770 | }); |
| 1771 | } |
| 1772 | |
| 1773 | return new Response(null, { |
| 1774 | status: 204, |
| 1775 | headers: { |
| 1776 | 'X-Subtitle-Frame-Status': 'none', |
| 1777 | }, |
| 1778 | }); |
| 1779 | }); |
| 1780 | |
| 1781 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1782 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1783 | }, { |
| 1784 | lookaheadSeconds: 0, |
| 1785 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1786 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1787 | }), |
| 1788 | }); |
| 1789 | |
| 1790 | await flushAsyncWork(window, 8); |
| 1791 | const afterInitialLoadCalls = window.fetch.mock.calls.length; |
| 1792 | expect(afterInitialLoadCalls).toBeGreaterThan(0); |
| 1793 | |
| 1794 | player.setCurrentTime(4); |
| 1795 | player.trigger('timeupdate'); |
| 1796 | await flushAsyncWork(window, 8); |
| 1797 | const afterForwardSeekCalls = window.fetch.mock.calls.length; |
| 1798 | expect(afterForwardSeekCalls).toBeGreaterThanOrEqual(afterInitialLoadCalls); |
| 1799 | |
| 1800 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 1801 | expect(overlay).toBeTruthy(); |
| 1802 | expect(overlay.style.display).toBe('none'); |
| 1803 | |
| 1804 | player.setCurrentTime(2.5); |
| 1805 | player.trigger('timeupdate'); |
| 1806 | await flushAsyncWork(window, 8); |
| 1807 | |
| 1808 | expect(window.fetch.mock.calls.length).toBe(afterForwardSeekCalls); |
| 1809 | expect(overlay.style.display).toBe('block'); |
| 1810 | |
| 1811 | controller.detach(); |
| 1812 | await flushAsyncWork(window, 2); |
| 1813 | window.close(); |
| 1814 | }); |
| 1815 | |
| 1816 | test('registers stream cache service worker once when supported', async () => |
| 1817 | { |
| 1818 | const window = createWindow(); |
| 1819 | const helper = loadSharedVideoPlayer(window); |
| 1820 | const register = jest.fn(async () => ({})); |
| 1821 | window.navigator.serviceWorker = { register }; |
| 1822 | |
| 1823 | const first = await helper.enableVideoStreamClientCache(); |
| 1824 | const second = await helper.enableVideoStreamClientCache(); |
| 1825 | |
| 1826 | expect(first).toBe(true); |
| 1827 | expect(second).toBe(true); |
| 1828 | expect(register).toHaveBeenCalledTimes(1); |
| 1829 | expect(register).toHaveBeenCalledWith('/stream-cache-sw.js', { |
| 1830 | scope: '/', |
| 1831 | updateViaCache: 'none', |
| 1832 | }); |
| 1833 | |
| 1834 | window.close(); |
| 1835 | }); |
| 1836 | |
| 1837 | test('stream-ahead prefetch follows playlists and fetches segment assets', async () => |
| 1838 | { |
| 1839 | const window = createWindow(); |
| 1840 | const helper = loadSharedVideoPlayer(window); |
| 1841 | const requestedUrls = []; |
| 1842 | |
| 1843 | window.fetch = jest.fn(async (input) => |
| 1844 | { |
| 1845 | const url = String(input && input.url ? input.url : input); |
| 1846 | requestedUrls.push(url); |
| 1847 | |
| 1848 | if (url === 'http://localhost/master.m3u8') |
| 1849 | { |
| 1850 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1851 | status: 200, |
| 1852 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1853 | }); |
| 1854 | } |
| 1855 | |
| 1856 | if (url === 'http://localhost/stream_0.m3u8') |
| 1857 | { |
| 1858 | return new Response('#EXTM3U\n#EXTINF:4,\nseg0.ts\n#EXTINF:4,\nseg1.ts\n#EXT-X-ENDLIST\n', { |
| 1859 | status: 200, |
| 1860 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1861 | }); |
| 1862 | } |
| 1863 | |
| 1864 | if (url === 'http://localhost/seg0.ts' || url === 'http://localhost/seg1.ts') |
| 1865 | { |
| 1866 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1867 | status: 200, |
| 1868 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1869 | }); |
| 1870 | } |
| 1871 | |
| 1872 | return new Response(null, { status: 404 }); |
| 1873 | }); |
| 1874 | |
| 1875 | const controller = helper.bindStreamAheadPrefetch({ |
| 1876 | playlistUrl: 'http://localhost/master.m3u8', |
| 1877 | pollIntervalMs: 0, |
| 1878 | credentials: 'same-origin', |
| 1879 | }); |
| 1880 | |
| 1881 | expect(controller).toBeTruthy(); |
| 1882 | await controller.done(); |
| 1883 | |
| 1884 | expect(requestedUrls).toEqual(expect.arrayContaining([ |
| 1885 | 'http://localhost/master.m3u8', |
| 1886 | 'http://localhost/stream_0.m3u8', |
| 1887 | 'http://localhost/seg0.ts', |
| 1888 | 'http://localhost/seg1.ts', |
| 1889 | ])); |
| 1890 | |
| 1891 | controller.stop(); |
| 1892 | window.close(); |
| 1893 | }); |
| 1894 | |
| 1895 | test('stream-ahead prefetch reports cached-through progress for fetched segments', async () => |
| 1896 | { |
| 1897 | const window = createWindow(); |
| 1898 | const helper = loadSharedVideoPlayer(window); |
| 1899 | const progressSamples = []; |
| 1900 | |
| 1901 | window.fetch = jest.fn(async (input) => |
| 1902 | { |
| 1903 | const url = String(input && input.url ? input.url : input); |
| 1904 | |
| 1905 | if (url === 'http://localhost/master.m3u8') |
| 1906 | { |
| 1907 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1908 | status: 200, |
| 1909 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1910 | }); |
| 1911 | } |
| 1912 | |
| 1913 | if (url === 'http://localhost/stream_0.m3u8') |
| 1914 | { |
| 1915 | return new Response('#EXTM3U\n#EXTINF:4,\nseg0.ts\n#EXTINF:4,\nseg1.ts\n#EXT-X-ENDLIST\n', { |
| 1916 | status: 200, |
| 1917 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1918 | }); |
| 1919 | } |
| 1920 | |
| 1921 | if (url === 'http://localhost/seg0.ts' || url === 'http://localhost/seg1.ts') |
| 1922 | { |
| 1923 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1924 | status: 200, |
| 1925 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1926 | }); |
| 1927 | } |
| 1928 | |
| 1929 | return new Response(null, { status: 404 }); |
| 1930 | }); |
| 1931 | |
| 1932 | const controller = helper.bindStreamAheadPrefetch({ |
| 1933 | playlistUrl: 'http://localhost/master.m3u8', |
| 1934 | pollIntervalMs: 0, |
| 1935 | credentials: 'same-origin', |
| 1936 | }); |
| 1937 | |
| 1938 | expect(controller).toBeTruthy(); |
| 1939 | const unsubscribe = controller.subscribe((status) => |
| 1940 | { |
| 1941 | progressSamples.push(status && status.cachedThroughSeconds); |
| 1942 | }); |
| 1943 | |
| 1944 | await controller.done(); |
| 1945 | unsubscribe(); |
| 1946 | |
| 1947 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 1948 | |
| 1949 | controller.stop(); |
| 1950 | window.close(); |
| 1951 | }); |
| 1952 | |
| 1953 | test('stream-ahead prefetch infers cached-through progress from segment filenames', async () => |
| 1954 | { |
| 1955 | const window = createWindow(); |
| 1956 | const helper = loadSharedVideoPlayer(window); |
| 1957 | const progressSamples = []; |
| 1958 | |
| 1959 | window.fetch = jest.fn(async (input) => |
| 1960 | { |
| 1961 | const url = String(input && input.url ? input.url : input); |
| 1962 | |
| 1963 | if (url === 'http://localhost/master.m3u8') |
| 1964 | { |
| 1965 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1966 | status: 200, |
| 1967 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1968 | }); |
| 1969 | } |
| 1970 | |
| 1971 | if (url === 'http://localhost/stream_0.m3u8') |
| 1972 | { |
| 1973 | return new Response('#EXTM3U\nstream_0_segment_000000.ts\nstream_0_segment_000001.ts\n#EXT-X-ENDLIST\n', { |
| 1974 | status: 200, |
| 1975 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1976 | }); |
| 1977 | } |
| 1978 | |
| 1979 | if (url === 'http://localhost/stream_0_segment_000000.ts' || url === 'http://localhost/stream_0_segment_000001.ts') |
| 1980 | { |
| 1981 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1982 | status: 200, |
| 1983 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1984 | }); |
| 1985 | } |
| 1986 | |
| 1987 | return new Response(null, { status: 404 }); |
| 1988 | }); |
| 1989 | |
| 1990 | const controller = helper.bindStreamAheadPrefetch({ |
| 1991 | playlistUrl: 'http://localhost/master.m3u8', |
| 1992 | pollIntervalMs: 0, |
| 1993 | credentials: 'same-origin', |
| 1994 | }); |
| 1995 | |
| 1996 | expect(controller).toBeTruthy(); |
| 1997 | const unsubscribe = controller.subscribe((status) => |
| 1998 | { |
| 1999 | progressSamples.push(status && status.cachedThroughSeconds); |
| 2000 | }); |
| 2001 | |
| 2002 | await controller.done(); |
| 2003 | unsubscribe(); |
| 2004 | |
| 2005 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 2006 | |
| 2007 | controller.stop(); |
| 2008 | window.close(); |
| 2009 | }); |
| 2010 | |
| 2011 | test('stream-ahead prefetch infers cached-through progress for m4s segment filenames', async () => |
| 2012 | { |
| 2013 | const window = createWindow(); |
| 2014 | const helper = loadSharedVideoPlayer(window); |
| 2015 | const progressSamples = []; |
| 2016 | |
| 2017 | window.fetch = jest.fn(async (input) => |
| 2018 | { |
| 2019 | const url = String(input && input.url ? input.url : input); |
| 2020 | |
| 2021 | if (url === 'http://localhost/master.m3u8') |
| 2022 | { |
| 2023 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 2024 | status: 200, |
| 2025 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 2026 | }); |
| 2027 | } |
| 2028 | |
| 2029 | if (url === 'http://localhost/stream_0.m3u8') |
| 2030 | { |
| 2031 | return new Response('#EXTM3U\nstream_0_segment_000000.m4s\nstream_0_segment_000001.m4s\n#EXT-X-ENDLIST\n', { |
| 2032 | status: 200, |
| 2033 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 2034 | }); |
| 2035 | } |
| 2036 | |
| 2037 | if (url === 'http://localhost/stream_0_segment_000000.m4s' || url === 'http://localhost/stream_0_segment_000001.m4s') |
| 2038 | { |
| 2039 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 2040 | status: 200, |
| 2041 | headers: { 'Content-Type': 'video/mp4' }, |
| 2042 | }); |
| 2043 | } |
| 2044 | |
| 2045 | return new Response(null, { status: 404 }); |
| 2046 | }); |
| 2047 | |
| 2048 | const controller = helper.bindStreamAheadPrefetch({ |
| 2049 | playlistUrl: 'http://localhost/master.m3u8', |
| 2050 | pollIntervalMs: 0, |
| 2051 | credentials: 'same-origin', |
| 2052 | }); |
| 2053 | |
| 2054 | expect(controller).toBeTruthy(); |
| 2055 | const unsubscribe = controller.subscribe((status) => |
| 2056 | { |
| 2057 | progressSamples.push(status && status.cachedThroughSeconds); |
| 2058 | }); |
| 2059 | |
| 2060 | await controller.done(); |
| 2061 | unsubscribe(); |
| 2062 | |
| 2063 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 2064 | |
| 2065 | controller.stop(); |
| 2066 | window.close(); |
| 2067 | }); |
| 2068 | |
| 2069 | test('stream-ahead prefetch reports ratio progress from fetched sections when timing is unknown', async () => |
| 2070 | { |
| 2071 | const window = createWindow(); |
| 2072 | const helper = loadSharedVideoPlayer(window); |
| 2073 | const statusSamples = []; |
| 2074 | |
| 2075 | window.fetch = jest.fn(async (input) => |
| 2076 | { |
| 2077 | const url = String(input && input.url ? input.url : input); |
| 2078 | |
| 2079 | if (url === 'http://localhost/master.m3u8') |
| 2080 | { |
| 2081 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 2082 | status: 200, |
| 2083 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 2084 | }); |
| 2085 | } |
| 2086 | |
| 2087 | if (url === 'http://localhost/stream_0.m3u8') |
| 2088 | { |
| 2089 | return new Response('#EXTM3U\nchunk-alpha.bin\nchunk-beta.bin\n#EXT-X-ENDLIST\n', { |
| 2090 | status: 200, |
| 2091 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 2092 | }); |
| 2093 | } |
| 2094 | |
| 2095 | if (url === 'http://localhost/chunk-alpha.bin' || url === 'http://localhost/chunk-beta.bin') |
| 2096 | { |
| 2097 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 2098 | status: 200, |
| 2099 | headers: { 'Content-Type': 'application/octet-stream' }, |
| 2100 | }); |
| 2101 | } |
| 2102 | |
| 2103 | return new Response(null, { status: 404 }); |
| 2104 | }); |
| 2105 | |
| 2106 | const controller = helper.bindStreamAheadPrefetch({ |
| 2107 | playlistUrl: 'http://localhost/master.m3u8', |
| 2108 | pollIntervalMs: 0, |
| 2109 | credentials: 'same-origin', |
| 2110 | }); |
| 2111 | |
| 2112 | expect(controller).toBeTruthy(); |
| 2113 | const unsubscribe = controller.subscribe((status) => |
| 2114 | { |
| 2115 | statusSamples.push(status || {}); |
| 2116 | }); |
| 2117 | |
| 2118 | await controller.done(); |
| 2119 | unsubscribe(); |
| 2120 | |
| 2121 | const finalStatus = statusSamples[statusSamples.length - 1] || {}; |
| 2122 | expect(finalStatus.totalSegmentCount).toBe(2); |
| 2123 | expect(finalStatus.fetchedSegmentCount).toBe(2); |
| 2124 | expect(finalStatus.cachedThroughRatio).toBe(1); |
| 2125 | |
| 2126 | controller.stop(); |
| 2127 | window.close(); |
| 2128 | }); |
| 2129 | |
| 2130 | test('clears cached stream assets for a finished playlist session', async () => |
| 2131 | { |
| 2132 | const window = createWindow(); |
| 2133 | const helper = loadSharedVideoPlayer(window); |
| 2134 | const requests = [ |
| 2135 | new window.Request('http://localhost/api/files/video-stream/sess-a/master.m3u8'), |
| 2136 | new window.Request('http://localhost/api/files/video-stream/sess-a/stream_0_segment_000001.ts'), |
| 2137 | new window.Request('http://localhost/api/files/video-stream/sess-b/master.m3u8'), |
| 2138 | ]; |
| 2139 | const deleteCalls = []; |
| 2140 | const cache = { |
| 2141 | keys: jest.fn(async () => requests), |
| 2142 | delete: jest.fn(async (request) => |
| 2143 | { |
| 2144 | deleteCalls.push(request.url); |
| 2145 | return true; |
| 2146 | }), |
| 2147 | }; |
| 2148 | window.caches = { |
| 2149 | open: jest.fn(async () => cache), |
| 2150 | }; |
| 2151 | |
| 2152 | await helper.clearVideoStreamClientCache('http://localhost/api/files/video-stream/sess-a/master.m3u8'); |
| 2153 | |
| 2154 | expect(window.caches.open).toHaveBeenCalledWith('filemanager-stream-cache-v1'); |
| 2155 | expect(cache.keys).toHaveBeenCalledTimes(1); |
| 2156 | expect(deleteCalls).toEqual(expect.arrayContaining([ |
| 2157 | 'http://localhost/api/files/video-stream/sess-a/master.m3u8', |
| 2158 | 'http://localhost/api/files/video-stream/sess-a/stream_0_segment_000001.ts', |
| 2159 | ])); |
| 2160 | expect(deleteCalls).not.toContain('http://localhost/api/files/video-stream/sess-b/master.m3u8'); |
| 2161 | |
| 2162 | window.close(); |
| 2163 | }); |
| 2164 | }); |