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.ts 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import { Component } from 'react';
  2. import { IReduxState } from '../../app/types';
  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 interface IProps {
  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. * Custom icon style.
  27. */
  28. iconStyle?: Object;
  29. /**
  30. * The ID of the participant associated with the displayed connection indication and
  31. * stats.
  32. */
  33. participantId: string;
  34. }
  35. /**
  36. * The type of the React {@code Component} state of {@link ConnectionIndicator}.
  37. */
  38. export interface IState {
  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: {
  51. bandwidth?: any;
  52. bitrate?: any;
  53. bridgeCount?: any;
  54. codec?: any;
  55. framerate?: any;
  56. maxEnabledResolution?: any;
  57. packetLoss?: any;
  58. percent?: number;
  59. resolution?: any;
  60. serverRegion?: any;
  61. transport?: any;
  62. };
  63. }
  64. /**
  65. * Implements a React {@link Component} which displays the current connection
  66. * quality.
  67. *
  68. * @augments {Component}
  69. */
  70. class AbstractConnectionIndicator<P extends IProps, S extends IState> extends Component<P, S> {
  71. /**
  72. * The timeout for automatically hiding the indicator.
  73. */
  74. autoHideTimeout: number | undefined;
  75. /**
  76. * Initializes a new {@code ConnectionIndicator} instance.
  77. *
  78. * @param {P} props - The read-only properties with which the new
  79. * instance is to be initialized.
  80. */
  81. constructor(props: P) {
  82. super(props);
  83. // Bind event handlers so they are only bound once for every instance.
  84. this._onStatsUpdated = this._onStatsUpdated.bind(this);
  85. }
  86. /**
  87. * Starts listening for stat updates.
  88. *
  89. * @inheritdoc
  90. * returns {void}
  91. */
  92. componentDidMount() {
  93. statsEmitter.subscribeToClientStats(this._getRealParticipantId(this.props), this._onStatsUpdated);
  94. }
  95. /**
  96. * Updates which user's stats are being listened to.
  97. *
  98. * @inheritdoc
  99. * returns {void}
  100. */
  101. componentDidUpdate(prevProps: IProps) {
  102. const prevParticipantId = this._getRealParticipantId(prevProps);
  103. const participantId = this._getRealParticipantId(this.props);
  104. if (prevParticipantId !== participantId) {
  105. statsEmitter.unsubscribeToClientStats(prevParticipantId, this._onStatsUpdated);
  106. statsEmitter.subscribeToClientStats(participantId, this._onStatsUpdated);
  107. }
  108. }
  109. /**
  110. * Cleans up any queued processes, which includes listening for new stats
  111. * and clearing any timeout to hide the indicator.
  112. *
  113. * @private
  114. * @returns {void}
  115. */
  116. componentWillUnmount() {
  117. statsEmitter.unsubscribeToClientStats(this._getRealParticipantId(this.props), this._onStatsUpdated);
  118. clearTimeout(this.autoHideTimeout ?? 0);
  119. }
  120. /**
  121. * Gets the "real" participant ID. FOr a virtual screenshare participant, that is its "owner".
  122. *
  123. * @param {Props} props - The props where to extract the data from.
  124. * @returns {string | undefined } The resolved participant ID.
  125. */
  126. _getRealParticipantId(props: IProps) {
  127. if (props._isVirtualScreenshareParticipant) {
  128. return getVirtualScreenshareParticipantOwnerId(props.participantId);
  129. }
  130. return props.participantId;
  131. }
  132. /**
  133. * Callback invoked when new connection stats associated with the passed in
  134. * user ID are available. Will update the component's display of current
  135. * statistics.
  136. *
  137. * @param {Object} stats - Connection stats from the library.
  138. * @private
  139. * @returns {void}
  140. */
  141. _onStatsUpdated(stats = { connectionQuality: undefined }) {
  142. // Rely on React to batch setState actions.
  143. const { connectionQuality } = stats;
  144. const newPercentageState = typeof connectionQuality === 'undefined'
  145. ? {} : { percent: connectionQuality };
  146. const newStats = Object.assign(
  147. {},
  148. this.state.stats,
  149. stats,
  150. newPercentageState);
  151. this.setState({
  152. stats: newStats
  153. });
  154. this._updateIndicatorAutoHide(newStats.percent ?? 0);
  155. }
  156. /**
  157. * Updates the internal state for automatically hiding the indicator.
  158. *
  159. * @param {number} percent - The current connection quality percentage
  160. * between the values 0 and 100.
  161. * @private
  162. * @returns {void}
  163. */
  164. _updateIndicatorAutoHide(percent: number) {
  165. if (percent < INDICATOR_DISPLAY_THRESHOLD) {
  166. clearTimeout(this.autoHideTimeout ?? 0);
  167. this.autoHideTimeout = undefined;
  168. this.setState({
  169. showIndicator: true
  170. });
  171. } else if (this.autoHideTimeout) {
  172. // This clause is intentionally left blank because no further action
  173. // is needed if the percent is below the threshold and there is an
  174. // autoHideTimeout set.
  175. } else {
  176. this.autoHideTimeout = window.setTimeout(() => {
  177. this.setState({
  178. showIndicator: false
  179. });
  180. }, this.props._autoHideTimeout);
  181. }
  182. }
  183. }
  184. /**
  185. * Maps (parts of) the Redux state to the associated props for the
  186. * {@code ConnectorIndicator} component.
  187. *
  188. * @param {Object} state - The Redux state.
  189. * @private
  190. * @returns {IProps}
  191. */
  192. export function mapStateToProps(state: IReduxState) {
  193. return {
  194. _autoHideTimeout: state['features/base/config'].connectionIndicators?.autoHideTimeout ?? defaultAutoHideTimeout
  195. };
  196. }
  197. export default AbstractConnectionIndicator;