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

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