Skip to content
Eric Richardson edited this page Jul 6, 2013 · 1 revision

StreamMachine's big idea is keeping a big buffer of audio data in memory, allowing clients to connect to "nearly-live" radio. Because audio data is relatively small, a station can easily keep hours of audio in memory. For instance, a 64k MP3 stream will store 8 hours of audio data in just a little over 200MB of memory.

Using the Rewind Buffer, players can add support for concepts like "Play the current program from its start", or "Play the 9am broadcast." Unlike podcasts, these functions are available immediately and keep the user connected to the station's live stream.

Implementation

The RewindBuffer maintains an array of audio chunks, along with accompanying timestamp and metadata. When the array reaches the max length set in the stream's configuration, RewindBuffer starts shifting chunks off the beginning of the array as it pushes new chunks onto the back.

Listeners all get an offset from the end of the array. Each time a new chunk is pushed onto the array, each listener gets sent the chunk at their offset. While the overall size of the array is always changing (at least until the buffer fills up), the fact that each chunk is approximately the same duration means that the listener's requested offset is always at the same index in the array.

Rewinder

Each listener gets a Rewinder instance, which implements a Readable Stream interface for outputs to use. Internally, the Rewinder registers its offset with the RewindBuffer, which then starts feeding it audio chunks via the _insert function. The Rewinder queues up audio that has been sent from the RewindBuffer but hasn't yet been sent to the user.