Do proper error handling when loading sound files.

This fixes the segfault grg got when trying to play a 5.1 channel file.
This commit is contained in:
Bill Currie 2010-08-08 03:02:55 +00:00 committed by Jeff Teunissen
parent 0bbb805b94
commit 46a067b2f6
7 changed files with 45 additions and 30 deletions

View File

@ -419,36 +419,41 @@ void SND_ResampleStereo (sfxbuffer_t *sc, byte *data, int length);
//@{
/** Load the referenced sound.
\param sfx sound reference
\return 0 if ok, -1 on error
*/
void SND_Load (sfx_t *sfx);
int SND_Load (sfx_t *sfx);
/** Load the referenced sound from the specified Ogg file.
\param file pre-opened Ogg file
\param sfx sound reference
\param realname path of sound file should it need to be re-opened
\return 0 if ok, -1 on error
*/
void SND_LoadOgg (QFile *file, sfx_t *sfx, char *realname);
int SND_LoadOgg (QFile *file, sfx_t *sfx, char *realname);
/** Load the referenced sound from the specified FLAC file.
\param file pre-opened FLAC file
\param sfx sound reference
\param realname path of sound file should it need to be re-opened
\return 0 if ok, -1 on error
*/
void SND_LoadFLAC (QFile *file, sfx_t *sfx, char *realname);
int SND_LoadFLAC (QFile *file, sfx_t *sfx, char *realname);
/** Load the referenced sound from the specified WAV file.
\param file pre-opened WAV file
\param sfx sound reference
\param realname path of sound file should it need to be re-opened
\return 0 if ok, -1 on error
*/
void SND_LoadWav (QFile *file, sfx_t *sfx, char *realname);
int SND_LoadWav (QFile *file, sfx_t *sfx, char *realname);
/** Load the referenced sound from the specified MIDI file.
\param file pre-opened MIDI file
\param sfx sound reference
\param realname path of sound file should it need to be re-opened
\return 0 if ok, -1 on error
*/
void SND_LoadMidi (QFile *file, sfx_t *sfx, char *realname);
int SND_LoadMidi (QFile *file, sfx_t *sfx, char *realname);
//@}
/** \defgroup sound_render_cache_stream Cache/Stream Functions.

View File

@ -516,7 +516,7 @@ get_info (flacfile_t *ff)
return info;
}
void
int
SND_LoadFLAC (QFile *file, sfx_t *sfx, char *realname)
{
flacfile_t *ff;
@ -524,13 +524,12 @@ SND_LoadFLAC (QFile *file, sfx_t *sfx, char *realname)
if (!(ff = open_flac (file))) {
Sys_Printf ("Input does not appear to be an Ogg bitstream.\n");
Qclose (file);
return;
return -1;
}
info = get_info (ff);
if (info.channels < 1 || info.channels > 2) {
Sys_Printf ("unsupported number of channels");
return;
return -1;
}
if (info.samples / info.rate < 3) {
Sys_DPrintf ("cache %s\n", realname);
@ -539,6 +538,7 @@ SND_LoadFLAC (QFile *file, sfx_t *sfx, char *realname)
Sys_DPrintf ("stream %s\n", realname);
flac_stream (sfx, realname, ff, info);
}
return 0;
}
#endif//HAVE_FLAC

View File

@ -149,7 +149,7 @@ midi_stream_open (sfx_t *sfx)
midi_stream_close);
}
void
int
SND_LoadMidi (QFile *file, sfx_t *sfx, char *realname)
{
wavinfo_t info;
@ -159,7 +159,7 @@ SND_LoadMidi (QFile *file, sfx_t *sfx, char *realname)
if (!midi_intiialized) {
if (midi_init ()) {
return;
return -1;
}
}
@ -172,7 +172,7 @@ SND_LoadMidi (QFile *file, sfx_t *sfx, char *realname)
handle = WildMidi_OpenBuffer (local_buffer, local_buffer_size);
if (handle == NULL)
return;
return -1;
info = get_info (handle);
@ -182,5 +182,6 @@ SND_LoadMidi (QFile *file, sfx_t *sfx, char *realname)
// we init stream here cause we will only ever stream
SND_SFX_Stream (sfx, realname, info, midi_stream_open);
return 0;
}
#endif // HAVE_WILDMIDI

View File

@ -315,7 +315,7 @@ SND_StreamAdvance (sfxbuffer_t *buffer, unsigned int count)
fill_buffer (sfx, stream, buffer, info, headpos);
}
void
int
SND_Load (sfx_t *sfx)
{
dstring_t *foundname = dstring_new ();
@ -332,7 +332,7 @@ SND_Load (sfx_t *sfx)
if (!file) {
Sys_Printf ("Couldn't load %s\n", sfx->name);
dstring_delete (foundname);
return;
return -1;
}
sfx->open = snd_open;
if (!strequal (foundname->str, sfx->name)) {
@ -346,32 +346,38 @@ SND_Load (sfx_t *sfx)
#ifdef HAVE_VORBIS
if (strnequal ("OggS", buf, 4)) {
Sys_DPrintf ("SND_Load: ogg file\n");
SND_LoadOgg (file, sfx, realname);
return;
if (SND_LoadOgg (file, sfx, realname) == -1)
goto bail;
return 0;
}
#endif
#ifdef HAVE_FLAC
if (strnequal ("fLaC", buf, 4)) {
Sys_DPrintf ("SND_Load: flac file\n");
SND_LoadFLAC (file, sfx, realname);
return;
if (SND_LoadFLAC (file, sfx, realname) == -1)
goto bail;
return 0;
}
#endif
#ifdef HAVE_WILDMIDI
if (strnequal ("MThd", buf, 4)) {
Sys_DPrintf ("SND_Load: midi file\n");
SND_LoadMidi (file, sfx, realname);
return;
if (SND_LoadMidi (file, sfx, realname) == -1)
goto bail;
return 0;
}
#endif
if (strnequal ("RIFF", buf, 4)) {
Sys_DPrintf ("SND_Load: wav file\n");
SND_LoadWav (file, sfx, realname);
return;
if (SND_LoadWav (file, sfx, realname) == -1)
goto bail;
return 0;
}
bail:
Qclose (file);
if (realname != sfx->name)
free (realname);
return -1;
}
sfxbuffer_t *

View File

@ -187,7 +187,10 @@ SND_LoadSound (const char *name)
sfx = &snd_sfx[snd_num_sfx++];
sfx->name = strdup (name);
sfx->owner = sfx;
SND_Load (sfx);
if (SND_Load (sfx) == -1) {
snd_num_sfx--;
return 0;
}
Hash_Add (snd_sfx_hash, sfx);
return sfx;
}

View File

@ -271,7 +271,7 @@ vorbis_stream (sfx_t *sfx, char *realname, OggVorbis_File *vf, wavinfo_t info)
SND_SFX_Stream (sfx, realname, info, vorbis_stream_open);
}
void
int
SND_LoadOgg (QFile *file, sfx_t *sfx, char *realname)
{
OggVorbis_File vf;
@ -279,14 +279,13 @@ SND_LoadOgg (QFile *file, sfx_t *sfx, char *realname)
if (ov_open_callbacks (file, &vf, 0, 0, callbacks) < 0) {
Sys_Printf ("Input does not appear to be an Ogg bitstream.\n");
Qclose (file);
free (realname);
return;
return -1;
}
info = get_info (&vf);
if (info.channels < 1 || info.channels > 2) {
Sys_Printf ("unsupported number of channels");
return;
return -1;
}
if (info.samples / info.rate < 3) {
Sys_DPrintf ("cache %s\n", realname);
@ -295,6 +294,7 @@ SND_LoadOgg (QFile *file, sfx_t *sfx, char *realname)
Sys_DPrintf ("stream %s\n", realname);
vorbis_stream (sfx, realname, &vf, info);
}
return 0;
}
#endif//HAVE_VORBIS

View File

@ -232,15 +232,14 @@ bail:
return info;
}
void
int
SND_LoadWav (QFile *file, sfx_t *sfx, char *realname)
{
wavinfo_t info;
info = get_info (file);
if (!info.rate) {
Qclose (file);
return;
return -1;
}
if (info.samples / info.rate < 3) {
@ -250,4 +249,5 @@ SND_LoadWav (QFile *file, sfx_t *sfx, char *realname)
Sys_DPrintf ("stream %s\n", realname);
wav_stream (sfx, realname, file, info);
}
return 0;
}