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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. function TraceablePeerConnection(ice_config, constraints, session) {
  7. var self = this;
  8. this.session = session;
  9. var RTCPeerConnectionType = null;
  10. if (RTCBrowserType.isFirefox()) {
  11. RTCPeerConnectionType = mozRTCPeerConnection;
  12. } else if (RTCBrowserType.isTemasysPluginUsed()) {
  13. RTCPeerConnectionType = RTCPeerConnection;
  14. } else {
  15. RTCPeerConnectionType = webkitRTCPeerConnection;
  16. }
  17. this.peerconnection = new RTCPeerConnectionType(ice_config, constraints);
  18. this.updateLog = [];
  19. this.stats = {};
  20. this.statsinterval = null;
  21. this.maxstats = 0; // limit to 300 values, i.e. 5 minutes; set to 0 to disable
  22. var Interop = require('sdp-interop').Interop;
  23. this.interop = new Interop();
  24. var Simulcast = require('sdp-simulcast');
  25. this.simulcast = new Simulcast({numOfLayers: 3,
  26. explodeRemoteSimulcast: false});
  27. this.eventEmitter = this.session.room.eventEmitter;
  28. // override as desired
  29. this.trace = function (what, info) {
  30. /*logger.warn('WTRACE', what, info);
  31. if (info && RTCBrowserType.isIExplorer()) {
  32. if (info.length > 1024) {
  33. logger.warn('WTRACE', what, info.substr(1024));
  34. }
  35. if (info.length > 2048) {
  36. logger.warn('WTRACE', what, info.substr(2048));
  37. }
  38. }*/
  39. self.updateLog.push({
  40. time: new Date(),
  41. type: what,
  42. value: info || ""
  43. });
  44. };
  45. this.onicecandidate = null;
  46. this.peerconnection.onicecandidate = function (event) {
  47. // FIXME: this causes stack overflow with Temasys Plugin
  48. if (!RTCBrowserType.isTemasysPluginUsed())
  49. self.trace('onicecandidate', JSON.stringify(event.candidate, null, ' '));
  50. if (self.onicecandidate !== null) {
  51. self.onicecandidate(event);
  52. }
  53. };
  54. this.onaddstream = null;
  55. this.peerconnection.onaddstream = function (event) {
  56. self.trace('onaddstream', event.stream.id);
  57. if (self.onaddstream !== null) {
  58. self.onaddstream(event);
  59. }
  60. };
  61. this.onremovestream = null;
  62. this.peerconnection.onremovestream = function (event) {
  63. self.trace('onremovestream', event.stream.id);
  64. if (self.onremovestream !== null) {
  65. self.onremovestream(event);
  66. }
  67. };
  68. this.onsignalingstatechange = null;
  69. this.peerconnection.onsignalingstatechange = function (event) {
  70. self.trace('onsignalingstatechange', self.signalingState);
  71. if (self.onsignalingstatechange !== null) {
  72. self.onsignalingstatechange(event);
  73. }
  74. };
  75. this.oniceconnectionstatechange = null;
  76. this.peerconnection.oniceconnectionstatechange = function (event) {
  77. self.trace('oniceconnectionstatechange', self.iceConnectionState);
  78. if (self.oniceconnectionstatechange !== null) {
  79. self.oniceconnectionstatechange(event);
  80. }
  81. };
  82. this.onnegotiationneeded = null;
  83. this.peerconnection.onnegotiationneeded = function (event) {
  84. self.trace('onnegotiationneeded');
  85. if (self.onnegotiationneeded !== null) {
  86. self.onnegotiationneeded(event);
  87. }
  88. };
  89. self.ondatachannel = null;
  90. this.peerconnection.ondatachannel = function (event) {
  91. self.trace('ondatachannel', event);
  92. if (self.ondatachannel !== null) {
  93. self.ondatachannel(event);
  94. }
  95. };
  96. // XXX: do all non-firefox browsers which we support also support this?
  97. if (!RTCBrowserType.isFirefox() && this.maxstats) {
  98. this.statsinterval = window.setInterval(function() {
  99. self.peerconnection.getStats(function(stats) {
  100. var results = stats.result();
  101. var now = new Date();
  102. for (var i = 0; i < results.length; ++i) {
  103. results[i].names().forEach(function (name) {
  104. var id = results[i].id + '-' + name;
  105. if (!self.stats[id]) {
  106. self.stats[id] = {
  107. startTime: now,
  108. endTime: now,
  109. values: [],
  110. times: []
  111. };
  112. }
  113. self.stats[id].values.push(results[i].stat(name));
  114. self.stats[id].times.push(now.getTime());
  115. if (self.stats[id].values.length > self.maxstats) {
  116. self.stats[id].values.shift();
  117. self.stats[id].times.shift();
  118. }
  119. self.stats[id].endTime = now;
  120. });
  121. }
  122. });
  123. }, 1000);
  124. }
  125. }
  126. /**
  127. * Returns a string representation of a SessionDescription object.
  128. */
  129. var dumpSDP = function(description) {
  130. if (typeof description === 'undefined' || description == null) {
  131. return '';
  132. }
  133. return 'type: ' + description.type + '\r\n' + description.sdp;
  134. };
  135. var insertRecvOnlySSRC = function (desc) {
  136. if (typeof desc !== 'object' || desc === null ||
  137. typeof desc.sdp !== 'string') {
  138. console.warn('An empty description was passed as an argument.');
  139. return desc;
  140. }
  141. var transform = require('sdp-transform');
  142. var RandomUtil = require('../util/RandomUtil');
  143. var session = transform.parse(desc.sdp);
  144. if (!Array.isArray(session.media))
  145. {
  146. return;
  147. }
  148. var modded = false;
  149. session.media.forEach(function (bLine) {
  150. if (bLine.direction != 'recvonly')
  151. {
  152. return;
  153. }
  154. modded = true;
  155. if (!Array.isArray(bLine.ssrcs) || bLine.ssrcs.length === 0)
  156. {
  157. var ssrc = RandomUtil.randomInt(1, 0xffffffff);
  158. bLine.ssrcs = [{
  159. id: ssrc,
  160. attribute: 'cname',
  161. value: ['recvonly-', ssrc].join('')
  162. }];
  163. }
  164. });
  165. return (!modded) ? desc : new RTCSessionDescription({
  166. type: desc.type,
  167. sdp: transform.write(session),
  168. });
  169. };
  170. /**
  171. * Takes a SessionDescription object and returns a "normalized" version.
  172. * Currently it only takes care of ordering the a=ssrc lines.
  173. */
  174. var normalizePlanB = function(desc) {
  175. if (typeof desc !== 'object' || desc === null ||
  176. typeof desc.sdp !== 'string') {
  177. logger.warn('An empty description was passed as an argument.');
  178. return desc;
  179. }
  180. var transform = require('sdp-transform');
  181. var session = transform.parse(desc.sdp);
  182. if (typeof session !== 'undefined' && typeof session.media !== 'undefined' &&
  183. Array.isArray(session.media)) {
  184. session.media.forEach(function (mLine) {
  185. // Chrome appears to be picky about the order in which a=ssrc lines
  186. // are listed in an m-line when rtx is enabled (and thus there are
  187. // a=ssrc-group lines with FID semantics). Specifically if we have
  188. // "a=ssrc-group:FID S1 S2" and the "a=ssrc:S2" lines appear before
  189. // the "a=ssrc:S1" lines, SRD fails.
  190. // So, put SSRC which appear as the first SSRC in an FID ssrc-group
  191. // first.
  192. var firstSsrcs = [];
  193. var newSsrcLines = [];
  194. if (typeof mLine.ssrcGroups !== 'undefined' && Array.isArray(mLine.ssrcGroups)) {
  195. mLine.ssrcGroups.forEach(function (group) {
  196. if (typeof group.semantics !== 'undefined' &&
  197. group.semantics === 'FID') {
  198. if (typeof group.ssrcs !== 'undefined') {
  199. firstSsrcs.push(Number(group.ssrcs.split(' ')[0]));
  200. }
  201. }
  202. });
  203. }
  204. if (typeof mLine.ssrcs !== 'undefined' && Array.isArray(mLine.ssrcs)) {
  205. var i;
  206. for (i = 0; i<mLine.ssrcs.length; i++){
  207. if (typeof mLine.ssrcs[i] === 'object'
  208. && typeof mLine.ssrcs[i].id !== 'undefined'
  209. && !$.inArray(mLine.ssrcs[i].id, firstSsrcs)) {
  210. newSsrcLines.push(mLine.ssrcs[i]);
  211. delete mLine.ssrcs[i];
  212. }
  213. }
  214. for (i = 0; i<mLine.ssrcs.length; i++){
  215. if (typeof mLine.ssrcs[i] !== 'undefined') {
  216. newSsrcLines.push(mLine.ssrcs[i]);
  217. }
  218. }
  219. mLine.ssrcs = newSsrcLines;
  220. }
  221. });
  222. }
  223. var resStr = transform.write(session);
  224. return new RTCSessionDescription({
  225. type: desc.type,
  226. sdp: resStr
  227. });
  228. };
  229. var getters = {
  230. signalingState: function () {
  231. return this.peerconnection.signalingState;
  232. },
  233. iceConnectionState: function () {
  234. return this.peerconnection.iceConnectionState;
  235. },
  236. localDescription: function() {
  237. var desc = this.peerconnection.localDescription;
  238. this.trace('getLocalDescription::preTransform', dumpSDP(desc));
  239. // if we're running on FF, transform to Plan B first.
  240. if (RTCBrowserType.usesUnifiedPlan()) {
  241. desc = this.interop.toPlanB(desc);
  242. this.trace('getLocalDescription::postTransform (Plan B)',
  243. dumpSDP(desc));
  244. }
  245. return desc;
  246. },
  247. remoteDescription: function() {
  248. var desc = this.peerconnection.remoteDescription;
  249. this.trace('getRemoteDescription::preTransform', dumpSDP(desc));
  250. // if we're running on FF, transform to Plan B first.
  251. if (RTCBrowserType.usesUnifiedPlan()) {
  252. desc = this.interop.toPlanB(desc);
  253. this.trace('getRemoteDescription::postTransform (Plan B)', dumpSDP(desc));
  254. }
  255. return desc;
  256. }
  257. };
  258. Object.keys(getters).forEach(function (prop) {
  259. Object.defineProperty(
  260. TraceablePeerConnection.prototype,
  261. prop, {
  262. get: getters[prop]
  263. }
  264. );
  265. });
  266. TraceablePeerConnection.prototype.addStream = function (stream) {
  267. this.trace('addStream', stream.id);
  268. try
  269. {
  270. this.peerconnection.addStream(stream);
  271. }
  272. catch (e)
  273. {
  274. logger.error(e);
  275. }
  276. };
  277. TraceablePeerConnection.prototype.removeStream = function (stream, stopStreams) {
  278. this.trace('removeStream', stream.id);
  279. if(stopStreams) {
  280. RTC.stopMediaStream(stream);
  281. }
  282. try {
  283. // FF doesn't support this yet.
  284. if (this.peerconnection.removeStream)
  285. this.peerconnection.removeStream(stream);
  286. } catch (e) {
  287. logger.error(e);
  288. }
  289. };
  290. TraceablePeerConnection.prototype.createDataChannel = function (label, opts) {
  291. this.trace('createDataChannel', label, opts);
  292. return this.peerconnection.createDataChannel(label, opts);
  293. };
  294. TraceablePeerConnection.prototype.setLocalDescription
  295. = function (description, successCallback, failureCallback) {
  296. this.trace('setLocalDescription::preTransform', dumpSDP(description));
  297. // if we're running on FF, transform to Plan A first.
  298. if (RTCBrowserType.usesUnifiedPlan()) {
  299. description = this.interop.toUnifiedPlan(description);
  300. this.trace('setLocalDescription::postTransform (Plan A)',
  301. dumpSDP(description));
  302. }
  303. var self = this;
  304. this.peerconnection.setLocalDescription(description,
  305. function () {
  306. self.trace('setLocalDescriptionOnSuccess');
  307. successCallback();
  308. },
  309. function (err) {
  310. self.trace('setLocalDescriptionOnFailure', err);
  311. self.eventEmitter.emit(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  312. err, self.peerconnection);
  313. failureCallback(err);
  314. }
  315. );
  316. };
  317. TraceablePeerConnection.prototype.setRemoteDescription
  318. = function (description, successCallback, failureCallback) {
  319. this.trace('setRemoteDescription::preTransform', dumpSDP(description));
  320. // TODO the focus should squeze or explode the remote simulcast
  321. description = this.simulcast.mungeRemoteDescription(description);
  322. this.trace('setRemoteDescription::postTransform (simulcast)', dumpSDP(description));
  323. // if we're running on FF, transform to Plan A first.
  324. if (RTCBrowserType.usesUnifiedPlan()) {
  325. description = this.interop.toUnifiedPlan(description);
  326. this.trace('setRemoteDescription::postTransform (Plan A)', dumpSDP(description));
  327. }
  328. if (RTCBrowserType.usesPlanB()) {
  329. description = normalizePlanB(description);
  330. }
  331. var self = this;
  332. this.peerconnection.setRemoteDescription(description,
  333. function () {
  334. self.trace('setRemoteDescriptionOnSuccess');
  335. successCallback();
  336. },
  337. function (err) {
  338. self.trace('setRemoteDescriptionOnFailure', err);
  339. self.eventEmitter.emit(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  340. err, self.peerconnection);
  341. failureCallback(err);
  342. }
  343. );
  344. /*
  345. if (this.statsinterval === null && this.maxstats > 0) {
  346. // start gathering stats
  347. }
  348. */
  349. };
  350. TraceablePeerConnection.prototype.close = function () {
  351. this.trace('stop');
  352. if (this.statsinterval !== null) {
  353. window.clearInterval(this.statsinterval);
  354. this.statsinterval = null;
  355. }
  356. this.peerconnection.close();
  357. };
  358. TraceablePeerConnection.prototype.createOffer
  359. = function (successCallback, failureCallback, constraints) {
  360. var self = this;
  361. this.trace('createOffer', JSON.stringify(constraints, null, ' '));
  362. this.peerconnection.createOffer(
  363. function (offer) {
  364. self.trace('createOfferOnSuccess::preTransform', dumpSDP(offer));
  365. // NOTE this is not tested because in meet the focus generates the
  366. // offer.
  367. // if we're running on FF, transform to Plan B first.
  368. if (RTCBrowserType.usesUnifiedPlan()) {
  369. offer = self.interop.toPlanB(offer);
  370. self.trace('createOfferOnSuccess::postTransform (Plan B)', dumpSDP(offer));
  371. }
  372. if (RTCBrowserType.isChrome())
  373. {
  374. offer = insertRecvOnlySSRC(offer);
  375. self.trace('createOfferOnSuccess::mungeLocalVideoSSRC', dumpSDP(offer));
  376. }
  377. if (!self.session.room.options.disableSimulcast
  378. && self.simulcast.isSupported()) {
  379. offer = self.simulcast.mungeLocalDescription(offer);
  380. self.trace('createOfferOnSuccess::postTransform (simulcast)',
  381. dumpSDP(offer));
  382. }
  383. successCallback(offer);
  384. },
  385. function(err) {
  386. self.trace('createOfferOnFailure', err);
  387. self.eventEmitter.emit(XMPPEvents.CREATE_OFFER_FAILED, err,
  388. self.peerconnection);
  389. failureCallback(err);
  390. },
  391. constraints
  392. );
  393. };
  394. TraceablePeerConnection.prototype.createAnswer
  395. = function (successCallback, failureCallback, constraints) {
  396. var self = this;
  397. this.trace('createAnswer', JSON.stringify(constraints, null, ' '));
  398. this.peerconnection.createAnswer(
  399. function (answer) {
  400. self.trace('createAnswerOnSuccess::preTransform', dumpSDP(answer));
  401. // if we're running on FF, transform to Plan A first.
  402. if (RTCBrowserType.usesUnifiedPlan()) {
  403. answer = self.interop.toPlanB(answer);
  404. self.trace('createAnswerOnSuccess::postTransform (Plan B)',
  405. dumpSDP(answer));
  406. }
  407. if (RTCBrowserType.isChrome())
  408. {
  409. answer = insertRecvOnlySSRC(answer);
  410. self.trace('createAnswerOnSuccess::mungeLocalVideoSSRC', dumpSDP(answer));
  411. }
  412. if (!self.session.room.options.disableSimulcast
  413. && self.simulcast.isSupported()) {
  414. answer = self.simulcast.mungeLocalDescription(answer);
  415. self.trace('createAnswerOnSuccess::postTransform (simulcast)',
  416. dumpSDP(answer));
  417. }
  418. successCallback(answer);
  419. },
  420. function(err) {
  421. self.trace('createAnswerOnFailure', err);
  422. self.eventEmitter.emit(XMPPEvents.CREATE_ANSWER_FAILED, err,
  423. self.peerconnection);
  424. failureCallback(err);
  425. },
  426. constraints
  427. );
  428. };
  429. TraceablePeerConnection.prototype.addIceCandidate
  430. = function (candidate, successCallback, failureCallback) {
  431. //var self = this;
  432. this.trace('addIceCandidate', JSON.stringify(candidate, null, ' '));
  433. this.peerconnection.addIceCandidate(candidate);
  434. /* maybe later
  435. this.peerconnection.addIceCandidate(candidate,
  436. function () {
  437. self.trace('addIceCandidateOnSuccess');
  438. successCallback();
  439. },
  440. function (err) {
  441. self.trace('addIceCandidateOnFailure', err);
  442. failureCallback(err);
  443. }
  444. );
  445. */
  446. };
  447. TraceablePeerConnection.prototype.getStats = function(callback, errback) {
  448. // TODO: Is this the correct way to handle Opera, Temasys?
  449. if (RTCBrowserType.isFirefox()) {
  450. // ignore for now...
  451. if(!errback)
  452. errback = function () {};
  453. this.peerconnection.getStats(null, callback, errback);
  454. } else {
  455. this.peerconnection.getStats(callback);
  456. }
  457. };
  458. module.exports = TraceablePeerConnection;