您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TraceablePeerConnection.js 25KB

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