diff --git a/app/layout.tsx b/app/layout.tsx index 50d8fb7..6916ddf 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -1,4 +1,5 @@ import React, { ReactNode } from 'react'; +import { Analytics } from '@vercel/analytics/next'; import type { Metadata, Viewport } from 'next'; import { Jost } from 'next/font/google'; @@ -71,6 +72,7 @@ const RootLayout = ({ children }: { children: ReactNode }) => {
{children}
+ ); diff --git a/components/Preview/components/Torrents/components/Player/index.tsx b/components/Preview/components/Torrents/components/Player/index.tsx index aae4834..010b03b 100644 --- a/components/Preview/components/Torrents/components/Player/index.tsx +++ b/components/Preview/components/Torrents/components/Player/index.tsx @@ -30,9 +30,10 @@ interface Props { magnet: string; subtitles: Source[]; playlist: Source[]; + onStreamError?: (reason?: string) => void; } -const Player: FC = ({ magnet, playlist, subtitles }) => { +const Player: FC = ({ magnet, playlist, subtitles, onStreamError }) => { const setCanPlay = useSetCanPlay(); const { index, setIndex } = useSource({ magnet }); @@ -50,7 +51,7 @@ const Player: FC = ({ magnet, playlist, subtitles }) => { const source = playlist[index]; - const { tracks: audioTracks, audio, setAudio, resolvedSrc } = useGstAudio(source?.src); + const { tracks: audioTracks, audio, setAudio, resolvedSrc, error } = useGstAudio(source?.src); const resumeTime = useRef(0); @@ -59,7 +60,15 @@ const Player: FC = ({ magnet, playlist, subtitles }) => { setAudio(track); }; - const playerSrc = useMemo(() => (source ? ({ ...source, src: resolvedSrc } as VideoSrc) : []), [source, resolvedSrc]); + // report the transcoder rejection up to the modal and stop hls.js from retrying a dead stream + useEffect(() => { + onStreamError?.(error); + }, [error, onStreamError]); + + const playerSrc = useMemo( + () => (source && !error ? ({ ...source, src: resolvedSrc } as VideoSrc) : []), + [source, resolvedSrc, error] + ); const onProviderChange = (provider: MediaProviderAdapter | null) => { if (isHLSProvider(provider)) { diff --git a/components/Preview/components/Torrents/components/Player/useGstAudio.ts b/components/Preview/components/Torrents/components/Player/useGstAudio.ts index 4f5e802..d636460 100644 --- a/components/Preview/components/Torrents/components/Player/useGstAudio.ts +++ b/components/Preview/components/Torrents/components/Player/useGstAudio.ts @@ -1,6 +1,8 @@ import { useEffect, useState } from 'react'; import { useQuery } from '@tanstack/react-query'; +import useUnplayable from '@/components/Preview/components/Torrents/useUnplayable'; + export interface GstAudioTrack { index: number; label: string; @@ -23,26 +25,42 @@ export const useGstAudio = (src?: string) => { const match = src?.match(GST_RE); const [base, hash, index] = match ? [match[1], match[2], match[3]] : []; + const { markUnplayable } = useUnplayable(); + const [audio, setAudio] = useState(0); useEffect(() => { setAudio(0); }, [hash, index]); - const { data: tracks = [] } = useQuery({ + const { data: tracks = [], error } = useQuery({ enabled: Boolean(hash && index), + // a rejected codec/container won't change on retry + retry: false, queryKey: ['gst-audio', hash, index], - queryFn: () => - fetch(`${base}/gst/${hash}/probe?index=${index}`) - .then((response) => response.json()) - .then((data: { Tracks?: ProbeTrack[] }) => - (data.Tracks ?? []) - .filter((track) => track.Type === 'audio') - .map((track) => ({ index: track.Index, label: label(track) })) - ) + queryFn: async () => { + const response = await fetch(`${base}/gst/${hash}/probe?index=${index}`); + + // the transcoder answers 5xx with a plain-text reason, e.g. "unsupported video codec" + if (!response.ok) throw new Error((await response.text()).trim() || `stream error ${response.status}`); + + const data: { Tracks?: ProbeTrack[] } = await response.json(); + + return (data.Tracks ?? []) + .filter((track) => track.Type === 'audio') + .map((track) => ({ index: track.Index, label: label(track) })); + } }); + const reason = error instanceof Error ? error.message : undefined; + + // codec/container rejections are permanent for this backend — remember them to grey out Play + useEffect(() => { + if (reason && /unsupported (video codec|container)/i.test(reason)) markUnplayable(hash, reason); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [reason, hash]); + const resolvedSrc = match ? `${src}&audio=${audio}` : src; - return { tracks, audio, setAudio, resolvedSrc }; + return { tracks, audio, setAudio, resolvedSrc, error: reason }; }; diff --git a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/components/Player/index.tsx b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/components/Player/index.tsx index 8a17c06..ea3d960 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/components/Player/index.tsx +++ b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/components/Player/index.tsx @@ -1,5 +1,5 @@ +import { useState } from 'react'; import { create, InstanceProps } from 'react-modal-promise'; -import { Loader } from 'lucide-react'; import Player from '@/components/Preview/components/Torrents/components/Player'; import { Modal } from '@/components/ui/dialog'; @@ -22,6 +22,8 @@ const showPlayerModal = create(({ onResolve, hash, backdrop, title, magnet }: Pr const sources = useFiles({ magnet }); + const [streamError, setStreamError] = useState(); + const handleClose = () => { setCanPlay(false); onResolve(); @@ -29,22 +31,39 @@ const showPlayerModal = create(({ onResolve, hash, backdrop, title, magnet }: Pr return ( -
+
- {sources && } + {sources && ( + + )}
- {(!isReady || !canPlay) && ( -
+ {streamError && ( +
+

This release can’t be played in the browser

+

{streamError}

+

+ The server can’t transcode this video. Use the playlist or VLC option to open it externally, or pick + another release. +

+
+ )} + {!streamError && (!isReady || !canPlay) && ( +
{title} + {/* dark cover recedes from the centre out to the top and bottom edges as it preloads */}
+
-
-
- -
-

Buffering: {preloadingProgress.toFixed()}%

@@ -53,6 +72,31 @@ const showPlayerModal = create(({ onResolve, hash, backdrop, title, magnet }: Pr

Download speed: {downloadSpeed || '0 B/s'}

+ {/* a highlight sweeps around the block's 2px border while it preloads */} + +
)}
diff --git a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx index 6c3a126..11e8685 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx +++ b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx @@ -11,6 +11,7 @@ import { useStreamUrl } from '@/hooks/useStreamUrl'; import { cn, detectSafari, getMagnetHash, isStandaloneApp, torrentKey } from '@/lib/utils'; import usePinnedTorrents from '../../../../usePinnedTorrents'; +import useUnplayable from '../../../../useUnplayable'; import showPlayerModal from './components/Player'; import useMagnet from './useMagnet'; @@ -46,6 +47,7 @@ const Actions: FC = ({ torrent, title, provider, show }) => { const fetchMagnet = useMagnet(torrent); const { pin, toggle, isPinned, savedMagnet, rememberMagnet } = usePinnedTorrents(); + const { reasonFor } = useUnplayable(); const key = torrentKey(torrent); const pinned = isPinned(key); @@ -55,6 +57,9 @@ const Actions: FC = ({ torrent, title, provider, show }) => { const hash = getMagnetHash(magnet); + // a release that already failed transcoding stays greyed out so we don't hit the same 502 twice + const unplayable = reasonFor(hash); + // persist the fetched magnet while pinned; only Toloka needs it (others carry it in the result) useEffect(() => { if (pinned && magnet && !torrent.magnet) rememberMagnet(key, magnet); @@ -101,16 +106,19 @@ const Actions: FC = ({ torrent, title, provider, show }) => { {supportedForStream && ( diff --git a/components/Preview/components/Torrents/useUnplayable.ts b/components/Preview/components/Torrents/useUnplayable.ts new file mode 100644 index 0000000..9d23b8f --- /dev/null +++ b/components/Preview/components/Torrents/useUnplayable.ts @@ -0,0 +1,34 @@ +import { useAtom } from 'jotai'; +import { atomWithStorage } from 'jotai/utils'; + +// Some releases use a video codec/container the TorrServer transcoder can't handle +// (e.g. VC-1 remuxes, AVI). Those fail with a 502 only when you actually press Play, so +// remember them by infohash to grey out Play instead of hitting the same dead-end again. +const unplayableAtom = atomWithStorage>('unplayable-torrents', {}, undefined, { + unstable_getOnInit: true +}); + +// getMagnetHash uppercases, the /gst URL hash is lowercase — normalise so both sides match +const normalize = (hash?: string) => hash?.toLowerCase(); + +const useUnplayable = () => { + const [unplayable, setUnplayable] = useAtom(unplayableAtom); + + const reasonFor = (hash?: string) => { + const key = normalize(hash); + + return key ? unplayable[key] : undefined; + }; + + const markUnplayable = (hash?: string, reason?: string) => { + const key = normalize(hash); + + if (!key || !reason) return; + + setUnplayable((current) => (current[key] === reason ? current : { ...current, [key]: reason })); + }; + + return { reasonFor, markUnplayable }; +}; + +export default useUnplayable; diff --git a/package-lock.json b/package-lock.json index 401f250..d34bab2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,7 @@ "@tanstack/react-query-next-experimental": "^5.59.16", "@types/cheerio": "^0.22.35", "@upstash/redis": "^1.38.2", + "@vercel/analytics": "^2.0.1", "@vidstack/react": "^1.12.13", "axios": "^1.7.7", "axios-cookiejar-support": "^5.0.3", @@ -4609,6 +4610,48 @@ "uncrypto": "^0.1.3" } }, + "node_modules/@vercel/analytics": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/@vercel/analytics/-/analytics-2.0.1.tgz", + "integrity": "sha512-MTQG6V9qQrt1tsDeF+2Uoo5aPjqbVPys1xvnIftXSJYG2SrwXRHnqEvVoYID7BTruDz4lCd2Z7rM1BdkUehk2g==", + "license": "MIT", + "peerDependencies": { + "@remix-run/react": "^2", + "@sveltejs/kit": "^1 || ^2", + "next": ">= 13", + "nuxt": ">= 3", + "react": "^18 || ^19 || ^19.0.0-rc", + "svelte": ">= 4", + "vue": "^3", + "vue-router": "^4" + }, + "peerDependenciesMeta": { + "@remix-run/react": { + "optional": true + }, + "@sveltejs/kit": { + "optional": true + }, + "next": { + "optional": true + }, + "nuxt": { + "optional": true + }, + "react": { + "optional": true + }, + "svelte": { + "optional": true + }, + "vue": { + "optional": true + }, + "vue-router": { + "optional": true + } + } + }, "node_modules/@vidstack/react": { "version": "1.12.13", "resolved": "https://registry.npmjs.org/@vidstack/react/-/react-1.12.13.tgz", @@ -15204,6 +15247,12 @@ "uncrypto": "^0.1.3" } }, + "@vercel/analytics": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/@vercel/analytics/-/analytics-2.0.1.tgz", + "integrity": "sha512-MTQG6V9qQrt1tsDeF+2Uoo5aPjqbVPys1xvnIftXSJYG2SrwXRHnqEvVoYID7BTruDz4lCd2Z7rM1BdkUehk2g==", + "requires": {} + }, "@vidstack/react": { "version": "1.12.13", "resolved": "https://registry.npmjs.org/@vidstack/react/-/react-1.12.13.tgz", diff --git a/package.json b/package.json index 8d2c5a6..f11d69c 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "@tanstack/react-query-next-experimental": "^5.59.16", "@types/cheerio": "^0.22.35", "@upstash/redis": "^1.38.2", + "@vercel/analytics": "^2.0.1", "@vidstack/react": "^1.12.13", "axios": "^1.7.7", "axios-cookiejar-support": "^5.0.3",