modified lib-jitsi-meet dev repo
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

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