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

VideoQualitySlider.web.js 12KB

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