How to use pulseaudio for network stream with Raspbmc (Kodi)

pulseaudioraspbmc

I am using Raspbmc (with Kodi 14) on my Raspberry Pi. Now I would like to set up pulseaudio to be able to stream music from all my Linux computers easily to the Raspberry pi.
My problem is, that I am not able to get pulseaudio running on the Raspberry Pi. I tried the following steps:

1. Install pulseaudio

sudo apt-get install pulseaudio pulseaudio-module-zeroconf

2. Setting up pulseaudio

sudo nano /etc/pulse/default.pa

I added the following lines:

load-module module-native-protocol-unix auth-anonymous=1
load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1;192.168.1.0/24 auth-anonymous=1
load-module module-zeroconf-publish

3. Start pulseaudio

First I tried to start pulse as daemon:

pulseaudio -D

I also tried to start it system wide:

sudo pulseaudio --system 

But pulse does not show up on my other machines at all (it is configured correctly there as they find each other).
What is wrong here?

Best Answer

You have to edit system.pa, not default.pa. When you log in to your RPI and run ps fax you will notice this line:

/usr/bin/pulseaudio --system --realtime --log-target=syslog --log-level=1 --disallow-exit --disable-shm --exit-idle-time=-1

The --system is what makes pulseaudio read system.pa instead of default.pa. Also notice, kodi already launches an instance of pulseaudio for you and you can't simply start another one. The first will bind to 0.0.0.0:4713, so the second instance won't be able to! My kodi won't let me restart pulseaudio, so a reboot is required after changing the configuration file.

To make the remote sink actually show up on clients you have to (on the client):

  • load the module module-zeroconf-discover (pactl load-module module-zeroconf-discover) and
  • use paprefs to enable remote sinks. You have to check the first checkbox on the first tab paprefs shows. (Simply loading the module wasn't enough for me.)
  • restart avahi-daemon on the client.

You can use avahi-browse -a on your client to see if it actually sees your pa service. The important line will look like this:

+  wlan0 IPv4 root@raspbmc                   PulseAudio Sound Server local

I just did this about 2h ago and it doesn't really work for me. Yes, the remote sink shows up in pavucontrol and I can set it as default sink and control its volume, but spotify wouldn't work (Does not play music. Was my only test.). When I open a terminal and run export PULSE_SERVER=rpi02.local:4713; spotify it works. The approach of setting PULSE_SERVER works out of the box without any of this zeroconf shenanigans. (This maybe an Ubuntu spefic issue though.)

And if you intent to involve wifi there is another RPI specific issue:

There is the common problem of garbled audio playback when using the Pi's pulseaudio remotely over a network. Apparently using wifi frequently triggers this problem, but it's still a Pi-hardware specific problem with an available solution.

  1. On your RPi: sudo sed -i 's/^\(load-module module-udev-detect\)/\1 tsched=0/' /etc/pulse/system.pa
  2. restart pulseaudio on the Pi
  3. connect remote clients to the pa server on the Pi

This is a known issue stated together with this solution on kodi.wiki. Reading up on tsched, for example on ubunutu SE, we learn that it's a timing issue and arises from the sound hardware. By putting tsched=0 Linux uses a different timing model, which I believe is more CPU intense. With this solution, the pulseaudio service claims 17% CPU on my B+ when playing one remote stream.

Related Question