Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add msbc codec to hfp profile #533

Closed
bmt1596 opened this issue Feb 8, 2022 · 21 comments
Closed

How to add msbc codec to hfp profile #533

bmt1596 opened this issue Feb 8, 2022 · 21 comments

Comments

@bmt1596
Copy link

bmt1596 commented Feb 8, 2022

Hi everybody. First I want to thank for bluealsa. Currently I am using Bluealsa to support device (HF) for A2DP-Sink and HFP and using phone (AG) to connect and transmit Sound and Voice to my embedded device.

My embedded device uses BlueZ version 5.46. Unexpectedly after installing Bluealsa on the device it seems to have worked quite well.

I installed for BlueALSA including the following options:

../configure --enable-debug --disable-payloadcheck --enable-cli --enable-rfcomm

It works when I start bluealsa with the command:
bluealsa -i hci0 --syslog -p a2dp-sink -p hfp-hfp &

The displayed result of bluealsa-aplay-l is:

root@user:~ # bluealsa-aplay -l
**** List of PLAYBACK Bluetooth Devices ****
hci0: F8:C3:CC:92:08:6A [Minh Tung], trusted phone
  SCO (CVSD): S16_LE 1 channel 8000 Hz
**** List of CAPTURE Bluetooth Devices ****
hci0: F8:C3:CC:92:08:6A [Minh Tung], trusted phone
  A2DP (SBC): S16_LE 2 channels 44100 Hz
  SCO (CVSD): S16_LE 1 channel 8000 Hz

So it can be said that BlueALSA has done its job well, but is there any other way to upgrade the sampling rate for voice channels? I have consulted in Bluealsa's issues and found that there are some cases where WBS is enabled via --enable-msbc but when I try it with my Linux Kernel version, it shows the following results:

root@user:~ # bluealsa-aplay -l
**** List of PLAYBACK Bluetooth Devices ****
hci0: F8:C3:CC:92:08:6A [Minh Tung], trusted phone
  SCO (<null>): S16_LE 1 channel 0 Hz
**** List of CAPTURE Bluetooth Devices ****
hci0: F8:C3:CC:92:08:6A [Minh Tung], trusted phone
  SCO (<null>): S16_LE 1 channel 0 Hz
  A2DP (SBC): S16_LE 2 channels 44100 Hz

Looks like the sampling rate for SCO has not been configured. Do I need to upgrade the Linux-kernel version? and what is the minimum linux-kernel version for Bluealsa to update the mSBC-Codec. I am using petalinux 2018.3 and linux-kernel version is 4.14.0.

Hope to receive your reply as soon. Have a nice day!!

@arkq
Copy link
Owner

arkq commented Feb 9, 2022

Looks like the sampling rate for SCO has not been configured.

When mSBC is enabled, the sampling rate is not know prior to codec selection procedure. Codec selection might be performed just before first audio connection. So, in other words, you might see sampling rate as 0 Hz, which will change to 8000 or 16000 after first audio connection.

In most cases codec selection is performed by the AG, so in case of Android HFP codec is selected before first audio transfer. When BlueALSA acts as an AG, codec selection is performed just after connection establishment, so PCM client will know sampling rate before starting audio. I know that this might be inconvenient for BlueALSA in HF mode, however, I'm not sure how to solve this issue. I might set codec to CVSD as a sane default, and it will be updated by the AG before audio transfer. However, this will most likely mean that prior to first audio transfer the PCM configuration will be incorrect (which right now is indicated by 0 Hz sampling rate).

@bmt1596
Copy link
Author

bmt1596 commented Feb 9, 2022

thanks for your suggestion, indeed it worked as soon as I opened and configured 2 PCMs for SCO. The displayed result of bluealsa-aplay-l is:

bluealsa-aplay -l
**** List of PLAYBACK Bluetooth Devices ****
hci0: 18:F6:43:60:22:FA [18-F6-43-60-22-FA], trusted phone
  SCO (mSBC): S16_LE 1 channel 16000 Hz
**** List of CAPTURE Bluetooth Devices ****
hci0: 18:F6:43:60:22:FA [18-F6-43-60-22-FA], trusted phone
  SCO (mSBC): S16_LE 1 channel 16000 Hz
  A2DP (SBC): S16_LE 2 channels 44100 Hz

