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

VideoQualitySlider.web.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. import InlineMessage from '@atlaskit/inline-message';
  2. import PropTypes from 'prop-types';
  3. import React, { Component } from 'react';
  4. import { connect } from 'react-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. const logger = require('jitsi-meet-logger').getLogger(__filename);
  17. const {
  18. HIGH,
  19. STANDARD,
  20. LOW
  21. } = VIDEO_QUALITY_LEVELS;
  22. /**
  23. * Creates an analytics event for a press of one of the buttons in the video
  24. * quality dialog.
  25. *
  26. * @param {string} quality - The quality which was selected.
  27. * @returns {Object} The event in a format suitable for sending via
  28. * sendAnalytics.
  29. */
  30. const createEvent = function(quality) {
  31. return createToolbarEvent(
  32. 'video.quality',
  33. {
  34. quality
  35. });
  36. };
  37. /**
  38. * Implements a React {@link Component} which displays a slider for selecting a
  39. * new receive video quality.
  40. *
  41. * @extends Component
  42. */
  43. class VideoQualitySlider extends Component {
  44. /**
  45. * {@code VideoQualitySlider}'s property types.
  46. *
  47. * @static
  48. */
  49. static propTypes = {
  50. /**
  51. * Whether or not the conference is in audio only mode.
  52. */
  53. _audioOnly: PropTypes.bool,
  54. /**
  55. * Whether or not the conference is in peer to peer mode.
  56. */
  57. _p2p: PropTypes.bool,
  58. /**
  59. * The currently configured maximum quality resolution to be received
  60. * from remote participants.
  61. */
  62. _receiverVideoQuality: PropTypes.number,
  63. /**
  64. * Whether or not displaying video is supported in the current
  65. * environment. If false, the slider will be disabled.
  66. */
  67. _videoSupported: PropTypes.bool,
  68. /**
  69. * Invoked to request toggling of audio only mode.
  70. */
  71. dispatch: PropTypes.func,
  72. /**
  73. * Invoked to obtain translated strings.
  74. */
  75. t: PropTypes.func
  76. };
  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. /**
  238. * Dispatches an action to enable audio only mode.
  239. *
  240. * @private
  241. * @returns {void}
  242. */
  243. _enableAudioOnly() {
  244. sendAnalytics(createEvent('audio.only'));
  245. logger.log('Video quality: audio only enabled');
  246. this.props.dispatch(setAudioOnly(true));
  247. }
  248. /**
  249. * Handles the action of the high definition video being selected.
  250. * Dispatches an action to receive high quality video from remote
  251. * participants.
  252. *
  253. * @private
  254. * @returns {void}
  255. */
  256. _enableHighDefinition() {
  257. sendAnalytics(createEvent('high'));
  258. logger.log('Video quality: high enabled');
  259. this.props.dispatch(setPreferredReceiverVideoQuality(HIGH));
  260. }
  261. /**
  262. * Dispatches an action to receive low quality video from remote
  263. * participants.
  264. *
  265. * @private
  266. * @returns {void}
  267. */
  268. _enableLowDefinition() {
  269. sendAnalytics(createEvent('low'));
  270. logger.log('Video quality: low enabled');
  271. this.props.dispatch(setPreferredReceiverVideoQuality(LOW));
  272. }
  273. /**
  274. * Dispatches an action to receive standard quality video from remote
  275. * participants.
  276. *
  277. * @private
  278. * @returns {void}
  279. */
  280. _enableStandardDefinition() {
  281. sendAnalytics(createEvent('standard'));
  282. logger.log('Video quality: standard enabled');
  283. this.props.dispatch(setPreferredReceiverVideoQuality(STANDARD));
  284. }
  285. /**
  286. * Matches the current video quality state with corresponding index of the
  287. * component's slider options.
  288. *
  289. * @private
  290. * @returns {void}
  291. */
  292. _mapCurrentQualityToSliderValue() {
  293. const { _audioOnly, _receiverVideoQuality } = this.props;
  294. const { _sliderOptions } = this;
  295. if (_audioOnly) {
  296. const audioOnlyOption = _sliderOptions.find(
  297. ({ audioOnly }) => audioOnly);
  298. return _sliderOptions.indexOf(audioOnlyOption);
  299. }
  300. const matchingOption = _sliderOptions.find(
  301. ({ videoQuality }) => videoQuality === _receiverVideoQuality);
  302. return _sliderOptions.indexOf(matchingOption);
  303. }
  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, _receiverVideoQuality } = 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 === _receiverVideoQuality)) {
  322. return;
  323. }
  324. onSelect();
  325. }
  326. }
  327. /**
  328. * Maps (parts of) the Redux state to the associated props for the
  329. * {@code VideoQualitySlider} component.
  330. *
  331. * @param {Object} state - The Redux state.
  332. * @private
  333. * @returns {{
  334. * _audioOnly: boolean,
  335. * _p2p: boolean,
  336. * _receiverVideoQuality: boolean
  337. * }}
  338. */
  339. function _mapStateToProps(state) {
  340. const {
  341. audioOnly,
  342. p2p,
  343. preferredReceiverVideoQuality
  344. } = state['features/base/conference'];
  345. return {
  346. _audioOnly: audioOnly,
  347. _p2p: p2p,
  348. _receiverVideoQuality: preferredReceiverVideoQuality,
  349. _videoSupported: JitsiMeetJS.mediaDevices.supportsVideo()
  350. };
  351. }
  352. export default translate(connect(_mapStateToProps)(VideoQualitySlider));