<commit_before>var EventEmitter = require('eventemitter3');
var crel = require('crel');
var defaults = require('cog/defaults');
var extend = require('cog/extend');
var quickconnect = require('rtc-quickconnect');
var captureconfig = require('rtc-captureconfig');
var media = require('rtc-media');
var DEFAULT_CONSTRAINTS = { video: true, audio: true };

/**
  # rtc

  This is a package that will provide you a "one-stop shop" for building
  WebRTC applications.  It aggregates together a variety of packages (primarily
  from the [rtc.io](https://github.com/rtc-io) suite) to deliver a single
  package for building a WebRTC application.

  ## Getting Started

  <<< docs/getting-started.md

  ## Example Usage

  <<< docs/examples.md

**/

module.exports = function(opts) {
  var rtc = new EventEmitter();
  var constraints = [].concat((opts || {}).capture || [ DEFAULT_CONSTRAINTS ]);
  var plugins = (opts || {}).plugins || [];
  var signalhost = (opts || {}).signaller || '//switchboard.rtc.io';
  var localStreams = [];
  var localVideo;
  var remoteVideo;

  // capture media
  var captureTargets = constraints.map(parseConstraints).map(function(constraints) {
    return media({ constraints: constraints, plugins: plugins });
  });

  function announce() {
    // create the signaller
    var signaller = rtc.signaller = quickconnect(signalhost, opts);

    signaller
      .on('call:started', handleCallStart)
      .on('call:ended', handleCallEnd);

    // add the local streams
    localStreams.forEach(function(stream) {
      signaller.addStream(stream);
    });

    // emit a ready event for the rtc
    rtc.emit('ready');
  }

  function gotLocalStream(stream) {
    media({ stream: stream, plugins: plugins, muted: true }).render(localVideo);

    localStreams.push(stream);
    if (localStreams.length >= captureTargets.length) {
      announce();
    }
  }

  function handleCallStart(id, pc, data) {
    // create the container for this peers streams
    var container = crel('div', {
      class: 'rtc-peer',
      'data-peerid': id
    });

    console.log('call started with peer: ' + id);
    pc.getRemoteStreams().forEach(function(stream) {
      media({ stream: stream, plugins: plugins }).render(container);
    });

    remoteVideo.appendChild(container);
  }

  function handleCallEnd(id, pc, data) {
    var el = remoteVideo.querySelector('div[data-peerid="' + id + '"]');

    if (el) {
      el.parentNode.removeChild(el);
    }
  }

  function parseConstraints(input) {
    if (typeof input == 'string') {
      return captureconfig(input).toConstraints();
    }

    return input;
  }

  // once we've captured all the streams start the call
  captureTargets.forEach(function(target) {
    target.once('capture', gotLocalStream);
  });

  // create the local container
  localVideo = rtc.local = crel('div', {
    class: 'rtc-media rtc-localvideo'
  });

  // create the remote container
  remoteVideo = rtc.remote = crel('div', {
    class: 'rtc-media rtc-remotevideo'
  });

  return rtc;
}
<commit_msg>Pass through the quickconnect instance for the ready event<commit_after>var EventEmitter = require('eventemitter3');
var crel = require('crel');
var defaults = require('cog/defaults');
var extend = require('cog/extend');
var quickconnect = require('rtc-quickconnect');
var captureconfig = require('rtc-captureconfig');
var media = require('rtc-media');
var DEFAULT_CONSTRAINTS = { video: true, audio: true };

/**
  # rtc

  This is a package that will provide you a "one-stop shop" for building
  WebRTC applications.  It aggregates together a variety of packages (primarily
  from the [rtc.io](https://github.com/rtc-io) suite) to deliver a single
  package for building a WebRTC application.

  ## Getting Started

  <<< docs/getting-started.md

  ## Example Usage

  <<< docs/examples.md

**/

module.exports = function(opts) {
  var rtc = new EventEmitter();
  var constraints = [].concat((opts || {}).capture || [ DEFAULT_CONSTRAINTS ]);
  var plugins = (opts || {}).plugins || [];
  var signalhost = (opts || {}).signaller || '//switchboard.rtc.io';
  var localStreams = [];
  var localVideo;
  var remoteVideo;

  // capture media
  var captureTargets = constraints.map(parseConstraints).map(function(constraints) {
    return media({ constraints: constraints, plugins: plugins });
  });

  function announce() {
    // create the signaller
    var signaller = rtc.signaller = quickconnect(signalhost, opts);

    signaller
      .on('call:started', handleCallStart)
      .on('call:ended', handleCallEnd);

    // add the local streams
    localStreams.forEach(function(stream) {
      signaller.addStream(stream);
    });

    // emit a ready event for the rtc
    rtc.emit('ready', signaller);
  }

  function gotLocalStream(stream) {
    media({ stream: stream, plugins: plugins, muted: true }).render(localVideo);

    localStreams.push(stream);
    if (localStreams.length >= captureTargets.length) {
      announce();
    }
  }

  function handleCallStart(id, pc, data) {
    // create the container for this peers streams
    var container = crel('div', {
      class: 'rtc-peer',
      'data-peerid': id
    });

    console.log('call started with peer: ' + id);
    pc.getRemoteStreams().forEach(function(stream) {
      media({ stream: stream, plugins: plugins }).render(container);
    });

    remoteVideo.appendChild(container);
  }

  function handleCallEnd(id, pc, data) {
    var el = remoteVideo.querySelector('div[data-peerid="' + id + '"]');

    if (el) {
      el.parentNode.removeChild(el);
    }
  }

  function parseConstraints(input) {
    if (typeof input == 'string') {
      return captureconfig(input).toConstraints();
    }

    return input;
  }

  // once we've captured all the streams start the call
  captureTargets.forEach(function(target) {
    target.once('capture', gotLocalStream);
  });

  // create the local container
  localVideo = rtc.local = crel('div', {
    class: 'rtc-media rtc-localvideo'
  });

  // create the remote container
  remoteVideo = rtc.remote = crel('div', {
    class: 'rtc-media rtc-remotevideo'
  });

  return rtc;
}

{'fraction_non_alphanumeric': 0.14073711676451403, 'fraction_numerical': 0.00032615786040443573, 'mean_word_length': 3.4701166180758016, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '5114223', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1835, 'n_words': 485}