Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TraceablePeerConnection.js 11KB

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