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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // @flow
  2. import React from 'react';
  3. import { translate } from '../../../base/i18n';
  4. import { Icon, IconConnectionActive, IconConnectionInactive } from '../../../base/icons';
  5. import { JitsiParticipantConnectionStatus } from '../../../base/lib-jitsi-meet';
  6. import { Popover } from '../../../base/popover';
  7. import { ConnectionStatsTable } from '../../../connection-stats';
  8. import AbstractConnectionIndicator, {
  9. INDICATOR_DISPLAY_THRESHOLD,
  10. type Props as AbstractProps,
  11. type State as AbstractState
  12. } from '../AbstractConnectionIndicator';
  13. declare var interfaceConfig: Object;
  14. /**
  15. * An array of display configurations for the connection indicator and its bars.
  16. * The ordering is done specifically for faster iteration to find a matching
  17. * configuration to the current connection strength percentage.
  18. *
  19. * @type {Object[]}
  20. */
  21. const QUALITY_TO_WIDTH: Array<Object> = [
  22. // Full (3 bars)
  23. {
  24. colorClass: 'status-high',
  25. percent: INDICATOR_DISPLAY_THRESHOLD,
  26. tip: 'connectionindicator.quality.good',
  27. width: '100%'
  28. },
  29. // 2 bars
  30. {
  31. colorClass: 'status-med',
  32. percent: 10,
  33. tip: 'connectionindicator.quality.nonoptimal',
  34. width: '66%'
  35. },
  36. // 1 bar
  37. {
  38. colorClass: 'status-low',
  39. percent: 0,
  40. tip: 'connectionindicator.quality.poor',
  41. width: '33%'
  42. }
  43. // Note: we never show 0 bars as long as there is a connection.
  44. ];
  45. /**
  46. * The type of the React {@code Component} props of {@link ConnectionIndicator}.
  47. */
  48. type Props = AbstractProps & {
  49. /**
  50. * Whether or not the component should ignore setting a visibility class for
  51. * hiding the component when the connection quality is not strong.
  52. */
  53. alwaysVisible: boolean,
  54. /**
  55. * The current condition of the user's connection, matching one of the
  56. * enumerated values in the library.
  57. */
  58. connectionStatus: string,
  59. /**
  60. * Whether or not clicking the indicator should display a popover for more
  61. * details.
  62. */
  63. enableStatsDisplay: boolean,
  64. /**
  65. * The font-size for the icon.
  66. */
  67. iconSize: number,
  68. /**
  69. * Whether or not the displays stats are for local video.
  70. */
  71. isLocalVideo: boolean,
  72. /**
  73. * Relative to the icon from where the popover for more connection details
  74. * should display.
  75. */
  76. statsPopoverPosition: string,
  77. /**
  78. * Invoked to obtain translated strings.
  79. */
  80. t: Function
  81. };
  82. /**
  83. * The type of the React {@code Component} state of {@link ConnectionIndicator}.
  84. */
  85. type State = AbstractState & {
  86. /**
  87. * Whether or not the popover content should display additional statistics.
  88. */
  89. showMoreStats: boolean
  90. };
  91. /**
  92. * Implements a React {@link Component} which displays the current connection
  93. * quality percentage and has a popover to show more detailed connection stats.
  94. *
  95. * @extends {Component}
  96. */
  97. class ConnectionIndicator extends AbstractConnectionIndicator<Props, State> {
  98. /**
  99. * Initializes a new {@code ConnectionIndicator} instance.
  100. *
  101. * @param {Object} props - The read-only properties with which the new
  102. * instance is to be initialized.
  103. */
  104. constructor(props: Props) {
  105. super(props);
  106. this.state = {
  107. autoHideTimeout: undefined,
  108. showIndicator: false,
  109. showMoreStats: false,
  110. stats: {}
  111. };
  112. // Bind event handlers so they are only bound once for every instance.
  113. this._onToggleShowMore = this._onToggleShowMore.bind(this);
  114. }
  115. /**
  116. * Implements React's {@link Component#render()}.
  117. *
  118. * @inheritdoc
  119. * @returns {ReactElement}
  120. */
  121. render() {
  122. const visibilityClass = this._getVisibilityClass();
  123. const rootClassNames = `indicator-container ${visibilityClass}`;
  124. const colorClass = this._getConnectionColorClass();
  125. const indicatorContainerClassNames
  126. = `connection-indicator indicator ${colorClass}`;
  127. return (
  128. <Popover
  129. className = { rootClassNames }
  130. content = { this._renderStatisticsTable() }
  131. disablePopover = { !this.props.enableStatsDisplay }
  132. position = { this.props.statsPopoverPosition }>
  133. <div className = 'popover-trigger'>
  134. <div
  135. className = { indicatorContainerClassNames }
  136. style = {{ fontSize: this.props.iconSize }}>
  137. <div className = 'connection indicatoricon'>
  138. { this._renderIcon() }
  139. </div>
  140. </div>
  141. </div>
  142. </Popover>
  143. );
  144. }
  145. /**
  146. * Returns a CSS class that interprets the current connection status as a
  147. * color.
  148. *
  149. * @private
  150. * @returns {string}
  151. */
  152. _getConnectionColorClass() {
  153. const { connectionStatus } = this.props;
  154. const { percent } = this.state.stats;
  155. const { INACTIVE, INTERRUPTED } = JitsiParticipantConnectionStatus;
  156. if (connectionStatus === INACTIVE) {
  157. return 'status-other';
  158. } else if (connectionStatus === INTERRUPTED) {
  159. return 'status-lost';
  160. } else if (typeof percent === 'undefined') {
  161. return 'status-high';
  162. }
  163. return this._getDisplayConfiguration(percent).colorClass;
  164. }
  165. /**
  166. * Returns a string that describes the current connection status.
  167. *
  168. * @private
  169. * @returns {string}
  170. */
  171. _getConnectionStatusTip() {
  172. let tipKey;
  173. switch (this.props.connectionStatus) {
  174. case JitsiParticipantConnectionStatus.INTERRUPTED:
  175. tipKey = 'connectionindicator.quality.lost';
  176. break;
  177. case JitsiParticipantConnectionStatus.INACTIVE:
  178. tipKey = 'connectionindicator.quality.inactive';
  179. break;
  180. default: {
  181. const { percent } = this.state.stats;
  182. if (typeof percent === 'undefined') {
  183. // If percentage is undefined then there are no stats available
  184. // yet, likely because only a local connection has been
  185. // established so far. Assume a strong connection to start.
  186. tipKey = 'connectionindicator.quality.good';
  187. } else {
  188. const config = this._getDisplayConfiguration(percent);
  189. tipKey = config.tip;
  190. }
  191. }
  192. }
  193. return this.props.t(tipKey);
  194. }
  195. /**
  196. * Get the icon configuration from QUALITY_TO_WIDTH which has a percentage
  197. * that matches or exceeds the passed in percentage. The implementation
  198. * assumes QUALITY_TO_WIDTH is already sorted by highest to lowest
  199. * percentage.
  200. *
  201. * @param {number} percent - The connection percentage, out of 100, to find
  202. * the closest matching configuration for.
  203. * @private
  204. * @returns {Object}
  205. */
  206. _getDisplayConfiguration(percent: number): Object {
  207. return QUALITY_TO_WIDTH.find(x => percent >= x.percent) || {};
  208. }
  209. /**
  210. * Returns additional class names to add to the root of the component. The
  211. * class names are intended to be used for hiding or showing the indicator.
  212. *
  213. * @private
  214. * @returns {string}
  215. */
  216. _getVisibilityClass() {
  217. const { connectionStatus } = this.props;
  218. return this.state.showIndicator
  219. || this.props.alwaysVisible
  220. || connectionStatus === JitsiParticipantConnectionStatus.INTERRUPTED
  221. || connectionStatus === JitsiParticipantConnectionStatus.INACTIVE
  222. ? 'show-connection-indicator' : 'hide-connection-indicator';
  223. }
  224. _onToggleShowMore: () => void;
  225. /**
  226. * Callback to invoke when the show more link in the popover content is
  227. * clicked. Sets the state which will determine if the popover should show
  228. * additional statistics about the connection.
  229. *
  230. * @returns {void}
  231. */
  232. _onToggleShowMore() {
  233. this.setState({ showMoreStats: !this.state.showMoreStats });
  234. }
  235. /**
  236. * Creates a ReactElement for displaying an icon that represents the current
  237. * connection quality.
  238. *
  239. * @returns {ReactElement}
  240. */
  241. _renderIcon() {
  242. if (this.props.connectionStatus
  243. === JitsiParticipantConnectionStatus.INACTIVE) {
  244. return (
  245. <span className = 'connection_ninja'>
  246. <Icon
  247. className = 'icon-ninja'
  248. size = '1.5em'
  249. src = { IconConnectionInactive } />
  250. </span>
  251. );
  252. }
  253. let iconWidth;
  254. let emptyIconWrapperClassName = 'connection_empty';
  255. if (this.props.connectionStatus
  256. === JitsiParticipantConnectionStatus.INTERRUPTED) {
  257. // emptyIconWrapperClassName is used by the torture tests to
  258. // identify lost connection status handling.
  259. emptyIconWrapperClassName = 'connection_lost';
  260. iconWidth = '0%';
  261. } else if (typeof this.state.stats.percent === 'undefined') {
  262. iconWidth = '100%';
  263. } else {
  264. const { percent } = this.state.stats;
  265. iconWidth = this._getDisplayConfiguration(percent).width;
  266. }
  267. return [
  268. <span
  269. className = { emptyIconWrapperClassName }
  270. key = 'icon-empty'>
  271. <Icon
  272. className = 'icon-gsm-bars'
  273. size = '1em'
  274. src = { IconConnectionActive } />
  275. </span>,
  276. <span
  277. className = 'connection_full'
  278. key = 'icon-full'
  279. style = {{ width: iconWidth }}>
  280. <Icon
  281. className = 'icon-gsm-bars'
  282. size = '1em'
  283. src = { IconConnectionActive } />
  284. </span>
  285. ];
  286. }
  287. /**
  288. * Creates a {@code ConnectionStatisticsTable} instance.
  289. *
  290. * @returns {ReactElement}
  291. */
  292. _renderStatisticsTable() {
  293. const {
  294. bandwidth,
  295. bitrate,
  296. bridgeCount,
  297. e2eRtt,
  298. framerate,
  299. packetLoss,
  300. region,
  301. resolution,
  302. serverRegion,
  303. transport
  304. } = this.state.stats;
  305. return (
  306. <ConnectionStatsTable
  307. bandwidth = { bandwidth }
  308. bitrate = { bitrate }
  309. bridgeCount = { bridgeCount }
  310. connectionSummary = { this._getConnectionStatusTip() }
  311. e2eRtt = { e2eRtt }
  312. framerate = { framerate }
  313. isLocalVideo = { this.props.isLocalVideo }
  314. onShowMore = { this._onToggleShowMore }
  315. packetLoss = { packetLoss }
  316. region = { region }
  317. resolution = { resolution }
  318. serverRegion = { serverRegion }
  319. shouldShowMore = { this.state.showMoreStats }
  320. transport = { transport } />
  321. );
  322. }
  323. }
  324. export default translate(ConnectionIndicator);