Compare commits
22
Commits
0713f35513
...
v0.5.3.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d6734e505c | ||
|
|
d20482d5dd | ||
|
|
cf731c16c2 | ||
|
|
03919dab98 | ||
|
|
beba1e66f0 | ||
|
|
0270cd1bc1 | ||
|
|
08cbef83a3 | ||
|
|
c25e40070a | ||
|
|
0bf0a1f1da | ||
|
|
1176c2009c | ||
|
|
30f97808ae | ||
|
|
85fea48159 | ||
|
|
986d30dfb4 | ||
|
|
ab36d2f25c | ||
|
|
bfcd102ed0 | ||
|
|
131e0349b8 | ||
|
|
d0fe04bf25 | ||
|
|
51ab1a4b48 | ||
|
|
5279f3929a | ||
|
|
ac85de2cb8 | ||
|
|
7e25861498 | ||
|
|
f4fe3d61fc |
+7
-1
@@ -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__/
|
||||
@@ -18,3 +18,9 @@ __pycache__/
|
||||
Thumbs.db
|
||||
*.swp
|
||||
*~
|
||||
|
||||
# --- .NET plugin build -------------------------------------------------
|
||||
plugin/**/bin/
|
||||
plugin/**/obj/
|
||||
# pack.py output: the packaged plugin zip (distributed via Gitea Releases)
|
||||
dist/plugin/
|
||||
|
||||
+260
-7
@@ -122,7 +122,7 @@ Style: RightEye, Alignment=8, MarginL=1920, MarginR=50 , MarginV=700
|
||||
| `XRWebGLBinding.createQuadLayer` | **true** |
|
||||
| `XRWebGLBinding.getSubImage` | **需要 2 个参数** `(layer, frame)` |
|
||||
| `MAX_TEXTURE_SIZE` / `MAX_RENDERBUFFER_SIZE` | **8192 / 16384** |
|
||||
| 8K HEVC `decodingInfo` | `supported:true, powerEfficient:true` |
|
||||
| 8K HEVC `decodingInfo` | `supported:true, powerEfficient:true`(仅 8-bit Main,见 ADDENDUM 12) |
|
||||
| `supportedFrameRates` | **[72, 80, 90, 120]** |
|
||||
| `enabledFeatures` | `[local, viewer, bounded-floor, layers, hand-tracking, webxr, local-floor]` |
|
||||
| 手部追踪 | ✅ 25 关节齐全,profiles `["oculus-hand","generic-hand","generic-hand-select","generic-trigger"]` |
|
||||
@@ -386,7 +386,13 @@ cd /home/cat/Desktop/myProject/jelllyfin_vr_web_plugin
|
||||
# 语法检查(全部模块)
|
||||
python3 build.py --check
|
||||
|
||||
# 构建 + 部署(自动构建 → 上传 → 注入 script 标签)
|
||||
# ---- 插件路线(推荐)----
|
||||
python3 pack.py # 构建 + 打包 + 更新 manifest.json
|
||||
python3 pack.py --bump --publish # 版本号 +1,建 Gitea Release,上传 zip,推 manifest
|
||||
python3 pack.py --print-urls # 只打印 Jellyfin 要填的仓库地址
|
||||
# 需要 GITEA_TOKEN(write:repository),从环境变量或 secrets.local.md 读
|
||||
|
||||
# ---- 老路线:直接注入容器内文件(兜底)----
|
||||
# 密码从环境变量读;真实值放在本地 secrets.local.md(不入库)
|
||||
export JF_PW='<NAS 密码>'
|
||||
python3 deploy.py
|
||||
@@ -401,7 +407,8 @@ python3 deploy.py --legacy
|
||||
./quest.sh
|
||||
```
|
||||
|
||||
**部署后必须让用户刷新页面**(脚本按页面加载缓存,URL 里的 `?r=<md5前6位>` 是破缓存用的)。
|
||||
**插件路线**:Jellyfin 里点更新/重装 → 重启 → 刷新浏览器。
|
||||
**老路线**:部署后必须让用户刷新页面(脚本按页面加载缓存,URL 里的 `?r=<md5前6位>` 是破缓存用的)。
|
||||
|
||||
### 诊断信息
|
||||
|
||||
@@ -432,10 +439,12 @@ errors[] 最近错误
|
||||
|
||||
## 10. 长期隐患
|
||||
|
||||
1. **注入写在容器内文件**(`/jellyfin/jellyfin-web/index.html`)→
|
||||
`jellyfin/jellyfin:latest` 一升级就没了,需重跑 `deploy.py`。
|
||||
正式版应改用 **JavaScript Injector**(`n00bcodr/jellyfin-plugins` 的 `12/manifest.json`,
|
||||
targetAbi 12.0.0.0)或 **File Transformation**(`IAmParadox27/jellyfin-plugin-file-transformation`,554⭐)。
|
||||
1. ~~**注入写在容器内文件**(`/jellyfin/jellyfin-web/index.html`)→ `jellyfin/jellyfin:latest`
|
||||
一升级就没了~~ —— **已解决**:现在是插件 `Jellyfin.Plugin.JFVR`,在请求时改写
|
||||
`index.html`,磁盘上的 web 目录一个字节都不动。见 `plugin/` 和 `pack.py`,
|
||||
安装地址 `https://git.cat-shark.xyz/cat-shark/jellyfin_vr_web_plugin/raw/branch/main/manifest.json`。
|
||||
`deploy.py` 保留作为兜底(两者不要同时用)。
|
||||
剩下的维护成本:Jellyfin 升大版本时可能要重新编译(插件对 `Jellyfin.Controller 12.1.0` / net10.0)。
|
||||
2. **Three.js 从 CDN 加载**(1.27MB)→ 依赖外网。可改为本地托管。
|
||||
3. **Quest 浏览器版本会随系统更新变化** → 能力探测(诊断信息里的能力段)是唯一的自检手段。
|
||||
|
||||
@@ -780,3 +789,247 @@ handPref=left(持久化后重开会话) pointer hand:left HIT 0.22,0
|
||||
右手按住画面拖动条,左手松开 viewDrag 仍 "armed"(拖动存活)
|
||||
右手按住画面拖动条,右手松开 viewDrag "reset (tap)"(拖动结束)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 18. ADDENDUM 12 — 「这台机器放不出来」:8K Main 10 与客户端自报能力(插件 0.2.0.0 / bundle `edca47`)
|
||||
|
||||
### 18.1 现象
|
||||
|
||||
同一个文件 `masex.tv@kiwvr00896_1_8k.mp4`:
|
||||
|
||||
- **Quest 3 上正常播放**
|
||||
- **PC 浏览器上黑屏 / 一直转圈,进度条不走**,而且**播放器界面(含码率菜单)根本不出现**
|
||||
|
||||
### 18.2 根因
|
||||
|
||||
**文件是 10-bit,而库里其它文件都是 8-bit。** 实测:
|
||||
|
||||
```
|
||||
FAILS : kiwvr00896_1_8k profile=Main 10 pix_fmt=yuv420p10le 8192x4096 level 183 (6.1) 30 Mbps
|
||||
kiwvr00896_2_8k profile=Main 10 pix_fmt=yuv420p10le
|
||||
WORKS : 13dsvr01786_1_8k profile=Main pix_fmt=yuv420p 8192x4096 level 183
|
||||
13dsvr01794_1_8k profile=Main pix_fmt=yuv420p
|
||||
13dsvr01797_1_8k profile=Main pix_fmt=yuv420p
|
||||
```
|
||||
|
||||
**关键:Jellyfin 的直连/转码决策 100% 看客户端上报的 DeviceProfile。** Jellyfin Web 是这么生成它的(`src/scripts/browserDeviceProfile.js`):
|
||||
|
||||
```js
|
||||
let maxHevcLevel = 120;
|
||||
let hevcProfiles = 'main';
|
||||
// hevc main10 level 6.1
|
||||
if (videoTestElement.canPlayType('video/mp4; codecs="hvc1.2.4.L183"')...) {
|
||||
maxHevcLevel = 183; // 8K
|
||||
hevcProfiles = 'main|main 10'; // 10-bit
|
||||
}
|
||||
```
|
||||
|
||||
PC 浏览器对 `hvc1.2.4.L183` 返回 **"probably"**(系统里有*某个* HEVC 解码器,`canPlayType` 判断很粗),于是声明「支持 Main 10 到 level 6.1(8K)」→ 服务器选了 **Direct Play** → 实际硬件解不动 → 黑屏。Quest 声明同样的话但它真做得到。
|
||||
|
||||
**服务器侧没有「强制转码」开关。** `UserPolicy` 只有:`EnableVideoPlaybackTranscoding`(已开)、`EnablePlaybackRemuxing`(已开)、`RemoteClientBitrateLimit`(只对远程连接)、`ForceRemoteSourceTranscoding`(只对远程片源)。没有任何按条目/按设备的强制转码。
|
||||
|
||||
**顺带发现:本项目的能力探测有盲区。** `src/00-core.js:326` 只测了
|
||||
|
||||
```js
|
||||
['video/mp4; codecs="hev1.1.6.L183.B0"', // profile_idc 1 = Main(8-bit)
|
||||
'video/mp4; codecs="hvc1.1.6.L183.B0"',
|
||||
'video/mp4; codecs="hev1"', 'video/mp4; codecs="hvc1"']
|
||||
```
|
||||
|
||||
`hev1.**1.6**.L183.B0` 里的 `1.6` 是 **Main**;**Main 10 是 `hev1.2.4.L183.B0`,从来没测过**。所以 README §2.0 那句「8K HEVC decodingInfo supported:true」**只对 8-bit 成立** —— 之前碰巧全是 8-bit 文件才没暴露。
|
||||
|
||||
### 18.3 为什么卡住之后连码率都调不了(重要)
|
||||
|
||||
Jellyfin Web 的播放器 UI **只在 `playbackstart` 之后才被创建**:
|
||||
|
||||
```
|
||||
HtmlVideoPlayer.play() htmlVideoPlayer/plugin.js:502
|
||||
createMediaElement() :1802 loading.show() 转圈
|
||||
<div.videoPlayerContainer><video>
|
||||
document.body.insertBefore(...) ← 还没有 src
|
||||
updateVideoUrl() / setCurrentSrc() :593 elem.src = url
|
||||
→ loadedmetadata → canplay → play
|
||||
→ 'playbackstart'
|
||||
→ OSD 控制器(apps/legacy/controllers/playback/video/index.js:591)
|
||||
```
|
||||
|
||||
媒体起不来 → 事件不触发 → **OSD(含码率菜单)从未被创建**。所以「卡住后调不了码率」不是菜单藏起来了,是**它还不存在**。
|
||||
|
||||
推论:看门狗不能用 `90-main.js` 的 `findVideo()`(它要求 `videoWidth`,卡死时返回 null),必须直接看 `.videoPlayerContainer video`。
|
||||
|
||||
### 18.4 对策:`src/40-watchdog.js`
|
||||
|
||||
检测「OSD 之前就卡住」这个状态并给出唯一的解 —— 让服务器转码。见 README §1.2。
|
||||
|
||||
阈值:媒体报错立即弹;直连 15 秒;转码(src 已是 hls)45 秒;有画面立即退出;VR 会话中不跑。
|
||||
|
||||
选码率后写入 Jellyfin 自己那套键再重载:
|
||||
|
||||
```
|
||||
maxbitrate-Video-<true|false> = 20000000
|
||||
enableautobitratebitrate-Video-<true|false> = false ← 上游拼写,bitrate 出现两次
|
||||
```
|
||||
|
||||
> 不能用 `playbackManager.setMaxStreamingBitrate()`:那个模块**没有暴露到 window**
|
||||
> (实测 `window.playbackManager === undefined`,`window.Emby` 只剩 `Page`)。
|
||||
|
||||
### 18.5 转码在这台 NAS 上的实测能力
|
||||
|
||||
2026-10-05 那次播放走的就是转码:
|
||||
|
||||
```
|
||||
-codec:v h264_vaapi -b:v 2744000 -vf "scale_vaapi=w=1280:h=640"
|
||||
→ 1280x640 @ 2.7 Mbps,ffmpeg 每次 exit 0,无失败
|
||||
```
|
||||
|
||||
8K 10-bit 硬解 + h264 硬编在 UHD 上跑得动,但**输出只有 1280×640**(当时目标码率 2.7 Mbps;UHD 编码上限约 4K)。
|
||||
|
||||
### 18.6 附带查清的两件事
|
||||
|
||||
1. **续播点 33:22.759 的来历**:就是 2026-10-05 那次转码会话留下的(`stopped ... at 2002759ms`)。所以 10-17 两次播放都从 33:22 续播,画面出不来进度自然不动,于是「停在 33:22」重复了两次。
|
||||
2. **文件本身没问题**:完全分配无空洞(`du` 实际用量 == 表观大小)、`moov` 在文件头(faststart 正常)、33:22 处 ffmpeg 解码无报错、Jellyfin 数据库里时长正确(50:58)。同目录还有 3 个垃圾广告文件(`.url` × 3 + 一个 20 MB 的 `三 上 悠 亚…mp4`),是盗版站打包进来的。
|
||||
|
||||
### 18.7 已做(插件 0.5.0.0 / bundle `72681d`)
|
||||
|
||||
- [x] `src/00-core.js` 的能力探测补上 Main 10 和 8K/4K 分档;新增 `NS.caps.codec`(`decodingInfo` 逐档结果)与 `NS.capsDiag()` 里的 `HEVC real` 段
|
||||
- [x] **彻底自动化**:用 `MediaCapabilities.decodingInfo` 真机探测后覆盖 `HTMLVideoElement.prototype.canPlayType`,让 Jellyfin Web 自己那套 `maxHevcLevel` 逻辑算出准确 profile
|
||||
- [x] **补上「实时吞吐」这一维**(`smooth`)—— 见下
|
||||
|
||||
覆盖逻辑的关键点:
|
||||
|
||||
- 每档在**它对应的分辨率和码率**上探测(level 字符串不带尺寸):`L120→1920x1080@30/8Mbps`、`L123→1080p60/8Mbps`、`L153→3840x2160@60/20Mbps`、`L183/L186→8192x4096@60/30Mbps`
|
||||
- codec string → `'<profile_idc>:<level>'`(`hvc1.2.4.L183` → `2:183`);`hvc1`/`hev1` 同键,因为差的只是 sample entry
|
||||
- **拦掉一档的两个条件**:`supported === false`(完全解不了)或 **`smooth === false`**(能解但跟不上)。只有 `powerEfficient:false`(纯软解)本身不拦
|
||||
- 钩子在 bundle 执行时**同步**装上(此时还没有 verdict,一律交给原生),探测结果落地后才生效
|
||||
- 逃生开关:`localStorage['jfvr-cpt'] = 'off'` 完全关掉;`localStorage['jfvr-cpt-keep'] = '2:183'` 只保留某一档(单台设备误报时用)
|
||||
|
||||
### 18.7.1 为什么最后把 `smooth` 加上了(重要)
|
||||
|
||||
0.3.0.0 只看了 `supported`,当时故意不动 `smooth`,理由是怕误伤 Quest。但那样会漏掉**最现实的那种桌面场景**:
|
||||
|
||||
> 显卡**有** HEVC 解码器(所以 `canPlayType` 报 "probably"、`supported:true`),但**在 8K60 下跑不动**(`smooth:false`)
|
||||
|
||||
结果就是:声称支持 level 6.1 → 服务器 DirectPlay → 黑屏。而这恰好就是本机上那个文件的真实情况。所以 0.5.0.0 把它纳入判定。
|
||||
|
||||
**定位真机问题时看这个**:在出问题的浏览器控制台跑
|
||||
|
||||
```js
|
||||
window.__JFVR.capsDiag()
|
||||
```
|
||||
|
||||
会列出每一档的 `supported / smooth / powerEfficient`、探测用的负载、以及**为什么被拦**(`unsupported` 还是 `not-smooth`)。一次就能看出是哪一维的问题。
|
||||
|
||||
五种模式本地实测(`_harness-codec.html`,全过):
|
||||
|
||||
| 模式 | 模拟 | 结果 |
|
||||
|---|---|---|
|
||||
| `pc` | `supported=false` | 封顶 level 153 → **转码** |
|
||||
| `slow` | `supported=true, smooth=false`(**真实桌面场景**) | 封顶 level 153 → **转码** |
|
||||
| `quest` | 全部 `smooth` | 保持 ≥183 → **Direct Play** |
|
||||
| `pin` | `slow` + `jfvr-cpt-keep=2:183` | 恢复 183 → **Direct Play** |
|
||||
| `off` | 开关关闭 | `canPlayType` 完全未动 |
|
||||
|
||||
### 18.8 待办
|
||||
|
||||
- [ ] 探测目前只覆盖 HEVC。AV1 / VP9 走同一套机制,但库里没有这类片源,先不做。
|
||||
- [ ] 网络带宽那一维不在设备档案范围内(档案是在播放开始时判一次的)。Jellyfin 自带的 Auto 码率探测管这个。
|
||||
- [ ] 看门狗(§1.2)现在退居为纯兜底:如果检测结果准了,它就不应该再触发。它仍然保留,作为「声称能播结果没播出来」的最后一道。
|
||||
|
||||
---
|
||||
|
||||
## 19. ADDENDUM 13 — 真机根因:`nvidia-vaapi-driver` 0.0.14 的自引用初始化器
|
||||
|
||||
### 19.1 结论(先看这个)
|
||||
|
||||
本机(Kubuntu / NVIDIA TU116M GTX 1660 Ti Mobile / Chrome 152)播不了 8K Main 10,
|
||||
**根因不在 Jellyfin、不在插件、也不在显卡硬件**,而在 `nvidia-vaapi-driver` 的版本:
|
||||
|
||||
```
|
||||
已安装: nvidia-vaapi-driver 0.0.14-1 (2025-06-08,Ubuntu 26.04 仓库里只有这个)
|
||||
已修复: 0.0.18 (2026-08-31)
|
||||
```
|
||||
|
||||
**升级到 0.0.18 即可,不需要转码、不需要插件做任何补偿。**
|
||||
|
||||
### 19.2 证据链
|
||||
|
||||
**1) 硬件能做,中间层不行** —— 同一文件、同一台机器:
|
||||
|
||||
| 路径 | 结果 |
|
||||
|---|---|
|
||||
| `ffmpeg -hwaccel vaapi`(走 nvidia-vaapi-driver) | ❌ `Failed to create decode context: 2 (resource allocation failed)` |
|
||||
| `ffmpeg -hwaccel cuda`(直连 NVDEC) | ✅ 正常 |
|
||||
| VAAPI 解 8K **Main(8-bit)** | ✅ 正常 |
|
||||
| VAAPI 解 1080p **Main 10** | ✅ 正常 |
|
||||
|
||||
→ 只有 **8K + 10-bit** 这个组合失败;硬件本身没问题。
|
||||
|
||||
**2) Chrome 侧的配置是对的** —— `chrome://gpu` 实测:
|
||||
|
||||
```
|
||||
Graphics Feature Status
|
||||
* Video Decode: Hardware accelerated ← VAAPI 生效中
|
||||
Video Acceleration Information
|
||||
Decode hevc main : 144x144 to 8192x8192 pixels
|
||||
Decode hevc main 10 : 144x144 to 8192x8192 pixels ← 驱动"声称"支持
|
||||
```
|
||||
|
||||
**驱动声称支持 8K Main 10,实际建不出解码器。** 于是:
|
||||
|
||||
```
|
||||
canPlayType('hvc1.2.4.L183') → truthy
|
||||
→ Jellyfin Web 声明 maxHevcLevel=183 / main|main 10
|
||||
→ 服务器选 DirectPlay(日志里没有任何 ffmpeg)
|
||||
→ Chrome 真正解码时 cuvidCreateDecoder 失败
|
||||
→ 黑屏 / 一直转圈(而 OSD 只在 playbackstart 之后创建,所以连码率菜单都没有)
|
||||
```
|
||||
|
||||
**3) 上游 bug** —— 0.0.14 的 `src/vabackend.c`:
|
||||
|
||||
```c
|
||||
CUVIDDECODECREATEINFO vdci = {
|
||||
.ulWidth = vdci.ulMaxWidth = vdci.ulTargetWidth = picture_width, // ← 在自身初始化器里引用自己
|
||||
.ulHeight = vdci.ulMaxHeight = vdci.ulTargetHeight = picture_height, // C 里这是未定义行为
|
||||
```
|
||||
|
||||
修复提交 `a565237`(2026-07-09)标题即 **"fix: drop self-referential CUVIDDECODECREATEINFO initializer"**,
|
||||
把赋值移出初始化器;`#449`(2026-07-17 合入)把同样的修复镜像到另一个函数。**0.0.17 仍有问题,0.0.18 已修复。**
|
||||
|
||||
### 19.3 Kubuntu 上正确让 Chrome 用显卡
|
||||
|
||||
用户现有的启动参数**是对的**(`chrome://gpu` 已确认 `Video Decode: Hardware accelerated`):
|
||||
|
||||
```
|
||||
--enable-features=VaapiOnNvidiaGPUs,VaapiVideoDecodeLinuxGL,VaapiVideoDecoder
|
||||
--disable-features=UseChromeOSDirectVideoDecoder
|
||||
```
|
||||
|
||||
配套条件:
|
||||
|
||||
- `nvidia-vaapi-driver` **≥ 0.0.18**(当前 0.0.14 → 这是要补的一环)
|
||||
- 驱动 README 要求内核参数 `nvidia-drm.modeset=1`(**本机当前未设置**,`/proc/cmdline` 里没有)
|
||||
- `LIBVA_DRIVER_NAME=nvidia`
|
||||
- 注意:驱动 README 明说 *"specifically designed to be used by Firefox … may not operate correctly in other applications"*、*"Chrome is currently unsupported"*。Chrome 能用但不受官方支持。
|
||||
|
||||
升级(Ubuntu 仓库只有 0.0.14,需从源码构建):
|
||||
|
||||
```bash
|
||||
sudo apt install meson ninja-build libva-dev libegl-dev gstreamer1.0-plugins-bad libffmpeg-nvenc-dev
|
||||
git clone --branch v0.0.18 https://github.com/elFarto/nvidia-vaapi-driver
|
||||
cd nvidia-vaapi-driver && meson setup build && sudo meson install -C build
|
||||
```
|
||||
|
||||
### 19.4 这对插件意味着什么
|
||||
|
||||
升级驱动后:Chrome 能真正解 8K Main 10 → `decodingInfo` 报 `smooth:true` → 插件不拦任何档
|
||||
→ 直连 8K → 看门狗也不会触发。**插件侧不需要做任何补偿。**
|
||||
|
||||
但 **0.5.0.0 的判定逻辑本身有个缺陷,已在 0.5.1.0 修掉**:它在 `smooth === false` 时就压低
|
||||
level 上限,而 Jellyfin 的档案**无法表达「Main 支持到 8K,Main 10 只到 4K」**(level 上限和
|
||||
profile 列表来自同一条 Main 10 判断链)。所以在那台「8-bit 8K 正常、10-bit 8K 失败」的机器上,
|
||||
拦掉 `2:183` 会把**整库 61 个 8-bit 8K 文件**一起拖进转码 —— 而 NAS 转 8K 只能出 1280×640。
|
||||
|
||||
0.5.1.0 改成:**只有当同位深的 Main(8-bit)档也不可用时,才压低 level 上限**(为此补测了
|
||||
Main L5.1 / L6.1 / L6.2 作为对照)。代价是「8-bit 能解、10-bit 不能」的设备仍会声称 10-bit 并在
|
||||
那种文件上黑屏 —— 那正是 `40-watchdog.js` 存在的意义。
|
||||
|
||||
@@ -16,9 +16,149 @@ VR180 / VR360 沉浸播放,不用再手动切全屏 + 选投影。
|
||||
| 备份 | `/vol2/jellyfin/config/_vr_backup/index.html.orig` |
|
||||
| 两个入口都生效 | `http://192.168.123.199:8096/web/` 与 `https://jellyfin.cat-shark.xyz/web/` |
|
||||
|
||||
> ⚠️ 注入写在**容器内文件**里,`jellyfin/jellyfin:latest` 一升级就没了,重跑 `deploy.py` 即可。
|
||||
> 正式版应改用 **JavaScript Injector 插件**(`n00bcodr/jellyfin-plugins` 的 `12/manifest.json`,targetAbi 12.0.0.0)
|
||||
> 或 **File Transformation**,这样升级不丢、可开关。
|
||||
> ⚠️ 老路子(`deploy.py`):注入写在**容器内文件**里,`jellyfin/jellyfin:latest` 一升级就没了,
|
||||
> 得重跑 `deploy.py`。**现在已经有插件版了,见下面 §1.1,优先用插件。**
|
||||
|
||||
### 1.1 插件化(推荐,已实现)
|
||||
|
||||
JFVR 同时也是一个标准的 **Jellyfin 插件**,通过自建 Gitea 仓库分发,走 Dashboard 的
|
||||
「添加存储库 → Catalog 安装」,**不再改容器内任何文件**。
|
||||
|
||||
| 项 | 值 |
|
||||
|---|---|
|
||||
| 插件仓库地址 | `https://git.cat-shark.xyz/cat-shark/jellyfin_vr_web_plugin/raw/branch/main/manifest.json` |
|
||||
| 插件名 / GUID | `Jellyfin Web VR` / `7d33ef59-f9b0-412b-93bd-5cd5353018de` |
|
||||
| 目标 ABI | `12.0.0.0`(Jellyfin 只保留 `targetAbi <= 服务器版本` 的条目,所以一份包覆盖整个 12.x) |
|
||||
| 插件代码 | `plugin/Jellyfin.Plugin.JFVR/`(C#,net10.0,对 `Jellyfin.Controller 12.1.0`) |
|
||||
| 打包 / 发布 | `python3 pack.py` / `python3 pack.py --bump --publish` |
|
||||
| 脚本端点 | `/JFVR/jfvr.js`(bundle 内嵌在 DLL 里,URL 带内容哈希做缓存键) |
|
||||
| 状态查询 | `/JFVR/status` |
|
||||
| 设置页 | Dashboard → 插件 → Jellyfin Web VR(开关注入 / 覆盖脚本 URL) |
|
||||
| 页面按钮 | 「VR 播放」按钮**可拖动**(位置记住;双击回到默认右下角) |
|
||||
|
||||
**安装**:Dashboard → 插件 → 目录 → ⚙️ → ➕ → 粘贴上面的仓库地址 → 保存 →
|
||||
在 Catalog 里找到 **Jellyfin Web VR** → 安装 → **重启 Jellyfin**。
|
||||
|
||||
**工作方式**:`ScriptInjectionStartupFilter` 在**请求时**把
|
||||
`<script plugin="JFVR" src="../JFVR/jfvr.js?v=<hash>" defer></script>`
|
||||
插到 `index.html` 的 `</body>` 之前。磁盘上的 `/jellyfin/jellyfin-web/` 一个字节都不动,
|
||||
所以升级 Jellyfin 不会丢。如果检测到老 `deploy.py` 留下的 `<script plugin="JellyfinVR" ...>`,
|
||||
会**自动清掉**,不会重复加载 bundle。
|
||||
|
||||
**改完 web 代码后的流程**:
|
||||
|
||||
```bash
|
||||
python3 pack.py --bump --publish # 构建 → 打包 → 建 Release → 推 manifest
|
||||
# 然后在 Jellyfin 里:插件页点更新(或重装),重启,刷新浏览器
|
||||
```
|
||||
|
||||
> 老路子仍然可用,作为插件的兜底:`JF_PW=... python3 deploy.py`。
|
||||
> 但**两者不要同时开** —— 装了插件就别再跑 `deploy.py`(跑完插件也会把那个标签清掉)。
|
||||
|
||||
### 1.2 播放看门狗(卡住检测)
|
||||
|
||||
`src/40-watchdog.js`。针对一种很坑的失败:**某些设备/浏览器放不出来时,Jellyfin 的播放器界面根本不会被创建**。
|
||||
|
||||
原因(已从 Jellyfin Web 源码确认):播放器 UI 只在 `playbackstart` 事件之后构建,而 `playbackstart` 要等媒体真的起来了才会触发。加载顺序是:
|
||||
|
||||
```
|
||||
HtmlVideoPlayer.play() htmlVideoPlayer/plugin.js:502
|
||||
createMediaElement() :1802 转圈 + <div.videoPlayerContainer> + <video>
|
||||
(此时还没 src)
|
||||
setCurrentSrc() :593 elem.src = url
|
||||
→ loadedmetadata → canplay → play → 'playbackstart'
|
||||
→ OSD 控制器(apps/legacy/controllers/playback/video/index.js:591)
|
||||
```
|
||||
|
||||
媒体起不来 → `playbackstart` 不触发 → **码率菜单根本不存在**,页面上只剩一个转圈和一个 `videoWidth === 0` 的 `<video>`。所以「卡住之后调不了码率」不是菜单藏起来了,是菜单还没被创建。
|
||||
|
||||
看门狗就盯这个状态(`.videoPlayerContainer` 里的 `<video>` 仍然是 `videoWidth === 0`):
|
||||
|
||||
| | 阈值 |
|
||||
|---|---|
|
||||
| 媒体报错(`video.error`) | 立即弹 |
|
||||
| 直连没画面 | 15 秒 |
|
||||
| 转码(src 已经是 hls) | 45 秒(服务器要先出片) |
|
||||
| 已经有画面 | 立即退出,不打扰 |
|
||||
| VR 会话中 | 完全不跑 |
|
||||
|
||||
弹窗会写明**具体格式**(走 Jellyfin API 查 MediaStreams),并给 20/8/3 Mbps 三档 + 「继续等待」。选一档后写入 Jellyfin 自己那套 localStorage 键然后重载:
|
||||
|
||||
```
|
||||
maxbitrate-Video-<true|false> = 20000000
|
||||
enableautobitratebitrate-Video-<true|false> = false
|
||||
```
|
||||
|
||||
> 注意 `enableautobitratebitrate` 里 `bitrate` 确实出现了两次 —— 这是 Jellyfin 上游的拼写,照抄的。
|
||||
> 之所以不用 `playbackManager.setMaxStreamingBitrate()`:那个模块没暴露到 window(已实测 `window.playbackManager === undefined`,`window.Emby` 只剩 `Page`),所以只能写设置 + 重载。
|
||||
|
||||
选过之后 120 秒内不再打扰(记在 sessionStorage)。
|
||||
|
||||
### 1.3 让客户端如实上报解码能力(`canPlayType` 覆盖)
|
||||
|
||||
`src/00-core.js`。解决的问题:**Jellyfin 的直连/转码决策 100% 看客户端上报的设备档案**,而 Jellyfin Web 是从 `canPlayType` 推出整个 HEVC 声明的(`browserDeviceProfile.js`):
|
||||
|
||||
```js
|
||||
if (canPlayType('video/mp4; codecs="hvc1.2.4.L183"')) {
|
||||
maxHevcLevel = 183; // level 6.1 = 8K
|
||||
hevcProfiles = 'main|main 10';
|
||||
}
|
||||
```
|
||||
|
||||
`canPlayType` 回答的是「这台机器*有没有* HEVC 解码器」,不是「能不能解 8192×4096 的 Main 10 @60fps」。所以一个只是「系统里有 HEVC 解码器」的桌面浏览器会声称支持 8K Main 10 → 服务器选 DirectPlay → 黑屏(而 OSD 压根不会创建,见 §1.2)。
|
||||
|
||||
插件的做法:
|
||||
|
||||
1. 页面加载时用 **`MediaCapabilities.decodingInfo`** 对 HEVC 各档做真机探测(每档在它对应的分辨率和码率上测:L120→1080p30/8Mbps、L153→4K60/20Mbps、L183/L186→8K60/30Mbps,因为 level 字符串本身不带尺寸);
|
||||
2. 把结果回灌给 `HTMLVideoElement.prototype.canPlayType`。
|
||||
|
||||
**拦掉一档的条件有两个,都是「确定的不能」**:
|
||||
|
||||
| 条件 | 含义 |
|
||||
|---|---|
|
||||
| `supported === false` | 完全解不了这个格式 |
|
||||
| `smooth === false` | 能解,但**在那种分辨率和帧率下跟不上** —— 就是「有 HEVC 解码器,但 8K 跑不动」这种桌面场景 |
|
||||
|
||||
只有 `powerEfficient:false`(纯软解)**本身不拦**。
|
||||
|
||||
这样 Jellyfin Web 自己那套 `maxHevcLevel` 逻辑就会得出**准确**档案:
|
||||
|
||||
| 设备 | 结果 |
|
||||
|---|---|
|
||||
| 桌面(声称支持但实际解不动 8K Main 10) | 降到 level 153 → 8K 文件超出声明 → **服务器自动转码** |
|
||||
| Quest 3(真能解 8K Main 10) | 保持 ≥183 → **继续 Direct Play** |
|
||||
|
||||
注意:这里**不问用户任何东西**。客户端如实上报,Jellyfin 在 `PlaybackInfo` 那一步自己选 Transcode —— 这正是「提示用户切码率」想要达到的目的,只是做在了正确的层。
|
||||
|
||||
逃生开关:
|
||||
|
||||
```js
|
||||
localStorage['jfvr-cpt'] = 'off'; // 完全关掉钩子
|
||||
localStorage['jfvr-cpt-keep'] = '2:183'; // 只保留某一档(单台设备误报时用,不用整个关掉)
|
||||
```
|
||||
|
||||
当前状态可以在诊断面板里看到,每档会写明**为什么**被拦(`unsupported` / `not-smooth`)或是否被 pin:
|
||||
|
||||
```
|
||||
canPlayType hook : on (blocked: Main10 L6.1 (8K) (not-smooth), ...)
|
||||
HEVC real (decodingInfo):
|
||||
Main10 L6.1 (8K) supported=true smooth=false powerEfficient=true [8192x4096@60 30Mbps] <-- BLOCKED (not-smooth)
|
||||
```
|
||||
|
||||
这也是 Meta 官方文档推荐的做法 —— 他们明确说「用 Media Capabilities API 探测,不要靠 UA 嗅探」。
|
||||
|
||||
### 1.4 「VR 播放」按钮可以拖了
|
||||
|
||||
原来钉死在右下角(`right:16px; bottom:120px`),窗口矮的时候正好压在播放看门狗弹窗的码率按钮上,没地方躲。
|
||||
|
||||
现在:
|
||||
|
||||
- 用 `left/top` 定位(不再用 `right/bottom`),用 pointer 事件拖动,位置记在 `localStorage['jfvr-btn-pos']`
|
||||
- `touch-action:none` —— 不加这个,触屏上拖动会变成滚动页面
|
||||
- **拖动结束仍然会触发一次 click**,代码会把那一次吃掉,所以拖完不会意外弹出面板
|
||||
- 限制在视窗内,窗口缩放时会重新夹紧
|
||||
- **双击回到默认右下角**(拖到尴尬位置时的逃生口)
|
||||
- 设置面板跟着按钮走(不再是钉在视窗角落),优先放在按钮上方,上方放不下就放下方,再放不下就夹在屏幕内
|
||||
|
||||
---
|
||||
|
||||
@@ -41,10 +181,14 @@ UA Mozilla/5.0 (X11; Linux x86_64; Quest 3) ... Chrome/152 ... VR Safari/537.36
|
||||
| `XRWebGLBinding` | — | true ← 回退路径可用 |
|
||||
| `MAX_TEXTURE_SIZE` | 8192 | 8192 |
|
||||
| `MAX_RENDERBUFFER_SIZE` | 16384 | 16384 |
|
||||
| 8K HEVC `decodingInfo` | supported / powerEfficient | **supported:true, powerEfficient:true** |
|
||||
| 8K HEVC `decodingInfo` | supported / powerEfficient | **supported:true, powerEfficient:true**(仅 8-bit Main) |
|
||||
|
||||
结论:**HTTPS 下 Quest 浏览器提供全部所需 WebXR 能力,8K HEVC 走硬件解码。**
|
||||
|
||||
> ⚠️ 上面那行 `decodingInfo` 当年只测了 **8-bit Main** 的 codec string(`hev1.1.6.L183.B0`),
|
||||
> **10-bit Main 10(`hev1.2.4.L183.B0`)从来没测过**。插件现在会分档探测(见 §1.3),
|
||||
> 诊断面板里会列出 Main/Main10 × 4K/8K 的真实结果。
|
||||
|
||||
### 2.1 网络实测:HTTPS 域名在 Quest 上就是内网直连
|
||||
|
||||
```
|
||||
@@ -255,6 +399,16 @@ WebXR 规范原文:
|
||||
|
||||
## 8. 修改 / 部署 / 回滚
|
||||
|
||||
### 8.1 插件(推荐)
|
||||
|
||||
```bash
|
||||
python3 pack.py --bump --publish # 构建 + 打包 + 建 Release + 推 manifest.json
|
||||
```
|
||||
|
||||
然后在 Jellyfin 里更新插件并重启,刷新浏览器。
|
||||
|
||||
### 8.2 老路子:直接注入容器内文件(兜底)
|
||||
|
||||
```bash
|
||||
# 改 jfvr-mvp.js 后重新部署
|
||||
JF_PW='<ssh密码>' python3 deploy.py
|
||||
@@ -264,6 +418,7 @@ JF_PW='<ssh密码>' python3 deploy.py rollback
|
||||
```
|
||||
|
||||
`deploy.py` 用文件 md5 前 6 位作为版本号写进 script 标签的 query,自动破缓存。
|
||||
插件走的是同一套破缓存思路,只是 hash 由 DLL 内嵌 bundle 的 md5 算出来。
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ SRC = os.path.join(HERE, "src")
|
||||
DIST = os.path.join(HERE, "dist")
|
||||
OUT = os.path.join(DIST, "jfvr.js")
|
||||
|
||||
ORDER = ["00-core.js", "10-subtitles.js", "20-controls.js", "30-hands.js", "90-main.js"]
|
||||
ORDER = ["00-core.js", "10-subtitles.js", "20-controls.js", "30-hands.js", "40-watchdog.js", "90-main.js"]
|
||||
|
||||
|
||||
def sources():
|
||||
|
||||
Vendored
+814
-10
@@ -1,7 +1,7 @@
|
||||
/*!
|
||||
* JFVR (Jellyfin Web VR) — built bundle. DO NOT EDIT.
|
||||
* Edit src/*.js and run: python3 build.py
|
||||
* Modules: 00-core.js, 10-subtitles.js, 20-controls.js, 30-hands.js, 90-main.js
|
||||
* Modules: 00-core.js, 10-subtitles.js, 20-controls.js, 30-hands.js, 40-watchdog.js, 90-main.js
|
||||
*/
|
||||
|
||||
/* ===== 00-core.js ===== */
|
||||
@@ -315,9 +315,132 @@
|
||||
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: [],
|
||||
codecKept: [],
|
||||
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.
|
||||
*
|
||||
* A tier is blocked when:
|
||||
*
|
||||
* 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 "decoder exists, 8K does not work"
|
||||
* case
|
||||
*
|
||||
* AND, for a Main 10 tier, only when the SAME LEVEL IS ALSO UNUSABLE IN MAIN
|
||||
* (8-bit). That second condition is not cosmetic; it is forced by a real
|
||||
* limitation in Jellyfin's device profile.
|
||||
*
|
||||
* browserDeviceProfile.js derives the level ceiling and the profile list from
|
||||
* ONE chain of Main 10 probes -- the only Main (8-bit) probes are L4.0 and
|
||||
* L4.1. So the reachable states are only:
|
||||
*
|
||||
* (120, main) (123, main) (123, main|main 10) (153, main|main 10)
|
||||
* (183, main|main 10) (186, main|main 10)
|
||||
*
|
||||
* "Main up to 8K, Main 10 only up to 4K" is NOT expressible. So blocking
|
||||
* 2:183 on a device that decodes 8-bit 8K but not 10-bit 8K does not just
|
||||
* spare the 10-bit file -- it drags every 8-bit 8K file down to level 153 and
|
||||
* transcodes them too. On a server that cannot transcode 8K in real time
|
||||
* (measured: 8192x4096 -> 1280x640), that trades one broken file for sixty.
|
||||
*
|
||||
* Verified on the machine this was written for: NVIDIA VAAPI decodes
|
||||
* 8192x4096 Main 8-bit fine and fails to create a decoder for 8192x4096
|
||||
* Main 10 (nvidia-vaapi-driver passes the surface bit depth straight to
|
||||
* cuvidCreateDecoder). Its whole library is 8-bit 8K, so lowering the ceiling
|
||||
* would have broken all of it.
|
||||
*
|
||||
* The cost of this rule: a device that decodes 8-bit at a level but not
|
||||
* 10-bit will still claim 10-bit at that level and can black-screen on such a
|
||||
* file. 40-watchdog.js covers exactly that, which is why it stays.
|
||||
*
|
||||
* 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 and bitrate it exists for. Main
|
||||
// (8-bit) is probed at the same levels as Main 10 because it is the control
|
||||
// that decides whether a level is really out of reach.
|
||||
//
|
||||
// NOTE on the codec strings: they MUST carry a constraint byte. Chrome's
|
||||
// MediaCapabilities.decodingInfo does not recognise the bare 'hvc1.1.L120'
|
||||
// spelling and answers supported:false for it even on a machine that decodes
|
||||
// HEVC perfectly, while 'hvc1.1.0.L120' is answered correctly. That one
|
||||
// character used to matter far more than it looks: browserDeviceProfile.js
|
||||
// gates the WHOLE hevc entry on supportsHevc(), which is exactly
|
||||
//
|
||||
// canPlayType('...codecs="hvc1.1.L120"') ||
|
||||
// canPlayType('...codecs="hev1.1.L120"') ||
|
||||
// canPlayType('...codecs="hvc1.1.0.L120"') || ...
|
||||
//
|
||||
// so a spurious block of tier 1:120 (all spellings map to it) removes hevc
|
||||
// from every DirectPlayProfile and transcode every HEVC file in the library.
|
||||
// 1:120 is NOT part of Jellyfin's level chain; it is only that gate.
|
||||
var CODEC_PROBES = [
|
||||
{ key: '1:120', name: 'Main L4.0', w: 1920, h: 1080, fps: 30, mbps: 8, codec: 'hvc1.1.0.L120' },
|
||||
{ key: '1:123', name: 'Main L4.1', w: 1920, h: 1080, fps: 60, mbps: 8, codec: 'hvc1.1.4.L123' },
|
||||
{ key: '1:153', name: 'Main L5.1 (4K)', w: 3840, h: 2160, fps: 60, mbps: 20, codec: 'hvc1.1.5.L153.B0' },
|
||||
{ key: '1:183', name: 'Main L6.1 (8K)', w: 8192, h: 4096, fps: 60, mbps: 30, codec: 'hvc1.1.6.L183.B0' },
|
||||
{ key: '1:186', name: 'Main L6.2 (8K60)', w: 8192, h: 4096, fps: 60, mbps: 30, codec: 'hvc1.1.6.L186.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' }
|
||||
];
|
||||
|
||||
/**
|
||||
* '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 +454,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 +474,162 @@
|
||||
}
|
||||
})();
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* MediaCapabilities probe + canPlayType override
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
/** Tiers the user has pinned as "keep claiming this", '<key>,<key>' 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;
|
||||
}
|
||||
|
||||
/** True when this tier is not usable on its own terms. */
|
||||
function tierUnusable(key) {
|
||||
var v = NS.caps.codec[key];
|
||||
return !!v && !v.error && (v.supported === false || v.smooth === false);
|
||||
}
|
||||
|
||||
/**
|
||||
* The decision the canPlayType hook uses.
|
||||
*
|
||||
* A Main 10 tier is only blocked when the SAME LEVEL is also unusable in Main
|
||||
* (8-bit). See the long comment above CODEC_PROBES: Jellyfin's profile cannot
|
||||
* express "Main up to 8K, Main 10 up to 4K", so blocking 2:183 on a device
|
||||
* that handles 8-bit 8K would drag all of its 8-bit 8K files into transcoding.
|
||||
*/
|
||||
function shouldBlock(key, keep) {
|
||||
var v = NS.caps.codec[key];
|
||||
if (!v || v.error) return null;
|
||||
if (keep.indexOf(key) >= 0) return null;
|
||||
var own = blockReason(v, keep);
|
||||
if (!own) return null;
|
||||
var parts = key.split(':');
|
||||
|
||||
// Base tier. 1:120 is not in Jellyfin's level chain -- it is only the
|
||||
// coarse "does this client have HEVC at all" gate (supportsHevc() in
|
||||
// browserDeviceProfile.js, which probes exactly this level). Blocking it
|
||||
// drops hevc from every DirectPlayProfile, i.e. transcodes the entire
|
||||
// HEVC library. Never act on it unless the device is unusable at EVERY
|
||||
// Main tier, so one flaky/early probe cannot do that.
|
||||
if (key === '1:120') {
|
||||
var anyMainUsable = CODEC_PROBES.some(function (p) {
|
||||
if (p.key === '1:120' || p.key.charAt(0) !== '1') return false;
|
||||
var m = NS.caps.codec[p.key];
|
||||
return !!m && !m.error && m.supported !== false && m.smooth !== false;
|
||||
});
|
||||
if (anyMainUsable) return null;
|
||||
}
|
||||
|
||||
if (parts[0] === '2') {
|
||||
// Control: the same level in Main (8-bit).
|
||||
if (!tierUnusable('1:' + parts[1])) return null;
|
||||
}
|
||||
return own;
|
||||
}
|
||||
|
||||
function applyVerdicts() {
|
||||
var keep = NS.caps.__keep || [];
|
||||
NS.caps.codecKept = [];
|
||||
NS.caps.codecBlocked = [];
|
||||
CODEC_PROBES.forEach(function (p) {
|
||||
var v = NS.caps.codec[p.key];
|
||||
if (!v) return;
|
||||
var why = shouldBlock(p.key, 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() {
|
||||
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);
|
||||
// Only a definite "no" is acted on. Anything else is the browser's call.
|
||||
if (key && shouldBlock(key, NS.caps.__keep || [])) 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; }
|
||||
|
||||
// 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.
|
||||
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: p.mbps * 1000000, framerate: p.fps
|
||||
}
|
||||
};
|
||||
mc.decodingInfo(cfg).then(function (info) {
|
||||
NS.caps.codec[p.key] = {
|
||||
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] = { 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.caps.codecKept.length ? '; pinned = ' + NS.caps.codecKept.join(', ') : ''));
|
||||
}
|
||||
});
|
||||
});
|
||||
})();
|
||||
|
||||
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 = shouldBlock(p.key, keep);
|
||||
var own = blockReason(v, keep);
|
||||
var note = why ? ' <-- BLOCKED (' + why + ')' :
|
||||
(own ? ' (unusable, but kept: 8-bit at this level works)' :
|
||||
(keep.indexOf(p.key) >= 0 ? ' <-- pinned' : ''));
|
||||
return ' ' + p.name.padEnd(20) + ' supported=' + v.supported +
|
||||
' smooth=' + v.smooth + ' powerEfficient=' + v.powerEfficient +
|
||||
' [' + v.workload + ']' + note;
|
||||
}).join('\n');
|
||||
|
||||
return [
|
||||
'origin : ' + location.origin,
|
||||
'secureContext : ' + c.secureContext,
|
||||
@@ -357,7 +639,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' ? ' (blocked: ' + (c.codecBlocked.join(', ') || 'nothing yet') + ')' : ''),
|
||||
'HEVC real (decodingInfo):',
|
||||
real
|
||||
].join('\n');
|
||||
};
|
||||
})();
|
||||
@@ -4396,6 +4682,368 @@
|
||||
|
||||
|
||||
|
||||
/* ===== 40-watchdog.js ===== */
|
||||
|
||||
/*!
|
||||
* JFVR watchdog — "this video never started" detection.
|
||||
*
|
||||
* WHY THIS EXISTS
|
||||
* ---------------
|
||||
* Jellyfin Web builds the whole player UI (including the quality menu) only
|
||||
* after the 'playbackstart' event, which the media element fires once it has
|
||||
* actually started. The load order is:
|
||||
*
|
||||
* HtmlVideoPlayer.play() htmlVideoPlayer/plugin.js:502
|
||||
* createMediaElement() :1802 -> loading.show() spinner
|
||||
* -> <div.videoPlayerContainer><video>
|
||||
* -> inserted into document.body
|
||||
* (no src yet!)
|
||||
* setCurrentSrc() :593 -> elem.src = url, only now
|
||||
* -> loadedmetadata -> canplay -> play
|
||||
* -> 'playbackstart'
|
||||
* -> OSD controller (apps/legacy/controllers/playback/video/index.js:591)
|
||||
*
|
||||
* So when the browser cannot use the stream, the OSD never appears at all: the
|
||||
* page is a spinner over an empty container with a <video> whose videoWidth is
|
||||
* still 0. There is no quality menu to click, because it was never created.
|
||||
*
|
||||
* That is the case this module covers. It watches for exactly that state and
|
||||
* offers the only thing that can help: tell Jellyfin to transcode.
|
||||
*
|
||||
* WHY localStorage AND NOT THE PLAYBACK MANAGER
|
||||
* --------------------------------------------
|
||||
* The bitrate is normally set through playbackManager.setMaxStreamingBitrate(),
|
||||
* which does a live changeStream(). That manager is an ES module export and is
|
||||
* NOT on window (verified: window.playbackManager is undefined; window.Emby only
|
||||
* has Page). So the watchdog writes the same keys appSettings would have written
|
||||
* (src/scripts/settings/appSettings.js) and reloads, which makes Jellyfin request
|
||||
* PlaybackInfo again with the new cap and pick Transcode.
|
||||
*
|
||||
* NOTE the key name really does say "bitrate" twice — that is upstream's typo,
|
||||
* reproduced here deliberately.
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var NS = window.__JFVR;
|
||||
if (!NS) { console.error('[JFVR] core missing'); return; }
|
||||
if (NS.__watchdog) return;
|
||||
NS.__watchdog = true;
|
||||
|
||||
var S = NS.state;
|
||||
var log = NS.log;
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* tunables
|
||||
* ------------------------------------------------------------------ */
|
||||
var POLL_MS = 500;
|
||||
|
||||
// Direct play on a LAN should have metadata almost immediately; the file is
|
||||
// 11 GB but its moov atom is at the front, so there is nothing to wait for.
|
||||
// A transcode is different: the server has to decode the first GOP and produce
|
||||
// segments before the browser sees any metadata, and this NAS is doing 8K.
|
||||
var DEAD_MS = 15000;
|
||||
var DEAD_TRANSCODE_MS = 45000;
|
||||
|
||||
// After the user answers (either way) stay quiet, so a slow-but-working
|
||||
// playback does not get nagged at.
|
||||
var QUIET_MS = 120000;
|
||||
|
||||
var QUIET_KEY = 'jfvr-watchdog-quiet-until';
|
||||
|
||||
// Values from Jellyfin's own menu (src/components/qualityOptions.js).
|
||||
var CHOICES = [
|
||||
{ label: '20 Mbps', bitrate: 20000000, note: '≈4K,最接近原始画质' },
|
||||
{ label: '8 Mbps', bitrate: 8000000, note: '1080p 档,稳' },
|
||||
{ label: '3 Mbps', bitrate: 3000000, note: '720p 档,最省算力' }
|
||||
];
|
||||
|
||||
var W = {
|
||||
seenAt: 0, // when the current stalled <video> first showed up
|
||||
shown: false,
|
||||
quietUntil: 0,
|
||||
lastReason: '',
|
||||
applied: null,
|
||||
codec: null, // filled in best-effort from the Jellyfin API
|
||||
panel: null
|
||||
};
|
||||
NS.Watchdog = W;
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* helpers
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
function now() { return Date.now(); }
|
||||
|
||||
/**
|
||||
* The element Jellyfin creates in createMediaElement(). It exists from the
|
||||
* very start of playback — including the failure case, which is the whole
|
||||
* point: findVideo() in 90-main.js requires videoWidth and would return null.
|
||||
*/
|
||||
function playerVideo() {
|
||||
var c = document.querySelector('.videoPlayerContainer');
|
||||
if (!c) return null;
|
||||
return c.querySelector('video') || null;
|
||||
}
|
||||
|
||||
function isTranscode(v) {
|
||||
var src = v.currentSrc || v.src || '';
|
||||
return src.indexOf('/hls') >= 0 || src.indexOf('.m3u8') >= 0;
|
||||
}
|
||||
|
||||
/** Metadata parsed AND a frame available. False means "no picture". */
|
||||
function hasPicture(v) {
|
||||
return v.videoWidth > 0 && v.readyState >= 2;
|
||||
}
|
||||
|
||||
function readQuiet() {
|
||||
try {
|
||||
var raw = window.sessionStorage && sessionStorage.getItem(QUIET_KEY);
|
||||
return raw ? parseInt(raw, 10) || 0 : 0;
|
||||
} catch (e) { return 0; }
|
||||
}
|
||||
|
||||
function setQuiet(ms) {
|
||||
W.quietUntil = now() + ms;
|
||||
try { sessionStorage.setItem(QUIET_KEY, String(W.quietUntil)); } catch (e) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the same keys Jellyfin's own quality menu writes, for BOTH in-network
|
||||
* and out-of-network, because the client is the only side that knows which one
|
||||
* applies and we cannot ask it (appSettings is not on window).
|
||||
*/
|
||||
function applyBitrate(bitrate) {
|
||||
var media = 'Video';
|
||||
['true', 'false'].forEach(function (inNet) {
|
||||
try {
|
||||
localStorage.setItem('enableautobitratebitrate-' + media + '-' + inNet, 'false');
|
||||
localStorage.setItem('maxbitrate-' + media + '-' + inNet, String(bitrate));
|
||||
} catch (e) {
|
||||
log('watchdog: could not persist bitrate', e && e.message);
|
||||
}
|
||||
});
|
||||
W.applied = bitrate;
|
||||
log('watchdog: capped bitrate at', bitrate, 'and reloading');
|
||||
setQuiet(QUIET_MS);
|
||||
window.location.reload();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* best-effort: name the actual format so the message is actionable
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
function itemIdFrom(v) {
|
||||
var s = (v && (v.currentSrc || v.src)) || '';
|
||||
var m = s.match(/\/Videos\/([0-9a-f]{32})\//i);
|
||||
if (m) return m[1];
|
||||
m = (window.location.hash || '').match(/[?&]id=([0-9a-f]{32})/i);
|
||||
return m ? m[1] : null;
|
||||
}
|
||||
|
||||
function describeCodec(v) {
|
||||
if (W.codec) return;
|
||||
var id = itemIdFrom(v);
|
||||
if (!id || !window.ApiClient) return;
|
||||
var url;
|
||||
try { url = window.ApiClient.getUrl('Items/' + id); } catch (e) { return; }
|
||||
window.ApiClient.ajax({ type: 'GET', url: url, dataType: 'json' }).then(function (item) {
|
||||
var ms = (item && item.MediaSources && item.MediaSources[0] && item.MediaSources[0].MediaStreams) || [];
|
||||
for (var i = 0; i < ms.length; i++) {
|
||||
var st = ms[i];
|
||||
if (st.Type !== 'Video') continue;
|
||||
W.codec = [
|
||||
st.Width + '×' + st.Height,
|
||||
(st.Codec || '').toUpperCase(),
|
||||
st.Profile || '',
|
||||
st.BitDepth ? st.BitDepth + '-bit' : ''
|
||||
].filter(Boolean).join(' ');
|
||||
break;
|
||||
}
|
||||
if (W.shown) render(); // update the message in place
|
||||
}).catch(function () { /* the prompt works fine without this */ });
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* the prompt
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
var WD_CSS = [
|
||||
'.jfvr-wd{position:fixed;left:50%;top:50%;transform:translate(-50%,-50%);z-index:2147483002;width:420px;max-width:calc(100vw - 32px);',
|
||||
'background:rgba(18,18,22,.97);color:#eaeaea;border:1px solid rgba(255,255,255,.16);border-radius:14px;',
|
||||
'box-shadow:0 18px 60px rgba(0,0,0,.7);font-family:inherit;font-size:14px;padding:18px 18px 14px;backdrop-filter:blur(10px)}',
|
||||
'.jfvr-wd h3{margin:0 0 6px;font-size:16px;font-weight:600}',
|
||||
'.jfvr-wd p{margin:0 0 10px;font-size:13px;line-height:1.6;color:#c3c9d4}',
|
||||
'.jfvr-wd .jfvr-wd-fmt{color:#ffd479;font-weight:600}',
|
||||
'.jfvr-wd .jfvr-wd-sub{font-size:12px;color:#8b93a1;line-height:1.55;margin-bottom:12px}',
|
||||
'.jfvr-wd-btns{display:flex;flex-direction:column;gap:8px;margin-bottom:10px}',
|
||||
'.jfvr-wd-btns button{display:flex;justify-content:space-between;align-items:baseline;gap:10px;width:100%;padding:10px 12px;',
|
||||
'border:1px solid rgba(255,255,255,.16);border-radius:9px;background:rgba(255,255,255,.05);color:#eaeaea;',
|
||||
'font-size:14px;font-weight:600;cursor:pointer;font-family:inherit;text-align:left}',
|
||||
'.jfvr-wd-btns button:hover{background:rgba(78,161,255,.18);border-color:#4ea1ff}',
|
||||
'.jfvr-wd-btns button span.note{font-size:11px;font-weight:400;color:#8b93a1}',
|
||||
'.jfvr-wd-quiet{width:100%;padding:9px;border:0;border-radius:9px;background:transparent;color:#8b93a1;',
|
||||
'font-size:13px;cursor:pointer;font-family:inherit}',
|
||||
'.jfvr-wd-quiet:hover{color:#eaeaea}',
|
||||
'.jfvr-wd-diag{margin-top:10px;font-size:11px;color:#8b93a1}',
|
||||
'.jfvr-wd-diag summary{cursor:pointer;user-select:none}',
|
||||
'.jfvr-wd-diag pre{white-space:pre-wrap;word-break:break-all;margin:8px 0 0;font-size:10px;line-height:1.5;',
|
||||
'max-height:150px;overflow:auto;color:#9fb0c4;background:rgba(0,0,0,.35);padding:8px;border-radius:7px}'
|
||||
].join('');
|
||||
|
||||
function injectWdCss() {
|
||||
if (document.getElementById('jfvr-wd-css')) return;
|
||||
var s = NS.makeEl('style');
|
||||
s.id = 'jfvr-wd-css';
|
||||
s.textContent = WD_CSS;
|
||||
document.head.appendChild(s);
|
||||
}
|
||||
|
||||
function diagText(v) {
|
||||
var e = v.error;
|
||||
return [
|
||||
'readyState : ' + v.readyState + ' (0=HAVE_NOTHING, 1=HAVE_METADATA, 2=HAVE_CURRENT_DATA)',
|
||||
'networkState: ' + v.networkState,
|
||||
'videoWidth : ' + v.videoWidth + '×' + v.videoHeight,
|
||||
'paused : ' + v.paused,
|
||||
'currentTime : ' + v.currentTime,
|
||||
'error : ' + (e ? e.code + ' ' + (e.message || '') : 'none'),
|
||||
'src : ' + String(v.currentSrc || v.src || '').slice(0, 160),
|
||||
'格式 : ' + (W.codec || '(未知)')
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
function render() {
|
||||
if (!W.panel) return;
|
||||
var body = W.panel.querySelector('.jfvr-wd-body');
|
||||
var v = playerVideo();
|
||||
if (body && v) {
|
||||
var fmt = body.querySelector('.jfvr-wd-fmt');
|
||||
if (fmt) fmt.textContent = W.codec || '这个格式';
|
||||
var pre = body.querySelector('pre');
|
||||
if (pre) pre.textContent = diagText(v);
|
||||
}
|
||||
}
|
||||
|
||||
function show(v, reason) {
|
||||
if (W.shown) { render(); return; }
|
||||
W.shown = true;
|
||||
W.lastReason = reason;
|
||||
injectWdCss();
|
||||
|
||||
var p = NS.makeEl('div', 'jfvr-wd');
|
||||
p.id = 'jfvr-wd';
|
||||
p.appendChild(NS.makeEl('h3', null, '视频没能开始播放'));
|
||||
|
||||
var body = NS.makeEl('div', 'jfvr-wd-body');
|
||||
|
||||
var msg = NS.makeEl('p');
|
||||
var waited = Math.round(timeoutMs(v) / 1000);
|
||||
msg.innerHTML =
|
||||
(reason === 'error'
|
||||
? '播放器报错了,一个画面都没解出来。'
|
||||
: '等了 ' + waited + ' 秒都没拿到任何画面(<code>videoWidth = 0</code>),' +
|
||||
'所以播放器界面也没有被创建出来。') +
|
||||
'最常见的原因是<strong>这台设备解不动这个格式</strong>:' +
|
||||
'<span class="jfvr-wd-fmt">' + (W.codec || '该格式') + '</span>。<br>' +
|
||||
'让服务器转码成 H.264 再播就能看到画面,代价是画质下降(这台 NAS 的转码上限约 4K)。';
|
||||
body.appendChild(msg);
|
||||
|
||||
body.appendChild(NS.makeEl('div', 'jfvr-wd-sub',
|
||||
'选一档码率后会写入播放设置并重新加载页面。'));
|
||||
|
||||
var btns = NS.makeEl('div', 'jfvr-wd-btns');
|
||||
CHOICES.forEach(function (c) {
|
||||
var b = NS.makeEl('button');
|
||||
b.type = 'button';
|
||||
b.appendChild(NS.makeEl('span', null, c.label));
|
||||
b.appendChild(NS.makeEl('span', 'note', c.note));
|
||||
b.addEventListener('click', function (ev) {
|
||||
ev.stopPropagation();
|
||||
applyBitrate(c.bitrate);
|
||||
});
|
||||
btns.appendChild(b);
|
||||
});
|
||||
body.appendChild(btns);
|
||||
|
||||
var quiet = NS.makeEl('button', 'jfvr-wd-quiet', '继续等待,先别管我');
|
||||
quiet.type = 'button';
|
||||
quiet.addEventListener('click', function (ev) {
|
||||
ev.stopPropagation();
|
||||
setQuiet(QUIET_MS);
|
||||
hide();
|
||||
});
|
||||
body.appendChild(quiet);
|
||||
|
||||
var diag = NS.makeEl('details', 'jfvr-wd-diag');
|
||||
diag.appendChild(NS.makeEl('summary', null, '诊断信息'));
|
||||
diag.appendChild(NS.makeEl('pre', null, diagText(v)));
|
||||
body.appendChild(diag);
|
||||
|
||||
p.appendChild(body);
|
||||
document.body.appendChild(p);
|
||||
W.panel = p;
|
||||
|
||||
NS.recordError('watchdog', new Error('playback never started: ' + reason));
|
||||
log('watchdog: playback never started, reason =', reason, 'codec =', W.codec);
|
||||
}
|
||||
|
||||
function hide() {
|
||||
W.shown = false;
|
||||
if (W.panel) { W.panel.remove(); W.panel = null; }
|
||||
}
|
||||
|
||||
function reset() {
|
||||
W.seenAt = 0;
|
||||
W.codec = null;
|
||||
if (W.shown) hide();
|
||||
}
|
||||
|
||||
function timeoutMs(v) {
|
||||
return isTranscode(v) ? DEAD_TRANSCODE_MS : DEAD_MS;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* poll
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
function poll() {
|
||||
// Never interfere with an active VR session: the session owns the flow and
|
||||
// a modal in the middle of it would be a disaster.
|
||||
if (S.session) { if (W.shown) hide(); return; }
|
||||
|
||||
var v = playerVideo();
|
||||
if (!v) { reset(); return; }
|
||||
|
||||
// Playback is fine (or at least has a picture): stand down.
|
||||
if (hasPicture(v)) { reset(); return; }
|
||||
|
||||
// A media error is unambiguous — no need to wait out the timer.
|
||||
if (v.error) {
|
||||
describeCodec(v);
|
||||
if (W.quietUntil && now() < W.quietUntil) return;
|
||||
show(v, 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!W.seenAt) W.seenAt = now();
|
||||
if (now() - W.seenAt < timeoutMs(v)) return;
|
||||
|
||||
if (W.quietUntil && now() < W.quietUntil) return;
|
||||
|
||||
describeCodec(v);
|
||||
show(v, 'timeout');
|
||||
}
|
||||
|
||||
NS.Watchdog.init = function () {
|
||||
W.quietUntil = readQuiet();
|
||||
setInterval(poll, POLL_MS);
|
||||
log('watchdog armed (dead=' + DEAD_MS + 'ms, transcode=' + DEAD_TRANSCODE_MS + 'ms)');
|
||||
};
|
||||
})();
|
||||
|
||||
|
||||
|
||||
/* ===== 90-main.js ===== */
|
||||
|
||||
/*!
|
||||
@@ -4699,16 +5347,23 @@
|
||||
// Sits ABOVE Jellyfin's own OSD bar: at the viewport's bottom-right it
|
||||
// covered the player's fullscreen button (owner report). 120 px clears the
|
||||
// OSD bottom bar; the settings panel stacks above it.
|
||||
'.jfvr-btn{position:fixed;right:16px;bottom:120px;z-index:2147483000;display:flex;align-items:center;gap:8px;',
|
||||
// left/top are set from JS (see placeButton) so the button can be dragged
|
||||
// anywhere. touch-action:none is what makes a drag work on a touch screen
|
||||
// instead of scrolling the page underneath.
|
||||
'.jfvr-btn{position:fixed;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}',
|
||||
'font-size:15px;font-weight:600;cursor:grab;box-shadow:0 6px 22px rgba(0,0,0,.45);font-family:inherit;',
|
||||
'touch-action:none;user-select:none;-webkit-user-select:none;-webkit-tap-highlight-color:transparent}',
|
||||
'.jfvr-btn:active{transform:scale(.96)}',
|
||||
'.jfvr-btn.jfvr-drag{cursor:grabbing;transform:none;box-shadow:0 10px 32px rgba(0,0,0,.65)}',
|
||||
// the panel must always fit the viewport AND keep the CTA reachable, so the
|
||||
// body scrolls and the button lives in a sticky footer
|
||||
'.jfvr-panel{position:fixed;right:16px;bottom:180px;z-index:2147483001;width:290px;background:rgba(20,20,24,.96);',
|
||||
// Positioned from JS (positionPanel) so it follows the button once the
|
||||
// button has been dragged; it no longer lives in the viewport corner.
|
||||
'.jfvr-panel{position:fixed;z-index:2147483001;width:290px;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);display:flex;flex-direction:column;',
|
||||
'max-height:calc(100vh - 200px);overflow:hidden}',
|
||||
'max-height:calc(100vh - 32px);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;flex:0 0 auto}',
|
||||
'.jfvr-x{background:none;border:0;color:#aaa;font-size:20px;line-height:1;cursor:pointer;padding:0 4px}',
|
||||
'.jfvr-body{padding:12px;overflow-y:auto;flex:1 1 auto;min-height:0}',
|
||||
@@ -4756,9 +5411,151 @@
|
||||
var b = NS.makeEl('button', 'jfvr-btn');
|
||||
b.type = 'button';
|
||||
b.innerHTML = '<span>🎥</span><span>VR 播放</span>';
|
||||
b.addEventListener('click', function (ev) { ev.stopPropagation(); togglePanel(); });
|
||||
b.title = '点击打开设置;按住拖动可以换位置;双击回到默认位置';
|
||||
b.addEventListener('click', function (ev) {
|
||||
ev.stopPropagation();
|
||||
// A drag still produces a click on most browsers. Swallow that one so
|
||||
// dragging the button never opens the panel by accident.
|
||||
if (dragMoved) { dragMoved = false; return; }
|
||||
togglePanel();
|
||||
});
|
||||
// Escape hatch: a button dragged half off a small window is annoying to
|
||||
// recover by hand.
|
||||
b.addEventListener('dblclick', function (ev) {
|
||||
ev.stopPropagation();
|
||||
resetButtonPos();
|
||||
});
|
||||
installDrag(b);
|
||||
document.body.appendChild(b);
|
||||
btn = b;
|
||||
btnPos = readBtnPos();
|
||||
placeButton(b, btnPos);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* button placement / dragging
|
||||
* ------------------------------------------------------------------
|
||||
* The button used to be pinned to the bottom-right corner. On a short window
|
||||
* that put it right on top of the bitrate choices the playback watchdog
|
||||
* shows (that panel is centred), and there was no way to move it. It is
|
||||
* draggable now and the spot is remembered per browser.
|
||||
*
|
||||
* The settings panel follows the button rather than the viewport corner, so
|
||||
* the two cannot drift apart.
|
||||
* ------------------------------------------------------------------ */
|
||||
var BTN_POS_KEY = 'jfvr-btn-pos';
|
||||
var BTN_MARGIN = 8;
|
||||
var DRAG_SLOP = 4; // px of travel before it counts as a drag, not a tap
|
||||
|
||||
var btnPos = null; // {left, top} once placed; persisted on drop
|
||||
var dragging = false;
|
||||
var dragMoved = false;
|
||||
var dragStart = null;
|
||||
|
||||
function readBtnPos() {
|
||||
try {
|
||||
var o = JSON.parse(localStorage.getItem(BTN_POS_KEY) || 'null');
|
||||
if (o && typeof o.left === 'number' && typeof o.top === 'number') return o;
|
||||
} catch (e) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
function writeBtnPos(pos) {
|
||||
try { localStorage.setItem(BTN_POS_KEY, JSON.stringify(pos)); } catch (e) {}
|
||||
}
|
||||
|
||||
function clampPos(left, top, w, h) {
|
||||
var maxLeft = Math.max(BTN_MARGIN, window.innerWidth - w - BTN_MARGIN);
|
||||
var maxTop = Math.max(BTN_MARGIN, window.innerHeight - h - BTN_MARGIN);
|
||||
return {
|
||||
left: Math.round(Math.min(Math.max(left, BTN_MARGIN), maxLeft)),
|
||||
top: Math.round(Math.min(Math.max(top, BTN_MARGIN), maxTop))
|
||||
};
|
||||
}
|
||||
|
||||
/** The default spot: the old fixed corner, expressed in left/top. */
|
||||
function defaultPos(b) {
|
||||
return { left: window.innerWidth - b.offsetWidth - 16,
|
||||
top: window.innerHeight - b.offsetHeight - 120 };
|
||||
}
|
||||
|
||||
function placeButton(b, pos) {
|
||||
var p = pos || defaultPos(b);
|
||||
var c = clampPos(p.left, p.top, b.offsetWidth, b.offsetHeight);
|
||||
b.style.left = c.left + 'px';
|
||||
b.style.top = c.top + 'px';
|
||||
b.style.right = 'auto';
|
||||
b.style.bottom = 'auto';
|
||||
return c;
|
||||
}
|
||||
|
||||
function resetButtonPos() {
|
||||
if (!btn) return;
|
||||
try { localStorage.removeItem(BTN_POS_KEY); } catch (e) {}
|
||||
btnPos = placeButton(btn, null);
|
||||
positionPanel();
|
||||
}
|
||||
|
||||
function installDrag(b) {
|
||||
b.addEventListener('pointerdown', function (ev) {
|
||||
if (ev.button != null && ev.button !== 0) return; // primary button / touch only
|
||||
var r = b.getBoundingClientRect();
|
||||
dragging = true;
|
||||
dragMoved = false;
|
||||
dragStart = { x: ev.clientX, y: ev.clientY, left: r.left, top: r.top };
|
||||
b.classList.add('jfvr-drag');
|
||||
try { b.setPointerCapture(ev.pointerId); } catch (e) {}
|
||||
});
|
||||
|
||||
b.addEventListener('pointermove', function (ev) {
|
||||
if (!dragging || !dragStart) return;
|
||||
var dx = ev.clientX - dragStart.x;
|
||||
var dy = ev.clientY - dragStart.y;
|
||||
if (!dragMoved) {
|
||||
if (Math.abs(dx) < DRAG_SLOP && Math.abs(dy) < DRAG_SLOP) return;
|
||||
dragMoved = true;
|
||||
if (panel) hidePanel(); // do not drag the panel around with it
|
||||
}
|
||||
btnPos = clampPos(dragStart.left + dx, dragStart.top + dy, b.offsetWidth, b.offsetHeight);
|
||||
b.style.left = btnPos.left + 'px';
|
||||
b.style.top = btnPos.top + 'px';
|
||||
});
|
||||
|
||||
function endDrag(ev) {
|
||||
if (!dragging) return;
|
||||
dragging = false;
|
||||
dragStart = null;
|
||||
b.classList.remove('jfvr-drag');
|
||||
try { b.releasePointerCapture(ev.pointerId); } catch (e) {}
|
||||
if (dragMoved) writeBtnPos(btnPos);
|
||||
}
|
||||
b.addEventListener('pointerup', endDrag);
|
||||
b.addEventListener('pointercancel', endDrag);
|
||||
}
|
||||
|
||||
/** Put the panel next to the button: above it, or below when there is no room. */
|
||||
function positionPanel() {
|
||||
if (!panel || !btn) return;
|
||||
var b = btn.getBoundingClientRect();
|
||||
var w = panel.offsetWidth;
|
||||
var h = panel.offsetHeight;
|
||||
var gap = 8;
|
||||
|
||||
var left = b.right - w; // right edges aligned
|
||||
var top = b.top - h - gap;
|
||||
if (top < BTN_MARGIN) top = b.bottom + gap; // no room above -> below
|
||||
|
||||
var maxLeft = Math.max(BTN_MARGIN, window.innerWidth - w - BTN_MARGIN);
|
||||
var maxTop = Math.max(BTN_MARGIN, window.innerHeight - h - BTN_MARGIN);
|
||||
panel.style.left = Math.round(Math.min(Math.max(left, BTN_MARGIN), maxLeft)) + 'px';
|
||||
panel.style.top = Math.round(Math.min(Math.max(top, BTN_MARGIN), maxTop)) + 'px';
|
||||
panel.style.right = 'auto';
|
||||
panel.style.bottom = 'auto';
|
||||
}
|
||||
|
||||
function onViewportResize() {
|
||||
if (btn && document.body.contains(btn)) btnPos = placeButton(btn, btnPos);
|
||||
positionPanel();
|
||||
}
|
||||
|
||||
function removeButton() {
|
||||
@@ -4942,6 +5739,7 @@
|
||||
panel.appendChild(foot);
|
||||
|
||||
document.body.appendChild(panel);
|
||||
positionPanel();
|
||||
|
||||
panelSync = function () {
|
||||
for (var i = 0; i < panelSyncs.length; i++) {
|
||||
@@ -6343,6 +7141,9 @@
|
||||
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); }
|
||||
// Watches for "the player never produced a picture" and offers the only fix
|
||||
// that helps (let the server transcode). See 40-watchdog.js.
|
||||
try { if (NS.Watchdog && NS.Watchdog.init) NS.Watchdog.init(); } catch (e) { NS.recordError('Watchdog.init', e); }
|
||||
|
||||
scan();
|
||||
new MutationObserver(scan).observe(document.documentElement, { childList: true, subtree: true });
|
||||
@@ -6350,6 +7151,9 @@
|
||||
window.addEventListener('beforeunload', function () {
|
||||
if (S.session) { try { S.session.end(); } catch (e) {} }
|
||||
});
|
||||
// Keep the draggable button (and the panel that follows it) on screen when
|
||||
// the window changes size.
|
||||
window.addEventListener('resize', onViewportResize);
|
||||
installFrameTracker();
|
||||
|
||||
window.__JFVR_STATE__ = S;
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
[
|
||||
{
|
||||
"name": "Jellyfin Web VR",
|
||||
"guid": "7d33ef59-f9b0-412b-93bd-5cd5353018de",
|
||||
"overview": "VR180/VR360 playback inside Jellyfin Web for Meta Quest 3.",
|
||||
"description": "Injects a \"VR 播放\" button into the Jellyfin Web video player page and drives an immersive WebXR session on Meta Quest 3, preferring XRMediaBinding.createEquirectLayer and falling back to XRWebGLBinding. Ships equirectangular 180/360 plus SBS/OU layouts, in-headset subtitles, a wrist controller, and hand tracking.\n",
|
||||
"owner": "cat-shark",
|
||||
"category": "General",
|
||||
"versions": [
|
||||
{
|
||||
"version": "0.5.2.0",
|
||||
"targetAbi": "12.0.0.0",
|
||||
"changelog": "Bundle version: `16ae94`\n\nFirst plugin build. The web bundle is embedded in the DLL and served from /JFVR/jfvr.js; index.html is rewritten at request time, so nothing is written to the jellyfin-web folder on disk.",
|
||||
"sourceUrl": "https://git.cat-shark.xyz/cat-shark/jellyfin_vr_web_plugin/releases/download/v0.5.2.0/Jellyfin.Plugin.JFVR_12.0.0.0.zip",
|
||||
"checksum": "C5BD8E8C384642D4502BE1A0F80B8E68",
|
||||
"timestamp": "2026-10-19T15:01:57"
|
||||
},
|
||||
{
|
||||
"version": "0.5.1.0",
|
||||
"targetAbi": "12.0.0.0",
|
||||
"changelog": "Bundle version: `3e611e`\n\nFirst plugin build. The web bundle is embedded in the DLL and served from /JFVR/jfvr.js; index.html is rewritten at request time, so nothing is written to the jellyfin-web folder on disk.",
|
||||
"sourceUrl": "https://git.cat-shark.xyz/cat-shark/jellyfin_vr_web_plugin/releases/download/v0.5.1.0/Jellyfin.Plugin.JFVR_12.0.0.0.zip",
|
||||
"checksum": "54AB6541A1BFD7A62636A435350821E0",
|
||||
"timestamp": "2026-10-18T03:57:47"
|
||||
},
|
||||
{
|
||||
"version": "0.5.0.0",
|
||||
"targetAbi": "12.0.0.0",
|
||||
"changelog": "Bundle version: `72681d`\n\nFirst plugin build. The web bundle is embedded in the DLL and served from /JFVR/jfvr.js; index.html is rewritten at request time, so nothing is written to the jellyfin-web folder on disk.",
|
||||
"sourceUrl": "https://git.cat-shark.xyz/cat-shark/jellyfin_vr_web_plugin/releases/download/v0.5.0.0/Jellyfin.Plugin.JFVR_12.0.0.0.zip",
|
||||
"checksum": "F9773C70C9CC4858AF042E40D5CFBAA9",
|
||||
"timestamp": "2026-10-17T17:34:28"
|
||||
},
|
||||
{
|
||||
"version": "0.4.0.0",
|
||||
"targetAbi": "12.0.0.0",
|
||||
"changelog": "Bundle version: `ae28bd`\n\nFirst plugin build. The web bundle is embedded in the DLL and served from /JFVR/jfvr.js; index.html is rewritten at request time, so nothing is written to the jellyfin-web folder on disk.",
|
||||
"sourceUrl": "https://git.cat-shark.xyz/cat-shark/jellyfin_vr_web_plugin/releases/download/v0.4.0.0/Jellyfin.Plugin.JFVR_12.0.0.0.zip",
|
||||
"checksum": "30A0E388FFD6A3D88C73DAB5928C6388",
|
||||
"timestamp": "2026-10-17T17:20:34"
|
||||
},
|
||||
{
|
||||
"version": "0.3.0.0",
|
||||
"targetAbi": "12.0.0.0",
|
||||
"changelog": "Bundle version: `4e9a8f`\n\nFirst plugin build. The web bundle is embedded in the DLL and served from /JFVR/jfvr.js; index.html is rewritten at request time, so nothing is written to the jellyfin-web folder on disk.",
|
||||
"sourceUrl": "https://git.cat-shark.xyz/cat-shark/jellyfin_vr_web_plugin/releases/download/v0.3.0.0/Jellyfin.Plugin.JFVR_12.0.0.0.zip",
|
||||
"checksum": "1BFAF246451C090E351F9DDD516BD4DC",
|
||||
"timestamp": "2026-10-17T17:08:08"
|
||||
},
|
||||
{
|
||||
"version": "0.2.0.0",
|
||||
"targetAbi": "12.0.0.0",
|
||||
"changelog": "Bundle version: `edca47`\n\nFirst plugin build. The web bundle is embedded in the DLL and served from /JFVR/jfvr.js; index.html is rewritten at request time, so nothing is written to the jellyfin-web folder on disk.",
|
||||
"sourceUrl": "https://git.cat-shark.xyz/cat-shark/jellyfin_vr_web_plugin/releases/download/v0.2.0.0/Jellyfin.Plugin.JFVR_12.0.0.0.zip",
|
||||
"checksum": "355DD18DC228A6586334431518C4B60D",
|
||||
"timestamp": "2026-10-17T16:28:23"
|
||||
},
|
||||
{
|
||||
"version": "0.1.0.1",
|
||||
"targetAbi": "12.0.0.0",
|
||||
"changelog": "Bundle version: `917fde`\n\nFirst plugin build. The web bundle is embedded in the DLL and served from /JFVR/jfvr.js; index.html is rewritten at request time, so nothing is written to the jellyfin-web folder on disk.",
|
||||
"sourceUrl": "https://git.cat-shark.xyz/cat-shark/jellyfin_vr_web_plugin/releases/download/v0.1.0.1/Jellyfin.Plugin.JFVR_12.0.0.0.zip",
|
||||
"checksum": "5BB9DA51A50C5449E7C1D0D6602E8813",
|
||||
"timestamp": "2026-10-17T15:33:14"
|
||||
},
|
||||
{
|
||||
"version": "0.1.0.0",
|
||||
"targetAbi": "12.0.0.0",
|
||||
"changelog": "Bundle version: `917fde`\n\nFirst plugin build. The web bundle is embedded in the DLL and served from /JFVR/jfvr.js; index.html is rewritten at request time, so nothing is written to the jellyfin-web folder on disk.",
|
||||
"sourceUrl": "https://git.cat-shark.xyz/cat-shark/jellyfin_vr_web_plugin/releases/download/v0.1.0.0/Jellyfin.Plugin.JFVR_12.0.0.0.zip",
|
||||
"checksum": "F9CC1843F0B9386A9E990669112FF533",
|
||||
"timestamp": "2026-10-17T15:25:41"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,451 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Build the JFVR Jellyfin plugin, package it the way Jellyfin expects, and
|
||||
(optionally) publish it to the Gitea repo that doubles as the plugin catalog.
|
||||
|
||||
What it does, in order:
|
||||
1. build.py src/*.js -> dist/jfvr.js
|
||||
2. dotnet build plugin/Jellyfin.Plugin.JFVR -> Jellyfin.Plugin.JFVR.dll
|
||||
(the bundle is embedded straight from dist/jfvr.js)
|
||||
3. verify the DLL really contains the bytes we just built
|
||||
4. zip one DLL, named <assembly>_<targetAbi>.zip
|
||||
5. manifest.json add/replace this version's entry (MD5 = the zip's MD5,
|
||||
which Jellyfin re-checks at install time)
|
||||
6. --publish create the Gitea release, upload the zip, push the
|
||||
manifest so Jellyfin can see it
|
||||
|
||||
Usage:
|
||||
python3 pack.py # build + package + update manifest.json
|
||||
python3 pack.py --bump # same, after bumping the last version field
|
||||
python3 pack.py --version 0.2.0.0 # same, with an explicit version
|
||||
python3 pack.py --publish # ... and publish to Gitea
|
||||
python3 pack.py --print-urls # just show the catalog URL
|
||||
|
||||
Env:
|
||||
GITEA_TOKEN required by --publish. Scope: write:repository.
|
||||
Falls back to a GITEA_TOKEN line in secrets.local.md.
|
||||
GITEA_URL default https://git.cat-shark.xyz
|
||||
GITEA_REPO default cat-shark/jellyfin_vr_web_plugin
|
||||
GITEA_BRANCH default main
|
||||
DOTNET path to the dotnet binary (default: $PATH, then ~/.dotnet/dotnet)
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
import zipfile
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import yaml
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
PLUGIN_DIR = os.path.join(HERE, "plugin")
|
||||
BUILD_YAML = os.path.join(PLUGIN_DIR, "build.yaml")
|
||||
PROJECT = os.path.join(PLUGIN_DIR, "Jellyfin.Plugin.JFVR", "Jellyfin.Plugin.JFVR.csproj")
|
||||
BUNDLE = os.path.join(HERE, "dist", "jfvr.js")
|
||||
OUT_DIR = os.path.join(HERE, "dist", "plugin")
|
||||
MANIFEST = os.path.join(HERE, "manifest.json")
|
||||
SECRETS = os.path.join(HERE, "secrets.local.md")
|
||||
|
||||
ASSEMBLY = "Jellyfin.Plugin.JFVR"
|
||||
MAX_VERSIONS = 20
|
||||
|
||||
GITEA_URL = os.environ.get("GITEA_URL", "https://git.cat-shark.xyz").rstrip("/")
|
||||
GITEA_REPO = os.environ.get("GITEA_REPO", "cat-shark/jellyfin_vr_web_plugin")
|
||||
GITEA_BRANCH = os.environ.get("GITEA_BRANCH", "main")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- helpers
|
||||
|
||||
|
||||
def run(cmd, **kw):
|
||||
"""Run a command, echo it, and abort on failure."""
|
||||
print("$ " + " ".join(cmd))
|
||||
r = subprocess.run(cmd, capture_output=True, text=True, **kw)
|
||||
if r.stdout.strip():
|
||||
print(r.stdout.rstrip())
|
||||
if r.returncode != 0:
|
||||
print(r.stderr.rstrip(), file=sys.stderr)
|
||||
sys.exit("command failed: %s" % cmd[0])
|
||||
if r.stderr.strip():
|
||||
print(r.stderr.rstrip(), file=sys.stderr)
|
||||
return r
|
||||
|
||||
|
||||
def dotnet_bin():
|
||||
explicit = os.environ.get("DOTNET")
|
||||
if explicit:
|
||||
return explicit
|
||||
found = shutil.which("dotnet")
|
||||
if found:
|
||||
return found
|
||||
local = os.path.expanduser("~/.dotnet/dotnet")
|
||||
if os.path.exists(local):
|
||||
return local
|
||||
sys.exit("dotnet not found. Install the .NET 10 SDK, or set DOTNET=/path/to/dotnet")
|
||||
|
||||
|
||||
def md5_of(path):
|
||||
h = hashlib.md5()
|
||||
with open(path, "rb") as fh:
|
||||
for chunk in iter(lambda: fh.read(1 << 20), b""):
|
||||
h.update(chunk)
|
||||
return h.hexdigest().upper()
|
||||
|
||||
|
||||
def bundle_version():
|
||||
"""The 6-char version the in-page HUD reports (build.py uses the same rule)."""
|
||||
with open(BUNDLE, "rb") as fh:
|
||||
return hashlib.md5(fh.read()).hexdigest()[:6]
|
||||
|
||||
|
||||
def read_build_yaml():
|
||||
with open(BUILD_YAML, encoding="utf-8") as fh:
|
||||
return yaml.safe_load(fh)
|
||||
|
||||
|
||||
def bump(version):
|
||||
parts = version.split(".")
|
||||
while len(parts) < 4:
|
||||
parts.append("0")
|
||||
parts[-1] = str(int(parts[-1]) + 1)
|
||||
return ".".join(parts)
|
||||
|
||||
|
||||
def write_build_yaml_version(version):
|
||||
with open(BUILD_YAML, encoding="utf-8") as fh:
|
||||
text = fh.read()
|
||||
text = re.sub(r'^version:\s*".*?"', 'version: "%s"' % version, text, count=1, flags=re.M)
|
||||
with open(BUILD_YAML, "w", encoding="utf-8") as fh:
|
||||
fh.write(text)
|
||||
|
||||
|
||||
def catalog_url():
|
||||
return "%s/%s/raw/branch/%s/manifest.json" % (GITEA_URL, GITEA_REPO, GITEA_BRANCH)
|
||||
|
||||
|
||||
def source_url(tag, asset):
|
||||
return "%s/%s/releases/download/%s/%s" % (GITEA_URL, GITEA_REPO, tag, asset)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- gitea
|
||||
|
||||
|
||||
def gitea_token():
|
||||
token = os.environ.get("GITEA_TOKEN")
|
||||
if token:
|
||||
return token.strip()
|
||||
if os.path.exists(SECRETS):
|
||||
with open(SECRETS, encoding="utf-8") as fh:
|
||||
for line in fh:
|
||||
m = re.match(r"^\s*GITEA_TOKEN\s*[:=]\s*(\S+)", line)
|
||||
if m:
|
||||
return m.group(1)
|
||||
sys.exit(
|
||||
"--publish needs a Gitea token.\n"
|
||||
"Create one at %s/user/settings/applications (scope: write:repository)\n"
|
||||
"then export GITEA_TOKEN=... or add a 'GITEA_TOKEN: ...' line to secrets.local.md"
|
||||
% GITEA_URL
|
||||
)
|
||||
|
||||
|
||||
def gitea_request(method, path, token, body=None, ctype="application/json"):
|
||||
url = "%s/api/v1%s" % (GITEA_URL, path)
|
||||
data = json.dumps(body).encode() if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, method=method)
|
||||
req.add_header("Authorization", "token " + token)
|
||||
req.add_header("Content-Type", ctype)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=60) as resp:
|
||||
raw = resp.read()
|
||||
return resp.status, (json.loads(raw) if raw else None)
|
||||
except urllib.error.HTTPError as e:
|
||||
raw = e.read()
|
||||
try:
|
||||
return e.code, json.loads(raw)
|
||||
except Exception:
|
||||
return e.code, raw.decode("utf-8", "replace")
|
||||
|
||||
|
||||
def multipart(fields, files):
|
||||
boundary = "----JFVR" + uuid.uuid4().hex
|
||||
out = bytearray()
|
||||
for key, value in fields.items():
|
||||
out += ('--%s\r\nContent-Disposition: form-data; name="%s"\r\n\r\n%s\r\n'
|
||||
% (boundary, key, value)).encode()
|
||||
for key, (filename, data, ctype) in files.items():
|
||||
out += ('--%s\r\nContent-Disposition: form-data; name="%s"; filename="%s"\r\n'
|
||||
'Content-Type: %s\r\n\r\n' % (boundary, key, filename, ctype)).encode()
|
||||
out += data + b"\r\n"
|
||||
out += ("--%s--\r\n" % boundary).encode()
|
||||
return bytes(out), "multipart/form-data; boundary=%s" % boundary
|
||||
|
||||
|
||||
def publish(tag, meta, zip_path, asset_name, changelog, token):
|
||||
path = "/repos/%s/releases/tags/%s" % (GITEA_REPO, tag)
|
||||
status, release = gitea_request("GET", path, token)
|
||||
|
||||
if status == 200:
|
||||
print("release %s already exists (id %s)" % (tag, release["id"]))
|
||||
else:
|
||||
status, release = gitea_request("POST", "/repos/%s/releases" % GITEA_REPO, token, {
|
||||
"tag_name": tag,
|
||||
"name": "%s %s" % (meta["name"], tag),
|
||||
"body": changelog,
|
||||
"draft": False,
|
||||
"prerelease": False,
|
||||
})
|
||||
if status not in (200, 201):
|
||||
sys.exit("creating release %s failed (%s): %s" % (tag, status, release))
|
||||
print("created release %s (id %s)" % (tag, release["id"]))
|
||||
|
||||
release_id = release["id"]
|
||||
|
||||
for asset in release.get("assets") or []:
|
||||
if asset["name"] == asset_name:
|
||||
gitea_request("DELETE",
|
||||
"/repos/%s/releases/%s/assets/%s" % (GITEA_REPO, release_id, asset["id"]),
|
||||
token)
|
||||
print("replaced existing asset %s" % asset_name)
|
||||
|
||||
with open(zip_path, "rb") as fh:
|
||||
payload, ctype = multipart({}, {"attachment": (asset_name, fh.read(), "application/zip")})
|
||||
|
||||
url = "%s/api/v1/repos/%s/releases/%s/assets?name=%s" % (GITEA_URL, GITEA_REPO, release_id, asset_name)
|
||||
req = urllib.request.Request(url, data=payload, method="POST")
|
||||
req.add_header("Authorization", "token " + token)
|
||||
req.add_header("Content-Type", ctype)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=300) as resp:
|
||||
json.loads(resp.read())
|
||||
except urllib.error.HTTPError as e:
|
||||
sys.exit("uploading %s failed (%s): %s" % (asset_name, e.code, e.read().decode("utf-8", "replace")))
|
||||
print("uploaded %s" % asset_name)
|
||||
|
||||
|
||||
def head_ok(url):
|
||||
"""A draft release 404s on its download URL; verify the asset is really live."""
|
||||
req = urllib.request.Request(url, method="HEAD")
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=60) as resp:
|
||||
return resp.status, resp.headers.get("Content-Length")
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, None
|
||||
|
||||
|
||||
def fetch_md5(url):
|
||||
"""Download the published asset and hash it -- exactly what Jellyfin does.
|
||||
|
||||
This is the check that catches a manifest whose checksum no longer matches the
|
||||
uploaded zip (a real failure mode: the install then dies with
|
||||
"The checksum of the received data doesn't match.").
|
||||
"""
|
||||
h = hashlib.md5()
|
||||
req = urllib.request.Request(url, method="GET")
|
||||
with urllib.request.urlopen(req, timeout=300) as resp:
|
||||
while True:
|
||||
chunk = resp.read(1 << 20)
|
||||
if not chunk:
|
||||
break
|
||||
h.update(chunk)
|
||||
return h.hexdigest().upper()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- steps
|
||||
|
||||
|
||||
def build_bundle():
|
||||
run([sys.executable, os.path.join(HERE, "build.py")], cwd=HERE)
|
||||
|
||||
|
||||
def build_plugin(version):
|
||||
env = dict(os.environ)
|
||||
dotnet = dotnet_bin()
|
||||
env["DOTNET_ROOT"] = os.path.dirname(dotnet)
|
||||
env["DOTNET_CLI_TELEMETRY_OPTOUT"] = "1"
|
||||
env["DOTNET_NOLOGO"] = "1"
|
||||
run([dotnet, "build", PROJECT, "-c", "Release", "-p:Version=" + version], cwd=HERE, env=env)
|
||||
dll = os.path.join(os.path.dirname(PROJECT), "bin", "Release", "net10.0", ASSEMBLY + ".dll")
|
||||
if not os.path.exists(dll):
|
||||
sys.exit("expected build output missing: %s" % dll)
|
||||
return dll
|
||||
|
||||
|
||||
def verify_embedded(dll):
|
||||
"""Guard against shipping a DLL whose embedded bundle is not what we built."""
|
||||
with open(dll, "rb") as fh:
|
||||
blob = fh.read()
|
||||
with open(BUNDLE, "rb") as fh:
|
||||
bundle = fh.read()
|
||||
at = blob.find(bundle)
|
||||
if at < 0:
|
||||
sys.exit("the built DLL does not contain dist/jfvr.js -- refusing to package")
|
||||
if int.from_bytes(blob[at - 4:at], "little") != len(bundle):
|
||||
sys.exit("embedded bundle length prefix mismatch -- refusing to package")
|
||||
print("verified: DLL embeds %d bytes of dist/jfvr.js (md5 %s)" % (len(bundle), bundle_version()))
|
||||
|
||||
|
||||
def make_zip(dll, target_abi):
|
||||
"""Package the DLL the way Jellyfin expects: one file, named after the assembly.
|
||||
|
||||
The entry gets a fixed timestamp on purpose. zip stores mtimes, so packaging
|
||||
the same DLL twice would otherwise produce different bytes, a different MD5,
|
||||
and a manifest checksum that no longer matches the already-uploaded asset.
|
||||
"""
|
||||
os.makedirs(OUT_DIR, exist_ok=True)
|
||||
asset = "%s_%s.zip" % (ASSEMBLY, target_abi)
|
||||
path = os.path.join(OUT_DIR, asset)
|
||||
|
||||
info = zipfile.ZipInfo(ASSEMBLY + ".dll", date_time=(1980, 1, 1, 0, 0, 0))
|
||||
info.compress_type = zipfile.ZIP_DEFLATED
|
||||
info.external_attr = 0o644 << 16
|
||||
|
||||
with open(dll, "rb") as fh:
|
||||
payload = fh.read()
|
||||
with zipfile.ZipFile(path, "w") as zf:
|
||||
zf.writestr(info, payload)
|
||||
|
||||
print("packaged %s (%d bytes)" % (path, os.path.getsize(path)))
|
||||
return path, asset
|
||||
|
||||
|
||||
def update_manifest(meta, version, asset, checksum, changelog):
|
||||
if os.path.exists(MANIFEST):
|
||||
with open(MANIFEST, encoding="utf-8") as fh:
|
||||
manifest = json.load(fh)
|
||||
else:
|
||||
manifest = []
|
||||
|
||||
package = next((p for p in manifest if p.get("guid") == meta["guid"]), None)
|
||||
if package is None:
|
||||
package = {
|
||||
"name": meta["name"],
|
||||
"guid": meta["guid"],
|
||||
"overview": meta["overview"],
|
||||
"description": meta["description"],
|
||||
"owner": meta["owner"],
|
||||
"category": meta["category"],
|
||||
"versions": [],
|
||||
}
|
||||
if meta.get("imageUrl"):
|
||||
package["imageUrl"] = meta["imageUrl"]
|
||||
manifest.append(package)
|
||||
|
||||
for key in ("name", "overview", "description", "owner", "category"):
|
||||
package[key] = meta[key]
|
||||
|
||||
entry = {
|
||||
"version": version,
|
||||
"targetAbi": meta["targetAbi"],
|
||||
"changelog": changelog,
|
||||
"sourceUrl": source_url("v" + version, asset),
|
||||
"checksum": checksum,
|
||||
"timestamp": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S"),
|
||||
}
|
||||
package["versions"] = [v for v in package["versions"] if v.get("version") != version]
|
||||
package["versions"].insert(0, entry)
|
||||
package["versions"] = package["versions"][:MAX_VERSIONS]
|
||||
|
||||
with open(MANIFEST, "w", encoding="utf-8") as fh:
|
||||
json.dump(manifest, fh, indent=2, ensure_ascii=False)
|
||||
fh.write("\n")
|
||||
return entry
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description=__doc__,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
ap.add_argument("--version", help="plugin version, e.g. 0.2.0.0")
|
||||
ap.add_argument("--bump", action="store_true", help="bump the last version component")
|
||||
ap.add_argument("--publish", action="store_true", help="create the Gitea release and upload the zip")
|
||||
ap.add_argument("--no-push", action="store_true", help="with --publish: skip git commit/push")
|
||||
ap.add_argument("--print-urls", action="store_true", help="print the catalog URL and exit")
|
||||
args = ap.parse_args()
|
||||
|
||||
if args.print_urls:
|
||||
print("Jellyfin plugin repository URL:")
|
||||
print(" " + catalog_url())
|
||||
return
|
||||
|
||||
meta = read_build_yaml()
|
||||
|
||||
if args.version:
|
||||
version = args.version
|
||||
write_build_yaml_version(version)
|
||||
elif args.bump:
|
||||
version = bump(meta["version"])
|
||||
write_build_yaml_version(version)
|
||||
meta = read_build_yaml()
|
||||
else:
|
||||
version = meta["version"]
|
||||
|
||||
print("=" * 72)
|
||||
print("JFVR plugin %s (targetAbi %s)" % (version, meta["targetAbi"]))
|
||||
print("=" * 72)
|
||||
|
||||
build_bundle()
|
||||
js_ver = bundle_version()
|
||||
|
||||
dll = build_plugin(version)
|
||||
verify_embedded(dll)
|
||||
|
||||
zip_path, asset = make_zip(dll, meta["targetAbi"])
|
||||
checksum = md5_of(zip_path)
|
||||
print("zip md5: %s" % checksum)
|
||||
|
||||
changelog = (meta.get("changelog") or "").strip()
|
||||
changelog = "Bundle version: `%s`\n\n%s" % (js_ver, changelog)
|
||||
|
||||
entry = update_manifest(meta, version, asset, checksum, changelog)
|
||||
print("manifest.json updated: %s" % entry["sourceUrl"])
|
||||
|
||||
if args.publish:
|
||||
token = gitea_token()
|
||||
publish("v" + version, meta, zip_path, asset, changelog, token)
|
||||
|
||||
status, length = head_ok(entry["sourceUrl"])
|
||||
print("asset check: HTTP %s, %s bytes" % (status, length))
|
||||
if status != 200:
|
||||
sys.exit("the asset is not downloadable -- is the release a draft?")
|
||||
|
||||
# What Jellyfin will do at install time: download it and compare MD5.
|
||||
live = fetch_md5(entry["sourceUrl"])
|
||||
if live != checksum:
|
||||
sys.exit("published asset md5 %s != manifest checksum %s -- install would fail"
|
||||
% (live, checksum))
|
||||
print("asset md5 matches the manifest checksum (%s)" % live)
|
||||
|
||||
if not args.no_push:
|
||||
run(["git", "add", "manifest.json", "plugin/build.yaml"], cwd=HERE)
|
||||
dirty = subprocess.run(
|
||||
["git", "diff", "--cached", "--quiet", "--", "manifest.json", "plugin/build.yaml"],
|
||||
cwd=HERE,
|
||||
).returncode != 0
|
||||
if dirty:
|
||||
run(["git", "commit", "-m", "plugin: publish %s (bundle %s)" % (version, js_ver)], cwd=HERE)
|
||||
else:
|
||||
print("manifest.json unchanged -- nothing to commit")
|
||||
run(["git", "push", "origin", GITEA_BRANCH], cwd=HERE)
|
||||
else:
|
||||
print("--no-push: push manifest.json yourself, Jellyfin reads it from the branch")
|
||||
|
||||
print()
|
||||
print("=" * 72)
|
||||
print("Jellyfin plugin repository URL:")
|
||||
print(" " + catalog_url())
|
||||
print()
|
||||
print("Bundle (in-page HUD version): %s" % js_ver)
|
||||
print("Install: Dashboard > Plugins > Catalog > gear > + > paste the URL above.")
|
||||
if not args.publish:
|
||||
print("Then: python3 pack.py --publish (creates the release and pushes the manifest)")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,129 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Jellyfin Web VR</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="JfvrConfigPage" data-role="page" class="page type-interior pluginConfigurationPage">
|
||||
<div data-role="content">
|
||||
<div class="content-primary">
|
||||
<form id="JfvrConfigForm">
|
||||
<div class="verticalSection">
|
||||
<div class="sectionTitleContainer flex align-items-center">
|
||||
<h2 class="sectionTitle">Jellyfin Web VR</h2>
|
||||
<a is="emby-linkbutton" class="raised raised-mini emby-button" style="margin-left:2em"
|
||||
target="_blank" rel="noopener"
|
||||
href="https://git.cat-shark.xyz/cat-shark/jellyfin_vr_web_plugin">
|
||||
<i class="md-icon button-icon button-icon-left secondaryText"></i>
|
||||
<span>Help</span>
|
||||
</a>
|
||||
</div>
|
||||
<p class="fieldDescription">
|
||||
在 jellyfin-web 的 <code>index.html</code> 里注入 VR 播放脚本,让 Quest 3
|
||||
浏览器直接进入 VR180 / VR360 沉浸播放。改写发生在请求时,磁盘上的文件不会被改动,
|
||||
所以 Jellyfin 升级不会丢掉注入。
|
||||
</p>
|
||||
<p class="fieldDescription">
|
||||
<strong>改完设置后需要刷新浏览器</strong>(<code>index.html</code> 本身没有强缓存,
|
||||
普通刷新即可)。
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="checkboxContainer checkboxContainer-withDescription">
|
||||
<label>
|
||||
<input is="emby-checkbox" type="checkbox" id="chkEnabled" />
|
||||
<span>启用注入</span>
|
||||
</label>
|
||||
<div class="fieldDescription checkboxFieldDescription">
|
||||
关闭后 <code>index.html</code> 原样返回,不再注入;脚本端点仍然可以访问。
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="inputContainer">
|
||||
<input is="emby-input" type="text" id="txtScriptUrl" label="脚本 URL 覆盖(可选)" />
|
||||
<div class="fieldDescription">
|
||||
留空则使用插件内置的 bundle:<code id="builtinUrl">/JFVR/jfvr.js</code>。
|
||||
填了就注入这个 URL(例如自建 CDN 上的副本),内置 bundle 不再使用。
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<button is="emby-button" type="submit" class="raised button-submit block">
|
||||
<span>保存</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="verticalSection" style="margin-top:1.5em">
|
||||
<h3 class="sectionTitle" style="font-size:1.1em">状态</h3>
|
||||
<div id="statusBox" class="fieldDescription">读取中…</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var PLUGIN_ID = '7d33ef59-f9b0-412b-93bd-5cd5353018de';
|
||||
|
||||
function statusLine(label, value) {
|
||||
return '<div><strong>' + label + ':</strong> ' + value + '</div>';
|
||||
}
|
||||
|
||||
function renderStatus() {
|
||||
var box = document.querySelector('#statusBox');
|
||||
var url;
|
||||
try {
|
||||
url = ApiClient.getUrl('JFVR/status');
|
||||
} catch (err) {
|
||||
url = '/JFVR/status';
|
||||
}
|
||||
|
||||
ApiClient.ajax({ type: 'GET', url: url, dataType: 'json' }).then(function (s) {
|
||||
box.innerHTML =
|
||||
statusLine('插件版本', s.version) +
|
||||
statusLine('bundle 版本', s.bundleHash + '(' + s.bundleBytes + ' bytes)') +
|
||||
statusLine('注入', s.enabled ? '已启用' : '已关闭') +
|
||||
statusLine('注入的 URL', '<code>' + s.injectedTag + '</code>');
|
||||
}).catch(function () {
|
||||
box.textContent = '无法读取 /JFVR/status(插件可能尚未加载完成)。';
|
||||
});
|
||||
}
|
||||
|
||||
function loadConfiguration() {
|
||||
Dashboard.showLoadingMsg();
|
||||
ApiClient.getPluginConfiguration(PLUGIN_ID).then(function (config) {
|
||||
document.querySelector('#chkEnabled').checked = config.Enabled !== false;
|
||||
document.querySelector('#txtScriptUrl').value = config.ScriptUrlOverride || '';
|
||||
Dashboard.hideLoadingMsg();
|
||||
renderStatus();
|
||||
});
|
||||
}
|
||||
|
||||
function saveConfiguration(e) {
|
||||
e.preventDefault();
|
||||
Dashboard.showLoadingMsg();
|
||||
ApiClient.getPluginConfiguration(PLUGIN_ID).then(function (config) {
|
||||
config.Enabled = document.querySelector('#chkEnabled').checked;
|
||||
config.ScriptUrlOverride = document.querySelector('#txtScriptUrl').value || '';
|
||||
ApiClient.updatePluginConfiguration(PLUGIN_ID, config).then(function (result) {
|
||||
Dashboard.processPluginConfigurationUpdateResult(result);
|
||||
renderStatus();
|
||||
});
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
document.querySelector('#JfvrConfigForm').addEventListener('submit', saveConfiguration);
|
||||
|
||||
document.addEventListener('pageshow', function (e) {
|
||||
if (e.target.id === 'JfvrConfigPage') {
|
||||
loadConfiguration();
|
||||
}
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.JFVR.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace Jellyfin.Plugin.JFVR.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// Serves the JFVR web bundle and a small status endpoint.
|
||||
///
|
||||
/// The bundle is anonymous on purpose: the injected tag lives in
|
||||
/// <c>index.html</c>, which the browser fetches before the user has
|
||||
/// authenticated, and the payload is the same JavaScript that is already public
|
||||
/// in the project repository.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("JFVR")]
|
||||
public class JfvrController : ControllerBase
|
||||
{
|
||||
private readonly BundleProvider _bundle;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JfvrController"/> class.
|
||||
/// </summary>
|
||||
/// <param name="bundle">Embedded web bundle.</param>
|
||||
public JfvrController(BundleProvider bundle)
|
||||
{
|
||||
_bundle = bundle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the JFVR web bundle.
|
||||
/// </summary>
|
||||
/// <returns>The bundle as JavaScript.</returns>
|
||||
[HttpGet("jfvr.js")]
|
||||
[AllowAnonymous]
|
||||
[Produces("application/javascript")]
|
||||
public ActionResult GetBundle()
|
||||
{
|
||||
var etag = "\"" + _bundle.Hash + "\"";
|
||||
|
||||
// The URL is versioned with ?v=<hash>, so the body for a given URL never
|
||||
// changes: it is safe to cache forever, and an update always produces a
|
||||
// new URL.
|
||||
Response.Headers[HeaderNames.CacheControl] = "public, max-age=31536000, immutable";
|
||||
Response.Headers[HeaderNames.ETag] = etag;
|
||||
|
||||
if (Request.Headers[HeaderNames.IfNoneMatch].ToString().Contains(_bundle.Hash, StringComparison.Ordinal))
|
||||
{
|
||||
return StatusCode(304);
|
||||
}
|
||||
|
||||
return File(_bundle.Bytes, "application/javascript; charset=utf-8");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reports which bundle the server is serving and whether injection is on.
|
||||
/// Handy for verifying a deployment without opening DevTools on the Quest.
|
||||
/// </summary>
|
||||
/// <returns>A small JSON status object.</returns>
|
||||
[HttpGet("status")]
|
||||
[AllowAnonymous]
|
||||
public ActionResult<object> GetStatus()
|
||||
{
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
|
||||
return new
|
||||
{
|
||||
plugin = "Jellyfin.Plugin.JFVR",
|
||||
version = typeof(Plugin).Assembly.GetName().Version?.ToString() ?? "unknown",
|
||||
bundleHash = _bundle.Hash,
|
||||
bundleBytes = _bundle.Bytes.Length,
|
||||
enabled = config?.Enabled ?? false,
|
||||
scriptUrlOverride = config?.ScriptUrlOverride ?? string.Empty,
|
||||
injectedTag = string.IsNullOrWhiteSpace(config?.ScriptUrlOverride)
|
||||
? "../JFVR/jfvr.js?v=" + _bundle.Hash
|
||||
: config!.ScriptUrlOverride.Trim()
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<RootNamespace>Jellyfin.Plugin.JFVR</RootNamespace>
|
||||
<AssemblyName>Jellyfin.Plugin.JFVR</AssemblyName>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<GenerateDocumentationFile>false</GenerateDocumentationFile>
|
||||
<EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
|
||||
|
||||
<!-- Overridden by pack.py from plugin/build.yaml (dotnet build -p:Version=...). -->
|
||||
<Version>0.1.0.0</Version>
|
||||
|
||||
<!--
|
||||
Single source of truth for the injected bundle. dist/jfvr.js is produced by
|
||||
build.py from src/*.js; embedding it straight from there (instead of copying
|
||||
it into the project) means the DLL can never ship a stale bundle.
|
||||
-->
|
||||
<JfvrBundlePath>$(MSBuildThisFileDirectory)..\..\dist\jfvr.js</JfvrBundlePath>
|
||||
</PropertyGroup>
|
||||
|
||||
<Target Name="JfvrCheckBundle" BeforeTargets="PrepareForBuild">
|
||||
<Error
|
||||
Condition="!Exists('$(JfvrBundlePath)')"
|
||||
Text="dist/jfvr.js is missing. Run 'python3 build.py' first (pack.py does this for you)." />
|
||||
</Target>
|
||||
|
||||
<ItemGroup>
|
||||
<!--
|
||||
ExcludeAssets=runtime is required: Jellyfin provides these at runtime, and
|
||||
copying them into the plugin folder prevents the plugin from registering.
|
||||
-->
|
||||
<PackageReference Include="Jellyfin.Controller" Version="12.1.0">
|
||||
<ExcludeAssets>runtime</ExcludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Jellyfin.Model" Version="12.1.0">
|
||||
<ExcludeAssets>runtime</ExcludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="$(JfvrBundlePath)" LogicalName="Jellyfin.Plugin.JFVR.Web.jfvr.js" />
|
||||
<EmbeddedResource Include="Configuration\configPage.html" LogicalName="Jellyfin.Plugin.JFVR.Configuration.configPage.html" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Jellyfin.Plugin.JFVR.Services;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Common.Plugins;
|
||||
using MediaBrowser.Model.Plugins;
|
||||
using MediaBrowser.Model.Serialization;
|
||||
|
||||
namespace Jellyfin.Plugin.JFVR;
|
||||
|
||||
/// <summary>
|
||||
/// The JFVR plugin. Owns the identity (GUID + version) that the Jellyfin plugin
|
||||
/// catalog matches on, and serves the embedded web bundle.
|
||||
/// </summary>
|
||||
public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Plugin"/> class.
|
||||
/// </summary>
|
||||
/// <param name="applicationPaths">Jellyfin application paths.</param>
|
||||
/// <param name="xmlSerializer">XML serializer for the configuration.</param>
|
||||
/// <param name="bundle">Embedded web bundle.</param>
|
||||
public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer, BundleProvider bundle)
|
||||
: base(applicationPaths, xmlSerializer)
|
||||
{
|
||||
Instance = this;
|
||||
Bundle = bundle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current plugin instance. Set by the DI container at startup;
|
||||
/// the injection middleware reads the configuration through it.
|
||||
/// </summary>
|
||||
public static Plugin? Instance { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the embedded web bundle.
|
||||
/// </summary>
|
||||
public BundleProvider Bundle { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Name => "Jellyfin Web VR";
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Guid Id => Guid.Parse("7d33ef59-f9b0-412b-93bd-5cd5353018de");
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Description =>
|
||||
"Injects a VR play button into Jellyfin Web so Meta Quest 3 can play VR180/VR360 in WebXR.";
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<PluginPageInfo> GetPages()
|
||||
{
|
||||
return new[]
|
||||
{
|
||||
new PluginPageInfo
|
||||
{
|
||||
Name = "JFVR",
|
||||
DisplayName = "Jellyfin Web VR",
|
||||
EmbeddedResourcePath = GetType().Namespace + ".Configuration.configPage.html",
|
||||
EnableInMainMenu = true,
|
||||
MenuIcon = "view_in_ar"
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using MediaBrowser.Model.Plugins;
|
||||
|
||||
namespace Jellyfin.Plugin.JFVR;
|
||||
|
||||
/// <summary>
|
||||
/// JFVR plugin configuration. Persisted by Jellyfin as XML under
|
||||
/// <c><config>/plugins/configurations/Jellyfin.Plugin.JFVR.xml</c>, so it
|
||||
/// survives container recreation and Jellyfin upgrades.
|
||||
/// </summary>
|
||||
public class PluginConfiguration : BasePluginConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the plugin rewrites the served
|
||||
/// <c>index.html</c>. When disabled the response is passed through untouched
|
||||
/// (the legacy injection, if any, is left alone too) but the bundle endpoint
|
||||
/// stays reachable.
|
||||
/// </summary>
|
||||
public bool Enabled { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets an optional URL used instead of the embedded bundle. Leave
|
||||
/// empty to serve <c>../JFVR/jfvr.js?v=<hash></c> from this plugin.
|
||||
/// </summary>
|
||||
public string ScriptUrlOverride { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Jellyfin.Plugin.JFVR.Services;
|
||||
using MediaBrowser.Controller;
|
||||
using MediaBrowser.Controller.Plugins;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Jellyfin.Plugin.JFVR;
|
||||
|
||||
/// <summary>
|
||||
/// Registers the plugin's services with the Jellyfin host container.
|
||||
/// </summary>
|
||||
public class PluginServiceRegistrator : IPluginServiceRegistrator
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void RegisterServices(IServiceCollection serviceCollection, IServerApplicationHost applicationHost)
|
||||
{
|
||||
serviceCollection.AddSingleton<BundleProvider>();
|
||||
|
||||
// The request-time injection path: rewrites jellyfin-web's index.html on
|
||||
// the way out. Nothing is written to disk.
|
||||
serviceCollection.AddSingleton<IStartupFilter, ScriptInjectionStartupFilter>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Jellyfin.Plugin.JFVR.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Loads the JFVR web bundle that is embedded in this assembly and exposes it
|
||||
/// together with a short content hash.
|
||||
///
|
||||
/// The hash is the cache-busting token in the injected
|
||||
/// <c><script src="../JFVR/jfvr.js?v=HASH"></c> tag, so a new plugin build
|
||||
/// always forces browsers to refetch the bundle. It is also the same
|
||||
/// <c>md5[:6]</c> version string the in-page HUD reports, which makes "which JS
|
||||
/// is the Quest actually running?" answerable from both sides.
|
||||
/// </summary>
|
||||
public sealed class BundleProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Logical name of the embedded resource, set explicitly in the .csproj so it
|
||||
/// does not depend on the project's folder layout.
|
||||
/// </summary>
|
||||
internal const string ResourceName = "Jellyfin.Plugin.JFVR.Web.jfvr.js";
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BundleProvider"/> class.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// Thrown when the bundle was not embedded, which means the build is broken.
|
||||
/// </exception>
|
||||
public BundleProvider()
|
||||
{
|
||||
using var stream = typeof(BundleProvider).Assembly.GetManifestResourceStream(ResourceName);
|
||||
if (stream is null)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Embedded resource '{ResourceName}' is missing from {typeof(BundleProvider).Assembly.FullName}.");
|
||||
}
|
||||
|
||||
using var buffer = new MemoryStream();
|
||||
stream.CopyTo(buffer);
|
||||
Bytes = buffer.ToArray();
|
||||
Hash = Convert.ToHexString(MD5.HashData(Bytes)).ToLowerInvariant()[..8];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the raw bundle bytes.
|
||||
/// </summary>
|
||||
public byte[] Bytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the short (8 hex chars) MD5 of <see cref="Bytes"/>.
|
||||
/// </summary>
|
||||
public string Hash { get; }
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Jellyfin.Plugin.JFVR.Services;
|
||||
|
||||
/// <summary>
|
||||
/// The pure text transformation behind <see cref="ScriptInjectionStartupFilter"/>:
|
||||
/// given jellyfin-web's <c>index.html</c>, return the version with the JFVR
|
||||
/// script tag in it.
|
||||
///
|
||||
/// Deliberately free of ASP.NET and Jellyfin types so it can be exercised
|
||||
/// directly against captured HTML.
|
||||
/// </summary>
|
||||
public static class IndexHtmlRewriter
|
||||
{
|
||||
/// <summary>
|
||||
/// Marker that identifies an index.html this plugin already rewrote.
|
||||
/// </summary>
|
||||
public const string StartComment = "<!-- BEGIN JFVR (Jellyfin Web VR) -->";
|
||||
|
||||
/// <summary>
|
||||
/// Closing half of the injected block.
|
||||
/// </summary>
|
||||
public const string EndComment = "<!-- END JFVR (Jellyfin Web VR) -->";
|
||||
|
||||
/// <summary>
|
||||
/// Matches the tag written by the old <c>deploy.py</c>
|
||||
/// (<c><script plugin="JellyfinVR" ... src="jfvr-mvp.js?v=..."></script></c>).
|
||||
///
|
||||
/// The plugin's own tag deliberately uses <c>plugin="JFVR"</c>, so the two can
|
||||
/// never be confused: installing the plugin takes over from the legacy
|
||||
/// injection automatically instead of loading the bundle twice.
|
||||
/// </summary>
|
||||
private static readonly Regex LegacyTag = new(
|
||||
"<script[^>]*plugin=\"JellyfinVR\"[^>]*>\\s*</script>\\s*",
|
||||
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether <paramref name="html"/> already carries our block.
|
||||
/// </summary>
|
||||
/// <param name="html">The served HTML.</param>
|
||||
/// <returns>True when the block is already present.</returns>
|
||||
public static bool IsInjected(string html)
|
||||
{
|
||||
return html.Contains(StartComment, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the block that gets inserted immediately before <c></body></c>.
|
||||
/// </summary>
|
||||
/// <param name="scriptUrl">URL of the bundle to load.</param>
|
||||
/// <returns>The HTML block, newline terminated.</returns>
|
||||
public static string BuildBlock(string scriptUrl)
|
||||
{
|
||||
return StartComment
|
||||
+ "\n<script plugin=\"JFVR\" data-jfvr=\"1\" src=\"" + scriptUrl + "\" defer></script>\n"
|
||||
+ EndComment
|
||||
+ "\n";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Injects the JFVR block into <paramref name="html"/>.
|
||||
///
|
||||
/// Idempotent: an already-injected document is returned unchanged. A legacy
|
||||
/// <c>deploy.py</c> tag is removed on the way so the bundle is never loaded twice.
|
||||
/// If there is no <c></body></c> to anchor to, the (possibly
|
||||
/// legacy-cleaned) input is returned unchanged.
|
||||
/// </summary>
|
||||
/// <param name="html">The served HTML.</param>
|
||||
/// <param name="scriptUrl">URL of the bundle to load.</param>
|
||||
/// <param name="removedLegacy">True when a legacy tag was stripped.</param>
|
||||
/// <returns>The HTML to serve.</returns>
|
||||
public static string Rewrite(string html, string scriptUrl, out bool removedLegacy)
|
||||
{
|
||||
removedLegacy = false;
|
||||
|
||||
if (IsInjected(html))
|
||||
{
|
||||
return html;
|
||||
}
|
||||
|
||||
var cleaned = html;
|
||||
if (LegacyTag.IsMatch(html))
|
||||
{
|
||||
cleaned = LegacyTag.Replace(html, string.Empty);
|
||||
removedLegacy = true;
|
||||
}
|
||||
|
||||
var bodyClose = cleaned.LastIndexOf("</body>", StringComparison.OrdinalIgnoreCase);
|
||||
if (bodyClose < 0)
|
||||
{
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
return cleaned[..bodyClose] + BuildBlock(scriptUrl) + "\n" + cleaned[bodyClose..];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.JFVR.Services;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace Jellyfin.Plugin.JFVR.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Injects the JFVR <c><script></c> tag into jellyfin-web's index.html at
|
||||
/// request time, through ASP.NET Core middleware registered via
|
||||
/// <see cref="IStartupFilter"/>.
|
||||
///
|
||||
/// Why request-time rewriting instead of the old deploy.py approach (which
|
||||
/// <c>sed</c>-patched <c>/jellyfin/jellyfin-web/index.html</c> inside the
|
||||
/// container):
|
||||
/// - nothing on disk is modified, so a <c>jellyfin/jellyfin:latest</c> upgrade
|
||||
/// cannot silently drop the injection;
|
||||
/// - it is idempotent and additive;
|
||||
/// - on any error the original response is served unchanged -- the middleware
|
||||
/// never throws into the pipeline;
|
||||
/// - it can be switched off from the dashboard.
|
||||
///
|
||||
/// The technique (strip Accept-Encoding so the static handler returns plain
|
||||
/// text, buffer the body, rewrite, fix up the validators) is the same one the
|
||||
/// community JavaScript Injector plugin uses; that plugin's own history is why
|
||||
/// the details below look paranoid.
|
||||
/// </summary>
|
||||
public sealed class ScriptInjectionStartupFilter : IStartupFilter
|
||||
{
|
||||
private readonly BundleProvider _bundle;
|
||||
private readonly ILogger<ScriptInjectionStartupFilter> _logger;
|
||||
private int _loggedOnce;
|
||||
private int _legacyLoggedOnce;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ScriptInjectionStartupFilter"/> class.
|
||||
/// </summary>
|
||||
/// <param name="bundle">Embedded web bundle.</param>
|
||||
/// <param name="logger">Logger.</param>
|
||||
public ScriptInjectionStartupFilter(BundleProvider bundle, ILogger<ScriptInjectionStartupFilter> logger)
|
||||
{
|
||||
_bundle = bundle;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
|
||||
{
|
||||
return app =>
|
||||
{
|
||||
// Registered before next(app) so this runs outermost. That ordering
|
||||
// matters: stripping Accept-Encoding here means the response
|
||||
// compression middleware downstream never compresses the body, so
|
||||
// what we buffer is always complete, plain-text HTML.
|
||||
app.Use(InvokeAsync);
|
||||
next(app);
|
||||
};
|
||||
}
|
||||
|
||||
private async Task InvokeAsync(HttpContext context, Func<Task> nextMw)
|
||||
{
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
if (config is null || !config.Enabled || !IsIndexRequest(context.Request))
|
||||
{
|
||||
await nextMw().ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Normalize the request so the static handler returns a complete 200:
|
||||
// - no Accept-Encoding -> no compression, plain text body;
|
||||
// - no Range/If-Range -> no 206 partial response, which would otherwise
|
||||
// pass through un-injected with a total length that no longer matches.
|
||||
var hadAcceptEncoding = context.Request.Headers.TryGetValue(HeaderNames.AcceptEncoding, out StringValues acceptEncoding);
|
||||
context.Request.Headers.Remove(HeaderNames.AcceptEncoding);
|
||||
context.Request.Headers.Remove(HeaderNames.Range);
|
||||
context.Request.Headers.Remove(HeaderNames.IfRange);
|
||||
|
||||
var originalBody = context.Response.Body;
|
||||
using var buffer = new MemoryStream();
|
||||
context.Response.Body = buffer;
|
||||
try
|
||||
{
|
||||
await nextMw().ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Not ours to swallow. Discard the partially buffered body (it never
|
||||
// reached the real stream, so the response has not started) and let
|
||||
// the host render a clean error page.
|
||||
context.Response.Body = originalBody;
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (hadAcceptEncoding)
|
||||
{
|
||||
context.Request.Headers[HeaderNames.AcceptEncoding] = acceptEncoding;
|
||||
}
|
||||
}
|
||||
|
||||
context.Response.Body = originalBody;
|
||||
buffer.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
var isHtml = context.Response.StatusCode == 200
|
||||
&& context.Response.ContentType is not null
|
||||
&& context.Response.ContentType.Contains("text/html", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
if (!isHtml)
|
||||
{
|
||||
// 304, redirects, non-HTML: pass straight through, byte for byte.
|
||||
await buffer.CopyToAsync(originalBody).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
string html;
|
||||
using (var reader = new StreamReader(buffer, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, leaveOpen: true))
|
||||
{
|
||||
html = await reader.ReadToEndAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
html = Rewrite(html, config);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Never break index.html: serve whatever we have.
|
||||
_logger.LogWarning(ex, "JFVR: index.html rewrite failed, serving the original response.");
|
||||
}
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(html);
|
||||
context.Response.ContentType = "text/html; charset=utf-8";
|
||||
context.Response.ContentLength = bytes.Length;
|
||||
|
||||
// The body changed, so validators computed for the original bytes are
|
||||
// invalid, and range requests on the rewritten document are not supported
|
||||
// (they are already stripped on the way in).
|
||||
context.Response.Headers.Remove(HeaderNames.ETag);
|
||||
context.Response.Headers.Remove(HeaderNames.LastModified);
|
||||
context.Response.Headers.Remove(HeaderNames.AcceptRanges);
|
||||
context.Response.Headers.Remove(HeaderNames.ContentEncoding);
|
||||
|
||||
await originalBody.WriteAsync(bytes).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private string Rewrite(string html, PluginConfiguration config)
|
||||
{
|
||||
var scriptUrl = string.IsNullOrWhiteSpace(config.ScriptUrlOverride)
|
||||
? "../JFVR/jfvr.js?v=" + _bundle.Hash
|
||||
: config.ScriptUrlOverride.Trim();
|
||||
|
||||
var rewritten = IndexHtmlRewriter.Rewrite(html, scriptUrl, out var removedLegacy);
|
||||
|
||||
if (removedLegacy && Interlocked.Exchange(ref _legacyLoggedOnce, 1) == 0)
|
||||
{
|
||||
// Logged once, not per request: the legacy tag lives in the on-disk
|
||||
// index.html, so it is stripped from every single response until the
|
||||
// file itself is cleaned up (deploy.py rollback) or deploy.py is run
|
||||
// again. Per-request logging would just fill the log with the same line.
|
||||
_logger.LogInformation(
|
||||
"JFVR: stripping the legacy deploy.py <script> tag from index.html. "
|
||||
+ "It is still present on disk; run 'python3 deploy.py rollback' to clean it up.");
|
||||
}
|
||||
|
||||
if (IndexHtmlRewriter.IsInjected(html) || rewritten == html)
|
||||
{
|
||||
// Already ours (the common case on every reload), or nothing to anchor to.
|
||||
return rewritten;
|
||||
}
|
||||
|
||||
if (Interlocked.Exchange(ref _loggedOnce, 1) == 0)
|
||||
{
|
||||
_logger.LogInformation("JFVR: injecting {Block}", IndexHtmlRewriter.BuildBlock(scriptUrl).Trim());
|
||||
}
|
||||
|
||||
return rewritten;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Matches the web app shell however it is requested: bare <c>/web</c>,
|
||||
/// <c>/web/</c> (SPA serve) and an explicit <c>/web/index.html</c>. The
|
||||
/// EndsWith form stays correct when Jellyfin is hosted under a base-url
|
||||
/// prefix (e.g. <c>/jellyfin/web/</c>).
|
||||
/// </summary>
|
||||
private static bool IsIndexRequest(HttpRequest request)
|
||||
{
|
||||
if (!HttpMethods.IsGet(request.Method))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var path = request.Path.Value;
|
||||
if (string.IsNullOrEmpty(path) || path == "/")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return path.EndsWith("/web/index.html", StringComparison.OrdinalIgnoreCase)
|
||||
|| path.EndsWith("/web/", StringComparison.OrdinalIgnoreCase)
|
||||
|| path.Equals("/web", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
---
|
||||
# Metadata for the Jellyfin plugin catalog entry. pack.py reads this file to
|
||||
# build manifest.json, and passes `version` to dotnet as -p:Version.
|
||||
#
|
||||
# targetAbi is deliberately 12.0.0.0 rather than the exact server version:
|
||||
# Jellyfin keeps a catalog version only when `targetAbi <= serverVersion`
|
||||
# (InstallationManager.GetAvailablePackages), so one package covers all of 12.x.
|
||||
name: "Jellyfin Web VR"
|
||||
guid: "7d33ef59-f9b0-412b-93bd-5cd5353018de"
|
||||
version: "0.5.2.0"
|
||||
targetAbi: "12.0.0.0"
|
||||
framework: "net10.0"
|
||||
overview: "VR180/VR360 playback inside Jellyfin Web for Meta Quest 3."
|
||||
description: >
|
||||
Injects a "VR 播放" button into the Jellyfin Web video player page and drives
|
||||
an immersive WebXR session on Meta Quest 3, preferring
|
||||
XRMediaBinding.createEquirectLayer and falling back to XRWebGLBinding.
|
||||
Ships equirectangular 180/360 plus SBS/OU layouts, in-headset subtitles,
|
||||
a wrist controller, and hand tracking.
|
||||
category: "General"
|
||||
owner: "cat-shark"
|
||||
artifacts:
|
||||
- "Jellyfin.Plugin.JFVR.dll"
|
||||
changelog: >
|
||||
First plugin build. The web bundle is embedded in the DLL and served from
|
||||
/JFVR/jfvr.js; index.html is rewritten at request time, so nothing is written
|
||||
to the jellyfin-web folder on disk.
|
||||
+290
-4
@@ -307,9 +307,132 @@
|
||||
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: [],
|
||||
codecKept: [],
|
||||
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.
|
||||
*
|
||||
* A tier is blocked when:
|
||||
*
|
||||
* 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 "decoder exists, 8K does not work"
|
||||
* case
|
||||
*
|
||||
* AND, for a Main 10 tier, only when the SAME LEVEL IS ALSO UNUSABLE IN MAIN
|
||||
* (8-bit). That second condition is not cosmetic; it is forced by a real
|
||||
* limitation in Jellyfin's device profile.
|
||||
*
|
||||
* browserDeviceProfile.js derives the level ceiling and the profile list from
|
||||
* ONE chain of Main 10 probes -- the only Main (8-bit) probes are L4.0 and
|
||||
* L4.1. So the reachable states are only:
|
||||
*
|
||||
* (120, main) (123, main) (123, main|main 10) (153, main|main 10)
|
||||
* (183, main|main 10) (186, main|main 10)
|
||||
*
|
||||
* "Main up to 8K, Main 10 only up to 4K" is NOT expressible. So blocking
|
||||
* 2:183 on a device that decodes 8-bit 8K but not 10-bit 8K does not just
|
||||
* spare the 10-bit file -- it drags every 8-bit 8K file down to level 153 and
|
||||
* transcodes them too. On a server that cannot transcode 8K in real time
|
||||
* (measured: 8192x4096 -> 1280x640), that trades one broken file for sixty.
|
||||
*
|
||||
* Verified on the machine this was written for: NVIDIA VAAPI decodes
|
||||
* 8192x4096 Main 8-bit fine and fails to create a decoder for 8192x4096
|
||||
* Main 10 (nvidia-vaapi-driver passes the surface bit depth straight to
|
||||
* cuvidCreateDecoder). Its whole library is 8-bit 8K, so lowering the ceiling
|
||||
* would have broken all of it.
|
||||
*
|
||||
* The cost of this rule: a device that decodes 8-bit at a level but not
|
||||
* 10-bit will still claim 10-bit at that level and can black-screen on such a
|
||||
* file. 40-watchdog.js covers exactly that, which is why it stays.
|
||||
*
|
||||
* 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 and bitrate it exists for. Main
|
||||
// (8-bit) is probed at the same levels as Main 10 because it is the control
|
||||
// that decides whether a level is really out of reach.
|
||||
//
|
||||
// NOTE on the codec strings: they MUST carry a constraint byte. Chrome's
|
||||
// MediaCapabilities.decodingInfo does not recognise the bare 'hvc1.1.L120'
|
||||
// spelling and answers supported:false for it even on a machine that decodes
|
||||
// HEVC perfectly, while 'hvc1.1.0.L120' is answered correctly. That one
|
||||
// character used to matter far more than it looks: browserDeviceProfile.js
|
||||
// gates the WHOLE hevc entry on supportsHevc(), which is exactly
|
||||
//
|
||||
// canPlayType('...codecs="hvc1.1.L120"') ||
|
||||
// canPlayType('...codecs="hev1.1.L120"') ||
|
||||
// canPlayType('...codecs="hvc1.1.0.L120"') || ...
|
||||
//
|
||||
// so a spurious block of tier 1:120 (all spellings map to it) removes hevc
|
||||
// from every DirectPlayProfile and transcode every HEVC file in the library.
|
||||
// 1:120 is NOT part of Jellyfin's level chain; it is only that gate.
|
||||
var CODEC_PROBES = [
|
||||
{ key: '1:120', name: 'Main L4.0', w: 1920, h: 1080, fps: 30, mbps: 8, codec: 'hvc1.1.0.L120' },
|
||||
{ key: '1:123', name: 'Main L4.1', w: 1920, h: 1080, fps: 60, mbps: 8, codec: 'hvc1.1.4.L123' },
|
||||
{ key: '1:153', name: 'Main L5.1 (4K)', w: 3840, h: 2160, fps: 60, mbps: 20, codec: 'hvc1.1.5.L153.B0' },
|
||||
{ key: '1:183', name: 'Main L6.1 (8K)', w: 8192, h: 4096, fps: 60, mbps: 30, codec: 'hvc1.1.6.L183.B0' },
|
||||
{ key: '1:186', name: 'Main L6.2 (8K60)', w: 8192, h: 4096, fps: 60, mbps: 30, codec: 'hvc1.1.6.L186.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' }
|
||||
];
|
||||
|
||||
/**
|
||||
* '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 +446,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 +466,162 @@
|
||||
}
|
||||
})();
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* MediaCapabilities probe + canPlayType override
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
/** Tiers the user has pinned as "keep claiming this", '<key>,<key>' 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;
|
||||
}
|
||||
|
||||
/** True when this tier is not usable on its own terms. */
|
||||
function tierUnusable(key) {
|
||||
var v = NS.caps.codec[key];
|
||||
return !!v && !v.error && (v.supported === false || v.smooth === false);
|
||||
}
|
||||
|
||||
/**
|
||||
* The decision the canPlayType hook uses.
|
||||
*
|
||||
* A Main 10 tier is only blocked when the SAME LEVEL is also unusable in Main
|
||||
* (8-bit). See the long comment above CODEC_PROBES: Jellyfin's profile cannot
|
||||
* express "Main up to 8K, Main 10 up to 4K", so blocking 2:183 on a device
|
||||
* that handles 8-bit 8K would drag all of its 8-bit 8K files into transcoding.
|
||||
*/
|
||||
function shouldBlock(key, keep) {
|
||||
var v = NS.caps.codec[key];
|
||||
if (!v || v.error) return null;
|
||||
if (keep.indexOf(key) >= 0) return null;
|
||||
var own = blockReason(v, keep);
|
||||
if (!own) return null;
|
||||
var parts = key.split(':');
|
||||
|
||||
// Base tier. 1:120 is not in Jellyfin's level chain -- it is only the
|
||||
// coarse "does this client have HEVC at all" gate (supportsHevc() in
|
||||
// browserDeviceProfile.js, which probes exactly this level). Blocking it
|
||||
// drops hevc from every DirectPlayProfile, i.e. transcodes the entire
|
||||
// HEVC library. Never act on it unless the device is unusable at EVERY
|
||||
// Main tier, so one flaky/early probe cannot do that.
|
||||
if (key === '1:120') {
|
||||
var anyMainUsable = CODEC_PROBES.some(function (p) {
|
||||
if (p.key === '1:120' || p.key.charAt(0) !== '1') return false;
|
||||
var m = NS.caps.codec[p.key];
|
||||
return !!m && !m.error && m.supported !== false && m.smooth !== false;
|
||||
});
|
||||
if (anyMainUsable) return null;
|
||||
}
|
||||
|
||||
if (parts[0] === '2') {
|
||||
// Control: the same level in Main (8-bit).
|
||||
if (!tierUnusable('1:' + parts[1])) return null;
|
||||
}
|
||||
return own;
|
||||
}
|
||||
|
||||
function applyVerdicts() {
|
||||
var keep = NS.caps.__keep || [];
|
||||
NS.caps.codecKept = [];
|
||||
NS.caps.codecBlocked = [];
|
||||
CODEC_PROBES.forEach(function (p) {
|
||||
var v = NS.caps.codec[p.key];
|
||||
if (!v) return;
|
||||
var why = shouldBlock(p.key, 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() {
|
||||
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);
|
||||
// Only a definite "no" is acted on. Anything else is the browser's call.
|
||||
if (key && shouldBlock(key, NS.caps.__keep || [])) 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; }
|
||||
|
||||
// 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.
|
||||
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: p.mbps * 1000000, framerate: p.fps
|
||||
}
|
||||
};
|
||||
mc.decodingInfo(cfg).then(function (info) {
|
||||
NS.caps.codec[p.key] = {
|
||||
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] = { 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.caps.codecKept.length ? '; pinned = ' + NS.caps.codecKept.join(', ') : ''));
|
||||
}
|
||||
});
|
||||
});
|
||||
})();
|
||||
|
||||
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 = shouldBlock(p.key, keep);
|
||||
var own = blockReason(v, keep);
|
||||
var note = why ? ' <-- BLOCKED (' + why + ')' :
|
||||
(own ? ' (unusable, but kept: 8-bit at this level works)' :
|
||||
(keep.indexOf(p.key) >= 0 ? ' <-- pinned' : ''));
|
||||
return ' ' + p.name.padEnd(20) + ' supported=' + v.supported +
|
||||
' smooth=' + v.smooth + ' powerEfficient=' + v.powerEfficient +
|
||||
' [' + v.workload + ']' + note;
|
||||
}).join('\n');
|
||||
|
||||
return [
|
||||
'origin : ' + location.origin,
|
||||
'secureContext : ' + c.secureContext,
|
||||
@@ -349,7 +631,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' ? ' (blocked: ' + (c.codecBlocked.join(', ') || 'nothing yet') + ')' : ''),
|
||||
'HEVC real (decodingInfo):',
|
||||
real
|
||||
].join('\n');
|
||||
};
|
||||
})();
|
||||
|
||||
@@ -0,0 +1,357 @@
|
||||
/*!
|
||||
* JFVR watchdog — "this video never started" detection.
|
||||
*
|
||||
* WHY THIS EXISTS
|
||||
* ---------------
|
||||
* Jellyfin Web builds the whole player UI (including the quality menu) only
|
||||
* after the 'playbackstart' event, which the media element fires once it has
|
||||
* actually started. The load order is:
|
||||
*
|
||||
* HtmlVideoPlayer.play() htmlVideoPlayer/plugin.js:502
|
||||
* createMediaElement() :1802 -> loading.show() spinner
|
||||
* -> <div.videoPlayerContainer><video>
|
||||
* -> inserted into document.body
|
||||
* (no src yet!)
|
||||
* setCurrentSrc() :593 -> elem.src = url, only now
|
||||
* -> loadedmetadata -> canplay -> play
|
||||
* -> 'playbackstart'
|
||||
* -> OSD controller (apps/legacy/controllers/playback/video/index.js:591)
|
||||
*
|
||||
* So when the browser cannot use the stream, the OSD never appears at all: the
|
||||
* page is a spinner over an empty container with a <video> whose videoWidth is
|
||||
* still 0. There is no quality menu to click, because it was never created.
|
||||
*
|
||||
* That is the case this module covers. It watches for exactly that state and
|
||||
* offers the only thing that can help: tell Jellyfin to transcode.
|
||||
*
|
||||
* WHY localStorage AND NOT THE PLAYBACK MANAGER
|
||||
* --------------------------------------------
|
||||
* The bitrate is normally set through playbackManager.setMaxStreamingBitrate(),
|
||||
* which does a live changeStream(). That manager is an ES module export and is
|
||||
* NOT on window (verified: window.playbackManager is undefined; window.Emby only
|
||||
* has Page). So the watchdog writes the same keys appSettings would have written
|
||||
* (src/scripts/settings/appSettings.js) and reloads, which makes Jellyfin request
|
||||
* PlaybackInfo again with the new cap and pick Transcode.
|
||||
*
|
||||
* NOTE the key name really does say "bitrate" twice — that is upstream's typo,
|
||||
* reproduced here deliberately.
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var NS = window.__JFVR;
|
||||
if (!NS) { console.error('[JFVR] core missing'); return; }
|
||||
if (NS.__watchdog) return;
|
||||
NS.__watchdog = true;
|
||||
|
||||
var S = NS.state;
|
||||
var log = NS.log;
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* tunables
|
||||
* ------------------------------------------------------------------ */
|
||||
var POLL_MS = 500;
|
||||
|
||||
// Direct play on a LAN should have metadata almost immediately; the file is
|
||||
// 11 GB but its moov atom is at the front, so there is nothing to wait for.
|
||||
// A transcode is different: the server has to decode the first GOP and produce
|
||||
// segments before the browser sees any metadata, and this NAS is doing 8K.
|
||||
var DEAD_MS = 15000;
|
||||
var DEAD_TRANSCODE_MS = 45000;
|
||||
|
||||
// After the user answers (either way) stay quiet, so a slow-but-working
|
||||
// playback does not get nagged at.
|
||||
var QUIET_MS = 120000;
|
||||
|
||||
var QUIET_KEY = 'jfvr-watchdog-quiet-until';
|
||||
|
||||
// Values from Jellyfin's own menu (src/components/qualityOptions.js).
|
||||
var CHOICES = [
|
||||
{ label: '20 Mbps', bitrate: 20000000, note: '≈4K,最接近原始画质' },
|
||||
{ label: '8 Mbps', bitrate: 8000000, note: '1080p 档,稳' },
|
||||
{ label: '3 Mbps', bitrate: 3000000, note: '720p 档,最省算力' }
|
||||
];
|
||||
|
||||
var W = {
|
||||
seenAt: 0, // when the current stalled <video> first showed up
|
||||
shown: false,
|
||||
quietUntil: 0,
|
||||
lastReason: '',
|
||||
applied: null,
|
||||
codec: null, // filled in best-effort from the Jellyfin API
|
||||
panel: null
|
||||
};
|
||||
NS.Watchdog = W;
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* helpers
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
function now() { return Date.now(); }
|
||||
|
||||
/**
|
||||
* The element Jellyfin creates in createMediaElement(). It exists from the
|
||||
* very start of playback — including the failure case, which is the whole
|
||||
* point: findVideo() in 90-main.js requires videoWidth and would return null.
|
||||
*/
|
||||
function playerVideo() {
|
||||
var c = document.querySelector('.videoPlayerContainer');
|
||||
if (!c) return null;
|
||||
return c.querySelector('video') || null;
|
||||
}
|
||||
|
||||
function isTranscode(v) {
|
||||
var src = v.currentSrc || v.src || '';
|
||||
return src.indexOf('/hls') >= 0 || src.indexOf('.m3u8') >= 0;
|
||||
}
|
||||
|
||||
/** Metadata parsed AND a frame available. False means "no picture". */
|
||||
function hasPicture(v) {
|
||||
return v.videoWidth > 0 && v.readyState >= 2;
|
||||
}
|
||||
|
||||
function readQuiet() {
|
||||
try {
|
||||
var raw = window.sessionStorage && sessionStorage.getItem(QUIET_KEY);
|
||||
return raw ? parseInt(raw, 10) || 0 : 0;
|
||||
} catch (e) { return 0; }
|
||||
}
|
||||
|
||||
function setQuiet(ms) {
|
||||
W.quietUntil = now() + ms;
|
||||
try { sessionStorage.setItem(QUIET_KEY, String(W.quietUntil)); } catch (e) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the same keys Jellyfin's own quality menu writes, for BOTH in-network
|
||||
* and out-of-network, because the client is the only side that knows which one
|
||||
* applies and we cannot ask it (appSettings is not on window).
|
||||
*/
|
||||
function applyBitrate(bitrate) {
|
||||
var media = 'Video';
|
||||
['true', 'false'].forEach(function (inNet) {
|
||||
try {
|
||||
localStorage.setItem('enableautobitratebitrate-' + media + '-' + inNet, 'false');
|
||||
localStorage.setItem('maxbitrate-' + media + '-' + inNet, String(bitrate));
|
||||
} catch (e) {
|
||||
log('watchdog: could not persist bitrate', e && e.message);
|
||||
}
|
||||
});
|
||||
W.applied = bitrate;
|
||||
log('watchdog: capped bitrate at', bitrate, 'and reloading');
|
||||
setQuiet(QUIET_MS);
|
||||
window.location.reload();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* best-effort: name the actual format so the message is actionable
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
function itemIdFrom(v) {
|
||||
var s = (v && (v.currentSrc || v.src)) || '';
|
||||
var m = s.match(/\/Videos\/([0-9a-f]{32})\//i);
|
||||
if (m) return m[1];
|
||||
m = (window.location.hash || '').match(/[?&]id=([0-9a-f]{32})/i);
|
||||
return m ? m[1] : null;
|
||||
}
|
||||
|
||||
function describeCodec(v) {
|
||||
if (W.codec) return;
|
||||
var id = itemIdFrom(v);
|
||||
if (!id || !window.ApiClient) return;
|
||||
var url;
|
||||
try { url = window.ApiClient.getUrl('Items/' + id); } catch (e) { return; }
|
||||
window.ApiClient.ajax({ type: 'GET', url: url, dataType: 'json' }).then(function (item) {
|
||||
var ms = (item && item.MediaSources && item.MediaSources[0] && item.MediaSources[0].MediaStreams) || [];
|
||||
for (var i = 0; i < ms.length; i++) {
|
||||
var st = ms[i];
|
||||
if (st.Type !== 'Video') continue;
|
||||
W.codec = [
|
||||
st.Width + '×' + st.Height,
|
||||
(st.Codec || '').toUpperCase(),
|
||||
st.Profile || '',
|
||||
st.BitDepth ? st.BitDepth + '-bit' : ''
|
||||
].filter(Boolean).join(' ');
|
||||
break;
|
||||
}
|
||||
if (W.shown) render(); // update the message in place
|
||||
}).catch(function () { /* the prompt works fine without this */ });
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* the prompt
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
var WD_CSS = [
|
||||
'.jfvr-wd{position:fixed;left:50%;top:50%;transform:translate(-50%,-50%);z-index:2147483002;width:420px;max-width:calc(100vw - 32px);',
|
||||
'background:rgba(18,18,22,.97);color:#eaeaea;border:1px solid rgba(255,255,255,.16);border-radius:14px;',
|
||||
'box-shadow:0 18px 60px rgba(0,0,0,.7);font-family:inherit;font-size:14px;padding:18px 18px 14px;backdrop-filter:blur(10px)}',
|
||||
'.jfvr-wd h3{margin:0 0 6px;font-size:16px;font-weight:600}',
|
||||
'.jfvr-wd p{margin:0 0 10px;font-size:13px;line-height:1.6;color:#c3c9d4}',
|
||||
'.jfvr-wd .jfvr-wd-fmt{color:#ffd479;font-weight:600}',
|
||||
'.jfvr-wd .jfvr-wd-sub{font-size:12px;color:#8b93a1;line-height:1.55;margin-bottom:12px}',
|
||||
'.jfvr-wd-btns{display:flex;flex-direction:column;gap:8px;margin-bottom:10px}',
|
||||
'.jfvr-wd-btns button{display:flex;justify-content:space-between;align-items:baseline;gap:10px;width:100%;padding:10px 12px;',
|
||||
'border:1px solid rgba(255,255,255,.16);border-radius:9px;background:rgba(255,255,255,.05);color:#eaeaea;',
|
||||
'font-size:14px;font-weight:600;cursor:pointer;font-family:inherit;text-align:left}',
|
||||
'.jfvr-wd-btns button:hover{background:rgba(78,161,255,.18);border-color:#4ea1ff}',
|
||||
'.jfvr-wd-btns button span.note{font-size:11px;font-weight:400;color:#8b93a1}',
|
||||
'.jfvr-wd-quiet{width:100%;padding:9px;border:0;border-radius:9px;background:transparent;color:#8b93a1;',
|
||||
'font-size:13px;cursor:pointer;font-family:inherit}',
|
||||
'.jfvr-wd-quiet:hover{color:#eaeaea}',
|
||||
'.jfvr-wd-diag{margin-top:10px;font-size:11px;color:#8b93a1}',
|
||||
'.jfvr-wd-diag summary{cursor:pointer;user-select:none}',
|
||||
'.jfvr-wd-diag pre{white-space:pre-wrap;word-break:break-all;margin:8px 0 0;font-size:10px;line-height:1.5;',
|
||||
'max-height:150px;overflow:auto;color:#9fb0c4;background:rgba(0,0,0,.35);padding:8px;border-radius:7px}'
|
||||
].join('');
|
||||
|
||||
function injectWdCss() {
|
||||
if (document.getElementById('jfvr-wd-css')) return;
|
||||
var s = NS.makeEl('style');
|
||||
s.id = 'jfvr-wd-css';
|
||||
s.textContent = WD_CSS;
|
||||
document.head.appendChild(s);
|
||||
}
|
||||
|
||||
function diagText(v) {
|
||||
var e = v.error;
|
||||
return [
|
||||
'readyState : ' + v.readyState + ' (0=HAVE_NOTHING, 1=HAVE_METADATA, 2=HAVE_CURRENT_DATA)',
|
||||
'networkState: ' + v.networkState,
|
||||
'videoWidth : ' + v.videoWidth + '×' + v.videoHeight,
|
||||
'paused : ' + v.paused,
|
||||
'currentTime : ' + v.currentTime,
|
||||
'error : ' + (e ? e.code + ' ' + (e.message || '') : 'none'),
|
||||
'src : ' + String(v.currentSrc || v.src || '').slice(0, 160),
|
||||
'格式 : ' + (W.codec || '(未知)')
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
function render() {
|
||||
if (!W.panel) return;
|
||||
var body = W.panel.querySelector('.jfvr-wd-body');
|
||||
var v = playerVideo();
|
||||
if (body && v) {
|
||||
var fmt = body.querySelector('.jfvr-wd-fmt');
|
||||
if (fmt) fmt.textContent = W.codec || '这个格式';
|
||||
var pre = body.querySelector('pre');
|
||||
if (pre) pre.textContent = diagText(v);
|
||||
}
|
||||
}
|
||||
|
||||
function show(v, reason) {
|
||||
if (W.shown) { render(); return; }
|
||||
W.shown = true;
|
||||
W.lastReason = reason;
|
||||
injectWdCss();
|
||||
|
||||
var p = NS.makeEl('div', 'jfvr-wd');
|
||||
p.id = 'jfvr-wd';
|
||||
p.appendChild(NS.makeEl('h3', null, '视频没能开始播放'));
|
||||
|
||||
var body = NS.makeEl('div', 'jfvr-wd-body');
|
||||
|
||||
var msg = NS.makeEl('p');
|
||||
var waited = Math.round(timeoutMs(v) / 1000);
|
||||
msg.innerHTML =
|
||||
(reason === 'error'
|
||||
? '播放器报错了,一个画面都没解出来。'
|
||||
: '等了 ' + waited + ' 秒都没拿到任何画面(<code>videoWidth = 0</code>),' +
|
||||
'所以播放器界面也没有被创建出来。') +
|
||||
'最常见的原因是<strong>这台设备解不动这个格式</strong>:' +
|
||||
'<span class="jfvr-wd-fmt">' + (W.codec || '该格式') + '</span>。<br>' +
|
||||
'让服务器转码成 H.264 再播就能看到画面,代价是画质下降(这台 NAS 的转码上限约 4K)。';
|
||||
body.appendChild(msg);
|
||||
|
||||
body.appendChild(NS.makeEl('div', 'jfvr-wd-sub',
|
||||
'选一档码率后会写入播放设置并重新加载页面。'));
|
||||
|
||||
var btns = NS.makeEl('div', 'jfvr-wd-btns');
|
||||
CHOICES.forEach(function (c) {
|
||||
var b = NS.makeEl('button');
|
||||
b.type = 'button';
|
||||
b.appendChild(NS.makeEl('span', null, c.label));
|
||||
b.appendChild(NS.makeEl('span', 'note', c.note));
|
||||
b.addEventListener('click', function (ev) {
|
||||
ev.stopPropagation();
|
||||
applyBitrate(c.bitrate);
|
||||
});
|
||||
btns.appendChild(b);
|
||||
});
|
||||
body.appendChild(btns);
|
||||
|
||||
var quiet = NS.makeEl('button', 'jfvr-wd-quiet', '继续等待,先别管我');
|
||||
quiet.type = 'button';
|
||||
quiet.addEventListener('click', function (ev) {
|
||||
ev.stopPropagation();
|
||||
setQuiet(QUIET_MS);
|
||||
hide();
|
||||
});
|
||||
body.appendChild(quiet);
|
||||
|
||||
var diag = NS.makeEl('details', 'jfvr-wd-diag');
|
||||
diag.appendChild(NS.makeEl('summary', null, '诊断信息'));
|
||||
diag.appendChild(NS.makeEl('pre', null, diagText(v)));
|
||||
body.appendChild(diag);
|
||||
|
||||
p.appendChild(body);
|
||||
document.body.appendChild(p);
|
||||
W.panel = p;
|
||||
|
||||
NS.recordError('watchdog', new Error('playback never started: ' + reason));
|
||||
log('watchdog: playback never started, reason =', reason, 'codec =', W.codec);
|
||||
}
|
||||
|
||||
function hide() {
|
||||
W.shown = false;
|
||||
if (W.panel) { W.panel.remove(); W.panel = null; }
|
||||
}
|
||||
|
||||
function reset() {
|
||||
W.seenAt = 0;
|
||||
W.codec = null;
|
||||
if (W.shown) hide();
|
||||
}
|
||||
|
||||
function timeoutMs(v) {
|
||||
return isTranscode(v) ? DEAD_TRANSCODE_MS : DEAD_MS;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* poll
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
function poll() {
|
||||
// Never interfere with an active VR session: the session owns the flow and
|
||||
// a modal in the middle of it would be a disaster.
|
||||
if (S.session) { if (W.shown) hide(); return; }
|
||||
|
||||
var v = playerVideo();
|
||||
if (!v) { reset(); return; }
|
||||
|
||||
// Playback is fine (or at least has a picture): stand down.
|
||||
if (hasPicture(v)) { reset(); return; }
|
||||
|
||||
// A media error is unambiguous — no need to wait out the timer.
|
||||
if (v.error) {
|
||||
describeCodec(v);
|
||||
if (W.quietUntil && now() < W.quietUntil) return;
|
||||
show(v, 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!W.seenAt) W.seenAt = now();
|
||||
if (now() - W.seenAt < timeoutMs(v)) return;
|
||||
|
||||
if (W.quietUntil && now() < W.quietUntil) return;
|
||||
|
||||
describeCodec(v);
|
||||
show(v, 'timeout');
|
||||
}
|
||||
|
||||
NS.Watchdog.init = function () {
|
||||
W.quietUntil = readQuiet();
|
||||
setInterval(poll, POLL_MS);
|
||||
log('watchdog armed (dead=' + DEAD_MS + 'ms, transcode=' + DEAD_TRANSCODE_MS + 'ms)');
|
||||
};
|
||||
})();
|
||||
+161
-5
@@ -299,16 +299,23 @@
|
||||
// Sits ABOVE Jellyfin's own OSD bar: at the viewport's bottom-right it
|
||||
// covered the player's fullscreen button (owner report). 120 px clears the
|
||||
// OSD bottom bar; the settings panel stacks above it.
|
||||
'.jfvr-btn{position:fixed;right:16px;bottom:120px;z-index:2147483000;display:flex;align-items:center;gap:8px;',
|
||||
// left/top are set from JS (see placeButton) so the button can be dragged
|
||||
// anywhere. touch-action:none is what makes a drag work on a touch screen
|
||||
// instead of scrolling the page underneath.
|
||||
'.jfvr-btn{position:fixed;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}',
|
||||
'font-size:15px;font-weight:600;cursor:grab;box-shadow:0 6px 22px rgba(0,0,0,.45);font-family:inherit;',
|
||||
'touch-action:none;user-select:none;-webkit-user-select:none;-webkit-tap-highlight-color:transparent}',
|
||||
'.jfvr-btn:active{transform:scale(.96)}',
|
||||
'.jfvr-btn.jfvr-drag{cursor:grabbing;transform:none;box-shadow:0 10px 32px rgba(0,0,0,.65)}',
|
||||
// the panel must always fit the viewport AND keep the CTA reachable, so the
|
||||
// body scrolls and the button lives in a sticky footer
|
||||
'.jfvr-panel{position:fixed;right:16px;bottom:180px;z-index:2147483001;width:290px;background:rgba(20,20,24,.96);',
|
||||
// Positioned from JS (positionPanel) so it follows the button once the
|
||||
// button has been dragged; it no longer lives in the viewport corner.
|
||||
'.jfvr-panel{position:fixed;z-index:2147483001;width:290px;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);display:flex;flex-direction:column;',
|
||||
'max-height:calc(100vh - 200px);overflow:hidden}',
|
||||
'max-height:calc(100vh - 32px);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;flex:0 0 auto}',
|
||||
'.jfvr-x{background:none;border:0;color:#aaa;font-size:20px;line-height:1;cursor:pointer;padding:0 4px}',
|
||||
'.jfvr-body{padding:12px;overflow-y:auto;flex:1 1 auto;min-height:0}',
|
||||
@@ -356,9 +363,151 @@
|
||||
var b = NS.makeEl('button', 'jfvr-btn');
|
||||
b.type = 'button';
|
||||
b.innerHTML = '<span>🎥</span><span>VR 播放</span>';
|
||||
b.addEventListener('click', function (ev) { ev.stopPropagation(); togglePanel(); });
|
||||
b.title = '点击打开设置;按住拖动可以换位置;双击回到默认位置';
|
||||
b.addEventListener('click', function (ev) {
|
||||
ev.stopPropagation();
|
||||
// A drag still produces a click on most browsers. Swallow that one so
|
||||
// dragging the button never opens the panel by accident.
|
||||
if (dragMoved) { dragMoved = false; return; }
|
||||
togglePanel();
|
||||
});
|
||||
// Escape hatch: a button dragged half off a small window is annoying to
|
||||
// recover by hand.
|
||||
b.addEventListener('dblclick', function (ev) {
|
||||
ev.stopPropagation();
|
||||
resetButtonPos();
|
||||
});
|
||||
installDrag(b);
|
||||
document.body.appendChild(b);
|
||||
btn = b;
|
||||
btnPos = readBtnPos();
|
||||
placeButton(b, btnPos);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* button placement / dragging
|
||||
* ------------------------------------------------------------------
|
||||
* The button used to be pinned to the bottom-right corner. On a short window
|
||||
* that put it right on top of the bitrate choices the playback watchdog
|
||||
* shows (that panel is centred), and there was no way to move it. It is
|
||||
* draggable now and the spot is remembered per browser.
|
||||
*
|
||||
* The settings panel follows the button rather than the viewport corner, so
|
||||
* the two cannot drift apart.
|
||||
* ------------------------------------------------------------------ */
|
||||
var BTN_POS_KEY = 'jfvr-btn-pos';
|
||||
var BTN_MARGIN = 8;
|
||||
var DRAG_SLOP = 4; // px of travel before it counts as a drag, not a tap
|
||||
|
||||
var btnPos = null; // {left, top} once placed; persisted on drop
|
||||
var dragging = false;
|
||||
var dragMoved = false;
|
||||
var dragStart = null;
|
||||
|
||||
function readBtnPos() {
|
||||
try {
|
||||
var o = JSON.parse(localStorage.getItem(BTN_POS_KEY) || 'null');
|
||||
if (o && typeof o.left === 'number' && typeof o.top === 'number') return o;
|
||||
} catch (e) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
function writeBtnPos(pos) {
|
||||
try { localStorage.setItem(BTN_POS_KEY, JSON.stringify(pos)); } catch (e) {}
|
||||
}
|
||||
|
||||
function clampPos(left, top, w, h) {
|
||||
var maxLeft = Math.max(BTN_MARGIN, window.innerWidth - w - BTN_MARGIN);
|
||||
var maxTop = Math.max(BTN_MARGIN, window.innerHeight - h - BTN_MARGIN);
|
||||
return {
|
||||
left: Math.round(Math.min(Math.max(left, BTN_MARGIN), maxLeft)),
|
||||
top: Math.round(Math.min(Math.max(top, BTN_MARGIN), maxTop))
|
||||
};
|
||||
}
|
||||
|
||||
/** The default spot: the old fixed corner, expressed in left/top. */
|
||||
function defaultPos(b) {
|
||||
return { left: window.innerWidth - b.offsetWidth - 16,
|
||||
top: window.innerHeight - b.offsetHeight - 120 };
|
||||
}
|
||||
|
||||
function placeButton(b, pos) {
|
||||
var p = pos || defaultPos(b);
|
||||
var c = clampPos(p.left, p.top, b.offsetWidth, b.offsetHeight);
|
||||
b.style.left = c.left + 'px';
|
||||
b.style.top = c.top + 'px';
|
||||
b.style.right = 'auto';
|
||||
b.style.bottom = 'auto';
|
||||
return c;
|
||||
}
|
||||
|
||||
function resetButtonPos() {
|
||||
if (!btn) return;
|
||||
try { localStorage.removeItem(BTN_POS_KEY); } catch (e) {}
|
||||
btnPos = placeButton(btn, null);
|
||||
positionPanel();
|
||||
}
|
||||
|
||||
function installDrag(b) {
|
||||
b.addEventListener('pointerdown', function (ev) {
|
||||
if (ev.button != null && ev.button !== 0) return; // primary button / touch only
|
||||
var r = b.getBoundingClientRect();
|
||||
dragging = true;
|
||||
dragMoved = false;
|
||||
dragStart = { x: ev.clientX, y: ev.clientY, left: r.left, top: r.top };
|
||||
b.classList.add('jfvr-drag');
|
||||
try { b.setPointerCapture(ev.pointerId); } catch (e) {}
|
||||
});
|
||||
|
||||
b.addEventListener('pointermove', function (ev) {
|
||||
if (!dragging || !dragStart) return;
|
||||
var dx = ev.clientX - dragStart.x;
|
||||
var dy = ev.clientY - dragStart.y;
|
||||
if (!dragMoved) {
|
||||
if (Math.abs(dx) < DRAG_SLOP && Math.abs(dy) < DRAG_SLOP) return;
|
||||
dragMoved = true;
|
||||
if (panel) hidePanel(); // do not drag the panel around with it
|
||||
}
|
||||
btnPos = clampPos(dragStart.left + dx, dragStart.top + dy, b.offsetWidth, b.offsetHeight);
|
||||
b.style.left = btnPos.left + 'px';
|
||||
b.style.top = btnPos.top + 'px';
|
||||
});
|
||||
|
||||
function endDrag(ev) {
|
||||
if (!dragging) return;
|
||||
dragging = false;
|
||||
dragStart = null;
|
||||
b.classList.remove('jfvr-drag');
|
||||
try { b.releasePointerCapture(ev.pointerId); } catch (e) {}
|
||||
if (dragMoved) writeBtnPos(btnPos);
|
||||
}
|
||||
b.addEventListener('pointerup', endDrag);
|
||||
b.addEventListener('pointercancel', endDrag);
|
||||
}
|
||||
|
||||
/** Put the panel next to the button: above it, or below when there is no room. */
|
||||
function positionPanel() {
|
||||
if (!panel || !btn) return;
|
||||
var b = btn.getBoundingClientRect();
|
||||
var w = panel.offsetWidth;
|
||||
var h = panel.offsetHeight;
|
||||
var gap = 8;
|
||||
|
||||
var left = b.right - w; // right edges aligned
|
||||
var top = b.top - h - gap;
|
||||
if (top < BTN_MARGIN) top = b.bottom + gap; // no room above -> below
|
||||
|
||||
var maxLeft = Math.max(BTN_MARGIN, window.innerWidth - w - BTN_MARGIN);
|
||||
var maxTop = Math.max(BTN_MARGIN, window.innerHeight - h - BTN_MARGIN);
|
||||
panel.style.left = Math.round(Math.min(Math.max(left, BTN_MARGIN), maxLeft)) + 'px';
|
||||
panel.style.top = Math.round(Math.min(Math.max(top, BTN_MARGIN), maxTop)) + 'px';
|
||||
panel.style.right = 'auto';
|
||||
panel.style.bottom = 'auto';
|
||||
}
|
||||
|
||||
function onViewportResize() {
|
||||
if (btn && document.body.contains(btn)) btnPos = placeButton(btn, btnPos);
|
||||
positionPanel();
|
||||
}
|
||||
|
||||
function removeButton() {
|
||||
@@ -542,6 +691,7 @@
|
||||
panel.appendChild(foot);
|
||||
|
||||
document.body.appendChild(panel);
|
||||
positionPanel();
|
||||
|
||||
panelSync = function () {
|
||||
for (var i = 0; i < panelSyncs.length; i++) {
|
||||
@@ -1943,6 +2093,9 @@
|
||||
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); }
|
||||
// Watches for "the player never produced a picture" and offers the only fix
|
||||
// that helps (let the server transcode). See 40-watchdog.js.
|
||||
try { if (NS.Watchdog && NS.Watchdog.init) NS.Watchdog.init(); } catch (e) { NS.recordError('Watchdog.init', e); }
|
||||
|
||||
scan();
|
||||
new MutationObserver(scan).observe(document.documentElement, { childList: true, subtree: true });
|
||||
@@ -1950,6 +2103,9 @@
|
||||
window.addEventListener('beforeunload', function () {
|
||||
if (S.session) { try { S.session.end(); } catch (e) {} }
|
||||
});
|
||||
// Keep the draggable button (and the panel that follows it) on screen when
|
||||
// the window changes size.
|
||||
window.addEventListener('resize', onViewportResize);
|
||||
installFrameTracker();
|
||||
|
||||
window.__JFVR_STATE__ = S;
|
||||
|
||||
Reference in New Issue
Block a user