選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

statistics.js 12KB

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