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.

AudioSettingsButton.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { IconArrowDown } from '../../../base/icons';
  4. import JitsiMeetJS from '../../../base/lib-jitsi-meet/_';
  5. import { connect } from '../../../base/redux';
  6. import { ToolboxButtonWithIcon } from '../../../base/toolbox';
  7. import { getMediaPermissionPromptVisibility } from '../../../overlay';
  8. import { AudioSettingsPopup, toggleAudioSettings } from '../../../settings';
  9. import { isAudioSettingsButtonDisabled } from '../../functions';
  10. import AudioMuteButton from '../AudioMuteButton';
  11. type Props = {
  12. /**
  13. * Click handler for the small icon. Opens audio options.
  14. */
  15. onAudioOptionsClick: Function,
  16. /**
  17. * Whether the permission prompt is visible or not.
  18. * Useful for enabling the button on permission grant.
  19. */
  20. permissionPromptVisibility: boolean,
  21. /**
  22. * If the button should be disabled.
  23. */
  24. isDisabled: boolean,
  25. /**
  26. * Flag controlling the visibility of the button.
  27. */
  28. visible: boolean,
  29. };
  30. type State = {
  31. /**
  32. * If there are permissions for audio devices.
  33. */
  34. hasPermissions: boolean,
  35. }
  36. /**
  37. * Button used for audio & audio settings.
  38. *
  39. * @returns {ReactElement}
  40. */
  41. class AudioSettingsButton extends Component<Props, State> {
  42. _isMounted: boolean;
  43. /**
  44. * Initializes a new {@code AudioSettingsButton} instance.
  45. *
  46. * @param {Object} props - The read-only properties with which the new
  47. * instance is to be initialized.
  48. */
  49. constructor(props) {
  50. super(props);
  51. this._isMounted = true;
  52. this.state = {
  53. hasPermissions: false
  54. };
  55. }
  56. /**
  57. * Updates device permissions.
  58. *
  59. * @returns {Promise<void>}
  60. */
  61. async _updatePermissions() {
  62. const hasPermissions = await JitsiMeetJS.mediaDevices.isDevicePermissionGranted(
  63. 'audio',
  64. );
  65. this._isMounted && this.setState({
  66. hasPermissions
  67. });
  68. }
  69. /**
  70. * Implements React's {@link Component#componentDidMount}.
  71. *
  72. * @inheritdoc
  73. */
  74. componentDidMount() {
  75. this._updatePermissions();
  76. }
  77. /**
  78. * Implements React's {@link Component#componentDidUpdate}.
  79. *
  80. * @inheritdoc
  81. */
  82. componentDidUpdate(prevProps) {
  83. if (this.props.permissionPromptVisibility !== prevProps.permissionPromptVisibility) {
  84. this._updatePermissions();
  85. }
  86. }
  87. /**
  88. * Implements React's {@link Component#componentWillUnmount}.
  89. *
  90. * @inheritdoc
  91. */
  92. componentWillUnmount() {
  93. this._isMounted = false;
  94. }
  95. /**
  96. * Implements React's {@link Component#render}.
  97. *
  98. * @inheritdoc
  99. */
  100. render() {
  101. const { isDisabled, onAudioOptionsClick, visible } = this.props;
  102. const settingsDisabled = !this.state.hasPermissions
  103. || isDisabled
  104. || !JitsiMeetJS.mediaDevices.isMultipleAudioInputSupported();
  105. return visible ? (
  106. <AudioSettingsPopup>
  107. <ToolboxButtonWithIcon
  108. icon = { IconArrowDown }
  109. iconDisabled = { settingsDisabled }
  110. onIconClick = { onAudioOptionsClick }>
  111. <AudioMuteButton />
  112. </ToolboxButtonWithIcon>
  113. </AudioSettingsPopup>
  114. ) : null;
  115. }
  116. }
  117. /**
  118. * Function that maps parts of Redux state tree into component props.
  119. *
  120. * @param {Object} state - Redux state.
  121. * @returns {Object}
  122. */
  123. function mapStateToProps(state) {
  124. return {
  125. isDisabled: isAudioSettingsButtonDisabled(state),
  126. permissionPromptVisibility: getMediaPermissionPromptVisibility(state)
  127. };
  128. }
  129. const mapDispatchToProps = {
  130. onAudioOptionsClick: toggleAudioSettings
  131. };
  132. export default connect(
  133. mapStateToProps,
  134. mapDispatchToProps,
  135. )(AudioSettingsButton);