Jul. 23., 2012

Streaming audio with MPD and Icecast2 on Raspberry Pi

Author: . License: Creative Commons License CC BY-SA 3.0, published on t3node.com

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.

Prerequisites

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.

Package installation

It's straighforward:

$ 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

Configuring MPD

A configuration set for the shout output needs to be added to /etc/mpd.conf

audio_output {
type "shout"
name "RasPi MPD Stream"
description "MPD stream on Raspberry Pi"
host "localhost"
port "8000"
mount "/mpd"
password "ICECAST_SOURCE_PASSWORD"
bitrate "128"
format "44100:16:2"
encoding "mp3"
}

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:

bind_to_address "127.0.0.1"

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

Start streaming

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:

$ groups
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

Fix permissions:

$ 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? Spread the word!

If you liked this article, please share it in your preferred social media circles (twitter, g+, facebook, etc.)

--> Back to the list of articles

License

Licensed under creative commonsThis 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.

Comments

  1. Dave LaDelfa wrote on August 17, 2012 at 02:20

    After the wget, I needed to do this, otherwise mpc ls wasn't seeing the file:

    mpc update

  2. Steffen wrote on August 19, 2012 at 17:50

    Thx Dave, I added it to the text.

  3. Steffen wrote on September 19, 2012 at 16:04

    If you have a whole bunch of songs to be added to the actual playlist, better use:

    $ mpc add /

    instead of

    $ mpc ls | mpc add

  4. ALI AKIN wrote on September 25, 2012 at 08:45

    Dear MR. Steffen MULLER..

    I'bout a Raspberry PI board for make a Live Audio Streamer
    I red your wroute at above..
    But I am junior beginner to Raspbian and RaspberryPI.
    I tried to install Darkness and Icecast to Raspberry,but it's
    not succesfully.
    Couyld you advice me that how can I ?nstall and configure of my Raspberry PI board for create a Audio IP Streamer,

    Thank you for your kindly help

    Best Regards

  5. Jeremy wrote on September 27, 2012 at 03:25

    I actually had to add the following to my icecast.xml file to get it to work:


    /mpd


    Did anyone else have to do this?

  6. Jeremy wrote on September 27, 2012 at 03:27

    Crap! The form removed the code:

    <mount>
        <mount-name >/mpd< /mount-name>
    </mount>

  7. Jeremy wrote on September 27, 2012 at 03:28

    Remove the spaces of course. I had to put those since the form removed it otherwise.

  8. Steffen wrote on October 1, 2012 at 13:33

    Sorry Jeremy for the hassle. The comments module is indeed crap.

  9. mmmatjaz wrote on October 15, 2012 at 00:33

    I'm using the built in httpd stream instead of icecast and the stream isn't exactly continuous, it keeps stopping and skipping. I think the reason is that mpd has to transcode the sound in realtime, which is probably to much to handle for the PI's cpu. Is there any other option that would allow to stream the sound in it's original format? I've got plenty of bandwidth, so that won't be a problem. I just won't to take that load off the CPU.

  10. mmmatjaz wrote on October 15, 2012 at 00:37

    FIX:
    * I want to take that load off the CPU.

  11. Steffen wrote on October 15, 2012 at 09:27

    I never used the builtin http streaming of mpd. AFAIK it also requires encoding, so you won't gain much performance here. I suggest to use raspbian, which ships hardfloat FPU support. This will reduce the load on your CPU significantly.

  12. ronald wrote on October 19, 2012 at 19:24

    Dr Muller
    New to Raspbian!
    Found the tutorial
    Error on install - fetch fails because they have changed the file name recently:

    E: Failed to fetch http://mirrordirector.raspbian.org/raspbian/pool/main/j/jackd2/libjack-jackd2-0_1.9.8~dfsg.4+20120529git007cdc37-4_armhf.deb: 404 Not Found

    Please, any suggestions?
    thanks

  13. Steffen wrote on October 19, 2012 at 20:09

    Dear ronald,

    try:

    $ aptitude update
    $ aptitude safe-upgrade

    Then continue with installation. It's safe to do that before installing new packages.

  14. om wrote on October 21, 2012 at 10:15

    Thanks very useful information

  15. om wrote on October 21, 2012 at 10:16

    why'd it link my name to a site? lol

  16. Gethin Evans wrote on November 29, 2012 at 07:28

    Excellent stuff! Great tutorial. Appreciated.

    This actually stopped my DAC from crunching, one of the libraries must have fixed it. Before I installed these libraries, my Wolfson DAC would sound horrible. Maybe now it bypasses alsa? Not sure, but now it sounds clean, even with 24bit 96khz flac files. mpc and vlc now plays back perfectly on my first gen pi.

  17. Mitesh wrote on February 13, 2013 at 19:55

    i m getting error...

    after typing: sudo /etc/init.d/mpd restart

    Starting Music Player Daemon: mpdFailed to load database: Failed to open database file "/var/lib/mpd/tag_cache": No such file or directory
    Failed to create sticker table: database or disk is full
    failed!

  18. Nikola wrote on February 16, 2013 at 10:08

    Hey, thanks for the tutorial. I was wandering if it is possible to make it work other way around. I have a music library in my Android and a Raspberry Pi (with speakers) in my bedroom. I want to play the music from my Android on the Raspberry speakers.
    Thank you in advance.

  19. Steffen wrote on February 16, 2013 at 18:00

    Hi Nikola.

    Sticking to my setup, you would need an app for android which is capable of sending audio streams (e.g. http streams). On RasPi you could run icecast which receives the stream from android. A simple command line player (like mplayer) on RasPi could then connect to the icecast instance and play the stream.
    Unfortunately I don't know such an android app.

    There might be other ways to solve your need, for example with upnp. But I'm sorry I don't know much about this and can't help you.

  20. matt wrote on February 28, 2013 at 13:51

    Brilliant tutorial, worked perfectly for me. As you mentioned, my RasPi's CPU is stuck at around 80% when I have mpd running in this configuration. I'd like to send multiple channels, and even do other things with the RasPi at the same time. Does anyone know of a way to reduce the CPU load? Perhaps by doing less or no processing on the sound files?

  21. Niko wrote on March 18, 2013 at 15:04

    all this manipulation works ;)
    thanks!!

Leave a comment

Sorry, due to technical issues, commenting is disabled at the moment. I'm on it.