diff --git a/zlt-web/react-web/src/main/frontend/src/pages/device-ops/monitor/index.tsx b/zlt-web/react-web/src/main/frontend/src/pages/device-ops/monitor/index.tsx index 61a32194..f4f39aeb 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/device-ops/monitor/index.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/device-ops/monitor/index.tsx @@ -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 ( - - ); - } else if (isRTMP) { - // RTMP流播放器(需要Flash或特殊播放器) + // 检查ReactPlayer是否可用 + if (!ReactPlayer) { + console.error('ReactPlayer组件未加载'); return (
{ justifyContent: 'center', color: '#fff' }}> -
-
RTMP流地址: {videoStreamUrl}
-
- 需要特殊的RTMP播放器支持 -
-
+
ReactPlayer组件未加载,请检查依赖
); - } else if (isWebRTC) { - // WebRTC流播放器 - return ( + } + + // 使用ReactPlayer组件,支持多种流格式 + return ( +
+ { + 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, + }, + }, + }} + /> +
+ ); + }; + + // 备用播放器(如果ReactPlayer不工作) + const renderFallbackPlayer = () => { + if (!videoStreamUrl) return null; + + console.log('使用备用播放器,URL:', videoStreamUrl); + + return ( +
- ); - } else { - // 通用视频播放器 - return ( - - ); - } +
+ ); }; // 解析机场状态数据 @@ -3116,8 +3244,29 @@ const DeviceMonitor: React.FC = () => { {videoStreamUrl && !videoLoading && !videoError && (
- {/* 使用新的视频播放器组件 */} - {renderVideoPlayer()} + {/* 播放器切换按钮 */} +
+ + 播放器类型: + + + +
+ + {/* 使用选择的播放器组件 */} + {useReactPlayer ? renderVideoPlayer() : renderFallbackPlayer()} {/* 视频流信息 */}
@@ -3135,9 +3284,24 @@ const DeviceMonitor: React.FC = () => { - {videoStreamUrl.includes('.m3u8') ? 'HLS' : - videoStreamUrl.startsWith('rtmp://') ? 'RTMP' : - videoStreamUrl.startsWith('webrtc://') ? 'WebRTC' : 'HTTP'} + {currentStreamType === 'hls' ? 'HLS' : + currentStreamType === 'flv' ? 'FLV' : + currentStreamType === 'rtmp' ? 'RTMP' : '未知'} + + + + + {videoStreamStatus === 'playing' ? '播放中' : + videoStreamStatus === 'loading' ? '加载中' : + videoStreamStatus === 'error' ? '错误' : + videoStreamStatus === 'ended' ? '已结束' : + videoStreamStatus === 'stalled' ? '停滞' : '未知'} @@ -3153,11 +3317,77 @@ const DeviceMonitor: React.FC = () => { 流畅 + + + + + + + {videoStreamUrl} + {allStreamUrls && ( + + + {allStreamUrls.pushUrl && ( +
+ 推流地址 + + {allStreamUrls.pushUrl} + +
+ )} + {allStreamUrls.playM3u8Url && ( +
+ HLS播放 + + {allStreamUrls.playM3u8Url} + +
+ )} + {allStreamUrls.playFlvUrl && ( +
+ FLV播放 + + {allStreamUrls.playFlvUrl} + +
+ )} + {allStreamUrls.playRtmpUrl && ( +
+ RTMP播放 + + {allStreamUrls.playRtmpUrl} + +
+ )} +
+
+ )}