comparison src/audio/arts/SDL_artsaudio.c @ 3959:33c248ea75f9 SDL-1.2

Prevent arts audio target from crashing/hanging SDL if the audio hardware isn't available. Fixes Bugzilla #372.
author Ryan C. Gordon <icculus@icculus.org>
date Thu, 14 Jun 2007 14:09:25 +0000
parents d910939febfa
children 7e878cc4250a
comparison
equal deleted inserted replaced
3958:85b6fb6a5e3c 3959:33c248ea75f9
58 static arts_stream_t (*SDL_NAME(arts_play_stream))(int rate, int bits, int channels, const char *name); 58 static arts_stream_t (*SDL_NAME(arts_play_stream))(int rate, int bits, int channels, const char *name);
59 static int (*SDL_NAME(arts_stream_set))(arts_stream_t s, arts_parameter_t param, int value); 59 static int (*SDL_NAME(arts_stream_set))(arts_stream_t s, arts_parameter_t param, int value);
60 static int (*SDL_NAME(arts_stream_get))(arts_stream_t s, arts_parameter_t param); 60 static int (*SDL_NAME(arts_stream_get))(arts_stream_t s, arts_parameter_t param);
61 static int (*SDL_NAME(arts_write))(arts_stream_t s, const void *buffer, int count); 61 static int (*SDL_NAME(arts_write))(arts_stream_t s, const void *buffer, int count);
62 static void (*SDL_NAME(arts_close_stream))(arts_stream_t s); 62 static void (*SDL_NAME(arts_close_stream))(arts_stream_t s);
63 static int (*SDL_NAME(arts_suspended))(void);
64 static const char *(*SDL_NAME(arts_error_text))(int errorcode);
63 65
64 static struct { 66 static struct {
65 const char *name; 67 const char *name;
66 void **func; 68 void **func;
67 } arts_functions[] = { 69 } arts_functions[] = {
70 { "arts_play_stream", (void **)&SDL_NAME(arts_play_stream) }, 72 { "arts_play_stream", (void **)&SDL_NAME(arts_play_stream) },
71 { "arts_stream_set", (void **)&SDL_NAME(arts_stream_set) }, 73 { "arts_stream_set", (void **)&SDL_NAME(arts_stream_set) },
72 { "arts_stream_get", (void **)&SDL_NAME(arts_stream_get) }, 74 { "arts_stream_get", (void **)&SDL_NAME(arts_stream_get) },
73 { "arts_write", (void **)&SDL_NAME(arts_write) }, 75 { "arts_write", (void **)&SDL_NAME(arts_write) },
74 { "arts_close_stream", (void **)&SDL_NAME(arts_close_stream) }, 76 { "arts_close_stream", (void **)&SDL_NAME(arts_close_stream) },
77 { "arts_suspended", (void **)&SDL_NAME(arts_suspended) },
78 { "arts_error_text", (void **)&SDL_NAME(arts_error_text) },
75 }; 79 };
76 80
77 static void UnloadARTSLibrary() 81 static void UnloadARTSLibrary()
78 { 82 {
79 if ( arts_loaded ) { 83 if ( arts_loaded ) {
125 129
126 if ( LoadARTSLibrary() < 0 ) { 130 if ( LoadARTSLibrary() < 0 ) {
127 return available; 131 return available;
128 } 132 }
129 if ( SDL_NAME(arts_init)() == 0 ) { 133 if ( SDL_NAME(arts_init)() == 0 ) {
130 #define ARTS_CRASH_HACK /* Play a stream so aRts doesn't crash */ 134 if ( SDL_NAME(arts_suspended)() ) {
131 #ifdef ARTS_CRASH_HACK 135 /* Play a stream so aRts doesn't crash */
132 arts_stream_t stream2; 136 arts_stream_t stream2;
133 stream2=SDL_NAME(arts_play_stream)(44100, 16, 2, "SDL"); 137 stream2=SDL_NAME(arts_play_stream)(44100, 16, 2, "SDL");
134 SDL_NAME(arts_write)(stream2, "", 0); 138 SDL_NAME(arts_write)(stream2, "", 0);
135 SDL_NAME(arts_close_stream)(stream2); 139 SDL_NAME(arts_close_stream)(stream2);
136 #endif 140 available = 1;
137 available = 1; 141 }
138 SDL_NAME(arts_free)(); 142 SDL_NAME(arts_free)();
139 } 143 }
140 UnloadARTSLibrary(); 144 UnloadARTSLibrary();
141 145
142 return available; 146 return available;
253 257
254 static int ARTS_OpenAudio(_THIS, SDL_AudioSpec *spec) 258 static int ARTS_OpenAudio(_THIS, SDL_AudioSpec *spec)
255 { 259 {
256 int bits, frag_spec; 260 int bits, frag_spec;
257 Uint16 test_format, format; 261 Uint16 test_format, format;
262 int error_code;
258 263
259 /* Reset the timer synchronization flag */ 264 /* Reset the timer synchronization flag */
260 frame_ticks = 0.0; 265 frame_ticks = 0.0;
261 266
262 mixbuf = NULL; 267 mixbuf = NULL;
290 SDL_SetError("Couldn't find any hardware audio formats"); 295 SDL_SetError("Couldn't find any hardware audio formats");
291 return(-1); 296 return(-1);
292 } 297 }
293 spec->format = test_format; 298 spec->format = test_format;
294 299
295 if ( SDL_NAME(arts_init)() != 0 ) { 300 error_code = SDL_NAME(arts_init)();
296 SDL_SetError("Unable to initialize ARTS"); 301 if ( error_code != 0 ) {
302 SDL_SetError("Unable to initialize ARTS: %s", SDL_NAME(arts_error_text)(error_code));
303 return(-1);
304 }
305 if ( ! SDL_NAME(arts_suspended)() ) {
306 SDL_SetError("ARTS can not open audio device");
297 return(-1); 307 return(-1);
298 } 308 }
299 stream = SDL_NAME(arts_play_stream)(spec->freq, bits, spec->channels, "SDL"); 309 stream = SDL_NAME(arts_play_stream)(spec->freq, bits, spec->channels, "SDL");
300 310
301 /* Calculate the final parameters for this audio specification */ 311 /* Calculate the final parameters for this audio specification */