<commit_before>/*global require*/
'use strict';
var ConnectionDescription = require('./../../../erizoAPI/build/Release/addon')
                                                          .ConnectionDescription;
var SdpInfo = require('./../../common/semanticSdp/SDPInfo');
var MediaInfo = require('./../../common/semanticSdp/MediaInfo');
var ICEInfo = require('./../../common/semanticSdp/ICEInfo');
var DTLSInfo = require('./../../common/semanticSdp/DTLSInfo');
var CodecInfo = require('./../../common/semanticSdp/CodecInfo');
var SourceInfo = require('./../../common/semanticSdp/SourceInfo');
var StreamInfo = require('./../../common/semanticSdp/StreamInfo');
var TrackInfo = require('./../../common/semanticSdp/TrackInfo');
var RIDInfo = require('./../../common/semanticSdp/RIDInfo');
var CandidateInfo = require('./../../common/semanticSdp/CandidateInfo');
var SimulcastInfo = require('./../../common/semanticSdp/SimulcastInfo');
var Direction = require('./../../common/semanticSdp/Direction');
var DirectionWay = require('./../../common/semanticSdp/DirectionWay');
var Setup = require('./../../common/semanticSdp/Setup');
var Helpers = require('./Helpers');

function getRandomArbitrary(min, max) {
  return Math.floor(Math.random() * (max - min) + min);
}

function generateRandom(length) {
  const alphanum = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  const lastChar = alphanum.length - 1;
  let value = '';

  for (let i = 0; i < length; i += 1) {
    value += '' + alphanum[getRandomArbitrary(0, lastChar) % lastChar];
  }

  return value;
}

function addSsrc(sources, ssrc, sdp, media) {
  let source = sources.get(ssrc);
  const msid = sdp.msidSemantic.token;
  if (!source) {
    source = new SourceInfo(ssrc);
    sources.set(ssrc, source);
  }
  source.setCName('o/i14u9pJrxRKAsu');
  source.setMSLabel(msid);
  source.setLabel(msid);
  source.setStreamId(msid);
  source.setTrackId(media.getId());
  let stream = sdp.getStream(msid);
  if (!stream) {
    stream = new StreamInfo(msid);
    sdp.addStream(stream);
  }
  let track = stream.getTrack(media.getId());
  if (!track) {
    track = new TrackInfo(media.getType(), media.getId());
    stream.addTrack(track);
  }
  track.addSSRC(source);
}

function getMediaInfoFromDescription(info, sdp, mediaType) {
  let media = new MediaInfo(info.getMediaId(mediaType), 1, mediaType);
  media.rtcp = { port: 1, netType: 'IN', ipVer: 4, address: '0.0.0.0' };
  media.setConnection({ version: 4, ip: '0.0.0.0' });
  const direction = info.getDirection(mediaType);
  media.setDirection(Direction.byValue(direction.toUpperCase()));

  let ice = info.getICECredentials(mediaType);
  if (ice) {
    media.setICE(new ICEInfo(ice[0], ice[1]));
  }

  const fingerprint = info.getFingerprint(mediaType);
  if (fingerprint) {
    let setup = Setup.byValue(info.getDtlsRole(mediaType));
    media.setDTLS(new DTLSInfo(setup, 'sha-256', fingerprint));
  }

  const candidates = info.getCandidates();
  if (candidates) {
    candidates.forEach((candidate) => {
      media.addCandidate(new CandidateInfo(candidate.foundation, candidate.componentId,
        candidate.protocol, candidate.priority, candidate.hostIp, candidate.hostPort,
        candidate.hostType, 0, candidate.relayIp, candidate.relayPort));
    });
  }

  const apts = new Map();
  const codecs = info.getCodecs(mediaType);
  if (codecs) {
    codecs.forEach((codec) => {
      const type = codec.type;
      const codecName = codec.name;
      const rate = codec.rate;
      const encoding = codec.channels;

      let params = {};
      const feedback = [];

      if (codec.feedbacks) {
        codec.feedbacks.forEach((rtcpFb) => {
          const tokens = rtcpFb.split(' ');
          const fbType = tokens[0];
          const fbSubType = tokens[1];
          feedback.push({ type: fbType, subtype: fbSubType });
        });
      }

      if (codec.params) {
        params = codec.params;
      }

      if (codecName.toUpperCase() === 'RTX') {
        apts.set(parseInt(params.apt, 10), type);
      } else {
        media.addCodec(new CodecInfo(codecName, type, rate, encoding, params, feedback));
      }
    });
  }

  apts.forEach((apt, id) => {
    const codecInfo = media.getCodecForType(id);
    if (codecInfo) {
      codecInfo.setRTX(apt);
    }
  });

  const extmaps = info.getExtensions(mediaType);
  if (extmaps) {
    Object.keys(extmaps).forEach((value) => {
      media.addExtension(value, extmaps[value]);
    });
  }

  const sources = new Map();
  if (mediaType === 'audio') {
    addSsrc(sources, info.getAudioSsrc(), sdp, media);
  } else if (mediaType === 'video') {
    media.setBitrate(info.getVideoBandwidth());

    info.getVideoSsrcList().forEach((ssrc) => {
      addSsrc(sources, ssrc, sdp, media);
    });

    const rids = info.getRids();
    let isSimulcast = false;
    let simulcast = new SimulcastInfo();
    let ridsData = [];
    let direction;
    Object.keys(rids).forEach((id) => {
      isSimulcast = true;
      direction = rids[id];
      ridsData.push(id);
      const ridInfo = new RIDInfo(id, DirectionWay.byValue(rids[id]));
      media.addRID(ridInfo);
    });

    if (isSimulcast) {
      /*jshint camelcase: false */
      simulcast.setSimulcastPlainString(direction + ' rid=' + ridsData.join(';'));
      media.simulcast_03 = simulcast;
    }

  }
  return media;
}

