diff --git a/dist/jfvr.js b/dist/jfvr.js index 7c4465e..2b5d845 100644 --- a/dist/jfvr.js +++ b/dist/jfvr.js @@ -320,6 +320,7 @@ /* MediaCapabilities verdicts, keyed ':'. See below. */ codec: {}, codecBlocked: [], + codecKept: [], cptPatched: false, cptMode: null }; @@ -359,24 +360,32 @@ * 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". + * Only a definite "no" downgrades, but "no" has two shapes and both matter: * - * Kill switch if a device reports nonsense: - * localStorage['jfvr-cpt'] = 'off' then reload + * 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 it exists for. + // 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, 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' } + { 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' } ]; /** @@ -427,11 +436,34 @@ * 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() { - NS.caps.codecBlocked = CODEC_PROBES.filter(function (p) { + var keep = readKeepList(); + NS.caps.codecKept = []; + NS.caps.codecBlocked = []; + CODEC_PROBES.forEach(function (p) { var v = NS.caps.codec[p.key]; - return v && v.supported === false; - }).map(function (p) { return p.name; }); + 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() { @@ -445,7 +477,7 @@ 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 ''; + if (v && blockReason(v, NS.caps.__keep || [])) return ''; } catch (e) { /* never break the native answer */ } return orig.apply(this, arguments); }; @@ -461,6 +493,10 @@ 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. @@ -472,20 +508,21 @@ type: 'file', video: { contentType: 'video/mp4; codecs="' + p.codec + '"', - width: p.w, height: p.h, bitrate: 20000000, framerate: p.fps + width: p.w, height: p.h, bitrate: p.mbps * 1000000, 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, + 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] = { name: p.name, error: String(e && e.message || 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.log('codec probe done; blocked = ' + (NS.caps.codecBlocked.join(', ') || 'none') + + (NS.caps.codecKept.length ? '; pinned = ' + NS.caps.codecKept.join(', ') : '')); } }); }); @@ -493,13 +530,16 @@ 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 + ']' + (v.supported === false ? ' <-- BLOCKED' : ''); + ' [' + v.workload + ']' + + (why ? ' <-- BLOCKED (' + why + ')' : (keep.indexOf(p.key) >= 0 ? ' <-- pinned' : '')); }).join('\n'); return [ @@ -513,7 +553,7 @@ 'MAX_TEXTURE_SIZE : ' + c.maxTextureSize, 'HEVC canPlayType : ' + JSON.stringify(c.hevc), 'canPlayType hook : ' + (c.cptMode || 'pending') + - (c.cptMode === 'on' ? ' (downgraded: ' + (c.codecBlocked.join(', ') || 'nothing yet') + ')' : ''), + (c.cptMode === 'on' ? ' (blocked: ' + (c.codecBlocked.join(', ') || 'nothing yet') + ')' : ''), 'HEVC real (decodingInfo):', real ].join('\n'); diff --git a/src/00-core.js b/src/00-core.js index 027cfbb..93919a5 100644 --- a/src/00-core.js +++ b/src/00-core.js @@ -312,6 +312,7 @@ /* MediaCapabilities verdicts, keyed ':'. See below. */ codec: {}, codecBlocked: [], + codecKept: [], cptPatched: false, cptMode: null }; @@ -351,24 +352,32 @@ * 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". + * Only a definite "no" downgrades, but "no" has two shapes and both matter: * - * Kill switch if a device reports nonsense: - * localStorage['jfvr-cpt'] = 'off' then reload + * 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 it exists for. + // 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, 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' } + { 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' } ]; /** @@ -419,11 +428,34 @@ * 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() { - NS.caps.codecBlocked = CODEC_PROBES.filter(function (p) { + var keep = readKeepList(); + NS.caps.codecKept = []; + NS.caps.codecBlocked = []; + CODEC_PROBES.forEach(function (p) { var v = NS.caps.codec[p.key]; - return v && v.supported === false; - }).map(function (p) { return p.name; }); + 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() { @@ -437,7 +469,7 @@ 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 ''; + if (v && blockReason(v, NS.caps.__keep || [])) return ''; } catch (e) { /* never break the native answer */ } return orig.apply(this, arguments); }; @@ -453,6 +485,10 @@ 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. @@ -464,20 +500,21 @@ type: 'file', video: { contentType: 'video/mp4; codecs="' + p.codec + '"', - width: p.w, height: p.h, bitrate: 20000000, framerate: p.fps + width: p.w, height: p.h, bitrate: p.mbps * 1000000, 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, + 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] = { name: p.name, error: String(e && e.message || 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.log('codec probe done; blocked = ' + (NS.caps.codecBlocked.join(', ') || 'none') + + (NS.caps.codecKept.length ? '; pinned = ' + NS.caps.codecKept.join(', ') : '')); } }); }); @@ -485,13 +522,16 @@ 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 + ']' + (v.supported === false ? ' <-- BLOCKED' : ''); + ' [' + v.workload + ']' + + (why ? ' <-- BLOCKED (' + why + ')' : (keep.indexOf(p.key) >= 0 ? ' <-- pinned' : '')); }).join('\n'); return [ @@ -505,7 +545,7 @@ 'MAX_TEXTURE_SIZE : ' + c.maxTextureSize, 'HEVC canPlayType : ' + JSON.stringify(c.hevc), 'canPlayType hook : ' + (c.cptMode || 'pending') + - (c.cptMode === 'on' ? ' (downgraded: ' + (c.codecBlocked.join(', ') || 'nothing yet') + ')' : ''), + (c.cptMode === 'on' ? ' (blocked: ' + (c.codecBlocked.join(', ') || 'nothing yet') + ')' : ''), 'HEVC real (decodingInfo):', real ].join('\n');