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

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