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

AudioMuteButton.tsx 7.2KB

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, AUDIO_MUTE, createShortcutEvent } from '../../analytics/AnalyticsEvents';
  5. import { sendAnalytics } from '../../analytics/functions';
  6. import { IReduxState } from '../../app/types';
  7. import { AUDIO_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 AbstractAudioMuteButton from '../../base/toolbox/components/AbstractAudioMuteButton';
  13. import AbstractButton, { IProps as AbstractButtonProps } from '../../base/toolbox/components/AbstractButton';
  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 { muteLocal } from '../../video-menu/actions';
  18. import { SPINNER_COLOR } from '../constants';
  19. import { isAudioMuteButtonDisabled } 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 AudioMuteButton}.
  31. */
  32. interface IProps extends AbstractButtonProps {
  33. /**
  34. * Whether audio is currently muted or not.
  35. */
  36. _audioMuted: boolean;
  37. /**
  38. * Whether the button is disabled.
  39. */
  40. _disabled: boolean;
  41. /**
  42. * The gumPending state from redux.
  43. */
  44. _gumPending: IGUMPendingState;
  45. /**
  46. * The @mui/styles classes.
  47. */
  48. classes: ClassNameMap<string>;
  49. }
  50. /**
  51. * Component that renders a toolbar button for toggling audio mute.
  52. *
  53. * @augments AbstractAudioMuteButton
  54. */
  55. class AudioMuteButton extends AbstractAudioMuteButton<IProps> {
  56. accessibilityLabel = 'toolbar.accessibilityLabel.mute';
  57. toggledAccessibilityLabel = 'toolbar.accessibilityLabel.unmute';
  58. label = 'toolbar.mute';
  59. tooltip = 'toolbar.mute';
  60. toggledTooltip = 'toolbar.unmute';
  61. /**
  62. * Initializes a new {@code AudioMuteButton} 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 audio 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: 'M',
  85. helpDescription: 'keyboardShortcuts.mute',
  86. handler: this._onKeyboardShortcut
  87. }));
  88. }
  89. /**
  90. * Unregisters the keyboard shortcut that toggles the audio muting.
  91. *
  92. * @inheritdoc
  93. * @returns {void}
  94. */
  95. componentWillUnmount() {
  96. if (typeof APP === 'undefined') {
  97. return;
  98. }
  99. this.props.dispatch(unregisterShortcut('M'));
  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.muteGUMPending';
  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.muteGUMPending';
  131. }
  132. /**
  133. * Indicates if audio is currently muted or not.
  134. *
  135. * @override
  136. * @protected
  137. * @returns {boolean}
  138. */
  139. _isAudioMuted() {
  140. const { _audioMuted, _gumPending } = this.props;
  141. if (_gumPending === IGUMPendingState.PENDING_UNMUTE) {
  142. return false;
  143. }
  144. return _audioMuted;
  145. }
  146. /**
  147. * Creates an analytics keyboard shortcut event and dispatches an action to
  148. * toggle the audio muting.
  149. *
  150. * @private
  151. * @returns {void}
  152. */
  153. _onKeyboardShortcut() {
  154. // Ignore keyboard shortcuts if the audio button is disabled.
  155. if (this._isDisabled()) {
  156. return;
  157. }
  158. sendAnalytics(
  159. createShortcutEvent(
  160. AUDIO_MUTE,
  161. ACTION_SHORTCUT_TRIGGERED,
  162. { enable: !this._isAudioMuted() }));
  163. AbstractButton.prototype._onClick.call(this);
  164. }
  165. /**
  166. * Returns a spinner if there is pending GUM.
  167. *
  168. * @returns {ReactElement | null}
  169. */
  170. _getElementAfter(): ReactElement | null {
  171. const { _gumPending, classes } = this.props;
  172. return _gumPending === IGUMPendingState.NONE ? null
  173. : (
  174. <div className = { classes.pendingContainer }>
  175. <Spinner
  176. color = { SPINNER_COLOR }
  177. size = 'small' />
  178. </div>
  179. );
  180. }
  181. /**
  182. * Changes the muted state.
  183. *
  184. * @param {boolean} audioMuted - Whether audio should be muted or not.
  185. * @protected
  186. * @returns {void}
  187. */
  188. _setAudioMuted(audioMuted: boolean) {
  189. this.props.dispatch(muteLocal(audioMuted, MEDIA_TYPE.AUDIO));
  190. }
  191. /**
  192. * Return a boolean value indicating if this button is disabled or not.
  193. *
  194. * @returns {boolean}
  195. */
  196. _isDisabled() {
  197. return this.props._disabled;
  198. }
  199. }
  200. /**
  201. * Maps (parts of) the redux state to the associated props for the
  202. * {@code AudioMuteButton} component.
  203. *
  204. * @param {Object} state - The Redux state.
  205. * @private
  206. * @returns {{
  207. * _audioMuted: boolean,
  208. * _disabled: boolean
  209. * }}
  210. */
  211. function _mapStateToProps(state: IReduxState) {
  212. const _audioMuted = isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.AUDIO);
  213. const _disabled = isAudioMuteButtonDisabled(state);
  214. const enabledFlag = getFeatureFlag(state, AUDIO_MUTE_BUTTON_ENABLED, true);
  215. const { gumPending } = state['features/base/media'].audio;
  216. return {
  217. _audioMuted,
  218. _disabled,
  219. _gumPending: gumPending,
  220. visible: enabledFlag
  221. };
  222. }
  223. export default withStyles(styles)(translate(connect(_mapStateToProps)(AudioMuteButton)));