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.

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