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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  7. //var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  8. //var CallStats = require("./CallStats");
  9. //var RTCEvents = require("../../service/RTC/RTCEvents");
  10. //
  11. //function onDisposeConference(onUnload) {
  12. // CallStats.sendTerminateEvent();
  13. // stopRemote();
  14. // if(onUnload) {
  15. // stopLocal();
  16. // eventEmitter.removeAllListeners();
  17. // }
  18. //}
  19. var eventEmitter = new EventEmitter();
  20. function Statistics() {
  21. this.rtpStats = null;
  22. this.eventEmitter = new EventEmitter();
  23. }
  24. Statistics.prototype.startRemoteStats = function (peerconnection) {
  25. if (this.rtpStats) {
  26. this.rtpStats.stop();
  27. }
  28. this.rtpStats = new RTPStats(peerconnection, 200, 2000, this.eventEmitter);
  29. this.rtpStats.start();
  30. }
  31. Statistics.localStats = [];
  32. Statistics.startLocalStats = function (stream, callback) {
  33. var localStats = new LocalStats(stream, 200, callback);
  34. this.localStats.push(localStats);
  35. localStats.start();
  36. }
  37. Statistics.prototype.addAudioLevelListener = function(listener)
  38. {
  39. this.eventEmitter.on(StatisticsEvents.AUDIO_LEVEL, listener);
  40. }
  41. Statistics.prototype.removeAudioLevelListener = function(listener)
  42. {
  43. this.eventEmitter.removeListener(StatisticsEvents.AUDIO_LEVEL, listener);
  44. }
  45. Statistics.prototype.dispose = function () {
  46. Statistics.stopAllLocalStats();
  47. this.stopRemote();
  48. if(this.eventEmitter)
  49. this.eventEmitter.removeAllListeners();
  50. if(eventEmitter)
  51. eventEmitter.removeAllListeners();
  52. }
  53. Statistics.stopAllLocalStats = function () {
  54. for(var i = 0; i < this.localStats.length; i++)
  55. this.localStats[i].stop();
  56. this.localStats = [];
  57. }
  58. Statistics.stopLocalStats = function (stream) {
  59. for(var i = 0; i < Statistics.localStats.length; i++)
  60. if(Statistics.localStats[i].stream === stream){
  61. var localStats = Statistics.localStats.splice(i, 1);
  62. localStats.stop();
  63. break;
  64. }
  65. }
  66. Statistics.prototype.stopRemote = function () {
  67. if (this.rtpStats) {
  68. this.rtpStats.stop();
  69. this.eventEmitter.emit(StatisticsEvents.STOP);
  70. this.rtpStats = null;
  71. }
  72. };
  73. /**
  74. * Obtains audio level reported in the stats for specified peer.
  75. * @param peerJid full MUC jid of the user for whom we want to obtain last
  76. * audio level.
  77. * @param ssrc the SSRC of audio stream for which we want to obtain audio
  78. * level.
  79. * @returns {*} a float form 0 to 1 that represents current audio level or
  80. * <tt>null</tt> if for any reason the value is not available
  81. * at this time.
  82. */
  83. Statistics.prototype.getPeerSSRCAudioLevel = function (peerJid, ssrc) {
  84. var peerStats = this.rtpStats.jid2stats[peerJid];
  85. return peerStats ? peerStats.ssrc2AudioLevel[ssrc] : null;
  86. };
  87. Statistics.LOCAL_JID = require("../../service/statistics/constants").LOCAL_JID;
  88. //
  89. //var statistics = {
  90. // /**
  91. // * Indicates that this audio level is for local jid.
  92. // * @type {string}
  93. // */
  94. // LOCAL_JID: 'local',
  95. //
  96. // addConnectionStatsListener: function(listener)
  97. // {
  98. // eventEmitter.on("statistics.connectionstats", listener);
  99. // },
  100. //
  101. // removeConnectionStatsListener: function(listener)
  102. // {
  103. // eventEmitter.removeListener("statistics.connectionstats", listener);
  104. // },
  105. //
  106. //
  107. // addRemoteStatsStopListener: function(listener)
  108. // {
  109. // eventEmitter.on("statistics.stop", listener);
  110. // },
  111. //
  112. // removeRemoteStatsStopListener: function(listener)
  113. // {
  114. // eventEmitter.removeListener("statistics.stop", listener);
  115. // },
  116. //
  117. //
  118. // stopRemoteStatistics: function()
  119. // {
  120. // stopRemote();
  121. // },
  122. //
  123. //// Already implemented with the constructor
  124. // start: function () {
  125. // APP.RTC.addStreamListener(onStreamCreated,
  126. // StreamEventTypes.EVENT_TYPE_LOCAL_CREATED);
  127. // APP.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE, onDisposeConference);
  128. // //FIXME: we may want to change CALL INCOMING event to onnegotiationneeded
  129. // APP.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  130. // startRemoteStats(event.peerconnection);
  131. //// CallStats.init(event);
  132. // });
  133. //// APP.xmpp.addListener(XMPPEvents.PEERCONNECTION_READY, function (session) {
  134. //// CallStats.init(session);
  135. //// });
  136. // //FIXME: that event is changed to TRACK_MUTE_CHANGED
  137. //// APP.RTC.addListener(RTCEvents.AUDIO_MUTE, function (mute) {
  138. //// CallStats.sendMuteEvent(mute, "audio");
  139. //// });
  140. //// APP.xmpp.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  141. //// CallStats.sendSetupFailedEvent();
  142. //// });
  143. // //FIXME: that event is changed to TRACK_MUTE_CHANGED
  144. //// APP.RTC.addListener(RTCEvents.VIDEO_MUTE, function (mute) {
  145. //// CallStats.sendMuteEvent(mute, "video");
  146. //// });
  147. // }
  148. //};
  149. module.exports = Statistics;