Files
web-Iot/src/components/devices/DroneVideoPlayer.tsx

348 lines
10 KiB
TypeScript
Raw Normal View History

2026-05-25 10:30:20 +08:00
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;
2026-05-27 14:30:46 +08:00
dualVideo?: boolean;
2026-05-25 10:30:20 +08:00
defaultSource?: 'station' | 'drone';
2026-05-25 10:47:12 +08:00
videoSource?: 'station' | 'drone';
onSourceChange?: (source: 'station' | 'drone') => void;
2026-05-25 10:30:20 +08:00
}
const DroneVideoPlayer: React.FC<DroneVideoPlayerProps> = ({
device,
2026-05-25 10:30:20 +08:00
height = 160,
showSourceSelect = true,
2026-05-27 14:30:46 +08:00
dualVideo = false,
2026-05-25 10:30:20 +08:00
defaultSource = 'drone',
videoSource: propsVideoSource,
onSourceChange
}) => {
const [internalVideoSource, setInternalVideoSource] = useState<'station' | 'drone'>(defaultSource);
const videoSource = propsVideoSource !== undefined ? propsVideoSource : internalVideoSource;
const setVideoSource = (source: 'station' | 'drone') => {
2026-05-25 10:47:12 +08:00
if (onSourceChange) onSourceChange(source);
else setInternalVideoSource(source);
2026-05-25 10:30:20 +08:00
};
2026-05-25 10:47:12 +08:00
const [selectedCamera, setSelectedCamera] = useState<'indoor' | 'outdoor'>('indoor'); // 默认选择室内
const [selectedLens, setSelectedLens] = useState<'wide' | 'zoom' | 'ir'>(''); // 默认选择广角
2026-05-25 10:47:12 +08:00
// 当前选中的摄像头 index
const [selectedDroneCameraIdx, setSelectedDroneCameraIdx] = useState<number>(0);
2026-05-27 14:30:46 +08:00
const [droneStreamData, setDroneStreamData] = useState<any>(null);
const [stationStreamData, setStationStreamData] = useState<any>(null);
const [loadingDrone, setLoadingDrone] = useState(false);
const [loadingStation, setLoadingStation] = useState(false);
2026-05-25 10:30:20 +08:00
const switchingLock = useRef(false);
// 加载机场摄像头
const loadStationCamera = async (cameraPosition: 'indoor' | 'outdoor') => {
setSelectedCamera(cameraPosition);
if (!device?.gateway_sn || switchingLock.current) return;
switchingLock.current = true;
2026-05-27 14:30:46 +08:00
setLoadingStation(true);
2026-05-25 10:30:20 +08:00
try {
2026-05-25 11:08:45 +08:00
const cameraIndex = device?.gateway_camera_list[0]?.camera_index || ""; // 目前先默认用第一个摄像头
2026-05-25 10:30:20 +08:00
if (!cameraIndex) {
2026-05-27 14:30:46 +08:00
setStationStreamData(null);
2026-05-25 10:30:20 +08:00
utils.message.warning(`未找到${cameraPosition === 'indoor' ? '室内' : '室外'}摄像头配置`);
return;
}
const res = await changeCameragateway({
sn: device.gateway_sn,
cameraIndex,
cameraPosition,
qualityType: "adaptive",
videoExpire: 7200,
});
2026-05-27 14:30:46 +08:00
if (res.code == 200) setStationStreamData(res.data);
2026-05-25 10:47:12 +08:00
else utils.message.error(res.msg || '切换机场摄像头失败');
2026-05-25 10:30:20 +08:00
} catch (err) {
console.error('切换机场摄像头失败', err);
} finally {
2026-05-27 14:30:46 +08:00
setLoadingStation(false);
2026-05-25 10:30:20 +08:00
switchingLock.current = false;
}
};
2026-05-25 10:47:12 +08:00
// 加载无人机镜头(使用当前选中的摄像头索引)
2026-05-25 10:30:20 +08:00
const loadDroneLens = async (lensType: 'wide' | 'zoom' | 'ir') => {
setSelectedLens(lensType);
if (!device?.device_sn || switchingLock.current) return;
switchingLock.current = true;
2026-05-27 14:30:46 +08:00
setLoadingDrone(true);
2026-05-25 10:30:20 +08:00
try {
2026-05-25 10:47:12 +08:00
const cameraList = device.drone_camera_list || [];
const targetCamera = cameraList[selectedDroneCameraIdx];
const cameraIndex = targetCamera?.camera_index || "";
2026-05-25 10:30:20 +08:00
if (!cameraIndex) {
return;
}
const res = await changeCameraPlane({
sn: device.device_sn,
lensType,
cameraIndex,
qualityType: "adaptive",
videoExpire: 7200,
});
2026-05-27 14:30:46 +08:00
if (res.code === 200) setDroneStreamData(res.data);
2026-05-25 10:47:12 +08:00
else utils.message.error(res.msg || '切换无人机镜头失败');
2026-05-25 10:30:20 +08:00
} catch (err) {
console.error('切换无人机镜头失败', err);
} finally {
2026-05-27 14:30:46 +08:00
setLoadingDrone(false);
2026-05-25 10:30:20 +08:00
switchingLock.current = false;
}
};
2026-05-25 10:47:12 +08:00
// 切换无人机摄像头
const switchDroneCamera = async (idx: number) => {
2026-05-25 10:47:12 +08:00
setSelectedDroneCameraIdx(idx);
// 切换摄像头后也需要调用接口加载当前选择的镜头
if (selectedLens) {
await loadDroneLens(selectedLens);
}
2026-05-25 10:47:12 +08:00
};
// 用来记住上一次的值,判断哪一路发生了变化
const prevRef = useRef({
selectedCamera: '',
selectedLens: '',
});
2026-05-25 10:47:12 +08:00
2026-05-25 10:30:20 +08:00
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) {
2026-05-27 14:30:46 +08:00
setStationStreamData(null);
loadStationCamera(currentCamera);
}
// 只有【无人机镜头】变化了,才刷新无人机
if (currentLens !== prev.selectedLens) {
setDroneStreamData(null);
loadDroneLens(currentLens);
2026-05-25 10:30:20 +08:00
}
// 保存最新值,供下一次对比
prevRef.current = {
selectedCamera: currentCamera,
selectedLens: currentLens,
};
return;
}
// 单画面:保持原有逻辑
if (videoSource === 'station') {
setStationStreamData(null);
loadStationCamera(currentCamera);
} else {
setDroneStreamData(null);
loadDroneLens(currentLens);
2026-05-25 10:30:20 +08:00
}
}, [
device,
videoSource,
selectedDroneCameraIdx,
dualVideo,
selectedCamera,
selectedLens,
]);
2026-05-25 10:47:12 +08:00
// 生成摄像头下拉选项
const droneCameraOptions = device?.drone_camera_list?.map((item: any, idx: number) => ({
label: item.camera_name || `摄像头 ${idx + 1}`,
value: idx,
})) || [];
2026-05-25 10:30:20 +08:00
2026-05-27 14:30:46 +08:00
const renderVideo = (type: 'drone' | 'station', streamData: any, loading: boolean) => {
const isDrone = type === 'drone';
return (
2026-05-25 10:30:20 +08:00
<div style={{
2026-05-27 14:30:46 +08:00
flex: 1,
height: height,
2026-05-25 10:30:20 +08:00
borderRadius: 8,
position: 'relative',
overflow: 'hidden',
2026-05-27 14:30:46 +08:00
background: '#000',
border: dualVideo ? '1px solid #243157' : 'none',
minWidth: dualVideo ? '300px' : 'auto' // 确保在双视频模式下有最小宽度
}} key={type}>
2026-05-25 10:47:12 +08:00
{/* 视频控制按钮 */}
2026-05-25 10:30:20 +08:00
<div style={{
position: 'absolute',
top: '6px',
right: '6px',
display: 'flex',
gap: '4px',
zIndex: 10,
2026-05-25 10:47:12 +08:00
flexWrap: 'wrap',
2026-05-25 10:30:20 +08:00
}}>
2026-05-27 14:30:46 +08:00
{!isDrone ? (
2026-05-25 10:30:20 +08:00
<>
2026-05-25 10:47:12 +08:00
<Button disabled={loading} size="small" type="text" style={{
background: selectedCamera === 'indoor' ? '#1890ff' : 'rgba(0,0,0,0.4)',
2026-05-27 14:30:46 +08:00
color: '#fff', fontSize: 10, padding: '0 4px', height: 20, borderRadius: 4
2026-05-25 10:47:12 +08:00
}} onClick={() => loadStationCamera('indoor')}>室内</Button>
<Button disabled={loading} size="small" type="text" style={{
background: selectedCamera === 'outdoor' ? '#1890ff' : 'rgba(0,0,0,0.4)',
2026-05-27 14:30:46 +08:00
color: '#fff', fontSize: 10, padding: '0 4px', height: 20, borderRadius: 4
2026-05-25 10:47:12 +08:00
}} onClick={() => loadStationCamera('outdoor')}>室外</Button>
2026-05-25 10:30:20 +08:00
</>
) : (
<>
2026-05-27 14:30:46 +08:00
{droneCameraOptions.length > 1 && (
2026-05-25 10:47:12 +08:00
<Select
disabled={loading}
size="small"
2026-05-27 14:30:46 +08:00
style={{ width: 80, fontSize: 10 }}
2026-05-25 10:47:12 +08:00
value={selectedDroneCameraIdx}
onChange={switchDroneCamera}
options={droneCameraOptions}
status={loading ? 'warning' : undefined}
/>
)}
<Button disabled={loading} size="small" type="text" style={{
background: selectedLens === 'wide' ? '#1890ff' : 'rgba(0,0,0,0.4)',
2026-05-27 14:30:46 +08:00
color: '#fff', fontSize: 10, padding: '0 4px', height: 20, borderRadius: 4
2026-05-25 10:47:12 +08:00
}} onClick={() => loadDroneLens('wide')}>广角</Button>
<Button disabled={loading} size="small" type="text" style={{
background: selectedLens === 'zoom' ? '#1890ff' : 'rgba(0,0,0,0.4)',
2026-05-27 14:30:46 +08:00
color: '#fff', fontSize: 10, padding: '0 4px', height: 20, borderRadius: 4
2026-05-25 10:47:12 +08:00
}} onClick={() => loadDroneLens('zoom')}>变焦</Button>
<Button disabled={loading} size="small" type="text" style={{
background: selectedLens === 'ir' ? '#1890ff' : 'rgba(0,0,0,0.4)',
2026-05-27 14:30:46 +08:00
color: '#fff', fontSize: 10, padding: '0 4px', height: 20, borderRadius: 4
2026-05-25 10:47:12 +08:00
}} onClick={() => loadDroneLens('ir')}>红外</Button>
2026-05-25 10:30:20 +08:00
</>
)}
</div>
{/* 视频内容 */}
{streamData ? (
<DroneLivePlayer streamData={streamData}
/>
2026-05-25 10:30:20 +08:00
) : (
<div style={{
color: '#ccc',
display: 'flex',
height: '100%',
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'column',
gap: 8
}}>
2026-05-25 14:39:05 +08:00
{loading ? <ReloadOutlined spin style={{ color: '#fff' }} /> : null}
<span style={{ color: '#fff', fontSize: 12 }}>
{loading ? '视频加载中...' : '暂无视频信号'}
</span>
2026-05-25 10:30:20 +08:00
</div>
)}
2026-05-27 15:08:26 +08:00
{showSourceSelect && (
<div style={{
position: 'absolute',
top: 6,
left: 6,
background: 'rgba(0,0,0,0.5)',
color: '#fff',
padding: '2px 6px',
borderRadius: 4,
fontSize: 10,
zIndex: 5
}}>
{isDrone ? '无人机视频' : '机场视频'}
</div>
)}
2026-05-27 14:30:46 +08:00
2026-05-25 10:30:20 +08:00
{/* 底部信息叠加 */}
2026-05-27 14:30:46 +08:00
{isDrone && device && (
2026-05-25 10:30:20 +08:00
<div style={{
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
background: 'rgba(0,0,0,0.6)',
color: '#fff',
display: 'flex',
justifyContent: 'space-between',
2026-05-27 14:30:46 +08:00
padding: '4px 8px',
fontSize: 10
2026-05-25 10:30:20 +08:00
}}>
{/*<span>高度 {Number(device?.height)?.toFixed(2) || '-'}m</span>
<span>风速 {Number(device?.wind_speed?.toFixed(2) || '0')}m/s</span>*/}
2026-05-25 10:30:20 +08:00
</div>
)}
</div>
2026-05-27 14:30:46 +08:00
);
};
return (
<div style={{ width: '100%', height: '100%', display: 'flex', flexDirection: 'column' }}>
{!dualVideo && showSourceSelect && (
<div style={{ display: 'flex', justifyContent: 'flex-end', marginBottom: 8 }}>
<Select
size="small"
style={{ width: 120 }}
value={videoSource}
onChange={(val) => setVideoSource(val)}
options={[
{ label: '机场视频', value: 'station' },
{ label: '无人机视频', value: 'drone' },
]}
/>
</div>
)}
<div style={{
width: '100%',
flex: 1,
display: 'flex',
flexDirection: 'row',
gap: dualVideo ? 8 : 0
}}>
{dualVideo ? (
<>
{renderVideo('drone', droneStreamData, loadingDrone)}
{renderVideo('station', stationStreamData, loadingStation)}
</>
) : (
videoSource === 'station'
? renderVideo('station', stationStreamData, loadingStation)
: renderVideo('drone', droneStreamData, loadingDrone)
)}
</div>
2026-05-25 10:30:20 +08:00
</div>
);
};
export default DroneVideoPlayer;