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

ConnectionIndicatorContent.js 10KB

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