This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathgetusermedia.js
75 lines (66 loc) · 2.49 KB
/
getusermedia.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
// getUserMedia helper by @HenrikJoreteg used for navigator.getUserMedia shim
var adapter = require('webrtc-adapter');
module.exports = function (constraints, cb) {
var error;
var haveOpts = arguments.length === 2;
var defaultOpts = {video: true, audio: true};
var denied = 'PermissionDeniedError';
var altDenied = 'PERMISSION_DENIED';
var notSatisfied = 'ConstraintNotSatisfiedError';
// make constraints optional
if (!haveOpts) {
cb = constraints;
constraints = defaultOpts;
}
// treat lack of browser support like an error
if (typeof navigator === 'undefined' || !navigator.getUserMedia) {
// throw proper error per spec
error = new Error('MediaStreamError');
error.name = 'NotSupportedError';
// keep all callbacks async
return setTimeout(function () {
cb(error);
}, 0);
}
// normalize error handling when no media types are requested
if (!constraints.audio && !constraints.video) {
error = new Error('MediaStreamError');
error.name = 'NoMediaRequestedError';
// keep all callbacks async
return setTimeout(function () {
cb(error);
}, 0);
}
navigator.mediaDevices.getUserMedia(constraints)
.then(function (stream) {
cb(null, stream);
}).catch(function (err) {
var error;
// coerce into an error object since FF gives us a string
// there are only two valid names according to the spec
// we coerce all non-denied to "constraint not satisfied".
if (typeof err === 'string') {
error = new Error('MediaStreamError');
if (err === denied || err === altDenied) {
error.name = denied;
} else {
error.name = notSatisfied;
}
} else {
// if we get an error object make sure '.name' property is set
// according to spec: http://dev.w3.org/2011/webrtc/editor/getusermedia.html#navigatorusermediaerror-and-navigatorusermediaerrorcallback
error = err;
if (!error.name) {
// this is likely chrome which
// sets a property called "ERROR_DENIED" on the error object
// if so we make sure to set a name
if (error[denied]) {
err.name = denied;
} else {
err.name = notSatisfied;
}
}
}
cb(error);
});
};