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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. // Since callstats.io is a third party, we cannot guarantee the quality of
  8. // their service. More specifically, their server may take noticeably long
  9. // time to respond. Consequently, it is in our best interest (in the sense
  10. // that the intergration of callstats.io is pretty important to us but not
  11. // enough to allow it to prevent people from joining a conference) to (1)
  12. // start downloading their API as soon as possible and (2) do the
  13. // downloading asynchronously.
  14. function loadCallStatsAPI() {
  15. (function (d, src) {
  16. var elementName = 'script';
  17. var newScript = d.createElement(elementName);
  18. var referenceNode = d.getElementsByTagName(elementName)[0];
  19. newScript.async = true;
  20. newScript.src = src;
  21. referenceNode.parentNode.insertBefore(newScript, referenceNode);
  22. })(document, 'https://api.callstats.io/static/callstats.min.js');
  23. // FIXME At the time of this writing, we hope that the callstats.io API will
  24. // have loaded by the time we needed it (i.e. CallStats.init is invoked).
  25. }
  26. var eventEmitter = new EventEmitter();
  27. function Statistics(options) {
  28. this.rtpStats = null;
  29. this.eventEmitter = new EventEmitter();
  30. this.options = options || {};
  31. this.callStatsIntegrationEnabled
  32. = this.options.callStatsID && this.options.callStatsSecret
  33. // Even though AppID and AppSecret may be specified, the integration of
  34. // callstats.io may be disabled because of globally-disallowed requests
  35. // to any third parties.
  36. && (this.options.disableThirdPartyRequests !== true);
  37. if(this.callStatsIntegrationEnabled)
  38. loadCallStatsAPI();
  39. this.audioLevelsEnabled = !this.disableAudioLevels || true;
  40. this.callStats = null;
  41. }
  42. Statistics.prototype.startRemoteStats = function (peerconnection) {
  43. if(!this.audioLevelsEnabled)
  44. return;
  45. if (this.rtpStats) {
  46. this.rtpStats.stop();
  47. }
  48. this.rtpStats = new RTPStats(peerconnection, 200, 2000, this.eventEmitter);
  49. this.rtpStats.start();
  50. }
  51. Statistics.localStats = [];
  52. Statistics.startLocalStats = function (stream, callback) {
  53. if(!this.audioLevelsEnabled)
  54. return;
  55. var localStats = new LocalStats(stream, 200, callback);
  56. this.localStats.push(localStats);
  57. localStats.start();
  58. }
  59. Statistics.prototype.addAudioLevelListener = function(listener)
  60. {
  61. if(!this.audioLevelsEnabled)
  62. return;
  63. this.eventEmitter.on(StatisticsEvents.AUDIO_LEVEL, listener);
  64. }
  65. Statistics.prototype.removeAudioLevelListener = function(listener)
  66. {
  67. if(!this.audioLevelsEnabled)
  68. return;
  69. this.eventEmitter.removeListener(StatisticsEvents.AUDIO_LEVEL, listener);
  70. }
  71. Statistics.prototype.dispose = function () {
  72. if(this.audioLevelsEnabled) {
  73. Statistics.stopAllLocalStats();
  74. this.stopRemote();
  75. if(this.eventEmitter)
  76. this.eventEmitter.removeAllListeners();
  77. if(eventEmitter)
  78. eventEmitter.removeAllListeners();
  79. }
  80. if(this.callstats)
  81. {
  82. this.callstats.sendTerminateEvent();
  83. this.callstats = null;
  84. }
  85. }
  86. Statistics.stopAllLocalStats = function () {
  87. if(!this.audioLevelsEnabled)
  88. return;
  89. for(var i = 0; i < this.localStats.length; i++)
  90. this.localStats[i].stop();
  91. this.localStats = [];
  92. }
  93. Statistics.stopLocalStats = function (stream) {
  94. if(!this.audioLevelsEnabled)
  95. return;
  96. for(var i = 0; i < Statistics.localStats.length; i++)
  97. if(Statistics.localStats[i].stream === stream){
  98. var localStats = Statistics.localStats.splice(i, 1);
  99. localStats.stop();
  100. break;
  101. }
  102. }
  103. Statistics.prototype.stopRemote = function () {
  104. if (this.rtpStats && this.audioLevelsEnabled) {
  105. this.rtpStats.stop();
  106. this.eventEmitter.emit(StatisticsEvents.STOP);
  107. this.rtpStats = null;
  108. }
  109. };
  110. /**
  111. * Obtains audio level reported in the stats for specified peer.
  112. * @param peerJid full MUC jid of the user for whom we want to obtain last
  113. * audio level.
  114. * @param ssrc the SSRC of audio stream for which we want to obtain audio
  115. * level.
  116. * @returns {*} a float form 0 to 1 that represents current audio level or
  117. * <tt>null</tt> if for any reason the value is not available
  118. * at this time.
  119. */
  120. Statistics.prototype.getPeerSSRCAudioLevel = function (peerJid, ssrc) {
  121. if(!this.audioLevelsEnabled)
  122. return;
  123. var peerStats = this.rtpStats.jid2stats[peerJid];
  124. return peerStats ? peerStats.ssrc2AudioLevel[ssrc] : null;
  125. };
  126. //CALSTATS METHODS
  127. /**
  128. * Initializes the callstats.io API.
  129. * @param peerConnection {JingleSessionPC} the session object
  130. * @param Settings {Settings} the settings instance. Declared in
  131. * /modules/settings/Settings.js
  132. */
  133. Statistics.prototype.startCallStats = function (session, settings) {
  134. if(this.callStatsIntegrationEnabled) {
  135. this.callstats = new CallStats(session, settings, this.options);
  136. }
  137. }
  138. /**
  139. * Returns true if the callstats integration is enabled, otherwise returns
  140. * false.
  141. *
  142. * @returns true if the callstats integration is enabled, otherwise returns
  143. * false.
  144. */
  145. Statistics.prototype.isCallstatsEnabled = function () {
  146. return this.callStatsIntegrationEnabled;
  147. }
  148. /**
  149. * Notifies CallStats for connection setup errors
  150. */
  151. Statistics.prototype.sendSetupFailedEvent = function () {
  152. if(this.callStatsIntegrationEnabled && this.callstats)
  153. this.callstats.sendSetupFailedEvent();
  154. }
  155. /**
  156. * Notifies CallStats for mute events
  157. * @param mute {boolean} true for muted and false for not muted
  158. * @param type {String} "audio"/"video"
  159. */
  160. Statistics.prototype.sendMuteEvent = function (muted, type) {
  161. if(this.callStatsIntegrationEnabled && this.callstats)
  162. this.callstats.sendMuteEvent(muted, type);
  163. }
  164. /**
  165. * Notifies CallStats that getUserMedia failed.
  166. *
  167. * @param {Error} e error to send
  168. */
  169. Statistics.prototype.sendGetUserMediaFailed = function (e) {
  170. if(this.callStatsIntegrationEnabled)
  171. CallStats.sendGetUserMediaFailed(e, this.callstats);
  172. };
  173. /**
  174. * Notifies CallStats that getUserMedia failed.
  175. *
  176. * @param {Error} e error to send
  177. */
  178. Statistics.sendGetUserMediaFailed = function (e) {
  179. CallStats.sendGetUserMediaFailed(e, null);
  180. };
  181. /**
  182. * Notifies CallStats that peer connection failed to create offer.
  183. *
  184. * @param {Error} e error to send
  185. * @param {RTCPeerConnection} pc connection on which failure occured.
  186. */
  187. Statistics.prototype.sendCreateOfferFailed = function (e, pc) {
  188. if(this.callStatsIntegrationEnabled)
  189. CallStats.sendCreateOfferFailed(e, pc, this.callstats);
  190. };
  191. /**
  192. * Notifies CallStats that peer connection failed to create answer.
  193. *
  194. * @param {Error} e error to send
  195. * @param {RTCPeerConnection} pc connection on which failure occured.
  196. */
  197. Statistics.prototype.sendCreateAnswerFailed = function (e, pc) {
  198. if(this.callStatsIntegrationEnabled)
  199. CallStats.sendCreateAnswerFailed(e, pc, this.callstats);
  200. };
  201. /**
  202. * Notifies CallStats that peer connection failed to set local description.
  203. *
  204. * @param {Error} e error to send
  205. * @param {RTCPeerConnection} pc connection on which failure occured.
  206. */
  207. Statistics.prototype.sendSetLocalDescFailed = function (e, pc) {
  208. if(this.callStatsIntegrationEnabled)
  209. CallStats.sendSetLocalDescFailed(e, pc, this.callstats);
  210. }
  211. /**
  212. * Notifies CallStats that peer connection failed to set remote description.
  213. *
  214. * @param {Error} e error to send
  215. * @param {RTCPeerConnection} pc connection on which failure occured.
  216. */
  217. Statistics.prototype.sendSetRemoteDescFailed = function (e, pc) {
  218. if(this.callStatsIntegrationEnabled)
  219. CallStats.sendSetRemoteDescFailed(e, pc, this.callstats);
  220. }
  221. /**
  222. * Notifies CallStats that peer connection failed to add ICE candidate.
  223. *
  224. * @param {Error} e error to send
  225. * @param {RTCPeerConnection} pc connection on which failure occured.
  226. */
  227. Statistics.prototype.sendAddIceCandidateFailed = function (e, pc) {
  228. if(this.callStatsIntegrationEnabled)
  229. CallStats.sendAddIceCandidateFailed(e, pc, this.callstats);
  230. }
  231. /**
  232. * Sends the given feedback through CallStats.
  233. *
  234. * @param overallFeedback an integer between 1 and 5 indicating the
  235. * user feedback
  236. * @param detailedFeedback detailed feedback from the user. Not yet used
  237. */
  238. Statistics.prototype.sendFeedback =
  239. function(overallFeedback, detailedFeedback){
  240. if(this.callStatsIntegrationEnabled && this.callstats)
  241. this.callstats.sendFeedback(overallFeedback, detailedFeedback);
  242. }
  243. Statistics.LOCAL_JID = require("../../service/statistics/constants").LOCAL_JID;
  244. module.exports = Statistics;