Audio-playback interface in snaps running in daemon mode

While I created my first snap for the shairport-sync Apple Airplay daemon I ran into some trouble with audio playback. In the documentation it is recommended to use the new audio-playback interface which deprecates the old pulseaudio interface with some searching on Google and GitHub I managed to get the snap up and running after adding

# Make PulseAudio socket available inside the snap-specific $XDG_RUNTIME_DIR
if [ -n "$XDG_RUNTIME_DIR" ]; then
    if [ -S "$pulseaudio_sockpath" ]; then
        export PULSE_SERVER="unix:${pulseaudio_sockpath}"

To the wrapper script which I am using to launch the shairport-sync executable in the snap. If I do run the executable in the daemon: simple configuration I do get the following messages in the logs:

Mai 03 11:11:02 sauron shairport-sync[11234]: failed to connect to the pulseaudio context -- the error message is "Connection refused".                                                       
Mai 03 11:11:02 sauron audit[11234]: ANOM_ABEND auid=4294967295 uid=0 gid=0 ses=4294967295 pid=11234 comm="shairport-sync" exe="/snap/shairport-sync/x19/bin/shairport-sync" sig=6 res=1      
Mai 03 11:11:02 sauron systemd[1]: snap.shairport-sync.shairport-sync.service: Main process exited, code=dumped, status=6/ABRT                                                                
Mai 03 11:11:02 sauron systemd[1]: snap.shairport-sync.shairport-sync.service: Failed with result 'core-dump'.                                                                                
Mai 03 11:11:02 sauron audit[1]: SERVICE_STOP pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=snap.shairport-sync.shairport-sync comm="systemd" exe="/lib/systemd/systemd" hostname=? add
r=? terminal=? res=failed'                                                                                                                                                                    
Mai 03 11:11:02 sauron systemd[1]: snap.shairport-sync.shairport-sync.service: Service hold-off time over, scheduling restart.                                                                
Mai 03 11:11:02 sauron systemd[1]: snap.shairport-sync.shairport-sync.service: Scheduled restart job, restart counter is at 1.       

Unfortunately I was not able to find any snap which uses the audio-playback interface and run in daemon mode. The only documentation I could find was the usage of the pulseaudio interface in Ubuntu core Pulseaudio access from others snaps

Which seems to take advantage from the Pulseaudio server running in system mode and not user mode which is the typical case on a desktop.

Anyway, my goal is to have a running shairport-sync which can be installed both on the desktop and on a core device for example an Raspberry PI which can be used as an Airplay Backend.

The issue you are having is that daemons run as root and pulseaudio is not running on your system in a root session.

Based on your stated goal, for the desktop you’ll want to remove ‘daemon: simple’ and use the the autostart mechanism. Work is ongoing for session services but you should have something soon.

For Ubuntu Core, you’ll need a pulseaudio running on the system and that will either need to be provided by your snap or another. Today, there is the pulseaudio snap, but it doesn’t yet ‘slots’ audio-playback (though I believe the desktop team may be taking over this snap, @kenvandine, can you comment? If so, have it slots audio-record and audio-playback and pull in the mediation patches). For now, your snap should plugs both audio-playback and pulseaudio if you are going to use the existing pulseaudio snap on Ubuntu Core.

Thanks for the hint with autostart. The link you have posted is pointing to the forum top level page. I guess this is the correct link: autostart

Is there a way to define a single snapcraft.yaml which will work both on core and on the desktop or are there two different snapcraft.yaml files needed?