-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPeerConnection.js
83 lines (72 loc) · 2.92 KB
/
PeerConnection.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
/* MIT License: https://webrtc-experiment.appspot.com/licence/
2013, Muaz Khan<muazkh>--[github.com/muaz-khan]
Demo & Documentation: http://bit.ly/RTCPeerConnection-Documentation */
window.moz = !! navigator.mozGetUserMedia;
var PeerConnection = function (options) {
var PeerConnection = window.mozRTCPeerConnection || window.webkitRTCPeerConnection,
SessionDescription = window.mozRTCSessionDescription || window.RTCSessionDescription,
IceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;
// See https://gist.github.com/zziuni/3741933 for a list of public STUN servers
var iceServers = {
iceServers: [{ url: 'stun:stunserver.org' }]
};
var optional = {
optional: []
};
if (!moz) {
// See http://www.webrtc.org/interop under "Constraints / configurations issues."
optional.optional = [{
DtlsSrtpKeyAgreement: true
}
];
}
var peerConnection = new PeerConnection(iceServers, optional);
peerConnection.onicecandidate = function(event) {
if (!event.candidate) return;
options.onicecandidate(event.candidate);
}
peerConnection.onaddstream = function(event) {
console.log('------------onaddstream');
options.onaddstream(event.stream);
}
var constraints = options.constraints || {
optional: [],
mandatory: {
OfferToReceiveAudio: true,
OfferToReceiveVideo: true
}
};
if (moz) constraints.mandatory.MozDontOfferDataChannel = true;
return {
createOffer: function (callback) {
peerConnection.createOffer(function (sessionDescription) {
peerConnection.setLocalDescription(sessionDescription);
callback(sessionDescription);
}, null, constraints);
},
createAnswer: function (offerSDP, callback) {
peerConnection.setRemoteDescription(new SessionDescription(offerSDP));
peerConnection.createAnswer(function (sessionDescription) {
peerConnection.setLocalDescription(sessionDescription);
callback(sessionDescription);
}, null, constraints);
},
setRemoteDescription: function (sdp) {
console.log('--------adding answer sdp:');
console.log(sdp.sdp);
sdp = new SessionDescription(sdp);
peerConnection.setRemoteDescription(sdp);
},
addICECandidate: function (candidate) {
console.log("addICE: got candidate: " + candidate.candidate);
peerConnection.addIceCandidate(new IceCandidate({
sdpMLineIndex: candidate.sdpMLineIndex,
candidate: candidate.candidate
}));
},
addStream: function(stream) {
console.log("stream provided, attaching...");
peerConnection.addStream(stream);
}
};
};