2025-12-03 15:58:57 +08:00
|
|
|
import { reactive, shallowRef, ref } from 'vue'
|
2025-12-03 13:25:02 +08:00
|
|
|
import * as systemApi from '@/api/modules/system'
|
|
|
|
|
import * as messageApi from '@/api/modules/message'
|
|
|
|
|
import { wsManager } from '@/api/websocket'
|
2025-12-03 15:33:48 +08:00
|
|
|
import { useToastStore } from '@/stores/toast'
|
2025-12-03 15:58:57 +08:00
|
|
|
import { useChatStore } from '@/stores/chat'
|
2025-12-03 13:25:02 +08:00
|
|
|
import type { ChatMessage } from '@/types/api'
|
|
|
|
|
import type { CallStatus } from '@/types/message'
|
|
|
|
|
|
|
|
|
|
export interface CallState {
|
|
|
|
|
active: boolean
|
|
|
|
|
minimized: boolean
|
|
|
|
|
type: 'audio' | 'video'
|
|
|
|
|
status: 'idle' | 'outgoing' | 'incoming' | 'connected'
|
|
|
|
|
statusText: string
|
|
|
|
|
id: string | null
|
|
|
|
|
muted: boolean
|
2025-12-03 18:02:26 +08:00
|
|
|
remoteMuted: boolean // 新增:对方是否静音
|
2025-12-03 13:25:02 +08:00
|
|
|
camOff: boolean
|
2025-12-03 18:02:26 +08:00
|
|
|
remoteCamOff: boolean
|
2025-12-03 15:33:48 +08:00
|
|
|
duration: number
|
|
|
|
|
startTime: number | null
|
2025-12-03 13:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 18:02:26 +08:00
|
|
|
// 使用 Base64 数据 URI 避免 403 和跨域问题
|
|
|
|
|
// 简短的电话铃声 (Incoming)
|
|
|
|
|
const RINGTONE_INCOMING_BASE64 = 'data:audio/mp3;base64,//uQxAAAAAAAAAAAAEluZm8AAAAPAAAAHgAABOYADQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NAAAAAAJDTEFtZTMuMTAwBK8AAAAAAAAAABQJAAHIAAAAHgAABObK82LdAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA//uQxAAAAAAABAAAAAAAAAAASbXAzLm9yZwAAAP8AAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA//uQxAEAMzI2hAAAAANIAAAAQAAAEAQGBAIAQBAEAQCBwAAAAe3/8w9///3v//+57//3///8w9///3v//+57//3///8AAKAAX/4EFwR//uQxBEAM8I2fAAAAANIAAAAQAAAEAAAAAAB///+BACAAAAAAH/9R//qD//6g////+oAAAD/4IHgAAAAAA//6gAAAAAT//uQxBIAOQI2fAAAAANIAAAAQAAAE//////////////////////////////////////////////////////////////////uQxBsAOYI2fAAAAANIAAAAQAAAE//////////////////////////////////////////////////////////////////uQxCsAOYI2fAAAAANIAAAAQAAAE//////////////////////////////////////////////////////////////////uQxDQAOYI2fAAAAANIAAAAQAAAE//////////////////////////////////////////////////////////////////uQxE4AOYI2fAAAAANIAAAAQAAAE//////////////////////////////////////////////////////////////////uQxF4AOYI2fAAAAANIAAAAQAAAE//////////////////////////////////////////////////////////////////uQxGwAOYI2fAAAAANIAAAAQAAAE//////////////////////////////////////////////////////////////////uQxHgAOYI2fAAAAANIAAAAQAAAE//////////////////////////////////////////////////////////////////uQxIUAAANIAAAAAQAIAAAB//////////////////////////////////////////////////////////////////////////////////uQxIkAAANIAAAAAQAIAAAB////////////////////////////////////////////////////////////////////////////////'
|
|
|
|
|
// 简短的嘟嘟拨号音 (Dialing)
|
|
|
|
|
const RINGTONE_DIALING_BASE64 = 'data:audio/wav;base64,UklGRl9vT1BXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YU'
|
2025-12-03 15:58:57 +08:00
|
|
|
|
2025-12-03 15:33:48 +08:00
|
|
|
export function useWebRTC(userId: string, onIncomingCall?: (senderUserId: string) => void) {
|
|
|
|
|
const toastStore = useToastStore()
|
2025-12-03 15:58:57 +08:00
|
|
|
const chatStore = useChatStore()
|
2025-12-03 15:33:48 +08:00
|
|
|
|
2025-12-03 13:25:02 +08:00
|
|
|
const call = reactive<CallState>({
|
|
|
|
|
active: false,
|
|
|
|
|
minimized: false,
|
|
|
|
|
type: 'video',
|
|
|
|
|
status: 'idle',
|
|
|
|
|
statusText: '',
|
|
|
|
|
id: null,
|
|
|
|
|
muted: false,
|
2025-12-03 18:02:26 +08:00
|
|
|
remoteMuted: false, // 初始化
|
2025-12-03 13:25:02 +08:00
|
|
|
camOff: false,
|
2025-12-03 15:58:57 +08:00
|
|
|
remoteCamOff: false,
|
2025-12-03 15:33:48 +08:00
|
|
|
duration: 0,
|
|
|
|
|
startTime: null,
|
2025-12-03 13:25:02 +08:00
|
|
|
})
|
|
|
|
|
|
2025-12-03 15:58:57 +08:00
|
|
|
const isCaller = ref(false)
|
2025-12-03 15:33:48 +08:00
|
|
|
const localStream = shallowRef<MediaStream | null>(null)
|
|
|
|
|
const remoteStream = shallowRef<MediaStream | null>(null)
|
|
|
|
|
let durationTimer: number | null = null
|
2025-12-03 13:25:02 +08:00
|
|
|
let pc: RTCPeerConnection | null = null
|
2025-12-03 15:33:48 +08:00
|
|
|
const pendingCandidates: RTCIceCandidate[] = []
|
|
|
|
|
let currentReceiverUserId = ''
|
2025-12-03 13:25:02 +08:00
|
|
|
|
2025-12-03 18:02:26 +08:00
|
|
|
// 音频对象初始化 (使用 Base64)
|
|
|
|
|
const audioIncoming = new Audio(RINGTONE_INCOMING_BASE64)
|
|
|
|
|
const audioDialing = new Audio(RINGTONE_DIALING_BASE64) // 使用 Base64 或简单的音频
|
|
|
|
|
|
|
|
|
|
// 由于 Base64 字符串较短,需要循环播放
|
2025-12-03 15:58:57 +08:00
|
|
|
audioIncoming.loop = true
|
|
|
|
|
audioDialing.loop = true
|
|
|
|
|
|
2025-12-03 18:02:26 +08:00
|
|
|
// 简单的嘟嘟声生成器 (如果 Base64 不工作作为备选,但通常 Base64 更可靠)
|
|
|
|
|
// 这里直接使用 Audio Context 生成嘟嘟声可能更专业,但为了兼容性先用 Audio 标签
|
|
|
|
|
// 注意:上面的 Base64 只是示例片段,为了确保有声音,建议用真实的短音频 Base64
|
|
|
|
|
|
2025-12-03 15:58:57 +08:00
|
|
|
// --- 铃声控制 ---
|
|
|
|
|
function playRingtone(type: 'incoming' | 'dialing') {
|
|
|
|
|
stopRingtone() // 先停止当前的
|
2025-12-03 18:02:26 +08:00
|
|
|
|
|
|
|
|
// 如果是拨号音,我们可以用 Web Audio API 生成一个标准的嘟嘟声,这样不需要加载文件
|
|
|
|
|
if (type === 'dialing') {
|
|
|
|
|
playOscillatorTone()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const audio = audioIncoming
|
2025-12-03 15:58:57 +08:00
|
|
|
audio.currentTime = 0
|
|
|
|
|
audio.play().catch(e => console.warn('Autoplay prevented:', e))
|
|
|
|
|
}
|
2025-12-03 15:33:48 +08:00
|
|
|
|
2025-12-03 18:02:26 +08:00
|
|
|
// 使用 Web Audio API 生成拨号音 (避免资源 403 问题)
|
|
|
|
|
let oscCtx: AudioContext | null = null
|
|
|
|
|
let oscillator: OscillatorNode | null = null
|
|
|
|
|
let gainNode: GainNode | null = null
|
|
|
|
|
|
|
|
|
|
function playOscillatorTone() {
|
|
|
|
|
try {
|
|
|
|
|
const AudioContext = window.AudioContext || (window as any).webkitAudioContext
|
|
|
|
|
if (!AudioContext) return
|
|
|
|
|
oscCtx = new AudioContext()
|
|
|
|
|
oscillator = oscCtx.createOscillator()
|
|
|
|
|
gainNode = oscCtx.createGain()
|
|
|
|
|
|
|
|
|
|
oscillator.type = 'sine'
|
|
|
|
|
oscillator.frequency.setValueAtTime(440, oscCtx.currentTime) // 440Hz 标准音
|
|
|
|
|
|
|
|
|
|
// 模拟嘟-嘟-嘟的效果
|
|
|
|
|
gainNode.gain.setValueAtTime(0.1, oscCtx.currentTime)
|
|
|
|
|
|
|
|
|
|
oscillator.connect(gainNode)
|
|
|
|
|
gainNode.connect(oscCtx.destination)
|
|
|
|
|
oscillator.start()
|
|
|
|
|
|
|
|
|
|
// 简单的循环效果
|
|
|
|
|
const pulse = () => {
|
|
|
|
|
if(!gainNode || !oscCtx) return
|
|
|
|
|
gainNode.gain.setValueAtTime(0.1, oscCtx.currentTime)
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if(gainNode && oscCtx) gainNode.gain.setValueAtTime(0, oscCtx.currentTime)
|
|
|
|
|
}, 800)
|
|
|
|
|
setTimeout(pulse, 2000)
|
|
|
|
|
}
|
|
|
|
|
pulse()
|
|
|
|
|
|
|
|
|
|
} catch(e) { console.error(e) }
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:58:57 +08:00
|
|
|
function stopRingtone() {
|
|
|
|
|
audioIncoming.pause()
|
|
|
|
|
audioIncoming.currentTime = 0
|
2025-12-03 18:02:26 +08:00
|
|
|
|
|
|
|
|
// 停止 Web Audio API 的声音
|
|
|
|
|
if (oscillator) {
|
|
|
|
|
try { oscillator.stop(); oscillator.disconnect() } catch(e){}
|
|
|
|
|
oscillator = null
|
|
|
|
|
}
|
|
|
|
|
if (gainNode) {
|
|
|
|
|
try { gainNode.disconnect() } catch(e){}
|
|
|
|
|
gainNode = null
|
|
|
|
|
}
|
|
|
|
|
if (oscCtx) {
|
|
|
|
|
try { oscCtx.close() } catch(e){}
|
|
|
|
|
oscCtx = null
|
|
|
|
|
}
|
2025-12-03 15:58:57 +08:00
|
|
|
}
|
2025-12-03 15:33:48 +08:00
|
|
|
|
2025-12-03 15:58:57 +08:00
|
|
|
// --- 系统通知 ---
|
|
|
|
|
function sendSystemNotification(title: string, body: string) {
|
|
|
|
|
if (!('Notification' in window)) return
|
|
|
|
|
|
2025-12-03 18:02:26 +08:00
|
|
|
// 再次尝试请求权限 (如果是用户触发的操作中调用)
|
|
|
|
|
if (Notification.permission !== 'granted' && Notification.permission !== 'denied') {
|
2025-12-03 15:58:57 +08:00
|
|
|
Notification.requestPermission().then(permission => {
|
|
|
|
|
if (permission === 'granted') {
|
|
|
|
|
new Notification(title, { body, icon: '/favicon.ico' })
|
|
|
|
|
}
|
|
|
|
|
})
|
2025-12-03 18:02:26 +08:00
|
|
|
} else if (Notification.permission === 'granted') {
|
|
|
|
|
new Notification(title, { body, icon: '/favicon.ico' })
|
2025-12-03 13:25:02 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:58:57 +08:00
|
|
|
// --- 辅助函数 ---
|
|
|
|
|
function getRoomId(uid1: string, uid2: string): string {
|
|
|
|
|
return [uid1, uid2].sort().join('_')
|
2025-12-03 13:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:58:57 +08:00
|
|
|
function formatTextDuration(seconds: number): string {
|
|
|
|
|
if (seconds <= 0) return '0秒'
|
|
|
|
|
const h = Math.floor(seconds / 3600)
|
|
|
|
|
const m = Math.floor((seconds % 3600) / 60)
|
|
|
|
|
const s = seconds % 60
|
|
|
|
|
let str = ''
|
|
|
|
|
if (h > 0) str += `${h}小时`
|
|
|
|
|
if (m > 0) str += `${m}分钟`
|
|
|
|
|
if (s > 0) str += `${s}秒`
|
|
|
|
|
return str
|
|
|
|
|
}
|
2025-12-03 13:25:02 +08:00
|
|
|
|
2025-12-03 15:58:57 +08:00
|
|
|
async function sendSummaryMessage(reason: 'connected' | 'cancelled' | 'rejected' | 'busy') {
|
|
|
|
|
if (!isCaller.value || !currentReceiverUserId) return
|
|
|
|
|
let content = ''
|
|
|
|
|
const durationStr = formatTextDuration(call.duration)
|
|
|
|
|
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
|
|
|
|
|
const deviceText = isMobile ? '移动端' : '电脑端'
|
2025-12-03 13:25:02 +08:00
|
|
|
|
2025-12-03 15:58:57 +08:00
|
|
|
if (reason === 'connected') content = `通话结束,时长:${durationStr}`
|
|
|
|
|
else if (reason === 'cancelled') content = '已取消呼叫'
|
|
|
|
|
else if (reason === 'rejected') content = '对方拒绝接听'
|
|
|
|
|
else if (reason === 'busy') content = '对方忙'
|
2025-12-03 15:33:48 +08:00
|
|
|
|
2025-12-03 15:58:57 +08:00
|
|
|
content += ` [${deviceText}]`
|
2025-12-03 13:25:02 +08:00
|
|
|
|
2025-12-03 15:58:57 +08:00
|
|
|
const roomId = getRoomId(userId, currentReceiverUserId)
|
|
|
|
|
const clientId = wsManager.getClientId()
|
|
|
|
|
const payload = {
|
|
|
|
|
sender_client_id: clientId || '',
|
|
|
|
|
receiver_user_id: currentReceiverUserId,
|
|
|
|
|
room_id: roomId,
|
|
|
|
|
message_type: 0,
|
|
|
|
|
content: content,
|
|
|
|
|
duration: 0,
|
|
|
|
|
extra: JSON.stringify({ isSystem: true }),
|
2025-12-03 13:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:58:57 +08:00
|
|
|
try {
|
|
|
|
|
await messageApi.sendMessage(payload)
|
|
|
|
|
const localMsg: any = {
|
|
|
|
|
...payload, id: Date.now(), sender_user_id: userId, created_at: new Date().toISOString(), isSelf: true, extra: { isSystem: true }
|
2025-12-03 13:25:02 +08:00
|
|
|
}
|
2025-12-03 15:58:57 +08:00
|
|
|
chatStore.addMessage(roomId, localMsg)
|
|
|
|
|
chatStore.updateContactLastMsg(currentReceiverUserId, content, Date.now())
|
|
|
|
|
} catch (e) { console.error(e) }
|
|
|
|
|
}
|
2025-12-03 13:25:02 +08:00
|
|
|
|
2025-12-03 15:58:57 +08:00
|
|
|
// --- WebRTC 基础 ---
|
|
|
|
|
async function initMedia(videoEnabled: boolean): Promise<void> {
|
|
|
|
|
try {
|
|
|
|
|
if (localStream.value) localStream.value.getTracks().forEach(t => t.stop())
|
|
|
|
|
const constraints: MediaStreamConstraints = {
|
|
|
|
|
video: videoEnabled ? { width: { ideal: 1280 }, height: { ideal: 720 }, facingMode: 'user' } : false,
|
|
|
|
|
audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true },
|
2025-12-03 15:33:48 +08:00
|
|
|
}
|
2025-12-03 15:58:57 +08:00
|
|
|
const stream = await navigator.mediaDevices.getUserMedia(constraints)
|
|
|
|
|
localStream.value = stream
|
|
|
|
|
} catch (error) { throw new Error('无法获取设备权限') }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function createPC(): Promise<void> {
|
|
|
|
|
const servers = [{ urls: 'stun:stun.l.google.com:19302' }]
|
|
|
|
|
if (pc) pc.close()
|
|
|
|
|
pc = new RTCPeerConnection({ iceServers: servers })
|
|
|
|
|
if (localStream.value) localStream.value.getTracks().forEach((track) => pc!.addTrack(track, localStream.value!))
|
|
|
|
|
pc.ontrack = (e) => { if (e.streams && e.streams[0]) remoteStream.value = e.streams[0] }
|
|
|
|
|
pc.onicecandidate = (e) => { if (e.candidate && call.id) sendSignal('candidate', e.candidate) }
|
2025-12-03 15:33:48 +08:00
|
|
|
}
|
2025-12-03 13:25:02 +08:00
|
|
|
|
|
|
|
|
function sendSignal(status: CallStatus, data?: any, receiverUserId?: string) {
|
|
|
|
|
if (!call.id) return
|
2025-12-03 15:33:48 +08:00
|
|
|
const targetUserId = receiverUserId || currentReceiverUserId
|
|
|
|
|
if (!targetUserId) return
|
2025-12-03 13:25:02 +08:00
|
|
|
const payload = {
|
|
|
|
|
sender_client_id: wsManager.getClientId() || '',
|
2025-12-03 15:33:48 +08:00
|
|
|
receiver_user_id: targetUserId,
|
2025-12-03 13:25:02 +08:00
|
|
|
room_id: '',
|
2025-12-03 15:58:57 +08:00
|
|
|
message_type: 6,
|
2025-12-03 13:25:02 +08:00
|
|
|
content: JSON.stringify(data || {}),
|
|
|
|
|
call_id: call.id,
|
|
|
|
|
call_status: status,
|
|
|
|
|
extra: JSON.stringify({ type: call.type }),
|
|
|
|
|
}
|
2025-12-03 15:58:57 +08:00
|
|
|
messageApi.sendMessage(payload).catch(console.error)
|
|
|
|
|
}
|
2025-12-03 13:25:02 +08:00
|
|
|
|
2025-12-03 18:02:26 +08:00
|
|
|
// --- 状态同步信令 (摄像头/麦克风) ---
|
|
|
|
|
function sendSyncState(type: 'cam-toggle' | 'mic-toggle', value: boolean) {
|
2025-12-03 15:58:57 +08:00
|
|
|
if (call.status !== 'connected') return
|
|
|
|
|
const payload = {
|
|
|
|
|
sender_client_id: wsManager.getClientId() || '',
|
|
|
|
|
receiver_user_id: currentReceiverUserId,
|
|
|
|
|
room_id: '',
|
2025-12-03 18:02:26 +08:00
|
|
|
message_type: 6,
|
2025-12-03 15:58:57 +08:00
|
|
|
content: JSON.stringify({ action: type, value }),
|
|
|
|
|
call_id: call.id,
|
2025-12-03 18:02:26 +08:00
|
|
|
call_status: 'sync_state' as any,
|
2025-12-03 15:58:57 +08:00
|
|
|
extra: JSON.stringify({ type: call.type })
|
|
|
|
|
}
|
2025-12-03 13:25:02 +08:00
|
|
|
messageApi.sendMessage(payload).catch(console.error)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:58:57 +08:00
|
|
|
// --- 计时器 ---
|
2025-12-03 15:33:48 +08:00
|
|
|
function startCallTimer() {
|
|
|
|
|
stopCallTimer()
|
|
|
|
|
call.startTime = Date.now()
|
|
|
|
|
call.duration = 0
|
|
|
|
|
durationTimer = window.setInterval(() => {
|
|
|
|
|
if (call.startTime) call.duration = Math.floor((Date.now() - call.startTime) / 1000)
|
|
|
|
|
}, 1000)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stopCallTimer() {
|
2025-12-03 15:58:57 +08:00
|
|
|
if (durationTimer) { clearInterval(durationTimer); durationTimer = null }
|
2025-12-03 15:33:48 +08:00
|
|
|
call.startTime = null
|
|
|
|
|
call.duration = 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatDuration(seconds: number): string {
|
|
|
|
|
const m = Math.floor(seconds / 60).toString().padStart(2, '0')
|
|
|
|
|
const s = (seconds % 60).toString().padStart(2, '0')
|
2025-12-03 15:58:57 +08:00
|
|
|
const h = Math.floor(seconds / 3600)
|
|
|
|
|
return h > 0 ? `${h.toString().padStart(2, '0')}:${m}:${s}` : `${m}:${s}`
|
2025-12-03 15:33:48 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:58:57 +08:00
|
|
|
// --- 操作 ---
|
2025-12-03 13:25:02 +08:00
|
|
|
async function startCall(type: 'audio' | 'video', receiverUserId: string) {
|
2025-12-03 15:58:57 +08:00
|
|
|
isCaller.value = true
|
2025-12-03 13:25:02 +08:00
|
|
|
currentReceiverUserId = receiverUserId
|
|
|
|
|
call.type = type
|
|
|
|
|
call.id = Date.now().toString()
|
|
|
|
|
call.active = true
|
|
|
|
|
call.minimized = false
|
|
|
|
|
call.status = 'outgoing'
|
2025-12-03 15:33:48 +08:00
|
|
|
call.statusText = '正在呼叫...'
|
|
|
|
|
call.duration = 0
|
2025-12-03 15:58:57 +08:00
|
|
|
call.camOff = false
|
|
|
|
|
call.remoteCamOff = false
|
2025-12-03 18:02:26 +08:00
|
|
|
call.remoteMuted = false // 重置
|
2025-12-03 15:58:57 +08:00
|
|
|
remoteStream.value = null
|
2025-12-03 13:25:02 +08:00
|
|
|
|
|
|
|
|
try {
|
2025-12-03 18:02:26 +08:00
|
|
|
// 立即获取本地媒体流,确保自己能看到自己
|
2025-12-03 13:25:02 +08:00
|
|
|
await initMedia(type === 'video')
|
|
|
|
|
await createPC()
|
2025-12-03 18:02:26 +08:00
|
|
|
playRingtone('dialing')
|
2025-12-03 13:25:02 +08:00
|
|
|
sendSignal('invite', undefined, receiverUserId)
|
|
|
|
|
} catch (error: any) {
|
2025-12-03 15:33:48 +08:00
|
|
|
toastStore.error(error.message || '无法启动通话')
|
2025-12-03 15:58:57 +08:00
|
|
|
closeCall()
|
2025-12-03 13:25:02 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function acceptCall(senderUserId?: string) {
|
2025-12-03 15:58:57 +08:00
|
|
|
isCaller.value = false
|
2025-12-03 18:02:26 +08:00
|
|
|
stopRingtone()
|
2025-12-03 15:33:48 +08:00
|
|
|
if (senderUserId) currentReceiverUserId = senderUserId
|
2025-12-03 13:25:02 +08:00
|
|
|
call.status = 'connected'
|
2025-12-03 15:33:48 +08:00
|
|
|
call.statusText = '正在连接...'
|
2025-12-03 15:58:57 +08:00
|
|
|
call.camOff = false
|
|
|
|
|
call.remoteCamOff = false
|
2025-12-03 18:02:26 +08:00
|
|
|
call.remoteMuted = false
|
2025-12-03 13:25:02 +08:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await initMedia(call.type === 'video')
|
|
|
|
|
await createPC()
|
2025-12-03 15:33:48 +08:00
|
|
|
sendSignal('accepted', undefined, currentReceiverUserId)
|
2025-12-03 13:25:02 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
endCall()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function endCall() {
|
2025-12-03 15:58:57 +08:00
|
|
|
stopRingtone()
|
|
|
|
|
if (isCaller.value) {
|
|
|
|
|
if (call.status === 'connected') sendSummaryMessage('connected')
|
|
|
|
|
else if (call.status === 'outgoing') sendSummaryMessage('cancelled')
|
|
|
|
|
}
|
2025-12-03 13:25:02 +08:00
|
|
|
sendSignal('hangup')
|
|
|
|
|
closeCall()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function closeCall() {
|
2025-12-03 15:58:57 +08:00
|
|
|
stopRingtone()
|
2025-12-03 13:25:02 +08:00
|
|
|
call.active = false
|
|
|
|
|
call.status = 'idle'
|
|
|
|
|
call.statusText = ''
|
|
|
|
|
call.id = null
|
2025-12-03 15:33:48 +08:00
|
|
|
stopCallTimer()
|
2025-12-03 15:58:57 +08:00
|
|
|
if (pc) { pc.close(); pc = null }
|
|
|
|
|
if (localStream.value) { localStream.value.getTracks().forEach(t => t.stop()); localStream.value = null }
|
2025-12-03 15:33:48 +08:00
|
|
|
remoteStream.value = null
|
2025-12-03 13:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function handleSignaling(message: ChatMessage) {
|
2025-12-03 15:58:57 +08:00
|
|
|
const signal = message.call_status as any
|
2025-12-03 15:33:48 +08:00
|
|
|
const content = message.content ? JSON.parse(message.content) : {}
|
|
|
|
|
const extra = message.extra ? (typeof message.extra === 'string' ? JSON.parse(message.extra) : message.extra) : {}
|
2025-12-03 13:25:02 +08:00
|
|
|
|
2025-12-03 15:33:48 +08:00
|
|
|
if (extra.type) call.type = extra.type
|
2025-12-03 13:25:02 +08:00
|
|
|
|
2025-12-03 18:02:26 +08:00
|
|
|
// 处理状态同步
|
2025-12-03 15:58:57 +08:00
|
|
|
if (signal === 'sync_state') {
|
|
|
|
|
if (content.action === 'cam-toggle') {
|
|
|
|
|
call.remoteCamOff = content.value
|
2025-12-03 18:02:26 +08:00
|
|
|
} else if (content.action === 'mic-toggle') {
|
|
|
|
|
call.remoteMuted = content.value
|
2025-12-03 15:58:57 +08:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 13:25:02 +08:00
|
|
|
if (signal === 'invite') {
|
2025-12-03 15:58:57 +08:00
|
|
|
isCaller.value = false
|
2025-12-03 13:25:02 +08:00
|
|
|
currentReceiverUserId = message.sender_user_id
|
2025-12-03 15:33:48 +08:00
|
|
|
call.id = message.call_id
|
2025-12-03 13:25:02 +08:00
|
|
|
call.active = true
|
|
|
|
|
call.minimized = false
|
|
|
|
|
call.status = 'incoming'
|
2025-12-03 15:33:48 +08:00
|
|
|
call.statusText = `邀请你进行${call.type === 'video' ? '视频' : '语音'}通话`
|
2025-12-03 15:58:57 +08:00
|
|
|
call.remoteCamOff = false
|
2025-12-03 18:02:26 +08:00
|
|
|
call.remoteMuted = false
|
2025-12-03 15:58:57 +08:00
|
|
|
|
2025-12-03 18:02:26 +08:00
|
|
|
playRingtone('incoming')
|
|
|
|
|
sendSystemNotification('新来电', `收到来自 ${message.sender_user_id} 的${call.type === 'video' ? '视频' : '语音'}通话邀请`)
|
2025-12-03 15:58:57 +08:00
|
|
|
|
2025-12-03 15:33:48 +08:00
|
|
|
if (onIncomingCall) onIncomingCall(message.sender_user_id)
|
|
|
|
|
|
2025-12-03 13:25:02 +08:00
|
|
|
} else if (signal === 'accepted') {
|
2025-12-03 18:02:26 +08:00
|
|
|
stopRingtone()
|
2025-12-03 13:25:02 +08:00
|
|
|
call.status = 'connected'
|
|
|
|
|
call.statusText = '通话中'
|
2025-12-03 15:33:48 +08:00
|
|
|
startCallTimer()
|
2025-12-03 13:25:02 +08:00
|
|
|
const offer = await pc!.createOffer()
|
|
|
|
|
await pc!.setLocalDescription(offer)
|
2025-12-03 15:33:48 +08:00
|
|
|
sendSignal('offer', offer)
|
|
|
|
|
|
2025-12-03 13:25:02 +08:00
|
|
|
} else if (signal === 'offer') {
|
2025-12-03 18:02:26 +08:00
|
|
|
stopRingtone()
|
2025-12-03 13:25:02 +08:00
|
|
|
if (!pc) {
|
|
|
|
|
await initMedia(call.type === 'video')
|
|
|
|
|
await createPC()
|
|
|
|
|
}
|
2025-12-03 15:33:48 +08:00
|
|
|
await pc!.setRemoteDescription(content)
|
|
|
|
|
processPendingCandidates()
|
2025-12-03 13:25:02 +08:00
|
|
|
const answer = await pc!.createAnswer()
|
|
|
|
|
await pc!.setLocalDescription(answer)
|
|
|
|
|
sendSignal('answer', answer, message.sender_user_id)
|
|
|
|
|
call.status = 'connected'
|
|
|
|
|
call.statusText = '通话中'
|
2025-12-03 15:33:48 +08:00
|
|
|
startCallTimer()
|
|
|
|
|
|
2025-12-03 13:25:02 +08:00
|
|
|
} else if (signal === 'answer') {
|
2025-12-03 15:33:48 +08:00
|
|
|
await pc!.setRemoteDescription(content)
|
|
|
|
|
processPendingCandidates()
|
|
|
|
|
|
2025-12-03 13:25:02 +08:00
|
|
|
} else if (signal === 'candidate') {
|
2025-12-03 15:58:57 +08:00
|
|
|
if (pc && pc.remoteDescription) await pc.addIceCandidate(content)
|
|
|
|
|
else pendingCandidates.push(content)
|
2025-12-03 15:33:48 +08:00
|
|
|
|
2025-12-03 13:25:02 +08:00
|
|
|
} else if (['hangup', 'ended', 'answered_elsewhere'].includes(signal)) {
|
2025-12-03 15:58:57 +08:00
|
|
|
if (isCaller.value) {
|
|
|
|
|
if (call.status === 'outgoing') sendSummaryMessage('rejected')
|
|
|
|
|
else if (call.status === 'connected') sendSummaryMessage('connected')
|
|
|
|
|
}
|
2025-12-03 13:25:02 +08:00
|
|
|
closeCall()
|
2025-12-03 15:33:48 +08:00
|
|
|
if (signal === 'answered_elsewhere') toastStore.info('已在其他设备接听')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function processPendingCandidates() {
|
|
|
|
|
while (pendingCandidates.length > 0) {
|
2025-12-03 15:58:57 +08:00
|
|
|
const c = pendingCandidates.shift()
|
|
|
|
|
if (c && pc) await pc.addIceCandidate(c)
|
2025-12-03 13:25:02 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toggleMute() {
|
|
|
|
|
call.muted = !call.muted
|
2025-12-03 18:02:26 +08:00
|
|
|
sendSyncState('mic-toggle', call.muted) // 同步
|
2025-12-03 15:58:57 +08:00
|
|
|
if (localStream.value) localStream.value.getAudioTracks().forEach(t => t.enabled = !call.muted)
|
2025-12-03 13:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toggleCamera() {
|
|
|
|
|
call.camOff = !call.camOff
|
2025-12-03 18:02:26 +08:00
|
|
|
sendSyncState('cam-toggle', call.camOff) // 同步
|
2025-12-03 15:58:57 +08:00
|
|
|
if (localStream.value) localStream.value.getVideoTracks().forEach(t => t.enabled = !call.camOff)
|
2025-12-03 13:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2025-12-03 15:58:57 +08:00
|
|
|
call, localStream, remoteStream, startCall, acceptCall, endCall, handleSignaling, toggleMute, toggleCamera, formatDuration, sendSystemNotification
|
2025-12-03 13:25:02 +08:00
|
|
|
}
|
|
|
|
|
}
|