-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
52 lines (43 loc) · 1.65 KB
/
utils.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
var findPlugin = require('rtc-core/plugin');
var detect = require('rtc-core/detect');
var RTCPeerConnection = detect('RTCPeerConnection');
var RTCSessionDescription = detect('RTCSessionDescription');
var defaults = require('cog/defaults');
var pluck = require('whisk/pluck');
var createIceServers = exports.createIceServers = function(session, opts) {
var config = session.ua.configuration || {};
var stun = (opts || {}).stunServers || config.stunServers || [];
var turn = (opts || {}).turnServers || config.turnServers || [];
return [{'url': stun }].concat(turn.map(function(server) {
return {
url: server.urls,
username: server.username,
credential: server.password
}
}));
};
var createPeer = exports.createPeer = function(config, session, opts) {
var pluckConstraints = pluck('mandatory', 'optional');
var constraints = defaults(pluckConstraints((opts || {}).RTCConstraints), {
mandatory: {},
optional: []
});
var plugin = findPlugin((config || {}).plugins);
var pcConfig = {
iceServers: createIceServers(session, opts)
};
if (plugin && typeof plugin.createConnection == 'function') {
return plugin.createConnection(pcConfig, constraints);
}
return new RTCPeerConnection(pcConfig, constraints);
};
var createSessionDesc = exports.createSessionDesc = function(config, raw) {
var plugin = findPlugin((config || {}).plugins);
if (plugin && typeof plugin.createSessionDescription == 'function') {
return plugin.createSessionDescription(raw);
}
return new RTCSessionDescription(raw);
};
exports.hasOffer = function(pc, type) {
return pc.signalingState === 'have-' + (type || 'local') + '-offer';
};