This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
/
App.tsx
207 lines (184 loc) · 6 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import React, { useEffect, useState } from "react";
import {
AgoraVideoPlayer,
createClient,
createMicrophoneAndCameraTracks,
ClientConfig,
IAgoraRTCRemoteUser,
ICameraVideoTrack,
IMicrophoneAudioTrack,
} from "agora-rtc-react";
const config: ClientConfig = {
mode: "rtc", codec: "vp8",
};
const appId: string = ""; //ENTER APP ID HERE
const token: string | null = null;
const App = () => {
const [inCall, setInCall] = useState(false);
const [channelName, setChannelName] = useState("");
return (
<div>
<h1 className="heading">Agora RTC NG SDK React Wrapper</h1>
{inCall ? (
<VideoCall setInCall={setInCall} channelName={channelName} />
) : (
<ChannelForm setInCall={setInCall} setChannelName={setChannelName} />
)}
</div>
);
};
// the create methods in the wrapper return a hook
// the create method should be called outside the parent component
// this hook can be used the get the client/stream in any component
const useClient = createClient(config);
const useMicrophoneAndCameraTracks = createMicrophoneAndCameraTracks();
const VideoCall = (props: {
setInCall: React.Dispatch<React.SetStateAction<boolean>>;
channelName: string;
}) => {
const { setInCall, channelName } = props;
const [users, setUsers] = useState<IAgoraRTCRemoteUser[]>([]);
const [start, setStart] = useState<boolean>(false);
// using the hook to get access to the client object
const client = useClient();
// ready is a state variable, which returns true when the local tracks are initialized, untill then tracks variable is null
const { ready, tracks } = useMicrophoneAndCameraTracks();
useEffect(() => {
// function to initialise the SDK
let init = async (name: string) => {
client.on("user-published", async (user, mediaType) => {
await client.subscribe(user, mediaType);
console.log("subscribe success");
if (mediaType === "video") {
setUsers((prevUsers) => {
return [...prevUsers, user];
});
}
if (mediaType === "audio") {
user.audioTrack?.play();
}
});
client.on("user-unpublished", (user, type) => {
console.log("unpublished", user, type);
if (type === "audio") {
user.audioTrack?.stop();
}
if (type === "video") {
setUsers((prevUsers) => {
return prevUsers.filter((User) => User.uid !== user.uid);
});
}
});
client.on("user-left", (user) => {
console.log("leaving", user);
setUsers((prevUsers) => {
return prevUsers.filter((User) => User.uid !== user.uid);
});
});
await client.join(appId, name, token, null);
if (tracks) await client.publish([tracks[0], tracks[1]]);
setStart(true);
};
if (ready && tracks) {
console.log("init ready");
init(channelName);
}
}, [channelName, client, ready, tracks]);
return (
<div className="App">
{ready && tracks && (
<Controls tracks={tracks} setStart={setStart} setInCall={setInCall} />
)}
{start && tracks && <Videos users={users} tracks={tracks} />}
</div>
);
};
const Videos = (props: {
users: IAgoraRTCRemoteUser[];
tracks: [IMicrophoneAudioTrack, ICameraVideoTrack];
}) => {
const { users, tracks } = props;
return (
<div>
<div id="videos">
{/* AgoraVideoPlayer component takes in the video track to render the stream,
you can pass in other props that get passed to the rendered div */}
<AgoraVideoPlayer style={{height: '95%', width: '95%'}} className='vid' videoTrack={tracks[1]} />
{users.length > 0 &&
users.map((user) => {
if (user.videoTrack) {
return (
<AgoraVideoPlayer style={{height: '95%', width: '95%'}} className='vid' videoTrack={user.videoTrack} key={user.uid} />
);
} else return null;
})}
</div>
</div>
);
};
export const Controls = (props: {
tracks: [IMicrophoneAudioTrack, ICameraVideoTrack];
setStart: React.Dispatch<React.SetStateAction<boolean>>;
setInCall: React.Dispatch<React.SetStateAction<boolean>>;
}) => {
const client = useClient();
const { tracks, setStart, setInCall } = props;
const [trackState, setTrackState] = useState({ video: true, audio: true });
const mute = async (type: "audio" | "video") => {
if (type === "audio") {
await tracks[0].setEnabled(!trackState.audio);
setTrackState((ps) => {
return { ...ps, audio: !ps.audio };
});
} else if (type === "video") {
await tracks[1].setEnabled(!trackState.video);
setTrackState((ps) => {
return { ...ps, video: !ps.video };
});
}
};
const leaveChannel = async () => {
await client.leave();
client.removeAllListeners();
// we close the tracks to perform cleanup
tracks[0].close();
tracks[1].close();
setStart(false);
setInCall(false);
};
return (
<div className="controls">
<p className={trackState.audio ? "on" : ""}
onClick={() => mute("audio")}>
{trackState.audio ? "MuteAudio" : "UnmuteAudio"}
</p>
<p className={trackState.video ? "on" : ""}
onClick={() => mute("video")}>
{trackState.video ? "MuteVideo" : "UnmuteVideo"}
</p>
{<p onClick={() => leaveChannel()}>Leave</p>}
</div>
);
};
const ChannelForm = (props: {
setInCall: React.Dispatch<React.SetStateAction<boolean>>;
setChannelName: React.Dispatch<React.SetStateAction<string>>;
}) => {
const { setInCall, setChannelName } = props;
return (
<form className="join">
{appId === '' && <p style={{color: 'red'}}>Please enter your Agora App ID in App.tsx and refresh the page</p>}
<input type="text"
placeholder="Enter Channel Name"
onChange={(e) => setChannelName(e.target.value)}
/>
<button onClick={(e) => {
e.preventDefault();
setInCall(true);
}}>
Join
</button>
</form>
);
};
export default App;