2010.06.17-howto-downmix-live-stream

Truth is there's often a reason to use a low-bandwidth stream. Sure, it's not as high quality, but sometimes you need to be polite and use less bandwidth. Or the bandwidth simply isn't available. Or 32kbit audio is really 4 times phone quality, so how much more superior than -that- does it have to be?

The Simple

mplayer -> fifo -> lame -> stdout -> libshout example program that copies stdin to shoutcast server

The Complicated

Not that complicated, but there are a few command line switches that will help you out. And done in reverse order, because of the dependency requrements. Why this way? VLC claims to have a Icecast streaming output plugin, but all it ever does for me is segfault. Audacious2 doesn't have a IceCAST output plugin. Running any Windows streamer under WINE isn't practical on my Pegasos2 1ghz PPC machine.

The Shoutcast server

Out of scope for this document - there are several excellent tutorials on the net.

streamer

Go to the Icecast homepage and download the libshout package. Follow the instructions and install it (you don't necessarily have to install as root - I configured it to dump the files in my home directory). There will be a share/doc/libshout directory with two example .c programs - one blocking and the other nonblocking. The blocking one will do fine for our purposes.

cp example.c streamer.c
vi streamer.c (or whatever editor you use)

and change the login details (hostname/ip, mountpoint, password). Compile with

gcc -I/path/to/ICES0/include -L/path/to/ICES0/lib -o streamer streamer.c -lshout -lspeex

Note you need the libspeex libraries for this - apt-get install libspeex1 would work on a Debian/Ubuntu system, for example. I am considering making this better..stay tuned.

lame

The lame process is responsible for reading the FIFO (it will be wav data), converting it to mp3, and sending it to stdout for the streamer pipe (which will connect to the Shoutcast server). Start this process first, as it will block until mplayer starts sending it data through the FIFO. The -S switch is for very silent mode. I am converting to 32kbit, you can adjust your quality to suit your needs. (Can we optimize this?)

mkfifo radioin
lame -S -q 7 -b 32 radioin - | ./streamer

mplayer

Here we connect to our source. It can be anything you want to stream. Use -really-quiet to suppress a lot of mplayer output. The -idle switch will prevent mplayer from disconnecting to the stream prematurely. Without the switch I suspect mplayer dumps data too quickly and runs out of data to play. The other switches turn off mplayer's video algorithms (to save CPU) and streams a pcm wav to the FIFO.

mplayer -really-quiet -idle -vc null -vo null -ao pcm:waveheader:file=radioin http://stream.kraftyradio.com/radio

Conclusion

Voila! Results in 30 seconds that took me hours of research and many dead ends to find. And now, I can listen to the stream that doesn't get cut off every 2 seconds. It's a nominal 7-10% cpu increase on my box, though I haven't done any benchmarking other than casually observing top.