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 15KB

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