/
index.js
92 lines (71 loc) · 1.91 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* jshint node: true */
'use strict';
var reSeparator = /[\,\s]\s*/;
var CaptureConfig = require('./capture-config');
/**
# rtc-streamclass
This is a simple parser that takes a string of text and determines what
that means in the context of WebRTC.
## Why?
It provides a simple, textual way of describing your requirements for
media capture. Trying to remember the structure of the constraints object
is painful.
## How
A simple text string is converted to an intermediate JS object
representation, which can then be converted to a getUserMedia constraints
data structure using a `toConstraints()` call.
For example, the following text input:
```
camera min:1280x720 max:1280x720 min:15fps max:25fps
```
Is converted into an intermedia representation (via the `CaptureConfig`
utility class) that looks like the following:
```js
{
camera: 0,
microphone: 0,
res: {
min: { w: 1280, h: 720 },
max: { w: 1280, h: 720 }
},
fps: {
min: 15,
max: 25
}
}
```
Which in turn is converted into the following media constraints for
a getUserMedia call:
```js
{
audio: true,
video: {
mandatory: {
minFrameRate: 15,
maxFrameRate: 25,
minWidth: 1280,
minHeight: 720,
maxWidth: 1280,
maxHeight: 720
},
optional: []
}
}
```
## Reference
**/
module.exports = function(input) {
// create a new configuration object using defaults
var config = new CaptureConfig();
// process each of the directives
(input || '').split(reSeparator).forEach(function(directive) {
// now further split the directive on the : character
var parts = directive.split(':');
var method = config[(parts[0] || '').toLowerCase()];
// if we have the method apply
if (typeof method == 'function') {
method.apply(config, parts.slice(1));
}
});
return config;
};