Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Changed audio subsystem's OpenDevice interface to return -1 on error.
Browse files Browse the repository at this point in the history
This lets us 'return SDL_SetError("whatever");' on one line.

 Fixes Bugzilla #1778.
  • Loading branch information
icculus committed Mar 31, 2013
1 parent 8163efd commit 2f7d3cc
Show file tree
Hide file tree
Showing 21 changed files with 169 additions and 295 deletions.
4 changes: 2 additions & 2 deletions src/audio/SDL_audio.c
Expand Up @@ -191,7 +191,7 @@ SDL_AudioDeinitialize_Default(void)
static int
SDL_AudioOpenDevice_Default(_THIS, const char *devname, int iscapture)
{
return 0;
return -1;
}

static void
Expand Down Expand Up @@ -940,7 +940,7 @@ open_audio_device(const char *devname, int iscapture,
((!iscapture) && (current_audio.outputDevices == NULL)) )
SDL_GetNumAudioDevices(iscapture);

if (!current_audio.impl.OpenDevice(device, devname, iscapture)) {
if (current_audio.impl.OpenDevice(device, devname, iscapture) < 0) {
close_audio_device(device);
return 0;
}
Expand Down
55 changes: 21 additions & 34 deletions src/audio/alsa/SDL_alsa_audio.c
Expand Up @@ -482,8 +482,7 @@ ALSA_OpenDevice(_THIS, const char *devname, int iscapture)
this->hidden = (struct SDL_PrivateAudioData *)
SDL_malloc((sizeof *this->hidden));
if (this->hidden == NULL) {
SDL_OutOfMemory();
return 0;
return SDL_OutOfMemory();
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));

Expand All @@ -495,9 +494,8 @@ ALSA_OpenDevice(_THIS, const char *devname, int iscapture)

if (status < 0) {
ALSA_CloseDevice(this);
SDL_SetError("ALSA: Couldn't open audio device: %s",
ALSA_snd_strerror(status));
return 0;
return SDL_SetError("ALSA: Couldn't open audio device: %s",
ALSA_snd_strerror(status));
}

this->hidden->pcm_handle = pcm_handle;
Expand All @@ -507,19 +505,17 @@ ALSA_OpenDevice(_THIS, const char *devname, int iscapture)
status = ALSA_snd_pcm_hw_params_any(pcm_handle, hwparams);
if (status < 0) {
ALSA_CloseDevice(this);
SDL_SetError("ALSA: Couldn't get hardware config: %s",
ALSA_snd_strerror(status));
return 0;
return SDL_SetError("ALSA: Couldn't get hardware config: %s",
ALSA_snd_strerror(status));
}

/* SDL only uses interleaved sample output */
status = ALSA_snd_pcm_hw_params_set_access(pcm_handle, hwparams,
SND_PCM_ACCESS_RW_INTERLEAVED);
if (status < 0) {
ALSA_CloseDevice(this);
SDL_SetError("ALSA: Couldn't set interleaved access: %s",
return SDL_SetError("ALSA: Couldn't set interleaved access: %s",
ALSA_snd_strerror(status));
return 0;
}

/* Try for a closest match on audio format */
Expand Down Expand Up @@ -572,8 +568,7 @@ ALSA_OpenDevice(_THIS, const char *devname, int iscapture)
}
if (status < 0) {
ALSA_CloseDevice(this);
SDL_SetError("ALSA: Couldn't find any hardware audio formats");
return 0;
return SDL_SetError("ALSA: Couldn't find any hardware audio formats");
}
this->spec.format = test_format;

Expand All @@ -585,8 +580,7 @@ ALSA_OpenDevice(_THIS, const char *devname, int iscapture)
status = ALSA_snd_pcm_hw_params_get_channels(hwparams, &channels);
if (status < 0) {
ALSA_CloseDevice(this);
SDL_SetError("ALSA: Couldn't set audio channels");
return 0;
return SDL_SetError("ALSA: Couldn't set audio channels");
}
this->spec.channels = channels;
}
Expand All @@ -597,9 +591,8 @@ ALSA_OpenDevice(_THIS, const char *devname, int iscapture)
&rate, NULL);
if (status < 0) {
ALSA_CloseDevice(this);
SDL_SetError("ALSA: Couldn't set audio frequency: %s",
ALSA_snd_strerror(status));
return 0;
return SDL_SetError("ALSA: Couldn't set audio frequency: %s",
ALSA_snd_strerror(status));
}
this->spec.freq = rate;

