288 lines
3.7 KiB
TypeScript
288 lines
3.7 KiB
TypeScript
|
|
import React, { useEffect, useState } from "react";
|
|||
|
|
import AgoraRTC, {
|
|||
|
|
IMicrophoneAudioTrack,
|
|||
|
|
} from "agora-rtc-sdk-ng";
|
|||
|
|
|
|||
|
|
import {
|
|||
|
|
Button,
|
|||
|
|
Card,
|
|||
|
|
Badge,
|
|||
|
|
message,
|
|||
|
|
} 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 AgoraRtcTest() {
|
|||
|
|
|
|||
|
|
const [uid] = useState(() =>
|
|||
|
|
String(Math.floor(Math.random() * 100000))
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
const [connected, setConnected] =
|
|||
|
|
useState(false);
|
|||
|
|
|
|||
|
|
const [published, setPublished] =
|
|||
|
|
useState(false);
|
|||
|
|
|
|||
|
|
const [remoteUsers, setRemoteUsers] =
|
|||
|
|
useState<any[]>([]);
|
|||
|
|
|
|||
|
|
useEffect(() => {
|
|||
|
|
|
|||
|
|
const init = async () => {
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
|
|||
|
|
// 连接状态
|
|||
|
|
client.on(
|
|||
|
|
"connection-state-change",
|
|||
|
|
(state) => {
|
|||
|
|
|
|||
|
|
console.log(
|
|||
|
|
"连接状态:",
|
|||
|
|
state
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
if (
|
|||
|
|
state === "CONNECTED"
|
|||
|
|
) {
|
|||
|
|
|
|||
|
|
setConnected(true);
|
|||
|
|
|
|||
|
|
message.success(
|
|||
|
|
"RTC 已连接"
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// 远端用户发布
|
|||
|
|
client.on(
|
|||
|
|
"user-published",
|
|||
|
|
async (user, mediaType) => {
|
|||
|
|
|
|||
|
|
console.log(
|
|||
|
|
"收到远端流:",
|
|||
|
|
user.uid,
|
|||
|
|
mediaType
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
await client.subscribe(
|
|||
|
|
user,
|
|||
|
|
mediaType
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
if (
|
|||
|
|
mediaType === "audio"
|
|||
|
|
) {
|
|||
|
|
|
|||
|
|
user.audioTrack?.play();
|
|||
|
|
|
|||
|
|
message.success(
|
|||
|
|
`收到用户 ${user.uid} 音频流`
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
setRemoteUsers([
|
|||
|
|
...client.remoteUsers
|
|||
|
|
]);
|
|||
|
|
}
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// 用户离开
|
|||
|
|
client.on(
|
|||
|
|
"user-left",
|
|||
|
|
(user) => {
|
|||
|
|
|
|||
|
|
console.log(
|
|||
|
|
"用户离开:",
|
|||
|
|
user.uid
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
setRemoteUsers([
|
|||
|
|
...client.remoteUsers
|
|||
|
|
]);
|
|||
|
|
}
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// 加入频道
|
|||
|
|
await client.join(
|
|||
|
|
APP_ID,
|
|||
|
|
CHANNEL,
|
|||
|
|
TOKEN,
|
|||
|
|
uid
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
console.log(
|
|||
|
|
"加入频道成功"
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
} catch (err) {
|
|||
|
|
|
|||
|
|
console.error(err);
|
|||
|
|
|
|||
|
|
message.error(
|
|||
|
|
"初始化失败"
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
init();
|
|||
|
|
|
|||
|
|
return () => {
|
|||
|
|
|
|||
|
|
client.leave();
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
}, []);
|
|||
|
|
|
|||
|
|
// 推流
|
|||
|
|
const startPublish =
|
|||
|
|
async () => {
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
|
|||
|
|
// 创建麦克风轨道
|
|||
|
|
const micTrack:
|
|||
|
|
IMicrophoneAudioTrack =
|
|||
|
|
await AgoraRTC.createMicrophoneAudioTrack();
|
|||
|
|
|
|||
|
|
// 推流
|
|||
|
|
await client.publish([
|
|||
|
|
micTrack
|
|||
|
|
]);
|
|||
|
|
|
|||
|
|
console.log(
|
|||
|
|
"推流成功"
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
message.success(
|
|||
|
|
"开始推流"
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
setPublished(true);
|
|||
|
|
|
|||
|
|
} catch (err) {
|
|||
|
|
|
|||
|
|
console.error(
|
|||
|
|
"推流失败:",
|
|||
|
|
err
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
message.error(
|
|||
|
|
"推流失败"
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
return (
|
|||
|
|
|
|||
|
|
<Card
|
|||
|
|
title="Agora RTC 推拉流测试"
|
|||
|
|
style={{
|
|||
|
|
width: 700,
|
|||
|
|
margin: "30px auto",
|
|||
|
|
}}
|
|||
|
|
>
|
|||
|
|
|
|||
|
|
<div
|
|||
|
|
style={{
|
|||
|
|
marginBottom: 12,
|
|||
|
|
}}
|
|||
|
|
>
|
|||
|
|
当前 UID:{uid}
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div
|
|||
|
|
style={{
|
|||
|
|
marginBottom: 12,
|
|||
|
|
}}
|
|||
|
|
>
|
|||
|
|
|
|||
|
|
<Badge
|
|||
|
|
color={
|
|||
|
|
connected
|
|||
|
|
? "green"
|
|||
|
|
: "red"
|
|||
|
|
}
|
|||
|
|
text={
|
|||
|
|
connected
|
|||
|
|
? "RTC 已连接"
|
|||
|
|
: "RTC 未连接"
|
|||
|
|
}
|
|||
|
|
/>
|
|||
|
|
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div
|
|||
|
|
style={{
|
|||
|
|
marginBottom: 20,
|
|||
|
|
}}
|
|||
|
|
>
|
|||
|
|
|
|||
|
|
<Badge
|
|||
|
|
color={
|
|||
|
|
published
|
|||
|
|
? "green"
|
|||
|
|
: "orange"
|
|||
|
|
}
|
|||
|
|
text={
|
|||
|
|
published
|
|||
|
|
? "已推流"
|
|||
|
|
: "未推流"
|
|||
|
|
}
|
|||
|
|
/>
|
|||
|
|
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<Button
|
|||
|
|
type="primary"
|
|||
|
|
onClick={startPublish}
|
|||
|
|
disabled={
|
|||
|
|
!connected ||
|
|||
|
|
published
|
|||
|
|
}
|
|||
|
|
>
|
|||
|
|
开始推流
|
|||
|
|
</Button>
|
|||
|
|
|
|||
|
|
<div
|
|||
|
|
style={{
|
|||
|
|
marginTop: 30,
|
|||
|
|
}}
|
|||
|
|
>
|
|||
|
|
|
|||
|
|
<h3>
|
|||
|
|
远端用户:
|
|||
|
|
</h3>
|
|||
|
|
|
|||
|
|
{remoteUsers.map(
|
|||
|
|
(user) => (
|
|||
|
|
|
|||
|
|
<div
|
|||
|
|
key={user.uid}
|
|||
|
|
>
|
|||
|
|
用户:
|
|||
|
|
{user.uid}
|
|||
|
|
</div>
|
|||
|
|
)
|
|||
|
|
)}
|
|||
|
|
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
</Card>
|
|||
|
|
);
|
|||
|
|
}
|