Using a Raspberry Pi with Android phones for media streaming with UPnP / DLNA

Posted in Posted on 2013-01-04 23:03

This summarises and completes my previous posts on configuring a Raspberry Pi to play music streamed from another computer in the house, using Android phones as remote controls.

There is quite a long discussion below and also an area on the Raspberry Pi forum for discussing this solution.

Attention!

Edit, 2013-07-21: Please note there is now a completely updated (and simpler) blog post on this subject.

What we are aiming for

A music system with your music stored on a server and played through your Hi-Fi via the Raspberry Pi, all controlled by multiple, synchronised Android phones/tablets. In addition you can play music from the server on your phone either in the house or anywhere else. This is similar to using Apple's AirPlay system but uses free software and open standards.

With any luck your initial outlay will only be about £45. The end result will be similar to the systems from Sonos and Squeezebox costing loadsamoney. Further additional devices to play music through another Hi-Fi or TV would also be £45.

What you need

  • A Raspberry Pi
    • I got mine with a case from ModMyPi for £35
  • A wireless USB dongle (or wired ethernet connection near your Hi-Fi)
  • A micro-USB power supply (most phones use these)
  • An SD Card (2GB minimum) - perhaps an old one from a camera?
  • An SD Card reader/writer - often built in to a computer
  • Your music available on a linux server (e.g. CDs ripped to MP3 files) - this could be replaced with e.g. a NAS device or a Windows machine
  • An amplifier with a 3.5mm auxiliary input and speakers (perhaps a TV sound bar?)
  • At least one Android device
  • Some sort of router / wireless network to connect the media server to the Raspberry Pi
  • You might need a USB keyboard and a TV/monitor to do the initial set-up (hopefully you have these lying around)

Note: these instructions assume you have your music on a linux server, but it's also possible to get all the necessary software on one Raspberry Pi and just point it at a NAS (or, indeed, plug a USB drive in directly). See comments for some more suggestions.

Instructions

Install Debian Wheezy on the Pi

The Raspberry Pi comes with no operating system (OS). The first thing to do is to download one and put it onto the SD card. These instructions assume you are using the Debian Wheezy image (I have checked these instructions work with the 2012-12-16-wheezy-raspbian.zip) from the Raspberry Pi site. There are brief instructions there and detailed instructions at elinux.org.

Once you have the OS on the SD card, put it in the Raspberry Pi, plug in your keyboard and TV (via the HDMI or composite video sockets), plug in the micro-USB power supply and turn it on.

Getting the Pi on the wireless network

You can of course skip this if you're just going to plug it in with an ethernet cable.

Different wireless dongles use different drivers and different home networks have different security settings. It's therefore pretty tricky to describe how to do this in a generic way but I can give you some pointers.

The lsusb command shows you what USB devices are plugged in. The 4th one on the list below is the Wi-Fi dongle.

$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter

I didn't have to edit the /etc/network/interfaces file. It was already how I needed it, namely:

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

Amongst other things, this says that the wireless dongle can be plugged in and out when you like, it is configured with DHCP (so it gets an IP address and hostname automatically from your router) and that the password and other security settings for your Wi-Fi can be found in the /etc/wpa_supplicant/wpa_supplicant.conf file.

By typing the command sudo vi /etc/wpa_supplicant/wpa_supplicant.conf you can edit the file (replace vi with nano if you like your text editors to help you out a bit). For me I needed to add these lines:

network={
  ssid="your_ssid_goes_here"
  scan_ssid=1
  proto=RSN
  key_mgmt=WPA-PSK
  pairwise=TKIP
  psk="this is where your secret passphrase goes"
}

Your configuration depends on what Wi-Fi security settings you have on your router: the above work for "WPA2 personal" with "TKIP+AES" for the WPA algorithm with a WPA shared key. There's more info to be found by typing man wpa_supplicant.conf.

At this point, typing ifconfig will show you the configuration of "eth0" (the ethernet interface with the cable in), "lo" (a dummy interface) and "wlan0": the new wireless interface.

wlan0 Link encap:Ethernet HWaddr 00:01:02:03:04:05
      UP BROADCAST MULTICAST MTU:1500 Metric:1
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000
      RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

At this point you should probably also configure your router to provide the Pi with an IP address via DHCP (it may do this automatically). I use DD-WRT on my router which lets me assign it a static IP address and a hostname as well. Again, it's a bit beyond the scope of this post to explain all that but you may need the Wi-Fi dongle's MAC address which is called the "HWaddr" in the ifconfig output.

If you then do a sudo ifdown wlan0 and a sudo ifup wlan0 to take the interface down and bring it up again then with any luck, on executing ifconfig again you'll see an additional line showing that your router has assigned an IP address to the Wi-Fi dongle:

