fix:设备监控新增视频流功能
This commit is contained in:
@@ -18,6 +18,7 @@ import {
|
||||
import { device, getStreamUrls } from '@/services/device/api';
|
||||
import { request } from 'umi';
|
||||
import { Amap } from '@amap/amap-react';
|
||||
import ReactPlayer from 'react-player';
|
||||
|
||||
const { RangePicker } = DatePicker;
|
||||
const { TabPane } = Tabs;
|
||||
@@ -424,10 +425,14 @@ const DeviceMonitor: React.FC = () => {
|
||||
const [videoFullscreen, setVideoFullscreen] = useState(false);
|
||||
const [videoQuality, setVideoQuality] = useState('auto');
|
||||
const [allStreamUrls, setAllStreamUrls] = useState<{
|
||||
pushUrl: string;
|
||||
playFlvUrl: string;
|
||||
playM3u8Url: string;
|
||||
playRtmpUrl: string;
|
||||
} | null>(null);
|
||||
const [currentStreamType, setCurrentStreamType] = useState<'hls' | 'flv' | 'rtmp'>('hls');
|
||||
const [useReactPlayer, setUseReactPlayer] = useState(true);
|
||||
const [videoStreamStatus, setVideoStreamStatus] = useState<'loading' | 'playing' | 'error' | 'ended' | 'stalled'>('loading');
|
||||
|
||||
const tokenCache = {
|
||||
value: '',
|
||||
@@ -1937,24 +1942,28 @@ const DeviceMonitor: React.FC = () => {
|
||||
|
||||
console.log('视频流API响应:', response);
|
||||
|
||||
if (response && response.data) {
|
||||
const { playFlvUrl, playM3u8Url, playRtmpUrl } = response.data;
|
||||
console.log('获取到视频流地址:', { playFlvUrl, playM3u8Url, playRtmpUrl });
|
||||
if (response && response.resp_code === 0 && response.data) {
|
||||
const { pushUrl, playFlvUrl, playM3u8Url, playRtmpUrl } = response.data;
|
||||
console.log('获取到视频流地址:', { pushUrl, playFlvUrl, playM3u8Url, playRtmpUrl });
|
||||
|
||||
// 保存所有流URL
|
||||
setAllStreamUrls({ playFlvUrl, playM3u8Url, playRtmpUrl });
|
||||
setAllStreamUrls({ pushUrl, playFlvUrl, playM3u8Url, playRtmpUrl });
|
||||
|
||||
// 优先使用HLS流,然后是FLV,最后是RTMP
|
||||
const streamUrl = playM3u8Url || playFlvUrl || playRtmpUrl;
|
||||
|
||||
if (streamUrl) {
|
||||
console.log('选择的流URL:', streamUrl);
|
||||
console.log('流类型:', streamUrl.includes('.m3u8') ? 'HLS' :
|
||||
streamUrl.includes('.flv') ? 'FLV' :
|
||||
streamUrl.startsWith('rtmp://') ? 'RTMP' : '未知');
|
||||
setVideoStreamUrl(streamUrl);
|
||||
return streamUrl;
|
||||
} else {
|
||||
throw new Error('未找到可用的视频流地址');
|
||||
}
|
||||
} else {
|
||||
throw new Error('获取视频流地址失败');
|
||||
throw new Error(String(response?.resp_msg || '获取视频流地址失败'));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取视频流地址失败:', error);
|
||||
@@ -1976,6 +1985,8 @@ const DeviceMonitor: React.FC = () => {
|
||||
setVideoStreamVisible(true);
|
||||
setVideoError('');
|
||||
setVideoStreamUrl('');
|
||||
setAllStreamUrls(null);
|
||||
setCurrentStreamType('hls');
|
||||
|
||||
// 获取视频流地址
|
||||
const streamUrl = await getVideoStreamUrl(device.deviceSn);
|
||||
@@ -1994,6 +2005,10 @@ const DeviceMonitor: React.FC = () => {
|
||||
setVideoStreamUrl('');
|
||||
setVideoError('');
|
||||
setVideoLoading(false);
|
||||
setAllStreamUrls(null);
|
||||
setCurrentStreamType('hls');
|
||||
setUseReactPlayer(true); // 重置为ReactPlayer
|
||||
setVideoStreamStatus('loading'); // 重置状态
|
||||
};
|
||||
|
||||
// 重新加载视频流
|
||||
@@ -2001,6 +2016,8 @@ const DeviceMonitor: React.FC = () => {
|
||||
if (currentVideoDevice) {
|
||||
setVideoError('');
|
||||
setVideoStreamUrl('');
|
||||
setAllStreamUrls(null);
|
||||
setCurrentStreamType('hls');
|
||||
const streamUrl = await getVideoStreamUrl(currentVideoDevice.deviceSn);
|
||||
if (streamUrl) {
|
||||
setVideoStreamUrl(streamUrl);
|
||||
@@ -2009,6 +2026,31 @@ const DeviceMonitor: React.FC = () => {
|
||||
}
|
||||
};
|
||||
|
||||
// 切换流格式
|
||||
const handleSwitchStreamType = (type: 'hls' | 'flv' | 'rtmp') => {
|
||||
if (!allStreamUrls) return;
|
||||
|
||||
let newUrl = '';
|
||||
switch (type) {
|
||||
case 'hls':
|
||||
newUrl = allStreamUrls.playM3u8Url;
|
||||
break;
|
||||
case 'flv':
|
||||
newUrl = allStreamUrls.playFlvUrl;
|
||||
break;
|
||||
case 'rtmp':
|
||||
newUrl = allStreamUrls.playRtmpUrl;
|
||||
break;
|
||||
}
|
||||
|
||||
if (newUrl) {
|
||||
setCurrentStreamType(type);
|
||||
setVideoStreamUrl(newUrl);
|
||||
setVideoPlayerKey(prev => prev + 1);
|
||||
console.log(`切换到${type}格式:`, newUrl);
|
||||
}
|
||||
};
|
||||
|
||||
// 截图功能
|
||||
const handleScreenshot = () => {
|
||||
const video = document.querySelector('video');
|
||||
@@ -2063,35 +2105,13 @@ const DeviceMonitor: React.FC = () => {
|
||||
const renderVideoPlayer = () => {
|
||||
if (!videoStreamUrl) return null;
|
||||
|
||||
// 判断视频流类型
|
||||
const isHLS = videoStreamUrl.includes('.m3u8') || videoStreamUrl.includes('application/x-mpegURL');
|
||||
const isRTMP = videoStreamUrl.startsWith('rtmp://');
|
||||
const isWebRTC = videoStreamUrl.startsWith('webrtc://');
|
||||
console.log('渲染视频播放器,URL:', videoStreamUrl);
|
||||
console.log('当前流类型:', currentStreamType);
|
||||
console.log('ReactPlayer组件:', ReactPlayer);
|
||||
|
||||
if (isHLS) {
|
||||
// HLS流播放器
|
||||
return (
|
||||
<video
|
||||
key={videoPlayerKey}
|
||||
controls
|
||||
autoPlay
|
||||
muted
|
||||
style={{
|
||||
width: '100%',
|
||||
maxHeight: '500px',
|
||||
backgroundColor: '#000'
|
||||
}}
|
||||
onError={(e) => {
|
||||
console.error('HLS视频播放错误:', e);
|
||||
setVideoError('HLS视频播放失败,请检查网络连接');
|
||||
}}
|
||||
>
|
||||
<source src={videoStreamUrl} type="application/x-mpegURL" />
|
||||
您的浏览器不支持HLS视频播放
|
||||
</video>
|
||||
);
|
||||
} else if (isRTMP) {
|
||||
// RTMP流播放器(需要Flash或特殊播放器)
|
||||
// 检查ReactPlayer是否可用
|
||||
if (!ReactPlayer) {
|
||||
console.error('ReactPlayer组件未加载');
|
||||
return (
|
||||
<div style={{
|
||||
width: '100%',
|
||||
@@ -2102,17 +2122,108 @@ const DeviceMonitor: React.FC = () => {
|
||||
justifyContent: 'center',
|
||||
color: '#fff'
|
||||
}}>
|
||||
<div>
|
||||
<div>RTMP流地址: {videoStreamUrl}</div>
|
||||
<div style={{ marginTop: 8, fontSize: '12px' }}>
|
||||
需要特殊的RTMP播放器支持
|
||||
</div>
|
||||
</div>
|
||||
<div>ReactPlayer组件未加载,请检查依赖</div>
|
||||
</div>
|
||||
);
|
||||
} else if (isWebRTC) {
|
||||
// WebRTC流播放器
|
||||
return (
|
||||
}
|
||||
|
||||
// 使用ReactPlayer组件,支持多种流格式
|
||||
return (
|
||||
<div style={{
|
||||
width: '100%',
|
||||
maxHeight: '500px',
|
||||
backgroundColor: '#000',
|
||||
position: 'relative'
|
||||
}}>
|
||||
<ReactPlayer
|
||||
key={videoPlayerKey}
|
||||
url={videoStreamUrl}
|
||||
controls={true}
|
||||
playing={true}
|
||||
muted={true}
|
||||
width="100%"
|
||||
height="100%"
|
||||
style={{ maxHeight: '500px' }}
|
||||
onError={(error: any) => {
|
||||
console.error('ReactPlayer播放错误:', error);
|
||||
setVideoStreamStatus('error');
|
||||
// 根据错误类型显示不同的错误信息
|
||||
if (error && error.message) {
|
||||
if (error.message.includes('404') || error.message.includes('not found')) {
|
||||
setVideoError('视频流地址不存在,请检查设备是否在线并开始推流');
|
||||
} else if (error.message.includes('timeout') || error.message.includes('time out')) {
|
||||
setVideoError('视频流连接超时,设备可能未开始推流');
|
||||
} else if (error.message.includes('network') || error.message.includes('connection')) {
|
||||
setVideoError('网络连接失败,请检查网络状态');
|
||||
} else {
|
||||
setVideoError(`视频播放失败: ${error.message}`);
|
||||
}
|
||||
} else {
|
||||
setVideoError('视频播放失败,设备可能未开始推流');
|
||||
}
|
||||
}}
|
||||
onReady={() => {
|
||||
console.log('ReactPlayer准备就绪');
|
||||
setVideoError('');
|
||||
setVideoStreamStatus('playing');
|
||||
}}
|
||||
onStart={() => {
|
||||
console.log('ReactPlayer开始播放');
|
||||
setVideoError('');
|
||||
setVideoStreamStatus('playing');
|
||||
}}
|
||||
onBuffer={() => {
|
||||
console.log('ReactPlayer缓冲中');
|
||||
setVideoStreamStatus('loading');
|
||||
}}
|
||||
onEnded={() => {
|
||||
console.log('ReactPlayer播放结束');
|
||||
setVideoStreamStatus('ended');
|
||||
setVideoError('视频流播放结束,设备可能已停止推流');
|
||||
}}
|
||||
config={{
|
||||
file: {
|
||||
attributes: {
|
||||
style: {
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
},
|
||||
},
|
||||
forceVideo: true,
|
||||
forceHLS: currentStreamType === 'hls',
|
||||
hlsOptions: {
|
||||
maxBufferLength: 30,
|
||||
maxMaxBufferLength: 60,
|
||||
maxBufferSize: 0,
|
||||
maxBufferHole: 0.5,
|
||||
// 添加重试配置
|
||||
maxLoadingDelay: 4,
|
||||
lowLatencyMode: false,
|
||||
backBufferLength: 90,
|
||||
// 禁用自动重试,避免无限重试
|
||||
enableWorker: true,
|
||||
debug: false,
|
||||
},
|
||||
},
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
// 备用播放器(如果ReactPlayer不工作)
|
||||
const renderFallbackPlayer = () => {
|
||||
if (!videoStreamUrl) return null;
|
||||
|
||||
console.log('使用备用播放器,URL:', videoStreamUrl);
|
||||
|
||||
return (
|
||||
<div style={{
|
||||
width: '100%',
|
||||
maxHeight: '500px',
|
||||
backgroundColor: '#000',
|
||||
position: 'relative'
|
||||
}}>
|
||||
<video
|
||||
key={videoPlayerKey}
|
||||
controls
|
||||
@@ -2124,38 +2235,55 @@ const DeviceMonitor: React.FC = () => {
|
||||
backgroundColor: '#000'
|
||||
}}
|
||||
onError={(e) => {
|
||||
console.error('WebRTC视频播放错误:', e);
|
||||
setVideoError('WebRTC视频播放失败,请检查网络连接');
|
||||
console.error('备用播放器错误:', e);
|
||||
setVideoStreamStatus('error');
|
||||
const target = e.target as HTMLVideoElement;
|
||||
const error = target.error;
|
||||
if (error) {
|
||||
switch (error.code) {
|
||||
case MediaError.MEDIA_ERR_NETWORK:
|
||||
setVideoError('网络错误,设备可能未开始推流');
|
||||
break;
|
||||
case MediaError.MEDIA_ERR_DECODE:
|
||||
setVideoError('视频解码错误,流格式可能不支持');
|
||||
break;
|
||||
case MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED:
|
||||
setVideoError('视频源不支持,请检查流地址');
|
||||
break;
|
||||
default:
|
||||
setVideoError('视频播放失败,设备可能未开始推流');
|
||||
}
|
||||
} else {
|
||||
setVideoError('视频播放失败,请检查网络连接');
|
||||
}
|
||||
}}
|
||||
>
|
||||
您的浏览器不支持WebRTC视频播放
|
||||
</video>
|
||||
);
|
||||
} else {
|
||||
// 通用视频播放器
|
||||
return (
|
||||
<video
|
||||
key={videoPlayerKey}
|
||||
controls
|
||||
autoPlay
|
||||
muted
|
||||
style={{
|
||||
width: '100%',
|
||||
maxHeight: '500px',
|
||||
backgroundColor: '#000'
|
||||
onLoadStart={() => {
|
||||
console.log('备用播放器开始加载');
|
||||
setVideoStreamStatus('loading');
|
||||
}}
|
||||
onError={(e) => {
|
||||
console.error('视频播放错误:', e);
|
||||
setVideoError('视频播放失败,请检查网络连接');
|
||||
onCanPlay={() => {
|
||||
console.log('备用播放器可以播放');
|
||||
setVideoError('');
|
||||
setVideoStreamStatus('playing');
|
||||
}}
|
||||
onEnded={() => {
|
||||
console.log('备用播放器播放结束');
|
||||
setVideoStreamStatus('ended');
|
||||
setVideoError('视频流播放结束,设备可能已停止推流');
|
||||
}}
|
||||
onStalled={() => {
|
||||
console.log('备用播放器停滞');
|
||||
setVideoStreamStatus('stalled');
|
||||
setVideoError('视频流停滞,设备可能已停止推流');
|
||||
}}
|
||||
>
|
||||
<source src={videoStreamUrl} type="application/x-mpegURL" />
|
||||
<source src={videoStreamUrl} type="video/mp4" />
|
||||
<source src={videoStreamUrl} type="video/webm" />
|
||||
<source src={videoStreamUrl} type="video/ogg" />
|
||||
您的浏览器不支持视频播放
|
||||
</video>
|
||||
);
|
||||
}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
// 解析机场状态数据
|
||||
@@ -3116,8 +3244,29 @@ const DeviceMonitor: React.FC = () => {
|
||||
|
||||
{videoStreamUrl && !videoLoading && !videoError && (
|
||||
<div style={{ position: 'relative' }}>
|
||||
{/* 使用新的视频播放器组件 */}
|
||||
{renderVideoPlayer()}
|
||||
{/* 播放器切换按钮 */}
|
||||
<div style={{ marginBottom: 16, textAlign: 'left' }}>
|
||||
<Space>
|
||||
<Text>播放器类型:</Text>
|
||||
<Button
|
||||
size="small"
|
||||
type={useReactPlayer ? 'primary' : 'default'}
|
||||
onClick={() => setUseReactPlayer(true)}
|
||||
>
|
||||
ReactPlayer
|
||||
</Button>
|
||||
<Button
|
||||
size="small"
|
||||
type={!useReactPlayer ? 'primary' : 'default'}
|
||||
onClick={() => setUseReactPlayer(false)}
|
||||
>
|
||||
原生播放器
|
||||
</Button>
|
||||
</Space>
|
||||
</div>
|
||||
|
||||
{/* 使用选择的播放器组件 */}
|
||||
{useReactPlayer ? renderVideoPlayer() : renderFallbackPlayer()}
|
||||
|
||||
{/* 视频流信息 */}
|
||||
<div style={{ marginTop: 16, textAlign: 'left' }}>
|
||||
@@ -3135,9 +3284,24 @@ const DeviceMonitor: React.FC = () => {
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="流类型">
|
||||
<Tag color="blue">
|
||||
{videoStreamUrl.includes('.m3u8') ? 'HLS' :
|
||||
videoStreamUrl.startsWith('rtmp://') ? 'RTMP' :
|
||||
videoStreamUrl.startsWith('webrtc://') ? 'WebRTC' : 'HTTP'}
|
||||
{currentStreamType === 'hls' ? 'HLS' :
|
||||
currentStreamType === 'flv' ? 'FLV' :
|
||||
currentStreamType === 'rtmp' ? 'RTMP' : '未知'}
|
||||
</Tag>
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="播放状态">
|
||||
<Tag color={
|
||||
videoStreamStatus === 'playing' ? 'green' :
|
||||
videoStreamStatus === 'loading' ? 'blue' :
|
||||
videoStreamStatus === 'error' ? 'red' :
|
||||
videoStreamStatus === 'ended' ? 'orange' :
|
||||
videoStreamStatus === 'stalled' ? 'yellow' : 'default'
|
||||
}>
|
||||
{videoStreamStatus === 'playing' ? '播放中' :
|
||||
videoStreamStatus === 'loading' ? '加载中' :
|
||||
videoStreamStatus === 'error' ? '错误' :
|
||||
videoStreamStatus === 'ended' ? '已结束' :
|
||||
videoStreamStatus === 'stalled' ? '停滞' : '未知'}
|
||||
</Tag>
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="视频质量">
|
||||
@@ -3153,11 +3317,77 @@ const DeviceMonitor: React.FC = () => {
|
||||
<Select.Option value="low">流畅</Select.Option>
|
||||
</Select>
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="流格式切换">
|
||||
<Space>
|
||||
<Button
|
||||
size="small"
|
||||
type={currentStreamType === 'hls' ? 'primary' : 'default'}
|
||||
onClick={() => handleSwitchStreamType('hls')}
|
||||
disabled={!allStreamUrls?.playM3u8Url}
|
||||
>
|
||||
HLS
|
||||
</Button>
|
||||
<Button
|
||||
size="small"
|
||||
type={currentStreamType === 'flv' ? 'primary' : 'default'}
|
||||
onClick={() => handleSwitchStreamType('flv')}
|
||||
disabled={!allStreamUrls?.playFlvUrl}
|
||||
>
|
||||
FLV
|
||||
</Button>
|
||||
<Button
|
||||
size="small"
|
||||
type={currentStreamType === 'rtmp' ? 'primary' : 'default'}
|
||||
onClick={() => handleSwitchStreamType('rtmp')}
|
||||
disabled={!allStreamUrls?.playRtmpUrl}
|
||||
>
|
||||
RTMP
|
||||
</Button>
|
||||
</Space>
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="流地址" span={2}>
|
||||
<Text code style={{ fontSize: '12px' }}>
|
||||
{videoStreamUrl}
|
||||
</Text>
|
||||
</Descriptions.Item>
|
||||
{allStreamUrls && (
|
||||
<Descriptions.Item label="可用流格式" span={2}>
|
||||
<Space direction="vertical" size="small" style={{ width: '100%' }}>
|
||||
{allStreamUrls.pushUrl && (
|
||||
<div>
|
||||
<Tag color="red">推流地址</Tag>
|
||||
<Text code style={{ fontSize: '11px' }}>
|
||||
{allStreamUrls.pushUrl}
|
||||
</Text>
|
||||
</div>
|
||||
)}
|
||||
{allStreamUrls.playM3u8Url && (
|
||||
<div>
|
||||
<Tag color="green">HLS播放</Tag>
|
||||
<Text code style={{ fontSize: '11px' }}>
|
||||
{allStreamUrls.playM3u8Url}
|
||||
</Text>
|
||||
</div>
|
||||
)}
|
||||
{allStreamUrls.playFlvUrl && (
|
||||
<div>
|
||||
<Tag color="blue">FLV播放</Tag>
|
||||
<Text code style={{ fontSize: '11px' }}>
|
||||
{allStreamUrls.playFlvUrl}
|
||||
</Text>
|
||||
</div>
|
||||
)}
|
||||
{allStreamUrls.playRtmpUrl && (
|
||||
<div>
|
||||
<Tag color="orange">RTMP播放</Tag>
|
||||
<Text code style={{ fontSize: '11px' }}>
|
||||
{allStreamUrls.playRtmpUrl}
|
||||
</Text>
|
||||
</div>
|
||||
)}
|
||||
</Space>
|
||||
</Descriptions.Item>
|
||||
)}
|
||||
</Descriptions>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user