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.

VideoQualityDialog.web.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import InlineMessage from '@atlaskit/inline-message';
  2. import PropTypes from 'prop-types';
  3. import React, { Component } from 'react';
  4. import { connect } from 'react-redux';
  5. import { sendAnalyticsEvent } from '../../analytics';
  6. import {
  7. setAudioOnly,
  8. setReceiveVideoQuality,
  9. VIDEO_QUALITY_LEVELS
  10. } from '../../base/conference';
  11. import { translate } from '../../base/i18n';
  12. const logger = require('jitsi-meet-logger').getLogger(__filename);
  13. const {
  14. HIGH,
  15. STANDARD,
  16. LOW
  17. } = VIDEO_QUALITY_LEVELS;
  18. /**
  19. * Implements a React {@link Component} which displays a dialog with a slider
  20. * for selecting a new receive video quality.
  21. *
  22. * @extends Component
  23. */
  24. class VideoQualityDialog extends Component {
  25. /**
  26. * {@code VideoQualityDialog}'s property types.
  27. *
  28. * @static
  29. */
  30. static propTypes = {
  31. /**
  32. * Whether or not the conference is in audio only mode.
  33. */
  34. _audioOnly: PropTypes.bool,
  35. /**
  36. * Whether or not the conference is in peer to peer mode.
  37. */
  38. _p2p: PropTypes.bool,
  39. /**
  40. * The currently configured maximum quality resolution to be received
  41. * from remote participants.
  42. */
  43. _receiveVideoQuality: PropTypes.number,
  44. /**
  45. * Invoked to request toggling of audio only mode.
  46. */
  47. dispatch: PropTypes.func,
  48. /**
  49. * Invoked to obtain translated strings.
  50. */
  51. t: PropTypes.func
  52. };
  53. /**
  54. * Initializes a new {@code VideoQualityDialog} instance.
  55. *
  56. * @param {Object} props - The read-only React Component props with which
  57. * the new instance is to be initialized.
  58. */
  59. constructor(props) {
  60. super(props);
  61. // Bind event handlers so they are only bound once for every instance.
  62. this._enableAudioOnly = this._enableAudioOnly.bind(this);
  63. this._enableHighDefinition = this._enableHighDefinition.bind(this);
  64. this._enableLowDefinition = this._enableLowDefinition.bind(this);
  65. this._enableStandardDefinition
  66. = this._enableStandardDefinition.bind(this);
  67. this._onSliderChange = this._onSliderChange.bind(this);
  68. /**
  69. * An array of configuration options for displaying a choice in the
  70. * input. The onSelect callback will be invoked when the option is
  71. * selected and videoQuality helps determine which choice matches with
  72. * the currently active quality level.
  73. *
  74. * @private
  75. * @type {Object[]}
  76. */
  77. this._sliderOptions = [
  78. {
  79. audioOnly: true,
  80. onSelect: this._enableAudioOnly,
  81. textKey: 'audioOnly.audioOnly'
  82. },
  83. {
  84. onSelect: this._enableLowDefinition,
  85. textKey: 'videoStatus.lowDefinition',
  86. videoQuality: LOW
  87. },
  88. {
  89. onSelect: this._enableStandardDefinition,
  90. textKey: 'videoStatus.standardDefinition',
  91. videoQuality: STANDARD
  92. },
  93. {
  94. onSelect: this._enableHighDefinition,
  95. textKey: 'videoStatus.highDefinition',
  96. videoQuality: HIGH
  97. }
  98. ];
  99. }
  100. /**
  101. * Implements React's {@link Component#render()}.
  102. *
  103. * @inheritdoc
  104. * @returns {ReactElement}
  105. */
  106. render() {
  107. const { _audioOnly, _p2p, t } = this.props;
  108. const activeSliderOption = this._mapCurrentQualityToSliderValue();
  109. const showP2PWarning = _p2p && !_audioOnly;
  110. return (
  111. <div className = 'video-quality-dialog'>
  112. <h3 className = 'video-quality-dialog-title'>
  113. { t('videoStatus.callQuality') }
  114. </h3>
  115. <div className = { showP2PWarning ? '' : 'hide-warning' }>
  116. { this._renderP2PMessage() }
  117. </div>
  118. <div className = 'video-quality-dialog-contents'>
  119. <div className = 'video-quality-dialog-slider-container'>
  120. { /* FIXME: onChange and onMouseUp are both used for
  121. * compatibility with IE11. This workaround can be
  122. * removed after upgrading to React 16.
  123. */ }
  124. <input
  125. className = 'video-quality-dialog-slider'
  126. max = { this._sliderOptions.length - 1 }
  127. min = '0'
  128. onChange = { this._onSliderChange }
  129. onMouseUp = { this._onSliderChange }
  130. step = '1'
  131. type = 'range'
  132. value
  133. = { activeSliderOption } />
  134. </div>
  135. <div className = 'video-quality-dialog-labels'>
  136. { this._createLabels(activeSliderOption) }
  137. </div>
  138. </div>
  139. </div>
  140. );
  141. }
  142. /**
  143. * Creates React Elements for notifying that peer to peer is enabled.
  144. *
  145. * @private
  146. * @returns {ReactElement}
  147. */
  148. _renderP2PMessage() {
  149. const { t } = this.props;
  150. return (
  151. <InlineMessage
  152. secondaryText = { t('videoStatus.recHighDefinitionOnly') }
  153. title = { t('videoStatus.p2pEnabled') }>
  154. { t('videoStatus.p2pVideoQualityDescription') }
  155. </InlineMessage>
  156. );
  157. }
  158. /**
  159. * Creates React Elements to display mock tick marks with associated labels.
  160. *
  161. * @param {number} activeLabelIndex - Which of the sliderOptions should
  162. * display as currently active.
  163. * @private
  164. * @returns {ReactElement[]}
  165. */
  166. _createLabels(activeLabelIndex) {
  167. const labelsCount = this._sliderOptions.length;
  168. const maxWidthOfLabel = `${100 / labelsCount}%`;
  169. return this._sliderOptions.map((sliderOption, index) => {
  170. const style = {
  171. maxWidth: maxWidthOfLabel,
  172. left: `${(index * 100) / (labelsCount - 1)}%`
  173. };
  174. const isActiveClass = activeLabelIndex === index ? 'active' : '';
  175. const className
  176. = `video-quality-dialog-label-container ${isActiveClass}`;
  177. return (
  178. <div
  179. className = { className }
  180. key = { index }
  181. style = { style }>
  182. <div className = 'video-quality-dialog-label'>
  183. { this.props.t(sliderOption.textKey) }
  184. </div>
  185. </div>
  186. );
  187. });
  188. }
  189. /**
  190. * Dispatches an action to enable audio only mode.
  191. *
  192. * @private
  193. * @returns {void}
  194. */
  195. _enableAudioOnly() {
  196. sendAnalyticsEvent('toolbar.audioonly.enabled');
  197. logger.log('Video quality: audio only enabled');
  198. this.props.dispatch(setAudioOnly(true));
  199. }
  200. /**
  201. * Dispatches an action to receive high quality video from remote
  202. * participants.
  203. *
  204. * @private
  205. * @returns {void}
  206. */
  207. _enableHighDefinition() {
  208. sendAnalyticsEvent('toolbar.videoquality.high');
  209. logger.log('Video quality: high enabled');
  210. this.props.dispatch(setReceiveVideoQuality(HIGH));
  211. }
  212. /**
  213. * Dispatches an action to receive low quality video from remote
  214. * participants.
  215. *
  216. * @private
  217. * @returns {void}
  218. */
  219. _enableLowDefinition() {
  220. sendAnalyticsEvent('toolbar.videoquality.low');
  221. logger.log('Video quality: low enabled');
  222. this.props.dispatch(setReceiveVideoQuality(LOW));
  223. }
  224. /**
  225. * Dispatches an action to receive standard quality video from remote
  226. * participants.
  227. *
  228. * @private
  229. * @returns {void}
  230. */
  231. _enableStandardDefinition() {
  232. sendAnalyticsEvent('toolbar.videoquality.standard');
  233. logger.log('Video quality: standard enabled');
  234. this.props.dispatch(setReceiveVideoQuality(STANDARD));
  235. }
  236. /**
  237. * Matches the current video quality state with corresponding index of the
  238. * component's slider options.
  239. *
  240. * @private
  241. * @returns {void}
  242. */
  243. _mapCurrentQualityToSliderValue() {
  244. const { _audioOnly, _receiveVideoQuality } = this.props;
  245. const { _sliderOptions } = this;
  246. if (_audioOnly) {
  247. const audioOnlyOption = _sliderOptions.find(
  248. ({ audioOnly }) => audioOnly);
  249. return _sliderOptions.indexOf(audioOnlyOption);
  250. }
  251. const matchingOption = _sliderOptions.find(
  252. ({ videoQuality }) => videoQuality === _receiveVideoQuality);
  253. return _sliderOptions.indexOf(matchingOption);
  254. }
  255. /**
  256. * Invokes a callback when the selected video quality changes.
  257. *
  258. * @param {Object} event - The slider's change event.
  259. * @private
  260. * @returns {void}
  261. */
  262. _onSliderChange(event) {
  263. const { _audioOnly, _receiveVideoQuality } = this.props;
  264. const {
  265. audioOnly,
  266. onSelect,
  267. videoQuality
  268. } = this._sliderOptions[event.target.value];
  269. // Take no action if the newly chosen option does not change audio only
  270. // or video quality state.
  271. if ((_audioOnly && audioOnly)
  272. || (!_audioOnly && videoQuality === _receiveVideoQuality)) {
  273. return;
  274. }
  275. onSelect();
  276. }
  277. }
  278. /**
  279. * Maps (parts of) the Redux state to the associated props for the
  280. * {@code VideoQualityDialog} component.
  281. *
  282. * @param {Object} state - The Redux state.
  283. * @private
  284. * @returns {{
  285. * _audioOnly: boolean,
  286. * _p2p: boolean,
  287. * _receiveVideoQuality: boolean
  288. * }}
  289. */
  290. function _mapStateToProps(state) {
  291. const {
  292. audioOnly,
  293. p2p,
  294. receiveVideoQuality
  295. } = state['features/base/conference'];
  296. return {
  297. _audioOnly: audioOnly,
  298. _p2p: p2p,
  299. _receiveVideoQuality: receiveVideoQuality
  300. };
  301. }
  302. export default translate(connect(_mapStateToProps)(VideoQualityDialog));