This repository has been archived by the owner on Sep 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathApp.js
218 lines (194 loc) · 5.44 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
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
208
209
210
211
212
213
214
215
216
217
218
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
Platform,
Button,
StyleSheet,
Text,
View,
Image,
findNodeHandle,
NativeEventEmitter,
NativeModules,
UIManager,
DeviceEventEmitter
} from 'react-native';
import AgoraRtcEngine from './components/AgoraRtcEngineModule'
import AgoraRendererView from './components/AgoraRendererView'
const agoraKitEmitter = new NativeEventEmitter(AgoraRtcEngine);
var isSpeakerPhone = false;
//provide this url if you want rtmp relevant features
var cdn_url = "YOUR_CDN_URL"
export default class App extends Component {
state = {
localStream: null,
remoteStreams: []
}
viewRegistry = {}
listeners = []
componentDidMount() {
this.registerCallbacks();
AgoraRtcEngine.createEngine("YOUR_APP_ID");
AgoraRtcEngine.enableVideo();
AgoraRtcEngine.enableAudio();
AgoraRtcEngine.setVideoProfileDetail(360, 640, 15, 300);
}
componentDidUpdate() {
//the view registry could be changed after render update
//resetup remote views here
let remoteStreams = this.state.remoteStreams || [];
for (let i = 0; i < remoteStreams.length; i++) {
let stream = remoteStreams[i];
let { uid } = stream;
AgoraRtcEngine.setRemoteVideoView(this.viewRegistry[uid], uid, AgoraRtcEngine.AgoraVideoRenderModeFit)
}
}
// Agora Action
_joinChannel() {
AgoraRtcEngine.setLocalVideoView(this.viewRegistry["local"], AgoraRtcEngine.AgoraVideoRenderModeFit);
AgoraRtcEngine.setChannelProfile(1);
AgoraRtcEngine.setClientRole(1);
AgoraRtcEngine.setVideoProfile(AgoraRtcEngine.AgoraVideoProfileDEFAULT, true);
AgoraRtcEngine.startPreview();
AgoraRtcEngine.joinChannel(null, "rnchannel01", "React Native for Agora RTC SDK", 0);
}
_leaveChannel() {
AgoraRtcEngine.stopPreview();
AgoraRtcEngine.leaveChannel();
}
_switchCamera() {
AgoraRtcEngine.switchCamera();
}
_switchAudio() {
AgoraRtcEngine.setEnableSpeakerphone(isSpeakerPhone);
isSpeakerPhone = !isSpeakerPhone;
}
_startPublish() {
AgoraRtcEngine.addPublishStreamUrl(cdn_url, false)
}
_stopPublish() {
AgoraRtcEngine.removePublishStreamUrl(cdn_url)
}
render() {
let remoteViews = this.state.remoteStreams.map(stream => {
let { uid } = stream;
return (
<AgoraRendererView
ref={component => this.viewRegistry[uid] = component}
key={uid}
style={{ width: 360, height: 240 }}
/>
)
})
return (
<View style={styles.container} >
<AgoraRendererView key="local"
ref={component => this.viewRegistry["local"] = component}
style={{ width: 360, height: 240 }}
/>
{remoteViews}
<View style={{ flexDirection: 'row' }}>
<Button style={{ flex: 1 }}
onPress={this._joinChannel.bind(this)}
title="Join Channel"
style={{ width: 180, float: "left", backgroundColor: "rgb(0,0,0)" }}
color="#841584"
/>
<Button style={{ flex: 1 }}
onPress={this._leaveChannel.bind(this)}
title="Leave Channel"
color="#841584"
style={{ width: 180, float: "left" }}
/>
</View>
<View style={{ flexDirection: 'row' }}>
<Button
onPress={this._switchCamera.bind(this)}
title="Switch Camera"
color="#841584"
/>
<Button
onPress={this._switchAudio.bind(this)}
title="Switch Audio Route"
color="#841584"
/>
</View>
<View style={{ flexDirection: 'row' }}>
<Button
onPress={this._startPublish.bind(this)}
title="Start publish"
color="#841584"
/>
<Button
onPress={this._stopPublish.bind(this)}
title="Stop publish"
color="#841584"
/>
</View>
</View>
);
}
registerCallbacks() {
let listeners = this.listeners || [];
// Aogra CallBack
listeners.push(agoraKitEmitter.addListener(
'RemoteDidJoinedChannel',
(notify) => {
//update state
let remoteStreams = this.state.remoteStreams || [];
remoteStreams.push({ uid: notify.uid })
this.setState({ remoteStreams })
}
));
listeners.push(agoraKitEmitter.addListener(
'RemoteDidOfflineOfUid',
(notify) => {
//remove stream and update state
let remoteStreams = this.state.remoteStreams || [];
remoteStreams = remoteStreams.filter(s => s.uid !== notify.uid)
this.setState({ remoteStreams })
}
));
listeners.push(agoraKitEmitter.addListener(
'StreamPublished',
(notify) => {
console.log(`stream published ${notify.errorCode}`)
}
));
listeners.push(agoraKitEmitter.addListener(
'StreamUnpublished',
(notify) => {
console.log(`stream unpublished`)
}
));
}
componentWillUnmount() {
this.listeners.forEach(i => i.remove())
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
thumbnail: {
width: 53,
height: 81,
},
});