/* * Copyright @ 2015 Atlassian Pty Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Created by hristo on 8/4/14. */ var LocalStats = require("./LocalStatsCollector.js"); var RTPStats = require("./RTPStatsCollector.js"); var EventEmitter = require("events"); var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js"); var XMPPEvents = require("../../service/xmpp/XMPPEvents"); var eventEmitter = new EventEmitter(); var localStats = null; var rtpStats = null; function stopLocal() { if(localStats) { localStats.stop(); localStats = null; } } function stopRemote() { if(rtpStats) { rtpStats.stop(); eventEmitter.emit("statistics.stop"); rtpStats = null; } } function startRemoteStats (peerconnection) { if(rtpStats) { rtpStats.stop(); rtpStats = null; } rtpStats = new RTPStats(peerconnection, 200, 2000, eventEmitter); rtpStats.start(); } function onStreamCreated(stream) { if(stream.getOriginalStream().getAudioTracks().length === 0) return; localStats = new LocalStats(stream.getOriginalStream(), 200, statistics, eventEmitter); localStats.start(); } function onDisposeConference(onUnload) { stopRemote(); if(onUnload) { stopLocal(); eventEmitter.removeAllListeners(); } } var statistics = { /** * Indicates that this audio level is for local jid. * @type {string} */ LOCAL_JID: 'local', addAudioLevelListener: function(listener) { eventEmitter.on("statistics.audioLevel", listener); }, removeAudioLevelListener: function(listener) { eventEmitter.removeListener("statistics.audioLevel", listener); }, addConnectionStatsListener: function(listener) { eventEmitter.on("statistics.connectionstats", listener); }, removeConnectionStatsListener: function(listener) { eventEmitter.removeListener("statistics.connectionstats", listener); }, addRemoteStatsStopListener: function(listener) { eventEmitter.on("statistics.stop", listener); }, removeRemoteStatsStopListener: function(listener) { eventEmitter.removeListener("statistics.stop", listener); }, stop: function () { stopLocal(); stopRemote(); if(eventEmitter) { eventEmitter.removeAllListeners(); } }, stopRemoteStatistics: function() { stopRemote(); }, start: function () { APP.RTC.addStreamListener(onStreamCreated, StreamEventTypes.EVENT_TYPE_LOCAL_CREATED); APP.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE, onDisposeConference); APP.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) { startRemoteStats(event.peerconnection); }); } }; module.exports = statistics;