This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
send_recv_av.js
393 lines (338 loc) · 11.4 KB
/
send_recv_av.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//
//
// initial code retrieved from the following link on 9/13/2017
// https://github.com/googlecodelabs/webrtc-web/blob/master/step-05/js/main.js
//
// initial code licensed with Apache License 2.0
//
'use strict';
var objects_received = [];
var objects_sent = [];
var isChannelReady = false;
var isStarted = false;
var localStream;
var pc;
var remoteStream;
var displayStream;
var turnReady;
var requestedRobot;
var dataChannel;
var dataConstraint;
// Free STUN server offered by Google
var pcConfig = {
'iceServers': [{
'urls': 'stun:stun.l.google.com:19302'
}]
};
// Prototype STUN and TURN server used internally by Hello Robot
//
// var pcConfig = {
// iceServers: [
// { urls: "stun:pilot.hello-robot.io:5349",
// username: "r1",
// credential: "kWJuyF5i2jh0"},
// { urls: "turn:pilot.hello-robot.io:5349",
// username: "r1",
// credential: "kWJuyF5i2jh0"}
// ]
// };
////////////////////////////////////////////////////////////
// safelyParseJSON code copied from
// https://stackoverflow.com/questions/29797946/handling-bad-json-parse-in-node-safely
// on August 18, 2017
function safelyParseJSON (json) {
// This function cannot be optimised, it's best to
// keep it small!
var parsed;
try {
parsed = JSON.parse(json);
} catch (e) {
// Oh well, but whatever...
}
return parsed; // Could be undefined!
}
////////////////////////////////////////////////////////////
/////////////////////////////////////////////
var socket = io.connect();
socket.on('created', function(room) {
console.log('Created room ' + room);
});
socket.on('full', function(room) {
console.log('Room ' + room + ' is full');
});
socket.on('join', function (room){
console.log('Another peer made a request to join room ' + room);
console.log('This peer is the ' + peer_name + '!');
isChannelReady = true;
maybeStart();
});
socket.on('joined', function(room) {
console.log('joined: ' + room);
isChannelReady = true;
});
////////////////////////////////////////////////
if (peer_name === 'OPERATOR') {
var robotToControlSelect = document.querySelector('select#robotToControl');
robotToControlSelect.onchange = connectToRobot;
}
function availableRobots() {
console.log('asking server what robots are available');
socket.emit('what robots are available');
}
function connectToRobot() {
var robot = robotToControlSelect.value;
if(robot === 'no robot connected') {
console.log('no robot selected');
console.log('attempt to hangup');
hangup();
} else {
console.log('attempting to connect to robot =');
console.log(robot);
requestedRobot = robot;
socket.emit('join', robot);
}
}
socket.on('available robots', function(available_robots) {
console.log('received response from the server with available robots');
console.log('available_robots =');
console.log(available_robots);
// remove any old options
while (robotToControlSelect.firstChild) {
robotToControlSelect.removeChild(robotToControlSelect.firstChild);
}
var option = document.createElement('option');
option.value = 'no robot connected';
option.text = 'no robot connected';
robotToControlSelect.appendChild(option);
// add all new options
for (let r of available_robots) {
option = document.createElement('option');
option.value = r;
option.text = r;
robotToControlSelect.appendChild(option);
}
});
///////////////////////////////////////////////////
function sendWebRTCMessage(message) {
console.log('Client sending WebRTC message: ', message);
socket.emit('webrtc message', message);
}
// This client receives a message
socket.on('webrtc message', function(message) {
console.log('Client received message:', message);
if (message === 'got user media') {
maybeStart();
} else if (message.type === 'offer') {
if ((peer_name === 'ROBOT') && !isStarted) {
maybeStart();
} else if ((peer_name === 'OPERATOR') && !isStarted) {
maybeStart();
}
pc.setRemoteDescription(new RTCSessionDescription(message));
doAnswer();
} else if (message.type === 'answer' && isStarted) {
pc.setRemoteDescription(new RTCSessionDescription(message));
} else if (message.type === 'candidate' && isStarted) {
var candidate = new RTCIceCandidate({
sdpMLineIndex: message.label,
candidate: message.candidate
});
pc.addIceCandidate(candidate);
} else if (message === 'bye' && isStarted) {
handleRemoteHangup();
}
});
////////////////////////////////////////////////////
var remoteVideo = document.querySelector('#remoteVideo');
function maybeStart() {
console.log('>>>>>>> maybeStart() ', isStarted, localStream, isChannelReady);
if (!isStarted && isChannelReady) {
console.log('>>>>>> creating peer connection');
createPeerConnection();
if (localStream != undefined) {
console.log('adding local media stream to peer connection');
pc.addStream(localStream);
}
console.log('This peer is the ' + peer_name + '.');
if (peer_name === 'ROBOT') {
dataConstraint = null;
dataChannel = pc.createDataChannel('DataChannel', dataConstraint);
console.log('Creating data channel.');
dataChannel.onmessage = onReceiveMessageCallback;
dataChannel.onopen = onDataChannelStateChange;
dataChannel.onclose = onDataChannelStateChange;
doCall();
}
isStarted = true;
}
}
window.onbeforeunload = function() {
sendWebRTCMessage('bye');
};
/////////////////////////////////////////////////////////
function createPeerConnection() {
try {
pc = new RTCPeerConnection(pcConfig);
pc.onicecandidate = handleIceCandidate;
pc.ondatachannel = dataChannelCallback;
pc.onopen = function() {
console.log('RTC channel opened.');
};
pc.onaddstream = handleRemoteStreamAdded;
pc.onremovestream = handleRemoteStreamRemoved;
console.log('Created RTCPeerConnnection');
} catch (e) {
console.log('Failed to create PeerConnection, exception: ' + e.message);
alert('Cannot create RTCPeerConnection object.');
return;
}
}
function handleIceCandidate(event) {
console.log('icecandidate event: ', event);
if (event.candidate) {
sendWebRTCMessage({
type: 'candidate',
label: event.candidate.sdpMLineIndex,
id: event.candidate.sdpMid,
candidate: event.candidate.candidate
});
} else {
console.log('End of candidates.');
}
}
function handleRemoteStreamAdded(event) {
console.log('Remote stream added.');
if (peer_name === 'OPERATOR') {
console.log('OPERATOR: starting to display remote stream');
remoteVideo.srcObject = event.stream;
} else if (peer_name === 'ROBOT') {
console.log('ROBOT: adding remote audio to display');
// remove audio tracks from displayStream
for (let a of displayStream.getAudioTracks()) {
displayStream.removeTrack(a);
}
var remoteaudio = event.stream.getAudioTracks()[0]; // get remotely captured audio track
displayStream.addTrack(remoteaudio); // add remotely captured audio track to the local display
videoDisplayElement.srcObject = displayStream;
}
remoteStream = event.stream;
}
function handleCreateOfferError(event) {
console.log('createOffer() error: ', event);
}
function doCall() {
console.log('Sending offer to peer');
pc.createOffer(setLocalAndSendMessage, handleCreateOfferError);
}
function doAnswer() {
console.log('Sending answer to peer.');
pc.createAnswer().then(
setLocalAndSendMessage,
onCreateSessionDescriptionError
);
}
function setLocalAndSendMessage(sessionDescription) {
pc.setLocalDescription(sessionDescription);
console.log('setLocalAndSendMessage sending message', sessionDescription);
sendWebRTCMessage(sessionDescription);
}
function onCreateSessionDescriptionError(error) {
console.log('Failed to create session description: ' + error.toString());
}
function handleRemoteStreamRemoved(event) {
console.log('Remote stream removed. Event: ', event);
}
function hangup() {
console.log('Hanging up.');
stop();
sendWebRTCMessage('bye');
}
function handleRemoteHangup() {
console.log('Session terminated.');
stop();
}
function stop() {
isStarted = false;
// isAudioMuted = false;
// isVideoMuted = false;
pc.close();
pc = null;
}
////////////////////////////////////////////////////////////
// RTCDataChannel
// on Sept. 15, 2017 copied initial code from
// https://github.com/googlecodelabs/webrtc-web/blob/master/step-03/js/main.js
// initial code licensed with Apache License 2.0
////////////////////////////////////////////////////////////
function sendData(obj) {
if (isStarted && (dataChannel.readyState === 'open')) {
var data = JSON.stringify(obj);
switch(obj.type) {
case 'command':
if (recordOn && addToCommandLog) {
addToCommandLog(obj);
}
objects_sent.push(obj);
dataChannel.send(data);
console.log('Sent Data: ' + data);
break;
case 'sensor':
// unless being recorded, don't store or write information to the console due to high
// frequency and large amount of data (analogous to audio and video).
dataChannel.send(data);
break;
default:
console.log('*************************************************************');
console.log('REQUEST TO SEND UNRECOGNIZED MESSAGE TYPE, SO NOTHING SENT...');
console.log('Received Data: ' + event.data);
console.log('Received Object: ' + obj);
console.log('*************************************************************');
}
}
}
function closeDataChannels() {
console.log('Closing data channels.');
dataChannel.close();
console.log('Closed data channel with label: ' + dataChannel.label);
console.log('Closed peer connections.');
}
function dataChannelCallback(event) {
console.log('Data channel callback executed.');
dataChannel = event.channel;
dataChannel.onmessage = onReceiveMessageCallback;
dataChannel.onopen = onDataChannelStateChange;
dataChannel.onclose = onDataChannelStateChange;
}
function onReceiveMessageCallback(event) {
var obj = safelyParseJSON(event.data);
switch(obj.type) {
case 'command':
objects_received.push(obj);
console.log('Received Data: ' + event.data);
//console.log('Received Object: ' + obj);
executeCommand(obj);
break;
case 'sensor':
// unless being recorded, don't store or write information to the console due to high
// frequency and large amount of data (analogous to audio and video).
if (recordOn && addToSensorLog) {
addToSensorLog(obj);
}
receiveSensorReading(obj);
break;
default:
console.log('*******************************************************');
console.log('UNRECOGNIZED MESSAGE TYPE RECEIVED, SO DOING NOTHING...');
console.log('Received Data: ' + event.data);
console.log('Received Object: ' + obj);
console.log('*******************************************************');
}
}
function onDataChannelStateChange() {
var readyState = dataChannel.readyState;
console.log('Data channel state is: ' + readyState);
if (readyState === 'open') {
runOnOpenDataChannel();
} else {
}
}