Skip to content

Commit

Permalink
refactor: move audio level out of prebuilt
Browse files Browse the repository at this point in the history
  • Loading branch information
raviteja83 committed Sep 19, 2023
1 parent 875b7ba commit c69b87f
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 116 deletions.
100 changes: 70 additions & 30 deletions packages/roomkit-react/src/AudioLevel/AudioLevel.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,74 @@
import { useCallback, useRef } from 'react';
import { HMSTrackID } from '@100mslive/hms-video-store';
import { useAudioLevelStyles } from '@100mslive/react-sdk';
import { useTheme } from '../Theme';
import React, { useEffect, useRef } from 'react';
import { selectTrackAudioByID, useHMSVanillaStore } from '@100mslive/react-sdk';
import { Box, Flex } from '../Layout';
import { keyframes } from '../Theme';
//@ts-ignore
import bg from './audio-level.png';

/**
* pass in a track id and get a ref. That ref can be attached to an element which will have border
* as per audio level post that.
*/
export function useBorderAudioLevel(audioTrackId?: HMSTrackID) {
const { theme } = useTheme();
const color = theme.colors.primary_default.value;
const getStyle = useCallback(
(level: number) => {
const style: Record<string, string> = {
transition: 'outline 0.4s ease-in-out',
};
style['outline'] = level ? `${sigmoid(level) * 4}px solid ${color}` : '0px solid transparent';
return style;
},
[color],
// keep the calculated values before hand to avoid recalcuation everytime
const positionValues = new Array(101).fill(0).reduce((acc, _, index) => {
acc[index] = Math.round((index / 100) * 4) / 4; // convert to 0.25 multiples
return acc;
}, {});

const barAnimation = keyframes({
from: {
maskSize: '4em .8em',
'-webkit-mask-position-y': '.1em',
},

'50%': {
maskSize: '4em 1em',
'-webkit-mask-position-y': 0,
},

to: {
maskSize: '4em .8em',
'-webkit-mask-position-y': '.1em',
},
});

const AudioBar = () => {
return (
<Box
css={{
width: '.25em',
height: '1em',
maskImage: `url(${bg})`,
'-webkit-mask-repeat': 'no-repeat',
backgroundColor: '$on_primary_high',
maskSize: '4em 1em',
maskPosition: '0 0.1em',
}}
/>
);
const ref = useRef(null);
useAudioLevelStyles({
trackId: audioTrackId,
getStyle,
ref,
});
return ref;
}
};

export const AudioLevel = ({ trackId }: { trackId?: string }) => {
const ref = useRef<HTMLDivElement | null>(null);
const vanillaStore = useHMSVanillaStore();

export const sigmoid = (z: number) => {
return 1 / (1 + Math.exp(-z));
useEffect(() => {
const unsubscribe = vanillaStore.subscribe(audioLevel => {
if (ref.current) {
let index = 0;
//@ts-ignore
for (const child of ref.current.children) {
const positionX = `-${positionValues[audioLevel] * (index === 1 ? 2.5 : 1.25)}em`;
child.style['-webkit-mask-position-x'] = positionX;
child.style['animation'] =
positionValues[audioLevel] > 0 ? `${barAnimation} 0.6s steps(3,jump-none) 0s infinite` : 'none';
index++;
}
}
}, selectTrackAudioByID(trackId));
return unsubscribe;
}, [vanillaStore, trackId]);
return (
<Flex ref={ref} css={{ fontSize: '1rem', gap: '$2' }}>
<AudioBar />
<AudioBar />
<AudioBar />
</Flex>
);
};
3 changes: 2 additions & 1 deletion packages/roomkit-react/src/AudioLevel/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { useBorderAudioLevel } from './AudioLevel';
export { useBorderAudioLevel } from './useBorderAudioLevel';
export { AudioLevel } from './AudioLevel';
34 changes: 34 additions & 0 deletions packages/roomkit-react/src/AudioLevel/useBorderAudioLevel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useCallback, useRef } from 'react';
import { HMSTrackID } from '@100mslive/hms-video-store';
import { useAudioLevelStyles } from '@100mslive/react-sdk';
import { useTheme } from '../Theme';

/**
* pass in a track id and get a ref. That ref can be attached to an element which will have border
* as per audio level post that.
*/
export function useBorderAudioLevel(audioTrackId?: HMSTrackID) {
const { theme } = useTheme();
const color = theme.colors.primary_default.value;
const getStyle = useCallback(
(level: number) => {
const style: Record<string, string> = {
transition: 'outline 0.4s ease-in-out',
};
style['outline'] = level ? `${sigmoid(level) * 4}px solid ${color}` : '0px solid transparent';
return style;
},
[color],
);
const ref = useRef(null);
useAudioLevelStyles({
trackId: audioTrackId,
getStyle,
ref,
});
return ref;
}

export const sigmoid = (z: number) => {
return 1 / (1 + Math.exp(-z));
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '@100mslive/react-sdk';
import { MicOffIcon, SettingsIcon } from '@100mslive/react-icons';
import { Avatar, Box, config as cssConfig, Flex, flexCenter, styled, StyledVideoTile, Text, Video } from '../../..';
import { AudioLevel } from '../../../AudioLevel';
import { useHMSPrebuiltContext } from '../../AppContext';
// @ts-ignore: No implicit Any
import IconButton from '../../IconButton';
Expand All @@ -31,8 +32,6 @@ import { Logo } from '../Header/HeaderComponents';
// @ts-ignore: No implicit Any
import SettingsModal from '../Settings/SettingsModal';
// @ts-ignore: No implicit Any
import { AudioLevel } from '../VideoTile';
// @ts-ignore: No implicit Any
import PreviewForm from './PreviewForm';
// @ts-ignore: No implicit Any
import { useAuthToken, useUISettings } from '../AppData/useUISettings';
Expand Down Expand Up @@ -244,7 +243,9 @@ export const PreviewTile = ({ name, error }: { name: string; error?: boolean })
<MicOffIcon />
</StyledVideoTile.AudioIndicator>
) : (
<AudioLevel trackId={localPeer?.audioTrack} />
<StyledVideoTile.AudioIndicator size="medium">
<AudioLevel trackId={localPeer?.audioTrack} />
</StyledVideoTile.AudioIndicator>
)}
</StyledVideoTile.Container>
);
Expand Down
14 changes: 4 additions & 10 deletions packages/roomkit-react/src/Prebuilt/components/VideoTile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import {
import { BrbTileIcon, HandIcon, MicOffIcon } from '@100mslive/react-icons';
import TileConnection from './Connection/TileConnection';
import TileMenu, { isSameTile } from './TileMenu/TileMenu';
import { AudioLevel } from '../../AudioLevel';
import { Avatar } from '../../Avatar';
import { VideoTileStats } from '../../Stats';
import { config as cssConfig } from '../../Theme';
import { Video } from '../../Video';
import { StyledVideoTile } from '../../VideoTile';
import { AudioLevelAnimation } from './AudioLevelAnimation';
import { getVideoTileLabel } from './peerTileUtils';
import { useSetAppDataByKey, useUISettings } from './AppData/useUISettings';
import { APP_DATA, SESSION_STORE_KEY, UI_SETTINGS } from '../common/constants';
Expand Down Expand Up @@ -136,7 +136,9 @@ const Tile = ({
<MicOffIcon />
</StyledVideoTile.AudioIndicator>
) : (
<AudioLevel trackId={audioTrack?.id} />
<StyledVideoTile.AudioIndicator>
<AudioLevel trackId={audioTrack?.id} />
</StyledVideoTile.AudioIndicator>
)
) : null}
{isMouseHovered || (isDragabble && isMobile) ? (
Expand Down Expand Up @@ -167,14 +169,6 @@ const Tile = ({

const metaStyles = { top: '$4', left: '$4', width: '$14', height: '$14' };

export const AudioLevel = ({ trackId }) => {
return (
<StyledVideoTile.AudioIndicator>
<AudioLevelAnimation trackId={trackId} />
</StyledVideoTile.AudioIndicator>
);
};

const PeerMetadata = ({ peerId }) => {
const metaData = useHMSStore(selectPeerMetadata(peerId));
const isBRB = metaData?.isBRBOn || false;
Expand Down

0 comments on commit c69b87f

Please sign in to comment.