2022-11-03 21:36:30 -05:00
|
|
|
import { useState } from 'react'
|
|
|
|
import Box from '@mui/material/Box'
|
|
|
|
import RecordVoiceOver from '@mui/icons-material/RecordVoiceOver'
|
|
|
|
import VoiceOverOff from '@mui/icons-material/VoiceOverOff'
|
|
|
|
import List from '@mui/material/List'
|
|
|
|
import ListItem from '@mui/material/ListItem'
|
|
|
|
import ListItemText from '@mui/material/ListItemText'
|
|
|
|
import Menu from '@mui/material/Menu'
|
|
|
|
import MenuItem from '@mui/material/MenuItem'
|
|
|
|
import Fab from '@mui/material/Fab'
|
2022-11-06 13:36:15 -06:00
|
|
|
import Tooltip from '@mui/material/Tooltip'
|
2022-11-03 21:36:30 -05:00
|
|
|
|
|
|
|
import { PeerRoom } from 'services/PeerRoom/PeerRoom'
|
|
|
|
|
|
|
|
import { useRoomAudio } from './useRoomAudio'
|
|
|
|
|
|
|
|
export interface RoomAudioControlsProps {
|
|
|
|
peerRoom: PeerRoom
|
|
|
|
}
|
|
|
|
|
|
|
|
export function RoomAudioControls({ peerRoom }: RoomAudioControlsProps) {
|
|
|
|
const {
|
|
|
|
audioDevices,
|
|
|
|
isSpeakingToRoom,
|
|
|
|
setIsSpeakingToRoom,
|
|
|
|
handleAudioDeviceSelect,
|
|
|
|
} = useRoomAudio({ peerRoom })
|
|
|
|
|
|
|
|
const [audioAnchorEl, setAudioAnchorEl] = useState<null | HTMLElement>(null)
|
|
|
|
const isAudioDeviceSelectOpen = Boolean(audioAnchorEl)
|
|
|
|
const [selectedAudioDeviceIdx, setSelectedAudioDeviceIdx] = useState(0)
|
|
|
|
|
|
|
|
const handleVoiceCallClick = () => {
|
|
|
|
setIsSpeakingToRoom(!isSpeakingToRoom)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleAudioDeviceListItemClick = (
|
|
|
|
event: React.MouseEvent<HTMLElement>
|
|
|
|
) => {
|
|
|
|
setAudioAnchorEl(event.currentTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleAudioDeviceMenuItemClick = (
|
|
|
|
_event: React.MouseEvent<HTMLElement>,
|
|
|
|
idx: number
|
|
|
|
) => {
|
|
|
|
setSelectedAudioDeviceIdx(idx)
|
|
|
|
handleAudioDeviceSelect(audioDevices[idx])
|
|
|
|
setAudioAnchorEl(null)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleAudioInputSelectMenuClose = () => {
|
|
|
|
setAudioAnchorEl(null)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-11-06 13:36:15 -06:00
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
alignItems: 'center',
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column',
|
|
|
|
justifyContent: 'center',
|
|
|
|
px: 1,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Tooltip
|
|
|
|
title={
|
|
|
|
isSpeakingToRoom
|
|
|
|
? 'Turn off microphone'
|
|
|
|
: 'Turn on microphone and speak to room'
|
|
|
|
}
|
2022-11-03 21:36:30 -05:00
|
|
|
>
|
2022-11-06 13:36:15 -06:00
|
|
|
<Fab
|
|
|
|
color={isSpeakingToRoom ? 'error' : 'success'}
|
|
|
|
aria-label="call"
|
|
|
|
onClick={handleVoiceCallClick}
|
|
|
|
>
|
|
|
|
{isSpeakingToRoom ? <VoiceOverOff /> : <RecordVoiceOver />}
|
|
|
|
</Fab>
|
|
|
|
</Tooltip>
|
2022-11-03 21:36:30 -05:00
|
|
|
{audioDevices.length > 0 && (
|
|
|
|
<Box sx={{ mt: 1 }}>
|
|
|
|
<List
|
|
|
|
component="nav"
|
2022-11-06 13:36:15 -06:00
|
|
|
aria-label="Microphone selection"
|
2022-11-03 21:36:30 -05:00
|
|
|
sx={{ bgcolor: 'background.paper' }}
|
|
|
|
>
|
|
|
|
<ListItem
|
|
|
|
button
|
|
|
|
id="audio-input-select-button"
|
|
|
|
aria-haspopup="listbox"
|
|
|
|
aria-controls="audio-input-select-menu"
|
2022-11-06 13:36:15 -06:00
|
|
|
aria-label="Microphone to use"
|
2022-11-03 21:36:30 -05:00
|
|
|
aria-expanded={isAudioDeviceSelectOpen ? 'true' : undefined}
|
|
|
|
onClick={handleAudioDeviceListItemClick}
|
|
|
|
>
|
|
|
|
<ListItemText
|
2022-11-06 13:36:15 -06:00
|
|
|
primary="Selected microphone"
|
2022-11-03 21:36:30 -05:00
|
|
|
secondary={audioDevices[selectedAudioDeviceIdx]?.label}
|
|
|
|
/>
|
|
|
|
</ListItem>
|
|
|
|
</List>
|
|
|
|
<Menu
|
|
|
|
id="audio-input-select-menu"
|
|
|
|
anchorEl={audioAnchorEl}
|
|
|
|
open={isAudioDeviceSelectOpen}
|
|
|
|
onClose={handleAudioInputSelectMenuClose}
|
|
|
|
MenuListProps={{
|
|
|
|
'aria-labelledby': 'audio-input-select-button',
|
|
|
|
role: 'listbox',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{audioDevices.map((audioDevice, idx) => (
|
|
|
|
<MenuItem
|
|
|
|
key={audioDevice.deviceId}
|
|
|
|
selected={idx === selectedAudioDeviceIdx}
|
|
|
|
onClick={event => handleAudioDeviceMenuItemClick(event, idx)}
|
|
|
|
>
|
|
|
|
{audioDevice.label}
|
|
|
|
</MenuItem>
|
|
|
|
))}
|
|
|
|
</Menu>
|
|
|
|
</Box>
|
|
|
|
)}
|
2022-11-06 13:36:15 -06:00
|
|
|
</Box>
|
2022-11-03 21:36:30 -05:00
|
|
|
)
|
|
|
|
}
|