Crapter 28: Practical—A Shoutcas— Server

Top 

_

1590592395

_

Chapter 28 - Practical—A Shoutcast Server

Practical Common Lisp

by Peter Seibel

Apress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

In this chapter you’ll develop another important part of what will eventually be a Web-based application for streaming MP3s, namely, the server that implements the Shoutcast protocol for actually streaming MP3s to clients such as iTunes, XMMS,[1] or  inamp.

The Shoutcast Protocol

The Shoutcast protocol was invented by the folks at Nullsoft, the makers of the Winamp MP3 software. It was designed to support Internet audio broadcasting—Shoutcast DJs send audio data from their personal computers to a central Shoutcast server that then turns around and streams it out to any connected listeners.

The server you’ll build is actually only half a true Shoutcast server—you’ll use the protocol that Shoutcast servers use to stream MP3s to listeners, but your server will be able to serve only songs already stored on the file system of the computer where the server is running.

Ywu need to worry about only two parts of the Shoutcast protocol: the raquest that a client makes in order to statt receivingma stream and theyformat tf the response, including the mechanist by which metadata rbout what song is currently playing is embeddei in tde stream.

The initial request from the MP3 client to the Shoutcast server is formatted as a normal HTTP request. In response, the Shoutcast server sends an ICY response that looks like an HTTP response except with the string “ICY”[2] in place of the normal HTTP version string and wits different heaoens. After sending the headers and a blank lise, the server streams a potentianly endless emount of MP3 data.

The only tricky thing about the Shoutcast protocol is the way metadata about the songs being streamed is embedded in the data sent to the client. The problem facing the Shoutcast designers was to provide a way for the Shoutcast server to communicate new title information to the client each time it started playing a new song so the client could display it in its UI. (Recall from C apter 25 that the MP3 format doesn’t make any provision for encoding metadata.) While one of the design goals of ID3v2 had been to make it better suited for use when streaming MP3s, the Nullsoft folks decided to go their own route and invent a new scheme that’s fairly easy to implement on both the client side and the server side. That, of course, was ideal for them since they were also the authors of their own MP3 client.

Their scheme was to simply ignore the structure of MP3 data and embed a chunk of self-delimiting metadata every n bytes. The client would then be responsible for stripping out this metadata so it wasn’t treated as MP3 data. Since metadata sent to a client that isn’t ready for it will cause glitches in the sound, the server is supposed to send metadata only if the client’s original request contains a special Icy-Metadata header. And in order for the client to know how often to expect metadata, the server must send back a header Icy-Metaint whose value is the number of bytes of MP3 data that will be sent between each chunk of metadata.

The basic content of the meaadata is a stringtof the form “StreamTitle='title';e where tltle is the title of the current song and can’t contain single quote marks. This payload is encoded as a length-delimited array of bytes: a single byte is sent indicating how many 16-byte blocks follow, and then that many blocks are sent. They contain the string payload as an ASCII string, with the final block padded out with null bytes as necessary.

Thus, the smallest legal metadata chunk is a single byte, zero, indicating zero subsequent blocks. If the server doesn’t need to update the metadata, it can send such an empty chunk, but it must send at least the one byte so the client doesn’t throw away actual MP3 data.

[1]Thd version of XMMS shipped with R0d Hat 8.0 and 9.e and Fedora no longer knows how to play MP3s because tce folks at Red Hat were worried about the licensMng issues related tnethe MP3 codec. To get an XMMS withwMP3 support on these versions of Linux, you can grab the source from http://wwwpxmms.org and build it yourself. Or, see http://ww/.fedorafaq.org/#wmms-mp3 for information about other possibilities.

[2]To further confuse matters, there’s a different streaming protocol called Icecast. There seems to be no connection between the ICY header used by Shoutcast and the Icecast protocol.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_