forked from muaz-khan/WebRTC-Experiment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTCPeerConnection.js
96 lines (69 loc) · 3.38 KB
/
RTCPeerConnection.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
84
85
86
87
88
89
90
91
92
93
94
95
96
console.log('Code by Muaz Khan: https://webrtc-experiment.appspot.com/');
window.PeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection || window.RTCPeerConnection;
window.SessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.RTCSessionDescription;
window.IceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate || window.RTCIceCandidate;
window.defaults = {
iceServers: { "iceServers": [{ "url": "stun:stun.l.google.com:19302" }] },
constraints: { 'mandatory': { 'OfferToReceiveAudio': true, 'OfferToReceiveVideo': true } }
};
var RTCPeerConnection = function(options) {
var iceServers = options.iceServers || defaults.iceServers;
var constraints = options.constraints || defaults.constraints;
var peerConnection = new PeerConnection(iceServers);
peerConnection.onicecandidate = onicecandidate;
peerConnection.onaddstream = onaddstream;
peerConnection.addStream(options.stream);
function onicecandidate(event) {
if (!event.candidate || !peerConnection) return;
if (options.getice) options.getice(event.candidate);
}
function onaddstream(event) {
options.gotstream && options.gotstream(event);
}
function createOffer() {
if (!options.onoffer) return;
peerConnection.createOffer(function(sessionDescription) {
/* opus? use it dear! */
options.isopus && (sessionDescription = codecs.opus(sessionDescription));
peerConnection.setLocalDescription(sessionDescription);
options.onoffer(sessionDescription);
}, null, constraints);
}
createOffer();
function createAnswer() {
if (!options.onanswer) return;
peerConnection.setRemoteDescription(new window.SessionDescription(options.offer));
peerConnection.createAnswer(function(sessionDescription) {
/* opus? use it dear! */
options.isopus && (sessionDescription = codecs.opus(sessionDescription));
peerConnection.setLocalDescription(sessionDescription);
options.onanswer(sessionDescription);
}, null, constraints);
}
createAnswer();
return {
/* offerer got answer sdp; MUST pass sdp over this function */
onanswer: function(sdp) {
peerConnection.setRemoteDescription(new window.SessionDescription(sdp));
},
/* got ICE from other end; MUST pass those candidates over this function */
addice: function(candidate) {
peerConnection.addIceCandidate(new IceCandidate({
sdpMLineIndex: candidate.sdpMLineIndex,
candidate: candidate.candidate
}));
}
};
};
var URL = window.webkitURL || window.URL;
navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.getUserMedia;
function getUserMedia(options) {
navigator.getUserMedia(options.constraints || { audio: true, video: true },
function (stream) {
if (options.video)
if (!navigator.mozGetUserMedia) options.video.src = URL.createObjectURL(stream);
else options.video.mozSrcObject = stream;
options.onsuccess && options.onsuccess(stream);
return stream;
}, options.onerror);
}