306 lines
7.8 KiB
TypeScript
306 lines
7.8 KiB
TypeScript
|
|
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<DroneVideoPlayerProps> = ({
|
|||
|
|
device,
|
|||
|
|
height = 160,
|
|||
|
|
showSourceSelect = true,
|
|||
|
|
defaultSource = 'drone',
|
|||
|
|
videoSource: propsVideoSource,
|
|||
|
|
onSourceChange
|
|||
|
|
}) => {
|
|||
|
|
const [internalVideoSource, setInternalVideoSource] = useState<'station' | 'drone'>(defaultSource);
|
|||
|
|
|
|||
|
|
// 如果提供了 propsVideoSource,则使用受控值,否则使用内部状态
|
|||
|
|
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'>('wide');
|
|||
|
|
const [streamData, setStreamData] = useState<any>(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?.find(
|
|||
|
|
(item: any) => item.camera_position === cameraPosition
|
|||
|
|
)?.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;
|
|||
|
|
|
|||
|
|
// 如果已经是当前镜头,不重复请求(超级重要!)
|
|||
|
|
if (selectedLens === lensType && streamData) {
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
switchingLock.current = true;
|
|||
|
|
setLoading(true);
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
const cameraIndex = device.drone_camera_list?.[1]?.camera_index ||
|
|||
|
|
device.drone_camera_list?.[0]?.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;
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 自动加载默认画面
|
|||
|
|
useEffect(() => {
|
|||
|
|
if (device) {
|
|||
|
|
setStreamData(null);
|
|||
|
|
if (videoSource === 'station') {
|
|||
|
|
loadStationCamera(selectedCamera);
|
|||
|
|
} else {
|
|||
|
|
loadDroneLens(selectedLens);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}, [device, videoSource]);
|
|||
|
|
|
|||
|
|
return (
|
|||
|
|
<div style={{ width: '100%' }}>
|
|||
|
|
{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%',
|
|||
|
|
height,
|
|||
|
|
borderRadius: 8,
|
|||
|
|
position: 'relative',
|
|||
|
|
overflow: 'hidden',
|
|||
|
|
background: '#000'
|
|||
|
|
}}>
|
|||
|
|
{/* 切换按钮覆盖层 */}
|
|||
|
|
<div style={{
|
|||
|
|
position: 'absolute',
|
|||
|
|
top: '6px',
|
|||
|
|
right: '6px',
|
|||
|
|
display: 'flex',
|
|||
|
|
gap: '4px',
|
|||
|
|
zIndex: 10,
|
|||
|
|
}}>
|
|||
|
|
{videoSource === 'station' ? (
|
|||
|
|
<>
|
|||
|
|
<Button
|
|||
|
|
disabled={loading}
|
|||
|
|
size="small"
|
|||
|
|
type="text"
|
|||
|
|
style={{
|
|||
|
|
background: selectedCamera === 'indoor' ? '#1890ff' : 'rgba(0,0,0,0.4)',
|
|||
|
|
color: '#fff',
|
|||
|
|
fontSize: '12px',
|
|||
|
|
padding: '0 6px',
|
|||
|
|
height: '22px',
|
|||
|
|
borderRadius: '4px',
|
|||
|
|
}}
|
|||
|
|
onClick={() => loadStationCamera('indoor')}
|
|||
|
|
>
|
|||
|
|
室内
|
|||
|
|
</Button>
|
|||
|
|
<Button
|
|||
|
|
disabled={loading}
|
|||
|
|
size="small"
|
|||
|
|
type="text"
|
|||
|
|
style={{
|
|||
|
|
background: selectedCamera === 'outdoor' ? '#1890ff' : 'rgba(0,0,0,0.4)',
|
|||
|
|
color: '#fff',
|
|||
|
|
fontSize: '12px',
|
|||
|
|
padding: '0 6px',
|
|||
|
|
height: '22px',
|
|||
|
|
borderRadius: '4px',
|
|||
|
|
}}
|
|||
|
|
onClick={() => loadStationCamera('outdoor')}
|
|||
|
|
>
|
|||
|
|
室外
|
|||
|
|
</Button>
|
|||
|
|
</>
|
|||
|
|
) : (
|
|||
|
|
<>
|
|||
|
|
<Button
|
|||
|
|
disabled={loading}
|
|||
|
|
size="small"
|
|||
|
|
type="text"
|
|||
|
|
style={{
|
|||
|
|
background: selectedLens === 'wide' ? '#1890ff' : 'rgba(0,0,0,0.4)',
|
|||
|
|
color: '#fff',
|
|||
|
|
fontSize: '12px',
|
|||
|
|
padding: '0 6px',
|
|||
|
|
height: '22px',
|
|||
|
|
borderRadius: '4px',
|
|||
|
|
}}
|
|||
|
|
onClick={() => loadDroneLens('wide')}
|
|||
|
|
>
|
|||
|
|
广角
|
|||
|
|
</Button>
|
|||
|
|
<Button
|
|||
|
|
disabled={loading}
|
|||
|
|
size="small"
|
|||
|
|
type="text"
|
|||
|
|
style={{
|
|||
|
|
background: selectedLens === 'zoom' ? '#1890ff' : 'rgba(0,0,0,0.4)',
|
|||
|
|
color: '#fff',
|
|||
|
|
fontSize: '12px',
|
|||
|
|
padding: '0 6px',
|
|||
|
|
height: '22px',
|
|||
|
|
borderRadius: '4px',
|
|||
|
|
}}
|
|||
|
|
onClick={() => loadDroneLens('zoom')}
|
|||
|
|
>
|
|||
|
|
变焦
|
|||
|
|
</Button>
|
|||
|
|
<Button
|
|||
|
|
disabled={loading}
|
|||
|
|
size="small"
|
|||
|
|
type="text"
|
|||
|
|
style={{
|
|||
|
|
background: selectedLens === 'ir' ? '#1890ff' : 'rgba(0,0,0,0.4)',
|
|||
|
|
color: '#fff',
|
|||
|
|
fontSize: '12px',
|
|||
|
|
padding: '0 6px',
|
|||
|
|
height: '22px',
|
|||
|
|
borderRadius: '4px',
|
|||
|
|
}}
|
|||
|
|
onClick={() => loadDroneLens('ir')}
|
|||
|
|
>
|
|||
|
|
红外
|
|||
|
|
</Button>
|
|||
|
|
</>
|
|||
|
|
)}
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
{/* 视频内容 */}
|
|||
|
|
{streamData ? (
|
|||
|
|
<DroneLivePlayer streamData={streamData} />
|
|||
|
|
) : (
|
|||
|
|
<div style={{
|
|||
|
|
color: '#ccc',
|
|||
|
|
display: 'flex',
|
|||
|
|
height: '100%',
|
|||
|
|
alignItems: 'center',
|
|||
|
|
justifyContent: 'center',
|
|||
|
|
flexDirection: 'column',
|
|||
|
|
gap: 8
|
|||
|
|
}}>
|
|||
|
|
{loading ? <ReloadOutlined spin /> : null}
|
|||
|
|
<Text style={{ color: '#ccc' }}>{loading ? '视频加载中...' : '暂无视频信号'}</Text>
|
|||
|
|
</div>
|
|||
|
|
)}
|
|||
|
|
|
|||
|
|
{/* 底部信息叠加 */}
|
|||
|
|
{videoSource === 'drone' && device && (
|
|||
|
|
<div style={{
|
|||
|
|
position: 'absolute',
|
|||
|
|
bottom: 0,
|
|||
|
|
left: 0,
|
|||
|
|
right: 0,
|
|||
|
|
background: 'rgba(0,0,0,0.6)',
|
|||
|
|
color: '#fff',
|
|||
|
|
display: 'flex',
|
|||
|
|
justifyContent: 'space-between',
|
|||
|
|
padding: '8px 12px',
|
|||
|
|
fontSize: 12
|
|||
|
|
}}>
|
|||
|
|
<span>高度 {device.height?.toFixed(2) || '-'}m</span>
|
|||
|
|
<span>风速 {device.wind_speed?.toFixed(2) || '-'}m/s</span>
|
|||
|
|
</div>
|
|||
|
|
)}
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
);
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
export default DroneVideoPlayer;
|