diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ea39237..e52cb36c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/bun.lock b/bun.lock index b40f989b..951b6ba1 100644 --- a/bun.lock +++ b/bun.lock @@ -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=="], diff --git a/packages/webui/components/compare/compare-control-bar.tsx b/packages/webui/components/compare/compare-control-bar.tsx index 58dae4e7..e4a19691 100644 --- a/packages/webui/components/compare/compare-control-bar.tsx +++ b/packages/webui/components/compare/compare-control-bar.tsx @@ -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 diff --git a/packages/webui/components/compare/types.ts b/packages/webui/components/compare/types.ts index 2ad44fcf..33574719 100644 --- a/packages/webui/components/compare/types.ts +++ b/packages/webui/components/compare/types.ts @@ -33,6 +33,8 @@ export interface PaneReportedState { isCurrentHdr?: boolean resolutions: DisplayTranscode[] buffered: number + activeAutoResolution?: string + isHlsManualSupported?: boolean } } diff --git a/packages/webui/components/viewers/video/compare-video-pane.test.tsx b/packages/webui/components/viewers/video/compare-video-pane.test.tsx new file mode 100644 index 00000000..7ad2d29e --- /dev/null +++ b/packages/webui/components/viewers/video/compare-video-pane.test.tsx @@ -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: () =>
, +})) + +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 + } + 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 + } + 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() + + const { container } = render( + 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() + + const { container } = render( + 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() + } + }) +}) diff --git a/packages/webui/components/viewers/video/compare-video-pane.tsx b/packages/webui/components/viewers/video/compare-video-pane.tsx index 05ab35a7..ad902a10 100644 --- a/packages/webui/components/viewers/video/compare-video-pane.tsx +++ b/packages/webui/components/viewers/video/compare-video-pane.tsx @@ -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(null) - const videoContainerRef = useRef(null) + const hlsRef = useRef(null) const containerRef = useRef(null) const videoRef = useRef(null) @@ -132,6 +130,9 @@ export const CompareVideoPane = forwardRef(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 { - 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 { - 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 | 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 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 ({ 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 -
)} {isAudio && ( -
+