-
Notifications
You must be signed in to change notification settings - Fork 10
/
generators.js
87 lines (68 loc) · 2.46 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
77
78
79
80
81
82
83
84
85
86
87
/* 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-tools/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.
In the event that you use short term authentication for TURN, and you want
to generate new `iceServers` regularly, you can specify an iceServerGenerator
that will be used prior to coupling. This generator should return a fully
compliant W3C (RTCIceServer dictionary)[http://www.w3.org/TR/webrtc/#idl-def-RTCIceServer].
If you pass in both a generator and iceServers, the iceServers _will be
ignored and the generator used instead.
**/
exports.config = function(config) {
var iceServerGenerator = (config || {}).iceServerGenerator;
return defaults({}, config, {
iceServers: typeof iceServerGenerator == 'function' ? iceServerGenerator() : []
});
};
/**
#### 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;
};