xonecas.io
4828
Sr

stream-together / commit 4dda7b2

Implement audience page

Rename /host to /broadcast
Added the host parameter
Owner of the stream and guest host are now represented by constant
numbers (1, 2)

sean2024-06-28 4dda7b23d960e50d392a102ab537f7d204b37839 parent 7dcaa8b

7 files changed

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