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.

AudioInputPreview.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import { JitsiTrackEvents } from '../../base/lib-jitsi-meet';
  4. /**
  5. * The type of the React {@code Component} props of {@link AudioInputPreview}.
  6. */
  7. type Props = {
  8. /**
  9. * The JitsiLocalTrack to show an audio level meter for.
  10. */
  11. track: Object
  12. };
  13. /**
  14. * The type of the React {@code Component} props of {@link AudioInputPreview}.
  15. */
  16. type State = {
  17. /**
  18. * The current audio input level being received, from 0 to 1.
  19. */
  20. audioLevel: number
  21. };
  22. /**
  23. * React component for displaying a audio level meter for a JitsiLocalTrack.
  24. */
  25. class AudioInputPreview extends Component<Props, State> {
  26. /**
  27. * Initializes a new AudioInputPreview instance.
  28. *
  29. * @param {Object} props - The read-only React Component props with which
  30. * the new instance is to be initialized.
  31. */
  32. constructor(props: Props) {
  33. super(props);
  34. this.state = {
  35. audioLevel: 0
  36. };
  37. this._updateAudioLevel = this._updateAudioLevel.bind(this);
  38. }
  39. /**
  40. * Starts listening for audio level updates after the initial render.
  41. *
  42. * @inheritdoc
  43. * @returns {void}
  44. */
  45. componentDidMount() {
  46. this._listenForAudioUpdates(this.props.track);
  47. }
  48. /**
  49. * Stops listening for audio level updates on the old track and starts
  50. * listening instead on the new track.
  51. *
  52. * @inheritdoc
  53. * @returns {void}
  54. */
  55. componentDidUpdate(prevProps: Props) {
  56. if (prevProps.track !== this.props.track) {
  57. this._listenForAudioUpdates(this.props.track);
  58. this._updateAudioLevel(0);
  59. }
  60. }
  61. /**
  62. * Unsubscribe from audio level updates.
  63. *
  64. * @inheritdoc
  65. * @returns {void}
  66. */
  67. componentWillUnmount() {
  68. this._stopListeningForAudioUpdates();
  69. }
  70. /**
  71. * Implements React's {@link Component#render()}.
  72. *
  73. * @inheritdoc
  74. * @returns {ReactElement}
  75. */
  76. render() {
  77. const audioMeterFill = {
  78. width: `${Math.floor(this.state.audioLevel * 100)}%`
  79. };
  80. return (
  81. <div className = 'audio-input-preview' >
  82. <div
  83. className = 'audio-input-preview-level'
  84. style = { audioMeterFill } />
  85. </div>
  86. );
  87. }
  88. /**
  89. * Starts listening for audio level updates from the library.
  90. *
  91. * @param {JitstiLocalTrack} track - The track to listen to for audio level
  92. * updates.
  93. * @private
  94. * @returns {void}
  95. */
  96. _listenForAudioUpdates(track) {
  97. this._stopListeningForAudioUpdates();
  98. track && track.on(
  99. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  100. this._updateAudioLevel);
  101. }
  102. /**
  103. * Stops listening to further updates from the current track.
  104. *
  105. * @private
  106. * @returns {void}
  107. */
  108. _stopListeningForAudioUpdates() {
  109. this.props.track && this.props.track.off(
  110. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  111. this._updateAudioLevel);
  112. }
  113. _updateAudioLevel: (number) => void;
  114. /**
  115. * Updates the internal state of the last know audio level. The level should
  116. * be between 0 and 1, as the level will be used as a percentage out of 1.
  117. *
  118. * @param {number} audioLevel - The new audio level for the track.
  119. * @private
  120. * @returns {void}
  121. */
  122. _updateAudioLevel(audioLevel) {
  123. this.setState({
  124. audioLevel
  125. });
  126. }
  127. }
  128. export default AudioInputPreview;