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

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