123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- import { getLogger } from '@jitsi/logger';
-
- import * as ConferenceEvents from '../../JitsiConferenceEvents';
- import CodecMimeType from '../../service/RTC/CodecMimeType';
- import * as RTCEvents from '../../service/RTC/RTCEvents';
- import * as ConnectionQualityEvents from '../../service/connectivity/ConnectionQualityEvents';
-
- const Resolutions = require('../../service/RTC/Resolutions');
- const { VideoType } = require('../../service/RTC/VideoType');
- const { XMPPEvents } = require('../../service/xmpp/XMPPEvents');
-
- const logger = getLogger(__filename);
-
- /**
- * The value to use for the "type" field for messages sent by ConnectionQuality
- * over the data channel.
- */
- const STATS_MESSAGE_TYPE = 'stats';
-
- const kSimulcastFormats = [
- { width: 1920,
- height: 1080,
- layers: 3,
- target: 'high' },
- { width: 1280,
- height: 720,
- layers: 3,
- target: 'high' },
- { width: 960,
- height: 540,
- layers: 3,
- target: 'standard' },
- { width: 640,
- height: 360,
- layers: 2,
- target: 'standard' },
- { width: 480,
- height: 270,
- layers: 2,
- target: 'low' },
- { width: 320,
- height: 180,
- layers: 1,
- target: 'low' }
- ];
-
- /**
- * The maximum bitrate to use as a measurement against the participant's current
- * bitrate. This cap helps in the cases where the participant's bitrate is high
- * but not enough to fulfill high targets, such as with 1080p.
- */
- const MAX_TARGET_BITRATE = 2500;
-
- /**
- * The initial bitrate for video in kbps.
- */
- let startBitrate = 800;
-
- /**
- * Gets the expected bitrate (in kbps) in perfect network conditions.
- * @param simulcast {boolean} whether simulcast is enabled or not.
- * @param resolution {Resolution} the resolution.
- * @param millisSinceStart {number} the number of milliseconds since sending video started.
- * @param videoQualitySettings {Object} the bitrate and codec settings for the local video source.
- */
- function getTarget(simulcast, resolution, millisSinceStart, videoQualitySettings) {
- let target = 0;
- let height = Math.min(resolution.height, resolution.width);
-
- // Find the first format with height no bigger than ours.
- let simulcastFormat = kSimulcastFormats.find(f => f.height <= height);
-
- if (simulcastFormat && simulcast && videoQualitySettings.codec === CodecMimeType.VP8) {
- // Sum the target fields from all simulcast layers for the given
- // resolution (e.g. 720p + 360p + 180p) for VP8 simulcast.
- for (height = simulcastFormat.height; height >= 180; height /= 2) {
- const targetHeight = height;
-
- simulcastFormat = kSimulcastFormats.find(f => f.height === targetHeight);
- if (simulcastFormat) {
- target += videoQualitySettings[simulcastFormat.target];
- } else {
- break;
- }
- }
- } else if (simulcastFormat) {
- // For VP9 SVC, H.264 (simulcast automatically disabled) and p2p, target bitrate will be
- // same as that of the individual stream bitrate.
- target = videoQualitySettings[simulcastFormat.target];
- }
-
- // Allow for an additional 1 second for ramp up -- delay any initial drop
- // of connection quality by 1 second. Convert target from bps to kbps.
- return Math.min(target / 1000, rampUp(Math.max(0, millisSinceStart - 1000)));
- }
-
- /**
- * Gets the bitrate to which GCC would have ramped up in perfect network
- * conditions after millisSinceStart milliseconds.
- * @param millisSinceStart {number} the number of milliseconds since sending
- * video was enabled.
- */
- function rampUp(millisSinceStart) {
- if (millisSinceStart > 60000) {
- return Number.MAX_SAFE_INTEGER;
- }
-
- // According to GCC the send side bandwidth estimation grows with at most
- // 8% per second.
- // https://tools.ietf.org/html/draft-ietf-rmcat-gcc-02#section-5.5
- return startBitrate * Math.pow(1.08, millisSinceStart / 1000);
- }
-
- /**
- * A class which monitors the local statistics coming from the RTC modules, and
- * calculates a "connection quality" value, in percent, for the media
- * connection. A value of 100% indicates a very good network connection, and a
- * value of 0% indicates a poor connection.
- */
- export default class ConnectionQuality {
- /**
- *
- * @param conference
- * @param eventEmitter
- * @param options
- */
- constructor(conference, eventEmitter, options) {
- this.eventEmitter = eventEmitter;
-
- /**
- * The owning JitsiConference.
- */
- this._conference = conference;
-
- /**
- * Holds statistics about the local connection quality.
- */
- this._localStats = {
- connectionQuality: 100,
- jvbRTT: undefined
- };
-
- /**
- * The time this._localStats.connectionQuality was last updated.
- */
- this._lastConnectionQualityUpdate = -1;
-
- /**
- * Conference options.
- */
- this._options = options;
-
- /**
- * Maps a participant ID to an object holding connection quality
- * statistics received from this participant.
- */
- this._remoteStats = {};
-
- /**
- * The time that the ICE state last changed to CONNECTED. We use this
- * to calculate how much time we as a sender have had to ramp-up.
- */
- this._timeIceConnected = -1;
-
- /**
- * The time that local video was unmuted. We use this to calculate how
- * much time we as a sender have had to ramp-up.
- */
- this._timeVideoUnmuted = -1;
-
- // We assume a global startBitrate value for the sake of simplicity.
- if (this._options.config?.startBitrate > 0) {
- startBitrate = this._options.config.startBitrate;
- }
-
- // TODO: consider ignoring these events and letting the user of
- // lib-jitsi-meet handle these separately.
- conference.on(
- ConferenceEvents.CONNECTION_INTERRUPTED,
- () => {
- this._updateLocalConnectionQuality(0);
- this.eventEmitter.emit(
- ConnectionQualityEvents.LOCAL_STATS_UPDATED,
- this._localStats);
- this._broadcastLocalStats();
- });
-
- conference.room.addListener(
- XMPPEvents.ICE_CONNECTION_STATE_CHANGED,
- (jingleSession, newState) => {
- if (!jingleSession.isP2P && newState === 'connected') {
- this._timeIceConnected = window.performance.now();
- }
- });
-
- // Listen to DataChannel message from other participants in the
- // conference, and update the _remoteStats field accordingly.
- // TODO - Delete this when all the mobile endpoints switch to using the new Colibri
- // message format for sending the endpoint stats.
- conference.on(
- ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
- (participant, payload) => {
- if (payload.type === STATS_MESSAGE_TYPE) {
- this._updateRemoteStats(
- participant.getId(), payload.values);
- }
- });
-
- conference.on(
- ConferenceEvents.ENDPOINT_STATS_RECEIVED,
- (participant, payload) => {
- this._updateRemoteStats(participant.getId(), payload);
- });
-
- if (!this._options.config.disableLocalStats) {
- // Listen to local statistics events originating from the RTC module and update the _localStats field.
- conference.statistics.addConnectionStatsListener(this._updateLocalStats.bind(this));
- }
-
- // Save the last time we were unmuted.
- conference.on(
- ConferenceEvents.TRACK_MUTE_CHANGED,
- track => {
- if (track.isVideoTrack()) {
- if (track.isMuted()) {
- this._timeVideoUnmuted = -1;
- } else {
- this._maybeUpdateUnmuteTime();
- }
- }
- });
- conference.on(
- ConferenceEvents.TRACK_ADDED,
- track => {
- if (track.isVideoTrack() && !track.isMuted()) {
- this._maybeUpdateUnmuteTime();
- }
- });
- conference.rtc.on(
- RTCEvents.LOCAL_TRACK_MAX_ENABLED_RESOLUTION_CHANGED,
- track => {
- this._localStats.maxEnabledResolution = track.maxEnabledResolution;
- });
-
- conference.on(
- ConferenceEvents.SERVER_REGION_CHANGED,
- serverRegion => {
- this._localStats.serverRegion = serverRegion;
- });
-
- conference.on(
- ConferenceEvents.PROPERTIES_CHANGED,
- properties => {
- this._localStats.bridgeCount
- = Number((properties || {})['bridge-count']);
- }
- );
- }
-
- /**
- * Sets _timeVideoUnmuted if it was previously unset. If it was already set,
- * doesn't change it.
- */
- _maybeUpdateUnmuteTime() {
- if (this._timeVideoUnmuted < 0) {
- this._timeVideoUnmuted = window.performance.now();
- }
- }
-
- /**
- * Calculates a new "connection quality" value.
- * @param videoType {VideoType} the type of the video source (camera or a screen capture).
- * @param isMuted {boolean} whether the local video is muted.
- * @param resolutionName {Resolution} the input resolution used by the camera.
- * @returns {*} the newly calculated connection quality.
- */
- _calculateConnectionQuality(videoType, isMuted, resolutionName) {
-
- // resolutionName is an index into Resolutions (where "720" is
- // "1280x720" and "960" is "960x720" ...).
- const resolution = Resolutions[resolutionName];
-
- let quality = 100;
- let packetLoss;
-
- // TODO: take into account packet loss for received streams
-
- if (this._localStats.packetLoss) {
- packetLoss = this._localStats.packetLoss.upload;
-
- // Ugly Hack Alert (UHA):
- // The packet loss for the upload direction is calculated based on
- // incoming RTCP Receiver Reports. Since we don't have RTCP
- // termination for audio, these reports come from the actual
- // receivers in the conference and therefore the reported packet
- // loss includes loss from the bridge to the receiver.
- // When we are sending video this effect is small, because the
- // number of video packets is much larger than the number of audio
- // packets (and our calculation is based on the total number of
- // received and lost packets).
- // When video is muted, however, the effect might be significant,
- // but we don't know what it is. We do know that it is positive, so
- // as a temporary solution, until RTCP termination is implemented
- // for the audio streams, we relax the packet loss checks here.
- if (isMuted) {
- packetLoss *= 0.5;
- }
- }
-
- if (isMuted || !resolution || videoType === VideoType.DESKTOP
- || this._timeIceConnected < 0
- || this._timeVideoUnmuted < 0) {
-
- // Calculate a value based on packet loss only.
- if (packetLoss === undefined) {
- logger.error('Cannot calculate connection quality, unknown '
- + 'packet loss.');
- quality = 100;
- } else if (packetLoss <= 2) {
- quality = 100; // Full 5 bars.
- } else if (packetLoss <= 4) {
- quality = 70; // 4 bars
- } else if (packetLoss <= 6) {
- quality = 50; // 3 bars
- } else if (packetLoss <= 8) {
- quality = 30; // 2 bars
- } else if (packetLoss <= 12) {
- quality = 10; // 1 bars
- } else {
- quality = 0; // Still 1 bar, but slower climb-up.
- }
- } else {
- // Calculate a value based on the send video bitrate on the active TPC.
- const activeTPC = this._conference.getActivePeerConnection();
-
- if (activeTPC) {
- const isSimulcastOn = activeTPC.isSimulcastOn();
- const videoQualitySettings = activeTPC.getTargetVideoBitrates();
-
- // Add the codec info as well.
- videoQualitySettings.codec = activeTPC.getConfiguredVideoCodec();
-
- // Time since sending of video was enabled.
- const millisSinceStart = window.performance.now()
- - Math.max(this._timeVideoUnmuted, this._timeIceConnected);
- const statsInterval = this._options.config?.pcStatsInterval ?? 10000;
-
- // Expected sending bitrate in perfect conditions.
- let target = getTarget(isSimulcastOn, resolution, millisSinceStart, videoQualitySettings);
-
- target = Math.min(target, MAX_TARGET_BITRATE);
-
- // Calculate the quality only after the stats are available (after video was enabled).
- if (millisSinceStart > statsInterval) {
- quality = 100 * this._localStats.bitrate.upload / target;
- }
- }
-
- // Whatever the bitrate, drop early if there is significant loss
- if (packetLoss && packetLoss >= 10) {
- quality = Math.min(quality, 30);
- }
- }
-
- // Make sure that the quality doesn't climb quickly
- if (this._lastConnectionQualityUpdate > 0) {
- const maxIncreasePerSecond = 2;
- const prevConnectionQuality = this._localStats.connectionQuality;
- const diffSeconds = (window.performance.now() - this._lastConnectionQualityUpdate) / 1000;
-
- quality = Math.min(quality, prevConnectionQuality + (diffSeconds * maxIncreasePerSecond));
- }
-
- return Math.min(100, quality);
- }
-
- /**
- * Updates the localConnectionQuality value
- * @param values {number} the new value. Should be in [0, 100].
- */
- _updateLocalConnectionQuality(value) {
- this._localStats.connectionQuality = value;
- this._lastConnectionQualityUpdate = window.performance.now();
- }
-
- /**
- * Broadcasts the local statistics to all other participants in the
- * conference.
- */
- _broadcastLocalStats() {
- // Send only the data that remote participants care about.
- const data = {
- bitrate: this._localStats.bitrate,
- packetLoss: this._localStats.packetLoss,
- connectionQuality: this._localStats.connectionQuality,
- jvbRTT: this._localStats.jvbRTT,
- serverRegion: this._localStats.serverRegion,
- maxEnabledResolution: this._localStats.maxEnabledResolution,
- avgAudioLevels: this._localStats.localAvgAudioLevels
- };
-
- try {
- this._conference.sendEndpointStatsMessage(data);
- } catch (err) {
- // Ignore the error as we might hit it in the beginning of the call before the channel is ready.
- // The statistics will be sent again after few seconds and error is logged elseware as well.
- }
- }
-
- /**
- * Updates the local statistics
- * @param {TraceablePeerConnection} tpc the peerconnection which emitted
- * the stats
- * @param data new statistics
- */
- _updateLocalStats(tpc, data) {
- // Update jvbRTT
- if (!tpc.isP2P) {
- const jvbRTT
- = data.transport
- && data.transport.length && data.transport[0].rtt;
-
- this._localStats.jvbRTT = jvbRTT ? jvbRTT : undefined;
- }
-
- // Do not continue with processing of other stats if they do not
- // originate from the active peerconnection
- if (tpc !== this._conference.getActivePeerConnection()) {
- return;
- }
-
- let key;
- const updateLocalConnectionQuality
- = !this._conference.isConnectionInterrupted();
- const localVideoTrack
- = this._conference.getLocalVideoTrack();
- const videoType
- = localVideoTrack ? localVideoTrack.videoType : undefined;
- const isMuted = localVideoTrack ? localVideoTrack.isMuted() : true;
- const resolution = localVideoTrack
- ? Math.min(localVideoTrack.resolution, localVideoTrack.maxEnabledResolution) : null;
-
- if (!isMuted) {
- this._maybeUpdateUnmuteTime();
- }
-
- // Copy the fields already in 'data'.
- for (key in data) {
- if (data.hasOwnProperty(key)) {
- this._localStats[key] = data[key];
- }
- }
-
- // And re-calculate the connectionQuality field.
- if (updateLocalConnectionQuality) {
- this._updateLocalConnectionQuality(
- this._calculateConnectionQuality(
- videoType,
- isMuted,
- resolution));
- }
-
- this.eventEmitter.emit(
- ConnectionQualityEvents.LOCAL_STATS_UPDATED,
- this._localStats);
- this._broadcastLocalStats();
- }
-
- /**
- * Updates remote statistics
- * @param id the id of the remote participant
- * @param data the statistics received
- */
- _updateRemoteStats(id, data) {
- // Use only the fields we need
- this._remoteStats[id] = {
- bitrate: data.bitrate,
- packetLoss: data.packetLoss,
- connectionQuality: data.connectionQuality,
- jvbRTT: data.jvbRTT,
- serverRegion: data.serverRegion,
- maxEnabledResolution: data.maxEnabledResolution,
- avgAudioLevels: data.avgAudioLevels
- };
-
- this.eventEmitter.emit(
- ConnectionQualityEvents.REMOTE_STATS_UPDATED,
- id,
- this._remoteStats[id]);
- }
-
- /**
- * Returns the local statistics.
- * Exported only for use in jitsi-meet-torture.
- */
- getStats() {
- return this._localStats;
- }
- }
|