修复:
1. 本地视频流能获取到了 2. 找到了视频连接错误的原因:挂断后本地麦克风和视频并没有关闭 现有问题: 1. 需要在挂断后关闭麦克风和摄像头 2. 没有渲染远程流推送的视频
This commit is contained in:
@@ -139,20 +139,26 @@ export function useWebRTC() {
|
||||
}
|
||||
}
|
||||
|
||||
// 接收远程流 - 修复开始
|
||||
// 接收远程流 - 修复轨道处理逻辑
|
||||
pc.ontrack = (event) => {
|
||||
console.log("📹 接收到远程轨道:", event.track.kind)
|
||||
console.log("📹 接收到远程轨道:", event.track.kind, "id:", event.track.id)
|
||||
|
||||
// 如果还没有远程流,创建一个新的MediaStream
|
||||
if (!remoteStream.value) {
|
||||
console.log("🆕 创建新的远程流")
|
||||
remoteStream.value = new MediaStream()
|
||||
}
|
||||
|
||||
// 将轨道添加到流中
|
||||
remoteStream.value.addTrack(event.track)
|
||||
console.log("✅ 远程流已更新,轨道数:", remoteStream.value.getTracks().length)
|
||||
// 确保轨道尚未添加
|
||||
const existingTrack = remoteStream.value.getTracks().find(t => t.id === event.track.id)
|
||||
if (!existingTrack) {
|
||||
remoteStream.value.addTrack(event.track)
|
||||
console.log("✅ 添加远程轨道成功,轨道数:", remoteStream.value.getTracks().length)
|
||||
console.log("✅ 添加远程轨道成功:", remoteStream.value)
|
||||
} else {
|
||||
console.log("⏭️ 轨道已存在,跳过添加")
|
||||
}
|
||||
}
|
||||
// 修复结束
|
||||
|
||||
// 连接状态变化
|
||||
pc.onconnectionstatechange = () => {
|
||||
@@ -218,7 +224,11 @@ export function useWebRTC() {
|
||||
if (localStream.value) {
|
||||
console.log("➕ 添加本地流到PeerConnection")
|
||||
localStream.value.getTracks().forEach((track) => {
|
||||
pc.addTrack(track, localStream.value)
|
||||
// 确保只添加活跃的轨道
|
||||
if (track.readyState === 'live') {
|
||||
pc.addTrack(track, localStream.value)
|
||||
console.log(`✅ 添加 ${track.kind} 轨道到连接`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -349,27 +359,41 @@ export function useWebRTC() {
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭连接
|
||||
// 关闭连接 - 彻底释放资源
|
||||
const closeConnection = () => {
|
||||
console.log("🔄 关闭WebRTC连接")
|
||||
|
||||
// 停止所有媒体轨道
|
||||
// 停止所有媒体轨道并释放设备
|
||||
if (localStream.value) {
|
||||
console.log("📹 停止本地流轨道")
|
||||
localStream.value.getTracks().forEach(track => {
|
||||
track.stop()
|
||||
console.log(`⏹ 停止轨道: ${track.kind} (${track.id})`)
|
||||
track.stop() // 停止轨道以释放设备
|
||||
track.enabled = false
|
||||
})
|
||||
localStream.value = null
|
||||
}
|
||||
|
||||
// 清除远程流
|
||||
if (remoteStream.value) {
|
||||
console.log("📹 清除远程流")
|
||||
remoteStream.value.getTracks().forEach(track => {
|
||||
console.log(`⏹ 停止远程轨道: ${track.kind} (${track.id})`)
|
||||
track.stop()
|
||||
})
|
||||
remoteStream.value = null
|
||||
}
|
||||
|
||||
// 关闭对等连接
|
||||
if (peerConnection.value) {
|
||||
console.log("🔌 关闭对等连接")
|
||||
peerConnection.value.close()
|
||||
peerConnection.value = null
|
||||
}
|
||||
|
||||
// 清理重连计时器
|
||||
if (reconnectTimer) {
|
||||
console.log("⏲ 清除重连计时器")
|
||||
clearTimeout(reconnectTimer)
|
||||
reconnectTimer = null
|
||||
}
|
||||
@@ -383,9 +407,6 @@ export function useWebRTC() {
|
||||
|
||||
// 清空候选队列
|
||||
iceCandidateQueue.value = []
|
||||
|
||||
// 清除远程流
|
||||
remoteStream.value = null
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -20,6 +20,7 @@ export function useWebSocket() {
|
||||
chatStore.connectionStatus = "connecting"
|
||||
|
||||
try {
|
||||
// const wsUrl = import.meta.env.VITE_WS_URL || "wss://g-ws.nailaoyun.cn/ws"
|
||||
const wsUrl = import.meta.env.VITE_WS_URL || "ws://localhost:12080/ws"
|
||||
socket.value = new WebSocket(wsUrl)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user