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 11KB

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