Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

statistics.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* global require, APP */
  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. var eventEmitter = new EventEmitter();
  24. function Statistics(options) {
  25. this.rtpStats = null;
  26. this.eventEmitter = new EventEmitter();
  27. this.options = options || {};
  28. this.callStatsIntegrationEnabled
  29. = this.options.callStatsID && this.options.callStatsSecret
  30. // Even though AppID and AppSecret may be specified, the integration of
  31. // callstats.io may be disabled because of globally-disallowed requests
  32. // to any third parties.
  33. && (this.options.disableThirdPartyRequests !== true);
  34. if(this.callStatsIntegrationEnabled)
  35. loadCallStatsAPI();
  36. this.callStats = null;
  37. }
  38. Statistics.audioLevelsEnabled = false;
  39. Statistics.prototype.startRemoteStats = function (peerconnection) {
  40. if(!Statistics.audioLevelsEnabled)
  41. return;
  42. if (this.rtpStats) {
  43. this.rtpStats.stop();
  44. }
  45. this.rtpStats = new RTPStats(peerconnection, 200, 2000, this.eventEmitter);
  46. this.rtpStats.start();
  47. }
  48. Statistics.localStats = [];
  49. Statistics.startLocalStats = function (stream, callback) {
  50. if(!Statistics.audioLevelsEnabled)
  51. return;
  52. var localStats = new LocalStats(stream, 200, callback);
  53. this.localStats.push(localStats);
  54. localStats.start();
  55. }
  56. Statistics.prototype.addAudioLevelListener = function(listener)
  57. {
  58. if(!Statistics.audioLevelsEnabled)
  59. return;
  60. this.eventEmitter.on(StatisticsEvents.AUDIO_LEVEL, listener);
  61. }
  62. Statistics.prototype.removeAudioLevelListener = function(listener)
  63. {
  64. if(!Statistics.audioLevelsEnabled)
  65. return;
  66. this.eventEmitter.removeListener(StatisticsEvents.AUDIO_LEVEL, listener);
  67. }
  68. Statistics.prototype.dispose = function () {
  69. if(Statistics.audioLevelsEnabled) {
  70. Statistics.stopAllLocalStats();
  71. this.stopRemote();
  72. if(this.eventEmitter)
  73. this.eventEmitter.removeAllListeners();
  74. if(eventEmitter)
  75. eventEmitter.removeAllListeners();
  76. }
  77. if(this.callstats)
  78. {
  79. this.callstats.sendTerminateEvent();
  80. this.callstats = null;
  81. }
  82. }
  83. Statistics.stopAllLocalStats = function () {
  84. if(!Statistics.audioLevelsEnabled)
  85. return;
  86. for(var i = 0; i < this.localStats.length; i++)
  87. this.localStats[i].stop();
  88. this.localStats = [];
  89. }
  90. Statistics.stopLocalStats = function (stream) {
  91. if(!Statistics.audioLevelsEnabled)
  92. return;
  93. for(var i = 0; i < Statistics.localStats.length; i++)
  94. if(Statistics.localStats[i].stream === stream){
  95. var localStats = Statistics.localStats.splice(i, 1);
  96. localStats.stop();
  97. break;
  98. }
  99. }
  100. Statistics.prototype.stopRemote = function () {
  101. if (this.rtpStats && Statistics.audioLevelsEnabled) {
  102. this.rtpStats.stop();
  103. this.eventEmitter.emit(StatisticsEvents.STOP);
  104. this.rtpStats = null;
  105. }
  106. };
  107. /**
  108. * Obtains audio level reported in the stats for specified peer.
  109. * @param peerJid full MUC jid of the user for whom we want to obtain last
  110. * audio level.
  111. * @param ssrc the SSRC of audio stream for which we want to obtain audio
  112. * level.
  113. * @returns {*} a float form 0 to 1 that represents current audio level or
  114. * <tt>null</tt> if for any reason the value is not available
  115. * at this time.
  116. */
  117. Statistics.prototype.getPeerSSRCAudioLevel = function (peerJid, ssrc) {
  118. if(!Statistics.audioLevelsEnabled)
  119. return;
  120. var peerStats = this.rtpStats.jid2stats[peerJid];
  121. return peerStats ? peerStats.ssrc2AudioLevel[ssrc] : null;
  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 connection setup errors
  147. */
  148. Statistics.prototype.sendSetupFailedEvent = function () {
  149. if(this.callStatsIntegrationEnabled && this.callstats)
  150. this.callstats.sendSetupFailedEvent();
  151. }
  152. /**
  153. * Notifies CallStats for mute events
  154. * @param mute {boolean} true for muted and false for not muted
  155. * @param type {String} "audio"/"video"
  156. */
  157. Statistics.prototype.sendMuteEvent = function (muted, type) {
  158. if(this.callStatsIntegrationEnabled && this.callstats)
  159. this.callstats.sendMuteEvent(muted, type);
  160. }
  161. /**
  162. * Notifies CallStats that getUserMedia failed.
  163. *
  164. * @param {Error} e error to send
  165. */
  166. Statistics.prototype.sendGetUserMediaFailed = function (e) {
  167. if(this.callStatsIntegrationEnabled)
  168. CallStats.sendGetUserMediaFailed(e, this.callstats);
  169. };
  170. /**
  171. * Notifies CallStats that getUserMedia failed.
  172. *
  173. * @param {Error} e error to send
  174. */
  175. Statistics.sendGetUserMediaFailed = function (e) {
  176. CallStats.sendGetUserMediaFailed(e, null);
  177. };
  178. /**
  179. * Notifies CallStats that peer connection failed to create offer.
  180. *
  181. * @param {Error} e error to send
  182. * @param {RTCPeerConnection} pc connection on which failure occured.
  183. */
  184. Statistics.prototype.sendCreateOfferFailed = function (e, pc) {
  185. if(this.callStatsIntegrationEnabled)
  186. CallStats.sendCreateOfferFailed(e, pc, this.callstats);
  187. };
  188. /**
  189. * Notifies CallStats that peer connection failed to create answer.
  190. *
  191. * @param {Error} e error to send
  192. * @param {RTCPeerConnection} pc connection on which failure occured.
  193. */
  194. Statistics.prototype.sendCreateAnswerFailed = function (e, pc) {
  195. if(this.callStatsIntegrationEnabled)
  196. CallStats.sendCreateAnswerFailed(e, pc, this.callstats);
  197. };
  198. /**
  199. * Notifies CallStats that peer connection failed to set local description.
  200. *
  201. * @param {Error} e error to send
  202. * @param {RTCPeerConnection} pc connection on which failure occured.
  203. */
  204. Statistics.prototype.sendSetLocalDescFailed = function (e, pc) {
  205. if(this.callStatsIntegrationEnabled)
  206. CallStats.sendSetLocalDescFailed(e, pc, this.callstats);
  207. }
  208. /**
  209. * Notifies CallStats that peer connection failed to set remote description.
  210. *
  211. * @param {Error} e error to send
  212. * @param {RTCPeerConnection} pc connection on which failure occured.
  213. */
  214. Statistics.prototype.sendSetRemoteDescFailed = function (e, pc) {
  215. if(this.callStatsIntegrationEnabled)
  216. CallStats.sendSetRemoteDescFailed(e, pc, this.callstats);
  217. }
  218. /**
  219. * Notifies CallStats that peer connection failed to add ICE candidate.
  220. *
  221. * @param {Error} e error to send
  222. * @param {RTCPeerConnection} pc connection on which failure occured.
  223. */
  224. Statistics.prototype.sendAddIceCandidateFailed = function (e, pc) {
  225. if(this.callStatsIntegrationEnabled)
  226. CallStats.sendAddIceCandidateFailed(e, pc, this.callstats);
  227. }
  228. /**
  229. * Sends the given feedback through CallStats.
  230. *
  231. * @param overallFeedback an integer between 1 and 5 indicating the
  232. * user feedback
  233. * @param detailedFeedback detailed feedback from the user. Not yet used
  234. */
  235. Statistics.prototype.sendFeedback =
  236. function(overallFeedback, detailedFeedback){
  237. if(this.callStatsIntegrationEnabled && this.callstats)
  238. this.callstats.sendFeedback(overallFeedback, detailedFeedback);
  239. }
  240. Statistics.LOCAL_JID = require("../../service/statistics/constants").LOCAL_JID;
  241. module.exports = Statistics;