wlan0 Link encap:Ethernet HWaddr 00:01:02:03:04:05
      inet addr:192.168.1.99 Bcast:192.168.1.255 Mask:255.255.255.0
      etc, etc... (addresses changed to protect the innocent)

Installing software on the Pi

Now the Raspberry Pi is on the wireless network, you can disconnect it from the TV and keyboard and put it next to the Hi-Fi. Connect it to the auxiliary input of the Hi-Fi with a stereo audio cable (3.5mm jack like headphones use) and power it up again.

Log in to the Pi from another computer using SSH (username "pi", password "raspberry"). I use PuTTY from my Windows laptop which does the job nicely. You can then type in comfort at your laptop rather than sitting on the floor by the TV. Incidentally, with PuTTY you paste by clicking with the right mouse button (and copy just by highlighting with the left). If you're on a Windows machine therefore you can copy and paste from this blog post using Ctrl-C to copy and right-click in PuTTY to paste.

The first thing to complete the set-up of the Pi and update all the packages already installed on the Pi to the latest versions:

$ sudo raspi-config
$ sudo apt-get update
$ sudo apt-get upgrade

The first command runs a config utility that lets you choose your country, keyboard, etc and also lets you choose to stop the Pi booting straight into a desktop which we don't want. The next gets the latest list of packages and the third command updates existing packages to the latest versions found on that list. This will take a little while.

We then need to install gmrender-resurrect which is a UPnP "renderer". That is, it plays (renders) music files when told to via the UPnP protocol. It is not provided as a pre-built package so we also have to compile it and manually install all the prerequisites.

# install all the packages we'll need

$ sudo apt-get install git automake libglib2.0-dev gstreamer0.10-alsa gstreamer0.10-tools libgstreamer0.10-dev libupnp-dev libxml2-dev gstreamer0.10-ffmpeg gstreamer0.10-plugins-base gstreamer0.10-plugins-good gstreamer0.10-fluendo-mp3 gstreamer0.10-pulseaudio pulseaudio

# Now get the latest gmrender-ressurect using git

$ cd
$ git clone https://github.com/hzeller/gmrender-resurrect.git
$ cd gmrender-resurrect

# build and install gmrender

$ ./autogen.sh
$ ./configure LIBS=-lm
$ make
$ sudo make install

gmrender uses GStreamer to play sound and GStreamer uses ALSA. For some reason, the sound quality through GStreamer at this point is really bad. To get it sounding good we need to direct the sound through the PulseAudio system (which then uses ALSA). To make GStreamer use PulseAudio rather than directly using ALSA you need to do the following configuration:

$ gconftool-2 -t string --set /system/gstreamer/0.10/default/audiosink pulsesink
$ gconftool-2 -t string --set /system/gstreamer/0.10/default/audiosrc pulsesrc

Note that these commands are executed as the "pi" user and only apply to the "pi" user. If you later run gmrender (or GStreamer directly) as another user (such as root) then the sound quality will be bad again as PulseAudio won't be in the stack.

To get PulseAudio to start on boot and to allow the "pi" user to access it, two more changes are required:

  1. Edit /etc/default/pulseaudio and change the first uncommented line to PULSEAUDIO_SYSTEM_START=1. This is not recommended but it works in this case.
  2. Execute the command sudo adduser pi pulse-access to add the pi user to the pulse-access group so that it is permitted to use PulseAudio.

You'll also need to turn the volume up on the Pi. To do this, run alsamixer and press the up arrow cursor key until the volume display gets to the top (if this isn't doing anything then it may be muted: press "M"). Then press the Esc key to exit. To then save this configuration for the next time the Pi boots, type sudo alsactl store which updates the /var/lib/alsa/asound.state file.

2013-01-91 Edit: I've written much more about configuring ALSA on the Raspberry Pi here.

To make gmrender start when the Pi is turned on, create the file /etc/init.d/gmediarenderer (as root) with the following contents (e.g. using vi or nano):

#!/bin/sh

### BEGIN INIT INFO
# Provides: gmediarender
# Required-Start: $remote_fs $syslog $all
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start GMediaRender at boot time
# Description: Start GMediaRender at boot time.
### END INIT INFO

USER=root
HOME=/root
export USER HOME
case "$1" in
start)
echo "Starting GMediaRender"
start-stop-daemon -x /usr/local/bin/gmediarender -c pi:audio -S -- -f "Raspberry" -d
;;
stop)
echo "Stopping GMediaRender"
start-stop-daemon -x /usr/local/bin/gmediarender -K
;;
*)
echo "Usage: /etc/init.d/gmediarender {start|stop}"
exit 1
;;
esac
exit 0

