libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
9.94k stars 1.84k forks source link

[RPi] Cannot open HDMI audio device (Unsupported audio format) #8660

Open rtomasa opened 11 months ago

rtomasa commented 11 months ago

System Details

System: Raspberry Pi 4B + Raspberry Pi OS Lite (Bookworm, 64bits, No X) SDL version: 2.26.4 Audio devices (names retrieved using SDL_GetAudioDeviceName):

bcm2835 Headphones, bcm2835 Headphones
vc4-hdmi-0, MAI PCM i2s-hifi-0
vc4-hdmi-1, MAI PCM i2s-hifi-0
KT USB Audio, USB Audio

ISSUE

When opening the audio devices with SDL_OpenAudioDevice(<device name>,...) using the above device names, only the 1st and 4th device (headphones and USB) work as expected. 2nd and 3rd hdmi devices are opened but immediately fails with the following error: alsa: Unsupported audio format

Interesting enough, if I configure /etc/asound.conf with the following configuration to set the vc4-hdmi-0 device:

defaults.pcm.card 1
defaults.ctl.card 1

and then I use SDL_GetAudioDeviceName(NULL,...), the hdmi sound device works fine.

SDL_AudioSpec configuration is the same in all test scenarios. AUDIO_S16SYS is the format used.

slouken commented 9 months ago

Do you know what effect changing the configuration in /etc/asound.conf has? Are you able to debug into SDL and find out why it's failing?

rtomasa commented 9 months ago

The effect of modifying /etc/asound.conf is limited to setting the default sound card. By default, Linux designates headphones as the default sound card (index 0). The configuration I've provided above is intended for establishing the first HDMI as the default sound card. This ensures that it is also the one automatically selected by SDL when passing the NULL value.

With that said, I'm curious about the differences in SDL's behavior when allowing it to choose the default card versus explicitly passing the name. If you have any suggestions for debugging this issue, I would appreciate it. I ran out of ideas.

rtomasa commented 9 months ago

In case it helps, this is the output of my aplay -L:

null
    Discard all samples (playback) or generate zero samples (capture)
default
    Default Audio Device
sysdefault
    Default Audio Device
hw:CARD=sndrpihifiberry,DEV=0
    snd_rpi_hifiberry_dac, HifiBerry DAC HiFi pcm5102a-hifi-0
    Direct hardware device without any conversions
plughw:CARD=sndrpihifiberry,DEV=0
    snd_rpi_hifiberry_dac, HifiBerry DAC HiFi pcm5102a-hifi-0
    Hardware device with all software conversions
default:CARD=sndrpihifiberry
    snd_rpi_hifiberry_dac, HifiBerry DAC HiFi pcm5102a-hifi-0
    Default Audio Device
sysdefault:CARD=sndrpihifiberry
    snd_rpi_hifiberry_dac, HifiBerry DAC HiFi pcm5102a-hifi-0
    Default Audio Device
dmix:CARD=sndrpihifiberry,DEV=0
    snd_rpi_hifiberry_dac, HifiBerry DAC HiFi pcm5102a-hifi-0
    Direct sample mixing device
hw:CARD=vc4hdmi0,DEV=0
    vc4-hdmi-0, MAI PCM i2s-hifi-0
    Direct hardware device without any conversions
plughw:CARD=vc4hdmi0,DEV=0
    vc4-hdmi-0, MAI PCM i2s-hifi-0
    Hardware device with all software conversions
default:CARD=vc4hdmi0
    vc4-hdmi-0, MAI PCM i2s-hifi-0
    Default Audio Device
sysdefault:CARD=vc4hdmi0
    vc4-hdmi-0, MAI PCM i2s-hifi-0
    Default Audio Device
hdmi:CARD=vc4hdmi0,DEV=0
    vc4-hdmi-0, MAI PCM i2s-hifi-0
    HDMI Audio Output
dmix:CARD=vc4hdmi0,DEV=0
    vc4-hdmi-0, MAI PCM i2s-hifi-0
    Direct sample mixing device
hw:CARD=vc4hdmi1,DEV=0
    vc4-hdmi-1, MAI PCM i2s-hifi-0
    Direct hardware device without any conversions
plughw:CARD=vc4hdmi1,DEV=0
    vc4-hdmi-1, MAI PCM i2s-hifi-0
    Hardware device with all software conversions
default:CARD=vc4hdmi1
    vc4-hdmi-1, MAI PCM i2s-hifi-0
    Default Audio Device
sysdefault:CARD=vc4hdmi1
    vc4-hdmi-1, MAI PCM i2s-hifi-0
    Default Audio Device
hdmi:CARD=vc4hdmi1,DEV=0
    vc4-hdmi-1, MAI PCM i2s-hifi-0
    HDMI Audio Output
dmix:CARD=vc4hdmi1,DEV=0
    vc4-hdmi-1, MAI PCM i2s-hifi-0
    Direct sample mixing device

And these are the testing results for hdmi0:

aplay -D default /usr/share/sounds/alsa/Noise.wav plays OK aplay -D sysdefault /usr/share/sounds/alsa/Noise.wav plays OK aplay -D default:CARD=vc4hdmi0 /usr/share/sounds/alsa/Noise.wav plays OK aplay -D sysdefault:CARD=vc4hdmi0 /usr/share/sounds/alsa/Noise.wav plays OK

aplay -D hw:CARD=vc4hdmi0,DEV=0 /usr/share/sounds/alsa/Noise.wav

Playing WAVE '/usr/share/sounds/alsa/Noise.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
aplay: set_params:1343: Sample format non available
Available formats:
- IEC958_SUBFRAME_LE

aplay -D plughw:CARD=vc4hdmi0,DEV=0 /usr/share/sounds/alsa/Noise.wav

Playing WAVE '/usr/share/sounds/alsa/Noise.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
aplay: set_params:1343: Sample format non available
Available formats:
- IEC958_SUBFRAME_LE

aplay -D hdmi:CARD=vc4hdmi0,DEV=0 /usr/share/sounds/alsa/Noise.wav

Playing WAVE '/usr/share/sounds/alsa/Noise.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
aplay: set_params:1349: Channels count non available

aplay -D dmix:CARD=vc4hdmi0,DEV=0 /usr/share/sounds/alsa/Noise.wav

ALSA lib pcm_direct.c:1206:(snd1_pcm_direct_initialize_slave) requested or auto-format is not available
ALSA lib pcm_dmix.c:1087:(snd_pcm_dmix_open) unable to initialize slave
aplay: main:830: audio open error: Invalid argument
slouken commented 1 month ago

We are scoping work for the SDL 3.2.0 release, so please let us know if this is a showstopper for you.

rtomasa commented 1 month ago

It is not an immediate stopper but it prevents the ability to choose/change between audio devices on the fly, requiring a change in the OS default audio device and system restart to make it effect.