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 11KB

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