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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. return (
  107. <div className = 'video-quality-dialog'>
  108. <h3 className = 'video-quality-dialog-title'>
  109. { t('videoStatus.callQuality') }
  110. </h3>
  111. { !_audioOnly && _p2p ? this._renderP2PMessage() : null }
  112. <div className = 'video-quality-dialog-contents'>
  113. <div className = 'video-quality-dialog-slider-container'>
  114. { /* FIXME: onChange and onMouseUp are both used for
  115. * compatibility with IE11. This workaround can be
  116. * removed after upgrading to React 16.
  117. */ }
  118. <input
  119. className = 'video-quality-dialog-slider'
  120. max = { this._sliderOptions.length - 1 }
  121. min = '0'
  122. onChange = { this._onSliderChange }
  123. onMouseUp = { this._onSliderChange }
  124. step = '1'
  125. type = 'range'
  126. value
  127. = { activeSliderOption } />
  128. </div>
  129. <div className = 'video-quality-dialog-labels'>
  130. { this._createLabels(activeSliderOption) }
  131. </div>
  132. </div>
  133. </div>
  134. );
  135. }
  136. /**
  137. * Creates React Elements for notifying that peer to peer is enabled.
  138. *
  139. * @private
  140. * @returns {ReactElement}
  141. */
  142. _renderP2PMessage() {
  143. const { t } = this.props;
  144. return (
  145. <InlineMessage
  146. secondaryText = { t('videoStatus.recHighDefinitionOnly') }
  147. title = { t('videoStatus.p2pEnabled') }>
  148. { t('videoStatus.p2pVideoQualityDescription') }
  149. </InlineMessage>
  150. );
  151. }
  152. /**
  153. * Creates React Elements to display mock tick marks with associated labels.
  154. *
  155. * @param {number} activeLabelIndex - Which of the sliderOptions should
  156. * display as currently active.
  157. * @private
  158. * @returns {ReactElement[]}
  159. */
  160. _createLabels(activeLabelIndex) {
  161. const labelsCount = this._sliderOptions.length;
  162. const maxWidthOfLabel = `${100 / labelsCount}%`;
  163. return this._sliderOptions.map((sliderOption, index) => {
  164. const style = {
  165. maxWidth: maxWidthOfLabel,
  166. left: `${(index * 100) / (labelsCount - 1)}%`
  167. };
  168. const isActiveClass = activeLabelIndex === index ? 'active' : '';
  169. const className
  170. = `video-quality-dialog-label-container ${isActiveClass}`;
  171. return (
  172. <div
  173. className = { className }
  174. key = { index }
  175. style = { style }>
  176. <div className = 'video-quality-dialog-label'>
  177. { this.props.t(sliderOption.textKey) }
  178. </div>
  179. </div>
  180. );
  181. });
  182. }
  183. /**
  184. * Dispatches an action to enable audio only mode.
  185. *
  186. * @private
  187. * @returns {void}
  188. */
  189. _enableAudioOnly() {
  190. this.props.dispatch(setAudioOnly(true));
  191. }
  192. /**
  193. * Dispatches an action to receive high quality video from remote
  194. * participants.
  195. *
  196. * @private
  197. * @returns {void}
  198. */
  199. _enableHighDefinition() {
  200. this.props.dispatch(setReceiveVideoQuality(HIGH));
  201. }
  202. /**
  203. * Dispatches an action to receive low quality video from remote
  204. * participants.
  205. *
  206. * @private
  207. * @returns {void}
  208. */
  209. _enableLowDefinition() {
  210. this.props.dispatch(setReceiveVideoQuality(LOW));
  211. }
  212. /**
  213. * Dispatches an action to receive standard quality video from remote
  214. * participants.
  215. *
  216. * @private
  217. * @returns {void}
  218. */
  219. _enableStandardDefinition() {
  220. this.props.dispatch(setReceiveVideoQuality(STANDARD));
  221. }
  222. /**
  223. * Matches the current video quality state with corresponding index of the
  224. * component's slider options.
  225. *
  226. * @private
  227. * @returns {void}
  228. */
  229. _mapCurrentQualityToSliderValue() {
  230. const { _audioOnly, _receiveVideoQuality } = this.props;
  231. const { _sliderOptions } = this;
  232. if (_audioOnly) {
  233. const audioOnlyOption = _sliderOptions.find(
  234. ({ audioOnly }) => audioOnly);
  235. return _sliderOptions.indexOf(audioOnlyOption);
  236. }
  237. const matchingOption = _sliderOptions.find(
  238. ({ videoQuality }) => videoQuality === _receiveVideoQuality);
  239. return _sliderOptions.indexOf(matchingOption);
  240. }
  241. /**
  242. * Invokes a callback when the selected video quality changes.
  243. *
  244. * @param {Object} event - The slider's change event.
  245. * @private
  246. * @returns {void}
  247. */
  248. _onSliderChange(event) {
  249. const { _audioOnly, _receiveVideoQuality } = this.props;
  250. const {
  251. audioOnly,
  252. onSelect,
  253. videoQuality
  254. } = this._sliderOptions[event.target.value];
  255. // Take no action if the newly chosen option does not change audio only
  256. // or video quality state.
  257. if ((_audioOnly && audioOnly)
  258. || (!_audioOnly && videoQuality === _receiveVideoQuality)) {
  259. return;
  260. }
  261. onSelect();
  262. }
  263. }
  264. /**
  265. * Maps (parts of) the Redux state to the associated props for the
  266. * {@code VideoQualityDialog} component.
  267. *
  268. * @param {Object} state - The Redux state.
  269. * @private
  270. * @returns {{
  271. * _audioOnly: boolean,
  272. * _p2p: boolean,
  273. * _receiveVideoQuality: boolean
  274. * }}
  275. */
  276. function _mapStateToProps(state) {
  277. const {
  278. audioOnly,
  279. p2p,
  280. receiveVideoQuality
  281. } = state['features/base/conference'];
  282. return {
  283. _audioOnly: audioOnly,
  284. _p2p: p2p,
  285. _receiveVideoQuality: receiveVideoQuality
  286. };
  287. }
  288. export default translate(connect(_mapStateToProps)(VideoQualityDialog));