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

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