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

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