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'; const { Text } = Typography; interface DroneVideoPlayerProps { device: any; height?: number | string; showSourceSelect?: boolean; defaultSource?: 'station' | 'drone'; videoSource?: 'station' | 'drone'; onSourceChange?: (source: 'station' | 'drone') => void; } const DroneVideoPlayer: React.FC = ({ device, height = 160, showSourceSelect = true, 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'>('indoor'); const [selectedLens, setSelectedLens] = useState<'wide' | 'zoom' | 'ir'>('wide'); // 当前选中的摄像头 index const [selectedDroneCameraIdx, setSelectedDroneCameraIdx] = useState(0); const [streamData, setStreamData] = useState(null); const [loading, setLoading] = useState(false); const switchingLock = useRef(false); // 加载机场摄像头 const loadStationCamera = async (cameraPosition: 'indoor' | 'outdoor') => { setSelectedCamera(cameraPosition); if (!device?.gateway_sn || switchingLock.current) return; switchingLock.current = true; setLoading(true); try { const cameraIndex = device?.gateway_camera_list[0]?.camera_index || ""; // 目前先默认用第一个摄像头 if (!cameraIndex) { setStreamData(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) setStreamData(res.data); else utils.message.error(res.msg || '切换机场摄像头失败'); } catch (err) { console.error('切换机场摄像头失败', err); } finally { setLoading(false); switchingLock.current = false; } }; // 加载无人机镜头(使用当前选中的摄像头索引) const loadDroneLens = async (lensType: 'wide' | 'zoom' | 'ir') => { setSelectedLens(lensType); if (!device?.device_sn || switchingLock.current) return; switchingLock.current = true; setLoading(true); try { const cameraList = device.drone_camera_list || []; const targetCamera = cameraList[selectedDroneCameraIdx]; const cameraIndex = targetCamera?.camera_index || ""; if (!cameraIndex) { utils.message.warning('未找到无人机相机配置'); return; } const res = await changeCameraPlane({ sn: device.device_sn, lensType, cameraIndex, qualityType: "adaptive", videoExpire: 7200, }); if (res.code === 200) setStreamData(res.data); else utils.message.error(res.msg || '切换无人机镜头失败'); } catch (err) { console.error('切换无人机镜头失败', err); } finally { setLoading(false); switchingLock.current = false; } }; // 切换无人机摄像头 const switchDroneCamera = (idx: number) => { setSelectedDroneCameraIdx(idx); }; // 自动加载 useEffect(() => { if (device) { setStreamData(null); if (videoSource === 'station') { loadStationCamera(selectedCamera); } else { loadDroneLens(selectedLens); } } }, [device, videoSource, selectedDroneCameraIdx]); // 生成摄像头下拉选项 const droneCameraOptions = device?.drone_camera_list?.map((item: any, idx: number) => ({ label: item.camera_name || `摄像头 ${idx + 1}`, value: idx, })) || []; return (
{showSourceSelect && (
)} )}
{/* 视频内容 */} {streamData ? ( ) : (
{loading ? : null} {loading ? '视频加载中...' : '暂无视频信号'}
)} {/* 底部信息叠加 */} {videoSource === 'drone' && device && (
高度 {device.height?.toFixed(2) || '-'}m 风速 {device.wind_speed?.toFixed(2) || '-'}m/s
)}
); }; export default DroneVideoPlayer;