WebRTC android application is creating echo when earphones are connected

1k Views Asked by At

Peer connection is getting established between devices and people on either side are able to have communication. No issue is happening when the EARPIECE is used by anyone or both of the devices.

Problem is occurring when EARPHONES or INBUILT_SPEAKER is used for audio communication by both devices.

Dependency: 'org.webrtc:google-webrtc:1.0.30039'

I have already tried the below options

OPTION-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"));

OPTION-2

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

Kindly help me figure out what is missing. How can I eliminate or reduce this echo?

1

There are 1 best solutions below

2
On BEST ANSWER

The following two steps solved the issue.

AudioDeviceModule was added to 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 based noise suppressor and echo canceller were enabled while creating audio stream

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 );
    }