Skip to content

Commit

Permalink
Make sure the audio callback clears the stream before passing it to t…
Browse files Browse the repository at this point in the history
…he app.
  • Loading branch information
icculus committed Mar 1, 2019
1 parent 5bd5570 commit 9ac2fc3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
56 changes: 56 additions & 0 deletions src/SDL12_compat.c
Expand Up @@ -3638,4 +3638,60 @@ SDL_LoadWAV_RW(SDL12_RWops *rwops12, int freerwops12,
return retval;
}

typedef struct
{
void (SDLCALL *app_callback)(void *userdata, Uint8 *stream, int len);
void *app_userdata;
Uint8 silence;
} AudioCallbackWrapperData;

static void SDLCALL
AudioCallbackWrapper(void *userdata, Uint8 *stream, int len)
{
AudioCallbackWrapperData *data = (AudioCallbackWrapperData *) userdata;
SDL_memset(stream, data->silence, len); // SDL2 doesn't clear the stream before calling in here, but 1.2 expects it.
data->app_callback(data->app_userdata, stream, len);
}


DECLSPEC int SDLCALL
SDL_OpenAudio(SDL_AudioSpec *want, SDL_AudioSpec *obtained)
{
// SDL2 uses a NULL callback to mean "we play to use SDL_QueueAudio()"
if (want && (want->callback == NULL)) {
return SDL20_SetError("Callback can't be NULL");
}

AudioCallbackWrapperData *data = (AudioCallbackWrapperData *) SDL_calloc(1, sizeof (AudioCallbackWrapperData));
if (!data) {
return SDL20_SetError("Out of memory");
}
data->app_callback = want->callback;
data->app_userdata = want->userdata;
want->callback = AudioCallbackWrapper;
want->userdata = data;

FIXME("Don't allow int32 or float32");
FIXME("clamp output to mono/stereo");
const int retval = SDL20_OpenAudio(want, obtained);
want->callback = data->app_callback;
want->userdata = data->app_userdata;
if (retval == -1) {
SDL_free(data);
} else {
FIXME("memory leak on callback data");
if (!obtained) {
data->silence = want->silence;
} else {
data->silence = obtained->silence;
obtained->callback = data->app_callback;
obtained->userdata = data->app_userdata;
}
}

return retval;
}



/* vi: set ts=4 sw=4 expandtab: */
2 changes: 1 addition & 1 deletion src/SDL20_syms.h
Expand Up @@ -160,7 +160,7 @@ SDL20_SYM_PASSTHROUGH(int,CondWait,(SDL_cond *a, SDL_mutex *b),(a,b),return)
SDL20_SYM_PASSTHROUGH(int,CondWaitTimeout,(SDL_cond *a, SDL_mutex *b, Uint32 c),(a,b,c),return)

SDL20_SYM(SDL_AudioSpec *,LoadWAV_RW,(SDL_RWops *a, int b, SDL_AudioSpec *c, Uint8 **d, Uint32 *e),(a,b,c,d,e),return)
SDL20_SYM_PASSTHROUGH(int,OpenAudio,(SDL_AudioSpec *a, SDL_AudioSpec *b),(a,b),return)
SDL20_SYM(int,OpenAudio,(SDL_AudioSpec *a, SDL_AudioSpec *b),(a,b),return)
SDL20_SYM_PASSTHROUGH(SDL_AudioStatus,GetAudioStatus,(void),(),return)
SDL20_SYM_PASSTHROUGH(void,PauseAudio,(int a),(a),)
SDL20_SYM_PASSTHROUGH(void,FreeWAV,(Uint8 *a),(a),)
Expand Down

0 comments on commit 9ac2fc3

Please sign in to comment.