anonymix007 / libldacdec

A library for decoding audio in Sony's LDAC format
11 stars 7 forks source link

Decoding fails with errors "Unknown error" and "Invalid sync-word". #6

Closed Vinchethescript closed 1 year ago

Vinchethescript commented 1 year ago

I'm not sure if this is a bug of bluealsa or of this library, but I'm posting this issue here because it's related to decoding LDAC.

I followed the build instructions for BlueALSA (using v4.1.1) and Raspberry Pi (running Raspberry Pi OS Bullseye with all packages up to date). Just for testing, I'm running bluealsa -p a2dp-sink -c aac -c aptx -c sbc -c aptx-hd -c ldac as root on a terminal instead of using the systemd daemon for now. I can connect the phone (Galaxy S21 running One UI 5.1 with the August patch) to it, and I can enable LDAC on my phone's settings, but when I start playing music, the bluealsa output floods the terminal with this:

[...]
bluealsa: E: LDAC decoding error: Unknown error
bluealsa: E: LDAC decoding error: Unknown error
bluealsa: E: LDAC decoding error: Unknown error
bluealsa: E: LDAC decoding error: Unknown error
bluealsa: E: LDAC decoding error: Unknown error
bluealsa: E: LDAC decoding error: Unknown error
bluealsa: E: LDAC decoding error: Invalid sync-word
bluealsa: E: LDAC decoding error: Invalid sync-word
bluealsa: E: LDAC decoding error: Invalid sync-word
bluealsa: E: LDAC decoding error: Invalid sync-word
bluealsa: E: LDAC decoding error: Invalid sync-word
bluealsa: E: LDAC decoding error: Invalid sync-word
[...]

While these errors keep appearing on the console, you either hear weird noises or the music with lots of random stuttering (it changes as you restart the Bluetooth connection or BlueALSA). This is the configure command I used to build BlueALSA, if useful:

../configure --enable-manpages --enable-aac --enable-ldac --enable-mp3lame --enable-mpg123 --enable-aptx --enable-aptx-hd --enable-msbc --enable-rfcomm --enable-hcitop --enable-systemd --enable-cli --with-libopenaptx

I installed all the dependencies needed by BlueALSA, including the optional ones, and I installed the patched kernel for the Pi to support this.

If more information is needed, then you can ask me and I'll send it.

Vinchethescript commented 1 year ago

Update: I recompiled BlueALSA with the --enable-debug flag for configure. Along with the Unknown error message, I now got the following:

bluealsa: [497431] D: ../../src/utils.c:404: Unknown error code: 0xffffffff (API: 4095, handle: 1023, block: 1023)
bluealsa: [497431] E: ../../src/a2dp-ldac.c:375: LDAC decoding error: Unknown error

The same error message also appeared with these error codes and blocks:

Error code Block
0xffffffed 1005
0xfffffff0 1008
0xfffffff1 1009
0xfffffff2 1010
0xfffffff3 1011
0xfffffff4 1012
0xfffffff5 1013
0xfffffff6 1014
0xfffffff7 1015
0xfffffff8 1016
0xfffffff9 1017
0xfffffffb 1019
0xfffffffc 1020
0xfffffffd 1021
0xfffffffe 1022
0xffffffff 1023
anonymix007 commented 1 year ago

Rebuild libldacdec with debug cflags and post logs. You can also record dump in Wireshark.

Have you patched your kernel? It's necessary, actually. Also try this

Vinchethescript commented 1 year ago

I patched the RPi kernel using the kernel source packages I got through apt source. With that patch to bluealsa, I no longer get these error messages. On the first connection attempt, when I tried to play some audio, there were weird noises on the left channel, and no sound on the right channel. After a reconnection, you could hear the music. It isn't stuttering that much anymore, but now it just sounds choppy. Here are the logs as you asked.

anonymix007 commented 1 year ago

It seems that you aren't running the patched kernel (should be R:1021 W:1024, 672 isn't enough):

bluealsa: [2505908] D: ../../src/ba-transport.c:742: A2DP socket MTU: 14: R:672 W:1024

So your issue has exactly the same roots as #1, but it has different observed effect. Why? Well, seems that this is for the same reason why you don't have aptX Adaptive. Samsung devices use AOSP bluetooth stack even on Snapdragon SoCs. Apparently, something is different between how they handle this situation (packet is bigger than MTU): QTI stack just ignores the MTU (which is incorrect), AOSP fragments packets (which is not what bluealsa expect).

Vinchethescript commented 1 year ago

I see. Well, I realised that the kernel wasn't actually built because debhelper was just compiling the headers and the bootloader, and you had to compile the kernel itself manually; that was the cause of the issue persisting even after installing a "patched" kernel. I wanted to use the APT source packages because I wanted to keep the same kernel version as the latest released one. About the bluealsa patches from #1, I think you should add them to the README.md, so that people won't run into the same issue. Tomorrow I'll send a script about how I compiled the kernel for people who don't know how to build it, but now I'm just closing this issue. Thank you for your help and sorry for the "useless" issue.

anonymix007 commented 1 year ago

That patch doesn't actually fix the issue (packets are getting truncated, so decoder might produce incorrect audio), the proper fix is to apply kernel patch. This issue wasn't actually useless, it got me to think about this once more and I actually understood what exactly were those 4 bytes and why I did get different symptoms of the same issue. Script would be nice, could you make a PR with it in README.md?

Vinchethescript commented 1 year ago

Sure, I just made the script. It was too big to be put in the README.md, so I just created a different file for it. I'm creating the PR right now.