-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
153 lines (144 loc) · 3.9 KB
/
App.js
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
import {
View,
SafeAreaView,
StyleSheet,
Text,
Button,
TextInput,
} from "react-native";
import React, { useEffect, useState, useCallback } from "react";
import Daily, {
DailyMediaView,
DailyEventObjectParticipant,
} from "@daily-co/react-native-daily-js";
const styles = StyleSheet.create({
safeArea: {
flex: 1,
backgroundColor: "#f7f9fa",
width: "100%",
},
outCallContainer: {
flex: 1,
justifyContent: "center",
alignItems: "center",
},
inCallContainer: {
position: "absolute",
width: "100%",
height: "100%",
},
dailyMediaView: {
flex: 1,
aspectRatio: 9 / 16,
},
roomUrlInput: {
borderRadius: 8,
marginVertical: 8,
padding: 12,
fontStyle: "normal",
fontWeight: "normal",
borderWidth: 1,
width: "100%",
},
infoView: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
controlButton: {
flex: 1,
},
});
const ROOM_URL_TEMPLATE = "https://YOUR_DOMAIN.daily.co/YOUR_ROOM";
export default function App() {
const [videoTrack, setVideoTrack] = useState();
const [callObject, setCallObject] = useState();
const [inCall, setInCall] = useState(false);
const [roomUrl, setRoomUrl] = useState(ROOM_URL_TEMPLATE);
const [remoteParticipantCount, setRemoteParticipantCount] = useState(0);
const handleNewParticipantsState = (event: DailyEventObjectParticipant) => {
const participant = event.participant;
// Early out as needed to avoid display the local participant's video
if (participant.local) {
return;
}
const videoTrack = participant.tracks.video;
setVideoTrack(videoTrack.persistentTrack);
// Set participant count minus the local participant
setRemoteParticipantCount(callObject.participantCounts().present - 1);
};
const joinRoom = () => {
console.log("Joining room");
callObject.join({
url: roomUrl,
});
};
const leaveRoom = async () => {
console.log("Leaving the room");
await callObject.leave();
};
// Create the callObject and join the meeting
useEffect(() => {
const callObject = Daily.createCallObject();
setCallObject(callObject);
return () => {};
}, []);
//Add the listeners
useEffect(() => {
if (!callObject) {
return;
}
callObject
.on("joined-meeting", () => setInCall(true))
.on("left-meeting", () => setInCall(false))
.on("participant-joined", handleNewParticipantsState)
.on("participant-updated", handleNewParticipantsState)
.on("participant-left", handleNewParticipantsState);
return () => {};
}, [callObject]);
return (
<SafeAreaView style={styles.safeArea}>
{inCall ? (
<View style={styles.inCallContainer}>
{remoteParticipantCount > 0 ? (
<DailyMediaView
videoTrack={videoTrack}
mirror={false}
objectFit="cover"
style={styles.dailyMediaView}
/>
) : (
<View style={styles.infoView}>
<Text>No one else is in the call yet!</Text>
<Text>Invite others to join the call using this link:</Text>
<Text>{roomUrl}</Text>
</View>
)}
<Button
style={styles.controlButton}
onPress={() => leaveRoom()}
title="Leave call"
></Button>
</View>
) : (
<View style={styles.outCallContainer}>
<View style={styles.infoView}>
<Text>Not in a call yet</Text>
<TextInput
style={styles.roomUrlInput}
value={roomUrl}
onChangeText={(newRoomURL) => {
setRoomUrl(newRoomURL);
}}
/>
<Button
style={styles.controlButton}
onPress={() => joinRoom()}
title="Join call"
></Button>
</View>
</View>
)}
</SafeAreaView>
);
}