装备 完善
This commit is contained in:
108
src/components/VolcRtcPlayer.jsx
Normal file
108
src/components/VolcRtcPlayer.jsx
Normal file
@@ -0,0 +1,108 @@
|
||||
import React, { useEffect, useRef } from "react";
|
||||
import { joinRoom, leaveRoom } from "./droneplayer";
|
||||
import { postLiveHeartbeat } from "@/api/api";
|
||||
|
||||
export default function DroneLivePlayer({ streamData, videoMode = "NORMAL" }) {
|
||||
console.log(streamData)
|
||||
const containerRef = useRef(null);
|
||||
const timerRef = useRef(null);
|
||||
|
||||
// ------------ 核心修复:防止异步竞态 ------------
|
||||
const isBusyRef = useRef(false); // 正在加入/离开,禁止重复操作
|
||||
const mountedRef = useRef(true); // 组件是否挂载
|
||||
|
||||
useEffect(() => {
|
||||
mountedRef.current = true;
|
||||
const container = containerRef.current;
|
||||
|
||||
// 清空容器,防止 DOM 不为空
|
||||
if (container) container.innerHTML = "";
|
||||
|
||||
if (!container || !streamData?.url) return;
|
||||
|
||||
// 解析参数
|
||||
const params = new URLSearchParams(streamData.url);
|
||||
const appId = (params.get("app_id") || params.get("appid") || "").trim();
|
||||
const roomId = (params.get("room_id") || params.get("roomid") || "").trim();
|
||||
const token = decodeURIComponent((params.get("token") || "").trim());
|
||||
const userId = (params.get("user_id") || params.get("uid") || "").trim();
|
||||
|
||||
if (!appId || !roomId || !token || !userId) return;
|
||||
|
||||
// 心跳
|
||||
const liveheartbeat = async () => {
|
||||
try {
|
||||
await postLiveHeartbeat({
|
||||
sn: streamData.sn,
|
||||
cameraIndex: streamData.camera_index,
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("心跳失败", err);
|
||||
}
|
||||
};
|
||||
|
||||
// ---------------- 真正安全的初始化流程 ----------------
|
||||
const safeInit = async () => {
|
||||
// 1. 防止并发:正在忙就直接退出
|
||||
if (isBusyRef.current) return;
|
||||
isBusyRef.current = true;
|
||||
|
||||
try {
|
||||
// 2. 【强制顺序】先销毁旧房间 + 清心跳
|
||||
if (timerRef.current) {
|
||||
clearInterval(timerRef.current);
|
||||
timerRef.current = null;
|
||||
}
|
||||
leaveRoom(); // 先离开旧房间
|
||||
await new Promise((resolve) => setTimeout(resolve, 100)); // 等待 SDK 内部清理完成
|
||||
|
||||
// 3. 组件已卸载,直接放弃
|
||||
if (!mountedRef.current) {
|
||||
isBusyRef.current = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. 加入新房间(此时绝对安全)
|
||||
await joinRoom(container, appId, roomId, token, userId, videoMode);
|
||||
|
||||
// 5. 开启心跳
|
||||
//timerRef.current = setInterval(liveheartbeat, 3000);
|
||||
//liveheartbeat();
|
||||
} catch (err) {
|
||||
console.error("RTC 初始化失败", err);
|
||||
} finally {
|
||||
// 解锁
|
||||
isBusyRef.current = false;
|
||||
}
|
||||
};
|
||||
|
||||
safeInit();
|
||||
|
||||
// ---------------- 清理函数 ----------------
|
||||
return () => {
|
||||
mountedRef.current = false;
|
||||
|
||||
// 清理心跳
|
||||
if (timerRef.current) {
|
||||
clearInterval(timerRef.current);
|
||||
timerRef.current = null;
|
||||
}
|
||||
|
||||
// 离开房间
|
||||
leaveRoom();
|
||||
};
|
||||
}, [
|
||||
streamData?.url,
|
||||
streamData?.sn,
|
||||
streamData?.camera_index,
|
||||
videoMode,
|
||||
]);
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={containerRef}
|
||||
className="rounded"
|
||||
style={{ width: "100%", height: "100%", background: "#000" }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user