Files
jellyfin_vr_web_plugin/legacy/jfvr-mvp-v6-working.js
T
cat-shark 838675baa6 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.
2026-10-11 19:19:06 +08:00

1101 lines
42 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*!
* 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 <video>';
var src = v.currentSrc || v.src || '';
var mode = /\/Videos\/[^/]+\/stream/i.test(src) ? 'DirectPlay (stream)'
: /master\.m3u8|\/videos\/[^/]+\/hls/i.test(src) ? 'HLS (likely transcode)'
: /\.m3u8/i.test(src) ? 'HLS'
: /\.mp4|\.mkv|\.webm/i.test(src) ? 'file/remux'
: 'unknown';
return [
'size : ' + v.videoWidth + ' x ' + v.videoHeight +
(v.videoWidth ? ' (ratio ' + (v.videoWidth / v.videoHeight).toFixed(3) + ')' : ''),
'readyState : ' + v.readyState + ' paused: ' + v.paused,
'duration : ' + (isFinite(v.duration) ? v.duration.toFixed(1) + 's' : 'n/a'),
'playbackMode: ' + mode,
'currentSrc : ' + src.slice(0, 110),
'fileName : ' + (S.fileName || '(n/a)'),
'detected : ' + (S.detected || '(manual)')
].join('\n');
}
function capDiag() {
return [
'origin : ' + window.location.origin,
'secureContext : ' + CAP.secureContext,
'navigator.xr : ' + CAP.hasXR,
'immersive-vr : ' + CAP.immersiveVR,
'XRMediaBinding : ' + CAP.hasXRMediaBinding,
'createEquirectLayer: ' + CAP.hasEquirectLayer,
'XRWebGLBinding : ' + CAP.hasXRWebGLBinding,
'MAX_TEXTURE_SIZE : ' + CAP.maxTextureSize,
'HEVC codecs : ' + JSON.stringify(CAP.msSupported),
'decodingInfo : ' + JSON.stringify(CAP.decode),
'UA : ' + CAP.ua
].join('\n');
}
/* ============================================================
* format auto-detection
* ========================================================== */
function detectFromName(name) {
if (!name) return null;
var n = name.toLowerCase();
var is180 = /(^|[^0-9])180([^0-9]|$)/.test(n) || /\bvr180\b/.test(n);
var is360 = /(^|[^0-9])360([^0-9]|$)/.test(n) || /\bvr360\b/.test(n);
var sbs = /(^|[^a-z])(sbs|lr|3dh)([^a-z]|$)/.test(n);
var ou = /(^|[^a-z])(ou|tb|3dv)([^a-z]|$)/.test(n);
if (!is180 && !is360 && !sbs && !ou) return null;
return {
projection: is360 ? '360' : '180',
stereo: ou ? 'ou' : (sbs || is180 || is360) ? 'sbs' : 'mono'
};
}
function detectFromShape(w, h) {
if (!w || !h) return null;
var r = w / h;
if (r >= 3.6) return { projection: '360', stereo: 'sbs' }; // 4:1
if (r >= 1.8 && r <= 2.2) return { projection: '180', stereo: 'sbs' }; // 2:1 -> VR180 SBS
if (r >= 1.15 && r <= 1.45) return { projection: '180', stereo: 'mono' };
if (r <= 0.6) return { projection: '180', stereo: 'ou' };
return { projection: '180', stereo: 'sbs' };
}
function currentItemId() {
var m = /[?&]id=([a-f0-9]{32})/i.exec(window.location.hash || '');
return m ? m[1] : null;
}
function tryFetchFileName() {
var id = currentItemId();
if (!id || typeof ApiClient === 'undefined' || !ApiClient.getItem) return Promise.resolve('');
var uid;
try { uid = ApiClient.getCurrentUserId(); } catch (e) { uid = null; }
if (!uid) return Promise.resolve('');
return ApiClient.getItem(uid, id).then(function (item) {
return (item && (item.Path || item.Name)) || '';
}).catch(function () { return ''; });
}
function applyAutoDetect() {
return tryFetchFileName().then(function (name) {
if (name) {
S.fileName = name.split(/[\\/]/).pop();
var d = detectFromName(S.fileName);
if (d) {
S.projection = d.projection;
S.stereo = d.stereo;
S.detected = 'filename: ' + S.fileName;
return;
}
}
var v = S.video;
var d2 = v ? detectFromShape(v.videoWidth, v.videoHeight) : null;
if (d2) {
S.projection = d2.projection;
S.stereo = d2.stereo;
S.detected = 'resolution: ' + v.videoWidth + 'x' + v.videoHeight;
}
}).catch(function () { /* ignore */ });
}
/* ============================================================
* UI
* ========================================================== */
var CSS = [
'.jfvr-btn{position:fixed;right:16px;bottom:118px;z-index:2147483000;',
'display:flex;align-items:center;gap:8px;padding:10px 16px;border:0;border-radius:24px;',
'background:linear-gradient(135deg,#4ea1ff,#7b5cff);color:#fff;font-size:15px;font-weight:600;',
'cursor:pointer;box-shadow:0 6px 22px rgba(0,0,0,.45);font-family:inherit}',
'.jfvr-btn:active{transform:scale(.96)}',
'.jfvr-panel{position:fixed;right:16px;bottom:170px;z-index:2147483001;width:252px;',
'background:rgba(20,20,24,.96);color:#eaeaea;border:1px solid rgba(255,255,255,.14);',
'border-radius:14px;box-shadow:0 12px 40px rgba(0,0,0,.6);font-family:inherit;font-size:14px;',
'backdrop-filter:blur(10px);overflow:hidden}',
'.jfvr-head{display:flex;justify-content:space-between;align-items:center;padding:10px 12px;',
'background:rgba(255,255,255,.06);font-weight:600}',
'.jfvr-x{background:none;border:0;color:#aaa;font-size:20px;line-height:1;cursor:pointer;padding:0 4px}',
'.jfvr-body{padding:12px}',
'.jfvr-row{display:flex;align-items:center;justify-content:space-between;margin-bottom:10px}',
'.jfvr-lbl{color:#9aa0aa;font-size:13px}',
'.jfvr-seg{display:flex;border:1px solid rgba(255,255,255,.16);border-radius:8px;overflow:hidden}',
'.jfvr-seg button{background:none;border:0;color:#bbb;padding:6px 10px;font-size:13px;cursor:pointer;font-family:inherit}',
'.jfvr-seg button+button{border-left:1px solid rgba(255,255,255,.16)}',
'.jfvr-seg button.on{background:#4ea1ff;color:#fff;font-weight:600}',
'.jfvr-hint{font-size:12px;color:#8b93a1;margin:2px 0 10px;line-height:1.5;word-break:break-all}',
'.jfvr-go{width:100%;padding:10px;border:0;border-radius:9px;background:#4ea1ff;color:#fff;',
'font-size:15px;font-weight:600;cursor:pointer;font-family:inherit}',
'.jfvr-go:disabled{background:#3a3f47;color:#888;cursor:default}',
'.jfvr-diag{margin-top:10px;font-size:11px;color:#8b93a1}',
'.jfvr-diag summary{cursor:pointer;user-select:none}',
'.jfvr-diag pre{white-space:pre-wrap;word-break:break-all;margin:8px 0 0;font-size:10px;',
'line-height:1.45;max-height:190px;overflow:auto;color:#9fb0c4;background:rgba(0,0,0,.35);',
'padding:8px;border-radius:7px}',
'.jfvr-msg{margin-top:8px;font-size:12px;color:#ffb3b3;line-height:1.55;white-space:pre-line;word-break:break-all}'
].join('');
function injectCss() {
if (document.getElementById('jfvr-css')) return;
var s = document.createElement('style');
s.id = 'jfvr-css';
s.textContent = CSS;
document.head.appendChild(s);
}
function mk(tag, cls, text) {
var e = document.createElement(tag);
if (cls) e.className = cls;
if (text != null) e.textContent = text;
return e;
}
function ensureButton() {
if (S.btn && document.body.contains(S.btn)) return;
injectCss();
var b = mk('button', 'jfvr-btn');
b.type = 'button';
b.innerHTML = '<span>&#127909;</span><span>VR 播放</span>';
b.addEventListener('click', function (ev) { ev.stopPropagation(); togglePanel(); });
document.body.appendChild(b);
S.btn = b;
}
function removeButton() {
if (S.btn) { S.btn.remove(); S.btn = null; }
hidePanel();
}
function segRow(label, group, options) {
var row = mk('div', 'jfvr-row');
row.appendChild(mk('span', 'jfvr-lbl', label));
var seg = mk('div', 'jfvr-seg');
seg.dataset.group = group;
options.forEach(function (o) {
var b = mk('button', null, o.label);
b.type = 'button';
b.dataset.value = o.value;
if (S[group] === o.value) b.classList.add('on');
b.addEventListener('click', function (ev) {
ev.stopPropagation();
S[group] = o.value;
S.detected = 'manual';
Array.prototype.forEach.call(seg.children, function (c) { c.classList.remove('on'); });
b.classList.add('on');
refreshHint();
});
seg.appendChild(b);
});
row.appendChild(seg);
return row;
}
function togglePanel() {
if (S.panel) { hidePanel(); return; }
injectCss();
// prepare the subtitle canvas while we still can (see enlargeSubtitleCanvas)
if (S.subtitles) enlargeSubtitleCanvas();
var p = mk('div', 'jfvr-panel');
p.addEventListener('click', function (ev) { ev.stopPropagation(); });
var head = mk('div', 'jfvr-head');
head.appendChild(mk('span', null, 'VR 播放'));
var x = mk('button', 'jfvr-x', '\u00d7');
x.type = 'button';
x.addEventListener('click', function (ev) { ev.stopPropagation(); hidePanel(); });
head.appendChild(x);
p.appendChild(head);
var body = mk('div', 'jfvr-body');
if (!window.isSecureContext) {
var warn = mk('div', 'jfvr-msg',
'⚠ 当前不是安全上下文(' + window.location.origin + '),点了也进不去 VR。\n' +
'先在 Quest 浏览器 chrome://flags 里启用 unsafely-treat-insecure-origin-as-secure,' +
'填入 http://192.168.123.199:8096,Relaunch 后重开本页。');
body.appendChild(warn);
}
body.appendChild(segRow('投影', 'projection', [
{ label: '180\u00b0', value: '180' },
{ label: '360\u00b0', value: '360' }
]));
body.appendChild(segRow('立体', 'stereo', [
{ label: '单目', value: 'mono' },
{ label: '左右', value: 'sbs' },
{ label: '上下', value: 'ou' }
]));
// subtitle compositing toggle (libass canvas -> stereo layer)
(function () {
var row = mk('div', 'jfvr-row');
row.appendChild(mk('span', 'jfvr-lbl', 'VR 内字幕'));
var seg = mk('div', 'jfvr-seg');
[['开', true], ['关', false]].forEach(function (o) {
var b = mk('button', null, o[0]);
b.type = 'button';
if (S.subtitles === o[1]) b.classList.add('on');
b.addEventListener('click', function (ev) {
ev.stopPropagation();
S.subtitles = o[1];
var hasCanvas = !!document.querySelector('canvas.libassjs-canvas');
if (o[1] && !hasCanvas) {
setMsg('未检测到 libass 字幕画布:请先在 Jellyfin 播放器里选中 ASS 字幕轨。');
} else {
setMsg('');
}
Array.prototype.forEach.call(seg.children, function (c) { c.classList.remove('on'); });
b.classList.add('on');
});
seg.appendChild(b);
});
row.appendChild(seg);
body.appendChild(row);
})();
var hint = mk('div', 'jfvr-hint');
hint.id = 'jfvr-hint';
body.appendChild(hint);
var go = mk('button', 'jfvr-go', '进入 VR');
go.type = 'button';
go.id = 'jfvr-go';
go.addEventListener('click', function (ev) {
ev.stopPropagation();
go.disabled = true;
go.textContent = '进入中…';
enterVR().catch(function (e) { log('enterVR threw', e); }).then(function () {
go.disabled = false;
go.textContent = '进入 VR';
});
});
body.appendChild(go);
var msg = mk('div', 'jfvr-msg');
msg.id = 'jfvr-msg';
body.appendChild(msg);
var det = document.createElement('details');
det.className = 'jfvr-diag';
var sum = document.createElement('summary');
sum.textContent = '诊断信息(请把这里的内容发我)';
det.appendChild(sum);
var pre = document.createElement('pre');
pre.id = 'jfvr-diag';
det.appendChild(pre);
body.appendChild(det);
p.appendChild(body);
document.body.appendChild(p);
S.panel = p;
refreshHint();
updateDiag();
probeAsync().then(updateDiag);
// keep the runtime block (fps / path) live while the panel is open
if (!S.diagTimer) {
S.diagTimer = setInterval(function () {
if (S.panel) updateDiag(); else { clearInterval(S.diagTimer); S.diagTimer = null; }
}, 1000);
}
}
function hidePanel() {
if (S.panel) { S.panel.remove(); S.panel = null; }
// only shrink back when we are not about to render subtitles in VR
if (!S.session) restoreSubtitleCanvas();
}
function refreshHint() {
var h = document.getElementById('jfvr-hint');
if (!h) return;
var v = S.video;
var shape = v && v.videoWidth ? (v.videoWidth + '\u00d7' + v.videoHeight) : '未知';
var txt = '画面 ' + shape + ' · ' + (S.detected || '自动');
if (S.projection === '180' && S.stereo === 'sbs') txt += ' → VR180 左右';
else if (S.projection === '180' && S.stereo === 'ou') txt += ' → VR180 上下';
else if (S.projection === '180') txt += ' → VR180 单目';
else if (S.stereo === 'sbs') txt += ' → VR360 左右';
else if (S.stereo === 'ou') txt += ' → VR360 上下';
else txt += ' → VR360 单目';
h.textContent = txt;
}
function updateDiag() {
var pre = document.getElementById('jfvr-diag');
if (!pre) return;
var st = S.stats || {};
var runtime = [
'path : ' + (st.path || '(not in VR)'),
'targetFrameRate : ' + (st.targetFrameRate || '-') +
' supported: ' + ((st.supportedFrameRates || []).join('/') || '-'),
'measured FPS : ' + (st.fps || '-'),
'raf pump : ' + (st.rafPump || '-') + ' pumped=' + RAFQ.pumped + ' pending=' + RAFQ.queue.size +
(RAFQ.err ? ' err=' + RAFQ.err : ''),
'subtitle layer : ' + st.subLayer + (st.subErr ? ' err=' + st.subErr : ''),
'sub canvas : ' + (S.subCanvas ? S.subCanvas.width + 'x' + S.subCanvas.height : '-')
].join('\n');
pre.textContent = '--- 能力 ---\n' + capDiag() +
'\n\n--- 视频 ---\n' + videoDiag() +
'\n\n--- 运行 ---\n' + runtime;
}
function setMsg(t) {
var m = document.getElementById('jfvr-msg');
if (m) m.textContent = t || '';
}
/* ============================================================
* video discovery
* ========================================================== */
function findVideo() {
var list = Array.prototype.slice.call(document.querySelectorAll('video'));
var playing = null, any = null;
list.forEach(function (v) {
if (!v.videoWidth) return;
if (!any) any = v;
if (!v.paused && v.readyState >= 2 && !playing) playing = v;
});
return playing || any || null;
}
var lastVideo = null;
function scan() {
var v = findVideo();
if (v === lastVideo) return;
lastVideo = v;
S.video = v;
if (v) {
log('video found', v.videoWidth + 'x' + v.videoHeight, v.currentSrc.slice(0, 80));
ensureButton();
S.detected = '';
applyAutoDetect().then(function () {
refreshHint();
updateDiag();
});
} else {
removeButton();
}
}
/* ============================================================
* WebXR
* ========================================================== */
function stereoLayout() {
return S.stereo === 'sbs' ? 'stereo-left-right'
: S.stereo === 'ou' ? 'stereo-top-bottom'
: 'mono';
}
// Drive the intercepted rAF callbacks while in XR. requestVideoFrameCallback
// fires in step with decoded video frames, which is exactly what the
// subtitle renderer wants; fall back to a timer when it is unavailable.
function startRafPump(video) {
RAFQ.inXR = true;
RAFQ.queue.clear();
RAFQ.pumped = 0;
var stopped = false;
S.stats.rafPump = 'videoFrameCallback';
if (video && video.requestVideoFrameCallback) {
var step = function (now) {
if (stopped) return;
pumpRaf(now);
video.requestVideoFrameCallback(step);
};
video.requestVideoFrameCallback(step);
S._rafStop = function () { stopped = true; };
return;
}
S.stats.rafPump = 'interval';
var id = setInterval(function () { pumpRaf(performance.now()); }, 16);
S._rafStop = function () { stopped = true; clearInterval(id); };
}
function stopRafPump() {
try { if (S._rafStop) S._rafStop(); } catch (e) {}
S._rafStop = null;
RAFQ.inXR = false;
// hand any leftovers back to the real thing so nobody gets stuck
RAFQ.queue.forEach(function (cb) { try { RAFQ.native(cb); } catch (e) {} });
RAFQ.queue.clear();
}
function centralAngle() {
return S.projection === '360' ? Math.PI * 2 : Math.PI;
}
function enterVR() {
var video = S.video;
if (!video) { setMsg('未找到视频元素。'); return Promise.resolve(); }
if (!window.isSecureContext) {
setMsg(
'当前页不是安全上下文,WebXR 不可用。\n\n' +
'Quest 浏览器解决办法:\n' +
'1. 地址栏输入 chrome://flags\n' +
'2. 搜 insecure\n' +
'3. 启用 unsafely-treat-insecure-origin-as-secure\n' +
'4. 输入框填:' + window.location.origin + '\n' +
'5. 点空白处失焦 → 底部 Relaunch\n' +
'6. 重新打开本页\n\n' +
'(必须带 http:// 和端口号)'
);
return Promise.resolve();
}
if (!navigator.xr) {
setMsg(
'此浏览器没有暴露 WebXR(navigator.xr 不存在)。\n' +
'当前源:' + window.location.origin + '\n' +
'若是 http:// 源,请先按 chrome://flags 的 insecure 方式授权后 Relaunch。'
);
return Promise.resolve();
}
setMsg('');
// IMPORTANT: requestSession() must be called synchronously inside the click's
// transient-activation task. Never await anything before it, or the UA rejects it.
var sessionPromise;
try {
log('requesting immersive-vr …');
sessionPromise = navigator.xr.requestSession('immersive-vr', {
optionalFeatures: ['local-floor', 'bounded-floor', 'layers', 'hand-tracking']
});
} catch (e) {
log('requestSession threw', e);
setMsg('进入 VR 失败:' + (e && e.message ? e.message : e));
return Promise.resolve();
}
// keep the video rolling, then leave FS / PiP (these may await freely now)
try { video.play().catch(function () {}); } catch (e) {}
var pre = Promise.resolve();
try { if (document.pictureInPictureElement) pre = document.exitPictureInPicture().catch(function () {}); } catch (e) {}
try { if (document.fullscreenElement) pre = pre.then(function () { return document.exitFullscreen().catch(function () {}); }); } catch (e) {}
return pre.then(function () { return sessionPromise; }).then(function (session) {
S.session = session;
log('session started', session);
// ---------------------------------------------------------------
// THE STUTTER FIX.
// The video is 59.94 fps. An XR session at 90 Hz shows each video frame
// 1 or 2 refreshes alternately (90/59.94 = 1.5) - classic 3:2 pulldown
// judder. 120 Hz is an exact multiple (2:1), so every video frame is
// shown the same number of refreshes.
// ---------------------------------------------------------------
try {
var rates = session.supportedFrameRates
? Array.prototype.slice.call(session.supportedFrameRates) : [];
var want = rates.indexOf(120) >= 0 ? 120 : (rates.length ? rates[rates.length - 1] : 0);
S.stats.supportedFrameRates = rates;
if (want && typeof session.updateTargetFrameRate === 'function') {
session.updateTargetFrameRate(want);
S.stats.targetFrameRate = want;
log('updateTargetFrameRate ->', want, 'Hz (supported: ' + rates.join('/') + ')');
}
} catch (e) {
log('updateTargetFrameRate failed', e);
}
session.addEventListener('end', function () {
log('session ended', S.stats);
stopRafPump();
S.session = null;
S.subLayer = null; S.subCanvas = null; S.subBinding = null;
S.gl = null; S.prog = null; S.tex = null; S.refSpace = null;
restoreSubtitleCanvas();
setMsg('');
try { video.play().catch(function () {}); } catch (e) {}
}, { once: true });
var useLayers = CAP.hasEquirectLayer || (typeof XRMediaBinding !== 'undefined' &&
typeof XRMediaBinding.prototype.createEquirectLayer === 'function');
log('XRMediaBinding available:', useLayers);
return useLayers ? startEquirectLayer(session, video) : startWebGLFallback(session, video);
}).then(function () {
try { video.play().catch(function () {}); } catch (e) {}
setMsg('已进入 VR。摘下头显或在系统菜单退出即可返回。');
}).catch(function (e) {
log('enterVR failed', e);
setMsg('进入 VR 失败:' + (e && e.message ? e.message : e));
});
}
/* ---------- path A: XRMediaBinding equirect layer (preferred) ---------- */
function startEquirectLayer(session, video) {
return session.requestReferenceSpace('local').then(function (refSpace) {
startRafPump(video);
var binding = new XRMediaBinding(session);
var opts = {
space: refSpace,
layout: stereoLayout(),
centralHorizontalAngle: centralAngle(),
upperVerticalAngle: Math.PI / 2,
lowerVerticalAngle: -Math.PI / 2
};
log('createEquirectLayer', JSON.stringify({
layout: opts.layout,
centralHorizontalAngle: opts.centralHorizontalAngle
}));
var layer = binding.createEquirectLayer(video, opts);
var layers = [layer];
// --- subtitle layer (libass canvas composited on top, same stereo geometry) ---
if (S.subtitles) {
try {
var sub = createSubtitleLayer(session, refSpace);
if (sub) { layers.push(sub); S.stats.subLayer = true; }
else { S.stats.subLayer = false; }
} catch (e) {
log('subtitle layer failed', e);
S.stats.subLayer = 'error: ' + (e && e.message);
}
}
session.updateRenderState({ layers: layers });
S.stats.path = 'XRMediaBinding.equirect (+' + (layers.length - 1) + ' sub)';
var frames = 0, lastFpsT = 0;
var loop = function (time, frame) {
if (S.session !== session) return;
frames++;
if (!lastFpsT) lastFpsT = time;
if (time - lastFpsT >= 1000) {
S.stats.fps = +(frames * 1000 / (time - lastFpsT)).toFixed(1);
S.stats.frames += frames;
frames = 0;
lastFpsT = time;
}
if (S.subLayer && frame) {
try { drawSubtitleFrame(frame); } catch (e) {
if (!S.stats.subErr) S.stats.subErr = String((e && e.message) || e);
}
}
session.requestAnimationFrame(loop);
};
session.requestAnimationFrame(loop);
});
}
/* ---------- subtitle layer: dynamic equirect layer drawn from the libass canvas ---------- */
var SUB_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');
var SUB_FS = [
'#version 300 es',
'precision mediump float;',
'in vec2 vUv;',
'uniform sampler2D uTex;',
'out vec4 outColor;',
'void main(){ outColor = texture(uTex, vUv); }'
].join('\n');
// libass sizes its canvas to the on-screen video box (~1416x708), i.e.
// ~4 px per degree on the 180 deg sphere - badly blurry. Blow the player box
// up so libass re-renders at a usable resolution (it lands on 4320x2160,
// still exactly 2:1).
//
// This MUST happen before the immersive session starts: libass does not
// recompute its backing store while an XR session is running. The panel's
// open click is the natural place, since entering VR requires the panel.
function enlargeSubtitleCanvas() {
var video = S.video;
if (!video || S.sizeBackup) return;
var holder = document.querySelector('.videoPlayerContainer') || video;
S.sizeBackup = { holder: holder.style.cssText, video: video.style.cssText };
var grow = ';width:7680px !important;height:3840px !important;' +
'max-width:none !important;max-height:none !important;';
holder.style.cssText += grow;
video.style.cssText += grow;
window.dispatchEvent(new Event('resize'));
document.dispatchEvent(new Event('resize'));
log('subtitle canvas enlargement requested');
}
function restoreSubtitleCanvas() {
if (!S.sizeBackup) return;
var video = S.video;
var holder = document.querySelector('.videoPlayerContainer') || video;
try {
if (holder) holder.style.cssText = S.sizeBackup.holder;
if (video) video.style.cssText = S.sizeBackup.video;
window.dispatchEvent(new Event('resize'));
} catch (e) {}
S.sizeBackup = null;
log('subtitle canvas size restored');
}
function createSubtitleLayer(session, space) {
var canvas = document.querySelector('canvas.libassjs-canvas');
if (!canvas) { log('no libass canvas - skipping subtitle layer'); return null; }
if (typeof XRWebGLBinding === 'undefined') { log('no XRWebGLBinding'); return null; }
// NOTE: the canvas must already have been enlarged (see togglePanel ->
// enlargeSubtitleCanvas). libass does not recompute its backing store while
// an immersive session is running, so this can only be prepared beforehand.
S.subCanvas = canvas;
var gl = document.createElement('canvas').getContext('webgl2', {
xrCompatible: true, alpha: true, premultipliedAlpha: true, antialias: false, depth: false
});
if (!gl) { log('no webgl2 for subtitle layer'); return null; }
S.subGl = gl;
var prog = gl.createProgram();
gl.attachShader(prog, compile(gl, gl.VERTEX_SHADER, SUB_VS));
gl.attachShader(prog, compile(gl, gl.FRAGMENT_SHADER, SUB_FS));
gl.linkProgram(prog);
if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {
throw new Error(gl.getProgramInfoLog(prog) || 'subtitle program link failed');
}
gl.useProgram(prog);
S.subProg = prog;
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(prog, 'aPos');
gl.enableVertexAttribArray(loc);
gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);
gl.uniform1i(gl.getUniformLocation(prog, 'uTex'), 0);
var tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
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);
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
S.subTex = tex;
gl.disable(gl.DEPTH_TEST);
gl.disable(gl.BLEND);
var fbo = gl.createFramebuffer();
S.subFbo = fbo;
var binding = new XRWebGLBinding(session, gl);
S.subBinding = binding;
// dynamic (no-texture) equirect layer; OculusBrowser requires viewPixel* here
var layer = binding.createEquirectLayer({
space: space,
layout: 'stereo-left-right',
centralHorizontalAngle: centralAngle(),
upperVerticalAngle: Math.PI / 2,
lowerVerticalAngle: -Math.PI / 2,
viewPixelWidth: 4096,
viewPixelHeight: 2048,
alpha: true
});
S.subLayer = layer;
log('subtitle layer created', canvas.width + 'x' + canvas.height);
return layer;
}
function drawSubtitleFrame(frame) {
var gl = S.subGl, layer = S.subLayer, canvas = S.subCanvas;
if (!gl || !layer || !canvas || !canvas.width) return;
// Subtitles change a few times per second at most: uploading a multi-megabyte
// canvas on every 120 Hz tick is pure waste. ~30 Hz is plenty.
S.stats.subFrame = (S.stats.subFrame || 0) + 1;
if (S.stats.subFrame % 4 !== 0) return;
var sub = S.subBinding.getSubImage(layer, frame);
if (!sub || !sub.colorTexture) return;
gl.bindFramebuffer(gl.FRAMEBUFFER, S.subFbo);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, sub.colorTexture, 0);
gl.viewport(0, 0, sub.colorTextureWidth || 4096, sub.colorTextureHeight || 2048);
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, S.subTex);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas);
gl.useProgram(S.subProg);
gl.drawArrays(gl.TRIANGLES, 0, 3);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
/* ---------- path B: WebGL2 equirect ray reconstruction ---------- */
var VS = [
'#version 300 es',
'in vec2 aPos;',
'uniform mat4 uInvProj;',
'uniform mat4 uInvView;',
'out vec3 vDir;',
'void main(){',
' vec4 p = uInvProj * vec4(aPos, -1.0, 1.0);',
' vec3 dirView = p.xyz / p.w;',
' vDir = (uInvView * vec4(dirView, 0.0)).xyz;',
' gl_Position = vec4(aPos, 0.0, 1.0);',
'}'
].join('\n');
var FS = [
'#version 300 es',
'precision highp float;',
'in vec3 vDir;',
'uniform sampler2D uTex;',
'uniform float uCentral;',
'uniform int uStereo;',
'uniform int uEye;',
'out vec4 outColor;',
'const float PI = 3.14159265359;',
'void main(){',
' vec3 d = normalize(vDir);',
' float lon = atan(d.x, -d.z);',
' float lat = asin(clamp(d.y, -1.0, 1.0));',
' float u = 0.5 + lon / uCentral;',
' float v = 0.5 - lat / PI;',
' if (u < 0.0 || u > 1.0) { outColor = vec4(0.0,0.0,0.0,1.0); return; }',
' vec2 uv = vec2(u, v);',
' if (uStereo == 1) uv.x = mix(u * 0.5, 0.5 + u * 0.5, float(uEye));',
' else if (uStereo == 2) uv.y = mix(v * 0.5, 0.5 + v * 0.5, float(uEye));',
' outColor = texture(uTex, uv);',
'}'
].join('\n');
function compile(gl, type, src) {
var sh = gl.createShader(type);
gl.shaderSource(sh, src);
gl.compileShader(sh);
if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) {
throw new Error(gl.getShaderInfoLog(sh) || 'shader compile failed');
}
return sh;
}
function invert4(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
]);
}
function startWebGLFallback(session, video) {
log('fallback: WebGL2 renderer');
startRafPump(video);
return session.requestReferenceSpace('local').then(function (refSpace) {
S.refSpace = refSpace;
var canvas = document.createElement('canvas');
var gl = canvas.getContext('webgl2', { xrCompatible: true, alpha: false, antialias: false });
if (!gl) throw new Error('WebGL2 不可用');
S.gl = gl;
var prog = gl.createProgram();
gl.attachShader(prog, compile(gl, gl.VERTEX_SHADER, VS));
gl.attachShader(prog, compile(gl, gl.FRAGMENT_SHADER, FS));
gl.linkProgram(prog);
if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {
throw new Error(gl.getProgramInfoLog(prog) || 'program link failed');
}
gl.useProgram(prog);
S.prog = prog;
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(prog, 'aPos');
gl.enableVertexAttribArray(loc);
gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);
S.uInvProj = gl.getUniformLocation(prog, 'uInvProj');
S.uView = gl.getUniformLocation(prog, 'uInvView');
S.uCentral = gl.getUniformLocation(prog, 'uCentral');
S.uStereo = gl.getUniformLocation(prog, 'uStereo');
S.uEye = gl.getUniformLocation(prog, 'uEye');
var tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
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);
S.tex = tex;
gl.uniform1i(gl.getUniformLocation(prog, 'uTex'), 0);
var layer = new XRWebGLLayer(session, gl);
session.updateRenderState({ baseLayer: layer });
var stereoI = S.stereo === 'sbs' ? 1 : S.stereo === 'ou' ? 2 : 0;
var onFrame = function (time, frame) {
if (S.session !== session) return;
session.requestAnimationFrame(onFrame);
var pose = frame.getViewerPose(S.refSpace);
if (!pose) return;
var glLayer = session.renderState.baseLayer;
gl.bindFramebuffer(gl.FRAMEBUFFER, glLayer.framebuffer);
gl.useProgram(prog);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, tex);
if (video.readyState >= 2) {
try {
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, video);
} catch (e) {
if (!onFrame.__warned) { onFrame.__warned = true; log('texImage2D failed', e); }
}
}
gl.uniform1f(S.uCentral, centralAngle());
gl.uniform1i(S.uStereo, stereoI);
for (var i = 0; i < pose.views.length; i++) {
var view = pose.views[i];
var vp = glLayer.getViewport(view);
gl.viewport(vp.x, vp.y, vp.width, vp.height);
gl.uniformMatrix4fv(S.uInvProj, false, invert4(view.projectionMatrix));
gl.uniformMatrix4fv(S.uView, false, view.transform.inverse.matrix);
gl.uniform1i(S.uEye, view.eye === 'right' ? 1 : 0);
gl.drawArrays(gl.TRIANGLES, 0, 3);
}
};
session.requestAnimationFrame(onFrame);
});
}
/* ============================================================
* boot
* ========================================================== */
function boot() {
log('loaded v' + VERSION + ' | secure=' + window.isSecureContext +
' xr=' + !!navigator.xr + ' XRMediaBinding=' + (typeof XRMediaBinding !== 'undefined'));
scan();
new MutationObserver(function () { scan(); }).observe(document.documentElement, {
childList: true, subtree: true
});
setInterval(scan, 1500);
window.addEventListener('beforeunload', function () {
if (S.session) { try { S.session.end(); } catch (e) {} }
});
// warm the capability probe so the panel opens instantly
probeAsync().then(function () { log('capabilities', CAP); });
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', boot, { once: true });
} else {
boot();
}
})();