com.unity.webrtc icon indicating copy to clipboard operation
com.unity.webrtc copied to clipboard

[BUG]: The sound is captured back to the microphone

Open MaximKurbanov opened this issue 2 years ago • 23 comments

Package version

3.0.0-pre.6

Environment

* OS: Android
* Unity version:2021.3

Hi!
I have big problem with my voice chat App. When 2 people voice each other by mobile phone. The voice sound of the speaker is recorded back to the microphone, and causing echo. After many times the sound is echoed and resonant with each other, causing loud noises. I've been looking for solutions like AEC(Acoustic Echo Cancellation), Echo Suppression, but it's not work for Unity.
Have you ever had this problem or has any solutions, please let me know. Thanks so much!!!

Steps To Reproduce

Run app on 2 mobile devices

Current Behavior

No response

Expected Behavior

No response

Anything else?

No response

MaximKurbanov avatar Aug 22 '23 11:08 MaximKurbanov

@MaximKurbanov

#651 Is this the same issue?

karasusan avatar Aug 23 '23 06:08 karasusan

Hello,

Thank you so much for your tireless efforts with this package @karasusan! My team has been using this package extensively and we've found that it works very well. I'm wondering if this bug might be a good opportunity for our team to contribute back to this repository.

I've noticed that the current implementation hard-codes the AudioOptions within the WebRTCPlugin Context object.

rtc::scoped_refptr<AudioSourceInterface> Context::CreateAudioSource()
    {
        // avoid optimization specially for voice
        cricket::AudioOptions audioOptions; // echo_cancellation will default to nullptr
        audioOptions.auto_gain_control = false;
        audioOptions.noise_suppression = false;
        audioOptions.highpass_filter = false;
        return UnityAudioTrackSource::Create(audioOptions);
    }

