您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ConnectionIndicator.tsx 11KB

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