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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. function TraceablePeerConnection(ice_config, constraints) {
  2. var self = this;
  3. var RTCPeerconnection = navigator.mozGetUserMedia ? mozRTCPeerConnection : webkitRTCPeerConnection;
  4. this.peerconnection = new RTCPeerconnection(ice_config, constraints);
  5. this.updateLog = [];
  6. this.stats = {};
  7. this.statsinterval = null;
  8. this.maxstats = 0; // limit to 300 values, i.e. 5 minutes; set to 0 to disable
  9. var Interop = require('sdp-interop').Interop;
  10. this.interop = new Interop();
  11. var Simulcast = require('sdp-simulcast');
  12. this.simulcast = new Simulcast({numOfLayers: 3, explodeRemoteSimulcast: false});
  13. // override as desired
  14. this.trace = function (what, info) {
  15. //console.warn('WTRACE', what, info);
  16. self.updateLog.push({
  17. time: new Date(),
  18. type: what,
  19. value: info || ""
  20. });
  21. };
  22. this.onicecandidate = null;
  23. this.peerconnection.onicecandidate = function (event) {
  24. self.trace('onicecandidate', JSON.stringify(event.candidate, null, ' '));
  25. if (self.onicecandidate !== null) {
  26. self.onicecandidate(event);
  27. }
  28. };
  29. this.onaddstream = null;
  30. this.peerconnection.onaddstream = function (event) {
  31. self.trace('onaddstream', event.stream.id);
  32. if (self.onaddstream !== null) {
  33. self.onaddstream(event);
  34. }
  35. };
  36. this.onremovestream = null;
  37. this.peerconnection.onremovestream = function (event) {
  38. self.trace('onremovestream', event.stream.id);
  39. if (self.onremovestream !== null) {
  40. self.onremovestream(event);
  41. }
  42. };
  43. this.onsignalingstatechange = null;
  44. this.peerconnection.onsignalingstatechange = function (event) {
  45. self.trace('onsignalingstatechange', self.signalingState);
  46. if (self.onsignalingstatechange !== null) {
  47. self.onsignalingstatechange(event);
  48. }
  49. };
  50. this.oniceconnectionstatechange = null;
  51. this.peerconnection.oniceconnectionstatechange = function (event) {
  52. self.trace('oniceconnectionstatechange', self.iceConnectionState);
  53. if (self.oniceconnectionstatechange !== null) {
  54. self.oniceconnectionstatechange(event);
  55. }
  56. };
  57. this.onnegotiationneeded = null;
  58. this.peerconnection.onnegotiationneeded = function (event) {
  59. self.trace('onnegotiationneeded');
  60. if (self.onnegotiationneeded !== null) {
  61. self.onnegotiationneeded(event);
  62. }
  63. };
  64. self.ondatachannel = null;
  65. this.peerconnection.ondatachannel = function (event) {
  66. self.trace('ondatachannel', event);
  67. if (self.ondatachannel !== null) {
  68. self.ondatachannel(event);
  69. }
  70. };
  71. if (!navigator.mozGetUserMedia && this.maxstats) {
  72. this.statsinterval = window.setInterval(function() {
  73. self.peerconnection.getStats(function(stats) {
  74. var results = stats.result();
  75. for (var i = 0; i < results.length; ++i) {
  76. //console.log(results[i].type, results[i].id, results[i].names())
  77. var now = new Date();
  78. results[i].names().forEach(function (name) {
  79. var id = results[i].id + '-' + name;
  80. if (!self.stats[id]) {
  81. self.stats[id] = {
  82. startTime: now,
  83. endTime: now,
  84. values: [],
  85. times: []
  86. };
  87. }
  88. self.stats[id].values.push(results[i].stat(name));
  89. self.stats[id].times.push(now.getTime());
  90. if (self.stats[id].values.length > self.maxstats) {
  91. self.stats[id].values.shift();
  92. self.stats[id].times.shift();
  93. }
  94. self.stats[id].endTime = now;
  95. });
  96. }
  97. });
  98. }, 1000);
  99. }
  100. };
  101. dumpSDP = function(description) {
  102. if (typeof description === 'undefined' || description == null) {
  103. return '';
  104. }
  105. return 'type: ' + description.type + '\r\n' + description.sdp;
  106. };
  107. if (TraceablePeerConnection.prototype.__defineGetter__ !== undefined) {
  108. TraceablePeerConnection.prototype.__defineGetter__('signalingState', function() { return this.peerconnection.signalingState; });
  109. TraceablePeerConnection.prototype.__defineGetter__('iceConnectionState', function() { return this.peerconnection.iceConnectionState; });
  110. TraceablePeerConnection.prototype.__defineGetter__('localDescription', function() {
  111. var desc = this.peerconnection.localDescription;
  112. this.trace('getLocalDescription::preTransform', dumpSDP(desc));
  113. // if we're running on FF, transform to Plan B first.
  114. if (navigator.mozGetUserMedia) {
  115. desc = this.interop.toPlanB(desc);
  116. this.trace('getLocalDescription::postTransform (Plan B)', dumpSDP(desc));
  117. }
  118. return desc;
  119. });
  120. TraceablePeerConnection.prototype.__defineGetter__('remoteDescription', function() {
  121. var desc = this.peerconnection.remoteDescription;
  122. this.trace('getRemoteDescription::preTransform', dumpSDP(desc));
  123. // if we're running on FF, transform to Plan B first.
  124. if (navigator.mozGetUserMedia) {
  125. desc = this.interop.toPlanB(desc);
  126. this.trace('getRemoteDescription::postTransform (Plan B)', dumpSDP(desc));
  127. }
  128. return desc;
  129. });
  130. }
  131. TraceablePeerConnection.prototype.addStream = function (stream) {
  132. this.trace('addStream', stream.id);
  133. try
  134. {
  135. this.peerconnection.addStream(stream);
  136. }
  137. catch (e)
  138. {
  139. console.error(e);
  140. return;
  141. }
  142. };
  143. TraceablePeerConnection.prototype.removeStream = function (stream, stopStreams) {
  144. this.trace('removeStream', stream.id);
  145. if(stopStreams) {
  146. stream.getAudioTracks().forEach(function (track) {
  147. track.stop();
  148. });
  149. stream.getVideoTracks().forEach(function (track) {
  150. track.stop();
  151. });
  152. }
  153. try {
  154. // FF doesn't support this yet.
  155. this.peerconnection.removeStream(stream);
  156. } catch (e) {
  157. console.error(e);
  158. }
  159. };
  160. TraceablePeerConnection.prototype.createDataChannel = function (label, opts) {
  161. this.trace('createDataChannel', label, opts);
  162. return this.peerconnection.createDataChannel(label, opts);
  163. };
  164. TraceablePeerConnection.prototype.setLocalDescription = function (description, successCallback, failureCallback) {
  165. this.trace('setLocalDescription::preTransform', dumpSDP(description));
  166. // if we're running on FF, transform to Plan A first.
  167. if (navigator.mozGetUserMedia) {
  168. description = this.interop.toUnifiedPlan(description);
  169. this.trace('setLocalDescription::postTransform (Plan A)', dumpSDP(description));
  170. }
  171. var self = this;
  172. this.peerconnection.setLocalDescription(description,
  173. function () {
  174. self.trace('setLocalDescriptionOnSuccess');
  175. successCallback();
  176. },
  177. function (err) {
  178. self.trace('setLocalDescriptionOnFailure', err);
  179. failureCallback(err);
  180. }
  181. );
  182. /*
  183. if (this.statsinterval === null && this.maxstats > 0) {
  184. // start gathering stats
  185. }
  186. */
  187. };
  188. TraceablePeerConnection.prototype.setRemoteDescription = function (description, successCallback, failureCallback) {
  189. this.trace('setRemoteDescription::preTransform', dumpSDP(description));
  190. // TODO the focus should squeze or explode the remote simulcast
  191. description = this.simulcast.mungeRemoteDescription(description);
  192. this.trace('setRemoteDescription::postTransform (simulcast)', dumpSDP(description));
  193. // if we're running on FF, transform to Plan A first.
  194. if (navigator.mozGetUserMedia) {
  195. description = this.interop.toUnifiedPlan(description);
  196. this.trace('setRemoteDescription::postTransform (Plan A)', dumpSDP(description));
  197. }
  198. var self = this;
  199. this.peerconnection.setRemoteDescription(description,
  200. function () {
  201. self.trace('setRemoteDescriptionOnSuccess');
  202. successCallback();
  203. },
  204. function (err) {
  205. self.trace('setRemoteDescriptionOnFailure', err);
  206. failureCallback(err);
  207. }
  208. );
  209. /*
  210. if (this.statsinterval === null && this.maxstats > 0) {
  211. // start gathering stats
  212. }
  213. */
  214. };
  215. TraceablePeerConnection.prototype.close = function () {
  216. this.trace('stop');
  217. if (this.statsinterval !== null) {
  218. window.clearInterval(this.statsinterval);
  219. this.statsinterval = null;
  220. }
  221. this.peerconnection.close();
  222. };
  223. TraceablePeerConnection.prototype.createOffer = function (successCallback, failureCallback, constraints) {
  224. var self = this;
  225. this.trace('createOffer', JSON.stringify(constraints, null, ' '));
  226. this.peerconnection.createOffer(
  227. function (offer) {
  228. self.trace('createOfferOnSuccess::preTransform', dumpSDP(offer));
  229. // if we're running on FF, transform to Plan B first.
  230. // NOTE this is not tested because in meet the focus generates the
  231. // offer.
  232. if (navigator.mozGetUserMedia) {
  233. offer = self.interop.toPlanB(offer);
  234. self.trace('createOfferOnSuccess::postTransform (Plan B)', dumpSDP(offer));
  235. }
  236. if (config.enableSimulcast && self.simulcast.isSupported()) {
  237. offer = self.simulcast.mungeLocalDescription(offer);
  238. self.trace('createOfferOnSuccess::postTransform (simulcast)', dumpSDP(offer));
  239. }
  240. successCallback(offer);
  241. },
  242. function(err) {
  243. self.trace('createOfferOnFailure', err);
  244. failureCallback(err);
  245. },
  246. constraints
  247. );
  248. };
  249. TraceablePeerConnection.prototype.createAnswer = function (successCallback, failureCallback, constraints) {
  250. var self = this;
  251. this.trace('createAnswer', JSON.stringify(constraints, null, ' '));
  252. this.peerconnection.createAnswer(
  253. function (answer) {
  254. self.trace('createAnswerOnSuccess::preTransfom', dumpSDP(answer));
  255. // if we're running on FF, transform to Plan A first.
  256. if (navigator.mozGetUserMedia) {
  257. answer = self.interop.toPlanB(answer);
  258. self.trace('createAnswerOnSuccess::postTransfom (Plan B)', dumpSDP(answer));
  259. }
  260. if (config.enableSimulcast && self.simulcast.isSupported()) {
  261. answer = self.simulcast.mungeLocalDescription(answer);
  262. self.trace('createAnswerOnSuccess::postTransfom (simulcast)', dumpSDP(answer));
  263. }
  264. successCallback(answer);
  265. },
  266. function(err) {
  267. self.trace('createAnswerOnFailure', err);
  268. failureCallback(err);
  269. },
  270. constraints
  271. );
  272. };
  273. TraceablePeerConnection.prototype.addIceCandidate = function (candidate, successCallback, failureCallback) {
  274. var self = this;
  275. this.trace('addIceCandidate', JSON.stringify(candidate, null, ' '));
  276. this.peerconnection.addIceCandidate(candidate);
  277. /* maybe later
  278. this.peerconnection.addIceCandidate(candidate,
  279. function () {
  280. self.trace('addIceCandidateOnSuccess');
  281. successCallback();
  282. },
  283. function (err) {
  284. self.trace('addIceCandidateOnFailure', err);
  285. failureCallback(err);
  286. }
  287. );
  288. */
  289. };
  290. TraceablePeerConnection.prototype.getStats = function(callback, errback) {
  291. if (navigator.mozGetUserMedia) {
  292. // ignore for now...
  293. if(!errback)
  294. errback = function () {
  295. }
  296. this.peerconnection.getStats(null,callback,errback);
  297. } else {
  298. this.peerconnection.getStats(callback);
  299. }
  300. };
  301. module.exports = TraceablePeerConnection;