您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ConnectionStatusComponent.tsx 15KB

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