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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // @flow
  2. import { connect } from 'react-redux';
  3. import {
  4. ACTION_SHORTCUT_TRIGGERED,
  5. AUDIO_MUTE,
  6. createShortcutEvent,
  7. createToolbarEvent,
  8. sendAnalytics
  9. } from '../../analytics';
  10. import { translate } from '../../base/i18n';
  11. import { MEDIA_TYPE, setAudioMuted } from '../../base/media';
  12. import { AbstractAudioMuteButton } from '../../base/toolbox';
  13. import type { AbstractButtonProps } from '../../base/toolbox';
  14. import { isLocalTrackMuted } from '../../base/tracks';
  15. import UIEvents from '../../../../service/UI/UIEvents';
  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. * The redux {@code dispatch} function.
  27. */
  28. dispatch: Function
  29. }
  30. /**
  31. * Component that renders a toolbar button for toggling audio mute.
  32. *
  33. * @extends AbstractAudioMuteButton
  34. */
  35. class AudioMuteButton extends AbstractAudioMuteButton<Props, *> {
  36. accessibilityLabel = 'toolbar.accessibilityLabel.mute';
  37. label = 'toolbar.mute';
  38. tooltip = 'toolbar.mute';
  39. /**
  40. * Initializes a new {@code AudioMuteButton} instance.
  41. *
  42. * @param {Props} props - The read-only React {@code Component} props with
  43. * which the new instance is to be initialized.
  44. */
  45. constructor(props: Props) {
  46. super(props);
  47. // Bind event handlers so they are only bound once per instance.
  48. this._onKeyboardShortcut = this._onKeyboardShortcut.bind(this);
  49. }
  50. /**
  51. * Registers the keyboard shortcut that toggles the audio muting.
  52. *
  53. * @inheritdoc
  54. * @returns {void}
  55. */
  56. componentDidMount() {
  57. typeof APP === 'undefined'
  58. || APP.keyboardshortcut.registerShortcut(
  59. 'M',
  60. null,
  61. this._onKeyboardShortcut,
  62. 'keyboardShortcuts.mute');
  63. }
  64. /**
  65. * Unregisters the keyboard shortcut that toggles the audio muting.
  66. *
  67. * @inheritdoc
  68. * @returns {void}
  69. */
  70. componentWillUnmount() {
  71. typeof APP === 'undefined'
  72. || APP.keyboardshortcut.unregisterShortcut('M');
  73. }
  74. /**
  75. * Indicates if audio is currently muted ot nor.
  76. *
  77. * @override
  78. * @protected
  79. * @returns {boolean}
  80. */
  81. _isAudioMuted() {
  82. return this.props._audioMuted;
  83. }
  84. _onKeyboardShortcut: () => void;
  85. /**
  86. * Creates an analytics keyboard shortcut event and dispatches an action to
  87. * toggle the audio muting.
  88. *
  89. * @private
  90. * @returns {void}
  91. */
  92. _onKeyboardShortcut() {
  93. sendAnalytics(
  94. createShortcutEvent(
  95. AUDIO_MUTE,
  96. ACTION_SHORTCUT_TRIGGERED,
  97. { enable: !this._isAudioMuted() }));
  98. super._handleClick();
  99. }
  100. /**
  101. * Changes the muted state.
  102. *
  103. * @param {boolean} audioMuted - Whether audio should be muted or not.
  104. * @protected
  105. * @returns {void}
  106. */
  107. _setAudioMuted(audioMuted: boolean) {
  108. sendAnalytics(createToolbarEvent(AUDIO_MUTE, { enable: audioMuted }));
  109. this.props.dispatch(setAudioMuted(audioMuted, /* ensureTrack */ true));
  110. // FIXME: The old conference logic as well as the shared video feature
  111. // still rely on this event being emitted.
  112. typeof APP === 'undefined'
  113. || APP.UI.emitEvent(UIEvents.AUDIO_MUTED, audioMuted, true);
  114. }
  115. }
  116. /**
  117. * Maps (parts of) the redux state to the associated props for the
  118. * {@code AudioMuteButton} component.
  119. *
  120. * @param {Object} state - The Redux state.
  121. * @private
  122. * @returns {{
  123. * _audioMuted: boolean
  124. * }}
  125. */
  126. function _mapStateToProps(state): Object {
  127. const tracks = state['features/base/tracks'];
  128. return {
  129. _audioMuted: isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO)
  130. };
  131. }
  132. export default translate(connect(_mapStateToProps)(AudioMuteButton));