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.

ConnectionIndicator.js 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import React, { Component } from 'react';
  2. import JitsiPopover from '../../../../modules/UI/util/JitsiPopover';
  3. import { JitsiParticipantConnectionStatus } from '../../base/lib-jitsi-meet';
  4. import { ConnectionStatsTable } from '../../connection-stats';
  5. import statsEmitter from '../statsEmitter';
  6. declare var $: Object;
  7. declare var interfaceConfig: Object;
  8. // Converts the percent for connection quality into a string recognized for CSS.
  9. const QUALITY_TO_WIDTH = [
  10. // Full (5 bars)
  11. {
  12. percent: 80,
  13. width: '100%'
  14. },
  15. // 4 bars
  16. {
  17. percent: 60,
  18. width: '80%'
  19. },
  20. // 3 bars
  21. {
  22. percent: 40,
  23. width: '55%'
  24. },
  25. // 2 bars
  26. {
  27. percent: 20,
  28. width: '40%'
  29. },
  30. // 1 bar
  31. {
  32. percent: 0,
  33. width: '20%'
  34. }
  35. // Note: we never show 0 bars.
  36. ];
  37. /**
  38. * Implements a React {@link Component} which displays the current connection
  39. * quality percentage and has a popover to show more detailed connection stats.
  40. *
  41. * @extends {Component}
  42. */
  43. class ConnectionIndicator extends Component {
  44. /**
  45. * {@code ConnectionIndicator} component's property types.
  46. *
  47. * @static
  48. */
  49. static propTypes = {
  50. /**
  51. * The current condition of the user's connection, matching one of the
  52. * enumerated values in the library.
  53. *
  54. * @type {JitsiParticipantConnectionStatus}
  55. */
  56. connectionStatus: React.PropTypes.string,
  57. /**
  58. * Whether or not the displays stats are for local video.
  59. */
  60. isLocalVideo: React.PropTypes.bool,
  61. /**
  62. * The callback to invoke when the hover state over the popover changes.
  63. */
  64. onHover: React.PropTypes.func,
  65. /**
  66. * Whether or not the popover should display a link that can toggle
  67. * a more detailed view of the stats.
  68. */
  69. showMoreLink: React.PropTypes.bool,
  70. /**
  71. * Invoked to obtain translated strings.
  72. */
  73. t: React.PropTypes.func,
  74. /**
  75. * The user ID associated with the displayed connection indication and
  76. * stats.
  77. */
  78. userID: React.PropTypes.string
  79. };
  80. /**
  81. * Initializes a new {@code ConnectionIndicator} instance.
  82. *
  83. * @param {Object} props - The read-only properties with which the new
  84. * instance is to be initialized.
  85. */
  86. constructor(props) {
  87. super(props);
  88. /**
  89. * The internal reference to topmost DOM/HTML element backing the React
  90. * {@code Component}. Accessed directly for associating an element as
  91. * the trigger for a popover.
  92. *
  93. * @private
  94. * @type {HTMLDivElement}
  95. */
  96. this._rootElement = null;
  97. this.state = {
  98. /**
  99. * Whether or not the popover content should display additional
  100. * statistics.
  101. *
  102. * @type {boolean}
  103. */
  104. showMoreStats: false,
  105. /**
  106. * Cache of the stats received from subscribing to stats emitting.
  107. * The keys should be the name of the stat. With each stat update,
  108. * updates stats are mixed in with cached stats and a new stats
  109. * object is set in state.
  110. */
  111. stats: {}
  112. };
  113. // Bind event handlers so they are only bound once for every instance.
  114. this._onStatsUpdated = this._onStatsUpdated.bind(this);
  115. this._onToggleShowMore = this._onToggleShowMore.bind(this);
  116. this._setRootElement = this._setRootElement.bind(this);
  117. }
  118. /**
  119. * Creates a popover instance to display when the component is hovered.
  120. *
  121. * @inheritdoc
  122. * returns {void}
  123. */
  124. componentDidMount() {
  125. statsEmitter.subscribeToClientStats(
  126. this.props.userID, this._onStatsUpdated);
  127. this.popover = new JitsiPopover($(this._rootElement), {
  128. content: this._renderStatisticsTable(),
  129. skin: 'black',
  130. position: interfaceConfig.VERTICAL_FILMSTRIP ? 'left' : 'top'
  131. });
  132. this.popover.addOnHoverPopover(this.props.onHover);
  133. }
  134. /**
  135. * Updates the contents of the popover. This is done manually because the
  136. * popover is not a React Component yet and so is not automatiucally aware
  137. * of changed data.
  138. *
  139. * @inheritdoc
  140. * returns {void}
  141. */
  142. componentDidUpdate(prevProps) {
  143. if (prevProps.userID !== this.props.userID) {
  144. statsEmitter.unsubscribeToClientStats(
  145. prevProps.userID, this._onStatsUpdated);
  146. statsEmitter.subscribeToClientStats(
  147. this.props.userID, this._onStatsUpdated);
  148. }
  149. this.popover.updateContent(this._renderStatisticsTable());
  150. }
  151. /**
  152. * Cleans up any popover instance that is linked to the component.
  153. *
  154. * @inheritdoc
  155. * returns {void}
  156. */
  157. componentWillUnmount() {
  158. statsEmitter.unsubscribeToClientStats(
  159. this.props.userID, this._onStatsUpdated);
  160. this.popover.forceHide();
  161. this.popover.remove();
  162. }
  163. /**
  164. * Implements React's {@link Component#render()}.
  165. *
  166. * @inheritdoc
  167. * @returns {ReactElement}
  168. */
  169. render() {
  170. return (
  171. <div
  172. className = 'connection-indicator indicator'
  173. ref = { this._setRootElement }>
  174. <div className = 'connection indicatoricon'>
  175. { this._renderIcon() }
  176. </div>
  177. </div>
  178. );
  179. }
  180. /**
  181. * Callback invoked when new connection stats associated with the passed in
  182. * user ID are available. Will update the component's display of current
  183. * statistics.
  184. *
  185. * @param {Object} stats - Connection stats from the library.
  186. * @private
  187. * @returns {void}
  188. */
  189. _onStatsUpdated(stats = {}) {
  190. const { connectionQuality } = stats;
  191. const newPercentageState = typeof connectionQuality === 'undefined'
  192. ? {} : { percent: connectionQuality };
  193. const newStats = Object.assign(
  194. {},
  195. this.state.stats,
  196. stats,
  197. newPercentageState);
  198. this.setState({
  199. stats: newStats
  200. });
  201. }
  202. /**
  203. * Callback to invoke when the show more link in the popover content is
  204. * clicked. Sets the state which will determine if the popover should show
  205. * additional statistics about the connection.
  206. *
  207. * @returns {void}
  208. */
  209. _onToggleShowMore() {
  210. this.setState({ showMoreStats: !this.state.showMoreStats });
  211. }
  212. /**
  213. * Creates a ReactElement for displaying an icon that represents the current
  214. * connection quality.
  215. *
  216. * @returns {ReactElement}
  217. */
  218. _renderIcon() {
  219. switch (this.props.connectionStatus) {
  220. case JitsiParticipantConnectionStatus.INTERRUPTED:
  221. return (
  222. <span className = 'connection_lost'>
  223. <i className = 'icon-connection-lost' />
  224. </span>
  225. );
  226. case JitsiParticipantConnectionStatus.INACTIVE:
  227. return (
  228. <span className = 'connection_ninja'>
  229. <i className = 'icon-ninja' />
  230. </span>
  231. );
  232. default: {
  233. const { percent } = this.state.stats;
  234. const width = QUALITY_TO_WIDTH.find(x => percent >= x.percent);
  235. const iconWidth = width && width.width
  236. ? { width: width && width.width } : {};
  237. return [
  238. <span
  239. className = 'connection_empty'
  240. key = 'icon-empty'>
  241. <i className = 'icon-connection' />
  242. </span>,
  243. <span
  244. className = 'connection_full'
  245. key = 'icon-full'
  246. style = { iconWidth }>
  247. <i className = 'icon-connection' />
  248. </span>
  249. ];
  250. }
  251. }
  252. }
  253. /**
  254. * Creates a {@code ConnectionStatisticsTable} instance.
  255. *
  256. * @returns {ReactElement}
  257. */
  258. _renderStatisticsTable() {
  259. const {
  260. bandwidth,
  261. bitrate,
  262. framerate,
  263. packetLoss,
  264. resolution,
  265. transport
  266. } = this.state.stats;
  267. return (
  268. <ConnectionStatsTable
  269. bandwidth = { bandwidth }
  270. bitrate = { bitrate }
  271. framerate = { framerate }
  272. isLocalVideo = { this.props.isLocalVideo }
  273. onShowMore = { this._onToggleShowMore }
  274. packetLoss = { packetLoss }
  275. resolution = { resolution }
  276. shouldShowMore = { this.state.showMoreStats }
  277. transport = { transport } />
  278. );
  279. }
  280. /**
  281. * Sets an internal reference to the component's root element.
  282. *
  283. * @param {Object} element - The highest DOM element in the component.
  284. * @private
  285. * @returns {void}
  286. */
  287. _setRootElement(element) {
  288. this._rootElement = element;
  289. }
  290. }
  291. export default ConnectionIndicator;