crow-misia / libmediasoup-android

libmediasoupclient for Android
Apache License 2.0
9 stars 9 forks source link

Echo + Voice loop back in Android #7

Closed ahmedbhesaniya97 closed 1 year ago

ahmedbhesaniya97 commented 1 year ago

Hello, @crow-misia We have been using your library (latest version) for three to four months, and while it was previously flawless, we currently have two significant problems.

  1. Noise and echoes are produced during the session.
  2. When someone uses their phone to speak, they get their voice back.
crow-misia commented 1 year ago

very busy and can't find time.

crow-misia commented 1 year ago

Could not find the issue with Pixel6a and PC browser.

It may depend on hardware echo canceller in Android device, AudioManager settings, and other factors.

You may need to set WebRTC's MediaConstraints correctly, but you should explore this issue in your environment.

ahmedbhesaniya97 commented 1 year ago

Hi @crow-misia Thanks for responding. Did you try with two mobile devices ?

ahmedbhesaniya97 commented 1 year ago

Previously, everything was functioning flawlessly, but a week ago, we noticed this problem happen in certain of our key devices.

ahmedbhesaniya97 commented 1 year ago

We were able to address the Noise and Echo issues by passing a specific MediaConstraints attribute. But the voice loopback problem is not resolve (We are on it since week :cry:).

crow-misia commented 1 year ago

What MediaConstraints attribute did you set?

I installed mediasoup-demo-android app on Pixel6a and ZenFone3 and checked it, but cannot confirm the issue.

When two devices are placed side by side, the microphone picks up the sound of both devices. It is need to keep them in separate rooms or otherwise minimize the interference.

ahmedbhesaniya97 commented 1 year ago

Hi @crow-misia We passed this attribute for resolving Echo issue.

audiomediaContraints.mandatory.add(new MediaConstraints.KeyValuePair("googEchoCancellation", "false"));
audiomediaContraints.mandatory.add(new MediaConstraints.KeyValuePair("googEchoCancellation2", "false"));
audiomediaContraints.mandatory.add(new MediaConstraints.KeyValuePair("googAutoGainControl", "false"));
audiomediaContraints.mandatory.add(new MediaConstraints.KeyValuePair("googAutoGainControl2", "false"));
audiomediaContraints.mandatory.add(new MediaConstraints.KeyValuePair("googNoiseSuppression", "false"));
audiomediaContraints.mandatory.add(new MediaConstraints.KeyValuePair("googNoiseSuppression2", "false"));
audiomediaContraints.mandatory.add(new MediaConstraints.KeyValuePair("googNoiseReduction", "false"));
audiomediaContraints.mandatory.add(new MediaConstraints.KeyValuePair("googHighpassFilter", "false"));
JavaAudioDeviceModule.builder(appContext)
        .setUseHardwareAcousticEchoCanceler(false)
        .setUseHardwareNoiseSuppressor(false)
        .setAudioRecordErrorCallback(audioRecordErrorCallback)
        .setAudioTrackErrorCallback(audioTrackErrorCallback)
        .createAudioDeviceModule();

As you can see, we also set setUseHardwareAcousticEchoCanceler and setUseHardwareNoiseSuppressor to false.

Above parameter helps us to resolve echo issue in our devices

ahmedbhesaniya97 commented 1 year ago

Voice Loop back Issue

To eliminate interference, we tested on multiple networks with the device far away from other device.

We also test your mediaSoup demo, but voice is coming from Earpiece. so we set isSpeakerphoneOn and communicationMode .

val audioManager : AudioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
audioManager.isSpeakerphoneOn = true
audioManager.mode = AudioManager.MODE_IN_COMMUNICATION

In demo, we got the loop back voice, let me attach video for the same. I and my colleague are taking session and she is getting her voice in her device.

Device : Samsung M52(Android OS 12), Realme 5 Pro(Android OS 11)

https://user-images.githubusercontent.com/65531261/205428266-666f0010-f096-4e7b-9172-e71bca920e9e.mp4

crow-misia commented 1 year ago

I have checked it.

Seems to be caused by echo cancellation not working.

You need AudioSource set to VOICE_COMMUNICATION, not MIC, if you want to enable echo cancellation and noise reduction on the device.

Try the following settings.

JavaAudioDeviceModule.builder(appContext)
   ...
   .setUseHardwareAcousticEchoCanceler(true)
   .setUseHardwareNoiseSuppressor(true)
   .setAudioSource(android.media.MediaRecorder.AudioSource.VOICE_COMMUNICATION)
   ...
   .createAudioDeviceModule()
ahmedbhesaniya97 commented 1 year ago

Thanks @crow-misia Let us try.