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

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