Files
web-Iot/src/components/AgoraRtc.tsx

368 lines
4.8 KiB
TypeScript
Raw Normal View History

2026-05-12 11:21:31 +08:00
import React, {
useEffect,
useRef,
useState,
} from "react";
import AgoraRTC from "agora-rtc-sdk-ng";
2026-05-12 11:11:30 +08:00
import {
Button,
Card,
Badge,
message,
2026-05-12 11:21:31 +08:00
Space,
2026-05-12 11:11:30 +08:00
} from "antd";
const APP_ID =
"62a87b833b7e48888aca06b625f88c14";
const CHANNEL =
"test_rtc_channel";
2026-05-12 11:21:31 +08:00
const TOKEN = "007eJxTYGhZyLP1Ekt5av/jV1mPpi98fG75FqcXvDl7/wuUJjNeEVukwGBmlGhhnmRhbJxknmpiAQSJyYkGZklmRqZpFhbJhiYfZjJlNQQyMrDxcrIwMkAgiC/AUJJaXBJfVJIcn5yRmJeXmsPAAABcCyR4";
2026-05-12 11:11:30 +08:00
const client = AgoraRTC.createClient({
mode: "rtc",
codec: "vp8",
});
2026-05-12 11:21:31 +08:00
export default function AgoraRtcRoom() {
2026-05-12 11:11:30 +08:00
const [uid] = useState(() =>
2026-05-12 11:21:31 +08:00
String(
Math.floor(
Math.random() * 100000
)
)
2026-05-12 11:11:30 +08:00
);
const [connected, setConnected] =
useState(false);
const [published, setPublished] =
useState(false);
2026-05-12 12:36:07 +08:00
const [joined, setJoined] =
useState(false);
2026-05-12 11:21:31 +08:00
const localVideoRef =
useRef<HTMLDivElement>(null);
const remoteVideoRef =
useRef<HTMLDivElement>(null);
2026-05-12 11:11:30 +08:00
useEffect(() => {
const init = async () => {
try {
2026-05-12 12:36:07 +08:00
// RTC 状态监听
2026-05-12 11:11:30 +08:00
client.on(
"connection-state-change",
(state) => {
console.log(
2026-05-12 12:36:07 +08:00
"RTC状态:",
2026-05-12 11:11:30 +08:00
state
);
if (
2026-05-12 11:21:31 +08:00
state ===
"CONNECTED"
2026-05-12 11:11:30 +08:00
) {
2026-05-12 11:21:31 +08:00
setConnected(
true
2026-05-12 11:11:30 +08:00
);
2026-05-12 12:36:07 +08:00
} else {
setConnected(
false
);
2026-05-12 11:11:30 +08:00
}
}
);
2026-05-12 12:36:07 +08:00
// 远端用户发布流
2026-05-12 11:11:30 +08:00
client.on(
"user-published",
2026-05-12 11:21:31 +08:00
async (
user,
mediaType
) => {
2026-05-12 11:11:30 +08:00
console.log(
2026-05-12 12:36:07 +08:00
"远端用户发布:",
2026-05-12 11:11:30 +08:00
user.uid,
mediaType
);
2026-05-12 12:36:07 +08:00
// 订阅远端流
2026-05-12 11:11:30 +08:00
await client.subscribe(
user,
mediaType
);
2026-05-12 11:21:31 +08:00
// 视频
2026-05-12 11:11:30 +08:00
if (
2026-05-12 11:21:31 +08:00
mediaType ===
"video"
2026-05-12 11:11:30 +08:00
) {
2026-05-12 11:21:31 +08:00
user.videoTrack?.play(
remoteVideoRef.current!
2026-05-12 11:11:30 +08:00
);
}
2026-05-12 11:21:31 +08:00
// 音频
if (
mediaType ===
"audio"
) {
2026-05-12 11:11:30 +08:00
2026-05-12 11:21:31 +08:00
user.audioTrack?.play();
}
2026-05-12 11:11:30 +08:00
}
);
2026-05-12 12:36:07 +08:00
// 用户离开
client.on(
"user-left",
(user) => {
console.log(
"用户离开:",
user.uid
);
if (
remoteVideoRef.current
) {
remoteVideoRef.current.innerHTML =
"";
}
}
);
// 加入频道
2026-05-12 11:11:30 +08:00
await client.join(
APP_ID,
CHANNEL,
TOKEN,
uid
);
2026-05-12 12:36:07 +08:00
console.log(
"加入频道成功"
);
setJoined(true);
2026-05-12 11:21:31 +08:00
message.success(
2026-05-12 12:36:07 +08:00
"RTC 已加入频道"
2026-05-12 11:11:30 +08:00
);
} catch (err) {
2026-05-12 12:36:07 +08:00
console.error(
"初始化失败:",
err
);
message.error(
"初始化失败"
);
2026-05-12 11:11:30 +08:00
}
};
init();
return () => {
client.leave();
};
2026-05-12 12:36:07 +08:00
}, [uid]);
2026-05-12 11:11:30 +08:00
2026-05-12 12:36:07 +08:00
// 开始推流
2026-05-12 11:11:30 +08:00
const startPublish =
async () => {
2026-05-12 12:36:07 +08:00
if (!joined) {
message.warning(
"RTC 还未加入频道"
);
return;
}
2026-05-12 11:11:30 +08:00
try {
2026-05-12 12:36:07 +08:00
// 麦克风轨道
2026-05-12 11:21:31 +08:00
const audioTrack =
2026-05-12 11:11:30 +08:00
await AgoraRTC.createMicrophoneAudioTrack();
2026-05-12 12:36:07 +08:00
// 摄像头轨道
2026-05-12 11:21:31 +08:00
const videoTrack =
await AgoraRTC.createCameraVideoTrack();
// 本地播放
videoTrack.play(
localVideoRef.current!
);
2026-05-12 12:36:07 +08:00
// 发布流
2026-05-12 11:11:30 +08:00
await client.publish([
2026-05-12 11:21:31 +08:00
audioTrack,
videoTrack,
2026-05-12 11:11:30 +08:00
]);
2026-05-12 12:36:07 +08:00
console.log(
"推流成功"
);
2026-05-12 11:21:31 +08:00
setPublished(true);
2026-05-12 11:11:30 +08:00
message.success(
2026-05-12 12:36:07 +08:00
"开始推流"
2026-05-12 11:11:30 +08:00
);
} catch (err) {
2026-05-12 12:36:07 +08:00
console.error(
"推流失败:",
err
);
2026-05-12 11:11:30 +08:00
message.error(
"推流失败"
);
}
};
return (
<Card
2026-05-12 12:36:07 +08:00
title="Agora RTC 音视频测试"
2026-05-12 11:11:30 +08:00
style={{
2026-05-12 11:21:31 +08:00
width: 1000,
margin:
"20px auto",
2026-05-12 11:11:30 +08:00
}}
>
2026-05-12 11:21:31 +08:00
<Space
direction="vertical"
2026-05-12 11:11:30 +08:00
style={{
2026-05-12 11:21:31 +08:00
width: "100%",
2026-05-12 11:11:30 +08:00
}}
>
2026-05-12 11:21:31 +08:00
<div>
2026-05-12 12:36:07 +08:00
当前 UID:{uid}
2026-05-12 11:21:31 +08:00
</div>
2026-05-12 11:11:30 +08:00
<Badge
color={
connected
? "green"
: "red"
}
text={
connected
? "RTC 已连接"
: "RTC 未连接"
}
/>
2026-05-12 12:36:07 +08:00
<Badge
color={
published
? "green"
: "orange"
}
text={
published
? "已推流"
: "未推流"
}
/>
2026-05-12 11:21:31 +08:00
<Button
type="primary"
onClick={
startPublish
2026-05-12 11:11:30 +08:00
}
2026-05-12 11:21:31 +08:00
disabled={
2026-05-12 12:36:07 +08:00
!joined ||
2026-05-12 11:11:30 +08:00
published
}
2026-05-12 11:21:31 +08:00
>
开始音视频推流
</Button>
2026-05-12 11:11:30 +08:00
2026-05-12 11:21:31 +08:00
<div
style={{
display: "flex",
gap: 20,
2026-05-12 12:36:07 +08:00
marginTop: 20,
2026-05-12 11:21:31 +08:00
}}
>
2026-05-12 11:11:30 +08:00
2026-05-12 11:21:31 +08:00
<div>
2026-05-12 11:11:30 +08:00
2026-05-12 11:21:31 +08:00
<h3>
本地视频
</h3>
2026-05-12 11:11:30 +08:00
2026-05-12 11:21:31 +08:00
<div
ref={
localVideoRef
}
style={{
2026-05-12 12:36:07 +08:00
width: 400,
height: 300,
2026-05-12 11:21:31 +08:00
background:
"#000",
2026-05-12 12:36:07 +08:00
borderRadius: 8,
overflow:
"hidden",
2026-05-12 11:21:31 +08:00
}}
/>
</div>
2026-05-12 11:11:30 +08:00
2026-05-12 11:21:31 +08:00
<div>
<h3>
远端视频
</h3>
2026-05-12 11:11:30 +08:00
<div
2026-05-12 11:21:31 +08:00
ref={
remoteVideoRef
}
style={{
2026-05-12 12:36:07 +08:00
width: 400,
height: 300,
2026-05-12 11:21:31 +08:00
background:
"#000",
2026-05-12 12:36:07 +08:00
borderRadius: 8,
overflow:
"hidden",
2026-05-12 11:21:31 +08:00
}}
/>
</div>
</div>
</Space>
2026-05-12 11:11:30 +08:00
</Card>
);
}