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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. Statistics.prototype.removeConnectionStatsListener = function (listener) {
  129. this.eventEmitter.removeListener(StatisticsEvents.CONNECTION_STATS, listener);
  130. };
  131. Statistics.prototype.dispose = function () {
  132. if(Statistics.audioLevelsEnabled) {
  133. Statistics.stopAllLocalStats();
  134. this.stopRemoteStats();
  135. if(this.eventEmitter)
  136. this.eventEmitter.removeAllListeners();
  137. }
  138. };
  139. Statistics.stopAllLocalStats = function () {
  140. if(!Statistics.audioLevelsEnabled)
  141. return;
  142. for(var i = 0; i < this.localStats.length; i++)
  143. this.localStats[i].stop();
  144. this.localStats = [];
  145. };
  146. Statistics.stopLocalStats = function (stream) {
  147. if(!Statistics.audioLevelsEnabled)
  148. return;
  149. for(var i = 0; i < Statistics.localStats.length; i++)
  150. if(Statistics.localStats[i].stream === stream){
  151. var localStats = Statistics.localStats.splice(i, 1);
  152. localStats[0].stop();
  153. break;
  154. }
  155. };
  156. Statistics.prototype.stopRemoteStats = function () {
  157. if (!Statistics.audioLevelsEnabled || !this.rtpStats) {
  158. return;
  159. }
  160. this.rtpStats.stop();
  161. this.rtpStats = null;
  162. if (this.logStatsIntervalId) {
  163. clearInterval(this.logStatsIntervalId);
  164. this.logStatsIntervalId = null;
  165. }
  166. };
  167. //CALSTATS METHODS
  168. /**
  169. * Initializes the callstats.io API.
  170. * @param peerConnection {JingleSessionPC} the session object
  171. * @param Settings {Settings} the settings instance. Declared in
  172. * /modules/settings/Settings.js
  173. */
  174. Statistics.prototype.startCallStats = function (session, settings) {
  175. if(this.callStatsIntegrationEnabled && !this.callstats) {
  176. this.callstats = new CallStats(session, settings, this.options);
  177. Statistics.callsStatsInstances.push(this.callstats);
  178. }
  179. };
  180. /**
  181. * Removes the callstats.io instances.
  182. */
  183. Statistics.prototype.stopCallStats = function () {
  184. if(this.callStatsIntegrationEnabled && this.callstats) {
  185. var callstatsList = Statistics.callsStatsInstances;
  186. for(var i = 0;i < callstatsList.length; i++) {
  187. if(this.callstats === callstatsList[i]) {
  188. Statistics.callsStatsInstances.splice(i, 1);
  189. break;
  190. }
  191. }
  192. this.callstats = null;
  193. CallStats.dispose();
  194. }
  195. };
  196. /**
  197. * Returns true if the callstats integration is enabled, otherwise returns
  198. * false.
  199. *
  200. * @returns true if the callstats integration is enabled, otherwise returns
  201. * false.
  202. */
  203. Statistics.prototype.isCallstatsEnabled = function () {
  204. return this.callStatsIntegrationEnabled;
  205. };
  206. /**
  207. * Notifies CallStats for ice connection failed
  208. * @param {RTCPeerConnection} pc connection on which failure occured.
  209. */
  210. Statistics.prototype.sendIceConnectionFailedEvent = function (pc) {
  211. if(this.callstats)
  212. this.callstats.sendIceConnectionFailedEvent(pc, this.callstats);
  213. };
  214. /**
  215. * Notifies CallStats for mute events
  216. * @param mute {boolean} true for muted and false for not muted
  217. * @param type {String} "audio"/"video"
  218. */
  219. Statistics.prototype.sendMuteEvent = function (muted, type) {
  220. if(this.callstats)
  221. CallStats.sendMuteEvent(muted, type, this.callstats);
  222. };
  223. /**
  224. * Notifies CallStats for screen sharing events
  225. * @param start {boolean} true for starting screen sharing and
  226. * false for not stopping
  227. */
  228. Statistics.prototype.sendScreenSharingEvent = function (start) {
  229. if(this.callstats)
  230. CallStats.sendScreenSharingEvent(start, this.callstats);
  231. };
  232. /**
  233. * Notifies the statistics module that we are now the dominant speaker of the
  234. * conference.
  235. */
  236. Statistics.prototype.sendDominantSpeakerEvent = function () {
  237. if(this.callstats)
  238. CallStats.sendDominantSpeakerEvent(this.callstats);
  239. };
  240. /**
  241. * Notifies about active device.
  242. * @param {{deviceList: {String:String}}} list of devices with their data
  243. */
  244. Statistics.sendАctiveDeviceListEvent = function (devicesData) {
  245. if (Statistics.callsStatsInstances.length) {
  246. Statistics.callsStatsInstances.forEach(function (cs) {
  247. CallStats.sendАctiveDeviceListEvent(devicesData, cs);
  248. });
  249. } else {
  250. CallStats.sendАctiveDeviceListEvent(devicesData, null);
  251. }
  252. };
  253. /**
  254. * Lets the underlying statistics module know where is given SSRC rendered by
  255. * providing renderer tag ID.
  256. * @param ssrc {number} the SSRC of the stream
  257. * @param isLocal {boolean} <tt>true<tt> if this stream is local or
  258. * <tt>false</tt> otherwise.
  259. * @param usageLabel {string} meaningful usage label of this stream like
  260. * 'microphone', 'camera' or 'screen'.
  261. * @param containerId {string} the id of media 'audio' or 'video' tag which
  262. * renders the stream.
  263. */
  264. Statistics.prototype.associateStreamWithVideoTag =
  265. function (ssrc, isLocal, usageLabel, containerId) {
  266. if(this.callstats) {
  267. this.callstats.associateStreamWithVideoTag(
  268. ssrc, isLocal, usageLabel, containerId);
  269. }
  270. };
  271. /**
  272. * Notifies CallStats that getUserMedia failed.
  273. *
  274. * @param {Error} e error to send
  275. */
  276. Statistics.sendGetUserMediaFailed = function (e) {
  277. if (Statistics.callsStatsInstances.length) {
  278. Statistics.callsStatsInstances.forEach(function (cs) {
  279. CallStats.sendGetUserMediaFailed(
  280. e instanceof JitsiTrackError
  281. ? formatJitsiTrackErrorForCallStats(e)
  282. : e,
  283. cs);
  284. });
  285. } else {
  286. CallStats.sendGetUserMediaFailed(
  287. e instanceof JitsiTrackError
  288. ? formatJitsiTrackErrorForCallStats(e)
  289. : e,
  290. null);
  291. }
  292. };
  293. /**
  294. * Notifies CallStats that peer connection failed to create offer.
  295. *
  296. * @param {Error} e error to send
  297. * @param {RTCPeerConnection} pc connection on which failure occured.
  298. */
  299. Statistics.prototype.sendCreateOfferFailed = function (e, pc) {
  300. if(this.callstats)
  301. CallStats.sendCreateOfferFailed(e, pc, this.callstats);
  302. };
  303. /**
  304. * Notifies CallStats that peer connection failed to create answer.
  305. *
  306. * @param {Error} e error to send
  307. * @param {RTCPeerConnection} pc connection on which failure occured.
  308. */
  309. Statistics.prototype.sendCreateAnswerFailed = function (e, pc) {
  310. if(this.callstats)
  311. CallStats.sendCreateAnswerFailed(e, pc, this.callstats);
  312. };
  313. /**
  314. * Notifies CallStats that peer connection failed to set local description.
  315. *
  316. * @param {Error} e error to send
  317. * @param {RTCPeerConnection} pc connection on which failure occured.
  318. */
  319. Statistics.prototype.sendSetLocalDescFailed = function (e, pc) {
  320. if(this.callstats)
  321. CallStats.sendSetLocalDescFailed(e, pc, this.callstats);
  322. };
  323. /**
  324. * Notifies CallStats that peer connection failed to set remote description.
  325. *
  326. * @param {Error} e error to send
  327. * @param {RTCPeerConnection} pc connection on which failure occured.
  328. */
  329. Statistics.prototype.sendSetRemoteDescFailed = function (e, pc) {
  330. if(this.callstats)
  331. CallStats.sendSetRemoteDescFailed(e, pc, this.callstats);
  332. };
  333. /**
  334. * Notifies CallStats that peer connection failed to add ICE candidate.
  335. *
  336. * @param {Error} e error to send
  337. * @param {RTCPeerConnection} pc connection on which failure occured.
  338. */
  339. Statistics.prototype.sendAddIceCandidateFailed = function (e, pc) {
  340. if(this.callstats)
  341. CallStats.sendAddIceCandidateFailed(e, pc, this.callstats);
  342. };
  343. /**
  344. * Adds to CallStats an application log.
  345. *
  346. * @param {String} a log message to send or an {Error} object to be reported
  347. */
  348. Statistics.sendLog = function (m) {
  349. if (Statistics.callsStatsInstances.length) {
  350. Statistics.callsStatsInstances.forEach(function (cs) {
  351. CallStats.sendApplicationLog(m, cs);
  352. });
  353. } else {
  354. CallStats.sendApplicationLog(m, null);
  355. }
  356. };
  357. /**
  358. * Sends the given feedback through CallStats.
  359. *
  360. * @param overall an integer between 1 and 5 indicating the user feedback
  361. * @param detailed detailed feedback from the user. Not yet used
  362. */
  363. Statistics.prototype.sendFeedback = function(overall, detailed) {
  364. if(this.callstats)
  365. this.callstats.sendFeedback(overall, detailed);
  366. };
  367. Statistics.LOCAL_JID = require("../../service/statistics/constants").LOCAL_JID;
  368. /**
  369. * Reports global error to CallStats.
  370. *
  371. * @param {Error} error
  372. */
  373. Statistics.reportGlobalError = function (error) {
  374. if (error instanceof JitsiTrackError && error.gum) {
  375. Statistics.sendGetUserMediaFailed(error);
  376. } else {
  377. Statistics.sendLog(error);
  378. }
  379. };
  380. module.exports = Statistics;