import React, { useEffect, useState } from 'react'; import Button from '@custom/shared/components/Button'; import { CardBody } from '@custom/shared/components/Card'; import Field from '@custom/shared/components/Field'; import { TextInput, SelectInput } from '@custom/shared/components/Input'; import Modal from '@custom/shared/components/Modal'; import Well from '@custom/shared/components/Well'; import { useLiveStreaming } from '@custom/shared/contexts/LiveStreamingProvider'; import { useParticipants } from '@custom/shared/contexts/ParticipantsProvider'; import { useUIState } from '@custom/shared/contexts/UIStateProvider'; export const LIVE_STREAMING_MODAL = 'live-streaming'; const LAYOUTS = [ { label: 'Grid (default)', value: 'default' }, { label: 'Single participant', value: 'single-participant' }, { label: 'Active participant', value: 'active-participant' }, ]; export const LiveStreamingModal = () => { const { participants } = useParticipants(); const { currentModals, closeModal } = useUIState(); const { isStreaming, streamError, startLiveStreaming, stopLiveStreaming, } = useLiveStreaming(); const [pending, setPending] = useState(false); const [rtmpUrl, setRtmpUrl] = useState(''); const [layoutType, setLayoutType] = useState('default'); const [maxCams, setMaxCams] = useState(9); const [participantId, setParticipantId] = useState(0); useEffect(() => { // Reset pending state whenever stream state changes setPending(false); }, [isStreaming]); function startLiveStream() { const config = { rtmpUrl, layout: { preset: layoutType, }, }; if (layoutType === 'single-participant') config.layout.session_id = participantId; else if (layoutType === 'default') config.layout.max_cam_streams = maxCams; startLiveStreaming(config); } function stopLiveStream() { setPending(true); stopLiveStreaming(); } return ( closeModal(LIVE_STREAMING_MODAL)} actions={[ , !isStreaming ? ( ) : ( ), ]} > {streamError && ( Unable to start stream. Error message: {streamError} )} setLayoutType(e.target.value)} value={layoutType} > {LAYOUTS.map((l) => ( ))} {layoutType === 'default' && ( setMaxCams(Number(e.target.value))} value={maxCams} > )} {layoutType === 'single-participant' && ( setParticipantId(e.target.value)} value={participantId} > {participants.map((p) => ( ))} )} setRtmpUrl(e.target.value)} /> ); }; export default LiveStreamingModal;