forked from rtc-io/rtc-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generators.js
76 lines (61 loc) · 1.89 KB
/
generators.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
/* jshint node: true */
'use strict';
var debug = require('cog/logger')('generators');
var detect = require('./detect');
var defaults = require('cog/defaults');
var mappings = {
create: {
dtls: function(c) {
if (! detect.moz) {
c.optional = (c.optional || []).concat({ DtlsSrtpKeyAgreement: true });
}
}
}
};
/**
### rtc/generators
The generators package provides some utility methods for generating
constraint objects and similar constructs.
```js
var generators = require('rtc/generators');
```
**/
/**
#### generators.config(config)
Generate a configuration object suitable for passing into an W3C
RTCPeerConnection constructor first argument, based on our custom config.
**/
exports.config = function(config) {
return defaults(config, {
iceServers: []
});
};
/**
#### generators.connectionConstraints(flags, constraints)
This is a helper function that will generate appropriate connection
constraints for a new `RTCPeerConnection` object which is constructed
in the following way:
```js
var conn = new RTCPeerConnection(flags, constraints);
```
In most cases the constraints object can be left empty, but when creating
data channels some additional options are required. This function
can generate those additional options and intelligently combine any
user defined constraints (in `constraints`) with shorthand flags that
might be passed while using the `rtc.createConnection` helper.
**/
exports.connectionConstraints = function(flags, constraints) {
var generated = {};
var m = mappings.create;
var out;
// iterate through the flags and apply the create mappings
Object.keys(flags || {}).forEach(function(key) {
if (m[key]) {
m[key](generated);
}
});
// generate the connection constraints
out = defaults({}, constraints, generated);
debug('generated connection constraints: ', out);
return out;
};