when i run the arecord command to record the voice signal, everything works fine, but when i send the recorded voice signal from the microphone to the phone with aplay, I use aplay to send it with the aplay command as follows:

root@user:~ # aplay -D bluealsa:PROFILE=sco voicemail-3.wav
D: ../../../src/asound/bluealsa-pcm.c:1053: Getting BlueALSA PCM: PLAYBACK 00:00:00:00:00:00 sco
D: ../../../src/asound/bluealsa-pcm.c:921: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Setting constraints
Playing WAVE 'voicemail-3.wav' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono
D: ../../../src/asound/bluealsa-pcm.c:443: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Initializing HW
D: ../../../src/asound/bluealsa-pcm.c:466: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: FIFO buffer size: 2048 frames
D: ../../../src/asound/bluealsa-pcm.c:474: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Selected HW buffer: 4 periods x 4000 bytes == 16000 bytes
D: ../../../src/asound/bluealsa-pcm.c:489: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Initializing SW
D: ../../../src/asound/bluealsa-pcm.c:489: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Initializing SW
D: ../../../src/asound/bluealsa-pcm.c:489: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Initializing SW
D: ../../../src/asound/bluealsa-pcm.c:528: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Prepared
D: ../../../src/asound/bluealsa-pcm.c:489: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Initializing SW
D: ../../../src/asound/bluealsa-pcm.c:345: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Starting
D: ../../../src/asound/bluealsa-pcm.c:212: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Starting IO loop: 8

it shows the following error, that it's mSBC packets is missing. and I can't hear the sounds sent from my embedded device at all. Do you know where the cause lies?

bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such process
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 0
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 1 != 0
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such process
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: D: ../../src/at.c:166: AT message: RESP: command:+CIEV, value: 5,3
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such process
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such file or directory
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: D: ../../src/at.c:166: AT message: RESP: command:+CIEV, value: 5,4
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 1 != 0
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 1 != 0
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 1 != 0
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such file or directory

After a while, BlueALSA is completely stopped.


D: ../../../src/asound/bluealsa-pcm.c:443: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Initializing HW
bluealsa: D: ../../src/dbus.c:63: Called: org.bluealsa.PCM1.Open() on /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink
D: ../../../src/asound/bluealsa-pcm.c:450: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Couldn't open PCM: HFP audio codec not selected
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: W: ../../src/hci.c:126: Couldn't get SCO socket options: Transport endpoint is not connected
bluealsa: D: ../../src/hci.c:132: SCO link socket MTU: 21: 0
bluealsa: D: ../../src/sco.c:417: SCO poll error status: 0x18
bluealsa: D: ../../src/ba-transport.c:997: Closing SCO: 21
D: ../../../src/asound/bluealsa-pcm.c:431: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: Closing
D: ../../../src/asound/bluealsa-pcm.c:431: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Closing

@borine
Copy link
Collaborator

borine commented Feb 9, 2022

Playing WAVE 'voicemail-3.wav' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono

Is voicemail-3.wav a recording of the mSBC audio from the phone? If so an mSBC recording should be 16000Hz, so I think your arecord command line needs to include -r 16000. That may make some improvement, but I'm not sure that it is the complete answer. Give it a try and report back the result.

@bmt1596
Copy link
Author

bmt1596 commented Feb 10, 2022

Tks @borine for reply!!! Looks like things are getting worse. Right now the phonedown recording is no longer possible. I tried to try recording phonedown again with the following command for 16 kHz:

arecord -d 10 -f S16_LE -r 16000 -D bluealsa:PROFILE=sco Record_PhoneCall.wav

