优化声网视频 几分钟停止订阅的bug,地图上给每个机器加上背景光圈,显示工作状态,光圈支持实时根据定位走,优化实时路线和机器的中心不在同一点的问题

This commit is contained in:
mmc
2026-09-15 16:28:28 +08:00
parent f852174fde
commit 9017cf9c87
8 changed files with 542 additions and 89 deletions

View File

@@ -1,47 +1,92 @@
import React, { useEffect, useRef, useState } from "react";
import AgoraRTC, { IAgoraRTCClient, IRemoteVideoTrack } from "agora-rtc-sdk-ng";
import { Badge, message } from "antd";
AgoraRTC.setLogLevel(2); // 2=只输出warning/error,屏蔽debug日志
const APP_ID = "7887b4db6033410d9ac3264b40ca9c1a";
const TOKEN = "007eJxTYKjmeKazOKdUZ5qMsU389u45l6ruuuQlze8o+OZWHdBUHK3AYG5hYZ5kkpJkZmBsbGJokGKZmGxsZGaSZGKQnGiZbJh4dsuSrIZARoYFWw4zMTJAIIjPxWBiqFuSkVqUm5jDwAAA1JUhaw==";
export default function AgrtcRoomRobot({ CHANNEL }) {
const TOKEN = "007eJxTYDhxaLPL8617mtfHFi2tfJhQNSXh5vNJnqvu8b/3PenKKdumwGBuYWGeZJKSZGZgbGxiaJBimZhsbGRmkmRikJxomWyYuHHFiqyGQEaGB6+fMDEyQCCIz81gYqhbklGUmphrYMTAAAAyfSTX";
export default function AgrtcRoomRobot({ CHANNEL }: { CHANNEL: string }) {
const [uid] = useState(() => Number(Math.floor(Math.random() * 100000)));
const [connected, setConnected] = useState(false);
const [joined, setJoined] = useState(false);
// 标记是否存在远端视频流
const [hasRemoteVideo, setHasRemoteVideo] = useState(false);
const remoteVideoRef = useRef<HTMLDivElement>(null);
const clientRef = useRef<IAgoraRTCClient | null>(null);
const remoteVideoTrackRef = useRef<IRemoteVideoTrack | null>(null);
const joiningRef = useRef(false);
const reconnectTimerRef = useRef<NodeJS.Timeout | null>(null);
const subscribedUidRef = useRef<number | null>(null);
useEffect(() => {
const client = AgoraRTC.createClient({
mode: "live",
mode: "rtc",
codec: "h264",
});
clientRef.current = client;
client.setClientRole("audience");
let isUnmounted = false;
// ========= 删掉了 setParameters,Web NG不支持!========
let isUnmounted = false;
let videoWatchDogTimer: NodeJS.Timeout | null = null;
let unpublishTimer: NodeJS.Timeout | null = null;
// 停止并清空远端视频
const clearRemoteVideo = () => {
if (remoteVideoTrackRef.current) {
try {
remoteVideoTrackRef.current.stop();
} catch (e) { /* */ }
} catch (e) {
console.warn("stop track error", e);
}
remoteVideoTrackRef.current = null;
}
if (remoteVideoRef.current) {
remoteVideoRef.current.innerHTML = "";
}
subscribedUidRef.current = null;
setHasRemoteVideo(false);
if (videoWatchDogTimer) clearInterval(videoWatchDogTimer);
};
const startVideoWatchDog = () => {
if (videoWatchDogTimer) clearInterval(videoWatchDogTimer);
videoWatchDogTimer = setInterval(async () => {
const track = remoteVideoTrackRef.current;
const dom = remoteVideoRef.current;
if (!track || !dom) return;
const videoEl = dom.querySelector("video") as HTMLVideoElement;
if (!videoEl) return;
if (videoEl.readyState < 2) {
console.warn("看门狗检测视频无画面,尝试重新play");
try {
await track.play(dom);
} catch (e) {
console.warn("看门狗play异常", e);
}
}
}, 3000);
};
const playRemoteTrack = async (track: IRemoteVideoTrack) => {
const dom = remoteVideoRef.current;
if (!dom || !track) return;
try {
await track.play(dom);
console.log("远端视频播放成功");
setHasRemoteVideo(true);
startVideoWatchDog();
} catch (err: any) {
if (err.name === "AbortError") {
console.warn("播放请求被中断 AbortError,忽略", err);
return;
}
console.error("视频播放失败", err);
message.error("视频播放失败");
}
};
// 重连逻辑
const doReconnect = async () => {
if (isUnmounted || joiningRef.current) return;
console.log("执行RTC重连");
console.log("执行RTC重连(信令断开)");
clearRemoteVideo();
setJoined(false);
try {
@@ -50,14 +95,13 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
if (isUnmounted) return;
setJoined(true);
message.info("RTC重连成功");
// 重连成功后重新订阅已经在线的上位机
for (const remoteUser of client.remoteUsers) {
if (isUnmounted) break;
if (remoteUser.hasVideo) {
if (remoteUser.hasVideo && subscribedUidRef.current !== remoteUser.uid) {
await client.subscribe(remoteUser, "video");
remoteVideoTrackRef.current = remoteUser.videoTrack;
remoteUser.videoTrack?.play(remoteVideoRef.current!);
setHasRemoteVideo(true);
subscribedUidRef.current = remoteUser.uid;
await playRemoteTrack(remoteUser.videoTrack!);
}
}
} catch (err: any) {
@@ -69,12 +113,10 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
}
};
// 连接状态变化
const onConnStateChange = (curState: string, prevState: string) => {
if (isUnmounted) return;
console.log("连接状态变更", prevState, "→", curState);
setConnected(curState === "CONNECTED");
// 信令断开,延迟重连
if (curState === "DISCONNECTED") {
clearRemoteVideo();
if (reconnectTimerRef.current) clearTimeout(reconnectTimerRef.current);
@@ -84,48 +126,53 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
}
};
// 远端用户发布流
const onUserPublished = async (user: any, mediaType: "video" | "audio") => {
if (isUnmounted) return;
console.log("user-published", user.uid, mediaType);
await client.subscribe(user, mediaType);
if (mediaType === "video" && remoteVideoRef.current) {
clearRemoteVideo();
remoteVideoTrackRef.current = user.videoTrack;
user.videoTrack?.play(remoteVideoRef.current);
setHasRemoteVideo(true);
if (mediaType !== "video") return;
if (subscribedUidRef.current === user.uid) {
console.log("该用户已订阅,跳过");
return;
}
// 取消待执行的清空定时器
if (unpublishTimer) clearTimeout(unpublishTimer);
await client.subscribe(user, mediaType);
remoteVideoTrackRef.current = user.videoTrack;
subscribedUidRef.current = user.uid;
await playRemoteTrack(user.videoTrack);
};
// 远端停止发布流(上位机停止推流)
const onUserUnpublished = (user: any, mediaType: "video" | "audio") => {
if (isUnmounted) return;
console.log("user-unpublished", user.uid, mediaType);
if (mediaType === "video") {
if (mediaType === "video" && subscribedUidRef.current === user.uid) {
if (unpublishTimer) clearTimeout(unpublishTimer);
// 延迟2s清空,短闪断自动恢复不黑屏
unpublishTimer = setTimeout(() => {
clearRemoteVideo();
}, 2000);
}
};
const onUserLeft = (user: any) => {
if (isUnmounted) return;
console.log("user-left", user.uid);
if (subscribedUidRef.current === user.uid) {
if (unpublishTimer) clearTimeout(unpublishTimer);
clearRemoteVideo();
}
};
// 远端用户离开频道
const onUserLeft = (user: any) => {
if (isUnmounted) return;
console.log("user-left", user.uid);
clearRemoteVideo();
};
// 远端用户进入
const onUserJoined = (user: any) => {
if (!isUnmounted) console.log("远端用户进入频道:", user.uid);
};
// Token即将过期提醒(测试Token有效期有限)
const onTokenWillExpire = () => {
console.warn("token即将过期,请更新token");
message.warning("Token即将过期,视频可能很快断开");
// 正式环境:请求后端拿新token client.renewToken(newToken)
};
// 注册事件,保存回调引用用于off解绑
client.on("connection-state-change", onConnStateChange);
client.on("user-published", onUserPublished);
client.on("user-unpublished", onUserUnpublished);
@@ -145,20 +192,19 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
console.log("加入频道成功");
setJoined(true);
message.success("RTC已加入频道");
// 上位机已经提前推流,手动订阅
for (const remoteUser of client.remoteUsers) {
if (isUnmounted) break;
console.log("发现已有远端用户:", remoteUser.uid);
if (remoteUser.hasVideo) {
if (remoteUser.hasVideo && subscribedUidRef.current !== remoteUser.uid) {
await client.subscribe(remoteUser, "video");
remoteVideoTrackRef.current = remoteUser.videoTrack;
remoteUser.videoTrack?.play(remoteVideoRef.current!);
setHasRemoteVideo(true);
subscribedUidRef.current = remoteUser.uid;
await playRemoteTrack(remoteUser.videoTrack!);
}
}
} catch (err: any) {
if (err?.code === "OPERATION_ABORTED") {
console.log("join操作被取消(StrictMode/组件卸载),忽略");
console.log("join操作被取消,忽略");
return;
}
console.error("加入频道失败:", err);
@@ -172,10 +218,11 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
init();
// 组件卸载清理
return () => {
isUnmounted = true;
if (reconnectTimerRef.current) clearTimeout(reconnectTimerRef.current);
if (unpublishTimer) clearTimeout(unpublishTimer);
if (videoWatchDogTimer) clearInterval(videoWatchDogTimer);
clearRemoteVideo();
const c = clientRef.current;
if (!c) return;
@@ -188,10 +235,10 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
c.leave();
clientRef.current = null;
};
}, [uid, CHANNEL]);
}, [CHANNEL]);
return (
<div style={{ width: "100%", height: "100%", display: "flex", flexDirection: "column", borderRadius: 8, overflow: "hidden", background: "#000" }}>
<div style={{ width: "100%", height: "95%", display: "flex", flexDirection: "column", borderRadius: 8, overflow: "hidden", background: "#000" }}>
<div style={{ display: "flex", justifyContent: "end", alignItems: "center", gap: 12, padding: "4px 8px", background: "rgba(255,255,255,0.08)", flexShrink: 0 }}>
<Badge
color={connected ? "green" : "red"}
@@ -206,7 +253,6 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
<span style={{ fontSize: 11, color: "#888" }}>频道:{CHANNEL}</span>
</div>
<div style={{ flex: 1, width: "100%", minHeight: 0, position: "relative" }}>
{/* 无视频时显示加载占位 */}
{!hasRemoteVideo && (
<div style={{
position: "absolute",
@@ -216,15 +262,18 @@ export default function AgrtcRoomRobot({ CHANNEL }) {
justifyContent: "center",
color: "#aaa",
fontSize: 14,
background: "#000"
background: "#000",
zIndex: 2
}}>
视频加载中,等待远端推流...
</div>
)}
{/* Agora 视频容器,内部会被SDK填充video dom */}
<div ref={remoteVideoRef} style={{
position: "absolute",
inset: 0
inset: 0,
width: "100%",
height: "100%",
zIndex: 1
}} />
</div>
</div >