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.1KB

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