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.

VideoSettingsButton.js 4.4KB

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