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.

AbstractConnectionIndicator.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // @flow
  2. import { Component } from 'react';
  3. import { getVirtualScreenshareParticipantOwnerId } from '../../base/participants/functions';
  4. import statsEmitter from '../statsEmitter';
  5. const defaultAutoHideTimeout = 5000;
  6. /**
  7. * The connection quality percentage that must be reached to be considered of
  8. * good quality and can result in the connection indicator being hidden.
  9. *
  10. * @type {number}
  11. */
  12. export const INDICATOR_DISPLAY_THRESHOLD = 30;
  13. /**
  14. * The type of the React {@code Component} props of {@link ConnectionIndicator}.
  15. */
  16. export type Props = {
  17. /**
  18. * How long the connection indicator should remain displayed before hiding.
  19. */
  20. _autoHideTimeout: number,
  21. /**
  22. * Whether or not the statistics are for screen share.
  23. */
  24. _isVirtualScreenshareParticipant: boolean,
  25. /**
  26. * The ID of the participant associated with the displayed connection indication and
  27. * stats.
  28. */
  29. participantId: string,
  30. /**
  31. * Custom icon style.
  32. */
  33. iconStyle?: Object
  34. };
  35. /**
  36. * The type of the React {@code Component} state of {@link ConnectionIndicator}.
  37. */
  38. export type State = {
  39. /**
  40. * Whether or not a CSS class should be applied to the root for hiding the
  41. * connection indicator. By default the indicator should start out hidden
  42. * because the current connection status is not known at mount.
  43. */
  44. showIndicator: boolean,
  45. /**
  46. * Cache of the stats received from subscribing to stats emitting. The keys
  47. * should be the name of the stat. With each stat update, updates stats are
  48. * mixed in with cached stats and a new stats object is set in state.
  49. */
  50. stats: Object
  51. };
  52. /**
  53. * Implements a React {@link Component} which displays the current connection
  54. * quality.
  55. *
  56. * @augments {Component}
  57. */
  58. class AbstractConnectionIndicator<P: Props, S: State> extends Component<P, S> {
  59. /**
  60. * The timeout for automatically hiding the indicator.
  61. */
  62. autoHideTimeout: ?TimeoutID;
  63. /**
  64. * Initializes a new {@code ConnectionIndicator} instance.
  65. *
  66. * @param {P} props - The read-only properties with which the new
  67. * instance is to be initialized.
  68. */
  69. constructor(props: P) {
  70. super(props);
  71. // Bind event handlers so they are only bound once for every instance.
  72. this._onStatsUpdated = this._onStatsUpdated.bind(this);
  73. }
  74. /**
  75. * Starts listening for stat updates.
  76. *
  77. * @inheritdoc
  78. * returns {void}
  79. */
  80. componentDidMount() {
  81. statsEmitter.subscribeToClientStats(this._getRealParticipantId(this.props), this._onStatsUpdated);
  82. }
  83. /**
  84. * Updates which user's stats are being listened to.
  85. *
  86. * @inheritdoc
  87. * returns {void}
  88. */
  89. componentDidUpdate(prevProps: Props) {
  90. const prevParticipantId = this._getRealParticipantId(prevProps);
  91. const participantId = this._getRealParticipantId(this.props);
  92. if (prevParticipantId !== participantId) {
  93. statsEmitter.unsubscribeToClientStats(prevParticipantId, this._onStatsUpdated);
  94. statsEmitter.subscribeToClientStats(participantId, this._onStatsUpdated);
  95. }
  96. }
  97. /**
  98. * Cleans up any queued processes, which includes listening for new stats
  99. * and clearing any timeout to hide the indicator.
  100. *
  101. * @private
  102. * @returns {void}
  103. */
  104. componentWillUnmount() {
  105. statsEmitter.unsubscribeToClientStats(this._getRealParticipantId(this.props), this._onStatsUpdated);
  106. clearTimeout(this.autoHideTimeout);
  107. }
  108. /**
  109. * Gets the "real" participant ID. FOr a virtual screenshare participant, that is its "owner".
  110. *
  111. * @param {Props} props - The props where to extract the data from.
  112. * @returns {string | undefined } The resolved participant ID.
  113. */
  114. _getRealParticipantId(props: Props) {
  115. if (props._isVirtualScreenshareParticipant) {
  116. return getVirtualScreenshareParticipantOwnerId(props.participantId);
  117. }
  118. return props.participantId;
  119. }
  120. _onStatsUpdated: (Object) => void;
  121. /**
  122. * Callback invoked when new connection stats associated with the passed in
  123. * user ID are available. Will update the component's display of current
  124. * statistics.
  125. *
  126. * @param {Object} stats - Connection stats from the library.
  127. * @private
  128. * @returns {void}
  129. */
  130. _onStatsUpdated(stats = {}) {
  131. // Rely on React to batch setState actions.
  132. const { connectionQuality } = stats;
  133. const newPercentageState = typeof connectionQuality === 'undefined'
  134. ? {} : { percent: connectionQuality };
  135. const newStats = Object.assign(
  136. {},
  137. this.state.stats,
  138. stats,
  139. newPercentageState);
  140. this.setState({
  141. stats: newStats
  142. });
  143. this._updateIndicatorAutoHide(newStats.percent);
  144. }
  145. /**
  146. * Updates the internal state for automatically hiding the indicator.
  147. *
  148. * @param {number} percent - The current connection quality percentage
  149. * between the values 0 and 100.
  150. * @private
  151. * @returns {void}
  152. */
  153. _updateIndicatorAutoHide(percent) {
  154. if (percent < INDICATOR_DISPLAY_THRESHOLD) {
  155. clearTimeout(this.autoHideTimeout);
  156. this.autoHideTimeout = undefined;
  157. this.setState({
  158. showIndicator: true
  159. });
  160. } else if (this.autoHideTimeout) {
  161. // This clause is intentionally left blank because no further action
  162. // is needed if the percent is below the threshold and there is an
  163. // autoHideTimeout set.
  164. } else {
  165. this.autoHideTimeout = setTimeout(() => {
  166. this.setState({
  167. showIndicator: false
  168. });
  169. }, this.props._autoHideTimeout);
  170. }
  171. }
  172. }
  173. /**
  174. * Maps (parts of) the Redux state to the associated props for the
  175. * {@code ConnectorIndicator} component.
  176. *
  177. * @param {Object} state - The Redux state.
  178. * @private
  179. * @returns {Props}
  180. */
  181. export function mapStateToProps(state: Object) {
  182. return {
  183. _autoHideTimeout: state['features/base/config'].connectionIndicators?.autoHideTimeout ?? defaultAutoHideTimeout
  184. };
  185. }
  186. export default AbstractConnectionIndicator;