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.

MicrophoneEntry.tsx 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import clsx from 'clsx';
  2. import React, { Component } from 'react';
  3. import Icon from '../../../../base/icons/components/Icon';
  4. import { IconCheck, IconExclamationSolid } from '../../../../base/icons/svg';
  5. // eslint-disable-next-line lines-around-comment
  6. // @ts-ignore
  7. import JitsiMeetJS from '../../../../base/lib-jitsi-meet/_';
  8. import ContextMenuItem from '../../../../base/ui/components/web/ContextMenuItem';
  9. import { TEXT_OVERFLOW_TYPES } from '../../../../base/ui/constants.any';
  10. import Meter from './Meter';
  11. const JitsiTrackEvents = JitsiMeetJS.events.track;
  12. type Props = {
  13. /**
  14. * The text for this component.
  15. */
  16. children: string;
  17. /**
  18. * The deviceId of the microphone.
  19. */
  20. deviceId: string;
  21. /**
  22. * Flag indicating if there is a problem with the device.
  23. */
  24. hasError?: boolean;
  25. /**
  26. * Flag indicating if there is a problem with the device.
  27. */
  28. index?: number;
  29. /**
  30. * Flag indicating the selection state.
  31. */
  32. isSelected: boolean;
  33. /**
  34. * The audio track for the current entry.
  35. */
  36. jitsiTrack: any;
  37. /**
  38. * The id for the label, that contains the item text.
  39. */
  40. labelId?: string;
  41. /**
  42. * The length of the microphone list.
  43. */
  44. length: number;
  45. listHeaderId: string;
  46. /**
  47. * Used to decide whether to listen to audio level changes.
  48. */
  49. measureAudioLevels: boolean;
  50. /**
  51. * Click handler for component.
  52. */
  53. onClick: Function;
  54. };
  55. type State = {
  56. /**
  57. * The audio level.
  58. */
  59. level: number;
  60. };
  61. /**
  62. * React {@code Component} representing an entry for the microphone audio settings.
  63. *
  64. * @param {Props} props - The props of the component.
  65. * @returns { ReactElement}
  66. */
  67. export default class MicrophoneEntry extends Component<Props, State> {
  68. /**
  69. * Initializes a new {@code MicrophoneEntry} instance.
  70. *
  71. * @param {Object} props - The read-only properties with which the new
  72. * instance is to be initialized.
  73. */
  74. constructor(props: Props) {
  75. super(props);
  76. this.state = {
  77. level: -1
  78. };
  79. this._onClick = this._onClick.bind(this);
  80. this._onKeyPress = this._onKeyPress.bind(this);
  81. this._updateLevel = this._updateLevel.bind(this);
  82. }
  83. /**
  84. * Click handler for the entry.
  85. *
  86. * @returns {void}
  87. */
  88. _onClick() {
  89. this.props.onClick(this.props.deviceId);
  90. }
  91. /**
  92. * Key pressed handler for the entry.
  93. *
  94. * @param {Object} e - The event.
  95. * @private
  96. *
  97. * @returns {void}
  98. */
  99. _onKeyPress(e: React.KeyboardEvent) {
  100. if (e.key === ' ') {
  101. e.preventDefault();
  102. this.props.onClick(this.props.deviceId);
  103. }
  104. }
  105. /**
  106. * Updates the level of the meter.
  107. *
  108. * @param {number} num - The audio level provided by the jitsiTrack.
  109. * @returns {void}
  110. */
  111. _updateLevel(num: number) {
  112. this.setState({
  113. level: Math.floor(num / 0.125)
  114. });
  115. }
  116. /**
  117. * Subscribes to audio level changes coming from the jitsiTrack.
  118. *
  119. * @returns {void}
  120. */
  121. _startListening() {
  122. const { jitsiTrack, measureAudioLevels } = this.props;
  123. jitsiTrack && measureAudioLevels && jitsiTrack.on(
  124. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  125. this._updateLevel);
  126. }
  127. /**
  128. * Unsubscribes from changes coming from the jitsiTrack.
  129. *
  130. * @param {Object} jitsiTrack - The jitsiTrack to unsubscribe from.
  131. * @returns {void}
  132. */
  133. _stopListening(jitsiTrack?: any) {
  134. jitsiTrack?.off(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, this._updateLevel);
  135. this.setState({
  136. level: -1
  137. });
  138. }
  139. /**
  140. * Implements React's {@link Component#componentDidUpdate}.
  141. *
  142. * @inheritdoc
  143. */
  144. componentDidUpdate(prevProps: Props) {
  145. if (prevProps.jitsiTrack !== this.props.jitsiTrack) {
  146. this._stopListening(prevProps.jitsiTrack);
  147. this._startListening();
  148. }
  149. }
  150. /**
  151. * Implements React's {@link Component#componentDidMount}.
  152. *
  153. * @inheritdoc
  154. */
  155. componentDidMount() {
  156. this._startListening();
  157. }
  158. /**
  159. * Implements React's {@link Component#componentWillUnmount}.
  160. *
  161. * @inheritdoc
  162. */
  163. componentWillUnmount() {
  164. this._stopListening(this.props.jitsiTrack);
  165. }
  166. /**
  167. * Implements React's {@link Component#render}.
  168. *
  169. * @inheritdoc
  170. */
  171. render() {
  172. const {
  173. deviceId,
  174. children,
  175. hasError,
  176. index,
  177. isSelected,
  178. length,
  179. jitsiTrack,
  180. listHeaderId,
  181. measureAudioLevels
  182. } = this.props;
  183. const deviceTextId = `choose_microphone${deviceId}`;
  184. const labelledby = `${listHeaderId} ${deviceTextId} `;
  185. const className = `audio-preview-microphone ${measureAudioLevels
  186. ? 'audio-preview-microphone--withmeter' : 'audio-preview-microphone--nometer'}`;
  187. return (
  188. <li
  189. aria-checked = { isSelected }
  190. aria-labelledby = { labelledby }
  191. aria-posinset = { index }
  192. aria-setsize = { length }
  193. className = { className }
  194. onClick = { this._onClick }
  195. onKeyPress = { this._onKeyPress }
  196. role = 'radio'
  197. tabIndex = { 0 }>
  198. <ContextMenuItem
  199. accessibilityLabel = ''
  200. icon = { isSelected ? IconCheck : undefined }
  201. overflowType = { TEXT_OVERFLOW_TYPES.SCROLL_ON_HOVER }
  202. selected = { isSelected }
  203. text = { children }
  204. textClassName = { clsx('audio-preview-entry-text', !isSelected && 'left-margin') }>
  205. {hasError && <Icon
  206. className = 'audio-preview-icon audio-preview-icon--exclamation'
  207. size = { 16 }
  208. src = { IconExclamationSolid } />}
  209. </ContextMenuItem>
  210. { Boolean(jitsiTrack) && measureAudioLevels && <Meter
  211. className = 'audio-preview-meter-mic'
  212. isDisabled = { hasError }
  213. level = { this.state.level } />
  214. }
  215. </li>
  216. );
  217. }
  218. }