forked from muaz-khan/WebRTC-Experiment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTCPeerConnection-v1.2.js
142 lines (109 loc) · 5.1 KB
/
RTCPeerConnection-v1.2.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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 } },
optional: { optional: [{ RtpDataChannels: true}] }
};
var RTCPeerConnection = function (options) {
var iceServers = options.iceServers || defaults.iceServers;
var constraints = options.constraints || defaults.constraints;
var optional = options.optional || defaults.optional;
var peerConnection = new PeerConnection(iceServers, optional);
openDataChannel();
peerConnection.onicecandidate = onicecandidate;
if (options.attachStream) {
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();
var channel;
function openDataChannel() {
if (!options.onChannelMessage)return;
if (!peerConnection || typeof peerConnection.createDataChannel == 'undefined') {
if (options.onChannelMessage) {
var error = 'RTCDataChannel is not enabled. Use Chrome Canary and enable this flag via chrome://flags';
console.error(error);
}
return;
}
channel = peerConnection.createDataChannel(options.channel || 'RTCDataChannel', { reliable: false });
channel.onmessage = function (event) {
if (options.onChannelMessage) options.onChannelMessage(event);
};
channel.onopen = function (event) {
console.log('RTCDataChannel opened.');
if (options.onChannelOpened) options.onChannelOpened(event);
};
channel.onclose = function (event) {
console.log('RTCDataChannel closed.');
if (options.onChannelClosed) options.onChannelClosed(event);
};
channel.onerror = function (event) {
console.error(event);
if (options.onChannelError) options.onChannelError(event);
};
peerConnection.ondatachannel = function () {
console.log('peerConnection.ondatachannel event fired.');
};
}
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
}));
},
peer: peerConnection,
channel: channel,
sendData: function (message) {
channel && channel.send(message);
}
};
};
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);
}