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

ConnectionIndicatorContent.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. type Props as AbstractProps,
  20. type State as AbstractState,
  21. INDICATOR_DISPLAY_THRESHOLD
  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. framerate,
  169. maxEnabledResolution,
  170. packetLoss,
  171. resolution,
  172. serverRegion,
  173. transport
  174. } = this.state.stats;
  175. return (
  176. <ConnectionStatsTable
  177. audioSsrc = { this.props._audioSsrc }
  178. bandwidth = { bandwidth }
  179. bitrate = { bitrate }
  180. bridgeCount = { bridgeCount }
  181. codec = { codec }
  182. connectionSummary = { this._getConnectionStatusTip() }
  183. disableShowMoreStats = { this.props._disableShowMoreStats }
  184. enableSaveLogs = { this.props._enableSaveLogs }
  185. framerate = { framerate }
  186. isLocalVideo = { this.props._isLocalVideo }
  187. isVirtualScreenshareParticipant = { this.props._isVirtualScreenshareParticipant }
  188. maxEnabledResolution = { maxEnabledResolution }
  189. onSaveLogs = { this.props._onSaveLogs }
  190. onShowMore = { this._onToggleShowMore }
  191. packetLoss = { packetLoss }
  192. participantId = { this.props.participantId }
  193. region = { this.props._region }
  194. resolution = { resolution }
  195. serverRegion = { serverRegion }
  196. shouldShowMore = { this.state.showMoreStats }
  197. sourceNameSignalingEnabled = { this.props._sourceNameSignalingEnabled }
  198. transport = { transport }
  199. videoSsrc = { this.props._videoSsrc } />
  200. );
  201. }
  202. /**
  203. * Returns a string that describes the current connection status.
  204. *
  205. * @private
  206. * @returns {string}
  207. */
  208. _getConnectionStatusTip() {
  209. let tipKey;
  210. const { _isConnectionStatusInactive, _isConnectionStatusInterrupted } = this.props;
  211. switch (true) {
  212. case _isConnectionStatusInterrupted:
  213. tipKey = 'connectionindicator.quality.lost';
  214. break;
  215. case _isConnectionStatusInactive:
  216. tipKey = 'connectionindicator.quality.inactive';
  217. break;
  218. default: {
  219. const { percent } = this.state.stats;
  220. if (typeof percent === 'undefined') {
  221. // If percentage is undefined then there are no stats available
  222. // yet, likely because only a local connection has been
  223. // established so far. Assume a strong connection to start.
  224. tipKey = 'connectionindicator.quality.good';
  225. } else {
  226. const config = this._getDisplayConfiguration(percent);
  227. tipKey = config.tip;
  228. }
  229. }
  230. }
  231. return this.props.t(tipKey);
  232. }
  233. /**
  234. * Get the icon configuration from QUALITY_TO_WIDTH which has a percentage
  235. * that matches or exceeds the passed in percentage. The implementation
  236. * assumes QUALITY_TO_WIDTH is already sorted by highest to lowest
  237. * percentage.
  238. *
  239. * @param {number} percent - The connection percentage, out of 100, to find
  240. * the closest matching configuration for.
  241. * @private
  242. * @returns {Object}
  243. */
  244. _getDisplayConfiguration(percent: number): Object {
  245. return QUALITY_TO_WIDTH.find(x => percent >= x.percent) || {};
  246. }
  247. _onToggleShowMore: () => void;
  248. /**
  249. * Callback to invoke when the show more link in the popover content is
  250. * clicked. Sets the state which will determine if the popover should show
  251. * additional statistics about the connection.
  252. *
  253. * @returns {void}
  254. */
  255. _onToggleShowMore() {
  256. this.setState({ showMoreStats: !this.state.showMoreStats });
  257. }
  258. }
  259. /**
  260. * Maps redux actions to the props of the component.
  261. *
  262. * @param {Function} dispatch - The redux action {@code dispatch} function.
  263. * @returns {{
  264. * _onSaveLogs: Function,
  265. * }}
  266. * @private
  267. */
  268. export function _mapDispatchToProps(dispatch: Dispatch<any>) {
  269. return {
  270. /**
  271. * Saves the conference logs.
  272. *
  273. * @returns {Function}
  274. */
  275. _onSaveLogs() {
  276. dispatch(saveLogs());
  277. }
  278. };
  279. }
  280. /**
  281. * Maps part of the Redux state to the props of this component.
  282. *
  283. * @param {Object} state - The Redux state.
  284. * @param {Props} ownProps - The own props of the component.
  285. * @returns {Props}
  286. */
  287. export function _mapStateToProps(state: Object, ownProps: Props) {
  288. const { participantId } = ownProps;
  289. const conference = state['features/base/conference'].conference;
  290. const participant
  291. = participantId ? getParticipantById(state, participantId) : getLocalParticipant(state);
  292. const firstVideoTrack = getTrackByMediaTypeAndParticipant(
  293. state['features/base/tracks'], MEDIA_TYPE.VIDEO, participantId);
  294. const sourceNameSignalingEnabled = getSourceNameSignalingFeatureFlag(state);
  295. const _isConnectionStatusInactive = sourceNameSignalingEnabled
  296. ? isTrackStreamingStatusInactive(firstVideoTrack)
  297. : isParticipantConnectionStatusInactive(participant);
  298. const _isConnectionStatusInterrupted = sourceNameSignalingEnabled
  299. ? isTrackStreamingStatusInterrupted(firstVideoTrack)
  300. : isParticipantConnectionStatusInterrupted(participant);
  301. const props = {
  302. _connectionStatus: participant?.connectionStatus,
  303. _enableSaveLogs: state['features/base/config'].enableSaveLogs,
  304. _disableShowMoreStats: state['features/base/config'].disableShowMoreStats,
  305. _isConnectionStatusInactive,
  306. _isConnectionStatusInterrupted,
  307. _isVirtualScreenshareParticipant: sourceNameSignalingEnabled && participant?.isVirtualScreenshareParticipant,
  308. _isLocalVideo: participant?.local,
  309. _region: participant?.region,
  310. _sourceName: getSourceNameByParticipantId(state, participantId),
  311. _sourceNameSignalingEnabled: sourceNameSignalingEnabled
  312. };
  313. if (conference) {
  314. const firstAudioTrack = getTrackByMediaTypeAndParticipant(
  315. state['features/base/tracks'], MEDIA_TYPE.AUDIO, participantId);
  316. return {
  317. ...props,
  318. _audioSsrc: firstAudioTrack ? conference.getSsrcByTrack(firstAudioTrack.jitsiTrack) : undefined,
  319. _videoSsrc: firstVideoTrack ? conference.getSsrcByTrack(firstVideoTrack.jitsiTrack) : undefined
  320. };
  321. }
  322. return props;
  323. }
  324. export default translate(connect(_mapStateToProps, _mapDispatchToProps)(ConnectionIndicatorContent));