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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. var JitsiTrackError = require("../../JitsiTrackError");
  10. // Since callstats.io is a third party, we cannot guarantee the quality of their
  11. // service. More specifically, their server may take noticeably long time to
  12. // respond. Consequently, it is in our best interest (in the sense that the
  13. // intergration of callstats.io is pretty important to us but not enough to
  14. // allow it to prevent people from joining a conference) to (1) start
  15. // downloading their API as soon as possible and (2) do the downloading
  16. // asynchronously.
  17. function loadCallStatsAPI() {
  18. ScriptUtil.loadScript(
  19. 'https://api.callstats.io/static/callstats.min.js',
  20. /* async */ true,
  21. /* prepend */ true);
  22. // FIXME At the time of this writing, we hope that the callstats.io API will
  23. // have loaded by the time we needed it (i.e. CallStats.init is invoked).
  24. }
  25. /**
  26. * Log stats via the focus once every this many milliseconds.
  27. */
  28. var LOG_INTERVAL = 60000;
  29. /**
  30. * callstats strips any additional fields from Error except for "name", "stack",
  31. * "message" and "constraintName". So we need to bundle additional information
  32. * from JitsiTrackError into error passed to callstats to preserve valuable
  33. * information about error.
  34. * @param {JitsiTrackError} error
  35. */
  36. function formatJitsiTrackErrorForCallStats(error) {
  37. var err = new Error();
  38. // Just copy original stack from error
  39. err.stack = error.stack;
  40. // Combine name from error's name plus (possibly) name of original GUM error
  41. err.name = (error.name || "Unknown error") + (error.gum && error.gum.error
  42. && error.gum.error.name ? " - " + error.gum.error.name : "");
  43. // Put all constraints into this field. For constraint failed errors we will
  44. // still know which exactly constraint failed as it will be a part of
  45. // message.
  46. err.constraintName = error.gum && error.gum.constraints
  47. ? JSON.stringify(error.gum.constraints) : "";
  48. // Just copy error's message.
  49. err.message = error.message;
  50. return err;
  51. }
  52. function Statistics(xmpp, options) {
  53. this.rtpStats = null;
  54. this.eventEmitter = new EventEmitter();
  55. this.xmpp = xmpp;
  56. this.options = options || {};
  57. this.callStatsIntegrationEnabled
  58. = this.options.callStatsID && this.options.callStatsSecret
  59. // Even though AppID and AppSecret may be specified, the integration
  60. // of callstats.io may be disabled because of globally-disallowed
  61. // requests to any third parties.
  62. && (this.options.disableThirdPartyRequests !== true);
  63. if(this.callStatsIntegrationEnabled)
  64. loadCallStatsAPI();
  65. this.callStats = null;
  66. // audioLevelsInterval = 200 unless overridden in the config
  67. this.audioLevelsInterval
  68. = typeof this.options.audioLevelsInterval !== 'undefined'
  69. ? this.options.audioLevelsInterval : 200;
  70. /**
  71. * Send the stats already saved in rtpStats to be logged via the focus.
  72. */
  73. this.logStatsIntervalId = null;
  74. }
  75. Statistics.audioLevelsEnabled = false;
  76. /**
  77. * Array of callstats instances. Used to call Statistics static methods and
  78. * send stats to all cs instances.
  79. */
  80. Statistics.callsStatsInstances = [];
  81. Statistics.prototype.startRemoteStats = function (peerconnection) {
  82. if(!Statistics.audioLevelsEnabled)
  83. return;
  84. this.stopRemoteStats();
  85. try {
  86. this.rtpStats
  87. = new RTPStats(peerconnection,
  88. this.audioLevelsInterval, 2000, this.eventEmitter);
  89. this.rtpStats.start();
  90. } catch (e) {
  91. this.rtpStats = null;
  92. logger.error('Failed to start collecting remote statistics: ' + e);
  93. }
  94. if (this.rtpStats) {
  95. this.logStatsIntervalId = setInterval(function () {
  96. var stats = this.rtpStats.getCollectedStats();
  97. if (this.xmpp.sendLogs(stats)) {
  98. this.rtpStats.clearCollectedStats();
  99. }
  100. }.bind(this), LOG_INTERVAL);
  101. }
  102. };
  103. Statistics.localStats = [];
  104. Statistics.startLocalStats = function (stream, callback) {
  105. if(!Statistics.audioLevelsEnabled)
  106. return;
  107. var localStats = new LocalStats(stream, this.audioLevelsInterval, callback);
  108. this.localStats.push(localStats);
  109. localStats.start();
  110. };
  111. Statistics.prototype.addAudioLevelListener = function(listener) {
  112. if(!Statistics.audioLevelsEnabled)
  113. return;
  114. this.eventEmitter.on(StatisticsEvents.AUDIO_LEVEL, listener);
  115. };
  116. Statistics.prototype.removeAudioLevelListener = function(listener) {
  117. if(!Statistics.audioLevelsEnabled)
  118. return;
  119. this.eventEmitter.removeListener(StatisticsEvents.AUDIO_LEVEL, listener);
  120. };
  121. Statistics.prototype.addConnectionStatsListener = function (listener) {
  122. this.eventEmitter.on(StatisticsEvents.CONNECTION_STATS, listener);
  123. };
  124. Statistics.prototype.removeConnectionStatsListener = function (listener) {
  125. this.eventEmitter.removeListener(StatisticsEvents.CONNECTION_STATS, listener);
  126. };
  127. Statistics.prototype.dispose = function () {
  128. if(Statistics.audioLevelsEnabled) {
  129. Statistics.stopAllLocalStats();
  130. this.stopRemoteStats();
  131. if(this.eventEmitter)
  132. this.eventEmitter.removeAllListeners();
  133. }
  134. };
  135. Statistics.stopAllLocalStats = function () {
  136. if(!Statistics.audioLevelsEnabled)
  137. return;
  138. for(var i = 0; i < this.localStats.length; i++)
  139. this.localStats[i].stop();
  140. this.localStats = [];
  141. };
  142. Statistics.stopLocalStats = function (stream) {
  143. if(!Statistics.audioLevelsEnabled)
  144. return;
  145. for(var i = 0; i < Statistics.localStats.length; i++)
  146. if(Statistics.localStats[i].stream === stream){
  147. var localStats = Statistics.localStats.splice(i, 1);
  148. localStats[0].stop();
  149. break;
  150. }
  151. };
  152. Statistics.prototype.stopRemoteStats = function () {
  153. if (!Statistics.audioLevelsEnabled || !this.rtpStats) {
  154. return;
  155. }
  156. this.rtpStats.stop();
  157. this.rtpStats = null;
  158. if (this.logStatsIntervalId) {
  159. clearInterval(this.logStatsIntervalId);
  160. this.logStatsIntervalId = null;
  161. }
  162. };
  163. //CALSTATS METHODS
  164. /**
  165. * Initializes the callstats.io API.
  166. * @param peerConnection {JingleSessionPC} the session object
  167. * @param Settings {Settings} the settings instance. Declared in
  168. * /modules/settings/Settings.js
  169. */
  170. Statistics.prototype.startCallStats = function (session, settings) {
  171. if(this.callStatsIntegrationEnabled && !this.callstats) {
  172. this.callstats = new CallStats(session, settings, this.options);
  173. Statistics.callsStatsInstances.push(this.callstats);
  174. }
  175. };
  176. /**
  177. * Returns true if the callstats integration is enabled, otherwise returns
  178. * false.
  179. *
  180. * @returns true if the callstats integration is enabled, otherwise returns
  181. * false.
  182. */
  183. Statistics.prototype.isCallstatsEnabled = function () {
  184. return this.callStatsIntegrationEnabled;
  185. };
  186. /**
  187. * Notifies CallStats for ice connection failed
  188. * @param {RTCPeerConnection} pc connection on which failure occured.
  189. */
  190. Statistics.prototype.sendIceConnectionFailedEvent = function (pc) {
  191. if(this.callstats)
  192. this.callstats.sendIceConnectionFailedEvent(pc, this.callstats);
  193. };
  194. /**
  195. * Notifies CallStats for mute events
  196. * @param mute {boolean} true for muted and false for not muted
  197. * @param type {String} "audio"/"video"
  198. */
  199. Statistics.prototype.sendMuteEvent = function (muted, type) {
  200. if(this.callstats)
  201. CallStats.sendMuteEvent(muted, type, this.callstats);
  202. };
  203. /**
  204. * Notifies CallStats for screen sharing events
  205. * @param start {boolean} true for starting screen sharing and
  206. * false for not stopping
  207. */
  208. Statistics.prototype.sendScreenSharingEvent = function (start) {
  209. if(this.callstats)
  210. CallStats.sendScreenSharingEvent(start, this.callstats);
  211. };
  212. /**
  213. * Notifies the statistics module that we are now the dominant speaker of the
  214. * conference.
  215. */
  216. Statistics.prototype.sendDominantSpeakerEvent = function () {
  217. if(this.callstats)
  218. CallStats.sendDominantSpeakerEvent(this.callstats);
  219. };
  220. /**
  221. * Lets the underlying statistics module know where is given SSRC rendered by
  222. * providing renderer tag ID.
  223. * @param ssrc {number} the SSRC of the stream
  224. * @param isLocal {boolean} <tt>true<tt> if this stream is local or
  225. * <tt>false</tt> otherwise.
  226. * @param usageLabel {string} meaningful usage label of this stream like
  227. * 'microphone', 'camera' or 'screen'.
  228. * @param containerId {string} the id of media 'audio' or 'video' tag which
  229. * renders the stream.
  230. */
  231. Statistics.prototype.associateStreamWithVideoTag =
  232. function (ssrc, isLocal, usageLabel, containerId) {
  233. if(this.callstats) {
  234. this.callstats.associateStreamWithVideoTag(
  235. ssrc, isLocal, usageLabel, containerId);
  236. }
  237. };
  238. /**
  239. * Notifies CallStats that getUserMedia failed.
  240. *
  241. * @param {Error} e error to send
  242. */
  243. Statistics.sendGetUserMediaFailed = function (e) {
  244. if (Statistics.callsStatsInstances.length) {
  245. Statistics.callsStatsInstances.forEach(function (cs) {
  246. CallStats.sendGetUserMediaFailed(
  247. e instanceof JitsiTrackError
  248. ? formatJitsiTrackErrorForCallStats(e)
  249. : e,
  250. cs);
  251. });
  252. } else {
  253. CallStats.sendGetUserMediaFailed(
  254. e instanceof JitsiTrackError
  255. ? formatJitsiTrackErrorForCallStats(e)
  256. : e,
  257. null);
  258. }
  259. };
  260. /**
  261. * Notifies CallStats that peer connection failed to create offer.
  262. *
  263. * @param {Error} e error to send
  264. * @param {RTCPeerConnection} pc connection on which failure occured.
  265. */
  266. Statistics.prototype.sendCreateOfferFailed = function (e, pc) {
  267. if(this.callstats)
  268. CallStats.sendCreateOfferFailed(e, pc, this.callstats);
  269. };
  270. /**
  271. * Notifies CallStats that peer connection failed to create answer.
  272. *
  273. * @param {Error} e error to send
  274. * @param {RTCPeerConnection} pc connection on which failure occured.
  275. */
  276. Statistics.prototype.sendCreateAnswerFailed = function (e, pc) {
  277. if(this.callstats)
  278. CallStats.sendCreateAnswerFailed(e, pc, this.callstats);
  279. };
  280. /**
  281. * Notifies CallStats that peer connection failed to set local description.
  282. *
  283. * @param {Error} e error to send
  284. * @param {RTCPeerConnection} pc connection on which failure occured.
  285. */
  286. Statistics.prototype.sendSetLocalDescFailed = function (e, pc) {
  287. if(this.callstats)
  288. CallStats.sendSetLocalDescFailed(e, pc, this.callstats);
  289. };
  290. /**
  291. * Notifies CallStats that peer connection failed to set remote description.
  292. *
  293. * @param {Error} e error to send
  294. * @param {RTCPeerConnection} pc connection on which failure occured.
  295. */
  296. Statistics.prototype.sendSetRemoteDescFailed = function (e, pc) {
  297. if(this.callstats)
  298. CallStats.sendSetRemoteDescFailed(e, pc, this.callstats);
  299. };
  300. /**
  301. * Notifies CallStats that peer connection failed to add ICE candidate.
  302. *
  303. * @param {Error} e error to send
  304. * @param {RTCPeerConnection} pc connection on which failure occured.
  305. */
  306. Statistics.prototype.sendAddIceCandidateFailed = function (e, pc) {
  307. if(this.callstats)
  308. CallStats.sendAddIceCandidateFailed(e, pc, this.callstats);
  309. };
  310. /**
  311. * Notifies CallStats that there is unhandled exception.
  312. *
  313. * @param {Error} e error to send
  314. */
  315. Statistics.sendUnhandledError = function (e) {
  316. if (Statistics.callsStatsInstances.length) {
  317. Statistics.callsStatsInstances.forEach(function (cs) {
  318. CallStats.sendUnhandledError(e, cs);
  319. });
  320. } else {
  321. CallStats.sendUnhandledError(e, null);
  322. }
  323. };
  324. /**
  325. * Adds to CallStats an application log.
  326. *
  327. * @param {String} a log message to send
  328. */
  329. Statistics.sendLog = function (m) {
  330. // uses the same field for cs stat as unhandled error
  331. Statistics.sendUnhandledError(m);
  332. };
  333. /**
  334. * Adds to CallStats an application log.
  335. *
  336. * @param {String} a log message to send
  337. */
  338. Statistics.prototype.sendLog = function (m) {
  339. // uses the same field for cs stat as unhandled error
  340. CallStats.sendUnhandledError(m, this.callstats);
  341. };
  342. /**
  343. * Sends the given feedback through CallStats.
  344. *
  345. * @param overall an integer between 1 and 5 indicating the user feedback
  346. * @param detailed detailed feedback from the user. Not yet used
  347. */
  348. Statistics.prototype.sendFeedback = function(overall, detailed) {
  349. if(this.callstats)
  350. this.callstats.sendFeedback(overall, detailed);
  351. };
  352. Statistics.LOCAL_JID = require("../../service/statistics/constants").LOCAL_JID;
  353. /**
  354. * Reports global error to CallStats.
  355. *
  356. * @param {Error} error
  357. */
  358. Statistics.reportGlobalError = function (error) {
  359. if (error instanceof JitsiTrackError && error.gum) {
  360. Statistics.sendGetUserMediaFailed(error);
  361. } else {
  362. Statistics.sendUnhandledError(error);
  363. }
  364. };
  365. module.exports = Statistics;