| | |
| | | this.ua = null; |
| | | this.currentSession = null; |
| | | this.onStatusChange = null; // 状态变化回调 |
| | | this.onCallStatusChange = null; // 新增通话状态回调 |
| | | } |
| | | |
| | | // 初始化SIP客户端 |
| | |
| | | uri: config.sipUri, |
| | | password: config.password, |
| | | display_name: config.displayName, |
| | | iceservers:[], |
| | | iceservers: [], |
| | | // realm: config.realm, |
| | | register: true, |
| | | session_expires: 180, |
| | |
| | | "Allow: INVITE, ACK, BYE, CANCEL, OPTIONS", |
| | | ], |
| | | eventHandlers: { |
| | | progress: (e) => console.log("呼叫中..."), |
| | | failed: (e) => console.error("呼叫失败:", e), |
| | | ended: (e) => console.log("通话结束"), |
| | | confirmed: (e) => console.log("通话已接通"), |
| | | progress: (e) => { |
| | | this.updateCallStatus("calling", "呼叫中..."); |
| | | }, |
| | | failed: (e) => { |
| | | this.updateCallStatus("ended", `呼叫失败: ${e.cause}`); |
| | | }, |
| | | ended: (e) => { |
| | | this.updateCallStatus("ended", "通话结束"); |
| | | }, |
| | | confirmed: (e) => { |
| | | this.updateCallStatus("connected", "通话已接通"); |
| | | }, |
| | | }, |
| | | mediaConstraints: { |
| | | audio: true, |
| | |
| | | ); |
| | | // 在会话创建后修改 SDP |
| | | this.currentSession.on("peerconnection", (pc) => { |
| | | this.updateCallStatus('calling', '呼叫中...'); |
| | | pc.createOffer = (offerOptions) => { |
| | | return RTCPeerConnection.prototype.createOffer |
| | | .call(pc, offerOptions) |
| | |
| | | }); |
| | | }; |
| | | }); |
| | | this.currentSession.on('failed', (e) => { |
| | | this.updateCallStatus('failed', `呼叫失败2: ${e.cause}`); |
| | | }); |
| | | |
| | | this.currentSession.on('ended', () => { |
| | | this.updateCallStatus('ended', '通话已结束'); |
| | | }); |
| | | |
| | | this.currentSession.on('confirmed', () => { |
| | | this.updateCallStatus('connected', '通话已接通'); |
| | | }); |
| | | this.setupAudio(this.currentSession); |
| | | } |
| | | setupAudio(session) { |
| | |
| | | } |
| | | // 挂断当前通话 |
| | | endCall() { |
| | | if (this.currentSession) { |
| | | if (this.currentSession) { |
| | | this.currentSession.terminate(); |
| | | this.updateCallStatus('ended', '通话已结束'); |
| | | this.currentSession = null; |
| | | } |
| | | } |
| | | |
| | | // 新增方法:更新通话状态 |
| | | updateCallStatus(type, text) { |
| | | console.log(`通话状态更新: ${type} - ${text}`); |
| | | if (this.onCallStatusChange) { |
| | | this.onCallStatusChange({ type, text }); |
| | | } |
| | | } |
| | | } |
| | | |
| | | export default new SipService(); |