Then do:

$ sudo chmod 755 /etc/init.d/gmediarenderer
$ sudo update-rc.d gmediarenderer defaults

Reboot (either sudo shutdown -r now or just turn it off and on again) and you should have a working system.

Installing software on the media server

I have all my CDs ripped as FLAC files along with some MP3s I've bought on a small linux box (an Acer Revo Aspire 3700 bought a couple of years ago in the pre-Raspberry era) with 2 USB drives plugged into it. It currently runs some version or other of Ubuntu. There's no reason that you couldn't use another Raspberry Pi for this with a USB disc drive attached or even the same Raspberry Pi, perhaps pointed at a NAS box for the media storage.

We need to install:

  • minidlna: this serves up the music files using the DLNA / UPnP protocol;
  • BubbleUPnP server: this provides the OpenHome protocol on top of all the UPnP devices so that all your controllers (phones, tablets) share the same playlist and "playing now" view.

For minidlna you can just use apt-get:

$ sudo apt-get install minidlna

Point minidlna at your music files by editing the /etc/minidlna.conf file, e.g.:

# set this to the directory you want scanned.
media_dir=A,/mnt/usbdisc1/music/albums

# set this if you want to customize the name that shows up on your clients
friendly_name=My DLNA Server

# set this if you would like to specify the directory where you want MiniDLNA to store its database and album art cache
db_dir=/var/cache/minidlna

I also had to sudo chown minidlna.minidlna /var/cache/minidlna to get it to work, but YMMV.

The BubbleUPnP server is also easy to install, just following the instructions on the web site, which for me (on Ubuntu) was just:

$ sudo add-apt-repository ppa:bubbleguuum/bubbleupnpserver
$ sudo apt-get update
$ sudo apt-get install bubbleupnpserver
$ sudo start bubbleupnp

To install BubbleUPnP on a Pi, follow the "Other Java Platforms" instructions on the BubbleUPnP server web page (e.g. download the zip file etc).

Then go to the address http://your.media.server:58050 and you will see the BubbleUPnP server admin interface. The important page for us is the "Media Renderers" tab where you should find the Raspberry Pi's gmrender service listed as "Raspberry". Select it and tick the "Create an OpenHome renderer" box on the right and that's that sorted.

Installing software on Android devices

If you have an Android phone then go to Google Play and install BubbleUPnP. It is a UPnP renderer and control point and also works with the OpenHome protocol. In other words, it will play music (and videos and pictures) from the media server and can also tell the Pi to play music. As it uses the OpenHome system, playlists and so on are shared between all Android devices connected to the system. The app has some limitations (not too restrictive) and you can pay £3 to have it unrestricted which is well worth it whether you need to or not if you ask me.

If you don't have an Android phone, just go and buy a second-hand one for £50, don't bother sticking a SIM card in it and just use it as a remote control!

Once installed on the phone, run BubbleUPnP and go to the "Devices" page. You should see under "Renderers" the "Local renderer" (your phone), "Raspberry" and "Raspberry (OpenHome)". "Raspberry" will work as the renderer but choosing that bypasses the OpenHome goodness already described. So, choose "Raspberry (OpenHome)" and under the "Libraries" select your media server. You will also see "Local Media Server" there (your phone's files) and perhaps some other computers on your network.

Finally, go to the "Library" page, choose some music and play it: it should come out of the Hi-Fi!

Final thoughts

In this post I have concentrated on the particular steps I have taken to get a working system from my starting point. There are many possible variations on this. For instance, you could have a workable system without a dedicated media server but just using your home PC instead: BubbleUPnP Server runs on Windows as well (if you need it at all), and you could use MediaMonkey for instance on the same machine to be the UPnP server. Or you could do without the media server entirely and just stream music from your phone to the Raspberry Pi.

Finally, there is a lot more to the BubbleUPnP client and server that I couldn't cover here and which isn't to do with the Raspberry Pi. The client for instance can play music from Google Music. You can also set up the server so that you can access it from outside your house and by some clever configuration make it so that if you go to a friend's house who has a DLNA renderer (lots of TVs do these days) then you can use your phone to tell your friend's TV to play music from your server!

Please see the Raspberry Pi forum for more discussion as well as the comments below.

Updates:

  • 2013-01-08: added instructions for getting PulseAudio to start on boot and altered gmediarenderer init script to make it start after PulseAudio.
  • 2013-01-09: added instructions to save ALSA volume between reboots.
  • 2013-01-10: clarified how to install BubbleUPnP Server on a Raspberry Pi.
  • 2013-01-14: added link to Raspberry Pi forum.
  • 2013-01-19: added better ALSA instructions and linked to my other post.

Comments

Comments powered by Disqus