您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ConnectionIndicator.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // @flow
  2. import React from 'react';
  3. import type { Dispatch } from 'redux';
  4. import { translate } from '../../../base/i18n';
  5. import { Icon, IconConnectionActive, IconConnectionInactive } from '../../../base/icons';
  6. import { JitsiParticipantConnectionStatus } from '../../../base/lib-jitsi-meet';
  7. import { MEDIA_TYPE } from '../../../base/media';
  8. import { Popover } from '../../../base/popover';
  9. import { connect } from '../../../base/redux';
  10. import { getTrackByMediaTypeAndParticipant } from '../../../base/tracks';
  11. import { ConnectionStatsTable } from '../../../connection-stats';
  12. import { saveLogs } from '../../actions';
  13. import AbstractConnectionIndicator, {
  14. INDICATOR_DISPLAY_THRESHOLD,
  15. type Props as AbstractProps,
  16. type State as AbstractState
  17. } from '../AbstractConnectionIndicator';
  18. declare var interfaceConfig: Object;
  19. /**
  20. * An array of display configurations for the connection indicator and its bars.
  21. * The ordering is done specifically for faster iteration to find a matching
  22. * configuration to the current connection strength percentage.
  23. *
  24. * @type {Object[]}
  25. */
  26. const QUALITY_TO_WIDTH: Array<Object> = [
  27. // Full (3 bars)
  28. {
  29. colorClass: 'status-high',
  30. percent: INDICATOR_DISPLAY_THRESHOLD,
  31. tip: 'connectionindicator.quality.good',
  32. width: '100%'
  33. },
  34. // 2 bars
  35. {
  36. colorClass: 'status-med',
  37. percent: 10,
  38. tip: 'connectionindicator.quality.nonoptimal',
  39. width: '66%'
  40. },
  41. // 1 bar
  42. {
  43. colorClass: 'status-low',
  44. percent: 0,
  45. tip: 'connectionindicator.quality.poor',
  46. width: '33%'
  47. }
  48. // Note: we never show 0 bars as long as there is a connection.
  49. ];
  50. /**
  51. * The type of the React {@code Component} props of {@link ConnectionIndicator}.
  52. */
  53. type Props = AbstractProps & {
  54. /**
  55. * Whether or not the component should ignore setting a visibility class for
  56. * hiding the component when the connection quality is not strong.
  57. */
  58. alwaysVisible: boolean,
  59. /**
  60. * The audio SSRC of this client.
  61. */
  62. audioSsrc: number,
  63. /**
  64. * The current condition of the user's connection, matching one of the
  65. * enumerated values in the library.
  66. */
  67. connectionStatus: string,
  68. /**
  69. * The Redux dispatch function.
  70. */
  71. dispatch: Dispatch<any>,
  72. /**
  73. * Whether or not clicking the indicator should display a popover for more
  74. * details.
  75. */
  76. enableStatsDisplay: boolean,
  77. /**
  78. * The font-size for the icon.
  79. */
  80. iconSize: number,
  81. /**
  82. * Whether or not the displays stats are for local video.
  83. */
  84. isLocalVideo: boolean,
  85. /**
  86. * Relative to the icon from where the popover for more connection details
  87. * should display.
  88. */
  89. statsPopoverPosition: string,
  90. /**
  91. * Invoked to obtain translated strings.
  92. */
  93. t: Function,
  94. /**
  95. * The video SSRC of this client.
  96. */
  97. videoSsrc: number,
  98. /**
  99. * Invoked to save the conference logs.
  100. */
  101. _onSaveLogs: Function
  102. };
  103. /**
  104. * The type of the React {@code Component} state of {@link ConnectionIndicator}.
  105. */
  106. type State = AbstractState & {
  107. /**
  108. * Whether or not the popover content should display additional statistics.
  109. */
  110. showMoreStats: boolean
  111. };
  112. /**
  113. * Implements a React {@link Component} which displays the current connection
  114. * quality percentage and has a popover to show more detailed connection stats.
  115. *
  116. * @extends {Component}
  117. */
  118. class ConnectionIndicator extends AbstractConnectionIndicator<Props, State> {
  119. /**
  120. * Initializes a new {@code ConnectionIndicator} instance.
  121. *
  122. * @param {Object} props - The read-only properties with which the new
  123. * instance is to be initialized.
  124. */
  125. constructor(props: Props) {
  126. super(props);
  127. this.state = {
  128. autoHideTimeout: undefined,
  129. showIndicator: false,
  130. showMoreStats: false,
  131. stats: {}
  132. };
  133. // Bind event handlers so they are only bound once for every instance.
  134. this._onToggleShowMore = this._onToggleShowMore.bind(this);
  135. }
  136. /**
  137. * Implements React's {@link Component#render()}.
  138. *
  139. * @inheritdoc
  140. * @returns {ReactElement}
  141. */
  142. render() {
  143. const visibilityClass = this._getVisibilityClass();
  144. const rootClassNames = `indicator-container ${visibilityClass}`;
  145. const colorClass = this._getConnectionColorClass();
  146. const indicatorContainerClassNames
  147. = `connection-indicator indicator ${colorClass}`;
  148. return (
  149. <Popover
  150. className = { rootClassNames }
  151. content = { this._renderStatisticsTable() }
  152. disablePopover = { !this.props.enableStatsDisplay }
  153. position = { this.props.statsPopoverPosition }>
  154. <div className = 'popover-trigger'>
  155. <div
  156. className = { indicatorContainerClassNames }
  157. style = {{ fontSize: this.props.iconSize }}>
  158. <div className = 'connection indicatoricon'>
  159. { this._renderIcon() }
  160. </div>
  161. </div>
  162. </div>
  163. </Popover>
  164. );
  165. }
  166. /**
  167. * Returns a CSS class that interprets the current connection status as a
  168. * color.
  169. *
  170. * @private
  171. * @returns {string}
  172. */
  173. _getConnectionColorClass() {
  174. const { connectionStatus } = this.props;
  175. const { percent } = this.state.stats;
  176. const { INACTIVE, INTERRUPTED } = JitsiParticipantConnectionStatus;
  177. if (connectionStatus === INACTIVE) {
  178. return 'status-other';
  179. } else if (connectionStatus === INTERRUPTED) {
  180. return 'status-lost';
  181. } else if (typeof percent === 'undefined') {
  182. return 'status-high';
  183. }
  184. return this._getDisplayConfiguration(percent).colorClass;
  185. }
  186. /**
  187. * Returns a string that describes the current connection status.
  188. *
  189. * @private
  190. * @returns {string}
  191. */
  192. _getConnectionStatusTip() {
  193. let tipKey;
  194. switch (this.props.connectionStatus) {
  195. case JitsiParticipantConnectionStatus.INTERRUPTED:
  196. tipKey = 'connectionindicator.quality.lost';
  197. break;
  198. case JitsiParticipantConnectionStatus.INACTIVE:
  199. tipKey = 'connectionindicator.quality.inactive';
  200. break;
  201. default: {
  202. const { percent } = this.state.stats;
  203. if (typeof percent === 'undefined') {
  204. // If percentage is undefined then there are no stats available
  205. // yet, likely because only a local connection has been
  206. // established so far. Assume a strong connection to start.
  207. tipKey = 'connectionindicator.quality.good';
  208. } else {
  209. const config = this._getDisplayConfiguration(percent);
  210. tipKey = config.tip;
  211. }
  212. }
  213. }
  214. return this.props.t(tipKey);
  215. }
  216. /**
  217. * Get the icon configuration from QUALITY_TO_WIDTH which has a percentage
  218. * that matches or exceeds the passed in percentage. The implementation
  219. * assumes QUALITY_TO_WIDTH is already sorted by highest to lowest
  220. * percentage.
  221. *
  222. * @param {number} percent - The connection percentage, out of 100, to find
  223. * the closest matching configuration for.
  224. * @private
  225. * @returns {Object}
  226. */
  227. _getDisplayConfiguration(percent: number): Object {
  228. return QUALITY_TO_WIDTH.find(x => percent >= x.percent) || {};
  229. }
  230. /**
  231. * Returns additional class names to add to the root of the component. The
  232. * class names are intended to be used for hiding or showing the indicator.
  233. *
  234. * @private
  235. * @returns {string}
  236. */
  237. _getVisibilityClass() {
  238. const { connectionStatus } = this.props;
  239. return this.state.showIndicator
  240. || this.props.alwaysVisible
  241. || connectionStatus === JitsiParticipantConnectionStatus.INTERRUPTED
  242. || connectionStatus === JitsiParticipantConnectionStatus.INACTIVE
  243. ? 'show-connection-indicator' : 'hide-connection-indicator';
  244. }
  245. _onToggleShowMore: () => void;
  246. /**
  247. * Callback to invoke when the show more link in the popover content is
  248. * clicked. Sets the state which will determine if the popover should show
  249. * additional statistics about the connection.
  250. *
  251. * @returns {void}
  252. */
  253. _onToggleShowMore() {
  254. this.setState({ showMoreStats: !this.state.showMoreStats });
  255. }
  256. /**
  257. * Creates a ReactElement for displaying an icon that represents the current
  258. * connection quality.
  259. *
  260. * @returns {ReactElement}
  261. */
  262. _renderIcon() {
  263. if (this.props.connectionStatus
  264. === JitsiParticipantConnectionStatus.INACTIVE) {
  265. return (
  266. <span className = 'connection_ninja'>
  267. <Icon
  268. className = 'icon-ninja'
  269. size = '1.5em'
  270. src = { IconConnectionInactive } />
  271. </span>
  272. );
  273. }
  274. let iconWidth;
  275. let emptyIconWrapperClassName = 'connection_empty';
  276. if (this.props.connectionStatus
  277. === JitsiParticipantConnectionStatus.INTERRUPTED) {
  278. // emptyIconWrapperClassName is used by the torture tests to
  279. // identify lost connection status handling.
  280. emptyIconWrapperClassName = 'connection_lost';
  281. iconWidth = '0%';
  282. } else if (typeof this.state.stats.percent === 'undefined') {
  283. iconWidth = '100%';
  284. } else {
  285. const { percent } = this.state.stats;
  286. iconWidth = this._getDisplayConfiguration(percent).width;
  287. }
  288. return [
  289. <span
  290. className = { emptyIconWrapperClassName }
  291. key = 'icon-empty'>
  292. <Icon
  293. className = 'icon-gsm-bars'
  294. size = '1em'
  295. src = { IconConnectionActive } />
  296. </span>,
  297. <span
  298. className = 'connection_full'
  299. key = 'icon-full'
  300. style = {{ width: iconWidth }}>
  301. <Icon
  302. className = 'icon-gsm-bars'
  303. size = '1em'
  304. src = { IconConnectionActive } />
  305. </span>
  306. ];
  307. }
  308. /**
  309. * Creates a {@code ConnectionStatisticsTable} instance.
  310. *
  311. * @returns {ReactElement}
  312. */
  313. _renderStatisticsTable() {
  314. const {
  315. bandwidth,
  316. bitrate,
  317. bridgeCount,
  318. codec,
  319. e2eRtt,
  320. framerate,
  321. maxEnabledResolution,
  322. packetLoss,
  323. region,
  324. resolution,
  325. serverRegion,
  326. transport
  327. } = this.state.stats;
  328. return (
  329. <ConnectionStatsTable
  330. audioSsrc = { this.props.audioSsrc }
  331. bandwidth = { bandwidth }
  332. bitrate = { bitrate }
  333. bridgeCount = { bridgeCount }
  334. codec = { codec }
  335. connectionSummary = { this._getConnectionStatusTip() }
  336. e2eRtt = { e2eRtt }
  337. framerate = { framerate }
  338. isLocalVideo = { this.props.isLocalVideo }
  339. maxEnabledResolution = { maxEnabledResolution }
  340. onSaveLogs = { this.props._onSaveLogs }
  341. onShowMore = { this._onToggleShowMore }
  342. packetLoss = { packetLoss }
  343. participantId = { this.props.participantId }
  344. region = { region }
  345. resolution = { resolution }
  346. serverRegion = { serverRegion }
  347. shouldShowMore = { this.state.showMoreStats }
  348. transport = { transport }
  349. videoSsrc = { this.props.videoSsrc } />
  350. );
  351. }
  352. }
  353. /**
  354. * Maps redux actions to the props of the component.
  355. *
  356. * @param {Function} dispatch - The redux action {@code dispatch} function.
  357. * @returns {{
  358. * _onSaveLogs: Function,
  359. * }}
  360. * @private
  361. */
  362. export function _mapDispatchToProps(dispatch: Dispatch<any>) {
  363. return {
  364. /**
  365. * Saves the conference logs.
  366. *
  367. * @returns {Function}
  368. */
  369. _onSaveLogs() {
  370. dispatch(saveLogs());
  371. }
  372. };
  373. }
  374. /**
  375. * Maps part of the Redux state to the props of this component.
  376. *
  377. * @param {Object} state - The Redux state.
  378. * @param {Props} ownProps - The own props of the component.
  379. * @returns {Props}
  380. */
  381. export function _mapStateToProps(state: Object, ownProps: Props) {
  382. const firstVideoTrack = getTrackByMediaTypeAndParticipant(
  383. state['features/base/tracks'], MEDIA_TYPE.VIDEO, ownProps.participantId);
  384. const firstAudioTrack = getTrackByMediaTypeAndParticipant(
  385. state['features/base/tracks'], MEDIA_TYPE.AUDIO, ownProps.participantId);
  386. return {
  387. audioSsrc: firstAudioTrack
  388. ? state['features/base/conference'].conference.getSsrcByTrack(firstAudioTrack.jitsiTrack) : undefined,
  389. videoSsrc: firstVideoTrack
  390. ? state['features/base/conference'].conference.getSsrcByTrack(firstVideoTrack.jitsiTrack) : undefined
  391. };
  392. }
  393. export default translate(connect(_mapStateToProps, _mapDispatchToProps)(ConnectionIndicator));