Sam Lantinga - Sun Nov 8 08:34:48 PST 2009
authorSam Lantinga <slouken@libsdl.org>
Sun, 08 Nov 2009 16:35:36 +0000
changeset 4705cebd6c5be2d
parent 469 5b6aac99ebf1
child 471 f56ee4fcc9d5
Sam Lantinga - Sun Nov 8 08:34:48 PST 2009
* Added Mix_Init()/Mix_Quit() to prevent constantly loading and unloading DLLs
CHANGES
SDL_mixer.h
dynamic_flac.c
dynamic_flac.h
dynamic_mod.c
dynamic_mod.h
dynamic_mp3.c
dynamic_mp3.h
dynamic_ogg.c
dynamic_ogg.h
load_flac.c
load_ogg.c
mixer.c
music.c
music_flac.c
music_mod.c
music_ogg.c
     1.1 --- a/CHANGES	Sun Nov 08 15:06:43 2009 +0000
     1.2 +++ b/CHANGES	Sun Nov 08 16:35:36 2009 +0000
     1.3 @@ -1,4 +1,6 @@
     1.4  1.2.10:
     1.5 +Sam Lantinga - Sun Nov  8 08:34:48 PST 2009
     1.6 + * Added Mix_Init()/Mix_Quit() to prevent constantly loading and unloading DLLs
     1.7  Mike Frysinger - 2009-11-05 09:11:43 PST
     1.8   * Check for fork/vfork on any platform, don't just assume it on UNIX
     1.9  Jon Atkins - Thu Nov 5 00:02:50 2009 UTC
     2.1 --- a/SDL_mixer.h	Sun Nov 08 15:06:43 2009 +0000
     2.2 +++ b/SDL_mixer.h	Sun Nov 08 16:35:36 2009 +0000
     2.3 @@ -65,6 +65,23 @@
     2.4   */
     2.5  extern DECLSPEC const SDL_version * SDLCALL Mix_Linked_Version(void);
     2.6  
     2.7 +typedef enum
     2.8 +{
     2.9 +    MIX_INIT_FLAC = 0x00000001,
    2.10 +    MIX_INIT_MOD  = 0x00000002,
    2.11 +    MIX_INIT_MP3  = 0x00000004,
    2.12 +    MIX_INIT_OGG  = 0x00000008
    2.13 +} MIX_InitFlags;
    2.14 +
    2.15 +/* Loads dynamic libraries and prepares them for use.  Flags should be
    2.16 +   one or more flags from MIX_InitFlags OR'd together.
    2.17 +   It returns the flags successfully initialized, or 0 on failure.
    2.18 + */
    2.19 +extern DECLSPEC int SDLCALL Mix_Init(int flags);
    2.20 +
    2.21 +/* Unloads libraries loaded with Mix_Init */
    2.22 +extern DECLSPEC void SDLCALL Mix_Quit();
    2.23 +
    2.24  
    2.25  /* The default mixer has 8 simultaneous mixing channels */
    2.26  #ifndef MIX_CHANNELS
     3.1 --- a/dynamic_flac.c	Sun Nov 08 15:06:43 2009 +0000
     3.2 +++ b/dynamic_flac.c	Sun Nov 08 16:35:36 2009 +0000
     3.3 @@ -35,8 +35,8 @@
     3.4  };
     3.5  
     3.6  #ifdef FLAC_DYNAMIC
     3.7 -
     3.8 -int Mix_InitFLAC() {
     3.9 +int Mix_InitFLAC()
    3.10 +{
    3.11  	if ( flac.loaded == 0 ) {
    3.12  		flac.handle = SDL_LoadObject(FLAC_DYNAMIC);
    3.13  		if ( flac.handle == NULL ) {
    3.14 @@ -130,7 +130,8 @@
    3.15  
    3.16  	return 0;
    3.17  }
    3.18 -void Mix_QuitFLAC() {
    3.19 +void Mix_QuitFLAC()
    3.20 +{
    3.21  	if ( flac.loaded == 0 ) {
    3.22  		return;
    3.23  	}
    3.24 @@ -140,7 +141,8 @@
    3.25  	--flac.loaded;
    3.26  }
    3.27  #else
    3.28 -int Mix_InitFLAC() {
    3.29 +int Mix_InitFLAC()
    3.30 +{
    3.31  	if ( flac.loaded == 0 ) {
    3.32  		flac.FLAC__stream_decoder_new = FLAC__stream_decoder_new;
    3.33  		flac.FLAC__stream_decoder_delete = FLAC__stream_decoder_delete;
    3.34 @@ -163,7 +165,8 @@
    3.35  
    3.36  	return 0;
    3.37  }
    3.38 -void Mix_QuitFLAC() {
    3.39 +void Mix_QuitFLAC()
    3.40 +{
    3.41  	if ( flac.loaded == 0 ) {
    3.42  		return;
    3.43  	}
    3.44 @@ -171,6 +174,18 @@
    3.45  	}
    3.46  	--flac.loaded;
    3.47  }
    3.48 -#endif // FLAC_DYNAMIC
    3.49 +#endif /* FLAC_DYNAMIC */
    3.50  
    3.51 -#endif // FLAC_MUSIC
    3.52 +#else
    3.53 +
    3.54 +int Mix_InitFLAC()
    3.55 +{
    3.56 +	Mix_SetError("FLAC audio is not supported");
    3.57 +	return -1;
    3.58 +}
    3.59 +
    3.60 +void Mix_QuitFLAC()
    3.61 +{
    3.62 +}
    3.63 +
    3.64 +#endif /* FLAC_MUSIC */
     4.1 --- a/dynamic_flac.h	Sun Nov 08 15:06:43 2009 +0000
     4.2 +++ b/dynamic_flac.h	Sun Nov 08 16:35:36 2009 +0000
     4.3 @@ -62,7 +62,7 @@
     4.4  
     4.5  extern flac_loader flac;
     4.6  
     4.7 +#endif /* FLAC_MUSIC */
     4.8 +
     4.9  extern int Mix_InitFLAC();
    4.10  extern void Mix_QuitFLAC();
    4.11 -
    4.12 -#endif // FLAC_MUSIC
     5.1 --- a/dynamic_mod.c	Sun Nov 08 15:06:43 2009 +0000
     5.2 +++ b/dynamic_mod.c	Sun Nov 08 16:35:36 2009 +0000
     5.3 @@ -273,4 +273,16 @@
     5.4  }
     5.5  #endif /* MOD_DYNAMIC */
     5.6  
     5.7 +#else
     5.8 +
     5.9 +int Mix_InitMOD()
    5.10 +{
    5.11 +	Mix_SetError("MOD audio is not supported");
    5.12 +	return -1;
    5.13 +}
    5.14 +
    5.15 +void Mix_QuitMOD()
    5.16 +{
    5.17 +}
    5.18 +
    5.19  #endif /* MOD_MUSIC */
     6.1 --- a/dynamic_mod.h	Sun Nov 08 15:06:43 2009 +0000
     6.2 +++ b/dynamic_mod.h	Sun Nov 08 16:35:36 2009 +0000
     6.3 @@ -57,7 +57,7 @@
     6.4  
     6.5  extern mikmod_loader mikmod;
     6.6  
     6.7 +#endif /* MOD_MUSIC */
     6.8 +
     6.9  extern int Mix_InitMOD();
    6.10  extern void Mix_QuitMOD();
    6.11 -
    6.12 -#endif
     7.1 --- a/dynamic_mp3.c	Sun Nov 08 15:06:43 2009 +0000
     7.2 +++ b/dynamic_mp3.c	Sun Nov 08 16:35:36 2009 +0000
     7.3 @@ -177,4 +177,16 @@
     7.4  }
     7.5  #endif /* MP3_DYNAMIC */
     7.6  
     7.7 +#else
     7.8 +
     7.9 +int Mix_InitMP3()
    7.10 +{
    7.11 +	Mix_SetError("MP3 audio is not supported");
    7.12 +	return -1;
    7.13 +}
    7.14 +
    7.15 +void Mix_QuitMP3()
    7.16 +{
    7.17 +}
    7.18 +
    7.19  #endif /* MP3_MUSIC */
     8.1 --- a/dynamic_mp3.h	Sun Nov 08 15:06:43 2009 +0000
     8.2 +++ b/dynamic_mp3.h	Sun Nov 08 16:35:36 2009 +0000
     8.3 @@ -43,7 +43,7 @@
     8.4  
     8.5  extern smpeg_loader smpeg;
     8.6  
     8.7 +#endif /* MUSIC_MP3 */
     8.8 +
     8.9  extern int Mix_InitMP3();
    8.10  extern void Mix_QuitMP3();
    8.11 -
    8.12 -#endif
     9.1 --- a/dynamic_ogg.c	Sun Nov 08 15:06:43 2009 +0000
     9.2 +++ b/dynamic_ogg.c	Sun Nov 08 16:35:36 2009 +0000
     9.3 @@ -125,4 +125,16 @@
     9.4  }
     9.5  #endif /* OGG_DYNAMIC */
     9.6  
     9.7 +#else
     9.8 +
     9.9 +int Mix_InitOgg()
    9.10 +{
    9.11 +	Mix_SetError("Ogg Vorbis audio is not supported");
    9.12 +	return -1;
    9.13 +}
    9.14 +
    9.15 +void Mix_QuitOgg()
    9.16 +{
    9.17 +}
    9.18 +
    9.19  #endif /* OGG_MUSIC */
    10.1 --- a/dynamic_ogg.h	Sun Nov 08 15:06:43 2009 +0000
    10.2 +++ b/dynamic_ogg.h	Sun Nov 08 16:35:36 2009 +0000
    10.3 @@ -44,7 +44,7 @@
    10.4  
    10.5  extern vorbis_loader vorbis;
    10.6  
    10.7 +#endif /* OGG_MUSIC */
    10.8 +
    10.9  extern int Mix_InitOgg();
   10.10  extern void Mix_QuitOgg();
   10.11 -
   10.12 -#endif
    11.1 --- a/load_flac.c	Sun Nov 08 15:06:43 2009 +0000
    11.2 +++ b/load_flac.c	Sun Nov 08 16:35:36 2009 +0000
    11.3 @@ -208,7 +208,8 @@
    11.4  static void flac_metadata_load_cb(
    11.5  					const FLAC__StreamDecoder *decoder,
    11.6  					const FLAC__StreamMetadata *metadata,
    11.7 -					void *client_data) {
    11.8 +					void *client_data)
    11.9 +{
   11.10  	FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
   11.11  	FLAC__uint64 total_samples;
   11.12  	unsigned bps;
   11.13 @@ -237,7 +238,8 @@
   11.14  static void flac_error_load_cb(
   11.15  				const FLAC__StreamDecoder *decoder,
   11.16  				FLAC__StreamDecoderErrorStatus status,
   11.17 -				void *client_data) {
   11.18 +				void *client_data)
   11.19 +{
   11.20  	// print an SDL error based on the error status
   11.21  	switch (status) {
   11.22  		case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
   11.23 @@ -260,7 +262,8 @@
   11.24  
   11.25  /* don't call this directly; use Mix_LoadWAV_RW() for now. */
   11.26  SDL_AudioSpec *Mix_LoadFLAC_RW (SDL_RWops *src, int freesrc,
   11.27 -        SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len) {
   11.28 +        SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
   11.29 +{
   11.30  	FLAC__StreamDecoder *decoder = 0;
   11.31  	FLAC__StreamDecoderInitStatus init_status;
   11.32  	int was_error = 1;
   11.33 @@ -271,11 +274,11 @@
   11.34  	FLAC_SDL_Data* client_data;
   11.35  	client_data = (FLAC_SDL_Data *)malloc (sizeof (FLAC_SDL_Data));
   11.36  
   11.37 -    if ((!src) || (!audio_buf) || (!audio_len))   /* sanity checks. */
   11.38 -        goto done;
   11.39 +	if ((!src) || (!audio_buf) || (!audio_len))   /* sanity checks. */
   11.40 +		goto done;
   11.41  
   11.42 -    if (Mix_InitFLAC() < 0)
   11.43 -        goto done;
   11.44 +	if (!Mix_Init(MIX_INIT_FLAC))
   11.45 +		goto done;
   11.46  
   11.47  	if ((decoder = flac.FLAC__stream_decoder_new ()) == NULL) {
   11.48  		SDL_SetError ("Unable to allocate FLAC decoder.");
   11.49 @@ -313,25 +316,23 @@
   11.50      *audio_len &= ~(samplesize - 1);
   11.51  
   11.52  done:
   11.53 -	if(was_init && decoder) {
   11.54 +	if (was_init && decoder) {
   11.55  		flac.FLAC__stream_decoder_finish (decoder);
   11.56  	}
   11.57  
   11.58 -	if(decoder) {
   11.59 +	if (decoder) {
   11.60  		flac.FLAC__stream_decoder_delete (decoder);
   11.61  	}
   11.62  
   11.63 -    if (src) {
   11.64 -        if (freesrc)
   11.65 -            SDL_RWclose (src);
   11.66 -        else
   11.67 -            SDL_RWseek (src, 0, SEEK_SET);
   11.68 -    }
   11.69 +	if (src) {
   11.70 +		if (freesrc)
   11.71 +			SDL_RWclose (src);
   11.72 +		else
   11.73 +			SDL_RWseek (src, 0, SEEK_SET);
   11.74 +	}
   11.75  
   11.76 -    if (was_error)
   11.77 -        spec = NULL;
   11.78 -
   11.79 -    Mix_QuitFLAC ();
   11.80 +	if (was_error)
   11.81 +		spec = NULL;
   11.82  
   11.83      return spec;
   11.84  }
    12.1 --- a/load_ogg.c	Sun Nov 08 15:06:43 2009 +0000
    12.2 +++ b/load_ogg.c	Sun Nov 08 16:35:36 2009 +0000
    12.3 @@ -80,7 +80,7 @@
    12.4      if ( (!src) || (!audio_buf) || (!audio_len) )   /* sanity checks. */
    12.5          goto done;
    12.6  
    12.7 -    if ( Mix_InitOgg() < 0 )
    12.8 +    if ( !Mix_Init(MIX_INIT_OGG) )
    12.9          goto done;
   12.10  
   12.11      callbacks.read_func = sdl_read_func;
   12.12 @@ -153,8 +153,6 @@
   12.13      if ( was_error )
   12.14          spec = NULL;
   12.15  
   12.16 -    Mix_QuitOgg();
   12.17 -
   12.18      return(spec);
   12.19  } /* Mix_LoadOGG_RW */
   12.20  
    13.1 --- a/mixer.c	Sun Nov 08 15:06:43 2009 +0000
    13.2 +++ b/mixer.c	Sun Nov 08 16:35:36 2009 +0000
    13.3 @@ -35,6 +35,10 @@
    13.4  #include "load_voc.h"
    13.5  #include "load_ogg.h"
    13.6  #include "load_flac.h"
    13.7 +#include "dynamic_flac.h"
    13.8 +#include "dynamic_mod.h"
    13.9 +#include "dynamic_mp3.h"
   13.10 +#include "dynamic_ogg.h"
   13.11  
   13.12  #define __MIX_INTERNAL_EFFECT__
   13.13  #include "effects_internal.h"
   13.14 @@ -134,6 +138,54 @@
   13.15  	return(&linked_version);
   13.16  }
   13.17  
   13.18 +static int initialized = 0;
   13.19 +
   13.20 +int Mix_Init(int flags)
   13.21 +{
   13.22 +	int result = 0;
   13.23 +
   13.24 +	if ((flags & MIX_INIT_FLAC) && !(initialized & MIX_INIT_FLAC)) {
   13.25 +		if (Mix_InitFLAC() == 0) {
   13.26 +			result |= MIX_INIT_FLAC;
   13.27 +		}
   13.28 +	}
   13.29 +	if ((flags & MIX_INIT_MOD) && !(initialized & MIX_INIT_MOD)) {
   13.30 +		if (Mix_InitMOD() == 0) {
   13.31 +			result |= MIX_INIT_MOD;
   13.32 +		}
   13.33 +	}
   13.34 +	if ((flags & MIX_INIT_MP3) && !(initialized & MIX_INIT_MP3)) {
   13.35 +		if (Mix_InitMP3() == 0) {
   13.36 +			result |= MIX_INIT_MP3;
   13.37 +		}
   13.38 +	}
   13.39 +	if ((flags & MIX_INIT_OGG) && !(initialized & MIX_INIT_OGG)) {
   13.40 +		if (Mix_InitOgg() == 0) {
   13.41 +			result |= MIX_INIT_OGG;
   13.42 +		}
   13.43 +	}
   13.44 +	initialized |= result;
   13.45 +
   13.46 +	return (result);
   13.47 +}
   13.48 +
   13.49 +void Mix_Quit()
   13.50 +{
   13.51 +	if (initialized & MIX_INIT_FLAC) {
   13.52 +		Mix_QuitFLAC();
   13.53 +	}
   13.54 +	if (initialized & MIX_INIT_MOD) {
   13.55 +		Mix_QuitMOD();
   13.56 +	}
   13.57 +	if (initialized & MIX_INIT_MP3) {
   13.58 +		Mix_QuitMP3();
   13.59 +	}
   13.60 +	if (initialized & MIX_INIT_OGG) {
   13.61 +		Mix_QuitOgg();
   13.62 +	}
   13.63 +	initialized = 0;
   13.64 +}
   13.65 +
   13.66  static int _Mix_remove_all_effects(int channel, effect_info **e);
   13.67  
   13.68  /*
    14.1 --- a/music.c	Sun Nov 08 15:06:43 2009 +0000
    14.2 +++ b/music.c	Sun Nov 08 16:35:36 2009 +0000
    14.3 @@ -510,7 +510,7 @@
    14.4  	     (ext && MIX_string_equals(ext, "MPEG")) ||
    14.5  	     (magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) ||
    14.6  	     (strncmp((char *)magic, "ID3", 3) == 0) ) {
    14.7 -		if ( Mix_InitMP3() == 0 ) {
    14.8 +		if ( Mix_Init(MIX_INIT_MP3) ) {
    14.9  			SMPEG_Info info;
   14.10  			music->type = MUS_MP3;
   14.11  			music->data.mp3 = smpeg.SMPEG_new(file, &info, 0);
   14.12 @@ -621,7 +621,6 @@
   14.13  #ifdef MP3_MUSIC
   14.14  			case MUS_MP3:
   14.15  				smpeg.SMPEG_delete(music->data.mp3);
   14.16 -				Mix_QuitMP3();
   14.17  				break;
   14.18  #endif
   14.19  #ifdef MP3_MAD_MUSIC
   14.20 @@ -1297,7 +1296,7 @@
   14.21  #endif
   14.22  #ifdef MP3_MUSIC
   14.23  	if ( ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) || ( strncmp((char *)magic, "ID3", 3) == 0 ) ) {
   14.24 -		if ( Mix_InitMP3() == 0 ) {
   14.25 +		if ( Mix_Init(MIX_INIT_MP3) ) {
   14.26  			SMPEG_Info info;
   14.27  			music->type = MUS_MP3;
   14.28  			music->data.mp3 = smpeg.SMPEG_new_rwops(rw, &info, 0);
    15.1 --- a/music_flac.c	Sun Nov 08 15:06:43 2009 +0000
    15.2 +++ b/music_flac.c	Sun Nov 08 16:35:36 2009 +0000
    15.3 @@ -337,7 +337,7 @@
    15.4  		music->flac_data.data_len = 0;
    15.5  		music->flac_data.data_read = 0;
    15.6  
    15.7 -		if (Mix_InitFLAC () >= 0) {
    15.8 +		if (Mix_Init(MIX_INIT_FLAC)) {
    15.9  			init_stage++; // stage 1!
   15.10  
   15.11  			music->flac_decoder = flac.FLAC__stream_decoder_new ();
   15.12 @@ -357,8 +357,14 @@
   15.13  					if (flac.FLAC__stream_decoder_process_until_end_of_metadata
   15.14  											(music->flac_decoder)) {
   15.15  						was_error = 0;
   15.16 +					} else {
   15.17 +						SDL_SetError("FLAC__stream_decoder_process_until_end_of_metadata() failed");
   15.18  					}
   15.19 +				} else {
   15.20 +					SDL_SetError("FLAC__stream_decoder_init_stream() failed");
   15.21  				}
   15.22 +			} else {
   15.23 +				SDL_SetError("FLAC__stream_decoder_new() failed");
   15.24  			}
   15.25  		}
   15.26  
   15.27 @@ -369,16 +375,11 @@
   15.28  				case 2:
   15.29  					flac.FLAC__stream_decoder_delete( music->flac_decoder );
   15.30  				case 1:
   15.31 -					Mix_QuitFLAC();
   15.32  				case 0:
   15.33  					free(music);
   15.34  					SDL_RWclose(rw);
   15.35  					break;
   15.36  			}
   15.37 -
   15.38 -			SDL_SetError ("There was an error in stage [%d] of FLAC init.",
   15.39 -							init_stage);
   15.40 -
   15.41  			return NULL;
   15.42  		}
   15.43  	}
   15.44 @@ -559,8 +560,6 @@
   15.45  		}
   15.46  
   15.47  		free (music);
   15.48 -
   15.49 -		Mix_QuitFLAC ();
   15.50  	}
   15.51  }
   15.52  
    16.1 --- a/music_mod.c	Sun Nov 08 15:06:43 2009 +0000
    16.2 +++ b/music_mod.c	Sun Nov 08 16:35:36 2009 +0000
    16.3 @@ -53,7 +53,7 @@
    16.4  {
    16.5  	CHAR *list;
    16.6  
    16.7 -	if ( Mix_InitMOD() < 0 ) {
    16.8 +	if ( !Mix_Init(MIX_INIT_MOD) ) {
    16.9  		return -1;
   16.10  	}
   16.11  
   16.12 @@ -132,7 +132,6 @@
   16.13  void MOD_exit(void)
   16.14  {
   16.15  	mikmod.MikMod_Exit();
   16.16 -	Mix_QuitMOD();
   16.17  }
   16.18  
   16.19  /* Set the volume for a MOD stream */
    17.1 --- a/music_ogg.c	Sun Nov 08 15:06:43 2009 +0000
    17.2 +++ b/music_ogg.c	Sun Nov 08 16:35:36 2009 +0000
    17.3 @@ -105,13 +105,12 @@
    17.4  		OGG_setvolume(music, MIX_MAX_VOLUME);
    17.5  		music->section = -1;
    17.6  
    17.7 -		if ( Mix_InitOgg() < 0 ) {
    17.8 +		if ( !Mix_Init(MIX_INIT_OGG) ) {
    17.9  			return(NULL);
   17.10  		}
   17.11  		if ( vorbis.ov_open_callbacks(rw, &music->vf, NULL, 0, callbacks) < 0 ) {
   17.12  			free(music);
   17.13  			SDL_RWclose(rw);
   17.14 -			Mix_QuitOgg();
   17.15  			SDL_SetError("Not an Ogg Vorbis audio stream");
   17.16  			return(NULL);
   17.17  		}
   17.18 @@ -224,7 +223,6 @@
   17.19  		}
   17.20  		vorbis.ov_clear(&music->vf);
   17.21  		free(music);
   17.22 -		Mix_QuitOgg();
   17.23  	}
   17.24  }
   17.25