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

ConnectionIndicator.js 11KB

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