-
-
Save awidegreen/6003640 to your computer and use it in GitHub Desktop.
# systemd service spec for pulseaudio running in system mode -- not recommended though! | |
# on arch, put it under /etc/systemd/system/pulseaudio.service | |
# start with: systemctl start pulseaudio.service | |
# enable on boot: systemctl enable pulseaudio.service | |
[Unit] | |
Description=Pulseaudio sound server | |
After=avahi-daemon.service network.target | |
[Service] | |
ExecStart=/usr/bin/pulseaudio --system --disallow-exit --disallow-module-loading | |
ExecReload=/bin/kill -HUP $MAINPID | |
[Install] | |
WantedBy=multi-user.target |
@koenschepens
I managed to get it working. See my Audio setup here below:
Audio setup
Setup of pulseaudio in user mode
- Install latest version of pulseaudio
# command to install pulseaudio is:
# dnf install pulseaudio
# below command returns version of installed pulseaudio
[root@nuc-jan ~]# pulseaudio --version
pulseaudio 13.99.1-rebootstrapped
- create a specific user for pulseaudio server
useradd -m pulseaudio
- assure that systemd is running for user
pulseaudio
at all times (also after reboot)
loginctl enable-linger pulseaudio
# you can check this by below command which should return a line like:
# pulseau+ 1187308 1 0 10:32 ? 00:00:00 /usr/lib/systemd/systemd --user
ps -ef | grep pulse | grep systemd
- activate pulseaudio in user mode for user
pulseaudio
(hasuid = 1041
in my case)
[root@nuc-jan ~]# su - pulseaudio
[pulseaudio@nuc-jan ~]$ export XDG_RUNTIME_DIR=/run/user/1041
[pulseaudio@nuc-jan ~]$ systemctl --user enable pulseaudio
Created symlink /home/pulseaudio/.config/systemd/user/default.target.wants/pulseaudio.service → /usr/lib/systemd/user/pulseaudio.service.
Created symlink /home/pulseaudio/.config/systemd/user/sockets.target.wants/pulseaudio.socket → /usr/lib/systemd/user/pulseaudio.socket.
[pulseaudio@nuc-jan ~]$
- Check if pulseaudio socket (=
/run/user/1041/pulse/native
) is active for userpulseaudio
# assure you are user `pulseaudio` and then enter following command
# su - pulseaudio
[pulseaudio@nuc-jan ~]$ systemctl --user status pulseaudio.socket
● pulseaudio.socket - Sound System
Loaded: loaded (/usr/lib/systemd/user/pulseaudio.socket; enabled; vendor preset: enabled)
Active: active (listening) since Fri 2020-12-18 10:32:23 CET; 3min 53s ago
Listen: /run/user/1041/pulse/native (Stream)
CGroup: /user.slice/user-1041.slice/[email protected]/pulseaudio.socket
[pulseaudio@nuc-jan ~]$
- give user
pulseaudio
access toaudio
group so it has access to audio hardware (/dev/snd/*
)
# following command must be executed as user root:
usermod -a -G audio pulseaudio
This can be checked by:
# logon as user pulseaudio
root@nuc-jan ~]# su - pulseaudio
[pulseaudio@nuc-jan ~]$ groups
pulseaudio audio
# check if audio hardware is recognized (it might be needed to install alsa utilities)
[pulseaudio@nuc-jan ~]$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC283 Analog [ALC283 Analog]
Subdevices: 0/1
Subdevice #0: subdevice #0
[pulseaudio@nuc-jan ~]$
-
reboot the system
-
Following command can be used to check if pulseaudio can access audio card
(assure that no other program is accessing the audio card)
runuser -l pulseaudio -c 'export XDG_RUNTIME_DIR=/run/user/1041 ; pactl info'
- NOT NEEDED: Assure that pulseaudio socket is accessible by any user
chmod a+rwx -R /run/user/1041/pulse
chmod a+x /run/pulse/1041
- Changes to
/etc/pulse/daemon.conf
# JVA 2020-12-18 : next line assures that the daemon keeps on running
exit-idle-time = -1
- Changes to
/etc/pulse/default.pa
# JVA 2020-12-18: added auth-anonymous=true to below line
load-module module-native-protocol-unix auth-anonymous=true
- reboot the system to check if everything is properly setup
Test pulseaudio installation (including pavucontrol)
- Check if pulseaudio is running by entering following command as root user on host machine (=nuc-jan)
[root@nuc-jan ~]# export PULSE_SERVER=unix:/run/user/1041/pulse/native; pactl info
Server String: unix:/run/user/1041/pulse/native
Library Protocol Version: 33
Server Protocol Version: 33
Is Local: yes
Client Index: 16
Tile Size: 65472
User Name: pulseaudio
Host Name: nuc-jan.borsbeek
Server Name: pulseaudio
Server Version: 13.99.1-rebootstrapped
Default Sample Specification: s16le 2ch 44100Hz
Default Channel Map: front-left,front-right
Default Sink: alsa_output.pci-0000_00_0e.0.analog-stereo
Default Source: alsa_input.pci-0000_00_0e.0.analog-stereo
Cookie: ded1:9276
[root@nuc-jan ~]#
- install pavucontrol on host (pavucontrol cannot be installed in current node-red alpine image !)
dnf install pavucontrol
# assure that XQuartz is running on macbook and
# that you have enabled remote access (type "xhost +" in xterm window)
# you can then launch pavocontrol by following command
export PULSE_SERVER=unix:/run/user/1041/pulse/native; pavucontrol --display=Jans-MBP.lan:0.0
Accessing pulseaudio server (on host) in docker container
Assure that pulseaudio server (on host) is setup as in previous sections.
- Add following line to alpine docker image to install pulseaudio and also alsa, sox and pulseaudio utilities.
# https://wiki.alpinelinux.org/wiki/PulseAudio
# notes:
# - pavucontrol is not yet available in alpine image used in node-red:1.2.6-12
# - alsa-plugins-pulse allows to use alsa applications via default pulseaudio
RUN set -ex && apk --no-cache add sox pulseaudio alsa-utils pulseaudio-utils pulseaudio-alsa alsa-plugins-pulse
- In docker compose file add following lines for the container:
volumes:
...
# map the location of pulseaudio unix socket to /run/pulseaudio/socket
- '/run/user/1041/pulse/native:/run/pulseaudio/socket'
environment:
...
- PULSE_SERVER=unix:/run/pulseaudio/socket
- You can validate this by opening a console on the container and running below command
bash-5.0$ pactl info
Server String: unix:/run/pulseaudio/socket
Library Protocol Version: 33
Server Protocol Version: 33
Is Local: yes
Client Index: 4
Tile Size: 65472
User Name: pulseaudio
Host Name: nuc-jan.borsbeek
Server Name: pulseaudio
Server Version: 13.99.1-rebootstrapped
Default Sample Specification: s16le 2ch 44100Hz
Default Channel Map: front-left,front-right
Default Sink: alsa_output.pci-0000_00_0e.0.analog-stereo
Default Source: alsa_input.pci-0000_00_0e.0.analog-stereo
Cookie: ded1:9276
bash-5.0$
Change pulseaudio profile (using hdmi / analog / digital output)
Here below the commands to change the audio profile of the card.
Note that it is also possible to do this using pavucontrol.
# set profile to use digital output port (S/PDIF)
pactl set-card-profile alsa_card.pci-0000_00_0e.0 output:iec958-stereo+input:analog-stereo
# set profile to use analog output port (headphone)
pactl set-card-profile alsa_card.pci-0000_00_0e.0 output:analog-stereo+input:analog-stereo
# list audio card details (including available profiles)
pactl list cards
# check default sink/source
pactl info
Wow thank you so much for the extensive description! I will try it out :)
@janvda
Hello! I have a server with 3 VMs that I want to have sound-enabled. In particular I wanted the VM running my music server to start on boot, and with sound enabled. The audio part of that solution evaded me until I found your post. I i implemented it as described, except for using a different account, and it works beautifully.
Thanks!
As im trying to do something with this there is a problem with "8 NOT NEEDED: Assure that pulseaudio socket is accessible by any user" it looks like /run/user/1041/pulse is 700 it is restored after daemon restart.
To ensure systemd knows when PulseAudio has finished starting, you can use Type=dbus
. This is necessary if you need to start other services that depend on PulseAudio (for example receivers for streaming audio).
[Unit]
Description=Pulseaudio sound server
After=avahi-daemon.service network.target
[Service]
Type=dbus
ExecStart=/usr/bin/pulseaudio --system --disallow-exit --disallow-module-loading --daemonize=no
ExecReload=/bin/kill -HUP $MAINPID
BusName=org.pulseaudio.Server
[Install]
WantedBy=multi-user.target
I have the exact same problem. Did you ever manage to fix this?