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

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