-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
72 lines (55 loc) · 1.85 KB
/
index.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
var plugin = require('rtc-core/plugin');
var detect = require('rtc-core/detect');
// patch navigator getUserMedia
navigator.getUserMedia = navigator.getUserMedia ||
detect.call(navigator, 'getUserMedia');
/**
# rtc-capture
Roughly equivalent to the
[`getUserMedia`](https://www.npmjs.org/package/getusermedia) package but with
support for rtc.io plugins.
## Example Usage
<<< examples/simple.js
## Example with using Plugins
<<< examples/plugins.js
## Reference
### `capture(constraints, opts?, callback)`
Capture media with the supplied `constraints`. If an `opts` argument is
supplied look for plugins that may change the behaviour of the capture
operation.
**/
module.exports = function(constraints, opts, callback) {
var pinst;
function handleCapture(stream) {
callback(null, stream);
}
function capture() {
// Check if we have support for the promises-based `navigator.mediaDevices.getUserMedia` option
if (navigator.mediaDevices && typeof navigator.mediaDevices.getUserMedia === 'function') {
return navigator.mediaDevices.getUserMedia(constraints).then(handleCapture).catch(callback);
}
// If we don't, default back to the old detected getUserMedia options
if (typeof navigator.getUserMedia != 'function') {
return callback(new Error('getUserMedia not supported'));
}
return navigator.getUserMedia(constraints, handleCapture, callback);
}
if (typeof opts == 'function') {
callback = opts;
opts = {};
}
// see if we are using a plugin
pinst = plugin((opts || {}).plugins);
if (pinst) {
return pinst.init(opts, function(err) {
if (err) {
return callback(err);
}
if (typeof navigator.getUserMedia != 'function') {
return callback(new Error('plugin does not support media capture'));
}
capture();
});
}
capture();
};