You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AudioMuteButton.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // @flow
  2. import {
  3. ACTION_SHORTCUT_TRIGGERED,
  4. AUDIO_MUTE,
  5. createShortcutEvent,
  6. sendAnalytics
  7. } from '../../analytics';
  8. import { getFeatureFlag, AUDIO_MUTE_BUTTON_ENABLED } from '../../base/flags';
  9. import { translate } from '../../base/i18n';
  10. import { MEDIA_TYPE } from '../../base/media';
  11. import { connect } from '../../base/redux';
  12. import { AbstractAudioMuteButton } from '../../base/toolbox/components';
  13. import type { AbstractButtonProps } from '../../base/toolbox/components';
  14. import { isLocalTrackMuted } from '../../base/tracks';
  15. import { muteLocal } from '../../video-menu/actions';
  16. import { isAudioMuteButtonDisabled } from '../functions';
  17. declare var APP: Object;
  18. /**
  19. * The type of the React {@code Component} props of {@link AudioMuteButton}.
  20. */
  21. type Props = AbstractButtonProps & {
  22. /**
  23. * Whether audio is currently muted or not.
  24. */
  25. _audioMuted: boolean,
  26. /**
  27. * Whether the button is disabled.
  28. */
  29. _disabled: boolean,
  30. /**
  31. * The redux {@code dispatch} function.
  32. */
  33. dispatch: Function
  34. }
  35. /**
  36. * Component that renders a toolbar button for toggling audio mute.
  37. *
  38. * @augments AbstractAudioMuteButton
  39. */
  40. class AudioMuteButton extends AbstractAudioMuteButton<Props, *> {
  41. accessibilityLabel = 'toolbar.accessibilityLabel.mute';
  42. label = 'toolbar.mute';
  43. tooltip = 'toolbar.mute';
  44. /**
  45. * Initializes a new {@code AudioMuteButton} instance.
  46. *
  47. * @param {Props} props - The read-only React {@code Component} props with
  48. * which the new instance is to be initialized.
  49. */
  50. constructor(props: Props) {
  51. super(props);
  52. // Bind event handlers so they are only bound once per instance.
  53. this._onKeyboardShortcut = this._onKeyboardShortcut.bind(this);
  54. }
  55. /**
  56. * Registers the keyboard shortcut that toggles the audio muting.
  57. *
  58. * @inheritdoc
  59. * @returns {void}
  60. */
  61. componentDidMount() {
  62. typeof APP === 'undefined'
  63. || APP.keyboardshortcut.registerShortcut(
  64. 'M',
  65. null,
  66. this._onKeyboardShortcut,
  67. 'keyboardShortcuts.mute');
  68. }
  69. /**
  70. * Unregisters the keyboard shortcut that toggles the audio muting.
  71. *
  72. * @inheritdoc
  73. * @returns {void}
  74. */
  75. componentWillUnmount() {
  76. typeof APP === 'undefined'
  77. || APP.keyboardshortcut.unregisterShortcut('M');
  78. }
  79. /**
  80. * Indicates if audio is currently muted or not.
  81. *
  82. * @override
  83. * @protected
  84. * @returns {boolean}
  85. */
  86. _isAudioMuted() {
  87. return this.props._audioMuted;
  88. }
  89. _onKeyboardShortcut: () => void;
  90. /**
  91. * Creates an analytics keyboard shortcut event and dispatches an action to
  92. * toggle the audio muting.
  93. *
  94. * @private
  95. * @returns {void}
  96. */
  97. _onKeyboardShortcut() {
  98. sendAnalytics(
  99. createShortcutEvent(
  100. AUDIO_MUTE,
  101. ACTION_SHORTCUT_TRIGGERED,
  102. { enable: !this._isAudioMuted() }));
  103. super._handleClick();
  104. }
  105. /**
  106. * Changes the muted state.
  107. *
  108. * @param {boolean} audioMuted - Whether audio should be muted or not.
  109. * @protected
  110. * @returns {void}
  111. */
  112. _setAudioMuted(audioMuted: boolean) {
  113. this.props.dispatch(muteLocal(audioMuted, MEDIA_TYPE.AUDIO));
  114. }
  115. /**
  116. * Return a boolean value indicating if this button is disabled or not.
  117. *
  118. * @returns {boolean}
  119. */
  120. _isDisabled() {
  121. return this.props._disabled;
  122. }
  123. }
  124. /**
  125. * Maps (parts of) the redux state to the associated props for the
  126. * {@code AudioMuteButton} component.
  127. *
  128. * @param {Object} state - The Redux state.
  129. * @private
  130. * @returns {{
  131. * _audioMuted: boolean,
  132. * _disabled: boolean
  133. * }}
  134. */
  135. function _mapStateToProps(state): Object {
  136. const _audioMuted = isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.AUDIO);
  137. const _disabled = state['features/base/config'].startSilent || isAudioMuteButtonDisabled(state);
  138. const enabledFlag = getFeatureFlag(state, AUDIO_MUTE_BUTTON_ENABLED, true);
  139. return {
  140. _audioMuted,
  141. _disabled,
  142. visible: enabledFlag
  143. };
  144. }
  145. export default translate(connect(_mapStateToProps)(AudioMuteButton));