1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
| import RtcEngine from '@/components/Agora-RTC-JS/index' import { ClientRole, ChannelProfile } from '@/components/Agora-RTC-JS/common/Enums' import config from '@/common/agora.config' import permision from "@/js_sdk/wa-permission/permission"
class CallManager { constructor() { this.engine = null this.isInCall = false this.currentCallId = null this.remoteUsers = [] this.isVideo = false this.localMuted = false this.localVideoEnabled = true }
async initEngine() { if (this.engine) return this.engine try { this.engine = await RtcEngine.create(config.appId) this.addEngineListeners() await this.engine.setChannelProfile(ChannelProfile.Communication) await this.engine.setClientRole(ClientRole.Broadcaster) console.log('声网引擎初始化成功') return this.engine } catch (error) { console.error('声网引擎初始化失败:', error) throw error } }
addEngineListeners() { this.engine.addListener('JoinChannelSuccess', (channel, uid, elapsed) => { console.log('加入频道成功:', channel, uid) this.isInCall = true uni.$emit('callStatusChanged', { status: 'connected', uid }) })
this.engine.addListener('UserJoined', (uid, elapsed) => { console.log('远端用户加入:', uid) this.remoteUsers.push(uid) uni.$emit('remoteUserJoined', { uid }) })
this.engine.addListener('UserOffline', (uid, reason) => { console.log('远端用户离开:', uid, reason) this.remoteUsers = this.remoteUsers.filter(id => id !== uid) uni.$emit('remoteUserLeft', { uid, reason }) if (this.remoteUsers.length === 0) { this.endCall() } })
this.engine.addListener('LeaveChannel', (stats) => { console.log('离开频道:', stats) this.isInCall = false this.currentCallId = null this.remoteUsers = [] uni.$emit('callStatusChanged', { status: 'ended' }) })
this.engine.addListener('ConnectionStateChanged', (state, reason) => { console.log('网络状态变化:', state, reason) uni.$emit('networkStateChanged', { state, reason }) }) }
async startCall(targetUserId, isVideo = false) { try { if (!this.engine) { await this.initEngine() }
this.isVideo = isVideo await this.requestPermissions(isVideo) const callId = `call_${Date.now()}_${Math.random().toString(36).substr(2, 9)}` this.currentCallId = callId await this.engine.enableAudio() if (isVideo) { await this.engine.enableVideo() await this.engine.startPreview() }
await this.sendCallInvitation(targetUserId, callId, isVideo) await this.joinChannel(callId) return { success: true, callId } } catch (error) { console.error('发起通话失败:', error) return { success: false, error: error.message } } }
async acceptCall(callId, isVideo = false) { try { if (!this.engine) { await this.initEngine() }
this.isVideo = isVideo this.currentCallId = callId await this.requestPermissions(isVideo) await this.engine.enableAudio() if (isVideo) { await this.engine.enableVideo() await this.engine.startPreview() }
await this.joinChannel(callId) await this.sendCallResponse(callId, 'accepted') return { success: true } } catch (error) { console.error('接受通话失败:', error) return { success: false, error: error.message } } }
async rejectCall(callId) { await this.sendCallResponse(callId, 'rejected') }
async endCall() { try { if (this.engine && this.isInCall) { await this.engine.leaveChannel() } if (this.currentCallId) { await this.sendCallResponse(this.currentCallId, 'ended') } this.isInCall = false this.currentCallId = null this.remoteUsers = [] return { success: true } } catch (error) { console.error('结束通话失败:', error) return { success: false, error: error.message } } }
async joinChannel(channelId) { const uid = 0 await this.engine.joinChannel(config.token, channelId, null, uid) }
async requestPermissions(needCamera = false) { if (uni.getSystemInfoSync().platform === 'android') { await permision.requestAndroidPermission('android.permission.RECORD_AUDIO') if (needCamera) { await permision.requestAndroidPermission('android.permission.CAMERA') } } }
async switchCamera() { if (this.engine && this.isVideo) { await this.engine.switchCamera() } }
async toggleMute() { if (this.engine) { this.localMuted = !this.localMuted await this.engine.enableLocalAudio(!this.localMuted) return this.localMuted } return false }
async toggleVideo() { if (this.engine) { this.localVideoEnabled = !this.localVideoEnabled await this.engine.enableLocalVideo(this.localVideoEnabled) return this.localVideoEnabled } return false }
async sendCallInvitation(targetUserId, callId, isVideo) { const SignalingService = require('./SignalingService.js').default const message = { type: 'call_invitation', from: getCurrentUserId(), to: targetUserId, callId: callId, isVideo: isVideo, timestamp: Date.now() } await SignalingService.sendMessage(message) }
async sendCallResponse(callId, response) { const SignalingService = require('./SignalingService.js').default const message = { type: 'call_response', callId: callId, response: response, timestamp: Date.now() } await SignalingService.sendMessage(message) }
async destroy() { if (this.engine) { await this.engine.destroy() this.engine = null } } }
function getCurrentUserId() { return uni.getStorageSync('userId') || 'user_' + Date.now() }
export default new CallManager()
|