Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TraceablePeerConnection.js 16KB

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