-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
173 lines (155 loc) · 4.96 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
/**
*
* @format
* @flow strict-local
*/
import React, {useState, useEffect} from 'react';
import type {Node} from 'react';
import {
SafeAreaView,
ScrollView,
Dimensions,
useColorScheme,
View,
} from 'react-native';
import {Colors} from 'react-native/Libraries/NewAppScreen';
import Peer from 'react-native-peerjs';
import {RTCView, mediaDevices} from 'react-native-webrtc';
// TODO: Configure peer server
const PEER_SERVER = {
secure: true, // Your configuration is here
host: 'peerjs-server.xxx.yyy', // Your configuration is here
port: 443, // Your configuration is here
path: '/', // Your configuration is here
key: 'peerjs', // Optional, your configuration is here
};
const {width, height} = Dimensions.get('window');
const App: () => Node = () => {
const isDarkMode = useColorScheme() === 'dark';
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
flex: 1,
};
const [localPeer] = useState(() => new Peer(undefined, PEER_SERVER));
const [localStream, setLocalStream] = useState();
const [remoteStreams, setRemoteStreams] = useState();
useEffect(() => {
console.log('Local peer is opening ...');
let isFront = true;
mediaDevices.enumerateDevices().then(sourceInfos => {
let videoSourceId;
for (let i = 0; i < sourceInfos.length; i++) {
const sourceInfo = sourceInfos[i];
if (
sourceInfo.kind == 'videoinput' &&
sourceInfo.facing == (isFront ? 'front' : 'environment')
) {
videoSourceId = sourceInfo.deviceId;
}
}
mediaDevices
.getUserMedia({
audio: false,
video: {
mandatory: {
minFrameRate: 30,
},
facingMode: isFront ? 'user' : 'environment',
optional: videoSourceId ? [{sourceId: videoSourceId}] : [],
},
})
.then(stream => {
setLocalStream(stream);
localPeer.on('open', peerId => {
console.log('Local peer is opened with Id = ' + peerId);
localPeer.on('call', call => {
console.log('Local peer receives incoming call');
call.answer(stream);
call?.on('stream', remoteStream => {
console.log('Local peer receives stream from remote peer');
setRemoteStreams([remoteStream, remoteStream, remoteStream]);
});
});
console.log('Local peer will connect to remote peer');
const outgoingCall = localPeer.call(
'A valid remote connected peer ID', // TODO: Your must specify a valid remote connected peer ID here to simulate video call
stream,
);
outgoingCall.on('stream', remoteStream => {
console.log('Local peer receives stream from remote peer');
setRemoteStreams([remoteStream, remoteStream, remoteStream]);
});
});
})
.catch(error => {
console.log(error);
});
});
return () => {
if (localPeer) {
localPeer.destroy();
console.log('Local peer is destroyed');
}
};
}, []);
return (
<SafeAreaView style={backgroundStyle}>
<View style={{flex: 1}}>
<View
style={{
flex: 0.7,
justifyContent: 'center',
alignItems: 'center',
}}>
{localStream ? (
<RTCView
streamURL={localStream.toURL()}
style={{
width: width * 0.9 - 8,
height: 200,
}}
/>
) : null}
</View>
<ScrollView
horizontal
style={{flex: 0.8}}
showsHorizontalScrollIndicator={false}>
<>
{remoteStreams ? (
remoteStreams.length > 0 ? (
<>
{remoteStreams.map((stream, index) => {
return (
<View
key={index}
style={{
width: width * 0.9,
marginRight: 8,
justifyContent: 'center',
alignItems: 'center',
}}>
<RTCView
streamURL={stream.toURL()}
style={{
width: width * 0.9 - 8,
height: 300,
}}
/>
</View>
);
})}
</>
) : null
) : null}
</>
</ScrollView>
<View
style={{
flex: 0.5,
}}></View>
</View>
</SafeAreaView>
);
};
export default App;