| 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 | |
| 71 | const player = { |
| 72 | ready (cb) |
| 73 | { |
| 74 | if (typeof cb === 'function') cb(); |
| 75 | }, |
| 76 | on (type, fn) |
| 77 | { |
| 78 | if (!events.has(type)) events.set(type, new Set()); |
| 79 | events.get(type).add(fn); |
| 80 | }, |
| 81 | trigger (type) |
| 82 | { |
| 83 | const listeners = events.get(type); |
| 84 | if (!listeners) return; |
| 85 | for (const fn of Array.from(listeners)) fn(); |
| 86 | }, |
| 87 | currentTime () |
| 88 | { |
| 89 | return currentTime; |
| 90 | }, |
| 91 | setCurrentTime (next) |
| 92 | { |
| 93 | currentTime = Number(next); |
| 94 | }, |
| 95 | duration (nextValue) |
| 96 | { |
| 97 | if (typeof nextValue === 'number') |
| 98 | { |
| 99 | durationSeconds = nextValue; |
| 100 | } |
| 101 | return durationSeconds; |
| 102 | }, |
| 103 | el () |
| 104 | { |
| 105 | return playerEl; |
| 106 | }, |
| 107 | getChild (name) |
| 108 | { |
| 109 | if (name !== 'controlBar') return null; |
| 110 | return { |
| 111 | addChild (componentName) |
| 112 | { |
| 113 | const Component = registeredComponents.get(componentName); |
| 114 | if (!Component) return null; |
| 115 | const instance = new Component(player); |
| 116 | playerEl.appendChild(instance.el()); |
| 117 | return instance; |
| 118 | }, |
| 119 | }; |
| 120 | }, |
| 121 | }; |
| 122 | |
| 123 | return { player, videojs }; |
| 124 | } |
| 125 | |
| 126 | function loadSharedVideoPlayer (window) |
| 127 | { |
| 128 | const script = fs.readFileSync(path.join(__dirname, '../public/shared-video-player.js'), 'utf8'); |
| 129 | window.eval(script); |
| 130 | return window.FilemanagerSharedVideoPlayer; |
| 131 | } |
| 132 | |
| 133 | describe('shared image subtitle prefetching', () => |
| 134 | { |
| 135 | function createWindow () |
| 136 | { |
| 137 | const dom = new JSDOM('<!doctype html><html><body></body></html>', { |
| 138 | url: 'http://localhost/', |
| 139 | runScripts: 'dangerously', |
| 140 | pretendToBeVisual: true, |
| 141 | }); |
| 142 | |
| 143 | dom.window.AbortController = AbortController; |
| 144 | dom.window.Response = Response; |
| 145 | dom.window.Headers = Headers; |
| 146 | dom.window.Request = Request; |
| 147 | dom.window.URL.createObjectURL = () => 'blob:subtitle'; |
| 148 | dom.window.URL.revokeObjectURL = () => { }; |
| 149 | |
| 150 | return dom.window; |
| 151 | } |
| 152 | |
| 153 | test('prefetches lookahead frames even when no subtitle is currently active', async () => |
| 154 | { |
| 155 | const window = createWindow(); |
| 156 | const helper = loadSharedVideoPlayer(window); |
| 157 | const { player, videojs } = buildPlayerHarness(window); |
| 158 | const requestedTimes = []; |
| 159 | |
| 160 | window.fetch = jest.fn(async (url) => |
| 161 | { |
| 162 | const parsed = new URL(url, 'http://localhost/'); |
| 163 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 164 | return new Response(null, { |
| 165 | status: 204, |
| 166 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 167 | }); |
| 168 | }); |
| 169 | |
| 170 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 171 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 172 | }, { |
| 173 | lookaheadSeconds: 1, |
| 174 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 175 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 176 | }), |
| 177 | }); |
| 178 | |
| 179 | await flushAsyncWork(window); |
| 180 | |
| 181 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1])); |
| 182 | |
| 183 | window.close(); |
| 184 | }); |
| 185 | |
| 186 | test('does not restart current subtitle request while one is in flight', async () => |
| 187 | { |
| 188 | const window = createWindow(); |
| 189 | const helper = loadSharedVideoPlayer(window); |
| 190 | const { player, videojs } = buildPlayerHarness(window); |
| 191 | const pendingResolvers = []; |
| 192 | const requestTimes = []; |
| 193 | |
| 194 | window.fetch = jest.fn((url, init = {}) => |
| 195 | { |
| 196 | const parsed = new URL(url, 'http://localhost/'); |
| 197 | requestTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 198 | |
| 199 | if (pendingResolvers.length === 0) |
| 200 | { |
| 201 | return new Promise((resolve) => |
| 202 | { |
| 203 | pendingResolvers.push(() => resolve(new Response(null, { |
| 204 | status: 204, |
| 205 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 206 | }))); |
| 207 | }); |
| 208 | } |
| 209 | |
| 210 | expect(init.signal && init.signal.aborted).toBe(false); |
| 211 | return Promise.resolve(new Response(null, { |
| 212 | status: 204, |
| 213 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 214 | })); |
| 215 | }); |
| 216 | |
| 217 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 218 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 219 | }, { |
| 220 | lookaheadSeconds: 0, |
| 221 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 222 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 223 | }), |
| 224 | }); |
| 225 | |
| 226 | await flushAsyncWork(window, 3); |
| 227 | expect(window.fetch).toHaveBeenCalledTimes(1); |
| 228 | |
| 229 | player.setCurrentTime(1.5); |
| 230 | player.trigger('timeupdate'); |
| 231 | player.setCurrentTime(2.25); |
| 232 | player.trigger('timeupdate'); |
| 233 | await flushAsyncWork(window, 2); |
| 234 | |
| 235 | expect(window.fetch).toHaveBeenCalledTimes(1); |
| 236 | |
| 237 | pendingResolvers[0](); |
| 238 | await flushAsyncWork(window, 5); |
| 239 | |
| 240 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 241 | expect(requestTimes).toEqual(expect.arrayContaining([0, 2.25])); |
| 242 | |
| 243 | window.close(); |
| 244 | }); |
| 245 | |
| 246 | test('aborts in-flight prefetch requests when the overlay is detached', async () => |
| 247 | { |
| 248 | const window = createWindow(); |
| 249 | const helper = loadSharedVideoPlayer(window); |
| 250 | const { player, videojs } = buildPlayerHarness(window); |
| 251 | const activeSignals = []; |
| 252 | |
| 253 | window.fetch = jest.fn((url, init = {}) => |
| 254 | { |
| 255 | const parsed = new URL(url, 'http://localhost/'); |
| 256 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 257 | |
| 258 | if (timeSeconds === 0) |
| 259 | { |
| 260 | return Promise.resolve(new Response(null, { |
| 261 | status: 204, |
| 262 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 263 | })); |
| 264 | } |
| 265 | |
| 266 | activeSignals.push(init.signal || null); |
| 267 | |
| 268 | return new Promise((resolve, reject) => |
| 269 | { |
| 270 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 271 | { |
| 272 | init.signal.addEventListener('abort', () => |
| 273 | { |
| 274 | reject(new DOMException('Aborted', 'AbortError')); |
| 275 | }, { once: true }); |
| 276 | } |
| 277 | }); |
| 278 | }); |
| 279 | |
| 280 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 281 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 282 | }, { |
| 283 | lookaheadSeconds: 1, |
| 284 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 285 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 286 | }), |
| 287 | }); |
| 288 | |
| 289 | await flushAsyncWork(window, 3); |
| 290 | expect(activeSignals.length).toBeGreaterThan(0); |
| 291 | |
| 292 | controller.detach(); |
| 293 | await flushAsyncWork(window, 2); |
| 294 | |
| 295 | expect(activeSignals.every(signal => signal && signal.aborted)).toBe(true); |
| 296 | |
| 297 | window.close(); |
| 298 | }); |
| 299 | |
| 300 | test('logs current subtitle request lifecycle events and flushes them on detach', async () => |
| 301 | { |
| 302 | const window = createWindow(); |
| 303 | const helper = loadSharedVideoPlayer(window); |
| 304 | const { player, videojs } = buildPlayerHarness(window); |
| 305 | const sentBatches = []; |
| 306 | |
| 307 | window.fetch = jest.fn(async () => |
| 308 | { |
| 309 | return new Response('png-data', { |
| 310 | status: 200, |
| 311 | headers: { |
| 312 | 'Content-Type': 'image/png', |
| 313 | 'X-Subtitle-Frame-Status': 'ok', |
| 314 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 315 | 'X-Subtitle-Response-Sent-At-Ms': '1012', |
| 316 | 'X-Subtitle-Frame-Start-Time': '1.5', |
| 317 | 'X-Subtitle-Frame-End-Time': '3.2', |
| 318 | 'X-Subtitle-Frame-Width': '320', |
| 319 | 'X-Subtitle-Frame-Height': '180', |
| 320 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 321 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 322 | }, |
| 323 | }); |
| 324 | }); |
| 325 | |
| 326 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 327 | sessionId: 'sess-1', |
| 328 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 329 | }, { |
| 330 | lookaheadSeconds: 0, |
| 331 | sendSubtitleLogBatch: (entries) => |
| 332 | { |
| 333 | sentBatches.push(entries); |
| 334 | return Promise.resolve(); |
| 335 | }, |
| 336 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 337 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 338 | }), |
| 339 | }); |
| 340 | |
| 341 | await flushAsyncWork(window, 8); |
| 342 | controller.detach(); |
| 343 | await flushAsyncWork(window, 4); |
| 344 | |
| 345 | const flattened = sentBatches.flat(); |
| 346 | expect(flattened.map((entry) => entry.eventType)).toEqual(expect.arrayContaining([ |
| 347 | 'subtitle_png_requested', |
| 348 | 'subtitle_png_sent', |
| 349 | 'subtitle_png_received', |
| 350 | ])); |
| 351 | |
| 352 | window.close(); |
| 353 | }); |
| 354 | |
| 355 | test('logs prefetch subtitle lifecycle events', async () => |
| 356 | { |
| 357 | const window = createWindow(); |
| 358 | const helper = loadSharedVideoPlayer(window); |
| 359 | const { player, videojs } = buildPlayerHarness(window); |
| 360 | const sentBatches = []; |
| 361 | |
| 362 | window.fetch = jest.fn(async () => |
| 363 | { |
| 364 | return new Response(null, { |
| 365 | status: 204, |
| 366 | headers: { |
| 367 | 'X-Subtitle-Frame-Status': 'none', |
| 368 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 369 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 370 | 'X-Subtitle-Frame-Start-Time': '2', |
| 371 | 'X-Subtitle-Frame-End-Time': '3', |
| 372 | }, |
| 373 | }); |
| 374 | }); |
| 375 | |
| 376 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 377 | sessionId: 'sess-1', |
| 378 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 379 | }, { |
| 380 | lookaheadSeconds: 1, |
| 381 | sendSubtitleLogBatch: (entries) => |
| 382 | { |
| 383 | sentBatches.push(entries); |
| 384 | return Promise.resolve(); |
| 385 | }, |
| 386 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 387 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 388 | }), |
| 389 | }); |
| 390 | |
| 391 | await flushAsyncWork(window, 8); |
| 392 | controller.detach(); |
| 393 | await flushAsyncWork(window, 4); |
| 394 | |
| 395 | const flattened = sentBatches.flat(); |
| 396 | const prefetchEvents = flattened.filter((entry) => entry.requestKind === 'prefetch').map((entry) => entry.eventType); |
| 397 | expect(prefetchEvents).toEqual(expect.arrayContaining([ |
| 398 | 'subtitle_png_requested', |
| 399 | 'subtitle_png_sent', |
| 400 | 'subtitle_png_not_available', |
| 401 | ])); |
| 402 | |
| 403 | window.close(); |
| 404 | }); |
| 405 | |
| 406 | test('renders requested and received subtitle markers on the seek bar', async () => |
| 407 | { |
| 408 | const window = createWindow(); |
| 409 | const helper = loadSharedVideoPlayer(window); |
| 410 | const { player, videojs } = buildPlayerHarness(window); |
| 411 | |
| 412 | window.fetch = jest.fn(async () => |
| 413 | { |
| 414 | return new Response('png-data', { |
| 415 | status: 200, |
| 416 | headers: { |
| 417 | 'Content-Type': 'image/png', |
| 418 | 'X-Subtitle-Frame-Status': 'ok', |
| 419 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 420 | 'X-Subtitle-Response-Sent-At-Ms': '1012', |
| 421 | 'X-Subtitle-Frame-Start-Time': '2.5', |
| 422 | 'X-Subtitle-Frame-End-Time': '3.5', |
| 423 | 'X-Subtitle-Frame-Width': '320', |
| 424 | 'X-Subtitle-Frame-Height': '180', |
| 425 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 426 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 427 | }, |
| 428 | }); |
| 429 | }); |
| 430 | |
| 431 | player.duration(10); |
| 432 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 433 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 434 | }, { |
| 435 | lookaheadSeconds: 0, |
| 436 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 437 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 438 | }), |
| 439 | }); |
| 440 | |
| 441 | await flushAsyncWork(window, 8); |
| 442 | |
| 443 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 444 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 445 | expect(requestedMarker).toBeTruthy(); |
| 446 | expect(receivedMarker).toBeTruthy(); |
| 447 | expect(requestedMarker.style.display).toBe('block'); |
| 448 | expect(receivedMarker.style.display).toBe('block'); |
| 449 | expect(requestedMarker.style.left).toBe('25.0000%'); |
| 450 | expect(receivedMarker.style.left).toBe('25.0000%'); |
| 451 | |
| 452 | controller.detach(); |
| 453 | await flushAsyncWork(window, 2); |
| 454 | |
| 455 | window.close(); |
| 456 | }); |
| 457 | }); |