Bluetooth HFP phone call loopback problem

I want to use bluetooth as a speakerphone (A2DP) and to make phone calls (HFP).
To listen to music it works very well, however when I make a call from a phone to the phone paired in bluetooth on the apalis IMX8QM, I have an echo with a latency of 1 second on the phone sending the call, and no sound coming out on the speakers or headset + microphone plugged into the jack of the Ixora.
How to remove this echo?

I use pulseaudio to manage all sounds (bluetooth, mp3, webradio, radio). And the application is made with Qt in C++/QML

BSP6.x.y Yocto kirstone toradex
Apalis IMX8QM v1.1A
Ixora carrier board v1.1A

config (/etc/bluetooth/main.conf)
[General]
Enable = Source,Sink,Media,Socket,Headset
Discoverable = yes
Name = apalis-imx8

Class = 0x00041C

/etc/pulse/default.pa:

.nofail
### Load something into the sample cache
#load-sample-lazy x11-bell /usr/share/sounds/gtk-events/activate.wav
#load-sample-lazy pulse-hotplug /usr/share/sounds/startup3.wav
#load-sample-lazy pulse-coldplug /usr/share/sounds/startup3.wav
#load-sample-lazy pulse-access /usr/share/sounds/generic.wav
.fail

### Automatically restore the volume of streams and devices
load-module module-device-restore
load-module module-stream-restore
load-module module-card-restore

### Automatically augment property information from .desktop files
### stored in /usr/share/application
load-module module-augment-properties

### Should be after module-*-restore but before module-*-detect
load-module module-switch-on-port-available

load-module module-switch-on-connect

### Load audio drivers statically
### (it's probably better to not load these drivers manually, but instead
### use module-udev-detect -- see below -- for doing this automatically)
load-module module-alsa-sink
load-module module-alsa-source device=hw:0,0
#load-module module-oss device="/dev/dsp" sink_name=output source_name=input
#load-module module-oss-mmap device="/dev/dsp" sink_name=output source_name=input
#load-module module-null-sink
#load-module module-pipe-sink

### Automatically load driver modules depending on the hardware available
.ifexists module-udev-detect.so
load-module module-udev-detect tsched=0
.else
### Use the static hardware detection module (for systems that lack udev support)
load-module module-detect
.endif

### Automatically connect sink and source if JACK server is present
.ifexists module-jackdbus-detect.so
.nofail
load-module module-jackdbus-detect channels=2
.fail
.endif

load-module module-loopback source=alsa_input.hw_0_0 sink=alsa_output.default

### Automatically load driver modules for Bluetooth hardware
.ifexists module-bluetooth-policy.so
load-module module-bluetooth-policy
.endif

.ifexists module-bluetooth-discover.so
load-module module-bluetooth-discover headset=auto
.endif

### Load several protocols
.ifexists module-esound-protocol-unix.so
load-module module-esound-protocol-unix
.endif
load-module module-native-protocol-unix

### Network access (may be configured with paprefs, so leave this commented
### here if you plan to use paprefs)
#load-module module-esound-protocol-tcp
#load-module module-native-protocol-tcp
#load-module module-zeroconf-publish

### Load the RTP receiver module (also configured via paprefs, see above)
#load-module module-rtp-recv

### Load the RTP sender module (also configured via paprefs, see above)
#load-module module-null-sink sink_name=rtp format=s16be channels=2 rate=44100 sink_properties="device.description='RTP Multicast Sink'"
#load-module module-rtp-send source=rtp.monitor

### Load additional modules from GConf settings. This can be configured with the paprefs tool.
### Please keep in mind that the modules configured by paprefs might conflict with manually
### loaded modules.
.ifexists module-gconf.so
.nofail
load-module module-gconf
.fail
.endif

### Automatically restore the default sink/source when changed by the user
### during runtime
### NOTE: This should be loaded as early as possible so that subsequent modules
### that look up the default sink/source get the right value
load-module module-default-device-restore

