The problem
iTunes + Remote app for iPod touch + AirPort Express = best kitchen radio ever. Most of my favorite public radio stations offer MP3 streams over HTTP. My wife, however, has a soft spot in her heart for certain stations that insist on broadcasting RealAudio over RTSP. How to get these to stream to the Airport Express in the kitchen?
The solution
The ingredients:
- Mplayer built with support for RealAudio/COOK (an exercise for the reader)
- LAME mp3 encoder
- Python!
The rudimentary solution
Mplayer, when built properly, can stream and decode RealAudio over RTSP. In the simplest form, this looks something like:
url="rtsp://stream2.rbb-online.de/encoder/antenne-live.ra"
mplayer -cache 48 -vo null -vc null -ao pcm:waveheader -ao pcm:file=foo.wav "${url}"
This is nice, but ideally we’d like to transcode a stream on the fly. Named pipes to the rescue!
url="rtsp://stream2.rbb-online.de/encoder/antenne-live.ra"
mkfifo dump.pipe
mkfifo transcode.pipe
mplayer -cache 48 -vo null -vc null -ao pcm:waveheader -ao pcm:file=dump.pipe "${url}" &
lame -r dump.pipe transcode.pipe
The transcoded MP3 can be read from transcode.pipe. Now, to wrap the whole kit an caboodle up in Python.
The full solution
This all gets wrapped up in a Python HTTP request handler based on SimpleHTTPRequestHandler. The handler forks instances of mplayer and lame and creates the named pipes on the fly. When the client closes the stream, the child processes are cleaned up automatically.
from subprocess import *
from SimpleHTTPServer import SimpleHTTPRequestHandler
from BaseHTTPServer import HTTPServer
import os,tempfile,signal,urlparse
dumper = None
default_url = "rtsp://stream2.rbb-online.de/encoder/antenne-live.ra"
values =
return
"""Serve a GET request."""
f =
if f:
try:
except:
if self.dumper is not None:
# mplayer forks a child, so we should go all Agamemnon via SIGINT
# reap dead children
# remove tempfiles
# unset instance variables
self.dumper = None
self.transcoder = None
self.dump_pipe = None
self.trans_pipe = None
self.tmpdir = None
if is_get:
f =
else:
f =
return f
qs = .query
if > 0:
query =
else:
query =
url =
self.tmpdir =
self.dump_pipe =
self.trans_pipe =
self.fnull =
self.dumper =
self.transcoder =
return self.trans_pipe
httpd =
sa =
print "Serving HTTP on", , "port", , "..."
if __name__ == "__main__":
To add a transcoded radio stream, make an M3U playlist like this:
#EXTM3U
#EXTINF:0,Antenne Brandenburg Frankenstream
http://localhost:8001/?url=rtsp://stream2.rbb-online.de/encoder/antenne-live.ra
and drag it into iTunes. Voila, (fake) RealAudio/RTSP support in iTunes!
Subtleties
There are a few potential stumbling blocks to be aware of:
- When streaming unlimited data over HTTP, you should not set the Content-Length header, as this will cause the client to close the connection after reading a set number of bytes rather than streaming forever.
- When streaming and transcoding, Mplayer forks a child process to handle some of the work. If killed with SIGINT, the parent process will kill the child, but if killed with SIGKILL, it will exit immediately, leaving a zombie Mplayer every time the stream is stopped and restarted.
No comments:
Post a Comment