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

statistics.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* global require */
  2. var LocalStats = require("./LocalStatsCollector.js");
  3. var RTPStats = require("./RTPStatsCollector.js");
  4. var EventEmitter = require("events");
  5. var StatisticsEvents = require("../../service/statistics/Events");
  6. var CallStats = require("./CallStats");
  7. var ScriptUtil = require('../util/ScriptUtil');
  8. // Since callstats.io is a third party, we cannot guarantee the quality of
  9. // their service. More specifically, their server may take noticeably long
  10. // time to respond. Consequently, it is in our best interest (in the sense
  11. // that the intergration of callstats.io is pretty important to us but not
  12. // enough to allow it to prevent people from joining a conference) to (1)
  13. // start downloading their API as soon as possible and (2) do the
  14. // downloading asynchronously.
  15. function loadCallStatsAPI() {
  16. ScriptUtil.loadScript(
  17. 'https://api.callstats.io/static/callstats.min.js',
  18. /* async */ true,
  19. /* prepend */ true);
  20. // FIXME At the time of this writing, we hope that the callstats.io API will
  21. // have loaded by the time we needed it (i.e. CallStats.init is invoked).
  22. }
  23. /**
  24. * Log stats via the focus once every this many milliseconds.
  25. */
  26. var LOG_INTERVAL = 60000;
  27. var eventEmitter = new EventEmitter();
  28. function Statistics(xmpp, options) {
  29. this.rtpStats = null;
  30. this.eventEmitter = new EventEmitter();
  31. this.xmpp = xmpp;
  32. this.options = options || {};
  33. this.callStatsIntegrationEnabled
  34. = this.options.callStatsID && this.options.callStatsSecret
  35. // Even though AppID and AppSecret may be specified, the integration of
  36. // callstats.io may be disabled because of globally-disallowed requests
  37. // to any third parties.
  38. && (this.options.disableThirdPartyRequests !== true);
  39. if(this.callStatsIntegrationEnabled)
  40. loadCallStatsAPI();
  41. this.callStats = null;
  42. /**
  43. * Send the stats already saved in rtpStats to be logged via
  44. * the focus.
  45. */
  46. this.logStatsIntervalId = null;
  47. }
  48. Statistics.audioLevelsEnabled = false;
  49. Statistics.prototype.startRemoteStats = function (peerconnection) {
  50. if(!Statistics.audioLevelsEnabled)
  51. return;
  52. this.stopRemoteStats();
  53. this.rtpStats = new RTPStats(peerconnection, 200, 2000, this.eventEmitter);
  54. this.rtpStats.start();
  55. this.logStatsIntervalId = setInterval(function () {
  56. var stats = this.rtpStats.getCollectedStats();
  57. if (this.xmpp.sendLogs(stats)) {
  58. this.rtpStats.clearCollectedStats();
  59. }
  60. }.bind(this), LOG_INTERVAL);
  61. };
  62. Statistics.localStats = [];
  63. Statistics.startLocalStats = function (stream, callback) {
  64. if(!Statistics.audioLevelsEnabled)
  65. return;
  66. var localStats = new LocalStats(stream, 200, callback);
  67. this.localStats.push(localStats);
  68. localStats.start();
  69. };
  70. Statistics.prototype.addAudioLevelListener = function(listener)
  71. {
  72. if(!Statistics.audioLevelsEnabled)
  73. return;
  74. this.eventEmitter.on(StatisticsEvents.AUDIO_LEVEL, listener);
  75. };
  76. Statistics.prototype.removeAudioLevelListener = function(listener)
  77. {
  78. if(!Statistics.audioLevelsEnabled)
  79. return;
  80. this.eventEmitter.removeListener(StatisticsEvents.AUDIO_LEVEL, listener);
  81. };
  82. Statistics.prototype.addConnectionStatsListener = function (listener) {
  83. this.eventEmitter.on(StatisticsEvents.CONNECTION_STATS, listener);
  84. };
  85. Statistics.prototype.removeConnectionStatsListener = function (listener) {
  86. this.eventEmitter.removeListener(StatisticsEvents.CONNECTION_STATS, listener);
  87. };
  88. Statistics.prototype.dispose = function () {
  89. if(Statistics.audioLevelsEnabled) {
  90. Statistics.stopAllLocalStats();
  91. this.stopRemoteStats();
  92. if(this.eventEmitter)
  93. this.eventEmitter.removeAllListeners();
  94. if(eventEmitter)
  95. eventEmitter.removeAllListeners();
  96. }
  97. };
  98. Statistics.stopAllLocalStats = function () {
  99. if(!Statistics.audioLevelsEnabled)
  100. return;
  101. for(var i = 0; i < this.localStats.length; i++)
  102. this.localStats[i].stop();
  103. this.localStats = [];
  104. };
  105. Statistics.stopLocalStats = function (stream) {
  106. if(!Statistics.audioLevelsEnabled)
  107. return;
  108. for(var i = 0; i < Statistics.localStats.length; i++)
  109. if(Statistics.localStats[i].stream === stream){
  110. var localStats = Statistics.localStats.splice(i, 1);
  111. localStats[0].stop();
  112. break;
  113. }
  114. };
  115. Statistics.prototype.stopRemoteStats = function () {
  116. if (!Statistics.audioLevelsEnabled || !this.rtpStats) {
  117. return;
  118. }
  119. this.rtpStats.stop();
  120. this.rtpStats = null;
  121. if (this.logStatsIntervalId) {
  122. clearInterval(this.logStatsIntervalId);
  123. this.logStatsIntervalId = null;
  124. }
  125. };
  126. //CALSTATS METHODS
  127. /**
  128. * Initializes the callstats.io API.
  129. * @param peerConnection {JingleSessionPC} the session object
  130. * @param Settings {Settings} the settings instance. Declared in
  131. * /modules/settings/Settings.js
  132. */
  133. Statistics.prototype.startCallStats = function (session, settings) {
  134. if(this.callStatsIntegrationEnabled && !this.callstats) {
  135. this.callstats = new CallStats(session, settings, this.options);
  136. }
  137. };
  138. /**
  139. * Returns true if the callstats integration is enabled, otherwise returns
  140. * false.
  141. *
  142. * @returns true if the callstats integration is enabled, otherwise returns
  143. * false.
  144. */
  145. Statistics.prototype.isCallstatsEnabled = function () {
  146. return this.callStatsIntegrationEnabled;
  147. };
  148. /**
  149. * Notifies CallStats for ice connection failed
  150. * @param {RTCPeerConnection} pc connection on which failure occured.
  151. */
  152. Statistics.prototype.sendIceConnectionFailedEvent = function (pc) {
  153. if(this.callStatsIntegrationEnabled && this.callstats)
  154. this.callstats.sendIceConnectionFailedEvent(pc, this.callstats);
  155. };
  156. /**
  157. * Notifies CallStats for mute events
  158. * @param mute {boolean} true for muted and false for not muted
  159. * @param type {String} "audio"/"video"
  160. */
  161. Statistics.prototype.sendMuteEvent = function (muted, type) {
  162. if(this.callStatsIntegrationEnabled)
  163. CallStats.sendMuteEvent(muted, type, this.callstats);
  164. };
  165. /**
  166. * Notifies CallStats for screen sharing events
  167. * @param start {boolean} true for starting screen sharing and
  168. * false for not stopping
  169. */
  170. Statistics.prototype.sendScreenSharingEvent = function (start) {
  171. if(this.callStatsIntegrationEnabled)
  172. CallStats.sendScreenSharingEvent(start, this.callstats);
  173. };
  174. /**
  175. * Notifies the statistics module that we are now the dominant speaker of the
  176. * conference.
  177. */
  178. Statistics.prototype.sendDominantSpeakerEvent = function () {
  179. if(this.callStatsIntegrationEnabled)
  180. CallStats.sendDominantSpeakerEvent(this.callstats);
  181. };
  182. /**
  183. * Lets the underlying statistics module know where is given SSRC rendered by
  184. * providing renderer tag ID.
  185. * @param ssrc {number} the SSRC of the stream
  186. * @param isLocal {boolean} <tt>true<tt> if this stream is local or
  187. * <tt>false</tt> otherwise.
  188. * @param usageLabel {string} meaningful usage label of this stream like
  189. * 'microphone', 'camera' or 'screen'.
  190. * @param containerId {string} the id of media 'audio' or 'video' tag which
  191. * renders the stream.
  192. */
  193. Statistics.prototype.associateStreamWithVideoTag =
  194. function (ssrc, isLocal, usageLabel, containerId) {
  195. if(this.callStatsIntegrationEnabled && this.callstats) {
  196. this.callstats.associateStreamWithVideoTag(
  197. ssrc, isLocal, usageLabel, containerId);
  198. }
  199. };
  200. /**
  201. * Notifies CallStats that getUserMedia failed.
  202. *
  203. * @param {Error} e error to send
  204. */
  205. Statistics.prototype.sendGetUserMediaFailed = function (e) {
  206. if(this.callStatsIntegrationEnabled)
  207. CallStats.sendGetUserMediaFailed(e, this.callstats);
  208. };
  209. /**
  210. * Notifies CallStats that getUserMedia failed.
  211. *
  212. * @param {Error} e error to send
  213. */
  214. Statistics.sendGetUserMediaFailed = function (e) {
  215. CallStats.sendGetUserMediaFailed(e, null);
  216. };
  217. /**
  218. * Notifies CallStats that peer connection failed to create offer.
  219. *
  220. * @param {Error} e error to send
  221. * @param {RTCPeerConnection} pc connection on which failure occured.
  222. */
  223. Statistics.prototype.sendCreateOfferFailed = function (e, pc) {
  224. if(this.callStatsIntegrationEnabled)
  225. CallStats.sendCreateOfferFailed(e, pc, this.callstats);
  226. };
  227. /**
  228. * Notifies CallStats that peer connection failed to create answer.
  229. *
  230. * @param {Error} e error to send
  231. * @param {RTCPeerConnection} pc connection on which failure occured.
  232. */
  233. Statistics.prototype.sendCreateAnswerFailed = function (e, pc) {
  234. if(this.callStatsIntegrationEnabled)
  235. CallStats.sendCreateAnswerFailed(e, pc, this.callstats);
  236. };
  237. /**
  238. * Notifies CallStats that peer connection failed to set local description.
  239. *
  240. * @param {Error} e error to send
  241. * @param {RTCPeerConnection} pc connection on which failure occured.
  242. */
  243. Statistics.prototype.sendSetLocalDescFailed = function (e, pc) {
  244. if(this.callStatsIntegrationEnabled)
  245. CallStats.sendSetLocalDescFailed(e, pc, this.callstats);
  246. };
  247. /**
  248. * Notifies CallStats that peer connection failed to set remote description.
  249. *
  250. * @param {Error} e error to send
  251. * @param {RTCPeerConnection} pc connection on which failure occured.
  252. */
  253. Statistics.prototype.sendSetRemoteDescFailed = function (e, pc) {
  254. if(this.callStatsIntegrationEnabled)
  255. CallStats.sendSetRemoteDescFailed(e, pc, this.callstats);
  256. };
  257. /**
  258. * Notifies CallStats that peer connection failed to add ICE candidate.
  259. *
  260. * @param {Error} e error to send
  261. * @param {RTCPeerConnection} pc connection on which failure occured.
  262. */
  263. Statistics.prototype.sendAddIceCandidateFailed = function (e, pc) {
  264. if(this.callStatsIntegrationEnabled)
  265. CallStats.sendAddIceCandidateFailed(e, pc, this.callstats);
  266. };
  267. /**
  268. * Notifies CallStats that there is an unhandled error on the page.
  269. *
  270. * @param {Error} e error to send
  271. * @param {RTCPeerConnection} pc connection on which failure occured.
  272. */
  273. Statistics.prototype.sendUnhandledError = function (e) {
  274. if(this.callStatsIntegrationEnabled)
  275. CallStats.sendUnhandledError(e, this.callstats);
  276. };
  277. /**
  278. * Notifies CallStats that there is unhandled exception.
  279. *
  280. * @param {Error} e error to send
  281. */
  282. Statistics.sendUnhandledError = function (e) {
  283. CallStats.sendUnhandledError(e, null);
  284. };
  285. /**
  286. * Sends the given feedback through CallStats.
  287. *
  288. * @param overallFeedback an integer between 1 and 5 indicating the
  289. * user feedback
  290. * @param detailedFeedback detailed feedback from the user. Not yet used
  291. */
  292. Statistics.prototype.sendFeedback =
  293. function(overallFeedback, detailedFeedback){
  294. if(this.callStatsIntegrationEnabled && this.callstats)
  295. this.callstats.sendFeedback(overallFeedback, detailedFeedback);
  296. };
  297. Statistics.LOCAL_JID = require("../../service/statistics/constants").LOCAL_JID;
  298. module.exports = Statistics;