[sdlsound] MP3 streaming, RWops and decoding (fwd)

(I replied to this on the sdlsound mailing list, but in case this is
useful information to anyone here, I’m crossposting. --ryan.)---------- Forwarded message ----------
Date: Tue, 4 Jun 2002 11:33:27 -0400 (EDT)
From: @icculus (icculus)
Reply-To: “sdlsound at icculus.org
To: “sdlsound at icculus.org
Subject: Re: [sdlsound] MP3 streaming, RWops and decoding

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

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.