You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

statistics.js 11KB

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