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

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