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.js 5.6KB

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