您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

statistics.js 3.6KB

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