feat(webui): switch from video.js to hls.js and native video player (#437)

* feat(webui): switch from video.js to hls.js and native video player

* fix(webui): prevent stale seek hijacking and guard native HLS compare mode
This commit is contained in:
Yiling-J
2026-09-30 20:23:44 +08:00
committed by GitHub
parent 123a8cb5f4
commit a84da8678e
13 changed files with 1053 additions and 441 deletions
+2
View File
@@ -18,6 +18,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
### Changed
- **webui**: Modernize video player architecture to use native HTML5 video and Hls.js, enabling instant rendition switching when changing resolution and eliminating heavy player wrapper overhead while preserving sub-frame accurate playback and annotations
## [0.4.9] - 2026-09-28
### Added
+3 -35
View File
@@ -364,6 +364,7 @@
"cmdk": "1.1.1",
"date-fns": "4.4.0",
"embla-carousel-react": "8.6.0",
"hls.js": "1.7.3",
"hono": "4.13.5",
"input-otp": "1.5.0",
"konva": "10.3.2",
@@ -387,7 +388,6 @@
"ulid": "3.0.2",
"use-image": "1.1.4",
"vaul": "1.1.2",
"video.js": "8.24.0",
"zod": "4.5.4",
"zustand": "5.0.15",
},
@@ -1632,12 +1632,6 @@
"@vercel/oidc": ["@vercel/oidc@3.2.0", "", {}, "sha512-UycprH3T6n3jH0k44NHMa7pnFHGu/N05MjojYr+Mc6I7obkoLIJujSWwin1pCvdy/eOxrI/l3uDLQsmcrOb4ug=="],
"@videojs/http-streaming": ["@videojs/http-streaming@3.17.5", "", { "dependencies": { "@babel/runtime": "^7.12.5", "@videojs/vhs-utils": "^4.1.2", "aes-decrypter": "^4.0.2", "global": "^4.4.0", "m3u8-parser": "^7.2.0", "mpd-parser": "^1.4.0", "mux.js": "7.1.0", "video.js": "^7 || ^8" } }, "sha512-PRRQVu0hYW5h0XvRWpIyIpEatpMZzZtf7L1avV89Hnu+69f+0c7oLsSL9jwpg2nbvzGkdX5rQxQkz5Vc7tFR9w=="],
"@videojs/vhs-utils": ["@videojs/vhs-utils@4.1.2", "", { "dependencies": { "@babel/runtime": "^7.12.5", "global": "^4.4.0" } }, "sha512-1Dsv5dtWOq0LP1G37O46FQFA2tKAZON5UMe6dFPxT7JvPhEx8QIXcFQxan8+hHI3fX6aomFRFrUBJNgRYQDSXA=="],
"@videojs/xhr": ["@videojs/xhr@2.7.0", "", { "dependencies": { "@babel/runtime": "^7.5.5", "global": "~4.4.0", "is-function": "^1.0.1" } }, "sha512-giab+EVRanChIupZK7gXjHy90y3nncA2phIOyG3Ne5fvpiMJzvqYwiTOnEVW2S4CoYcuKJkomat7bMXA/UoUZQ=="],
"@visx/curve": ["@visx/curve@4.0.1-alpha.0", "", { "dependencies": { "@visx/vendor": "4.0.0-alpha.0" } }, "sha512-jRu61Uz274pV1zyioXmboyrLutYbnKsgjj4njSGCnhdXj5GkZvZbg+ThDb6oOzoAnJOBRLz4rzPlWvNJOzuVMg=="],
"@visx/event": ["@visx/event@4.0.1-alpha.0", "", { "dependencies": { "@types/react": "*", "@visx/point": "4.0.1-alpha.0" } }, "sha512-EQqCMSv/s8NbFjo+hz3FKsvvYfP+2QslsFJ/24/O5l/W+7UC6J6aAvO0ujVwrTwdYbuQ+vhxKi1xdPdKR/qj1g=="],
@@ -1702,8 +1696,6 @@
"@workflow/serde": ["@workflow/serde@4.1.0", "", {}, "sha512-pav4F2BoirECWR7Nf1TKt+2eETcBj7jj4cBefQ8VXQCA6NPkaKeLfj/zMgi+3zYV5ZIBT4GuUiphsj0/b9hPQQ=="],
"@xmldom/xmldom": ["@xmldom/xmldom@0.8.15", "", {}, "sha512-/5NV/vDALVFDXgLmfsy9TRCBlKwO2LNBFzpzvb9iIj+jR+eSc6DLYYvVOdivT/jm7MtU6TebYuRmzEOI7w40UA=="],
"@xtuc/ieee754": ["@xtuc/ieee754@1.2.0", "", {}, "sha512-DX8nKgqcGwsc0eJSqYt5lwP4DH5FlHnmuWWBRy7X0NcaGR0ZtuyeESgMwTYVEtxmsNGY+qit4QYT/MIYTOTPeA=="],
"@xtuc/long": ["@xtuc/long@4.2.2", "", {}, "sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ=="],
@@ -1716,8 +1708,6 @@
"adm-zip": ["adm-zip@0.6.0", "", {}, "sha512-XleryMhbuksdKtofnWZ9Sk+4CUTbms4Mb/EU32SZwToAyZ5RgVos/ki8n+yr0LWHOGKuakbXTuuYNHLQjhddgg=="],
"aes-decrypter": ["aes-decrypter@4.0.2", "", { "dependencies": { "@babel/runtime": "^7.12.5", "@videojs/vhs-utils": "^4.1.1", "global": "^4.4.0", "pkcs7": "^1.0.4" } }, "sha512-lc+/9s6iJvuaRe5qDlMTpCFjnwpkeOXp8qP3oiZ5jsj1MRg+SBVUmmICrhxHvc8OELSmc+fEyyxAuppY6hrWzw=="],
"agent-base": ["agent-base@7.1.4", "", {}, "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ=="],
"ai": ["ai@7.0.84", "", { "dependencies": { "@ai-sdk/gateway": "4.0.68", "@ai-sdk/provider": "4.0.8", "@ai-sdk/provider-utils": "5.0.33" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-znHNBWADOy79k8EK7636kDm5LmnjF4tMzegC2TfmlLDobDZbs0z0OXD2sxtH3eDTp5Re+pJQESvjinWJ6+kadQ=="],
@@ -2008,8 +1998,6 @@
"dom-accessibility-api": ["dom-accessibility-api@0.5.16", "", {}, "sha512-X7BJ2yElsnOJ30pZF4uIIDfBEVgF4XEBxL9Bxhy6dnrm5hkzqmsWHGTiHqRiITNhMyFLyAiWndIJP7Z1NTteDg=="],
"dom-walk": ["dom-walk@0.1.2", "", {}, "sha512-6QvTW9mrGeIegrFXdtQi9pk7O/nSK6lSdXW2eqUspN5LWD7UTji2Fqw5V2YLjBpHEoU9Xl/eUWNpDeZvoyOv2w=="],
"dotenv": ["dotenv@17.4.2", "", {}, "sha512-nI4U3TottKAcAD9LLud4Cb7b2QztQMUEfHbvhTH09bqXTxnSie8WnjPALV/WMCrJZ6UV/qHJ6L03OqO3LcdYZw=="],
"dotenv-expand": ["dotenv-expand@13.0.0", "", { "dependencies": { "dotenv": "^17.4.2" } }, "sha512-aBfBS8eYIeXmpHI9ThIlA7/WLq+SLt18iXUZhb52rW89QLKQFoIpPG1bPeewoPZsTyjSSO3T7234FBVUM1V2rA=="],
@@ -2226,8 +2214,6 @@
"glob-to-regex.js": ["glob-to-regex.js@1.2.0", "", { "peerDependencies": { "tslib": "2" } }, "sha512-QMwlOQKU/IzqMUOAZWubUOT8Qft+Y0KQWnX9nK3ch0CJg0tTp4TvGZsTfudYKv2NzoQSyPcnA6TYeIQ3jGichQ=="],
"global": ["global@4.4.0", "", { "dependencies": { "min-document": "^2.19.0", "process": "^0.11.10" } }, "sha512-wv/LAoHdRE3BeTGz53FAamhGlPLhlssK45usmGFThIi4XqnBmjKQ16u+RNbP7WvigRZDxUsM0J3gcQ5yicaL0w=="],
"globals": ["globals@17.11.0", "", {}, "sha512-Z2I8hM+PbJDXQDq3Icgpzv+mPdwr68iZUU9d5WW4FuXfDUQfkZaZuvjMv42/5crNyw154+9+VWXbYrUgDXbxNw=="],
"globalthis": ["globalthis@1.0.4", "", { "dependencies": { "define-properties": "^1.2.1", "gopd": "^1.0.1" } }, "sha512-DpLKbNU4WylpxJykQujfCcwYWiV/Jhm50Goo0wrVILAv5jOr9d+H+UR3PhSCD2rCCEIg0uc+G+muBTwD54JhDQ=="],
@@ -2272,6 +2258,8 @@
"help-me": ["help-me@5.0.0", "", {}, "sha512-7xgomUX6ADmcYzFik0HzAxh/73YlKR9bmFzf51CZwR+b6YtzU2m0u49hQCqV6SvlqIqsaxovfwdvbnsw3b/zpg=="],
"hls.js": ["hls.js@1.7.3", "", {}, "sha512-MsPlx6yVW4Qv4C7mEVou4/gk/5cN2dVxOTnrNjPSmyH2f0Ln+/UkG10Ax1PE6OvrmTVPKgQYozTtBaPOuCzq7A=="],
"hono": ["hono@4.13.5", "", {}, "sha512-O6+/eCYRkzzzy0rPWwKLiGBR1nFuUPZynnwjxN1MBA62NNqbT0wQEzQyK2gSO5yDIDB336sXQleAhOHrzlYyKw=="],
"html-url-attributes": ["html-url-attributes@3.0.1", "", {}, "sha512-ol6UPyBWqsrO6EJySPz2O7ZSr856WDrEzM5zMqp+FJJLGMW35cLYmmZnl0vztAZxRUoNZJFTCohfjuIJ8I4QBQ=="],
@@ -2332,8 +2320,6 @@
"is-fullwidth-code-point": ["is-fullwidth-code-point@3.0.0", "", {}, "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg=="],
"is-function": ["is-function@1.0.2", "", {}, "sha512-lw7DUp0aWXYg+CBCN+JKkcE0Q2RayZnSvnZBlwgxHBQhqt5pZNVy4Ri7H9GmmXkdu7LUthszM+Tor1u/2iBcpQ=="],
"is-generator-function": ["is-generator-function@1.1.2", "", { "dependencies": { "call-bound": "^1.0.4", "generator-function": "^2.0.0", "get-proto": "^1.0.1", "has-tostringtag": "^1.0.2", "safe-regex-test": "^1.1.0" } }, "sha512-upqt1SkGkODW9tsGNG5mtXTXtECizwtS2kA161M+gJPc1xdb/Ax629af6YrTwcOeQHbewrPNlE5Dx7kzvXTizA=="],
"is-glob": ["is-glob@4.0.3", "", { "dependencies": { "is-extglob": "^2.1.1" } }, "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg=="],
@@ -2490,8 +2476,6 @@
"lz-string": ["lz-string@1.5.0", "", { "bin": { "lz-string": "bin/bin.js" } }, "sha512-h5bgJWpxJNswbU7qCrV0tIKQCaS3blPDrqKWx+QxzuzL1zGUzij9XCWLrSLsJPu5t+eWA/ycetzYAO5IOMcWAQ=="],
"m3u8-parser": ["m3u8-parser@7.2.0", "", { "dependencies": { "@babel/runtime": "^7.12.5", "@videojs/vhs-utils": "^4.1.1", "global": "^4.4.0" } }, "sha512-CRatFqpjVtMiMaKXxNvuI3I++vUumIXVVT/JpCpdU/FynV/ceVw1qpPyyBNindL+JlPMSesx+WX1QJaZEJSaMQ=="],
"magic-string": ["magic-string@0.30.21", "", { "dependencies": { "@jridgewell/sourcemap-codec": "^1.5.5" } }, "sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ=="],
"math-intrinsics": ["math-intrinsics@1.1.0", "", {}, "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g=="],
@@ -2562,8 +2546,6 @@
"mime-match": ["mime-match@1.0.2", "", { "dependencies": { "wildcard": "^1.1.0" } }, "sha512-VXp/ugGDVh3eCLOBCiHZMYWQaTNUHv2IJrut+yXA6+JbLPXHglHwfS/5A5L0ll+jkCY7fIzRJcH6OIunF+c6Cg=="],
"min-document": ["min-document@2.19.2", "", { "dependencies": { "dom-walk": "^0.1.0" } }, "sha512-8S5I8db/uZN8r9HSLFVWPdJCvYOejMcEC82VIzNUc6Zkklf/d1gg2psfE79/vyhWOj4+J8MtwmoOz3TmvaGu5A=="],
"minimatch": ["minimatch@10.2.6", "", { "dependencies": { "brace-expansion": "^5.0.8" } }, "sha512-vpLQEs+VLCr1nU0BXS07maYoFwlDAH0gngQuuttxIwutDFEMHq2blX+8vpgxDdK3J1PwjCJiep77OitTZ4Ll1A=="],
"minimist": ["minimist@1.2.8", "", {}, "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA=="],
@@ -2576,12 +2558,8 @@
"mkdirp-classic": ["mkdirp-classic@0.5.3", "", {}, "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A=="],
"mpd-parser": ["mpd-parser@1.4.0", "", { "dependencies": { "@babel/runtime": "^7.12.5", "@videojs/vhs-utils": "^4.1.2", "@xmldom/xmldom": "^0.8.3", "global": "^4.4.0" }, "bin": { "mpd-to-m3u8-json": "bin/parse.js" } }, "sha512-blbA7XpAaOdC/PR0Tu8GiUxlx6CpBuRowQPYV7lhi9Yr9G9+dkPXIUjqjzc/NCn/uZZPopluaJlIYntIEI/VLw=="],
"ms": ["ms@2.1.3", "", {}, "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="],
"mux.js": ["mux.js@7.1.0", "", { "dependencies": { "@babel/runtime": "^7.11.2", "global": "^4.4.0" }, "bin": { "muxjs-transmux": "bin/transmux.js" } }, "sha512-NTxawK/BBELJrYsZThEulyUMDVlLizKdxyAsMuzoCD1eFj97BVaA8D/CvKsKu6FOLYkFojN5CbM9h++ZTZtknA=="],
"mysql2": ["mysql2@3.15.3", "", { "dependencies": { "aws-ssl-profiles": "^1.1.1", "denque": "^2.1.0", "generate-function": "^2.3.1", "iconv-lite": "^0.7.0", "long": "^5.2.1", "lru.min": "^1.0.0", "named-placeholders": "^1.1.3", "seq-queue": "^0.0.5", "sqlstring": "^2.3.2" } }, "sha512-FBrGau0IXmuqg4haEZRBfHNWB5mUARw6hNwPDXXGg0XzVJ50mr/9hb267lvpVMnhZ1FON3qNd4Xfcez1rbFwSg=="],
"named-placeholders": ["named-placeholders@1.1.6", "", { "dependencies": { "lru.min": "^1.1.0" } }, "sha512-Tz09sEL2EEuv5fFowm419c1+a/jSMiBjI9gHxVLrVdbUkkNUUfjsVYs9pVZu5oCon/kmRh9TfLEObFtkVxmY0w=="],
@@ -2712,8 +2690,6 @@
"pkce-challenge": ["pkce-challenge@5.0.1", "", {}, "sha512-wQ0b/W4Fr01qtpHlqSqspcj3EhBvimsdh0KlHhH8HRZnMsEa0ea2fTULOXOS9ccQr3om+GcGRk4e+isrZWV8qQ=="],
"pkcs7": ["pkcs7@1.0.4", "", { "dependencies": { "@babel/runtime": "^7.5.5" }, "bin": { "pkcs7": "bin/cli.js" } }, "sha512-afRERtHn54AlwaF2/+LFszyAANTCggGilmcmILUzEjvs3XgFZT+xE6+QWQcAGmu4xajy+Xtj7acLOPdx5/eXWQ=="],
"pkg-types": ["pkg-types@2.3.1", "", { "dependencies": { "confbox": "^0.2.4", "exsolve": "^1.0.8", "pathe": "^2.0.3" } }, "sha512-y+ichcgc2LrADuhLNAx8DFjVfgz91pRxfZdI3UDhxHvcVEZsenLO+7XaU5vOp0u/7V/wZ+plyuQxtrDlZJ+yeg=="],
"playwright": ["playwright@1.61.1", "", { "dependencies": { "playwright-core": "1.61.1" }, "optionalDependencies": { "fsevents": "2.3.2" }, "bin": { "playwright": "cli.js" } }, "sha512-DWnY5o3YbLWK4GovuAVwpqL+1VwGNdUGrRr++8j8PtQQzvAVZUIMjKQ90fY689sEJZJBbZVw1rXaOKSTitkzPQ=="],
@@ -3118,14 +3094,6 @@
"victory-vendor": ["victory-vendor@37.3.6", "", { "dependencies": { "@types/d3-array": "^3.0.3", "@types/d3-ease": "^3.0.0", "@types/d3-interpolate": "^3.0.1", "@types/d3-scale": "^4.0.2", "@types/d3-shape": "^3.1.0", "@types/d3-time": "^3.0.0", "@types/d3-timer": "^3.0.0", "d3-array": "^3.1.6", "d3-ease": "^3.0.1", "d3-interpolate": "^3.0.1", "d3-scale": "^4.0.2", "d3-shape": "^3.1.0", "d3-time": "^3.0.0", "d3-timer": "^3.0.1" } }, "sha512-SbPDPdDBYp+5MJHhBCAyI7wKM3d5ivekigc2Dk2s7pgbZ9wIgIBYGVw4zGHBml/qTFbexrofXW6Gu4noGxrOwQ=="],
"video.js": ["video.js@8.24.0", "", { "dependencies": { "@babel/runtime": "^7.28.4", "@videojs/http-streaming": "^3.17.5", "@videojs/vhs-utils": "^4.1.2", "@videojs/xhr": "2.7.0", "aes-decrypter": "^4.0.2", "global": "4.4.0", "m3u8-parser": "^7.2.0", "mpd-parser": "^1.4.0", "mux.js": "^7.0.1", "videojs-contrib-quality-levels": "4.1.0", "videojs-font": "4.2.0", "videojs-vtt.js": "0.15.5" } }, "sha512-ZUuxTlDRkEWhpp2mvGCSFzI0Tc3zcC6IUZbIhBcZ3QW0Ms+IRp/tzYkhqexcMYwrcukRFiWWJmRANsLKvv8Vsg=="],
"videojs-contrib-quality-levels": ["videojs-contrib-quality-levels@4.1.0", "", { "dependencies": { "global": "^4.4.0" }, "peerDependencies": { "video.js": "^8" } }, "sha512-TfrXJJg1Bv4t6TOCMEVMwF/CoS8iENYsWNKip8zfhB5kTcegiFYezEA0eHAJPU64ZC8NQbxQgOwAsYU8VXbOWA=="],
"videojs-font": ["videojs-font@4.2.0", "", {}, "sha512-YPq+wiKoGy2/M7ccjmlvwi58z2xsykkkfNMyIg4xb7EZQQNwB71hcSsB3o75CqQV7/y5lXkXhI/rsGAS7jfEmQ=="],
"videojs-vtt.js": ["videojs-vtt.js@0.15.5", "", { "dependencies": { "global": "^4.3.1" } }, "sha512-yZbBxvA7QMYn15Lr/ZfhhLPrNpI/RmCSCqgIff57GC2gIrV5YfyzLfLyZMj0NnZSAz8syB4N0nHXpZg9MyrMOQ=="],
"vite": ["vite@8.2.2", "", { "dependencies": { "lightningcss": "^1.33.0", "picomatch": "^4.0.5", "postcss": "^8.5.26", "rolldown": "~1.2.4", "tinyglobby": "^0.2.17" }, "optionalDependencies": { "fsevents": "~2.3.3" }, "peerDependencies": { "@types/node": "^20.19.0 || >=22.12.0", "@vitejs/devtools": "^0.4.0 || ^0.5.0", "esbuild": "^0.27.0 || ^0.28.0", "jiti": ">=1.21.0", "less": "^4.0.0", "sass": "^1.70.0", "sass-embedded": "^1.70.0", "stylus": ">=0.54.8", "sugarss": "^5.0.0", "terser": "^5.16.0", "tsx": "^4.8.1", "yaml": "^2.4.2" }, "optionalPeers": ["@types/node", "@vitejs/devtools", "esbuild", "jiti", "less", "sass", "sass-embedded", "stylus", "sugarss", "terser", "tsx", "yaml"], "bin": { "vite": "bin/vite.js" } }, "sha512-cFKLV/PRgAUlIRm5WjMjJ86jrftzpqcgH+Us+DS8mI3CDNiH30Whrz8uHL3+MOLPAgqbMBAqWdAHAphOAM+z/Q=="],
"vite-tsconfig-paths": ["vite-tsconfig-paths@6.1.1", "", { "dependencies": { "debug": "^4.1.1", "globrex": "^0.1.2", "tsconfck": "^3.0.3" }, "peerDependencies": { "vite": "*" } }, "sha512-2cihq7zliibCCZ8P9cKJrQBkfgdvcFkOOc3Y02o3GWUDLgqjWsZudaoiuOwO/gzTzy17cS5F7ZPo4bsnS4DGkg=="],
@@ -66,6 +66,8 @@ export function CompareControlBar({
showFrames: false,
currentResolution: v.currentResolution,
isCurrentHdr: v.isCurrentHdr,
activeAutoResolution: v.activeAutoResolution,
isHlsManualSupported: v.isHlsManualSupported,
}
// The active-side asset carries the metadata used for timecode display and
@@ -33,6 +33,8 @@ export interface PaneReportedState {
isCurrentHdr?: boolean
resolutions: DisplayTranscode[]
buffered: number
activeAutoResolution?: string
isHlsManualSupported?: boolean
}
}
@@ -0,0 +1,217 @@
// @vitest-environment happy-dom
import { act, cleanup, render } from '@testing-library/react'
import React, { createRef } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { CompareVideoPane } from './compare-video-pane'
import type { ComparePaneHandle, PaneReportedState } from '../../compare/types'
import type { AssetInfo } from '@shumai/dtos'
const { mockHlsInstance, mockHlsConstructor } = vi.hoisted(() => {
const instance = {
attachMedia: vi.fn(),
loadSource: vi.fn(),
on: vi.fn(),
off: vi.fn(),
once: vi.fn(),
destroy: vi.fn(),
levels: [
{ height: 1080, width: 1920, bitrate: 5000000 },
{ height: 720, width: 1280, bitrate: 2500000 },
],
currentLevel: -1,
startLoad: vi.fn(),
recoverMediaError: vi.fn(),
}
const constructor = vi.fn(function () {
return instance
})
return { mockHlsInstance: instance, mockHlsConstructor: constructor }
})
vi.mock('hls.js', () => {
const MockHls = mockHlsConstructor
// @ts-expect-error adding static mock property
MockHls.isSupported = vi.fn(() => true)
// @ts-expect-error adding static mock property
MockHls.Events = {
MANIFEST_PARSED: 'hlsManifestParsed',
LEVEL_SWITCHED: 'hlsLevelSwitched',
ERROR: 'hlsError',
}
// @ts-expect-error adding static mock property
MockHls.ErrorTypes = {
NETWORK_ERROR: 'networkError',
MEDIA_ERROR: 'mediaError',
OTHER_ERROR: 'otherError',
}
return {
default: MockHls,
}
})
vi.mock('@/ui/components/drawing-canvas', () => ({
default: () => <div data-testid="drawing-canvas" />,
}))
vi.mock('@/ui/paraglide/messages.js', () => ({
m: new Proxy({}, { get: () => () => '' }),
}))
describe('CompareVideoPane', () => {
beforeEach(async () => {
vi.clearAllMocks()
mockHlsInstance.currentLevel = -1
const Hls = (await import('hls.js')).default as unknown as {
isSupported: ReturnType<typeof vi.fn>
}
Hls.isSupported.mockReturnValue(true)
})
afterEach(() => {
cleanup()
})
it('safely handles native HLS fallback without clearing video.src when manual resolution is invoked', async () => {
const Hls = (await import('hls.js')).default as unknown as {
isSupported: ReturnType<typeof vi.fn>
}
Hls.isSupported.mockReturnValue(false)
// Mock HTMLMediaElement.prototype.canPlayType to support native HLS
const originalCanPlayType = HTMLMediaElement.prototype.canPlayType
HTMLMediaElement.prototype.canPlayType = vi.fn((type: string) =>
type === 'application/vnd.apple.mpegurl' ? 'maybe' : '',
)
try {
const hlsVideo: AssetInfo = {
id: 'hls-compare',
name: 'hls-compare.m3u8',
proxyType: 'video',
media: {
isHls: true,
hls: {
key: 'hls-key',
url: 'https://cdn.example.com/master.m3u8',
resolutions: [
{ width: 1920, height: 1080, resolution: '1080p' },
{ width: 1280, height: 720, resolution: '720p' },
],
},
metadata: {
originalWidth: 1920,
originalHeight: 1080,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
},
} as unknown as AssetInfo
const reportedStates: PaneReportedState[] = []
const ref = createRef<ComparePaneHandle>()
const { container } = render(
<CompareVideoPane
ref={ref}
file={hlsVideo}
isActive={true}
annotations={[]}
onStateChange={(state) => reportedStates.push(state)}
onActivate={vi.fn()}
/>,
)
const video = container.querySelector('video') as HTMLVideoElement
expect(video.src).toBe('https://cdn.example.com/master.m3u8')
// Check reported state: isHlsManualSupported should be false
const lastState = reportedStates[reportedStates.length - 1]
expect(lastState?.video?.isHlsManualSupported).toBe(false)
// Invoke changeResolution('1080p')
act(() => {
ref.current?.changeResolution('1080p')
})
// In native fallback, changeResolution should NO-OP and NOT clear video.src to empty string
expect(video.src).toBe('https://cdn.example.com/master.m3u8')
} finally {
HTMLMediaElement.prototype.canPlayType = originalCanPlayType
}
})
it('switches HLS resolution immediately using currentLevel and does not hijack subsequent seeks', async () => {
vi.useFakeTimers()
try {
const hlsVideo: AssetInfo = {
id: 'hls-compare-switch',
name: 'hls-compare-switch.m3u8',
proxyType: 'video',
media: {
isHls: true,
hls: {
key: 'hls-key',
url: 'https://cdn.example.com/master.m3u8',
resolutions: [
{ width: 1920, height: 1080, resolution: '1080p' },
{ width: 1280, height: 720, resolution: '720p' },
],
},
metadata: {
originalWidth: 1920,
originalHeight: 1080,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
},
} as unknown as AssetInfo
const reportedStates: PaneReportedState[] = []
const ref = createRef<ComparePaneHandle>()
const { container } = render(
<CompareVideoPane
ref={ref}
file={hlsVideo}
isActive={true}
annotations={[]}
onStateChange={(state) => reportedStates.push(state)}
onActivate={vi.fn()}
/>,
)
const video = container.querySelector('video') as HTMLVideoElement
Object.defineProperty(video, 'paused', { value: true, configurable: true })
video.currentTime = 0
// In Hls.js supported mode, isHlsManualSupported should be true
const lastState = reportedStates[reportedStates.length - 1]
expect(lastState?.video?.isHlsManualSupported).toBe(true)
// Switch to 720p (index 1)
act(() => {
ref.current?.changeResolution('720p')
})
expect(mockHlsInstance.currentLevel).toBe(1)
// Advance timers past the 1500ms safety timeout without seeked event
act(() => {
vi.advanceTimersByTime(2000)
})
// Now user performs an unrelated seek to frame 150 (currentTime = 5.0)
video.currentTime = 5.0
act(() => {
video.dispatchEvent(new Event('seeked'))
})
// video.currentTime must remain at frame 150, NOT rewound to frame 0
expect(Math.floor(video.currentTime * 30 + 0.001)).toBe(150)
expect(video.currentTime).toBeCloseTo(5.0167, 3)
} finally {
vi.useRealTimers()
}
})
})
@@ -6,10 +6,9 @@ import type { Annotation } from '@/ui/types'
import type { AssetInfo } from '@shumai/dtos'
import { Play, AudioLines } from 'lucide-react'
import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react'
import videojs from 'video.js'
import type Player from 'video.js/dist/types/player'
import Hls from 'hls.js'
import { useFramePlayer } from './use-frame-player'
import { resolveTotalFrames } from './utils'
import { calculateFrameCenterTime, resolveTotalFrames } from './utils'
import { clampFrame as clampFrameUtil } from '../../compare/compare-utils'
import type { ComparePaneHandle, DisplayTranscode, PaneReportedState } from '../../compare/types'
@@ -92,8 +91,7 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
},
ref,
) {
const playerRef = useRef<Player | null>(null)
const videoContainerRef = useRef<HTMLDivElement>(null)
const hlsRef = useRef<Hls | null>(null)
const containerRef = useRef<HTMLDivElement>(null)
const videoRef = useRef<HTMLVideoElement | null>(null)
@@ -132,6 +130,9 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
isHls ? 'Auto' : (initialRes?.resolution ?? ''),
)
const [isCurrentHdr, setIsCurrentHdr] = useState(initialRes?.hdr)
const [activeAutoResolution, setActiveAutoResolution] = useState<string | undefined>(undefined)
const pendingResolutionCorrectionRef = useRef<(() => void) | null>(null)
const isHlsManualSupported = !isHls || Boolean(Hls.isSupported())
const currentSrcRef = useRef(isHls ? file.media!.hls!.url : initialRes?.url)
const isAudio = file.proxyType === 'audio'
@@ -174,78 +175,150 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
const res = isHls ? null : getInitialResolution(resolutions)
setCurrentResolution(isHls ? 'Auto' : (res?.resolution ?? ''))
setIsCurrentHdr(res?.hdr)
setActiveAutoResolution(undefined)
currentSrcRef.current = isHls ? file.media!.hls!.url : res?.url
setHasStartedPlaying(false)
}, [file.id, Boolean(isHls ? file.media?.hls?.url : initialRes?.url)])
// Initialize video.js
// Initialize player (Hls.js or native HTML5 video)
useEffect(() => {
if (!videoContainerRef.current) return
const videoElement = document.createElement('video-js')
videoElement.classList.add('vjs-big-play-centered', '!h-full', '!w-full')
videoElement.style.pointerEvents = 'none'
videoContainerRef.current.appendChild(videoElement)
const video = videoRef.current
if (!video) return
const targetSrc = isHls ? file.media!.hls!.url! : (currentSrcRef.current ?? '')
const targetType = isHls ? 'application/x-mpegURL' : 'video/mp4'
if (!targetSrc) return
const player = (playerRef.current = videojs(videoElement, {
controls: false,
autoplay: false,
preload: 'auto',
playsinline: true,
sources: [{ src: targetSrc, type: targetType }],
}))
const htmlVid = videoElement.querySelector('video')
if (htmlVid) {
videoRef.current = htmlVid
} else {
player.ready(() => {
const techEl = player.tech({ iWillNotUseThisInPlugins: true })?.el() as HTMLVideoElement
if (techEl) {
videoRef.current = techEl
}
})
if (hlsRef.current) {
hlsRef.current.destroy()
hlsRef.current = null
}
player.on('play', () => {
setIsPlayerReady(false)
let hls: Hls | null = null
if (isHls) {
if (Hls.isSupported()) {
hls = new Hls({
autoStartLoad: true,
startLevel: -1,
capLevelToPlayerSize: false,
enableWorker: true,
})
hlsRef.current = hls
hls.attachMedia(video)
hls.loadSource(targetSrc)
hls.on(Hls.Events.MANIFEST_PARSED, () => {
setIsPlayerReady(true)
})
hls.on(Hls.Events.LEVEL_SWITCHED, (_event, eventData) => {
const level = hls?.levels[eventData.level]
if (level) {
setActiveAutoResolution(`${level.height}p`)
}
})
hls.on(Hls.Events.ERROR, (_event, eventData) => {
if (eventData.fatal) {
switch (eventData.type) {
case Hls.ErrorTypes.NETWORK_ERROR:
hls?.startLoad()
break
case Hls.ErrorTypes.MEDIA_ERROR:
hls?.recoverMediaError()
break
default:
hls?.destroy()
break
}
}
})
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = targetSrc
}
} else {
video.src = targetSrc
}
const handlePlay = () => {
if (pendingResolutionCorrectionRef.current) {
pendingResolutionCorrectionRef.current()
pendingResolutionCorrectionRef.current = null
}
setIsPlaying(true)
setHasStartedPlaying(true)
onPlay?.()
})
player.on('pause', () => setIsPlaying(false))
player.on('ended', () => setIsPlaying(false))
}
const handlePause = () => setIsPlaying(false)
const handleEnded = () => setIsPlaying(false)
const handleLoadedMetadata = () => setIsPlayerReady(true)
const handleLoadStart = () => setIsPlayerReady(false)
const handleProgress = () => {
const vidDuration = video.duration || containerDuration || 0
if (vidDuration > 0 && video.buffered.length > 0) {
let bufferedEnd = 0
for (let i = 0; i < video.buffered.length; i++) {
if (
video.buffered.start(i) <= video.currentTime &&
video.currentTime <= video.buffered.end(i)
) {
bufferedEnd = video.buffered.end(i)
break
}
}
if (bufferedEnd === 0) {
bufferedEnd = video.buffered.end(video.buffered.length - 1)
}
setBuffered((bufferedEnd / vidDuration) * 100)
}
}
const handleVolumeChange = () => {
setPlayerVolume(video.volume || 0)
setPlayerMuted(video.muted || false)
}
const handleRateChange = () => setPlaybackRate(video.playbackRate || 1)
player.on('timeupdate', () => {
setBuffered(player.bufferedPercent())
})
player.on('loadedmetadata', () => {
setIsPlayerReady(true)
})
player.on('loadstart', () => setIsPlayerReady(false))
player.on('volumechange', () => {
setPlayerVolume(player.volume() || 0)
setPlayerMuted(player.muted() || false)
})
player.on('ratechange', () => setPlaybackRate(player.playbackRate() || 1))
video.addEventListener('play', handlePlay)
video.addEventListener('pause', handlePause)
video.addEventListener('ended', handleEnded)
video.addEventListener('loadedmetadata', handleLoadedMetadata)
video.addEventListener('loadstart', handleLoadStart)
video.addEventListener('timeupdate', handleProgress)
video.addEventListener('progress', handleProgress)
video.addEventListener('volumechange', handleVolumeChange)
video.addEventListener('ratechange', handleRateChange)
return () => {
if (player && !player.isDisposed()) {
player.dispose()
playerRef.current = null
videoRef.current = null
video.removeEventListener('play', handlePlay)
video.removeEventListener('pause', handlePause)
video.removeEventListener('ended', handleEnded)
video.removeEventListener('loadedmetadata', handleLoadedMetadata)
video.removeEventListener('loadstart', handleLoadStart)
video.removeEventListener('timeupdate', handleProgress)
video.removeEventListener('progress', handleProgress)
video.removeEventListener('volumechange', handleVolumeChange)
video.removeEventListener('ratechange', handleRateChange)
if (pendingResolutionCorrectionRef.current) {
pendingResolutionCorrectionRef.current()
pendingResolutionCorrectionRef.current = null
}
if (hlsRef.current) {
hlsRef.current.destroy()
hlsRef.current = null
}
}
}, [file.id])
// Apply muted / volume (audio routing: only active side unmuted)
useEffect(() => {
const player = playerRef.current
if (!player || !isPlayerReady) return
player.muted(muted)
player.volume(volume)
const video = videoRef.current
if (!video || !isPlayerReady) return
video.muted = muted
video.volume = volume
}, [muted, volume, isPlayerReady])
// Report state upward
@@ -266,6 +339,8 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
isCurrentHdr,
resolutions,
buffered,
activeAutoResolution,
isHlsManualSupported,
},
}
onStateChange(state)
@@ -282,6 +357,8 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
currentResolution,
isCurrentHdr,
buffered,
activeAutoResolution,
isHlsManualSupported,
])
// Propagate active playhead time
@@ -337,28 +414,77 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
const changeResolution = useCallback(
(resolution: string, hdr?: boolean) => {
const player = playerRef.current
if (!player) return
const video = videoRef.current
if (!video) return
if (pendingResolutionCorrectionRef.current) {
pendingResolutionCorrectionRef.current()
pendingResolutionCorrectionRef.current = null
}
if (isHls) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ql = (player as any).qualityLevels?.()
if (!hlsRef.current) {
// Safari native HLS fallback does not support manual rendition switching
return
}
const hls = hlsRef.current
const wasPaused = video.paused
const targetFrame = currentFrameRef.current
if (resolution === 'Auto') {
if (ql) {
for (let i = 0; i < ql.length; i++) {
ql[i].enabled = true
}
if (currentResolution === 'Auto' && hls.currentLevel === -1) {
return
}
hls.currentLevel = -1
setCurrentResolution('Auto')
} else {
const target = resolutions.find((r) => r.resolution === resolution)
if (target && ql) {
for (let i = 0; i < ql.length; i++) {
ql[i].enabled = ql[i].height === target.height
if (target) {
const targetIndex = hls.levels.findIndex(
(lvl) =>
lvl.height === target.height ||
Math.max(lvl.width, lvl.height) === Math.max(target.width, target.height),
)
if (targetIndex !== -1) {
if (targetIndex === hls.currentLevel && currentResolution === resolution) {
return
}
hls.currentLevel = targetIndex
setCurrentResolution(resolution)
setIsCurrentHdr(target.hdr)
}
}
setCurrentResolution(resolution)
setIsCurrentHdr(target?.hdr)
}
if (wasPaused) {
let timer: ReturnType<typeof setTimeout> | null = null
const cleanup = () => {
if (timer) {
clearTimeout(timer)
timer = null
}
video.removeEventListener('seeked', onSeeked)
hls.off?.(Hls.Events.ERROR, cleanup)
if (pendingResolutionCorrectionRef.current === cleanup) {
pendingResolutionCorrectionRef.current = null
}
}
const onSeeked = () => {
cleanup()
const safeCenterTime = calculateFrameCenterTime(targetFrame, frameRate)
const frameDuration = 1 / frameRate
if (Math.abs(video.currentTime - safeCenterTime) > frameDuration / 4) {
video.currentTime = safeCenterTime
}
}
// Safety timeout (1.5s): if no seeked event fires from rendition switch, cleanly detach
timer = setTimeout(cleanup, 1500)
pendingResolutionCorrectionRef.current = cleanup
video.addEventListener('seeked', onSeeked)
hls.once?.(Hls.Events.ERROR, cleanup)
}
return
}
@@ -370,21 +496,24 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
)
: resolutions.find((r) => r.resolution === resolution)
if (!target) return
const wasPlaying = !player.paused()
const currentT = player.currentTime()
const wasPlaying = !video.paused
const currentT = video.currentTime
setCurrentResolution(resolution)
setIsCurrentHdr(target.hdr)
currentSrcRef.current = target.url
player.src({ type: 'video/mp4', src: target.url })
player.one('loadedmetadata', () => {
player.currentTime(currentT)
video.src = target.url || ''
const onLoadedMetadata = () => {
video.removeEventListener('loadedmetadata', onLoadedMetadata)
video.currentTime = currentT
if (wasPlaying) {
const p = player.play()
const p = video.play()
if (p !== undefined) p.catch(() => {})
}
})
video.playbackRate = playbackRate
}
video.addEventListener('loadedmetadata', onLoadedMetadata)
},
[resolutions, isHls],
[resolutions, isHls, frameRate, playbackRate, currentResolution],
)
useImperativeHandle(
@@ -392,47 +521,71 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
(): ComparePaneHandle => ({
getKind: () => 'video',
play: () => {
const p = playerRef.current?.play()
if (pendingResolutionCorrectionRef.current) {
pendingResolutionCorrectionRef.current()
pendingResolutionCorrectionRef.current = null
}
const p = videoRef.current?.play()
if (p !== undefined) p.catch(() => {})
},
pause: () => playerRef.current?.pause(),
pause: () => videoRef.current?.pause(),
togglePlay: () => {
const player = playerRef.current
if (!player) return
if (player.paused() || player.ended()) {
const p = player.play()
const video = videoRef.current
if (!video) return
if (pendingResolutionCorrectionRef.current) {
pendingResolutionCorrectionRef.current()
pendingResolutionCorrectionRef.current = null
}
if (video.paused || video.ended) {
const p = video.play()
if (p !== undefined) p.catch(() => {})
} else {
player.pause()
video.pause()
}
},
seekToFrame: (frame) => {
if (pendingResolutionCorrectionRef.current) {
pendingResolutionCorrectionRef.current()
pendingResolutionCorrectionRef.current = null
}
setHasStartedPlaying(true)
return seekToFrame(clampFrame(frame))
},
seekToSecond: (second) => {
if (pendingResolutionCorrectionRef.current) {
pendingResolutionCorrectionRef.current()
pendingResolutionCorrectionRef.current = null
}
setHasStartedPlaying(true)
const frame = Math.floor(second * frameRate + 0.45)
seekToFrame(clampFrame(frame))
},
stepFrame: (delta) => {
if (pendingResolutionCorrectionRef.current) {
pendingResolutionCorrectionRef.current()
pendingResolutionCorrectionRef.current = null
}
setHasStartedPlaying(true)
return seekToFrame(clampFrame(currentFrameRef.current + delta))
},
setMuted: (m) => playerRef.current?.muted(m),
setMuted: (m) => {
if (videoRef.current) videoRef.current.muted = m
},
setVolume: (v) => {
const player = playerRef.current
if (!player) return
player.volume(v)
if (v > 0 && player.muted()) player.muted(false)
else if (v === 0 && !player.muted()) player.muted(true)
const video = videoRef.current
if (!video) return
video.volume = v
if (v > 0 && video.muted) video.muted = false
else if (v === 0 && !video.muted) video.muted = true
},
setPlaybackRate: (rate) => {
if (videoRef.current) videoRef.current.playbackRate = rate
},
setPlaybackRate: (rate) => playerRef.current?.playbackRate(rate),
toggleLoop: () => {
const player = playerRef.current
if (!player) return
const next = !player.loop()
player.loop(next)
const video = videoRef.current
if (!video) return
const next = !video.loop
video.loop = next
setIsLooping(next)
},
changeResolution,
@@ -512,14 +665,21 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
transformOrigin: '0 0',
}}
>
<div
ref={videoContainerRef}
className="w-full h-full [&_.video-js]:!w-full [&_.video-js]:!h-full [&_video]:!w-full [&_video]:!h-full [&_video]:!block [&_video]:!object-contain"
<video
ref={videoRef}
className="w-full h-full block object-contain pointer-events-none"
playsInline
preload="auto"
/>
</div>
)}
{isAudio && (
<div ref={videoContainerRef} className="absolute inset-0 pointer-events-none opacity-0" />
<video
ref={videoRef}
className="absolute inset-0 pointer-events-none opacity-0"
playsInline
preload="auto"
/>
)}
{isAudio ? (
@@ -190,7 +190,9 @@ export const MobileVideoControlBar: React.FC<ControlBarProps> = ({
<span>
{!isAudio && state.currentResolution
? state.currentResolution === 'Auto'
? m.quality_auto()
? state.activeAutoResolution
? `${m.quality_auto()} (${state.activeAutoResolution})`
: m.quality_auto()
: state.currentResolution === 'Original'
? m.original()
: state.currentResolution
@@ -234,13 +236,17 @@ export const MobileVideoControlBar: React.FC<ControlBarProps> = ({
const isSelected =
state.currentResolution === res.resolution &&
Boolean(state.isCurrentHdr) === Boolean(res.hdr)
const isManualDisabled =
Boolean(data.media?.isHls) && state.isHlsManualSupported === false
return (
<DropdownMenuItem
key={itemKey}
onClick={() => changeResolution(res)}
disabled={isManualDisabled}
onClick={() => !isManualDisabled && changeResolution(res)}
className={cn(
'flex w-full items-center justify-between text-xs cursor-pointer',
'flex w-full items-center justify-between text-xs',
isSelected ? 'text-primary font-medium' : 'text-foreground',
isManualDisabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer',
)}
>
<div className="flex items-center gap-1.5">
@@ -44,6 +44,8 @@ export interface PlayerState {
currentResolution: string // 'Original' or '720p', '480p' etc.
currentSrc?: string
isCurrentHdr?: boolean
activeAutoResolution?: string
isHlsManualSupported?: boolean
}
export interface DisplayTranscode {
@@ -332,7 +334,9 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
<span className="flex items-center gap-1">
<span>
{state.currentResolution === 'Auto'
? m.quality_auto()
? state.activeAutoResolution
? `${m.quality_auto()} (${state.activeAutoResolution})`
: m.quality_auto()
: state.currentResolution === 'Original'
? m.original()
: state.currentResolution}
@@ -372,13 +376,17 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
const isSelected =
state.currentResolution === res.resolution &&
Boolean(state.isCurrentHdr) === Boolean(res.hdr)
const isManualDisabled =
Boolean(data.media?.isHls) && state.isHlsManualSupported === false
return (
<DropdownMenuItem
key={itemKey}
onClick={() => changeResolution(res)}
disabled={isManualDisabled}
onClick={() => !isManualDisabled && changeResolution(res)}
className={cn(
'flex w-full items-center justify-between',
isSelected ? 'text-primary font-medium' : 'text-foreground',
isManualDisabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer',
)}
>
<div className="flex items-center gap-1.5">
@@ -1,28 +1,52 @@
// @vitest-environment happy-dom
import { act, cleanup, render } from '@testing-library/react'
import { act, cleanup, fireEvent, render } from '@testing-library/react'
import React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import VideoViewer from './video-viewer'
import type { AssetInfo } from '@shumai/dtos'
const videojsMock = vi.fn((...args: unknown[]) => {
void args
return {
const { mockHlsInstance, mockHlsConstructor } = vi.hoisted(() => {
const instance = {
attachMedia: vi.fn(),
loadSource: vi.fn(),
on: vi.fn(),
volume: vi.fn(),
duration: vi.fn(() => 10),
currentTime: vi.fn(() => 0),
bufferedEnd: vi.fn(() => 0),
playbackRate: vi.fn(() => 1),
ready: vi.fn(),
dispose: vi.fn(),
isDisposed: vi.fn(() => false),
off: vi.fn(),
once: vi.fn(),
destroy: vi.fn(),
levels: [
{ height: 1080, width: 1920, bitrate: 5000000 },
{ height: 720, width: 1280, bitrate: 2500000 },
],
currentLevel: -1,
startLoad: vi.fn(),
recoverMediaError: vi.fn(),
}
const constructor = vi.fn(function () {
return instance
})
return { mockHlsInstance: instance, mockHlsConstructor: constructor }
})
vi.mock('video.js', () => ({
default: (...args: unknown[]) => videojsMock(...args),
}))
vi.mock('hls.js', () => {
const MockHls = mockHlsConstructor
// @ts-expect-error adding static mock property
MockHls.isSupported = vi.fn(() => true)
// @ts-expect-error adding static mock property
MockHls.Events = {
MANIFEST_PARSED: 'hlsManifestParsed',
LEVEL_SWITCHED: 'hlsLevelSwitched',
ERROR: 'hlsError',
}
// @ts-expect-error adding static mock property
MockHls.ErrorTypes = {
NETWORK_ERROR: 'networkError',
MEDIA_ERROR: 'mediaError',
OTHER_ERROR: 'otherError',
}
return {
default: MockHls,
}
})
vi.mock('@/ui/components/drawing-canvas', () => ({
default: () => <div data-testid="drawing-canvas" />,
@@ -34,7 +58,8 @@ vi.mock('@/ui/paraglide/messages.js', () => ({
describe('VideoViewer', () => {
beforeEach(() => {
videojsMock.mockClear()
vi.clearAllMocks()
mockHlsInstance.currentLevel = -1
})
afterEach(() => {
@@ -88,39 +113,20 @@ describe('VideoViewer', () => {
},
} as unknown as AssetInfo
const { rerender } = render(<VideoViewer file={videoA} />)
const { container, rerender } = render(<VideoViewer file={videoA} />)
const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement
// Initial render should initialize videojs with video A URL
expect(videojsMock).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
sources: [
{
src: 'https://cdn.example.com/videoA-720p.mp4',
type: 'video/mp4',
},
],
}),
)
// Initial render should set video element src with video A URL
expect(video.src).toBe('https://cdn.example.com/videoA-720p.mp4')
// Rerender with video B (simulating selecting another video from the carousel)
rerender(<VideoViewer file={videoB} />)
// VideoJS should be re-initialized with video B's URL
expect(videojsMock).toHaveBeenLastCalledWith(
expect.anything(),
expect.objectContaining({
sources: [
{
src: 'https://cdn.example.com/videoB-1080p.mp4',
type: 'video/mp4',
},
],
}),
)
// Native video element should now have video B's URL
expect(video.src).toBe('https://cdn.example.com/videoB-1080p.mp4')
})
it('initializes VideoJS when transcode URL becomes available after initially empty with same ID', () => {
it('initializes video when transcode URL becomes available after initially empty with same ID', () => {
const initialVideo: AssetInfo = {
id: 'video-pending',
name: 'pending.mp4',
@@ -136,7 +142,6 @@ describe('VideoViewer', () => {
videoTranscodes: [
{
resolution: '1080p',
// URL not yet presigned (e.g. from file list payload)
url: undefined as unknown as string,
width: 1920,
height: 1080,
@@ -160,27 +165,17 @@ describe('VideoViewer', () => {
},
} as unknown as AssetInfo
const { rerender } = render(<VideoViewer file={initialVideo} autoPlay={true} />)
const { container, rerender } = render(<VideoViewer file={initialVideo} autoPlay={true} />)
const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement
// Initially targetSrc is empty, videojs should not be initialized
expect(videojsMock).not.toHaveBeenCalled()
// Initially targetSrc is empty, video.src should be empty
expect(video.src).toBe('')
// Rerender with detailedVideo (same id, but signed URL now available)
rerender(<VideoViewer file={detailedVideo} autoPlay={true} />)
// VideoJS should now be initialized with the new URL
expect(videojsMock).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
autoplay: false,
sources: [
{
src: 'https://cdn.example.com/signed-pending-1080p.mp4',
type: 'video/mp4',
},
],
}),
)
// Video element should now have the newly available signed URL
expect(video.src).toBe('https://cdn.example.com/signed-pending-1080p.mp4')
})
it('auto-selects HDR proxy when display supports HDR (dynamic-range: high)', () => {
@@ -227,19 +222,10 @@ describe('VideoViewer', () => {
},
} as unknown as AssetInfo
render(<VideoViewer file={hdrVideo} />)
const { container } = render(<VideoViewer file={hdrVideo} />)
const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement
expect(videojsMock).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
sources: [
{
src: 'https://cdn.example.com/video-1080p-hdr.mp4',
type: 'video/mp4',
},
],
}),
)
expect(video.src).toBe('https://cdn.example.com/video-1080p-hdr.mp4')
window.matchMedia = originalMatchMedia
})
@@ -288,39 +274,15 @@ describe('VideoViewer', () => {
},
} as unknown as AssetInfo
render(<VideoViewer file={hdrVideo} />)
const { container } = render(<VideoViewer file={hdrVideo} />)
const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement
expect(videojsMock).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
sources: [
{
src: 'https://cdn.example.com/video-1080p.mp4',
type: 'video/mp4',
},
],
}),
)
expect(video.src).toBe('https://cdn.example.com/video-1080p.mp4')
window.matchMedia = originalMatchMedia
})
it('shows play button and darkened overlay initially, but never again after playing (even when paused or scrubbed back to start)', () => {
const listeners: Record<string, () => void> = {}
videojsMock.mockImplementation(() => ({
on: vi.fn((event: string, cb: () => void) => {
listeners[event] = cb
}),
volume: vi.fn(),
duration: vi.fn(() => 10),
currentTime: vi.fn(() => 0),
bufferedEnd: vi.fn(() => 0),
playbackRate: vi.fn(() => 1),
ready: vi.fn(),
dispose: vi.fn(),
isDisposed: vi.fn(() => false),
}))
const testVideo: AssetInfo = {
id: 'test-video-lifecycle',
name: 'test.mp4',
@@ -346,27 +308,191 @@ describe('VideoViewer', () => {
const { container } = render(<VideoViewer file={testVideo} />)
const videoArea = container.querySelector('[data-testid="video-area"]')
const video = videoArea?.querySelector('video') as HTMLVideoElement
expect(videoArea).toBeTruthy()
expect(video).toBeTruthy()
// 1. Initially (before playing): play button & 20% black tint are present
expect(videoArea?.querySelector('.bg-black\\/20')).not.toBeNull()
// 2. Start playback
act(() => {
listeners['play']?.()
video.dispatchEvent(new Event('play'))
})
expect(videoArea?.querySelector('.bg-black\\/20')).toBeNull()
// 3. Pause video: overlay must NOT show again
act(() => {
listeners['pause']?.()
video.dispatchEvent(new Event('pause'))
})
expect(videoArea?.querySelector('.bg-black\\/20')).toBeNull()
// 4. Drag seekbar / time back to 0: overlay must STILL NOT show
act(() => {
listeners['timeupdate']?.()
video.dispatchEvent(new Event('timeupdate'))
})
expect(videoArea?.querySelector('.bg-black\\/20')).toBeNull()
})
it('initializes Hls.js with capLevelToPlayerSize false when video is HLS', () => {
const hlsVideo: AssetInfo = {
id: 'video-hls',
name: 'video-hls.m3u8',
proxyType: 'video',
media: {
isHls: true,
hls: {
key: 'hls-key',
url: 'https://cdn.example.com/master.m3u8',
resolutions: [
{ width: 1920, height: 1080, resolution: '1080p' },
{ width: 1280, height: 720, resolution: '720p' },
],
},
metadata: {
originalWidth: 1920,
originalHeight: 1080,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
},
} as unknown as AssetInfo
const { container } = render(<VideoViewer file={hlsVideo} />)
const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement
expect(mockHlsConstructor).toHaveBeenCalledWith(
expect.objectContaining({
autoStartLoad: true,
startLevel: -1,
capLevelToPlayerSize: false,
enableWorker: true,
}),
)
expect(mockHlsInstance.attachMedia).toHaveBeenCalledWith(video)
expect(mockHlsInstance.loadSource).toHaveBeenCalledWith('https://cdn.example.com/master.m3u8')
})
it('switches HLS resolution immediately using currentLevel', () => {
const hlsVideo: AssetInfo = {
id: 'video-hls-switch',
name: 'video-hls-switch.m3u8',
proxyType: 'video',
media: {
isHls: true,
hls: {
key: 'hls-key',
url: 'https://cdn.example.com/master.m3u8',
resolutions: [
{ width: 1920, height: 1080, resolution: '1080p' },
{ width: 1280, height: 720, resolution: '720p' },
],
},
metadata: {
originalWidth: 1920,
originalHeight: 1080,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
},
} as unknown as AssetInfo
const { container } = render(<VideoViewer file={hlsVideo} />)
// Open settings / quality dropdown
const settingsButton = container.querySelector('button:has(.lucide-settings)')
expect(settingsButton).toBeTruthy()
fireEvent.pointerDown(settingsButton!, { pointerType: 'mouse', button: 0 })
fireEvent.click(settingsButton!)
// Click 720p (index 1 in levels: 1080p is index 0, 720p is index 1)
const items = document.querySelectorAll('[role="menuitem"]')
const item720p = Array.from(items).find((el) => el.textContent?.includes('720p'))
expect(item720p).toBeTruthy()
act(() => {
fireEvent.click(item720p!)
})
// currentLevel should be set immediately to 1 (720p)
expect(mockHlsInstance.currentLevel).toBe(1)
// Click Auto
const itemAuto = Array.from(document.querySelectorAll('[role="menuitem"]')).find(
(el) => el.textContent?.includes('Auto') || el.textContent === '',
)
if (itemAuto) {
act(() => {
fireEvent.click(itemAuto)
})
// Auto should reset currentLevel to -1
expect(mockHlsInstance.currentLevel).toBe(-1)
}
})
it('does not hijack subsequent seeks if paused HLS switch did not emit seeked', () => {
vi.useFakeTimers()
try {
const hlsVideo: AssetInfo = {
id: 'video-hls-stale-seek',
name: 'video-hls-stale-seek.m3u8',
proxyType: 'video',
media: {
isHls: true,
hls: {
key: 'hls-key',
url: 'https://cdn.example.com/master.m3u8',
resolutions: [
{ width: 1920, height: 1080, resolution: '1080p' },
{ width: 1280, height: 720, resolution: '720p' },
],
},
metadata: {
originalWidth: 1920,
originalHeight: 1080,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
},
} as unknown as AssetInfo
const { container } = render(<VideoViewer file={hlsVideo} />)
const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement
// Video is paused at 0s (frame 0)
Object.defineProperty(video, 'paused', { value: true, configurable: true })
video.currentTime = 0
// Open settings and click 720p
const settingsButton = container.querySelector('button:has(.lucide-settings)')
fireEvent.pointerDown(settingsButton!, { pointerType: 'mouse', button: 0 })
fireEvent.click(settingsButton!)
const item720p = Array.from(document.querySelectorAll('[role="menuitem"]')).find((el) =>
el.textContent?.includes('720p'),
)
act(() => {
fireEvent.click(item720p!)
})
// Simulate that no seeked event occurred within the timeout (e.g. 1500ms elapses)
act(() => {
vi.advanceTimersByTime(2000)
})
// Now user performs an unrelated seek to frame 150 (currentTime = 5.0)
video.currentTime = 5.0
act(() => {
fireEvent(video, new Event('seeked'))
})
// video.currentTime must remain at frame 150, NOT rewound to frame 0
expect(Math.floor(video.currentTime * 30 + 0.001)).toBe(150)
expect(video.currentTime).toBeCloseTo(5.0167, 3)
} finally {
vi.useRealTimers()
}
})
})
@@ -2,10 +2,9 @@ import { client } from '@/ui/api/client'
import { cn } from '@/ui/lib/utils'
import { Play, AudioLines } from 'lucide-react'
import React, { useCallback, useEffect, useRef, useState, useImperativeHandle } from 'react'
import videojs from 'video.js'
import type Player from 'video.js/dist/types/player'
import Hls from 'hls.js'
import { useFramePlayer } from './use-frame-player'
import { resolveTotalFrames } from './utils'
import { calculateFrameCenterTime, resolveTotalFrames } from './utils'
import { VideoControlBar, type PlayerState, type DisplayTranscode } from './video-control-bar'
import { MobileVideoControlBar } from './mobile-video-control-bar'
import { useIsMobile } from '@/ui/hooks/use-mobile'
@@ -31,8 +30,10 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
},
ref,
) => {
const localPlayerRef = useRef<Player | null>(null)
const playerRef = localPlayerRef
const hlsRef = useRef<Hls | null>(null)
const videoRef = useRef<HTMLVideoElement | null>(null)
const [activeAutoResolution, setActiveAutoResolution] = useState<string | undefined>(undefined)
const [isHlsManualSupported, setIsHlsManualSupported] = useState<boolean>(true)
const isHls = Boolean(data.media?.isHls && data.media.hls?.url)
const baseTranscodes = isHls
? (data.media?.hls?.resolutions ?? [])
@@ -59,8 +60,6 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
// Only transcoded proxy versions are ever displayed; the raw original file
// is never used as a playback source.
const hasMedia = (isHls || resolutions.length > 0) && !!data.media?.metadata
// We use a container ref to manually append the video element
const videoContainerRef = useRef<HTMLDivElement>(null)
// Logic to select best resolution based on screen size and dynamic range
const getInitialResolution = (): DisplayTranscode | null => {
@@ -120,35 +119,38 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
const containerRef = useRef<HTMLDivElement>(null)
const rootRef = useRef<HTMLDivElement>(null)
const controlsTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
const pendingResolutionCorrectionRef = useRef<(() => void) | null>(null)
useImperativeHandle(ref, () => ({
play: () => {
if (localPlayerRef.current) {
localPlayerRef.current.play()
if (pendingResolutionCorrectionRef.current) {
pendingResolutionCorrectionRef.current()
pendingResolutionCorrectionRef.current = null
}
videoRef.current?.play().catch(() => {})
},
pause: () => {
if (localPlayerRef.current) {
localPlayerRef.current.pause()
}
videoRef.current?.pause()
},
seekTo: (second: number) => {
if (pendingResolutionCorrectionRef.current) {
pendingResolutionCorrectionRef.current()
pendingResolutionCorrectionRef.current = null
}
setHasStartedPlaying(true)
if (localPlayerRef.current) {
localPlayerRef.current.currentTime(second)
if (videoRef.current) {
videoRef.current.currentTime = second
}
},
getCurrentTime: () => {
return (localPlayerRef.current ? localPlayerRef.current.currentTime() : 0) ?? 0
return (videoRef.current ? videoRef.current.currentTime : 0) ?? 0
},
getDuration: () => {
return (localPlayerRef.current ? localPlayerRef.current.duration() : 0) ?? 0
return (videoRef.current ? videoRef.current.duration : 0) ?? 0
},
}))
const isOverControlsRef = useRef(false)
const [videoHtmlEl, setVideoHtmlEl] = useState<HTMLVideoElement | undefined>(undefined)
const videoRef = useRef<HTMLVideoElement | null>(null)
const [zoom, setZoom] = useState(1)
const [pan, setPan] = useState({ x: 0, y: 0 })
const [hasManuallyZoomed, setHasManuallyZoomed] = useState(false)
@@ -166,6 +168,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
currentSrc: isHls ? (data.media?.hls?.url ?? '') : (res?.url ?? ''),
isCurrentHdr: res?.hdr ?? false,
}))
setActiveAutoResolution(undefined)
setHasManuallyZoomed(false)
setIsPlayerReady(false)
setBuffered(0)
@@ -274,6 +277,10 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
const handleSeekToFrame = useCallback(
(frame: number) => {
if (pendingResolutionCorrectionRef.current) {
pendingResolutionCorrectionRef.current()
pendingResolutionCorrectionRef.current = null
}
setHasStartedPlaying(true)
return seekToFrame(frame)
},
@@ -289,6 +296,8 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
currentTime,
duration,
progress,
activeAutoResolution,
isHlsManualSupported,
}
// Trigger time update event reactively
@@ -298,146 +307,199 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
}
}, [currentFrame, frameRate, onTimeUpdate])
// Initialize Video.js
// Initialize player (Hls.js or native HTML5 video)
const targetSrc = isHls ? (data.media?.hls?.url ?? '') : (getInitialResolution()?.url ?? '')
useEffect(() => {
if (!videoContainerRef.current) return
const video = videoRef.current
if (!video) return
const targetSrc = isHls ? (data.media?.hls?.url ?? '') : (getInitialResolution()?.url ?? '')
const targetType = isHls ? 'application/x-mpegURL' : 'video/mp4'
if (!targetSrc) return
// Clean up previous player if exists
if (playerRef.current && !playerRef.current.isDisposed()) {
playerRef.current.dispose()
playerRef.current = null
videoRef.current = null
// Clean up previous HLS instance if exists
if (hlsRef.current) {
hlsRef.current.destroy()
hlsRef.current = null
}
// 1. Create the video element manually
// This avoids conflicts with React Strict Mode where the DOM might not match React's virtual DOM expectations after Video.js modifies it.
const videoElement = document.createElement('video-js')
videoElement.classList.add('vjs-big-play-centered', '!h-full', '!w-full')
setIsPlayerReady(false)
setActiveAutoResolution(undefined)
// Keep it in DOM, enable visible rendering
videoElement.style.pointerEvents = 'none'
const startAutoPlay = () => {
const playPromise = video.play()
if (playPromise !== undefined) {
playPromise.catch(() => {
video.muted = true
setState((prev) => ({ ...prev, isMuted: true }))
video.play()?.catch(() => {})
})
}
}
// Append to our container
videoContainerRef.current.appendChild(videoElement)
let hls: Hls | null = null
// 2. Initialize the player
const player = (playerRef.current = videojs(videoElement, {
controls: false,
autoplay: false,
preload: 'auto',
playsinline: true,
sources: [
{
src: targetSrc,
type: targetType,
},
],
}))
if (isHls) {
if (Hls.isSupported()) {
setIsHlsManualSupported(true)
hls = new Hls({
autoStartLoad: true,
startLevel: -1,
capLevelToPlayerSize: false,
enableWorker: true,
})
hlsRef.current = hls
hls.attachMedia(video)
hls.loadSource(targetSrc)
if (autoPlay) {
const startAutoPlay = () => {
if (player.isDisposed()) return
const playPromise = player.play()
if (playPromise !== undefined) {
playPromise.catch(() => {
if (player.isDisposed()) return
player.muted(true)
setState((prev) => ({ ...prev, isMuted: true }))
player.play()?.catch(() => {})
})
hls.on(Hls.Events.MANIFEST_PARSED, () => {
setIsPlayerReady(true)
if (autoPlay) {
if (video.readyState >= 2) {
startAutoPlay()
} else {
video.addEventListener('canplay', startAutoPlay, { once: true })
}
}
})
hls.on(Hls.Events.LEVEL_SWITCHED, (_event, eventData) => {
const level = hls?.levels[eventData.level]
if (level) {
const h = level.height || Math.min(level.width, level.height)
setActiveAutoResolution(h ? `${h}p` : undefined)
}
})
hls.on(Hls.Events.ERROR, (_event, eventData) => {
if (eventData.fatal) {
switch (eventData.type) {
case Hls.ErrorTypes.NETWORK_ERROR:
hls?.startLoad()
break
case Hls.ErrorTypes.MEDIA_ERROR:
hls?.recoverMediaError()
break
default:
hls?.destroy()
break
}
}
})
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
// Native Safari HLS fallback
setIsHlsManualSupported(false)
video.src = targetSrc
if (autoPlay) {
if (video.readyState >= 2) {
startAutoPlay()
} else {
video.addEventListener('canplay', startAutoPlay, { once: true })
}
}
}
player.ready(() => {
if (player.isDisposed()) return
if (player.readyState() >= 2) {
} else {
// Native MP4
video.src = targetSrc
if (autoPlay) {
if (video.readyState >= 2) {
startAutoPlay()
} else {
player.one('canplay', startAutoPlay)
video.addEventListener('canplay', startAutoPlay, { once: true })
}
})
}
}
// Extract video element for Konva
const htmlVid = videoElement.querySelector('video')
if (htmlVid) {
setVideoHtmlEl(htmlVid)
videoRef.current = htmlVid
} else {
// Fallback: try player.tech().el()
// Wait for ready?
player.ready(() => {
const techEl = player.tech({ iWillNotUseThisInPlugins: true })?.el() as HTMLVideoElement
if (techEl) {
setVideoHtmlEl(techEl)
videoRef.current = techEl
}
})
}
player.on('play', () => {
const handlePlay = () => {
if (pendingResolutionCorrectionRef.current) {
pendingResolutionCorrectionRef.current()
pendingResolutionCorrectionRef.current = null
}
setState((p) => ({ ...p, isPlaying: true }))
setHasStartedPlaying(true)
onPlay?.()
})
player.on('pause', () => {
}
const handlePause = () => {
setState((p) => ({ ...p, isPlaying: false }))
onPause?.()
})
player.on('ended', () => setState((p) => ({ ...p, isPlaying: false })))
player.on('loadedmetadata', () => {
}
const handleEnded = () => {
setState((p) => ({ ...p, isPlaying: false }))
}
const handleLoadedMetadata = () => {
setIsPlayerReady(true)
})
player.on('loadstart', () => {
}
const handleLoadStart = () => {
setIsPlayerReady(false)
})
player.on('timeupdate', () => {
const playerDuration = player.duration() || data.media?.metadata?.duration || 0
const bufferedEnd = player.bufferedEnd()
if (playerDuration > 0) {
setBuffered((bufferedEnd / playerDuration) * 100)
}
})
player.on('volumechange', () => {
const vol = player.volume()
const muted = player.muted()
setState((prev) => ({
...prev,
volume: vol || 0,
isMuted: muted || false,
}))
})
player.on('ratechange', () => {
setState((prev) => ({
...prev,
playbackRate: player.playbackRate() || 1,
}))
})
player.on('error', () => {
console.error('VideoJS Error:', player.error())
})
// Set initial state
player.volume(state.volume)
// 4. Cleanup
return () => {
if (player && !player.isDisposed()) {
player.dispose()
playerRef.current = null
videoRef.current = null
}
const handleProgress = () => {
const vidDuration = video.duration || data.media?.metadata?.duration || 0
if (vidDuration > 0 && video.buffered.length > 0) {
let bufferedEnd = 0
for (let i = 0; i < video.buffered.length; i++) {
if (
video.buffered.start(i) <= video.currentTime &&
video.currentTime <= video.buffered.end(i)
) {
bufferedEnd = video.buffered.end(i)
break
}
}
if (bufferedEnd === 0) {
bufferedEnd = video.buffered.end(video.buffered.length - 1)
}
setBuffered((bufferedEnd / vidDuration) * 100)
}
}
}, [data.id, Boolean(isHls ? data.media?.hls?.url : initialRes?.url), autoPlay])
const handleVolumeChange = () => {
setState((prev) => ({
...prev,
volume: video.volume,
isMuted: video.muted,
}))
}
const handleRateChange = () => {
setState((prev) => ({
...prev,
playbackRate: video.playbackRate,
}))
}
const handleError = () => {
console.error('Video Error:', video.error)
}
video.addEventListener('play', handlePlay)
video.addEventListener('pause', handlePause)
video.addEventListener('ended', handleEnded)
video.addEventListener('loadedmetadata', handleLoadedMetadata)
video.addEventListener('loadstart', handleLoadStart)
video.addEventListener('timeupdate', handleProgress)
video.addEventListener('progress', handleProgress)
video.addEventListener('volumechange', handleVolumeChange)
video.addEventListener('ratechange', handleRateChange)
video.addEventListener('error', handleError)
// Set initial state
video.volume = state.volume
// Cleanup
return () => {
video.removeEventListener('play', handlePlay)
video.removeEventListener('pause', handlePause)
video.removeEventListener('ended', handleEnded)
video.removeEventListener('loadedmetadata', handleLoadedMetadata)
video.removeEventListener('loadstart', handleLoadStart)
video.removeEventListener('timeupdate', handleProgress)
video.removeEventListener('progress', handleProgress)
video.removeEventListener('volumechange', handleVolumeChange)
video.removeEventListener('ratechange', handleRateChange)
video.removeEventListener('error', handleError)
if (hlsRef.current) {
hlsRef.current.destroy()
hlsRef.current = null
}
}
}, [data.id, targetSrc, isHls, autoPlay])
// Handle changes to startTime (e.g., clicking different chunks in search results)
useEffect(() => {
@@ -514,11 +576,16 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
// Disable click-to-play if drawing
if (useAnnotationStore.getState().isDrawing) return
const player = playerRef.current
if (!player) return
if (pendingResolutionCorrectionRef.current) {
pendingResolutionCorrectionRef.current()
pendingResolutionCorrectionRef.current = null
}
if (player.paused() || player.ended()) {
const playPromise = player.play()
const video = videoRef.current
if (!video) return
if (video.paused || video.ended) {
const playPromise = video.play()
if (playPromise !== undefined) {
playPromise.catch((error) => {
// Ignore AbortError which happens when pausing rapidly after playing
@@ -527,7 +594,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
})
}
} else {
player.pause()
video.pause()
}
}, [])
@@ -573,10 +640,10 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
}, [togglePlay])
const toggleLoop = () => {
const player = playerRef.current
if (!player) return
const video = videoRef.current
if (!video) return
const newLoop = !state.isLooping
player.loop(newLoop)
video.loop = newLoop
setState((prev) => ({ ...prev, isLooping: newLoop }))
}
@@ -588,76 +655,121 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
isMuted: newVolume === 0, // If dragging to 0, consider it muted
}))
const player = playerRef.current
if (!player) return
const video = videoRef.current
if (!video) return
// Set volume first so if we unmute, it is at the correct level
player.volume(newVolume)
video.volume = newVolume
// Manage mute state based on volume
if (newVolume > 0 && player.muted()) {
player.muted(false)
} else if (newVolume === 0 && !player.muted()) {
player.muted(true)
if (newVolume > 0 && video.muted) {
video.muted = false
} else if (newVolume === 0 && !video.muted) {
video.muted = true
}
}
const toggleMute = () => {
const player = playerRef.current
if (!player) return
const video = videoRef.current
if (!video) return
const isMuted = player.muted()
const isMuted = video.muted
if (isMuted) {
player.muted(false)
video.muted = false
// If volume was 0 (e.g. user dragged to 0), restore to default 0.5 so they hear something
if (player.volume() === 0) {
player.volume(0.5)
if (video.volume === 0) {
video.volume = 0.5
}
} else {
player.muted(true)
video.muted = true
}
}
const changePlaybackRate = (rate: number) => {
const player = playerRef.current
if (!player) return
player.playbackRate(rate)
const video = videoRef.current
if (!video) return
video.playbackRate = rate
setState((prev) => ({ ...prev, playbackRate: rate }))
}
const changeResolution = (res: DisplayTranscode) => {
const player = playerRef.current
if (!player) return
const video = videoRef.current
if (!video) return
if (pendingResolutionCorrectionRef.current) {
pendingResolutionCorrectionRef.current()
pendingResolutionCorrectionRef.current = null
}
if (isHls && hlsRef.current) {
const hls = hlsRef.current
const wasPaused = video.paused
const targetFrame = currentFrame
if (isHls) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ql = (player as any).qualityLevels?.()
if (res.resolution === 'Auto') {
if (ql) {
for (let i = 0; i < ql.length; i++) {
ql[i].enabled = true
}
if (state.currentResolution === 'Auto' && hls.currentLevel === -1) {
return
}
hls.currentLevel = -1
setState((prev) => ({
...prev,
currentResolution: 'Auto',
}))
} else {
if (ql) {
for (let i = 0; i < ql.length; i++) {
ql[i].enabled = ql[i].height === res.height
const targetIndex = hls.levels.findIndex(
(lvl) =>
lvl.height === res.height ||
Math.max(lvl.width, lvl.height) === Math.max(res.width, res.height),
)
if (targetIndex !== -1) {
if (targetIndex === hls.currentLevel && state.currentResolution === res.resolution) {
return
}
hls.currentLevel = targetIndex
setState((prev) => ({
...prev,
currentResolution: res.resolution,
isCurrentHdr: res.hdr ?? false,
}))
}
}
// Guarantee frame accuracy if paused: re-nudge to safe center time once fragment is decoded
if (wasPaused) {
let timer: ReturnType<typeof setTimeout> | null = null
const cleanup = () => {
if (timer) {
clearTimeout(timer)
timer = null
}
video.removeEventListener('seeked', onSeeked)
hls.off?.(Hls.Events.ERROR, cleanup)
if (pendingResolutionCorrectionRef.current === cleanup) {
pendingResolutionCorrectionRef.current = null
}
}
setState((prev) => ({
...prev,
currentResolution: res.resolution,
isCurrentHdr: res.hdr ?? false,
}))
const onSeeked = () => {
cleanup()
const safeCenterTime = calculateFrameCenterTime(targetFrame, frameRate)
const frameDuration = 1 / frameRate
if (Math.abs(video.currentTime - safeCenterTime) > frameDuration / 4) {
video.currentTime = safeCenterTime
}
}
// Safety timeout (1.5s): if no seeked event fires from rendition switch, cleanly detach
timer = setTimeout(cleanup, 1500)
pendingResolutionCorrectionRef.current = cleanup
video.addEventListener('seeked', onSeeked)
hls.once?.(Hls.Events.ERROR, cleanup)
}
return
}
const wasPlaying = !player.paused()
const currentT = player.currentTime()
const wasPlaying = !video.paused
const currentT = video.currentTime
setState((prev) => ({
...prev,
@@ -666,26 +778,27 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
isCurrentHdr: res.hdr ?? false,
}))
player.src({ type: 'video/mp4', src: res.url })
video.src = res.url || ''
player.one('loadedmetadata', () => {
player.currentTime(currentT)
const onLoadedMetadata = () => {
video.removeEventListener('loadedmetadata', onLoadedMetadata)
video.currentTime = currentT
if (wasPlaying) {
const playPromise = player.play()
const playPromise = video.play()
if (playPromise !== undefined) {
playPromise.catch((error) => {
if (error.name === 'AbortError') return
console.error('Play after seek failed:', error)
if (error.name !== 'AbortError') console.error('Play after seek failed:', error)
})
}
}
player.playbackRate(state.playbackRate)
})
video.playbackRate = state.playbackRate
}
video.addEventListener('loadedmetadata', onLoadedMetadata)
}
const toggleFullScreen = () => {
const rootEl = rootRef.current
const videoEl = videoHtmlEl || videoRef.current
const videoEl = videoRef.current
// 1. Standard W3C Fullscreen API (Desktop Chrome/Firefox/Safari, Android, iPadOS)
if (rootEl && typeof rootEl.requestFullscreen === 'function') {
@@ -763,7 +876,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
document.addEventListener('fullscreenchange', onFsChange)
// iOS WebKit fullscreen events on <video>
const videoEl = videoHtmlEl || videoRef.current
const videoEl = videoRef.current
const onWebkitBeginFs = () => {
setState((prev) => ({ ...prev, isFullScreen: true }))
}
@@ -782,7 +895,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
videoEl.removeEventListener('webkitendfullscreen', onWebkitEndFs)
}
}
}, [videoHtmlEl])
}, [])
// Center pan is owned by `pan` state (set by auto-fit / gestures).
const scale = zoom
@@ -819,7 +932,6 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
'flex-1 bg-black cursor-pointer relative flex items-center justify-center overflow-hidden min-h-0 touch-none',
)}
onClick={togglePlay}
data-vjs-player
data-testid="video-area"
>
{/* Native Video Layer (Hardware-accelerated, full HDR EDR) */}
@@ -835,16 +947,20 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
transformOrigin: '0 0',
}}
>
<div
ref={videoContainerRef}
className="w-full h-full [&_.video-js]:!w-full [&_.video-js]:!h-full [&_video]:!w-full [&_video]:!h-full [&_video]:!block [&_video]:!object-contain"
<video
ref={videoRef}
className="w-full h-full block object-contain pointer-events-none"
playsInline
preload="auto"
/>
</div>
)}
{isAudio && (
<div
ref={videoContainerRef}
<video
ref={videoRef}
className="absolute inset-0 pointer-events-none opacity-0"
playsInline
preload="auto"
/>
)}
+13 -6
View File
@@ -19,7 +19,7 @@ Shumai's video player is a decoupled, frame-locked system that coordinates three
▼ │
┌────────────────────────────┴──┐
│ Native HTML5 Video Player │
│ (VideoJS Engine Wrapper) │
│ (Hls.js / Native MSE) │
└───────────────────────────────┘
▲ ▲
│ │
@@ -32,7 +32,7 @@ Shumai's video player is a decoupled, frame-locked system that coordinates three
└──────────────┘ └──────────────┘
```
1. **Native HTML5 Video Player (VideoJS Engine Wrapper):** Manages the underlying browser media stream, buffering, audio clock, and presentation.
1. **Native HTML5 Video Player (Hls.js / Native MSE):** Manages the underlying browser media stream, buffering, audio clock, and presentation. Direct `<video>` element with zero library wrappers for MP4, and lightweight `hls.js` for adaptive HLS streams.
2. **Seekbar (Scrubber & Progress Bar):** Visualizes playback progress and maps horizontal screen clicks/drags to playhead percentages.
3. **Comment Area (Sidebar):** Renders comment list, displays timestamp labels, captures drawings, and triggers seeks to target frames.
@@ -204,14 +204,21 @@ When playing, the playhead loop coordinates two mechanisms in parallel to preven
---
### G. Switch Resolution
#### 1. HLS Streams (`hls.js`)
* **Immediate Rendition Switch (`currentLevel`)**: When the user selects a specific rendition (e.g. `1080p`), the player assigns `hls.currentLevel = targetIndex` rather than `nextLevel`. This causes `hls.js` to immediately flush the MSE buffer around `currentTime` and fetch fragments for the target rendition (matching Frame.io's responsive behavior).
* **Auto Mode**: When selecting `Auto`, `hls.currentLevel = -1` is set to re-enable adaptive bitrate (ABR). The player listens to `Hls.Events.LEVEL_SWITCHED` to update the control bar label dynamically (e.g., `Auto (1080p)`).
* **Paused Frame-Center Re-Nudge**: If the user switches quality while paused on a frame (e.g. Frame 120), the player listens for the subsequent `seeked` event once the new fragment renders and re-nudges `video.currentTime` to `calculateFrameCenterTime(currentFrame, frameRate)`. This guarantees zero frame drift across quality switches.
#### 2. Normal MP4 Streams (Native Video)
1. User selects a different resolution (e.g. `720p` or `Original`).
2. The player intercepts the click:
* Captures `wasPlaying = !player.paused()` and the current playhead time: `currentT = player.currentTime()`.
* Updates the source URL: `player.src({ type: 'video/mp4', src: res.url })`.
* Captures `wasPlaying = !video.paused` and the current playhead time: `currentT = video.currentTime`.
* Updates the source URL: `video.src = res.url`.
3. The player registers a one-shot `loadedmetadata` event listener on the new source:
* Restores playhead time: `player.currentTime(currentT)`.
* Restores playhead time: `video.currentTime = currentT`.
* Restores playback state (`play()` if `wasPlaying` was true) and playback rate.
4. The seek triggered by `player.currentTime(currentT)` fires a `seeked` event, which automatically calls `handleExternalSeeked`, recalculating the frame index and nudging the playhead to the safe frame center on the new media stream.
4. The seek triggered by `video.currentTime = currentT` fires a `seeked` event, which automatically calls `handleExternalSeeked`, recalculating the frame index and nudging the playhead to the safe frame center on the new media stream.
---
-2
View File
@@ -5,8 +5,6 @@
@source "./node_modules/@ferrucc-io/emoji-picker";
@source "../../node_modules/@ferrucc-io/emoji-picker";
@import 'video.js/dist/video-js.css' layer(base);
@custom-variant dark (&:is(.dark *));
:root {
+1 -1
View File
@@ -63,6 +63,7 @@
"cmdk": "1.1.1",
"date-fns": "4.4.0",
"embla-carousel-react": "8.6.0",
"hls.js": "1.7.3",
"hono": "4.13.5",
"input-otp": "1.5.0",
"konva": "10.3.2",
@@ -86,7 +87,6 @@
"ulid": "3.0.2",
"use-image": "1.1.4",
"vaul": "1.1.2",
"video.js": "8.24.0",
"zod": "4.5.4",
"zustand": "5.0.15"
},