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 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 or not the displays stats are for local video.
  74. */
  75. _isLocalVideo: boolean,
  76. /**
  77. * Invoked to save the conference logs.
  78. */
  79. _onSaveLogs: Function,
  80. /**
  81. * The region reported by the participant.
  82. */
  83. _region: String,
  84. /**
  85. * The video SSRC of this client.
  86. */
  87. _videoSsrc: number,
  88. /**
  89. * Css class to apply on container.
  90. */
  91. className: string,
  92. /**
  93. * The Redux dispatch function.
  94. */
  95. dispatch: Dispatch<any>,
  96. /**
  97. * Optional param for passing existing connection stats on component instantiation.
  98. */
  99. inheritedStats: Object,
  100. /**
  101. * Invoked to obtain translated strings.
  102. */
  103. t: Function
  104. };
  105. /**
  106. * The type of the React {@code Component} state of {@link ConnectionIndicator}.
  107. */
  108. type State = AbstractState & {
  109. /**
  110. * Whether or not the popover content should display additional statistics.
  111. */
  112. showMoreStats: boolean
  113. };
  114. /**
  115. * Implements a React {@link Component} which displays the current connection
  116. * quality percentage and has a popover to show more detailed connection stats.
  117. *
  118. * @augments {Component}
  119. */
  120. class ConnectionIndicatorContent extends AbstractConnectionIndicator<Props, State> {
  121. /**
  122. * Initializes a new {@code ConnectionIndicator} instance.
  123. *
  124. * @param {Object} props - The read-only properties with which the new
  125. * instance is to be initialized.
  126. */
  127. constructor(props: Props) {
  128. super(props);
  129. this.state = {
  130. autoHideTimeout: undefined,
  131. showIndicator: false,
  132. showMoreStats: false,
  133. stats: props.inheritedStats || {}
  134. };
  135. // Bind event handlers so they are only bound once for every instance.
  136. this._onToggleShowMore = this._onToggleShowMore.bind(this);
  137. }
  138. /**
  139. * Implements React's {@link Component#render()}.
  140. *
  141. * @inheritdoc
  142. * @returns {ReactElement}
  143. */
  144. render() {
  145. const {
  146. bandwidth,
  147. bitrate,
  148. bridgeCount,
  149. codec,
  150. framerate,
  151. maxEnabledResolution,
  152. packetLoss,
  153. resolution,
  154. serverRegion,
  155. transport
  156. } = this.state.stats;
  157. return (
  158. <ConnectionStatsTable
  159. audioSsrc = { this.props._audioSsrc }
  160. bandwidth = { bandwidth }
  161. bitrate = { bitrate }
  162. bridgeCount = { bridgeCount }
  163. codec = { codec }
  164. connectionSummary = { this._getConnectionStatusTip() }
  165. disableShowMoreStats = { this.props._disableShowMoreStats }
  166. enableSaveLogs = { this.props._enableSaveLogs }
  167. framerate = { framerate }
  168. isLocalVideo = { this.props._isLocalVideo }
  169. isVirtualScreenshareParticipant = { this.props._isVirtualScreenshareParticipant }
  170. maxEnabledResolution = { maxEnabledResolution }
  171. onSaveLogs = { this.props._onSaveLogs }
  172. onShowMore = { this._onToggleShowMore }
  173. packetLoss = { packetLoss }
  174. participantId = { this.props.participantId }
  175. region = { this.props._region }
  176. resolution = { resolution }
  177. serverRegion = { serverRegion }
  178. shouldShowMore = { this.state.showMoreStats }
  179. transport = { transport }
  180. videoSsrc = { this.props._videoSsrc } />
  181. );
  182. }
  183. /**
  184. * Returns a string that describes the current connection status.
  185. *
  186. * @private
  187. * @returns {string}
  188. */
  189. _getConnectionStatusTip() {
  190. let tipKey;
  191. const { _isConnectionStatusInactive, _isConnectionStatusInterrupted } = this.props;
  192. switch (true) {
  193. case _isConnectionStatusInterrupted:
  194. tipKey = 'connectionindicator.quality.lost';
  195. break;
  196. case _isConnectionStatusInactive:
  197. tipKey = 'connectionindicator.quality.inactive';
  198. break;
  199. default: {
  200. const { percent } = this.state.stats;
  201. if (typeof percent === 'undefined') {
  202. // If percentage is undefined then there are no stats available
  203. // yet, likely because only a local connection has been
  204. // established so far. Assume a strong connection to start.
  205. tipKey = 'connectionindicator.quality.good';
  206. } else {
  207. const config = this._getDisplayConfiguration(percent);
  208. tipKey = config.tip;
  209. }
  210. }
  211. }
  212. return this.props.t(tipKey);
  213. }
  214. /**
  215. * Get the icon configuration from QUALITY_TO_WIDTH which has a percentage
  216. * that matches or exceeds the passed in percentage. The implementation
  217. * assumes QUALITY_TO_WIDTH is already sorted by highest to lowest
  218. * percentage.
  219. *
  220. * @param {number} percent - The connection percentage, out of 100, to find
  221. * the closest matching configuration for.
  222. * @private
  223. * @returns {Object}
  224. */
  225. _getDisplayConfiguration(percent: number): Object {
  226. return QUALITY_TO_WIDTH.find(x => percent >= x.percent) || {};
  227. }
  228. _onToggleShowMore: () => void;
  229. /**
  230. * Callback to invoke when the show more link in the popover content is
  231. * clicked. Sets the state which will determine if the popover should show
  232. * additional statistics about the connection.
  233. *
  234. * @returns {void}
  235. */
  236. _onToggleShowMore() {
  237. this.setState({ showMoreStats: !this.state.showMoreStats });
  238. }
  239. }
  240. /**
  241. * Maps redux actions to the props of the component.
  242. *
  243. * @param {Function} dispatch - The redux action {@code dispatch} function.
  244. * @returns {{
  245. * _onSaveLogs: Function,
  246. * }}
  247. * @private
  248. */
  249. export function _mapDispatchToProps(dispatch: Dispatch<any>) {
  250. return {
  251. /**
  252. * Saves the conference logs.
  253. *
  254. * @returns {Function}
  255. */
  256. _onSaveLogs() {
  257. dispatch(saveLogs());
  258. }
  259. };
  260. }
  261. /**
  262. * Maps part of the Redux state to the props of this component.
  263. *
  264. * @param {Object} state - The Redux state.
  265. * @param {Props} ownProps - The own props of the component.
  266. * @returns {Props}
  267. */
  268. export function _mapStateToProps(state: Object, ownProps: Props) {
  269. const { participantId } = ownProps;
  270. const conference = state['features/base/conference'].conference;
  271. const participant
  272. = participantId ? getParticipantById(state, participantId) : getLocalParticipant(state);
  273. const tracks = state['features/base/tracks'];
  274. const audioTrack = getTrackByMediaTypeAndParticipant(tracks, MEDIA_TYPE.AUDIO, participantId);
  275. let videoTrack = getTrackByMediaTypeAndParticipant(tracks, MEDIA_TYPE.VIDEO, participantId);
  276. if (isScreenShareParticipant(participant)) {
  277. videoTrack = getVirtualScreenshareParticipantTrack(tracks, participant?.id);
  278. }
  279. const _isConnectionStatusInactive = isTrackStreamingStatusInactive(videoTrack);
  280. const _isConnectionStatusInterrupted = isTrackStreamingStatusInterrupted(videoTrack);
  281. return {
  282. _audioSsrc: audioTrack ? conference?.getSsrcByTrack(audioTrack.jitsiTrack) : undefined,
  283. _enableSaveLogs: state['features/base/config'].enableSaveLogs,
  284. _disableShowMoreStats: state['features/base/config'].disableShowMoreStats,
  285. _isConnectionStatusInactive,
  286. _isConnectionStatusInterrupted,
  287. _isVirtualScreenshareParticipant: isScreenShareParticipant(participant),
  288. _isLocalVideo: participant?.local,
  289. _region: participant?.region,
  290. _videoSsrc: videoTrack ? conference?.getSsrcByTrack(videoTrack.jitsiTrack) : undefined
  291. };
  292. }
  293. export default translate(connect(_mapStateToProps, _mapDispatchToProps)(ConnectionIndicatorContent));