xonecas.io
4828
Sr

stream-together / commit 80b03d4

Adds host view

Implements basic usage of the AgoraIO SDK (connect, toggle video and
audio)
Adds react-icons package, and use the icons for mute, video and stream
controls.

sean2024-06-27 80b03d4cd866e61ed84409f83f9d0f0ef64f36a1 parent add32be

6 files changed

app/[slug]/page.tsx+0−36
@@ -1,36 +0,0 @@
1-"use client";
2-
3-import dynamic from "next/dynamic";
4-import { useLocalStorage } from "usehooks-ts";
5-
6-const AgoraProvider = dynamic(() => import("@/components/agora-provider"), {
7- ssr: false,
8-});
9-
10-const AgoraHost = dynamic(() => import("@/components/agora-host-video"), {
11- ssr: false,
12-});
13-
14-export default function StreamSetupPage({
15- params,
16-}: {
17- params: { slug: string };
18-}) {
19- const [appId] = useLocalStorage("app-id", "");
20- const [token] = useLocalStorage("token", "");
21- const [title] = useLocalStorage("title", "");
22-
23- return (
24- <AgoraProvider>
25- <div className="flex flex-col items-center gap-16">
26- <h2 className="text-2xl text-rose-600">
27- {title} ({params.slug})
28- </h2>
29-
30- <div>
31- <AgoraHost config={{ appId, channel: title, token }} />
32- </div>
33- </div>
34- </AgoraProvider>
35- );
36-}
app/new/page.tsx+21−22
@@ -10,10 +10,9 @@ import PrimaryButton from "@/components/primary-button";
1010 import slugify from "@/lib/slugify";
1111
1212 export default function NewStreamPage() {
13- // TODO: Allow user to forget their stored details
1413 const [appId, setAppId] = useLocalStorage("app-id", "");
1514 const [token, setToken] = useLocalStorage("token", "");
16- const [title, setTitle] = useLocalStorage("title", "");
15+ const [channel, setChannel] = useLocalStorage("channel", "");
1716 const [error, setError] = useState("");
1817 const router = useRouter();
1918
@@ -23,41 +22,41 @@ export default function NewStreamPage() {
2322
2423 if (name === "app-id") setAppId(e.target.value);
2524 if (name === "token") setToken(e.target.value);
26- if (name === "title") setTitle(e.target.value);
25+ if (name === "channel") setChannel(e.target.value);
2726
2827 // Clear the error if it exists after a new change
2928 if (error) setError("");
3029 },
31- [error, setAppId, setToken, setTitle],
30+ [error, setAppId, setToken, setChannel],
3231 );
3332
3433 const handleNextClick = useCallback(() => {
35- if (!appId || !token || !title) setError("All fields are required.");
34+ if (!appId || !token || !channel) setError("All fields are required.");
3635 else {
37- const slug = slugify(title);
36+ const slug = slugify(channel);
3837 router.push("/" + slug);
3938 }
40- }, [appId, token, title, router]);
39+ }, [appId, token, channel, router]);
4140
4241 return (
4342 <div className="flex flex-col items-center gap-8">
4443 <h3
4544 className={`text-2xl font-bold p-16 text-rose-300 ${pacifico.className}`}
4645 >
47- Please fill in the details bellow
46+ Please fill in the details bellow,
4847 </h3>
48+ <p>
49+ All this information can be acquired from the{" "}
50+ <Link
51+ className="text-rose-600 active:text-rose-300 visited:text-rose-800"
52+ target="_blank"
53+ href="https://console.agora.io/v2/project-management"
54+ >
55+ AgoraIO Console
56+ </Link>
57+ </p>
4958 <div className="w-1/2 flex flex-col gap-4">
50- <label>
51- Tell us your App ID from AgoraIO (get one{" "}
52- <Link
53- className="text-rose-600 active:text-rose-300 visited:text-rose-800"
54- target="_blank"
55- href="https://console.agora.io/v2/project-management"
56- >
57- here
58- </Link>
59- )
60- </label>
59+ <label>Tell us your App ID from AgoraIO</label>
6160 <input
6261 className="text-stone-200 bg-stone-800 rounded-xl p-4 w-full placeholder:text-stone-700"
6362 name="app-id"
@@ -81,12 +80,12 @@ export default function NewStreamPage() {
8180 </div>
8281
8382 <div className="w-1/2 flex flex-col gap-4">
84- <label>What is your steam title?</label>
83+ <label>Tell us your AgoraIO channel name</label>
8584 <input
8685 className="text-stone-200 bg-stone-800 rounded-xl p-4 w-full placeholder:text-stone-700"
87- name="title"
86+ name="channel"
8887 type="text"
89- value={title}
88+ value={channel}
9089 onChange={handleChange}
9190 placeholder="Your token here..."
9291 />
components/agora-host-video.tsx+95−12
@@ -2,14 +2,24 @@
22
33 import {
44 LocalUser,
5+ RemoteUser,
6+ useIsConnected,
57 useJoin,
68 useLocalCameraTrack,
79 useLocalMicrophoneTrack,
810 usePublish,
11+ useRemoteUsers,
912 } from "agora-rtc-react";
1013 import { useCallback, useState } from "react";
14+import { AiOutlineAudio as UnmuteIcon } from "react-icons/ai";
15+import { AiOutlineAudioMuted as MuteIcon } from "react-icons/ai";
16+import { CiStreamOn as StreamOnIcon } from "react-icons/ci";
17+import { CiStreamOff as StreamOffIcon } from "react-icons/ci";
18+import { IoVideocamOutline as VideoOnIcon } from "react-icons/io5";
19+import { IoVideocamOffOutline as VideoOffIcon } from "react-icons/io5";
1120
1221 import PrimaryButton from "./primary-button";
22+import SecondaryButton from "./secondary-button";
1323
1424 export default function AgoraHost({
1525 config: { appId, channel, token },
@@ -18,20 +28,33 @@ export default function AgoraHost({
1828 }) {
1929 const [callStarted, setCallStarted] = useState(false);
2030 const [videoStarted, setVideoStarted] = useState(false);
21- const [muted, setMuted] = useState(false);
31+ const [audioStarted, setAudioStarted] = useState(false);
2232
23- const { localMicrophoneTrack } = useLocalMicrophoneTrack(!muted);
33+ const { localMicrophoneTrack } = useLocalMicrophoneTrack(audioStarted);
2434 const { localCameraTrack } = useLocalCameraTrack(videoStarted);
2535
26- const handleCall = useCallback(() => {
36+ const isConnected = useIsConnected();
37+
38+ const handleStarCall = useCallback(() => {
2739 setCallStarted(true);
28- setVideoStarted(true);
2940 }, []);
3041
42+ const handleEndCall = useCallback(() => {
43+ setCallStarted(false);
44+ }, []);
45+
46+ const handleToggleAudio = useCallback(() => {
47+ setAudioStarted(!audioStarted);
48+ }, [audioStarted]);
49+
50+ const handleToggleVideo = useCallback(() => {
51+ setVideoStarted(!videoStarted);
52+ }, [videoStarted]);
53+
3154 useJoin(
3255 {
3356 appid: appId,
34- channel: "stream-together",
57+ channel,
3558 token,
3659 },
3760 callStarted,
@@ -39,22 +62,82 @@ export default function AgoraHost({
3962
4063 usePublish([localMicrophoneTrack, localCameraTrack]);
4164
65+ const otherHosts = useRemoteUsers();
66+
4267 return (
43- <div className="flex flex-col gap-8">
44- <div style={{ width: 640, height: 480 }}>
68+ <div className="flex flex-col gap-8 w-full items-center">
69+ <div className="rounded-3xl overflow-hidden bg-black w-1/2 h-80">
4570 <LocalUser
4671 audioTrack={localMicrophoneTrack}
4772 videoTrack={localCameraTrack}
4873 cameraOn={videoStarted}
49- micOn={!muted}
74+ micOn={audioStarted}
5075 >
51- You
76+ <div className="flex flex-col justify-between h-full p-2">
77+ <p className="p-2">Your broadcast</p>
78+ {callStarted && isConnected ? (
79+ <div className="flex flex-row justify-center gap-4">
80+ <button
81+ onClick={handleToggleAudio}
82+ title={audioStarted ? "Mute" : "Unmute"}
83+ >
84+ <SecondaryButton>
85+ {audioStarted ? (
86+ <MuteIcon className="p-2" size="3em" />
87+ ) : (
88+ <UnmuteIcon className="p-2" size="3em" />
89+ )}
90+ </SecondaryButton>
91+ </button>
92+ <button
93+ onClick={handleToggleVideo}
94+ title={videoStarted ? "Turn video off" : "Turn video on"}
95+ >
96+ <SecondaryButton>
97+ {videoStarted ? (
98+ <VideoOffIcon className="p-2" size="3em" />
99+ ) : (
100+ <VideoOnIcon className="p-2" size="3em" />
101+ )}
102+ </SecondaryButton>
103+ </button>
104+ <button onClick={handleEndCall} title="End stream">
105+ <PrimaryButton>
106+ <StreamOffIcon className="p-1" size="3em" />
107+ </PrimaryButton>
108+ </button>
109+ </div>
110+ ) : callStarted ? (
111+ <div>Loading...</div>
112+ ) : (
113+ <div className="text-center">
114+ <button onClick={handleStarCall} title="Go live!">
115+ <PrimaryButton>
116+ <div className="flex flex-row items-center px-2 gap-2">
117+ <p>Go live!</p>
118+ <StreamOnIcon className="p-1" size="3em" />
119+ </div>
120+ </PrimaryButton>
121+ </button>
122+ </div>
123+ )}
124+ </div>
52125 </LocalUser>
53126 </div>
54127
55- <button onClick={handleCall}>
56- <PrimaryButton>Call</PrimaryButton>
57- </button>
128+ {otherHosts.map((host) => {
129+ console.log(host);
130+ return (
131+ <div
132+ key={host.uid}
133+ className="rounded-3xl overflow-hidden bg-black w-1/2 h-80"
134+ >
135+ <RemoteUser user={host}>
136+ <p className="p-4">{host.uid}</p>
137+ </RemoteUser>
138+ </div>
139+ );
140+ })}
58141 </div>
59142 );
60143 }
components/primary-button.tsx+1−1
@@ -4,7 +4,7 @@ export default function PrimaryButton({
44 children: React.ReactNode;
55 }>) {
66 return (
7- <div className="text-rose-100 bg-rose-700 hover:bg-rose-900 border-b-8 active:border-b-0 active:border-t-8 border-t-rose-950 border-b-rose-950 font-bold text-2xl py-4 px-8 rounded-t-lg active:rounded-t-xl rounded-b-xl shadow-sm shadow-stone-800 cursor-pointer text-center">
7+ <div className="text-rose-100 bg-rose-700 hover:bg-rose-900 border-b-8 active:border-b-0 active:border-t-8 border-t-rose-950 border-b-rose-950 font-bold rounded-t-lg active:rounded-t-xl rounded-b-xl shadow-sm shadow-stone-800 cursor-pointer text-center">
88 {children}
99 </div>
1010 );
package-lock.json+10−0
@@ -13,6 +13,7 @@
1313 "next": "14.2.4",
1414 "react": "^18",
1515 "react-dom": "^18",
16+ "react-icons": "^5.2.1",
1617 "usehooks-ts": "^3.1.0"
1718 },
1819 "devDependencies": {
@@ -4040,6 +4041,15 @@
40404041 "react": "^18.3.1"
40414042 }
40424043 },
4044+ "node_modules/react-icons": {
4045+ "version": "5.2.1",
4046+ "resolved": "https://registry.npmjs.org/react-icons/-/react-icons-5.2.1.tgz",
4047+ "integrity": "sha512-zdbW5GstTzXaVKvGSyTaBalt7HSfuK5ovrzlpyiWHAFXndXTdd/1hdDHI4xBM1Mn7YriT6aqESucFl9kEXzrdw==",
4048+ "license": "MIT",
4049+ "peerDependencies": {
4050+ "react": "*"
4051+ }
4052+ },
40434053 "node_modules/react-is": {
40444054 "version": "16.13.1",
40454055 "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",
package.json+1−0
@@ -14,6 +14,7 @@
1414 "next": "14.2.4",
1515 "react": "^18",
1616 "react-dom": "^18",
17+ "react-icons": "^5.2.1",
1718 "usehooks-ts": "^3.1.0"
1819 },
1920 "devDependencies": {