4828
Sr
stream-together / files
stream-together / components / agora-host.tsx
5.9 KB
1"use client"; 2 3import { 4 LocalUser, 5 RemoteUser, 6 useIsConnected, 7 useJoin, 8 usePublish, 9 useRemoteUsers, 10} from "agora-rtc-react"; 11import { useCallback, useState } from "react"; 12import { ImSpinner10 } from "react-icons/im"; 13import { IoEye } from "react-icons/io5"; 14 15import useVirtualBackground from "@/hooks/use-agora-virtual-background"; 16import useCamera from "@/hooks/use-camera"; 17import useMicrophone from "@/hooks/use-microphone"; 18import { appId, channel, HOST, OWNER, token } from "@/lib/config"; 19 20import EndStreamButton from "./end-stream-button"; 21import { Modal } from "./modal"; 22import MuteButton from "./mute-button"; 23import PrimaryButton from "./primary-button"; 24import SettingsButton from "./settings-button"; 25import StartStreamButton from "./start-stream-button"; 26import StreamSettings from "./stream-settings"; 27import VideoToggleButton from "./video-toggle-button"; 28 29export default function AgoraHost({ 30 userType, 31}: Readonly<{ userType: number }>) { 32 const [showSettingsModal, setShowSettingsModal] = useState(false); 33 const [callStarted, setCallStarted] = useState(false); 34 const [videoStarted, setVideoStarted] = useState(false); 35 const [audioStarted, setAudioStarted] = useState(false); 36 const { 37 localCameraTrack, 38 availableCameras, 39 selectedCameraId, 40 setSelectedCameraId, 41 } = useCamera(videoStarted); 42 const { 43 localMicrophoneTrack, 44 availableMicrophones, 45 selectedMicrophoneId, 46 setSelectedMicrophoneId, 47 } = useMicrophone(audioStarted); 48 const isConnected = useIsConnected(); 49 const remoteUsers = useRemoteUsers(); 50 const otherHosts = remoteUsers.filter((u) => u.uid === HOST); 51 const guests = remoteUsers.filter((u) => u.uid !== HOST && u.uid !== OWNER); 52 53 useJoin( 54 { 55 appid: appId, 56 channel, 57 token, 58 uid: userType, 59 }, 60 callStarted, 61 ); 62 63 usePublish([localMicrophoneTrack, localCameraTrack]); 64 65 const handleToggleAudio = useCallback(() => { 66 setAudioStarted(!audioStarted); 67 }, [audioStarted]); 68 69 const handleToggleVideo = useCallback(() => { 70 setVideoStarted(!videoStarted); 71 }, [videoStarted]); 72 73 const { backgroundBlurred, handleBlurBackground } = useVirtualBackground({ 74 localCameraTrack, 75 }); 76 77 return ( 78 <div className="w-full flex flex-col justify-center items-center gap-4"> 79 <div className="flex flex-col md:flex-row gap-8 w-full justify-center items-center"> 80 <div className="w-full flex flex-col gap-2 max-w-2xl"> 81 <div className="rounded-3xl overflow-hidden bg-black w-full aspect-[4/3]"> 82 <LocalUser 83 audioTrack={localMicrophoneTrack} 84 videoTrack={localCameraTrack} 85 cameraOn={videoStarted} 86 micOn={audioStarted} 87 playAudio={false} 88 > 89 <div className="flex flex-col justify-between h-full p-4"> 90 <div className="flex flex-row justify-end"> 91 <Modal 92 openButton={ 93 <SettingsButton 94 showSettingsModal={showSettingsModal} 95 setShowSettingsModal={setShowSettingsModal} 96 /> 97 } 98 showModal={showSettingsModal} 99 > 100 <div className="p-4 bg-stone-800 rounded-2xl flex flex-col gap-4 text-left"> 101 <StreamSettings 102 {...{ 103 availableCameras, 104 selectedCameraId, 105 setSelectedCameraId, 106 availableMicrophones, 107 selectedMicrophoneId, 108 setSelectedMicrophoneId, 109 backgroundBlurred, 110 handleBlurBackground, 111 }} 112 /> 113 114 <div className="text-center"> 115 <button onClick={() => setShowSettingsModal(false)}> 116 <PrimaryButton> 117 <p className="px-4 py-2">Close</p> 118 </PrimaryButton> 119 </button> 120 </div> 121 </div> 122 </Modal> 123 </div> 124 125 {callStarted && isConnected ? ( 126 <div className="flex flex-row justify-center gap-4"> 127 <MuteButton 128 audioStarted={audioStarted} 129 handleToggleAudio={handleToggleAudio} 130 /> 131 <VideoToggleButton 132 videoStarted={videoStarted} 133 handleToggleVideo={handleToggleVideo} 134 /> 135 <EndStreamButton 136 handleEndCall={() => setCallStarted(false)} 137 /> 138 </div> 139 ) : callStarted ? ( 140 <div className="flex flex-col flex-grow items-center justify-center"> 141 <ImSpinner10 size="4rem" className="animate-spin" /> 142 </div> 143 ) : ( 144 <div className="flex flex-row flex-grow justify-center"> 145 <StartStreamButton 146 handleStartCall={() => setCallStarted(true)} 147 /> 148 </div> 149 )} 150 </div> 151 </LocalUser> 152 </div> 153 </div> 154 155 {otherHosts.map((host) => { 156 return ( 157 <div 158 key={host.uid} 159 className="rounded-3xl overflow-hidden bg-black w-full max-w-2xl aspect-[4/3]" 160 > 161 <RemoteUser user={host} /> 162 </div> 163 ); 164 })} 165 </div> 166 167 <div className="text-center p-8 bg-stone-800 shadow-md shadow-stone-800 rounded-2xl max-w-2xl flex flex-row justify-center gap-2"> 168 <IoEye size="1.5em" /> 169 <p>Viewers: {guests?.length ?? 0}</p> 170 </div> 171 </div> 172 ); 173}