Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TraceablePeerConnection.js 24KB

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