| 1 | 'use strict'; |
| 2 | |
| 3 | const fs = require('fs'); |
| 4 | const path = require('path'); |
| 5 | const { JSDOM } = require('jsdom'); |
| 6 | |
| 7 | function flushAsyncWork (window, rounds = 6) |
| 8 | { |
| 9 | let chain = Promise.resolve(); |
| 10 | for (let i = 0; i < rounds; i++) |
| 11 | { |
| 12 | chain = chain.then(() => new Promise((resolve) => window.setTimeout(resolve, 0))); |
| 13 | } |
| 14 | return chain; |
| 15 | } |
| 16 | |
| 17 | function buildPlayerHarness (window) |
| 18 | { |
| 19 | const registeredComponents = new Map(); |
| 20 | |
| 21 | class MockButton |
| 22 | { |
| 23 | constructor(player) |
| 24 | { |
| 25 | this._player = player; |
| 26 | this._el = null; |
| 27 | } |
| 28 | |
| 29 | player () |
| 30 | { |
| 31 | return this._player; |
| 32 | } |
| 33 | |
| 34 | buildCSSClass () |
| 35 | { |
| 36 | return 'vjs-control'; |
| 37 | } |
| 38 | |
| 39 | createEl () |
| 40 | { |
| 41 | const el = window.document.createElement('button'); |
| 42 | el.className = this.buildCSSClass(); |
| 43 | return el; |
| 44 | } |
| 45 | |
| 46 | el () |
| 47 | { |
| 48 | if (!this._el) this._el = this.createEl(); |
| 49 | return this._el; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | registeredComponents.set('Button', MockButton); |
| 54 | |
| 55 | const videojs = { |
| 56 | getComponent: (name) => registeredComponents.get(name), |
| 57 | registerComponent: (name, component) => |
| 58 | { |
| 59 | registeredComponents.set(name, component); |
| 60 | }, |
| 61 | }; |
| 62 | |
| 63 | const events = new Map(); |
| 64 | const playerEl = window.document.createElement('div'); |
| 65 | const progressControlEl = window.document.createElement('div'); |
| 66 | progressControlEl.className = 'vjs-progress-control'; |
| 67 | playerEl.appendChild(progressControlEl); |
| 68 | let currentTime = 0; |
| 69 | let durationSeconds = 10; |
| 70 | let bufferedEndSeconds = null; |
| 71 | |
| 72 | const player = { |
| 73 | ready (cb) |
| 74 | { |
| 75 | if (typeof cb === 'function') cb(); |
| 76 | }, |
| 77 | on (type, fn) |
| 78 | { |
| 79 | if (!events.has(type)) events.set(type, new Set()); |
| 80 | events.get(type).add(fn); |
| 81 | }, |
| 82 | trigger (type) |
| 83 | { |
| 84 | const listeners = events.get(type); |
| 85 | if (!listeners) return; |
| 86 | for (const fn of Array.from(listeners)) fn(); |
| 87 | }, |
| 88 | currentTime () |
| 89 | { |
| 90 | return currentTime; |
| 91 | }, |
| 92 | setCurrentTime (next) |
| 93 | { |
| 94 | currentTime = Number(next); |
| 95 | }, |
| 96 | duration (nextValue) |
| 97 | { |
| 98 | if (typeof nextValue === 'number') |
| 99 | { |
| 100 | durationSeconds = nextValue; |
| 101 | } |
| 102 | return durationSeconds; |
| 103 | }, |
| 104 | bufferedEnd () |
| 105 | { |
| 106 | return bufferedEndSeconds; |
| 107 | }, |
| 108 | setBufferedEnd (next) |
| 109 | { |
| 110 | bufferedEndSeconds = Number.isFinite(next) ? Number(next) : null; |
| 111 | }, |
| 112 | el () |
| 113 | { |
| 114 | return playerEl; |
| 115 | }, |
| 116 | getChild (name) |
| 117 | { |
| 118 | if (name !== 'controlBar') return null; |
| 119 | return { |
| 120 | addChild (componentName) |
| 121 | { |
| 122 | const Component = registeredComponents.get(componentName); |
| 123 | if (!Component) return null; |
| 124 | const instance = new Component(player); |
| 125 | playerEl.appendChild(instance.el()); |
| 126 | return instance; |
| 127 | }, |
| 128 | }; |
| 129 | }, |
| 130 | }; |
| 131 | |
| 132 | return { player, videojs }; |
| 133 | } |
| 134 | |
| 135 | function loadSharedVideoPlayer (window) |
| 136 | { |
| 137 | const script = fs.readFileSync(path.join(__dirname, '../public/shared-video-player.js'), 'utf8'); |
| 138 | window.eval(script); |
| 139 | return window.FilemanagerSharedVideoPlayer; |
| 140 | } |
| 141 | |
| 142 | describe('shared image subtitle prefetching', () => |
| 143 | { |
| 144 | function createWindow () |
| 145 | { |
| 146 | const dom = new JSDOM('<!doctype html><html><body></body></html>', { |
| 147 | url: 'http://localhost/', |
| 148 | runScripts: 'dangerously', |
| 149 | pretendToBeVisual: true, |
| 150 | }); |
| 151 | |
| 152 | dom.window.AbortController = AbortController; |
| 153 | dom.window.Response = Response; |
| 154 | dom.window.Headers = Headers; |
| 155 | dom.window.Request = Request; |
| 156 | dom.window.URL.createObjectURL = () => 'blob:subtitle'; |
| 157 | dom.window.URL.revokeObjectURL = () => { }; |
| 158 | |
| 159 | return dom.window; |
| 160 | } |
| 161 | |
| 162 | test('prefetches lookahead frames even when no subtitle is currently active', async () => |
| 163 | { |
| 164 | const window = createWindow(); |
| 165 | const helper = loadSharedVideoPlayer(window); |
| 166 | const { player, videojs } = buildPlayerHarness(window); |
| 167 | const requestedTimes = []; |
| 168 | |
| 169 | window.fetch = jest.fn(async (url) => |
| 170 | { |
| 171 | const parsed = new URL(url, 'http://localhost/'); |
| 172 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 173 | return new Response(null, { |
| 174 | status: 204, |
| 175 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 176 | }); |
| 177 | }); |
| 178 | |
| 179 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 180 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 181 | }, { |
| 182 | lookaheadSeconds: 1, |
| 183 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 184 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 185 | }), |
| 186 | }); |
| 187 | |
| 188 | await flushAsyncWork(window); |
| 189 | |
| 190 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1])); |
| 191 | |
| 192 | window.close(); |
| 193 | }); |
| 194 | |
| 195 | test('prefetches subtitle frames through known video duration even with short lookahead', async () => |
| 196 | { |
| 197 | const window = createWindow(); |
| 198 | const helper = loadSharedVideoPlayer(window); |
| 199 | const { player, videojs } = buildPlayerHarness(window); |
| 200 | const requestedTimes = []; |
| 201 | |
| 202 | player.duration(3); |
| 203 | |
| 204 | window.fetch = jest.fn(async (url) => |
| 205 | { |
| 206 | const parsed = new URL(url, 'http://localhost/'); |
| 207 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 208 | return new Response(null, { |
| 209 | status: 204, |
| 210 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 211 | }); |
| 212 | }); |
| 213 | |
| 214 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 215 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 216 | }, { |
| 217 | lookaheadSeconds: 0.5, |
| 218 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 219 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 220 | }), |
| 221 | }); |
| 222 | |
| 223 | await flushAsyncWork(window, 18); |
| 224 | |
| 225 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3])); |
| 226 | |
| 227 | window.close(); |
| 228 | }); |
| 229 | |
| 230 | test('prefetch expands to video end when duration metadata appears later', async () => |
| 231 | { |
| 232 | const window = createWindow(); |
| 233 | const helper = loadSharedVideoPlayer(window); |
| 234 | const { player, videojs } = buildPlayerHarness(window); |
| 235 | const requestedTimes = []; |
| 236 | |
| 237 | player.duration(Number.NaN); |
| 238 | |
| 239 | window.fetch = jest.fn(async (url) => |
| 240 | { |
| 241 | const parsed = new URL(url, 'http://localhost/'); |
| 242 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 243 | return new Response(null, { |
| 244 | status: 204, |
| 245 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 246 | }); |
| 247 | }); |
| 248 | |
| 249 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 250 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 251 | }, { |
| 252 | lookaheadSeconds: 0.5, |
| 253 | initialPrefetchSeconds: 0, |
| 254 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 255 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 256 | }), |
| 257 | }); |
| 258 | |
| 259 | await flushAsyncWork(window, 6); |
| 260 | expect(requestedTimes).toContain(0); |
| 261 | |
| 262 | requestedTimes.length = 0; |
| 263 | player.duration(4); |
| 264 | player.trigger('durationchange'); |
| 265 | await flushAsyncWork(window, 14); |
| 266 | |
| 267 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4])); |
| 268 | |
| 269 | window.close(); |
| 270 | }); |
| 271 | |
| 272 | test('prefetches at least through the buffered video end', async () => |
| 273 | { |
| 274 | const window = createWindow(); |
| 275 | const helper = loadSharedVideoPlayer(window); |
| 276 | const { player, videojs } = buildPlayerHarness(window); |
| 277 | const requestedTimes = []; |
| 278 | |
| 279 | player.setBufferedEnd(3); |
| 280 | |
| 281 | window.fetch = jest.fn(async (url) => |
| 282 | { |
| 283 | const parsed = new URL(url, 'http://localhost/'); |
| 284 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 285 | return new Response(null, { |
| 286 | status: 204, |
| 287 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 288 | }); |
| 289 | }); |
| 290 | |
| 291 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 292 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 293 | }, { |
| 294 | lookaheadSeconds: 0.5, |
| 295 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 296 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 297 | }), |
| 298 | }); |
| 299 | |
| 300 | await flushAsyncWork(window, 12); |
| 301 | |
| 302 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3])); |
| 303 | |
| 304 | window.close(); |
| 305 | }); |
| 306 | |
| 307 | test('prefetch respects configured larger prefetch step size', async () => |
| 308 | { |
| 309 | const window = createWindow(); |
| 310 | const helper = loadSharedVideoPlayer(window); |
| 311 | const { player, videojs } = buildPlayerHarness(window); |
| 312 | const requestedTimes = []; |
| 313 | |
| 314 | player.duration(6); |
| 315 | |
| 316 | window.fetch = jest.fn(async (url) => |
| 317 | { |
| 318 | const parsed = new URL(url, 'http://localhost/'); |
| 319 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 320 | return new Response(null, { |
| 321 | status: 204, |
| 322 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 323 | }); |
| 324 | }); |
| 325 | |
| 326 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 327 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 328 | }, { |
| 329 | lookaheadSeconds: 0, |
| 330 | initialPrefetchSeconds: 0, |
| 331 | prefetchStepSeconds: 2, |
| 332 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 333 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 334 | }), |
| 335 | }); |
| 336 | |
| 337 | await flushAsyncWork(window, 14); |
| 338 | |
| 339 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 2, 4, 6])); |
| 340 | |
| 341 | window.close(); |
| 342 | }); |
| 343 | |
| 344 | test('adaptive prefetch step increases stride when cache lead grows', async () => |
| 345 | { |
| 346 | const window = createWindow(); |
| 347 | const helper = loadSharedVideoPlayer(window); |
| 348 | const { player, videojs } = buildPlayerHarness(window); |
| 349 | const requestedTimes = []; |
| 350 | |
| 351 | player.duration(40); |
| 352 | |
| 353 | window.fetch = jest.fn(async (url) => |
| 354 | { |
| 355 | const parsed = new URL(url, 'http://localhost/'); |
| 356 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 357 | return new Response(null, { |
| 358 | status: 204, |
| 359 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 360 | }); |
| 361 | }); |
| 362 | |
| 363 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 364 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 365 | }, { |
| 366 | lookaheadSeconds: 20, |
| 367 | initialPrefetchSeconds: 0, |
| 368 | prefetchAdaptiveStepEnabled: true, |
| 369 | prefetchStepSecondsMin: 1, |
| 370 | prefetchStepSecondsMax: 5, |
| 371 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 372 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 373 | }), |
| 374 | }); |
| 375 | |
| 376 | await flushAsyncWork(window, 20); |
| 377 | |
| 378 | const strideSamples = []; |
| 379 | for (let i = 1; i < requestedTimes.length; i++) |
| 380 | { |
| 381 | const stride = requestedTimes[i] - requestedTimes[i - 1]; |
| 382 | if (Number.isFinite(stride) && stride > 0) strideSamples.push(Number(stride.toFixed(3))); |
| 383 | } |
| 384 | |
| 385 | expect(strideSamples.some((stride) => stride > 1.25)).toBe(true); |
| 386 | |
| 387 | window.close(); |
| 388 | }); |
| 389 | |
| 390 | test('prefetch uses furthest buffered end when player.bufferedEnd under-reports', async () => |
| 391 | { |
| 392 | const window = createWindow(); |
| 393 | const helper = loadSharedVideoPlayer(window); |
| 394 | const { player, videojs } = buildPlayerHarness(window); |
| 395 | const requestedTimes = []; |
| 396 | |
| 397 | player.setBufferedEnd(0); |
| 398 | player.tech = () => ({ |
| 399 | el: () => ({ |
| 400 | buffered: { |
| 401 | length: 1, |
| 402 | end: () => 6, |
| 403 | }, |
| 404 | }), |
| 405 | }); |
| 406 | |
| 407 | window.fetch = jest.fn(async (url) => |
| 408 | { |
| 409 | const parsed = new URL(url, 'http://localhost/'); |
| 410 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 411 | return new Response(null, { |
| 412 | status: 204, |
| 413 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 414 | }); |
| 415 | }); |
| 416 | |
| 417 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 418 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 419 | }, { |
| 420 | lookaheadSeconds: 0, |
| 421 | initialPrefetchSeconds: 0, |
| 422 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 423 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 424 | }), |
| 425 | }); |
| 426 | |
| 427 | await flushAsyncWork(window, 16); |
| 428 | |
| 429 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6])); |
| 430 | |
| 431 | window.close(); |
| 432 | }); |
| 433 | |
| 434 | test('prefetch frontier jumps to current playback time instead of replaying stale lag', async () => |
| 435 | { |
| 436 | const window = createWindow(); |
| 437 | const helper = loadSharedVideoPlayer(window); |
| 438 | const { player, videojs } = buildPlayerHarness(window); |
| 439 | const requestedTimes = []; |
| 440 | |
| 441 | player.setBufferedEnd(35); |
| 442 | |
| 443 | window.fetch = jest.fn(async (url) => |
| 444 | { |
| 445 | const parsed = new URL(url, 'http://localhost/'); |
| 446 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 447 | requestedTimes.push(timeSeconds); |
| 448 | return new Response(null, { |
| 449 | status: 204, |
| 450 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 451 | }); |
| 452 | }); |
| 453 | |
| 454 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 455 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 456 | }, { |
| 457 | lookaheadSeconds: 0, |
| 458 | initialPrefetchSeconds: 0, |
| 459 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 460 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 461 | }), |
| 462 | }); |
| 463 | |
| 464 | await flushAsyncWork(window, 8); |
| 465 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5])); |
| 466 | |
| 467 | requestedTimes.length = 0; |
| 468 | player.setCurrentTime(20); |
| 469 | player.setBufferedEnd(40); |
| 470 | player.trigger('timeupdate'); |
| 471 | await flushAsyncWork(window, 8); |
| 472 | |
| 473 | expect(requestedTimes).toContain(20); |
| 474 | const prefetchAfterJump = requestedTimes.filter((value) => value > 20); |
| 475 | expect(prefetchAfterJump.length).toBeGreaterThan(0); |
| 476 | expect(Math.min(...prefetchAfterJump)).toBeGreaterThanOrEqual(20.5); |
| 477 | |
| 478 | window.close(); |
| 479 | }); |
| 480 | |
| 481 | test('does not restart current subtitle request while one is in flight', async () => |
| 482 | { |
| 483 | const window = createWindow(); |
| 484 | const helper = loadSharedVideoPlayer(window); |
| 485 | const { player, videojs } = buildPlayerHarness(window); |
| 486 | const pendingResolvers = []; |
| 487 | const requestTimes = []; |
| 488 | |
| 489 | window.fetch = jest.fn((url, init = {}) => |
| 490 | { |
| 491 | const parsed = new URL(url, 'http://localhost/'); |
| 492 | requestTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 493 | |
| 494 | if (pendingResolvers.length === 0) |
| 495 | { |
| 496 | return new Promise((resolve) => |
| 497 | { |
| 498 | pendingResolvers.push(() => resolve(new Response(null, { |
| 499 | status: 204, |
| 500 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 501 | }))); |
| 502 | }); |
| 503 | } |
| 504 | |
| 505 | expect(init.signal && init.signal.aborted).toBe(false); |
| 506 | return Promise.resolve(new Response(null, { |
| 507 | status: 204, |
| 508 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 509 | })); |
| 510 | }); |
| 511 | |
| 512 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 513 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 514 | }, { |
| 515 | lookaheadSeconds: 0, |
| 516 | initialPrefetchSeconds: 0, |
| 517 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 518 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 519 | }), |
| 520 | }); |
| 521 | |
| 522 | await flushAsyncWork(window, 3); |
| 523 | expect(window.fetch).toHaveBeenCalledTimes(1); |
| 524 | |
| 525 | player.setCurrentTime(1.5); |
| 526 | player.trigger('timeupdate'); |
| 527 | player.setCurrentTime(2.25); |
| 528 | player.trigger('timeupdate'); |
| 529 | await flushAsyncWork(window, 2); |
| 530 | |
| 531 | expect(window.fetch).toHaveBeenCalledTimes(1); |
| 532 | |
| 533 | pendingResolvers[0](); |
| 534 | await flushAsyncWork(window, 5); |
| 535 | |
| 536 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 537 | expect(requestTimes).toEqual(expect.arrayContaining([0, 2.25])); |
| 538 | |
| 539 | window.close(); |
| 540 | }); |
| 541 | |
| 542 | test('renders a prefetched frame for the current paused time without waiting for another timeupdate', async () => |
| 543 | { |
| 544 | const window = createWindow(); |
| 545 | const helper = loadSharedVideoPlayer(window); |
| 546 | const { player, videojs } = buildPlayerHarness(window); |
| 547 | |
| 548 | player.duration(120); |
| 549 | player.setCurrentTime(118.95); |
| 550 | |
| 551 | window.fetch = jest.fn(async (url) => |
| 552 | { |
| 553 | const parsed = new URL(url, 'http://localhost/'); |
| 554 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 555 | const headers = { |
| 556 | 'Content-Type': 'image/png', |
| 557 | }; |
| 558 | |
| 559 | if (Math.abs(timeSeconds - 118.95) < 0.001) |
| 560 | { |
| 561 | headers['X-Subtitle-Frame-Start-Time'] = '118.90'; |
| 562 | headers['X-Subtitle-Frame-End-Time'] = '119.00'; |
| 563 | headers['X-Subtitle-Frame-Width'] = '640'; |
| 564 | headers['X-Subtitle-Frame-Height'] = '80'; |
| 565 | headers['X-Subtitle-Frame-Canvas-Width'] = '1920'; |
| 566 | headers['X-Subtitle-Frame-Canvas-Height'] = '1080'; |
| 567 | return new Response(new Uint8Array([137, 80, 78, 71]), { status: 200, headers }); |
| 568 | } |
| 569 | |
| 570 | return new Response(null, { |
| 571 | status: 204, |
| 572 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 573 | }); |
| 574 | }); |
| 575 | |
| 576 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 577 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 578 | }, { |
| 579 | currentFetchEnabled: false, |
| 580 | cacheMissFallbackEnabled: false, |
| 581 | lookaheadSeconds: 0, |
| 582 | initialPrefetchSeconds: 0, |
| 583 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 584 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 585 | }), |
| 586 | }); |
| 587 | |
| 588 | await flushAsyncWork(window, 10); |
| 589 | |
| 590 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 591 | const overlayImg = overlay && overlay.querySelector('img'); |
| 592 | expect(overlay).toBeTruthy(); |
| 593 | expect(overlayImg).toBeTruthy(); |
| 594 | expect(overlay.style.display).toBe('block'); |
| 595 | expect(overlayImg.getAttribute('src')).toBe('blob:subtitle'); |
| 596 | |
| 597 | window.close(); |
| 598 | }); |
| 599 | |
| 600 | test('aborts in-flight prefetch requests when the overlay is detached', async () => |
| 601 | { |
| 602 | const window = createWindow(); |
| 603 | const helper = loadSharedVideoPlayer(window); |
| 604 | const { player, videojs } = buildPlayerHarness(window); |
| 605 | const activeSignals = []; |
| 606 | |
| 607 | window.fetch = jest.fn((url, init = {}) => |
| 608 | { |
| 609 | const parsed = new URL(url, 'http://localhost/'); |
| 610 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 611 | |
| 612 | if (timeSeconds === 0) |
| 613 | { |
| 614 | return Promise.resolve(new Response(null, { |
| 615 | status: 204, |
| 616 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 617 | })); |
| 618 | } |
| 619 | |
| 620 | activeSignals.push(init.signal || null); |
| 621 | |
| 622 | return new Promise((resolve, reject) => |
| 623 | { |
| 624 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 625 | { |
| 626 | init.signal.addEventListener('abort', () => |
| 627 | { |
| 628 | reject(new DOMException('Aborted', 'AbortError')); |
| 629 | }, { once: true }); |
| 630 | } |
| 631 | }); |
| 632 | }); |
| 633 | |
| 634 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 635 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 636 | }, { |
| 637 | lookaheadSeconds: 1, |
| 638 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 639 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 640 | }), |
| 641 | }); |
| 642 | |
| 643 | await flushAsyncWork(window, 3); |
| 644 | expect(activeSignals.length).toBeGreaterThan(0); |
| 645 | |
| 646 | controller.detach(); |
| 647 | await flushAsyncWork(window, 2); |
| 648 | |
| 649 | expect(activeSignals.every(signal => signal && signal.aborted)).toBe(true); |
| 650 | |
| 651 | window.close(); |
| 652 | }); |
| 653 | |
| 654 | test('logs current subtitle request lifecycle events and flushes them on detach', async () => |
| 655 | { |
| 656 | const window = createWindow(); |
| 657 | const helper = loadSharedVideoPlayer(window); |
| 658 | const { player, videojs } = buildPlayerHarness(window); |
| 659 | const sentBatches = []; |
| 660 | |
| 661 | window.fetch = jest.fn(async () => |
| 662 | { |
| 663 | return new Response('png-data', { |
| 664 | status: 200, |
| 665 | headers: { |
| 666 | 'Content-Type': 'image/png', |
| 667 | 'X-Subtitle-Frame-Status': 'ok', |
| 668 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 669 | 'X-Subtitle-Response-Sent-At-Ms': '1012', |
| 670 | 'X-Subtitle-Frame-Start-Time': '1.5', |
| 671 | 'X-Subtitle-Frame-End-Time': '3.2', |
| 672 | 'X-Subtitle-Frame-Width': '320', |
| 673 | 'X-Subtitle-Frame-Height': '180', |
| 674 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 675 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 676 | }, |
| 677 | }); |
| 678 | }); |
| 679 | |
| 680 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 681 | sessionId: 'sess-1', |
| 682 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 683 | }, { |
| 684 | lookaheadSeconds: 0, |
| 685 | sendSubtitleLogBatch: (entries) => |
| 686 | { |
| 687 | sentBatches.push(entries); |
| 688 | return Promise.resolve(); |
| 689 | }, |
| 690 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 691 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 692 | }), |
| 693 | }); |
| 694 | |
| 695 | await flushAsyncWork(window, 8); |
| 696 | controller.detach(); |
| 697 | await flushAsyncWork(window, 4); |
| 698 | |
| 699 | const flattened = sentBatches.flat(); |
| 700 | expect(flattened.map((entry) => entry.eventType)).toEqual(expect.arrayContaining([ |
| 701 | 'subtitle_png_requested', |
| 702 | 'subtitle_png_sent', |
| 703 | 'subtitle_png_received', |
| 704 | ])); |
| 705 | |
| 706 | window.close(); |
| 707 | }); |
| 708 | |
| 709 | test('logs prefetch subtitle lifecycle events', async () => |
| 710 | { |
| 711 | const window = createWindow(); |
| 712 | const helper = loadSharedVideoPlayer(window); |
| 713 | const { player, videojs } = buildPlayerHarness(window); |
| 714 | const sentBatches = []; |
| 715 | |
| 716 | window.fetch = jest.fn(async () => |
| 717 | { |
| 718 | return new Response(null, { |
| 719 | status: 204, |
| 720 | headers: { |
| 721 | 'X-Subtitle-Frame-Status': 'none', |
| 722 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 723 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 724 | 'X-Subtitle-Frame-Start-Time': '2', |
| 725 | 'X-Subtitle-Frame-End-Time': '3', |
| 726 | }, |
| 727 | }); |
| 728 | }); |
| 729 | |
| 730 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 731 | sessionId: 'sess-1', |
| 732 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 733 | }, { |
| 734 | lookaheadSeconds: 1, |
| 735 | sendSubtitleLogBatch: (entries) => |
| 736 | { |
| 737 | sentBatches.push(entries); |
| 738 | return Promise.resolve(); |
| 739 | }, |
| 740 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 741 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 742 | }), |
| 743 | }); |
| 744 | |
| 745 | await flushAsyncWork(window, 8); |
| 746 | controller.detach(); |
| 747 | await flushAsyncWork(window, 4); |
| 748 | |
| 749 | const flattened = sentBatches.flat(); |
| 750 | const prefetchEvents = flattened.filter((entry) => entry.requestKind === 'prefetch').map((entry) => entry.eventType); |
| 751 | expect(prefetchEvents).toEqual(expect.arrayContaining([ |
| 752 | 'subtitle_png_requested', |
| 753 | 'subtitle_png_sent', |
| 754 | 'subtitle_png_not_available', |
| 755 | ])); |
| 756 | |
| 757 | window.close(); |
| 758 | }); |
| 759 | |
| 760 | test('cache-only current mode avoids direct current subtitle fetch requests', async () => |
| 761 | { |
| 762 | const window = createWindow(); |
| 763 | const helper = loadSharedVideoPlayer(window); |
| 764 | const { player, videojs } = buildPlayerHarness(window); |
| 765 | const sentBatches = []; |
| 766 | |
| 767 | player.setCurrentTime(8); |
| 768 | window.fetch = jest.fn(async () => |
| 769 | { |
| 770 | return new Response(null, { |
| 771 | status: 204, |
| 772 | headers: { |
| 773 | 'X-Subtitle-Frame-Status': 'none', |
| 774 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 775 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 776 | }, |
| 777 | }); |
| 778 | }); |
| 779 | |
| 780 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 781 | sessionId: 'sess-1', |
| 782 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 783 | }, { |
| 784 | currentFetchEnabled: false, |
| 785 | cacheMissFallbackEnabled: false, |
| 786 | lookaheadSeconds: 0, |
| 787 | sendSubtitleLogBatch: (entries) => |
| 788 | { |
| 789 | sentBatches.push(entries); |
| 790 | return Promise.resolve(); |
| 791 | }, |
| 792 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 793 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 794 | }), |
| 795 | }); |
| 796 | |
| 797 | await flushAsyncWork(window, 8); |
| 798 | controller.detach(); |
| 799 | await flushAsyncWork(window, 4); |
| 800 | |
| 801 | const flattened = sentBatches.flat(); |
| 802 | expect(flattened.length).toBeGreaterThan(0); |
| 803 | const requestKinds = new Set(flattened.map((entry) => entry.requestKind).filter(Boolean)); |
| 804 | expect(requestKinds.has('prefetch')).toBe(true); |
| 805 | expect(requestKinds.has('current')).toBe(false); |
| 806 | |
| 807 | window.close(); |
| 808 | }); |
| 809 | |
| 810 | test('cache-only mode falls back to throttled current fetch on cache miss', async () => |
| 811 | { |
| 812 | const window = createWindow(); |
| 813 | const helper = loadSharedVideoPlayer(window); |
| 814 | const { player, videojs } = buildPlayerHarness(window); |
| 815 | |
| 816 | let requestCount = 0; |
| 817 | window.fetch = jest.fn(async () => |
| 818 | { |
| 819 | requestCount += 1; |
| 820 | if (requestCount === 1) |
| 821 | { |
| 822 | return new Response(new Uint8Array([137, 80, 78, 71]), { |
| 823 | status: 200, |
| 824 | headers: { |
| 825 | 'Content-Type': 'image/png', |
| 826 | 'X-Subtitle-Frame-Start-Time': '2.00', |
| 827 | 'X-Subtitle-Frame-End-Time': '2.50', |
| 828 | 'X-Subtitle-Frame-Width': '600', |
| 829 | 'X-Subtitle-Frame-Height': '72', |
| 830 | 'X-Subtitle-Frame-Canvas-Width': '1920', |
| 831 | 'X-Subtitle-Frame-Canvas-Height': '1080', |
| 832 | }, |
| 833 | }); |
| 834 | } |
| 835 | |
| 836 | return new Response(null, { |
| 837 | status: 204, |
| 838 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 839 | }); |
| 840 | }); |
| 841 | |
| 842 | player.setCurrentTime(2.2); |
| 843 | |
| 844 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 845 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 846 | }, { |
| 847 | currentFetchEnabled: false, |
| 848 | cacheMissFallbackIntervalMs: 2000, |
| 849 | lookaheadSeconds: 0, |
| 850 | initialPrefetchSeconds: 0, |
| 851 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 852 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 853 | }), |
| 854 | }); |
| 855 | |
| 856 | await flushAsyncWork(window, 10); |
| 857 | |
| 858 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 859 | const overlayImg = overlay && overlay.querySelector('img'); |
| 860 | expect(window.fetch).toHaveBeenCalled(); |
| 861 | expect(overlay.style.display).toBe('block'); |
| 862 | expect(overlayImg.getAttribute('src')).toBe('blob:subtitle'); |
| 863 | |
| 864 | window.close(); |
| 865 | }); |
| 866 | |
| 867 | test('cache-miss fallback does not reset prefetch progress on repeated triggers', async () => |
| 868 | { |
| 869 | const window = createWindow(); |
| 870 | const helper = loadSharedVideoPlayer(window); |
| 871 | const { player, videojs } = buildPlayerHarness(window); |
| 872 | |
| 873 | let nowMs = 1000; |
| 874 | const realNow = window.Date.now; |
| 875 | window.Date.now = () => nowMs; |
| 876 | |
| 877 | let abortedCount = 0; |
| 878 | window.fetch = jest.fn((url, init = {}) => |
| 879 | { |
| 880 | return new Promise((resolve, reject) => |
| 881 | { |
| 882 | const signal = init && init.signal; |
| 883 | const onAbort = () => |
| 884 | { |
| 885 | abortedCount += 1; |
| 886 | const err = new Error('aborted'); |
| 887 | err.name = 'AbortError'; |
| 888 | reject(err); |
| 889 | }; |
| 890 | |
| 891 | if (signal && typeof signal.addEventListener === 'function') |
| 892 | { |
| 893 | if (signal.aborted) return onAbort(); |
| 894 | signal.addEventListener('abort', onAbort, { once: true }); |
| 895 | } |
| 896 | |
| 897 | window.setTimeout(() => |
| 898 | { |
| 899 | if (signal && signal.aborted) return; |
| 900 | resolve(new Response(null, { |
| 901 | status: 204, |
| 902 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 903 | })); |
| 904 | }, 80); |
| 905 | }); |
| 906 | }); |
| 907 | |
| 908 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 909 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 910 | }, { |
| 911 | currentFetchEnabled: false, |
| 912 | cacheMissFallbackIntervalMs: 20, |
| 913 | lookaheadSeconds: 2, |
| 914 | initialPrefetchSeconds: 0, |
| 915 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 916 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 917 | }), |
| 918 | }); |
| 919 | |
| 920 | await flushAsyncWork(window, 4); |
| 921 | |
| 922 | player.setCurrentTime(5); |
| 923 | player.trigger('timeupdate'); |
| 924 | nowMs += 40; |
| 925 | player.setCurrentTime(5.5); |
| 926 | player.trigger('timeupdate'); |
| 927 | nowMs += 40; |
| 928 | player.setCurrentTime(6); |
| 929 | player.trigger('timeupdate'); |
| 930 | |
| 931 | await flushAsyncWork(window, 12); |
| 932 | expect(abortedCount).toBe(0); |
| 933 | |
| 934 | window.Date.now = realNow; |
| 935 | controller.detach(); |
| 936 | window.close(); |
| 937 | }); |
| 938 | |
| 939 | test('cache-only mode performs initial warmup prefetch window on load', async () => |
| 940 | { |
| 941 | const window = createWindow(); |
| 942 | const helper = loadSharedVideoPlayer(window); |
| 943 | const { player, videojs } = buildPlayerHarness(window); |
| 944 | const requestedTimes = []; |
| 945 | |
| 946 | window.fetch = jest.fn(async (url) => |
| 947 | { |
| 948 | const parsed = new URL(url, 'http://localhost/'); |
| 949 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 950 | return new Response(null, { |
| 951 | status: 204, |
| 952 | headers: { |
| 953 | 'X-Subtitle-Frame-Status': 'none', |
| 954 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 955 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 956 | }, |
| 957 | }); |
| 958 | }); |
| 959 | |
| 960 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 961 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 962 | }, { |
| 963 | currentFetchEnabled: false, |
| 964 | lookaheadSeconds: 0, |
| 965 | initialPrefetchSeconds: 2, |
| 966 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 967 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 968 | }), |
| 969 | }); |
| 970 | |
| 971 | await flushAsyncWork(window, 12); |
| 972 | |
| 973 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2])); |
| 974 | |
| 975 | controller.detach(); |
| 976 | await flushAsyncWork(window, 2); |
| 977 | window.close(); |
| 978 | }); |
| 979 | |
| 980 | test('renders requested and received subtitle markers on the seek bar', async () => |
| 981 | { |
| 982 | const window = createWindow(); |
| 983 | const helper = loadSharedVideoPlayer(window); |
| 984 | const { player, videojs } = buildPlayerHarness(window); |
| 985 | |
| 986 | window.fetch = jest.fn(async () => |
| 987 | { |
| 988 | return new Response('png-data', { |
| 989 | status: 200, |
| 990 | headers: { |
| 991 | 'Content-Type': 'image/png', |
| 992 | 'X-Subtitle-Frame-Status': 'ok', |
| 993 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 994 | 'X-Subtitle-Response-Sent-At-Ms': '1012', |
| 995 | 'X-Subtitle-Frame-Start-Time': '2.5', |
| 996 | 'X-Subtitle-Frame-End-Time': '3.5', |
| 997 | 'X-Subtitle-Frame-Width': '320', |
| 998 | 'X-Subtitle-Frame-Height': '180', |
| 999 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1000 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1001 | }, |
| 1002 | }); |
| 1003 | }); |
| 1004 | |
| 1005 | player.duration(10); |
| 1006 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1007 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1008 | }, { |
| 1009 | lookaheadSeconds: 0, |
| 1010 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1011 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1012 | }), |
| 1013 | }); |
| 1014 | |
| 1015 | await flushAsyncWork(window, 8); |
| 1016 | |
| 1017 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 1018 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 1019 | expect(requestedMarker).toBeTruthy(); |
| 1020 | expect(receivedMarker).toBeTruthy(); |
| 1021 | expect(requestedMarker.style.display).toBe('block'); |
| 1022 | expect(receivedMarker.style.display).toBe('block'); |
| 1023 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 1024 | expect(receivedMarker.style.left).toBe('25.0000%'); |
| 1025 | expect(requestedMarker.textContent).toBe(']'); |
| 1026 | expect(receivedMarker.textContent).toBe('['); |
| 1027 | |
| 1028 | controller.detach(); |
| 1029 | await new Promise((resolve) => window.setTimeout(resolve, 300)); |
| 1030 | await flushAsyncWork(window, 2); |
| 1031 | |
| 1032 | window.close(); |
| 1033 | }); |
| 1034 | |
| 1035 | test('advances both seek markers when subtitle request returns 204 without frame timing headers', async () => |
| 1036 | { |
| 1037 | const window = createWindow(); |
| 1038 | const helper = loadSharedVideoPlayer(window); |
| 1039 | const { player, videojs } = buildPlayerHarness(window); |
| 1040 | |
| 1041 | player.duration(10); |
| 1042 | player.setCurrentTime(4); |
| 1043 | window.fetch = jest.fn(async () => |
| 1044 | { |
| 1045 | return new Response(null, { |
| 1046 | status: 204, |
| 1047 | headers: { |
| 1048 | 'X-Subtitle-Frame-Status': 'none', |
| 1049 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 1050 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 1051 | }, |
| 1052 | }); |
| 1053 | }); |
| 1054 | |
| 1055 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1056 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1057 | }, { |
| 1058 | lookaheadSeconds: 0, |
| 1059 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1060 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1061 | }), |
| 1062 | }); |
| 1063 | |
| 1064 | await flushAsyncWork(window, 8); |
| 1065 | |
| 1066 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 1067 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 1068 | expect(requestedMarker).toBeTruthy(); |
| 1069 | expect(receivedMarker).toBeTruthy(); |
| 1070 | expect(requestedMarker.style.display).toBe('block'); |
| 1071 | expect(receivedMarker.style.display).toBe('block'); |
| 1072 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 1073 | expect(receivedMarker.style.left).toBe('100.0000%'); |
| 1074 | expect(requestedMarker.textContent).toBe(']'); |
| 1075 | expect(receivedMarker.textContent).toBe('['); |
| 1076 | |
| 1077 | controller.detach(); |
| 1078 | await flushAsyncWork(window, 2); |
| 1079 | window.close(); |
| 1080 | }); |
| 1081 | |
| 1082 | test('keeps existing marker positions while the next request is still in flight', async () => |
| 1083 | { |
| 1084 | const window = createWindow(); |
| 1085 | const helper = loadSharedVideoPlayer(window); |
| 1086 | const { player, videojs } = buildPlayerHarness(window); |
| 1087 | let pendingResolve = null; |
| 1088 | |
| 1089 | player.duration(10); |
| 1090 | window.fetch = jest.fn((url) => |
| 1091 | { |
| 1092 | const parsed = new URL(url, 'http://localhost/'); |
| 1093 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1094 | |
| 1095 | if (timeSeconds === 0) |
| 1096 | { |
| 1097 | return Promise.resolve(new Response('png-data', { |
| 1098 | status: 200, |
| 1099 | headers: { |
| 1100 | 'Content-Type': 'image/png', |
| 1101 | 'X-Subtitle-Frame-Status': 'ok', |
| 1102 | 'X-Subtitle-Frame-Start-Time': '2', |
| 1103 | 'X-Subtitle-Frame-End-Time': '3', |
| 1104 | 'X-Subtitle-Frame-Width': '320', |
| 1105 | 'X-Subtitle-Frame-Height': '180', |
| 1106 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1107 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1108 | }, |
| 1109 | })); |
| 1110 | } |
| 1111 | |
| 1112 | return new Promise((resolve) => |
| 1113 | { |
| 1114 | pendingResolve = resolve; |
| 1115 | }); |
| 1116 | }); |
| 1117 | |
| 1118 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1119 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1120 | }, { |
| 1121 | lookaheadSeconds: 0, |
| 1122 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1123 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1124 | }), |
| 1125 | }); |
| 1126 | |
| 1127 | await flushAsyncWork(window, 8); |
| 1128 | |
| 1129 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 1130 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 1131 | expect(requestedMarker).toBeTruthy(); |
| 1132 | expect(receivedMarker).toBeTruthy(); |
| 1133 | expect(requestedMarker.style.left).toBe('0.0000%'); |
| 1134 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 1135 | |
| 1136 | player.setCurrentTime(1.5); |
| 1137 | player.trigger('timeupdate'); |
| 1138 | await flushAsyncWork(window, 2); |
| 1139 | |
| 1140 | expect(requestedMarker.style.left).toBe('0.0000%'); |
| 1141 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 1142 | |
| 1143 | pendingResolve(new Response(null, { |
| 1144 | status: 204, |
| 1145 | headers: { |
| 1146 | 'X-Subtitle-Frame-Status': 'none', |
| 1147 | }, |
| 1148 | })); |
| 1149 | |
| 1150 | await flushAsyncWork(window, 8); |
| 1151 | expect(requestedMarker.style.display).toBe('block'); |
| 1152 | expect(receivedMarker.style.display).toBe('block'); |
| 1153 | expect(requestedMarker.style.left).toBe('15.0000%'); |
| 1154 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 1155 | |
| 1156 | controller.detach(); |
| 1157 | await flushAsyncWork(window, 2); |
| 1158 | window.close(); |
| 1159 | }); |
| 1160 | |
| 1161 | test('keeps marker positions forward-only when later responses resolve with earlier times', async () => |
| 1162 | { |
| 1163 | const window = createWindow(); |
| 1164 | const helper = loadSharedVideoPlayer(window); |
| 1165 | const { player, videojs } = buildPlayerHarness(window); |
| 1166 | const requestedTimes = []; |
| 1167 | |
| 1168 | player.duration(12); |
| 1169 | window.fetch = jest.fn(async (url) => |
| 1170 | { |
| 1171 | const parsed = new URL(url, 'http://localhost/'); |
| 1172 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1173 | requestedTimes.push(timeSeconds); |
| 1174 | |
| 1175 | if (requestedTimes.length === 1) |
| 1176 | { |
| 1177 | return new Response('png-data', { |
| 1178 | status: 200, |
| 1179 | headers: { |
| 1180 | 'Content-Type': 'image/png', |
| 1181 | 'X-Subtitle-Frame-Status': 'ok', |
| 1182 | 'X-Subtitle-Frame-Start-Time': '4', |
| 1183 | 'X-Subtitle-Frame-End-Time': '5', |
| 1184 | 'X-Subtitle-Frame-Width': '320', |
| 1185 | 'X-Subtitle-Frame-Height': '180', |
| 1186 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1187 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1188 | }, |
| 1189 | }); |
| 1190 | } |
| 1191 | |
| 1192 | return new Response('png-data', { |
| 1193 | status: 200, |
| 1194 | headers: { |
| 1195 | 'Content-Type': 'image/png', |
| 1196 | 'X-Subtitle-Frame-Status': 'ok', |
| 1197 | 'X-Subtitle-Frame-Start-Time': '2', |
| 1198 | 'X-Subtitle-Frame-End-Time': '3', |
| 1199 | 'X-Subtitle-Frame-Width': '320', |
| 1200 | 'X-Subtitle-Frame-Height': '180', |
| 1201 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1202 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1203 | }, |
| 1204 | }); |
| 1205 | }); |
| 1206 | |
| 1207 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1208 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1209 | }, { |
| 1210 | lookaheadSeconds: 0, |
| 1211 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1212 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1213 | }), |
| 1214 | }); |
| 1215 | |
| 1216 | await flushAsyncWork(window, 8); |
| 1217 | |
| 1218 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 1219 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 1220 | expect(requestedMarker).toBeTruthy(); |
| 1221 | expect(receivedMarker).toBeTruthy(); |
| 1222 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 1223 | expect(receivedMarker.style.left).toBe('33.3333%'); |
| 1224 | |
| 1225 | player.setCurrentTime(1.5); |
| 1226 | player.trigger('timeupdate'); |
| 1227 | await flushAsyncWork(window, 8); |
| 1228 | |
| 1229 | // Second response reports an older start time, but markers stay monotonic. |
| 1230 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 1231 | expect(receivedMarker.style.left).toBe('33.3333%'); |
| 1232 | |
| 1233 | controller.detach(); |
| 1234 | await flushAsyncWork(window, 2); |
| 1235 | window.close(); |
| 1236 | }); |
| 1237 | |
| 1238 | test('continues current subtitle refreshes after a force-seek aborts the in-flight request', async () => |
| 1239 | { |
| 1240 | const window = createWindow(); |
| 1241 | const helper = loadSharedVideoPlayer(window); |
| 1242 | const { player, videojs } = buildPlayerHarness(window); |
| 1243 | const requestedTimes = []; |
| 1244 | |
| 1245 | window.fetch = jest.fn((url, init = {}) => |
| 1246 | { |
| 1247 | const parsed = new URL(url, 'http://localhost/'); |
| 1248 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1249 | requestedTimes.push(timeSeconds); |
| 1250 | |
| 1251 | if (requestedTimes.length === 1) |
| 1252 | { |
| 1253 | return new Promise((resolve, reject) => |
| 1254 | { |
| 1255 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 1256 | { |
| 1257 | init.signal.addEventListener('abort', () => |
| 1258 | { |
| 1259 | reject(new window.DOMException('Aborted', 'AbortError')); |
| 1260 | }, { once: true }); |
| 1261 | } |
| 1262 | }); |
| 1263 | } |
| 1264 | |
| 1265 | return Promise.resolve(new Response(null, { |
| 1266 | status: 204, |
| 1267 | headers: { |
| 1268 | 'X-Subtitle-Frame-Status': 'none', |
| 1269 | }, |
| 1270 | })); |
| 1271 | }); |
| 1272 | |
| 1273 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1274 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1275 | }, { |
| 1276 | lookaheadSeconds: 0, |
| 1277 | currentRequestTimeoutMs: 300, |
| 1278 | logToConsole: false, |
| 1279 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1280 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1281 | }), |
| 1282 | }); |
| 1283 | |
| 1284 | await flushAsyncWork(window, 4); |
| 1285 | const afterInitialLoadCalls = window.fetch.mock.calls.length; |
| 1286 | expect(afterInitialLoadCalls).toBeGreaterThan(0); |
| 1287 | |
| 1288 | player.setCurrentTime(5); |
| 1289 | player.trigger('seeking'); |
| 1290 | await flushAsyncWork(window, 20); |
| 1291 | |
| 1292 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 1293 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 5])); |
| 1294 | |
| 1295 | controller.detach(); |
| 1296 | await flushAsyncWork(window, 2); |
| 1297 | window.close(); |
| 1298 | }); |
| 1299 | |
| 1300 | test('recovers when a current subtitle request hangs and times out', async () => |
| 1301 | { |
| 1302 | const window = createWindow(); |
| 1303 | const helper = loadSharedVideoPlayer(window); |
| 1304 | const { player, videojs } = buildPlayerHarness(window); |
| 1305 | const requestedTimes = []; |
| 1306 | |
| 1307 | window.fetch = jest.fn((url, init = {}) => |
| 1308 | { |
| 1309 | const parsed = new URL(url, 'http://localhost/'); |
| 1310 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1311 | requestedTimes.push(timeSeconds); |
| 1312 | |
| 1313 | if (requestedTimes.length === 1) |
| 1314 | { |
| 1315 | return new Promise((resolve, reject) => |
| 1316 | { |
| 1317 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 1318 | { |
| 1319 | init.signal.addEventListener('abort', () => |
| 1320 | { |
| 1321 | reject(new window.DOMException('Aborted', 'AbortError')); |
| 1322 | }, { once: true }); |
| 1323 | } |
| 1324 | }); |
| 1325 | } |
| 1326 | |
| 1327 | return Promise.resolve(new Response(null, { |
| 1328 | status: 204, |
| 1329 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 1330 | })); |
| 1331 | }); |
| 1332 | |
| 1333 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1334 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1335 | }, { |
| 1336 | lookaheadSeconds: 0, |
| 1337 | currentRequestTimeoutMs: 250, |
| 1338 | logToConsole: false, |
| 1339 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1340 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1341 | }), |
| 1342 | }); |
| 1343 | |
| 1344 | await flushAsyncWork(window, 3); |
| 1345 | |
| 1346 | player.setCurrentTime(1.5); |
| 1347 | player.trigger('timeupdate'); |
| 1348 | |
| 1349 | await new Promise((resolve) => window.setTimeout(resolve, 320)); |
| 1350 | player.trigger('timeupdate'); |
| 1351 | await new Promise((resolve) => window.setTimeout(resolve, 220)); |
| 1352 | await flushAsyncWork(window, 20); |
| 1353 | |
| 1354 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 1355 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 1.5])); |
| 1356 | |
| 1357 | controller.detach(); |
| 1358 | await new Promise((resolve) => window.setTimeout(resolve, 200)); |
| 1359 | await flushAsyncWork(window, 20); |
| 1360 | window.close(); |
| 1361 | }); |
| 1362 | |
| 1363 | test('reuses cached subtitle frames after rewinding when server no longer has older frames', async () => |
| 1364 | { |
| 1365 | const window = createWindow(); |
| 1366 | const helper = loadSharedVideoPlayer(window); |
| 1367 | const { player, videojs } = buildPlayerHarness(window); |
| 1368 | |
| 1369 | player.duration(10); |
| 1370 | player.setCurrentTime(2); |
| 1371 | window.fetch = jest.fn(async (url) => |
| 1372 | { |
| 1373 | const parsed = new URL(url, 'http://localhost/'); |
| 1374 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1375 | |
| 1376 | if (Math.abs(timeSeconds - 2) < 1e-6) |
| 1377 | { |
| 1378 | return new Response('png-data', { |
| 1379 | status: 200, |
| 1380 | headers: { |
| 1381 | 'Content-Type': 'image/png', |
| 1382 | 'X-Subtitle-Frame-Status': 'ok', |
| 1383 | 'X-Subtitle-Frame-Start-Time': '2', |
| 1384 | 'X-Subtitle-Frame-End-Time': '3', |
| 1385 | 'X-Subtitle-Frame-Width': '320', |
| 1386 | 'X-Subtitle-Frame-Height': '180', |
| 1387 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1388 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1389 | }, |
| 1390 | }); |
| 1391 | } |
| 1392 | |
| 1393 | return new Response(null, { |
| 1394 | status: 204, |
| 1395 | headers: { |
| 1396 | 'X-Subtitle-Frame-Status': 'none', |
| 1397 | }, |
| 1398 | }); |
| 1399 | }); |
| 1400 | |
| 1401 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1402 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1403 | }, { |
| 1404 | lookaheadSeconds: 0, |
| 1405 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1406 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1407 | }), |
| 1408 | }); |
| 1409 | |
| 1410 | await flushAsyncWork(window, 8); |
| 1411 | const afterInitialLoadCalls = window.fetch.mock.calls.length; |
| 1412 | expect(afterInitialLoadCalls).toBeGreaterThan(0); |
| 1413 | |
| 1414 | player.setCurrentTime(4); |
| 1415 | player.trigger('timeupdate'); |
| 1416 | await flushAsyncWork(window, 8); |
| 1417 | const afterForwardSeekCalls = window.fetch.mock.calls.length; |
| 1418 | expect(afterForwardSeekCalls).toBeGreaterThanOrEqual(afterInitialLoadCalls); |
| 1419 | |
| 1420 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 1421 | expect(overlay).toBeTruthy(); |
| 1422 | expect(overlay.style.display).toBe('none'); |
| 1423 | |
| 1424 | player.setCurrentTime(2.5); |
| 1425 | player.trigger('timeupdate'); |
| 1426 | await flushAsyncWork(window, 8); |
| 1427 | |
| 1428 | expect(window.fetch.mock.calls.length).toBe(afterForwardSeekCalls); |
| 1429 | expect(overlay.style.display).toBe('block'); |
| 1430 | |
| 1431 | controller.detach(); |
| 1432 | await flushAsyncWork(window, 2); |
| 1433 | window.close(); |
| 1434 | }); |
| 1435 | |
| 1436 | test('registers stream cache service worker once when supported', async () => |
| 1437 | { |
| 1438 | const window = createWindow(); |
| 1439 | const helper = loadSharedVideoPlayer(window); |
| 1440 | const register = jest.fn(async () => ({})); |
| 1441 | window.navigator.serviceWorker = { register }; |
| 1442 | |
| 1443 | const first = await helper.enableVideoStreamClientCache(); |
| 1444 | const second = await helper.enableVideoStreamClientCache(); |
| 1445 | |
| 1446 | expect(first).toBe(true); |
| 1447 | expect(second).toBe(true); |
| 1448 | expect(register).toHaveBeenCalledTimes(1); |
| 1449 | expect(register).toHaveBeenCalledWith('/stream-cache-sw.js', { |
| 1450 | scope: '/', |
| 1451 | updateViaCache: 'none', |
| 1452 | }); |
| 1453 | |
| 1454 | window.close(); |
| 1455 | }); |
| 1456 | |
| 1457 | test('stream-ahead prefetch follows playlists and fetches segment assets', async () => |
| 1458 | { |
| 1459 | const window = createWindow(); |
| 1460 | const helper = loadSharedVideoPlayer(window); |
| 1461 | const requestedUrls = []; |
| 1462 | |
| 1463 | window.fetch = jest.fn(async (input) => |
| 1464 | { |
| 1465 | const url = String(input && input.url ? input.url : input); |
| 1466 | requestedUrls.push(url); |
| 1467 | |
| 1468 | if (url === 'http://localhost/master.m3u8') |
| 1469 | { |
| 1470 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1471 | status: 200, |
| 1472 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1473 | }); |
| 1474 | } |
| 1475 | |
| 1476 | if (url === 'http://localhost/stream_0.m3u8') |
| 1477 | { |
| 1478 | return new Response('#EXTM3U\n#EXTINF:4,\nseg0.ts\n#EXTINF:4,\nseg1.ts\n#EXT-X-ENDLIST\n', { |
| 1479 | status: 200, |
| 1480 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1481 | }); |
| 1482 | } |
| 1483 | |
| 1484 | if (url === 'http://localhost/seg0.ts' || url === 'http://localhost/seg1.ts') |
| 1485 | { |
| 1486 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1487 | status: 200, |
| 1488 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1489 | }); |
| 1490 | } |
| 1491 | |
| 1492 | return new Response(null, { status: 404 }); |
| 1493 | }); |
| 1494 | |
| 1495 | const controller = helper.bindStreamAheadPrefetch({ |
| 1496 | playlistUrl: 'http://localhost/master.m3u8', |
| 1497 | pollIntervalMs: 0, |
| 1498 | credentials: 'same-origin', |
| 1499 | }); |
| 1500 | |
| 1501 | expect(controller).toBeTruthy(); |
| 1502 | await controller.done(); |
| 1503 | |
| 1504 | expect(requestedUrls).toEqual(expect.arrayContaining([ |
| 1505 | 'http://localhost/master.m3u8', |
| 1506 | 'http://localhost/stream_0.m3u8', |
| 1507 | 'http://localhost/seg0.ts', |
| 1508 | 'http://localhost/seg1.ts', |
| 1509 | ])); |
| 1510 | |
| 1511 | controller.stop(); |
| 1512 | window.close(); |
| 1513 | }); |
| 1514 | |
| 1515 | test('stream-ahead prefetch reports cached-through progress for fetched segments', async () => |
| 1516 | { |
| 1517 | const window = createWindow(); |
| 1518 | const helper = loadSharedVideoPlayer(window); |
| 1519 | const progressSamples = []; |
| 1520 | |
| 1521 | window.fetch = jest.fn(async (input) => |
| 1522 | { |
| 1523 | const url = String(input && input.url ? input.url : input); |
| 1524 | |
| 1525 | if (url === 'http://localhost/master.m3u8') |
| 1526 | { |
| 1527 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1528 | status: 200, |
| 1529 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1530 | }); |
| 1531 | } |
| 1532 | |
| 1533 | if (url === 'http://localhost/stream_0.m3u8') |
| 1534 | { |
| 1535 | return new Response('#EXTM3U\n#EXTINF:4,\nseg0.ts\n#EXTINF:4,\nseg1.ts\n#EXT-X-ENDLIST\n', { |
| 1536 | status: 200, |
| 1537 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1538 | }); |
| 1539 | } |
| 1540 | |
| 1541 | if (url === 'http://localhost/seg0.ts' || url === 'http://localhost/seg1.ts') |
| 1542 | { |
| 1543 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1544 | status: 200, |
| 1545 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1546 | }); |
| 1547 | } |
| 1548 | |
| 1549 | return new Response(null, { status: 404 }); |
| 1550 | }); |
| 1551 | |
| 1552 | const controller = helper.bindStreamAheadPrefetch({ |
| 1553 | playlistUrl: 'http://localhost/master.m3u8', |
| 1554 | pollIntervalMs: 0, |
| 1555 | credentials: 'same-origin', |
| 1556 | }); |
| 1557 | |
| 1558 | expect(controller).toBeTruthy(); |
| 1559 | const unsubscribe = controller.subscribe((status) => |
| 1560 | { |
| 1561 | progressSamples.push(status && status.cachedThroughSeconds); |
| 1562 | }); |
| 1563 | |
| 1564 | await controller.done(); |
| 1565 | unsubscribe(); |
| 1566 | |
| 1567 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 1568 | |
| 1569 | controller.stop(); |
| 1570 | window.close(); |
| 1571 | }); |
| 1572 | |
| 1573 | test('stream-ahead prefetch infers cached-through progress from segment filenames', async () => |
| 1574 | { |
| 1575 | const window = createWindow(); |
| 1576 | const helper = loadSharedVideoPlayer(window); |
| 1577 | const progressSamples = []; |
| 1578 | |
| 1579 | window.fetch = jest.fn(async (input) => |
| 1580 | { |
| 1581 | const url = String(input && input.url ? input.url : input); |
| 1582 | |
| 1583 | if (url === 'http://localhost/master.m3u8') |
| 1584 | { |
| 1585 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1586 | status: 200, |
| 1587 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1588 | }); |
| 1589 | } |
| 1590 | |
| 1591 | if (url === 'http://localhost/stream_0.m3u8') |
| 1592 | { |
| 1593 | return new Response('#EXTM3U\nstream_0_segment_000000.ts\nstream_0_segment_000001.ts\n#EXT-X-ENDLIST\n', { |
| 1594 | status: 200, |
| 1595 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1596 | }); |
| 1597 | } |
| 1598 | |
| 1599 | if (url === 'http://localhost/stream_0_segment_000000.ts' || url === 'http://localhost/stream_0_segment_000001.ts') |
| 1600 | { |
| 1601 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1602 | status: 200, |
| 1603 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1604 | }); |
| 1605 | } |
| 1606 | |
| 1607 | return new Response(null, { status: 404 }); |
| 1608 | }); |
| 1609 | |
| 1610 | const controller = helper.bindStreamAheadPrefetch({ |
| 1611 | playlistUrl: 'http://localhost/master.m3u8', |
| 1612 | pollIntervalMs: 0, |
| 1613 | credentials: 'same-origin', |
| 1614 | }); |
| 1615 | |
| 1616 | expect(controller).toBeTruthy(); |
| 1617 | const unsubscribe = controller.subscribe((status) => |
| 1618 | { |
| 1619 | progressSamples.push(status && status.cachedThroughSeconds); |
| 1620 | }); |
| 1621 | |
| 1622 | await controller.done(); |
| 1623 | unsubscribe(); |
| 1624 | |
| 1625 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 1626 | |
| 1627 | controller.stop(); |
| 1628 | window.close(); |
| 1629 | }); |
| 1630 | |
| 1631 | test('stream-ahead prefetch infers cached-through progress for m4s segment filenames', async () => |
| 1632 | { |
| 1633 | const window = createWindow(); |
| 1634 | const helper = loadSharedVideoPlayer(window); |
| 1635 | const progressSamples = []; |
| 1636 | |
| 1637 | window.fetch = jest.fn(async (input) => |
| 1638 | { |
| 1639 | const url = String(input && input.url ? input.url : input); |
| 1640 | |
| 1641 | if (url === 'http://localhost/master.m3u8') |
| 1642 | { |
| 1643 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1644 | status: 200, |
| 1645 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1646 | }); |
| 1647 | } |
| 1648 | |
| 1649 | if (url === 'http://localhost/stream_0.m3u8') |
| 1650 | { |
| 1651 | return new Response('#EXTM3U\nstream_0_segment_000000.m4s\nstream_0_segment_000001.m4s\n#EXT-X-ENDLIST\n', { |
| 1652 | status: 200, |
| 1653 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1654 | }); |
| 1655 | } |
| 1656 | |
| 1657 | if (url === 'http://localhost/stream_0_segment_000000.m4s' || url === 'http://localhost/stream_0_segment_000001.m4s') |
| 1658 | { |
| 1659 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1660 | status: 200, |
| 1661 | headers: { 'Content-Type': 'video/mp4' }, |
| 1662 | }); |
| 1663 | } |
| 1664 | |
| 1665 | return new Response(null, { status: 404 }); |
| 1666 | }); |
| 1667 | |
| 1668 | const controller = helper.bindStreamAheadPrefetch({ |
| 1669 | playlistUrl: 'http://localhost/master.m3u8', |
| 1670 | pollIntervalMs: 0, |
| 1671 | credentials: 'same-origin', |
| 1672 | }); |
| 1673 | |
| 1674 | expect(controller).toBeTruthy(); |
| 1675 | const unsubscribe = controller.subscribe((status) => |
| 1676 | { |
| 1677 | progressSamples.push(status && status.cachedThroughSeconds); |
| 1678 | }); |
| 1679 | |
| 1680 | await controller.done(); |
| 1681 | unsubscribe(); |
| 1682 | |
| 1683 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 1684 | |
| 1685 | controller.stop(); |
| 1686 | window.close(); |
| 1687 | }); |
| 1688 | |
| 1689 | test('stream-ahead prefetch reports ratio progress from fetched sections when timing is unknown', async () => |
| 1690 | { |
| 1691 | const window = createWindow(); |
| 1692 | const helper = loadSharedVideoPlayer(window); |
| 1693 | const statusSamples = []; |
| 1694 | |
| 1695 | window.fetch = jest.fn(async (input) => |
| 1696 | { |
| 1697 | const url = String(input && input.url ? input.url : input); |
| 1698 | |
| 1699 | if (url === 'http://localhost/master.m3u8') |
| 1700 | { |
| 1701 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1702 | status: 200, |
| 1703 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1704 | }); |
| 1705 | } |
| 1706 | |
| 1707 | if (url === 'http://localhost/stream_0.m3u8') |
| 1708 | { |
| 1709 | return new Response('#EXTM3U\nchunk-alpha.bin\nchunk-beta.bin\n#EXT-X-ENDLIST\n', { |
| 1710 | status: 200, |
| 1711 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1712 | }); |
| 1713 | } |
| 1714 | |
| 1715 | if (url === 'http://localhost/chunk-alpha.bin' || url === 'http://localhost/chunk-beta.bin') |
| 1716 | { |
| 1717 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1718 | status: 200, |
| 1719 | headers: { 'Content-Type': 'application/octet-stream' }, |
| 1720 | }); |
| 1721 | } |
| 1722 | |
| 1723 | return new Response(null, { status: 404 }); |
| 1724 | }); |
| 1725 | |
| 1726 | const controller = helper.bindStreamAheadPrefetch({ |
| 1727 | playlistUrl: 'http://localhost/master.m3u8', |
| 1728 | pollIntervalMs: 0, |
| 1729 | credentials: 'same-origin', |
| 1730 | }); |
| 1731 | |
| 1732 | expect(controller).toBeTruthy(); |
| 1733 | const unsubscribe = controller.subscribe((status) => |
| 1734 | { |
| 1735 | statusSamples.push(status || {}); |
| 1736 | }); |
| 1737 | |
| 1738 | await controller.done(); |
| 1739 | unsubscribe(); |
| 1740 | |
| 1741 | const finalStatus = statusSamples[statusSamples.length - 1] || {}; |
| 1742 | expect(finalStatus.totalSegmentCount).toBe(2); |
| 1743 | expect(finalStatus.fetchedSegmentCount).toBe(2); |
| 1744 | expect(finalStatus.cachedThroughRatio).toBe(1); |
| 1745 | |
| 1746 | controller.stop(); |
| 1747 | window.close(); |
| 1748 | }); |
| 1749 | |
| 1750 | test('clears cached stream assets for a finished playlist session', async () => |
| 1751 | { |
| 1752 | const window = createWindow(); |
| 1753 | const helper = loadSharedVideoPlayer(window); |
| 1754 | const requests = [ |
| 1755 | new window.Request('http://localhost/api/files/video-stream/sess-a/master.m3u8'), |
| 1756 | new window.Request('http://localhost/api/files/video-stream/sess-a/stream_0_segment_000001.ts'), |
| 1757 | new window.Request('http://localhost/api/files/video-stream/sess-b/master.m3u8'), |
| 1758 | ]; |
| 1759 | const deleteCalls = []; |
| 1760 | const cache = { |
| 1761 | keys: jest.fn(async () => requests), |
| 1762 | delete: jest.fn(async (request) => |
| 1763 | { |
| 1764 | deleteCalls.push(request.url); |
| 1765 | return true; |
| 1766 | }), |
| 1767 | }; |
| 1768 | window.caches = { |
| 1769 | open: jest.fn(async () => cache), |
| 1770 | }; |
| 1771 | |
| 1772 | await helper.clearVideoStreamClientCache('http://localhost/api/files/video-stream/sess-a/master.m3u8'); |
| 1773 | |
| 1774 | expect(window.caches.open).toHaveBeenCalledWith('filemanager-stream-cache-v1'); |
| 1775 | expect(cache.keys).toHaveBeenCalledTimes(1); |
| 1776 | expect(deleteCalls).toEqual(expect.arrayContaining([ |
| 1777 | 'http://localhost/api/files/video-stream/sess-a/master.m3u8', |
| 1778 | 'http://localhost/api/files/video-stream/sess-a/stream_0_segment_000001.ts', |
| 1779 | ])); |
| 1780 | expect(deleteCalls).not.toContain('http://localhost/api/files/video-stream/sess-b/master.m3u8'); |
| 1781 | |
| 1782 | window.close(); |
| 1783 | }); |
| 1784 | }); |