-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
83 lines (77 loc) · 2.89 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
<video id="localVideo" autoplay></video>
<video id="remoteVideo" autoplay></video>
<br>
<input type="text" id="server" value="ws://localhost:8765">
<input type="text" id="turn" value="stun:stun.l.google.com:19302">
<input id="connect" disabled type="button" value="connect">
<script>
//polyfills
var RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
var RTCSessionDescription = window.RTCSessionDescription || window.webkitRTCSessionDescription || window.mozRTCSessionDescription;
var RTCIceCandidate = window.RTCIceCandidate || window.webkitRTCIceCandidate || window.mozRTCIceCandidate;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
//request & load user's stream into videoA
navigator.getUserMedia({video: true}, function(stream) {
localVideo.src = URL.createObjectURL(stream);
server.disabled = false; //because https://bugzilla.mozilla.org/show_bug.cgi?id=685657
connect.disabled = false;
connect.onclick = function() {
connect.disabled = true;
server.disabled = true;
var socket = new WebSocket(server.value);
socket.onmessage = function(event) {
var message = JSON.parse(event.data);
switch (message.type) {
case 'SDP_Stage_1':
peerConnection.createOffer(function(offerDescription) {
peerConnection.setLocalDescription(offerDescription, function() {
socket.send(JSON.stringify({
type: 'offer',
data: offerDescription
}));
}, errorHandler);
}, errorHandler);
break;
case 'SDP_Stage_2':
peerConnection.setRemoteDescription(new RTCSessionDescription(message.data), function() {
peerConnection.createAnswer(function(answerDescription) {
peerConnection.setLocalDescription(answerDescription, function() {
socket.send(JSON.stringify({
type: 'answer',
data: answerDescription
}));
}, errorHandler);
}, errorHandler);
}, errorHandler);
break;
case 'SDP_Stage_3':
peerConnection.setRemoteDescription(new RTCSessionDescription(message.data), successHandler, errorHandler);
break;
case 'ice':
peerConnection.addIceCandidate(new RTCIceCandidate(message.data), successHandler, errorHandler);
break;
}
}
var peerConnection = new RTCPeerConnection({
'iceServers': [{'url': turn.value}]
});
peerConnection.onicecandidate = function(event) {
if (event.candidate)
socket.send(JSON.stringify({
type: 'ice',
data: event.candidate
}));
};
peerConnection.onaddstream = function(event) {
remoteVideo.src = URL.createObjectURL(event.stream);
};
peerConnection.addStream(stream);
};
}, errorHandler);
function errorHandler() {
console.log(arguments);
}
function successHandler() {
console.log(arguments);
}
</script>