您最多选择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. if (!navigator.mozGetUserMedia && this.maxstats) {
  92. this.statsinterval = window.setInterval(function() {
  93. self.peerconnection.getStats(function(stats) {
  94. var results = stats.result();
  95. for (var i = 0; i < results.length; ++i) {
  96. //console.log(results[i].type, results[i].id, results[i].names())
  97. var now = new Date();
  98. results[i].names().forEach(function (name) {
  99. var id = results[i].id + '-' + name;
  100. if (!self.stats[id]) {
  101. self.stats[id] = {
  102. startTime: now,
  103. endTime: now,
  104. values: [],
  105. times: []
  106. };
  107. }
  108. self.stats[id].values.push(results[i].stat(name));
  109. self.stats[id].times.push(now.getTime());
  110. if (self.stats[id].values.length > self.maxstats) {
  111. self.stats[id].values.shift();
  112. self.stats[id].times.shift();
  113. }
  114. self.stats[id].endTime = now;
  115. });
  116. }
  117. });
  118. }, 1000);
  119. }
  120. };
  121. dumpSDP = function(description) {
  122. if (typeof description === 'undefined' || description == null) {
  123. return '';
  124. }
  125. return 'type: ' + description.type + '\r\n' + description.sdp;
  126. };
  127. if (TraceablePeerConnection.prototype.__defineGetter__ !== undefined) {
  128. TraceablePeerConnection.prototype.__defineGetter__('signalingState', function() { return this.peerconnection.signalingState; });
  129. TraceablePeerConnection.prototype.__defineGetter__('iceConnectionState', function() { return this.peerconnection.iceConnectionState; });
  130. TraceablePeerConnection.prototype.__defineGetter__('localDescription', function() {
  131. var desc = this.peerconnection.localDescription;
  132. this.trace('getLocalDescription::preTransform', dumpSDP(desc));
  133. // if we're running on FF, transform to Plan B first.
  134. if (navigator.mozGetUserMedia) {
  135. desc = this.interop.toPlanB(desc);
  136. this.trace('getLocalDescription::postTransform (Plan B)', dumpSDP(desc));
  137. }
  138. return desc;
  139. });
  140. TraceablePeerConnection.prototype.__defineGetter__('remoteDescription', function() {
  141. var desc = this.peerconnection.remoteDescription;
  142. this.trace('getRemoteDescription::preTransform', dumpSDP(desc));
  143. // if we're running on FF, transform to Plan B first.
  144. if (navigator.mozGetUserMedia) {
  145. desc = this.interop.toPlanB(desc);
  146. this.trace('getRemoteDescription::postTransform (Plan B)', dumpSDP(desc));
  147. }
  148. return desc;
  149. });
  150. }
  151. TraceablePeerConnection.prototype.addStream = function (stream) {
  152. this.trace('addStream', stream.id);
  153. try
  154. {
  155. this.peerconnection.addStream(stream);
  156. }
  157. catch (e)
  158. {
  159. console.error(e);
  160. return;
  161. }
  162. };
  163. TraceablePeerConnection.prototype.removeStream = function (stream, stopStreams) {
  164. this.trace('removeStream', stream.id);
  165. if(stopStreams) {
  166. stream.getAudioTracks().forEach(function (track) {
  167. // stop() not supported with IE
  168. if (track.stop) {
  169. track.stop();
  170. }
  171. });
  172. stream.getVideoTracks().forEach(function (track) {
  173. // stop() not supported with IE
  174. if (track.stop) {
  175. track.stop();
  176. }
  177. });
  178. if (stream.stop) {
  179. stream.stop();
  180. }
  181. }
  182. try {
  183. // FF doesn't support this yet.
  184. if (this.peerconnection.removeStream)
  185. this.peerconnection.removeStream(stream);
  186. } catch (e) {
  187. console.error(e);
  188. }
  189. };
  190. TraceablePeerConnection.prototype.createDataChannel = function (label, opts) {
  191. this.trace('createDataChannel', label, opts);
  192. return this.peerconnection.createDataChannel(label, opts);
  193. };
  194. TraceablePeerConnection.prototype.setLocalDescription = function (description, successCallback, failureCallback) {
  195. this.trace('setLocalDescription::preTransform', 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. this.trace('setLocalDescription::postTransform (Plan A)', dumpSDP(description));
  200. }
  201. var self = this;
  202. this.peerconnection.setLocalDescription(description,
  203. function () {
  204. self.trace('setLocalDescriptionOnSuccess');
  205. successCallback();
  206. },
  207. function (err) {
  208. self.trace('setLocalDescriptionOnFailure', err);
  209. failureCallback(err);
  210. }
  211. );
  212. /*
  213. if (this.statsinterval === null && this.maxstats > 0) {
  214. // start gathering stats
  215. }
  216. */
  217. };
  218. TraceablePeerConnection.prototype.setRemoteDescription = function (description, successCallback, failureCallback) {
  219. this.trace('setRemoteDescription::preTransform', dumpSDP(description));
  220. // TODO the focus should squeze or explode the remote simulcast
  221. description = this.simulcast.mungeRemoteDescription(description);
  222. this.trace('setRemoteDescription::postTransform (simulcast)', dumpSDP(description));
  223. // if we're running on FF, transform to Plan A first.
  224. if (navigator.mozGetUserMedia) {
  225. description = this.interop.toUnifiedPlan(description);
  226. this.trace('setRemoteDescription::postTransform (Plan A)', dumpSDP(description));
  227. }
  228. var self = this;
  229. this.peerconnection.setRemoteDescription(description,
  230. function () {
  231. self.trace('setRemoteDescriptionOnSuccess');
  232. successCallback();
  233. },
  234. function (err) {
  235. self.trace('setRemoteDescriptionOnFailure', err);
  236. failureCallback(err);
  237. }
  238. );
  239. /*
  240. if (this.statsinterval === null && this.maxstats > 0) {
  241. // start gathering stats
  242. }
  243. */
  244. };
  245. TraceablePeerConnection.prototype.close = function () {
  246. this.trace('stop');
  247. if (this.statsinterval !== null) {
  248. window.clearInterval(this.statsinterval);
  249. this.statsinterval = null;
  250. }
  251. this.peerconnection.close();
  252. };
  253. TraceablePeerConnection.prototype.createOffer = function (successCallback, failureCallback, constraints) {
  254. var self = this;
  255. this.trace('createOffer', JSON.stringify(constraints, null, ' '));
  256. this.peerconnection.createOffer(
  257. function (offer) {
  258. self.trace('createOfferOnSuccess::preTransform', dumpSDP(offer));
  259. // if we're running on FF, transform to Plan B first.
  260. // NOTE this is not tested because in meet the focus generates the
  261. // offer.
  262. if (navigator.mozGetUserMedia) {
  263. offer = self.interop.toPlanB(offer);
  264. self.trace('createOfferOnSuccess::postTransform (Plan B)', dumpSDP(offer));
  265. }
  266. if (config.enableSimulcast && self.simulcast.isSupported()) {
  267. offer = self.simulcast.mungeLocalDescription(offer);
  268. self.trace('createOfferOnSuccess::postTransform (simulcast)', dumpSDP(offer));
  269. }
  270. successCallback(offer);
  271. },
  272. function(err) {
  273. self.trace('createOfferOnFailure', err);
  274. failureCallback(err);
  275. },
  276. constraints
  277. );
  278. };
  279. TraceablePeerConnection.prototype.createAnswer = function (successCallback, failureCallback, constraints) {
  280. var self = this;
  281. this.trace('createAnswer', JSON.stringify(constraints, null, ' '));
  282. this.peerconnection.createAnswer(
  283. function (answer) {
  284. self.trace('createAnswerOnSuccess::preTransfom', dumpSDP(answer));
  285. // if we're running on FF, transform to Plan A first.
  286. if (navigator.mozGetUserMedia) {
  287. answer = self.interop.toPlanB(answer);
  288. self.trace('createAnswerOnSuccess::postTransfom (Plan B)', dumpSDP(answer));
  289. }
  290. if (config.enableSimulcast && self.simulcast.isSupported()) {
  291. answer = self.simulcast.mungeLocalDescription(answer);
  292. self.trace('createAnswerOnSuccess::postTransfom (simulcast)', dumpSDP(answer));
  293. }
  294. successCallback(answer);
  295. },
  296. function(err) {
  297. self.trace('createAnswerOnFailure', err);
  298. failureCallback(err);
  299. },
  300. constraints
  301. );
  302. };
  303. TraceablePeerConnection.prototype.addIceCandidate = function (candidate, successCallback, failureCallback) {
  304. var self = this;
  305. this.trace('addIceCandidate', JSON.stringify(candidate, null, ' '));
  306. this.peerconnection.addIceCandidate(candidate);
  307. /* maybe later
  308. this.peerconnection.addIceCandidate(candidate,
  309. function () {
  310. self.trace('addIceCandidateOnSuccess');
  311. successCallback();
  312. },
  313. function (err) {
  314. self.trace('addIceCandidateOnFailure', err);
  315. failureCallback(err);
  316. }
  317. );
  318. */
  319. };
  320. TraceablePeerConnection.prototype.getStats = function(callback, errback) {
  321. if (navigator.mozGetUserMedia) {
  322. // ignore for now...
  323. if(!errback)
  324. errback = function () {
  325. }
  326. this.peerconnection.getStats(null,callback,errback);
  327. } else {
  328. this.peerconnection.getStats(callback);
  329. }
  330. };
  331. module.exports = TraceablePeerConnection;