Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ConnectionIndicatorContent.js 9.6KB

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