2015-07-28 17:49:58 +00:00
|
|
|
/* global require, APP */
|
2015-06-18 16:59:41 +00:00
|
|
|
/**
|
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");
|
2015-01-28 14:35:22 +00:00
|
|
|
var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
|
|
|
|
var XMPPEvents = require("../../service/xmpp/XMPPEvents");
|
2015-07-10 20:39:09 +00:00
|
|
|
var CallStats = require("./CallStats");
|
2015-06-26 12:32:40 +00:00
|
|
|
var RTCEvents = require("../../service/RTC/RTCEvents");
|
2015-09-15 20:34:16 +00:00
|
|
|
var StatisticsEvents = require("../../service/statistics/Events");
|
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();
|
2015-09-15 20:34:16 +00:00
|
|
|
eventEmitter.emit(StatisticsEvents.STOP);
|
2014-12-17 16:21:25 +00:00
|
|
|
rtpStats = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function startRemoteStats (peerconnection) {
|
2015-07-28 17:49:58 +00:00
|
|
|
if (rtpStats) {
|
2015-02-09 12:51:25 +00:00
|
|
|
rtpStats.stop();
|
2014-12-17 16:21:25 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 12:51: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
|
|
|
|
2015-02-11 16:29:20 +00:00
|
|
|
localStats = new LocalStats(stream.getOriginalStream(), 200, statistics,
|
2014-12-19 13:59:08 +00:00
|
|
|
eventEmitter);
|
|
|
|
localStats.start();
|
|
|
|
}
|
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
function onDisposeConference(onUnload) {
|
2015-06-26 12:32:40 +00:00
|
|
|
CallStats.sendTerminateEvent();
|
2015-01-19 09:20:00 +00:00
|
|
|
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',
|
|
|
|
|
2015-09-15 20:34:16 +00:00
|
|
|
addListener: function(type, listener) {
|
|
|
|
eventEmitter.on(type, listener);
|
2014-12-17 16:21:25 +00:00
|
|
|
},
|
2015-09-15 20:34:16 +00:00
|
|
|
removeListener: function (type, listener) {
|
|
|
|
eventEmitter.removeListener(type, listener);
|
2014-12-17 16:21:25 +00:00
|
|
|
},
|
|
|
|
stop: function () {
|
|
|
|
stopLocal();
|
|
|
|
stopRemote();
|
|
|
|
if(eventEmitter)
|
|
|
|
{
|
|
|
|
eventEmitter.removeAllListeners();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
stopRemoteStatistics: function()
|
|
|
|
{
|
|
|
|
stopRemote();
|
|
|
|
},
|
2014-12-19 13:59:08 +00:00
|
|
|
start: function () {
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.RTC.addStreamListener(onStreamCreated,
|
2014-12-19 13:59:08 +00:00
|
|
|
StreamEventTypes.EVENT_TYPE_LOCAL_CREATED);
|
2015-09-11 02:42:15 +00:00
|
|
|
APP.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
|
|
|
|
onDisposeConference);
|
|
|
|
//FIXME: we may want to change CALL INCOMING event to
|
|
|
|
// onnegotiationneeded
|
2015-01-28 14:35:22 +00:00
|
|
|
APP.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
|
2015-01-24 14:28:02 +00:00
|
|
|
startRemoteStats(event.peerconnection);
|
2015-06-26 12:32:40 +00:00
|
|
|
// CallStats.init(event);
|
|
|
|
});
|
2015-09-11 02:42:15 +00:00
|
|
|
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");
|
2015-01-24 14:28:02 +00:00
|
|
|
});
|
2015-11-05 16:53:15 +00:00
|
|
|
|
|
|
|
APP.RTC.addListener(RTCEvents.GET_USER_MEDIA_FAILED, function (e) {
|
|
|
|
CallStats.sendGetUserMediaFailed(e);
|
|
|
|
});
|
2015-11-13 15:58:59 +00:00
|
|
|
APP.xmpp.addListener(RTCEvents.CREATE_OFFER_FAILED, function (e, pc) {
|
|
|
|
CallStats.sendCreateOfferFailed(e, pc);
|
2015-11-05 16:53:15 +00:00
|
|
|
});
|
2015-11-13 15:58:59 +00:00
|
|
|
APP.xmpp.addListener(RTCEvents.CREATE_ANSWER_FAILED, function (e, pc) {
|
|
|
|
CallStats.sendCreateAnswerFailed(e, pc);
|
2015-11-05 16:53:15 +00:00
|
|
|
});
|
2015-11-10 22:40:25 +00:00
|
|
|
APP.xmpp.addListener(
|
|
|
|
RTCEvents.SET_LOCAL_DESCRIPTION_FAILED,
|
2015-11-13 15:58:59 +00:00
|
|
|
function (e, pc) {
|
|
|
|
CallStats.sendSetLocalDescFailed(e, pc);
|
|
|
|
}
|
|
|
|
);
|
2015-11-10 22:40:25 +00:00
|
|
|
APP.xmpp.addListener(
|
|
|
|
RTCEvents.SET_REMOTE_DESCRIPTION_FAILED,
|
2015-11-13 15:58:59 +00:00
|
|
|
function (e, pc) {
|
|
|
|
CallStats.sendSetRemoteDescFailed(e, pc);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
APP.xmpp.addListener(
|
|
|
|
RTCEvents.ADD_ICE_CANDIDATE_FAILED,
|
|
|
|
function (e, pc) {
|
|
|
|
CallStats.sendAddIceCandidateFailed(e, pc);
|
|
|
|
}
|
|
|
|
);
|
2015-11-25 22:05:58 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Obtains audio level reported in the stats for specified peer.
|
|
|
|
* @param peerJid full MUC jid of the user for whom we want to obtain last
|
|
|
|
* audio level.
|
|
|
|
* @param ssrc the SSRC of audio stream for which we want to obtain audio
|
|
|
|
* level.
|
|
|
|
* @returns {*} a float form 0 to 1 that represents current audio level or
|
|
|
|
* <tt>null</tt> if for any reason the value is not available
|
|
|
|
* at this time.
|
|
|
|
*/
|
|
|
|
getPeerSSRCAudioLevel: function (peerJid, ssrc) {
|
|
|
|
|
|
|
|
var peerStats = rtpStats.jid2stats[peerJid];
|
|
|
|
|
|
|
|
return peerStats ? peerStats.ssrc2AudioLevel[ssrc] : null;
|
2014-12-17 16:21:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = statistics;
|