Stream audio over network with PulseAudio running on Raspberry Pi

Revisions:

Introduction

I am tired of plugging in the mini-jack to my laptop in order to listen to some music on my living room speakers. I want to be able to stream over my local 5GHz wifi network.

Server installation

The server can be any kind of box with pulseaudio installed. My server is now a Raspberry Pi connected with the mini-jack to my speakers.

Install some pre-reqs

# apt update
# apt install pulseaudio pulseaudio-module-zeroconf avahi-daemon

Define the pulseaudio service file to make it start on boot and log to the journal.

# tee /etc/systemd/system/pulseaudio.service
[Unit]
Description=PulseAudio system-wide server

[Service]
Type=forking
PIDFile=/var/run/pulse/pid
ExecStart=/usr/bin/pulseaudio --daemonize --system --realtime --log-target=journal
ExecStop=/usr/bin/pulseaudio -k
Restart=on-failure
LimitRTPRIO=1000
LimitNICE=-20

[Install]
WantedBy=multi-user.target

Since I had some problems running pulseaudio on the Pi in user mode, I’m just going with --system mode, although the logs tells me it is not advised. This Pi is only used headlessly by me anyway.

Appending some lines to the system config file of pulseaudio to enable network audio. Allowing link-local IPv6 addresses is vital if you’re on an IPv6-enabled network and Avahi is configured for it.

# tee -a /etc/pulse/system.pa
load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1;10.1.1.0/24;fe80::/64
load-module module-zeroconf-publish

Start it right away, but also enable it to make it start on boot automatically.

# systemctl daemon-reload
# systemctl start pulseaudio.service
# systemctl enable pulseaudio.service

Check the logs for errors

# journalctl -u pulseaudio

If you need more verbosity in the logs, you can set log-level = debug in the pulseaudio daemon.conf.

Start Avahi to enable automatic service discovery

# systemctl start avahi-daemon.service
# systemctl enable avahi-daemon.service

Client setup

The below might be Arch Linux specific!

Start Avahi to enable automatic remote speaker discovery

# systemctl start avahi-daemon.service
# systemctl enable avahi-daemon.service

Restart the user service

$ systemctl --user restart pulseaudio.service

Some times I was experiencing choppy sound. I did the following to remedy this

# tee /etc/libao.conf
default_driver=pulse
quiet
buffer_time=50
dev=combined
server=localhost

And whenever I’m getting choppy sound again, as I usually do for the initial song I play on SoundCloud, I restart the pulseaudio user service again and all is fine.

Now I’m able to use pavucontrol to select audio outputs for specific applications on my system.

References