My Own Streaming Radio, Thanks to Icecast

3
10617

Icecast? Don’t worry, I am not going to talk about chiselling sculptures out of ice here. Icecast is a server—an audio streaming server to be precise. It can broadcast both MP3 and Ogg Vorbis format audio in the form of streams. What I wanted was to be able to access my music library on my iPod Touch while at home. So, I was looking for a way to stream my music over the home network. After a little search and review, MPD (Music Player Daemon) with the Icecast server seemed to be the best option.

With this set-up, you can run even your own Internet radio. You can select the bit rate of the stream, the number of channels to stream, and the quality of the Ogg streams…there are numerous options to choose from.

My own radio? Wow! Where do I begin?

It’s fairly simple actually. We will cover the basics about creating and using a basic MPD set-up, which can be used as a local streaming service on your machine. Personally, I find it better than most players out there. This will, in turn, help us with the streaming application in the backend. Then, we will explore setting up Icecast. Take a look at Figures 1 and 2 to get a more holistic view.

Figure 1: An overview of the streaming server
Figure 1: An overview of the streaming server
Figure 2: How the streaming server works
Figure 2: How the streaming server works

Music Player Daemon

MPD (Music Player Daemon) is a daemon (a piece of software that runs in the background) that manages your music for you. It’s a client-server architecture. MPD acts as a server; and when you install a client, you can use it to interact with MPD and play music. It’s great to set up some old computer as the MPD server and route that music throughout your network. The advantage lies in the fact that it will require less space for storage—everyone has their own account and they can listen to whatever music they want to as long as it is on the server (or optionally, on your local machine).

Unless you are using Linux from Scratch or Rock Linux, your distribution repositories have the binaries for MPD. Install MPD and copy the file /etc/mpd.conf.example to /etc/mpd.conf. Open it in any preferred text editor. And then configure MPD as follows.

Edit the file and add the following lines—this is the most basic and simple, single-user configuration for MPD that we will use with Icecast:

music_directory        "/home/user/music"# Your music dir.
playlist_directory     "/var/lib/mpd/playlists"
db_file                "/var/lib/mpd/db"
log_file               "/var/log/mpd/mpd.log"
error_file             "/var/log/mpd/mpd.error"
pid_file               "/var/run/mpd/mpd.pid"
state_file             "/var/lib/mpd/mpdstate"
user                   "mpd"
# Binding to address and port causing problems
# in mpd-0.14.2 best to leave commented.
# bind_to_address      "127.0.0.1"
# port                 "6600"

audio_output {
    type               "alsa"
    name                "My ALSA Device"
    device           "hw:0,0"           # optional
    format           "44100:16:2"      # optional
}

# We assume that you use ALSA in this config.
# If you are using any other driver, please visit the
# official docs at http://mpd.wikia.com/wiki/Configuration
# to know how to setup your driver.

Most of the fields in the configuration file above are self-explanatory. You only need to change the music directory to your music directory path. Keep the other paths and even the user as mpd. We will get back to that later. Save the file for now. Come back to the root console.

Let’s create a new user, mpd, which will be used to access MPD.

useradd -g mpd -d /var/lib/mpd

Then make sure that the directory is owned by the mpd user, as well as the mpd group, by issuing the following command:

chown mpd:mpd /var/lib/mpd -R

Moving along, we now have to create the files mentioned earlier, and then transfer ownership of some files and directories to mpd:mpd. Execute the following commands:

touch /var/lib/mpd/db
touch /var/lib/mpd/mpdstate
touch /var/run/mpd/mpd.pid
touch /var/log/mpd/mpd.log
touch /var/log/mpd/mpd.error
chown -R mpd:mpd /var/lib/mpd
chown -R mpd:mpd /var/run/mpd
chown -R mpd:mpd /var/log/mpd

Now almost everything is done. Create the MPD music database as the root user. Then set 755 permissions to your home music directory:

mpd --create-db
chmod a+x /home/user
chmod -R a+x /home/user/music

You are done. You can install some MPD client, say mpc, ncmpc or Sonata (my favourite) and check if MPD runs. Start MPD as follows:

/etc/rc.d/mpd start

If everything is working fine (which it should if you follow the instructions given), then we can move on to the next section.

Setting up Icecast

Install Icecast. I am not going into the details of that one either. The following section gives the details about setting up a live Internet stream feed, which can be used as Internet radio. This article does not, in any way, condone the use of copyrighted music, podcasts or any other media files for use on the Internet. If you do not have the legal rights to stream a particular file, please do not stream it over the Internet. The author and the magazine is not responsible for any illegal action committed by the user. I’m glad we had this talk. Let’s move on…

Open the file /etc/Icecast.xml in your favourite text editor and edit the file to match your needs. The following code is the sample configuration file for Icecast that I am using right now:

