You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConnectionIndicator.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. import { Theme } from '@mui/material';
  2. import { withStyles } from '@mui/styles';
  3. import clsx from 'clsx';
  4. import React from 'react';
  5. import { WithTranslation } from 'react-i18next';
  6. import { connect } from 'react-redux';
  7. import { IReduxState, IStore } from '../../../app/types';
  8. import { translate } from '../../../base/i18n/functions';
  9. import { MEDIA_TYPE } from '../../../base/media/constants';
  10. import {
  11. getLocalParticipant,
  12. getParticipantById,
  13. isScreenShareParticipant
  14. } from '../../../base/participants/functions';
  15. import Popover from '../../../base/popover/components/Popover.web';
  16. import {
  17. getTrackByMediaTypeAndParticipant,
  18. getVirtualScreenshareParticipantTrack
  19. } from '../../../base/tracks/functions';
  20. import { ITrack } from '../../../base/tracks/types';
  21. import {
  22. isTrackStreamingStatusInactive,
  23. isTrackStreamingStatusInterrupted
  24. } from '../../functions';
  25. import AbstractConnectionIndicator, {
  26. IProps as AbstractProps,
  27. IState as AbstractState,
  28. INDICATOR_DISPLAY_THRESHOLD,
  29. mapStateToProps as _abstractMapStateToProps
  30. } from '../AbstractConnectionIndicator';
  31. import ConnectionIndicatorContent from './ConnectionIndicatorContent';
  32. import { ConnectionIndicatorIcon } from './ConnectionIndicatorIcon';
  33. /**
  34. * An array of display configurations for the connection indicator and its bars.
  35. * The ordering is done specifically for faster iteration to find a matching
  36. * configuration to the current connection strength percentage.
  37. *
  38. * @type {Object[]}
  39. */
  40. const QUALITY_TO_WIDTH: Array<{
  41. colorClass: string;
  42. percent: number;
  43. tip: string;
  44. }> = [
  45. // Full (3 bars)
  46. {
  47. colorClass: 'status-high',
  48. percent: INDICATOR_DISPLAY_THRESHOLD,
  49. tip: 'connectionindicator.quality.good'
  50. },
  51. // 2 bars
  52. {
  53. colorClass: 'status-med',
  54. percent: 10,
  55. tip: 'connectionindicator.quality.nonoptimal'
  56. },
  57. // 1 bar
  58. {
  59. colorClass: 'status-low',
  60. percent: 0,
  61. tip: 'connectionindicator.quality.poor'
  62. }
  63. // Note: we never show 0 bars as long as there is a connection.
  64. ];
  65. /**
  66. * The type of the React {@code Component} props of {@link ConnectionIndicator}.
  67. */
  68. interface IProps extends AbstractProps, WithTranslation {
  69. /**
  70. * Disable/enable inactive indicator.
  71. */
  72. _connectionIndicatorInactiveDisabled: boolean;
  73. /**
  74. * Whether the connection status is inactive.
  75. */
  76. _isConnectionStatusInactive: boolean;
  77. /**
  78. * Whether the connection status is interrupted.
  79. */
  80. _isConnectionStatusInterrupted?: boolean;
  81. /**
  82. * Whether the indicator popover is disabled.
  83. */
  84. _popoverDisabled: boolean;
  85. /**
  86. * The participant's video track;.
  87. */
  88. _videoTrack?: ITrack;
  89. /**
  90. * Whether or not the component should ignore setting a visibility class for
  91. * hiding the component when the connection quality is not strong.
  92. */
  93. alwaysVisible: boolean;
  94. /**
  95. * The audio SSRC of this client.
  96. */
  97. audioSsrc?: number;
  98. /**
  99. * An object containing the CSS classes.
  100. */
  101. classes: any;
  102. /**
  103. * The Redux dispatch function.
  104. */
  105. dispatch: IStore['dispatch'];
  106. /**
  107. * Whether or not clicking the indicator should display a popover for more
  108. * details.
  109. */
  110. enableStatsDisplay: boolean;
  111. /**
  112. * The font-size for the icon.
  113. */
  114. iconSize: number;
  115. /**
  116. * Relative to the icon from where the popover for more connection details
  117. * should display.
  118. */
  119. statsPopoverPosition: string;
  120. }
  121. interface IState extends AbstractState {
  122. /**
  123. * Whether popover is visible or not.
  124. */
  125. popoverVisible: boolean;
  126. }
  127. const styles = (theme: Theme) => {
  128. return {
  129. container: {
  130. display: 'inline-block'
  131. },
  132. hidden: {
  133. display: 'none'
  134. },
  135. icon: {
  136. padding: '4px',
  137. borderRadius: '4px',
  138. '&.status-high': {
  139. backgroundColor: theme.palette.success01
  140. },
  141. '&.status-med': {
  142. backgroundColor: theme.palette.warning01
  143. },
  144. '&.status-low': {
  145. backgroundColor: theme.palette.iconError
  146. },
  147. '&.status-disabled': {
  148. background: 'transparent'
  149. },
  150. '&.status-lost': {
  151. backgroundColor: theme.palette.ui05
  152. },
  153. '&.status-other': {
  154. backgroundColor: theme.palette.action01
  155. }
  156. },
  157. inactiveIcon: {
  158. padding: 0,
  159. borderRadius: '50%'
  160. }
  161. };
  162. };
  163. /**
  164. * Implements a React {@link Component} which displays the current connection
  165. * quality percentage and has a popover to show more detailed connection stats.
  166. *
  167. * @augments {Component}
  168. */
  169. class ConnectionIndicator extends AbstractConnectionIndicator<IProps, IState> {
  170. /**
  171. * Initializes a new {@code ConnectionIndicator} instance.
  172. *
  173. * @param {Object} props - The read-only properties with which the new
  174. * instance is to be initialized.
  175. */
  176. constructor(props: IProps) {
  177. super(props);
  178. this.state = {
  179. showIndicator: false,
  180. stats: {},
  181. popoverVisible: false
  182. };
  183. this._onShowPopover = this._onShowPopover.bind(this);
  184. this._onHidePopover = this._onHidePopover.bind(this);
  185. }
  186. /**
  187. * Implements React's {@link Component#render()}.
  188. *
  189. * @inheritdoc
  190. * @returns {ReactElement}
  191. */
  192. render() {
  193. const { enableStatsDisplay, participantId, statsPopoverPosition, classes, t } = this.props;
  194. const visibilityClass = this._getVisibilityClass();
  195. if (this.props._popoverDisabled) {
  196. return this._renderIndicator();
  197. }
  198. return (
  199. <Popover
  200. className = { clsx(classes.container, visibilityClass) }
  201. content = { <ConnectionIndicatorContent
  202. inheritedStats = { this.state.stats }
  203. participantId = { participantId } /> }
  204. disablePopover = { !enableStatsDisplay }
  205. headingLabel = { t('videothumbnail.connectionInfo') }
  206. id = 'participant-connection-indicator'
  207. onPopoverClose = { this._onHidePopover }
  208. onPopoverOpen = { this._onShowPopover }
  209. position = { statsPopoverPosition }
  210. visible = { this.state.popoverVisible }>
  211. { this._renderIndicator() }
  212. </Popover>
  213. );
  214. }
  215. /**
  216. * Returns a CSS class that interprets the current connection status as a
  217. * color.
  218. *
  219. * @private
  220. * @returns {string}
  221. */
  222. _getConnectionColorClass() {
  223. // TODO We currently do not have logic to emit and handle stats changes for tracks.
  224. const { percent } = this.state.stats;
  225. const {
  226. _isConnectionStatusInactive,
  227. _isConnectionStatusInterrupted,
  228. _connectionIndicatorInactiveDisabled
  229. } = this.props;
  230. if (_isConnectionStatusInactive) {
  231. if (_connectionIndicatorInactiveDisabled) {
  232. return 'status-disabled';
  233. }
  234. return 'status-other';
  235. } else if (_isConnectionStatusInterrupted) {
  236. return 'status-lost';
  237. } else if (typeof percent === 'undefined') {
  238. return 'status-high';
  239. }
  240. return this._getDisplayConfiguration(percent).colorClass;
  241. }
  242. /**
  243. * Get the icon configuration from QUALITY_TO_WIDTH which has a percentage
  244. * that matches or exceeds the passed in percentage. The implementation
  245. * assumes QUALITY_TO_WIDTH is already sorted by highest to lowest
  246. * percentage.
  247. *
  248. * @param {number} percent - The connection percentage, out of 100, to find
  249. * the closest matching configuration for.
  250. * @private
  251. * @returns {Object}
  252. */
  253. _getDisplayConfiguration(percent: number): any {
  254. return QUALITY_TO_WIDTH.find(x => percent >= x.percent) || {};
  255. }
  256. /**
  257. * Returns additional class names to add to the root of the component. The
  258. * class names are intended to be used for hiding or showing the indicator.
  259. *
  260. * @private
  261. * @returns {string}
  262. */
  263. _getVisibilityClass() {
  264. const { _isConnectionStatusInactive, _isConnectionStatusInterrupted, classes } = this.props;
  265. return this.state.showIndicator
  266. || this.props.alwaysVisible
  267. || _isConnectionStatusInterrupted
  268. || _isConnectionStatusInactive
  269. ? '' : classes.hidden;
  270. }
  271. /**
  272. * Hides popover.
  273. *
  274. * @private
  275. * @returns {void}
  276. */
  277. _onHidePopover() {
  278. this.setState({ popoverVisible: false });
  279. }
  280. /**
  281. * Shows popover.
  282. *
  283. * @private
  284. * @returns {void}
  285. */
  286. _onShowPopover() {
  287. this.setState({ popoverVisible: true });
  288. }
  289. /**
  290. * Creates a ReactElement for displaying the indicator (GSM bar).
  291. *
  292. * @returns {ReactElement}
  293. */
  294. _renderIndicator() {
  295. const {
  296. _isConnectionStatusInactive,
  297. _isConnectionStatusInterrupted,
  298. _connectionIndicatorInactiveDisabled,
  299. _videoTrack,
  300. classes,
  301. iconSize,
  302. t
  303. } = this.props;
  304. return (
  305. <div
  306. style = {{ fontSize: iconSize }}>
  307. <span className = 'sr-only'>{ t('videothumbnail.connectionInfo') }</span>
  308. <ConnectionIndicatorIcon
  309. classes = { classes }
  310. colorClass = { this._getConnectionColorClass() }
  311. connectionIndicatorInactiveDisabled = { _connectionIndicatorInactiveDisabled }
  312. isConnectionStatusInactive = { _isConnectionStatusInactive }
  313. isConnectionStatusInterrupted = { _isConnectionStatusInterrupted }
  314. track = { _videoTrack } />
  315. </div>
  316. );
  317. }
  318. }
  319. /**
  320. * Maps part of the Redux state to the props of this component.
  321. *
  322. * @param {Object} state - The Redux state.
  323. * @param {IProps} ownProps - The own props of the component.
  324. * @returns {IProps}
  325. */
  326. export function _mapStateToProps(state: IReduxState, ownProps: any) {
  327. const { participantId } = ownProps;
  328. const tracks = state['features/base/tracks'];
  329. const participant = participantId ? getParticipantById(state, participantId) : getLocalParticipant(state);
  330. let _videoTrack = getTrackByMediaTypeAndParticipant(tracks, MEDIA_TYPE.VIDEO, participantId);
  331. if (isScreenShareParticipant(participant)) {
  332. _videoTrack = getVirtualScreenshareParticipantTrack(tracks, participantId);
  333. }
  334. const _isConnectionStatusInactive = isTrackStreamingStatusInactive(_videoTrack);
  335. const _isConnectionStatusInterrupted = isTrackStreamingStatusInterrupted(_videoTrack);
  336. return {
  337. ..._abstractMapStateToProps(state),
  338. _connectionIndicatorInactiveDisabled:
  339. Boolean(state['features/base/config'].connectionIndicators?.inactiveDisabled),
  340. _isVirtualScreenshareParticipant: isScreenShareParticipant(participant),
  341. _popoverDisabled: Boolean(state['features/base/config'].connectionIndicators?.disableDetails),
  342. _isConnectionStatusInactive,
  343. _isConnectionStatusInterrupted,
  344. _videoTrack
  345. };
  346. }
  347. export default connect(_mapStateToProps)(translate(withStyles(styles)(ConnectionIndicator)));