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

ConnectionIndicatorContent.js 11KB

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