您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VideoQualitySlider.web.js 12KB

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