-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebrtc-shim.js
261 lines (228 loc) · 7.48 KB
/
webrtc-shim.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
(function (globals, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(factory);
} else {
// Browser global
globals.WEBRTC_SHIM = factory();
}
}(this, function () {
// begin compatibility insanity
var IceCandidate, MediaStream, PeerConnection, SessionDescription, URL,
attachStream, browser, extract, getUserMedia, processSDPIn,
processSDPOut, removeCN, replaceCodec, shim, supported, useOPUS;
// First we deal with vendor prefixes
PeerConnection = window.mozRTCPeerConnection || window.PeerConnection ||
window.webkitPeerConnection00 || window.webkitRTCPeerConnection;
IceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;
SessionDescription = window.mozRTCSessionDescription || window.RTCSessionDescription;
MediaStream = window.MediaStream || window.webkitMediaStream;
getUserMedia = navigator.mozGetUserMedia || navigator.getUserMedia ||
navigator.webkitGetUserMedia || navigator.msGetUserMedia;
URL = window.URL || window.webkitURL || window.msURL || window.oURL;
// getUserMedia errors unless it is bound to the scope of navigator
if (getUserMedia) {
getUserMedia = getUserMedia.bind(navigator);
}
// Very simple browser detection for chrome and FF
browser = (navigator.mozGetUserMedia ? 'firefox' : 'chrome');
supported = (PeerConnection && getUserMedia);
// Simple util for dealing with regex matches
extract = function (str, reg) {
var match = str.match(reg);
return (match ? match[1] : null);
};
// replaceCodec takes an SDP line with a codec in it and replaces it with a new codec
replaceCodec = function (line, codec) {
var el, els, out, i, len;
els = line.split(' ');
out = [];
for (i = 0, len = els.length; i < len; ++i) {
el = els[i];
if (i === 3) {
out[i++] = codec;
}
if (el !== codec) {
out[i++] = el;
}
}
return out.join(' ');
};
// Removes troublesome CN lines from SDP messages that causes certain browsers to crash
removeCN = function (lines, mLineIdx) {
var cnPos, idx, line, mLineEls, payload, i, len;
mLineEls = lines[mLineIdx].split(' ');
for (i = 0, len = lines.length; i < len; ++i) {
line = lines[i];
if (!line) continue;
payload = extract(line, /a=rtpmap:(\d+) CN\/\d+/i);
if (payload) {
cnPos = mLineEls.indexOf(payload);
if (cnPos !== -1) {
mLineEls.splice(cnPos, 1);
}
lines.splice(i, 1);
}
}
lines[mLineIdx] = mLineEls.join(' ');
return lines;
};
// Replace audio codecs in SDP with OPUS
useOPUS = function (sdp) {
var line, lines, mLine, mLineIdx, payload, i, len;
lines = sdp.split('\r\n');
mLine = lines.filter(function(el){
return line.indexOf('m=audio') !== -1;
})[0];
mLineIdx = lines.indexOf(mLine);
if (!mLineIdx) return sdp;
for (i = 0, len = lines.length; i < len; ++i) {
line = lines[i];
if (line.indexOf('opus/48000') === -1) continue;
payload = extract(line, /:(\d+) opus\/48000/i);
if (payload) {
lines[mLineIdx] = replaceCodec(lines[mLineIdx], payload);
}
break;
}
lines = removeCN(lines, mLineIdx);
return lines.join('\r\n');
};
// Use this to format all outbound SDP Messages
processSDPOut = function (sdp, env) {
var addCrypto, lines, line, out;
if (!env) env = browser; // for testing
out = [];
lines = sdp.split('\r\n');
if (env === 'firefox') {
// FF does not support crypto yet - chrome does not support unencrypted though.
// If FF makes an offer to chrome you need to put a fake crypto key in or chrome will ignore it
addCrypto = "a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:BAADBAADBAADBAADBAADBAADBAADBAADBAADBAAD";
lines.forEach(function(line){
out.push(line);
if (line.indexOf('m=') === 0) {
out.push(addCrypto);
}
});
} else {
out = lines.filter(function(line){
return line.indexOf("a=ice-options:google-ice") !== -1;
});
}
return useOPUS(out.join('\r\n'));
};
// Use this to format all inbound SDP messages - currently does nothing
processSDPIn = function (sdp, env) {
if (!env) env = browser; // for testing
return sdp;
};
// Util for attaching a video stream to a DOM element
attachStream = function (uri, el) {
var i, len;
if (typeof el === "string") {
return attachStream(uri, document.getElementById(el));
} else if (el.jquery) {
el.attr('src', uri);
for (i = 0, len = el.length; i < len; i++) {
el[i].play();
}
} else {
el.src = uri;
el.play();
}
return el;
};
// Patches over RTC prototypes with missing functions
// Also exposes a config based on browser. FF and chrome require certain configs for interop
shim = function () {
var PeerConnConfig, mediaConstraints, out;
if (!supported) {
// no need to shim
return;
}
if (browser === 'firefox') {
PeerConnConfig = {
iceServers: [{
url: "stun:23.21.150.121" // FF doesn't support resolving DNS in iceServers yet
}
]
};
mediaConstraints = {
mandatory: {
OfferToReceiveAudio: true,
OfferToReceiveVideo: true,
MozDontOfferDataChannel: true // Tell FF not to put datachannel info in SDP or chrome will crash
}
};
// FF doesn't expose this yet
MediaStream.prototype.getVideoTracks = function () {
return [];
};
MediaStream.prototype.getAudioTracks = function () {
return [];
};
} else {
PeerConnConfig = {
iceServers: [{
url: "stun:stun.l.google.com:19302"
}
]
};
mediaConstraints = {
mandatory: {
OfferToReceiveAudio: true,
OfferToReceiveVideo: true
},
optional: [{
DtlsSrtpKeyAgreement: true
}
]
};
// API compat for older versions of chrome
if (!MediaStream.prototype.getVideoTracks) {
MediaStream.prototype.getVideoTracks = function () {
return this.videoTracks;
};
MediaStream.prototype.getAudioTracks = function () {
return this.audioTracks;
};
}
if (!PeerConnection.prototype.getLocalStreams) {
PeerConnection.prototype.getLocalStreams = function () {
return this.localStreams;
};
PeerConnection.prototype.getRemoteStreams = function () {
return this.remoteStreams;
};
}
}
// Not a shim - custom to holla. Allows you to do stream.pipe(element) which is more elegant than attachStream(streamUri, el)
MediaStream.prototype.pipe = function (el) {
var uri;
uri = URL.createObjectURL(this);
attachStream(uri, el);
return this;
};
out = {
PeerConnection: PeerConnection,
IceCandidate: IceCandidate,
SessionDescription: SessionDescription,
MediaStream: MediaStream,
getUserMedia: getUserMedia,
URL: URL,
attachStream: attachStream,
processSDPIn: processSDPIn,
processSDPOut: processSDPOut,
PeerConnConfig: PeerConnConfig,
browser: browser,
supported: supported,
constraints: mediaConstraints,
// This stuff is exposed for testing
useOPUS: useOPUS,
removeCN: removeCN,
replaceCodec: replaceCodec
};
return out;
};
return shim();
}));