Streaming audio with MPD and Icecast2 on Raspberry Pi
The main purpose of my Raspberry Pi is to act as a central music box. This task can easily be solved with MPD and Icecast2. Read on and get it working within 15 minutes.
I strongly suggest to use the Raspbian distribution as operating system. It has built-in hardfloat support for the FPU on your Raspberry Pi. This truly boosts performance for audio encodings. Without hardfloat support, you'll probably end up with 100% CPU usage. The result would be playback hickups and no fun at all.
Icecast2 is a free audio streaming server which supporting the shoutcast protocol. MPD is a music player daemon which serves as a backend for playing audio. MPD uses the shoutcast library to stream to icecast2. Fortunately, Raspbian MPD is compiled with libshout and also mp3 support by default so we don't have to build any custom packages. MPC is a command line client to control MPD, for example to manage playlists or to start/stop playing a song.
$ sudo aptitude install mpc mpd icecast2
The following NEW packages will be installed:
icecast2 libao-common libao4 libaudiofile1 libavahi-glib1 libavcodec53 libavformat53
libavutil51 libcurl3-gnutls libdirac-encoder0 libfaad2 libgsm1 libjack-jackd2-0
libmms0 libmp3lame0 libmpcdec6 libmpdclient2 libschroedinger-1.0-0 libshout3
libspeex1 libtheora0 libva1 libvpx1 libwavpack1 libx264-123 libxvidcore4 mpc mpd
The package manager will ask you to configure Icecast2. You should do so and set a hostname and passwords for source, relay and administration. Needless to say to use strong passwords. The source password will be needed in the MPD configuration. (I used ICECAST_SOURCE_PASSWORD in the example)
The autoconfiguration of the MPD package could report some warnings about ipv6 and tag_cache:
Setting up mpd (0.16.7-2) ...
[....] Starting Music Player Daemon: mpd
listen: bind to '[::1]:6600' failed: Failed to create socket: Address family not supported
by protocol (continuing anyway, because binding to '127.0.0.1:6600' succeeded)
Failed to load database: Failed to open database file "/var/lib/mpd/tag_cache":
No such file or directory
The tag_cache issue doesn't harm. The file will be added anyway and the warning will not appear again. This will hopefully be fixed by the package maintainer. The other issue relates to missing ipv6 network support. You probably need to load the ipv6 kernel module to get rid of this warning:
$ sudo modprobe ipv6
A configuration set for the shout output needs to be added to /etc/mpd.conf
name "RasPi MPD Stream"
description "MPD stream on Raspberry Pi"
This will stream to the icecast2 server on localhost:8000, using /mpd as mount point. The stream will be encoded on the fly to 16bit, 2 channel stereo mp3 at 128k bitrate.
If you ran into the ipv6 warning before, you could alternatively solve that by setting an ipv4-only address:
Now restart MPD to reload the configuration:
$ sudo /etc/init.d/mpd restart
Check if things are up an running
A new output should have been added and enabled:
$ mpc outputs
Output 1 (My ALSA Device) is enabled
Output 2 (RasPi MPD Stream) is enabled
If the stream output is disabled, you can enable it using:
$ mpc enable 2
Icecast2 should be running out of the box without any additional configuration.
Check if both icecast2 and mpd are listening on tcp:
$ sudo netstat -ltpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 1299/icecast2
tcp 0 0 127.0.0.1:6600 0.0.0.0:* LISTEN 1734/mpd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1531/sshd
tcp6 0 0 ::1:6600 :::* LISTEN 1734/mpd
First you need to add a minimal playlist with a song. If neccessary, fix permissions for the music and playlist directories and check if your user (pi) is member of the audio group:
pi adm dialout cdrom sudo audio video plugdev games users input
If audio does not appear in this list, add it:
$ sudo usermod -a pi -G audio
$ sudo chmod g+w /var/lib/mpd/music/ /var/lib/mpd/playlists/
$ sudo chgrp audio /var/lib/mpd/music/ /var/lib/mpd/playlists/
Now download a demo song:
$ cd /var/lib/mpd/music/
$ wget http://www.jonobacon.org/files/freesoftwaresong/jonobacon-freesoftwaresong2.ogg
Add the song to the actual playlist:
$ mpc update
$ mpc ls | mpc add
$ mpc playlist
Jono Bacon - Free Software Song
Enable repeat mode in the test phase to prevent MPD from stoping:
$ mpc repeat on
volume: 80% repeat: on random: off single: off consume: off
Now start to play the song:
$ mpc play
Jono Bacon - Free Software Song 2
[playing] #1/1 0:00/3:18 (0%)
volume: 80% repeat: on random: off single: off consume: off
If your speakers are connected, you should hear the song playing. Credits for the Free Software Song go to Richard Stallmann and Jono Bacon ;-)
Then open your browser and point it to your raspberry host on port 8000: !http://your.raspberry.hostname:8000/
You should get the icecast status page with your stream at the mount point /mpd.
If you navigate to http://your.raspberry.hostname:8000/mpd.m3u the browser should ask you to open the stream with your favorite music player. Have a look at the codec of the stream and notice that it's mp3 at 128k, although the original song is ogg at 160k. On-the-fly encoding is obviously working.
Check your CPU usage using the top or htop command: it should be about 80%.
Which MPC clients to use?
If you have an Android smartphone, I suggest to use MPDroid.
On the desktop I use Sonata, a lightweight GTK+ client written in Python.
Did this article help you? Become a sponsor!
If you liked this article, I invite you to sponsor me one or two license keys for my Raspberry Pi. The key for MPEG2 costs £2.40, the key for VC-1 is £1.20. You can order the MPEG-2 key and the VC-1 key directly at the Raspberry Pi store. Once you received the key(s), send it per mail to firstname.lastname@example.org. That would be awesome. Thanks!
My serial number is: 00000000b0cd0a01
This article is licensed under the Creative Commons License CC BY-SA 3.0. You are free to share (copy, distribute and transmit) and to remix (to adapt) the work under the following conditions:
- You must attribute the work by mentioning the name of the author (Steffen Müller) and setting a link back to the original article using its URL.
- If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.
Leave a comment
Sorry, due to technical issues, commenting is disabled at the moment. I'm on it.