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 { useCallState } from '@custom/shared/contexts/CallProvider'; import { useParticipants } from '@custom/shared/contexts/ParticipantsProvider'; import { useUIState } from '@custom/shared/contexts/UIStateProvider'; import { useLiveStreaming } from '../contexts/LiveStreamingProvider'; 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 { callObject } = useCallState(); const { allParticipants } = useParticipants(); const { currentModals, closeModal } = useUIState(); const { isStreaming, streamError } = useLiveStreaming(); const [pending, setPending] = useState(false); const [rtmpUrl, setRtmpUrl] = useState(''); const [layout, setLayout] = useState(0); const [maxCams, setMaxCams] = useState(9); const [participant, setParticipant] = useState(0); useEffect(() => { // Reset pending state whenever stream state changes setPending(false); }, [isStreaming]); function startLiveStream() { setPending(true); const opts = layout === 'single-participant' ? { session_id: participant.id } : { max_cam_streams: maxCams }; callObject.startLiveStreaming({ rtmpUrl, preset: layout, ...opts }); } function stopLiveStreaming() { setPending(true); callObject.stopLiveStreaming(); } return ( closeModal(LIVE_STREAMING_MODAL)} actions={[ , !isStreaming ? ( ) : ( ), ]} > {streamError && ( Unable to start stream. Error message: {streamError} )} setLayout(Number(e.target.value))} value={layout} > {LAYOUTS.map((l, i) => ( ))} {layout !== LAYOUTS.findIndex((l) => l.value === 'single-participant') && ( setMaxCams(Number(e.target.value))} value={maxCams} > )} {layout === LAYOUTS.findIndex((l) => l.value === 'single-participant') && ( setParticipant(e.target.value)} value={participant} > {allParticipants.map((p) => ( ))} )} setRtmpUrl(e.target.value)} /> ); }; export default LiveStreamingModal;