<icecast>
    <limits>
        <clients>100</clients>
        <sources>2</sources>
        <threadpool>5</threadpool>
        <queue-size>524288</queue-size>
        <client-timeout>30</client-timeout>
        <header-timeout>15</header-timeout>
        <source-timeout>10</source-timeout>
        <burst-on-connect>1</burst-on-connect>
        <burst-size>65535</burst-size>
    </limits>
    <authentication>
        <!-- Sources log in with username ‘source’ -->
        <source-password>yourPass</source-password>
        <!-- Relays log in username ‘relay’ -->
        <relay-password>yourPass</relay-password>
        <!-- Admin logs in with the username given below -->
        <admin-user>adminUserName</admin-user>
        <admin-password>adminPass</admin-password>
    </authentication>

    <hostname>Local IP of your machine</hostname>

    <listen-socket>
        <port>Port you want to use</port>
    </listen-socket>

    <fileserve>1</fileserve>

    <paths>
            <!-- basedir is only used if chroot is enabled -->
        <basedir>/usr/share/icecast</basedir>

        <!-- Note that if <chroot> is turned on below, these paths must both
            be relative to the new root, not the original root -->
        <logdir>/log</logdir>
        <webroot>/web</webroot>
        <adminroot>/admin</adminroot>
        <alias source="/"dest="/status.xsl"/>
    </paths>

    <logging>
        <accesslog>access.log</accesslog>
        <errorlog>error.log</errorlog>
        <loglevel>4</loglevel> <!-- 4 Debug, 3 Info, 2 Warn, 1 Error -->
        <logsize>10000</logsize> <!-- Max size of a logfile -->
    </logging>

    <security>
        <chroot>1</chroot>

        <changeowner>
            <user>icecast</user>
            <group>icecast</group>
        </changeowner>

    </security>

</icecast>

You need to change a few things in the above code to make the connection a more secure one. In the <authentication> tag, the default passwords and the port (8000) used by Icecast are listed. Change them to any suitable password and port. Remember, using the default port and passwords makes Icecast more prone to attacks.

Again, for more security, we will change the <chroot> section to reflect <chroot>1</chroot>. Enter the user as Icecast, and enter the group also as Icecast. More on this later. For now, also change the paths to:

<paths>
    <basedir>/usr/share/icecast</basedir>
    <logdir>/log</logdir>
    <webroot>/web</webroot>
    <adminroot>/admin</adminroot>
</paths>

Save the file and create a user Icecast with the default group Icecast, and the home directory as /usr/share/Icecast/ as follows:

useradd -g icecast -d /usr/share/icecast/ icecast

That’s about it. Run Icecast with the following command:

icecast -b -c /etc/icecast.xml

To test if Icecast is really working, open http://localhost:8000/ in your Web browser (replace 8000 with the port you are using). You should be greeted with an “Icecast2 Status”page (refer to Figure 3). This indicates that everything is running properly.

Figure 3: Icecast server status page
Figure 3: Icecast server status page

How to stream using MPD

All right, you have installed and configured Icecast. The next step now is to add the Icecast stream channel as the output for MPD. You will have to decide what stream you want to use (Ogg or MP3) and change the options accordingly. Just add the following to the audio outputs section in the mpd.conf file:

audio_output {
    type "shout"
    encoding "ogg"         # change to mp3 for mp3 stream.
    name "foobar stream"port "8000"            # the port you are using.
    host "localhost"       # the host in use.
    format  "44100:16:2"   # 44KHz 16 bit 2 channel audio.
    mount "/stream.ogg"    # Change to /stream.mp3 for mp3.
    password "somepass"    # The password for ‘source’.
    quality "2"            # optional for ogg stream.
    bitrate "64"           # optional for mp3 stream.
    user "source"
}

Then add the following lines in case you are NOT using Alsa as the second audio out, to make sure MPD does not crash when Icecast is not running:

audio_output {
    type "alsa"
    name "fake out"
    driver "null"
}

Well. That’s it folks. All you need to do is to start Icecast and restart MPD.

/etc/rc.d/icecast start
/etc/rc.d/mpd restart

Here, make sure that Icecast is started before MPD in order to allow MPD to recognise it. You can add both the daemons to the start-up list. Just add their names to /etc/rc.conf file. Make sure that the Icecast statement is before mpd on that file.

How to play the stream

If you have reached this point without any trouble, then it’s fairly simple to play the stream. If you are using a Linux machine to play the songs, you have numerous options. Almost all the standard players come with stream input compatibility. It’s available in MPlayer, Amarok, Exaile, Rhythmbox, and many others. The term used is either an Internet radio stream or just a streaming media. For Windows, you can use Winamp.

I started out doing this for iPod Touch. I found a free application called MPoD for iPod that can either be used as a controller for MPD or as a player with Icecast. If you are unsure whether your player can play the audio streams, please Google for the information, as providing a how-to for all players in this guide is impossible and impractical.

You now have yourself an Internet radio. Go test it out and, of course, don’t forget to have some fun.

3 COMMENTS

  1. Any suggestion for a client that integrates mpd controlling functions AND icecast stream playback, and that is not Gnome dependant? The only one I could find is GMPC with Shout plugin, but I am a KDE person.

LEAVE A REPLY

Please enter your comment!
Please enter your name here