Does AudioTrack need to be assigned the same AudioSessionId as AudioRecord when using AcousticEchoCanceler?

Viewed 83

I'm having trouble getting the AcousticEchoCanceler feature to work well (as in, actually subtract echo) on my Google Pixel 3a and in my setup. My setup consists of a phone (my Pixel, in this case) and a desktop computer that are networked together exchanging audio packets. The end goal is a two-way audio application. The two-way audio happens and works, but as I mentioned previously, there's echo.

I've searched SO, but I've found conflicting results on exactly what needs to be enabled/disabled and on how well things actually work even when they do.

By searching, I've found I need to enable InCommunication:

 AudioManager am = (AudioManager)this.GetSystemService(Context.AudioService);
            am.Mode = Mode.InCommunication;

But it's not entirely clear whether AudioTrack and AudioRecord need to be tied together through the session ID, though in my case it doesn't seem to matter as I still get echo:

                int bufferSize = AudioRecord.GetMinBufferSize(SampleRate,
                    ChannelIn.Mono,
                    Android.Media.Encoding.Pcm16bit);


                AudioRecord record = new AudioRecord(
                    AudioSource.VoiceCommunication,
                    //44100,
                    SampleRate,
                    ChannelIn.Mono,
                    Android.Media.Encoding.Pcm16bit,
                    bufferSize);

                if (EchoCancellationAvailable)
                {
                    AcousticEchoCanceler aec = AcousticEchoCanceler.Create(record.AudioSessionId);
                    if (!aec.Enabled)
                    {
                        aec.SetEnabled(true);
                    }
                }

                int numBytesToWrite = AudioTrack.GetMinBufferSize(SampleRate,
                    ChannelOut.Mono,
                    Android.Media.Encoding.Pcm16bit);

                AudioTrack play = new AudioTrack(
                    Android.Media.Stream.Music,
                    //44100,
                    SampleRate,
                    ChannelOut.Mono,
                    Android.Media.Encoding.Pcm16bit,
                    numBytesToWrite,
                    AudioTrackMode.Stream, 
                    record.AudioSessionId);

I then use AudioRecord and AudioTrack in two separate threads. Did I associate the two objects together correctly? Do I need to? Are there any other issues that perhaps stand out as to why echo cancellation isn't really subtracting the echo?

Thanks in advance.

0 Answers
Related