How to stream live audio over the web using Icecast2 and Puredata

A brief, illustrated guide to setting up web audio streaming on your domain with automatic archiving using Icecast2 and Puredata.

So far, a large part of my experimentations with graphical dataflow programming have been using Puredata which, whilst usable for general programming tasks, particularly appeals to me because of its focus on audiovisual media.

Whilst perusing the documentation I came across the [mp3cast~] object, which is used to send audio to a Shoutcast or Icecast2 server for broadcasting over the web. This sounded like potentially-useful fun, so I spent an afternoon setting it up. Here’s how I did it.

Please note: this article is aimed at people who have some experience hosting and maintaining their own server software. If that’s not you it probably won’t be very interesting and you should check out a consumer-oriented audio streaming thing like mixlr.com

Installation

My plan was to run Icecast2 on my Debian server and proxy requests to it via Apache, allowing HTTPS access to the admin UI, and port-80 HTTP access to the streams.

Install Icecast2 from the Debian package repo as usual:

sudo aptitude update && sudo aptitude install icecast2

The installation process with prompt you for various passwords for the server. The source password is the one you’ll use from Pd (or whatever other client you use for sending audio to the server), you are unlikely to use the relay feature so that password does not matter much, and the admin password gets you access to the administration UI. All of these passwords are stored by Icecast2 in plaintext, and transmitted in the clear using HTTP Basic auth unless you set up TLS.

After installing that you should already have a functioning Icecast2 server up and running on port 8000. If using a firewall, you’ll need to open up that port in order to be able to send audio to the server even if your listeners access streams over port 80, as Apache’s mod_proxy doesn’t support the nonstandard HTTP methods used to stream audio (SOURCE). If you are using another webserver as a proxy it’s possible you may be able to get this working.

I opened port 8000 by adding this line to the file my iptables rules are stored in and restoring them:

-A INPUT -p tcp -m tcp --dport 8000 -j ACCEPT

You should now be able to visit your machine’s IP address :8000 and see the Icecast2 web UI.

Making the URLs nice with an Apache proxy

My installation of Apache2 came with all the required proxy mods, but they required enabling as so:

sudo a2enmod proxy proxy_http

I created a new virtual host at radio.waterpigs.co.uk to house my audio streams, reusing my TLS certificate for waterpigs.co.uk but also enabling unencrypted connections as the cert doesn’t cover radio.waterpigs.co.uk and I only really wanted it there so I could log in to the admin UI without sending credentials in cleartext. If you don’t have TLS set up simply leave out the second VirtualHost definition. My site config ended up looking like this:

<VirtualHost *:80>
    ServerAdmin barnaby@waterpigs.co.uk
    ServerName radio.waterpigs.co.uk

    ProxyPreserveHost On
    ProxyPass / http://localhost:8000/
    ProxyPassReverse / http://localhost:8000/
</VirtualHost>

<VirtualHost *:443>
    ServerName radio.waterpigs.co.uk
    ProxyPreserveHost On
    ProxyPass / http://localhost:8000/
    ProxyPassReverse / http://localhost:8000/

SSLEngine on SSLCertificateFile /etc/ssl/localcerts/waterpigs.crt SSLCertificateKeyFile /etc/ssl/localcerts/waterpigs-private-decrypted.key SSLCompression off Header add Strict-Transport-Security "max-age=15768000" SSLProtocol all -SSLv2 -SSLv3 SSLHonorCipherOrder on SSLCipherSuite "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA 3DES RC4 !aNULL !eNULL !LOW !MD5 !EXP !PSK !SRP !DSS +3DES 3DES +RC4 RC4" </VirtualHost>

Save, enable and restart Apache as usual. You should now be able to see the Icecast2 web UI at the host you specified, without any pesky ports.

For more on how to set up TLS, see the HTTPS article on the indiewebcamp wiki. I hadn’t used Apache’s mod_proxy before and followed instructions from this DigitalOcean community article.

Streaming Audio

At this point you should be able to connect to your Icecast2 server from Pd-extended and listen to the audio. Here’s a basic demo patch: Simple Icecast2 Audio Streaming Client (download and save as streamer.pd, then open in Pd-extended).

a png

If your patch connected successfully, you should be able to go to youdomain.xyz/live and listen to yourself, with a several second delay. Congraulations! You can now stream audio over the web.

Creating a Broadcast Archive

Icecast2 can automatically archive broadcasts as they go out. To do so you will have to explicitly configure a mountpoint in the Icecast2 configuration file (stream URL) rather than just using the ad-hoc ones created on demand.

Aptitude puts the configuration file in /etc/icecast2/icecast.xml. Add a section which looks like this:

<mount>
    <mount-name>/live</mount-name>
    <dump-file>/path/to/archive/destination/%FT%T.mp3</dump-file>
</mount>

The %FT%T.mp3 sets the filename to the datetime the broadcast was started. I have a folder in my home directory called broadcast-archives, with a folder in for each mountpount. These will have to be writable by whatever user runs icecast.