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

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