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.

ConnectionIndicatorContent.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. import React from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { connect } from 'react-redux';
  4. import { IReduxState, IStore } from '../../../app/types';
  5. import { translate } from '../../../base/i18n/functions';
  6. import { MEDIA_TYPE } from '../../../base/media/constants';
  7. import {
  8. getLocalParticipant,
  9. getParticipantById,
  10. isScreenShareParticipant
  11. } from '../../../base/participants/functions';
  12. import {
  13. getTrackByMediaTypeAndParticipant,
  14. getVirtualScreenshareParticipantTrack
  15. } from '../../../base/tracks/functions.web';
  16. import ConnectionStatsTable from '../../../connection-stats/components/ConnectionStatsTable';
  17. import { saveLogs } from '../../actions.web';
  18. import {
  19. isTrackStreamingStatusInactive,
  20. isTrackStreamingStatusInterrupted
  21. } from '../../functions';
  22. import AbstractConnectionIndicator, {
  23. IProps as AbstractProps,
  24. IState as AbstractState,
  25. INDICATOR_DISPLAY_THRESHOLD
  26. } from '../AbstractConnectionIndicator';
  27. /**
  28. * An array of display configurations for the connection indicator and its bars.
  29. * The ordering is done specifically for faster iteration to find a matching
  30. * configuration to the current connection strength percentage.
  31. *
  32. * @type {Object[]}
  33. */
  34. const QUALITY_TO_WIDTH = [
  35. // Full (3 bars)
  36. {
  37. colorClass: 'status-high',
  38. percent: INDICATOR_DISPLAY_THRESHOLD,
  39. tip: 'connectionindicator.quality.good',
  40. width: '100%'
  41. },
  42. // 2 bars
  43. {
  44. colorClass: 'status-med',
  45. percent: 10,
  46. tip: 'connectionindicator.quality.nonoptimal',
  47. width: '66%'
  48. },
  49. // 1 bar
  50. {
  51. colorClass: 'status-low',
  52. percent: 0,
  53. tip: 'connectionindicator.quality.poor',
  54. width: '33%'
  55. }
  56. // Note: we never show 0 bars as long as there is a connection.
  57. ];
  58. /**
  59. * The type of the React {@code Component} props of {@link ConnectionIndicator}.
  60. */
  61. interface IProps extends AbstractProps, WithTranslation {
  62. /**
  63. * The audio SSRC of this client.
  64. */
  65. _audioSsrc: number;
  66. /**
  67. * Whether or not should display the "Show More" link in the local video
  68. * stats table.
  69. */
  70. _disableShowMoreStats: boolean;
  71. /**
  72. * Whether or not should display the "Save Logs" link in the local video
  73. * stats table.
  74. */
  75. _enableSaveLogs: boolean;
  76. _isConnectionStatusInactive: boolean;
  77. _isConnectionStatusInterrupted: boolean;
  78. _isE2EEVerified: boolean;
  79. /**
  80. * Whether or not the displays stats are for local video.
  81. */
  82. _isLocalVideo: boolean;
  83. /**
  84. * Whether is narrow layout or not.
  85. */
  86. _isNarrowLayout: boolean;
  87. /**
  88. * Invoked to save the conference logs.
  89. */
  90. _onSaveLogs: () => void;
  91. /**
  92. * The region reported by the participant.
  93. */
  94. _region?: string;
  95. /**
  96. * The video SSRC of this client.
  97. */
  98. _videoSsrc: number;
  99. /**
  100. * Css class to apply on container.
  101. */
  102. className: string;
  103. /**
  104. * The Redux dispatch function.
  105. */
  106. dispatch: IStore['dispatch'];
  107. /**
  108. * Optional param for passing existing connection stats on component instantiation.
  109. */
  110. inheritedStats: any;
  111. }
  112. /**
  113. * The type of the React {@code Component} state of {@link ConnectionIndicator}.
  114. */
  115. interface IState extends AbstractState {
  116. autoHideTimeout?: number;
  117. /**
  118. * Whether or not the popover content should display additional statistics.
  119. */
  120. showMoreStats: boolean;
  121. }
  122. /**
  123. * Implements a React {@link Component} which displays the current connection
  124. * quality percentage and has a popover to show more detailed connection stats.
  125. *
  126. * @augments {Component}
  127. */
  128. class ConnectionIndicatorContent extends AbstractConnectionIndicator<IProps, IState> {
  129. /**
  130. * Initializes a new {@code ConnectionIndicator} instance.
  131. *
  132. * @param {Object} props - The read-only properties with which the new
  133. * instance is to be initialized.
  134. */
  135. constructor(props: IProps) {
  136. super(props);
  137. this.state = {
  138. autoHideTimeout: undefined,
  139. showIndicator: false,
  140. showMoreStats: false,
  141. stats: props.inheritedStats || {}
  142. };
  143. // Bind event handlers so they are only bound once for every instance.
  144. this._onToggleShowMore = this._onToggleShowMore.bind(this);
  145. }
  146. /**
  147. * Implements React's {@link Component#render()}.
  148. *
  149. * @inheritdoc
  150. * @returns {ReactElement}
  151. */
  152. render() {
  153. const {
  154. bandwidth,
  155. bitrate,
  156. bridgeCount,
  157. codec,
  158. framerate,
  159. maxEnabledResolution,
  160. packetLoss,
  161. resolution,
  162. serverRegion,
  163. transport
  164. } = this.state.stats;
  165. return (
  166. <ConnectionStatsTable
  167. audioSsrc = { this.props._audioSsrc }
  168. bandwidth = { bandwidth }
  169. bitrate = { bitrate }
  170. bridgeCount = { bridgeCount }
  171. codec = { codec }
  172. connectionSummary = { this._getConnectionStatusTip() }
  173. disableShowMoreStats = { this.props._disableShowMoreStats }
  174. e2eeVerified = { this.props._isE2EEVerified }
  175. enableSaveLogs = { this.props._enableSaveLogs }
  176. framerate = { framerate }
  177. isLocalVideo = { this.props._isLocalVideo }
  178. isNarrowLayout = { this.props._isNarrowLayout }
  179. isVirtualScreenshareParticipant = { this.props._isVirtualScreenshareParticipant }
  180. maxEnabledResolution = { maxEnabledResolution }
  181. onSaveLogs = { this.props._onSaveLogs }
  182. onShowMore = { this._onToggleShowMore }
  183. packetLoss = { packetLoss }
  184. participantId = { this.props.participantId }
  185. region = { this.props._region ?? '' }
  186. resolution = { resolution }
  187. serverRegion = { serverRegion }
  188. shouldShowMore = { this.state.showMoreStats }
  189. transport = { transport }
  190. videoSsrc = { this.props._videoSsrc } />
  191. );
  192. }
  193. /**
  194. * Returns a string that describes the current connection status.
  195. *
  196. * @private
  197. * @returns {string}
  198. */
  199. _getConnectionStatusTip() {
  200. let tipKey;
  201. const { _isConnectionStatusInactive, _isConnectionStatusInterrupted } = this.props;
  202. switch (true) {
  203. case _isConnectionStatusInterrupted:
  204. tipKey = 'connectionindicator.quality.lost';
  205. break;
  206. case _isConnectionStatusInactive:
  207. tipKey = 'connectionindicator.quality.inactive';
  208. break;
  209. default: {
  210. const { percent } = this.state.stats;
  211. if (typeof percent === 'undefined') {
  212. // If percentage is undefined then there are no stats available
  213. // yet, likely because only a local connection has been
  214. // established so far. Assume a strong connection to start.
  215. tipKey = 'connectionindicator.quality.good';
  216. } else {
  217. const config = this._getDisplayConfiguration(percent);
  218. tipKey = config.tip;
  219. }
  220. }
  221. }
  222. return this.props.t(tipKey);
  223. }
  224. /**
  225. * Get the icon configuration from QUALITY_TO_WIDTH which has a percentage
  226. * that matches or exceeds the passed in percentage. The implementation
  227. * assumes QUALITY_TO_WIDTH is already sorted by highest to lowest
  228. * percentage.
  229. *
  230. * @param {number} percent - The connection percentage, out of 100, to find
  231. * the closest matching configuration for.
  232. * @private
  233. * @returns {Object}
  234. */
  235. _getDisplayConfiguration(percent: number) {
  236. return QUALITY_TO_WIDTH.find(x => percent >= x.percent) || { tip: '' };
  237. }
  238. /**
  239. * Callback to invoke when the show more link in the popover content is
  240. * clicked. Sets the state which will determine if the popover should show
  241. * additional statistics about the connection.
  242. *
  243. * @returns {void}
  244. */
  245. _onToggleShowMore() {
  246. this.setState({ showMoreStats: !this.state.showMoreStats });
  247. }
  248. }
  249. /**
  250. * Maps redux actions to the props of the component.
  251. *
  252. * @param {Function} dispatch - The redux action {@code dispatch} function.
  253. * @returns {{
  254. * _onSaveLogs: Function,
  255. * }}
  256. * @private
  257. */
  258. export function _mapDispatchToProps(dispatch: IStore['dispatch']) {
  259. return {
  260. /**
  261. * Saves the conference logs.
  262. *
  263. * @returns {Function}
  264. */
  265. _onSaveLogs() {
  266. dispatch(saveLogs());
  267. }
  268. };
  269. }
  270. /**
  271. * Maps part of the Redux state to the props of this component.
  272. *
  273. * @param {Object} state - The Redux state.
  274. * @param {IProps} ownProps - The own props of the component.
  275. * @returns {IProps}
  276. */
  277. export function _mapStateToProps(state: IReduxState, ownProps: any) {
  278. const { participantId } = ownProps;
  279. const conference = state['features/base/conference'].conference;
  280. const participant
  281. = participantId ? getParticipantById(state, participantId) : getLocalParticipant(state);
  282. const { isNarrowLayout } = state['features/base/responsive-ui'];
  283. const tracks = state['features/base/tracks'];
  284. const audioTrack = getTrackByMediaTypeAndParticipant(tracks, MEDIA_TYPE.AUDIO, participantId);
  285. let videoTrack = getTrackByMediaTypeAndParticipant(tracks, MEDIA_TYPE.VIDEO, participantId);
  286. if (isScreenShareParticipant(participant)) {
  287. videoTrack = getVirtualScreenshareParticipantTrack(tracks, participant?.id ?? '');
  288. }
  289. const _isConnectionStatusInactive = isTrackStreamingStatusInactive(videoTrack);
  290. const _isConnectionStatusInterrupted = isTrackStreamingStatusInterrupted(videoTrack);
  291. return {
  292. _audioSsrc: audioTrack ? conference?.getSsrcByTrack(audioTrack.jitsiTrack) : undefined,
  293. _enableSaveLogs: Boolean(state['features/base/config'].enableSaveLogs),
  294. _disableShowMoreStats: Boolean(state['features/base/config'].disableShowMoreStats),
  295. _isConnectionStatusInactive,
  296. _isConnectionStatusInterrupted,
  297. _isE2EEVerified: Boolean(participant?.e2eeVerified),
  298. _isNarrowLayout: isNarrowLayout,
  299. _isVirtualScreenshareParticipant: isScreenShareParticipant(participant),
  300. _isLocalVideo: Boolean(participant?.local),
  301. _region: participant?.region,
  302. _videoSsrc: videoTrack ? conference?.getSsrcByTrack(videoTrack.jitsiTrack) : undefined
  303. };
  304. }
  305. export default translate(connect(_mapStateToProps, _mapDispatchToProps)(ConnectionIndicatorContent));