WebRTC android 应用程序在连接耳机时产生回声

WebRTC android application is creating echo when earphones are connected

设备之间正在建立对等连接,双方都可以进行通信。 EARPIECE 被任何人或两个设备使用时没有问题发生。

当两个设备都使用 EARPHONESINBUILT_SPEAKER 进行音频通信时出现问题。

依赖关系:'org.webrtc:google-webrtc:1.0.30039'

我已经尝试过以下选项

选项-1

audioConstraints.mandatory.add(new MediaConstraints.KeyValuePair("googEchoCancellation", "true"));
audioConstraints.mandatory.add(new MediaConstraints.KeyValuePair("googEchoCancellation2", "true"));
audioConstraints.mandatory.add(new MediaConstraints.KeyValuePair("googDAEchoCancellation", "true"));
audioConstraints.mandatory.add(new MediaConstraints.KeyValuePair("googTypingNoiseDetection", "true"));
audioConstraints.mandatory.add(new MediaConstraints.KeyValuePair("googAutoGainControl", "true"));
audioConstraints.mandatory.add(new MediaConstraints.KeyValuePair("googAutoGainControl2", "true"));
audioConstraints.mandatory.add(new MediaConstraints.KeyValuePair("googNoiseSuppression", "true"));
audioConstraints.mandatory.add(new MediaConstraints.KeyValuePair("googNoiseSuppression2", "true"));
audioConstraints.mandatory.add(new MediaConstraints.KeyValuePair("googAudioMirroring", "false"));
audioConstraints.mandatory.add(new MediaConstraints.KeyValuePair("googHighpassFilter", "true"));

选项-2

JavaAudioDeviceModule.builder ( mContext )
                .setUseHardwareAcousticEchoCanceler(false)
                .setUseHardwareNoiseSuppressor(false)
                .createAudioDeviceModule ();
WebRtcAudioUtils.setWebRtcBasedNoiseSuppressor ( true );
WebRtcAudioUtils.setWebRtcBasedAcousticEchoCanceler ( true );
WebRtcAudioUtils.setWebRtcBasedAutomaticGainControl ( true );

请帮我弄清楚缺少什么。如何消除或减少这种回声?

以下两个步骤解决了问题。

AudioDeviceModule 已添加到 PeerConnectionFactory。

private PeerConnectionFactory createPeerConnectionFactory () {
        AudioDeviceModule audioDeviceModule = JavaAudioDeviceModule.builder ( mContext )
                .setUseHardwareAcousticEchoCanceler ( false )
                .setUseHardwareNoiseSuppressor ( false )
                .createAudioDeviceModule ();
        PeerConnectionFactory.InitializationOptions initializationOptions = PeerConnectionFactory.InitializationOptions.builder ( mContext )
                .createInitializationOptions ();
        PeerConnectionFactory.initialize ( initializationOptions );
        peerConnectionFactory = PeerConnectionFactory.builder ()
                .setAudioDeviceModule ( audioDeviceModule )
                .createPeerConnectionFactory ();
        return peerConnectionFactory;
    }

创建音频流时启用了基于 WebRTC 的噪声抑制器和回声消除器

private void createLocalStream () {
        WebRtcAudioUtils.setWebRtcBasedNoiseSuppressor ( true );
        WebRtcAudioUtils.setWebRtcBasedAcousticEchoCanceler ( true );
        WebRtcAudioUtils.setWebRtcBasedAutomaticGainControl ( true );
        MediaConstraints localMediaConstraints = new MediaConstraints ();
        AudioSource localAudioSource = peerConnectionFactory.createAudioSource ( localMediaConstraints );
        localTrack = peerConnectionFactory.createAudioTrack ( LOCAL_AUDIO_TRACK, localAudioSource );
        localTrack.setEnabled ( true );
        localMediaStream = peerConnectionFactory.createLocalMediaStream ( LOCAL_STREAM );
        localMediaStream.addTrack ( localTrack );
        peerConnection.addStream ( localMediaStream );
    }