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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // @flow
  2. import { AbstractAudioMuteButton } from '../base/toolbox';
  3. import type { AbstractButtonProps as Props } from '../base/toolbox';
  4. const { api } = window.alwaysOnTop;
  5. const logger = require('jitsi-meet-logger').getLogger(__filename);
  6. /**
  7. * The type of the React {@code Component} state of {@link AudioMuteButton}.
  8. */
  9. type State = {
  10. /**
  11. * Whether audio is available is not.
  12. */
  13. audioAvailable: boolean,
  14. /**
  15. * Whether audio is muted or not.
  16. */
  17. audioMuted: boolean
  18. };
  19. /**
  20. * Stateless "mute/unmute audio" button for the Always-on-Top windows.
  21. */
  22. export default class AudioMuteButton
  23. extends AbstractAudioMuteButton<Props, State> {
  24. accessibilityLabel = 'Audio mute';
  25. /**
  26. * Initializes a new {@code AudioMuteButton} instance.
  27. *
  28. * @param {Props} props - The React {@code Component} props to initialize
  29. * the new {@code AudioMuteButton} instance with.
  30. */
  31. constructor(props: Props) {
  32. super(props);
  33. this.state = {
  34. audioAvailable: false,
  35. audioMuted: true
  36. };
  37. // Bind event handlers so they are only bound once per instance.
  38. this._audioAvailabilityListener
  39. = this._audioAvailabilityListener.bind(this);
  40. this._audioMutedListener = this._audioMutedListener.bind(this);
  41. }
  42. /**
  43. * Sets mouse move listener and initial toolbar timeout.
  44. *
  45. * @inheritdoc
  46. * @returns {void}
  47. */
  48. componentDidMount() {
  49. api.on('audioAvailabilityChanged', this._audioAvailabilityListener);
  50. api.on('audioMuteStatusChanged', this._audioMutedListener);
  51. Promise.all([
  52. api.isAudioAvailable(),
  53. api.isAudioMuted()
  54. ])
  55. .then(([ audioAvailable, audioMuted ]) =>
  56. this.setState({
  57. audioAvailable,
  58. audioMuted
  59. }))
  60. .catch(logger.error);
  61. }
  62. /**
  63. * Removes all listeners.
  64. *
  65. * @inheritdoc
  66. * @returns {void}
  67. */
  68. componentWillUnmount() {
  69. api.removeListener(
  70. 'audioAvailabilityChanged',
  71. this._audioAvailabilityListener);
  72. api.removeListener(
  73. 'audioMuteStatusChanged',
  74. this._audioMutedListener);
  75. }
  76. _audioAvailabilityListener: ({ available: boolean }) => void;
  77. /**
  78. * Handles audio available api events.
  79. *
  80. * @param {{ available: boolean }} status - The new available status.
  81. * @returns {void}
  82. */
  83. _audioAvailabilityListener({ available }) {
  84. this.setState({ audioAvailable: available });
  85. }
  86. _audioMutedListener: ({ muted: boolean }) => void;
  87. /**
  88. * Handles audio muted api events.
  89. *
  90. * @param {{ muted: boolean }} status - The new muted status.
  91. * @returns {void}
  92. */
  93. _audioMutedListener({ muted }) {
  94. this.setState({ audioMuted: muted });
  95. }
  96. /**
  97. * Indicates if audio is currently muted ot nor.
  98. *
  99. * @override
  100. * @protected
  101. * @returns {boolean}
  102. */
  103. _isAudioMuted() {
  104. return this.state.audioMuted;
  105. }
  106. /**
  107. * Indicates whether this button is disabled or not.
  108. *
  109. * @override
  110. * @protected
  111. * @returns {boolean}
  112. */
  113. _isDisabled() {
  114. return !this.state.audioAvailable;
  115. }
  116. /**
  117. * Changes the muted state.
  118. *
  119. * @override
  120. * @param {boolean} audioMuted - Whether audio should be muted or not.
  121. * @protected
  122. * @returns {void}
  123. */
  124. _setAudioMuted(audioMuted: boolean) { // eslint-disable-line no-unused-vars
  125. this.state.audioAvailable && api.executeCommand('toggleAudio');
  126. }
  127. }