### Automatically move streams to the default sink if the sink they are
### connected to dies, similar for sources
#load-module module-rescue-streams

### Make sure we always have a sink around, even if it is a null sink.
load-module module-always-sink

### Honour intended role device property
load-module module-intended-roles

### Automatically suspend sinks/sources that become idle for too long
load-module module-suspend-on-idle

### If autoexit on idle is enabled we want to make sure we only quit
### when no local session needs us anymore.
.ifexists module-console-kit.so
load-module module-console-kit
.endif
.ifexists module-systemd-login.so
load-module module-systemd-login
.endif

### Enable positioned event sounds
load-module module-position-event-sounds

### Cork music/video streams when a phone stream is active
load-module module-role-cork

### Modules to allow autoloading of filters (such as echo cancellation)
### on demand. module-filter-heuristics tries to determine what filters
### make sense, and module-filter-apply does the heavy-lifting of
### loading modules and rerouting streams.
load-module module-filter-heuristics
load-module module-filter-apply

### Load DBus protocol
.ifexists module-dbus-protocol.so
load-module module-dbus-protocol
.endif

### Load X11 bell module
#load-module module-x11-bell sample=bell-windowing-system

### Register ourselves in the X11 session manager
#load-module module-x11-xsmp

### Publish connection data in the X11 root window
#.ifexists module-x11-publish.so
#.nofail
#load-module module-x11-publish
#.fail
#.endif

### Make some devices default
#set-default-sink output
#set-default-source input
pactl list sinks short
5       bluez_sink.D8_1C_79_64_1E_F1.handsfree_audio_gateway    module-bluez5-device.c  s16le 1ch 8000Hz        RUNNING
root@apalis-imx8-15310363:~# pactl list sources short
7       bluez_sink.D8_1C_79_64_1E_F1.handsfree_audio_gateway.monitor    module-bluez5-device.c  s16le 1ch 8000Hz        IDLE
8       bluez_source.D8_1C_79_64_1E_F1.handsfree_audio_gateway  module-bluez5-device.c  s16le 1ch 8000Hz        RUNNING

Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


It seems like you’re experiencing an echo and latency issue when making phone calls using Bluetooth HFP (Hands-Free Profile) on your Apalis iMX8QM module with the Ixora carrier board. Here are a few steps and considerations to help you troubleshoot and potentially resolve the issue:

  1. Echo Cancellation:

    • Echo is a common issue in audio systems, especially in hands-free communication. PulseAudio has a module for echo cancellation that you can try to load. Add the following line to your /etc/pulse/default.pa:
      load-module module-echo-cancel
      
    • This module can help reduce or eliminate the echo by processing the audio streams.
  2. Latency Adjustment:

    • The latency might be due to buffer sizes or the way audio streams are handled. You can try adjusting the buffer sizes in PulseAudio. Add or modify the following lines in your /etc/pulse/daemon.conf:
      default-fragments = 2
      default-fragment-size-msec = 10
      
    • These settings can help reduce the latency by adjusting the audio buffer sizes.
  3. Bluetooth Profile Configuration:

    • Ensure that the correct Bluetooth profiles are being used. In your /etc/bluetooth/main.conf, you have:
      Enable = Source,Sink,Media,Socket,Headset
      
    • Make sure that the Headset profile is correctly configured and that your device supports HFP.
  4. PulseAudio Loopback Configuration:

    • The loopback module you are using might be causing the echo. You can try to adjust or remove the loopback module to see if it helps. In your /etc/pulse/default.pa, you have:
      load-module module-loopback source=alsa_input.hw_0_0 sink=alsa_output.default
      
    • Try commenting out this line and see if the echo persists. If it does, you might need to reconfigure how the audio streams are routed.
  5. Testing with BlueALSA:

    • Since you are using PulseAudio, you might want to test with BlueALSA as an alternative to see if it handles the audio streams better. BlueALSA is specifically designed for Bluetooth audio and might provide better performance for HFP.
  6. Hardware Considerations:

    • Ensure that the microphone and speakers are properly connected and configured. Sometimes hardware issues can cause echo and latency problems.
  7. Debugging Tools:

    • Use tools like pavucontrol to monitor and adjust audio streams in real-time. This can help you identify which streams are causing the echo and adjust their properties accordingly.
  8. Update Software:

    • Ensure that you are using the latest versions of PulseAudio, BlueZ, and other related software. Sometimes, updates contain fixes for issues like the one you are experiencing.

