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

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