roomkit-web-vue3
Version:
<h1 align="center"> TUIRoomKit</h1> Conference (TUIRoomKit) is a product suitable for multi-person audio and video conversation scenarios such as business meetings, webinars, and online education. By integrating this product, you can add room management,
426 lines (425 loc) • 14.9 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import TUIRoomEngine__default, { TRTCVideoEncParam, TRTCVideoResolution, TUIRoomEvents, TUIRoomType, TUISeatMode, TUIVideoStreamType } from "@tencentcloud/tuiroom-engine-js";
import { EventType } from "../types.mjs";
import { isWeChat } from "../../utils/environment.mjs";
import logger from "../../utils/common/logger/index.mjs";
import "vue";
import "../../components/common/base/IconButton.vue2.mjs";
/* empty css */
/* empty css */
import "../../stores/room.mjs";
import "pinia";
/* empty css */
import "../../hooks/useZIndex.mjs";
import "../main.mjs";
import "../roomService.mjs";
import "../../locales/index.mjs";
import "mitt";
import "@tencentcloud/tui-core";
/* empty css */
/* empty css */
/* empty css */
/* empty css */
/* empty css */
import { useAudioDeviceState } from "../../core/hooks/useAudioDeviceState/index.mjs";
/* empty css */
/* empty css */
import "@tencentcloud/uikit-base-component-vue3";
/* empty css */
import "../../stores/basic.mjs";
/* empty css */
/* empty css */
/* empty css */
import "../../hooks/useRoomEngine.mjs";
import { useVideoDeviceState } from "../../core/hooks/useVideoDeviceState/index.mjs";
/* empty css */
/* empty css */
/* empty css */
/* empty css */
/* empty css */
/* empty css */
/* empty css */
const { microphone, speaker } = useAudioDeviceState();
const { camera } = useVideoDeviceState();
const logPrefix = "[RoomService:roomActionManager]";
const smallParam = new TRTCVideoEncParam();
smallParam.videoResolution = TRTCVideoResolution.TRTCVideoResolution_640_360;
smallParam.videoFps = 10;
smallParam.videoBitrate = 550;
class RoomActionManager {
constructor(service) {
__publicField(this, "service");
__publicField(this, "userListNextSequence", 0);
__publicField(this, "hasMoreUsers", false);
this.service = service;
this.onRoomUserCountChanged = this.onRoomUserCountChanged.bind(this);
this.bindRoomEngineEvents();
}
onRoomUserCountChanged(eventInfo) {
this.service.roomStore.setRoomMemberCount(eventInfo.userCount);
}
bindRoomEngineEvents() {
TUIRoomEngine__default.once("ready", () => {
var _a;
(_a = this.service.roomEngine.instance) == null ? void 0 : _a.on(
TUIRoomEvents.onRoomUserCountChanged,
this.onRoomUserCountChanged
);
});
}
unbindRoomEngineEvents() {
var _a;
(_a = this.service.roomEngine.instance) == null ? void 0 : _a.off(
TUIRoomEvents.onRoomUserCountChanged,
this.onRoomUserCountChanged
);
}
async start(roomId, params = {}) {
const {
roomName,
isSeatEnabled = false,
isOpenCamera = false,
isOpenMicrophone = false,
defaultCameraId,
defaultMicrophoneId,
defaultSpeakerId,
password
} = params;
const roomMode = isSeatEnabled ? "SpeakAfterTakingSeat" : "FreeToSpeak";
this.service.roomStore.resetRoomData();
await this.createRoom({
roomId,
roomName,
roomMode,
roomParam: {
isOpenCamera,
isOpenMicrophone,
defaultCameraId,
defaultMicrophoneId,
defaultSpeakerId,
password
}
});
await this.enterRoom({
roomId,
roomParam: {
isOpenCamera,
isOpenMicrophone,
defaultCameraId,
defaultMicrophoneId,
defaultSpeakerId,
password
}
});
this.service.emit(EventType.ROOM_START, { roomId });
}
async join(roomId, params = {}) {
const {
isOpenCamera = false,
isOpenMicrophone = false,
defaultCameraId,
defaultMicrophoneId,
defaultSpeakerId,
password
} = params;
this.service.roomStore.resetRoomData();
await this.enterRoom({
roomId,
roomParam: {
isOpenCamera,
isOpenMicrophone,
defaultCameraId,
defaultMicrophoneId,
defaultSpeakerId,
password
}
});
this.service.emit(EventType.ROOM_JOIN, { roomId });
}
async leaveRoom() {
var _a;
try {
this.closeMediaBeforeLeave();
const response = await ((_a = this.service.roomEngine.instance) == null ? void 0 : _a.exitRoom());
this.service.resetStore();
logger.log(`${logPrefix}leaveRoom:`, response);
this.service.emit(EventType.ROOM_LEAVE, response);
} catch (error) {
logger.error(`${logPrefix}leaveRoom error:`, error);
}
}
async dismissRoom() {
var _a;
try {
logger.log(`${logPrefix}dismissRoom: enter`);
this.closeMediaBeforeLeave();
await ((_a = this.service.roomEngine.instance) == null ? void 0 : _a.destroyRoom());
this.service.resetStore();
this.service.emit(EventType.ROOM_DISMISS, {});
} catch (error) {
logger.error(`${logPrefix}dismissRoom error:`, error);
}
}
async createRoom(options) {
try {
const { roomId, roomName, roomMode, roomParam } = options;
const roomParams = {
roomId,
roomName,
roomType: TUIRoomType.kConference,
isSeatEnabled: roomMode !== "FreeToSpeak",
seatMode: roomMode === "SpeakAfterTakingSeat" ? TUISeatMode.kApplyToTake : void 0,
password: (roomParam == null ? void 0 : roomParam.password) || ""
};
await this.handleRoomCreation(roomParams, options);
} catch (error) {
logger.error(`${logPrefix}createRoom error:`, error);
this.service.errorHandler.handleError(error, "createRoom");
throw error;
}
}
async handleRoomCreation(roomParams, options) {
var _a;
const { roomEngine } = this.service;
if (!roomEngine.instance) {
return;
}
this.service.basicStore.setRoomId(roomParams.roomId);
logger.debug(`${logPrefix}createRoom:`, roomParams, options);
await ((_a = roomEngine.instance) == null ? void 0 : _a.createRoom(roomParams));
}
async enterRoom(options) {
var _a;
try {
const { roomId, roomParam } = options;
const roomInfo = await this.doEnterRoom({
roomId,
roomType: TUIRoomType.kConference,
password: (roomParam == null ? void 0 : roomParam.password) || ""
});
this.service.roomStore.setRoomInfo(roomInfo);
await this.syncUserInfo(this.service.basicStore.userId);
await this.fetchAttendeeList(roomId);
await this.getInvitationList(roomId);
if (roomInfo.isSeatEnabled) {
await this.getSeatList();
this.service.roomStore.isMaster && await ((_a = this.service.roomEngine.instance) == null ? void 0 : _a.takeSeat({
seatIndex: -1,
timeout: 0
}));
}
this.setRoomParams(roomParam);
} catch (error) {
logger.error(`${logPrefix}enterRoom error:`, error);
this.service.errorHandler.handleError(error, "enterRoom");
throw error;
}
}
async setRoomParams(roomParam) {
if (!roomParam) {
return;
}
const {
isOpenCamera,
isOpenMicrophone,
defaultCameraId,
defaultMicrophoneId,
defaultSpeakerId
} = roomParam;
if (defaultCameraId) {
camera.setCurrentDevice({ deviceId: defaultCameraId });
}
if (defaultMicrophoneId) {
microphone.setCurrentDevice({ deviceId: defaultMicrophoneId });
}
if (defaultSpeakerId) {
speaker.setCurrentDevice({ deviceId: defaultSpeakerId });
}
const {
isMaster,
isMicrophoneDisableForAllUser,
isCameraDisableForAllUser,
isFreeSpeakMode
} = this.service.roomStore;
const isCanOpenMicrophone = isMaster || !isMicrophoneDisableForAllUser && isFreeSpeakMode;
if (isCanOpenMicrophone) {
if (isOpenMicrophone) {
await microphone.openLocalMicrophone();
await microphone.unmuteLocalAudio();
} else {
await microphone.muteLocalAudio();
}
}
const isCanOpenCamera = isMaster || !isCameraDisableForAllUser && isFreeSpeakMode;
if (isCanOpenCamera && isOpenCamera) {
camera.openLocalCamera();
}
}
async doEnterRoom(params) {
var _a, _b;
const { roomEngine } = this.service;
const { roomId, roomType, password } = params;
this.service.basicStore.setRoomId(roomId);
const isH5 = !isWeChat;
const trtcCloud = (_a = roomEngine.instance) == null ? void 0 : _a.getTRTCCloud();
trtcCloud == null ? void 0 : trtcCloud.setDefaultStreamRecvMode(true, false);
const roomInfo = await ((_b = roomEngine.instance) == null ? void 0 : _b.enterRoom({
roomId,
roomType,
options: {
password
}
}));
trtcCloud == null ? void 0 : trtcCloud.enableSmallVideoStream(!isH5, smallParam);
microphone.muteLocalAudio();
if (!roomInfo.isSeatEnabled) {
microphone.openLocalMicrophone();
}
return roomInfo;
}
async getUserList() {
var _a;
const { roomEngine } = this.service;
try {
const result = await ((_a = roomEngine.instance) == null ? void 0 : _a.getUserList({
nextSequence: 0
}));
result.userInfoList.forEach((user) => {
this.service.roomStore.addUserInfo(
Object.assign(user, { isInRoom: true })
);
if (this.service.roomStore.isFreeSpeakMode) {
this.service.roomStore.addStreamInfo(
user.userId,
TUIVideoStreamType.kCameraStream
);
}
});
this.userListNextSequence = result.nextSequence;
this.hasMoreUsers = result.nextSequence !== 0;
} catch (error) {
logger.error("TUIRoomEngine.getUserList", error.code, error.message);
}
}
async loadMoreUsers() {
var _a;
if (!this.hasMoreUsers) return;
const { roomEngine } = this.service;
try {
const result = await ((_a = roomEngine.instance) == null ? void 0 : _a.getUserList({
nextSequence: this.userListNextSequence
}));
result.userInfoList.forEach((user) => {
this.service.roomStore.addUserInfo(
Object.assign(user, { isInRoom: true })
);
if (this.service.roomStore.isFreeSpeakMode) {
this.service.roomStore.addStreamInfo(
user.userId,
TUIVideoStreamType.kCameraStream
);
}
});
this.userListNextSequence = result.nextSequence;
this.hasMoreUsers = result.nextSequence !== 0;
} catch (error) {
logger.error("TUIRoomEngine.loadMoreUsers", error.code, error.message);
}
}
async getInvitationList(roomId, cursor = "", result = []) {
const res = await this.service.conferenceInvitationManager.getInvitationList({
roomId,
cursor,
count: 20
});
if (!(res == null ? void 0 : res.invitationList)) return [];
result.push(...res == null ? void 0 : res.invitationList);
if (res.cursor !== "") {
await this.getInvitationList(roomId, res.cursor, result);
}
const list = result.map(({ invitee, status }) => ({
...invitee,
status
}));
this.service.roomStore.updateInviteeList(list);
}
async fetchAttendeeList(roomId, cursor = "", result = []) {
const res = await this.service.scheduleConferenceManager.fetchAttendeeList({
roomId,
cursor,
count: 20
});
if (!(res == null ? void 0 : res.attendeeList)) return [];
result.push(...res == null ? void 0 : res.attendeeList);
if (res.cursor !== "") {
await this.fetchAttendeeList(roomId, res.cursor, result);
}
const inviteeList = result.filter((user) => {
return !this.service.roomStore.userList.some(
(item) => item.userId === user.userId
);
});
this.service.roomStore.updateInviteeList(inviteeList);
}
async syncUserInfo(userId) {
var _a;
const { roomEngine } = this.service;
const userInfo = await ((_a = roomEngine.instance) == null ? void 0 : _a.getUserInfo({
userId
}));
const { isMessageDisabled } = userInfo;
this.service.chatStore.setSendMessageDisableChanged(isMessageDisabled);
}
async getSeatList() {
var _a;
const { roomEngine } = this.service;
try {
const seatList = await ((_a = roomEngine.instance) == null ? void 0 : _a.getSeatList());
await Promise.all(
seatList.map(async (seat) => {
var _a2;
const { userId } = seat;
if (!userId) {
return;
}
const user = this.service.roomStore.userInfoObj[userId];
if (user && user.userName) {
this.service.roomStore.updateUserInfo({ userId, onSeat: true });
} else {
const userInfo = await ((_a2 = roomEngine.instance) == null ? void 0 : _a2.getUserInfo({
userId
}));
this.service.roomStore.addUserInfo(
Object.assign(userInfo, { onSeat: true, isInRoom: true })
);
}
this.service.roomStore.addStreamInfo(
userId,
TUIVideoStreamType.kCameraStream
);
})
);
} catch (error) {
logger.error("TUIRoomEngine.getSeatList", error.code, error.message);
}
}
closeMediaBeforeLeave() {
var _a, _b;
const { roomEngine } = this.service;
if (this.service.roomStore.localUser.hasAudioStream) {
(_a = roomEngine.instance) == null ? void 0 : _a.closeLocalMicrophone();
}
if (this.service.roomStore.localUser.hasVideoStream) {
(_b = roomEngine.instance) == null ? void 0 : _b.closeLocalCamera();
}
}
async fetchRoomInfo(options) {
var _a;
return await ((_a = this.service.roomEngine.instance) == null ? void 0 : _a.fetchRoomInfo(options));
}
}
export {
RoomActionManager
};