123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758 |
- /* global __filename */
-
- import { getLogger } from 'jitsi-meet-logger';
- import * as ConnectionQualityEvents
- from '../../service/connectivity/ConnectionQualityEvents';
- import * as ConferenceEvents from '../../JitsiConferenceEvents';
- import * as MediaType from '../../service/RTC/MediaType';
- import RTCBrowserType from '../RTC/RTCBrowserType';
- import Statistics from './statistics';
- import * as VideoType from '../../service/RTC/VideoType';
-
- /**
- * All avg RTP stats are currently reported under 1 event name, but under
- * different keys. This constant stores the name of this event.
- * Example structure of "avg.rtp.stats" analytics event:
- *
- * {
- * "stat_avg_rtt": {
- * value: 200,
- * samples: [ 100, 200, 300 ]
- * },
- * "stat_avg_packetloss_total": {
- * value: 10,
- * samples: [ 5, 10, 15]
- * },
- * "p2p_stat_avg_packetloss_total": {
- * value: 15,
- * samples: [ 10, 15, 20]
- * }
- * }
- *
- * Note that the samples array is currently emitted for debug purposes only and
- * can be removed anytime soon from the structure.
- *
- * Also not all values are always present in "avg.rtp.stats", some of the values
- * are obtained and calculated as part of different process/event pipe. For
- * example {@link ConnectionAvgStats} instances are doing the reports for each
- * {@link TraceablePeerConnection} and work independently from the main stats
- * pipe.
- *
- * @type {string}
- */
- const AVG_RTP_STATS_EVENT = 'avg.rtp.stats';
-
- const logger = getLogger(__filename);
-
- /**
- * This will calculate an average for one, named stat and submit it to
- * the analytics module when requested. It automatically counts the samples.
- */
- class AverageStatReport {
- /**
- * Creates new <tt>AverageStatReport</tt> for given name.
- * @param {string} name that's the name of the event that will be reported
- * to the analytics module.
- */
- constructor(name) {
- this.name = name;
- this.count = 0;
- this.sum = 0;
- this.samples = [];
- }
-
- /**
- * Adds the next value that will be included in the average when
- * {@link calculate} is called.
- * @param {number} nextValue
- */
- addNext(nextValue) {
- if (typeof nextValue !== 'number') {
- logger.error(
- `${this.name} - invalid value for idx: ${this.count}`,
- nextValue);
- } else if (!isNaN(nextValue)) {
- this.sum += nextValue;
- this.samples.push(nextValue);
- this.count += 1;
- }
- }
-
- /**
- * Calculates an average for the samples collected using {@link addNext}.
- * @return {number|NaN} an average of all collected samples or <tt>NaN</tt>
- * if no samples were collected.
- */
- calculate() {
- return this.sum / this.count;
- }
-
- /**
- * Appends the report to the analytics "data" object. The object will be
- * added under {@link this.name} key or "p2p_" + {@link this.name} if
- * <tt>isP2P</tt> is <tt>true</tt>.
- * @param {Object} report the analytics "data" object
- * @param {boolean} isP2P <tt>true</tt> if the stats is being report for
- * P2P connection or <tt>false</tt> for the JVB connection.
- */
- appendReport(report, isP2P) {
- report[`${isP2P ? 'p2p_' : ''}${this.name}`] = {
- value: this.calculate(),
- samples: this.samples
- };
- }
-
- /**
- * Clears all memory of any samples collected, so that new average can be
- * calculated using this instance.
- */
- reset() {
- this.samples = [];
- this.sum = 0;
- this.count = 0;
- }
- }
-
- /**
- * Class gathers the stats that are calculated and reported for a
- * {@link TraceablePeerConnection} even if it's not currently active. For
- * example we want to monitor RTT for the JVB connection while in P2P mode.
- */
- class ConnectionAvgStats {
- /**
- * Creates new <tt>ConnectionAvgStats</tt>
- * @param {JitsiConference} conference
- * @param {boolean} isP2P
- * @param {number} n the number of samples, before arithmetic mean is to be
- * calculated and values submitted to the analytics module.
- */
- constructor(conference, isP2P, n) {
- /**
- * Is this instance for JVB or P2P connection ?
- * @type {boolean}
- */
- this.isP2P = isP2P;
-
- /**
- * How many samples are to be included in arithmetic mean calculation.
- * @type {number}
- * @private
- */
- this._n = n;
-
- /**
- * The current sample index. Starts from 0 and goes up to {@link _n})
- * when analytics report will be submitted.
- * @type {number}
- * @private
- */
- this._sampleIdx = 0;
-
- /**
- * Average round trip time reported by the ICE candidate pair.
- * @type {AverageStatReport}
- */
- this._avgRTT = new AverageStatReport('stat_avg_rtt');
-
- /**
- * Map stores average RTT to the JVB reported by remote participants.
- * Mapped per participant id {@link JitsiParticipant.getId}.
- *
- * This is used only when {@link ConnectionAvgStats.isP2P} equals to
- * <tt>false</tt>.
- *
- * @type {Map<string,AverageStatReport>}
- * @private
- */
- this._avgRemoteRTTMap = new Map();
-
- /**
- * The conference for which stats will be collected and reported.
- * @type {JitsiConference}
- * @private
- */
- this._conference = conference;
-
- this._onConnectionStats = (tpc, stats) => {
- if (this.isP2P === tpc.isP2P) {
- this._calculateAvgStats(stats);
- }
- };
- conference.statistics.addConnectionStatsListener(
- this._onConnectionStats);
-
- if (!this.isP2P) {
- this._onUserLeft = id => this._avgRemoteRTTMap.delete(id);
- conference.on(ConferenceEvents.USER_LEFT, this._onUserLeft);
-
- this._onRemoteStatsUpdated
- = (id, data) => this._processRemoteStats(id, data);
- conference.on(
- ConnectionQualityEvents.REMOTE_STATS_UPDATED,
- this._onRemoteStatsUpdated);
- }
- }
-
- /**
- * Processes next batch of stats.
- * @param {go figure} data
- * @private
- */
- _calculateAvgStats(data) {
- if (!data) {
- logger.error('No stats');
-
- return;
- }
-
- if (RTCBrowserType.supportsRTTStatistics()) {
- if (data.transport && data.transport.length) {
- this._avgRTT.addNext(data.transport[0].rtt);
- }
- }
-
- this._sampleIdx += 1;
-
- if (this._sampleIdx >= this._n) {
- if (RTCBrowserType.supportsRTTStatistics()) {
- const batchReport = { };
-
- this._avgRTT.appendReport(batchReport, this.isP2P);
-
- // Report end to end RTT only for JVB
- if (!this.isP2P) {
- const avgRemoteRTT = this._calculateAvgRemoteRTT();
- const avgLocalRTT = this._avgRTT.calculate();
-
- if (!isNaN(avgLocalRTT) && !isNaN(avgRemoteRTT)) {
- // eslint-disable-next-line camelcase
- batchReport.stat_avg_end2endrtt
- = { value: avgLocalRTT + avgRemoteRTT };
- }
- }
-
- Statistics.analytics.sendEvent(
- AVG_RTP_STATS_EVENT, batchReport);
- }
-
- this._resetAvgStats();
- }
- }
-
- /**
- * Calculates arithmetic mean of all RTTs towards the JVB reported by
- * participants.
- * @return {number|NaN} NaN if not available (not enough data)
- * @private
- */
- _calculateAvgRemoteRTT() {
- let count = 0, sum = 0;
-
- // FIXME should we ignore RTT for participant
- // who "is having connectivity issues" ?
- for (const remoteAvg of this._avgRemoteRTTMap.values()) {
- const avg = remoteAvg.calculate();
-
- if (!isNaN(avg)) {
- sum += avg;
- count += 1;
- remoteAvg.reset();
- }
- }
-
- return sum / count;
- }
-
- /**
- * Processes {@link ConnectionQualityEvents.REMOTE_STATS_UPDATED} to analyse
- * RTT towards the JVB reported by each participant.
- * @param {string} id {@link JitsiParticipant.getId}
- * @param {go figure in ConnectionQuality.js} data
- * @private
- */
- _processRemoteStats(id, data) {
- const validData = typeof data.jvbRTT === 'number';
- let rttAvg = this._avgRemoteRTTMap.get(id);
-
- if (!rttAvg && validData) {
- rttAvg = new AverageStatReport(`${id}_stat_rtt`);
- this._avgRemoteRTTMap.set(id, rttAvg);
- }
-
- if (validData) {
- rttAvg.addNext(data.jvbRTT);
- } else if (rttAvg) {
- this._avgRemoteRTTMap.delete(id);
- }
- }
-
- /**
- * Reset cache of all averages and {@link _sampleIdx}.
- * @private
- */
- _resetAvgStats() {
- this._avgRTT.reset();
- if (this._avgRemoteRTTMap) {
- this._avgRemoteRTTMap.clear();
- }
- this._sampleIdx = 0;
- }
-
- /**
- *
- */
- dispose() {
- this._conference.statistics.removeConnectionStatsListener(
- this._onConnectionStats);
- if (!this.isP2P) {
- this._conference.off(
- ConnectionQualityEvents.REMOTE_STATS_UPDATED,
- this._onRemoteStatsUpdated);
- this._conference.off(
- ConferenceEvents.USER_LEFT,
- this._onUserLeft);
- }
- }
- }
-
- /**
- * Reports average RTP statistics values (arithmetic mean) to the analytics
- * module for things like bit rate, bandwidth, packet loss etc. It keeps track
- * of the P2P vs JVB conference modes and submits the values under different
- * namespaces (the events for P2P mode have 'p2p.' prefix). Every switch between
- * P2P mode resets the data collected so far and averages are calculated from
- * scratch.
- */
- export default class AvgRTPStatsReporter {
- /**
- * Creates new instance of <tt>AvgRTPStatsReporter</tt>
- * @param {JitsiConference} conference
- * @param {number} n the number of samples, before arithmetic mean is to be
- * calculated and values submitted to the analytics module.
- */
- constructor(conference, n) {
- /**
- * How many {@link ConnectionQualityEvents.LOCAL_STATS_UPDATED} samples
- * are to be included in arithmetic mean calculation.
- * @type {number}
- * @private
- */
- this._n = n;
-
- if (n > 0) {
- logger.info(`Avg RTP stats will be calculated every ${n} samples`);
- } else {
- logger.info('Avg RTP stats reports are disabled.');
-
- // Do not initialize
- return;
- }
-
- /**
- * The current sample index. Starts from 0 and goes up to {@link _n})
- * when analytics report will be submitted.
- * @type {number}
- * @private
- */
- this._sampleIdx = 0;
-
- /**
- * The conference for which stats will be collected and reported.
- * @type {JitsiConference}
- * @private
- */
- this._conference = conference;
-
- /**
- * Average audio upload bitrate
- * @type {AverageStatReport}
- * @private
- */
- this._avgAudioBitrateUp
- = new AverageStatReport('stat_avg_bitrate_audio_upload');
-
- /**
- * Average audio download bitrate
- * @type {AverageStatReport}
- * @private
- */
- this._avgAudioBitrateDown
- = new AverageStatReport('stat_avg_bitrate_audio_download');
-
- /**
- * Average video upload bitrate
- * @type {AverageStatReport}
- * @private
- */
- this._avgVideoBitrateUp
- = new AverageStatReport('stat_avg_bitrate_video_upload');
-
- /**
- * Average video download bitrate
- * @type {AverageStatReport}
- * @private
- */
- this._avgVideoBitrateDown
- = new AverageStatReport('stat_avg_bitrate_video_download');
-
- /**
- * Average upload bandwidth
- * @type {AverageStatReport}
- * @private
- */
- this._avgBandwidthUp
- = new AverageStatReport('stat_avg_bandwidth_upload');
-
- /**
- * Average download bandwidth
- * @type {AverageStatReport}
- * @private
- */
- this._avgBandwidthDown
- = new AverageStatReport('stat_avg_bandwidth_download');
-
- /**
- * Average total packet loss
- * @type {AverageStatReport}
- * @private
- */
- this._avgPacketLossTotal
- = new AverageStatReport('stat_avg_packetloss_total');
-
- /**
- * Average upload packet loss
- * @type {AverageStatReport}
- * @private
- */
- this._avgPacketLossUp
- = new AverageStatReport('stat_avg_packetloss_upload');
-
- /**
- * Average download packet loss
- * @type {AverageStatReport}
- * @private
- */
- this._avgPacketLossDown
- = new AverageStatReport('stat_avg_packetloss_download');
-
- /**
- * Average FPS for remote videos
- * @type {AverageStatReport}
- * @private
- */
- this._avgRemoteFPS = new AverageStatReport('stat_avg_framerate_remote');
-
- /**
- * Average FPS for remote screen streaming videos (reported only if not
- * a <tt>NaN</tt>).
- * @type {AverageStatReport}
- * @private
- */
- this._avgRemoteScreenFPS
- = new AverageStatReport('stat_avg_framerate_screen_remote');
-
- /**
- * Average FPS for local video (camera)
- * @type {AverageStatReport}
- * @private
- */
- this._avgLocalFPS = new AverageStatReport('stat_avg_framerate_local');
-
- /**
- * Average FPS for local screen streaming video (reported only if not
- * a <tt>NaN</tt>).
- * @type {AverageStatReport}
- * @private
- */
- this._avgLocalScreenFPS
- = new AverageStatReport('stat_avg_framerate_screen_local');
-
- /**
- * Average connection quality as defined by
- * the {@link ConnectionQuality} module.
- * @type {AverageStatReport}
- * @private
- */
- this._avgCQ = new AverageStatReport('stat_avg_cq');
-
- this._onLocalStatsUpdated = data => this._calculateAvgStats(data);
- conference.on(
- ConnectionQualityEvents.LOCAL_STATS_UPDATED,
- this._onLocalStatsUpdated);
-
- this._onP2PStatusChanged = () => {
- logger.debug('Resetting average stats calculation');
- this._resetAvgStats();
- this.jvbStatsMonitor._resetAvgStats();
- this.p2pStatsMonitor._resetAvgStats();
- };
- conference.on(
- ConferenceEvents.P2P_STATUS,
- this._onP2PStatusChanged);
-
- this.jvbStatsMonitor
- = new ConnectionAvgStats(conference, false /* JVB */, n);
-
- this.p2pStatsMonitor
- = new ConnectionAvgStats(conference, true /* P2P */, n);
- }
-
- /**
- * Processes next batch of stats reported on
- * {@link ConnectionQualityEvents.LOCAL_STATS_UPDATED}.
- * @param {go figure} data
- * @private
- */
- _calculateAvgStats(data) {
-
- const isP2P = this._conference.isP2PActive();
- const peerCount = this._conference.getParticipants().length;
-
- if (!isP2P && peerCount < 1) {
-
- // There's no point in collecting stats for a JVB conference of 1.
- // That happens for short period of time after everyone leaves
- // the room, until Jicofo terminates the session.
- return;
- }
-
- /* Uncomment to figure out stats structure
- for (const key in data) {
- if (data.hasOwnProperty(key)) {
- logger.info(`local stat ${key}: `, data[key]);
- }
- } */
-
- if (!data) {
- logger.error('No stats');
-
- return;
- }
-
- const bitrate = data.bitrate;
- const bandwidth = data.bandwidth;
- const packetLoss = data.packetLoss;
- const frameRate = data.framerate;
-
- if (!bitrate) {
- logger.error('No "bitrate"');
-
- return;
- } else if (!bandwidth) {
- logger.error('No "bandwidth"');
-
- return;
- } else if (!packetLoss) {
- logger.error('No "packetloss"');
-
- return;
- } else if (!frameRate) {
- logger.error('No "framerate"');
-
- return;
- }
-
- this._avgAudioBitrateUp.addNext(bitrate.audio.upload);
- this._avgAudioBitrateDown.addNext(bitrate.audio.download);
-
- this._avgVideoBitrateUp.addNext(bitrate.video.upload);
- this._avgVideoBitrateDown.addNext(bitrate.video.download);
-
- if (RTCBrowserType.supportsBandwidthStatistics()) {
- this._avgBandwidthUp.addNext(bandwidth.upload);
- this._avgBandwidthDown.addNext(bandwidth.download);
- }
-
- this._avgPacketLossUp.addNext(packetLoss.upload);
- this._avgPacketLossDown.addNext(packetLoss.download);
- this._avgPacketLossTotal.addNext(packetLoss.total);
-
- this._avgCQ.addNext(data.connectionQuality);
-
- if (frameRate) {
- this._avgRemoteFPS.addNext(
- this._calculateAvgVideoFps(
- frameRate, false /* remote */, VideoType.CAMERA));
- this._avgRemoteScreenFPS.addNext(
- this._calculateAvgVideoFps(
- frameRate, false /* remote */, VideoType.DESKTOP));
-
- this._avgLocalFPS.addNext(
- this._calculateAvgVideoFps(
- frameRate, true /* local */, VideoType.CAMERA));
- this._avgLocalScreenFPS.addNext(
- this._calculateAvgVideoFps(
- frameRate, true /* local */, VideoType.DESKTOP));
- }
-
- this._sampleIdx += 1;
-
- if (this._sampleIdx >= this._n) {
- const batchReport = { };
-
- this._avgAudioBitrateUp.appendReport(batchReport, isP2P);
- this._avgAudioBitrateDown.appendReport(batchReport, isP2P);
-
- this._avgVideoBitrateUp.appendReport(batchReport, isP2P);
- this._avgVideoBitrateDown.appendReport(batchReport, isP2P);
-
- if (RTCBrowserType.supportsBandwidthStatistics()) {
- this._avgBandwidthUp.appendReport(batchReport, isP2P);
- this._avgBandwidthDown.appendReport(batchReport, isP2P);
- }
- this._avgPacketLossUp.appendReport(batchReport, isP2P);
- this._avgPacketLossDown.appendReport(batchReport, isP2P);
- this._avgPacketLossTotal.appendReport(batchReport, isP2P);
-
- this._avgRemoteFPS.appendReport(batchReport, isP2P);
- if (!isNaN(this._avgRemoteScreenFPS.calculate())) {
- this._avgRemoteScreenFPS.appendReport(batchReport, isP2P);
- }
- this._avgLocalFPS.appendReport(batchReport, isP2P);
- if (!isNaN(this._avgLocalScreenFPS.calculate())) {
- this._avgLocalScreenFPS.appendReport(batchReport, isP2P);
- }
-
- this._avgCQ.appendReport(batchReport, isP2P);
-
- Statistics.analytics.sendEvent(AVG_RTP_STATS_EVENT, batchReport);
-
- this._resetAvgStats();
- }
- }
-
- /**
- * Calculates average FPS for the report
- * @param {go figure} frameRate
- * @param {boolean} isLocal if the average is to be calculated for the local
- * video or <tt>false</tt> if for remote videos.
- * @param {VideoType} videoType
- * @return {number|NaN} average FPS or <tt>NaN</tt> if there are no samples.
- * @private
- */
- _calculateAvgVideoFps(frameRate, isLocal, videoType) {
- let peerFpsSum = 0;
- let peerCount = 0;
- const myID = this._conference.myUserId();
-
- for (const peerID of Object.keys(frameRate)) {
- if (isLocal ? peerID === myID : peerID !== myID) {
- const participant
- = isLocal
- ? null : this._conference.getParticipantById(peerID);
- const videosFps = frameRate[peerID];
-
- // Do not continue without participant for non local peerID
- if ((isLocal || participant) && videosFps) {
- const peerAvgFPS
- = this._calculatePeerAvgVideoFps(
- videosFps, participant, videoType);
-
- if (!isNaN(peerAvgFPS)) {
- peerFpsSum += peerAvgFPS;
- peerCount += 1;
- }
- }
- }
- }
-
- return peerFpsSum / peerCount;
- }
-
- /**
- * Calculate average FPS for either remote or local participant
- * @param {object} videos maps FPS per video SSRC
- * @param {JitsiParticipant|null} participant remote participant or
- * <tt>null</tt> for local FPS calculation.
- * @param {VideoType} videoType the type of the video for which an average
- * will be calculated.
- * @return {number|NaN} average FPS of all participant's videos or
- * <tt>NaN</tt> if currently not available
- * @private
- */
- _calculatePeerAvgVideoFps(videos, participant, videoType) {
- let ssrcs = Object.keys(videos).map(ssrc => Number(ssrc));
- let videoTracks = null;
-
- // NOTE that this method is supposed to be called for the stats
- // received from the current peerconnection.
- const tpc = this._conference.getActivePeerConnection();
-
- if (participant) {
- videoTracks = participant.getTracksByMediaType(MediaType.VIDEO);
- if (videoTracks) {
- ssrcs
- = ssrcs.filter(
- ssrc => videoTracks.find(
- track => !track.isMuted()
- && track.getSSRC() === ssrc
- && track.videoType === videoType));
- }
- } else {
- videoTracks = this._conference.getLocalTracks(MediaType.VIDEO);
- ssrcs
- = ssrcs.filter(
- ssrc => videoTracks.find(
- track => !track.isMuted()
- && tpc.getLocalSSRC(track) === ssrc
- && track.videoType === videoType));
- }
-
- let peerFpsSum = 0;
- let peerSsrcCount = 0;
-
- for (const ssrc of ssrcs) {
- const peerSsrcFps = Number(videos[ssrc]);
-
- // FPS is reported as 0 for users with no video
- if (!isNaN(peerSsrcFps) && peerSsrcFps > 0) {
- peerFpsSum += peerSsrcFps;
- peerSsrcCount += 1;
- }
- }
-
- return peerFpsSum / peerSsrcCount;
- }
-
- /**
- * Reset cache of all averages and {@link _sampleIdx}.
- * @private
- */
- _resetAvgStats() {
- this._avgAudioBitrateUp.reset();
- this._avgAudioBitrateDown.reset();
-
- this._avgVideoBitrateUp.reset();
- this._avgVideoBitrateDown.reset();
-
- this._avgBandwidthUp.reset();
- this._avgBandwidthDown.reset();
-
- this._avgPacketLossUp.reset();
- this._avgPacketLossDown.reset();
- this._avgPacketLossTotal.reset();
-
- this._avgRemoteFPS.reset();
- this._avgRemoteScreenFPS.reset();
- this._avgLocalFPS.reset();
- this._avgLocalScreenFPS.reset();
-
- this._avgCQ.reset();
-
- this._sampleIdx = 0;
- }
-
- /**
- * Unregisters all event listeners and stops working.
- */
- dispose() {
- this._conference.off(
- ConferenceEvents.P2P_STATUS,
- this._onP2PStatusChanged);
- this._conference.off(
- ConnectionQualityEvents.LOCAL_STATS_UPDATED,
- this._onLocalStatsUpdated);
- this.jvbStatsMonitor.dispose();
- this.p2pStatsMonitor.dispose();
- }
- }
|