|
|
@@ -1,10 +1,36 @@
|
|
|
import TRTC from 'trtc-sdk-v5'
|
|
|
import { useNotification } from 'naive-ui'
|
|
|
+import { ref } from 'vue'
|
|
|
+import userData from '@/utils/userdata'
|
|
|
+interface RoomOption {
|
|
|
+ userId: string,
|
|
|
+ roomId: number,
|
|
|
+ sdkAppId: number,
|
|
|
+ userSig: string
|
|
|
+}
|
|
|
+
|
|
|
+export interface UserList {
|
|
|
+ userName: string,
|
|
|
+ userId: number,
|
|
|
+ isMute: boolean, // 是否静音
|
|
|
+ isVideo: boolean, // 是否有视频
|
|
|
+ isState: boolean, // 是否到场
|
|
|
+ manageId: number,
|
|
|
+ type: boolean,
|
|
|
+ orgName: string,
|
|
|
+ isCreate: number,
|
|
|
+ isMe: boolean
|
|
|
+}
|
|
|
+
|
|
|
class meetingService {
|
|
|
private trtc: any = null
|
|
|
|
|
|
private notification = useNotification()
|
|
|
|
|
|
+ public list = ref([] as UserList[])
|
|
|
+
|
|
|
+ private loginInfo = userData.getUserData().loginInfo
|
|
|
+
|
|
|
// 监测设备
|
|
|
private async detectionDevice() {
|
|
|
let status = true
|
|
|
@@ -28,15 +54,19 @@ class meetingService {
|
|
|
}
|
|
|
|
|
|
// 加入房间+发布local video|audio
|
|
|
- public async joinRoom(options: { userId: string, roomId: number }, videoDomID: string) {
|
|
|
+ public async joinRoom(options: RoomOption, videoDomID: string) {
|
|
|
if (!this.trtc) return
|
|
|
await this.trtc.enterRoom(options)
|
|
|
- // 推送音视频
|
|
|
- await this.trtc.startLocalVideo({
|
|
|
- view: document.getElementById(videoDomID) // 在 DOM 中的 elementId 为 localVideo 的标签上预览视频。
|
|
|
+ this.isStartLocalVideo(false, videoDomID)
|
|
|
+ this.isStartLocalAuido(false)
|
|
|
+ const { userId } = options
|
|
|
+ this.list.value.forEach((el) => {
|
|
|
+ if (+userId === el.userId) {
|
|
|
+ el.isState = true
|
|
|
+ el.isMute = false
|
|
|
+ el.isVideo = false
|
|
|
+ }
|
|
|
})
|
|
|
- // 采集默认麦克风并发布
|
|
|
- await this.trtc.startLocalAudio()
|
|
|
}
|
|
|
|
|
|
// 退出房间
|
|
|
@@ -49,15 +79,43 @@ class meetingService {
|
|
|
}
|
|
|
|
|
|
// 是否静音远程用户
|
|
|
- public async stopRomoteAudio(userId:string, mute:boolean) {
|
|
|
+ public async isStartRomoteAudio(userId: string, mute: boolean) {
|
|
|
if (!this.trtc) return
|
|
|
await this.trtc.muteRemoteAudio(userId, mute)
|
|
|
}
|
|
|
|
|
|
- // 关闭本地视频推送
|
|
|
- public async stopLocalVideo() {
|
|
|
+ // 是否本地视频推送
|
|
|
+ public async isStartLocalVideo(mute: boolean, videoDomID: string) {
|
|
|
if (!this.trtc) return
|
|
|
- await this.trtc.stopLocalVideo()
|
|
|
+ if (mute) await this.trtc.stopLocalVideo()
|
|
|
+ if (!mute) await this.trtc.startLocalVideo({ view: document.getElementById(videoDomID) })
|
|
|
+ }
|
|
|
+
|
|
|
+ // 是否本地音频推送
|
|
|
+ public async isStartLocalAuido(mute: boolean) {
|
|
|
+ if (!this.trtc) return
|
|
|
+ if (mute) await this.trtc.stopLocalAudio()
|
|
|
+ if (!mute) await this.trtc.startLocalAudio()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置参会人员
|
|
|
+ public setUserList(userList: UserList[]) {
|
|
|
+ // 找出自己
|
|
|
+ const arr = []
|
|
|
+ for (let k = 0; k < userList.length; k++) {
|
|
|
+ const el = userList[k]
|
|
|
+ if (el.isCreate) {
|
|
|
+ arr.unshift({
|
|
|
+ ...el, isMute: true, isVideo: true, isState: false, isMe: this.loginInfo.userId === el.userId, type: !!el.isCreate
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ arr.push({
|
|
|
+ ...el, isMute: true, isVideo: true, isState: false, isMe: this.loginInfo.userId === el.userId, type: !!el.isCreate
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ console.log('参会人员', arr)
|
|
|
+ this.list.value = arr
|
|
|
}
|
|
|
|
|
|
// 初始化
|
|
|
@@ -67,9 +125,7 @@ class meetingService {
|
|
|
this.trtc = TRTC.create()
|
|
|
// 检测设备
|
|
|
const status = await this.detectionDevice()
|
|
|
- if (!status) return this.notification.error({ content: '请检查你的麦克风或扬声器是否正常' })
|
|
|
- // 加入房间
|
|
|
-
|
|
|
+ if (!status) this.notification.error({ content: '请检查你的麦克风或扬声器是否正常' })
|
|
|
// 监听房间事件
|
|
|
this.trtc.on(TRTC.EVENT.ERROR, this.handleError.bind(this))
|
|
|
// this.trtc.on(TRTC.EVENT.KICKED_OUT, this.handleKickedOut.bind(this))
|
|
|
@@ -92,33 +148,81 @@ class meetingService {
|
|
|
}
|
|
|
|
|
|
// 远程用户加入
|
|
|
- private handleRemoteUserEnter(event: any) {
|
|
|
+ private handleRemoteUserEnter(event: { userId: string }) {
|
|
|
console.log('远程用户加入', event)
|
|
|
+ const { userId } = event
|
|
|
+ this.list.value.forEach((el) => {
|
|
|
+ if (+userId === el.userId) {
|
|
|
+ el.isState = true
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
// 远程用户离开
|
|
|
- private handleRemoteUserExit(event: any) {
|
|
|
+ private handleRemoteUserExit(event: { userId: string }) {
|
|
|
console.log('远程用户离开', event)
|
|
|
+ const { userId } = event
|
|
|
+ this.list.value.forEach((el) => {
|
|
|
+ if (+userId === el.userId) {
|
|
|
+ el.isState = false
|
|
|
+ el.isMute = true
|
|
|
+ el.isVideo = true
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
// 远程用户音频不可用
|
|
|
- private handleRemoteAudioUnavailable(event: any) {
|
|
|
+ private handleRemoteAudioUnavailable(event: { userId: string }) {
|
|
|
console.log('远程用户音频不可用', event)
|
|
|
+ const { userId } = event
|
|
|
+ this.list.value.forEach((el) => {
|
|
|
+ if (+userId === el.userId) {
|
|
|
+ el.isMute = true
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
// 远程用户音频可用
|
|
|
- private handleRemoteAudioAvailable(event: any) {
|
|
|
+ private handleRemoteAudioAvailable(event: { userId: string }) {
|
|
|
console.log('远程用户音频可用', event)
|
|
|
+ const { userId } = event
|
|
|
+ this.list.value.forEach((el) => {
|
|
|
+ if (+userId === el.userId) {
|
|
|
+ el.isMute = false
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
// 远程用户视频不可用
|
|
|
- private handleRemoteVideoUnavailable(event: any) {
|
|
|
+ private handleRemoteVideoUnavailable(event: { userId: string; streamType: string }) {
|
|
|
console.log('远程用户视频不可用', event)
|
|
|
+ const { userId, streamType } = event
|
|
|
+ this.trtc.updateRemoteVideo({
|
|
|
+ userId,
|
|
|
+ streamType,
|
|
|
+ view: `D-${userId}`
|
|
|
+ })
|
|
|
+ this.list.value.forEach((el) => {
|
|
|
+ if (+userId === el.userId) {
|
|
|
+ el.isVideo = true
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
// 远程用户视频可用
|
|
|
- private handleRemoteVideoAvailable(event: any) {
|
|
|
+ private handleRemoteVideoAvailable(event: { userId: string; streamType: string }) {
|
|
|
console.log('远程用户视频可用', event)
|
|
|
+ const { userId, streamType } = event
|
|
|
+ this.trtc.startRemoteVideo({
|
|
|
+ userId,
|
|
|
+ streamType,
|
|
|
+ view: `D-${userId}`
|
|
|
+ })
|
|
|
+ this.list.value.forEach((el) => {
|
|
|
+ if (+userId === el.userId) {
|
|
|
+ el.isVideo = false
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
private destroy() {
|