-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathApp.tsx
150 lines (136 loc) · 3.88 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
import React, {useEffect, useRef, useState} from 'react';
import {Platform, ScrollView, Text, TouchableOpacity, View} from 'react-native';
import {
ChannelProfileType,
ClientRoleType,
createAgoraRtcEngine,
IRtcEngine,
RtcSurfaceView,
} from 'react-native-agora';
import styles from './components/Style';
import requestCameraAndAudioPermission from './components/Permission';
const config = {
appId: AgoraAppID,
token: '',
channelName: 'test',
};
const App = () => {
const _engine = useRef<IRtcEngine | null>(null);
const [isJoined, setJoined] = useState(false);
const [peerIds, setPeerIds] = useState<number[]>([]);
useEffect(() => {
if (Platform.OS === 'android') {
// Request required permissions from Android
requestCameraAndAudioPermission().then(() => {
console.log('requested!');
});
}
}, []);
/**
* @name init
* @description Create, Initialize and setup engine
*/
const init = async () => {
const {appId} = config;
_engine.current = await createAgoraRtcEngine();
_engine.current.initialize({appId});
_engine.current.setChannelProfile(
ChannelProfileType.ChannelProfileLiveBroadcasting,
);
_engine.current.setClientRole(ClientRoleType.ClientRoleBroadcaster);
_engine.current.enableVideo();
_engine.current.startPreview();
_engine.current.addListener('onUserJoined', (connection, uid) => {
console.log('UserJoined', connection, uid);
// If new user
if (peerIds.indexOf(uid) === -1) {
// Add peer ID to state array
setPeerIds(prev => [...prev, uid]);
}
});
_engine.current.addListener('onUserOffline', (connection, uid) => {
console.log('UserOffline', connection, uid);
// Remove peer ID from state array
setPeerIds(prev => prev.filter(id => id !== uid));
});
// If Local user joins RTC channel
_engine.current.addListener('onJoinChannelSuccess', connection => {
console.log('JoinChannelSuccess', connection);
// Set state variable to true
setJoined(true);
});
};
/**
* @name startCall
* @description Function to start the call
*/
const startCall = async () => {
// Join Channel using null token and channel name
await init();
await _engine.current?.joinChannel(config.token, config.channelName, 0, {});
};
/**
* @name endCall
* @description Function to end the call
*/
const endCall = async () => {
_engine.current?.leaveChannel();
_engine.current?.removeAllListeners();
try {
_engine.current?.release();
} catch (e) {
console.log('release error:', e);
}
setPeerIds([]);
setJoined(false);
};
const _renderVideos = () => {
return isJoined ? (
<View style={styles.fullView}>
<RtcSurfaceView
style={styles.max}
canvas={{
uid: 0,
}}
/>
{_renderRemoteVideos()}
</View>
) : null;
};
const _renderRemoteVideos = () => {
return (
<ScrollView
style={styles.remoteContainer}
contentContainerStyle={styles.padding}
horizontal={true}>
{peerIds.map(id => {
return (
<RtcSurfaceView
style={styles.remote}
canvas={{
uid: id,
}}
key={id}
/>
);
})}
</ScrollView>
);
};
return (
<View style={styles.max}>
<View style={styles.max}>
<View style={styles.buttonHolder}>
<TouchableOpacity onPress={startCall} style={styles.button}>
<Text style={styles.buttonText}> Start Call </Text>
</TouchableOpacity>
<TouchableOpacity onPress={endCall} style={styles.button}>
<Text style={styles.buttonText}> End Call </Text>
</TouchableOpacity>
</View>
{_renderVideos()}
</View>
</View>
);
};
export default App;