caps: ask the real decoder, and stop the client from over-claiming HEVC

The capability probe only ever asked canPlayType about Main profile (8-bit)
codec strings, which is why the project notes said "8K HEVC: supported" right up
until a Main 10 file showed up and could not be decoded at all. It now also asks
about Main 10, and -- more importantly -- asks the decoder itself.

The override: Jellyfin Web derives its entire HEVC claim from canPlayType
(browserDeviceProfile.js):

    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 60fps". A desktop that merely ships an HEVC decoder
therefore claims 8K Main 10, the server picks DirectPlay, and playback dies on a
black screen with no player UI at all. MediaCapabilities.decodingInfo asks the
actual decoder, so its verdict is fed back through canPlayType and Jellyfin's own
logic lands on the right profile -- the desktop drops to what 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.

Deliberately conservative: only supported === false downgrades.
supported:true/smooth:false is recorded but changes nothing -- too close to the
edge to act on, and the watchdog already covers "claimed to work, did not".
Kill switch if a device reports nonsense: localStorage['jfvr-cpt'] = 'off'.

Each level is probed at the resolution it exists for (L120 -> 1080p30,
L153 -> 4K60, L183/L186 -> 8K60), because a level string carries no dimensions.

Verified against a local harness in three modes, all passing:
  pc    -> 8K Main10 blocked, Jellyfin caps at level 153, the 8K Main 10 file
           exceeds the claim and the server transcodes
  quest -> nothing blocked, claim stays >= 183, DirectPlay preserved
  off   -> hook not installed, canPlayType untouched, old behaviour restored
Also re-ran the playback watchdog on the new bundle: still fires on media error.

