<commit_before>/* jshint node: true */
'use strict';

var detect = require('./detect');
var defaults = require('cog/defaults');

var mappings = {
  offer: {
    // audio toggle
    // { audio: false } in peer connection config turns off audio
    audio: function(c) {
      c.mandatory = c.mandatory || {};
      c.mandatory.OfferToReceiveAudio = true;
    },

    // video toggle
    // { video: false } in peer connection config turns off video
    video: function(c) {
      c.mandatory = c.mandatory || {};
      c.mandatory.OfferToReceiveVideo = true;
    }
  },

  create: {
    // data enabler
    data: function(c) {
      if (! detect.moz) {
        c.optional = (c.optional || []).concat({ RtpDataChannels: true });
      }
    },

    dtls: function(c) {
      if (! detect.moz) {
        c.optional = (c.optional || []).concat({ DtlsSrtpKeyAgreement: true });
      }
    }
  }
};

// initialise known flags
var knownFlags = ['video', 'audio', 'data'];

/**
  ## 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;

  // iterate through the flags and apply the create mappings
  Object.keys(flags || {}).forEach(function(key) {
    if (m[key]) {
      m[key](generated);
    }
  });

  return defaults({}, constraints, generated);
};

/**
  ### generators.mediaConstraints(flags, context)

  Generate mediaConstraints appropriate for the context in which they are
  being called (i.e. either constructing an RTCPeerConnection object, or
  on the `createOffer` or `createAnswer` calls).
**/
exports.mediaConstraints = function(flags, context) {
  // create an empty constraints object
  var constraints = {
    optional: [{ DtlsSrtpKeyAgreement: true }]
  };
<commit_msg>Added debug output when generating connection constraints<commit_after>/* jshint node: true */
'use strict';

var debug = require('cog/logger')('generators');
var detect = require('./detect');
var defaults = require('cog/defaults');

var mappings = {
  offer: {
    // audio toggle
    // { audio: false } in peer connection config turns off audio
    audio: function(c) {
      c.mandatory = c.mandatory || {};
      c.mandatory.OfferToReceiveAudio = true;
    },

    // video toggle
    // { video: false } in peer connection config turns off video
    video: function(c) {
      c.mandatory = c.mandatory || {};
      c.mandatory.OfferToReceiveVideo = true;
    }
  },

  create: {
    // data enabler
    data: function(c) {
      if (! detect.moz) {
        c.optional = (c.optional || []).concat({ RtpDataChannels: true });
      }
    },

    dtls: function(c) {
      if (! detect.moz) {
        c.optional = (c.optional || []).concat({ DtlsSrtpKeyAgreement: true });
      }
    }
  }
};

// initialise known flags
var knownFlags = ['video', 'audio', 'data'];

/**
  ## 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;
};

/**
  ### generators.mediaConstraints(flags, context)

  Generate mediaConstraints appropriate for the context in which they are
  being called (i.e. either constructing an RTCPeerConnection object, or
  on the `createOffer` or `createAnswer` calls).
**/
exports.mediaConstraints = function(flags, context) {
  // create an empty constraints object
  var constraints = {
    optional: [{ DtlsSrtpKeyAgreement: true }]
  };

{'fraction_non_alphanumeric': 0.11726220432813286, 'fraction_numerical': 0.00033551417547391375, 'mean_word_length': 3.3838235294117647, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4958684', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1638, 'n_words': 555}