-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcall.js
78 lines (71 loc) · 2.63 KB
/
call.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
/* MIT License: https://webrtc-experiment.appspot.com/licence/ */
var call = function (config) {
var
peerConnection = PeerConnection(makePeerConfig()),
webSocket = new WebSocket('ws://' + document.location.host + '/');
// configure the signalling WebSocket
webSocket.onmessage = function (event) {
console.log("received a message: " + event.data);
onIncomingMessage(JSON.parse(event.data));
};
webSocket.push = webSocket.send;
webSocket.send = function (data) {
webSocket.push(JSON.stringify(data));
};
function onIncomingMessage(response) {
// the other client has sent me an offer SDP
if (response.offerSDP) {
console.log("received offerSDP " + response.offerSDP + ", will answer");
peerConnection.addStream(config.localStream);
peerConnection.createAnswer(response.offerSDP, function (sdp) {
console.log("sending answer SDP");
webSocket.send({
answerSDP: sdp
});
});
}
// the other client has sent me an answer SDP
if (response.answerSDP) {
peerConnection.setRemoteDescription(response.answerSDP);
}
// the other client has sent me an ICE candidate
if (response.candidate) {
console.log("got a candidate message, passing to RTCPeerConnection");
peerConnection.addICECandidate({
sdpMLineIndex: response.candidate.sdpMLineIndex,
candidate: response.candidate.candidate
});
}
}
// PeerConnection.js's options structure
function makePeerConfig() {
return {
onicecandidate: function (candidate) {
console.log("onICE");
webSocket.send({
candidate: {
sdpMLineIndex: candidate.sdpMLineIndex,
candidate: candidate.candidate
}
});
},
onaddstream: function (stream) {
console.log("onRemoteStream");
config.video['src'] = URL.createObjectURL(stream);
}
};
}
return {
initiateCall: function(localStream) {
// attach the stream to the peer connection
peerConnection.addStream(localStream);
// create the offer SDP and send it when it's ready
peerConnection.createOffer(function (sdp) {
console.log("sending offer SDP");
webSocket.send({
offerSDP: sdp
})
});
}
};
};