If these steps do not resolve the issue, you might need to provide more specific logs or details about the behavior of the system during a call. This can help in diagnosing the problem more accurately.

Please, also check if these links can help you solve your problem:

https://community.toradex.com/tag/bluetooth

Bluetooth HFP echo on receiver side.

Hi, @jean!

We tested phone calls using Bluetooth in the past, but we do not support it right now. At the time, we made it work by doing the following steps:

First, we built an image with pulseaudio using Yocto (to learn how to do it, please refer to this article). Here is the .bb file:

require recipes-images/images/tdx-reference-multimedia-image.bb

SUMMARY = "Multimedia Image Pulseaudio Bluetooth HFP"
DESCRIPTION = "Multimedia image with pulseaudio bluetooth hfp"

export IMAGE_BASENAME = "Multimedia-Image-Pulseaudio-HFP"
MACHINE_NAME ?= "${MACHINE}"
IMAGE_NAME = "${MACHINE_NAME}_${IMAGE_BASENAME}"

IMAGE_INSTALL:append = " pulseaudio pulseaudio-misc webrtc-audio-processing"
DISTRO_FEATURES:append = " pulseaudio"
PACKAGECONFIG:append:pn-pulseaudio = " webrtc"

After that, we used the following script to configure pulseaudio:

#!/bin/bash

# to check if PulseAudio is running
check_pulseaudio() {
    ps aux | grep -i pulseaudio | grep -v grep > /dev/null
    return $?
}

echo "Killing and unblocking bluetooth"
rfkill unblock bluetooth
sleep 1

echo "Powering bluetooth" 
bluetoothctl power on

echo "Turn on agent"
bluetoothctl agent on 
sleep 1

echo "setting default agent"
bluetoothctl default-agent 

# Check for PulseAudio processes
process_id=$(ps aux | grep -i pulseaudio | grep -v grep | awk '{print $2}')
# If a PulseAudio process is found
if [ ! -z "$process_id" ]; then
    echo "PulseAudio process found with PID: $process_id"
    
    # Kill the PulseAudio process
    kill -9 $process_id
    
    # Verify if the process is killed
    if [ $? -eq 0 ]; then
        echo "PulseAudio process killed successfully."
    else
        echo "Failed to kill the PulseAudio process."
    fi
else
    echo "No PulseAudio process found."
fi

echo "starting Pulseaudio"
pulseaudio --start

# Wait for PulseAudio process to start
while ! check_pulseaudio; do
    echo "Waiting for PulseAudio to start..."
    sleep 1
done
echo "PulseAudio is now running."

echo "Loading sink/output device"
pactl load-module module-alsa-sink device=hw:0,0
sleep 1

echo "Setting default sink/output"
pactl set-default-sink alsa_output.hw_0_0

echo "Loading source/input"
pactl load-module module-alsa-source device=hw:0,0 

echo "Setting default source/input"
pactl set-default-source alsa_input.hw_0_0

echo "Enabling SCO "
hcitool -i hci0 cmd 0x3F 0x001D 0x00

echo "Device ready to connect/pair"

Then we paired the module with the phone and tested the phone call. You should be able to use the module for the phone call.

It is important to emphasize that we do not support it right now, but we tested it in the past, and this could be a starting point for you.

Best regards.
Lucas Azeituno.