From 1a2937d30334c08ffeb189aab8327e064508b96f Mon Sep 17 00:00:00 2001 From: kuekhaoyang Date: Sat, 6 Dec 2025 14:07:20 +0800 Subject: [PATCH] feat: Configure HLS player for faster startup with prefetch and buffer settings, and remove the history downloader. --- app/layout.tsx | 3 +- components/player/hooks/useHlsPlayer.ts | 4 ++ lib/hooks/useHistoryDownloader.ts | 65 ------------------------- 3 files changed, 5 insertions(+), 67 deletions(-) delete mode 100644 lib/hooks/useHistoryDownloader.ts diff --git a/app/layout.tsx b/app/layout.tsx index d18ad05..213d87f 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -4,7 +4,7 @@ import "./globals.css"; import { ThemeProvider } from "@/components/ThemeProvider"; import { Analytics } from "@vercel/analytics/react"; import { ServiceWorkerRegister } from "@/components/ServiceWorkerRegister"; -import { HistoryDownloader } from "@/lib/hooks/useHistoryDownloader"; + const geistSans = Geist({ variable: "--font-geist-sans", @@ -39,7 +39,6 @@ export default function RootLayout({ {children} - {/* ARIA Live Region for Screen Reader Announcements */} diff --git a/components/player/hooks/useHlsPlayer.ts b/components/player/hooks/useHlsPlayer.ts index 32e69bc..2f0c234 100644 --- a/components/player/hooks/useHlsPlayer.ts +++ b/components/player/hooks/useHlsPlayer.ts @@ -45,6 +45,10 @@ export function useHlsPlayer({ hls = new Hls({ enableWorker: true, lowLatencyMode: true, + startFragPrefetch: true, // Fetch first segment immediately while parsing manifest + // Reduce buffering requirement for startup + maxBufferLength: 30, + backBufferLength: 30, }); hlsRef.current = hls; diff --git a/lib/hooks/useHistoryDownloader.ts b/lib/hooks/useHistoryDownloader.ts deleted file mode 100644 index 4545007..0000000 --- a/lib/hooks/useHistoryDownloader.ts +++ /dev/null @@ -1,65 +0,0 @@ -/** - * History Downloader Hook - * Downloads all segments for videos in watch history - */ - -'use client'; - -import { useEffect, useRef } from 'react'; -import { usePathname } from 'next/navigation'; -import { useHistoryStore } from '@/lib/store/history-store'; -import { parseHLSManifest } from '@/lib/utils/hlsManifestParser'; -import { downloadSegmentQueue } from '@/lib/utils/segmentDownloader'; - -function useHistoryDownloader() { - const viewingHistory = useHistoryStore((state) => state.viewingHistory); - const processedUrlsRef = useRef>(new Set()); - const pathname = usePathname(); - - useEffect(() => { - // Don't download history while watching a video - if (pathname?.startsWith('/player')) return; - - if (viewingHistory.length === 0) return; - - const downloadHistoryVideos = async () => { - for (const item of viewingHistory) { - const { url } = item; - - // Skip if already processed - if (processedUrlsRef.current.has(url)) continue; - - // Skip if not m3u8 - if (!url.endsWith('.m3u8')) continue; - - // Queue background download - processedUrlsRef.current.add(url); - - try { - const segments = await parseHLSManifest(url); - const controller = new AbortController(); - - // Download all segments (not just from playback position) - downloadSegmentQueue({ - segments, - startIndex: 0, - signal: controller.signal, - videoUrl: url, // Track metadata for this video - onProgress: (current, total) => { - // console.log removed as per instruction - } - }); - } catch (error) { - console.error(`[HistoryDownloader] Failed to download ${item.title}:`, error); - } - } - }; - - downloadHistoryVideos(); - }, [viewingHistory]); -} - -export function HistoryDownloader() { - useHistoryDownloader(); - return null; -}