hud-9: render native (SRT) subtitles in the HUD; stop persisting the layout override
ADDENDUM 10. Two more independent subtitle problems found on device.
1. Normal (non-ASS) subtitles never reached the HUD. Jellyfin's
htmlVideoPlayer-plugin only routes ssa/ass through SubtitlesOctopus
(`if (!['ssa','ass'].includes(codec))` -> pgssub/vobsub/custom-element/native);
subrip falls through to the browser's own TextTrack rendering, so there is no
libass canvas at all (measured with the CN SRT selected: 0 libass canvases,
0 .videoSubtitles overlays, textTracks[0] = manualTrack/mode=showing/315 cues).
No layout setting could have made that work.
updateSource() now picks the source every XR frame: the libass canvas when it
exists, otherwise the active TextTrack cue (fallback: the .videoSubtitlesInner
overlay). The native text is cleaned, wrapped and painted white-on-black into a
private 1920x1080 canvas that flows through the unchanged crop/upload/HUD path.
prepare() is skipped when there is no libass canvas. New diagnostics:
stats.subSource / stats.subNativeText, Subtitles.getSourceKind().
2. ADDENDUM 9 persisted state.subLayout, and a stale 'mono' in jfvr.settings.v1
silently forced the mono branch on the default dual-eye content (dual:false,
eyeW:4320, crop across the seam -> both eyes side by side). The override is now
per-session: not saved, not restored, 'auto' at boot.
Verified on device with hud-9:
CN_dual_eye (ass) -> subSource=libass, dual=true, eyeW=2160,
crop=[1040,768,1176,924], copyND=0.055
CN (subrip) -> subSource=native, dual=false, eyeW=1920, text="进去了呢",
crop 256x88, uploads climbing
The private cue canvas was read back and OCR'd (cue "怎么样?好好地" -> "怎么样? 好好地"),
and injecting/removing a fake libass canvas flips subSource with no errors.
This commit is contained in:
Vendored
+211
-12
@@ -19,7 +19,7 @@
|
||||
|
||||
var NS = window.__JFVR = window.__JFVR || {};
|
||||
|
||||
NS.version = 'hud-8'; // hud-8 = ADDENDUM 9 (rAF broker always-on so the libass loop survives XR entry; auto dual/mono subtitle layout)
|
||||
NS.version = 'hud-9'; // hud-9 = ADDENDUM 10 (native/TextTrack subtitles painted by the HUD; layout override per-session, ADDENDUM 9 rAF broker + auto dual/mono layout)
|
||||
NS.__core = true;
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
@@ -439,6 +439,15 @@
|
||||
var CENTER_BAND_PX = 40; // source px around the eye seam that count as "centre ink"
|
||||
var COPY_TOL = 0.25; // column-profile copy distance for "two copies of one line"
|
||||
// (measured: dual 0.02-0.08, mono 1.00)
|
||||
// ADDENDUM 10 — Jellyfin only routes ssa/ass through SubtitlesOctopus (the
|
||||
// libass canvas). Every other text codec (subrip, vtt, ...) is drawn by the
|
||||
// BROWSER from a TextTrack (`video.addTextTrack` + mode 'showing'), so there is
|
||||
// no canvas at all to sample. The HUD then paints the active cue itself into a
|
||||
// private canvas and feeds it through the same crop/upload pipeline.
|
||||
var NATIVE_W = 1920, NATIVE_H = 1080;
|
||||
var NATIVE_FONT_H = 0.052; // cue font size as a fraction of the canvas height
|
||||
var NATIVE_LINE_H = 1.32; // line advance
|
||||
var NATIVE_MAX_W = 0.9; // max text width as a fraction of the canvas width
|
||||
var SPHERE_TEX_W = 4096; // legacy equirect layer size
|
||||
var SPHERE_TEX_H = 2048;
|
||||
var OPACITY_DEFAULT = 0.85; // dim the strip below full opacity by default
|
||||
@@ -515,6 +524,9 @@
|
||||
copyND: null, // diagnostics: normalised column-profile copy distance
|
||||
centerInk: null, // diagnostics: ink pixels in the seam band
|
||||
colL: null, colR: null, // reused column-profile scratch (half width each)
|
||||
// ADDENDUM 10: where the HUD's source pixels come from.
|
||||
srcKind: 'none', // 'libass' | 'native' | 'none'
|
||||
nativeCanvas: null, nativeCtx: null, nativeText: null,
|
||||
bbox: null,
|
||||
lastSample: null,
|
||||
hasInk: false,
|
||||
@@ -535,6 +547,174 @@
|
||||
catch (e) { return null; }
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* ADDENDUM 10 — native (non-libass) subtitle source
|
||||
* Jellyfin asks the browser to render subrip/vtt cues from a TextTrack, so the
|
||||
* HUD has no canvas to read. Read the active cue, paint it into a private
|
||||
* canvas, and let the normal crop/upload path take over from there.
|
||||
* ============================================================== */
|
||||
function ensureNativeCanvas() {
|
||||
if (!M.nativeCanvas) {
|
||||
try {
|
||||
M.nativeCanvas = document.createElement('canvas');
|
||||
M.nativeCanvas.width = NATIVE_W;
|
||||
M.nativeCanvas.height = NATIVE_H;
|
||||
M.nativeCtx = M.nativeCanvas.getContext('2d');
|
||||
// Kept in the DOM but hidden: Canvas2D works detached too, and this makes
|
||||
// the exact pixels the HUD uploads inspectable from DevTools.
|
||||
M.nativeCanvas.className = 'jfvr-native-subs';
|
||||
M.nativeCanvas.style.display = 'none';
|
||||
(document.body || document.documentElement).appendChild(M.nativeCanvas);
|
||||
} catch (e) { M.nativeCtx = null; }
|
||||
}
|
||||
return !!M.nativeCtx;
|
||||
}
|
||||
|
||||
// Jellyfin renders non-ASS text subtitles one of two ways depending on the
|
||||
// build: into a TextTrack (video.addTextTrack + mode 'showing', the browser
|
||||
// shows it through ::cue) or into a DOM overlay (div.videoSubtitlesInner).
|
||||
// Both leave no canvas behind.
|
||||
function hasNativeTracks() {
|
||||
try {
|
||||
var v = S.video;
|
||||
if (v && v.textTracks) {
|
||||
for (var i = 0; i < v.textTracks.length; i++) {
|
||||
var tr = v.textTracks[i];
|
||||
if (tr && tr.mode === 'showing' && tr.cues && tr.cues.length) return true;
|
||||
}
|
||||
}
|
||||
if (document.querySelector('.videoSubtitlesInner')) return true;
|
||||
} catch (e) {}
|
||||
return false;
|
||||
}
|
||||
|
||||
function activeNativeText() {
|
||||
var out = [];
|
||||
try {
|
||||
var v = S.video;
|
||||
if (v && v.textTracks) {
|
||||
for (var i = 0; i < v.textTracks.length; i++) {
|
||||
var tr = v.textTracks[i];
|
||||
if (!tr || tr.mode !== 'showing') continue;
|
||||
var cues = tr.activeCues; // maintained by the browser
|
||||
if (!cues) continue;
|
||||
for (var j = 0; j < cues.length; j++) {
|
||||
var s = cues[j] && cues[j].text;
|
||||
if (s) out.push(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!out.length) {
|
||||
var d = document.querySelector('.videoSubtitlesInner, .videoSubtitles');
|
||||
if (d) { var t = d.innerText || ''; if (t) out.push(t); }
|
||||
}
|
||||
} catch (e) { return ''; }
|
||||
return out.join('\n');
|
||||
}
|
||||
|
||||
function cleanCueText(s) {
|
||||
return String(s == null ? '' : s)
|
||||
.replace(/\{[^}]*\}/g, '') // ASS override tags
|
||||
.replace(/<[^>]*>/g, '') // html-ish tags
|
||||
.replace(/ /g, ' ').replace(/&/g, '&')
|
||||
.replace(/</g, '<').replace(/>/g, '>')
|
||||
.replace(/[\u200e\u200f\u202a-\u202e]/g, '') // bidi control marks
|
||||
.replace(/\r/g, '')
|
||||
.replace(/[ \t]+\n/g, '\n')
|
||||
.replace(/\n{3,}/g, '\n\n')
|
||||
.trim();
|
||||
}
|
||||
|
||||
function wrapCueLines(ctx, text, maxW) {
|
||||
var out = [];
|
||||
var paras = text.split('\n');
|
||||
for (var p = 0; p < paras.length; p++) {
|
||||
var para = paras[p];
|
||||
if (!para) { out.push(''); continue; }
|
||||
var line = '';
|
||||
for (var i = 0; i < para.length; i++) {
|
||||
var ch = para[i];
|
||||
if (line && ctx.measureText(line + ch).width > maxW) {
|
||||
var sp = line.lastIndexOf(' ');
|
||||
if (sp > 0 && /[A-Za-z0-9]/.test(line.charAt(line.length - 1))) {
|
||||
out.push(line.slice(0, sp));
|
||||
line = line.slice(sp + 1);
|
||||
} else {
|
||||
out.push(line);
|
||||
line = '';
|
||||
}
|
||||
}
|
||||
line += ch;
|
||||
}
|
||||
out.push(line);
|
||||
}
|
||||
return out.length ? out : [''];
|
||||
}
|
||||
|
||||
function drawNativeCue(text) {
|
||||
var c = M.nativeCanvas, ctx = M.nativeCtx;
|
||||
if (!c || !ctx) return;
|
||||
var W = c.width, H = c.height;
|
||||
ctx.clearRect(0, 0, W, H);
|
||||
if (!text) return;
|
||||
var fs = Math.max(12, Math.round(H * NATIVE_FONT_H));
|
||||
ctx.font = '600 ' + fs + 'px "Noto Sans SC","Noto Sans CJK SC","PingFang SC",' +
|
||||
'"Microsoft YaHei",system-ui,Arial,sans-serif';
|
||||
ctx.textAlign = 'center';
|
||||
ctx.textBaseline = 'middle';
|
||||
ctx.lineJoin = 'round';
|
||||
ctx.miterLimit = 2;
|
||||
var lines = wrapCueLines(ctx, text, W * NATIVE_MAX_W);
|
||||
var lh = fs * NATIVE_LINE_H;
|
||||
var y0 = H / 2 - (lines.length - 1) * lh / 2;
|
||||
ctx.lineWidth = Math.max(2, Math.round(fs * 0.1));
|
||||
ctx.strokeStyle = 'rgba(0,0,0,0.95)';
|
||||
ctx.fillStyle = '#ffffff';
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
ctx.strokeText(lines[i], W / 2, y0 + i * lh);
|
||||
ctx.fillText(lines[i], W / 2, y0 + i * lh);
|
||||
}
|
||||
}
|
||||
|
||||
// Decide, per frame, which canvas the HUD samples. libass wins when Jellyfin
|
||||
// created one; otherwise the native cue canvas. Returns false when there is no
|
||||
// subtitle source at all.
|
||||
function updateSource() {
|
||||
var lib = getCanvas();
|
||||
if (lib && lib.width > 0 && lib.height > 0) {
|
||||
if (M.srcKind !== 'libass') {
|
||||
M.srcKind = 'libass';
|
||||
M.canvas = lib;
|
||||
M.nativeText = null;
|
||||
M.lastSample = null; M.hasInk = false; M.bbox = null;
|
||||
computeTexDims();
|
||||
M.needRebuild = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (!hasNativeTracks()) {
|
||||
if (M.srcKind !== 'none') { M.srcKind = 'none'; M.hasInk = false; M.bbox = null; M.dirty = true; }
|
||||
return false;
|
||||
}
|
||||
if (!ensureNativeCanvas()) return false;
|
||||
var text = cleanCueText(activeNativeText());
|
||||
if (text !== M.nativeText) {
|
||||
M.nativeText = text;
|
||||
drawNativeCue(text);
|
||||
M.dirty = true;
|
||||
}
|
||||
if (M.srcKind !== 'native') {
|
||||
M.srcKind = 'native';
|
||||
M.canvas = M.nativeCanvas;
|
||||
M.dualEye = false; // our own canvas is always one centred line
|
||||
M.mismatch = 0;
|
||||
M.lastSample = null; M.hasInk = false; M.bbox = null;
|
||||
computeTexDims();
|
||||
M.needRebuild = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function getHolder(video) {
|
||||
var holder = null;
|
||||
try { holder = document.querySelector('.videoPlayerContainer'); } catch (e) {}
|
||||
@@ -549,6 +729,8 @@
|
||||
st.subLayer = !!M.layer;
|
||||
st.subPrepared = !!M.prepared;
|
||||
st.subCanvasSize = M.canvas ? (M.canvas.width + 'x' + M.canvas.height) : null;
|
||||
st.subSource = M.srcKind;
|
||||
st.subNativeText = M.nativeText ? M.nativeText.slice(0, 60) : null;
|
||||
st.subLayout = M.layout;
|
||||
st.subDual = M.dualEye;
|
||||
st.subEyeW = M.eyeW;
|
||||
@@ -578,6 +760,10 @@
|
||||
* ============================================================== */
|
||||
function prepare() {
|
||||
if (M.prepared) return; // idempotent via a live flag, not a backup check
|
||||
// Only libass needs the box blown up so it re-renders bigger. The native cue
|
||||
// canvas is ours, so resizing the player would be pointless (and would move
|
||||
// the browser's own cue overlay around).
|
||||
if (!getCanvas()) { log('prepare skipped: native subtitle source'); return; }
|
||||
var video = S.video || null;
|
||||
var holder = getHolder(video);
|
||||
try {
|
||||
@@ -824,10 +1010,6 @@
|
||||
|
||||
if (M.mode === 'off') { log('create: subtitle mode off'); return null; }
|
||||
|
||||
var canvas = getCanvas();
|
||||
if (!canvas || !canvas.width || !canvas.height) {
|
||||
setErr('no libass canvas'); log('create: no <canvas.libassjs-canvas>'); return null;
|
||||
}
|
||||
if (typeof window.XRWebGLBinding !== 'function') {
|
||||
setErr('no XRWebGLBinding'); log('create: no XRWebGLBinding'); return null;
|
||||
}
|
||||
@@ -839,11 +1021,17 @@
|
||||
var gl = NS.xrGl(true);
|
||||
if (!gl) { setErr('no webgl2'); log('create: no webgl2 context'); return null; }
|
||||
|
||||
M.canvas = canvas;
|
||||
M.gl = gl;
|
||||
M.glCanvas = gl.canvas;
|
||||
M.uploads = 0;
|
||||
M.samples = 0;
|
||||
// ADDENDUM 10: the source may be the libass canvas OR our own cue canvas.
|
||||
// Start on the native canvas so create() works even before Jellyfin has
|
||||
// created its renderer, then updateSource() upgrades to libass if it shows up.
|
||||
ensureNativeCanvas();
|
||||
M.canvas = M.nativeCanvas;
|
||||
M.srcKind = 'none';
|
||||
updateSource();
|
||||
computeTexDims();
|
||||
if (!M.texW || !M.texH) { setErr('bad canvas size'); log('create: bad canvas size'); destroy(); return null; }
|
||||
|
||||
@@ -890,13 +1078,15 @@
|
||||
|
||||
var layer = attachLayer(session);
|
||||
if (!layer) throw new Error('layer creation returned null');
|
||||
M.needRebuild = false; // the source was already resolved above
|
||||
|
||||
sample(); // seed bbox / hasInk before the first XR frame
|
||||
M.dirty = true;
|
||||
setErr(null);
|
||||
updateStats();
|
||||
log('subtitles layer created:', M.mode, M.texW + 'x' + M.texH,
|
||||
'alpha=' + M.alpha, 'canvas=' + canvas.width + 'x' + canvas.height);
|
||||
'alpha=' + M.alpha, 'source=' + M.srcKind,
|
||||
'canvas=' + (M.canvas ? M.canvas.width + 'x' + M.canvas.height : 'none'));
|
||||
return layer;
|
||||
} catch (e) {
|
||||
setErr((e && e.message) || e);
|
||||
@@ -922,6 +1112,7 @@
|
||||
} catch (e) {}
|
||||
M.gl = null; M.glCanvas = null; M.prog = null; M.tex = null; M.fbo = null;
|
||||
M.binding = null; M.session = null; M.canvas = null;
|
||||
M.srcKind = 'none'; M.nativeText = null;
|
||||
M.sampleCanvas = null; M.sampleCtx = null;
|
||||
M.cropCanvas = null; M.cropCtx = null;
|
||||
M.srcW = 0; M.srcH = 0; M.halfW = 1; M.fullW = 1; M.eyeW = 1; M.texW = 0; M.texH = 0;
|
||||
@@ -1071,7 +1262,8 @@
|
||||
if (hasInk) {
|
||||
M.centerInk = centerInk;
|
||||
var dual = M.dualEye;
|
||||
if (M.layout === 'dual') dual = true;
|
||||
if (M.srcKind === 'native') { M.copyND = null; dual = false; } // our own canvas is always mono
|
||||
else if (M.layout === 'dual') dual = true;
|
||||
else if (M.layout === 'mono') dual = false;
|
||||
else if (hasLeft && hasRight) {
|
||||
if (centerInk > 0) { M.copyND = null; dual = false; }
|
||||
@@ -1227,7 +1419,11 @@
|
||||
: (frame.predictedDisplayTime != null ? frame.predictedDisplayTime
|
||||
: (window.performance ? performance.now() : Date.now()));
|
||||
|
||||
if (t - M.lastSampleT >= SAMPLE_MS || M.lastSampleT < 0) {
|
||||
// Keep the source current every frame (native cue text changes on its own).
|
||||
var hasSrc = updateSource();
|
||||
if (!hasSrc) {
|
||||
if (M.hasInk) { M.hasInk = false; M.bbox = null; M.dirty = true; }
|
||||
} else if (t - M.lastSampleT >= SAMPLE_MS || M.lastSampleT < 0) {
|
||||
M.lastSampleT = t;
|
||||
sample();
|
||||
}
|
||||
@@ -1532,7 +1728,8 @@
|
||||
setOpacity: setOpacity,
|
||||
getOpacity: getOpacity,
|
||||
getLayer: getLayer,
|
||||
getMode: getMode
|
||||
getMode: getMode,
|
||||
getSourceKind: function () { return M.srcKind; }
|
||||
};
|
||||
})();
|
||||
|
||||
@@ -4369,7 +4566,9 @@
|
||||
S.subOpacity = num(o.subOpacity, 0.1, 1.0, S.subOpacity);
|
||||
S.ctlAutoHideMs = num(o.ctlAutoHideMs, 1000, 10000, S.ctlAutoHideMs);
|
||||
if (o.subMode === 'hud' || o.subMode === 'sphere') S.subMode = o.subMode;
|
||||
if (o.subLayout === 'auto' || o.subLayout === 'dual' || o.subLayout === 'mono') S.subLayout = o.subLayout;
|
||||
// subLayout is deliberately NOT restored: a persisted manual override
|
||||
// silently ruined the default dual-eye content once already (forced 'mono'
|
||||
// made the HUD show both eyes side by side). Layout always starts on 'auto'.
|
||||
// ADDENDUM 8: how the picture aim is applied. 'space' is the live path; the
|
||||
// 'rebuild' escape hatch exists because mutating a media layer's transform is
|
||||
// ignored by this runtime, so if an offset reference space turns out not to be
|
||||
@@ -4404,7 +4603,6 @@
|
||||
subAngleDeg: S.subAngleDeg,
|
||||
subOpacity: S.subOpacity,
|
||||
subMode: S.subMode,
|
||||
subLayout: S.subLayout,
|
||||
subAlpha: S.subAlpha,
|
||||
projection: S.projection,
|
||||
stereo: S.stereo,
|
||||
@@ -6005,6 +6203,7 @@
|
||||
// `subtitles` and `subMode` could otherwise leave the layer uncreated.
|
||||
S.subtitles = true;
|
||||
if (S.subMode === 'off') S.subMode = 'hud';
|
||||
S.subLayout = 'auto'; // ADDENDUM 10: the override is per-session only
|
||||
try { if (NS.Subtitles && NS.Subtitles.init) NS.Subtitles.init(); } catch (e) { NS.recordError('Subtitles.init', e); }
|
||||
try { if (NS.Controls && NS.Controls.init) NS.Controls.init(); } catch (e) { NS.recordError('Controls.init', e); }
|
||||
try { if (NS.Hands && NS.Hands.init) NS.Hands.init(); } catch (e) { NS.recordError('Hands.init', e); }
|
||||
|
||||
Reference in New Issue
Block a user