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 AudioSettingsEntry, { type Props as AudioSettingsEntryProps } from './AudioSettingsEntry';
  4. import JitsiMeetJS from '../../../../base/lib-jitsi-meet/_';
  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(
  92. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  93. this._updateLevel);
  94. this.setState({
  95. level: -1
  96. });
  97. }
  98. /**
  99. * Implements React's {@link Component#componentDidUpdate}.
  100. *
  101. * @inheritdoc
  102. */
  103. componentDidUpdate(prevProps: Props) {
  104. if (prevProps.jitsiTrack !== this.props.jitsiTrack) {
  105. this._stopListening(prevProps.jitsiTrack);
  106. this._startListening();
  107. }
  108. }
  109. /**
  110. * Implements React's {@link Component#componentDidMount}.
  111. *
  112. * @inheritdoc
  113. */
  114. componentDidMount() {
  115. this._startListening();
  116. }
  117. /**
  118. * Implements React's {@link Component#componentWillUnmount}.
  119. *
  120. * @inheritdoc
  121. */
  122. compmonentWillUnmount() {
  123. this._stopListening(this.props.jitsiTrack);
  124. }
  125. /**
  126. * Implements React's {@link Component#render}.
  127. *
  128. * @inheritdoc
  129. */
  130. render() {
  131. const { children, hasError, isSelected } = this.props;
  132. return (
  133. <div
  134. className = 'audio-preview-microphone'
  135. onClick = { this._onClick }>
  136. <AudioSettingsEntry
  137. hasError = { hasError }
  138. isSelected = { isSelected }>
  139. {children}
  140. </AudioSettingsEntry>
  141. <Meter
  142. className = 'audio-preview-meter-mic'
  143. isDisabled = { hasError }
  144. level = { this.state.level } />
  145. </div>
  146. );
  147. }
  148. }