/*! * Jellyfin Web VR (MVP) — Quest 3 / WebXR / VR180 & VR360 * Injected into jellyfin-web. Self-contained, no external deps. * Primary path: XRMediaBinding.createEquirectLayer (Meta-recommended for 8K) * Fallback path: WebGL2 per-eye equirect ray reconstruction */ (function () { 'use strict'; if (window.__JFVR_LOADED__) return; window.__JFVR_LOADED__ = true; var VERSION = 'mvp-6'; var log = function () { var a = Array.prototype.slice.call(arguments); console.log.apply(console, ['%c[JFVR ' + VERSION + ']', 'color:#4ea1ff;font-weight:bold'].concat(a)); }; /* ============================================================ * state * ========================================================== */ var S = { video: null, btn: null, panel: null, projection: '180', // '180' | '360' stereo: 'sbs', // 'mono' | 'sbs' | 'ou' detected: '', fileName: '', session: null, subtitles: true, // render the libass canvas into VR as a stereo layer subLayer: null, subGl: null, subProg: null, subTex: null, subFbo: null, subCanvas: null, subBinding: null, sizeBackup: null, stats: { fps: null, targetFrameRate: null, path: null, subLayer: false, frames: 0 }, // WebGL fallback resources gl: null, prog: null, tex: null, refSpace: null, uInvProj: null, uView: null, uEye: null, uCentral: null, uStereo: null }; // expose for remote diagnosis over CDP try { window.__JFVR_STATE__ = S; } catch (e) {} /* ============================================================ * rAF broker * ------------------------------------------------------------ * While an immersive XR session is running, window.requestAnimationFrame * is COMPLETELY stopped (measured: 0 callbacks in 6s). Jellyfin renders * libass subtitles from an rAF loop, so the subtitle canvas freezes on the * last frame drawn before entering VR - which is why subtitles "disappear". * * Fix: intercept requestAnimationFrame and, while in XR, hold callbacks in * our own queue and drive them from video.requestVideoFrameCallback (which * still fires, in step with the video). Outside XR we delegate to the native * implementation, so 2D playback is untouched. * ========================================================== */ var RAFQ = { inXR: false, native: (window.requestAnimationFrame || function () {}).bind(window), nativeCancel: (window.cancelAnimationFrame || function () {}).bind(window), queue: new Map(), nextId: 1000000000, pumped: 0 }; try { window.requestAnimationFrame = function (cb) { if (!RAFQ.inXR) return RAFQ.native(cb); var id = RAFQ.nextId++; RAFQ.queue.set(id, cb); return id; }; window.cancelAnimationFrame = function (id) { if (RAFQ.queue.has(id)) { RAFQ.queue.delete(id); return; } return RAFQ.nativeCancel(id); }; } catch (e) { log('rAF broker install failed', e); } function pumpRaf(t) { if (!RAFQ.inXR || !RAFQ.queue.size) return; var cbs = []; RAFQ.queue.forEach(function (cb) { cbs.push(cb); }); RAFQ.queue.clear(); RAFQ.pumped++; for (var i = 0; i < cbs.length; i++) { try { cbs[i](t); } catch (e) { if (!RAFQ.err) RAFQ.err = String((e && e.message) || e); } } } /* ============================================================ * capability probe (answers the 3 open questions on-device) * ========================================================== */ var CAP = { ua: navigator.userAgent, secureContext: window.isSecureContext, hasXR: !!navigator.xr, immersiveVR: null, hasXRMediaBinding: typeof XRMediaBinding !== 'undefined', hasEquirectLayer: false, hasXRWebGLBinding: typeof XRWebGLBinding !== 'undefined', maxTextureSize: null, msSupported: {}, decode: null, video: null }; function hevcCodecStrings() { // matches server-side ffprobe: hevc Main, level 183 (6.1), tag hev1 return [ 'video/mp4; codecs="hev1.1.6.L183.B0"', 'video/mp4; codecs="hvc1.1.6.L183.B0"', 'video/mp4; codecs="hev1.1.6.L153.B0"', 'video/mp4; codecs="hvc1.1.6.L153.B0"', 'video/mp4; codecs="hev1"', 'video/mp4; codecs="hvc1"' ]; } function probeVideoSupport() { var out = {}; hevcCodecStrings().forEach(function (cs) { try { out[cs] = (typeof MediaSource !== 'undefined' && MediaSource.isTypeSupported(cs)) || document.createElement('video').canPlayType(cs) || 'no'; } catch (e) { out[cs] = 'err'; } }); CAP.msSupported = out; } function probeTextureSize() { try { var c = document.createElement('canvas'); var gl = c.getContext('webgl2') || c.getContext('webgl'); if (gl) CAP.maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE); } catch (e) { /* ignore */ } } function probeDecodingInfo(video) { if (!video || !video.videoWidth) return Promise.resolve(null); if (!navigator.mediaCapabilities || !navigator.mediaCapabilities.decodingInfo) return Promise.resolve(null); var cfgs = hevcCodecStrings().slice(0, 3).map(function (cs) { return { type: 'file', video: { contentType: cs, width: video.videoWidth, height: video.videoHeight, bitrate: 25000000, framerate: 60 } }; }); return Promise.all(cfgs.map(function (c) { return navigator.mediaCapabilities.decodingInfo(c).catch(function (e) { return { error: String(e) }; }); })).then(function (res) { var o = {}; res.forEach(function (r, i) { o[cfgs[i].video.contentType] = r; }); return o; }); } function probeAsync() { var work = []; if (navigator.xr && navigator.xr.isSessionSupported) { work.push(navigator.xr.isSessionSupported('immersive-vr') .then(function (v) { CAP.immersiveVR = v; }) .catch(function (e) { CAP.immersiveVR = 'err:' + e.message; })); } if (typeof XRMediaBinding !== 'undefined') { try { CAP.hasEquirectLayer = typeof XRMediaBinding.prototype.createEquirectLayer === 'function'; } catch (e) { CAP.hasEquirectLayer = false; } } probeTextureSize(); probeVideoSupport(); return Promise.all(work).then(function () { return probeDecodingInfo(S.video); }) .then(function (d) { CAP.decode = d; }); } function videoDiag() { var v = S.video; if (!v) return 'no