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

TraceablePeerConnection.js 13KB

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