import React, { useEffect, useMemo, useState } from 'react'; import { StyleSheet, View } from 'react-native'; import { useTheme } from '../../../contexts'; import { type ViewerLivestreamTopViewProps } from '../LivestreamTopView/ViewerLivestreamTopView'; import { ViewerLivestreamControls as DefaultViewerLivestreamControls, type ViewerLivestreamControlsProps, } from '../LivestreamControls/ViewerLivestreamControls'; import type { ViewerLeaveStreamButtonProps } from '../LivestreamControls/ViewerLeaveStreamButton'; import { LivestreamLayout as DefaultLivestreamLayout, type LivestreamLayoutProps, } from '../LivestreamLayout'; import { useCall, useCallStateHooks } from '@stream-io/video-react-bindings'; import { FloatingParticipantView as DefaultFloatingParticipantView, type FloatingParticipantViewProps, } from '../../Participant'; import { CallingState, hasVideo } from '@stream-io/video-client'; import { CallEndedView } from '../LivestreamPlayer/LivestreamEnded'; import { ViewerLobby } from './ViewerLobby'; import { getRNInCallManagerLibNoThrow } from '../../../modules/call-manager/PrevLibDetection'; /** * Props for the ViewerLivestream component. */ export type ViewerLivestreamProps = ViewerLivestreamTopViewProps & ViewerLivestreamControlsProps & ViewerLeaveStreamButtonProps & { /** * Component to customize the top view at the viewer's live stream. */ ViewerLivestreamTopView?: React.ComponentType | null; /** * Component to customize the live stream video layout. */ LivestreamLayout?: React.ComponentType | null; /** * Component to customize the bottom view controls at the viewer's live stream. */ ViewerLivestreamControls?: React.ComponentType | null; /** * Component to customize the FloatingParticipantView when screen is shared. */ FloatingParticipantView?: React.ComponentType | null; /** * Determines when the viewer joins the call. * * `"asap"` behavior means joining the call as soon as it is possible * (either the `join_ahead_time_seconds` setting allows it, or the user * has a the capability to join backstage). */ joinBehavior?: 'asap' | 'live'; }; /** * The ViewerLivestream component renders the UI for the Viewer's live stream. */ export const ViewerLivestream = ({ ViewerLivestreamTopView, ViewerLivestreamControls = DefaultViewerLivestreamControls, LivestreamLayout = DefaultLivestreamLayout, FloatingParticipantView = DefaultFloatingParticipantView, LiveIndicator, FollowerCount, DurationBadge, ViewerLeaveStreamButton, onLeaveStreamHandler, joinBehavior, }: ViewerLivestreamProps) => { const styles = useStyles(); const call = useCall(); const { theme: { viewerLivestream }, } = useTheme(); const { useHasOngoingScreenShare, useParticipants, useCallCallingState, useCallEndedAt, useIsCallLive, useOwnCapabilities, } = useCallStateHooks(); const canJoinLive = useIsCallLive(); const callingState = useCallCallingState(); const endedAt = useCallEndedAt(); const hasOngoingScreenShare = useHasOngoingScreenShare(); const [currentSpeaker] = useParticipants(); const floatingParticipant = hasOngoingScreenShare && currentSpeaker && hasVideo(currentSpeaker) && currentSpeaker; const [hasLeft, setHasLeft] = useState(false); const canJoinEarly = useCanJoinEarly(); const canJoinBackstage = useOwnCapabilities()?.includes('join-backstage') ?? false; const [topViewHeight, setTopViewHeight] = React.useState(); const [controlsHeight, setControlsHeight] = React.useState(); // Automatically route audio to speaker devices as relevant for watching videos. useEffect(() => { const prevInCallManager = getRNInCallManagerLibNoThrow(); if (!prevInCallManager) return; prevInCallManager.start({ media: 'video' }); return () => { prevInCallManager.stop(); }; }, []); useEffect(() => { if (callingState === CallingState.LEFT) { setHasLeft(true); } }, [callingState]); const topViewProps: ViewerLivestreamTopViewProps = { LiveIndicator, FollowerCount, DurationBadge, onLayout: (event) => { setTopViewHeight(event.nativeEvent.layout.height); }, }; useEffect(() => { const canJoinAsap = canJoinLive || canJoinEarly || canJoinBackstage; const join = joinBehavior ?? 'asap'; const canJoin = (join === 'asap' && canJoinAsap) || (join === 'live' && canJoinLive); if (call && callingState === CallingState.IDLE && canJoin && !hasLeft) { call.join().catch((error) => { console.error('Failed to join call', error); }); } }, [ canJoinLive, call, canJoinBackstage, canJoinEarly, joinBehavior, callingState, hasLeft, ]); if (endedAt != null) { return ( ); } if (!canJoinLive || callingState !== CallingState.JOINED) { return ( ); } return ( {ViewerLivestreamTopView && } {FloatingParticipantView && floatingParticipant && topViewHeight && controlsHeight && ( )} {LivestreamLayout && } {ViewerLivestreamControls && ( { setControlsHeight(event.nativeEvent.layout.height); }} /> )} ); }; const useCanJoinEarly = () => { const { useCallStartsAt, useCallSettings } = useCallStateHooks(); const startsAt = useCallStartsAt(); const settings = useCallSettings(); const joinAheadTimeSeconds = settings?.backstage.join_ahead_time_seconds; const [canJoinEarly, setCanJoinEarly] = useState(() => checkCanJoinEarly(startsAt, joinAheadTimeSeconds), ); useEffect(() => { if (canJoinEarly) return; const handle = setInterval(() => { setCanJoinEarly(checkCanJoinEarly(startsAt, joinAheadTimeSeconds)); }, 1000); return () => clearInterval(handle); }, [canJoinEarly, startsAt, joinAheadTimeSeconds]); return canJoinEarly; }; const checkCanJoinEarly = ( startsAt: Date | undefined, joinAheadTimeSeconds: number | undefined, ) => { if (!startsAt) { return false; } return Date.now() >= +startsAt - (joinAheadTimeSeconds ?? 0) * 1000; }; const useStyles = () => { const { theme } = useTheme(); return useMemo( () => StyleSheet.create({ container: { flex: 1, paddingBottom: theme.variants.insets.bottom, paddingLeft: theme.variants.insets.left, paddingRight: theme.variants.insets.right, paddingTop: theme.variants.insets.top, backgroundColor: theme.colors.sheetPrimary, }, }), [theme], ); };