Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TraceablePeerConnection.js 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /* global mozRTCPeerConnection, webkitRTCPeerConnection */
  2. import { getLogger } from "jitsi-meet-logger";
  3. const logger = getLogger(__filename);
  4. import RTC from '../RTC/RTC';
  5. var RTCBrowserType = require("../RTC/RTCBrowserType.js");
  6. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  7. var transform = require('sdp-transform');
  8. var RandomUtil = require('../util/RandomUtil');
  9. var GlobalOnErrorHandler = require("../util/GlobalOnErrorHandler");
  10. var SIMULCAST_LAYERS = 3;
  11. function TraceablePeerConnection(ice_config, constraints, session) {
  12. var self = this;
  13. this.session = session;
  14. this.replaceSSRCs = {
  15. "audio": [],
  16. "video": []
  17. };
  18. this.recvOnlySSRCs = {};
  19. var RTCPeerConnectionType = null;
  20. if (RTCBrowserType.isFirefox()) {
  21. RTCPeerConnectionType = mozRTCPeerConnection;
  22. } else if (RTCBrowserType.isTemasysPluginUsed()) {
  23. RTCPeerConnectionType = RTCPeerConnection;
  24. } else {
  25. RTCPeerConnectionType = webkitRTCPeerConnection;
  26. }
  27. this.peerconnection = new RTCPeerConnectionType(ice_config, constraints);
  28. this.updateLog = [];
  29. this.stats = {};
  30. this.statsinterval = null;
  31. this.maxstats = 0; // limit to 300 values, i.e. 5 minutes; set to 0 to disable
  32. var Interop = require('sdp-interop').Interop;
  33. this.interop = new Interop();
  34. var Simulcast = require('sdp-simulcast');
  35. this.simulcast = new Simulcast({numOfLayers: SIMULCAST_LAYERS,
  36. explodeRemoteSimulcast: false});
  37. this.eventEmitter = this.session.room.eventEmitter;
  38. // override as desired
  39. this.trace = function (what, info) {
  40. /*logger.warn('WTRACE', what, info);
  41. if (info && RTCBrowserType.isIExplorer()) {
  42. if (info.length > 1024) {
  43. logger.warn('WTRACE', what, info.substr(1024));
  44. }
  45. if (info.length > 2048) {
  46. logger.warn('WTRACE', what, info.substr(2048));
  47. }
  48. }*/
  49. self.updateLog.push({
  50. time: new Date(),
  51. type: what,
  52. value: info || ""
  53. });
  54. };
  55. this.onicecandidate = null;
  56. this.peerconnection.onicecandidate = function (event) {
  57. // FIXME: this causes stack overflow with Temasys Plugin
  58. if (!RTCBrowserType.isTemasysPluginUsed())
  59. self.trace('onicecandidate', JSON.stringify(event.candidate, null, ' '));
  60. if (self.onicecandidate !== null) {
  61. self.onicecandidate(event);
  62. }
  63. };
  64. this.onaddstream = null;
  65. this.peerconnection.onaddstream = function (event) {
  66. self.trace('onaddstream', event.stream.id);
  67. if (self.onaddstream !== null) {
  68. self.onaddstream(event);
  69. }
  70. };
  71. this.onremovestream = null;
  72. this.peerconnection.onremovestream = function (event) {
  73. self.trace('onremovestream', event.stream.id);
  74. if (self.onremovestream !== null) {
  75. self.onremovestream(event);
  76. }
  77. };
  78. this.onsignalingstatechange = null;
  79. this.peerconnection.onsignalingstatechange = function (event) {
  80. self.trace('onsignalingstatechange', self.signalingState);
  81. if (self.onsignalingstatechange !== null) {
  82. self.onsignalingstatechange(event);
  83. }
  84. };
  85. this.oniceconnectionstatechange = null;
  86. this.peerconnection.oniceconnectionstatechange = function (event) {
  87. self.trace('oniceconnectionstatechange', self.iceConnectionState);
  88. if (self.oniceconnectionstatechange !== null) {
  89. self.oniceconnectionstatechange(event);
  90. }
  91. };
  92. this.onnegotiationneeded = null;
  93. this.peerconnection.onnegotiationneeded = function (event) {
  94. self.trace('onnegotiationneeded');
  95. if (self.onnegotiationneeded !== null) {
  96. self.onnegotiationneeded(event);
  97. }
  98. };
  99. self.ondatachannel = null;
  100. this.peerconnection.ondatachannel = function (event) {
  101. self.trace('ondatachannel', event);
  102. if (self.ondatachannel !== null) {
  103. self.ondatachannel(event);
  104. }
  105. };
  106. // XXX: do all non-firefox browsers which we support also support this?
  107. if (!RTCBrowserType.isFirefox() && this.maxstats) {
  108. this.statsinterval = window.setInterval(function() {
  109. self.peerconnection.getStats(function(stats) {
  110. var results = stats.result();
  111. var now = new Date();
  112. for (var i = 0; i < results.length; ++i) {
  113. results[i].names().forEach(function (name) {
  114. var id = results[i].id + '-' + name;
  115. if (!self.stats[id]) {
  116. self.stats[id] = {
  117. startTime: now,
  118. endTime: now,
  119. values: [],
  120. times: []
  121. };
  122. }
  123. self.stats[id].values.push(results[i].stat(name));
  124. self.stats[id].times.push(now.getTime());
  125. if (self.stats[id].values.length > self.maxstats) {
  126. self.stats[id].values.shift();
  127. self.stats[id].times.shift();
  128. }
  129. self.stats[id].endTime = now;
  130. });
  131. }
  132. });
  133. }, 1000);
  134. }
  135. }
  136. /**
  137. * Returns a string representation of a SessionDescription object.
  138. */
  139. var dumpSDP = function(description) {
  140. if (typeof description === 'undefined' || description == null) {
  141. return '';
  142. }
  143. return 'type: ' + description.type + '\r\n' + description.sdp;
  144. };
  145. /**
  146. * Injects receive only SSRC in the sdp if there are not other SSRCs.
  147. * @param desc the SDP that will be modified.
  148. * @returns the modified SDP.
  149. */
  150. TraceablePeerConnection.prototype.ssrcReplacement = function (desc) {
  151. if (typeof desc !== 'object' || desc === null ||
  152. typeof desc.sdp !== 'string') {
  153. logger.warn('An empty description was passed as an argument.');
  154. return desc;
  155. }
  156. var session = transform.parse(desc.sdp);
  157. if (!Array.isArray(session.media))
  158. {
  159. return;
  160. }
  161. var modded = false;
  162. session.media.forEach(function (bLine) {
  163. if(!this.replaceSSRCs[bLine.type])
  164. return;
  165. modded = true;
  166. var SSRCs = this.replaceSSRCs[bLine.type].splice(0,1);
  167. // Stores all SSRCs that should be used on other SRD/SDL operations.
  168. // For every stream that is unmuted we need to replace it SSRC
  169. // otherwise we are going to send jingle packet.
  170. var permSSRCs = [];
  171. //FIXME: The code expects that we have only SIM group or we
  172. // don't have any groups and we have only one SSRC per
  173. // stream. If we add another groups (FID, etc) this code
  174. // must be changed.
  175. while(SSRCs &&
  176. SSRCs.length){
  177. var ssrcOperation = SSRCs[0];
  178. switch(ssrcOperation.type) {
  179. case "mute":
  180. case "addMuted": {
  181. //FIXME: If we want to support multiple streams we have to add
  182. // recv-only ssrcs for the
  183. // muted streams on every change until the stream is unmuted
  184. // or removed. Otherwise the recv-only streams won't be included
  185. // in the SDP
  186. if(!bLine.ssrcs)
  187. bLine.ssrcs = [];
  188. const groups = ssrcOperation.ssrc.groups;
  189. let ssrc = null;
  190. if(groups && groups.length) {
  191. ssrc = groups[0].primarySSRC;
  192. } else if(ssrcOperation.ssrc.ssrcs &&
  193. ssrcOperation.ssrc.ssrcs.length) {
  194. ssrc = ssrcOperation.ssrc.ssrcs[0];
  195. } else {
  196. GlobalOnErrorHandler.callErrorHandler(
  197. new Error("SSRC replacement error!"));
  198. logger.error("SSRC replacement error!");
  199. break;
  200. }
  201. bLine.ssrcs.push({
  202. id: ssrc,
  203. attribute: 'cname',
  204. value: ['recvonly-', ssrc].join('')
  205. });
  206. // If this is executed for another reason we are going to
  207. // include that ssrc as receive only again instead of
  208. // generating new one. Here we are assuming that we have
  209. // only 1 video stream that is muted.
  210. this.recvOnlySSRCs[bLine.type] = ssrc;
  211. break;
  212. }
  213. case "unmute": {
  214. if(!ssrcOperation.ssrc || !ssrcOperation.ssrc.ssrcs ||
  215. !ssrcOperation.ssrc.ssrcs.length)
  216. break;
  217. var ssrcMap = {};
  218. var ssrcLastIdx = ssrcOperation.ssrc.ssrcs.length - 1;
  219. for(var i = 0; i < bLine.ssrcs.length; i++) {
  220. const ssrc = bLine.ssrcs[i];
  221. if (ssrc.attribute !== 'msid' &&
  222. ssrc.value !== ssrcOperation.msid) {
  223. continue;
  224. }
  225. ssrcMap[ssrc.id] =
  226. ssrcOperation.ssrc.ssrcs[ssrcLastIdx];
  227. ssrcLastIdx--;
  228. if(ssrcLastIdx < 0)
  229. break;
  230. }
  231. const groups = ssrcOperation.ssrc.groups;
  232. if (typeof bLine.ssrcGroups !== 'undefined' &&
  233. Array.isArray(bLine.ssrcGroups) && groups &&
  234. groups.length) {
  235. bLine.ssrcGroups.forEach(function (group) {
  236. if(!group.ssrcs)
  237. return;
  238. var currentSSRCs = group.ssrcs.split(" ");
  239. var newGroup = null;
  240. for(var i = 0; i < groups.length; i++) {
  241. newGroup = groups[i].group;
  242. var newSSRCs = newGroup.ssrcs.split(" ");
  243. if(newGroup.semantics !== group.semantics)
  244. continue;
  245. var wrongGroup = false;
  246. for(var j = 0; j < currentSSRCs.length; j++) {
  247. if(newGroup.ssrcs.indexOf(
  248. ssrcMap[currentSSRCs[j]]) === -1){
  249. wrongGroup = true;
  250. break;
  251. }
  252. }
  253. if(!wrongGroup) {
  254. for(j = 0; j < newSSRCs.length; j++) {
  255. ssrcMap[currentSSRCs[j]] = newSSRCs[j];
  256. }
  257. break;
  258. }
  259. }
  260. group.ssrcs = newGroup.ssrcs;
  261. });
  262. }
  263. bLine.ssrcs.forEach(function (ssrc) {
  264. if(ssrcMap[ssrc.id]) {
  265. ssrc.id = ssrcMap[ssrc.id];
  266. }
  267. });
  268. // Storing the unmuted SSRCs.
  269. permSSRCs.push(ssrcOperation);
  270. break;
  271. }
  272. default:
  273. break;
  274. }
  275. SSRCs = this.replaceSSRCs[bLine.type].splice(0,1);
  276. }
  277. // Restoring the unmuted SSRCs.
  278. this.replaceSSRCs[bLine.type] = permSSRCs;
  279. if (!Array.isArray(bLine.ssrcs) || bLine.ssrcs.length === 0)
  280. {
  281. const ssrc = this.recvOnlySSRCs[bLine.type]
  282. = this.recvOnlySSRCs[bLine.type] ||
  283. RandomUtil.randomInt(1, 0xffffffff);
  284. bLine.ssrcs = [{
  285. id: ssrc,
  286. attribute: 'cname',
  287. value: ['recvonly-', ssrc].join('')
  288. }];
  289. }
  290. }.bind(this));
  291. return (!modded) ? desc : new RTCSessionDescription({
  292. type: desc.type,
  293. sdp: transform.write(session),
  294. });
  295. };
  296. /**
  297. * Returns map with keys msid and values ssrc.
  298. * @param desc the SDP that will be modified.
  299. */
  300. function extractSSRCMap(desc) {
  301. if (typeof desc !== 'object' || desc === null ||
  302. typeof desc.sdp !== 'string') {
  303. logger.warn('An empty description was passed as an argument.');
  304. return desc;
  305. }
  306. var ssrcList = {};
  307. var ssrcGroups = {};
  308. var session = transform.parse(desc.sdp);
  309. if (!Array.isArray(session.media))
  310. {
  311. return;
  312. }
  313. session.media.forEach(function (bLine) {
  314. if (!Array.isArray(bLine.ssrcs))
  315. {
  316. return;
  317. }
  318. if (typeof bLine.ssrcGroups !== 'undefined' &&
  319. Array.isArray(bLine.ssrcGroups)) {
  320. bLine.ssrcGroups.forEach(function (group) {
  321. if (typeof group.semantics !== 'undefined' &&
  322. typeof group.ssrcs !== 'undefined') {
  323. var primarySSRC = Number(group.ssrcs.split(' ')[0]);
  324. ssrcGroups[primarySSRC] = ssrcGroups[primarySSRC] || [];
  325. ssrcGroups[primarySSRC].push(group);
  326. }
  327. });
  328. }
  329. bLine.ssrcs.forEach(function (ssrc) {
  330. if(ssrc.attribute !== 'msid')
  331. return;
  332. ssrcList[ssrc.value] = ssrcList[ssrc.value] ||
  333. {groups: [], ssrcs: []};
  334. ssrcList[ssrc.value].ssrcs.push(ssrc.id);
  335. if(ssrcGroups[ssrc.id]){
  336. ssrcGroups[ssrc.id].forEach(function (group) {
  337. ssrcList[ssrc.value].groups.push(
  338. {primarySSRC: ssrc.id, group: group});
  339. });
  340. }
  341. });
  342. });
  343. return ssrcList;
  344. }
  345. /**
  346. * Takes a SessionDescription object and returns a "normalized" version.
  347. * Currently it only takes care of ordering the a=ssrc lines.
  348. */
  349. var normalizePlanB = function(desc) {
  350. if (typeof desc !== 'object' || desc === null ||
  351. typeof desc.sdp !== 'string') {
  352. logger.warn('An empty description was passed as an argument.');
  353. return desc;
  354. }
  355. var transform = require('sdp-transform');
  356. var session = transform.parse(desc.sdp);
  357. if (typeof session !== 'undefined' &&
  358. typeof session.media !== 'undefined' && Array.isArray(session.media)) {
  359. session.media.forEach(function (mLine) {
  360. // Chrome appears to be picky about the order in which a=ssrc lines
  361. // are listed in an m-line when rtx is enabled (and thus there are
  362. // a=ssrc-group lines with FID semantics). Specifically if we have
  363. // "a=ssrc-group:FID S1 S2" and the "a=ssrc:S2" lines appear before
  364. // the "a=ssrc:S1" lines, SRD fails.
  365. // So, put SSRC which appear as the first SSRC in an FID ssrc-group
  366. // first.
  367. var firstSsrcs = [];
  368. var newSsrcLines = [];
  369. if (typeof mLine.ssrcGroups !== 'undefined' &&
  370. Array.isArray(mLine.ssrcGroups)) {
  371. mLine.ssrcGroups.forEach(function (group) {
  372. if (typeof group.semantics !== 'undefined' &&
  373. group.semantics === 'FID') {
  374. if (typeof group.ssrcs !== 'undefined') {
  375. firstSsrcs.push(Number(group.ssrcs.split(' ')[0]));
  376. }
  377. }
  378. });
  379. }
  380. if (typeof mLine.ssrcs !== 'undefined' && Array.isArray(mLine.ssrcs)) {
  381. var i;
  382. for (i = 0; i<mLine.ssrcs.length; i++){
  383. if (typeof mLine.ssrcs[i] === 'object'
  384. && typeof mLine.ssrcs[i].id !== 'undefined'
  385. && firstSsrcs.indexOf(mLine.ssrcs[i].id) >= 0) {
  386. newSsrcLines.push(mLine.ssrcs[i]);
  387. delete mLine.ssrcs[i];
  388. }
  389. }
  390. for (i = 0; i<mLine.ssrcs.length; i++){
  391. if (typeof mLine.ssrcs[i] !== 'undefined') {
  392. newSsrcLines.push(mLine.ssrcs[i]);
  393. }
  394. }
  395. mLine.ssrcs = newSsrcLines;
  396. }
  397. });
  398. }
  399. var resStr = transform.write(session);
  400. return new RTCSessionDescription({
  401. type: desc.type,
  402. sdp: resStr
  403. });
  404. };
  405. var getters = {
  406. signalingState: function () {
  407. return this.peerconnection.signalingState;
  408. },
  409. iceConnectionState: function () {
  410. return this.peerconnection.iceConnectionState;
  411. },
  412. localDescription: function() {
  413. var desc = this.peerconnection.localDescription;
  414. this.trace('getLocalDescription::preTransform', dumpSDP(desc));
  415. // if we're running on FF, transform to Plan B first.
  416. if (RTCBrowserType.usesUnifiedPlan()) {
  417. desc = this.interop.toPlanB(desc);
  418. this.trace('getLocalDescription::postTransform (Plan B)',
  419. dumpSDP(desc));
  420. }
  421. return desc;
  422. },
  423. remoteDescription: function() {
  424. var desc = this.peerconnection.remoteDescription;
  425. this.trace('getRemoteDescription::preTransform', dumpSDP(desc));
  426. // if we're running on FF, transform to Plan B first.
  427. if (RTCBrowserType.usesUnifiedPlan()) {
  428. desc = this.interop.toPlanB(desc);
  429. this.trace('getRemoteDescription::postTransform (Plan B)', dumpSDP(desc));
  430. }
  431. return desc;
  432. }
  433. };
  434. Object.keys(getters).forEach(function (prop) {
  435. Object.defineProperty(
  436. TraceablePeerConnection.prototype,
  437. prop, {
  438. get: getters[prop]
  439. }
  440. );
  441. });
  442. TraceablePeerConnection.prototype.addStream = function (stream, ssrcInfo) {
  443. this.trace('addStream', stream? stream.id : "null");
  444. if(stream)
  445. this.peerconnection.addStream(stream);
  446. if(ssrcInfo && this.replaceSSRCs[ssrcInfo.mtype])
  447. this.replaceSSRCs[ssrcInfo.mtype].push(ssrcInfo);
  448. };
  449. TraceablePeerConnection.prototype.removeStream = function (stream, stopStreams,
  450. ssrcInfo) {
  451. this.trace('removeStream', stream.id);
  452. if(stopStreams) {
  453. RTC.stopMediaStream(stream);
  454. }
  455. // FF doesn't support this yet.
  456. if (this.peerconnection.removeStream) {
  457. this.peerconnection.removeStream(stream);
  458. // Removing all cached ssrcs for the streams that are removed or
  459. // muted.
  460. if(ssrcInfo && this.replaceSSRCs[ssrcInfo.mtype]) {
  461. for(var i = 0; i < this.replaceSSRCs[ssrcInfo.mtype].length; i++) {
  462. var op = this.replaceSSRCs[ssrcInfo.mtype][i];
  463. if(op.type === "unmute" &&
  464. op.ssrc.ssrcs.join("_") ===
  465. ssrcInfo.ssrc.ssrcs.join("_")) {
  466. this.replaceSSRCs[ssrcInfo.mtype].splice(i, 1);
  467. break;
  468. }
  469. }
  470. this.replaceSSRCs[ssrcInfo.mtype].push(ssrcInfo);
  471. }
  472. }
  473. };
  474. TraceablePeerConnection.prototype.createDataChannel = function (label, opts) {
  475. this.trace('createDataChannel', label, opts);
  476. return this.peerconnection.createDataChannel(label, opts);
  477. };
  478. TraceablePeerConnection.prototype.setLocalDescription
  479. = function (description, successCallback, failureCallback) {
  480. this.trace('setLocalDescription::preTransform', dumpSDP(description));
  481. // if we're running on FF, transform to Plan A first.
  482. if (RTCBrowserType.usesUnifiedPlan()) {
  483. description = this.interop.toUnifiedPlan(description);
  484. this.trace('setLocalDescription::postTransform (Plan A)',
  485. dumpSDP(description));
  486. }
  487. var self = this;
  488. this.peerconnection.setLocalDescription(description,
  489. function () {
  490. self.trace('setLocalDescriptionOnSuccess');
  491. successCallback();
  492. },
  493. function (err) {
  494. self.trace('setLocalDescriptionOnFailure', err);
  495. self.eventEmitter.emit(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  496. err, self.peerconnection);
  497. failureCallback(err);
  498. }
  499. );
  500. };
  501. TraceablePeerConnection.prototype.setRemoteDescription
  502. = function (description, successCallback, failureCallback) {
  503. this.trace('setRemoteDescription::preTransform', dumpSDP(description));
  504. // TODO the focus should squeze or explode the remote simulcast
  505. description = this.simulcast.mungeRemoteDescription(description);
  506. this.trace('setRemoteDescription::postTransform (simulcast)', dumpSDP(description));
  507. // if we're running on FF, transform to Plan A first.
  508. if (RTCBrowserType.usesUnifiedPlan()) {
  509. description = this.interop.toUnifiedPlan(description);
  510. this.trace('setRemoteDescription::postTransform (Plan A)', dumpSDP(description));
  511. }
  512. if (RTCBrowserType.usesPlanB()) {
  513. description = normalizePlanB(description);
  514. }
  515. var self = this;
  516. this.peerconnection.setRemoteDescription(description,
  517. function () {
  518. self.trace('setRemoteDescriptionOnSuccess');
  519. successCallback();
  520. },
  521. function (err) {
  522. self.trace('setRemoteDescriptionOnFailure', err);
  523. self.eventEmitter.emit(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  524. err, self.peerconnection);
  525. failureCallback(err);
  526. }
  527. );
  528. /*
  529. if (this.statsinterval === null && this.maxstats > 0) {
  530. // start gathering stats
  531. }
  532. */
  533. };
  534. TraceablePeerConnection.prototype.close = function () {
  535. this.trace('stop');
  536. if (this.statsinterval !== null) {
  537. window.clearInterval(this.statsinterval);
  538. this.statsinterval = null;
  539. }
  540. this.peerconnection.close();
  541. };
  542. TraceablePeerConnection.prototype.createAnswer
  543. = function (successCallback, failureCallback, constraints) {
  544. var self = this;
  545. this.trace('createAnswer', JSON.stringify(constraints, null, ' '));
  546. this.peerconnection.createAnswer(
  547. function (answer) {
  548. try {
  549. self.trace(
  550. 'createAnswerOnSuccess::preTransform', dumpSDP(answer));
  551. // if we're running on FF, transform to Plan A first.
  552. if (RTCBrowserType.usesUnifiedPlan()) {
  553. answer = self.interop.toPlanB(answer);
  554. self.trace('createAnswerOnSuccess::postTransform (Plan B)',
  555. dumpSDP(answer));
  556. }
  557. if (!self.session.room.options.disableSimulcast
  558. && self.simulcast.isSupported()) {
  559. answer = self.simulcast.mungeLocalDescription(answer);
  560. self.trace(
  561. 'createAnswerOnSuccess::postTransform (simulcast)',
  562. dumpSDP(answer));
  563. }
  564. if (!RTCBrowserType.isFirefox())
  565. {
  566. answer = self.ssrcReplacement(answer);
  567. self.trace('createAnswerOnSuccess::mungeLocalVideoSSRC',
  568. dumpSDP(answer));
  569. }
  570. self.eventEmitter.emit(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  571. extractSSRCMap(answer));
  572. successCallback(answer);
  573. } catch (e) {
  574. // there can be error modifying the answer, for example
  575. // for ssrcReplacement there was a track with ssrc that is null
  576. // and if we do not catch the error no callback is called
  577. // at all
  578. self.trace('createAnswerOnError', e);
  579. self.trace('createAnswerOnError', dumpSDP(answer));
  580. logger.error('createAnswerOnError', e, dumpSDP(answer));
  581. failureCallback(e);
  582. }
  583. },
  584. function(err) {
  585. self.trace('createAnswerOnFailure', err);
  586. self.eventEmitter.emit(XMPPEvents.CREATE_ANSWER_FAILED, err,
  587. self.peerconnection);
  588. failureCallback(err);
  589. },
  590. constraints
  591. );
  592. };
  593. TraceablePeerConnection.prototype.addIceCandidate
  594. // eslint-disable-next-line no-unused-vars
  595. = function (candidate, successCallback, failureCallback) {
  596. //var self = this;
  597. this.trace('addIceCandidate', JSON.stringify(candidate, null, ' '));
  598. this.peerconnection.addIceCandidate(candidate);
  599. /* maybe later
  600. this.peerconnection.addIceCandidate(candidate,
  601. function () {
  602. self.trace('addIceCandidateOnSuccess');
  603. successCallback();
  604. },
  605. function (err) {
  606. self.trace('addIceCandidateOnFailure', err);
  607. failureCallback(err);
  608. }
  609. );
  610. */
  611. };
  612. TraceablePeerConnection.prototype.getStats = function(callback, errback) {
  613. // TODO: Is this the correct way to handle Opera, Temasys?
  614. if (RTCBrowserType.isFirefox()
  615. || RTCBrowserType.isTemasysPluginUsed()
  616. || RTCBrowserType.isReactNative()) {
  617. // ignore for now...
  618. if(!errback)
  619. errback = function () {};
  620. this.peerconnection.getStats(null, callback, errback);
  621. } else {
  622. this.peerconnection.getStats(callback);
  623. }
  624. };
  625. /**
  626. * Generate ssrc info object for a stream with the following properties:
  627. * - ssrcs - Array of the ssrcs associated with the stream.
  628. * - groups - Array of the groups associated with the stream.
  629. */
  630. TraceablePeerConnection.prototype.generateNewStreamSSRCInfo = function () {
  631. if (!this.session.room.options.disableSimulcast
  632. && this.simulcast.isSupported()) {
  633. var ssrcInfo = {ssrcs: [], groups: []};
  634. for(var i = 0; i < SIMULCAST_LAYERS; i++)
  635. ssrcInfo.ssrcs.push(RandomUtil.randomInt(1, 0xffffffff));
  636. ssrcInfo.groups.push({
  637. primarySSRC: ssrcInfo.ssrcs[0],
  638. group: {ssrcs: ssrcInfo.ssrcs.join(" "), semantics: "SIM"}});
  639. return ssrcInfo;
  640. } else {
  641. return {ssrcs: [RandomUtil.randomInt(1, 0xffffffff)], groups: []};
  642. }
  643. };
  644. module.exports = TraceablePeerConnection;