204 lines
3.6 KiB
JavaScript
204 lines
3.6 KiB
JavaScript
import React, { useEffect, useRef } from "react";
|
|
import { joinRoom, leaveRoom } from "./droneplayer";
|
|
import { postLiveHeartbeat } from "@/api/api";
|
|
|
|
export default function DroneLivePlayer({
|
|
streamData,
|
|
videoMode = "NORMAL",
|
|
}) {
|
|
const containerRef = useRef(null);
|
|
|
|
// 心跳 timer
|
|
const timerRef = useRef(null);
|
|
|
|
// 当前 effect 是否有效(防止异步穿透)
|
|
const aliveRef = useRef(false);
|
|
|
|
// 防止 join / leave 并发
|
|
const busyRef = useRef(false);
|
|
|
|
useEffect(() => {
|
|
const container = containerRef.current;
|
|
|
|
// 当前 effect 生效
|
|
aliveRef.current = true;
|
|
|
|
if (!container) return;
|
|
|
|
// 强制清空旧 DOM
|
|
container.innerHTML = "";
|
|
|
|
// 没有流地址直接退出
|
|
if (!streamData?.url) {
|
|
return;
|
|
}
|
|
|
|
// ---------------- 解析 RTC 参数 ----------------
|
|
|
|
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) {
|
|
console.error("RTC 参数缺失");
|
|
return;
|
|
}
|
|
|
|
// ---------------- 心跳 ----------------
|
|
|
|
const liveheartbeat = async () => {
|
|
try {
|
|
await postLiveHeartbeat({
|
|
sn: streamData?.sn,
|
|
cameraIndex: streamData?.camera_index,
|
|
});
|
|
} catch (err) {
|
|
console.error("心跳失败", err);
|
|
}
|
|
};
|
|
|
|
// ---------------- 初始化 RTC ----------------
|
|
|
|
const initRTC = async () => {
|
|
// 防止重复进入
|
|
if (busyRef.current) return;
|
|
|
|
busyRef.current = true;
|
|
|
|
try {
|
|
// =========================
|
|
// 1. 先彻底关闭旧 RTC
|
|
// =========================
|
|
|
|
if (timerRef.current) {
|
|
clearInterval(timerRef.current);
|
|
timerRef.current = null;
|
|
}
|
|
|
|
// 必须 await
|
|
await leaveRoom();
|
|
|
|
// 给 SDK 一点释放时间
|
|
await new Promise((resolve) =>
|
|
setTimeout(resolve, 300)
|
|
);
|
|
|
|
// 组件已经卸载
|
|
if (!aliveRef.current) {
|
|
return;
|
|
}
|
|
|
|
// =========================
|
|
// 2. 加入新房间
|
|
// =========================
|
|
|
|
await joinRoom(
|
|
container,
|
|
appId,
|
|
roomId,
|
|
token,
|
|
userId,
|
|
videoMode
|
|
);
|
|
|
|
// 再次检查
|
|
if (!aliveRef.current) {
|
|
await leaveRoom();
|
|
return;
|
|
}
|
|
|
|
// =========================
|
|
// 3. 开启心跳
|
|
// =========================
|
|
|
|
/*
|
|
timerRef.current = setInterval(() => {
|
|
liveheartbeat();
|
|
}, 3000);
|
|
|
|
liveheartbeat();
|
|
*/
|
|
} catch (err) {
|
|
console.error("RTC 初始化失败:", err);
|
|
|
|
// 出错强制释放
|
|
try {
|
|
await leaveRoom();
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
} finally {
|
|
busyRef.current = false;
|
|
}
|
|
};
|
|
|
|
initRTC();
|
|
|
|
// =========================
|
|
// cleanup
|
|
// =========================
|
|
|
|
return () => {
|
|
aliveRef.current = false;
|
|
|
|
// 清理心跳
|
|
if (timerRef.current) {
|
|
clearInterval(timerRef.current);
|
|
timerRef.current = null;
|
|
}
|
|
|
|
// 异步释放 RTC
|
|
(async () => {
|
|
try {
|
|
await leaveRoom();
|
|
|
|
// 清空 DOM
|
|
if (containerRef.current) {
|
|
containerRef.current.innerHTML = "";
|
|
}
|
|
} catch (err) {
|
|
console.error("leaveRoom error:", err);
|
|
}
|
|
})();
|
|
};
|
|
}, [
|
|
streamData?.url,
|
|
streamData?.sn,
|
|
streamData?.camera_index,
|
|
videoMode,
|
|
]);
|
|
|
|
return (
|
|
<div
|
|
ref={containerRef}
|
|
className="rounded"
|
|
style={{
|
|
width: "100%",
|
|
height: "100%",
|
|
background: "#000",
|
|
overflow: "hidden",
|
|
}}
|
|
/>
|
|
);
|
|
}
|