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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * The audio track for the current entry.
  18. */
  19. jitsiTrack: Object,
  20. /**
  21. * Click handler for component.
  22. */
  23. onClick: Function,
  24. }
  25. type State = {
  26. /**
  27. * The audio level.
  28. */
  29. level: number,
  30. }
  31. /**
  32. * React {@code Component} representing an entry for the microphone audio settings.
  33. *
  34. * @param {Props} props - The props of the component.
  35. * @returns { ReactElement}
  36. */
  37. export default class MicrophoneEntry extends Component<Props, State> {
  38. /**
  39. * Initializes a new {@code MicrophoneEntry} instance.
  40. *
  41. * @param {Object} props - The read-only properties with which the new
  42. * instance is to be initialized.
  43. */
  44. constructor(props: Props) {
  45. super(props);
  46. this.state = {
  47. level: -1
  48. };
  49. this._onClick = this._onClick.bind(this);
  50. this._updateLevel = this._updateLevel.bind(this);
  51. }
  52. _onClick: () => void;
  53. /**
  54. * Click handler for the entry.
  55. *
  56. * @returns {void}
  57. */
  58. _onClick() {
  59. this.props.onClick(this.props.deviceId);
  60. }
  61. _updateLevel: (number) => void;
  62. /**
  63. * Updates the level of the meter.
  64. *
  65. * @param {number} num - The audio level provided by the jitsiTrack.
  66. * @returns {void}
  67. */
  68. _updateLevel(num) {
  69. this.setState({
  70. level: Math.floor(num / 0.125)
  71. });
  72. }
  73. /**
  74. * Subscribes to audio level chanages comming from the jitsiTrack.
  75. *
  76. * @returns {void}
  77. */
  78. _startListening() {
  79. const { jitsiTrack } = this.props;
  80. jitsiTrack && jitsiTrack.on(
  81. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  82. this._updateLevel);
  83. }
  84. /**
  85. * Unsubscribes from chanages comming from the jitsiTrack.
  86. *
  87. * @param {Object} jitsiTrack - The jitsiTrack to unsubscribe from.
  88. * @returns {void}
  89. */
  90. _stopListening(jitsiTrack) {
  91. jitsiTrack && jitsiTrack.off(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, this._updateLevel);
  92. this.setState({
  93. level: -1
  94. });
  95. }
  96. /**
  97. * Implements React's {@link Component#componentDidUpdate}.
  98. *
  99. * @inheritdoc
  100. */
  101. componentDidUpdate(prevProps: Props) {
  102. if (prevProps.jitsiTrack !== this.props.jitsiTrack) {
  103. this._stopListening(prevProps.jitsiTrack);
  104. this._startListening();
  105. }
  106. }
  107. /**
  108. * Implements React's {@link Component#componentDidMount}.
  109. *
  110. * @inheritdoc
  111. */
  112. componentDidMount() {
  113. this._startListening();
  114. }
  115. /**
  116. * Implements React's {@link Component#componentWillUnmount}.
  117. *
  118. * @inheritdoc
  119. */
  120. componentWillUnmount() {
  121. this._stopListening(this.props.jitsiTrack);
  122. }
  123. /**
  124. * Implements React's {@link Component#render}.
  125. *
  126. * @inheritdoc
  127. */
  128. render() {
  129. const { children, hasError, isSelected, jitsiTrack } = this.props;
  130. return (
  131. <div
  132. className = 'audio-preview-microphone'
  133. onClick = { this._onClick }>
  134. <AudioSettingsEntry
  135. hasError = { hasError }
  136. isSelected = { isSelected }>
  137. {children}
  138. </AudioSettingsEntry>
  139. { Boolean(jitsiTrack) && <Meter
  140. className = 'audio-preview-meter-mic'
  141. isDisabled = { hasError }
  142. level = { this.state.level } />
  143. }
  144. </div>
  145. );
  146. }
  147. }