import { useCallback, useEffect, useMemo, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { app as $appContent } from '../content'; import { clsm } from '../utils'; import { Sensors } from '../assets/icons'; import { useStreams } from '../contexts/Streams'; import { useUser } from '../contexts/User'; import { VOLUME_MIN } from '../constants'; import Button from './Button'; import LivePill from './LivePill'; import Spinner from './Spinner'; import useCurrentPage from '../hooks/useCurrentPage'; import usePlayer from '../hooks/usePlayer'; import useStreamSessionData from '../contexts/Streams/useStreamSessionData'; const $content = $appContent.floating_player; const FloatingPlayer = () => { const { activeStreamSession, hasStreamSessions, isLive, setStreamSessions, streamSessions, updateActiveStreamSession } = useStreams(); const liveSession = useMemo( () => streamSessions?.find((streamSession) => streamSession.isLive), [streamSessions] ); const { updateStreamSessionDataFetchKey } = useStreamSessionData({ isLive, isRevalidationEnabled: !liveSession?.ingestConfiguration, setStreamSessions, streamSessions }); const { userData } = useUser(); const { canvasRef, isBlurReady, isLoading, playerRef, shouldBlurPlayer, videoRef } = usePlayer({ isLive, playbackUrl: userData?.playbackUrl, ingestConfiguration: liveSession?.ingestConfiguration, isBlurEnabled: true, defaultVolumeLevel: VOLUME_MIN }); const [isExpanded, setIsExpanded] = useState(true); const currentPage = useCurrentPage(); const navigate = useNavigate(); const shouldShowSpinner = (isLoading && isLive !== false) || (shouldBlurPlayer && !isBlurReady); const setLiveActiveStreamSession = useCallback(() => { updateActiveStreamSession(streamSessions?.[0]); if (currentPage !== 'stream_health') navigate('/health'); }, [currentPage, navigate, streamSessions, updateActiveStreamSession]); // Lower the rendition of the player to the lowest available resolution useEffect(() => { if (playerRef.current && isLive && !isLoading) { const qualities = playerRef.current?.getQualities() || []; const lowestQuality = qualities.pop(); if (lowestQuality) { playerRef.current.setQuality(lowestQuality, true); } } }, [isLive, isLoading, playerRef]); // Try to get the ingest configuration to determine if we need to blur the sides of the player (portrait stream) useEffect(() => { if (liveSession) updateStreamSessionDataFetchKey(liveSession); }, [liveSession, updateStreamSessionDataFetchKey]); return (
{$content.your_channel_is_offline}
) : ( <>{$content.offline_instructions}
{currentPage !== 'settings' && (