<commit_before>/*
 * (C) Copyright 2013 Kurento (http://kurento.org/)
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 */


var EventEmitter = require("events").EventEmitter;

var XMLHttpRequest = require("xmlhttprequest");
var RpcBuilder     = require("rpc-builder");


/**
 * @constructor
 * @abstract
 *
 * @param {String} url: URL of the WebRTC endpoint server.
 * @param {Object} options: optional configuration parameters
 *   {Enum('inactive', 'sendonly', 'recvonly', 'sendrecv')} audio: audio stream mode
 *   {Enum('inactive', 'sendonly', 'recvonly', 'sendrecv')} video: video stream mode
 *   {[Object]} iceServers: array of objects to initialize the ICE servers. It
 *     structure is the same as an Array of WebRTC RTCIceServer objects.
 *
 * @throws RangeError
 */
function Content(url, options)
{
  EventEmitter.call(this);

  var self = this;


  const ERROR_NO_REMOTE_VIDEO_TAG = -1;


  /**
   * Decode the mode of the streams
   *
   * @private
   *
   * @param {Object} options: constraints to update and decode
   * @param {String} type: name of the constraints to decode
   *
   * @returns {Object}
   *
   * @throws RangeError
   */
  function decodeMode(options, type)
  {
    var result = {};

    // If not defined, set send & receive by default
    options[type] = options[type] || 'sendrecv';

    switch(options[type])
    {
      case 'sendrecv':
        result.local  = true;
        result.remote = true;
      break;

      case 'sendonly':
        result.local  = true;
        result.remote = false;
      break;

      case 'recvonly':
        result.local  = false;
        result.remote = true;
      break;

      case 'inactive':
        result.local  = false;
        result.remote = false;
      break;

      default:
        throw new RangeError("Invalid "+type+" media mode");
    }

    return result;
  }

  // We can't disable both audio and video on a stream, raise error
  if(options.audio == 'inactive' && options.video == 'inactive')
    throw new RangeError("At least one audio or video must to be enabled");

  // Audio media
  this._audio = decodeMode(options, "audio");

  // Video media
  this._video = decodeMode(options, "video");

  // Init the KwsWebRtcContent object

  var _sessionId = null;
  var pollingTimeout = null;
  var terminatedByServer = false;


  this.__defineGetter__('sessionId', function()
  {
    return _sessionId;
  });


  var xhr = new XMLHttpRequest();
  var rpc = new RpcBuilder();

  // Init XmlHttpRequest object
  xhr.addEventListener('error', function(error)
  {
    self.emit('error', error);
  });


  function close()
  {
    xhr.abort();

    _sessionId = null;
  };

  self.on('error',     close);
  self.on('terminate', close);


  // Error dispatcher functions

  var MAX_ALLOWED_ERROR_TRIES = 10;

  var error_tries = 0;


  // RPC calls

  // Start

  this._start = function(params, success)
  {
    if(this.sessionId)
      throw new SyntaxError("Connection already open");


    // Register callback for the Application Server
    xhr.addEventListener('load', function()
    {
      rpc.decodeJSON(this.responseText);
    });

    // Connect to Content Server and send request
    var xhr = new XMLHttpRequest();
    xhr.open('POST', url);
    xhr.send(rpc.encodeJSON('start', params, function(error, result)
    {
      if(error)
        self.emit('error', error);

      else
      {
        if(result.rejected)
          self.emit('error', result.rejected)<commit_msg>Default getUserMedia maxFramerate set to 15fps<commit_after>/*
 * (C) Copyright 2013 Kurento (http://kurento.org/)
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 */


var EventEmitter = require("events").EventEmitter;

var XMLHttpRequest = require("xmlhttprequest");
var RpcBuilder     = require("rpc-builder");


const MAX_FRAMERATE = 15;


/**
 * @constructor
 * @abstract
 *
 * @param {String} url: URL of the WebRTC endpoint server.
 * @param {Object} options: optional configuration parameters
 *   {Enum('inactive', 'sendonly', 'recvonly', 'sendrecv')} audio: audio stream mode
 *   {Enum('inactive', 'sendonly', 'recvonly', 'sendrecv')} video: video stream mode
 *   {[Object]} iceServers: array of objects to initialize the ICE servers. It
 *     structure is the same as an Array of WebRTC RTCIceServer objects.
 *
 * @throws RangeError
 */
function Content(url, options)
{
  EventEmitter.call(this);

  var self = this;


  const ERROR_NO_REMOTE_VIDEO_TAG = -1;


  /**
   * Decode the mode of the streams
   *
   * @private
   *
   * @param {Object} options: constraints to update and decode
   * @param {String} type: name of the constraints to decode
   *
   * @returns {Object}
   *
   * @throws RangeError
   */
  function decodeMode(options, type)
  {
    var result = {};

    // If not defined, set send & receive by default
    options[type] = options[type] || 'sendrecv';

    switch(options[type])
    {
      case 'sendrecv':
        result.local  = true;
        result.remote = true;
      break;

      case 'sendonly':
        result.local  = true;
        result.remote = false;
      break;

      case 'recvonly':
        result.local  = false;
        result.remote = true;
      break;

      case 'inactive':
        result.local  = false;
        result.remote = false;
      break;

      default:
        throw new RangeError("Invalid "+type+" media mode");
    }

    return result;
  }

  // We can't disable both audio and video on a stream, raise error
  if(options.audio == 'inactive' && options.video == 'inactive')
    throw new RangeError("At least one audio or video must to be enabled");

  // Audio media
  this._audio = decodeMode(options, "audio");

  // Video media
  this._video = decodeMode(options, "video");

  if(this._video.local)
      this._video.local =
      {
        "mandatory":
        {
          "maxFrameRate": options.maxFrameRate || MAX_FRAMERATE
        }
      };

  // Init the KwsWebRtcContent object

  var _sessionId = null;
  var pollingTimeout = null;
  var terminatedByServer = false;


  this.__defineGetter__('sessionId', function()
  {
    return _sessionId;
  });


  var xhr = new XMLHttpRequest();
  var rpc = new RpcBuilder();

  // Init XmlHttpRequest object
  xhr.addEventListener('error', function(error)
  {
    self.emit('error', error);
  });


  function close()
  {
    xhr.abort();

    _sessionId = null;
  };

  self.on('error',     close);
  self.on('terminate', close);


  // Error dispatcher functions

  var MAX_ALLOWED_ERROR_TRIES = 10;

  var error_tries = 0;


  // RPC calls

  // Start

  this._start = function(params, success)
  {
    if(this.sessionId)
      throw new SyntaxError("Connection already open");


    // Register callback for the Application Server
    xhr.addEventListener('load', function()
    {
      rpc.decodeJSON(this.responseText);
    });

    // Connect to Content Server and send request
    var xhr2 = new XMLHttpRequest();
    xhr2.open('POST', url);
    xhr2.send(rpc.encodeJSON('start', params, function(error, result)
    {
      if(error)
        self.emit('error', error);

      else
      {
        if(result.rejected)
          self.emit('error', result.rejected)
{'fraction_non_alphanumeric': 0.11525714987111821, 'fraction_numerical': 0.0038050816251380877, 'mean_word_length': 2.8433962264150945, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '4351596', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2375, 'n_words': 753}