D: ../../../src/asound/bluealsa-pcm.c:1053: Getting BlueALSA PCM: CAPTURE 00:00:00:00:00:00 sco
bluealsa: D: ../../src/dbus.c:63: Called: org.bluealsa.Manager1.GetPCMs() on /org/bluealsa
D: ../../../src/asound/bluealsa-pcm.c:921: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: Setting constraints
Recording WAVE 'Record_PhoneCall.wav' : Signed 16 bit Little Endian, Rate 16000 Hz, Mono
D: ../../../src/asound/bluealsa-pcm.c:443: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: Initializing HW
bluealsa: D: ../../src/dbus.c:63: Called: org.bluealsa.PCM1.Open() on /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source
D: ../../../src/asound/bluealsa-pcm.c:466: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: FIFO buffer size: 32768 frames
D: ../../../src/asound/bluealsa-pcm.c:474: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: Selected HW buffer: 4 periods x 4000 bytes == 16000 bytes
D: ../../../src/asound/bluealsa-pcm.c:489: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: Initializing SW
D: ../../../src/asound/bluealsa-pcm.c:489: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: Initializing SW
D: ../../../src/asound/bluealsa-pcm.c:528: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: Prepared
D: ../../../src/asound/bluealsa-pcm.c:489: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: Initializing SW
D: ../../../src/asound/bluealsa-pcm.c:345: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: Starting
bluealsa: D: ../../src/ba-transport.c:779: PCM resumed: 23
D: ../../../src/asound/bluealsa-pcm.c:212: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: Starting IO loop: 7
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 1 != 0
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 1 != 0
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such file or directory
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such file or directory
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
D: ../../../src/asound/bluealsa-pcm.c:381: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: Stopping
D: ../../../src/asound/bluealsa-pcm.c:147: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: IO thread cleanup
D: ../../../src/asound/bluealsa-pcm.c:481: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: Freeing HW
D: ../../../src/asound/bluealsa-pcm.c:431: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source: Closing
bluealsa: D: ../../src/ba-transport.c:1025: Closing PCM: 23

after successful recording i tried to turn on Record_PhoneCall.wav file but it seems that the sound quality is quite low, sometimes there is a loss of sound. and this is the result when i try to send 16000 Hz PhoneUp signal through aplay. I didn't hear anything at all.

aplay -d 10 -f S16_LE -r 16000 -D bluealsa:PROFILE=sco Record_PhoneCall.wav

D: ../../../src/asound/bluealsa-pcm.c:1053: Getting BlueALSA PCM: PLAYBACK 00:00:00:00:00:00 sco
bluealsa: D: ../../src/dbus.c:63: Called: org.bluealsa.Manager1.GetPCMs() on /org/bluealsa
D: ../../../src/asound/bluealsa-pcm.c:921: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Setting constraints
Playing WAVE 'Record_PhoneCall.wav' : Signed 16 bit Little Endian, Rate 16000 Hz, Mono
D: ../../../src/asound/bluealsa-pcm.c:443: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Initializing HW
bluealsa: D: ../../src/dbus.c:63: Called: org.bluealsa.PCM1.Open() on /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink
D: ../../../src/asound/bluealsa-pcm.c:466: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: FIFO buffer size: 2048 frames
D: ../../../src/asound/bluealsa-pcm.c:474: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Selected HW buffer: 4 periods x 4000 bytes == 16000 bytes
D: ../../../src/asound/bluealsa-pcm.c:489: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Initializing SW
D: ../../../src/asound/bluealsa-pcm.c:489: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Initializing SW
D: ../../../src/asound/bluealsa-pcm.c:528: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Prepared
D: ../../../src/asound/bluealsa-pcm.c:489: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Initializing SW
D: ../../../src/asound/bluealsa-pcm.c:345: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Starting
bluealsa: D: ../../src/ba-transport.c:779: PCM resumed: 22
D: ../../../src/asound/bluealsa-pcm.c:212: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Starting IO loop: 8
bluealsa: D: ../../src/ba-transport.c:805: PCM drained: 22
D: ../../../src/asound/bluealsa-pcm.c:381: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Stopping
D: ../../../src/asound/bluealsa-pcm.c:147: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: IO thread cleanup
bluealsa: D: ../../src/ba-transport.c:811: PCM dropped: 22
D: ../../../src/asound/bluealsa-pcm.c:381: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Stopping
bluealsa: D: ../../src/ba-transport.c:811: PCM dropped: 22
D: ../../../src/asound/bluealsa-pcm.c:481: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Freeing HW
D: ../../../src/asound/bluealsa-pcm.c:431: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Closing
bluealsa: D: ../../src/ba-transport.c:1025: Closing PCM: 22

after checking the PCM again I see it is still complete with the configuration for SCO as mSBC codec:

