22 October 2015

Pulseaudio server in Docker container

The full title should actually read Pulseaudio server in docker container on Raspberry PI 2, but this doesn't really matter.

In short here it is:

Now everything one by one:
  • docker run --rm -ti - run non-persistent docker and prepare it as interactive (shell) run
  • --device=.... make local sound devices available in container. This feature is quite new in docker, so make sure you have recent version (mine is 1.8)
  • -p 4713:4713 set up port forwarding between host and container to make pulse server available to clients 
  • rpi-deb-pulse is my docker image built from armbuild/debian by running system upgrade and install pulseaudio
  • /bin/bash -c "..." is command to run inside container, more on this below
  • groupadd --gid $(...) alsadev create a group inside container named alsadev and having same id as group of /dev/snd/controlC0 device. We need this to allow pulse to access native alsa devices.
  • $(ls -l /dev/snd/controlC0 | cut -d' ' -f4 | xargs getent group | cut -d: -f3) this subshell is executed on host, not in container and prints group id of /dev/snd/controlC0
  • gpasswd -a pulse alsadev adds user pulse to our new group alsadev
  • pulseaudio ... run pulseaudio
  • -L 'module-alsa-sink device=hw:0,0' explicitly load alsa sink and specify which output to use
  • -L 'module-native-protocol-tcp auth-ip-acl=10.2.0.0/24' load tcp protocol listener and (optionally) configure ip-acl to only accept connections from given subnet

How to then use it to play sound remotely (client):
  1. pactl load-module module-tunnel-sink server=10.2.0.1
  2. in pavucontrol on Playback tab select desired sink for a stream
Things to improve:
  • list of devices may be replaced with another subshell to not specify each dev manually
  • add systemd unit to start container automatically
  • tune pulseaudio since sound over tcp tunnel is worse then playing locally
Why was this done :)
I have a Raspberry Pi 2 acting as a wireless router and i didn't wont to install pulse into host system. And i was just curious about whether this will work :)