import { useState, useEffect } from "react"; import { Signal, Activity, Wifi } from "lucide-react"; interface QualityIndicatorProps { hlsInstance: any; className?: string; } export default function QualityIndicator({ hlsInstance, className = "" }: QualityIndicatorProps) { const [currentQuality, setCurrentQuality] = useState(""); const [currentBitrate, setCurrentBitrate] = useState(0); const [networkType, setNetworkType] = useState(""); const [bufferHealth, setBufferHealth] = useState<"good" | "medium" | "poor">("good"); const [downloadSpeed, setDownloadSpeed] = useState(0); const [isBuffering, setIsBuffering] = useState(false); useEffect(() => { if (!hlsInstance) return; // Monitor quality changes const handleLevelSwitch = (event: any, data: any) => { const level = hlsInstance.levels[data.level]; setCurrentQuality(`${level.height}p`); setCurrentBitrate(Math.round(level.bitrate / 1000)); // Convert to kbps console.log(`Preklopil na: ${level.height}p @ ${Math.round(level.bitrate/1000)}kbps`); }; // Monitor buffer health const handleBufferAppending = () => { const video = hlsInstance.media; if (video && video.buffered.length > 0) { const bufferLevel = video.buffered.end(video.buffered.length - 1) - video.currentTime; if (bufferLevel > 10) { setBufferHealth("good"); } else if (bufferLevel > 3) { setBufferHealth("medium"); } else { setBufferHealth("poor"); } } }; // Monitor loading progress and speed const handleFragLoaded = (event: any, data: any) => { const stats = data.stats; if (stats) { const speed = (stats.total * 8) / (stats.loading.end - stats.loading.start); // bits per ms const speedKbps = Math.round(speed); // Convert to kbps setDownloadSpeed(speedKbps); console.log(`Download hitrost: ${speedKbps} kbps`); } }; const handleWaiting = () => setIsBuffering(true); const handlePlaying = () => setIsBuffering(false); hlsInstance.on('hlsLevelSwitched', handleLevelSwitch); hlsInstance.on('hlsBufferAppending', handleBufferAppending); hlsInstance.on('hlsFragLoaded', handleFragLoaded); const video = hlsInstance.media; if (video) { video.addEventListener('waiting', handleWaiting); video.addEventListener('playing', handlePlaying); } // Detect network connection const connection = (navigator as any).connection; if (connection) { setNetworkType(connection.effectiveType || "unknown"); const handleConnectionChange = () => { setNetworkType(connection.effectiveType || "unknown"); }; connection.addEventListener('change', handleConnectionChange); return () => { connection.removeEventListener('change', handleConnectionChange); }; } return () => { if (hlsInstance) { hlsInstance.off('hlsLevelSwitched', handleLevelSwitch); hlsInstance.off('hlsBufferAppending', handleBufferAppending); hlsInstance.off('hlsFragLoaded', handleFragLoaded); } if (video) { video.removeEventListener('waiting', handleWaiting); video.removeEventListener('playing', handlePlaying); } }; }, [hlsInstance]); const getSignalColor = () => { switch (bufferHealth) { case "good": return "text-green-500"; case "medium": return "text-yellow-500"; case "poor": return "text-red-500"; default: return "text-gray-500"; } }; if (!currentQuality) return null; return (
{/* Quality and Signal */}
{currentQuality} {isBuffering && ( )}
{/* Bitrate */} {currentBitrate > 0 && (
Bitrate: {currentBitrate} kbps
)} {/* Download Speed */} {downloadSpeed > 0 && (
{downloadSpeed} kbps
)} {/* Network Type */} {networkType && (
Omrežje: {networkType}
)}
); }