function candidateToString(cand) {
  var str = `candidate:${cand.foundation} ${cand.componentId} ${cand.transport}` +
            ` ${cand.priority} ${cand.address} ${cand.port} typ ${cand.type}`;<commit_msg>Avoid to announce recv ssrc in a recvonly connection (#1148)<commit_after>/*global require*/
'use strict';
var ConnectionDescription = require('./../../../erizoAPI/build/Release/addon')
                                                          .ConnectionDescription;
var SdpInfo = require('./../../common/semanticSdp/SDPInfo');
var MediaInfo = require('./../../common/semanticSdp/MediaInfo');
var ICEInfo = require('./../../common/semanticSdp/ICEInfo');
var DTLSInfo = require('./../../common/semanticSdp/DTLSInfo');
var CodecInfo = require('./../../common/semanticSdp/CodecInfo');
var SourceInfo = require('./../../common/semanticSdp/SourceInfo');
var StreamInfo = require('./../../common/semanticSdp/StreamInfo');
var TrackInfo = require('./../../common/semanticSdp/TrackInfo');
var RIDInfo = require('./../../common/semanticSdp/RIDInfo');
var CandidateInfo = require('./../../common/semanticSdp/CandidateInfo');
var SimulcastInfo = require('./../../common/semanticSdp/SimulcastInfo');
var Direction = require('./../../common/semanticSdp/Direction');
var DirectionWay = require('./../../common/semanticSdp/DirectionWay');
var Setup = require('./../../common/semanticSdp/Setup');
var Helpers = require('./Helpers');

function getRandomArbitrary(min, max) {
  return Math.floor(Math.random() * (max - min) + min);
}

function generateRandom(length) {
  const alphanum = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  const lastChar = alphanum.length - 1;
  let value = '';

  for (let i = 0; i < length; i += 1) {
    value += '' + alphanum[getRandomArbitrary(0, lastChar) % lastChar];
  }

  return value;
}

function addSsrc(sources, ssrc, sdp, media) {
  let source = sources.get(ssrc);
  const msid = sdp.msidSemantic.token;
  if (!source) {
    source = new SourceInfo(ssrc);
    sources.set(ssrc, source);
  }
  source.setCName('o/i14u9pJrxRKAsu');
  source.setMSLabel(msid);
  source.setLabel(msid);
  source.setStreamId(msid);
  source.setTrackId(media.getId());
  let stream = sdp.getStream(msid);
  if (!stream) {
    stream = new StreamInfo(msid);
    sdp.addStream(stream);
  }
  let track = stream.getTrack(media.getId());
  if (!track) {
    track = new TrackInfo(media.getType(), media.getId());
    stream.addTrack(track);
  }
  track.addSSRC(source);
}

function getMediaInfoFromDescription(info, sdp, mediaType) {
  let media = new MediaInfo(info.getMediaId(mediaType), 1, mediaType);
  media.rtcp = { port: 1, netType: 'IN', ipVer: 4, address: '0.0.0.0' };
  media.setConnection({ version: 4, ip: '0.0.0.0' });
  const direction = info.getDirection(mediaType);
  media.setDirection(Direction.byValue(direction.toUpperCase()));

  let ice = info.getICECredentials(mediaType);
  if (ice) {
    media.setICE(new ICEInfo(ice[0], ice[1]));
  }

  const fingerprint = info.getFingerprint(mediaType);
  if (fingerprint) {
    let setup = Setup.byValue(info.getDtlsRole(mediaType));
    media.setDTLS(new DTLSInfo(setup, 'sha-256', fingerprint));
  }

  const candidates = info.getCandidates();
  if (candidates) {
    candidates.forEach((candidate) => {
      media.addCandidate(new CandidateInfo(candidate.foundation, candidate.componentId,
        candidate.protocol, candidate.priority, candidate.hostIp, candidate.hostPort,
        candidate.hostType, 0, candidate.relayIp, candidate.relayPort));
    });
  }

  const apts = new Map();
  const codecs = info.getCodecs(mediaType);
  if (codecs) {
    codecs.forEach((codec) => {
      const type = codec.type;
      const codecName = codec.name;
      const rate = codec.rate;
      const encoding = codec.channels;

      let params = {};
      const feedback = [];

      if (codec.feedbacks) {
        codec.feedbacks.forEach((rtcpFb) => {
          const tokens = rtcpFb.split(' ');
          const fbType = tokens[0];
          const fbSubType = tokens[1];
          feedback.push({ type: fbType, subtype: fbSubType });
        });
      }

      if (codec.params) {
        params = codec.params;
      }

      if (codecName.toUpperCase() === 'RTX') {
        apts.set(parseInt(params.apt, 10), type);
      } else {
        media.addCodec(new CodecInfo(codecName, type, rate, encoding, params, feedback));
      }
    });
  }

  apts.forEach((apt, id) => {
    const codecInfo = media.getCodecForType(id);
    if (codecInfo) {
      codecInfo.setRTX(apt);
    }
  });

  const extmaps = info.getExtensions(mediaType);
  if (extmaps) {
    Object.keys(extmaps).forEach((value) => {
      media.addExtension(value, extmaps[value]);
    });
  }

  const sources = new Map();
  if (mediaType === 'audio' && info.getDirection() !== 'recvonly') {
    addSsrc(sources, info.getAudioSsrc(), sdp, media);
  } else if (mediaType === 'video') {
    media.setBitrate(info.getVideoBandwidth());

    if (info.getDirection() !== 'recvonly') {
      info.getVideoSsrcList().forEach((ssrc) => {
        addSsrc(sources, ssrc, sdp, media);
      });
    }

    const rids = info.getRids();
    let isSimulcast = false;
    let simulcast = new SimulcastInfo();
    let ridsData = [];
    let direction;
    Object.keys(rids).forEach((id) => {
      isSimulcast = true;
      direction = rids[id];
      ridsData.push(id);
      const ridInfo = new RIDInfo(id, DirectionWay.byValue(rids[id]));
      media.addRID(ridInfo);
    });

    if (isSimulcast) {
      /*jshint camelcase: false */
      simulcast.setSimulcastPlainString(direction + ' rid=' + ridsData.join(';'));
      media.simulcast_03 = simulcast;
    }

  }
  return media;
}

function candidateToString(cand) {
  var str = `candidate:${cand.foundation} ${cand.componentId} ${cand.transport}` +
            ` ${cand.priority} ${cand.address} ${cand.port} typ ${cand.type}`;
{'fraction_non_alphanumeric': 0.16777452805105025, 'fraction_numerical': 0.007622086324559072, 'mean_word_length': 3.20730797912006, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6715540', 'n_tokens_mistral': 3814, 'n_tokens_neox': 3712, 'n_words': 697}