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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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
  9. // their service. More specifically, their server may take noticeably long
  10. // time to respond. Consequently, it is in our best interest (in the sense
  11. // that the intergration of callstats.io is pretty important to us but not
  12. // enough to allow it to prevent people from joining a conference) to (1)
  13. // start downloading their API as soon as possible and (2) do the
  14. // downloading 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 of
  36. // callstats.io may be disabled because of globally-disallowed requests
  37. // 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
  44. * the focus.
  45. */
  46. this.logStatsIntervalId = null;
  47. }
  48. Statistics.audioLevelsEnabled = false;
  49. Statistics.prototype.startRemoteStats = function (peerconnection) {
  50. if(!Statistics.audioLevelsEnabled)
  51. return;
  52. this.stopRemoteStats();
  53. this.rtpStats = new RTPStats(peerconnection, 200, 2000, this.eventEmitter);
  54. this.rtpStats.start();
  55. this.logStatsIntervalId = setInterval(function () {
  56. var stats = this.rtpStats.getCollectedStats();
  57. if (this.xmpp.sendLogs(stats)) {
  58. this.rtpStats.clearCollectedStats();
  59. }
  60. }.bind(this), LOG_INTERVAL);
  61. };
  62. Statistics.localStats = [];
  63. Statistics.startLocalStats = function (stream, callback) {
  64. if(!Statistics.audioLevelsEnabled)
  65. return;
  66. var localStats = new LocalStats(stream, 200, callback);
  67. this.localStats.push(localStats);
  68. localStats.start();
  69. };
  70. Statistics.prototype.addAudioLevelListener = function(listener)
  71. {
  72. if(!Statistics.audioLevelsEnabled)
  73. return;
  74. this.eventEmitter.on(StatisticsEvents.AUDIO_LEVEL, listener);
  75. };
  76. Statistics.prototype.removeAudioLevelListener = function(listener)
  77. {
  78. if(!Statistics.audioLevelsEnabled)
  79. return;
  80. this.eventEmitter.removeListener(StatisticsEvents.AUDIO_LEVEL, listener);
  81. };
  82. Statistics.prototype.addConnectionStatsListener = function (listener) {
  83. this.eventEmitter.on(StatisticsEvents.CONNECTION_STATS, listener);
  84. };
  85. Statistics.prototype.removeConnectionStatsListener = function (listener) {
  86. this.eventEmitter.removeListener(StatisticsEvents.CONNECTION_STATS, listener);
  87. };
  88. Statistics.prototype.dispose = function () {
  89. if(Statistics.audioLevelsEnabled) {
  90. Statistics.stopAllLocalStats();
  91. this.stopRemoteStats();
  92. if(this.eventEmitter)
  93. this.eventEmitter.removeAllListeners();
  94. if(eventEmitter)
  95. eventEmitter.removeAllListeners();
  96. }
  97. if(this.callstats)
  98. {
  99. this.callstats.sendTerminateEvent();
  100. this.callstats = null;
  101. }
  102. };
  103. Statistics.stopAllLocalStats = function () {
  104. if(!Statistics.audioLevelsEnabled)
  105. return;
  106. for(var i = 0; i < this.localStats.length; i++)
  107. this.localStats[i].stop();
  108. this.localStats = [];
  109. };
  110. Statistics.stopLocalStats = function (stream) {
  111. if(!Statistics.audioLevelsEnabled)
  112. return;
  113. for(var i = 0; i < Statistics.localStats.length; i++)
  114. if(Statistics.localStats[i].stream === stream){
  115. var localStats = Statistics.localStats.splice(i, 1);
  116. localStats.stop();
  117. break;
  118. }
  119. };
  120. Statistics.prototype.stopRemoteStats = function () {
  121. if (!Statistics.audioLevelsEnabled || !this.rtpStats) {
  122. return;
  123. }
  124. this.rtpStats.stop();
  125. this.rtpStats = null;
  126. if (this.logStatsIntervalId) {
  127. clearInterval(this.logStatsIntervalId);
  128. this.logStatsIntervalId = null;
  129. }
  130. };
  131. /**
  132. * Obtains audio level reported in the stats for specified peer.
  133. * @param peerJid full MUC jid of the user for whom we want to obtain last
  134. * audio level.
  135. * @param ssrc the SSRC of audio stream for which we want to obtain audio
  136. * level.
  137. * @returns {*} a float form 0 to 1 that represents current audio level or
  138. * <tt>null</tt> if for any reason the value is not available
  139. * at this time.
  140. */
  141. Statistics.prototype.getPeerSSRCAudioLevel = function (peerJid, ssrc) {
  142. if(!Statistics.audioLevelsEnabled)
  143. return;
  144. // FIXME: not used, unknown property jid2stats
  145. var peerStats = this.rtpStats.jid2stats[peerJid];
  146. return peerStats ? peerStats.ssrc2AudioLevel[ssrc] : null;
  147. };
  148. //CALSTATS METHODS
  149. /**
  150. * Initializes the callstats.io API.
  151. * @param peerConnection {JingleSessionPC} the session object
  152. * @param Settings {Settings} the settings instance. Declared in
  153. * /modules/settings/Settings.js
  154. */
  155. Statistics.prototype.startCallStats = function (session, settings) {
  156. if(this.callStatsIntegrationEnabled && !this.callstats) {
  157. this.callstats = new CallStats(session, settings, this.options);
  158. }
  159. };
  160. /**
  161. * Returns true if the callstats integration is enabled, otherwise returns
  162. * false.
  163. *
  164. * @returns true if the callstats integration is enabled, otherwise returns
  165. * false.
  166. */
  167. Statistics.prototype.isCallstatsEnabled = function () {
  168. return this.callStatsIntegrationEnabled;
  169. };
  170. /**
  171. * Notifies CallStats for connection setup errors
  172. */
  173. Statistics.prototype.sendSetupFailedEvent = function () {
  174. if(this.callStatsIntegrationEnabled && this.callstats)
  175. this.callstats.sendSetupFailedEvent();
  176. };
  177. /**
  178. * Notifies CallStats for mute events
  179. * @param mute {boolean} true for muted and false for not muted
  180. * @param type {String} "audio"/"video"
  181. */
  182. Statistics.prototype.sendMuteEvent = function (muted, type) {
  183. if(this.callStatsIntegrationEnabled)
  184. CallStats.sendMuteEvent(muted, type, this.callstats);
  185. };
  186. /**
  187. * Notifies CallStats that getUserMedia failed.
  188. *
  189. * @param {Error} e error to send
  190. */
  191. Statistics.prototype.sendGetUserMediaFailed = function (e) {
  192. if(this.callStatsIntegrationEnabled)
  193. CallStats.sendGetUserMediaFailed(e, this.callstats);
  194. };
  195. /**
  196. * Notifies CallStats that getUserMedia failed.
  197. *
  198. * @param {Error} e error to send
  199. */
  200. Statistics.sendGetUserMediaFailed = function (e) {
  201. CallStats.sendGetUserMediaFailed(e, null);
  202. };
  203. /**
  204. * Notifies CallStats that peer connection failed to create offer.
  205. *
  206. * @param {Error} e error to send
  207. * @param {RTCPeerConnection} pc connection on which failure occured.
  208. */
  209. Statistics.prototype.sendCreateOfferFailed = function (e, pc) {
  210. if(this.callStatsIntegrationEnabled)
  211. CallStats.sendCreateOfferFailed(e, pc, this.callstats);
  212. };
  213. /**
  214. * Notifies CallStats that peer connection failed to create answer.
  215. *
  216. * @param {Error} e error to send
  217. * @param {RTCPeerConnection} pc connection on which failure occured.
  218. */
  219. Statistics.prototype.sendCreateAnswerFailed = function (e, pc) {
  220. if(this.callStatsIntegrationEnabled)
  221. CallStats.sendCreateAnswerFailed(e, pc, this.callstats);
  222. };
  223. /**
  224. * Notifies CallStats that peer connection failed to set local description.
  225. *
  226. * @param {Error} e error to send
  227. * @param {RTCPeerConnection} pc connection on which failure occured.
  228. */
  229. Statistics.prototype.sendSetLocalDescFailed = function (e, pc) {
  230. if(this.callStatsIntegrationEnabled)
  231. CallStats.sendSetLocalDescFailed(e, pc, this.callstats);
  232. };
  233. /**
  234. * Notifies CallStats that peer connection failed to set remote description.
  235. *
  236. * @param {Error} e error to send
  237. * @param {RTCPeerConnection} pc connection on which failure occured.
  238. */
  239. Statistics.prototype.sendSetRemoteDescFailed = function (e, pc) {
  240. if(this.callStatsIntegrationEnabled)
  241. CallStats.sendSetRemoteDescFailed(e, pc, this.callstats);
  242. };
  243. /**
  244. * Notifies CallStats that peer connection failed to add ICE candidate.
  245. *
  246. * @param {Error} e error to send
  247. * @param {RTCPeerConnection} pc connection on which failure occured.
  248. */
  249. Statistics.prototype.sendAddIceCandidateFailed = function (e, pc) {
  250. if(this.callStatsIntegrationEnabled)
  251. CallStats.sendAddIceCandidateFailed(e, pc, this.callstats);
  252. };
  253. /**
  254. * Sends the given feedback through CallStats.
  255. *
  256. * @param overallFeedback an integer between 1 and 5 indicating the
  257. * user feedback
  258. * @param detailedFeedback detailed feedback from the user. Not yet used
  259. */
  260. Statistics.prototype.sendFeedback =
  261. function(overallFeedback, detailedFeedback){
  262. if(this.callStatsIntegrationEnabled && this.callstats)
  263. this.callstats.sendFeedback(overallFeedback, detailedFeedback);
  264. };
  265. Statistics.LOCAL_JID = require("../../service/statistics/constants").LOCAL_JID;
  266. module.exports = Statistics;