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

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