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.js 12KB

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