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.1KB

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