优化视频显示加载占位

This commit is contained in:
mmc
2026-09-08 16:09:55 +08:00
parent 39e5e80926
commit 7ebba1bbf6
2 changed files with 33 additions and 20 deletions

View File

@@ -1,19 +1,17 @@
import React, { useEffect, useRef, useState } from "react"; import React, { useEffect, useRef, useState } from "react";
import AgoraRTC, { IAgoraRTCClient, IRemoteVideoTrack } from "agora-rtc-sdk-ng"; import AgoraRTC, { IAgoraRTCClient, IRemoteVideoTrack } from "agora-rtc-sdk-ng";
import { Badge, message } from "antd"; import { Badge, message } from "antd";
const APP_ID = "875f6494d87548e783fefbf33f598c97"; const APP_ID = "875f6494d87548e783fefbf33f598c97";
const TOKEN = "007eJxTYIjPuZr09PC0znd6+z0MMyL6ZHb68OetXW+kO1vHU2ZNtYECg4W5aZqZiaVJCpBhYpFqbmGclpqWlGZsnGZqaZFsaT6pZH5WQyAjA7e+LysjAwSC+JwMxmYlqcUlugZGDAwAbuYeJg=="; const TOKEN = "007eJxTYIjPuZr09PC0znd6+z0MMyL6ZHb68OetXW+kO1vHU2ZNtYECg4W5aZqZiaVJCpBhYpFqbmGclpqWlGZsnGZqaZFsaT6pZH5WQyAjA7e+LysjAwSC+JwMxmYlqcUlugZGDAwAbuYeJg==";
export default function AgrtcRoomRobot({ CHANNEL }) { export default function AgrtcRoomRobot({ CHANNEL }) {
const [uid] = useState(() => Number(Math.floor(Math.random() * 100000))); const [uid] = useState(() => Number(Math.floor(Math.random() * 100000)));
const [connected, setConnected] = useState(false); const [connected, setConnected] = useState(false);
const [joined, setJoined] = useState(false); const [joined, setJoined] = useState(false);
// 标记是否存在远端视频流
const [hasRemoteVideo, setHasRemoteVideo] = useState(false);
const remoteVideoRef = useRef<HTMLDivElement>(null); const remoteVideoRef = useRef<HTMLDivElement>(null);
const clientRef = useRef<IAgoraRTCClient | null>(null); const clientRef = useRef<IAgoraRTCClient | null>(null);
const remoteVideoTrackRef = useRef<IRemoteVideoTrack | null>(null); const remoteVideoTrackRef = useRef<IRemoteVideoTrack | null>(null);
const joiningRef = useRef(false); const joiningRef = useRef(false);
const reconnectTimerRef = useRef<NodeJS.Timeout | null>(null); const reconnectTimerRef = useRef<NodeJS.Timeout | null>(null);
@@ -24,7 +22,6 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
}); });
clientRef.current = client; clientRef.current = client;
client.setClientRole("audience"); client.setClientRole("audience");
let isUnmounted = false; let isUnmounted = false;
// 停止并清空远端视频 // 停止并清空远端视频
@@ -38,6 +35,7 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
if (remoteVideoRef.current) { if (remoteVideoRef.current) {
remoteVideoRef.current.innerHTML = ""; remoteVideoRef.current.innerHTML = "";
} }
setHasRemoteVideo(false);
}; };
// 重连逻辑 // 重连逻辑
@@ -46,14 +44,12 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
console.log("执行RTC重连"); console.log("执行RTC重连");
clearRemoteVideo(); clearRemoteVideo();
setJoined(false); setJoined(false);
try { try {
joiningRef.current = true; joiningRef.current = true;
await client.join(APP_ID, CHANNEL, TOKEN, uid); await client.join(APP_ID, CHANNEL, TOKEN, uid);
if (isUnmounted) return; if (isUnmounted) return;
setJoined(true); setJoined(true);
message.info("RTC重连成功"); message.info("RTC重连成功");
// 重连成功后重新订阅已经在线的上位机 // 重连成功后重新订阅已经在线的上位机
for (const remoteUser of client.remoteUsers) { for (const remoteUser of client.remoteUsers) {
if (isUnmounted) break; if (isUnmounted) break;
@@ -61,6 +57,7 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
await client.subscribe(remoteUser, "video"); await client.subscribe(remoteUser, "video");
remoteVideoTrackRef.current = remoteUser.videoTrack; remoteVideoTrackRef.current = remoteUser.videoTrack;
remoteUser.videoTrack?.play(remoteVideoRef.current!); remoteUser.videoTrack?.play(remoteVideoRef.current!);
setHasRemoteVideo(true);
} }
} }
} catch (err: any) { } catch (err: any) {
@@ -77,7 +74,6 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
if (isUnmounted) return; if (isUnmounted) return;
console.log("连接状态变更", prevState, "→", curState); console.log("连接状态变更", prevState, "→", curState);
setConnected(curState === "CONNECTED"); setConnected(curState === "CONNECTED");
// 信令断开,延迟重连 // 信令断开,延迟重连
if (curState === "DISCONNECTED") { if (curState === "DISCONNECTED") {
clearRemoteVideo(); clearRemoteVideo();
@@ -93,11 +89,11 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
if (isUnmounted) return; if (isUnmounted) return;
console.log("user-published", user.uid, mediaType); console.log("user-published", user.uid, mediaType);
await client.subscribe(user, mediaType); await client.subscribe(user, mediaType);
if (mediaType === "video" && remoteVideoRef.current) { if (mediaType === "video" && remoteVideoRef.current) {
clearRemoteVideo(); clearRemoteVideo();
remoteVideoTrackRef.current = user.videoTrack; remoteVideoTrackRef.current = user.videoTrack;
user.videoTrack?.play(remoteVideoRef.current); user.videoTrack?.play(remoteVideoRef.current);
setHasRemoteVideo(true);
} }
}; };
@@ -149,7 +145,6 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
console.log("加入频道成功"); console.log("加入频道成功");
setJoined(true); setJoined(true);
message.success("RTC已加入频道"); message.success("RTC已加入频道");
// 上位机已经提前推流,手动订阅 // 上位机已经提前推流,手动订阅
for (const remoteUser of client.remoteUsers) { for (const remoteUser of client.remoteUsers) {
if (isUnmounted) break; if (isUnmounted) break;
@@ -158,6 +153,7 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
await client.subscribe(remoteUser, "video"); await client.subscribe(remoteUser, "video");
remoteVideoTrackRef.current = remoteUser.videoTrack; remoteVideoTrackRef.current = remoteUser.videoTrack;
remoteUser.videoTrack?.play(remoteVideoRef.current!); remoteUser.videoTrack?.play(remoteVideoRef.current!);
setHasRemoteVideo(true);
} }
} }
} catch (err: any) { } catch (err: any) {
@@ -181,17 +177,14 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
isUnmounted = true; isUnmounted = true;
if (reconnectTimerRef.current) clearTimeout(reconnectTimerRef.current); if (reconnectTimerRef.current) clearTimeout(reconnectTimerRef.current);
clearRemoteVideo(); clearRemoteVideo();
const c = clientRef.current; const c = clientRef.current;
if (!c) return; if (!c) return;
c.off("connection-state-change", onConnStateChange); c.off("connection-state-change", onConnStateChange);
c.off("user-published", onUserPublished); c.off("user-published", onUserPublished);
c.off("user-unpublished", onUserUnpublished); c.off("user-unpublished", onUserUnpublished);
c.off("user-left", onUserLeft); c.off("user-left", onUserLeft);
c.off("user-joined", onUserJoined); c.off("user-joined", onUserJoined);
c.off("token-privilege-will-expire", onTokenWillExpire); c.off("token-privilege-will-expire", onTokenWillExpire);
c.leave(); c.leave();
clientRef.current = null; clientRef.current = null;
}; };
@@ -210,10 +203,30 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
text={joined ? "已加入频道" : "未加入频道"} text={joined ? "已加入频道" : "未加入频道"}
styles={{ content: { color: '#888' } }} styles={{ content: { color: '#888' } }}
/> />
<span style={{ fontSize: 11, color: "#888" }}>频道:{CHANNEL}</span> <span style={{ fontSize: 11, color: "#888" }}>频道:{CHANNEL}</span>
</div> </div>
<div ref={remoteVideoRef} style={{ flex: 1, width: "100%", minHeight: 0 }} /> <div style={{ flex: 1, width: "100%", minHeight: 0, position: "relative" }}>
{/* 无视频时显示加载占位 */}
{!hasRemoteVideo && (
<div style={{
position: "absolute",
inset: 0,
display: "flex",
alignItems: "center",
justifyContent: "center",
color: "#aaa",
fontSize: 14,
background: "#000"
}}>
视频加载中,等待远端推流...
</div>
)}
{/* Agora 视频容器,内部会被SDK填充video dom */}
<div ref={remoteVideoRef} style={{
position: "absolute",
inset: 0
}} />
</div>
</div > </div >
); );
} }

