How to delay camillaDSP service start until the HDMI playback device is available?

I have configured camillaDSP to start automatically as a service on my rPi4 (bookworm) but it always fails to open the HDMI playback device on startup.

Looking at the logs this seems to be because the ALSA hdmi playback device it is not (yet) available. If I restart the service manually after the boot it then always succeeds.

Unsuccessful service start at boot:
2024-01-11 11:40:26.391688 DEBUG [src/alsadevice.rs:334] Available Playback devices: [("hw:Loopback,0,0", "Loopback, ... <<< no mention of vc4hdmi0 !!!
2024-01-11 11:40:26.402195 ERROR [src/bin.rs:286] Playback error: ALSA function 'snd_pcm_open' failed with error 'EINVAL: Invalid argument'
Successful manual service restart a short time later:
2024-01-11 11:45:35.986926 DEBUG [src/alsadevice.rs:334] Available Playback devices: ... [("hw:vc4hdmi0,0,0", ... <<< vc4hdmi0 available
2024-01-11 11:45:35.991679 DEBUG [src/alsadevice.rs:352] Playback: supported channels, min: 2, max: 8, list: [2, 3, 4, 5, 6, 7, 8]
...
2024-01-11 11:45:35.992523 DEBUG [src/alsadevice.rs:392] Playback device "hdmi:vc4hdmi0,0" successfully opened

Is there a way to make the camillaDSP service start conditional on the ALSA HDMI device enumeration (preferred) or just to add a delay by trial and error (horrible)?

At the moment my /lib/systemd/system/camilladsp.service just waits for syslog:

[Unit]
After=syslog.target

Update

Funny how describing the problem focuses the mind... Immediately after posting I found out how to just delay any service start... and it all works as expected and hoped for, but I would still much prefer to avoid ad-hoc delays by using a suitable "After" rule!

If no better (e.g. "After") solution turns up, then here is how to fix it with a delay in the /lib/systemd/system/camilladsp.service:
[Service]
ExecStartPre=/bin/sleep 5
 
Last edited:
I assume the os device to target is "/dev/dri/card0"

I'll give this all a try but...
  • Does this risk starting before "syslog"? (I assume not due to the U in udev)
  • Does this guarantee to fix the problem? Is the device visible in ALSA environment as soon as the os device appears, or at least before udev triggers?
Thanks
 
I assume the os device to target is "/dev/dri/card0"
Alsa devices are in /dev/snd/cardXX.
Does this risk starting before "syslog"? (I assume not due to the U in udev)
I am afraid I do not understand, why?
Does this guarantee to fix the problem? Is the device visible in ALSA environment as soon as the os device appears, or at least before udev triggers?
udev is a user-space daemon (today part of systemd) which manages the dev virtual filesystem in /dev, based on uevent messages sent from the kernel and on its rules. See e.g. https://en.wikipedia.org/wiki/Udev + lots of info online.

A device is "visible in ALSA environment" when its /dev/snd/... device file is created. "ALSA environment" is just a library alsa-lib (libasound2...), linked to executables using alsa (clients). Clients must enumerate/check for the available devices (via alsalib, of course). If a client wants to know a new device was created (e.g. pulseaudio), it must listen to udev events - https://stackoverflow.com/questions/36975381/alsa-callback-when-new-card-device-is-added .

Using udev rules for actions on newly created/removed/anything devices is the standard method.
 
Alsa devices are in /dev/snd/cardXX.
Thanks. This is the key to my puzzle: That alsa devices are indeed represented in the file-system! (I couldn't "find" any devices matching the alsa names like "hdmi" "Loopback" etc. so I assumed they were some sort of user-space objects interfaced via the alsa library.)

My next puzzle... Which snd devices correspond to the alsa "hdmi0/1" devices?
$ ls -l /dev/snd
total 0
drwxr-xr-x 2 root root 120 Jan 12 00:52 by-path
crw-rw---- 1 root audio 116, 6 Jan 12 00:52 controlC0
crw-rw---- 1 root audio 116, 8 Jan 12 00:52 controlC1
crw-rw---- 1 root audio 116, 10 Jan 12 00:52 controlC2
crw-rw---- 1 root audio 116, 12 Jan 12 00:52 controlC3
crw-rw---- 1 root audio 116, 3 Jan 12 00:52 pcmC0D0c
crw-rw---- 1 root audio 116, 2 Jan 12 00:52 pcmC0D0p
crw-rw---- 1 root audio 116, 5 Jan 12 00:52 pcmC0D1c
crw-rw---- 1 root audio 116, 4 Jan 12 00:52 pcmC0D1p
crw-rw---- 1 root audio 116, 7 Jan 12 00:52 pcmC1D0p
crw-rw---- 1 root audio 116, 9 Jan 12 00:52 pcmC2D0p
crw-rw---- 1 root audio 116, 11 Jan 12 00:52 pcmC3D0p
crw-rw---- 1 root audio 116, 1 Jan 12 00:52 seq
crw-rw---- 1 root audio 116, 33 Jan 12 00:52 timer