123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- import React, { useEffect, useState } from 'react';
- import { makeStyles } from 'tss-react/mui';
-
- import JitsiMeetJS from '../../base/lib-jitsi-meet/_.web';
-
- const JitsiTrackEvents = JitsiMeetJS.events.track;
-
- /**
- * The type of the React {@code Component} props of {@link AudioInputPreview}.
- */
- interface IProps {
-
- /**
- * The JitsiLocalTrack to show an audio level meter for.
- */
- track: any;
- }
-
- const useStyles = makeStyles()(theme => {
- return {
- container: {
- display: 'flex'
- },
-
- section: {
- flex: 1,
- height: '4px',
- borderRadius: '1px',
- backgroundColor: theme.palette.ui04,
- marginRight: theme.spacing(1),
-
- '&:last-of-type': {
- marginRight: 0
- }
- },
-
- activeSection: {
- backgroundColor: theme.palette.success01
- }
- };
- });
-
- const NO_OF_PREVIEW_SECTIONS = 11;
-
- const AudioInputPreview = (props: IProps) => {
- const [ audioLevel, setAudioLevel ] = useState(0);
- const { classes, cx } = useStyles();
-
- /**
- * Starts listening for audio level updates from the library.
- *
- * @param {JitsiLocalTrack} track - The track to listen to for audio level
- * updates.
- * @private
- * @returns {void}
- */
- function _listenForAudioUpdates(track: any) {
- _stopListeningForAudioUpdates();
-
- track?.on(
- JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
- setAudioLevel);
- }
-
- /**
- * Stops listening to further updates from the current track.
- *
- * @private
- * @returns {void}
- */
- function _stopListeningForAudioUpdates() {
- props.track?.off(
- JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
- setAudioLevel);
- }
-
- useEffect(() => {
- _listenForAudioUpdates(props.track);
-
- return _stopListeningForAudioUpdates;
- }, []);
-
- useEffect(() => {
- _listenForAudioUpdates(props.track);
- setAudioLevel(0);
- }, [ props.track ]);
-
- const audioMeterFill = Math.ceil(Math.floor(audioLevel * 100) / (100 / NO_OF_PREVIEW_SECTIONS));
-
- return (
- <div className = { classes.container } >
- {new Array(NO_OF_PREVIEW_SECTIONS).fill(0)
- .map((_, idx) =>
- (<div
- className = { cx(classes.section, idx < audioMeterFill && classes.activeSection) }
- key = { idx } />)
- )}
- </div>
- );
- };
-
- export default AudioInputPreview;
|