123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742 |
- import EventEmitter from 'events';
-
- import { FEEDBACK } from '../../service/statistics/AnalyticsEvents';
- import analytics from './AnalyticsAdapter';
- import CallStats from './CallStats';
- import LocalStats from './LocalStatsCollector';
- import RTPStats from './RTPStatsCollector';
-
- import browser from '../browser';
- import Settings from '../settings/Settings';
- import ScriptUtil from '../util/ScriptUtil';
- import JitsiTrackError from '../../JitsiTrackError';
- import * as StatisticsEvents from '../../service/statistics/Events';
-
- const logger = require('jitsi-meet-logger').getLogger(__filename);
-
- /**
- * Stores all active {@link Statistics} instances.
- * @type {Set<Statistics>}
- */
- let _instances;
-
- /**
- * True if callstats API is loaded
- */
- let isCallstatsLoaded = false;
-
- /**
- * Since callstats.io is a third party, we cannot guarantee the quality of their
- * service. More specifically, their server may take noticeably long time to
- * respond. Consequently, it is in our best interest (in the sense that the
- * intergration of callstats.io is pretty important to us but not enough to
- * allow it to prevent people from joining a conference) to (1) start
- * downloading their API as soon as possible and (2) do the downloading
- * asynchronously.
- *
- * @param {StatisticsOptions} options - Options to use for downloading and
- * initializing callstats backend.
- */
- function loadCallStatsAPI(options) {
- if (!isCallstatsLoaded) {
- ScriptUtil.loadScript(
- options.customScriptUrl
- || 'https://api.callstats.io/static/callstats-ws.min.js',
- /* async */ true,
- /* prepend */ true,
- /* relativeURL */ undefined,
- /* loadCallback */ () => _initCallStatsBackend(options)
- );
- isCallstatsLoaded = true;
- }
- }
-
- /**
- * Initializes Callstats backend.
- *
- * @param {StatisticsOptions} options - The options to use for initializing
- * callstats backend.
- * @private
- */
- function _initCallStatsBackend(options) {
- if (CallStats.isBackendInitialized()) {
- return;
- }
-
- const userName = Settings.callStatsUserName;
-
- if (!CallStats.initBackend({
- callStatsID: options.callStatsID,
- callStatsSecret: options.callStatsSecret,
- userName: options.swapUserNameAndAlias
- ? options.callStatsAliasName : userName,
- aliasName: options.swapUserNameAndAlias
- ? userName : options.callStatsAliasName,
- applicationName: options.applicationName,
- getWiFiStatsMethod: options.getWiFiStatsMethod,
- confID: options.confID
- })) {
- logger.error('CallStats Backend initialization failed bad');
- }
- }
-
- /**
- * callstats strips any additional fields from Error except for "name", "stack",
- * "message" and "constraintName". So we need to bundle additional information
- * from JitsiTrackError into error passed to callstats to preserve valuable
- * information about error.
- * @param {JitsiTrackError} error
- */
- function formatJitsiTrackErrorForCallStats(error) {
- const err = new Error();
-
- // Just copy original stack from error
- err.stack = error.stack;
-
- // Combine name from error's name plus (possibly) name of original GUM error
- err.name = (error.name || 'Unknown error') + (error.gum && error.gum.error
- && error.gum.error.name ? ` - ${error.gum.error.name}` : '');
-
- // Put all constraints into this field. For constraint failed errors we will
- // still know which exactly constraint failed as it will be a part of
- // message.
- err.constraintName = error.gum && error.gum.constraints
- ? JSON.stringify(error.gum.constraints) : '';
-
- // Just copy error's message.
- err.message = error.message;
-
- return err;
- }
-
- /**
- * Init statistic options
- * @param options
- */
- Statistics.init = function(options) {
- Statistics.audioLevelsEnabled = !options.disableAudioLevels;
-
- if (typeof options.audioLevelsInterval === 'number') {
- Statistics.audioLevelsInterval = options.audioLevelsInterval;
- }
-
- Statistics.disableThirdPartyRequests = options.disableThirdPartyRequests;
- };
-
- /**
- * The options to configure Statistics.
- * @typedef {Object} StatisticsOptions
- * @property {string} applicationName - The application name to pass to
- * callstats.
- * @property {string} callStatsAliasName - The alias name to use when
- * initializing callstats.
- * @property {string} confID - The callstats conference ID to use.
- * @property {string} callStatsID - Callstats credentials - the id.
- * @property {string} callStatsSecret - Callstats credentials - the secret.
- * @property {string} customScriptUrl - A custom lib url to use when downloading
- * callstats library.
- * @property {boolean} swapUserNameAndAlias - Whether to swap the places of
- * username and alias when initiating callstats.
- */
- /**
- *
- * @param xmpp
- * @param {StatisticsOptions} options - The options to use creating the
- * Statistics.
- */
- export default function Statistics(xmpp, options) {
- /**
- * {@link RTPStats} mapped by {@link TraceablePeerConnection.id} which
- * collect RTP statistics for each peerconnection.
- * @type {Map<string, RTPStats}
- */
- this.rtpStatsMap = new Map();
- this.eventEmitter = new EventEmitter();
- this.xmpp = xmpp;
- this.options = options || {};
-
- this.callStatsIntegrationEnabled
- = this.options.callStatsID && this.options.callStatsSecret
-
- // Even though AppID and AppSecret may be specified, the integration
- // of callstats.io may be disabled because of globally-disallowed
- // requests to any third parties.
- && (Statistics.disableThirdPartyRequests !== true);
- if (this.callStatsIntegrationEnabled) {
- if (browser.isReactNative()) {
- _initCallStatsBackend(this.options);
- } else {
- loadCallStatsAPI(this.options);
- }
-
- if (!this.options.confID) {
- logger.warn('"confID" is not defined');
- }
- }
-
- /**
- * Stores {@link CallStats} instances for each
- * {@link TraceablePeerConnection} (one {@link CallStats} instance serves
- * one TPC). The instances are mapped by {@link TraceablePeerConnection.id}.
- * @type {Map<number, CallStats>}
- */
- this.callsStatsInstances = new Map();
-
- Statistics.instances.add(this);
- }
- Statistics.audioLevelsEnabled = false;
- Statistics.audioLevelsInterval = 200;
- Statistics.disableThirdPartyRequests = false;
- Statistics.analytics = analytics;
-
- Object.defineProperty(Statistics, 'instances', {
- /**
- * Returns the Set holding all active {@link Statistics} instances. Lazily
- * initializes the Set to allow any Set polyfills to be applied.
- * @type {Set<Statistics>}
- */
- get() {
- if (!_instances) {
- _instances = new Set();
- }
-
- return _instances;
- }
- });
-
- /**
- * Starts collecting RTP stats for given peerconnection.
- * @param {TraceablePeerConnection} peerconnection
- */
- Statistics.prototype.startRemoteStats = function(peerconnection) {
- this.stopRemoteStats(peerconnection);
-
- try {
- const rtpStats
- = new RTPStats(
- peerconnection,
- Statistics.audioLevelsInterval,
- 2000,
- this.eventEmitter);
-
- rtpStats.start(Statistics.audioLevelsEnabled);
- this.rtpStatsMap.set(peerconnection.id, rtpStats);
- } catch (e) {
- logger.error(`Failed to start collecting remote statistics: ${e}`);
- }
- };
-
- Statistics.localStats = [];
-
- Statistics.startLocalStats = function(stream, callback) {
- if (!Statistics.audioLevelsEnabled) {
- return;
- }
- const localStats = new LocalStats(stream, Statistics.audioLevelsInterval,
- callback);
-
- this.localStats.push(localStats);
- localStats.start();
- };
-
- Statistics.prototype.addAudioLevelListener = function(listener) {
- if (!Statistics.audioLevelsEnabled) {
- return;
- }
- this.eventEmitter.on(StatisticsEvents.AUDIO_LEVEL, listener);
- };
-
- Statistics.prototype.removeAudioLevelListener = function(listener) {
- if (!Statistics.audioLevelsEnabled) {
- return;
- }
- this.eventEmitter.removeListener(StatisticsEvents.AUDIO_LEVEL, listener);
- };
-
- Statistics.prototype.addBeforeDisposedListener = function(listener) {
- this.eventEmitter.on(StatisticsEvents.BEFORE_DISPOSED, listener);
- };
-
- Statistics.prototype.removeBeforeDisposedListener = function(listener) {
- this.eventEmitter.removeListener(
- StatisticsEvents.BEFORE_DISPOSED, listener);
- };
-
- Statistics.prototype.addConnectionStatsListener = function(listener) {
- this.eventEmitter.on(StatisticsEvents.CONNECTION_STATS, listener);
- };
-
- Statistics.prototype.removeConnectionStatsListener = function(listener) {
- this.eventEmitter.removeListener(
- StatisticsEvents.CONNECTION_STATS,
- listener);
- };
-
- Statistics.prototype.addByteSentStatsListener = function(listener) {
- this.eventEmitter.on(StatisticsEvents.BYTE_SENT_STATS, listener);
- };
-
- Statistics.prototype.removeByteSentStatsListener = function(listener) {
- this.eventEmitter.removeListener(StatisticsEvents.BYTE_SENT_STATS,
- listener);
- };
-
- Statistics.prototype.dispose = function() {
- try {
- // NOTE Before reading this please see the comment in stopCallStats...
- //
- // Here we prevent from emitting the event twice in case it will be
- // triggered from stopCallStats.
- // If the event is triggered from here it means that the logs will not
- // be submitted anyway (because there is no CallStats instance), but
- // we're doing that for the sake of some kind of consistency.
- if (!this.callsStatsInstances.size) {
- this.eventEmitter.emit(StatisticsEvents.BEFORE_DISPOSED);
- }
- for (const callStats of this.callsStatsInstances.values()) {
- this.stopCallStats(callStats.tpc);
- }
- for (const tpcId of this.rtpStatsMap.keys()) {
- this._stopRemoteStats(tpcId);
- }
- if (this.eventEmitter) {
- this.eventEmitter.removeAllListeners();
- }
- } finally {
- Statistics.instances.delete(this);
- }
- };
-
- Statistics.stopLocalStats = function(stream) {
- if (!Statistics.audioLevelsEnabled) {
- return;
- }
-
- for (let i = 0; i < Statistics.localStats.length; i++) {
- if (Statistics.localStats[i].stream === stream) {
- const localStats = Statistics.localStats.splice(i, 1);
-
- localStats[0].stop();
- break;
- }
- }
- };
-
- /**
- * Stops remote RTP stats for given peerconnection ID.
- * @param {string} tpcId {@link TraceablePeerConnection.id}
- * @private
- */
- Statistics.prototype._stopRemoteStats = function(tpcId) {
- const rtpStats = this.rtpStatsMap.get(tpcId);
-
- if (rtpStats) {
- rtpStats.stop();
- this.rtpStatsMap.delete(tpcId);
- }
- };
-
- /**
- * Stops collecting RTP stats for given peerconnection
- * @param {TraceablePeerConnection} tpc
- */
- Statistics.prototype.stopRemoteStats = function(tpc) {
- this._stopRemoteStats(tpc.id);
- };
-
- // CALSTATS METHODS
-
- /**
- * Initializes the callstats.io API.
- * @param {TraceablePeerConnection} tpc the {@link TraceablePeerConnection}
- * instance for which CalStats will be started.
- * @param {string} remoteUserID
- */
- Statistics.prototype.startCallStats = function(tpc, remoteUserID) {
- if (!this.callStatsIntegrationEnabled) {
- return;
- } else if (this.callsStatsInstances.has(tpc.id)) {
- logger.error('CallStats instance for ${tpc} exists already');
-
- return;
- }
-
- logger.info(`Starting CallStats for ${tpc}...`);
-
- const newInstance
- = new CallStats(
- tpc,
- {
- confID: this.options.confID,
- remoteUserID
- });
-
- this.callsStatsInstances.set(tpc.id, newInstance);
- };
-
- /**
- * Obtains the list of *all* {@link CallStats} instances collected from every
- * valid {@link Statistics} instance.
- * @return {Set<CallStats>}
- * @private
- */
- Statistics._getAllCallStatsInstances = function() {
- const csInstances = new Set();
-
- for (const statistics of Statistics.instances) {
- for (const cs of statistics.callsStatsInstances.values()) {
- csInstances.add(cs);
- }
- }
-
- return csInstances;
- };
-
- /**
- * Removes the callstats.io instances.
- */
- Statistics.prototype.stopCallStats = function(tpc) {
- const callStatsInstance = this.callsStatsInstances.get(tpc.id);
-
- if (callStatsInstance) {
- // FIXME the original purpose of adding BEFORE_DISPOSED event was to be
- // able to submit the last log batch from jitsi-meet to CallStats. After
- // recent changes we dispose the CallStats earlier
- // (before Statistics.dispose), so we need to emit this event here to
- // give this last chance for final log batch submission.
- //
- // Eventually there should be a separate module called "log storage"
- // which should emit proper events when it's underlying
- // CallStats instance is going away.
- if (this.callsStatsInstances.size === 1) {
- this.eventEmitter.emit(StatisticsEvents.BEFORE_DISPOSED);
- }
- this.callsStatsInstances.delete(tpc.id);
-
- // The fabric needs to be terminated when being stopped
- callStatsInstance.sendTerminateEvent();
- }
- };
-
- /**
- * Returns true if the callstats integration is enabled, otherwise returns
- * false.
- *
- * @returns true if the callstats integration is enabled, otherwise returns
- * false.
- */
- Statistics.prototype.isCallstatsEnabled = function() {
- return this.callStatsIntegrationEnabled;
- };
-
- /**
- * Logs either resume or hold event for the given peer connection.
- * @param {TraceablePeerConnection} tpc the connection for which event will be
- * reported
- * @param {boolean} isResume true for resume or false for hold
- */
- Statistics.prototype.sendConnectionResumeOrHoldEvent = function(tpc, isResume) {
- const instance = this.callsStatsInstances.get(tpc.id);
-
- if (instance) {
- instance.sendResumeOrHoldEvent(isResume);
- }
- };
-
- /**
- * Notifies CallStats and analytics (if present) for ice connection failed
- * @param {TraceablePeerConnection} tpc connection on which failure occurred.
- */
- Statistics.prototype.sendIceConnectionFailedEvent = function(tpc) {
- const instance = this.callsStatsInstances.get(tpc.id);
-
- if (instance) {
- instance.sendIceConnectionFailedEvent();
- }
- };
-
- /**
- * Notifies CallStats for mute events
- * @param {TraceablePeerConnection} tpc connection on which failure occurred.
- * @param {boolean} muted true for muted and false for not muted
- * @param {String} type "audio"/"video"
- */
- Statistics.prototype.sendMuteEvent = function(tpc, muted, type) {
- const instance = tpc && this.callsStatsInstances.get(tpc.id);
-
- CallStats.sendMuteEvent(muted, type, instance);
- };
-
- /**
- * Notifies CallStats for screen sharing events
- * @param start {boolean} true for starting screen sharing and
- * false for not stopping
- * @param {string|null} ssrc - optional ssrc value, used only when
- * starting screen sharing.
- */
- Statistics.prototype.sendScreenSharingEvent
- = function(start, ssrc) {
- for (const cs of this.callsStatsInstances.values()) {
- cs.sendScreenSharingEvent(start, ssrc);
- }
- };
-
- /**
- * Notifies the statistics module that we are now the dominant speaker of the
- * conference.
- * @param {String} roomJid - The room jid where the speaker event occurred.
- */
- Statistics.prototype.sendDominantSpeakerEvent = function(roomJid) {
- for (const cs of this.callsStatsInstances.values()) {
- cs.sendDominantSpeakerEvent();
- }
-
- // xmpp send dominant speaker event
- this.xmpp.sendDominantSpeakerEvent(roomJid);
- };
-
- /**
- * Notifies about active device.
- * @param {{deviceList: {String:String}}} devicesData - list of devices with
- * their data
- */
- Statistics.sendActiveDeviceListEvent = function(devicesData) {
- const globalSet = Statistics._getAllCallStatsInstances();
-
- if (globalSet.size) {
- for (const cs of globalSet) {
- CallStats.sendActiveDeviceListEvent(devicesData, cs);
- }
- } else {
- CallStats.sendActiveDeviceListEvent(devicesData, null);
- }
- };
-
- /* eslint-disable max-params */
-
- /**
- * Lets the underlying statistics module know where is given SSRC rendered by
- * providing renderer tag ID.
- * @param {TraceablePeerConnection} tpc the connection to which the stream
- * belongs to
- * @param {number} ssrc the SSRC of the stream
- * @param {boolean} isLocal
- * @param {string} userId
- * @param {string} usageLabel meaningful usage label of this stream like
- * 'microphone', 'camera' or 'screen'.
- * @param {string} containerId the id of media 'audio' or 'video' tag which
- * renders the stream.
- */
- Statistics.prototype.associateStreamWithVideoTag = function(
- tpc,
- ssrc,
- isLocal,
- userId,
- usageLabel,
- containerId) {
- const instance = this.callsStatsInstances.get(tpc.id);
-
- if (instance) {
- instance.associateStreamWithVideoTag(
- ssrc,
- isLocal,
- userId,
- usageLabel,
- containerId);
- }
- };
-
- /* eslint-enable max-params */
-
- /**
- * Notifies CallStats that getUserMedia failed.
- *
- * @param {Error} e error to send
- */
- Statistics.sendGetUserMediaFailed = function(e) {
- const error
- = e instanceof JitsiTrackError
- ? formatJitsiTrackErrorForCallStats(e) : e;
- const globalSet = Statistics._getAllCallStatsInstances();
-
- if (globalSet.size) {
- for (const cs of globalSet) {
- CallStats.sendGetUserMediaFailed(error, cs);
- }
- } else {
- CallStats.sendGetUserMediaFailed(error, null);
- }
- };
-
- /**
- * Notifies CallStats that peer connection failed to create offer.
- *
- * @param {Error} e error to send
- * @param {TraceablePeerConnection} tpc connection on which failure occurred.
- */
- Statistics.prototype.sendCreateOfferFailed = function(e, tpc) {
- const instance = this.callsStatsInstances.get(tpc.id);
-
- if (instance) {
- instance.sendCreateOfferFailed(e);
- }
- };
-
- /**
- * Notifies CallStats that peer connection failed to create answer.
- *
- * @param {Error} e error to send
- * @param {TraceablePeerConnection} tpc connection on which failure occured.
- */
- Statistics.prototype.sendCreateAnswerFailed = function(e, tpc) {
- const instance = this.callsStatsInstances.get(tpc.id);
-
- if (instance) {
- instance.sendCreateAnswerFailed(e);
- }
- };
-
- /**
- * Notifies CallStats that peer connection failed to set local description.
- *
- * @param {Error} e error to send
- * @param {TraceablePeerConnection} tpc connection on which failure occurred.
- */
- Statistics.prototype.sendSetLocalDescFailed = function(e, tpc) {
- const instance = this.callsStatsInstances.get(tpc.id);
-
- if (instance) {
- instance.sendSetLocalDescFailed(e);
- }
- };
-
- /**
- * Notifies CallStats that peer connection failed to set remote description.
- *
- * @param {Error} e error to send
- * @param {TraceablePeerConnection} tpc connection on which failure occurred.
- */
- Statistics.prototype.sendSetRemoteDescFailed = function(e, tpc) {
- const instance = this.callsStatsInstances.get(tpc.id);
-
- if (instance) {
- instance.sendSetRemoteDescFailed(e);
- }
- };
-
- /**
- * Notifies CallStats that peer connection failed to add ICE candidate.
- *
- * @param {Error} e error to send
- * @param {TraceablePeerConnection} tpc connection on which failure occurred.
- */
- Statistics.prototype.sendAddIceCandidateFailed = function(e, tpc) {
- const instance = this.callsStatsInstances.get(tpc.id);
-
- if (instance) {
- instance.sendAddIceCandidateFailed(e);
- }
- };
-
- /**
- * Adds to CallStats an application log.
- *
- * @param {String} m a log message to send or an {Error} object to be reported
- */
- Statistics.sendLog = function(m) {
- const globalSubSet = new Set();
-
- // FIXME we don't want to duplicate logs over P2P instance, but
- // here we should go over instances and call this method for each
- // unique conference ID rather than selecting the first one.
- // We don't have such use case though, so leaving as is for now.
- for (const stats of Statistics.instances) {
- if (stats.callsStatsInstances.size) {
- globalSubSet.add(stats.callsStatsInstances.values().next().value);
- }
- }
-
- if (globalSubSet.size) {
- for (const csPerStats of globalSubSet) {
- CallStats.sendApplicationLog(m, csPerStats);
- }
- } else {
- CallStats.sendApplicationLog(m, null);
- }
- };
-
- /**
- * Sends the given feedback through CallStats.
- *
- * @param overall an integer between 1 and 5 indicating the user's rating.
- * @param comment the comment from the user.
- */
- Statistics.prototype.sendFeedback = function(overall, comment) {
- CallStats.sendFeedback(this.options.confID, overall, comment);
- Statistics.analytics.sendEvent(
- FEEDBACK,
- {
- rating: overall,
- comment
- });
- };
-
- Statistics.LOCAL_JID = require('../../service/statistics/constants').LOCAL_JID;
-
- /**
- * Reports global error to CallStats.
- *
- * @param {Error} error
- */
- Statistics.reportGlobalError = function(error) {
- if (error instanceof JitsiTrackError && error.gum) {
- Statistics.sendGetUserMediaFailed(error);
- } else {
- Statistics.sendLog(error);
- }
- };
-
- /**
- * Sends event to analytics and logs a message to the logger/console. Console
- * messages might also be logged to callstats automatically.
- *
- * @param {string | Object} event the event name, or an object which
- * represents the entire event.
- * @param {Object} properties properties to attach to the event (if an event
- * name as opposed to an event object is provided).
- */
- Statistics.sendAnalyticsAndLog = function(event, properties = {}) {
- if (!event) {
- logger.warn('No event or event name given.');
-
- return;
- }
-
- let eventToLog;
-
- // Also support an API with a single object as an event.
- if (typeof event === 'object') {
- eventToLog = event;
- } else {
- eventToLog = {
- name: event,
- properties
- };
- }
-
- logger.log(JSON.stringify(eventToLog));
-
- // We do this last, because it may modify the object which is passed.
- this.analytics.sendEvent(event, properties);
- };
-
- /**
- * Sends event to analytics.
- *
- * @param {string | Object} eventName the event name, or an object which
- * represents the entire event.
- * @param {Object} properties properties to attach to the event
- */
- Statistics.sendAnalytics = function(eventName, properties = {}) {
- this.analytics.sendEvent(eventName, properties);
- };
|