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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // @flow
  2. import {
  3. ACTION_SHORTCUT_TRIGGERED,
  4. VIDEO_MUTE,
  5. createShortcutEvent,
  6. sendAnalytics
  7. } from '../../analytics';
  8. import { VIDEO_MUTE_BUTTON_ENABLED, getFeatureFlag } from '../../base/flags';
  9. import { translate } from '../../base/i18n';
  10. import { MEDIA_TYPE } from '../../base/media';
  11. import { connect } from '../../base/redux';
  12. import { AbstractButton, AbstractVideoMuteButton } from '../../base/toolbox/components';
  13. import type { AbstractButtonProps } from '../../base/toolbox/components';
  14. import { isLocalTrackMuted } from '../../base/tracks';
  15. import { handleToggleVideoMuted } from '../actions.any';
  16. import { isVideoMuteButtonDisabled } from '../functions';
  17. declare var APP: Object;
  18. /**
  19. * The type of the React {@code Component} props of {@link VideoMuteButton}.
  20. */
  21. type Props = AbstractButtonProps & {
  22. /**
  23. * Whether video is currently muted or not.
  24. */
  25. _videoMuted: boolean,
  26. /**
  27. * Whether video button is disabled or not.
  28. */
  29. _videoDisabled: boolean,
  30. /**
  31. * The redux {@code dispatch} function.
  32. */
  33. dispatch: Function
  34. }
  35. /**
  36. * Component that renders a toolbar button for toggling video mute.
  37. *
  38. * @augments AbstractVideoMuteButton
  39. */
  40. class VideoMuteButton extends AbstractVideoMuteButton<Props, *> {
  41. accessibilityLabel = 'toolbar.accessibilityLabel.videomute';
  42. label = 'toolbar.videomute';
  43. tooltip = 'toolbar.videomute';
  44. /**
  45. * Initializes a new {@code VideoMuteButton} instance.
  46. *
  47. * @param {Props} props - The read-only React {@code Component} props with
  48. * which the new instance is to be initialized.
  49. */
  50. constructor(props: Props) {
  51. super(props);
  52. // Bind event handlers so they are only bound once per instance.
  53. this._onKeyboardShortcut = this._onKeyboardShortcut.bind(this);
  54. }
  55. /**
  56. * Registers the keyboard shortcut that toggles the video muting.
  57. *
  58. * @inheritdoc
  59. * @returns {void}
  60. */
  61. componentDidMount() {
  62. typeof APP === 'undefined'
  63. || APP.keyboardshortcut.registerShortcut(
  64. 'V',
  65. null,
  66. this._onKeyboardShortcut,
  67. 'keyboardShortcuts.videoMute');
  68. }
  69. /**
  70. * Unregisters the keyboard shortcut that toggles the video muting.
  71. *
  72. * @inheritdoc
  73. * @returns {void}
  74. */
  75. componentWillUnmount() {
  76. typeof APP === 'undefined'
  77. || APP.keyboardshortcut.unregisterShortcut('V');
  78. }
  79. /**
  80. * Indicates if video is currently disabled or not.
  81. *
  82. * @override
  83. * @protected
  84. * @returns {boolean}
  85. */
  86. _isDisabled() {
  87. return this.props._videoDisabled;
  88. }
  89. /**
  90. * Indicates if video is currently muted or not.
  91. *
  92. * @override
  93. * @protected
  94. * @returns {boolean}
  95. */
  96. _isVideoMuted() {
  97. return this.props._videoMuted;
  98. }
  99. _onKeyboardShortcut: () => void;
  100. /**
  101. * Creates an analytics keyboard shortcut event and dispatches an action to
  102. * toggle the video muting.
  103. *
  104. * @private
  105. * @returns {void}
  106. */
  107. _onKeyboardShortcut() {
  108. // Ignore keyboard shortcuts if the video button is disabled.
  109. if (this._isDisabled()) {
  110. return;
  111. }
  112. sendAnalytics(
  113. createShortcutEvent(
  114. VIDEO_MUTE,
  115. ACTION_SHORTCUT_TRIGGERED,
  116. { enable: !this._isVideoMuted() }));
  117. AbstractButton.prototype._onClick.call(this);
  118. }
  119. /**
  120. * Changes the muted state.
  121. *
  122. * @override
  123. * @param {boolean} videoMuted - Whether video should be muted or not.
  124. * @protected
  125. * @returns {void}
  126. */
  127. _setVideoMuted(videoMuted: boolean) {
  128. this.props.dispatch(handleToggleVideoMuted(videoMuted, true, true));
  129. }
  130. }
  131. /**
  132. * Maps (parts of) the redux state to the associated props for the
  133. * {@code VideoMuteButton} component.
  134. *
  135. * @param {Object} state - The Redux state.
  136. * @private
  137. * @returns {{
  138. * _videoMuted: boolean
  139. * }}
  140. */
  141. function _mapStateToProps(state): Object {
  142. const tracks = state['features/base/tracks'];
  143. const enabledFlag = getFeatureFlag(state, VIDEO_MUTE_BUTTON_ENABLED, true);
  144. return {
  145. _videoDisabled: isVideoMuteButtonDisabled(state),
  146. _videoMuted: isLocalTrackMuted(tracks, MEDIA_TYPE.VIDEO),
  147. visible: enabledFlag
  148. };
  149. }
  150. export default translate(connect(_mapStateToProps)(VideoMuteButton));