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.tsx 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* eslint-disable lines-around-comment */
  2. import React from 'react';
  3. import { View } from 'react-native';
  4. import { IReduxState } from '../../../app/types';
  5. import { IconConnection } from '../../../base/icons/svg';
  6. import { MEDIA_TYPE } from '../../../base/media/constants';
  7. import {
  8. getLocalParticipant,
  9. getParticipantById,
  10. isScreenShareParticipant
  11. } from '../../../base/participants/functions';
  12. // @ts-ignore
  13. import BaseIndicator from '../../../base/react/components/native/BaseIndicator';
  14. import { connect } from '../../../base/redux/functions';
  15. import {
  16. getTrackByMediaTypeAndParticipant
  17. } from '../../../base/tracks/functions.native';
  18. // @ts-ignore
  19. import indicatorStyles from '../../../filmstrip/components/native/styles';
  20. import {
  21. isTrackStreamingStatusInactive,
  22. isTrackStreamingStatusInterrupted
  23. } from '../../functions';
  24. import AbstractConnectionIndicator, {
  25. type Props as AbstractProps,
  26. mapStateToProps as _abstractMapStateToProps
  27. // @ts-ignore
  28. } from '../AbstractConnectionIndicator';
  29. import {
  30. CONNECTOR_INDICATOR_COLORS,
  31. CONNECTOR_INDICATOR_LOST,
  32. CONNECTOR_INDICATOR_OTHER,
  33. iconStyle
  34. } from './styles';
  35. type IProps = AbstractProps & {
  36. /**
  37. * Whether connection indicators are disabled or not.
  38. */
  39. _connectionIndicatorDisabled: boolean;
  40. /**
  41. * Whether the inactive connection indicator is disabled or not.
  42. */
  43. _connectionIndicatorInactiveDisabled: boolean;
  44. /**
  45. * Whether the connection is inactive or not.
  46. */
  47. _isConnectionStatusInactive: boolean;
  48. /**
  49. * Whether the connection is interrupted or not.
  50. */
  51. _isConnectionStatusInterrupted: boolean;
  52. /**
  53. * Whether the current participant is a virtual screenshare.
  54. */
  55. _isVirtualScreenshareParticipant: boolean;
  56. /**
  57. * Redux dispatch function.
  58. */
  59. dispatch: Function;
  60. /**
  61. * Icon style override.
  62. */
  63. iconStyle: any;
  64. };
  65. type IState = {
  66. autoHideTimeout: number | undefined;
  67. showIndicator: boolean;
  68. stats: any;
  69. };
  70. /**
  71. * Implements an indicator to show the quality of the connection of a participant.
  72. */
  73. class ConnectionIndicator extends AbstractConnectionIndicator<IProps, IState> {
  74. /**
  75. * Initializes a new {@code ConnectionIndicator} instance.
  76. *
  77. * @inheritdoc
  78. */
  79. constructor(props: IProps) {
  80. super(props);
  81. // @ts-ignore
  82. this.state = {
  83. autoHideTimeout: undefined,
  84. showIndicator: false,
  85. stats: {}
  86. };
  87. }
  88. /**
  89. * Get the icon configuration from CONNECTOR_INDICATOR_COLORS which has a percentage
  90. * that matches or exceeds the passed in percentage. The implementation
  91. * assumes CONNECTOR_INDICATOR_COLORS is already sorted by highest to lowest
  92. * percentage.
  93. *
  94. * @param {number} percent - The connection percentage, out of 100, to find
  95. * the closest matching configuration for.
  96. * @private
  97. * @returns {Object}
  98. */
  99. _getDisplayConfiguration(percent: number): any {
  100. return CONNECTOR_INDICATOR_COLORS.find(x => percent >= x.percent) || {};
  101. }
  102. /**
  103. * Implements React's {@link Component#render()}.
  104. *
  105. * @inheritdoc
  106. * @returns {ReactElement}
  107. */
  108. render() {
  109. const {
  110. _connectionIndicatorInactiveDisabled,
  111. _connectionIndicatorDisabled,
  112. _isVirtualScreenshareParticipant,
  113. _isConnectionStatusInactive,
  114. _isConnectionStatusInterrupted
  115. // @ts-ignore
  116. } = this.props;
  117. const {
  118. showIndicator,
  119. stats
  120. // @ts-ignore
  121. } = this.state;
  122. const { percent } = stats;
  123. if (!showIndicator || typeof percent === 'undefined'
  124. || _connectionIndicatorDisabled || _isVirtualScreenshareParticipant) {
  125. return null;
  126. }
  127. let indicatorColor;
  128. if (_isConnectionStatusInactive) {
  129. if (_connectionIndicatorInactiveDisabled) {
  130. return null;
  131. }
  132. indicatorColor = CONNECTOR_INDICATOR_OTHER;
  133. } else if (_isConnectionStatusInterrupted) {
  134. indicatorColor = CONNECTOR_INDICATOR_LOST;
  135. } else {
  136. const displayConfig = this._getDisplayConfiguration(percent);
  137. if (!displayConfig) {
  138. return null;
  139. }
  140. indicatorColor = displayConfig.color;
  141. }
  142. return (
  143. <View
  144. style = {{
  145. ...indicatorStyles.indicatorContainer,
  146. backgroundColor: indicatorColor
  147. }}>
  148. <BaseIndicator
  149. icon = { IconConnection }
  150. // @ts-ignore
  151. iconStyle = { this.props.iconStyle || iconStyle } />
  152. </View>
  153. );
  154. }
  155. }
  156. /**
  157. * Maps part of the Redux state to the props of this component.
  158. *
  159. * @param {Object} state - The Redux state.
  160. * @param {IProps} ownProps - The own props of the component.
  161. * @returns {IProps}
  162. */
  163. export function _mapStateToProps(state: IReduxState, ownProps: IProps) {
  164. const { participantId } = ownProps;
  165. const tracks = state['features/base/tracks'];
  166. const participant = participantId ? getParticipantById(state, participantId) : getLocalParticipant(state);
  167. const _isVirtualScreenshareParticipant = isScreenShareParticipant(participant);
  168. let _isConnectionStatusInactive;
  169. let _isConnectionStatusInterrupted;
  170. if (!_isVirtualScreenshareParticipant) {
  171. const _videoTrack = getTrackByMediaTypeAndParticipant(tracks, MEDIA_TYPE.VIDEO, participantId);
  172. _isConnectionStatusInactive = isTrackStreamingStatusInactive(_videoTrack);
  173. _isConnectionStatusInterrupted = isTrackStreamingStatusInterrupted(_videoTrack);
  174. }
  175. return {
  176. ..._abstractMapStateToProps(state),
  177. _connectionIndicatorInactiveDisabled:
  178. Boolean(state['features/base/config'].connectionIndicators?.inactiveDisabled),
  179. _connectionIndicatorDisabled:
  180. Boolean(state['features/base/config'].connectionIndicators?.disabled),
  181. _isVirtualScreenshareParticipant,
  182. _isConnectionStatusInactive,
  183. _isConnectionStatusInterrupted
  184. };
  185. }
  186. // @ts-ignore
  187. export default connect(_mapStateToProps)(ConnectionIndicator);