diff --git a/audio/drivers/oss.c b/audio/drivers/oss.c index 919bb86753..e527e4cb4e 100644 --- a/audio/drivers/oss.c +++ b/audio/drivers/oss.c @@ -42,23 +42,27 @@ #define DEFAULT_OSS_DEV "/dev/dsp" #endif -/* TODO/FIXME - static global variable */ -static bool oss_is_paused = false; +typedef struct oss_audio +{ + int fd; + bool is_paused; +} oss_audio_t; -static void *oss_init(const char *device, unsigned rate, unsigned latency, +static void *oss_init(const char *device, + unsigned rate, unsigned latency, unsigned block_frames, unsigned *new_out_rate) { int frags, frag, channels, format, new_rate; - int *fd = (int*)calloc(1, sizeof(int)); + oss_audio_t *ossaudio = (oss_audio_t*)calloc(1, sizeof(oss_audio_t)); const char *oss_device = device ? device : DEFAULT_OSS_DEV; - if (!fd) + if (!ossaudio) return NULL; - if ((*fd = open(oss_device, O_WRONLY)) < 0) + if ((ossaudio->fd = open(oss_device, O_WRONLY)) < 0) { - free(fd); + free(ossaudio); perror("open"); return NULL; } @@ -66,21 +70,21 @@ static void *oss_init(const char *device, unsigned rate, unsigned latency, frags = (latency * rate * 4) / (1000 * (1 << 10)); frag = (frags << 16) | 10; - if (ioctl(*fd, SNDCTL_DSP_SETFRAGMENT, &frag) < 0) + if (ioctl(ossaudio->fd, SNDCTL_DSP_SETFRAGMENT, &frag) < 0) RARCH_WARN("Cannot set fragment sizes. Latency might not be as expected ...\n"); channels = 2; format = is_little_endian() ? AFMT_S16_LE : AFMT_S16_BE; - if (ioctl(*fd, SNDCTL_DSP_CHANNELS, &channels) < 0) + if (ioctl(ossaudio->fd, SNDCTL_DSP_CHANNELS, &channels) < 0) goto error; - if (ioctl(*fd, SNDCTL_DSP_SETFMT, &format) < 0) + if (ioctl(ossaudio->fd, SNDCTL_DSP_SETFMT, &format) < 0) goto error; new_rate = rate; - if (ioctl(*fd, SNDCTL_DSP_SPEED, &new_rate) < 0) + if (ioctl(ossaudio->fd, SNDCTL_DSP_SPEED, &new_rate) < 0) goto error; if (new_rate != (int)rate) @@ -89,11 +93,12 @@ static void *oss_init(const char *device, unsigned rate, unsigned latency, *new_out_rate = new_rate; } - return fd; + return ossaudio; error: - close(*fd); - free(fd); + close(ossaudio->fd); + if (ossaudio) + free(ossaudio); perror("ioctl"); return NULL; } @@ -101,14 +106,14 @@ error: static ssize_t oss_write(void *data, const void *buf, size_t size) { ssize_t ret; - int *fd = (int*)data; + oss_audio_t *ossaudio = (oss_audio_t*)data; if (size == 0) return 0; - if ((ret = write(*fd, buf, size)) < 0) + if ((ret = write(ossaudio->fd, buf, size)) < 0) { - if (errno == EAGAIN && (fcntl(*fd, F_GETFL) & O_NONBLOCK)) + if (errno == EAGAIN && (fcntl(ossaudio->fd, F_GETFL) & O_NONBLOCK)) return 0; return -1; @@ -119,9 +124,9 @@ static ssize_t oss_write(void *data, const void *buf, size_t size) static bool oss_stop(void *data) { - int *fd = (int*)data; + oss_audio_t *ossaudio = (oss_audio_t*)data; - if (ioctl(*fd, SNDCTL_DSP_RESET, 0) < 0) + if (ioctl(ossaudio->fd, SNDCTL_DSP_RESET, 0) < 0) return false; oss_is_paused = true; @@ -144,33 +149,35 @@ static bool oss_alive(void *data) static void oss_set_nonblock_state(void *data, bool state) { int rc; - int *fd = (int*)data; + oss_audio_t *ossaudio = (oss_audio_t*)data; if (state) - rc = fcntl(*fd, F_SETFL, fcntl(*fd, F_GETFL) | O_NONBLOCK); + rc = fcntl(ossaudio->fd, F_SETFL, + fcntl(ossaudio->fd, F_GETFL) | O_NONBLOCK); else - rc = fcntl(*fd, F_SETFL, fcntl(*fd, F_GETFL) & (~O_NONBLOCK)); + rc = fcntl(ossaudio->fd, F_SETFL, + fcntl(ossaudio->fd, F_GETFL) & (~O_NONBLOCK)); if (rc != 0) RARCH_WARN("Could not set nonblocking on OSS file descriptor. Will not be able to fast-forward.\n"); } static void oss_free(void *data) { - int *fd = (int*)data; + oss_audio_t *ossaudio = (oss_audio_t*)data; - if (ioctl(*fd, SNDCTL_DSP_RESET, 0) < 0) + if (ioctl(ossaudio->fd, SNDCTL_DSP_RESET, 0) < 0) return; - close(*fd); - free(fd); + close(ossaudio->fd); + free(data); } static size_t oss_write_avail(void *data) { audio_buf_info info; - int *fd = (int*)data; + oss_audio_t *ossaudio = (oss_audio_t*)data; - if (ioctl(*fd, SNDCTL_DSP_GETOSPACE, &info) < 0) + if (ioctl(ossaudio->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) { RARCH_ERR("[OSS]: SNDCTL_DSP_GETOSPACE failed ...\n"); return 0; @@ -182,9 +189,9 @@ static size_t oss_write_avail(void *data) static size_t oss_buffer_size(void *data) { audio_buf_info info; - int *fd = (int*)data; + oss_audio_t *ossaudio = (oss_audio_t*)data; - if (ioctl(*fd, SNDCTL_DSP_GETOSPACE, &info) < 0) + if (ioctl(ossaudio->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) { RARCH_ERR("[OSS]: SNDCTL_DSP_GETOSPACE failed ...\n"); return 1; /* Return something non-zero to avoid SIGFPE. */