Do you think noise_suppression, echo_cancellation, etc would work as expected if these options were set to true? If so, then we might be able to close out this issue by exposing the AudioOptions and allowing end-users to specify echo_cancellation if they need it (this would also close #587 I believe).

If you think this would be a good approach, and if you are open to contributions, then I would be happy to submit a fix. I'd be happy to outline the changes in a design document before submitting a PR, so you could approve the changes before I begin implementing.

Joshua-Douglas avatar Sep 13 '23 01:09 Joshua-Douglas

@Joshua-Douglas Hi, always we welcome to your contribution. Making these audio options to public is a first step to solve this issue. I'm not sure these opitions can fix it, therefore we need to test these options.

If you make a PR, we will be glad to review and test.

karasusan avatar Sep 15 '23 02:09 karasusan

Hello @karasusan, that is great to hear - thanks!

Would you rather I submit the PR directly, or provide you with an outline of my planned changes before I submit? I don't mind sharing my outlined design before implementing, since these changes will impact the public API.

Either option works for me. I'll keep you updated.

Joshua-Douglas avatar Sep 15 '23 03:09 Joshua-Douglas

@Joshua-Douglas I'd like to see your planned changes of the public API. Maybe we need to make a new class to express AudioOptions.

karasusan avatar Sep 15 '23 08:09 karasusan

@Joshua-Douglas Have you already develop for this issue? I'm going to start this task before long.

karasusan avatar Sep 20 '23 03:09 karasusan

@karasusan I have not started. If you are ready to start soon, then that would be great! I have another open source project than I'm supporting, so you would probably get it done much faster.

Can't wait to see your changes! It would be great if this does enable echo cancellationšŸ¤ž!

Joshua-Douglas avatar Sep 21 '23 19:09 Joshua-Douglas

@Joshua-Douglas I have already started this issue. This is a draft PR.

  • #978

Can you try that If I make a package file to test this fix?

karasusan avatar Sep 25 '23 03:09 karasusan

@Joshua-Douglas I have already started this issue. This is a draft PR.

Can you try that If I make a package file to test this fix?

I can.

MaximKurbanov avatar Sep 25 '23 13:09 MaximKurbanov

I found the issue in our implementation for audio input from Unity. We need to fix it to use these audio options. Sorry but this fix is dropped from the next release version pre.7.

We will focus on this issue in the next version.

karasusan avatar Oct 13 '23 07:10 karasusan

We have tried to implement AudioProcessingModule in native code, and added the following code in what I believe the two points at which your program either receives the audio, or sends it:

UnityAudioTrackSource.cpp

        rtc::scoped_refptr<::webrtc::AudioProcessing> audioProcessingModule = GetAudioProcessingModule();

        if (audioProcessingModule) {
            const size_t sliceDurationMs = 10;
            const size_t sliceLength = (nSampleRate * nNumChannels * sliceDurationMs) / 1000;
            const ::webrtc::StreamConfig streamConfig = StreamConfig(nSampleRate, nNumChannels);

            auto cfg = GetAdditionalAudioProcessingConfig();
            bool invertStreams = cfg.invertStreams;

            for (size_t offset = 0; offset < nNumFrames; offset += sliceLength) {
                size_t sliceLength = std::min(sliceLength, nNumFrames - offset);
                
                if (!invertStreams) {
                    audioProcessingModule->ProcessStream(&_convertedAudioData[offset], streamConfig, streamConfig, &_convertedAudioData[offset]);
                } else {
                    audioProcessingModule->ProcessReverseStream(&_convertedAudioData[offset], streamConfig, streamConfig, &_convertedAudioData[offset]);
                }
            }
        }

AudioTrackSinkAdapter.cpp

        rtc::scoped_refptr<::webrtc::AudioProcessing> audioProcessingModule = GetAudioProcessingModule();

        if (audioProcessingModule) {
            const size_t sliceDurationMs = 10;
            const size_t sliceLength = (sampleRate * channels * sliceDurationMs) / 1000;
            const ::webrtc::StreamConfig streamConfig = StreamConfig(sampleRate, channels);

            auto cfg = GetAdditionalAudioProcessingConfig();
            bool invertStreams = cfg.invertStreams;

            for (size_t offset = 0; offset < readLength; offset += sliceLength) {
                size_t sliceLength = std::min(sliceLength, readLength - offset);
                
                if (!invertStreams) {
                    audioProcessingModule->ProcessReverseStream(&_bufferIn[offset], streamConfig, streamConfig, &_bufferIn[offset]);
                } else {
                    audioProcessingModule->ProcessStream(&_bufferIn[offset], streamConfig, streamConfig, &_bufferIn[offset]);
                }
            }
        }

The audioProcessingModule is a Singleton to which we apply some AudioOptions, including AcousticEchoCancellation. However, we still get the echo. @karasusan Maybe you can pinpoint us to the location of the native code where we should look at to fix the problem with your implementation for audio input/output?

Shushpancheak avatar Nov 21 '23 18:11 Shushpancheak

@Shushpancheak I think UnityAudioTrackSource is a right place to process noise cancellation. Maybe there are bugs in the code.

karasusan avatar Nov 22 '23 05:11 karasusan

Could you please let us know if there has been a delivery date set for this fix? We're currently facing some difficulties due to this issue. If possible, could you kindly provide us with a tentative date? Thank you very much for your assistance @karasusan

Mahesh218 avatar Feb 21 '24 09:02 Mahesh218

Are there any updates?

Shushpancheak avatar Feb 27 '24 13:02 Shushpancheak

We are still held back by this problem on ios and android mobile devices. webrtc package is rendered unusable on mobile devices when microphone picks up speaker audio without filter. Can you give any update on this fix? Thank you for your help.

DanMcPete avatar Mar 27 '24 18:03 DanMcPete

Are there any updates? @karasusan

LoopIssuer avatar Apr 10 '24 12:04 LoopIssuer

We are facing the same issue on the Android platform any updates would be helpful. @karasusan

LetMeKillYA avatar Apr 18 '24 17:04 LetMeKillYA