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

TestConnectionInfo.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // @flow
  2. import React, { Component, Fragment } from 'react';
  3. import { statsEmitter } from '../../../connection-indicator';
  4. import { getLocalParticipant } from '../../participants';
  5. import { connect } from '../../redux';
  6. import { isTestModeEnabled } from '../functions';
  7. import { TestHint } from './index';
  8. /**
  9. * Defines the TestConnectionInfo's properties.
  10. */
  11. type Props = {
  12. /**
  13. * The JitsiConference's connection state. It's the lib-jitsi-meet's event
  14. * name converted to a string directly. At the time of this writing these
  15. * are the possible values:
  16. * 'conference.connectionEstablished'
  17. * 'conference.connectionInterrupted'
  18. * 'conference.connectionRestored'
  19. */
  20. _conferenceConnectionState: string,
  21. /**
  22. * This will be a boolean converted to a string. The value will be 'true'
  23. * once the conference is joined (the XMPP MUC room to be specific).
  24. */
  25. _conferenceJoinedState: string,
  26. /**
  27. * The local participant's ID. Required to be able to observe the local RTP
  28. * stats.
  29. */
  30. _localUserId: string,
  31. /**
  32. * Indicates whether or not the test mode is currently on. Otherwise the
  33. * TestConnectionInfo component will not render.
  34. */
  35. _testMode: boolean
  36. }
  37. /**
  38. * Describes the TestConnectionInfo's state.
  39. */
  40. type State = {
  41. /**
  42. * The RTP stats section.
  43. */
  44. stats: {
  45. /**
  46. * The local bitrate.
  47. */
  48. bitrate: {
  49. /**
  50. * The local download RTP bitrate.
  51. */
  52. download: number,
  53. /**
  54. * The local upload RTP bitrate.
  55. */
  56. upload: number
  57. }
  58. }
  59. }
  60. /**
  61. * The component will expose some of the app state to the jitsi-meet-torture
  62. * through the UI accessibility layer which is visible to the tests. The Web
  63. * tests currently will execute JavaScript and access globals variables to learn
  64. * this information, but there's no such option on React Native(maybe that's
  65. * a good thing).
  66. */
  67. class TestConnectionInfo extends Component<Props, State> {
  68. _onStatsUpdated: Object => void;
  69. /**
  70. * Initializes new <tt>TestConnectionInfo</tt> instance.
  71. *
  72. * @param {Object} props - The read-only properties with which the new
  73. * instance is to be initialized.
  74. */
  75. constructor(props: Props) {
  76. super(props);
  77. this._onStatsUpdated = this._onStatsUpdated.bind(this);
  78. this.state = {
  79. stats: {
  80. bitrate: {
  81. download: 0,
  82. upload: 0
  83. }
  84. }
  85. };
  86. }
  87. /**
  88. * The {@link statsEmitter} callback hoked up for the local participant.
  89. *
  90. * @param {Object} stats - These are the RTP stats. Look in
  91. * the lib-jitsi-meet for more details on the actual structure or add
  92. * a console print and figure out there.
  93. * @returns {void}
  94. * @private
  95. */
  96. _onStatsUpdated(stats = {}) {
  97. this.setState({
  98. stats: {
  99. bitrate: {
  100. download: stats.bitrate?.download || 0,
  101. upload: stats.bitrate?.upload || 0
  102. }
  103. }
  104. });
  105. }
  106. /**
  107. * Starts listening for the local RTP stat updates.
  108. *
  109. * @inheritdoc
  110. * returns {void}
  111. */
  112. componentDidMount() {
  113. statsEmitter.subscribeToClientStats(
  114. this.props._localUserId, this._onStatsUpdated);
  115. }
  116. /**
  117. * Updates which user's stats are being listened to (the local participant's
  118. * id changes).
  119. *
  120. * @inheritdoc
  121. * returns {void}
  122. */
  123. componentDidUpdate(prevProps: Props) {
  124. if (prevProps._localUserId !== this.props._localUserId) {
  125. statsEmitter.unsubscribeToClientStats(
  126. prevProps._localUserId, this._onStatsUpdated);
  127. statsEmitter.subscribeToClientStats(
  128. this.props._localUserId, this._onStatsUpdated);
  129. }
  130. }
  131. /**
  132. * Removes the local stats listener.
  133. *
  134. * @private
  135. * @returns {void}
  136. */
  137. componentWillUnmount() {
  138. statsEmitter.unsubscribeToClientStats(
  139. this.props._localUserId, this._onStatsUpdated);
  140. }
  141. /**
  142. * Renders the component if the app is currently running in the test mode
  143. * (config.testing.testMode == true).
  144. *
  145. * @returns {ReactElement|null}
  146. */
  147. render() {
  148. if (!this.props._testMode) {
  149. return null;
  150. }
  151. return (
  152. <Fragment accessible = { false } >
  153. <TestHint
  154. id = 'org.jitsi.meet.conference.connectionState'
  155. value = { this.props._conferenceConnectionState } />
  156. <TestHint
  157. id = 'org.jitsi.meet.conference.joinedState'
  158. value = { this.props._conferenceJoinedState } />
  159. <TestHint
  160. id = 'org.jitsi.meet.stats.rtp'
  161. value = { JSON.stringify(this.state.stats) } />
  162. </Fragment>
  163. );
  164. }
  165. }
  166. /**
  167. * Maps (parts of) the Redux state to the associated TestConnectionInfo's props.
  168. *
  169. * @param {Object} state - The Redux state.
  170. * @private
  171. * @returns {{
  172. * _conferenceConnectionState: string,
  173. * _conferenceJoinedState: string,
  174. * _localUserId: string,
  175. * _testMode: boolean
  176. * }}
  177. */
  178. function _mapStateToProps(state) {
  179. const conferenceJoined
  180. = Boolean(state['features/base/conference'].conference);
  181. const localParticipant = getLocalParticipant(state);
  182. return {
  183. _conferenceConnectionState: state['features/testing'].connectionState,
  184. _conferenceJoinedState: conferenceJoined.toString(),
  185. _localUserId: localParticipant && localParticipant.id,
  186. _testMode: isTestModeEnabled(state)
  187. };
  188. }
  189. export default connect(_mapStateToProps)(TestConnectionInfo);