Expand All @@ -609,40 +602,35 @@ ALSA_OpenDevice(_THIS, const char *devname, int iscapture)
/* Failed to set desired buffer size, do the best you can... */
if ( ALSA_set_period_size(this, hwparams, 1) < 0 ) {
ALSA_CloseDevice(this);
SDL_SetError("Couldn't set hardware audio parameters: %s", ALSA_snd_strerror(status));
return(-1);
return SDL_SetError("Couldn't set hardware audio parameters: %s", ALSA_snd_strerror(status));
}
}
/* Set the software parameters */
snd_pcm_sw_params_alloca(&swparams);
status = ALSA_snd_pcm_sw_params_current(pcm_handle, swparams);
if (status < 0) {
ALSA_CloseDevice(this);
SDL_SetError("ALSA: Couldn't get software config: %s",
ALSA_snd_strerror(status));
return 0;
return SDL_SetError("ALSA: Couldn't get software config: %s",
ALSA_snd_strerror(status));
}
status = ALSA_snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, this->spec.samples);
if (status < 0) {
ALSA_CloseDevice(this);
SDL_SetError("Couldn't set minimum available samples: %s",
ALSA_snd_strerror(status));
return 0;
return SDL_SetError("Couldn't set minimum available samples: %s",
ALSA_snd_strerror(status));
}
status =
ALSA_snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, 1);
if (status < 0) {
ALSA_CloseDevice(this);
SDL_SetError("ALSA: Couldn't set start threshold: %s",
ALSA_snd_strerror(status));
return 0;
return SDL_SetError("ALSA: Couldn't set start threshold: %s",
ALSA_snd_strerror(status));
}
status = ALSA_snd_pcm_sw_params(pcm_handle, swparams);
if (status < 0) {
ALSA_CloseDevice(this);
SDL_SetError("Couldn't set software audio parameters: %s",
ALSA_snd_strerror(status));
return 0;
return SDL_SetError("Couldn't set software audio parameters: %s",
ALSA_snd_strerror(status));
}

/* Calculate the final parameters for this audio specification */
Expand All @@ -653,16 +641,15 @@ ALSA_OpenDevice(_THIS, const char *devname, int iscapture)
this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
if (this->hidden->mixbuf == NULL) {
ALSA_CloseDevice(this);
SDL_OutOfMemory();
return 0;
return SDL_OutOfMemory();
}
SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);

/* Switch to blocking mode for playback */
ALSA_snd_pcm_nonblock(pcm_handle, 0);

/* We're ready to rock and roll. :-) */
return 1;
return 0;
}

static void
Expand Down
17 changes: 6 additions & 11 deletions src/audio/android/SDL_androidaudio.c
Expand Up @@ -42,21 +42,18 @@ AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture)

if (iscapture) {
//TODO: implement capture
SDL_SetError("Capture not supported on Android");
return 0;
return SDL_SetError("Capture not supported on Android");
}

if (audioDevice != NULL) {
SDL_SetError("Only one audio device at a time please!");
return 0;
return SDL_SetError("Only one audio device at a time please!");
}

audioDevice = this;

this->hidden = SDL_malloc(sizeof(*(this->hidden)));
if (!this->hidden) {
SDL_OutOfMemory();
return 0;
return SDL_OutOfMemory();
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));

Expand All @@ -71,8 +68,7 @@ AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture)

if (test_format == 0) {
// Didn't find a compatible format :(
SDL_SetError("No compatible audio format!");
return 0;
return SDL_SetError("No compatible audio format!");
}

if (this->spec.channels > 1) {
Expand All @@ -94,11 +90,10 @@ AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture)

if (this->spec.samples == 0) {
// Init failed?
SDL_SetError("Java-side initialization failed!");
return 0;
return SDL_SetError("Java-side initialization failed!");
}

return 1;
return 0;
}

static void
Expand Down
22 changes: 8 additions & 14 deletions src/audio/arts/SDL_artsaudio.c
Expand Up @@ -241,8 +241,7 @@ ARTS_OpenDevice(_THIS, const char *devname, int iscapture)
this->hidden = (struct SDL_PrivateAudioData *)
SDL_malloc((sizeof *this->hidden));
if (this->hidden == NULL) {
SDL_OutOfMemory();
return 0;
return SDL_OutOfMemory();
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));

Expand Down Expand Up @@ -271,22 +270,19 @@ ARTS_OpenDevice(_THIS, const char *devname, int iscapture)
}
if (format == 0) {
ARTS_CloseDevice(this);
SDL_SetError("Couldn't find any hardware audio formats");
return 0;
return SDL_SetError("Couldn't find any hardware audio formats");
}
this->spec.format = test_format;

if ((rc = SDL_NAME(arts_init) ()) != 0) {
ARTS_CloseDevice(this);
SDL_SetError("Unable to initialize ARTS: %s",
SDL_NAME(arts_error_text) (rc));
return 0;
return SDL_SetError("Unable to initialize ARTS: %s",
SDL_NAME(arts_error_text) (rc));
}