bluealsa-aplay -l
bluealsa: D: ../../src/dbus.c:63: Called: org.bluealsa.Manager1.GetPCMs() on /org/bluealsa
**** List of PLAYBACK Bluetooth Devices ****
hci0: 18:F6:43:60:22:FA [Minh Tùng], trusted phone
  SCO (mSBC): S16_LE 1 channel 16000 Hz
**** List of CAPTURE Bluetooth Devices ****
hci0: 18:F6:43:60:22:FA [Minh Tùng], trusted phone
  SCO (mSBC): S16_LE 1 channel 16000 Hz
  A2DP (SBC): S16_LE 2 channels 44100 Hz

strangely when I check with hciconfig -a command I see that there are still SCO signals sent and received.

hciconfig -a
hci0:   Type: Primary  Bus: USB
        BD Address: 00:16:A4:4A:02:0B  ACL MTU: 1021:8  SCO MTU: 64:1
        UP RUNNING PSCAN
        RX bytes:12457253 acl:96 sco:244130 events:179 errors:0
        TX bytes:844280 acl:78 sco:31129 commands:113 errors:0
        Features: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87
        Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
        Link policy: RSWITCH SNIFF
        Link mode: SLAVE ACCEPT
        Name: 'BlueZ 5.46'
        Class: 0x740000
        Service Classes: Rendering, Object Transfer, Audio, Telephony
        Device Class: Miscellaneous,
        HCI Version: 5.0 (0x9)  Revision: 0x10ca
        LMP Version: 5.0 (0x9)  Subversion: 0x220b
        Manufacturer: Cypress Semiconductor Corporation (305)

Also while running the arecord and aplay commands I always get the following debugs:

bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 1 != 0
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such process
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such file or directory
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1

whether my Bluealsa version has some error for mSBC decoding, i use bluealsa v3.1.0 and Bluetooth USB Modul BT850 of Cypress with CYW20704A2. In the datasheet I see that it also supports WBS (eSCO) over PCM, but I route my SCO Data through HCI via sudo hcitool cmd 0x3F 0x01C 0x01 0x02 0x00 0x01 0x01, will that have any effect?

@borine
Copy link
Collaborator

borine commented Feb 10, 2022

Hmm, looks like Linux has some problems with your BT adapter. The Linux USB bluetooth module (btusb) has a number of issues with SCO, especially with WBS. See issue #400 for a (long) discussion that started with a specific adapter, but covers USB adapters in general. There may be something in there that is helpful in your case. The latest bluealsa code also has some changes related to SCO socket handling, but I do not know if they will help you or not. I would also suggest a web search for your adapter, or the Cypress chip it uses, with Linux for SCO, HFP and WBS to see if anyone else has this issue and has found workarounds. I think this is not a bluealsa-specific problem, so any workaround for pulseaudio or pipewire may also be helpful for bluealsa.

@bmt1596
Copy link
Author

bmt1596 commented Feb 10, 2022

Thank you very much. Maybe I can also figure out where the cause lies and may need to find a patch for btusb. But when I switch to using UART will things be better for WBS ?? Although the transfer speed is slower but maybe it will have less WBS support errors. I will also find out where the problem lies in the btusb-driver and if found I will update you so that bluealsa can improve the SCO transmission with WBS for this Bluetooth Modul of Cypress.

@borine
Copy link
Collaborator

borine commented Feb 10, 2022

There is an important change to btusb in Linux kernel 5.15 regarding mSBC support, so you may like to try that kernel version (or a more recent one) first (see torvalds/linux@55981d3).

UART should work fine, provided the serial device config is correct. Its been a long time since a last used mSBC with a UART adapter though. The SCO issues that I am aware of all relate specifically to USB.

@bmt1596
Copy link
Author

bmt1596 commented Feb 10, 2022

Is this problem really in Linux-Kernel? Actually, I also find it a bit ridiculous. I can totally record Phone Downlink but Phone Uplink is not working. This results in sound being transmitted from only one direction. But if the problem is really in the Linux-Kernel then it's possible that both ways won't work. What do you think could be the cause?

@borine
Copy link
Collaborator

borine commented Feb 10, 2022

Actually, I also find it a bit ridiculous.

