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 11KB

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