| 1 | 'use strict'; |
| 2 | |
| 3 | const fs = require('fs'); |
| 4 | const path = require('path'); |
| 5 | const { JSDOM } = require('jsdom'); |
| 6 | |
| 7 | function flushAsyncWork (window, rounds = 6) |
| 8 | { |
| 9 | let chain = Promise.resolve(); |
| 10 | for (let i = 0; i < rounds; i++) |
| 11 | { |
| 12 | chain = chain.then(() => new Promise((resolve) => window.setTimeout(resolve, 0))); |
| 13 | } |
| 14 | return chain; |
| 15 | } |
| 16 | |
| 17 | function buildPlayerHarness (window) |
| 18 | { |
| 19 | const registeredComponents = new Map(); |
| 20 | |
| 21 | class MockButton |
| 22 | { |
| 23 | constructor(player) |
| 24 | { |
| 25 | this._player = player; |
| 26 | this._el = null; |
| 27 | } |
| 28 | |
| 29 | player () |
| 30 | { |
| 31 | return this._player; |
| 32 | } |
| 33 | |
| 34 | buildCSSClass () |
| 35 | { |
| 36 | return 'vjs-control'; |
| 37 | } |
| 38 | |
| 39 | createEl () |
| 40 | { |
| 41 | const el = window.document.createElement('button'); |
| 42 | el.className = this.buildCSSClass(); |
| 43 | return el; |
| 44 | } |
| 45 | |
| 46 | el () |
| 47 | { |
| 48 | if (!this._el) this._el = this.createEl(); |
| 49 | return this._el; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | registeredComponents.set('Button', MockButton); |
| 54 | |
| 55 | const videojs = { |
| 56 | getComponent: (name) => registeredComponents.get(name), |
| 57 | registerComponent: (name, component) => |
| 58 | { |
| 59 | registeredComponents.set(name, component); |
| 60 | }, |
| 61 | }; |
| 62 | |
| 63 | const events = new Map(); |
| 64 | const playerEl = window.document.createElement('div'); |
| 65 | const progressControlEl = window.document.createElement('div'); |
| 66 | progressControlEl.className = 'vjs-progress-control'; |
| 67 | playerEl.appendChild(progressControlEl); |
| 68 | let currentTime = 0; |
| 69 | let durationSeconds = 10; |
| 70 | let bufferedEndSeconds = null; |
| 71 | |
| 72 | const player = { |
| 73 | ready (cb) |
| 74 | { |
| 75 | if (typeof cb === 'function') cb(); |
| 76 | }, |
| 77 | on (type, fn) |
| 78 | { |
| 79 | if (!events.has(type)) events.set(type, new Set()); |
| 80 | events.get(type).add(fn); |
| 81 | }, |
| 82 | trigger (type) |
| 83 | { |
| 84 | const listeners = events.get(type); |
| 85 | if (!listeners) return; |
| 86 | for (const fn of Array.from(listeners)) fn(); |
| 87 | }, |
| 88 | currentTime () |
| 89 | { |
| 90 | return currentTime; |
| 91 | }, |
| 92 | setCurrentTime (next) |
| 93 | { |
| 94 | currentTime = Number(next); |
| 95 | }, |
| 96 | duration (nextValue) |
| 97 | { |
| 98 | if (typeof nextValue === 'number') |
| 99 | { |
| 100 | durationSeconds = nextValue; |
| 101 | } |
| 102 | return durationSeconds; |
| 103 | }, |
| 104 | bufferedEnd () |
| 105 | { |
| 106 | return bufferedEndSeconds; |
| 107 | }, |
| 108 | setBufferedEnd (next) |
| 109 | { |
| 110 | bufferedEndSeconds = Number.isFinite(next) ? Number(next) : null; |
| 111 | }, |
| 112 | el () |
| 113 | { |
| 114 | return playerEl; |
| 115 | }, |
| 116 | getChild (name) |
| 117 | { |
| 118 | if (name !== 'controlBar') return null; |
| 119 | return { |
| 120 | addChild (componentName) |
| 121 | { |
| 122 | const Component = registeredComponents.get(componentName); |
| 123 | if (!Component) return null; |
| 124 | const instance = new Component(player); |
| 125 | playerEl.appendChild(instance.el()); |
| 126 | return instance; |
| 127 | }, |
| 128 | }; |
| 129 | }, |
| 130 | }; |
| 131 | |
| 132 | return { player, videojs }; |
| 133 | } |
| 134 | |
| 135 | function loadSharedVideoPlayer (window) |
| 136 | { |
| 137 | const script = fs.readFileSync(path.join(__dirname, '../public/shared-video-player.js'), 'utf8'); |
| 138 | window.eval(script); |
| 139 | return window.FilemanagerSharedVideoPlayer; |
| 140 | } |
| 141 | |
| 142 | describe('shared image subtitle prefetching', () => |
| 143 | { |
| 144 | function createWindow () |
| 145 | { |
| 146 | const dom = new JSDOM('<!doctype html><html><body></body></html>', { |
| 147 | url: 'http://localhost/', |
| 148 | runScripts: 'dangerously', |
| 149 | pretendToBeVisual: true, |
| 150 | }); |
| 151 | |
| 152 | dom.window.AbortController = AbortController; |
| 153 | dom.window.Response = Response; |
| 154 | dom.window.Headers = Headers; |
| 155 | dom.window.Request = Request; |
| 156 | dom.window.URL.createObjectURL = () => 'blob:subtitle'; |
| 157 | dom.window.URL.revokeObjectURL = () => { }; |
| 158 | |
| 159 | return dom.window; |
| 160 | } |
| 161 | |
| 162 | test('prefetches lookahead frames even when no subtitle is currently active', async () => |
| 163 | { |
| 164 | const window = createWindow(); |
| 165 | const helper = loadSharedVideoPlayer(window); |
| 166 | const { player, videojs } = buildPlayerHarness(window); |
| 167 | const requestedTimes = []; |
| 168 | |
| 169 | window.fetch = jest.fn(async (url) => |
| 170 | { |
| 171 | const parsed = new URL(url, 'http://localhost/'); |
| 172 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 173 | return new Response(null, { |
| 174 | status: 204, |
| 175 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 176 | }); |
| 177 | }); |
| 178 | |
| 179 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 180 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 181 | }, { |
| 182 | lookaheadSeconds: 1, |
| 183 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 184 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 185 | }), |
| 186 | }); |
| 187 | |
| 188 | await flushAsyncWork(window); |
| 189 | |
| 190 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1])); |
| 191 | |
| 192 | window.close(); |
| 193 | }); |
| 194 | |
| 195 | test('prefetches subtitle frames through known video duration even with short lookahead', async () => |
| 196 | { |
| 197 | const window = createWindow(); |
| 198 | const helper = loadSharedVideoPlayer(window); |
| 199 | const { player, videojs } = buildPlayerHarness(window); |
| 200 | const requestedTimes = []; |
| 201 | |
| 202 | player.duration(3); |
| 203 | |
| 204 | window.fetch = jest.fn(async (url) => |
| 205 | { |
| 206 | const parsed = new URL(url, 'http://localhost/'); |
| 207 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 208 | return new Response(null, { |
| 209 | status: 204, |
| 210 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 211 | }); |
| 212 | }); |
| 213 | |
| 214 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 215 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 216 | }, { |
| 217 | lookaheadSeconds: 0.5, |
| 218 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 219 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 220 | }), |
| 221 | }); |
| 222 | |
| 223 | await flushAsyncWork(window, 18); |
| 224 | |
| 225 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3])); |
| 226 | |
| 227 | window.close(); |
| 228 | }); |
| 229 | |
| 230 | test('prefetches at least through the buffered video end', async () => |
| 231 | { |
| 232 | const window = createWindow(); |
| 233 | const helper = loadSharedVideoPlayer(window); |
| 234 | const { player, videojs } = buildPlayerHarness(window); |
| 235 | const requestedTimes = []; |
| 236 | |
| 237 | player.setBufferedEnd(3); |
| 238 | |
| 239 | window.fetch = jest.fn(async (url) => |
| 240 | { |
| 241 | const parsed = new URL(url, 'http://localhost/'); |
| 242 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 243 | return new Response(null, { |
| 244 | status: 204, |
| 245 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 246 | }); |
| 247 | }); |
| 248 | |
| 249 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 250 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 251 | }, { |
| 252 | lookaheadSeconds: 0.5, |
| 253 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 254 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 255 | }), |
| 256 | }); |
| 257 | |
| 258 | await flushAsyncWork(window, 12); |
| 259 | |
| 260 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3])); |
| 261 | |
| 262 | window.close(); |
| 263 | }); |
| 264 | |
| 265 | test('prefetch uses furthest buffered end when player.bufferedEnd under-reports', async () => |
| 266 | { |
| 267 | const window = createWindow(); |
| 268 | const helper = loadSharedVideoPlayer(window); |
| 269 | const { player, videojs } = buildPlayerHarness(window); |
| 270 | const requestedTimes = []; |
| 271 | |
| 272 | player.setBufferedEnd(0); |
| 273 | player.tech = () => ({ |
| 274 | el: () => ({ |
| 275 | buffered: { |
| 276 | length: 1, |
| 277 | end: () => 6, |
| 278 | }, |
| 279 | }), |
| 280 | }); |
| 281 | |
| 282 | window.fetch = jest.fn(async (url) => |
| 283 | { |
| 284 | const parsed = new URL(url, 'http://localhost/'); |
| 285 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 286 | return new Response(null, { |
| 287 | status: 204, |
| 288 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 289 | }); |
| 290 | }); |
| 291 | |
| 292 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 293 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 294 | }, { |
| 295 | lookaheadSeconds: 0, |
| 296 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 297 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 298 | }), |
| 299 | }); |
| 300 | |
| 301 | await flushAsyncWork(window, 16); |
| 302 | |
| 303 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6])); |
| 304 | |
| 305 | window.close(); |
| 306 | }); |
| 307 | |
| 308 | test('prefetch frontier jumps to current playback time instead of replaying stale lag', async () => |
| 309 | { |
| 310 | const window = createWindow(); |
| 311 | const helper = loadSharedVideoPlayer(window); |
| 312 | const { player, videojs } = buildPlayerHarness(window); |
| 313 | const requestedTimes = []; |
| 314 | |
| 315 | player.setBufferedEnd(35); |
| 316 | |
| 317 | window.fetch = jest.fn(async (url) => |
| 318 | { |
| 319 | const parsed = new URL(url, 'http://localhost/'); |
| 320 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 321 | requestedTimes.push(timeSeconds); |
| 322 | return new Response(null, { |
| 323 | status: 204, |
| 324 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 325 | }); |
| 326 | }); |
| 327 | |
| 328 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 329 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 330 | }, { |
| 331 | lookaheadSeconds: 0, |
| 332 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 333 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 334 | }), |
| 335 | }); |
| 336 | |
| 337 | await flushAsyncWork(window, 8); |
| 338 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5])); |
| 339 | |
| 340 | requestedTimes.length = 0; |
| 341 | player.setCurrentTime(20); |
| 342 | player.setBufferedEnd(40); |
| 343 | player.trigger('timeupdate'); |
| 344 | await flushAsyncWork(window, 8); |
| 345 | |
| 346 | expect(requestedTimes).toContain(20); |
| 347 | const prefetchAfterJump = requestedTimes.filter((value) => value > 20); |
| 348 | expect(prefetchAfterJump.length).toBeGreaterThan(0); |
| 349 | expect(Math.min(...prefetchAfterJump)).toBeGreaterThanOrEqual(20.5); |
| 350 | |
| 351 | window.close(); |
| 352 | }); |
| 353 | |
| 354 | test('does not restart current subtitle request while one is in flight', async () => |
| 355 | { |
| 356 | const window = createWindow(); |
| 357 | const helper = loadSharedVideoPlayer(window); |
| 358 | const { player, videojs } = buildPlayerHarness(window); |
| 359 | const pendingResolvers = []; |
| 360 | const requestTimes = []; |
| 361 | |
| 362 | window.fetch = jest.fn((url, init = {}) => |
| 363 | { |
| 364 | const parsed = new URL(url, 'http://localhost/'); |
| 365 | requestTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 366 | |
| 367 | if (pendingResolvers.length === 0) |
| 368 | { |
| 369 | return new Promise((resolve) => |
| 370 | { |
| 371 | pendingResolvers.push(() => resolve(new Response(null, { |
| 372 | status: 204, |
| 373 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 374 | }))); |
| 375 | }); |
| 376 | } |
| 377 | |
| 378 | expect(init.signal && init.signal.aborted).toBe(false); |
| 379 | return Promise.resolve(new Response(null, { |
| 380 | status: 204, |
| 381 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 382 | })); |
| 383 | }); |
| 384 | |
| 385 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 386 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 387 | }, { |
| 388 | lookaheadSeconds: 0, |
| 389 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 390 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 391 | }), |
| 392 | }); |
| 393 | |
| 394 | await flushAsyncWork(window, 3); |
| 395 | expect(window.fetch).toHaveBeenCalledTimes(1); |
| 396 | |
| 397 | player.setCurrentTime(1.5); |
| 398 | player.trigger('timeupdate'); |
| 399 | player.setCurrentTime(2.25); |
| 400 | player.trigger('timeupdate'); |
| 401 | await flushAsyncWork(window, 2); |
| 402 | |
| 403 | expect(window.fetch).toHaveBeenCalledTimes(1); |
| 404 | |
| 405 | pendingResolvers[0](); |
| 406 | await flushAsyncWork(window, 5); |
| 407 | |
| 408 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 409 | expect(requestTimes).toEqual(expect.arrayContaining([0, 2.25])); |
| 410 | |
| 411 | window.close(); |
| 412 | }); |
| 413 | |
| 414 | test('aborts in-flight prefetch requests when the overlay is detached', async () => |
| 415 | { |
| 416 | const window = createWindow(); |
| 417 | const helper = loadSharedVideoPlayer(window); |
| 418 | const { player, videojs } = buildPlayerHarness(window); |
| 419 | const activeSignals = []; |
| 420 | |
| 421 | window.fetch = jest.fn((url, init = {}) => |
| 422 | { |
| 423 | const parsed = new URL(url, 'http://localhost/'); |
| 424 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 425 | |
| 426 | if (timeSeconds === 0) |
| 427 | { |
| 428 | return Promise.resolve(new Response(null, { |
| 429 | status: 204, |
| 430 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 431 | })); |
| 432 | } |
| 433 | |
| 434 | activeSignals.push(init.signal || null); |
| 435 | |
| 436 | return new Promise((resolve, reject) => |
| 437 | { |
| 438 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 439 | { |
| 440 | init.signal.addEventListener('abort', () => |
| 441 | { |
| 442 | reject(new DOMException('Aborted', 'AbortError')); |
| 443 | }, { once: true }); |
| 444 | } |
| 445 | }); |
| 446 | }); |
| 447 | |
| 448 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 449 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 450 | }, { |
| 451 | lookaheadSeconds: 1, |
| 452 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 453 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 454 | }), |
| 455 | }); |
| 456 | |
| 457 | await flushAsyncWork(window, 3); |
| 458 | expect(activeSignals.length).toBeGreaterThan(0); |
| 459 | |
| 460 | controller.detach(); |
| 461 | await flushAsyncWork(window, 2); |
| 462 | |
| 463 | expect(activeSignals.every(signal => signal && signal.aborted)).toBe(true); |
| 464 | |
| 465 | window.close(); |
| 466 | }); |
| 467 | |
| 468 | test('logs current subtitle request lifecycle events and flushes them on detach', async () => |
| 469 | { |
| 470 | const window = createWindow(); |
| 471 | const helper = loadSharedVideoPlayer(window); |
| 472 | const { player, videojs } = buildPlayerHarness(window); |
| 473 | const sentBatches = []; |
| 474 | |
| 475 | window.fetch = jest.fn(async () => |
| 476 | { |
| 477 | return new Response('png-data', { |
| 478 | status: 200, |
| 479 | headers: { |
| 480 | 'Content-Type': 'image/png', |
| 481 | 'X-Subtitle-Frame-Status': 'ok', |
| 482 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 483 | 'X-Subtitle-Response-Sent-At-Ms': '1012', |
| 484 | 'X-Subtitle-Frame-Start-Time': '1.5', |
| 485 | 'X-Subtitle-Frame-End-Time': '3.2', |
| 486 | 'X-Subtitle-Frame-Width': '320', |
| 487 | 'X-Subtitle-Frame-Height': '180', |
| 488 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 489 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 490 | }, |
| 491 | }); |
| 492 | }); |
| 493 | |
| 494 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 495 | sessionId: 'sess-1', |
| 496 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 497 | }, { |
| 498 | lookaheadSeconds: 0, |
| 499 | sendSubtitleLogBatch: (entries) => |
| 500 | { |
| 501 | sentBatches.push(entries); |
| 502 | return Promise.resolve(); |
| 503 | }, |
| 504 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 505 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 506 | }), |
| 507 | }); |
| 508 | |
| 509 | await flushAsyncWork(window, 8); |
| 510 | controller.detach(); |
| 511 | await flushAsyncWork(window, 4); |
| 512 | |
| 513 | const flattened = sentBatches.flat(); |
| 514 | expect(flattened.map((entry) => entry.eventType)).toEqual(expect.arrayContaining([ |
| 515 | 'subtitle_png_requested', |
| 516 | 'subtitle_png_sent', |
| 517 | 'subtitle_png_received', |
| 518 | ])); |
| 519 | |
| 520 | window.close(); |
| 521 | }); |
| 522 | |
| 523 | test('logs prefetch subtitle lifecycle events', async () => |
| 524 | { |
| 525 | const window = createWindow(); |
| 526 | const helper = loadSharedVideoPlayer(window); |
| 527 | const { player, videojs } = buildPlayerHarness(window); |
| 528 | const sentBatches = []; |
| 529 | |
| 530 | window.fetch = jest.fn(async () => |
| 531 | { |
| 532 | return new Response(null, { |
| 533 | status: 204, |
| 534 | headers: { |
| 535 | 'X-Subtitle-Frame-Status': 'none', |
| 536 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 537 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 538 | 'X-Subtitle-Frame-Start-Time': '2', |
| 539 | 'X-Subtitle-Frame-End-Time': '3', |
| 540 | }, |
| 541 | }); |
| 542 | }); |
| 543 | |
| 544 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 545 | sessionId: 'sess-1', |
| 546 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 547 | }, { |
| 548 | lookaheadSeconds: 1, |
| 549 | sendSubtitleLogBatch: (entries) => |
| 550 | { |
| 551 | sentBatches.push(entries); |
| 552 | return Promise.resolve(); |
| 553 | }, |
| 554 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 555 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 556 | }), |
| 557 | }); |
| 558 | |
| 559 | await flushAsyncWork(window, 8); |
| 560 | controller.detach(); |
| 561 | await flushAsyncWork(window, 4); |
| 562 | |
| 563 | const flattened = sentBatches.flat(); |
| 564 | const prefetchEvents = flattened.filter((entry) => entry.requestKind === 'prefetch').map((entry) => entry.eventType); |
| 565 | expect(prefetchEvents).toEqual(expect.arrayContaining([ |
| 566 | 'subtitle_png_requested', |
| 567 | 'subtitle_png_sent', |
| 568 | 'subtitle_png_not_available', |
| 569 | ])); |
| 570 | |
| 571 | window.close(); |
| 572 | }); |
| 573 | |
| 574 | test('renders requested and received subtitle markers on the seek bar', async () => |
| 575 | { |
| 576 | const window = createWindow(); |
| 577 | const helper = loadSharedVideoPlayer(window); |
| 578 | const { player, videojs } = buildPlayerHarness(window); |
| 579 | |
| 580 | window.fetch = jest.fn(async () => |
| 581 | { |
| 582 | return new Response('png-data', { |
| 583 | status: 200, |
| 584 | headers: { |
| 585 | 'Content-Type': 'image/png', |
| 586 | 'X-Subtitle-Frame-Status': 'ok', |
| 587 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 588 | 'X-Subtitle-Response-Sent-At-Ms': '1012', |
| 589 | 'X-Subtitle-Frame-Start-Time': '2.5', |
| 590 | 'X-Subtitle-Frame-End-Time': '3.5', |
| 591 | 'X-Subtitle-Frame-Width': '320', |
| 592 | 'X-Subtitle-Frame-Height': '180', |
| 593 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 594 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 595 | }, |
| 596 | }); |
| 597 | }); |
| 598 | |
| 599 | player.duration(10); |
| 600 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 601 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 602 | }, { |
| 603 | lookaheadSeconds: 0, |
| 604 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 605 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 606 | }), |
| 607 | }); |
| 608 | |
| 609 | await flushAsyncWork(window, 8); |
| 610 | |
| 611 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 612 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 613 | expect(requestedMarker).toBeTruthy(); |
| 614 | expect(receivedMarker).toBeTruthy(); |
| 615 | expect(requestedMarker.style.display).toBe('block'); |
| 616 | expect(receivedMarker.style.display).toBe('block'); |
| 617 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 618 | expect(receivedMarker.style.left).toBe('25.0000%'); |
| 619 | expect(requestedMarker.textContent).toBe(']'); |
| 620 | expect(receivedMarker.textContent).toBe('['); |
| 621 | |
| 622 | controller.detach(); |
| 623 | await new Promise((resolve) => window.setTimeout(resolve, 300)); |
| 624 | await flushAsyncWork(window, 2); |
| 625 | |
| 626 | window.close(); |
| 627 | }); |
| 628 | |
| 629 | test('advances both seek markers when subtitle request returns 204 without frame timing headers', async () => |
| 630 | { |
| 631 | const window = createWindow(); |
| 632 | const helper = loadSharedVideoPlayer(window); |
| 633 | const { player, videojs } = buildPlayerHarness(window); |
| 634 | |
| 635 | player.duration(10); |
| 636 | player.setCurrentTime(4); |
| 637 | window.fetch = jest.fn(async () => |
| 638 | { |
| 639 | return new Response(null, { |
| 640 | status: 204, |
| 641 | headers: { |
| 642 | 'X-Subtitle-Frame-Status': 'none', |
| 643 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 644 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 645 | }, |
| 646 | }); |
| 647 | }); |
| 648 | |
| 649 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 650 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 651 | }, { |
| 652 | lookaheadSeconds: 0, |
| 653 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 654 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 655 | }), |
| 656 | }); |
| 657 | |
| 658 | await flushAsyncWork(window, 8); |
| 659 | |
| 660 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 661 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 662 | expect(requestedMarker).toBeTruthy(); |
| 663 | expect(receivedMarker).toBeTruthy(); |
| 664 | expect(requestedMarker.style.display).toBe('block'); |
| 665 | expect(receivedMarker.style.display).toBe('block'); |
| 666 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 667 | expect(receivedMarker.style.left).toBe('100.0000%'); |
| 668 | expect(requestedMarker.textContent).toBe(']'); |
| 669 | expect(receivedMarker.textContent).toBe('['); |
| 670 | |
| 671 | controller.detach(); |
| 672 | await flushAsyncWork(window, 2); |
| 673 | window.close(); |
| 674 | }); |
| 675 | |
| 676 | test('keeps existing marker positions while the next request is still in flight', async () => |
| 677 | { |
| 678 | const window = createWindow(); |
| 679 | const helper = loadSharedVideoPlayer(window); |
| 680 | const { player, videojs } = buildPlayerHarness(window); |
| 681 | let pendingResolve = null; |
| 682 | |
| 683 | player.duration(10); |
| 684 | window.fetch = jest.fn((url) => |
| 685 | { |
| 686 | const parsed = new URL(url, 'http://localhost/'); |
| 687 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 688 | |
| 689 | if (timeSeconds === 0) |
| 690 | { |
| 691 | return Promise.resolve(new Response('png-data', { |
| 692 | status: 200, |
| 693 | headers: { |
| 694 | 'Content-Type': 'image/png', |
| 695 | 'X-Subtitle-Frame-Status': 'ok', |
| 696 | 'X-Subtitle-Frame-Start-Time': '2', |
| 697 | 'X-Subtitle-Frame-End-Time': '3', |
| 698 | 'X-Subtitle-Frame-Width': '320', |
| 699 | 'X-Subtitle-Frame-Height': '180', |
| 700 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 701 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 702 | }, |
| 703 | })); |
| 704 | } |
| 705 | |
| 706 | return new Promise((resolve) => |
| 707 | { |
| 708 | pendingResolve = resolve; |
| 709 | }); |
| 710 | }); |
| 711 | |
| 712 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 713 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 714 | }, { |
| 715 | lookaheadSeconds: 0, |
| 716 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 717 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 718 | }), |
| 719 | }); |
| 720 | |
| 721 | await flushAsyncWork(window, 8); |
| 722 | |
| 723 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 724 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 725 | expect(requestedMarker).toBeTruthy(); |
| 726 | expect(receivedMarker).toBeTruthy(); |
| 727 | expect(requestedMarker.style.left).toBe('0.0000%'); |
| 728 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 729 | |
| 730 | player.setCurrentTime(1.5); |
| 731 | player.trigger('timeupdate'); |
| 732 | await flushAsyncWork(window, 2); |
| 733 | |
| 734 | expect(requestedMarker.style.left).toBe('0.0000%'); |
| 735 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 736 | |
| 737 | pendingResolve(new Response(null, { |
| 738 | status: 204, |
| 739 | headers: { |
| 740 | 'X-Subtitle-Frame-Status': 'none', |
| 741 | }, |
| 742 | })); |
| 743 | |
| 744 | await flushAsyncWork(window, 8); |
| 745 | expect(requestedMarker.style.display).toBe('block'); |
| 746 | expect(receivedMarker.style.display).toBe('block'); |
| 747 | expect(requestedMarker.style.left).toBe('15.0000%'); |
| 748 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 749 | |
| 750 | controller.detach(); |
| 751 | await flushAsyncWork(window, 2); |
| 752 | window.close(); |
| 753 | }); |
| 754 | |
| 755 | test('keeps marker positions forward-only when later responses resolve with earlier times', async () => |
| 756 | { |
| 757 | const window = createWindow(); |
| 758 | const helper = loadSharedVideoPlayer(window); |
| 759 | const { player, videojs } = buildPlayerHarness(window); |
| 760 | const requestedTimes = []; |
| 761 | |
| 762 | player.duration(12); |
| 763 | window.fetch = jest.fn(async (url) => |
| 764 | { |
| 765 | const parsed = new URL(url, 'http://localhost/'); |
| 766 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 767 | requestedTimes.push(timeSeconds); |
| 768 | |
| 769 | if (requestedTimes.length === 1) |
| 770 | { |
| 771 | return new Response('png-data', { |
| 772 | status: 200, |
| 773 | headers: { |
| 774 | 'Content-Type': 'image/png', |
| 775 | 'X-Subtitle-Frame-Status': 'ok', |
| 776 | 'X-Subtitle-Frame-Start-Time': '4', |
| 777 | 'X-Subtitle-Frame-End-Time': '5', |
| 778 | 'X-Subtitle-Frame-Width': '320', |
| 779 | 'X-Subtitle-Frame-Height': '180', |
| 780 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 781 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 782 | }, |
| 783 | }); |
| 784 | } |
| 785 | |
| 786 | return new Response('png-data', { |
| 787 | status: 200, |
| 788 | headers: { |
| 789 | 'Content-Type': 'image/png', |
| 790 | 'X-Subtitle-Frame-Status': 'ok', |
| 791 | 'X-Subtitle-Frame-Start-Time': '2', |
| 792 | 'X-Subtitle-Frame-End-Time': '3', |
| 793 | 'X-Subtitle-Frame-Width': '320', |
| 794 | 'X-Subtitle-Frame-Height': '180', |
| 795 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 796 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 797 | }, |
| 798 | }); |
| 799 | }); |
| 800 | |
| 801 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 802 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 803 | }, { |
| 804 | lookaheadSeconds: 0, |
| 805 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 806 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 807 | }), |
| 808 | }); |
| 809 | |
| 810 | await flushAsyncWork(window, 8); |
| 811 | |
| 812 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 813 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 814 | expect(requestedMarker).toBeTruthy(); |
| 815 | expect(receivedMarker).toBeTruthy(); |
| 816 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 817 | expect(receivedMarker.style.left).toBe('33.3333%'); |
| 818 | |
| 819 | player.setCurrentTime(1.5); |
| 820 | player.trigger('timeupdate'); |
| 821 | await flushAsyncWork(window, 8); |
| 822 | |
| 823 | // Second response reports an older start time, but markers stay monotonic. |
| 824 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 825 | expect(receivedMarker.style.left).toBe('33.3333%'); |
| 826 | |
| 827 | controller.detach(); |
| 828 | await flushAsyncWork(window, 2); |
| 829 | window.close(); |
| 830 | }); |
| 831 | |
| 832 | test('continues current subtitle refreshes after a force-seek aborts the in-flight request', async () => |
| 833 | { |
| 834 | const window = createWindow(); |
| 835 | const helper = loadSharedVideoPlayer(window); |
| 836 | const { player, videojs } = buildPlayerHarness(window); |
| 837 | const requestedTimes = []; |
| 838 | |
| 839 | window.fetch = jest.fn((url, init = {}) => |
| 840 | { |
| 841 | const parsed = new URL(url, 'http://localhost/'); |
| 842 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 843 | requestedTimes.push(timeSeconds); |
| 844 | |
| 845 | if (requestedTimes.length === 1) |
| 846 | { |
| 847 | return new Promise((resolve, reject) => |
| 848 | { |
| 849 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 850 | { |
| 851 | init.signal.addEventListener('abort', () => |
| 852 | { |
| 853 | reject(new window.DOMException('Aborted', 'AbortError')); |
| 854 | }, { once: true }); |
| 855 | } |
| 856 | }); |
| 857 | } |
| 858 | |
| 859 | return Promise.resolve(new Response(null, { |
| 860 | status: 204, |
| 861 | headers: { |
| 862 | 'X-Subtitle-Frame-Status': 'none', |
| 863 | }, |
| 864 | })); |
| 865 | }); |
| 866 | |
| 867 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 868 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 869 | }, { |
| 870 | lookaheadSeconds: 0, |
| 871 | currentRequestTimeoutMs: 300, |
| 872 | logToConsole: false, |
| 873 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 874 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 875 | }), |
| 876 | }); |
| 877 | |
| 878 | await flushAsyncWork(window, 4); |
| 879 | const afterInitialLoadCalls = window.fetch.mock.calls.length; |
| 880 | expect(afterInitialLoadCalls).toBeGreaterThan(0); |
| 881 | |
| 882 | player.setCurrentTime(5); |
| 883 | player.trigger('seeking'); |
| 884 | await flushAsyncWork(window, 20); |
| 885 | |
| 886 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 887 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 5])); |
| 888 | |
| 889 | controller.detach(); |
| 890 | await flushAsyncWork(window, 2); |
| 891 | window.close(); |
| 892 | }); |
| 893 | |
| 894 | test('recovers when a current subtitle request hangs and times out', async () => |
| 895 | { |
| 896 | const window = createWindow(); |
| 897 | const helper = loadSharedVideoPlayer(window); |
| 898 | const { player, videojs } = buildPlayerHarness(window); |
| 899 | const requestedTimes = []; |
| 900 | |
| 901 | window.fetch = jest.fn((url, init = {}) => |
| 902 | { |
| 903 | const parsed = new URL(url, 'http://localhost/'); |
| 904 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 905 | requestedTimes.push(timeSeconds); |
| 906 | |
| 907 | if (requestedTimes.length === 1) |
| 908 | { |
| 909 | return new Promise((resolve, reject) => |
| 910 | { |
| 911 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 912 | { |
| 913 | init.signal.addEventListener('abort', () => |
| 914 | { |
| 915 | reject(new window.DOMException('Aborted', 'AbortError')); |
| 916 | }, { once: true }); |
| 917 | } |
| 918 | }); |
| 919 | } |
| 920 | |
| 921 | return Promise.resolve(new Response(null, { |
| 922 | status: 204, |
| 923 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 924 | })); |
| 925 | }); |
| 926 | |
| 927 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 928 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 929 | }, { |
| 930 | lookaheadSeconds: 0, |
| 931 | currentRequestTimeoutMs: 250, |
| 932 | logToConsole: false, |
| 933 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 934 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 935 | }), |
| 936 | }); |
| 937 | |
| 938 | await flushAsyncWork(window, 3); |
| 939 | |
| 940 | player.setCurrentTime(1.5); |
| 941 | player.trigger('timeupdate'); |
| 942 | |
| 943 | await new Promise((resolve) => window.setTimeout(resolve, 320)); |
| 944 | player.trigger('timeupdate'); |
| 945 | await new Promise((resolve) => window.setTimeout(resolve, 220)); |
| 946 | await flushAsyncWork(window, 20); |
| 947 | |
| 948 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 949 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 1.5])); |
| 950 | |
| 951 | controller.detach(); |
| 952 | await new Promise((resolve) => window.setTimeout(resolve, 200)); |
| 953 | await flushAsyncWork(window, 20); |
| 954 | window.close(); |
| 955 | }); |
| 956 | |
| 957 | test('reuses cached subtitle frames after rewinding when server no longer has older frames', async () => |
| 958 | { |
| 959 | const window = createWindow(); |
| 960 | const helper = loadSharedVideoPlayer(window); |
| 961 | const { player, videojs } = buildPlayerHarness(window); |
| 962 | |
| 963 | player.duration(10); |
| 964 | player.setCurrentTime(2); |
| 965 | window.fetch = jest.fn(async (url) => |
| 966 | { |
| 967 | const parsed = new URL(url, 'http://localhost/'); |
| 968 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 969 | |
| 970 | if (Math.abs(timeSeconds - 2) < 1e-6) |
| 971 | { |
| 972 | return new Response('png-data', { |
| 973 | status: 200, |
| 974 | headers: { |
| 975 | 'Content-Type': 'image/png', |
| 976 | 'X-Subtitle-Frame-Status': 'ok', |
| 977 | 'X-Subtitle-Frame-Start-Time': '2', |
| 978 | 'X-Subtitle-Frame-End-Time': '3', |
| 979 | 'X-Subtitle-Frame-Width': '320', |
| 980 | 'X-Subtitle-Frame-Height': '180', |
| 981 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 982 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 983 | }, |
| 984 | }); |
| 985 | } |
| 986 | |
| 987 | return new Response(null, { |
| 988 | status: 204, |
| 989 | headers: { |
| 990 | 'X-Subtitle-Frame-Status': 'none', |
| 991 | }, |
| 992 | }); |
| 993 | }); |
| 994 | |
| 995 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 996 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 997 | }, { |
| 998 | lookaheadSeconds: 0, |
| 999 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1000 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1001 | }), |
| 1002 | }); |
| 1003 | |
| 1004 | await flushAsyncWork(window, 8); |
| 1005 | const afterInitialLoadCalls = window.fetch.mock.calls.length; |
| 1006 | expect(afterInitialLoadCalls).toBeGreaterThan(0); |
| 1007 | |
| 1008 | player.setCurrentTime(4); |
| 1009 | player.trigger('timeupdate'); |
| 1010 | await flushAsyncWork(window, 8); |
| 1011 | const afterForwardSeekCalls = window.fetch.mock.calls.length; |
| 1012 | expect(afterForwardSeekCalls).toBeGreaterThanOrEqual(afterInitialLoadCalls); |
| 1013 | |
| 1014 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 1015 | expect(overlay).toBeTruthy(); |
| 1016 | expect(overlay.style.display).toBe('none'); |
| 1017 | |
| 1018 | player.setCurrentTime(2.5); |
| 1019 | player.trigger('timeupdate'); |
| 1020 | await flushAsyncWork(window, 8); |
| 1021 | |
| 1022 | expect(window.fetch.mock.calls.length).toBe(afterForwardSeekCalls); |
| 1023 | expect(overlay.style.display).toBe('block'); |
| 1024 | |
| 1025 | controller.detach(); |
| 1026 | await flushAsyncWork(window, 2); |
| 1027 | window.close(); |
| 1028 | }); |
| 1029 | |
| 1030 | test('registers stream cache service worker once when supported', async () => |
| 1031 | { |
| 1032 | const window = createWindow(); |
| 1033 | const helper = loadSharedVideoPlayer(window); |
| 1034 | const register = jest.fn(async () => ({})); |
| 1035 | window.navigator.serviceWorker = { register }; |
| 1036 | |
| 1037 | const first = await helper.enableVideoStreamClientCache(); |
| 1038 | const second = await helper.enableVideoStreamClientCache(); |
| 1039 | |
| 1040 | expect(first).toBe(true); |
| 1041 | expect(second).toBe(true); |
| 1042 | expect(register).toHaveBeenCalledTimes(1); |
| 1043 | expect(register).toHaveBeenCalledWith('/stream-cache-sw.js', { |
| 1044 | scope: '/', |
| 1045 | updateViaCache: 'none', |
| 1046 | }); |
| 1047 | |
| 1048 | window.close(); |
| 1049 | }); |
| 1050 | |
| 1051 | test('stream-ahead prefetch follows playlists and fetches segment assets', async () => |
| 1052 | { |
| 1053 | const window = createWindow(); |
| 1054 | const helper = loadSharedVideoPlayer(window); |
| 1055 | const requestedUrls = []; |
| 1056 | |
| 1057 | window.fetch = jest.fn(async (input) => |
| 1058 | { |
| 1059 | const url = String(input && input.url ? input.url : input); |
| 1060 | requestedUrls.push(url); |
| 1061 | |
| 1062 | if (url === 'http://localhost/master.m3u8') |
| 1063 | { |
| 1064 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1065 | status: 200, |
| 1066 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1067 | }); |
| 1068 | } |
| 1069 | |
| 1070 | if (url === 'http://localhost/stream_0.m3u8') |
| 1071 | { |
| 1072 | return new Response('#EXTM3U\n#EXTINF:4,\nseg0.ts\n#EXTINF:4,\nseg1.ts\n#EXT-X-ENDLIST\n', { |
| 1073 | status: 200, |
| 1074 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1075 | }); |
| 1076 | } |
| 1077 | |
| 1078 | if (url === 'http://localhost/seg0.ts' || url === 'http://localhost/seg1.ts') |
| 1079 | { |
| 1080 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1081 | status: 200, |
| 1082 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1083 | }); |
| 1084 | } |
| 1085 | |
| 1086 | return new Response(null, { status: 404 }); |
| 1087 | }); |
| 1088 | |
| 1089 | const controller = helper.bindStreamAheadPrefetch({ |
| 1090 | playlistUrl: 'http://localhost/master.m3u8', |
| 1091 | pollIntervalMs: 0, |
| 1092 | credentials: 'same-origin', |
| 1093 | }); |
| 1094 | |
| 1095 | expect(controller).toBeTruthy(); |
| 1096 | await controller.done(); |
| 1097 | |
| 1098 | expect(requestedUrls).toEqual(expect.arrayContaining([ |
| 1099 | 'http://localhost/master.m3u8', |
| 1100 | 'http://localhost/stream_0.m3u8', |
| 1101 | 'http://localhost/seg0.ts', |
| 1102 | 'http://localhost/seg1.ts', |
| 1103 | ])); |
| 1104 | |
| 1105 | controller.stop(); |
| 1106 | window.close(); |
| 1107 | }); |
| 1108 | |
| 1109 | test('stream-ahead prefetch reports cached-through progress for fetched segments', async () => |
| 1110 | { |
| 1111 | const window = createWindow(); |
| 1112 | const helper = loadSharedVideoPlayer(window); |
| 1113 | const progressSamples = []; |
| 1114 | |
| 1115 | window.fetch = jest.fn(async (input) => |
| 1116 | { |
| 1117 | const url = String(input && input.url ? input.url : input); |
| 1118 | |
| 1119 | if (url === 'http://localhost/master.m3u8') |
| 1120 | { |
| 1121 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1122 | status: 200, |
| 1123 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1124 | }); |
| 1125 | } |
| 1126 | |
| 1127 | if (url === 'http://localhost/stream_0.m3u8') |
| 1128 | { |
| 1129 | return new Response('#EXTM3U\n#EXTINF:4,\nseg0.ts\n#EXTINF:4,\nseg1.ts\n#EXT-X-ENDLIST\n', { |
| 1130 | status: 200, |
| 1131 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1132 | }); |
| 1133 | } |
| 1134 | |
| 1135 | if (url === 'http://localhost/seg0.ts' || url === 'http://localhost/seg1.ts') |
| 1136 | { |
| 1137 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1138 | status: 200, |
| 1139 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1140 | }); |
| 1141 | } |
| 1142 | |
| 1143 | return new Response(null, { status: 404 }); |
| 1144 | }); |
| 1145 | |
| 1146 | const controller = helper.bindStreamAheadPrefetch({ |
| 1147 | playlistUrl: 'http://localhost/master.m3u8', |
| 1148 | pollIntervalMs: 0, |
| 1149 | credentials: 'same-origin', |
| 1150 | }); |
| 1151 | |
| 1152 | expect(controller).toBeTruthy(); |
| 1153 | const unsubscribe = controller.subscribe((status) => |
| 1154 | { |
| 1155 | progressSamples.push(status && status.cachedThroughSeconds); |
| 1156 | }); |
| 1157 | |
| 1158 | await controller.done(); |
| 1159 | unsubscribe(); |
| 1160 | |
| 1161 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 1162 | |
| 1163 | controller.stop(); |
| 1164 | window.close(); |
| 1165 | }); |
| 1166 | |
| 1167 | test('stream-ahead prefetch infers cached-through progress from segment filenames', async () => |
| 1168 | { |
| 1169 | const window = createWindow(); |
| 1170 | const helper = loadSharedVideoPlayer(window); |
| 1171 | const progressSamples = []; |
| 1172 | |
| 1173 | window.fetch = jest.fn(async (input) => |
| 1174 | { |
| 1175 | const url = String(input && input.url ? input.url : input); |
| 1176 | |
| 1177 | if (url === 'http://localhost/master.m3u8') |
| 1178 | { |
| 1179 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1180 | status: 200, |
| 1181 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1182 | }); |
| 1183 | } |
| 1184 | |
| 1185 | if (url === 'http://localhost/stream_0.m3u8') |
| 1186 | { |
| 1187 | return new Response('#EXTM3U\nstream_0_segment_000000.ts\nstream_0_segment_000001.ts\n#EXT-X-ENDLIST\n', { |
| 1188 | status: 200, |
| 1189 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1190 | }); |
| 1191 | } |
| 1192 | |
| 1193 | if (url === 'http://localhost/stream_0_segment_000000.ts' || url === 'http://localhost/stream_0_segment_000001.ts') |
| 1194 | { |
| 1195 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1196 | status: 200, |
| 1197 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1198 | }); |
| 1199 | } |
| 1200 | |
| 1201 | return new Response(null, { status: 404 }); |
| 1202 | }); |
| 1203 | |
| 1204 | const controller = helper.bindStreamAheadPrefetch({ |
| 1205 | playlistUrl: 'http://localhost/master.m3u8', |
| 1206 | pollIntervalMs: 0, |
| 1207 | credentials: 'same-origin', |
| 1208 | }); |
| 1209 | |
| 1210 | expect(controller).toBeTruthy(); |
| 1211 | const unsubscribe = controller.subscribe((status) => |
| 1212 | { |
| 1213 | progressSamples.push(status && status.cachedThroughSeconds); |
| 1214 | }); |
| 1215 | |
| 1216 | await controller.done(); |
| 1217 | unsubscribe(); |
| 1218 | |
| 1219 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 1220 | |
| 1221 | controller.stop(); |
| 1222 | window.close(); |
| 1223 | }); |
| 1224 | |
| 1225 | test('stream-ahead prefetch infers cached-through progress for m4s segment filenames', async () => |
| 1226 | { |
| 1227 | const window = createWindow(); |
| 1228 | const helper = loadSharedVideoPlayer(window); |
| 1229 | const progressSamples = []; |
| 1230 | |
| 1231 | window.fetch = jest.fn(async (input) => |
| 1232 | { |
| 1233 | const url = String(input && input.url ? input.url : input); |
| 1234 | |
| 1235 | if (url === 'http://localhost/master.m3u8') |
| 1236 | { |
| 1237 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1238 | status: 200, |
| 1239 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1240 | }); |
| 1241 | } |
| 1242 | |
| 1243 | if (url === 'http://localhost/stream_0.m3u8') |
| 1244 | { |
| 1245 | return new Response('#EXTM3U\nstream_0_segment_000000.m4s\nstream_0_segment_000001.m4s\n#EXT-X-ENDLIST\n', { |
| 1246 | status: 200, |
| 1247 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1248 | }); |
| 1249 | } |
| 1250 | |
| 1251 | if (url === 'http://localhost/stream_0_segment_000000.m4s' || url === 'http://localhost/stream_0_segment_000001.m4s') |
| 1252 | { |
| 1253 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1254 | status: 200, |
| 1255 | headers: { 'Content-Type': 'video/mp4' }, |
| 1256 | }); |
| 1257 | } |
| 1258 | |
| 1259 | return new Response(null, { status: 404 }); |
| 1260 | }); |
| 1261 | |
| 1262 | const controller = helper.bindStreamAheadPrefetch({ |
| 1263 | playlistUrl: 'http://localhost/master.m3u8', |
| 1264 | pollIntervalMs: 0, |
| 1265 | credentials: 'same-origin', |
| 1266 | }); |
| 1267 | |
| 1268 | expect(controller).toBeTruthy(); |
| 1269 | const unsubscribe = controller.subscribe((status) => |
| 1270 | { |
| 1271 | progressSamples.push(status && status.cachedThroughSeconds); |
| 1272 | }); |
| 1273 | |
| 1274 | await controller.done(); |
| 1275 | unsubscribe(); |
| 1276 | |
| 1277 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 1278 | |
| 1279 | controller.stop(); |
| 1280 | window.close(); |
| 1281 | }); |
| 1282 | |
| 1283 | test('stream-ahead prefetch reports ratio progress from fetched sections when timing is unknown', async () => |
| 1284 | { |
| 1285 | const window = createWindow(); |
| 1286 | const helper = loadSharedVideoPlayer(window); |
| 1287 | const statusSamples = []; |
| 1288 | |
| 1289 | window.fetch = jest.fn(async (input) => |
| 1290 | { |
| 1291 | const url = String(input && input.url ? input.url : input); |
| 1292 | |
| 1293 | if (url === 'http://localhost/master.m3u8') |
| 1294 | { |
| 1295 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1296 | status: 200, |
| 1297 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1298 | }); |
| 1299 | } |
| 1300 | |
| 1301 | if (url === 'http://localhost/stream_0.m3u8') |
| 1302 | { |
| 1303 | return new Response('#EXTM3U\nchunk-alpha.bin\nchunk-beta.bin\n#EXT-X-ENDLIST\n', { |
| 1304 | status: 200, |
| 1305 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1306 | }); |
| 1307 | } |
| 1308 | |
| 1309 | if (url === 'http://localhost/chunk-alpha.bin' || url === 'http://localhost/chunk-beta.bin') |
| 1310 | { |
| 1311 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1312 | status: 200, |
| 1313 | headers: { 'Content-Type': 'application/octet-stream' }, |
| 1314 | }); |
| 1315 | } |
| 1316 | |
| 1317 | return new Response(null, { status: 404 }); |
| 1318 | }); |
| 1319 | |
| 1320 | const controller = helper.bindStreamAheadPrefetch({ |
| 1321 | playlistUrl: 'http://localhost/master.m3u8', |
| 1322 | pollIntervalMs: 0, |
| 1323 | credentials: 'same-origin', |
| 1324 | }); |
| 1325 | |
| 1326 | expect(controller).toBeTruthy(); |
| 1327 | const unsubscribe = controller.subscribe((status) => |
| 1328 | { |
| 1329 | statusSamples.push(status || {}); |
| 1330 | }); |
| 1331 | |
| 1332 | await controller.done(); |
| 1333 | unsubscribe(); |
| 1334 | |
| 1335 | const finalStatus = statusSamples[statusSamples.length - 1] || {}; |
| 1336 | expect(finalStatus.totalSegmentCount).toBe(2); |
| 1337 | expect(finalStatus.fetchedSegmentCount).toBe(2); |
| 1338 | expect(finalStatus.cachedThroughRatio).toBe(1); |
| 1339 | |
| 1340 | controller.stop(); |
| 1341 | window.close(); |
| 1342 | }); |
| 1343 | |
| 1344 | test('clears cached stream assets for a finished playlist session', async () => |
| 1345 | { |
| 1346 | const window = createWindow(); |
| 1347 | const helper = loadSharedVideoPlayer(window); |
| 1348 | const requests = [ |
| 1349 | new window.Request('http://localhost/api/files/video-stream/sess-a/master.m3u8'), |
| 1350 | new window.Request('http://localhost/api/files/video-stream/sess-a/stream_0_segment_000001.ts'), |
| 1351 | new window.Request('http://localhost/api/files/video-stream/sess-b/master.m3u8'), |
| 1352 | ]; |
| 1353 | const deleteCalls = []; |
| 1354 | const cache = { |
| 1355 | keys: jest.fn(async () => requests), |
| 1356 | delete: jest.fn(async (request) => |
| 1357 | { |
| 1358 | deleteCalls.push(request.url); |
| 1359 | return true; |
| 1360 | }), |
| 1361 | }; |
| 1362 | window.caches = { |
| 1363 | open: jest.fn(async () => cache), |
| 1364 | }; |
| 1365 | |
| 1366 | await helper.clearVideoStreamClientCache('http://localhost/api/files/video-stream/sess-a/master.m3u8'); |
| 1367 | |
| 1368 | expect(window.caches.open).toHaveBeenCalledWith('filemanager-stream-cache-v1'); |
| 1369 | expect(cache.keys).toHaveBeenCalledTimes(1); |
| 1370 | expect(deleteCalls).toEqual(expect.arrayContaining([ |
| 1371 | 'http://localhost/api/files/video-stream/sess-a/master.m3u8', |
| 1372 | 'http://localhost/api/files/video-stream/sess-a/stream_0_segment_000001.ts', |
| 1373 | ])); |
| 1374 | expect(deleteCalls).not.toContain('http://localhost/api/files/video-stream/sess-b/master.m3u8'); |
| 1375 | |
| 1376 | window.close(); |
| 1377 | }); |
| 1378 | }); |