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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 { getSourceNameSignalingFeatureFlag } from '../../../base/config';
  7. import { BottomSheet, isDialogOpen, hideDialog } from '../../../base/dialog';
  8. import { bottomSheetStyles } from '../../../base/dialog/components/native/styles';
  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 { getSourceNameByParticipantId } from '../../../base/tracks';
  15. import statsEmitter from '../../../connection-indicator/statsEmitter';
  16. import styles from './styles';
  17. /**
  18. * Size of the rendered avatar in the menu.
  19. */
  20. const AVATAR_SIZE = 25;
  21. const CONNECTION_QUALITY = [
  22. 'Low',
  23. 'Medium',
  24. 'Good'
  25. ];
  26. export type Props = {
  27. /**
  28. * The Redux dispatch function.
  29. */
  30. dispatch: Function,
  31. /**
  32. * The ID of the participant that this button is supposed to pin.
  33. */
  34. participantID: string,
  35. /**
  36. * True if the menu is currently open, false otherwise.
  37. */
  38. _isOpen: boolean,
  39. /**
  40. * Display name of the participant retrieved from Redux.
  41. */
  42. _participantDisplayName: string,
  43. /**
  44. * The function to be used to translate i18n labels.
  45. */
  46. t: Function,
  47. /**
  48. * Theme used for styles.
  49. */
  50. theme: Object,
  51. /**
  52. * The source name of the track.
  53. */
  54. _sourceName: string,
  55. /**
  56. * Whether source name signaling is enabled.
  57. */
  58. _sourceNameSignalingEnabled: boolean
  59. }
  60. /**
  61. * The type of the React {@code Component} state of {@link ConnectionStatusComponent}.
  62. */
  63. type State = {
  64. resolutionString: string,
  65. downloadString: string,
  66. uploadString: string,
  67. packetLostDownloadString: string,
  68. packetLostUploadString: string,
  69. serverRegionString: string,
  70. codecString: string,
  71. connectionString: string
  72. };
  73. // eslint-disable-next-line prefer-const
  74. let ConnectionStatusComponent_;
  75. /**
  76. * Class to implement a popup menu that show the connection statistics.
  77. */
  78. class ConnectionStatusComponent extends Component<Props, State> {
  79. /**
  80. * Constructor of the component.
  81. *
  82. * @param {P} props - The read-only properties with which the new
  83. * instance is to be initialized.
  84. *
  85. * @inheritdoc
  86. */
  87. constructor(props: Props) {
  88. super(props);
  89. this._onStatsUpdated = this._onStatsUpdated.bind(this);
  90. this._onCancel = this._onCancel.bind(this);
  91. this._renderMenuHeader = this._renderMenuHeader.bind(this);
  92. this.state = {
  93. resolutionString: 'N/A',
  94. downloadString: 'N/A',
  95. uploadString: 'N/A',
  96. packetLostDownloadString: 'N/A',
  97. packetLostUploadString: 'N/A',
  98. serverRegionString: 'N/A',
  99. codecString: 'N/A',
  100. connectionString: 'N/A'
  101. };
  102. }
  103. /**
  104. * Implements React's {@link Component#render()}.
  105. *
  106. * @inheritdoc
  107. * @returns {React$Node}
  108. */
  109. render(): React$Node {
  110. const { t, theme } = this.props;
  111. const { palette } = theme;
  112. return (
  113. <BottomSheet
  114. onCancel = { this._onCancel }
  115. renderHeader = { this._renderMenuHeader }>
  116. <View style = { styles.statsWrapper }>
  117. <View style = { styles.statsInfoCell }>
  118. <Text style = { styles.statsTitleText }>
  119. { `${t('connectionindicator.status')} ` }
  120. </Text>
  121. <Text style = { styles.statsInfoText }>
  122. { this.state.connectionString }
  123. </Text>
  124. </View>
  125. <View style = { styles.statsInfoCell }>
  126. <Text style = { styles.statsTitleText }>
  127. { `${t('connectionindicator.bitrate')}` }
  128. </Text>
  129. <BaseIndicator
  130. icon = { IconArrowDownLarge }
  131. iconStyle = {{
  132. color: palette.icon03
  133. }} />
  134. <Text style = { styles.statsInfoText }>
  135. { this.state.downloadString }
  136. </Text>
  137. <BaseIndicator
  138. icon = { IconArrowUpLarge }
  139. iconStyle = {{
  140. color: palette.icon03
  141. }} />
  142. <Text style = { styles.statsInfoText }>
  143. { `${this.state.uploadString} Kbps` }
  144. </Text>
  145. </View>
  146. <View style = { styles.statsInfoCell }>
  147. <Text style = { styles.statsTitleText }>
  148. { `${t('connectionindicator.packetloss')}` }
  149. </Text>
  150. <BaseIndicator
  151. icon = { IconArrowDownLarge }
  152. iconStyle = {{
  153. color: palette.icon03
  154. }} />
  155. <Text style = { styles.statsInfoText }>
  156. { this.state.packetLostDownloadString }
  157. </Text>
  158. <BaseIndicator
  159. icon = { IconArrowUpLarge }
  160. iconStyle = {{
  161. color: palette.icon03
  162. }} />
  163. <Text style = { styles.statsInfoText }>
  164. { this.state.packetLostUploadString }
  165. </Text>
  166. </View>
  167. <View style = { styles.statsInfoCell }>
  168. <Text style = { styles.statsTitleText }>
  169. { `${t('connectionindicator.resolution')} ` }
  170. </Text>
  171. <Text style = { styles.statsInfoText }>
  172. { this.state.resolutionString }
  173. </Text>
  174. </View>
  175. <View style = { styles.statsInfoCell }>
  176. <Text style = { styles.statsTitleText }>
  177. { `${t('connectionindicator.codecs')}` }
  178. </Text>
  179. <Text style = { styles.statsInfoText }>
  180. { this.state.codecString }
  181. </Text>
  182. </View>
  183. </View>
  184. </BottomSheet>
  185. );
  186. }
  187. /**
  188. * Starts listening for stat updates.
  189. *
  190. * @inheritdoc
  191. * returns {void}
  192. */
  193. componentDidMount() {
  194. statsEmitter.subscribeToClientStats(
  195. this.props.participantID, this._onStatsUpdated);
  196. if (this.props._sourceNameSignalingEnabled) {
  197. statsEmitter.subscribeToClientStats(
  198. this.props._sourceName, this._onStatsUpdated);
  199. }
  200. }
  201. /**
  202. * Updates which user's stats are being listened to.
  203. *
  204. * @inheritdoc
  205. * returns {void}
  206. */
  207. componentDidUpdate(prevProps: Props) {
  208. if (prevProps.participantID !== this.props.participantID) {
  209. statsEmitter.unsubscribeToClientStats(
  210. prevProps.participantID, this._onStatsUpdated);
  211. statsEmitter.subscribeToClientStats(
  212. this.props.participantID, this._onStatsUpdated);
  213. }
  214. if (this.props._sourceNameSignalingEnabled) {
  215. if (prevProps._sourceName !== this.props._sourceName) {
  216. statsEmitter.unsubscribeToClientStats(
  217. prevProps._sourceName, this._onStatsUpdated);
  218. statsEmitter.subscribeToClientStats(
  219. this.props._sourceName, this._onStatsUpdated);
  220. }
  221. }
  222. }
  223. _onStatsUpdated: Object => void;
  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) {
  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) ?? this.state.connectionString
  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) {
  268. const { framerate, resolution } = stats;
  269. let frameRateString, resolutionString;
  270. if (this.props._sourceNameSignalingEnabled) {
  271. resolutionString = resolution ? `${resolution.width}x${resolution.height}` : null;
  272. frameRateString = framerate || null;
  273. } else {
  274. resolutionString = Object.keys(resolution || {})
  275. .map(ssrc => {
  276. const { width, height } = resolution[ssrc];
  277. return `${width}x${height}`;
  278. })
  279. .join(', ') || null;
  280. frameRateString = Object.keys(framerate || {})
  281. .map(ssrc => framerate[ssrc])
  282. .join(', ') || null;
  283. }
  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) {
  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) {
  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) {
  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) {
  324. const { codec } = stats;
  325. let codecString;
  326. if (this.props._sourceNameSignalingEnabled) {
  327. if (codec) {
  328. codecString = `${codec.audio}, ${codec.video}`;
  329. }
  330. } else {
  331. // Only report one codec, in case there are multiple for a user.
  332. Object.keys(codec || {})
  333. .forEach(ssrc => {
  334. const { audio, video } = codec[ssrc];
  335. codecString = `${audio}, ${video}`;
  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) {
  348. const { connectionQuality } = stats;
  349. if (connectionQuality) {
  350. const signalLevel = Math.floor(connectionQuality / 33.4);
  351. return CONNECTION_QUALITY[signalLevel];
  352. }
  353. }
  354. _onCancel: () => boolean;
  355. /**
  356. * Callback to hide the {@code ConnectionStatusComponent}.
  357. *
  358. * @private
  359. * @returns {boolean}
  360. */
  361. _onCancel() {
  362. statsEmitter.unsubscribeToClientStats(
  363. this.props.participantID, this._onStatsUpdated);
  364. if (this.props._sourceNameSignalingEnabled) {
  365. statsEmitter.unsubscribeToClientStats(
  366. this.props._sourceName, this._onStatsUpdated);
  367. }
  368. if (this.props._isOpen) {
  369. this.props.dispatch(hideDialog(ConnectionStatusComponent_));
  370. return true;
  371. }
  372. return false;
  373. }
  374. _renderMenuHeader: () => React$Element<any>;
  375. /**
  376. * Function to render the menu's header.
  377. *
  378. * @returns {React$Element}
  379. */
  380. _renderMenuHeader() {
  381. const { participantID } = this.props;
  382. return (
  383. <View
  384. style = { [
  385. bottomSheetStyles.sheet,
  386. styles.participantNameContainer ] }>
  387. <Avatar
  388. participantId = { participantID }
  389. size = { AVATAR_SIZE } />
  390. <Text style = { styles.participantNameLabel }>
  391. { this.props._participantDisplayName }
  392. </Text>
  393. </View>
  394. );
  395. }
  396. }
  397. /**
  398. * Function that maps parts of Redux state tree into component props.
  399. *
  400. * @param {Object} state - Redux state.
  401. * @param {Object} ownProps - Properties of component.
  402. * @private
  403. * @returns {Props}
  404. */
  405. function _mapStateToProps(state, ownProps) {
  406. const { participantID } = ownProps;
  407. return {
  408. _isOpen: isDialogOpen(state, ConnectionStatusComponent_),
  409. _participantDisplayName: getParticipantDisplayName(state, participantID),
  410. _sourceNameSignalingEnabled: getSourceNameSignalingFeatureFlag(state),
  411. _sourceName: getSourceNameByParticipantId(state, ownProps.participantId)
  412. };
  413. }
  414. ConnectionStatusComponent_ = translate(connect(_mapStateToProps)(withTheme(ConnectionStatusComponent)));
  415. export default ConnectionStatusComponent_;