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

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