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

VideoQualitySlider.web.tsx 11KB

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