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.

VideoMuteButton.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // @flow
  2. import { AbstractVideoMuteButton } 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 VideoMuteButton}.
  8. */
  9. type State = {
  10. /**
  11. * Whether video is available is not.
  12. */
  13. videoAvailable: boolean,
  14. /**
  15. * Whether video is muted or not.
  16. */
  17. videoMuted: boolean
  18. };
  19. /**
  20. * Stateless "mute/unmute video" button for the Always-on-Top windows.
  21. */
  22. export default class VideoMuteButton
  23. extends AbstractVideoMuteButton<Props, State> {
  24. accessibilityLabel = 'Video mute';
  25. /**
  26. * Initializes a new {@code VideoMuteButton} instance.
  27. *
  28. * @param {Props} props - The React {@code Component} props to initialize
  29. * the new {@code VideoMuteButton} instance with.
  30. */
  31. constructor(props: Props) {
  32. super(props);
  33. this.state = {
  34. videoAvailable: false,
  35. videoMuted: true
  36. };
  37. // Bind event handlers so they are only bound once per instance.
  38. this._videoAvailabilityListener
  39. = this._videoAvailabilityListener.bind(this);
  40. this._videoMutedListener = this._videoMutedListener.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('videoAvailabilityChanged', this._videoAvailabilityListener);
  50. api.on('videoMuteStatusChanged', this._videoMutedListener);
  51. Promise.all([
  52. api.isVideoAvailable(),
  53. api.isVideoMuted()
  54. ])
  55. .then(([ videoAvailable, videoMuted ]) =>
  56. this.setState({
  57. videoAvailable,
  58. videoMuted
  59. }))
  60. .catch(logger.error);
  61. }
  62. /**
  63. * Removes all listeners.
  64. *
  65. * @inheritdoc
  66. * @returns {void}
  67. */
  68. componentWillUnmount() {
  69. api.removeListener(
  70. 'videoAvailabilityChanged',
  71. this._videoAvailabilityListener);
  72. api.removeListener(
  73. 'videoMuteStatusChanged',
  74. this._videoMutedListener);
  75. }
  76. /**
  77. * Indicates whether this button is disabled or not.
  78. *
  79. * @override
  80. * @protected
  81. * @returns {boolean}
  82. */
  83. _isDisabled() {
  84. return !this.state.videoAvailable;
  85. }
  86. /**
  87. * Indicates if video is currently muted ot nor.
  88. *
  89. * @override
  90. * @protected
  91. * @returns {boolean}
  92. */
  93. _isVideoMuted() {
  94. return this.state.videoMuted;
  95. }
  96. /**
  97. * Changes the muted state.
  98. *
  99. * @override
  100. * @param {boolean} videoMuted - Whether video should be muted or not.
  101. * @protected
  102. * @returns {void}
  103. */
  104. _setVideoMuted(videoMuted: boolean) { // eslint-disable-line no-unused-vars
  105. this.state.videoAvailable && api.executeCommand('toggleVideo');
  106. }
  107. _videoAvailabilityListener: ({ available: boolean }) => void;
  108. /**
  109. * Handles video available api events.
  110. *
  111. * @param {{ available: boolean }} status - The new available status.
  112. * @returns {void}
  113. */
  114. _videoAvailabilityListener({ available }) {
  115. this.setState({ videoAvailable: available });
  116. }
  117. _videoMutedListener: ({ muted: boolean }) => void;
  118. /**
  119. * Handles video muted api events.
  120. *
  121. * @param {{ muted: boolean }} status - The new muted status.
  122. * @returns {void}
  123. */
  124. _videoMutedListener({ muted }) {
  125. this.setState({ videoMuted: muted });
  126. }
  127. }