Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

VideoQualitySlider.web.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. import { Theme } from '@mui/material';
  2. import { withStyles } from '@mui/styles';
  3. import clsx from 'clsx';
  4. import React, { Component } from 'react';
  5. import { WithTranslation } from 'react-i18next';
  6. import { createToolbarEvent } from '../../analytics/AnalyticsEvents';
  7. import { sendAnalytics } from '../../analytics/functions';
  8. import { IReduxState, IStore } from '../../app/types';
  9. import { setAudioOnly } from '../../base/audio-only/actions';
  10. import { translate } from '../../base/i18n/functions';
  11. import { setLastN } from '../../base/lastn/actions';
  12. import { getLastNForQualityLevel } from '../../base/lastn/functions';
  13. import { connect } from '../../base/redux/functions';
  14. import { withPixelLineHeight } from '../../base/styles/functions.web';
  15. // eslint-disable-next-line lines-around-comment
  16. // @ts-ignore
  17. import { setPreferredVideoQuality } from '../actions';
  18. import { DEFAULT_LAST_N, VIDEO_QUALITY_LEVELS } from '../constants';
  19. import logger from '../logger';
  20. import Slider from './Slider.web';
  21. const {
  22. ULTRA,
  23. HIGH,
  24. STANDARD,
  25. LOW
  26. } = VIDEO_QUALITY_LEVELS;
  27. /**
  28. * Creates an analytics event for a press of one of the buttons in the video
  29. * quality dialog.
  30. *
  31. * @param {string} quality - The quality which was selected.
  32. * @returns {Object} The event in a format suitable for sending via
  33. * sendAnalytics.
  34. */
  35. const createEvent = function(quality: string) {
  36. return createToolbarEvent(
  37. 'video.quality',
  38. {
  39. quality
  40. });
  41. };
  42. /**
  43. * The type of the React {@code Component} props of {@link VideoQualitySlider}.
  44. */
  45. interface IProps extends WithTranslation {
  46. /**
  47. * Whether or not the conference is in audio only mode.
  48. */
  49. _audioOnly: Boolean;
  50. /**
  51. * The channelLastN value configured for the conference.
  52. */
  53. _channelLastN: number;
  54. /**
  55. * Whether or not the conference is in peer to peer mode.
  56. */
  57. _p2p: Boolean;
  58. /**
  59. * The currently configured maximum quality resolution to be sent and
  60. * received from the remote participants.
  61. */
  62. _sendrecvVideoQuality: number;
  63. /**
  64. * An object containing the CSS classes.
  65. */
  66. classes: any;
  67. /**
  68. * Invoked to request toggling of audio only mode.
  69. */
  70. dispatch: IStore['dispatch'];
  71. }
  72. /**
  73. * Creates the styles for the component.
  74. *
  75. * @param {Object} theme - The current UI theme.
  76. *
  77. * @returns {Object}
  78. */
  79. const styles = (theme: Theme) => {
  80. return {
  81. dialog: {
  82. color: theme.palette.text01
  83. },
  84. dialogDetails: {
  85. ...withPixelLineHeight(theme.typography.bodyShortRegularLarge),
  86. marginBottom: 16
  87. },
  88. dialogContents: {
  89. background: theme.palette.ui01,
  90. padding: '16px 16px 48px 16px'
  91. },
  92. sliderDescription: {
  93. ...withPixelLineHeight(theme.typography.heading6),
  94. display: 'flex',
  95. justifyContent: 'space-between',
  96. marginBottom: 40
  97. }
  98. };
  99. };
  100. /**
  101. * Implements a React {@link Component} which displays a slider for selecting a
  102. * new receive video quality.
  103. *
  104. * @augments Component
  105. */
  106. class VideoQualitySlider extends Component<IProps> {
  107. _sliderOptions: Array<Object>;
  108. /**
  109. * Initializes a new {@code VideoQualitySlider} instance.
  110. *
  111. * @param {Object} props - The read-only React Component props with which
  112. * the new instance is to be initialized.
  113. */
  114. constructor(props: IProps) {
  115. super(props);
  116. // Bind event handlers so they are only bound once for every instance.
  117. this._enableAudioOnly = this._enableAudioOnly.bind(this);
  118. this._enableHighDefinition = this._enableHighDefinition.bind(this);
  119. this._enableLowDefinition = this._enableLowDefinition.bind(this);
  120. this._enableStandardDefinition
  121. = this._enableStandardDefinition.bind(this);
  122. this._enableUltraHighDefinition = this._enableUltraHighDefinition.bind(this);
  123. this._onSliderChange = this._onSliderChange.bind(this);
  124. /**
  125. * An array of configuration options for displaying a choice in the
  126. * input. The onSelect callback will be invoked when the option is
  127. * selected and videoQuality helps determine which choice matches with
  128. * the currently active quality level.
  129. *
  130. * @private
  131. * @type {Object[]}
  132. */
  133. this._sliderOptions = [
  134. {
  135. audioOnly: true,
  136. onSelect: this._enableAudioOnly,
  137. textKey: 'audioOnly.audioOnly'
  138. },
  139. {
  140. onSelect: this._enableLowDefinition,
  141. textKey: 'videoStatus.lowDefinition',
  142. videoQuality: LOW
  143. },
  144. {
  145. onSelect: this._enableStandardDefinition,
  146. textKey: 'videoStatus.standardDefinition',
  147. videoQuality: STANDARD
  148. },
  149. {
  150. onSelect: this._enableUltraHighDefinition,
  151. textKey: 'videoStatus.highDefinition',
  152. videoQuality: ULTRA
  153. }
  154. ];
  155. }
  156. /**
  157. * Implements React's {@link Component#render()}.
  158. *
  159. * @inheritdoc
  160. * @returns {ReactElement}
  161. */
  162. render() {
  163. const { classes, t } = this.props;
  164. const activeSliderOption = this._mapCurrentQualityToSliderValue();
  165. return (
  166. <div className = { clsx('video-quality-dialog', classes.dialog) }>
  167. <div className = { classes.dialogDetails }>{t('videoStatus.adjustFor')}</div>
  168. <div className = { classes.dialogContents }>
  169. <div className = { classes.sliderDescription }>
  170. <span>{t('videoStatus.bestPerformance')}</span>
  171. <span>{t('videoStatus.highestQuality')}</span>
  172. </div>
  173. <Slider
  174. ariaLabel = { t('videoStatus.callQuality') }
  175. max = { this._sliderOptions.length - 1 }
  176. min = { 0 }
  177. onChange = { this._onSliderChange }
  178. step = { 1 }
  179. value = { activeSliderOption } />
  180. </div>
  181. </div>
  182. );
  183. }
  184. /**
  185. * Dispatches an action to enable audio only mode.
  186. *
  187. * @private
  188. * @returns {void}
  189. */
  190. _enableAudioOnly() {
  191. sendAnalytics(createEvent('audio.only'));
  192. logger.log('Video quality: audio only enabled');
  193. this.props.dispatch(setAudioOnly(true));
  194. }
  195. /**
  196. * Handles the action of the high definition video being selected.
  197. * Dispatches an action to receive high quality video from remote
  198. * participants.
  199. *
  200. * @private
  201. * @returns {void}
  202. */
  203. _enableHighDefinition() {
  204. sendAnalytics(createEvent('high'));
  205. logger.log('Video quality: high enabled');
  206. this._setPreferredVideoQuality(HIGH);
  207. }
  208. /**
  209. * Dispatches an action to receive low quality video from remote
  210. * participants.
  211. *
  212. * @private
  213. * @returns {void}
  214. */
  215. _enableLowDefinition() {
  216. sendAnalytics(createEvent('low'));
  217. logger.log('Video quality: low enabled');
  218. this._setPreferredVideoQuality(LOW);
  219. }
  220. /**
  221. * Dispatches an action to receive standard quality video from remote
  222. * participants.
  223. *
  224. * @private
  225. * @returns {void}
  226. */
  227. _enableStandardDefinition() {
  228. sendAnalytics(createEvent('standard'));
  229. logger.log('Video quality: standard enabled');
  230. this._setPreferredVideoQuality(STANDARD);
  231. }
  232. /**
  233. * Dispatches an action to receive ultra HD quality video from remote
  234. * participants.
  235. *
  236. * @private
  237. * @returns {void}
  238. */
  239. _enableUltraHighDefinition() {
  240. sendAnalytics(createEvent('ultra high'));
  241. logger.log('Video quality: ultra high enabled');
  242. this._setPreferredVideoQuality(ULTRA);
  243. }
  244. /**
  245. * Matches the current video quality state with corresponding index of the
  246. * component's slider options.
  247. *
  248. * @private
  249. * @returns {void}
  250. */
  251. _mapCurrentQualityToSliderValue() {
  252. const { _audioOnly, _sendrecvVideoQuality } = this.props;
  253. const { _sliderOptions } = this;
  254. if (_audioOnly) {
  255. const audioOnlyOption = _sliderOptions.find(
  256. ({ audioOnly }: any) => audioOnly);
  257. // @ts-ignore
  258. return _sliderOptions.indexOf(audioOnlyOption);
  259. }
  260. for (let i = 0; i < _sliderOptions.length; i++) {
  261. // @ts-ignore
  262. if (_sliderOptions[i].videoQuality >= _sendrecvVideoQuality) {
  263. return i;
  264. }
  265. }
  266. return -1;
  267. }
  268. /**
  269. * Invokes a callback when the selected video quality changes.
  270. *
  271. * @param {Object} event - The slider's change event.
  272. * @private
  273. * @returns {void}
  274. */
  275. _onSliderChange(event: React.ChangeEvent<HTMLInputElement>) {
  276. const { _audioOnly, _sendrecvVideoQuality } = this.props;
  277. const {
  278. // @ts-ignore
  279. audioOnly,
  280. // @ts-ignore
  281. onSelect,
  282. // @ts-ignore
  283. videoQuality
  284. } = this._sliderOptions[event.target.value as keyof typeof this._sliderOptions];
  285. // Take no action if the newly chosen option does not change audio only
  286. // or video quality state.
  287. if ((_audioOnly && audioOnly)
  288. || (!_audioOnly && videoQuality === _sendrecvVideoQuality)) {
  289. return;
  290. }
  291. onSelect();
  292. }
  293. /**
  294. * Helper for changing the preferred maximum video quality to receive and
  295. * disable audio only.
  296. *
  297. * @param {number} qualityLevel - The new maximum video quality. Should be
  298. * a value enumerated in {@code VIDEO_QUALITY_LEVELS}.
  299. * @private
  300. * @returns {void}
  301. */
  302. _setPreferredVideoQuality(qualityLevel: number) {
  303. this.props.dispatch(setPreferredVideoQuality(qualityLevel));
  304. if (this.props._audioOnly) {
  305. this.props.dispatch(setAudioOnly(false));
  306. }
  307. // Determine the lastN value based on the quality setting.
  308. let { _channelLastN = DEFAULT_LAST_N } = this.props;
  309. _channelLastN = _channelLastN === -1 ? DEFAULT_LAST_N : _channelLastN;
  310. const lastN = getLastNForQualityLevel(qualityLevel, _channelLastN);
  311. // Set the lastN for the conference.
  312. this.props.dispatch(setLastN(lastN));
  313. }
  314. }
  315. /**
  316. * Maps (parts of) the Redux state to the associated props for the
  317. * {@code VideoQualitySlider} component.
  318. *
  319. * @param {Object} state - The Redux state.
  320. * @private
  321. * @returns {IProps}
  322. */
  323. function _mapStateToProps(state: IReduxState) {
  324. const { enabled: audioOnly } = state['features/base/audio-only'];
  325. const { p2p } = state['features/base/conference'];
  326. const { preferredVideoQuality } = state['features/video-quality'];
  327. const { channelLastN } = state['features/base/config'];
  328. return {
  329. _audioOnly: audioOnly,
  330. _channelLastN: channelLastN,
  331. _p2p: p2p,
  332. _sendrecvVideoQuality: preferredVideoQuality
  333. };
  334. }
  335. export default translate(connect(_mapStateToProps)(withStyles(styles)(VideoQualitySlider)));