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