Also widens the harness gitignore to _harness*.html for the new codec harness.
This commit is contained in:
2026-10-18 01:07:44 +08:00
parent bfcd102ed0
commit ab36d2f25c
3 changed files with 325 additions and 9 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ secrets*.md
# deploy.py --legacy actually uses), so only the legacy one is tracked.
/jfvr-mvp.js
# Temporary offline test page (fake <video> + libass canvas + XR stubs).
_harness.html
_harness*.html
# --- build / tooling noise --------------------------------------------
__pycache__/
+162 -4
View File
@@ -315,9 +315,81 @@
hasGetJointPose: typeof window.XRFrame !== 'undefined' && typeof XRFrame.prototype.getJointPose === 'function',
maxTextureSize: null,
hevc: {},
decoding8k: null
decoding8k: null,
/* MediaCapabilities verdicts, keyed '<profile_idc>:<level>'. See below. */
codec: {},
codecBlocked: [],
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 `supported === false` downgrades. `supported:true, smooth:false` is
* recorded but does not change behaviour: it is too close to the edge to act
* on, and 40-watchdog.js already covers "it claimed to work and did not".
*
* Kill switch if a device reports nonsense:
* localStorage['jfvr-cpt'] = 'off' then reload
*/
// decodingInfo needs real dimensions; a level string does not carry them, so
// each level is probed at the resolution it exists for.
var CODEC_PROBES = [
{ key: '1:120', name: 'Main L4.0', w: 1920, h: 1080, fps: 30, codec: 'hvc1.1.L120' },
{ key: '1:123', name: 'Main L4.1', w: 1920, h: 1080, fps: 60, codec: 'hvc1.1.4.L123' },
{ key: '1:183', name: 'Main L6.1 (8K)', w: 8192, h: 4096, fps: 60, codec: 'hvc1.1.6.L183.B0' },
{ key: '2:123', name: 'Main10 L4.1', w: 1920, h: 1080, fps: 60, codec: 'hvc1.2.4.L123' },
{ key: '2:153', name: 'Main10 L5.1 (4K)', w: 3840, h: 2160, fps: 60, codec: 'hvc1.2.4.L153' },
{ key: '2:183', name: 'Main10 L6.1 (8K)', w: 8192, h: 4096, fps: 60, codec: 'hvc1.2.4.L183' },
{ key: '2:186', name: 'Main10 L6.2 (8K60)', w: 8192, h: 4096, fps: 60, 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;
@@ -331,12 +403,17 @@
if (gl) NS.caps.maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
} catch (e) {}
try {
['video/mp4; codecs="hev1.1.6.L183.B0"',
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)) ||
document.createElement('video').canPlayType(cs) || 'no';
ve.canPlayType(cs) || 'no';
});
} catch (e) {}
if (navigator.xr && navigator.xr.isSessionSupported) {
@@ -346,8 +423,85 @@
}
})();
/* ------------------------------------------------------------------ *
* MediaCapabilities probe + canPlayType override
* ------------------------------------------------------------------ */
function applyVerdicts() {
NS.caps.codecBlocked = CODEC_PROBES.filter(function (p) {
var v = NS.caps.codec[p.key];
return v && v.supported === false;
}).map(function (p) { return 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 && v.supported === false) 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; }
// 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: 20000000, framerate: p.fps
}
};
mc.decodingInfo(cfg).then(function (info) {
NS.caps.codec[p.key] = {
name: p.name, workload: p.w + 'x' + p.h + '@' + p.fps,
supported: info.supported, smooth: info.smooth, powerEfficient: info.powerEfficient
};
}).catch(function (e) {
NS.caps.codec[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.capsDiag = function () {
var c = NS.caps;
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;
return ' ' + p.name.padEnd(20) + ' supported=' + v.supported +
' smooth=' + v.smooth + ' powerEfficient=' + v.powerEfficient +
' [' + v.workload + ']' + (v.supported === false ? ' <-- BLOCKED' : '');
}).join('\n');
return [
'origin : ' + location.origin,
'secureContext : ' + c.secureContext,
@@ -357,7 +511,11 @@
'createQuadLayer : ' + c.hasQuadLayer,
'hand tracking API : ' + c.hasHandTracking + ' / jointSpace=' + c.hasXRJointSpace + ' / getJointPose=' + c.hasGetJointPose,
'MAX_TEXTURE_SIZE : ' + c.maxTextureSize,
'HEVC : ' + JSON.stringify(c.hevc)
'HEVC canPlayType : ' + JSON.stringify(c.hevc),
'canPlayType hook : ' + (c.cptMode || 'pending') +
(c.cptMode === 'on' ? ' (downgraded: ' + (c.codecBlocked.join(', ') || 'nothing yet') + ')' : ''),
'HEVC real (decodingInfo):',
real
].join('\n');
};
})();
+162 -4
View File
@@ -307,9 +307,81 @@
hasGetJointPose: typeof window.XRFrame !== 'undefined' && typeof XRFrame.prototype.getJointPose === 'function',
maxTextureSize: null,
hevc: {},
decoding8k: null
decoding8k: null,
/* MediaCapabilities verdicts, keyed '<profile_idc>:<level>'. See below. */
codec: {},
codecBlocked: [],
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 `supported === false` downgrades. `supported:true, smooth:false` is
* recorded but does not change behaviour: it is too close to the edge to act
* on, and 40-watchdog.js already covers "it claimed to work and did not".
*
* Kill switch if a device reports nonsense:
* localStorage['jfvr-cpt'] = 'off' then reload
*/
// decodingInfo needs real dimensions; a level string does not carry them, so
// each level is probed at the resolution it exists for.
var CODEC_PROBES = [
{ key: '1:120', name: 'Main L4.0', w: 1920, h: 1080, fps: 30, codec: 'hvc1.1.L120' },
{ key: '1:123', name: 'Main L4.1', w: 1920, h: 1080, fps: 60, codec: 'hvc1.1.4.L123' },
{ key: '1:183', name: 'Main L6.1 (8K)', w: 8192, h: 4096, fps: 60, codec: 'hvc1.1.6.L183.B0' },
{ key: '2:123', name: 'Main10 L4.1', w: 1920, h: 1080, fps: 60, codec: 'hvc1.2.4.L123' },
{ key: '2:153', name: 'Main10 L5.1 (4K)', w: 3840, h: 2160, fps: 60, codec: 'hvc1.2.4.L153' },
{ key: '2:183', name: 'Main10 L6.1 (8K)', w: 8192, h: 4096, fps: 60, codec: 'hvc1.2.4.L183' },
{ key: '2:186', name: 'Main10 L6.2 (8K60)', w: 8192, h: 4096, fps: 60, 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;
@@ -323,12 +395,17 @@
if (gl) NS.caps.maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
} catch (e) {}
try {
['video/mp4; codecs="hev1.1.6.L183.B0"',
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)) ||
document.createElement('video').canPlayType(cs) || 'no';
ve.canPlayType(cs) || 'no';
});
} catch (e) {}
if (navigator.xr && navigator.xr.isSessionSupported) {
@@ -338,8 +415,85 @@
}
})();
/* ------------------------------------------------------------------ *
* MediaCapabilities probe + canPlayType override
* ------------------------------------------------------------------ */
function applyVerdicts() {
NS.caps.codecBlocked = CODEC_PROBES.filter(function (p) {
var v = NS.caps.codec[p.key];
return v && v.supported === false;
}).map(function (p) { return 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 && v.supported === false) 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; }
// 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: 20000000, framerate: p.fps
}
};
mc.decodingInfo(cfg).then(function (info) {
NS.caps.codec[p.key] = {
name: p.name, workload: p.w + 'x' + p.h + '@' + p.fps,
supported: info.supported, smooth: info.smooth, powerEfficient: info.powerEfficient
};
}).catch(function (e) {
NS.caps.codec[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.capsDiag = function () {
var c = NS.caps;
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;
return ' ' + p.name.padEnd(20) + ' supported=' + v.supported +
' smooth=' + v.smooth + ' powerEfficient=' + v.powerEfficient +
' [' + v.workload + ']' + (v.supported === false ? ' <-- BLOCKED' : '');
}).join('\n');
return [
'origin : ' + location.origin,
'secureContext : ' + c.secureContext,
@@ -349,7 +503,11 @@
'createQuadLayer : ' + c.hasQuadLayer,
'hand tracking API : ' + c.hasHandTracking + ' / jointSpace=' + c.hasXRJointSpace + ' / getJointPose=' + c.hasGetJointPose,
'MAX_TEXTURE_SIZE : ' + c.maxTextureSize,
'HEVC : ' + JSON.stringify(c.hevc)
'HEVC canPlayType : ' + JSON.stringify(c.hevc),
'canPlayType hook : ' + (c.cptMode || 'pending') +
(c.cptMode === 'on' ? ' (downgraded: ' + (c.codecBlocked.join(', ') || 'nothing yet') + ')' : ''),
'HEVC real (decodingInfo):',
real
].join('\n');
};
})();