Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ConnectionIndicator.tsx 12KB

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