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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // @flow
  2. import React, { Component } from 'react';
  3. import AudioMuteButton from '../AudioMuteButton';
  4. import { hasAvailableDevices } from '../../../base/devices';
  5. import { IconArrowDown } from '../../../base/icons';
  6. import JitsiMeetJS from '../../../base/lib-jitsi-meet/_';
  7. import { ToolboxButtonWithIcon } from '../../../base/toolbox';
  8. import { connect } from '../../../base/redux';
  9. import { getMediaPermissionPromptVisibility } from '../../../overlay';
  10. import { AudioSettingsPopup, toggleAudioSettings } from '../../../settings';
  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 user has audio input or audio output devices.
  23. */
  24. hasDevices: 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. /**
  43. * Initializes a new {@code AudioSettingsButton} instance.
  44. *
  45. * @param {Object} props - The read-only properties with which the new
  46. * instance is to be initialized.
  47. */
  48. constructor(props) {
  49. super(props);
  50. this.state = {
  51. hasPermissions: false
  52. };
  53. }
  54. /**
  55. * Updates device permissions.
  56. *
  57. * @returns {Promise<void>}
  58. */
  59. async _updatePermissions() {
  60. const hasPermissions = await JitsiMeetJS.mediaDevices.isDevicePermissionGranted(
  61. 'audio',
  62. );
  63. this.setState({
  64. hasPermissions
  65. });
  66. }
  67. /**
  68. * Implements React's {@link Component#componentDidMount}.
  69. *
  70. * @inheritdoc
  71. */
  72. componentDidMount() {
  73. this._updatePermissions();
  74. }
  75. /**
  76. * Implements React's {@link Component#componentDidUpdate}.
  77. *
  78. * @inheritdoc
  79. */
  80. componentDidUpdate(prevProps) {
  81. if (this.props.permissionPromptVisibility !== prevProps.permissionPromptVisibility) {
  82. this._updatePermissions();
  83. }
  84. }
  85. /**
  86. * Implements React's {@link Component#render}.
  87. *
  88. * @inheritdoc
  89. */
  90. render() {
  91. const { hasDevices, onAudioOptionsClick, visible } = this.props;
  92. const settingsDisabled = !this.state.hasPermissions || !hasDevices;
  93. return visible ? (
  94. <AudioSettingsPopup>
  95. <ToolboxButtonWithIcon
  96. icon = { IconArrowDown }
  97. iconDisabled = { settingsDisabled }
  98. onIconClick = { onAudioOptionsClick }>
  99. <AudioMuteButton />
  100. </ToolboxButtonWithIcon>
  101. </AudioSettingsPopup>
  102. ) : null;
  103. }
  104. }
  105. /**
  106. * Function that maps parts of Redux state tree into component props.
  107. *
  108. * @param {Object} state - Redux state.
  109. * @returns {Object}
  110. */
  111. function mapStateToProps(state) {
  112. return {
  113. hasDevices:
  114. hasAvailableDevices(state, 'audioInput')
  115. || hasAvailableDevices(state, 'audioOutput'),
  116. permissionPromptVisibility: getMediaPermissionPromptVisibility(state)
  117. };
  118. }
  119. const mapDispatchToProps = {
  120. onAudioOptionsClick: toggleAudioSettings
  121. };
  122. export default connect(
  123. mapStateToProps,
  124. mapDispatchToProps,
  125. )(AudioSettingsButton);