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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * Created by hristo on 8/4/14.
  3. */
  4. var LocalStats = require("./LocalStatsCollector.js");
  5. var RTPStats = require("./RTPStatsCollector.js");
  6. var EventEmitter = require("events");
  7. var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  8. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  9. var eventEmitter = new EventEmitter();
  10. var localStats = null;
  11. var rtpStats = null;
  12. function stopLocal()
  13. {
  14. if(localStats)
  15. {
  16. localStats.stop();
  17. localStats = null;
  18. }
  19. }
  20. function stopRemote()
  21. {
  22. if(rtpStats)
  23. {
  24. rtpStats.stop();
  25. eventEmitter.emit("statistics.stop");
  26. rtpStats = null;
  27. }
  28. }
  29. function startRemoteStats (peerconnection) {
  30. if(rtpStats)
  31. {
  32. rtpStats.stop();
  33. rtpStats = null;
  34. }
  35. rtpStats = new RTPStats(peerconnection, 200, 2000, eventEmitter);
  36. rtpStats.start();
  37. }
  38. function onStreamCreated(stream)
  39. {
  40. if(stream.getOriginalStream().getAudioTracks().length === 0)
  41. return;
  42. localStats = new LocalStats(stream.getOriginalStream(), 200, statistics,
  43. eventEmitter);
  44. localStats.start();
  45. }
  46. function onDisposeConference(onUnload) {
  47. stopRemote();
  48. if(onUnload) {
  49. stopLocal();
  50. eventEmitter.removeAllListeners();
  51. }
  52. }
  53. var statistics =
  54. {
  55. /**
  56. * Indicates that this audio level is for local jid.
  57. * @type {string}
  58. */
  59. LOCAL_JID: 'local',
  60. addAudioLevelListener: function(listener)
  61. {
  62. eventEmitter.on("statistics.audioLevel", listener);
  63. },
  64. removeAudioLevelListener: function(listener)
  65. {
  66. eventEmitter.removeListener("statistics.audioLevel", listener);
  67. },
  68. addConnectionStatsListener: function(listener)
  69. {
  70. eventEmitter.on("statistics.connectionstats", listener);
  71. },
  72. removeConnectionStatsListener: function(listener)
  73. {
  74. eventEmitter.removeListener("statistics.connectionstats", listener);
  75. },
  76. addRemoteStatsStopListener: function(listener)
  77. {
  78. eventEmitter.on("statistics.stop", listener);
  79. },
  80. removeRemoteStatsStopListener: function(listener)
  81. {
  82. eventEmitter.removeListener("statistics.stop", listener);
  83. },
  84. stop: function () {
  85. stopLocal();
  86. stopRemote();
  87. if(eventEmitter)
  88. {
  89. eventEmitter.removeAllListeners();
  90. }
  91. },
  92. stopRemoteStatistics: function()
  93. {
  94. stopRemote();
  95. },
  96. start: function () {
  97. APP.RTC.addStreamListener(onStreamCreated,
  98. StreamEventTypes.EVENT_TYPE_LOCAL_CREATED);
  99. APP.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE, onDisposeConference);
  100. APP.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  101. startRemoteStats(event.peerconnection);
  102. });
  103. }
  104. };
  105. module.exports = statistics;