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

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