Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ConnectionIndicator.tsx 11KB

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