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

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