设备接入 优化
This commit is contained in:
@@ -1,34 +1,159 @@
|
||||
import VERTC, { RoomProfileType, StreamIndex } from "@volcengine/rtc";
|
||||
import VERTC, {
|
||||
RoomProfileType,
|
||||
StreamIndex,
|
||||
} from "@volcengine/rtc";
|
||||
|
||||
let engine;
|
||||
let engine = null;
|
||||
|
||||
export async function joinRoom(container, appId, roomId, token, userId) {
|
||||
console.info('joinRoom...');
|
||||
|
||||
const engine = VERTC.createEngine(appId);
|
||||
let currentRemoteUserId = null;
|
||||
|
||||
await engine.joinRoom(token, roomId, { userId }, {
|
||||
isAutoPublish: true,
|
||||
isAutoSubscribeAudio: true,
|
||||
isAutoSubscribeVideo: true,
|
||||
roomProfileType: RoomProfileType.meeting,
|
||||
});
|
||||
let isJoining = false;
|
||||
|
||||
engine.on(VERTC.events.onUserPublishStream, async (e) => {
|
||||
if (!container) return;
|
||||
|
||||
// 清空旧内容
|
||||
container.innerHTML = '';
|
||||
// =========================
|
||||
// 创建单例 engine
|
||||
// =========================
|
||||
|
||||
await engine.setUserVisibility(false);
|
||||
engine.setRemoteVideoPlayer(StreamIndex.STREAM_INDEX_MAIN, {
|
||||
userId: e.userId,
|
||||
renderDom: container,
|
||||
visible: true,
|
||||
});
|
||||
});
|
||||
function getEngine(appId) {
|
||||
if (!engine) {
|
||||
engine = VERTC.createEngine(appId);
|
||||
}
|
||||
|
||||
return engine;
|
||||
}
|
||||
|
||||
|
||||
// =========================
|
||||
// 加入房间
|
||||
// =========================
|
||||
|
||||
export async function joinRoom(
|
||||
container,
|
||||
appId,
|
||||
roomId,
|
||||
token,
|
||||
userId
|
||||
) {
|
||||
if (isJoining) {
|
||||
console.warn("正在 joinRoom,跳过");
|
||||
return;
|
||||
}
|
||||
|
||||
isJoining = true;
|
||||
|
||||
try {
|
||||
console.log("joinRoom start");
|
||||
|
||||
// 先离开旧房间
|
||||
await leaveRoom();
|
||||
|
||||
const rtcEngine = getEngine(appId);
|
||||
|
||||
// 防止重复监听
|
||||
rtcEngine.removeAllListeners();
|
||||
|
||||
// 监听远端流
|
||||
rtcEngine.on(
|
||||
VERTC.events.onUserPublishStream,
|
||||
async (e) => {
|
||||
console.log("收到远端流", e);
|
||||
|
||||
currentRemoteUserId = e.userId;
|
||||
|
||||
if (!container) return;
|
||||
|
||||
try {
|
||||
// 清空旧 video
|
||||
container.innerHTML = "";
|
||||
|
||||
// 隐藏本地
|
||||
await rtcEngine.setUserVisibility(false);
|
||||
|
||||
// 播放远端
|
||||
rtcEngine.setRemoteVideoPlayer(
|
||||
StreamIndex.STREAM_INDEX_MAIN,
|
||||
{
|
||||
userId: e.userId,
|
||||
renderDom: container,
|
||||
visible: true,
|
||||
}
|
||||
);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// 加入房间
|
||||
await rtcEngine.joinRoom(
|
||||
token,
|
||||
roomId,
|
||||
{ userId },
|
||||
{
|
||||
isAutoPublish: false,
|
||||
isAutoSubscribeAudio: false,
|
||||
isAutoSubscribeVideo: true,
|
||||
roomProfileType:
|
||||
RoomProfileType.meeting,
|
||||
}
|
||||
);
|
||||
|
||||
console.log("joinRoom success");
|
||||
} catch (err) {
|
||||
console.error("joinRoom failed", err);
|
||||
} finally {
|
||||
isJoining = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// =========================
|
||||
// 离开房间
|
||||
// =========================
|
||||
|
||||
export async function leaveRoom() {
|
||||
if (engine) await engine.leaveRoom();
|
||||
if (!engine) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("leaveRoom start");
|
||||
|
||||
try {
|
||||
// 移除事件
|
||||
engine.removeAllListeners();
|
||||
|
||||
// 停止远端渲染
|
||||
if (currentRemoteUserId) {
|
||||
try {
|
||||
engine.setRemoteVideoPlayer(
|
||||
StreamIndex.STREAM_INDEX_MAIN,
|
||||
{
|
||||
userId: currentRemoteUserId,
|
||||
renderDom: null,
|
||||
visible: false,
|
||||
}
|
||||
);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
// 离开房间
|
||||
try {
|
||||
await engine.leaveRoom();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
// 等待 SDK 内部释放
|
||||
await new Promise((resolve) =>
|
||||
setTimeout(resolve, 500)
|
||||
);
|
||||
|
||||
currentRemoteUserId = null;
|
||||
|
||||
console.log("leaveRoom success");
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user