if (!ARTS_Suspend()) {
ARTS_CloseDevice(this);
SDL_SetError("ARTS can not open audio device");
return 0;
return SDL_SetError("ARTS can not open audio device");
}

this->hidden->stream = SDL_NAME(arts_play_stream) (this->spec.freq,
Expand All @@ -304,8 +300,7 @@ ARTS_OpenDevice(_THIS, const char *devname, int iscapture)
for (frag_spec = 0; (0x01 << frag_spec) < this->spec.size; ++frag_spec);
if ((0x01 << frag_spec) != this->spec.size) {
ARTS_CloseDevice(this);
SDL_SetError("Fragment size must be a power of two");
return 0;
return SDL_SetError("Fragment size must be a power of two");
}
frag_spec |= 0x00020000; /* two fragments, for low latency */

Expand All @@ -326,16 +321,15 @@ ARTS_OpenDevice(_THIS, const char *devname, int iscapture)
this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
if (this->hidden->mixbuf == NULL) {
ARTS_CloseDevice(this);
SDL_OutOfMemory();
return 0;
return SDL_OutOfMemory();
}
SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);

/* Get the parent process id (we're the parent of the audio thread) */
this->hidden->parent = getpid();

/* We're ready to rock and roll. :-) */
return 1;
return 0;
}


Expand Down
11 changes: 4 additions & 7 deletions src/audio/baudio/SDL_beaudio.cc
Expand Up @@ -95,8 +95,7 @@ BEOSAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
/* Initialize all variables that we clean on shutdown */
_this->hidden = new SDL_PrivateAudioData;
if (_this->hidden == NULL) {
SDL_OutOfMemory();
return 0;
return SDL_OutOfMemory();
}
SDL_memset(_this->hidden, 0, (sizeof *_this->hidden));

Expand Down Expand Up @@ -153,8 +152,7 @@ BEOSAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)

if (!valid_datatype) { /* shouldn't happen, but just in case... */
BEOSAUDIO_CloseDevice(_this);
SDL_SetError("Unsupported audio format");
return 0;
return SDL_SetError("Unsupported audio format");
}

/* Calculate the final parameters for this audio specification */
Expand All @@ -173,12 +171,11 @@ BEOSAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
_this->hidden->audio_obj->SetHasData(true);
} else {
BEOSAUDIO_CloseDevice(_this);
SDL_SetError("Unable to start Be audio");
return 0;
return SDL_SetError("Unable to start Be audio");
}

/* We're running! */
return 1;
return 0;
}

static void
Expand Down
20 changes: 7 additions & 13 deletions src/audio/bsd/SDL_bsdaudio.c
Expand Up @@ -239,25 +239,22 @@ BSDAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
if (devname == NULL) {
devname = SDL_GetAudioDeviceName(0, iscapture);
if (devname == NULL) {
SDL_SetError("No such audio device");
return 0;
return SDL_SetError("No such audio device");
}
}

/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
SDL_malloc((sizeof *this->hidden));
if (this->hidden == NULL) {
SDL_OutOfMemory();
return 0;
return SDL_OutOfMemory();
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));

/* Open the audio device */
this->hidden->audio_fd = open(devname, flags, 0);
if (this->hidden->audio_fd < 0) {
SDL_SetError("Couldn't open %s: %s", devname, strerror(errno));
return 0;
return SDL_SetError("Couldn't open %s: %s", devname, strerror(errno));
}

AUDIO_INITINFO(&info);
Expand All @@ -269,8 +266,7 @@ BSDAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
info.mode = AUMODE_PLAY;
if (ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info) < 0) {
BSDAUDIO_CloseDevice(this);
SDL_SetError("Couldn't put device into play mode");
return 0;
return SDL_SetError("Couldn't put device into play mode");
}

AUDIO_INITINFO(&info);
Expand Down Expand Up @@ -312,8 +308,7 @@ BSDAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)

if (!format) {
BSDAUDIO_CloseDevice(this);
SDL_SetError("No supported encoding for 0x%x", this->spec.format);
return 0;
return SDL_SetError("No supported encoding for 0x%x", this->spec.format);
}

this->spec.format = format;
Expand All @@ -336,15 +331,14 @@ BSDAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
if (this->hidden->mixbuf == NULL) {
BSDAUDIO_CloseDevice(this);
SDL_OutOfMemory();
return 0;
return SDL_OutOfMemory();
}
SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);

BSDAUDIO_Status(this);

/* We're ready to rock and roll. :-) */
return (0);
return 0;
}

static int
Expand Down

0 comments on commit 2f7d3cc

Please sign in to comment.