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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright @ 2015 Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * Created by hristo on 8/4/14.
  17. */
  18. var LocalStats = require("./LocalStatsCollector.js");
  19. var RTPStats = require("./RTPStatsCollector.js");
  20. var EventEmitter = require("events");
  21. var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  22. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  23. var eventEmitter = new EventEmitter();
  24. var localStats = null;
  25. var rtpStats = null;
  26. function stopLocal()
  27. {
  28. if(localStats)
  29. {
  30. localStats.stop();
  31. localStats = null;
  32. }
  33. }
  34. function stopRemote()
  35. {
  36. if(rtpStats)
  37. {
  38. rtpStats.stop();
  39. eventEmitter.emit("statistics.stop");
  40. rtpStats = null;
  41. }
  42. }
  43. function startRemoteStats (peerconnection) {
  44. if(rtpStats)
  45. {
  46. rtpStats.stop();
  47. rtpStats = null;
  48. }
  49. rtpStats = new RTPStats(peerconnection, 200, 2000, eventEmitter);
  50. rtpStats.start();
  51. }
  52. function onStreamCreated(stream)
  53. {
  54. if(stream.getOriginalStream().getAudioTracks().length === 0)
  55. return;
  56. localStats = new LocalStats(stream.getOriginalStream(), 200, statistics,
  57. eventEmitter);
  58. localStats.start();
  59. }
  60. function onDisposeConference(onUnload) {
  61. stopRemote();
  62. if(onUnload) {
  63. stopLocal();
  64. eventEmitter.removeAllListeners();
  65. }
  66. }
  67. var statistics =
  68. {
  69. /**
  70. * Indicates that this audio level is for local jid.
  71. * @type {string}
  72. */
  73. LOCAL_JID: 'local',
  74. addAudioLevelListener: function(listener)
  75. {
  76. eventEmitter.on("statistics.audioLevel", listener);
  77. },
  78. removeAudioLevelListener: function(listener)
  79. {
  80. eventEmitter.removeListener("statistics.audioLevel", listener);
  81. },
  82. addConnectionStatsListener: function(listener)
  83. {
  84. eventEmitter.on("statistics.connectionstats", listener);
  85. },
  86. removeConnectionStatsListener: function(listener)
  87. {
  88. eventEmitter.removeListener("statistics.connectionstats", listener);
  89. },
  90. addRemoteStatsStopListener: function(listener)
  91. {
  92. eventEmitter.on("statistics.stop", listener);
  93. },
  94. removeRemoteStatsStopListener: function(listener)
  95. {
  96. eventEmitter.removeListener("statistics.stop", listener);
  97. },
  98. stop: function () {
  99. stopLocal();
  100. stopRemote();
  101. if(eventEmitter)
  102. {
  103. eventEmitter.removeAllListeners();
  104. }
  105. },
  106. stopRemoteStatistics: function()
  107. {
  108. stopRemote();
  109. },
  110. start: function () {
  111. APP.RTC.addStreamListener(onStreamCreated,
  112. StreamEventTypes.EVENT_TYPE_LOCAL_CREATED);
  113. APP.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE, onDisposeConference);
  114. APP.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  115. startRemoteStats(event.peerconnection);
  116. });
  117. }
  118. };
  119. module.exports = statistics;