import React, { useEffect, useState } from 'react'; import PropTypes from 'prop-types'; const Controls = (props) => { const [muted, setMuted] = useState(false); const [videoStatus, setVideoStatus] = useState('Disabled'); const [spinning, setSpinning] = useState(false); const callback = (localMuted) => { setMuted(localMuted); }; useEffect(() => { props.chime.audioVideo.realtimeSubscribeToMuteAndUnmuteLocalAudio(callback); return function cleanup() { if (props && props.chime.audioVideo) { props.chime.audioVideo.realtimeUnsubscribeToMuteAndUnmuteLocalAudio( callback, ); } }; }, []); const muteButtonOnClick = async () => { if (muted) { props.chime.audioVideo.realtimeUnmuteLocalAudio(); } else { props.chime.audioVideo.realtimeMuteLocalAudio(); } await new Promise((resolve) => setTimeout(resolve, 10)); }; const videoButtonOnClick = async () => { await new Promise((resolve) => setTimeout(resolve, 10)); if (videoStatus === 'Disabled') { setVideoStatus('Loading'); try { if (!props.chime.currentVideoInputDevice) { throw new Error('currentVideoInputDevice does not exist'); } try { await props.chime.chooseVideoInputDevice( props.chime.currentVideoInputDevice, ); } catch (err) { const videoInputDevices = await props.chime.audioVideo.listVideoInputDevices(); await props.chime.audioVideo.chooseVideoInputDevice( videoInputDevices[0].deviceId, ); } props.chime.audioVideo.startLocalVideoTile(); setVideoStatus('Enabled'); } catch (error) { // eslint-disable-next-line console.error(error); setVideoStatus('Disabled'); } } else if (videoStatus === 'Enabled') { setVideoStatus('Loading'); props.chime.meetingSession.audioVideo.stopLocalVideoTile(); setVideoStatus('Disabled'); } }; const endButtonOnClick = async () => { setSpinning(true); await props.chime.leaveRoom(props.role === 'host'); sessionStorage.removeItem(props.ssName); const whereTo = `${props.baseHref}/${ props.role === 'host' ? '' : 'join?room=' + props.title }`; props.history.push(whereTo); setSpinning(false); }; const mic_controls = muted ? 'controls__btn--mic_on' : 'controls__btn--mic_off'; const cam_controls = videoStatus === 'Enabled' ? 'controls__btn--cam_off' : 'controls__btn--cam_on'; return (
{/* */} {/* */}
); }; Controls.propTypes = { chime: PropTypes.object, baseHref: PropTypes.string, ssName: PropTypes.string, title: PropTypes.string, openSettings: PropTypes.func, role: PropTypes.string, history: PropTypes.object, myVideoElement: PropTypes.object, }; export default Controls;