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