jiti-meet/modules/statistics/statistics.js

144 lines
3.6 KiB
JavaScript
Raw Normal View History

2015-07-28 17:49:58 +00:00
/* global require, APP */
/**
2014-12-17 16:21:25 +00:00
* 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 CallStats = require("./CallStats");
2015-06-26 12:32:40 +00:00
var RTCEvents = require("../../service/RTC/RTCEvents");
2014-12-17 16:21:25 +00:00
var eventEmitter = new EventEmitter();
var localStats = null;
var rtpStats = null;
2015-07-28 17:49:58 +00:00
function stopLocal() {
if (localStats) {
2014-12-17 16:21:25 +00:00
localStats.stop();
localStats = null;
}
}
2015-07-28 17:49:58 +00:00
function stopRemote() {
if (rtpStats) {
2014-12-17 16:21:25 +00:00
rtpStats.stop();
eventEmitter.emit("statistics.stop");
rtpStats = null;
}
}
function startRemoteStats (peerconnection) {
2015-07-28 17:49:58 +00:00
if (rtpStats) {
rtpStats.stop();
2014-12-17 16:21:25 +00:00
}
rtpStats = new RTPStats(peerconnection, 200, 2000, eventEmitter);
rtpStats.start();
2014-12-17 16:21:25 +00:00
}
2015-07-28 17:49:58 +00:00
function onStreamCreated(stream) {
if(stream.getOriginalStream().getAudioTracks().length === 0) {
2014-12-19 13:59:08 +00:00
return;
2015-07-28 17:49:58 +00:00
}
2014-12-19 13:59:08 +00:00
localStats = new LocalStats(stream.getOriginalStream(), 200, statistics,
2014-12-19 13:59:08 +00:00
eventEmitter);
localStats.start();
}
function onDisposeConference(onUnload) {
2015-06-26 12:32:40 +00:00
CallStats.sendTerminateEvent();
stopRemote();
if(onUnload) {
stopLocal();
eventEmitter.removeAllListeners();
}
}
2015-07-28 17:49:58 +00:00
var statistics = {
2014-12-17 16:21:25 +00:00
/**
* 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();
},
2014-12-19 13:59:08 +00:00
start: function () {
APP.RTC.addStreamListener(onStreamCreated,
2014-12-19 13:59:08 +00:00
StreamEventTypes.EVENT_TYPE_LOCAL_CREATED);
APP.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
onDisposeConference);
//FIXME: we may want to change CALL INCOMING event to
// onnegotiationneeded
APP.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
startRemoteStats(event.peerconnection);
2015-06-26 12:32:40 +00:00
// CallStats.init(event);
});
APP.xmpp.addListener(XMPPEvents.PEERCONNECTION_READY,
function (session) {
2015-06-26 12:32:40 +00:00
CallStats.init(session);
2015-07-28 17:49:58 +00:00
});
2015-06-26 12:32:40 +00:00
APP.RTC.addListener(RTCEvents.AUDIO_MUTE, function (mute) {
CallStats.sendMuteEvent(mute, "audio");
});
APP.xmpp.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
CallStats.sendSetupFailedEvent();
2015-07-28 17:49:58 +00:00
});
2015-06-26 12:32:40 +00:00
APP.RTC.addListener(RTCEvents.VIDEO_MUTE, function (mute) {
CallStats.sendMuteEvent(mute, "video");
});
2014-12-17 16:21:25 +00:00
}
};
module.exports = statistics;