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.js 10KB

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