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

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