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.

TestConnectionInfo.js 5.8KB

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