-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebrtc.js
66 lines (57 loc) · 1.61 KB
/
webrtc.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
function WebRTC() {
var that = this;
var ice = { "iceServers": [
{"url": "stun:stun.l.google.com:19302"}
]};
var mediaConstraints = {
mandatory: {
OfferToReceiveAudio: false,
OfferToReceiveVideo: false
}
};
var peer = new RTCPeerConnection(ice);
peer.onicecandidate = function(event) {
console.log(event, that.onReady);
if (event.candidate === null) {
var sdp = peer.localDescription;
if(that.onReady) that.onReady(sdp);
}
};
return {
createDC: function(channelName) {
var dc = peer.createDataChannel(channelName, {reliable: true});
return dc;
},
receiveDC: function(cb){
peer.ondatachannel = function(e){
cb(e.channel);
}
},
createOffer: function(cb) {
peer.createOffer(function(desc){
console.log("Started offer creation", peer.localDescription);
peer.setLocalDescription(desc, function () {}, function(){});
}, function(e){
console.log("error", e)
}, mediaConstraints);
that.onReady = cb;
},
receiveOfferAndGetAnswer: function(offer, cb){
peer.setRemoteDescription(offer, function(){
console.log("Starting answer");
peer.createAnswer(function(answer){
console.log("Done answer", answer);
peer.setLocalDescription(answer, function(){
console.log("Answer", answer);
});
}, function(e){
console.log("error", e);
});
});
that.onReady = cb;
},
receiveAnswer: function(answer){
peer.setRemoteDescription(answer, function () {}, function(){});
}
}
}