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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import { translate } from '../../base/i18n';
  4. import { JitsiParticipantConnectionStatus } from '../../base/lib-jitsi-meet';
  5. import { Popover } from '../../base/popover';
  6. import { ConnectionStatsTable } from '../../connection-stats';
  7. import statsEmitter from '../statsEmitter';
  8. declare var interfaceConfig: Object;
  9. /**
  10. * The connection quality percentage that must be reached to be considered of
  11. * good quality and can result in the connection indicator being hidden.
  12. *
  13. * @type {number}
  14. */
  15. const INDICATOR_DISPLAY_THRESHOLD = 30;
  16. /**
  17. * An array of display configurations for the connection indicator and its bars.
  18. * The ordering is done specifically for faster iteration to find a matching
  19. * configuration to the current connection strength percentage.
  20. *
  21. * @type {Object[]}
  22. */
  23. const QUALITY_TO_WIDTH: Array<Object> = [
  24. // Full (3 bars)
  25. {
  26. colorClass: 'status-high',
  27. percent: INDICATOR_DISPLAY_THRESHOLD,
  28. tip: 'connectionindicator.quality.good',
  29. width: '100%'
  30. },
  31. // 2 bars
  32. {
  33. colorClass: 'status-med',
  34. percent: 10,
  35. tip: 'connectionindicator.quality.nonoptimal',
  36. width: '66%'
  37. },
  38. // 1 bar
  39. {
  40. colorClass: 'status-low',
  41. percent: 0,
  42. tip: 'connectionindicator.quality.poor',
  43. width: '33%'
  44. }
  45. // Note: we never show 0 bars as long as there is a connection.
  46. ];
  47. /**
  48. * The type of the React {@code Component} props of {@link ConnectionIndicator}.
  49. */
  50. type Props = {
  51. /**
  52. * Whether or not the component should ignore setting a visibility class for
  53. * hiding the component when the connection quality is not strong.
  54. */
  55. alwaysVisible: boolean,
  56. /**
  57. * The current condition of the user's connection, matching one of the
  58. * enumerated values in the library.
  59. */
  60. connectionStatus: string,
  61. /**
  62. * Whether or not clicking the indicator should display a popover for more
  63. * details.
  64. */
  65. enableStatsDisplay: boolean,
  66. /**
  67. * The font-size for the icon.
  68. */
  69. iconSize: number,
  70. /**
  71. * Whether or not the displays stats are for local video.
  72. */
  73. isLocalVideo: boolean,
  74. /**
  75. * Relative to the icon from where the popover for more connection details
  76. * should display.
  77. */
  78. statsPopoverPosition: string,
  79. /**
  80. * Invoked to obtain translated strings.
  81. */
  82. t: Function,
  83. /**
  84. * The user ID associated with the displayed connection indication and
  85. * stats.
  86. */
  87. userID: string
  88. };
  89. /**
  90. * The type of the React {@code Component} state of {@link ConnectionIndicator}.
  91. */
  92. type State = {
  93. /**
  94. * The timeout for automatically hiding the indicator.
  95. */
  96. autoHideTimeout: TimeoutID | null,
  97. /**
  98. * Whether or not a CSS class should be applied to the root for hiding the
  99. * connection indicator. By default the indicator should start out hidden
  100. * because the current connection status is not known at mount.
  101. */
  102. showIndicator: boolean,
  103. /**
  104. * Whether or not the popover content should display additional statistics.
  105. */
  106. showMoreStats: boolean,
  107. /**
  108. * Cache of the stats received from subscribing to stats emitting. The keys
  109. * should be the name of the stat. With each stat update, updates stats are
  110. * mixed in with cached stats and a new stats object is set in state.
  111. */
  112. stats: Object
  113. };
  114. /**
  115. * Implements a React {@link Component} which displays the current connection
  116. * quality percentage and has a popover to show more detailed connection stats.
  117. *
  118. * @extends {Component}
  119. */
  120. class ConnectionIndicator extends Component<Props, State> {
  121. /**
  122. * Initializes a new {@code ConnectionIndicator} instance.
  123. *
  124. * @param {Object} props - The read-only properties with which the new
  125. * instance is to be initialized.
  126. */
  127. constructor(props: Props) {
  128. super(props);
  129. this.state = {
  130. autoHideTimeout: null,
  131. showIndicator: false,
  132. showMoreStats: false,
  133. stats: {}
  134. };
  135. // Bind event handlers so they are only bound once for every instance.
  136. this._onStatsUpdated = this._onStatsUpdated.bind(this);
  137. this._onToggleShowMore = this._onToggleShowMore.bind(this);
  138. }
  139. /**
  140. * Starts listening for stat updates.
  141. *
  142. * @inheritdoc
  143. * returns {void}
  144. */
  145. componentDidMount() {
  146. statsEmitter.subscribeToClientStats(
  147. this.props.userID, this._onStatsUpdated);
  148. }
  149. /**
  150. * Updates which user's stats are being listened to.
  151. *
  152. * @inheritdoc
  153. * returns {void}
  154. */
  155. componentDidUpdate(prevProps) {
  156. if (prevProps.userID !== this.props.userID) {
  157. statsEmitter.unsubscribeToClientStats(
  158. prevProps.userID, this._onStatsUpdated);
  159. statsEmitter.subscribeToClientStats(
  160. this.props.userID, this._onStatsUpdated);
  161. }
  162. }
  163. /**
  164. * Cleans up any queued processes, which includes listening for new stats
  165. * and clearing any timeout to hide the indicator.
  166. *
  167. * @private
  168. * @returns {void}
  169. */
  170. componentWillUnmount() {
  171. statsEmitter.unsubscribeToClientStats(
  172. this.props.userID, this._onStatsUpdated);
  173. if (this.state.autoHideTimeout) {
  174. clearTimeout(this.state.autoHideTimeout);
  175. }
  176. }
  177. /**
  178. * Implements React's {@link Component#render()}.
  179. *
  180. * @inheritdoc
  181. * @returns {ReactElement}
  182. */
  183. render() {
  184. const visibilityClass = this._getVisibilityClass();
  185. const rootClassNames = `indicator-container ${visibilityClass}`;
  186. const colorClass = this._getConnectionColorClass();
  187. const indicatorContainerClassNames
  188. = `connection-indicator indicator ${colorClass}`;
  189. return (
  190. <Popover
  191. className = { rootClassNames }
  192. content = { this._renderStatisticsTable() }
  193. disablePopover = { !this.props.enableStatsDisplay }
  194. position = { this.props.statsPopoverPosition }>
  195. <div className = 'popover-trigger'>
  196. <div
  197. className = { indicatorContainerClassNames }
  198. style = {{ fontSize: this.props.iconSize }}>
  199. <div className = 'connection indicatoricon'>
  200. { this._renderIcon() }
  201. </div>
  202. </div>
  203. </div>
  204. </Popover>
  205. );
  206. }
  207. /**
  208. * Returns a CSS class that interprets the current connection status as a
  209. * color.
  210. *
  211. * @private
  212. * @returns {string}
  213. */
  214. _getConnectionColorClass() {
  215. const { connectionStatus } = this.props;
  216. const { percent } = this.state.stats;
  217. const { INACTIVE, INTERRUPTED } = JitsiParticipantConnectionStatus;
  218. if (connectionStatus === INACTIVE) {
  219. return 'status-other';
  220. } else if (connectionStatus === INTERRUPTED) {
  221. return 'status-lost';
  222. } else if (typeof percent === 'undefined') {
  223. return 'status-high';
  224. }
  225. return this._getDisplayConfiguration(percent).colorClass;
  226. }
  227. /**
  228. * Returns a string that describes the current connection status.
  229. *
  230. * @private
  231. * @returns {string}
  232. */
  233. _getConnectionStatusTip() {
  234. let tipKey;
  235. switch (this.props.connectionStatus) {
  236. case JitsiParticipantConnectionStatus.INTERRUPTED:
  237. tipKey = 'connectionindicator.quality.lost';
  238. break;
  239. case JitsiParticipantConnectionStatus.INACTIVE:
  240. tipKey = 'connectionindicator.quality.inactive';
  241. break;
  242. default: {
  243. const { percent } = this.state.stats;
  244. if (typeof percent === 'undefined') {
  245. // If percentage is undefined then there are no stats available
  246. // yet, likely because only a local connection has been
  247. // established so far. Assume a strong connection to start.
  248. tipKey = 'connectionindicator.quality.good';
  249. } else {
  250. const config = this._getDisplayConfiguration(percent);
  251. tipKey = config.tip;
  252. }
  253. }
  254. }
  255. return this.props.t(tipKey);
  256. }
  257. /**
  258. * Get the icon configuration from QUALITY_TO_WIDTH which has a percentage
  259. * that matches or exceeds the passed in percentage. The implementation
  260. * assumes QUALITY_TO_WIDTH is already sorted by highest to lowest
  261. * percentage.
  262. *
  263. * @param {number} percent - The connection percentage, out of 100, to find
  264. * the closest matching configuration for.
  265. * @private
  266. * @returns {Object}
  267. */
  268. _getDisplayConfiguration(percent: number): Object {
  269. return QUALITY_TO_WIDTH.find(x => percent >= x.percent) || {};
  270. }
  271. /**
  272. * Returns additional class names to add to the root of the component. The
  273. * class names are intended to be used for hiding or showing the indicator.
  274. *
  275. * @private
  276. * @returns {string}
  277. */
  278. _getVisibilityClass() {
  279. const { connectionStatus } = this.props;
  280. return this.state.showIndicator
  281. || this.props.alwaysVisible
  282. || connectionStatus === JitsiParticipantConnectionStatus.INTERRUPTED
  283. || connectionStatus === JitsiParticipantConnectionStatus.INACTIVE
  284. ? 'show-connection-indicator' : 'hide-connection-indicator';
  285. }
  286. _onStatsUpdated: (Object) => void;
  287. /**
  288. * Callback invoked when new connection stats associated with the passed in
  289. * user ID are available. Will update the component's display of current
  290. * statistics.
  291. *
  292. * @param {Object} stats - Connection stats from the library.
  293. * @private
  294. * @returns {void}
  295. */
  296. _onStatsUpdated(stats = {}) {
  297. // Rely on React to batch setState actions.
  298. const { connectionQuality } = stats;
  299. const newPercentageState = typeof connectionQuality === 'undefined'
  300. ? {} : { percent: connectionQuality };
  301. const newStats = Object.assign(
  302. {},
  303. this.state.stats,
  304. stats,
  305. newPercentageState);
  306. this.setState({
  307. stats: newStats
  308. });
  309. this._updateIndicatorAutoHide(newStats.percent);
  310. }
  311. _onToggleShowMore: () => void;
  312. /**
  313. * Callback to invoke when the show more link in the popover content is
  314. * clicked. Sets the state which will determine if the popover should show
  315. * additional statistics about the connection.
  316. *
  317. * @returns {void}
  318. */
  319. _onToggleShowMore() {
  320. this.setState({ showMoreStats: !this.state.showMoreStats });
  321. }
  322. /**
  323. * Creates a ReactElement for displaying an icon that represents the current
  324. * connection quality.
  325. *
  326. * @returns {ReactElement}
  327. */
  328. _renderIcon() {
  329. if (this.props.connectionStatus
  330. === JitsiParticipantConnectionStatus.INACTIVE) {
  331. return (
  332. <span className = 'connection_ninja'>
  333. <i className = 'icon-ninja' />
  334. </span>
  335. );
  336. }
  337. let iconWidth;
  338. let emptyIconWrapperClassName = 'connection_empty';
  339. if (this.props.connectionStatus
  340. === JitsiParticipantConnectionStatus.INTERRUPTED) {
  341. // emptyIconWrapperClassName is used by the torture tests to
  342. // identify lost connection status handling.
  343. emptyIconWrapperClassName = 'connection_lost';
  344. iconWidth = '0%';
  345. } else if (typeof this.state.stats.percent === 'undefined') {
  346. iconWidth = '100%';
  347. } else {
  348. const { percent } = this.state.stats;
  349. iconWidth = this._getDisplayConfiguration(percent).width;
  350. }
  351. return [
  352. <span
  353. className = { emptyIconWrapperClassName }
  354. key = 'icon-empty'>
  355. <i className = 'icon-gsm-bars' />
  356. </span>,
  357. <span
  358. className = 'connection_full'
  359. key = 'icon-full'
  360. style = {{ width: iconWidth }}>
  361. <i className = 'icon-gsm-bars' />
  362. </span>
  363. ];
  364. }
  365. /**
  366. * Creates a {@code ConnectionStatisticsTable} instance.
  367. *
  368. * @returns {ReactElement}
  369. */
  370. _renderStatisticsTable() {
  371. const {
  372. bandwidth,
  373. bitrate,
  374. bridgeCount,
  375. e2eRtt,
  376. framerate,
  377. packetLoss,
  378. region,
  379. resolution,
  380. serverRegion,
  381. transport
  382. } = this.state.stats;
  383. return (
  384. <ConnectionStatsTable
  385. bandwidth = { bandwidth }
  386. bitrate = { bitrate }
  387. bridgeCount = { bridgeCount }
  388. connectionSummary = { this._getConnectionStatusTip() }
  389. e2eRtt = { e2eRtt }
  390. framerate = { framerate }
  391. isLocalVideo = { this.props.isLocalVideo }
  392. onShowMore = { this._onToggleShowMore }
  393. packetLoss = { packetLoss }
  394. region = { region }
  395. resolution = { resolution }
  396. serverRegion = { serverRegion }
  397. shouldShowMore = { this.state.showMoreStats }
  398. transport = { transport } />
  399. );
  400. }
  401. /**
  402. * Updates the internal state for automatically hiding the indicator.
  403. *
  404. * @param {number} percent - The current connection quality percentage
  405. * between the values 0 and 100.
  406. * @private
  407. * @returns {void}
  408. */
  409. _updateIndicatorAutoHide(percent) {
  410. if (percent < INDICATOR_DISPLAY_THRESHOLD) {
  411. if (this.state.autoHideTimeout) {
  412. clearTimeout(this.state.autoHideTimeout);
  413. }
  414. this.setState({
  415. autoHideTimeout: null,
  416. showIndicator: true
  417. });
  418. } else if (this.state.autoHideTimeout) {
  419. // This clause is intentionally left blank because no further action
  420. // is needed if the percent is below the threshold and there is an
  421. // autoHideTimeout set.
  422. } else {
  423. this.setState({
  424. autoHideTimeout: setTimeout(() => {
  425. this.setState({
  426. showIndicator: false
  427. });
  428. }, interfaceConfig.CONNECTION_INDICATOR_AUTO_HIDE_TIMEOUT)
  429. });
  430. }
  431. }
  432. }
  433. export default translate(ConnectionIndicator);