import React, { useState, useEffect, useRef } from 'react'; import { Button, Select, Space, Typography } from 'antd'; import { ReloadOutlined } from '@ant-design/icons'; import DroneLivePlayer from '../VolcRtcPlayer.jsx'; import { changeCameragateway, changeCameraPlane } from '../../api/device.ts'; import utils from '../../lib/utils'; import AgoraRtcRoom from '../AgoraRtc.tsx'; const { Text } = Typography; interface DroneVideoPlayerProps { device: any; height?: number | string; showSourceSelect?: boolean; dualVideo?: boolean; defaultSource?: 'station' | 'drone'; videoSource?: 'station' | 'drone'; onSourceChange?: (source: 'station' | 'drone') => void; } const DroneVideoPlayer: React.FC = ({ device, height = 160, showSourceSelect = true, dualVideo = false, defaultSource = 'drone', videoSource: propsVideoSource, onSourceChange }) => { const [internalVideoSource, setInternalVideoSource] = useState<'station' | 'drone'>(defaultSource); const videoSource = propsVideoSource !== undefined ? propsVideoSource : internalVideoSource; const setVideoSource = (source: 'station' | 'drone') => { if (onSourceChange) onSourceChange(source); else setInternalVideoSource(source); }; const [selectedCamera, setSelectedCamera] = useState<'indoor' | 'outdoor'>('outdoor'); // 默认选择室外 const [selectedLens, setSelectedLens] = useState<'wide' | 'zoom' | 'ir'>(''); // 默认选择广角 // 当前选中的摄像头 index const [selectedDroneCameraIdx, setSelectedDroneCameraIdx] = useState(0); const [droneStreamData, setDroneStreamData] = useState(null); const [stationStreamData, setStationStreamData] = useState(null); const [loadingDrone, setLoadingDrone] = useState(false); const [loadingStation, setLoadingStation] = useState(false); const switchingLock = useRef(false); // 加载机场摄像头 const loadStationCamera = async (cameraPosition: 'indoor' | 'outdoor') => { setSelectedCamera(cameraPosition); if (!device?.gateway_sn || switchingLock.current) return; switchingLock.current = true; setLoadingStation(true); try { const cameraIndex = device?.gateway_camera_list[0]?.camera_index || ""; // 目前先默认用第一个摄像头 if (!cameraIndex) { setStationStreamData(null); utils.message.warning(`未找到${cameraPosition === 'indoor' ? '室内' : '室外'}摄像头配置`); return; } const res = await changeCameragateway({ sn: device.gateway_sn, cameraIndex, cameraPosition, qualityType: "adaptive", videoExpire: 7200, }); if (res.code == 200) setStationStreamData(res.data); else utils.message.error(res.msg || '切换机场摄像头失败'); } catch (err) { console.error('切换机场摄像头失败', err); } finally { setLoadingStation(false); switchingLock.current = false; } }; // 加载无人机镜头(使用当前选中的摄像头索引) const loadDroneLens = async (lensType: 'wide' | 'zoom' | 'ir') => { setSelectedLens(lensType); if (!device?.device_sn || switchingLock.current) return; switchingLock.current = true; setLoadingDrone(true); try { const cameraList = device.drone_camera_list || []; const targetCamera = cameraList[selectedDroneCameraIdx]; const cameraIndex = targetCamera?.camera_index || ""; if (!cameraIndex) { return; } const res = await changeCameraPlane({ sn: device.device_sn, lensType, cameraIndex, qualityType: "adaptive", videoExpire: 7200, }); if (res.code === 200) setDroneStreamData(res.data); else utils.message.error(res.msg || '切换无人机镜头失败'); } catch (err) { console.error('切换无人机镜头失败', err); } finally { setLoadingDrone(false); switchingLock.current = false; } }; // 切换无人机摄像头 const switchDroneCamera = async (idx: number) => { setSelectedDroneCameraIdx(idx); // 切换摄像头后也需要调用接口加载当前选择的镜头 if (selectedLens) { await loadDroneLens(selectedLens); } }; // 用来记住上一次的值,判断哪一路发生了变化 const prevRef = useRef({ selectedCamera: '', selectedLens: '', }); useEffect(() => { if (!device?.device_sn && !device?.gateway_sn) return; const prev = prevRef.current; const currentCamera = selectedCamera; const currentLens = selectedLens; // 双画面:只刷新【真正变化】的那一路,另一路不动 if (dualVideo) { // 只有【机场摄像头】变化了,才刷新机场 if (currentCamera !== prev.selectedCamera) { setStationStreamData(null); loadStationCamera(currentCamera); } // 只有【无人机镜头】变化了,才刷新无人机 if (currentLens !== prev.selectedLens) { setDroneStreamData(null); loadDroneLens(currentLens); } // 保存最新值,供下一次对比 prevRef.current = { selectedCamera: currentCamera, selectedLens: currentLens, }; return; } // 单画面:保持原有逻辑 if (videoSource === 'station') { setStationStreamData(null); loadStationCamera(currentCamera); } else { setDroneStreamData(null); loadDroneLens(currentLens); } }, [ device, videoSource, selectedDroneCameraIdx, dualVideo, selectedCamera, selectedLens, ]); // 生成摄像头下拉选项 const droneCameraOptions = device?.drone_camera_list?.map((item: any, idx: number) => ({ label: item.camera_name || `摄像头 ${idx + 1}`, value: idx, })) || []; const renderVideo = (type: 'drone' | 'station', streamData: any, loading: boolean) => { const isDrone = type === 'drone'; return (
{/* 视频控制按钮 */}
{!isDrone ? ( <> ) : ( <> {droneCameraOptions.length > 1 && ( setVideoSource(val)} options={[ { label: '机场视频', value: 'station' }, { label: '无人机视频', value: 'drone' }, ]} />
)}
{dualVideo ? ( <> {renderVideo('drone', droneStreamData, loadingDrone)} {renderVideo('station', stationStreamData, loadingStation)} ) : ( videoSource === 'station' ? renderVideo('station', stationStreamData, loadingStation) : renderVideo('drone', droneStreamData, loadingDrone) )}
); }; export default DroneVideoPlayer;