August 2, 2006

Using MPD and Icecast to stream audio

Posted in Techie stuff at 21:33 by theBlackDragon

So what prompted me to do this in the first place? I tend to switch back and forth between my laptop and my desktop quite often and since both are in different rooms I like to have the same music playing on both…

Since I already used mpd to play my music from a remote Samba share I decided I might as well use that box to play it too and use a webinterface to control mpd and just stream the audio so I could listen to it from basically anywhere.

To make this work you’ll need a couple of things that you can probably install in a distribution specific way:

  • MPD from SVN (the release version won’t work at the time of writing)
  • Icecast, I used version 2.3.1
  • the Apache webserver with a working PHP configuration for phpMp
  • phpMp itself

Let’s start by setting up Icecast, after you have installed it you’ll have to edit the configuration file, which is an XML file that by default resides in /etc/icecast2/icecast.xml. In this file you’ll want to replace all occurences of ‘hackme’ with decent passwords. You’ll also want to set hostname to the hostname of the box icecast is running on. You can also change the port icecast uses here if you want to, though the default should be just fine. So much for setting up Icecast, let’s move on to MPD.

First you’ll have to do the default configuring of MPD, set up your music directory etc. once that’s done we are going to configure MPD to send it’s output to our Icecast server.

audio_output {
type            "shout"
name            "DragonStream"
host            "localhost"
port            "8000"
mount           "/stream.ogg"
password        "hackme"
quality         "5.0"
#       bitrate         "128"
format          "44100:16:1"
#
# Optional Parameters
user            "source"
#       description     "here's my long description"
#       genre           "jazz"
} # end of audio_output

Most of the options here are fairly self explanatory, name can be anything you want to call your stream and is what most clients will show you, the port is the port your Icecast server runs on. Mount defines the URL you can find your stream on, being icecast_hostname:icecast_port/mount. The quality option sets the quality of your stream, 5 is CD quality, alternatively you could set the bitrate option, but since this version of icecast only streams in Ogg Vorbis format it might be handy to just leave it as I suppose it’ll stream variable bitrate then (though I’m just wildly guessing here). There should be no need to change the format. Description and genre are optional and do what it looks they do: define the genre and description of your stream for clients that display use information.

I won’t go into the details of installing or configuring Apache and/or PHP as that’s such a huge and distro specific topic that I won’t even start to bother, so I assume you have a working Apache + PHP setup. So download phpMp, extract it somewhere in your webroot and open the config.php file, there are only two things of interest here: host and port, check if those are correct and close the file.

You should now be able to start playing songs from your phpMp interface and listen to them using some streaming client that supports Ogg Vorbis, like Winamp or XMMS, be aware that you can’t start listening to the stream if you’re not playing something, that’ll just give you an error.

You’re also not constrained to using phpMp, you can use any MPD client to connect to your MPD server, you just have to make sure you secure your server enough if you allow external connections. You can do this by disabling bind_to_address (just comment it out) and use the password directive to set a password.

All in all I like the flexibility this setup gives me, the only real issue is that pausing MPD disconnects Icecast clients.

Advertisements

2 Comments »

  1. somebody said,

    regarding the disconnect, many stream applications and also de icecast2 have the habit of disconnecting a silent stream.

    what you could do for having a more permanent stream, is to set up your mountpoint in icecast2 with the fallback-mountpoint and fallback-override options.

    When your remote stream goes down, the users get transferred to the fallback stream. when you are back, the users get back to your remote stream.

  2. anon said,

    > format “44100:16:1”

    > There should be no need to change the format.

    That format, the default, is mono. If the final 1 is a 2 the stream will be stereo. I’ve changed it here for LAN streaming, where there is plenty of bandwidth.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s