Jellyfin Web VR plugin for Quest 3 (VR180 stereo playback)
Injects a 「VR 播放」 button into Jellyfin Web's video player page and plays
VR180 / VR360 stereo content on Meta Quest 3 directly in the browser, with
DirectPlay 8K HEVC, in-VR subtitles and an in-VR control panel.
Modules (src/, concatenated in filename order into dist/jfvr.js):
00-core.js namespace, shared state, GL helpers, layer/frame-hook registry
10-subtitles.js libass canvas -> head-locked quad layer, auto-cropped to ink,
with depth/size decoupling (angular-size lock)
20-controls.js in-VR control panel: progress/seek, transport, subtitle rows,
aiming beam, padlock, pure 2D canvas -> XR quad layer
30-hands.js system hand model (Three.js + generic-hand GLB) via a manually
driven projection layer
90-main.js boot, XR session lifecycle, rAF broker (rAF stops during XR),
input dispatch, settings panel, localStorage persistence
Key on-device findings are documented in HANDOFF.md §6 and README.md addenda:
requestAnimationFrame stops during an XR session, 3:2 pulldown at 90 Hz needs
updateTargetFrameRate(120), libass must be resized before entering VR, a quad's
local -Z is its plane normal, and hand aiming must use the gaze ray because the
emulated targetRaySpace points sideways. Quest does not expose eye tracking.
Docs: INTERFACES.md (module contract + addenda), README.md (measurements),
HANDOFF.md (handover notes). Credentials are NOT in the repo: deploy.py reads
the SSH password from the JF_PW environment variable.
This commit is contained in:
+343
@@ -0,0 +1,343 @@
|
||||
/*!
|
||||
* 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-5'; // hud-4 = ADDENDUM 7; hud-5 = ADDENDUM 7b (padlock in the panel + locked mode)
|
||||
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'
|
||||
|
||||
subtitles: true,
|
||||
ctlLock: false, // ADDENDUM 7: control-bar lock badge (per VR session)
|
||||
subMode: 'hud', // 'hud' | 'sphere' | 'off'
|
||||
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,
|
||||
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
|
||||
};
|
||||
|
||||
(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 {
|
||||
['video/mp4; codecs="hev1.1.6.L183.B0"',
|
||||
'video/mp4; codecs="hvc1.1.6.L183.B0"',
|
||||
'video/mp4; codecs="hev1"',
|
||||
'video/mp4; codecs="hvc1"'].forEach(function (cs) {
|
||||
NS.caps.hevc[cs] = (typeof MediaSource !== 'undefined' && MediaSource.isTypeSupported(cs)) ||
|
||||
document.createElement('video').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; });
|
||||
}
|
||||
})();
|
||||
|
||||
NS.capsDiag = function () {
|
||||
var c = NS.caps;
|
||||
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 : ' + JSON.stringify(c.hevc)
|
||||
].join('\n');
|
||||
};
|
||||
})();
|
||||
Reference in New Issue
Block a user