forked from muaz-khan/WebRTC-Experiment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTCPeerConnection-v1.1.js
96 lines (69 loc) · 3.32 KB
/
RTCPeerConnection-v1.1.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
/* MIT License: https://webrtc-experiment.appspot.com/licence/ */
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.attachStream);
function onicecandidate(event) {
if (!event.candidate || !peerConnection) return;
if (options.onICE) options.onICE(event.candidate);
}
function onaddstream(event) {
event && options.onRemoteStream && options.onRemoteStream(event.stream);
}
function createOffer() {
if (!options.onOfferSDP) return;
peerConnection.createOffer(function(sessionDescription) {
/* opus? use it dear! */
options.isopus && (sessionDescription = codecs.opus(sessionDescription));
peerConnection.setLocalDescription(sessionDescription);
options.onOfferSDP(sessionDescription);
}, null, constraints);
}
createOffer();
function createAnswer() {
if (!options.onAnswerSDP) return;
peerConnection.setRemoteDescription(new SessionDescription(options.offerSDP));
peerConnection.createAnswer(function(sessionDescription) {
/* opus? use it dear! */
options.isopus && (sessionDescription = codecs.opus(sessionDescription));
peerConnection.setLocalDescription(sessionDescription);
options.onAnswerSDP(sessionDescription);
}, null, constraints);
}
createAnswer();
return {
/* offerer got answer sdp; MUST pass sdp over this function */
addAnswerSDP: function(sdp) {
peerConnection.setRemoteDescription(new 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);
}