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

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