UNPKG

rtc-tools

Version:
88 lines (68 loc) 2.52 kB
/* 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; };