@@ -0,0 +1,200 @@
1+"use client";
2+
3+import AgoraRTC, {
4+ LocalUser,
5+ RemoteUser,
6+ useIsConnected,
7+ useJoin,
8+ useLocalCameraTrack,
9+ useLocalMicrophoneTrack,
10+ usePublish,
11+ useRemoteUsers,
12+} from "agora-rtc-react";
13+import { useCallback, useEffect, useState } from "react";
14+import { ImSpinner10 } from "react-icons/im";
15+
16+import useNoiseReduction from "@/hooks/agora-ai-noise-reduction";
17+import useVirtualBackground from "@/hooks/agora-virtual-background";
18+import { appId, channel, HOST, OWNER, token } from "@/lib/config";
19+
20+import EndStreamButton from "./end-stream-button";
21+import { Modal } from "./modal";
22+import MuteButton from "./mute-button";
23+import PrimaryButton from "./primary-button";
24+import SettingsButton from "./settings-button";
25+import StartStreamButton from "./start-stream-button";
26+import StreamSettings from "./stream-settings";
27+import VideoToggleButton from "./video-toggle-button";
28+
29+export default function AgoraHost({
30+ userType,
31+}: Readonly<{ userType: number }>) {
32+ const [showSettingsModal, setShowSettingsModal] = useState(false);
33+
34+ const [callStarted, setCallStarted] = useState(false);
35+ const [videoStarted, setVideoStarted] = useState(false);
36+ const [audioStarted, setAudioStarted] = useState(false);
37+
38+ const [availableCameras, setAvailableCameras] = useState<
39+ { id: string; label: string }[]
40+ >([]);
41+ const [selectedCameraId, setSelectedCameraId] = useState("");
42+
43+ useEffect(() => {
44+ const getCameras = async () => {
45+ const cameras = await AgoraRTC.getCameras();
46+ setAvailableCameras(
47+ cameras.map((c) => ({ id: c.deviceId, label: c.label })),
48+ );
49+ // Choose the first one as a default
50+ setSelectedCameraId(cameras[0].deviceId);
51+ };
52+ getCameras();
53+ }, []);
54+
55+ const { localCameraTrack } = useLocalCameraTrack(videoStarted, {
56+ cameraId: selectedCameraId,
57+ });
58+
59+ const { localMicrophoneTrack } = useLocalMicrophoneTrack(audioStarted);
60+ useNoiseReduction({ localMicrophoneTrack });
61+
62+ const isConnected = useIsConnected();
63+
64+ const handleStarCall = useCallback(() => {
65+ setCallStarted(true);
66+ }, []);
67+
68+ const handleEndCall = useCallback(() => {
69+ setCallStarted(false);
70+ }, []);
71+
72+ const handleToggleAudio = useCallback(() => {
73+ setAudioStarted(!audioStarted);
74+ }, [audioStarted]);
75+
76+ const handleToggleVideo = useCallback(() => {
77+ setVideoStarted(!videoStarted);
78+ }, [videoStarted]);
79+
80+ const virtualBackgroundRef = useVirtualBackground({ localCameraTrack });
81+
82+ const handleBlurBackground = useCallback(() => {
83+ const enableBlur = async () => {
84+ if (virtualBackgroundRef.current?.enabled) {
85+ virtualBackgroundRef.current?.disable();
86+ } else {
87+ virtualBackgroundRef.current?.setOptions({
88+ type: "blur",
89+ blurDegree: 2,
90+ });
91+ virtualBackgroundRef.current?.enable();
92+ }
93+ };
94+ void enableBlur();
95+ }, [virtualBackgroundRef]);
96+
97+ useJoin(
98+ {
99+ appid: appId,
100+ channel,
101+ token,
102+ uid: userType,
103+ },
104+ callStarted,
105+ );
106+
107+ usePublish([localMicrophoneTrack, localCameraTrack]);
108+
109+ const remoteUsers = useRemoteUsers();
110+ const otherHosts = remoteUsers.filter((u) => u.uid === HOST);
111+ const guests = remoteUsers.filter((u) => u.uid !== HOST && u.uid !== OWNER);
112+
113+ return (
114+ <div className="w-full">
115+ <div className="flex flex-col md:flex-row gap-8 w-full justify-center items-center">
116+ <div className="w-full flex flex-col gap-2 max-w-2xl">
117+ <div className="rounded-3xl overflow-hidden bg-black w-full aspect-[4/3]">
118+ <LocalUser
119+ audioTrack={localMicrophoneTrack}
120+ videoTrack={localCameraTrack}
121+ cameraOn={videoStarted}
122+ micOn={audioStarted}
123+ playAudio={false}
124+ >
125+ <div className="flex flex-col justify-between h-full p-2">
126+ <div className="flex flex-row justify-between">
127+ <p className="p-2">Your broadcast</p>
128+
129+ <Modal
130+ openButton={
131+ <SettingsButton
132+ showSettingsModal={showSettingsModal}
133+ setShowSettingsModal={setShowSettingsModal}
134+ />
135+ }
136+ showModal={showSettingsModal}
137+ >
138+ <div className="p-4 bg-stone-800 rounded-2xl flex flex-col gap-4 text-left">
139+ <StreamSettings
140+ availableCameras={availableCameras}
141+ selectedCameraId={selectedCameraId}
142+ setSelectedCameraId={setSelectedCameraId}
143+ handleBlurBackground={handleBlurBackground}
144+ />
145+
146+ <div className="text-center">
147+ <button onClick={() => setShowSettingsModal(false)}>
148+ <PrimaryButton>
149+ <p className="px-4 py-2">Close</p>
150+ </PrimaryButton>
151+ </button>
152+ </div>
153+ </div>
154+ </Modal>
155+ </div>
156+
157+ {callStarted && isConnected ? (
158+ <div className="flex flex-row justify-center gap-4">
159+ <MuteButton
160+ audioStarted={audioStarted}
161+ handleToggleAudio={handleToggleAudio}
162+ />
163+ <VideoToggleButton
164+ videoStarted={videoStarted}
165+ handleToggleVideo={handleToggleVideo}
166+ />
167+ <EndStreamButton handleEndCall={handleEndCall} />
168+ </div>
169+ ) : callStarted ? (
170+ <div className="flex flex-col flex-grow items-center justify-center">
171+ <ImSpinner10 size="4rem" className="animate-spin" />
172+ </div>
173+ ) : (
174+ <div className="flex flex-row flex-grow justify-center">
175+ <StartStreamButton handleStartCall={handleStarCall} />
176+ </div>
177+ )}
178+ </div>
179+ </LocalUser>
180+ </div>
181+ </div>
182+
183+ {otherHosts.map((host) => {
184+ return (
185+ <div
186+ key={host.uid}
187+ className="rounded-3xl overflow-hidden bg-black w-full max-w-2xl aspect-[4/3]"
188+ >
189+ <RemoteUser user={host}>
190+ <p className="p-4">{host.uid}</p>
191+ </RemoteUser>
192+ </div>
193+ );
194+ })}
195+ </div>
196+
197+ <div className="text-center">People watching: {guests?.length ?? 0}</div>
198+ </div>
199+ );
200+}