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.js 13KB

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