/*! * JFVR core — shared namespace, state, helpers, registries. * Loaded first. Everything else attaches to window.__JFVR. * * See INTERFACES.md for the binding contract. */ (function () { 'use strict'; if (window.__JFVR && window.__JFVR.__core) return; // idempotent var NS = window.__JFVR = window.__JFVR || {}; NS.version = 'hud-11'; // hud-11 = ADDENDUM 12 (single pointer owner: dominant hand by default, press takes over, hold exclusion) + 11/10/9 NS.__core = true; /* ------------------------------------------------------------------ * * logging * ------------------------------------------------------------------ */ NS.log = function () { var a = Array.prototype.slice.call(arguments); try { console.log.apply(console, ['%c[JFVR ' + NS.version + ']', 'color:#4ea1ff;font-weight:bold'].concat(a)); } catch (e) { try { console.log('[JFVR]', a.join(' ')); } catch (e2) {} } }; /* ------------------------------------------------------------------ * * shared state (modules read/write; integrator owns lifecycle) * ------------------------------------------------------------------ */ var S = NS.state = { video: null, session: null, refSpace: null, viewerSpace: null, projection: '180', // '180' | '360' stereo: 'sbs', // 'mono' | 'sbs' | 'ou' // ADDENDUM 8 — where the 180/360 window points. The video layer is an // equirect sphere with radius 0 (content "at infinity"), so rotating its // transform rotates the CONTENT about the eye point without any parallax: // exactly "pan the picture so I do not have to crane my neck". Only the video // rotates — the panel (refSpace, fixed pose) and the subtitles (viewer space, // head-locked) stay in front of the user. Radians. NOT persisted: the owner // wants a fresh forward-facing view every time VR starts. videoYaw: 0, // + = the picture has been moved to the RIGHT videoPitch: 0, // + = the picture has been moved UP subtitles: true, ctlLock: false, // ADDENDUM 7: control-bar lock badge (per VR session) subMode: 'hud', // 'hud' | 'sphere' | 'off' subLayout: 'auto', // ADDENDUM 9: 'auto' | 'dual' | 'mono' subtitle layout subAlpha: 'premultiplied', // 'premultiplied' | 'straight' subOpacity: 0.85, // 0..1, applied to the subtitle layer live // y is the quad CENTRE; negative = below eye level. -0.42 keeps the strip // clearly under the visual centre rather than crowding it. subPlacement: { y: -0.42, z: -1.45, width: 2.0 }, // ADDENDUM 6 — depth / size decoupling. With subLockAngle ON, changing the // strip's DEPTH holds its angular size (subAngleDeg, the strip's full // angular width in degrees) and its angular height (the y/z ratio), so the // user can pull the subtitles to the depth of a nearby object without the // text blowing up or dropping out of the lower view. 69.2 deg is exactly // the pre-ADDENDUM-6 look: 2.0 m wide at 1.45 m. subLockAngle: true, subAngleDeg: 69.2, controls: true, handPref: 'right', // ADDENDUM 12: dominant hand = the pointer's default owner ctlAutoHideMs: 4000, // in-VR control bar auto-hide // written every XR frame by 90-main.js, read by modules pointer: null, // { visible, source, origin:{x,y,z}, dir:{x,y,z}, hit:{nx,ny}|null } stats: {}, errors: [] }; // live handles (mirrors of state, convenient for modules) Object.defineProperty(NS, 'session', { get: function () { return S.session; }, set: function (v) { S.session = v; } }); Object.defineProperty(NS, 'video', { get: function () { return S.video; }, set: function (v) { S.video = v; } }); Object.defineProperty(NS, 'refSpace', { get: function () { return S.refSpace; }, set: function (v) { S.refSpace = v; } }); Object.defineProperty(NS, 'viewerSpace', { get: function () { return S.viewerSpace; }, set: function (v) { S.viewerSpace = v; } }); NS.recordError = function (where, e) { var msg = where + ': ' + ((e && e.message) || e); S.errors.push(msg); if (S.errors.length > 40) S.errors.shift(); return msg; }; /* ------------------------------------------------------------------ * * DOM helpers * ------------------------------------------------------------------ */ NS.makeEl = function (tag, className, text) { var el = document.createElement(tag); if (className) el.className = className; if (text != null) el.textContent = text; return el; }; NS.clamp = function (v, lo, hi) { return v < lo ? lo : (v > hi ? hi : v); }; /* ------------------------------------------------------------------ * * GL helpers * ------------------------------------------------------------------ */ NS.compileShader = function (gl, type, src) { var sh = gl.createShader(type); gl.shaderSource(sh, src); gl.compileShader(sh); if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) { var info = gl.getShaderInfoLog(sh) || 'shader compile failed'; gl.deleteShader(sh); throw new Error(info); } return sh; }; // Fullscreen triangle in clip space. Fragment shader gets `in vec2 vUv` // with vUv.y already flipped so (0,0) is the top-left of the source image. NS.QUAD_VS = [ '#version 300 es', 'in vec2 aPos;', 'out vec2 vUv;', 'void main(){', ' vUv = vec2(aPos.x * 0.5 + 0.5, 0.5 - aPos.y * 0.5);', ' gl_Position = vec4(aPos, 0.0, 1.0);', '}' ].join('\n'); NS.bindFullscreenTriangle = function (gl, program) { var buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buf); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW); var loc = gl.getAttribLocation(program, 'aPos'); gl.enableVertexAttribArray(loc); gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0); return loc; }; NS.linkProgram = function (gl, vsSrc, fsSrc) { var prog = gl.createProgram(); gl.attachShader(prog, NS.compileShader(gl, gl.VERTEX_SHADER, vsSrc)); gl.attachShader(prog, NS.compileShader(gl, gl.FRAGMENT_SHADER, fsSrc)); gl.linkProgram(prog); if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) { var info = gl.getProgramInfoLog(prog) || 'program link failed'; throw new Error(info); } gl.useProgram(prog); return prog; }; NS.makeTexture = function (gl) { var t = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, t); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); return t; }; NS.xrGl = function (alpha) { var c = document.createElement('canvas'); return c.getContext('webgl2', { xrCompatible: true, alpha: alpha !== false, premultipliedAlpha: true, antialias: false, depth: false, desynchronized: true }); }; // 4x4 matrix inverse (column-major, as used by WebXR). NS.invert4 = function (m) { var a00 = m[0], a01 = m[1], a02 = m[2], a03 = m[3], a10 = m[4], a11 = m[5], a12 = m[6], a13 = m[7], a20 = m[8], a21 = m[9], a22 = m[10], a23 = m[11], a30 = m[12], a31 = m[13], a32 = m[14], a33 = m[15], b00 = a00 * a11 - a01 * a10, b01 = a00 * a12 - a02 * a10, b02 = a00 * a13 - a03 * a10, b03 = a01 * a12 - a02 * a11, b04 = a01 * a13 - a03 * a11, b05 = a02 * a13 - a03 * a12, b06 = a20 * a31 - a21 * a30, b07 = a20 * a32 - a22 * a30, b08 = a20 * a33 - a23 * a30, b09 = a21 * a32 - a22 * a31, b10 = a21 * a33 - a23 * a31, b11 = a22 * a33 - a23 * a32, det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; if (!det) return new Float32Array(16); det = 1.0 / det; return new Float32Array([ (a11 * b11 - a12 * b10 + a13 * b09) * det, (a02 * b10 - a01 * b11 - a03 * b09) * det, (a31 * b05 - a32 * b04 + a33 * b03) * det, (a22 * b04 - a21 * b05 - a23 * b03) * det, (a12 * b08 - a10 * b11 - a13 * b07) * det, (a00 * b11 - a02 * b08 + a03 * b07) * det, (a32 * b02 - a30 * b05 - a33 * b01) * det, (a20 * b05 - a22 * b02 + a23 * b01) * det, (a10 * b10 - a11 * b08 + a13 * b06) * det, (a01 * b08 - a00 * b10 - a03 * b06) * det, (a30 * b04 - a31 * b02 + a33 * b00) * det, (a21 * b02 - a20 * b04 - a23 * b00) * det, (a11 * b07 - a10 * b09 - a12 * b06) * det, (a00 * b09 - a01 * b07 + a02 * b06) * det, (a31 * b01 - a30 * b03 - a32 * b00) * det, (a20 * b03 - a21 * b01 + a22 * b00) * det ]); }; /* ------------------------------------------------------------------ * * XR layer + frame-hook registry * * Layers are owned by modules; main.js owns pushing the render state. * ------------------------------------------------------------------ */ var layers = []; var hooks = []; var pushRenderState = null; // set by main.js once a session exists NS.setRenderStatePusher = function (fn) { pushRenderState = fn; }; NS.addLayer = function (layer) { if (!layer || layers.indexOf(layer) >= 0) return layer; layers.push(layer); NS.log('layer added (' + layers.length + ' total)'); if (pushRenderState) { try { pushRenderState(); } catch (e) { NS.recordError('addLayer', e); } } return layer; }; NS.removeLayer = function (layer) { var i = layers.indexOf(layer); if (i < 0) return; layers.splice(i, 1); NS.log('layer removed (' + layers.length + ' left)'); if (pushRenderState) { try { pushRenderState(); } catch (e) { NS.recordError('removeLayer', e); } } }; NS.getLayers = function () { return layers.slice(); }; NS.clearLayers = function () { layers.length = 0; if (pushRenderState) { try { pushRenderState(); } catch (e) {} } }; NS.addFrameHook = function (fn) { if (typeof fn !== 'function' || hooks.indexOf(fn) >= 0) return fn; hooks.push(fn); return fn; }; NS.removeFrameHook = function (fn) { var i = hooks.indexOf(fn); if (i >= 0) hooks.splice(i, 1); }; NS.runFrameHooks = function (frame, time, session) { for (var i = 0; i < hooks.length; i++) { try { hooks[i](frame, time, session); } catch (e) { var msg = NS.recordError('frameHook', e); if (!S.stats.hookErr) S.stats.hookErr = msg; } } }; NS.clearFrameHooks = function () { hooks.length = 0; }; /* ------------------------------------------------------------------ * * stereo / projection helpers shared by modules * ------------------------------------------------------------------ */ NS.stereoLayout = function () { return S.stereo === 'sbs' ? 'stereo-left-right' : S.stereo === 'ou' ? 'stereo-top-bottom' : 'mono'; }; NS.centralAngle = function () { return S.projection === '360' ? Math.PI * 2 : Math.PI; }; /* ------------------------------------------------------------------ * * capabilities probe (safe at load time: no user gesture needed) * ------------------------------------------------------------------ */ NS.caps = { ua: navigator.userAgent, secureContext: !!window.isSecureContext, hasXR: !!navigator.xr, immersiveVR: null, hasXRMediaBinding: typeof window.XRMediaBinding !== 'undefined', hasEquirectLayer: false, hasXRWebGLBinding: typeof window.XRWebGLBinding !== 'undefined', hasHandTracking: typeof window.XRHand !== 'undefined', hasXRJointSpace: typeof window.XRJointSpace !== 'undefined', hasGetJointPose: typeof window.XRFrame !== 'undefined' && typeof XRFrame.prototype.getJointPose === 'function', maxTextureSize: null, hevc: {}, decoding8k: null, /* MediaCapabilities verdicts, keyed ':'. See below. */ codec: {}, codecBlocked: [], codecKept: [], cptPatched: false, cptMode: null }; /* ------------------------------------------------------------------ * * codec capability: what this browser will ACTUALLY decode * ------------------------------------------------------------------ * Two layers. * * 1. NS.caps.hevc -- raw canPlayType() answers, for the diagnostics panel. * This used to only ask about Main profile (8-bit), which is why the * project's notes said "8K HEVC: supported" right up until a Main 10 * (10-bit) file showed up and could not be decoded at all. The Main 10 * strings are the ones that actually matter for 8K VR here. * * 2. NS.caps.codec + the canPlayType override -- makes Jellyfin Web build * an ACCURATE device profile. * * Why the override: Jellyfin Web derives its entire HEVC claim from * canPlayType (src/scripts/browserDeviceProfile.js): * * let maxHevcLevel = 120, hevcProfiles = 'main'; * ... * if (canPlayType('video/mp4; codecs="hvc1.2.4.L183"')) { * maxHevcLevel = 183; // level 6.1 = 8K * hevcProfiles = 'main|main 10'; * } * * canPlayType answers "does this system have an HEVC decoder", not "can it * do 8192x4096 Main 10 at 60 fps". So a desktop browser that merely ships an * HEVC decoder claims 8K Main 10, the server picks DirectPlay, and playback * dies on a black screen with no player UI (the OSD is only built after * 'playbackstart'). decodingInfo asks the actual decoder, so feeding its * verdict back through canPlayType lets Jellyfin's own logic land on the * right profile: the desktop drops to whatever it can really do and the * server transcodes, while a Quest that genuinely decodes 8K Main 10 keeps * DirectPlay. Meta's browser-video docs recommend exactly this API over UA * sniffing. * * Only a definite "no" downgrades, but "no" has two shapes and both matter: * * supported === false the decoder cannot handle the format at all * smooth === false it can, but not in real time at that resolution and * frame rate -- the case a desktop with a small HEVC * decoder hits on 8K, where the picture never appears * even though the codec is "supported" * * Either one means the server should transcode. A tier that is only * `powerEfficient:false` (software decode) is NOT acted on by itself. * * Escape hatches, if a device reports nonsense: * localStorage['jfvr-cpt'] = 'off' disable the hook entirely * localStorage['jfvr-cpt-keep'] = '2:183' never downgrade these tiers */ // decodingInfo needs real dimensions; a level string does not carry them, so // each level is probed at the resolution and bitrate it exists for. var CODEC_PROBES = [ { key: '1:120', name: 'Main L4.0', w: 1920, h: 1080, fps: 30, mbps: 8, codec: 'hvc1.1.L120' }, { key: '1:123', name: 'Main L4.1', w: 1920, h: 1080, fps: 60, mbps: 8, codec: 'hvc1.1.4.L123' }, { key: '1:183', name: 'Main L6.1 (8K)', w: 8192, h: 4096, fps: 60, mbps: 30, codec: 'hvc1.1.6.L183.B0' }, { key: '2:123', name: 'Main10 L4.1', w: 1920, h: 1080, fps: 60, mbps: 8, codec: 'hvc1.2.4.L123' }, { key: '2:153', name: 'Main10 L5.1 (4K)', w: 3840, h: 2160, fps: 60, mbps: 20, codec: 'hvc1.2.4.L153' }, { key: '2:183', name: 'Main10 L6.1 (8K)', w: 8192, h: 4096, fps: 60, mbps: 30, codec: 'hvc1.2.4.L183' }, { key: '2:186', name: 'Main10 L6.2 (8K60)', w: 8192, h: 4096, fps: 60, mbps: 30, codec: 'hvc1.2.4.L186' } ]; /** * 'video/mp4; codecs="hvc1.2.4.L183.B0"' -> '2:183' (profile_idc : level). * Both the hvc1 and hev1 spellings map to the same key: they differ in the * sample entry, not in what has to be decoded. */ function hevcKey(type) { var m = /(?:hvc1|hev1)\.([0-9]+)(?:\.[0-9]+)?\.l([0-9]+)/i.exec(String(type || '')); return m ? m[1] + ':' + m[2] : null; } NS.caps.hevcKey = hevcKey; (function probe() { try { var mb = window.XRMediaBinding; if (mb && mb.prototype) NS.caps.hasEquirectLayer = typeof mb.prototype.createEquirectLayer === 'function'; var wg = window.XRWebGLBinding; if (wg && wg.prototype) NS.caps.hasQuadLayer = typeof wg.prototype.createQuadLayer === 'function'; } catch (e) {} try { var gl = document.createElement('canvas').getContext('webgl2') || document.createElement('canvas').getContext('webgl'); if (gl) NS.caps.maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE); } catch (e) {} try { var ve = document.createElement('video'); ['video/mp4; codecs="hev1.1.6.L183.B0"', // Main 8-bit 8K (the files that work) 'video/mp4; codecs="hvc1.1.6.L183.B0"', 'video/mp4; codecs="hev1.2.4.L183.B0"', // Main 10 8K (the files that do not) 'video/mp4; codecs="hvc1.2.4.L183.B0"', 'video/mp4; codecs="hev1.2.4.L153.B0"', // Main 10 4K 'video/mp4; codecs="hvc1.2.4.L153.B0"', 'video/mp4; codecs="hev1"', 'video/mp4; codecs="hvc1"'].forEach(function (cs) { NS.caps.hevc[cs] = (typeof MediaSource !== 'undefined' && MediaSource.isTypeSupported(cs)) || ve.canPlayType(cs) || 'no'; }); } catch (e) {} if (navigator.xr && navigator.xr.isSessionSupported) { navigator.xr.isSessionSupported('immersive-vr') .then(function (v) { NS.caps.immersiveVR = v; }) .catch(function (e) { NS.caps.immersiveVR = 'err:' + e.message; }); } })(); /* ------------------------------------------------------------------ * * MediaCapabilities probe + canPlayType override * ------------------------------------------------------------------ */ /** Tiers the user has pinned as "keep claiming this", ',' in localStorage. */ function readKeepList() { try { var raw = localStorage.getItem('jfvr-cpt-keep') || ''; return raw.split(',').map(function (s) { return s.trim(); }).filter(Boolean); } catch (e) { return []; } } /** Why a verdict means "do not claim this tier", or null if it is fine. */ function blockReason(v, keep) { if (!v || v.error) return null; // unknown -> leave it to the browser if (keep.indexOf(v.key) >= 0) return null; // user pinned it if (v.supported === false) return 'unsupported'; if (v.smooth === false) return 'not-smooth'; return null; } function applyVerdicts() { var keep = readKeepList(); NS.caps.codecKept = []; NS.caps.codecBlocked = []; CODEC_PROBES.forEach(function (p) { var v = NS.caps.codec[p.key]; if (!v) return; var why = blockReason(v, keep); if (why) NS.caps.codecBlocked.push(p.name + ' (' + why + ')'); else if (keep.indexOf(p.key) >= 0) NS.caps.codecKept.push(p.name); }); } function installCanPlayTypeOverride() { if (NS.caps.cptMode === 'off') return; var proto = window.HTMLVideoElement && window.HTMLVideoElement.prototype; if (!proto || typeof proto.canPlayType !== 'function') return; var orig = proto.canPlayType; proto.canPlayType = function (type) { try { var key = hevcKey(type); var v = key && NS.caps.codec[key]; // Only a definite "no" is acted on. Anything else is the browser's call. if (v && blockReason(v, NS.caps.__keep || [])) return ''; } catch (e) { /* never break the native answer */ } return orig.apply(this, arguments); }; NS.caps.cptPatched = true; NS.caps.cptMode = 'on'; } (function probeCodecs() { // Kill switch, and the escape hatch if this ever misbehaves on a device. try { if (localStorage.getItem('jfvr-cpt') === 'off') { NS.caps.cptMode = 'off'; return; } } catch (e) {} var mc = navigator.mediaCapabilities; if (!mc || typeof mc.decodingInfo !== 'function') { NS.caps.cptMode = 'no-api'; return; } // Read the pin list once, up front: canPlayType is synchronous and must not // touch localStorage on every call. NS.caps.__keep = readKeepList(); // Install FIRST, synchronously, so the hook is in place before anything can // read canPlayType. Until a verdict lands the hook just defers to native, // which is the safe default. installCanPlayTypeOverride(); var pending = CODEC_PROBES.length; CODEC_PROBES.forEach(function (p) { var cfg = { type: 'file', video: { contentType: 'video/mp4; codecs="' + p.codec + '"', width: p.w, height: p.h, bitrate: p.mbps * 1000000, framerate: p.fps } }; mc.decodingInfo(cfg).then(function (info) { NS.caps.codec[p.key] = { key: p.key, name: p.name, workload: p.w + 'x' + p.h + '@' + p.fps + ' ' + p.mbps + 'Mbps', supported: info.supported, smooth: info.smooth, powerEfficient: info.powerEfficient }; }).catch(function (e) { NS.caps.codec[p.key] = { key: p.key, name: p.name, error: String(e && e.message || e) }; }).then(function () { if (--pending === 0) { applyVerdicts(); NS.log('codec probe done; blocked = ' + (NS.caps.codecBlocked.join(', ') || 'none') + (NS.caps.codecKept.length ? '; pinned = ' + NS.caps.codecKept.join(', ') : '')); } }); }); })(); NS.capsDiag = function () { var c = NS.caps; var keep = readKeepList(); var real = CODEC_PROBES.map(function (p) { var v = c.codec[p.key]; if (!v) return ' ' + p.name.padEnd(20) + ' (pending)'; if (v.error) return ' ' + p.name.padEnd(20) + ' error: ' + v.error; var why = blockReason(v, keep); return ' ' + p.name.padEnd(20) + ' supported=' + v.supported + ' smooth=' + v.smooth + ' powerEfficient=' + v.powerEfficient + ' [' + v.workload + ']' + (why ? ' <-- BLOCKED (' + why + ')' : (keep.indexOf(p.key) >= 0 ? ' <-- pinned' : '')); }).join('\n'); return [ 'origin : ' + location.origin, 'secureContext : ' + c.secureContext, 'immersive-vr : ' + c.immersiveVR, 'XRMediaBinding : ' + c.hasXRMediaBinding, 'XRWebGLBinding : ' + c.hasXRWebGLBinding, 'createQuadLayer : ' + c.hasQuadLayer, 'hand tracking API : ' + c.hasHandTracking + ' / jointSpace=' + c.hasXRJointSpace + ' / getJointPose=' + c.hasGetJointPose, 'MAX_TEXTURE_SIZE : ' + c.maxTextureSize, 'HEVC canPlayType : ' + JSON.stringify(c.hevc), 'canPlayType hook : ' + (c.cptMode || 'pending') + (c.cptMode === 'on' ? ' (blocked: ' + (c.codecBlocked.join(', ') || 'nothing yet') + ')' : ''), 'HEVC real (decodingInfo):', real ].join('\n'); }; })();