anonymix007 / libldacdec

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

LDAC issue with bluez-alsa 4.0.0 #3

Open sydragos opened 1 year ago

sydragos commented 1 year ago

Hey, decided to open a separate issue to track it better.

Build bluez-alsa with: ../configure --enable-aac --enable-ofono --enable-aptx --enable-aptx-hd --with-libopenaptx --enable-ldac --enable-debug --enable-cli --with-alsaplugindir=/usr/lib/arm-linux-gnueabihf/alsa-lib

The syslogs appear identical when switching between aptX-HD and LDAC. But this is interesting:

pi@moode:~ $ bluealsa-cli -v info /org/bluealsa/hci0/dev_0C_C4_***/a2dpsnk/source
Device: /org/bluez/hci0/dev_0C_C4_***
Sequence: 9
Transport: A2DP-sink
Mode: source
Format: S32_LE
Channels: 2
Sampling: 96000 Hz
Available codecs: SBC AAC aptX aptX-HD
Selected codec: LDAC
Delay: 15.0 ms
SoftVolume: Y
Volume: L: 127 R: 127
Muted: L: N R: N

So even if LDAC was negotiated with the phone, it doesn't actually appear here as so. Moreover, I can use this command to change to any other codec except LDAC:

bluealsa-cli codec /org/bluealsa/hci0/dev_0C_C4_***/a2dpsnk/source SBC
bluealsa-cli codec /org/bluealsa/hci0/dev_0C_C4_***/a2dpsnk/source LDAC
bluealsa-cli: E: ../../../utils/cli/cli.c:447: CMD "codec": Couldn't select BlueALSA PCM Codec: SEP codec not available

But it was compiled in as mentioned in the readme:

pi@moode:~ $ bluealsa-cli status
Service: org.bluealsa
Version: v4.0.0
Adapters: hci0
Profiles:
  A2DP-source : SBC AAC aptX aptX-HD LDAC
  A2DP-sink   : SBC AAC aptX aptX-HD LDAC

pi@moode:~ $ bluealsa -h
Usage:
  bluealsa -p PROFILE [OPTION]...

Available BT profiles:
  - a2dp-source Advanced Audio Source (v1.3)
  - a2dp-sink   Advanced Audio Sink (v1.3)
  - hfp-ofono   Hands-Free AG/HF handled by oFono
  - hfp-ag      Hands-Free Audio Gateway (v1.7)
  - hfp-hf      Hands-Free (v1.7)
  - hsp-ag      Headset Audio Gateway (v1.2)
  - hsp-hs      Headset (v1.2)

Available BT audio codecs:
  a2dp-source:  SBC, AAC, aptX, aptX-HD, LDAC
  a2dp-sink:    SBC, AAC, aptX, aptX-HD, LDAC
  hfp-*:        CVSD

pi@moode:~ $ bluealsa-aplay -L
bluealsa:DEV=0C:C4:***,PROFILE=a2dp,SRV=org.bluealsa
    Pixel 7, trusted phone, capture
    A2DP (LDAC): S32_LE 2 channels 96000 Hz

EDIT: Using latest bluez-alsa or "--with-alsaplugindir=/usr/lib/arm-linux-gnueabihf/alsa-lib" doesn't change anything. Using recompiled kernel with your changes (but moode's original kernel modules, otherwise no audio out).

anonymix007 commented 1 year ago
  1. Write Raspberry OS Lite (64-bit) to USB Flash (I've used rpi-imager and 16 GiB USB drive)
  2. Build kernel according to official instructions with patch from README.md applied
  3. Connect via ssh and run build script (see README.md)
  4. sudo bluealsa -p a2dp-sink -c LDAC
  5. In another ssh window run pairable on and discoverable on in bluetoothctl, pair your phone once and later use bt-agent --capability=NoInputNoOutput &
  6. bluealsa-aplay

Now it started playing music. There seems to be a bug with changing sampling rate in bluealsa-aplay, but it's definitely issue with BluezALSA, not with my lib. After changing sample rate on phone from i.e. 96 kHz to 48 (or changing codec), music will be still played like with 96 kHz, so constant ALSA playback PCM underrun will be reported until bluealsa-aplay is restarted. UPD: this only happens in qemu and rpi os lite, so some resampling alsa component must be missing.

Report your issue to whatever distro you're using, something is clearly misconfigured.

sydragos commented 1 year ago

Do you think there could be an issue with 32 vs 64bit images? I will rebuild/retry on an Rpi OS Lite, and report back.

anonymix007 commented 1 year ago

That might be the case, but it's unlikely. How do you run bluealsa? It should be sudo bluealsa -p a2dp-sink -c LDAC

sydragos commented 1 year ago

I run it with multiple codecs so I can select on my phone later: -c LDAC -c SBC -c aptx-HD Recompiled LDAC with your instructions, it's the only codec that doesn't play any sound (I2S or HDMI speakers).

Tried on Moode and Rpi Lite OS. 32 vs 64bit board issue?

Edit: please let me know how to debug further...

anonymix007 commented 1 year ago

I'd say it's Moode issue, but if you're getting same issue with RPi OS Lite, it might be a problem with I2S or HDMI speakers (LDAC is 96 kHz, other codecs are 44.1/48, so maybe you DAC isn't properly configured). Try 3.5 mm audio output, that's what I've used with my RPi 4

sydragos commented 1 year ago

The Pi Zero W has no output jack. I did try putting down the sampling frequency to 44.1 or 48kHz, I see a change in the bluez-alsa logs, but no sound is coming out. What other commands/logs could I look into?

anonymix007 commented 1 year ago

Did you restart everything after changing frequency? bluez-alsa can force everything to be 44.1 kHz IIRC, so try that or remove temporarily disable 96 and 88.2 kHz sampling rates in source code. You can also try changing bitrate or using different phone.

The best way to see what's going on is to run dumpcap to capture Bluetooth packets, this will help understand why is there no sound. You can write simple program to parse the pcap file (or just export it into json and parse it) and feed the data into LDAC decoder (see ldacdec.c as a reference). To turn on debugging info in libldacdec, uncomment CFLAGS += -DDEBUG_ADAPTATION line in makefile and rebuild. It will print a lot of useful information.

Also, was the MTU set properly by kernel? bluez-alsa should print it

sydragos commented 1 year ago

Is this the MTU log line? bluealsa: [20744] D: ../../src/ba-transport.c:724: A2DP socket MTU: 14: R:672 W:1024

With the modded kernel and modules I see it as: bluealsa: [1371] D: ../../src/ba-transport.c:724: A2DP socket MTU: 14: R:1021 W:1024

anonymix007 commented 1 year ago

Yes, that's correct. Modified kernel seems to do the trick