|
1
|
|
|
// Last time updated at Fri Jan 08 2016 14:06 |
|
2
|
|
|
|
|
3
|
|
|
// getUserMedia hacks from git/webrtc/adapter; |
|
4
|
|
|
// removed redundant code |
|
5
|
|
|
// A-to-Zee, all copyrights goes to: |
|
6
|
|
|
// https://github.com/webrtc/adapter/blob/master/LICENSE.md |
|
7
|
|
|
|
|
8
|
|
|
// Used to detect browser and version for our purposes |
|
9
|
|
|
|
|
10
|
|
|
var getUserMedia = null; |
|
11
|
|
|
var webrtcDetectedBrowser = null; |
|
12
|
|
|
var webrtcDetectedVersion = null; |
|
13
|
|
|
var webrtcMinimumVersion = null; |
|
14
|
|
|
|
|
15
|
|
|
var webrtcUtils = window.webrtcUtils || {}; |
|
16
|
|
|
if(!webrtcUtils.enableLogs) { |
|
17
|
|
|
webrtcUtils.enableLogs = true; |
|
18
|
|
|
} |
|
19
|
|
|
if(!webrtcUtils.log) { |
|
20
|
|
|
webrtcUtils.log = function() { |
|
21
|
|
|
if(!webrtcUtils.enableLogs) { |
|
22
|
|
|
return; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
// suppress console.log output when being included as a module. |
|
26
|
|
|
if (typeof module !== 'undefined' || |
|
27
|
|
|
typeof require === 'function' && typeof define === 'function') { |
|
|
|
|
|
|
28
|
|
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
console.log.apply(console, arguments); |
|
31
|
|
|
}; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if(!webrtcUtils.extractVersion) { |
|
35
|
|
|
webrtcUtils.extractVersion = function(uastring, expr, pos) { |
|
36
|
|
|
var match = uastring.match(expr); |
|
37
|
|
|
return match && match.length >= pos && parseInt(match[pos], 10); |
|
38
|
|
|
}; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if (typeof window === 'object') { |
|
42
|
|
|
if (window.HTMLMediaElement && |
|
43
|
|
|
!('srcObject' in window.HTMLMediaElement.prototype)) { |
|
44
|
|
|
// Shim the srcObject property, once, when HTMLMediaElement is found. |
|
45
|
|
|
Object.defineProperty(window.HTMLMediaElement.prototype, 'srcObject', { |
|
46
|
|
|
get: function() { |
|
47
|
|
|
// If prefixed srcObject property exists, return it. |
|
48
|
|
|
// Otherwise use the shimmed property, _srcObject |
|
49
|
|
|
return 'mozSrcObject' in this ? this.mozSrcObject : this._srcObject; |
|
50
|
|
|
}, |
|
51
|
|
|
set: function(stream) { |
|
52
|
|
|
if ('mozSrcObject' in this) { |
|
53
|
|
|
this.mozSrcObject = stream; |
|
54
|
|
|
} else { |
|
55
|
|
|
// Use _srcObject as a private property for this shim |
|
56
|
|
|
this._srcObject = stream; |
|
57
|
|
|
// TODO: revokeObjectUrl(this.src) when !stream to release resources? |
|
58
|
|
|
this.src = stream ? URL.createObjectURL(stream) : null; |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
}); |
|
62
|
|
|
} |
|
63
|
|
|
// Proxy existing globals |
|
64
|
|
|
getUserMedia = window.navigator && window.navigator.getUserMedia; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (typeof window === 'undefined' || !window.navigator) { |
|
68
|
|
|
webrtcDetectedBrowser = 'not a browser'; |
|
69
|
|
|
} else if (navigator.mozGetUserMedia && window.mozRTCPeerConnection) { |
|
|
|
|
|
|
70
|
|
|
webrtcDetectedBrowser = 'firefox'; |
|
71
|
|
|
|
|
72
|
|
|
// the detected firefox version. |
|
73
|
|
|
webrtcDetectedVersion = webrtcUtils.extractVersion(navigator.userAgent, |
|
74
|
|
|
/Firefox\/([0-9]+)\./, 1); |
|
75
|
|
|
|
|
76
|
|
|
// the minimum firefox version still supported by adapter. |
|
77
|
|
|
webrtcMinimumVersion = 31; |
|
78
|
|
|
|
|
79
|
|
|
// getUserMedia constraints shim. |
|
80
|
|
|
getUserMedia = function(constraints, onSuccess, onError) { |
|
81
|
|
|
var constraintsToFF37 = function(c) { |
|
82
|
|
|
if (typeof c !== 'object' || c.require) { |
|
83
|
|
|
return c; |
|
84
|
|
|
} |
|
85
|
|
|
var require = []; |
|
86
|
|
|
Object.keys(c).forEach(function(key) { |
|
87
|
|
|
if (key === 'require' || key === 'advanced' || key === 'mediaSource') { |
|
88
|
|
|
return; |
|
89
|
|
|
} |
|
90
|
|
|
var r = c[key] = (typeof c[key] === 'object') ? |
|
91
|
|
|
c[key] : { |
|
92
|
|
|
ideal: c[key] |
|
93
|
|
|
}; |
|
94
|
|
|
if (r.min !== undefined || |
|
95
|
|
|
r.max !== undefined || r.exact !== undefined) { |
|
96
|
|
|
require.push(key); |
|
97
|
|
|
} |
|
98
|
|
|
if (r.exact !== undefined) { |
|
99
|
|
|
if (typeof r.exact === 'number') { |
|
100
|
|
|
r.min = r.max = r.exact; |
|
101
|
|
|
} else { |
|
102
|
|
|
c[key] = r.exact; |
|
103
|
|
|
} |
|
104
|
|
|
delete r.exact; |
|
105
|
|
|
} |
|
106
|
|
|
if (r.ideal !== undefined) { |
|
107
|
|
|
c.advanced = c.advanced || []; |
|
108
|
|
|
var oc = {}; |
|
109
|
|
|
if (typeof r.ideal === 'number') { |
|
110
|
|
|
oc[key] = { |
|
111
|
|
|
min: r.ideal, |
|
112
|
|
|
max: r.ideal |
|
113
|
|
|
}; |
|
114
|
|
|
} else { |
|
115
|
|
|
oc[key] = r.ideal; |
|
116
|
|
|
} |
|
117
|
|
|
c.advanced.push(oc); |
|
118
|
|
|
delete r.ideal; |
|
119
|
|
|
if (!Object.keys(r).length) { |
|
120
|
|
|
delete c[key]; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
}); |
|
124
|
|
|
if (require.length) { |
|
125
|
|
|
c.require = require; |
|
126
|
|
|
} |
|
127
|
|
|
return c; |
|
128
|
|
|
}; |
|
129
|
|
|
if (webrtcDetectedVersion < 38) { |
|
130
|
|
|
webrtcUtils.log('spec: ' + JSON.stringify(constraints)); |
|
131
|
|
|
if (constraints.audio) { |
|
132
|
|
|
constraints.audio = constraintsToFF37(constraints.audio); |
|
133
|
|
|
} |
|
134
|
|
|
if (constraints.video) { |
|
135
|
|
|
constraints.video = constraintsToFF37(constraints.video); |
|
136
|
|
|
} |
|
137
|
|
|
webrtcUtils.log('ff37: ' + JSON.stringify(constraints)); |
|
138
|
|
|
} |
|
139
|
|
|
return navigator.mozGetUserMedia(constraints, onSuccess, onError); |
|
|
|
|
|
|
140
|
|
|
}; |
|
141
|
|
|
|
|
142
|
|
|
navigator.getUserMedia = getUserMedia; |
|
143
|
|
|
|
|
144
|
|
|
// Shim for mediaDevices on older versions. |
|
145
|
|
|
if (!navigator.mediaDevices) { |
|
146
|
|
|
navigator.mediaDevices = { |
|
147
|
|
|
getUserMedia: requestUserMedia, |
|
148
|
|
|
addEventListener: function() {}, |
|
149
|
|
|
removeEventListener: function() {} |
|
150
|
|
|
}; |
|
151
|
|
|
} |
|
152
|
|
|
navigator.mediaDevices.enumerateDevices = |
|
|
|
|
|
|
153
|
|
|
navigator.mediaDevices.enumerateDevices || function() { |
|
154
|
|
|
return new Promise(function(resolve) { |
|
155
|
|
|
var infos = [{ |
|
156
|
|
|
kind: 'audioinput', |
|
157
|
|
|
deviceId: 'default', |
|
158
|
|
|
label: '', |
|
159
|
|
|
groupId: '' |
|
160
|
|
|
}, { |
|
161
|
|
|
kind: 'videoinput', |
|
162
|
|
|
deviceId: 'default', |
|
163
|
|
|
label: '', |
|
164
|
|
|
groupId: '' |
|
165
|
|
|
}]; |
|
166
|
|
|
resolve(infos); |
|
167
|
|
|
}); |
|
168
|
|
|
}; |
|
169
|
|
|
|
|
170
|
|
|
if (webrtcDetectedVersion < 41) { |
|
171
|
|
|
// Work around http://bugzil.la/1169665 |
|
172
|
|
|
var orgEnumerateDevices = |
|
173
|
|
|
navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices); |
|
|
|
|
|
|
174
|
|
|
navigator.mediaDevices.enumerateDevices = function() { |
|
175
|
|
|
return orgEnumerateDevices().then(undefined, function(e) { |
|
176
|
|
|
if (e.name === 'NotFoundError') { |
|
177
|
|
|
return []; |
|
178
|
|
|
} |
|
179
|
|
|
throw e; |
|
180
|
|
|
}); |
|
181
|
|
|
}; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
} else if (navigator.webkitGetUserMedia && window.webkitRTCPeerConnection) { |
|
|
|
|
|
|
185
|
|
|
webrtcDetectedBrowser = 'chrome'; |
|
186
|
|
|
|
|
187
|
|
|
webrtcDetectedVersion = webrtcUtils.extractVersion(navigator.userAgent, |
|
188
|
|
|
/Chrom(e|ium)\/([0-9]+)\./, 2); |
|
189
|
|
|
|
|
190
|
|
|
// the minimum chrome version still supported by adapter. |
|
191
|
|
|
webrtcMinimumVersion = 38; |
|
192
|
|
|
|
|
193
|
|
|
// getUserMedia constraints shim. |
|
194
|
|
|
var constraintsToChrome = function(c) { |
|
195
|
|
|
if (typeof c !== 'object' || c.mandatory || c.optional) { |
|
196
|
|
|
return c; |
|
197
|
|
|
} |
|
198
|
|
|
var cc = {}; |
|
199
|
|
|
Object.keys(c).forEach(function(key) { |
|
200
|
|
|
if (key === 'require' || key === 'advanced' || key === 'mediaSource') { |
|
201
|
|
|
return; |
|
202
|
|
|
} |
|
203
|
|
|
var r = (typeof c[key] === 'object') ? c[key] : { |
|
204
|
|
|
ideal: c[key] |
|
205
|
|
|
}; |
|
206
|
|
|
if (r.exact !== undefined && typeof r.exact === 'number') { |
|
207
|
|
|
r.min = r.max = r.exact; |
|
208
|
|
|
} |
|
209
|
|
|
var oldname = function(prefix, name) { |
|
210
|
|
|
if (prefix) { |
|
211
|
|
|
return prefix + name.charAt(0).toUpperCase() + name.slice(1); |
|
212
|
|
|
} |
|
213
|
|
|
return (name === 'deviceId') ? 'sourceId' : name; |
|
214
|
|
|
}; |
|
215
|
|
|
if (r.ideal !== undefined) { |
|
216
|
|
|
cc.optional = cc.optional || []; |
|
217
|
|
|
var oc = {}; |
|
218
|
|
|
if (typeof r.ideal === 'number') { |
|
219
|
|
|
oc[oldname('min', key)] = r.ideal; |
|
220
|
|
|
cc.optional.push(oc); |
|
221
|
|
|
oc = {}; |
|
222
|
|
|
oc[oldname('max', key)] = r.ideal; |
|
223
|
|
|
cc.optional.push(oc); |
|
224
|
|
|
} else { |
|
225
|
|
|
oc[oldname('', key)] = r.ideal; |
|
226
|
|
|
cc.optional.push(oc); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
if (r.exact !== undefined && typeof r.exact !== 'number') { |
|
230
|
|
|
cc.mandatory = cc.mandatory || {}; |
|
231
|
|
|
cc.mandatory[oldname('', key)] = r.exact; |
|
232
|
|
|
} else { |
|
233
|
|
|
['min', 'max'].forEach(function(mix) { |
|
234
|
|
|
if (r[mix] !== undefined) { |
|
235
|
|
|
cc.mandatory = cc.mandatory || {}; |
|
236
|
|
|
cc.mandatory[oldname(mix, key)] = r[mix]; |
|
237
|
|
|
} |
|
238
|
|
|
}); |
|
239
|
|
|
} |
|
240
|
|
|
}); |
|
241
|
|
|
if (c.advanced) { |
|
242
|
|
|
cc.optional = (cc.optional || []).concat(c.advanced); |
|
243
|
|
|
} |
|
244
|
|
|
return cc; |
|
245
|
|
|
}; |
|
246
|
|
|
|
|
247
|
|
|
getUserMedia = function(constraints, onSuccess, onError) { |
|
248
|
|
|
if (constraints.audio) { |
|
249
|
|
|
constraints.audio = constraintsToChrome(constraints.audio); |
|
250
|
|
|
} |
|
251
|
|
|
if (constraints.video) { |
|
252
|
|
|
constraints.video = constraintsToChrome(constraints.video); |
|
253
|
|
|
} |
|
254
|
|
|
webrtcUtils.log('chrome: ' + JSON.stringify(constraints)); |
|
255
|
|
|
return navigator.webkitGetUserMedia(constraints, onSuccess, onError); |
|
|
|
|
|
|
256
|
|
|
}; |
|
257
|
|
|
navigator.getUserMedia = getUserMedia; |
|
258
|
|
|
|
|
259
|
|
|
if (!navigator.mediaDevices) { |
|
260
|
|
|
navigator.mediaDevices = { |
|
261
|
|
|
getUserMedia: requestUserMedia |
|
262
|
|
|
}; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
// A shim for getUserMedia method on the mediaDevices object. |
|
266
|
|
|
// TODO(KaptenJansson) remove once implemented in Chrome stable. |
|
267
|
|
|
if (!navigator.mediaDevices.getUserMedia) { |
|
268
|
|
|
navigator.mediaDevices.getUserMedia = function(constraints) { |
|
269
|
|
|
return requestUserMedia(constraints); |
|
270
|
|
|
}; |
|
271
|
|
|
} else { |
|
272
|
|
|
// Even though Chrome 45 has navigator.mediaDevices and a getUserMedia |
|
273
|
|
|
// function which returns a Promise, it does not accept spec-style |
|
274
|
|
|
// constraints. |
|
275
|
|
|
var origGetUserMedia = navigator.mediaDevices.getUserMedia. |
|
|
|
|
|
|
276
|
|
|
bind(navigator.mediaDevices); |
|
277
|
|
|
navigator.mediaDevices.getUserMedia = function(c) { |
|
278
|
|
|
webrtcUtils.log('spec: ' + JSON.stringify(c)); // whitespace for alignment |
|
279
|
|
|
c.audio = constraintsToChrome(c.audio); |
|
280
|
|
|
c.video = constraintsToChrome(c.video); |
|
281
|
|
|
webrtcUtils.log('chrome: ' + JSON.stringify(c)); |
|
282
|
|
|
return origGetUserMedia(c); |
|
283
|
|
|
}; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
// Dummy devicechange event methods. |
|
287
|
|
|
// TODO(KaptenJansson) remove once implemented in Chrome stable. |
|
288
|
|
|
if (typeof navigator.mediaDevices.addEventListener === 'undefined') { |
|
|
|
|
|
|
289
|
|
|
navigator.mediaDevices.addEventListener = function() { |
|
290
|
|
|
webrtcUtils.log('Dummy mediaDevices.addEventListener called.'); |
|
291
|
|
|
}; |
|
292
|
|
|
} |
|
293
|
|
|
if (typeof navigator.mediaDevices.removeEventListener === 'undefined') { |
|
|
|
|
|
|
294
|
|
|
navigator.mediaDevices.removeEventListener = function() { |
|
295
|
|
|
webrtcUtils.log('Dummy mediaDevices.removeEventListener called.'); |
|
296
|
|
|
}; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
} else if (navigator.mediaDevices && navigator.userAgent.match(/Edge\/(\d+).(\d+)$/)) { |
|
|
|
|
|
|
300
|
|
|
webrtcUtils.log('This appears to be Edge'); |
|
301
|
|
|
webrtcDetectedBrowser = 'edge'; |
|
302
|
|
|
|
|
303
|
|
|
webrtcDetectedVersion = webrtcUtils.extractVersion(navigator.userAgent, /Edge\/(\d+).(\d+)$/, 2); |
|
304
|
|
|
|
|
305
|
|
|
// the minimum version still supported by adapter. |
|
306
|
|
|
webrtcMinimumVersion = 12; |
|
307
|
|
|
} else { |
|
308
|
|
|
webrtcUtils.log('Browser does not appear to be WebRTC-capable'); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
// Returns the result of getUserMedia as a Promise. |
|
312
|
|
|
function requestUserMedia(constraints) { |
|
313
|
|
|
return new Promise(function(resolve, reject) { |
|
314
|
|
|
getUserMedia(constraints, resolve, reject); |
|
315
|
|
|
}); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
if (typeof module !== 'undefined') { |
|
319
|
|
|
module.exports = { |
|
320
|
|
|
getUserMedia: getUserMedia, |
|
321
|
|
|
webrtcDetectedBrowser: webrtcDetectedBrowser, |
|
322
|
|
|
webrtcDetectedVersion: webrtcDetectedVersion, |
|
323
|
|
|
webrtcMinimumVersion: webrtcMinimumVersion, |
|
324
|
|
|
webrtcUtils: webrtcUtils |
|
325
|
|
|
}; |
|
326
|
|
|
} else if ((typeof require === 'function') && (typeof define === 'function')) { |
|
|
|
|
|
|
327
|
|
|
// Expose objects and functions when RequireJS is doing the loading. |
|
328
|
|
|
define([], function() { |
|
329
|
|
|
return { |
|
330
|
|
|
getUserMedia: getUserMedia, |
|
331
|
|
|
webrtcDetectedBrowser: webrtcDetectedBrowser, |
|
332
|
|
|
webrtcDetectedVersion: webrtcDetectedVersion, |
|
333
|
|
|
webrtcMinimumVersion: webrtcMinimumVersion, |
|
334
|
|
|
webrtcUtils: webrtcUtils |
|
335
|
|
|
}; |
|
336
|
|
|
}); |
|
337
|
|
|
} |
|
338
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.