[sdlsound] MP3 streaming, RWops and decoding

Ryan C. Gordon icculus at clutteredmind.org
Tue Jun 4 11:33:27 EDT 2002

> So far I setup an RWops handle to a local buffer that I will stream the
> mp3 to. Now my question is in the audio_callback function when the sound
> card needs more decoded data to process, how do I get that data into the
> sample?

I assume you're creating the SDL_RWops with SDL_RWFromMem().

Doing so will require the entire MP3 to be in memory first, since this is
a static memory buffer (once you read to the end of the buffer, it assumes
an "EOF" without a way to refill and start over.)

What you need to do is write your own RWops. Basically this means filling
in a read, write, seek, open and close method. Generally, these will just
be a couple of lines each.

For example, if you have an HTTP connection, and you stream new data in
with a function called read_more_from_the_web(), your RWops read method
might look like:

int http_read(SDL_RWops *context, void *ptr, int size, int maxnum)
    int bytes_read = read_more_from_the_web(my_http_handle, ptr, size *maxnum);

...every time playsound.c's audio callback needs more data, it will call
SDL_RWread(), which will call your http_read() function.

The best example of writing a RWops driver is the SDL source code itself:

Look at SDL_RWFromFP() to see how a RWops gets created, and emulate that.
Your seek and write methods can just return (-1) (since you can't
generally seek or write in an http stream).

We should probably do this ourselves for playsound, since it'd be neat to
stream from a webserver, but I'm pretty busy right now.


More information about the sdlsound mailing list