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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. var RTC = require('../RTC/RTC');
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var RTCBrowserType = require("../RTC/RTCBrowserType.js");
  4. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  5. var SSRCReplacement = require("./LocalSSRCReplacement");
  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. /**
  134. * Takes a SessionDescription object and returns a "normalized" version.
  135. * Currently it only takes care of ordering the a=ssrc lines.
  136. */
  137. var normalizePlanB = function(desc) {
  138. if (typeof desc !== 'object' || desc === null ||
  139. typeof desc.sdp !== 'string') {
  140. logger.warn('An empty description was passed as an argument.');
  141. return desc;
  142. }
  143. var transform = require('sdp-transform');
  144. var session = transform.parse(desc.sdp);
  145. if (typeof session !== 'undefined' && typeof session.media !== 'undefined' &&
  146. Array.isArray(session.media)) {
  147. session.media.forEach(function (mLine) {
  148. // Chrome appears to be picky about the order in which a=ssrc lines
  149. // are listed in an m-line when rtx is enabled (and thus there are
  150. // a=ssrc-group lines with FID semantics). Specifically if we have
  151. // "a=ssrc-group:FID S1 S2" and the "a=ssrc:S2" lines appear before
  152. // the "a=ssrc:S1" lines, SRD fails.
  153. // So, put SSRC which appear as the first SSRC in an FID ssrc-group
  154. // first.
  155. var firstSsrcs = [];
  156. var newSsrcLines = [];
  157. if (typeof mLine.ssrcGroups !== 'undefined' && Array.isArray(mLine.ssrcGroups)) {
  158. mLine.ssrcGroups.forEach(function (group) {
  159. if (typeof group.semantics !== 'undefined' &&
  160. group.semantics === 'FID') {
  161. if (typeof group.ssrcs !== 'undefined') {
  162. firstSsrcs.push(Number(group.ssrcs.split(' ')[0]));
  163. }
  164. }
  165. });
  166. }
  167. if (typeof mLine.ssrcs !== 'undefined' && Array.isArray(mLine.ssrcs)) {
  168. for (var i = 0; i<mLine.ssrcs.length; i++){
  169. if (typeof mLine.ssrcs[i] === 'object'
  170. && typeof mLine.ssrcs[i].id !== 'undefined'
  171. && $.inArray(mLine.ssrcs[i].id, firstSsrcs) == 0) {
  172. newSsrcLines.push(mLine.ssrcs[i]);
  173. delete mLine.ssrcs[i];
  174. }
  175. }
  176. for (var i = 0; i<mLine.ssrcs.length; i++){
  177. if (typeof mLine.ssrcs[i] !== 'undefined') {
  178. newSsrcLines.push(mLine.ssrcs[i]);
  179. }
  180. }
  181. mLine.ssrcs = newSsrcLines;
  182. }
  183. });
  184. }
  185. var resStr = transform.write(session);
  186. return new RTCSessionDescription({
  187. type: desc.type,
  188. sdp: resStr
  189. });
  190. };
  191. if (TraceablePeerConnection.prototype.__defineGetter__ !== undefined) {
  192. TraceablePeerConnection.prototype.__defineGetter__(
  193. 'signalingState',
  194. function() { return this.peerconnection.signalingState; });
  195. TraceablePeerConnection.prototype.__defineGetter__(
  196. 'iceConnectionState',
  197. function() { return this.peerconnection.iceConnectionState; });
  198. TraceablePeerConnection.prototype.__defineGetter__(
  199. 'localDescription',
  200. function() {
  201. var desc = this.peerconnection.localDescription;
  202. desc = SSRCReplacement.mungeLocalVideoSSRC(desc);
  203. this.trace('getLocalDescription::preTransform', dumpSDP(desc));
  204. // if we're running on FF, transform to Plan B first.
  205. if (RTCBrowserType.usesUnifiedPlan()) {
  206. desc = this.interop.toPlanB(desc);
  207. this.trace('getLocalDescription::postTransform (Plan B)', dumpSDP(desc));
  208. }
  209. return desc;
  210. });
  211. TraceablePeerConnection.prototype.__defineGetter__(
  212. 'remoteDescription',
  213. function() {
  214. var desc = this.peerconnection.remoteDescription;
  215. this.trace('getRemoteDescription::preTransform', dumpSDP(desc));
  216. // if we're running on FF, transform to Plan B first.
  217. if (RTCBrowserType.usesUnifiedPlan()) {
  218. desc = this.interop.toPlanB(desc);
  219. this.trace('getRemoteDescription::postTransform (Plan B)', dumpSDP(desc));
  220. }
  221. return desc;
  222. });
  223. }
  224. TraceablePeerConnection.prototype.addStream = function (stream) {
  225. this.trace('addStream', stream.id);
  226. try
  227. {
  228. this.peerconnection.addStream(stream);
  229. }
  230. catch (e)
  231. {
  232. logger.error(e);
  233. }
  234. };
  235. TraceablePeerConnection.prototype.removeStream = function (stream, stopStreams) {
  236. this.trace('removeStream', stream.id);
  237. if(stopStreams) {
  238. RTC.stopMediaStream(stream);
  239. }
  240. try {
  241. // FF doesn't support this yet.
  242. if (this.peerconnection.removeStream)
  243. this.peerconnection.removeStream(stream);
  244. } catch (e) {
  245. logger.error(e);
  246. }
  247. };
  248. TraceablePeerConnection.prototype.createDataChannel = function (label, opts) {
  249. this.trace('createDataChannel', label, opts);
  250. return this.peerconnection.createDataChannel(label, opts);
  251. };
  252. TraceablePeerConnection.prototype.setLocalDescription
  253. = function (description, successCallback, failureCallback) {
  254. this.trace('setLocalDescription::preTransform', dumpSDP(description));
  255. // if we're running on FF, transform to Plan A first.
  256. if (RTCBrowserType.usesUnifiedPlan()) {
  257. description = this.interop.toUnifiedPlan(description);
  258. this.trace('setLocalDescription::postTransform (Plan A)', dumpSDP(description));
  259. }
  260. var self = this;
  261. this.peerconnection.setLocalDescription(description,
  262. function () {
  263. self.trace('setLocalDescriptionOnSuccess');
  264. successCallback();
  265. },
  266. function (err) {
  267. self.trace('setLocalDescriptionOnFailure', err);
  268. failureCallback(err);
  269. }
  270. );
  271. /*
  272. if (this.statsinterval === null && this.maxstats > 0) {
  273. // start gathering stats
  274. }
  275. */
  276. };
  277. TraceablePeerConnection.prototype.setRemoteDescription
  278. = function (description, successCallback, failureCallback) {
  279. this.trace('setRemoteDescription::preTransform', dumpSDP(description));
  280. // TODO the focus should squeze or explode the remote simulcast
  281. description = this.simulcast.mungeRemoteDescription(description);
  282. this.trace('setRemoteDescription::postTransform (simulcast)', dumpSDP(description));
  283. // if we're running on FF, transform to Plan A first.
  284. if (RTCBrowserType.usesUnifiedPlan()) {
  285. description = this.interop.toUnifiedPlan(description);
  286. this.trace('setRemoteDescription::postTransform (Plan A)', dumpSDP(description));
  287. }
  288. if (RTCBrowserType.usesPlanB()) {
  289. description = normalizePlanB(description);
  290. }
  291. var self = this;
  292. this.peerconnection.setRemoteDescription(description,
  293. function () {
  294. self.trace('setRemoteDescriptionOnSuccess');
  295. successCallback();
  296. },
  297. function (err) {
  298. self.trace('setRemoteDescriptionOnFailure', err);
  299. failureCallback(err);
  300. }
  301. );
  302. /*
  303. if (this.statsinterval === null && this.maxstats > 0) {
  304. // start gathering stats
  305. }
  306. */
  307. };
  308. TraceablePeerConnection.prototype.close = function () {
  309. this.trace('stop');
  310. if (this.statsinterval !== null) {
  311. window.clearInterval(this.statsinterval);
  312. this.statsinterval = null;
  313. }
  314. this.peerconnection.close();
  315. };
  316. TraceablePeerConnection.prototype.createOffer
  317. = function (successCallback, failureCallback, constraints) {
  318. var self = this;
  319. this.trace('createOffer', JSON.stringify(constraints, null, ' '));
  320. this.peerconnection.createOffer(
  321. function (offer) {
  322. self.trace('createOfferOnSuccess::preTransform', dumpSDP(offer));
  323. // NOTE this is not tested because in meet the focus generates the
  324. // offer.
  325. // if we're running on FF, transform to Plan B first.
  326. if (RTCBrowserType.usesUnifiedPlan()) {
  327. offer = self.interop.toPlanB(offer);
  328. self.trace('createOfferOnSuccess::postTransform (Plan B)', dumpSDP(offer));
  329. }
  330. offer = SSRCReplacement.mungeLocalVideoSSRC(offer);
  331. if (self.session.room.options.enableSimulcast && self.simulcast.isSupported()) {
  332. offer = self.simulcast.mungeLocalDescription(offer);
  333. self.trace('createOfferOnSuccess::postTransform (simulcast)', dumpSDP(offer));
  334. }
  335. successCallback(offer);
  336. },
  337. function(err) {
  338. self.trace('createOfferOnFailure', err);
  339. failureCallback(err);
  340. },
  341. constraints
  342. );
  343. };
  344. TraceablePeerConnection.prototype.createAnswer
  345. = function (successCallback, failureCallback, constraints) {
  346. var self = this;
  347. this.trace('createAnswer', JSON.stringify(constraints, null, ' '));
  348. this.peerconnection.createAnswer(
  349. function (answer) {
  350. self.trace('createAnswerOnSuccess::preTransform', dumpSDP(answer));
  351. // if we're running on FF, transform to Plan A first.
  352. if (RTCBrowserType.usesUnifiedPlan()) {
  353. answer = self.interop.toPlanB(answer);
  354. self.trace('createAnswerOnSuccess::postTransform (Plan B)', dumpSDP(answer));
  355. }
  356. // munge local video SSRC
  357. answer = SSRCReplacement.mungeLocalVideoSSRC(answer);
  358. if (self.session.room.options.enableSimulcast && self.simulcast.isSupported()) {
  359. answer = self.simulcast.mungeLocalDescription(answer);
  360. self.trace('createAnswerOnSuccess::postTransform (simulcast)', dumpSDP(answer));
  361. }
  362. successCallback(answer);
  363. },
  364. function(err) {
  365. self.trace('createAnswerOnFailure', err);
  366. failureCallback(err);
  367. },
  368. constraints
  369. );
  370. };
  371. TraceablePeerConnection.prototype.addIceCandidate
  372. = function (candidate, successCallback, failureCallback) {
  373. //var self = this;
  374. this.trace('addIceCandidate', JSON.stringify(candidate, null, ' '));
  375. this.peerconnection.addIceCandidate(candidate);
  376. /* maybe later
  377. this.peerconnection.addIceCandidate(candidate,
  378. function () {
  379. self.trace('addIceCandidateOnSuccess');
  380. successCallback();
  381. },
  382. function (err) {
  383. self.trace('addIceCandidateOnFailure', err);
  384. failureCallback(err);
  385. }
  386. );
  387. */
  388. };
  389. TraceablePeerConnection.prototype.getStats = function(callback, errback) {
  390. // TODO: Is this the correct way to handle Opera, Temasys?
  391. if (RTCBrowserType.isFirefox()) {
  392. // ignore for now...
  393. if(!errback)
  394. errback = function () {};
  395. this.peerconnection.getStats(null, callback, errback);
  396. } else {
  397. this.peerconnection.getStats(callback);
  398. }
  399. };
  400. module.exports = TraceablePeerConnection;