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

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