Pulseaudio / alsa from a simple daemon

I am trying to create a snap package for a music playing server I am working on. I am using libasound to play audio and have been struggling to get that working. I am close to have something working, at least on Ubuntu 17.04, I haven’t considered Ubuntu core just yet.

When I run it as a daemon, I get:

ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused

If I run it with

snap run musciteer

It works. Probably because the daemon runs as root. I would like to run the service as a specific user, but I don’t believe that is possible as it is.

Any one with an idea on how to get audio working from a daemon?

This is the snapcraft.yaml

name: musciteer
version: 0
summary: Music playing daemon for Linux.
description: Musciteeer seamlessly mixes your local music with music streaming services and presents it as one music library.
confinement: devmode  # use "strict" to enforce system access only via declared interfaces

parts:
    alsa-lib:
        plugin: autotools
        source:  git://git.alsa-project.org/alsa-lib.git
        source-type: git
        source-tag: v1.1.3
        configflags:
            - --prefix=/usr
            - --sysconfdir=/etc
            - --libexec=/usr/lib
            - --libdir=/usr/lib
            - --localstatedir=/var
            - --with-configdir=/snap/musciteer/current/usr/share/alsa
            - --with-plugindir=/snap/musciteer/current/usr/lib/alsa-lib
        stage:
            - -usr/share/alsa/alsa.conf
            - -usr/share/alsa/pcm
            - -usr/share/alsa/topology/broadwell

    alsa-pulse-conf:
        plugin: dump
        source: /usr/share/alsa/alsa.conf.d
        organize:
            '*' : usr/share/alsa/alsa.conf.d/
        stage:
            - usr/share/alsa/alsa.conf.d/50-pulseaudio.conf
            - usr/share/alsa/alsa.conf.d/pulse.conf

    alsa-pulse-module:
        plugin: dump
        source: /usr/lib/x86_64-linux-gnu/alsa-lib/
        organize:
            '*' : usr/lib/alsa-lib/
        stage:
            - usr/lib/alsa-lib/libasound_module_ctl_pulse.so
            - usr/lib/alsa-lib/libasound_module_conf_pulse.so
            - usr/lib/alsa-lib/libasound_module_pcm_pulse.so

    musciteer:
        plugin: dump
        source: ..
        organize:
            build/musciteer: bin/
            #public/**: public/
            lib/libspotify-12.1.51-Linux-x86_64-release/lib/libspotify.so*: usr/lib/x86_64-linux-gnu/
        stage:
            - bin/musciteer
            - usr/lib/x86_64-linux-gnu/**
            - public/**
        stage-packages:
            - libasound2-data
            - pulseaudio

apps:
    musciteer:
        command: bin/musciteer
        daemon: simple
        plugs: [home, unity7, pulseaudio, alsa, network, network-bind]

I think today many distributions run pulseaudio as a session service and pass authentication cookies so you will find it hard/impossible to use pulse from a background service that runs as the root user. Once session services become possible though, it should work (the daemon would then run in the user’s session and not as root).