From ca23a771b36472f442cb146955dd04d6ad410d03 Mon Sep 17 00:00:00 2001 From: stary <834207172@qq.COM> Date: Mon, 9 Mar 2026 20:44:37 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90m=E3=80=91=E5=9B=9E=E5=A3=B0=E6=8A=91?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WebApp/client/public/js/config.js | 24 +++++++++ WebApp/client/public/onebyone/store.js | 75 +++++++++++++++++++++----- 2 files changed, 86 insertions(+), 13 deletions(-) diff --git a/WebApp/client/public/js/config.js b/WebApp/client/public/js/config.js index 6336048..487f439 100644 --- a/WebApp/client/public/js/config.js +++ b/WebApp/client/public/js/config.js @@ -10,5 +10,29 @@ export function getRTCConfiguration() { let config = {}; config.sdpSemantics = 'unified-plan'; config.iceServers = getServers(); + + // 添加音频处理选项,增强回声消除 + config.mediaConstraints = { + audio: { + echoCancellation: true, + noiseSuppression: true, + autoGainControl: true, + highpassFilter: true, + typingNoiseDetection: true + } + }; + + // 添加WebRTC音频处理选项 + config.rtcConfiguration = { + 'googEchoCancellation': true, + 'googEchoCancellation2': true, + 'googNoiseSuppression': true, + 'googNoiseSuppression2': true, + 'googAutoGainControl': true, + 'googAutoGainControl2': true, + 'googHighpassFilter': true, + 'googTypingNoiseDetection': true + }; + return config; } diff --git a/WebApp/client/public/onebyone/store.js b/WebApp/client/public/onebyone/store.js index a98b705..16d0069 100644 --- a/WebApp/client/public/onebyone/store.js +++ b/WebApp/client/public/onebyone/store.js @@ -75,10 +75,14 @@ class CallStateManager { throw new Error('getUserMedia is not supported'); } - // 请求摄像头权限并获取媒体流 + // 请求摄像头权限并获取媒体流,启用回声消除 const stream = await navigator.mediaDevices.getUserMedia({ video: true, - audio: true + audio: { + echoCancellation: true, + noiseSuppression: true, + autoGainControl: true + } }); console.log('Stream obtained successfully:', stream); @@ -133,24 +137,33 @@ class CallStateManager { }); await this.getLocalStream(); - // 更新WebRTC连接中的视频轨道 + // 更新WebRTC连接中的媒体轨道 if (this.renderstreaming) { - console.log('Updating video track in WebRTC connection'); + console.log('Updating media tracks in WebRTC connection'); // 获取所有收发器 const transceivers = this.renderstreaming.getTransceivers(); console.log('All transceivers:', transceivers); - // 查找现有的视频收发器 + // 查找现有的视频和音频收发器 const videoTransceivers = transceivers.filter(t => { return t.sender && t.sender.track && t.sender.track.kind === 'video'; }); console.log('Found video transceivers:', videoTransceivers); - // 获取新的视频轨道 + const audioTransceivers = transceivers.filter(t => { + return t.sender && t.sender.track && t.sender.track.kind === 'audio'; + }); + console.log('Found audio transceivers:', audioTransceivers); + + // 获取新的视频和音频轨道 const videoTracks = this.state.localStream.getVideoTracks(); console.log('New video tracks:', videoTracks); + const audioTracks = this.state.localStream.getAudioTracks(); + console.log('New audio tracks:', audioTracks); + + // 更新视频轨道 if (videoTracks.length > 0) { const newVideoTrack = videoTracks[0]; console.log('Using new video track:', newVideoTrack); @@ -176,12 +189,40 @@ class CallStateManager { console.error('Error adding new video transceiver:', error); } } - - // 延迟设置编解码器偏好,确保收发器已完全创建 - setTimeout(() => { - this.setCodecPreferences(); - }, 100); } + + // 更新音频轨道 + if (audioTracks.length > 0) { + const newAudioTrack = audioTracks[0]; + console.log('Using new audio track:', newAudioTrack); + + if (audioTransceivers.length > 0) { + // 替换现有的音频轨道 + for (const transceiver of audioTransceivers) { + try { + console.log('Replacing audio track in transceiver:', transceiver); + await transceiver.sender.replaceTrack(newAudioTrack); + console.log('Successfully replaced audio track'); + } catch (error) { + console.error('Error replacing audio track:', error); + } + } + } else { + // 添加新的音频收发器 + try { + console.log('Adding new audio transceiver'); + const transceiver = this.renderstreaming.addTransceiver(newAudioTrack, { direction: 'sendonly' }); + console.log('Added new audio transceiver:', transceiver); + } catch (error) { + console.error('Error adding new audio transceiver:', error); + } + } + } + + // 延迟设置编解码器偏好,确保收发器已完全创建 + setTimeout(() => { + this.setCodecPreferences(); + }, 100); } } else { // 直接更新媒体状态 @@ -244,11 +285,19 @@ class CallStateManager { // 创建信令实例 const signaling = this.useWebSocket ? new WebSocketSignaling() : new Signaling(); const config = getRTCConfiguration(); // 获取RTC配置 - // 优化RTC配置,确保支持高分辨率 + // 优化RTC配置,确保支持高分辨率和良好的音频处理 config.peerConnectionOptions = { optional: [ { googCpuOveruseDetection: false }, // 禁用CPU过度使用检测 - { googScreencastMinBitrate: 3000 } // 设置最小比特率 + { googScreencastMinBitrate: 3000 }, // 设置最小比特率 + { googEchoCancellation: true }, // 启用回声消除 + { googEchoCancellation2: true }, // 启用高级回声消除 + { googNoiseSuppression: true }, // 启用噪声抑制 + { googNoiseSuppression2: true }, // 启用高级噪声抑制 + { googAutoGainControl: true }, // 启用自动增益控制 + { googAutoGainControl2: true }, // 启用高级自动增益控制 + { googHighpassFilter: true }, // 启用高通滤波器 + { googTypingNoiseDetection: true } // 启用打字噪声检测 ] }; this.renderstreaming = new RenderStreaming(signaling, config); // 创建WebRTC连接管理实例