You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TraceablePeerConnection.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. // Stores all SSRCs that should be used on other SRD/SDL operations.
  166. // For every stream that is unmuted we need to replace it SSRC
  167. // otherwise we are going to send jingle packet.
  168. var permSSRCs = [];
  169. //FIXME: The code expects that we have only SIM group or we
  170. // don't have any groups and we have only one SSRC per
  171. // stream. If we add another groups (FID, etc) this code
  172. // must be changed.
  173. while(SSRCs &&
  174. SSRCs.length){
  175. var ssrcOperation = SSRCs[0];
  176. switch(ssrcOperation.type) {
  177. case "mute":
  178. case "addMuted":
  179. //FIXME: If we want to support multiple streams we have to add
  180. // recv-only ssrcs for the
  181. // muted streams on every change until the stream is unmuted
  182. // or removed. Otherwise the recv-only streams won't be included
  183. // in the SDP
  184. if(!bLine.ssrcs)
  185. bLine.ssrcs = [];
  186. var groups = ssrcOperation.ssrc.groups;
  187. var ssrc = null;
  188. if(groups && groups.length) {
  189. ssrc = groups[0].primarySSRC;
  190. } else if(ssrcOperation.ssrc.ssrcs &&
  191. ssrcOperation.ssrc.ssrcs.length) {
  192. ssrc = ssrcOperation.ssrc.ssrcs[0];
  193. } else {
  194. logger.error("SSRC replacement error!");
  195. break;
  196. }
  197. bLine.ssrcs.push({
  198. id: ssrc,
  199. attribute: 'cname',
  200. value: ['recvonly-', ssrc].join('')
  201. });
  202. // If this is executed for another reason we are going to
  203. // include that ssrc as receive only again instead of
  204. // generating new one. Here we are assuming that we have
  205. // only 1 video stream that is muted.
  206. this.recvOnlySSRCs[bLine.type] = ssrc;
  207. break;
  208. case "unmute":
  209. if(!ssrcOperation.ssrc || !ssrcOperation.ssrc.ssrcs ||
  210. !ssrcOperation.ssrc.ssrcs.length)
  211. break;
  212. var ssrcMap = {};
  213. var ssrcLastIdx = ssrcOperation.ssrc.ssrcs.length - 1;
  214. for(var i = 0; i < bLine.ssrcs.length; i++) {
  215. var ssrc = bLine.ssrcs[i];
  216. if (ssrc.attribute !== 'msid' &&
  217. ssrc.value !== ssrcOperation.msid) {
  218. continue;
  219. }
  220. ssrcMap[ssrc.id] =
  221. ssrcOperation.ssrc.ssrcs[ssrcLastIdx];
  222. ssrcLastIdx--;
  223. if(ssrcLastIdx < 0)
  224. break;
  225. }
  226. var groups = ssrcOperation.ssrc.groups;
  227. if (typeof bLine.ssrcGroups !== 'undefined' &&
  228. Array.isArray(bLine.ssrcGroups) && groups &&
  229. groups.length) {
  230. bLine.ssrcGroups.forEach(function (group) {
  231. if(!group.ssrcs)
  232. return;
  233. var currentSSRCs = group.ssrcs.split(" ");
  234. var newGroup = null;
  235. for(var i = 0; i < groups.length; i++) {
  236. newGroup = groups[i].group;
  237. var newSSRCs = newGroup.ssrcs.split(" ");
  238. if(newGroup.semantics !== group.semantics)
  239. continue;
  240. var wrongGroup = false;
  241. for(var j = 0; j < currentSSRCs.length; j++) {
  242. if(newGroup.ssrcs.indexOf(
  243. ssrcMap[currentSSRCs[j]]) === -1){
  244. wrongGroup = true;
  245. break;
  246. }
  247. }
  248. if(!wrongGroup) {
  249. for(j = 0; j < newSSRCs.length; j++) {
  250. ssrcMap[currentSSRCs[j]] = newSSRCs[j];
  251. }
  252. break;
  253. }
  254. }
  255. group.ssrcs = newGroup.ssrcs;
  256. });
  257. }
  258. bLine.ssrcs.forEach(function (ssrc) {
  259. if(ssrcMap[ssrc.id]) {
  260. ssrc.id = ssrcMap[ssrc.id];
  261. }
  262. });
  263. // Storing the unmuted SSRCs.
  264. permSSRCs.push(ssrcOperation);
  265. break;
  266. default:
  267. break;
  268. }
  269. SSRCs = this.replaceSSRCs[bLine.type].splice(0,1);
  270. }
  271. // Restoring the unmuted SSRCs.
  272. this.replaceSSRCs[bLine.type] = permSSRCs;
  273. if (!Array.isArray(bLine.ssrcs) || bLine.ssrcs.length === 0)
  274. {
  275. var ssrc = this.recvOnlySSRCs[bLine.type]
  276. = this.recvOnlySSRCs[bLine.type] ||
  277. RandomUtil.randomInt(1, 0xffffffff);
  278. bLine.ssrcs = [{
  279. id: ssrc,
  280. attribute: 'cname',
  281. value: ['recvonly-', ssrc].join('')
  282. }];
  283. }
  284. }.bind(this));
  285. return (!modded) ? desc : new RTCSessionDescription({
  286. type: desc.type,
  287. sdp: transform.write(session),
  288. });
  289. };
  290. /**
  291. * Returns map with keys msid and values ssrc.
  292. * @param desc the SDP that will be modified.
  293. */
  294. function extractSSRCMap(desc) {
  295. if (typeof desc !== 'object' || desc === null ||
  296. typeof desc.sdp !== 'string') {
  297. console.warn('An empty description was passed as an argument.');
  298. return desc;
  299. }
  300. var ssrcList = {};
  301. var ssrcGroups = {};
  302. var session = transform.parse(desc.sdp);
  303. if (!Array.isArray(session.media))
  304. {
  305. return;
  306. }
  307. session.media.forEach(function (bLine) {
  308. if (!Array.isArray(bLine.ssrcs))
  309. {
  310. return;
  311. }
  312. if (typeof bLine.ssrcGroups !== 'undefined' &&
  313. Array.isArray(bLine.ssrcGroups)) {
  314. bLine.ssrcGroups.forEach(function (group) {
  315. if (typeof group.semantics !== 'undefined' &&
  316. typeof group.ssrcs !== 'undefined') {
  317. var primarySSRC = Number(group.ssrcs.split(' ')[0]);
  318. ssrcGroups[primarySSRC] = ssrcGroups[primarySSRC] || [];
  319. ssrcGroups[primarySSRC].push(group);
  320. }
  321. });
  322. }
  323. bLine.ssrcs.forEach(function (ssrc) {
  324. if(ssrc.attribute !== 'msid')
  325. return;
  326. ssrcList[ssrc.value] = ssrcList[ssrc.value] ||
  327. {groups: [], ssrcs: []};
  328. ssrcList[ssrc.value].ssrcs.push(ssrc.id);
  329. if(ssrcGroups[ssrc.id]){
  330. ssrcGroups[ssrc.id].forEach(function (group) {
  331. ssrcList[ssrc.value].groups.push(
  332. {primarySSRC: ssrc.id, group: group});
  333. });
  334. }
  335. });
  336. });
  337. return ssrcList;
  338. }
  339. /**
  340. * Takes a SessionDescription object and returns a "normalized" version.
  341. * Currently it only takes care of ordering the a=ssrc lines.
  342. */
  343. var normalizePlanB = function(desc) {
  344. if (typeof desc !== 'object' || desc === null ||
  345. typeof desc.sdp !== 'string') {
  346. logger.warn('An empty description was passed as an argument.');
  347. return desc;
  348. }
  349. var transform = require('sdp-transform');
  350. var session = transform.parse(desc.sdp);
  351. if (typeof session !== 'undefined' &&
  352. typeof session.media !== 'undefined' && Array.isArray(session.media)) {
  353. session.media.forEach(function (mLine) {
  354. // Chrome appears to be picky about the order in which a=ssrc lines
  355. // are listed in an m-line when rtx is enabled (and thus there are
  356. // a=ssrc-group lines with FID semantics). Specifically if we have
  357. // "a=ssrc-group:FID S1 S2" and the "a=ssrc:S2" lines appear before
  358. // the "a=ssrc:S1" lines, SRD fails.
  359. // So, put SSRC which appear as the first SSRC in an FID ssrc-group
  360. // first.
  361. var firstSsrcs = [];
  362. var newSsrcLines = [];
  363. if (typeof mLine.ssrcGroups !== 'undefined' &&
  364. Array.isArray(mLine.ssrcGroups)) {
  365. mLine.ssrcGroups.forEach(function (group) {
  366. if (typeof group.semantics !== 'undefined' &&
  367. group.semantics === 'FID') {
  368. if (typeof group.ssrcs !== 'undefined') {
  369. firstSsrcs.push(Number(group.ssrcs.split(' ')[0]));
  370. }
  371. }
  372. });
  373. }
  374. if (typeof mLine.ssrcs !== 'undefined' && Array.isArray(mLine.ssrcs)) {
  375. var i;
  376. for (i = 0; i<mLine.ssrcs.length; i++){
  377. if (typeof mLine.ssrcs[i] === 'object'
  378. && typeof mLine.ssrcs[i].id !== 'undefined'
  379. && firstSsrcs.indexOf(mLine.ssrcs[i].id) >= 0) {
  380. newSsrcLines.push(mLine.ssrcs[i]);
  381. delete mLine.ssrcs[i];
  382. }
  383. }
  384. for (i = 0; i<mLine.ssrcs.length; i++){
  385. if (typeof mLine.ssrcs[i] !== 'undefined') {
  386. newSsrcLines.push(mLine.ssrcs[i]);
  387. }
  388. }
  389. mLine.ssrcs = newSsrcLines;
  390. }
  391. });
  392. }
  393. var resStr = transform.write(session);
  394. return new RTCSessionDescription({
  395. type: desc.type,
  396. sdp: resStr
  397. });
  398. };
  399. var getters = {
  400. signalingState: function () {
  401. return this.peerconnection.signalingState;
  402. },
  403. iceConnectionState: function () {
  404. return this.peerconnection.iceConnectionState;
  405. },
  406. localDescription: function() {
  407. var desc = this.peerconnection.localDescription;
  408. this.trace('getLocalDescription::preTransform', dumpSDP(desc));
  409. // if we're running on FF, transform to Plan B first.
  410. if (RTCBrowserType.usesUnifiedPlan()) {
  411. desc = this.interop.toPlanB(desc);
  412. this.trace('getLocalDescription::postTransform (Plan B)',
  413. dumpSDP(desc));
  414. }
  415. return desc;
  416. },
  417. remoteDescription: function() {
  418. var desc = this.peerconnection.remoteDescription;
  419. this.trace('getRemoteDescription::preTransform', dumpSDP(desc));
  420. // if we're running on FF, transform to Plan B first.
  421. if (RTCBrowserType.usesUnifiedPlan()) {
  422. desc = this.interop.toPlanB(desc);
  423. this.trace('getRemoteDescription::postTransform (Plan B)', dumpSDP(desc));
  424. }
  425. return desc;
  426. }
  427. };
  428. Object.keys(getters).forEach(function (prop) {
  429. Object.defineProperty(
  430. TraceablePeerConnection.prototype,
  431. prop, {
  432. get: getters[prop]
  433. }
  434. );
  435. });
  436. TraceablePeerConnection.prototype.addStream = function (stream, ssrcInfo) {
  437. this.trace('addStream', stream? stream.id : "null");
  438. try
  439. {
  440. if(stream)
  441. this.peerconnection.addStream(stream);
  442. if(ssrcInfo && this.replaceSSRCs[ssrcInfo.mtype])
  443. this.replaceSSRCs[ssrcInfo.mtype].push(ssrcInfo);
  444. }
  445. catch (e)
  446. {
  447. logger.error(e);
  448. }
  449. };
  450. TraceablePeerConnection.prototype.removeStream = function (stream, stopStreams,
  451. ssrcInfo) {
  452. this.trace('removeStream', stream.id);
  453. if(stopStreams) {
  454. RTC.stopMediaStream(stream);
  455. }
  456. try {
  457. // FF doesn't support this yet.
  458. if (this.peerconnection.removeStream) {
  459. this.peerconnection.removeStream(stream);
  460. // Removing all cached ssrcs for the streams that are removed or
  461. // muted.
  462. if(ssrcInfo && this.replaceSSRCs[ssrcInfo.mtype]) {
  463. for(i = 0; i < this.replaceSSRCs[ssrcInfo.mtype].length; i++) {
  464. var op = this.replaceSSRCs[ssrcInfo.mtype][i];
  465. if(op.type === "unmute" &&
  466. op.ssrc.ssrcs.join("_") ===
  467. ssrcInfo.ssrc.ssrcs.join("_")) {
  468. this.replaceSSRCs[ssrcInfo.mtype].splice(i, 1);
  469. break;
  470. }
  471. }
  472. this.replaceSSRCs[ssrcInfo.mtype].push(ssrcInfo);
  473. }
  474. }
  475. } catch (e) {
  476. logger.error(e);
  477. }
  478. };
  479. TraceablePeerConnection.prototype.createDataChannel = function (label, opts) {
  480. this.trace('createDataChannel', label, opts);
  481. return this.peerconnection.createDataChannel(label, opts);
  482. };
  483. TraceablePeerConnection.prototype.setLocalDescription
  484. = function (description, successCallback, failureCallback) {
  485. this.trace('setLocalDescription::preTransform', dumpSDP(description));
  486. // if we're running on FF, transform to Plan A first.
  487. if (RTCBrowserType.usesUnifiedPlan()) {
  488. description = this.interop.toUnifiedPlan(description);
  489. this.trace('setLocalDescription::postTransform (Plan A)',
  490. dumpSDP(description));
  491. }
  492. var self = this;
  493. this.peerconnection.setLocalDescription(description,
  494. function () {
  495. self.trace('setLocalDescriptionOnSuccess');
  496. successCallback();
  497. },
  498. function (err) {
  499. self.trace('setLocalDescriptionOnFailure', err);
  500. self.eventEmitter.emit(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  501. err, self.peerconnection);
  502. failureCallback(err);
  503. }
  504. );
  505. };
  506. TraceablePeerConnection.prototype.setRemoteDescription
  507. = function (description, successCallback, failureCallback) {
  508. this.trace('setRemoteDescription::preTransform', dumpSDP(description));
  509. // TODO the focus should squeze or explode the remote simulcast
  510. description = this.simulcast.mungeRemoteDescription(description);
  511. this.trace('setRemoteDescription::postTransform (simulcast)', dumpSDP(description));
  512. // if we're running on FF, transform to Plan A first.
  513. if (RTCBrowserType.usesUnifiedPlan()) {
  514. description = this.interop.toUnifiedPlan(description);
  515. this.trace('setRemoteDescription::postTransform (Plan A)', dumpSDP(description));
  516. }
  517. if (RTCBrowserType.usesPlanB()) {
  518. description = normalizePlanB(description);
  519. }
  520. var self = this;
  521. this.peerconnection.setRemoteDescription(description,
  522. function () {
  523. self.trace('setRemoteDescriptionOnSuccess');
  524. successCallback();
  525. },
  526. function (err) {
  527. self.trace('setRemoteDescriptionOnFailure', err);
  528. self.eventEmitter.emit(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  529. err, self.peerconnection);
  530. failureCallback(err);
  531. }
  532. );
  533. /*
  534. if (this.statsinterval === null && this.maxstats > 0) {
  535. // start gathering stats
  536. }
  537. */
  538. };
  539. TraceablePeerConnection.prototype.close = function () {
  540. this.trace('stop');
  541. if (this.statsinterval !== null) {
  542. window.clearInterval(this.statsinterval);
  543. this.statsinterval = null;
  544. }
  545. this.peerconnection.close();
  546. };
  547. TraceablePeerConnection.prototype.createAnswer
  548. = function (successCallback, failureCallback, constraints) {
  549. var self = this;
  550. this.trace('createAnswer', JSON.stringify(constraints, null, ' '));
  551. this.peerconnection.createAnswer(
  552. function (answer) {
  553. self.trace('createAnswerOnSuccess::preTransform', dumpSDP(answer));
  554. // if we're running on FF, transform to Plan A first.
  555. if (RTCBrowserType.usesUnifiedPlan()) {
  556. answer = self.interop.toPlanB(answer);
  557. self.trace('createAnswerOnSuccess::postTransform (Plan B)',
  558. dumpSDP(answer));
  559. }
  560. if (!self.session.room.options.disableSimulcast
  561. && self.simulcast.isSupported()) {
  562. answer = self.simulcast.mungeLocalDescription(answer);
  563. self.trace('createAnswerOnSuccess::postTransform (simulcast)',
  564. dumpSDP(answer));
  565. }
  566. if (!RTCBrowserType.isFirefox())
  567. {
  568. answer = self.ssrcReplacement(answer);
  569. self.trace('createAnswerOnSuccess::mungeLocalVideoSSRC',
  570. dumpSDP(answer));
  571. }
  572. self.eventEmitter.emit(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  573. extractSSRCMap(answer));
  574. successCallback(answer);
  575. },
  576. function(err) {
  577. self.trace('createAnswerOnFailure', err);
  578. self.eventEmitter.emit(XMPPEvents.CREATE_ANSWER_FAILED, err,
  579. self.peerconnection);
  580. failureCallback(err);
  581. },
  582. constraints
  583. );
  584. };
  585. TraceablePeerConnection.prototype.addIceCandidate
  586. = function (candidate, successCallback, failureCallback) {
  587. //var self = this;
  588. this.trace('addIceCandidate', JSON.stringify(candidate, null, ' '));
  589. this.peerconnection.addIceCandidate(candidate);
  590. /* maybe later
  591. this.peerconnection.addIceCandidate(candidate,
  592. function () {
  593. self.trace('addIceCandidateOnSuccess');
  594. successCallback();
  595. },
  596. function (err) {
  597. self.trace('addIceCandidateOnFailure', err);
  598. failureCallback(err);
  599. }
  600. );
  601. */
  602. };
  603. TraceablePeerConnection.prototype.getStats = function(callback, errback) {
  604. // TODO: Is this the correct way to handle Opera, Temasys?
  605. if (RTCBrowserType.isFirefox() || RTCBrowserType.isTemasysPluginUsed()) {
  606. // ignore for now...
  607. if(!errback)
  608. errback = function () {};
  609. this.peerconnection.getStats(null, callback, errback);
  610. } else {
  611. this.peerconnection.getStats(callback);
  612. }
  613. };
  614. /**
  615. * Generate ssrc info object for a stream with the following properties:
  616. * - ssrcs - Array of the ssrcs associated with the stream.
  617. * - groups - Array of the groups associated with the stream.
  618. */
  619. TraceablePeerConnection.prototype.generateNewStreamSSRCInfo = function () {
  620. if (!this.session.room.options.disableSimulcast
  621. && this.simulcast.isSupported()) {
  622. var ssrcInfo = {ssrcs: [], groups: []};
  623. for(var i = 0; i < SIMULCAST_LAYERS; i++)
  624. ssrcInfo.ssrcs.push(RandomUtil.randomInt(1, 0xffffffff));
  625. ssrcInfo.groups.push({
  626. primarySSRC: ssrcInfo.ssrcs[0],
  627. group: {ssrcs: ssrcInfo.ssrcs.join(" "), semantics: "SIM"}});
  628. return ssrcInfo;
  629. } else {
  630. return {ssrcs: [RandomUtil.randomInt(1, 0xffffffff)], groups: []};
  631. }
  632. };
  633. module.exports = TraceablePeerConnection;