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

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