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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /* global $, config, mozRTCPeerConnection, RTCPeerConnection,
  2. webkitRTCPeerConnection, RTCSessionDescription */
  3. /* jshint -W101 */
  4. var RTC = require('../RTC/RTC');
  5. var RTCBrowserType = require("../RTC/RTCBrowserType.js");
  6. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  7. var SSRCReplacement = require("./LocalSSRCReplacement");
  8. function TraceablePeerConnection(ice_config, constraints, session) {
  9. var self = this;
  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. /*console.warn('WTRACE', what, info);
  30. if (info && RTCBrowserType.isIExplorer()) {
  31. if (info.length > 1024) {
  32. console.warn('WTRACE', what, info.substr(1024));
  33. }
  34. if (info.length > 2048) {
  35. console.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. console.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. if (TraceablePeerConnection.prototype.__defineGetter__ !== undefined) {
  194. TraceablePeerConnection.prototype.__defineGetter__(
  195. 'signalingState',
  196. function() { return this.peerconnection.signalingState; });
  197. TraceablePeerConnection.prototype.__defineGetter__(
  198. 'iceConnectionState',
  199. function() { return this.peerconnection.iceConnectionState; });
  200. TraceablePeerConnection.prototype.__defineGetter__(
  201. 'localDescription',
  202. function() {
  203. var desc = this.peerconnection.localDescription;
  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. TraceablePeerConnection.prototype.__defineGetter__(
  214. 'remoteDescription',
  215. function() {
  216. var desc = this.peerconnection.remoteDescription;
  217. this.trace('getRemoteDescription::preTransform', dumpSDP(desc));
  218. // if we're running on FF, transform to Plan B first.
  219. if (RTCBrowserType.usesUnifiedPlan()) {
  220. desc = this.interop.toPlanB(desc);
  221. this.trace('getRemoteDescription::postTransform (Plan B)', dumpSDP(desc));
  222. }
  223. return desc;
  224. });
  225. }
  226. TraceablePeerConnection.prototype.addStream = function (stream) {
  227. this.trace('addStream', stream.id);
  228. try
  229. {
  230. this.peerconnection.addStream(stream);
  231. }
  232. catch (e)
  233. {
  234. console.error(e);
  235. }
  236. };
  237. TraceablePeerConnection.prototype.removeStream = function (stream, stopStreams) {
  238. this.trace('removeStream', stream.id);
  239. if(stopStreams) {
  240. stream.getAudioTracks().forEach(function (track) {
  241. // stop() not supported with IE
  242. if (track.stop) {
  243. track.stop();
  244. }
  245. });
  246. stream.getVideoTracks().forEach(function (track) {
  247. // stop() not supported with IE
  248. if (track.stop) {
  249. track.stop();
  250. }
  251. });
  252. if (stream.stop) {
  253. stream.stop();
  254. }
  255. }
  256. try {
  257. // FF doesn't support this yet.
  258. if (this.peerconnection.removeStream)
  259. this.peerconnection.removeStream(stream);
  260. } catch (e) {
  261. console.error(e);
  262. }
  263. };
  264. TraceablePeerConnection.prototype.createDataChannel = function (label, opts) {
  265. this.trace('createDataChannel', label, opts);
  266. return this.peerconnection.createDataChannel(label, opts);
  267. };
  268. TraceablePeerConnection.prototype.setLocalDescription
  269. = function (description, successCallback, failureCallback) {
  270. this.trace('setLocalDescription::preTransform', dumpSDP(description));
  271. // if we're running on FF, transform to Plan A first.
  272. if (RTCBrowserType.usesUnifiedPlan()) {
  273. description = this.interop.toUnifiedPlan(description);
  274. this.trace('setLocalDescription::postTransform (Plan A)', dumpSDP(description));
  275. }
  276. var self = this;
  277. this.peerconnection.setLocalDescription(description,
  278. function () {
  279. self.trace('setLocalDescriptionOnSuccess');
  280. successCallback();
  281. },
  282. function (err) {
  283. self.trace('setLocalDescriptionOnFailure', err);
  284. failureCallback(err);
  285. }
  286. );
  287. /*
  288. if (this.statsinterval === null && this.maxstats > 0) {
  289. // start gathering stats
  290. }
  291. */
  292. };
  293. TraceablePeerConnection.prototype.setRemoteDescription
  294. = function (description, successCallback, failureCallback) {
  295. this.trace('setRemoteDescription::preTransform', dumpSDP(description));
  296. // TODO the focus should squeze or explode the remote simulcast
  297. description = this.simulcast.mungeRemoteDescription(description);
  298. this.trace('setRemoteDescription::postTransform (simulcast)', dumpSDP(description));
  299. // if we're running on FF, transform to Plan A first.
  300. if (RTCBrowserType.usesUnifiedPlan()) {
  301. description = this.interop.toUnifiedPlan(description);
  302. this.trace('setRemoteDescription::postTransform (Plan A)', dumpSDP(description));
  303. }
  304. if (RTCBrowserType.usesPlanB()) {
  305. description = normalizePlanB(description);
  306. }
  307. var self = this;
  308. this.peerconnection.setRemoteDescription(description,
  309. function () {
  310. self.trace('setRemoteDescriptionOnSuccess');
  311. successCallback();
  312. },
  313. function (err) {
  314. self.trace('setRemoteDescriptionOnFailure', err);
  315. failureCallback(err);
  316. }
  317. );
  318. /*
  319. if (this.statsinterval === null && this.maxstats > 0) {
  320. // start gathering stats
  321. }
  322. */
  323. };
  324. TraceablePeerConnection.prototype.close = function () {
  325. this.trace('stop');
  326. if (this.statsinterval !== null) {
  327. window.clearInterval(this.statsinterval);
  328. this.statsinterval = null;
  329. }
  330. this.peerconnection.close();
  331. };
  332. TraceablePeerConnection.prototype.createOffer
  333. = function (successCallback, failureCallback, constraints) {
  334. var self = this;
  335. this.trace('createOffer', JSON.stringify(constraints, null, ' '));
  336. this.peerconnection.createOffer(
  337. function (offer) {
  338. self.trace('createOfferOnSuccess::preTransform', dumpSDP(offer));
  339. // NOTE this is not tested because in meet the focus generates the
  340. // offer.
  341. // if we're running on FF, transform to Plan B first.
  342. if (RTCBrowserType.usesUnifiedPlan()) {
  343. offer = self.interop.toPlanB(offer);
  344. self.trace('createOfferOnSuccess::postTransform (Plan B)', dumpSDP(offer));
  345. }
  346. offer = SSRCReplacement.mungeLocalVideoSSRC(offer);
  347. if (config.enableSimulcast && self.simulcast.isSupported()) {
  348. offer = self.simulcast.mungeLocalDescription(offer);
  349. self.trace('createOfferOnSuccess::postTransform (simulcast)', dumpSDP(offer));
  350. }
  351. successCallback(offer);
  352. },
  353. function(err) {
  354. self.trace('createOfferOnFailure', err);
  355. failureCallback(err);
  356. },
  357. constraints
  358. );
  359. };
  360. TraceablePeerConnection.prototype.createAnswer
  361. = function (successCallback, failureCallback, constraints) {
  362. var self = this;
  363. this.trace('createAnswer', JSON.stringify(constraints, null, ' '));
  364. this.peerconnection.createAnswer(
  365. function (answer) {
  366. self.trace('createAnswerOnSuccess::preTransform', dumpSDP(answer));
  367. // if we're running on FF, transform to Plan A first.
  368. if (RTCBrowserType.usesUnifiedPlan()) {
  369. answer = self.interop.toPlanB(answer);
  370. self.trace('createAnswerOnSuccess::postTransform (Plan B)', dumpSDP(answer));
  371. }
  372. // munge local video SSRC
  373. answer = SSRCReplacement.mungeLocalVideoSSRC(answer);
  374. if (config.enableSimulcast && self.simulcast.isSupported()) {
  375. answer = self.simulcast.mungeLocalDescription(answer);
  376. self.trace('createAnswerOnSuccess::postTransform (simulcast)', dumpSDP(answer));
  377. }
  378. successCallback(answer);
  379. },
  380. function(err) {
  381. self.trace('createAnswerOnFailure', err);
  382. failureCallback(err);
  383. },
  384. constraints
  385. );
  386. };
  387. TraceablePeerConnection.prototype.addIceCandidate
  388. = function (candidate, successCallback, failureCallback) {
  389. //var self = this;
  390. this.trace('addIceCandidate', JSON.stringify(candidate, null, ' '));
  391. this.peerconnection.addIceCandidate(candidate);
  392. /* maybe later
  393. this.peerconnection.addIceCandidate(candidate,
  394. function () {
  395. self.trace('addIceCandidateOnSuccess');
  396. successCallback();
  397. },
  398. function (err) {
  399. self.trace('addIceCandidateOnFailure', err);
  400. failureCallback(err);
  401. }
  402. );
  403. */
  404. };
  405. TraceablePeerConnection.prototype.getStats = function(callback, errback) {
  406. // TODO: Is this the correct way to handle Opera, Temasys?
  407. if (RTCBrowserType.isFirefox()) {
  408. // ignore for now...
  409. if(!errback)
  410. errback = function () {};
  411. this.peerconnection.getStats(null, callback, errback);
  412. } else {
  413. this.peerconnection.getStats(callback);
  414. }
  415. };
  416. module.exports = TraceablePeerConnection;