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

VideoMuteButton.tsx 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { ClassNameMap, withStyles } from '@mui/styles';
  2. import React, { ReactElement } from 'react';
  3. import { connect } from 'react-redux';
  4. import { ACTION_SHORTCUT_TRIGGERED, VIDEO_MUTE, createShortcutEvent } from '../../analytics/AnalyticsEvents';
  5. import { sendAnalytics } from '../../analytics/functions';
  6. import { IReduxState } from '../../app/types';
  7. import { VIDEO_MUTE_BUTTON_ENABLED } from '../../base/flags/constants';
  8. import { getFeatureFlag } from '../../base/flags/functions';
  9. import { translate } from '../../base/i18n/functions';
  10. import { MEDIA_TYPE } from '../../base/media/constants';
  11. import { IGUMPendingState } from '../../base/media/types';
  12. import AbstractButton, { IProps as AbstractButtonProps } from '../../base/toolbox/components/AbstractButton';
  13. import AbstractVideoMuteButton from '../../base/toolbox/components/AbstractVideoMuteButton';
  14. import { isLocalTrackMuted } from '../../base/tracks/functions';
  15. import Spinner from '../../base/ui/components/web/Spinner';
  16. import { registerShortcut, unregisterShortcut } from '../../keyboard-shortcuts/actions';
  17. import { handleToggleVideoMuted } from '../actions.any';
  18. import { SPINNER_COLOR } from '../constants';
  19. import { isVideoMuteButtonDisabled } from '../functions';
  20. const styles = () => {
  21. return {
  22. pendingContainer: {
  23. position: 'absolute' as const,
  24. bottom: '3px',
  25. right: '3px'
  26. }
  27. };
  28. };
  29. /**
  30. * The type of the React {@code Component} props of {@link VideoMuteButton}.
  31. */
  32. interface IProps extends AbstractButtonProps {
  33. /**
  34. * The gumPending state from redux.
  35. */
  36. _gumPending: IGUMPendingState;
  37. /**
  38. * Whether video button is disabled or not.
  39. */
  40. _videoDisabled: boolean;
  41. /**
  42. * Whether video is currently muted or not.
  43. */
  44. _videoMuted: boolean;
  45. /**
  46. * The @mui/styles classes.
  47. */
  48. classes: ClassNameMap<string>;
  49. }
  50. /**
  51. * Component that renders a toolbar button for toggling video mute.
  52. *
  53. * @augments AbstractVideoMuteButton
  54. */
  55. class VideoMuteButton extends AbstractVideoMuteButton<IProps> {
  56. accessibilityLabel = 'toolbar.accessibilityLabel.videomute';
  57. toggledAccessibilityLabel = 'toolbar.accessibilityLabel.videounmute';
  58. label = 'toolbar.videomute';
  59. tooltip = 'toolbar.videomute';
  60. toggledTooltip = 'toolbar.videounmute';
  61. /**
  62. * Initializes a new {@code VideoMuteButton} instance.
  63. *
  64. * @param {IProps} props - The read-only React {@code Component} props with
  65. * which the new instance is to be initialized.
  66. */
  67. constructor(props: IProps) {
  68. super(props);
  69. // Bind event handlers so they are only bound once per instance.
  70. this._onKeyboardShortcut = this._onKeyboardShortcut.bind(this);
  71. this._getTooltip = this._getLabel;
  72. }
  73. /**
  74. * Registers the keyboard shortcut that toggles the video muting.
  75. *
  76. * @inheritdoc
  77. * @returns {void}
  78. */
  79. componentDidMount() {
  80. if (typeof APP === 'undefined') {
  81. return;
  82. }
  83. this.props.dispatch(registerShortcut({
  84. character: 'V',
  85. helpDescription: 'keyboardShortcuts.videoMute',
  86. handler: this._onKeyboardShortcut
  87. }));
  88. }
  89. /**
  90. * Unregisters the keyboard shortcut that toggles the video muting.
  91. *
  92. * @inheritdoc
  93. * @returns {void}
  94. */
  95. componentWillUnmount() {
  96. if (typeof APP === 'undefined') {
  97. return;
  98. }
  99. this.props.dispatch(unregisterShortcut('V'));
  100. }
  101. /**
  102. * Gets the current accessibility label, taking the toggled and GUM pending state into account. If no toggled label
  103. * is provided, the regular accessibility label will also be used in the toggled state.
  104. *
  105. * The accessibility label is not visible in the UI, it is meant to be used by assistive technologies, mainly screen
  106. * readers.
  107. *
  108. * @private
  109. * @returns {string}
  110. */
  111. _getAccessibilityLabel() {
  112. const { _gumPending } = this.props;
  113. if (_gumPending === IGUMPendingState.NONE) {
  114. return super._getAccessibilityLabel();
  115. }
  116. return 'toolbar.accessibilityLabel.videomuteGUMPending';
  117. }
  118. /**
  119. * Gets the current label, taking the toggled and GUM pending state into account. If no
  120. * toggled label is provided, the regular label will also be used in the toggled state.
  121. *
  122. * @private
  123. * @returns {string}
  124. */
  125. _getLabel() {
  126. const { _gumPending } = this.props;
  127. if (_gumPending === IGUMPendingState.NONE) {
  128. return super._getLabel();
  129. }
  130. return 'toolbar.videomuteGUMPending';
  131. }
  132. /**
  133. * Indicates if video is currently disabled or not.
  134. *
  135. * @override
  136. * @protected
  137. * @returns {boolean}
  138. */
  139. _isDisabled() {
  140. return this.props._videoDisabled || this.props._gumPending !== IGUMPendingState.NONE;
  141. }
  142. /**
  143. * Indicates if video is currently muted or not.
  144. *
  145. * @override
  146. * @protected
  147. * @returns {boolean}
  148. */
  149. _isVideoMuted() {
  150. const { _gumPending, _videoMuted } = this.props;
  151. if (_gumPending === IGUMPendingState.PENDING_UNMUTE) {
  152. return false;
  153. }
  154. return _videoMuted;
  155. }
  156. /**
  157. * Returns a spinner if there is pending GUM.
  158. *
  159. * @returns {ReactElement | null}
  160. */
  161. _getElementAfter(): ReactElement | null {
  162. const { _gumPending, classes } = this.props;
  163. return _gumPending === IGUMPendingState.NONE ? null
  164. : (
  165. <div className = { classes.pendingContainer }>
  166. <Spinner
  167. color = { SPINNER_COLOR }
  168. size = 'small' />
  169. </div>
  170. );
  171. }
  172. /**
  173. * Creates an analytics keyboard shortcut event and dispatches an action to
  174. * toggle the video muting.
  175. *
  176. * @private
  177. * @returns {void}
  178. */
  179. _onKeyboardShortcut() {
  180. // Ignore keyboard shortcuts if the video button is disabled.
  181. if (this._isDisabled()) {
  182. return;
  183. }
  184. sendAnalytics(
  185. createShortcutEvent(
  186. VIDEO_MUTE,
  187. ACTION_SHORTCUT_TRIGGERED,
  188. { enable: !this._isVideoMuted() }));
  189. AbstractButton.prototype._onClick.call(this);
  190. }
  191. /**
  192. * Changes the muted state.
  193. *
  194. * @override
  195. * @param {boolean} videoMuted - Whether video should be muted or not.
  196. * @protected
  197. * @returns {void}
  198. */
  199. _setVideoMuted(videoMuted: boolean) {
  200. this.props.dispatch(handleToggleVideoMuted(videoMuted, true, true));
  201. }
  202. }
  203. /**
  204. * Maps (parts of) the redux state to the associated props for the
  205. * {@code VideoMuteButton} component.
  206. *
  207. * @param {Object} state - The Redux state.
  208. * @private
  209. * @returns {{
  210. * _videoMuted: boolean
  211. * }}
  212. */
  213. function _mapStateToProps(state: IReduxState) {
  214. const tracks = state['features/base/tracks'];
  215. const enabledFlag = getFeatureFlag(state, VIDEO_MUTE_BUTTON_ENABLED, true);
  216. const { gumPending } = state['features/base/media'].video;
  217. return {
  218. _videoDisabled: isVideoMuteButtonDisabled(state),
  219. _videoMuted: isLocalTrackMuted(tracks, MEDIA_TYPE.VIDEO),
  220. _gumPending: gumPending,
  221. visible: enabledFlag
  222. };
  223. }
  224. export default withStyles(styles)(translate(connect(_mapStateToProps)(VideoMuteButton)));