-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.html
468 lines (402 loc) · 11.8 KB
/
index.html
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<!--
Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
Use of this source code is governed by a BSD-style license
that can be found in the LICENSE file in the root of the source
tree. An additional intellectual property rights grant can be found
in the file PATENTS. All contributing project authors may
be found in the AUTHORS file in the root of the source tree.
-->
<html>
<head>
<title>WebRTC Test</title>
<style type="text/css">
body, input, button, select, table {
font-family:"Lucida Grande", "Lucida Sans", Verdana, Arial, sans-serif;
font-size: 13 px;
}
body, input:enable, button:enable, select:enable, table {
color: rgb(51, 51, 51);
}
h1 {font-size: 40 px;}
</style>
<script src="http://172.16.154.246:8080/socket.io/socket.io.js"></script>
<script type="text/javascript">
// TODO: Catch more exceptions
var server='http://172.16.154.246:8080';
var myId = -1;
var myName;
var remoteId = -1;
var remoteName;
var request = null;
var hangingGet = [];
var pc = [];
var localStream = null;
var disconnecting = false;
var callState = 0; // 0 - Not started, 1 - Call ongoing
var autoCallTries = 0;
var socket = io.connect(server);
function getUserMedia() {
try {
navigator.webkitGetUserMedia({audio:true, video:true}, gotStream, gotStreamFailed);
console.log("Requested access to local media");
} catch (e) {
console.error(e, "getUserMedia error");
}
}
function setCallState(state) {
console.log("Changing call state: " + callState + " -> " + state);
callState = state;
}
// Local stream generation
function gotStream(s) {
var url = webkitURL.createObjectURL(s);
document.getElementById("localView").src = url;
console.log("User has granted access to local media. url = " + url);
localStream = s;
}
function gotStreamFailed(error) {
alert("Failed to get access to local media. Error code was " + error.code +
".");
console.warn("Failed to get access to local media. ", error);
}
// Peer list and remote peer handling
function peerExists(id) {
try {
var peerList = document.getElementById("peers");
for (var i = 0; i < peerList.length; i++) {
if (parseInt(peerList.options[i].value, 10) == id)
return true;
}
} catch (e) {
console.error(e, "Error searching for peer");
}
return false;
}
function removePeer(id) {
try {
var peerList = document.getElementById("peers");
for (var i = 0; i < peerList.length; i++) {
if (parseInt(peerList.options[i].value) == id) {
peerList.remove(i);
break;
}
}
} catch (e) {
console.error(e, "Error removing peer");
}
}
function clearPeerList() {
var peerList = document.getElementById("peers");
while (peerList.length > 0)
peerList.remove(0);
}
function setSelectedPeer(id) {
try {
var peerList = document.getElementById("peers");
for (var i = 0; i < peerList.length; i++) {
if (parseInt(peerList.options[i].value) == id) {
peerList.options[i].selected = true;
return true;
}
}
} catch (e) {
console.error(e, "Error setting selected peer");
}
return false;
}
function getPeerName(id) {
try {
var peerList = document.getElementById("peers");
for (var i = 0; i < peerList.length; i++) {
if (parseInt(peerList.options[i].value) == id) {
return peerList.options[i].text;
}
}
} catch (e) {
console.error(e, "Error finding peer name");
return;
}
return;
}
function getPeerId(peer_name) {
try {
var peerList = document.getElementById("peers");
for (var i = 0; i < peerList.length; i++) {
if (peerList.options[i].text == peer_name) {
return parseInt(peerList.options[i].value);
}
}
} catch (e) {
console.error(e, "Error finding peer ID");
return -1;
}
return -1;
}
function storeRemoteInfo() {
try {
var peerList = document.getElementById("peers");
if (peerList.selectedIndex < 0) {
alert("Please select a peer.");
return false;
} else
remoteId = parseInt(peerList.options[peerList.selectedIndex].value);
remoteName = peerList.options[peerList.selectedIndex].text;
} catch (e) {
console.error(e, "Error storing remote peer info");
return false;
}
return true;
}
// Call control
function createPeerConnection(peerId) {
if (pc[peerId]) {
console.warn("PeerConnection object already exists");
}
console.log("Creating PeerConnection object");
try {
function onSignalingMessage(msg) {
console.log("Sending message to ID : " + peerId + "):\n" + msg);
sendToPeer(peerId, msg);
}
pc[peerId] = new webkitDeprecatedPeerConnection("STUN stun.l.google.com:19302",
onSignalingMessage);
pc[peerId].onaddstream = onAddStream;
pc[peerId].onremovestream = onRemoveStream;
} catch (e) {
console.error(e, "Create PeerConnection error");
}
}
function doCall() {
if (!storeRemoteInfo())
return;
document.getElementById("call").disabled = false;
//document.getElementById("peers").disabled = true;
createPeerConnection(remoteId);
console.warn("PeerConnectionCreated by doCall()");
console.log("Adding stream");
pc[remoteId].addStream(localStream);
document.getElementById("hangup").disabled = false;
setCallState(1);
}
function hangUp(peerId) {
document.getElementById("hangup").disabled = true;
console.log("Sending BYE to " + peerId + " (ID " + peerId + ")");
sendToPeer(peerId, "BYE");
closeCall(peerId);
}
function closeCall(peerId) {
console.log("Stopping showing remote stream for Peer ID : "+peerId);
//window.location.reload();
if (pc[peerId]) {
console.log("Stopping call [pc.close()]");
pc[peerId].close();
pc[peerId] = null;
} else
console.log("No pc object to close");
remoteId = -1;
document.getElementById("call").disabled = false;
document.getElementById("peers").disabled = false;
setCallState(0);
}
// PeerConnection callbacks
function onAddStream(e) {
var stream = e.stream;
var url = webkitURL.createObjectURL(stream);
var remoteVideoDiv = document.getElementById("remote");
remoteVideoDiv.innerHTML += '<video width="320" height="240" autoplay="autoplay"><source src="'+url+'"/></video>'
console.log("Started showing remote stream. url = " + url, e);
}
function onRemoveStream(e) {
// Currently if we get this callback, call has ended.
// document.getElementById("remoteView").src = "";
console.log("Stopped showing remote stream");
}
fetchUserlist = function(my_id, peers) {
myId = my_id;
console.log("My id: " + myId);
clearPeerList();
var added = 0;
for (var i = 0; i < peers.length; ++i) {
if (peers[i].length > 0 && i!=myId) {
console.log("Peer " + i + ": " + peers[i]);
addPeer(i, peers[i]);
++added;
}
}
if (added == 0) {
addPeer(-1, "No other peer connected");
document.getElementById("peers").disabled = true;
}
else {
document.getElementById("peers").disabled = false;
document.getElementById("call").disabled = false;
}
document.getElementById("connect").disabled = true;
document.getElementById("disconnect").disabled = false;
}
function connect() {
myName = document.getElementById("name").value.toLowerCase();
server = document.getElementById("server").value.toLowerCase();
if (myName.length == 0) {
alert("I need a name please.");
document.getElementById("local").focus();
} else {
// TODO: Disable connect button here, but we need a timeout and check if we
// have connected, if so enable it again.
signIn();
}
}
socket.on('connect', function () {
socket.on('error',function(error) {
alert(error);
});
// General
addPeer = function (id, pname) {
try {
if(id == myId) return;
var peerList = document.getElementById("peers");
var option = document.createElement("option");
option.text = pname;
option.value = id;
peerList.add(option);
} catch (e) {
console.error(e, "Error adding peer");
}
}
socket.on('addPeer', function(my_id, userList) {
fetchUserlist(my_id, userList);
});
// TODO: Add callbacks onconnecting, onopen and onstatechange.
// Server interaction
socket.on('receiveMessage', function(remoteId, content) {
var peerName = getPeerName(remoteId);
if(!pc[remoteId]) {
if(!setSelectedPeer(remoteId)) {
console.warn("Received message from unknown peer, ignoring...");
return;
}
if(!confirm("You have a call coming in from "+peerName+". Accept Call?")) { // Reject Call
socket.emit('reject', myId, remoteId);
console.error("Call Rejected!");
}
if (!storeRemoteInfo())
return;
document.getElementById("call").disabled = false;
document.getElementById("peers").disabled = false;
createPeerConnection(remoteId);
console.warn("PeerConnectionCreated by handlePeerMessage()");
try {
pc[remoteId].processSignalingMessage(content);
}
catch(e) {
console.error(e, "Process signaling message error");
}
console.log("Adding Stream");
pc[remoteId].addStream(localStream);
document.getElementById("hangup").disabled = false;
}
else {
if(content == 'REJECT') {
alert("The peer has rejected your call!");
console.log("The peer rejected the call");
document.getElementById("hangup").disabled = true;
closeCall(remoteid);
return;
}
try {
pc[remoteId].processSignalingMessage(content);
} catch (e) {
console.error(e, "Process signaling message error");
}
}
});
sendToPeer = function(peer_id, data) {
socket.emit('sendMessage',myId, peer_id, data);
}
signIn = function() {
socket.emit('signIn', myName);
socket.on('ready', function(my_id, userList) {
fetchUserlist(my_id, userList);
});
}
function disconnect() {
if (callState == 1)
hangUp();
disconnecting = true;
clearPeerList();
addPeer(-1, "Not connected");
document.getElementById("connect").disabled = false;
document.getElementById("disconnect").disabled = true;
document.getElementById("peers").disabled = true;
document.getElementById("call").disabled = true;
disconnecting = false;
}
});
// Window event handling
window.onload = function() {
if (navigator.webkitGetUserMedia) {
document.getElementById('testApp').hidden = false;
getUserMedia();
} else {
document.getElementById('errorText').hidden = false;
}
}
//window.onbeforeunload = disconnect();
</script>
</head>
<body>
<h1>WebRTC</h1>
<section id="errorText" hidden="true">
Could not detect WebRTC support.<p>
You must have WebRTC enabled Chrome browser to use this test page. The browser
must be started with the --enable-media-stream command line flag. For more
information, please see
<a href="https://sites.google.com/site/webrtc/blog/webrtcnowavailableinthechromedevchannel">
this blog post</a>.
</section>
<section id="testApp" hidden="true">
Local Preview :
<video width="320" height="240" id="localView" autoplay="autoplay"></video>
<div id="remote">
</div>
<table border="0">
<tr>
<td valign="top">
<table border="0" cellpaddning="0" cellspacing="0">
<tr>
<td>Server:</td>
<td>
<input type="text" id="server" size="30" value="http://172.16.154.246:8080"/>
</td>
</tr>
<tr>
<td>Name:</td><td><input type="text" id="name" size="30" value="name"/></td>
</tr>
</table>
</td>
<td valign="top">
<button id="connect" onclick="connect();">Connect</button><br>
<button id="disconnect" onclick="disconnect();" disabled="true">Disconnect
</button>
</td>
<td> </td>
<td valign="top">
Connected peers:<br>
<select id="peers" size="5" disabled="true">
<option value="-1">Not connected</option>
</select>
</td>
<td valign="top">
<!--input type="text" id="peer_id" size="3" value="1"/><br-->
<button id="call" onclick="doCall();" disabled="true">Call</button><br>
<button id="hangup" onclick="hangUp();" disabled="true">Hang up</button><br>
</td>
</tr>
</table>
<button onclick="document.getElementById('debug').innerHTML='';">Clear log
</button>
<pre id="debug"></pre>
</section>
</body>
</html>