@@ -1,20 +1,19 @@
11 "use client";
22
3-import AgoraRTC, {
3+import {
44 LocalUser,
55 RemoteUser,
66 useIsConnected,
77 useJoin,
8- useLocalCameraTrack,
9- useLocalMicrophoneTrack,
108 usePublish,
119 useRemoteUsers,
1210 } from "agora-rtc-react";
13-import { useCallback, useEffect, useState } from "react";
11+import { useCallback, useState } from "react";
1412 import { ImSpinner10 } from "react-icons/im";
13+import { IoEye } from "react-icons/io5";
1514
16-import useNoiseReduction from "@/hooks/agora-ai-noise-reduction";
17-import useVirtualBackground from "@/hooks/agora-virtual-background";
15+import useCamera from "@/hooks/use-camera";
16+import useMicrophone from "@/hooks/use-microphone";
1817 import { appId, channel, HOST, OWNER, token } from "@/lib/config";
1918
2019 import EndStreamButton from "./end-stream-button";
@@ -30,69 +29,15 @@ export default function AgoraHost({
3029 userType,
3130 }: Readonly<{ userType: number }>) {
3231 const [showSettingsModal, setShowSettingsModal] = useState(false);
33-
3432 const [callStarted, setCallStarted] = useState(false);
3533 const [videoStarted, setVideoStarted] = useState(false);
3634 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-
35+ const { localCameraTrack } = useCamera(videoStarted);
36+ const { localMicrophoneTrack } = useMicrophone(audioStarted);
6237 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]);
38+ const remoteUsers = useRemoteUsers();
39+ const otherHosts = remoteUsers.filter((u) => u.uid === HOST);
40+ const guests = remoteUsers.filter((u) => u.uid !== HOST && u.uid !== OWNER);
9641
9742 useJoin(
9843 {
@@ -106,12 +51,16 @@ export default function AgoraHost({
10651
10752 usePublish([localMicrophoneTrack, localCameraTrack]);
10853
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);
54+ const handleToggleAudio = useCallback(() => {
55+ setAudioStarted(!audioStarted);
56+ }, [audioStarted]);
57+
58+ const handleToggleVideo = useCallback(() => {
59+ setVideoStarted(!videoStarted);
60+ }, [videoStarted]);
11261
11362 return (
114- <div className="w-full">
63+ <div className="w-full flex flex-col justify-center items-center gap-4">
11564 <div className="flex flex-col md:flex-row gap-8 w-full justify-center items-center">
11665 <div className="w-full flex flex-col gap-2 max-w-2xl">
11766 <div className="rounded-3xl overflow-hidden bg-black w-full aspect-[4/3]">
@@ -122,10 +71,8 @@ export default function AgoraHost({
12271 micOn={audioStarted}
12372 playAudio={false}
12473 >
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-
74+ <div className="flex flex-col justify-between h-full p-4">
75+ <div className="flex flex-row justify-end">
12976 <Modal
13077 openButton={
13178 <SettingsButton
@@ -137,10 +84,8 @@ export default function AgoraHost({
13784 >
13885 <div className="p-4 bg-stone-800 rounded-2xl flex flex-col gap-4 text-left">
13986 <StreamSettings
140- availableCameras={availableCameras}
141- selectedCameraId={selectedCameraId}
142- setSelectedCameraId={setSelectedCameraId}
143- handleBlurBackground={handleBlurBackground}
87+ audioStarted={audioStarted}
88+ videoStarted={videoStarted}
14489 />
14590
14691 <div className="text-center">
@@ -164,7 +109,9 @@ export default function AgoraHost({
164109 videoStarted={videoStarted}
165110 handleToggleVideo={handleToggleVideo}
166111 />
167- <EndStreamButton handleEndCall={handleEndCall} />
112+ <EndStreamButton
113+ handleEndCall={() => setCallStarted(false)}
114+ />
168115 </div>
169116 ) : callStarted ? (
170117 <div className="flex flex-col flex-grow items-center justify-center">
@@ -172,7 +119,9 @@ export default function AgoraHost({
172119 </div>
173120 ) : (
174121 <div className="flex flex-row flex-grow justify-center">
175- <StartStreamButton handleStartCall={handleStarCall} />
122+ <StartStreamButton
123+ handleStartCall={() => setCallStarted(true)}
124+ />
176125 </div>
177126 )}
178127 </div>
@@ -186,15 +135,16 @@ export default function AgoraHost({
186135 key={host.uid}
187136 className="rounded-3xl overflow-hidden bg-black w-full max-w-2xl aspect-[4/3]"
188137 >
189- <RemoteUser user={host}>
190- <p className="p-4">{host.uid}</p>
191- </RemoteUser>
138+ <RemoteUser user={host} />
192139 </div>
193140 );
194141 })}
195142 </div>
196143
197- <div className="text-center">People watching: {guests?.length ?? 0}</div>
144+ <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">
145+ <IoEye size="1.5em" />
146+ <p>Viewers: {guests?.length ?? 0}</p>
147+ </div>
198148 </div>
199149 );
200150 }