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.

VideoQualitySlider.web.js 12KB

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