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.

ConnectionStatusComponent.tsx 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /* eslint-disable lines-around-comment */
  2. import React, { PureComponent } from 'react';
  3. import { WithTranslation } from 'react-i18next';
  4. import { Text, View } from 'react-native';
  5. import { withTheme } from 'react-native-paper';
  6. import { connect } from 'react-redux';
  7. import { IReduxState } from '../../../app/types';
  8. // @ts-ignore
  9. import Avatar from '../../../base/avatar/components/Avatar';
  10. import { hideSheet } from '../../../base/dialog/actions';
  11. // @ts-ignore
  12. import BottomSheet from '../../../base/dialog/components/native/BottomSheet';
  13. // @ts-ignore
  14. import { bottomSheetStyles } from '../../../base/dialog/components/native/styles';
  15. import { translate } from '../../../base/i18n/functions';
  16. import { IconArrowDownLarge, IconArrowUpLarge } from '../../../base/icons/svg';
  17. import { MEDIA_TYPE } from '../../../base/media/constants';
  18. import { getParticipantDisplayName } from '../../../base/participants/functions';
  19. // @ts-ignore
  20. import BaseIndicator from '../../../base/react/components/native/BaseIndicator';
  21. import {
  22. getTrackByMediaTypeAndParticipant
  23. } from '../../../base/tracks/functions.native';
  24. import {
  25. isTrackStreamingStatusInactive,
  26. isTrackStreamingStatusInterrupted
  27. } from '../../../connection-indicator/functions';
  28. import statsEmitter from '../../../connection-indicator/statsEmitter';
  29. // @ts-ignore
  30. import styles from './styles';
  31. /**
  32. * Size of the rendered avatar in the menu.
  33. */
  34. const AVATAR_SIZE = 25;
  35. const CONNECTION_QUALITY = [
  36. // Full (3 bars)
  37. {
  38. msg: 'connectionindicator.quality.good',
  39. percent: 30 // INDICATOR_DISPLAY_THRESHOLD
  40. },
  41. // 2 bars.
  42. {
  43. msg: 'connectionindicator.quality.nonoptimal',
  44. percent: 10
  45. },
  46. // 1 bar.
  47. {
  48. msg: 'connectionindicator.quality.poor',
  49. percent: 0
  50. }
  51. ];
  52. interface IProps extends WithTranslation {
  53. /**
  54. * Whether this participant's connection is inactive.
  55. */
  56. _isConnectionStatusInactive: boolean;
  57. /**
  58. * Whether this participant's connection is interrupted.
  59. */
  60. _isConnectionStatusInterrupted: boolean;
  61. /**
  62. * True if the menu is currently open, false otherwise.
  63. */
  64. _isOpen: boolean;
  65. /**
  66. * Display name of the participant retrieved from Redux.
  67. */
  68. _participantDisplayName: string;
  69. /**
  70. * The Redux dispatch function.
  71. */
  72. dispatch: Function;
  73. /**
  74. * The ID of the participant that this button is supposed to pin.
  75. */
  76. participantID: string;
  77. /**
  78. * Theme used for styles.
  79. */
  80. theme: any;
  81. }
  82. /**
  83. * The type of the React {@code Component} state of {@link ConnectionStatusComponent}.
  84. */
  85. type IState = {
  86. codecString: string;
  87. connectionString: string;
  88. downloadString: string;
  89. packetLostDownloadString: string;
  90. packetLostUploadString: string;
  91. resolutionString: string;
  92. serverRegionString: string;
  93. uploadString: string;
  94. };
  95. /**
  96. * Class to implement a popup menu that show the connection statistics.
  97. */
  98. class ConnectionStatusComponent extends PureComponent<IProps, IState> {
  99. /**
  100. * Constructor of the component.
  101. *
  102. * @param {P} props - The read-only properties with which the new
  103. * instance is to be initialized.
  104. *
  105. * @inheritdoc
  106. */
  107. constructor(props: IProps) {
  108. super(props);
  109. this._onStatsUpdated = this._onStatsUpdated.bind(this);
  110. this._onCancel = this._onCancel.bind(this);
  111. this._renderMenuHeader = this._renderMenuHeader.bind(this);
  112. this.state = {
  113. resolutionString: 'N/A',
  114. downloadString: 'N/A',
  115. uploadString: 'N/A',
  116. packetLostDownloadString: 'N/A',
  117. packetLostUploadString: 'N/A',
  118. serverRegionString: 'N/A',
  119. codecString: 'N/A',
  120. connectionString: 'N/A'
  121. };
  122. }
  123. /**
  124. * Implements React's {@link Component#render()}.
  125. *
  126. * @inheritdoc
  127. * @returns {ReactNode}
  128. */
  129. render() {
  130. const { t, theme } = this.props;
  131. const { palette } = theme;
  132. return (
  133. <BottomSheet
  134. onCancel = { this._onCancel }
  135. renderHeader = { this._renderMenuHeader }>
  136. <View style = { styles.statsWrapper }>
  137. <View style = { styles.statsInfoCell }>
  138. <Text style = { styles.statsTitleText }>
  139. { t('connectionindicator.status') }
  140. </Text>
  141. <Text style = { styles.statsInfoText }>
  142. { t(this.state.connectionString) }
  143. </Text>
  144. </View>
  145. <View style = { styles.statsInfoCell }>
  146. <Text style = { styles.statsTitleText }>
  147. { t('connectionindicator.bitrate') }
  148. </Text>
  149. <BaseIndicator
  150. icon = { IconArrowDownLarge }
  151. iconStyle = {{
  152. color: palette.icon03
  153. }} />
  154. <Text style = { styles.statsInfoText }>
  155. { this.state.downloadString }
  156. </Text>
  157. <BaseIndicator
  158. icon = { IconArrowUpLarge }
  159. iconStyle = {{
  160. color: palette.icon03
  161. }} />
  162. <Text style = { styles.statsInfoText }>
  163. { `${this.state.uploadString} Kbps` }
  164. </Text>
  165. </View>
  166. <View style = { styles.statsInfoCell }>
  167. <Text style = { styles.statsTitleText }>
  168. { t('connectionindicator.packetloss') }
  169. </Text>
  170. <BaseIndicator
  171. icon = { IconArrowDownLarge }
  172. iconStyle = {{
  173. color: palette.icon03
  174. }} />
  175. <Text style = { styles.statsInfoText }>
  176. { this.state.packetLostDownloadString }
  177. </Text>
  178. <BaseIndicator
  179. icon = { IconArrowUpLarge }
  180. iconStyle = {{
  181. color: palette.icon03
  182. }} />
  183. <Text style = { styles.statsInfoText }>
  184. { this.state.packetLostUploadString }
  185. </Text>
  186. </View>
  187. <View style = { styles.statsInfoCell }>
  188. <Text style = { styles.statsTitleText }>
  189. { t('connectionindicator.resolution') }
  190. </Text>
  191. <Text style = { styles.statsInfoText }>
  192. { this.state.resolutionString }
  193. </Text>
  194. </View>
  195. <View style = { styles.statsInfoCell }>
  196. <Text style = { styles.statsTitleText }>
  197. { t('connectionindicator.codecs') }
  198. </Text>
  199. <Text style = { styles.statsInfoText }>
  200. { this.state.codecString }
  201. </Text>
  202. </View>
  203. </View>
  204. </BottomSheet>
  205. );
  206. }
  207. /**
  208. * Starts listening for stat updates.
  209. *
  210. * @inheritdoc
  211. * returns {void}
  212. */
  213. componentDidMount() {
  214. statsEmitter.subscribeToClientStats(this.props.participantID, this._onStatsUpdated);
  215. }
  216. /**
  217. * Updates which user's stats are being listened to.
  218. *
  219. * @inheritdoc
  220. * returns {void}
  221. */
  222. componentDidUpdate(prevProps: IProps) {
  223. if (prevProps.participantID !== this.props.participantID) {
  224. statsEmitter.unsubscribeToClientStats(
  225. prevProps.participantID, this._onStatsUpdated);
  226. statsEmitter.subscribeToClientStats(
  227. this.props.participantID, this._onStatsUpdated);
  228. }
  229. }
  230. /**
  231. * Callback invoked when new connection stats associated with the passed in
  232. * user ID are available. Will update the component's display of current
  233. * statistics.
  234. *
  235. * @param {Object} stats - Connection stats from the library.
  236. * @private
  237. * @returns {void}
  238. */
  239. _onStatsUpdated(stats = {}) {
  240. const newState = this._buildState(stats);
  241. this.setState(newState);
  242. }
  243. /**
  244. * Extracts statistics and builds the state object.
  245. *
  246. * @param {Object} stats - Connection stats from the library.
  247. * @private
  248. * @returns {State}
  249. */
  250. _buildState(stats: any) {
  251. const { download: downloadBitrate, upload: uploadBitrate } = this._extractBitrate(stats) ?? {};
  252. const { download: downloadPacketLost, upload: uploadPacketLost } = this._extractPacketLost(stats) ?? {};
  253. return {
  254. resolutionString: this._extractResolutionString(stats) ?? this.state.resolutionString,
  255. downloadString: downloadBitrate ?? this.state.downloadString,
  256. uploadString: uploadBitrate ?? this.state.uploadString,
  257. packetLostDownloadString: downloadPacketLost === undefined
  258. ? this.state.packetLostDownloadString : `${downloadPacketLost}%`,
  259. packetLostUploadString: uploadPacketLost === undefined
  260. ? this.state.packetLostUploadString : `${uploadPacketLost}%`,
  261. serverRegionString: this._extractServer(stats) ?? this.state.serverRegionString,
  262. codecString: this._extractCodecs(stats) ?? this.state.codecString,
  263. connectionString: this._extractConnection(stats)
  264. };
  265. }
  266. /**
  267. * Extracts the resolution and framerate.
  268. *
  269. * @param {Object} stats - Connection stats from the library.
  270. * @private
  271. * @returns {string}
  272. */
  273. _extractResolutionString(stats: any) {
  274. const { framerate, resolution } = stats;
  275. const resolutionString = Object.keys(resolution || {})
  276. .map(ssrc => {
  277. const { width, height } = resolution[ssrc];
  278. return `${width}x${height}`;
  279. })
  280. .join(', ') || null;
  281. const frameRateString = Object.keys(framerate || {})
  282. .map(ssrc => framerate[ssrc])
  283. .join(', ') || null;
  284. return resolutionString && frameRateString ? `${resolutionString}@${frameRateString}fps` : undefined;
  285. }
  286. /**
  287. * Extracts the download and upload bitrates.
  288. *
  289. * @param {Object} stats - Connection stats from the library.
  290. * @private
  291. * @returns {{ download, upload }}
  292. */
  293. _extractBitrate(stats: any) {
  294. return stats.bitrate;
  295. }
  296. /**
  297. * Extracts the download and upload packet lost.
  298. *
  299. * @param {Object} stats - Connection stats from the library.
  300. * @private
  301. * @returns {{ download, upload }}
  302. */
  303. _extractPacketLost(stats: any) {
  304. return stats.packetLoss;
  305. }
  306. /**
  307. * Extracts the server name.
  308. *
  309. * @param {Object} stats - Connection stats from the library.
  310. * @private
  311. * @returns {string}
  312. */
  313. _extractServer(stats: any) {
  314. return stats.serverRegion;
  315. }
  316. /**
  317. * Extracts the audio and video codecs names.
  318. *
  319. * @param {Object} stats - Connection stats from the library.
  320. * @private
  321. * @returns {string}
  322. */
  323. _extractCodecs(stats: any) {
  324. const { codec } = stats;
  325. let codecString;
  326. if (codec) {
  327. const audioCodecs = Object.values(codec)
  328. .map((c: any) => c.audio)
  329. .filter(Boolean);
  330. const videoCodecs = Object.values(codec)
  331. .map((c: any) => c.video)
  332. .filter(Boolean);
  333. if (audioCodecs.length || videoCodecs.length) {
  334. // Use a Set to eliminate duplicates.
  335. codecString = Array.from(new Set([ ...audioCodecs, ...videoCodecs ])).join(', ');
  336. }
  337. }
  338. return codecString;
  339. }
  340. /**
  341. * Extracts the connection percentage and sets connection quality.
  342. *
  343. * @param {Object} stats - Connection stats from the library.
  344. * @private
  345. * @returns {string}
  346. */
  347. _extractConnection(stats: any) {
  348. const { connectionQuality } = stats;
  349. const {
  350. _isConnectionStatusInactive,
  351. _isConnectionStatusInterrupted
  352. } = this.props;
  353. if (_isConnectionStatusInactive) {
  354. return 'connectionindicator.quality.inactive';
  355. } else if (_isConnectionStatusInterrupted) {
  356. return 'connectionindicator.quality.lost';
  357. } else if (typeof connectionQuality === 'undefined') {
  358. return 'connectionindicator.quality.good';
  359. }
  360. const qualityConfig = this._getQualityConfig(connectionQuality);
  361. return qualityConfig.msg;
  362. }
  363. /**
  364. * Get the quality configuration from CONNECTION_QUALITY which has a percentage
  365. * that matches or exceeds the passed in percentage. The implementation
  366. * assumes CONNECTION_QUALITY is already sorted by highest to lowest
  367. * percentage.
  368. *
  369. * @param {number} percent - The connection percentage, out of 100, to find
  370. * the closest matching configuration for.
  371. * @private
  372. * @returns {Object}
  373. */
  374. _getQualityConfig(percent: number): any {
  375. return CONNECTION_QUALITY.find(x => percent >= x.percent) || {};
  376. }
  377. /**
  378. * Callback to hide the {@code ConnectionStatusComponent}.
  379. *
  380. * @private
  381. * @returns {boolean}
  382. */
  383. _onCancel() {
  384. statsEmitter.unsubscribeToClientStats(this.props.participantID, this._onStatsUpdated);
  385. this.props.dispatch(hideSheet());
  386. }
  387. /**
  388. * Function to render the menu's header.
  389. *
  390. * @returns {React$Element}
  391. */
  392. _renderMenuHeader() {
  393. const { participantID } = this.props;
  394. return (
  395. <View
  396. style = { [
  397. bottomSheetStyles.sheet,
  398. styles.participantNameContainer ] }>
  399. <Avatar
  400. participantId = { participantID }
  401. size = { AVATAR_SIZE } />
  402. <Text style = { styles.participantNameLabel }>
  403. { this.props._participantDisplayName }
  404. </Text>
  405. </View>
  406. );
  407. }
  408. }
  409. /**
  410. * Function that maps parts of Redux state tree into component props.
  411. *
  412. * @param {Object} state - Redux state.
  413. * @param {Object} ownProps - Properties of component.
  414. * @private
  415. * @returns {Props}
  416. */
  417. function _mapStateToProps(state: IReduxState, ownProps: IProps) {
  418. const { participantID } = ownProps;
  419. const tracks = state['features/base/tracks'];
  420. const _videoTrack = getTrackByMediaTypeAndParticipant(tracks, MEDIA_TYPE.VIDEO, participantID);
  421. const _isConnectionStatusInactive = isTrackStreamingStatusInactive(_videoTrack);
  422. const _isConnectionStatusInterrupted = isTrackStreamingStatusInterrupted(_videoTrack);
  423. return {
  424. _isConnectionStatusInactive,
  425. _isConnectionStatusInterrupted,
  426. _participantDisplayName: getParticipantDisplayName(state, participantID)
  427. };
  428. }
  429. export default translate(connect(_mapStateToProps)(withTheme(ConnectionStatusComponent)));