View File

@@ -1224,7 +1224,7 @@ export default function DeviceStatusPage() {
{/* 视频层:停止时不渲染 / 未播放时为空,不影响状态面板 */} {/* 视频层:停止时不渲染 / 未播放时为空,不影响状态面板 */}
{isVideoPlaying && ( {isVideoPlaying && (
mowerView === 'all' ? ( mowerView === 'all' ? (
robotVideo ? ( (
<div style={{ width: '100%', height: '100%' }}> <div style={{ width: '100%', height: '100%' }}>
<MowerAllView <MowerAllView
serialNumber={device?.serialNumber} serialNumber={device?.serialNumber}
@@ -1233,12 +1233,12 @@ export default function DeviceStatusPage() {
ready={ready} ready={ready}
/> />
</div> </div>
) : null )
) : mowerView === 'front' ? ( ) : mowerView === 'front' ? (
<div style={{ position: 'relative', width: '100%', height: '100%' }}> <div style={{ position: 'relative', width: '100%', height: '100%' }}>
{device?.hasHost ? ( {device?.hasHost ? (
<AgrtcRoomRobot CHANNEL={device?.serialNumber || ''} /> <AgrtcRoomRobot CHANNEL={device?.serialNumber || ''} />
) : robotVideo ? ( ) : (
<VideoPlayer <VideoPlayer
deviceId={device?.serialNumber} deviceId={device?.serialNumber}
idx={1} idx={1}
@@ -1247,7 +1247,7 @@ export default function DeviceStatusPage() {
error={error} error={error}
ready={ready} ready={ready}
/> />
) : null} )}
</div> </div>
) : ( ) : (
<div style={{ position: 'relative', width: '100%', height: '100%' }}> <div style={{ position: 'relative', width: '100%', height: '100%' }}>
@@ -1268,7 +1268,7 @@ export default function DeviceStatusPage() {
)} )}
{/* 视频未加载 / 停止播放的占位 */} {/* 视频未加载 / 停止播放的占位 */}
{(!robotVideo && !isVideoPlaying) && ( {!isVideoPlaying && (
<div style={{ <div style={{
width: '100%', height: '100%', width: '100%', height: '100%',
display: 'flex', alignItems: 'center', justifyContent: 'center', display: 'flex', alignItems: 'center', justifyContent: 'center',