| 1 | 'use strict'; |
| 2 | |
| 3 | (function () |
| 4 | { |
| 5 | const VIDEO_TEXT_TRACK_SETTINGS_KEY = 'vjs-text-track-settings'; |
| 6 | const VIDEO_TEXT_TRACK_OVERRIDE_SETTINGS_RAW = JSON.stringify({ |
| 7 | backgroundOpacity: '0', |
| 8 | edgeStyle: 'uniform', |
| 9 | }); |
| 10 | |
| 11 | function normaliseTrackName (name) |
| 12 | { |
| 13 | return String(name || '').trim().toLowerCase(); |
| 14 | } |
| 15 | |
| 16 | function getTrackName (track) |
| 17 | { |
| 18 | if (!track || typeof track !== 'object') return ''; |
| 19 | const label = String(track.label || '').trim(); |
| 20 | if (label) return label; |
| 21 | const name = String(track.name || '').trim(); |
| 22 | if (name) return name; |
| 23 | const language = String(track.language || '').trim(); |
| 24 | if (language) return language; |
| 25 | const id = String(track.id || '').trim(); |
| 26 | if (id) return id; |
| 27 | return ''; |
| 28 | } |
| 29 | |
| 30 | function trackListToArray (trackList) |
| 31 | { |
| 32 | if (!trackList || typeof trackList.length !== 'number') return []; |
| 33 | const out = []; |
| 34 | for (let i = 0; i < trackList.length; i++) |
| 35 | { |
| 36 | const track = trackList[i] || (typeof trackList.item === 'function' ? trackList.item(i) : null); |
| 37 | if (track) out.push(track); |
| 38 | } |
| 39 | return out; |
| 40 | } |
| 41 | |
| 42 | function subtitleTracksFromPlayer (player) |
| 43 | { |
| 44 | if (!player || typeof player.textTracks !== 'function') return []; |
| 45 | return trackListToArray(player.textTracks()).filter((track) => |
| 46 | { |
| 47 | const kind = String(track.kind || '').toLowerCase(); |
| 48 | return kind === 'subtitles' || kind === 'captions'; |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | function audioTracksFromPlayer (player) |
| 53 | { |
| 54 | if (!player || typeof player.audioTracks !== 'function') return []; |
| 55 | return trackListToArray(player.audioTracks()); |
| 56 | } |
| 57 | |
| 58 | function applySavedSubtitleTrack (player, savedName) |
| 59 | { |
| 60 | if (typeof savedName !== 'string') return; |
| 61 | const tracks = subtitleTracksFromPlayer(player); |
| 62 | if (!tracks.length) return; |
| 63 | |
| 64 | const wanted = normaliseTrackName(savedName); |
| 65 | if (!wanted) |
| 66 | { |
| 67 | for (const track of tracks) track.mode = 'disabled'; |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | const match = tracks.find((track) => normaliseTrackName(getTrackName(track)) === wanted); |
| 72 | if (!match) return; |
| 73 | |
| 74 | for (const track of tracks) |
| 75 | { |
| 76 | track.mode = track === match ? 'showing' : 'disabled'; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | function applySavedAudioTrack (player, savedName) |
| 81 | { |
| 82 | if (typeof savedName !== 'string') return; |
| 83 | const tracks = audioTracksFromPlayer(player); |
| 84 | if (!tracks.length) return; |
| 85 | |
| 86 | const wanted = normaliseTrackName(savedName); |
| 87 | if (!wanted) return; |
| 88 | |
| 89 | const match = tracks.find((track) => normaliseTrackName(getTrackName(track)) === wanted); |
| 90 | if (!match) return; |
| 91 | |
| 92 | for (const track of tracks) |
| 93 | { |
| 94 | track.enabled = track === match; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | function getSelectedSubtitleTrackName (player) |
| 99 | { |
| 100 | const tracks = subtitleTracksFromPlayer(player); |
| 101 | if (!tracks.length) return null; |
| 102 | const active = tracks.find((track) => String(track.mode || '').toLowerCase() === 'showing'); |
| 103 | return active ? getTrackName(active) : ''; |
| 104 | } |
| 105 | |
| 106 | function getSelectedAudioTrackName (player) |
| 107 | { |
| 108 | const tracks = audioTracksFromPlayer(player); |
| 109 | if (!tracks.length) return null; |
| 110 | const active = tracks.find((track) => !!track.enabled); |
| 111 | return active ? getTrackName(active) : ''; |
| 112 | } |
| 113 | |
| 114 | function bindTrackPrefs (player, prefs, savePrefs) |
| 115 | { |
| 116 | const save = typeof savePrefs === 'function' ? savePrefs : (() => {}); |
| 117 | const applySavedTracks = () => |
| 118 | { |
| 119 | applySavedSubtitleTrack(player, prefs.subtitleTrackName); |
| 120 | applySavedAudioTrack(player, prefs.audioTrackName); |
| 121 | }; |
| 122 | |
| 123 | const captureSelectedTracks = () => |
| 124 | { |
| 125 | const subtitleTrackName = getSelectedSubtitleTrackName(player); |
| 126 | const audioTrackName = getSelectedAudioTrackName(player); |
| 127 | const patch = {}; |
| 128 | |
| 129 | if (subtitleTrackName !== null) patch.subtitleTrackName = subtitleTrackName; |
| 130 | if (audioTrackName !== null) patch.audioTrackName = audioTrackName; |
| 131 | |
| 132 | if (Object.keys(patch).length) save(patch); |
| 133 | }; |
| 134 | |
| 135 | const textTrackList = typeof player.textTracks === 'function' ? player.textTracks() : null; |
| 136 | const audioTrackList = typeof player.audioTracks === 'function' ? player.audioTracks() : null; |
| 137 | const onTrackListChange = () => captureSelectedTracks(); |
| 138 | |
| 139 | if (textTrackList && typeof textTrackList.addEventListener === 'function') |
| 140 | { |
| 141 | textTrackList.addEventListener('change', onTrackListChange); |
| 142 | } |
| 143 | |
| 144 | if (audioTrackList && typeof audioTrackList.addEventListener === 'function') |
| 145 | { |
| 146 | audioTrackList.addEventListener('change', onTrackListChange); |
| 147 | } |
| 148 | |
| 149 | return { |
| 150 | applySavedTracks, |
| 151 | captureSelectedTracks, |
| 152 | detach: () => |
| 153 | { |
| 154 | if (textTrackList && typeof textTrackList.removeEventListener === 'function') |
| 155 | { |
| 156 | textTrackList.removeEventListener('change', onTrackListChange); |
| 157 | } |
| 158 | if (audioTrackList && typeof audioTrackList.removeEventListener === 'function') |
| 159 | { |
| 160 | audioTrackList.removeEventListener('change', onTrackListChange); |
| 161 | } |
| 162 | }, |
| 163 | }; |
| 164 | } |
| 165 | |
| 166 | function bindImageSubtitleOverlay (player, videojs, prefs, session, opts = {}) |
| 167 | { |
| 168 | const imageTracks = Array.isArray(session.imageSubtitleTracks) |
| 169 | ? session.imageSubtitleTracks.map((track) => ({ |
| 170 | label: String(track.label || '').trim() || `Subtitle ${Number(track.streamIndex || 0) + 1}`, |
| 171 | language: String(track.language || '').trim(), |
| 172 | isDefault: !!track.default, |
| 173 | streamIndex: Number(track.streamIndex), |
| 174 | })).filter((track) => Number.isInteger(track.streamIndex) && track.streamIndex >= 0) |
| 175 | : []; |
| 176 | |
| 177 | if (!imageTracks.length) return null; |
| 178 | |
| 179 | const savePrefs = typeof opts.savePrefs === 'function' ? opts.savePrefs : (() => {}); |
| 180 | const buildFrameRequest = typeof opts.buildFrameRequest === 'function' ? opts.buildFrameRequest : null; |
| 181 | const notifyError = typeof opts.onError === 'function' ? opts.onError : (() => {}); |
| 182 | if (!buildFrameRequest) return null; |
| 183 | |
| 184 | const trackByStreamIndex = new Map(imageTracks.map((track) => [track.streamIndex, track])); |
| 185 | let selectedStreamIndex = null; |
| 186 | let menuOpen = false; |
| 187 | let requestToken = 0; |
| 188 | let activeBucket = -1; |
| 189 | let activeObjectUrl = null; |
| 190 | let overlayEl = null; |
| 191 | let overlayImgEl = null; |
| 192 | let menuEl = null; |
| 193 | let buttonEl = null; |
| 194 | let trackDisabled = false; |
| 195 | let failureNotified = false; |
| 196 | |
| 197 | const closeObjectUrl = () => |
| 198 | { |
| 199 | if (activeObjectUrl) |
| 200 | { |
| 201 | URL.revokeObjectURL(activeObjectUrl); |
| 202 | activeObjectUrl = null; |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | const hideOverlay = () => |
| 207 | { |
| 208 | if (overlayEl) overlayEl.style.display = 'none'; |
| 209 | if (overlayImgEl) overlayImgEl.removeAttribute('src'); |
| 210 | closeObjectUrl(); |
| 211 | }; |
| 212 | |
| 213 | const updateButtonState = () => |
| 214 | { |
| 215 | if (!buttonEl) return; |
| 216 | const track = selectedStreamIndex === null ? null : trackByStreamIndex.get(selectedStreamIndex) || null; |
| 217 | buttonEl.setAttribute('title', track ? `Image subtitles: ${track.label}` : 'Image subtitles'); |
| 218 | buttonEl.setAttribute('aria-pressed', selectedStreamIndex === null ? 'false' : 'true'); |
| 219 | }; |
| 220 | |
| 221 | const updateMenuState = () => |
| 222 | { |
| 223 | if (!menuEl) return; |
| 224 | const items = menuEl.querySelectorAll('[data-image-subtitle-stream-index]'); |
| 225 | for (const itemEl of items) |
| 226 | { |
| 227 | const value = itemEl.getAttribute('data-image-subtitle-stream-index'); |
| 228 | const isSelected = value === (selectedStreamIndex === null ? 'off' : String(selectedStreamIndex)); |
| 229 | itemEl.classList.toggle('is-selected', isSelected); |
| 230 | itemEl.setAttribute('aria-checked', isSelected ? 'true' : 'false'); |
| 231 | itemEl.style.background = isSelected ? 'rgba(90, 196, 255, 0.28)' : 'transparent'; |
| 232 | itemEl.style.fontWeight = isSelected ? '600' : '400'; |
| 233 | itemEl.style.boxShadow = isSelected ? 'inset 0 0 0 1px rgba(90, 196, 255, 0.55)' : 'none'; |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | const closeMenu = () => |
| 238 | { |
| 239 | menuOpen = false; |
| 240 | if (menuEl) menuEl.style.display = 'none'; |
| 241 | }; |
| 242 | |
| 243 | const openMenu = () => |
| 244 | { |
| 245 | menuOpen = true; |
| 246 | if (menuEl) |
| 247 | { |
| 248 | updateMenuState(); |
| 249 | menuEl.style.display = 'block'; |
| 250 | } |
| 251 | }; |
| 252 | |
| 253 | const toggleMenu = () => |
| 254 | { |
| 255 | if (menuOpen) closeMenu(); |
| 256 | else openMenu(); |
| 257 | }; |
| 258 | |
| 259 | const saveSelection = () => |
| 260 | { |
| 261 | const selectedTrack = selectedStreamIndex === null ? null : trackByStreamIndex.get(selectedStreamIndex) || null; |
| 262 | savePrefs({ |
| 263 | imageSubtitleTrackStreamIndex: selectedStreamIndex, |
| 264 | imageSubtitleTrackName: selectedTrack ? selectedTrack.label : null, |
| 265 | }); |
| 266 | }; |
| 267 | |
| 268 | const disableTrack = () => |
| 269 | { |
| 270 | trackDisabled = true; |
| 271 | selectedStreamIndex = null; |
| 272 | activeBucket = -1; |
| 273 | hideOverlay(); |
| 274 | updateButtonState(); |
| 275 | updateMenuState(); |
| 276 | saveSelection(); |
| 277 | |
| 278 | if (!failureNotified) |
| 279 | { |
| 280 | failureNotified = true; |
| 281 | notifyError(); |
| 282 | } |
| 283 | }; |
| 284 | |
| 285 | const setSelectedStreamIndex = (streamIndex, { persist = true } = {}) => |
| 286 | { |
| 287 | const nextStreamIndex = Number.isInteger(streamIndex) ? streamIndex : null; |
| 288 | if (nextStreamIndex !== null && !trackByStreamIndex.has(nextStreamIndex)) return; |
| 289 | |
| 290 | trackDisabled = false; |
| 291 | failureNotified = false; |
| 292 | selectedStreamIndex = nextStreamIndex; |
| 293 | activeBucket = -1; |
| 294 | hideOverlay(); |
| 295 | updateButtonState(); |
| 296 | updateMenuState(); |
| 297 | if (persist) saveSelection(); |
| 298 | |
| 299 | if (selectedStreamIndex !== null) |
| 300 | { |
| 301 | void refreshFrame(true); |
| 302 | } |
| 303 | }; |
| 304 | |
| 305 | const refreshFrame = async (force = false) => |
| 306 | { |
| 307 | if (selectedStreamIndex === null) return; |
| 308 | if (!overlayEl || !overlayImgEl) return; |
| 309 | if (trackDisabled) return; |
| 310 | |
| 311 | const currentTime = Number(player.currentTime()); |
| 312 | if (!Number.isFinite(currentTime) || currentTime < 0) return; |
| 313 | |
| 314 | const bucket = Math.floor(currentTime * 4); |
| 315 | if (!force && bucket === activeBucket) return; |
| 316 | activeBucket = bucket; |
| 317 | |
| 318 | const token = ++requestToken; |
| 319 | const request = buildFrameRequest({ |
| 320 | streamIndex: selectedStreamIndex, |
| 321 | currentTime, |
| 322 | session, |
| 323 | }); |
| 324 | |
| 325 | if (!request || !request.url) |
| 326 | { |
| 327 | disableTrack(); |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | try |
| 332 | { |
| 333 | const res = await fetch(request.url, { |
| 334 | method: request.method || 'GET', |
| 335 | headers: request.headers, |
| 336 | credentials: request.credentials, |
| 337 | }); |
| 338 | |
| 339 | if (token !== requestToken) return; |
| 340 | |
| 341 | if (res.status === 204) |
| 342 | { |
| 343 | const frameStatus = String(res.headers.get('X-Subtitle-Frame-Status') || '').toLowerCase(); |
| 344 | hideOverlay(); |
| 345 | if (frameStatus === 'error') disableTrack(); |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | if (!res.ok) |
| 350 | { |
| 351 | disableTrack(); |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | const blob = await res.blob(); |
| 356 | if (token !== requestToken) return; |
| 357 | |
| 358 | if (!blob || blob.size === 0) |
| 359 | { |
| 360 | disableTrack(); |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | closeObjectUrl(); |
| 365 | activeObjectUrl = URL.createObjectURL(blob); |
| 366 | overlayImgEl.src = activeObjectUrl; |
| 367 | |
| 368 | const frameWidth = Number.parseInt(String(res.headers.get('X-Subtitle-Frame-Width') || ''), 10); |
| 369 | const frameHeight = Number.parseInt(String(res.headers.get('X-Subtitle-Frame-Height') || ''), 10); |
| 370 | const canvasWidth = Number.parseInt(String(res.headers.get('X-Subtitle-Frame-Canvas-Width') || ''), 10); |
| 371 | const canvasHeight = Number.parseInt(String(res.headers.get('X-Subtitle-Frame-Canvas-Height') || ''), 10); |
| 372 | |
| 373 | if (frameWidth > 0 && frameHeight > 0 && canvasWidth > 0 && canvasHeight > 0) |
| 374 | { |
| 375 | const widthPercent = (frameWidth / canvasWidth) * 100; |
| 376 | const heightPercent = (frameHeight / canvasHeight) * 100; |
| 377 | overlayImgEl.style.width = `${widthPercent}%`; |
| 378 | overlayImgEl.style.height = `${heightPercent}%`; |
| 379 | overlayImgEl.style.maxWidth = 'none'; |
| 380 | overlayImgEl.style.maxHeight = 'none'; |
| 381 | } else |
| 382 | { |
| 383 | overlayImgEl.style.width = 'auto'; |
| 384 | overlayImgEl.style.height = 'auto'; |
| 385 | overlayImgEl.style.maxWidth = '100%'; |
| 386 | overlayImgEl.style.maxHeight = '100%'; |
| 387 | } |
| 388 | |
| 389 | overlayEl.style.display = 'block'; |
| 390 | overlayImgEl.style.display = 'block'; |
| 391 | } catch |
| 392 | { |
| 393 | if (token === requestToken) disableTrack(); |
| 394 | } |
| 395 | }; |
| 396 | |
| 397 | const dispose = () => |
| 398 | { |
| 399 | requestToken++; |
| 400 | closeMenu(); |
| 401 | hideOverlay(); |
| 402 | if (overlayEl) overlayEl.remove(); |
| 403 | if (menuEl) menuEl.remove(); |
| 404 | player.imageSubtitleOverlayController = null; |
| 405 | buttonEl = null; |
| 406 | }; |
| 407 | |
| 408 | overlayEl = document.createElement('div'); |
| 409 | overlayEl.className = 'video-subtitle-overlay'; |
| 410 | overlayEl.style.position = 'absolute'; |
| 411 | overlayEl.style.left = '0'; |
| 412 | overlayEl.style.right = '0'; |
| 413 | overlayEl.style.bottom = '3rem'; |
| 414 | overlayEl.style.display = 'none'; |
| 415 | overlayEl.style.zIndex = '3'; |
| 416 | overlayEl.style.pointerEvents = 'none'; |
| 417 | overlayEl.style.textAlign = 'center'; |
| 418 | overlayEl.style.padding = '0 0.75rem'; |
| 419 | overlayEl.style.maxHeight = '70%'; |
| 420 | overlayEl.style.overflow = 'hidden'; |
| 421 | |
| 422 | overlayImgEl = document.createElement('img'); |
| 423 | overlayImgEl.alt = 'Image subtitles'; |
| 424 | overlayImgEl.style.display = 'none'; |
| 425 | overlayImgEl.style.margin = '0 auto'; |
| 426 | overlayImgEl.style.objectFit = 'contain'; |
| 427 | overlayImgEl.style.filter = 'drop-shadow(0 2px 2px rgba(0, 0, 0, 0.7))'; |
| 428 | overlayImgEl.style.maxWidth = '100%'; |
| 429 | overlayImgEl.style.height = 'auto'; |
| 430 | overlayEl.appendChild(overlayImgEl); |
| 431 | |
| 432 | menuEl = document.createElement('div'); |
| 433 | menuEl.className = 'video-subtitle-overlay-menu'; |
| 434 | menuEl.style.position = 'absolute'; |
| 435 | menuEl.style.right = '0.75rem'; |
| 436 | menuEl.style.bottom = 'calc(3rem + 0.5rem)'; |
| 437 | menuEl.style.minWidth = '14rem'; |
| 438 | menuEl.style.maxWidth = '20rem'; |
| 439 | menuEl.style.background = 'rgba(12, 12, 12, 0.96)'; |
| 440 | menuEl.style.border = '1px solid rgba(255, 255, 255, 0.15)'; |
| 441 | menuEl.style.borderRadius = '0.5rem'; |
| 442 | menuEl.style.boxShadow = '0 12px 28px rgba(0, 0, 0, 0.35)'; |
| 443 | menuEl.style.padding = '0.35rem'; |
| 444 | menuEl.style.display = 'none'; |
| 445 | menuEl.style.zIndex = '4'; |
| 446 | |
| 447 | const buildMenuItem = (label, streamIndex, isSelected) => |
| 448 | { |
| 449 | const itemEl = document.createElement('button'); |
| 450 | itemEl.type = 'button'; |
| 451 | itemEl.className = 'video-subtitle-overlay-menu-item'; |
| 452 | itemEl.setAttribute('data-image-subtitle-stream-index', streamIndex === null ? 'off' : String(streamIndex)); |
| 453 | itemEl.setAttribute('aria-checked', isSelected ? 'true' : 'false'); |
| 454 | itemEl.style.display = 'block'; |
| 455 | itemEl.style.width = '100%'; |
| 456 | itemEl.style.margin = '0'; |
| 457 | itemEl.style.padding = '0.5rem 0.75rem'; |
| 458 | itemEl.style.border = '0'; |
| 459 | itemEl.style.borderRadius = '0.35rem'; |
| 460 | itemEl.style.background = isSelected ? 'rgba(255, 255, 255, 0.18)' : 'transparent'; |
| 461 | itemEl.style.color = '#fff'; |
| 462 | itemEl.style.textAlign = 'left'; |
| 463 | itemEl.style.cursor = 'pointer'; |
| 464 | itemEl.textContent = label; |
| 465 | itemEl.addEventListener('click', () => |
| 466 | { |
| 467 | setSelectedStreamIndex(streamIndex); |
| 468 | closeMenu(); |
| 469 | }); |
| 470 | return itemEl; |
| 471 | }; |
| 472 | |
| 473 | menuEl.appendChild(buildMenuItem('Off', null, false)); |
| 474 | for (const track of imageTracks) |
| 475 | { |
| 476 | menuEl.appendChild(buildMenuItem(track.label, track.streamIndex, false)); |
| 477 | } |
| 478 | |
| 479 | const registerButton = () => |
| 480 | { |
| 481 | if (!videojs || typeof videojs.getComponent !== 'function' || typeof videojs.registerComponent !== 'function') return; |
| 482 | if (videojs.getComponent('ImageSubtitleButton')) return; |
| 483 | |
| 484 | const Button = videojs.getComponent('Button'); |
| 485 | class ImageSubtitleButton extends Button |
| 486 | { |
| 487 | buildCSSClass () |
| 488 | { |
| 489 | return `${super.buildCSSClass()} vjs-image-subtitle-button`; |
| 490 | } |
| 491 | |
| 492 | createEl () |
| 493 | { |
| 494 | const el = super.createEl(); |
| 495 | el.type = 'button'; |
| 496 | el.setAttribute('title', 'Image subtitles'); |
| 497 | el.setAttribute('aria-label', 'Image subtitles'); |
| 498 | el.innerHTML = '<span aria-hidden="true">IM</span>'; |
| 499 | return el; |
| 500 | } |
| 501 | |
| 502 | handleClick () |
| 503 | { |
| 504 | const controller = this.player().imageSubtitleOverlayController; |
| 505 | if (controller) controller.toggleMenu(); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | videojs.registerComponent('ImageSubtitleButton', ImageSubtitleButton); |
| 510 | }; |
| 511 | |
| 512 | const attachUi = () => |
| 513 | { |
| 514 | if (!player.el()) return; |
| 515 | registerButton(); |
| 516 | if (overlayEl.parentElement !== player.el()) player.el().appendChild(overlayEl); |
| 517 | if (menuEl.parentElement !== player.el()) player.el().appendChild(menuEl); |
| 518 | const controlBar = typeof player.getChild === 'function' ? player.getChild('controlBar') : null; |
| 519 | if (controlBar && typeof controlBar.addChild === 'function' && !buttonEl) |
| 520 | { |
| 521 | const button = controlBar.addChild('ImageSubtitleButton', {}); |
| 522 | buttonEl = button && typeof button.el === 'function' ? button.el() : null; |
| 523 | updateButtonState(); |
| 524 | } |
| 525 | }; |
| 526 | |
| 527 | const applySavedTracks = () => |
| 528 | { |
| 529 | const savedName = normaliseTrackName(prefs.imageSubtitleTrackName); |
| 530 | if (savedName) |
| 531 | { |
| 532 | const savedByName = imageTracks.find((track) => normaliseTrackName(track.label) === savedName) || null; |
| 533 | if (savedByName) |
| 534 | { |
| 535 | setSelectedStreamIndex(savedByName.streamIndex, { persist: false }); |
| 536 | return; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | const savedIndex = Number(prefs.imageSubtitleTrackStreamIndex); |
| 541 | if (Number.isInteger(savedIndex) && trackByStreamIndex.has(savedIndex)) |
| 542 | { |
| 543 | setSelectedStreamIndex(savedIndex, { persist: false }); |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | const defaultTrack = imageTracks.find((track) => track.isDefault) || null; |
| 548 | if (defaultTrack) |
| 549 | { |
| 550 | setSelectedStreamIndex(defaultTrack.streamIndex, { persist: false }); |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | setSelectedStreamIndex(null, { persist: false }); |
| 555 | }; |
| 556 | |
| 557 | player.imageSubtitleOverlayController = { |
| 558 | toggleMenu, |
| 559 | closeMenu, |
| 560 | applySavedTracks, |
| 561 | captureSelectedTracks: saveSelection, |
| 562 | detach: dispose, |
| 563 | }; |
| 564 | |
| 565 | player.on('timeupdate', () => { void refreshFrame(); }); |
| 566 | player.on('seeking', () => { void refreshFrame(true); }); |
| 567 | player.on('play', () => { void refreshFrame(true); }); |
| 568 | player.on('fullscreenchange', () => { closeMenu(); void refreshFrame(true); }); |
| 569 | player.on('dispose', dispose); |
| 570 | player.ready(() => |
| 571 | { |
| 572 | attachUi(); |
| 573 | updateMenuState(); |
| 574 | updateButtonState(); |
| 575 | applySavedTracks(); |
| 576 | if (menuOpen) openMenu(); |
| 577 | }); |
| 578 | |
| 579 | return player.imageSubtitleOverlayController; |
| 580 | } |
| 581 | |
| 582 | function formatDurationForDebug (seconds) |
| 583 | { |
| 584 | const value = Number(seconds); |
| 585 | if (!Number.isFinite(value) || value < 0) return 'unknown'; |
| 586 | |
| 587 | const totalSeconds = Math.floor(value); |
| 588 | const hh = String(Math.floor(totalSeconds / 3600)).padStart(2, '0'); |
| 589 | const mm = String(Math.floor((totalSeconds % 3600) / 60)).padStart(2, '0'); |
| 590 | const ss = String(totalSeconds % 60).padStart(2, '0'); |
| 591 | return `${hh}:${mm}:${ss}`; |
| 592 | } |
| 593 | |
| 594 | function installDurationFallback (player, originalDuration, duration) |
| 595 | { |
| 596 | if (!player || !originalDuration || player.__filemanagerDurationFallback) return; |
| 597 | |
| 598 | player.__filemanagerDurationFallback = true; |
| 599 | player.duration = function durationWithFallback (...args) |
| 600 | { |
| 601 | if (args.length) return originalDuration(...args); |
| 602 | const current = Number(originalDuration()); |
| 603 | return Number.isFinite(current) && current >= duration * 0.95 ? current : duration; |
| 604 | }; |
| 605 | } |
| 606 | |
| 607 | function normaliseVideoDuration (player, durationSeconds) |
| 608 | { |
| 609 | const duration = Number(durationSeconds); |
| 610 | if (!player || !Number.isFinite(duration) || duration <= 0) return; |
| 611 | |
| 612 | let originalDuration = null; |
| 613 | let durationWasChanged = false; |
| 614 | try |
| 615 | { |
| 616 | originalDuration = typeof player.duration === 'function' ? player.duration.bind(player) : null; |
| 617 | installDurationFallback(player, originalDuration, duration); |
| 618 | if (player.__filemanagerDurationApplied) return; |
| 619 | |
| 620 | const current = originalDuration ? Number(originalDuration()) : NaN; |
| 621 | const withinTolerance = Number.isFinite(current) && current >= duration * 0.95; |
| 622 | console.log(`Current duration: ${formatDurationForDebug(current)}, Target time: ${formatDurationForDebug(duration)}, within 95%? ${withinTolerance ? 'yes' : 'no'}`); |
| 623 | if (withinTolerance) |
| 624 | { |
| 625 | player.__filemanagerDurationApplied = true; |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | if (originalDuration) |
| 630 | { |
| 631 | durationWasChanged = true; |
| 632 | originalDuration(duration); |
| 633 | } |
| 634 | } catch |
| 635 | { |
| 636 | // Ignore players that do not support setting duration. |
| 637 | } |
| 638 | |
| 639 | try |
| 640 | { |
| 641 | const updated = originalDuration ? Number(originalDuration()) : NaN; |
| 642 | if (Number.isFinite(updated) && updated >= duration * 0.95) |
| 643 | { |
| 644 | player.__filemanagerDurationApplied = true; |
| 645 | if (durationWasChanged && typeof player.trigger === 'function') player.trigger('durationchange'); |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | player.__filemanagerDurationApplied = true; |
| 650 | if (typeof player.trigger === 'function') player.trigger('durationchange'); |
| 651 | } catch |
| 652 | { |
| 653 | // Ignore duration fallback failures. |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | function ensureVideoPlaybackUi (player, durationSeconds) |
| 658 | { |
| 659 | if (!player || typeof player.on !== 'function') return; |
| 660 | |
| 661 | const forceVodUi = () => |
| 662 | { |
| 663 | normaliseVideoDuration(player, durationSeconds); |
| 664 | |
| 665 | try |
| 666 | { |
| 667 | if (player.liveTracker && typeof player.liveTracker.stopTracking === 'function') |
| 668 | { |
| 669 | player.liveTracker.stopTracking(); |
| 670 | } |
| 671 | } catch |
| 672 | { |
| 673 | // Ignore live tracker availability issues. |
| 674 | } |
| 675 | |
| 676 | try |
| 677 | { |
| 678 | if (typeof player.removeClass === 'function') |
| 679 | { |
| 680 | player.removeClass('vjs-live'); |
| 681 | } |
| 682 | } catch |
| 683 | { |
| 684 | // Ignore class update failures. |
| 685 | } |
| 686 | |
| 687 | try |
| 688 | { |
| 689 | const display = player.el && player.el() && player.el().querySelector('.vjs-live-display'); |
| 690 | if (display) display.style.display = 'none'; |
| 691 | } catch |
| 692 | { |
| 693 | // Ignore missing DOM. |
| 694 | } |
| 695 | }; |
| 696 | |
| 697 | player.ready(forceVodUi); |
| 698 | player.on('loadedmetadata', forceVodUi); |
| 699 | player.on('durationchange', forceVodUi); |
| 700 | player.on('loadedplaylist', forceVodUi); |
| 701 | } |
| 702 | |
| 703 | function configureVideoJsBuffering (videojs, opts = {}) |
| 704 | { |
| 705 | if (opts.enabled === false) return; |
| 706 | if (!videojs || !videojs.Vhs || videojs.__filemanagerBufferingConfigured) return; |
| 707 | videojs.__filemanagerBufferingConfigured = true; |
| 708 | |
| 709 | const seconds = Number(opts.seconds); |
| 710 | const bufferSeconds = Number.isFinite(seconds) && seconds > 0 ? seconds : 60 * 60; |
| 711 | |
| 712 | try |
| 713 | { |
| 714 | videojs.Vhs.GOAL_BUFFER_LENGTH = bufferSeconds; |
| 715 | videojs.Vhs.MAX_GOAL_BUFFER_LENGTH = bufferSeconds; |
| 716 | } catch |
| 717 | { |
| 718 | // Ignore builds that do not expose VHS buffer configuration. |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | function applyDefaultTextTrackPrefs () |
| 723 | { |
| 724 | try |
| 725 | { |
| 726 | localStorage.setItem(VIDEO_TEXT_TRACK_SETTINGS_KEY, VIDEO_TEXT_TRACK_OVERRIDE_SETTINGS_RAW); |
| 727 | } catch |
| 728 | { |
| 729 | // Ignore localStorage failures. |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | window.FilemanagerSharedVideoPlayer = { |
| 734 | applyDefaultTextTrackPrefs, |
| 735 | bindImageSubtitleOverlay, |
| 736 | bindTrackPrefs, |
| 737 | configureVideoJsBuffering, |
| 738 | ensureVideoPlaybackUi, |
| 739 | normaliseVideoDuration, |
| 740 | }; |
| 741 | }()); |