You are absolutely correct there. Kernel 5.15 finally gets the USB Alt setting logic correct (I think, but I'm no expert), but it still fails to reliably inform the application of the correct packet size (MTU) for writing to a SCO socket. So applications such as bluealsa may not choose the correct value and therefore may fail to deliver data at the required rate. MTU for reading from the socket is not so critical, since the application can always read whatever data is available.

I use mSBC successfully with 2 USB adapters, both quite old models, so I know it can work.

There may be other underlying issues rather than btusb. The adapter may require updated firmware (such cases usually result in very many issues on Linux forums, so generally it easy to know if your adapter is one of these). There can also be issues with other devices sharing the same USB bus, and I really do not know how to diagnose that.

I just did a quick search for CYW20704A2 and 04b4:f901 but found nothing that suggests issues with it. Do you know if anyone else is successfully using this adapter with Linux for mSBC? If it works with PulseAudio or Pipewire then it should work with BlueALSA

@bmt1596
Copy link
Author

bmt1596 commented Feb 10, 2022

After carefully studying the specs of CYW20704A2 and checking hciconfig-a again I found that the SCO MTU is 64 bytes. Since the linux kernel version 4.14 doesn't support Alt6 I will try default setting the value of new_alts to 3 in btusb.c to match the MTU of CYW20704A2. Upgrading Linux-Version is quite difficult for me as it relies heavily on Petalinux, so a patch would be preferable. you can see that here: check conditions before enabling USB ALT 3 for WBS

is there any other way i can make the frames less than 21 bytes in bluealsa without affecting the linux kernel? I get the following Debug when running the arecord command:


bluealsa: D: ../../src/hci.c:132: SCO link socket MTU: 21: 64
bluealsa: D: ../../src/dbus.c:63: Called: org.bluealsa.Manager1.GetPCMs() on /org/bluealsa
bluealsa: D: ../../src/dbus.c:63: Called: org.bluealsa.PCM1.Open() on /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/source
bluealsa: D: ../../src/ba-transport.c:779: PCM resumed: 23
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 1 != 0
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such process
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such process
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such file or directory
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such file or directory
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such file or directory
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such process
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such file or directory
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 1 != 0
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 1 != 0
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 1 != 0
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such process
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 1 != 0
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 1 != 0
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such process
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 3 != 2
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 0 != 3
bluealsa: W: ../../src/sco.c:285: Couldn't decode mSBC: No such process

Looks like bluealsa configures SCO to be 21 bytes, but PCM returns 23 bytes. Is that the cause of the loss of SCO paket. One more assumption that leads me to only use arecord but not aplay, is the MTU configuration between Tx (aplay) and Rx (arecord) different?

I think when an incoming packet arrives the daemon needs to read it as soon as possible - otherwise there is a risk the next packet will overwrite it in the controller's buffer and data is lost. because with mSBC SCO my chip only got 21 bytes MTU but aplay writes 22 bytes.

Debug when a call is comming:

bluealsa: D: ../../src/sco.c:103: New incoming SCO link: 18:F6:43:60:22:FA: 21
bluealsa: D: ../../src/hci.c:132: SCO link socket MTU: 21: 64

Debug when i play the wav for PhoneUp

aplay -d 10 -f S16_LE -r 16000 -D bluealsa:PROFILE=sco Record_PhoneCall.wav
D: ../../../src/asound/bluealsa-pcm.c:1053: Getting BlueALSA PCM: PLAYBACK 00:00:00:00:00:00 sco
bluealsa: D: ../../src/dbus.c:63: Called: org.bluealsa.Manager1.GetPCMs() on /org/bluealsa
D: ../../../src/asound/bluealsa-pcm.c:921: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Setting constraints
Playing WAVE 'Record_PhoneCall.wav' : Signed 16 bit Little Endian, Rate 16000 Hz, Mono
D: ../../../src/asound/bluealsa-pcm.c:443: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Initializing HW
bluealsa: D: ../../src/dbus.c:63: Called: org.bluealsa.PCM1.Open() on /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink
D: ../../../src/asound/bluealsa-pcm.c:466: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: FIFO buffer size: 2048 frames
D: ../../../src/asound/bluealsa-pcm.c:474: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Selected HW buffer: 4 periods x 4000 bytes == 16000 bytes
D: ../../../src/asound/bluealsa-pcm.c:489: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Initializing SW
D: ../../../src/asound/bluealsa-pcm.c:489: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Initializing SW
D: ../../../src/asound/bluealsa-pcm.c:528: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Prepared
D: ../../../src/asound/bluealsa-pcm.c:489: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Initializing SW
D: ../../../src/asound/bluealsa-pcm.c:345: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Starting
bluealsa: D: ../../src/ba-transport.c:779: PCM resumed: 22
D: ../../../src/asound/bluealsa-pcm.c:212: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Starting IO loop: 8
bluealsa: D: ../../src/ba-transport.c:805: PCM drained: 22
D: ../../../src/asound/bluealsa-pcm.c:381: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Stopping
D: ../../../src/asound/bluealsa-pcm.c:147: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: IO thread cleanup
bluealsa: D: ../../src/ba-transport.c:811: PCM dropped: 22
D: ../../../src/asound/bluealsa-pcm.c:381: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Stopping
bluealsa: D: ../../src/ba-transport.c:811: PCM dropped: 22
D: ../../../src/asound/bluealsa-pcm.c:481: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Freeing HW
D: ../../../src/asound/bluealsa-pcm.c:431: /org/bluealsa/hci0/dev_18_F6_43_60_22_FA/hfphf/sink: Closing
bluealsa: D: ../../src/ba-transport.c:1025: Closing PCM: 22

@borine
Copy link
Collaborator

borine commented Feb 11, 2022

PCM returns 23 bytes
aplay writes 22 bytes

No. those numbers (22 and 23) are the file descriptors of the SCO sockets, and have nothing to do with the number of bytes written. To understand the required size of each write() you need to know how USB isochronous endpoints work, and their relationship to the USB interface bAlternateSetting value. This is discussed in #400 (comment) - the socket layer code will add a USB header of 3 bytes, so you need to subtract 3 from the URB sizes calculated there to arrive at the MTU from the application's perspective. So in the end we arrive at:
Alt-1 : 24 bytes
Alt-2 : 48 bytes
Alt-3 : 72 bytes (i think, but not sure about that one)
Alt-6 : 60 bytes

The problem for the application is that it does not know which Alternate setting is in use, and the socket reports wrong MTU values (e.g. in your case 64, which is wrong for any Alt setting).

Bluealsa simply chooses to write 24 bytes in all cases, since this has proved to be a "safe" value that works in most cases (to date the majority of adapters seem to use Alt-1). Writing too many bytes can cause lockups in the system that require a hard reset, so it is not advisable to experiment with random values.

@bmt1596
Copy link
Author

bmt1596 commented Feb 11, 2022

Thank you very much for the suggestions. I will try patching btusb.c in bluetooth-drivers and update the default Alt value to 1 and will update the results again.

@bmt1596
Copy link
Author

bmt1596 commented Feb 11, 2022

Very helpful! I was really surprised when I changed Alt to 1. It work now for me to play PhoneUp with aplay.

bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1
bluealsa: W: ../../src/codec-msbc.c:152: Missing mSBC packet: 2 != 1

but with arecord it seems to be a bit slow to read which leads to some loss of SCO pakets, is there any way to fix it via buffer_size or buffer_time via Bluealsa.

@borine
Copy link
Collaborator

borine commented Feb 11, 2022

If the client (arecord) runs too slowly, then bluealsa's internal buffers can become full and then BT packets will be dropped.

This can happen when the client is writing to another device with its own clock, especially if that involves resampling to a different rate, because the resampler may introduce rounding error. However, if arecord is writing to a file there should be no problem (unless the file system is very, very, slow).

The latest bluealsa source uses a revised SCO design, so that may help. But if the problem is in the USB subsystem, then there is nothing that bluealsa can do.

Rather than forcing the driver to use Alt-1, if you can find out which Alt setting it uses natively then you could instead change the packet size used by bluealsa to the correct value for that setting. That might help get better throughput.
[EDIT] - sorry, the fixed packet size in bluealsa is for writing, not reading, my mistake!

@bmt1596
Copy link
Author

bmt1596 commented Feb 13, 2022

thank you very much, up to this point can say Bluealsa works both ways quite well for mSBC. It's true that the buffer is a bit small, but since the audio signal is recorded directly to the file, it shouldn't have too much impact on the quality.

root@user:~ # bluealsa-aplay -l
**** List of PLAYBACK Bluetooth Devices ****
hci0: F8:C3:CC:92:08:6A [Minh Tung], trusted phone
  SCO (<null>): S16_LE 1 channel 0 Hz
**** List of CAPTURE Bluetooth Devices ****
hci0: F8:C3:CC:92:08:6A [Minh Tung], trusted phone
  SCO (<null>): S16_LE 1 channel 0 Hz
  A2DP (SBC): S16_LE 2 channels 44100 Hz

is there any way to make bluealsa default setting sco_codec as msbc as soon as bluetooth device is connected. Because I sometimes have some problems like the following when making calls in HF mode:

As @arkq explained, when there is an incoming call bluealsa will initiate the mSBC by itself but I make the call as soon as the device is connected bluealsa will hang in after a short time it will end completely:

bluealsa: D: ../../src/ba-rfcomm.c:108: Sending AT message: SET: command:+BCS, value:2
bluealsa: D: ../../src/sco.c:103: New incoming SCO link: F8:C3:CC:92:08:6A: 21
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: D: ../../src/ba-rfcomm.c:1106: RFCOMM poll timeout
bluealsa: W: ../../src/hci.c:126: Couldn't get SCO socket options: Transport endpoint is not connected
bluealsa: D: ../../src/hci.c:132: SCO link socket MTU: 21: 0
bluealsa: D: ../../src/sco.c:417: SCO poll error status: 0x18
bluealsa: D: ../../src/ba-transport.c:997: Closing SCO: 21

As far as I know, both Android phones and Iphones support mSBC, so the CVSD 8000 Hz configuration will not be needed and should always be configured for mSBC instead. Could the cause be that the SCO link has not been initialized?

@borine
Copy link
Collaborator

borine commented Feb 14, 2022

bluealsa: W: ../../src/hci.c:126: Couldn't get SCO socket options: Transport endpoint is not connected
bluealsa: D: ../../src/hci.c:132: SCO link socket MTU: 21: 0

hmm, you may have found a bug here. it seems the socket is not fully configured at that point, so the call to getsockopt() is failing. As a result we have an MTU of 0.

@arkq is the getsockopt() failure expected? before ac5706f the MTU would simply be set to 48, but since that commit it is left as 0. If the getsockopt() failure is normal, then a method of determining the MTU later is required; if the failure is not expected, maybe the message should be changed to error() and the transport code should abandon the link?

@arkq
Copy link
Owner

arkq commented Feb 14, 2022

I think that is should/shall not fail in any of the getsockopt() calls, otherwise we will end up with wrong MTU (not initialized). Warning could be changed to errors, but that will not resolve the problem. I think that we will have to know why the transport is not connected... It's strange, because we've got incoming connection, so why it's not connected? Maybe it was closed between accept() (sco.c file) and hci_sco_get_mtu() (hci.c file)...

@borine
Copy link
Collaborator

borine commented Feb 16, 2022

So, I now think this is probably not a bluealsa bug, at least not in the latest code. The debug logs given above appear to be from release 3.1.0. There has been a major re-design of SCO I/O in bluealsa since then, so before looking any deeper, we need to establish whether this behaviour persists in the latest code. I haven't been able to reproduce in simple tests, and I don't have time to set up a clean test system at the moment. @bmt1596 is it possible for you to build from the latest master branch and re-test?

@bmt1596
Copy link
Author

bmt1596 commented Feb 16, 2022

@borine no problem. i will try with the latest version of Bluealsa. Anyway, thank you very much for your help.

@bmt1596 bmt1596 closed this as completed Feb 16, 2022
@arkq
Copy link
Owner

arkq commented Feb 20, 2022

@bmt1596, since you are experimenting with mSBC support, maybe you could try this PR #539 ? It adds PLC for received mSBC packets, so it should improve overall user experience when using mSBC codec. I've got no real test setup to check this PR properly, so I thought that maybe you could use this new code :).

@bmt1596
Copy link
Author

bmt1596 commented Feb 21, 2022

@arkq Hi, thank you very much for this help. I'm also having the problem of SBC packet loss for SCO but overall it doesn't affect too much. I try this new version with PLC support and update you with the results as soon as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants