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

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