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

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