選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

VideoQualitySlider.web.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. 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: Boolean;
  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<Object>;
  106. /**
  107. * Initializes a new {@code VideoQualitySlider} instance.
  108. *
  109. * @param {Object} props - The read-only React Component props with which
  110. * the new instance is to be initialized.
  111. */
  112. constructor(props: IProps) {
  113. super(props);
  114. // Bind event handlers so they are only bound once for every instance.
  115. this._enableAudioOnly = this._enableAudioOnly.bind(this);
  116. this._enableHighDefinition = this._enableHighDefinition.bind(this);
  117. this._enableLowDefinition = this._enableLowDefinition.bind(this);
  118. this._enableStandardDefinition
  119. = this._enableStandardDefinition.bind(this);
  120. this._enableUltraHighDefinition = this._enableUltraHighDefinition.bind(this);
  121. this._onSliderChange = this._onSliderChange.bind(this);
  122. /**
  123. * An array of configuration options for displaying a choice in the
  124. * input. The onSelect callback will be invoked when the option is
  125. * selected and videoQuality helps determine which choice matches with
  126. * the currently active quality level.
  127. *
  128. * @private
  129. * @type {Object[]}
  130. */
  131. this._sliderOptions = [
  132. {
  133. audioOnly: true,
  134. onSelect: this._enableAudioOnly,
  135. textKey: 'audioOnly.audioOnly'
  136. },
  137. {
  138. onSelect: this._enableLowDefinition,
  139. textKey: 'videoStatus.lowDefinition',
  140. videoQuality: LOW
  141. },
  142. {
  143. onSelect: this._enableStandardDefinition,
  144. textKey: 'videoStatus.standardDefinition',
  145. videoQuality: STANDARD
  146. },
  147. {
  148. onSelect: this._enableUltraHighDefinition,
  149. textKey: 'videoStatus.highDefinition',
  150. videoQuality: ULTRA
  151. }
  152. ];
  153. }
  154. /**
  155. * Implements React's {@link Component#render()}.
  156. *
  157. * @inheritdoc
  158. * @returns {ReactElement}
  159. */
  160. render() {
  161. const { classes, t } = this.props;
  162. const activeSliderOption = this._mapCurrentQualityToSliderValue();
  163. return (
  164. <div className = { clsx('video-quality-dialog', classes.dialog) }>
  165. <div className = { classes.dialogDetails }>{t('videoStatus.adjustFor')}</div>
  166. <div className = { classes.dialogContents }>
  167. <div className = { classes.sliderDescription }>
  168. <span>{t('videoStatus.bestPerformance')}</span>
  169. <span>{t('videoStatus.highestQuality')}</span>
  170. </div>
  171. <Slider
  172. ariaLabel = { t('videoStatus.callQuality') }
  173. max = { this._sliderOptions.length - 1 }
  174. min = { 0 }
  175. onChange = { this._onSliderChange }
  176. step = { 1 }
  177. value = { activeSliderOption } />
  178. </div>
  179. </div>
  180. );
  181. }
  182. /**
  183. * Dispatches an action to enable audio only mode.
  184. *
  185. * @private
  186. * @returns {void}
  187. */
  188. _enableAudioOnly() {
  189. sendAnalytics(createEvent('audio.only'));
  190. logger.log('Video quality: audio only enabled');
  191. this.props.dispatch(setAudioOnly(true));
  192. }
  193. /**
  194. * Handles the action of the high definition video being selected.
  195. * Dispatches an action to receive high quality video from remote
  196. * participants.
  197. *
  198. * @private
  199. * @returns {void}
  200. */
  201. _enableHighDefinition() {
  202. sendAnalytics(createEvent('high'));
  203. logger.log('Video quality: high enabled');
  204. this._setPreferredVideoQuality(HIGH);
  205. }
  206. /**
  207. * Dispatches an action to receive low quality video from remote
  208. * participants.
  209. *
  210. * @private
  211. * @returns {void}
  212. */
  213. _enableLowDefinition() {
  214. sendAnalytics(createEvent('low'));
  215. logger.log('Video quality: low enabled');
  216. this._setPreferredVideoQuality(LOW);
  217. }
  218. /**
  219. * Dispatches an action to receive standard quality video from remote
  220. * participants.
  221. *
  222. * @private
  223. * @returns {void}
  224. */
  225. _enableStandardDefinition() {
  226. sendAnalytics(createEvent('standard'));
  227. logger.log('Video quality: standard enabled');
  228. this._setPreferredVideoQuality(STANDARD);
  229. }
  230. /**
  231. * Dispatches an action to receive ultra HD quality video from remote
  232. * participants.
  233. *
  234. * @private
  235. * @returns {void}
  236. */
  237. _enableUltraHighDefinition() {
  238. sendAnalytics(createEvent('ultra high'));
  239. logger.log('Video quality: ultra high enabled');
  240. this._setPreferredVideoQuality(ULTRA);
  241. }
  242. /**
  243. * Matches the current video quality state with corresponding index of the
  244. * component's slider options.
  245. *
  246. * @private
  247. * @returns {void}
  248. */
  249. _mapCurrentQualityToSliderValue() {
  250. const { _audioOnly, _sendrecvVideoQuality } = this.props;
  251. const { _sliderOptions } = this;
  252. if (_audioOnly) {
  253. const audioOnlyOption = _sliderOptions.find(
  254. ({ audioOnly }: any) => audioOnly);
  255. // @ts-ignore
  256. return _sliderOptions.indexOf(audioOnlyOption);
  257. }
  258. for (let i = 0; i < _sliderOptions.length; i++) {
  259. // @ts-ignore
  260. if (_sliderOptions[i].videoQuality >= _sendrecvVideoQuality) {
  261. return i;
  262. }
  263. }
  264. return -1;
  265. }
  266. /**
  267. * Invokes a callback when the selected video quality changes.
  268. *
  269. * @param {Object} event - The slider's change event.
  270. * @private
  271. * @returns {void}
  272. */
  273. _onSliderChange(event: React.ChangeEvent<HTMLInputElement>) {
  274. const { _audioOnly, _sendrecvVideoQuality } = this.props;
  275. const {
  276. // @ts-ignore
  277. audioOnly,
  278. // @ts-ignore
  279. onSelect,
  280. // @ts-ignore
  281. videoQuality
  282. } = this._sliderOptions[event.target.value as keyof typeof this._sliderOptions];
  283. // Take no action if the newly chosen option does not change audio only
  284. // or video quality state.
  285. if ((_audioOnly && audioOnly)
  286. || (!_audioOnly && videoQuality === _sendrecvVideoQuality)) {
  287. return;
  288. }
  289. onSelect();
  290. }
  291. /**
  292. * Helper for changing the preferred maximum video quality to receive and
  293. * disable audio only.
  294. *
  295. * @param {number} qualityLevel - The new maximum video quality. Should be
  296. * a value enumerated in {@code VIDEO_QUALITY_LEVELS}.
  297. * @private
  298. * @returns {void}
  299. */
  300. _setPreferredVideoQuality(qualityLevel: number) {
  301. this.props.dispatch(setPreferredVideoQuality(qualityLevel));
  302. if (this.props._audioOnly) {
  303. this.props.dispatch(setAudioOnly(false));
  304. }
  305. // Determine the lastN value based on the quality setting.
  306. let { _channelLastN = DEFAULT_LAST_N } = this.props;
  307. _channelLastN = _channelLastN === -1 ? DEFAULT_LAST_N : _channelLastN;
  308. const lastN = getLastNForQualityLevel(qualityLevel, _channelLastN);
  309. // Set the lastN for the conference.
  310. this.props.dispatch(setLastN(lastN));
  311. }
  312. }
  313. /**
  314. * Maps (parts of) the Redux state to the associated props for the
  315. * {@code VideoQualitySlider} component.
  316. *
  317. * @param {Object} state - The Redux state.
  318. * @private
  319. * @returns {IProps}
  320. */
  321. function _mapStateToProps(state: IReduxState) {
  322. const { enabled: audioOnly } = state['features/base/audio-only'];
  323. const { p2p } = state['features/base/conference'];
  324. const { preferredVideoQuality } = state['features/video-quality'];
  325. const { channelLastN } = state['features/base/config'];
  326. return {
  327. _audioOnly: audioOnly,
  328. _channelLastN: channelLastN,
  329. _p2p: p2p,
  330. _sendrecvVideoQuality: preferredVideoQuality
  331. };
  332. }
  333. export default translate(connect(_mapStateToProps)(withStyles(styles)(VideoQualitySlider)));