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

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