A few moths ago I had the idea of making a web-based broadcasting system to transmit audio from a server to mobile devices connected to my local network. As a web-handicapped programmer, I originally thought of a solution based on UDP sockets, but then realized that you can’t (or at least shouldn’t) work directly with UDP sockets from a web app. As it turns out, the easier way to go was to create a local Internet radio station, that way there is no need to write a single line of code for neither the server nor the clients.
Since most of the tutorials I found online were not entirely clear, I decided to write a
simple guide to get started with internet radio broadcasting mainly as a self reminder but also hoping that it will be useful for someone else.
This guide describes setup instructions for Ubuntu 14.04. Some of the tools are also available for other linux distributions and Operating Systems but these are not covered here.
First we need to install 3 things (all of them are free ):
idjc (website: http://idjc.sourceforge.net/index.html)
sudo apt-get install idjc
sudo apt-get install jackd
sudo apt-get install icecast2
MAKE SURE TO READ THE DOCUMENTATION OF THESE TOOLS TO AVOID UNWANTED RESULTS
Setting up the icecast server
An icecast server requires an xml configuration file. You can find a complete specification of this file and other basic setup steps in the official documentation (http://icecast.org/docs/icecast-2.4.1/basic-setup.html). For simplicity, in this post I will be working with some of the default settings but be aware that theses are VERY INSECURE, you should at least change the default security settings.
You can find an example configuration file after the installation of the icecast2 package at /etc/icecast2/icecast.xml. Make a copy of this file in a suitable location in order to modify it safely. For instance:
mkdir /home/omar/Desktop/icecastExample cp /etc/icecast2/icecast.xml /home/omar/Desktop/icecastExample/
You need to create two files called access.log and error.log in an appropriate path:
touch /home/omar/Desktop/icecastExample/access.log touch /home/omar/Desktop/icecastExample/error.log
Now open your copy of the icecast configuration file (icecast.xml) and look for the <logdir> tag. Change its contents from
to the path of the folder where your log files were created
Now the icecast server is ready to run. Use the -c option to indicate the path of the configuration file
icecast2 -c /home/omar/Desktop/icecastExample/icecast.xml
NOTE: If you get the message Server startup failed. Exiting, the icecast server might be already running. Type
sudo killall icecast2
to kill any previous instance of the server.
We need to tell our system which sound card to use for our broadcasting. To do so we will run jackd. The exact options to use will depend on your hardware but you can find a full description of jackd options at http://manpages.ubuntu.com/manpages/lucid/man1/jackd.1.html . To use the default sound card and the alsa driver you can run
jackd -d alsa
If you are unsure of which options to use, you can try
jackd -d dummy -r 44100
Now we’re almost done, just a few more steps!
Go to View/Output. You should now see a window like this:
Select a channel tab. Expand Configuration. In the Connection tab, press the new button. Fill the information in the new window:
Servert type – Icecast Master 2
Host name – The name or IP pf your computer (you can use localhost)
Mount Point – The name of your stream, it can be whatever you want but it is best to use something meaningful
Login Name – Needs to be source
Password – This is defined in icecast.xml. The default is hackme
Now go to the Format tab. Choose MPEG as Family. Click on the arrow at the bottom right corner of the window to display the following options. Choose MP3 as codec. Keep clickng the arrow button. You can use the default values for the following options.
Optionally, you can fill the info at the Stream Info tab. Fields are self explanatory.
Now expand Individual controls. Click the name of the stream. If everything went as planned your stream should now be up and running (the channel number should display a green dot).
Now you’re ready to stream something.
Testing your stream locally
Load a playlist in the player. Press play. Make sure that the Stream button of the audio feed section (under the playlist) is pressed. You should see the level bars moving.
To access your stream you can use the media player of your choice (mplayer, winamp, etc.) from any device on your local network using the URL of your stream as source. For instance to use mplayer and assuming the local ip of your server (the computer running icecast) is 192.168.0.8
Nevertheless, you can use an html5 compliant browser (such as google chrome) to play the stream by entering its URL.
Testing from outside your local network
To share your stream with the rest of the world you need to replace your local IP with your public address. You can obtain it from http://www.whatsmyip.org/ although you most likely need to configure port forwarding in your router (or talk to your network administrator) which is out of the scope of this post, but you can find detailed instructions for your router here http://portforward.com/english/applications/port_forwarding/Icecast/