modified lib-jitsi-meet dev repo
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

statistics.js 10KB

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