2017-09-26 16:55:09 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-06-19 20:20:13 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
|
2017-09-26 16:55:09 +00:00
|
|
|
import { translate } from '../../base/i18n';
|
2017-06-19 20:20:13 +00:00
|
|
|
import { JitsiParticipantConnectionStatus } from '../../base/lib-jitsi-meet';
|
2017-08-22 19:22:06 +00:00
|
|
|
import { Popover } from '../../base/popover';
|
2017-06-19 20:20:13 +00:00
|
|
|
import { ConnectionStatsTable } from '../../connection-stats';
|
|
|
|
|
2017-07-05 18:17:30 +00:00
|
|
|
import statsEmitter from '../statsEmitter';
|
|
|
|
|
2017-06-19 20:20:13 +00:00
|
|
|
declare var interfaceConfig: Object;
|
|
|
|
|
2017-09-26 16:55:09 +00:00
|
|
|
/**
|
|
|
|
* The connection quality percentage that must be reached to be considered of
|
|
|
|
* good quality and can result in the connection indicator being hidden.
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
const INDICATOR_DISPLAY_THRESHOLD = 70;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of display configurations for the connection indicator and its bars.
|
|
|
|
* The ordering is done specifically for faster iteration to find a matching
|
|
|
|
* configuration to the current connection strength percentage.
|
|
|
|
*
|
|
|
|
* @type {Object[]}
|
|
|
|
*/
|
2017-06-19 20:20:13 +00:00
|
|
|
const QUALITY_TO_WIDTH = [
|
|
|
|
|
2017-09-26 16:55:09 +00:00
|
|
|
// Full (3 bars)
|
2017-06-19 20:20:13 +00:00
|
|
|
{
|
2017-09-26 16:55:09 +00:00
|
|
|
colorClass: 'status-high',
|
|
|
|
percent: INDICATOR_DISPLAY_THRESHOLD,
|
|
|
|
tip: 'connectionindicator.quality.good',
|
2017-06-19 20:20:13 +00:00
|
|
|
width: '100%'
|
|
|
|
},
|
|
|
|
|
|
|
|
// 2 bars
|
|
|
|
{
|
2017-09-26 16:55:09 +00:00
|
|
|
colorClass: 'status-med',
|
|
|
|
percent: 40,
|
|
|
|
tip: 'connectionindicator.quality.nonoptimal',
|
|
|
|
width: '66%'
|
2017-06-19 20:20:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// 1 bar
|
|
|
|
{
|
2017-09-26 16:55:09 +00:00
|
|
|
colorClass: 'status-low',
|
2017-06-19 20:20:13 +00:00
|
|
|
percent: 0,
|
2017-09-26 16:55:09 +00:00
|
|
|
tip: 'connectionindicator.quality.poor',
|
|
|
|
width: '33%'
|
2017-06-19 20:20:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 16:55:09 +00:00
|
|
|
// Note: we never show 0 bars as long as there is a connection.
|
2017-06-19 20:20:13 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements a React {@link Component} which displays the current connection
|
|
|
|
* quality percentage and has a popover to show more detailed connection stats.
|
|
|
|
*
|
|
|
|
* @extends {Component}
|
|
|
|
*/
|
|
|
|
class ConnectionIndicator extends Component {
|
|
|
|
/**
|
|
|
|
* {@code ConnectionIndicator} component's property types.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
static propTypes = {
|
2017-09-26 16:55:09 +00:00
|
|
|
/**
|
|
|
|
* Whether or not the component should ignore setting a visibility class
|
|
|
|
* for hiding the component when the connection quality is not strong.
|
|
|
|
*/
|
|
|
|
alwaysVisible: PropTypes.bool,
|
|
|
|
|
2017-07-05 18:17:30 +00:00
|
|
|
/**
|
|
|
|
* The current condition of the user's connection, matching one of the
|
|
|
|
* enumerated values in the library.
|
|
|
|
*
|
|
|
|
* @type {JitsiParticipantConnectionStatus}
|
|
|
|
*/
|
2017-09-26 16:55:09 +00:00
|
|
|
connectionStatus: PropTypes.string,
|
2017-07-05 18:17:30 +00:00
|
|
|
|
2017-06-19 20:20:13 +00:00
|
|
|
/**
|
2017-08-14 15:02:58 +00:00
|
|
|
* Whether or not clicking the indicator should display a popover for
|
|
|
|
* more details.
|
2017-06-19 20:20:13 +00:00
|
|
|
*/
|
2017-09-26 16:55:09 +00:00
|
|
|
enableStatsDisplay: PropTypes.bool,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The font-size for the icon.
|
|
|
|
*/
|
|
|
|
iconSize: PropTypes.number,
|
2017-06-19 20:20:13 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-14 15:02:58 +00:00
|
|
|
* Whether or not the displays stats are for local video.
|
2017-06-19 20:20:13 +00:00
|
|
|
*/
|
2017-09-26 16:55:09 +00:00
|
|
|
isLocalVideo: PropTypes.bool,
|
2017-06-19 20:20:13 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-14 15:02:58 +00:00
|
|
|
* Relative to the icon from where the popover for more connection
|
|
|
|
* details should display.
|
2017-06-19 20:20:13 +00:00
|
|
|
*/
|
2017-09-26 16:55:09 +00:00
|
|
|
statsPopoverPosition: PropTypes.string,
|
2017-06-19 20:20:13 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-05 18:17:30 +00:00
|
|
|
* Invoked to obtain translated strings.
|
2017-06-19 20:20:13 +00:00
|
|
|
*/
|
2017-09-26 16:55:09 +00:00
|
|
|
t: PropTypes.func,
|
2017-06-19 20:20:13 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-05 18:17:30 +00:00
|
|
|
* The user ID associated with the displayed connection indication and
|
|
|
|
* stats.
|
2017-06-19 20:20:13 +00:00
|
|
|
*/
|
2017-09-26 16:55:09 +00:00
|
|
|
userID: PropTypes.string
|
2017-06-19 20:20:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new {@code ConnectionIndicator} instance.
|
|
|
|
*
|
|
|
|
* @param {Object} props - The read-only properties with which the new
|
|
|
|
* instance is to be initialized.
|
|
|
|
*/
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2017-09-26 16:55:09 +00:00
|
|
|
/**
|
|
|
|
* The timeout for automatically hiding the indicator.
|
|
|
|
*
|
|
|
|
* @type {timeoutID}
|
|
|
|
*/
|
|
|
|
autoHideTimeout: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not a CSS class should be applied to the root for
|
|
|
|
* hiding the connection indicator. By default the indicator should
|
|
|
|
* start out hidden because the current connection status is not
|
|
|
|
* known at mount.
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
showIndicator: false,
|
|
|
|
|
2017-06-19 20:20:13 +00:00
|
|
|
/**
|
|
|
|
* Whether or not the popover content should display additional
|
|
|
|
* statistics.
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2017-07-05 18:17:30 +00:00
|
|
|
showMoreStats: false,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache of the stats received from subscribing to stats emitting.
|
|
|
|
* The keys should be the name of the stat. With each stat update,
|
|
|
|
* updates stats are mixed in with cached stats and a new stats
|
|
|
|
* object is set in state.
|
|
|
|
*/
|
|
|
|
stats: {}
|
2017-06-19 20:20:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Bind event handlers so they are only bound once for every instance.
|
2017-08-14 15:02:58 +00:00
|
|
|
this._onStatsUpdated = this._onStatsUpdated.bind(this);
|
2017-06-19 20:20:13 +00:00
|
|
|
this._onToggleShowMore = this._onToggleShowMore.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-14 15:02:58 +00:00
|
|
|
* Starts listening for stat updates.
|
2017-06-19 20:20:13 +00:00
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* returns {void}
|
|
|
|
*/
|
|
|
|
componentDidMount() {
|
2017-07-05 18:17:30 +00:00
|
|
|
statsEmitter.subscribeToClientStats(
|
|
|
|
this.props.userID, this._onStatsUpdated);
|
2017-06-19 20:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-14 15:02:58 +00:00
|
|
|
* Updates which user's stats are being listened to.
|
2017-06-19 20:20:13 +00:00
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* returns {void}
|
|
|
|
*/
|
2017-07-05 18:17:30 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (prevProps.userID !== this.props.userID) {
|
|
|
|
statsEmitter.unsubscribeToClientStats(
|
2017-07-13 18:44:12 +00:00
|
|
|
prevProps.userID, this._onStatsUpdated);
|
2017-07-05 18:17:30 +00:00
|
|
|
statsEmitter.subscribeToClientStats(
|
|
|
|
this.props.userID, this._onStatsUpdated);
|
|
|
|
}
|
2017-06-19 20:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-26 16:55:09 +00:00
|
|
|
* Cleans up any queued processes, which includes listening for new stats
|
|
|
|
* and clearing any timeout to hide the indicator.
|
2017-06-19 20:20:13 +00:00
|
|
|
*
|
2017-08-14 15:02:58 +00:00
|
|
|
* @private
|
|
|
|
* @returns {void}
|
2017-06-19 20:20:13 +00:00
|
|
|
*/
|
|
|
|
componentWillUnmount() {
|
2017-07-05 18:17:30 +00:00
|
|
|
statsEmitter.unsubscribeToClientStats(
|
|
|
|
this.props.userID, this._onStatsUpdated);
|
2017-09-26 16:55:09 +00:00
|
|
|
|
|
|
|
clearTimeout(this.state.autoHideTimeout);
|
2017-06-19 20:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
2017-09-26 16:55:09 +00:00
|
|
|
const visibilityClass = this._getVisibilityClass();
|
|
|
|
const rootClassNames = `indicator-container ${visibilityClass}`;
|
|
|
|
|
|
|
|
const colorClass = this._getConnectionColorClass();
|
|
|
|
const indicatorContainerClassNames
|
|
|
|
= `connection-indicator indicator ${colorClass}`;
|
|
|
|
|
2017-06-19 20:20:13 +00:00
|
|
|
return (
|
2017-08-22 19:22:06 +00:00
|
|
|
<Popover
|
2017-09-26 16:55:09 +00:00
|
|
|
className = { rootClassNames }
|
2017-08-22 19:22:06 +00:00
|
|
|
content = { this._renderStatisticsTable() }
|
|
|
|
position = { this.props.statsPopoverPosition }>
|
|
|
|
<div className = 'popover-trigger'>
|
2017-09-26 16:55:09 +00:00
|
|
|
<div
|
|
|
|
className = { indicatorContainerClassNames }
|
|
|
|
style = {{ fontSize: this.props.iconSize }}>
|
2017-08-22 19:22:06 +00:00
|
|
|
<div className = 'connection indicatoricon'>
|
|
|
|
{ this._renderIcon() }
|
2017-08-14 15:02:58 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2017-08-22 19:22:06 +00:00
|
|
|
</div>
|
|
|
|
</Popover>
|
2017-06-19 20:20:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-26 16:55:09 +00:00
|
|
|
/**
|
|
|
|
* Returns a CSS class that interprets the current connection status as a
|
|
|
|
* color.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
_getConnectionColorClass() {
|
|
|
|
const { connectionStatus } = this.props;
|
|
|
|
const { percent } = this.state.stats;
|
|
|
|
const { INACTIVE, INTERRUPTED } = JitsiParticipantConnectionStatus;
|
|
|
|
|
|
|
|
if (connectionStatus === INACTIVE) {
|
|
|
|
return 'status-other';
|
|
|
|
} else if (connectionStatus === INTERRUPTED) {
|
|
|
|
return 'status-lost';
|
|
|
|
} else if (typeof percent === 'undefined') {
|
|
|
|
return 'status-high';
|
|
|
|
}
|
|
|
|
|
|
|
|
return QUALITY_TO_WIDTH.find(x => percent >= x.percent).colorClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a string that describes the current connection status.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
_getConnectionStatusTip() {
|
|
|
|
let tipKey;
|
|
|
|
|
|
|
|
switch (this.props.connectionStatus) {
|
|
|
|
case JitsiParticipantConnectionStatus.INTERRUPTED:
|
|
|
|
tipKey = 'connectionindicator.quality.lost';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JitsiParticipantConnectionStatus.INACTIVE:
|
|
|
|
tipKey = 'connectionindicator.quality.inactive';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: {
|
|
|
|
const { percent } = this.state.stats;
|
|
|
|
|
|
|
|
if (typeof percent === 'undefined') {
|
|
|
|
// If percentage is undefined then there are no stats available
|
|
|
|
// yet, likely because only a local connection has been
|
|
|
|
// established so far. Assume a strong connection to start.
|
|
|
|
tipKey = 'connectionindicator.quality.good';
|
|
|
|
} else {
|
|
|
|
const config = QUALITY_TO_WIDTH.find(x => percent >= x.percent);
|
|
|
|
|
|
|
|
tipKey = config.tip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.props.t(tipKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns additional class names to add to the root of the component. The
|
|
|
|
* class names are intended to be used for hiding or showing the indicator.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
_getVisibilityClass() {
|
|
|
|
const { connectionStatus } = this.props;
|
|
|
|
|
|
|
|
return this.state.showIndicator
|
|
|
|
|| this.props.alwaysVisible
|
|
|
|
|| connectionStatus === JitsiParticipantConnectionStatus.INTERRUPTED
|
|
|
|
|| connectionStatus === JitsiParticipantConnectionStatus.INACTIVE
|
|
|
|
? 'show-connection-indicator' : 'hide-connection-indicator';
|
|
|
|
}
|
|
|
|
|
2017-07-05 18:17:30 +00:00
|
|
|
/**
|
|
|
|
* Callback invoked when new connection stats associated with the passed in
|
|
|
|
* user ID are available. Will update the component's display of current
|
|
|
|
* statistics.
|
|
|
|
*
|
|
|
|
* @param {Object} stats - Connection stats from the library.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onStatsUpdated(stats = {}) {
|
|
|
|
const { connectionQuality } = stats;
|
|
|
|
const newPercentageState = typeof connectionQuality === 'undefined'
|
|
|
|
? {} : { percent: connectionQuality };
|
|
|
|
const newStats = Object.assign(
|
|
|
|
{},
|
|
|
|
this.state.stats,
|
|
|
|
stats,
|
|
|
|
newPercentageState);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
stats: newStats
|
|
|
|
});
|
2017-09-26 16:55:09 +00:00
|
|
|
|
|
|
|
// Rely on React to batch setState actions.
|
|
|
|
this._updateIndicatorAutoHide(newStats.percent);
|
2017-07-05 18:17:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 20:20:13 +00:00
|
|
|
/**
|
|
|
|
* Callback to invoke when the show more link in the popover content is
|
|
|
|
* clicked. Sets the state which will determine if the popover should show
|
|
|
|
* additional statistics about the connection.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onToggleShowMore() {
|
|
|
|
this.setState({ showMoreStats: !this.state.showMoreStats });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a ReactElement for displaying an icon that represents the current
|
|
|
|
* connection quality.
|
|
|
|
*
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_renderIcon() {
|
2017-09-26 16:55:09 +00:00
|
|
|
if (this.props.connectionStatus
|
|
|
|
=== JitsiParticipantConnectionStatus.INACTIVE) {
|
2017-06-19 20:20:13 +00:00
|
|
|
return (
|
|
|
|
<span className = 'connection_ninja'>
|
|
|
|
<i className = 'icon-ninja' />
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
2017-09-26 16:55:09 +00:00
|
|
|
|
|
|
|
let iconWidth;
|
|
|
|
let emptyIconWrapperClassName = 'connection_empty';
|
|
|
|
|
|
|
|
if (this.props.connectionStatus
|
|
|
|
=== JitsiParticipantConnectionStatus.INTERRUPTED) {
|
|
|
|
|
|
|
|
// emptyIconWrapperClassName is used by the torture tests to
|
|
|
|
// identify lost connection status handling.
|
|
|
|
emptyIconWrapperClassName = 'connection_lost';
|
|
|
|
iconWidth = '0%';
|
|
|
|
} else if (typeof this.state.stats.percent === 'undefined') {
|
|
|
|
iconWidth = '100%';
|
|
|
|
} else {
|
|
|
|
const { percent } = this.state.stats;
|
|
|
|
|
|
|
|
iconWidth = QUALITY_TO_WIDTH.find(x => percent >= x.percent).width;
|
2017-06-19 20:20:13 +00:00
|
|
|
}
|
2017-09-26 16:55:09 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
<span
|
|
|
|
className = { emptyIconWrapperClassName }
|
|
|
|
key = 'icon-empty'>
|
|
|
|
<i className = 'icon-gsm-bars' />
|
|
|
|
</span>,
|
|
|
|
<span
|
|
|
|
className = 'connection_full'
|
|
|
|
key = 'icon-full'
|
|
|
|
style = {{ width: iconWidth }}>
|
|
|
|
<i className = 'icon-gsm-bars' />
|
|
|
|
</span>
|
|
|
|
];
|
2017-06-19 20:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-22 19:22:06 +00:00
|
|
|
* Creates a {@code ConnectionStatisticsTable} instance.
|
2017-06-19 20:20:13 +00:00
|
|
|
*
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_renderStatisticsTable() {
|
|
|
|
const {
|
|
|
|
bandwidth,
|
|
|
|
bitrate,
|
|
|
|
framerate,
|
|
|
|
packetLoss,
|
|
|
|
resolution,
|
|
|
|
transport
|
2017-07-05 18:17:30 +00:00
|
|
|
} = this.state.stats;
|
2017-06-19 20:20:13 +00:00
|
|
|
|
|
|
|
return (
|
2017-08-22 19:22:06 +00:00
|
|
|
<ConnectionStatsTable
|
|
|
|
bandwidth = { bandwidth }
|
|
|
|
bitrate = { bitrate }
|
2017-09-26 16:55:09 +00:00
|
|
|
connectionSummary = { this._getConnectionStatusTip() }
|
2017-08-22 19:22:06 +00:00
|
|
|
framerate = { framerate }
|
|
|
|
isLocalVideo = { this.props.isLocalVideo }
|
|
|
|
onShowMore = { this._onToggleShowMore }
|
|
|
|
packetLoss = { packetLoss }
|
|
|
|
resolution = { resolution }
|
|
|
|
shouldShowMore = { this.state.showMoreStats }
|
|
|
|
transport = { transport } />
|
2017-06-19 20:20:13 +00:00
|
|
|
);
|
|
|
|
}
|
2017-09-26 16:55:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the internal state for automatically hiding the indicator.
|
|
|
|
*
|
|
|
|
* @param {number} percent - The current connection quality percentage
|
|
|
|
* between the values 0 and 100.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_updateIndicatorAutoHide(percent) {
|
|
|
|
if (percent < INDICATOR_DISPLAY_THRESHOLD) {
|
|
|
|
clearTimeout(this.state.autoHideTimeout);
|
|
|
|
this.setState({
|
|
|
|
autoHideTimeout: null,
|
|
|
|
showIndicator: true
|
|
|
|
});
|
|
|
|
} else if (this.state.autoHideTimeout) {
|
|
|
|
// This clause is intentionally left blank because no further action
|
|
|
|
// is needed if the percent is below the threshold and there is an
|
|
|
|
// autoHideTimeout set.
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
autoHideTimeout: setTimeout(() => {
|
|
|
|
this.setState({
|
|
|
|
showIndicator: false
|
|
|
|
});
|
|
|
|
}, interfaceConfig.CONNECTION_INDICATOR_AUTO_HIDE_TIMEOUT)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-06-19 20:20:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 16:55:09 +00:00
|
|
|
export default translate(ConnectionIndicator);
|