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

ConnectionIndicator.js 11KB

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