-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConnectItem.js
271 lines (223 loc) · 6.93 KB
/
ConnectItem.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import React from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
TextInput,
TouchableOpacity,
TouchableHighlight,
} from 'react-native';
import { NativeRouter, Route, Link } from "react-router-native";
import {
RTCView,
RTCPeerConnection,
RTCIceCandidate,
mediaDevices
} from 'react-native-webrtc';
import QRCodeScanner from 'react-native-qrcode-scanner';
import { RNCamera } from 'react-native-camera';
import io from 'socket.io-client';
console.disableYellowBox = true;
export const socket = io('https://socket.vconf.net:8080',{autoConnect: false,transports: ['websocket']});
const iceServers = [
{ 'urls': 'stun:stun.l.google.com:19302' },
{ 'urls': 'turn:numb.viagenie.ca', 'username': 'numb-server@matraex.com', 'credential': 'pass2app@' }
];
let pcs={}
const sendQuickNotice = (code) => {
console.log("called")
socket.emit("quicknoticeevent","addacam",code,{name:"Vineeth Nambiar"})
}
const mediaStream = mediaDevices.getUserMedia({
audio: false,
video: {
mandatory: {
minWidth: 500, // Provide your own width, height and frame rate here
minHeight: 300,
minFrameRate: 30
},
facingMode: "user",
}})
state={
code:"",
stream:null,
toggleView:true,
path: 1,
}
const getMediaStream = () => new Promise((resolve,reject)=>{
resolve(mediaStream)
})
class ConnectItem extends React.Component{
constructor(props){
super(props)
this.state={
code:"",
stream:null,
toggleView:true,
path: 1,
}
}
componentDidMount = () => {
socket.connect()
socket.on('connect', () => {
console.log(socket.connected); // true
});
this.socketListeners();
}
componentWillUnmount = () => {
socket.on('disconnect', () => {
console.log(socket.connected); // false
});
}
socketListeners = () => {
socket.on('connect', () => console.log('Socket is connected'));
socket.on('connect_error',(error)=>console.log(error,'error'))
socket.on("quicknoticemessage",(type,code,message)=>{
console.log(type,code)
console.log(',message',message)
let payload = message;
let pc = this.getPc(message.from)
if(message.payload!=undefined)
payload=message.payload
else if(message.message!=undefined)
payload=message.message
if (payload.ice!=undefined&&payload.ice==null)
{
return
}
else if (payload.ice)
{
pc.addIceCandidate(new RTCIceCandidate(payload.ice)).catch(e => {
}); // at one point this was really confusing, when the remote side sends us an ice event, we need to add the candidate to our list
return
}
else if(payload=='requestdevice')
{
// mediaDevices.getUserMedia({
// audio: false,
// video: {
// mandatory: {
// minWidth: 500, // Provide your own width, height and frame rate here
// minHeight: 300,
// minFrameRate: 30
// },
// facingMode: "user",
// }}).then(stream=>{
// this.setState({stream,toggleView:true});
// // stream.getTracks().forEach(track=>{
// // console.log(pc.addTrack,'this is addtrack funct')
// // pc.addTrack(track, stream)
// // });
// pc.addStream(stream)
// }).then(()=>{
// return pc.createOffer()
// }).then((offer)=>{
// pc.setLocalDescription(offer).then(()=>{
// let sendoffer={to:message.from, payload:offer}
// socket.emit("quicknoticemessageregistered",type,code,sendoffer)
// });
// })
// .catch(function(err){
// })
getMediaStream().then(stream=>{
this.setState({stream,toggleView:true});
pc.addStream(stream);
}).then(()=>{
return pc.createOffer();
}).then((offer)=>{
pc.setLocalDescription(offer).then(()=>{
let sendoffer={to:message.from, payload:offer}
socket.emit("quicknoticemessageregistered", type, code, sendoffer)
});
}).catch(function(err){
console.log(err);
})
}
else if(payload.type=='answer'){
pc.setRemoteDescription(payload)
.catch(function(err){
console.trace() ;
})
}else
console.log("receveid invalid message",payload);
})
}
getPc = (id) => {
if(pcs[id]==undefined)
{
pcs[id] = new RTCPeerConnection({
"iceServers": iceServers
})
pcs[id].onicecandidate = (event)=>{
console.log(event.candidate,'icecandy')
socket.emit("quicknoticemessageregistered","addacam",this.state.code,{ice: event.candidate, to:id})
}
}
return pcs[id];
}
onSuccessScan = (e) => {
console.log(e, 'e')
const code = e.data.split('https://addcam.io/c/')[1]
console.log(code, 'code');
sendQuickNotice(code);
}
render() {
console.log(this.state.code, '170')
console.log(this.state.stream, '171')
if(this.state.stream){
this.props.stateStreamPush(this.state.stream);
this.setState({stream: ''})
}
console.log(this.props.stateStream[this.props.index], '176');
console.log(this.state.toggleView, '177')
let url = "https://www.radiantmediaplayer.com/media/big-buck-bunny-360p.mp4"
return (
<View style={{flex:3, height: 300, alignItems:'center'}}>
<TextInput placeholder="Place code here..." onChangeText={(code)=>this.setState({code})}
style={{borderWidth:1, width:'90%', height:34, borderRadius:4, borderColor:'#ccc'}}
/>
<Text>{this.props.index}</Text>
<TouchableOpacity onPress={()=>sendQuickNotice(this.state.code)} style={[styles.buttonStyles,{marginTop:10}]} >
<Text>Connect</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>this.setState({toggleView:false})} style={[styles.buttonStyles,{marginTop:5}]} >
<Text>Scan</Text>
</TouchableOpacity>
{!this.state.toggleView?
<QRCodeScanner
onRead={this.onSuccessScan}
/>:null
}
{this.props.stateStream[this.props.index]&&this.state.toggleView?
<RTCView style={{ width:720, height:960}} mirror streamURL={this.props.stateStream[this.props.index].toURL()}/>:null
}
</View>
)
}
}
const styles = StyleSheet.create({
buttonStyles:{
backgroundColor:'#ccc',
width:'90%',
height:34,
borderRadius:4,
justifyContent:'center',
alignItems:'center'
},
nav: {
flexDirection: "row",
justifyContent: "flex-end",
alignItems: "center",
borderTopWidth: 1,
borderColor: "#000",
},
navItem: {
flex: 1,
alignItems: "center",
paddingTop: 10
}
})
export default ConnectItem;