RetroArch/audio/drivers/oss.c

231 lines
5.2 KiB
C
Raw Permalink Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 12:40:32 +00:00
* Copyright (C) 2011-2017 - Daniel De Matteis
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2010-05-28 16:21:33 +00:00
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2010-05-28 16:21:33 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2010-05-28 16:21:33 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
2010-05-28 12:33:18 +00:00
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
2011-06-16 21:20:12 +00:00
#ifdef HAVE_OSS_BSD
#include <soundcard.h>
#else
2010-05-28 12:33:18 +00:00
#include <sys/soundcard.h>
2011-06-16 21:20:12 +00:00
#endif
#include <retro_endianness.h>
#ifdef HAVE_CONFIG_H
2021-08-18 18:19:48 +00:00
#include "../../config.h"
#endif
#include "../audio_driver.h"
2015-11-23 11:03:38 +00:00
#include "../../verbosity.h"
2010-05-28 12:33:18 +00:00
2011-06-16 21:20:12 +00:00
#ifdef HAVE_OSS_BSD
#define DEFAULT_OSS_DEV "/dev/audio"
#else
#define DEFAULT_OSS_DEV "/dev/dsp"
#endif
2020-09-20 21:30:19 +00:00
typedef struct oss_audio
{
int fd;
bool is_paused;
} oss_audio_t;
2020-09-20 21:30:19 +00:00
static void *oss_init(const char *device,
unsigned rate, unsigned latency,
unsigned block_frames,
unsigned *new_out_rate)
2010-05-28 12:33:18 +00:00
{
2015-01-12 04:05:56 +00:00
int frags, frag, channels, format, new_rate;
2020-09-20 21:30:19 +00:00
oss_audio_t *ossaudio = (oss_audio_t*)calloc(1, sizeof(oss_audio_t));
2015-06-26 15:03:05 +00:00
const char *oss_device = device ? device : DEFAULT_OSS_DEV;
2020-09-20 21:30:19 +00:00
if (!ossaudio)
2010-05-28 12:33:18 +00:00
return NULL;
2020-09-20 21:30:19 +00:00
if ((ossaudio->fd = open(oss_device, O_WRONLY)) < 0)
2010-05-28 12:33:18 +00:00
{
2020-09-20 21:30:19 +00:00
free(ossaudio);
2012-02-23 22:53:18 +00:00
perror("open");
2010-05-28 12:33:18 +00:00
return NULL;
}
2015-01-12 04:05:56 +00:00
frags = (latency * rate * 4) / (1000 * (1 << 10));
2016-09-08 09:41:58 +00:00
frag = (frags << 16) | 10;
2010-05-28 12:33:18 +00:00
2020-09-20 21:30:19 +00:00
if (ioctl(ossaudio->fd, SNDCTL_DSP_SETFRAGMENT, &frag) < 0)
2012-04-21 21:25:32 +00:00
RARCH_WARN("Cannot set fragment sizes. Latency might not be as expected ...\n");
2010-05-28 12:33:18 +00:00
2015-01-12 04:05:56 +00:00
channels = 2;
2016-09-08 09:41:58 +00:00
format = is_little_endian() ? AFMT_S16_LE : AFMT_S16_BE;
2010-05-28 12:33:18 +00:00
2020-09-20 21:30:19 +00:00
if (ioctl(ossaudio->fd, SNDCTL_DSP_CHANNELS, &channels) < 0)
2016-09-08 09:41:58 +00:00
goto error;
2010-05-28 12:33:18 +00:00
2020-09-20 21:30:19 +00:00
if (ioctl(ossaudio->fd, SNDCTL_DSP_SETFMT, &format) < 0)
2016-09-08 09:41:58 +00:00
goto error;
2010-05-28 12:33:18 +00:00
2015-01-12 04:05:56 +00:00
new_rate = rate;
2020-09-20 21:30:19 +00:00
if (ioctl(ossaudio->fd, SNDCTL_DSP_SPEED, &new_rate) < 0)
2016-09-08 09:41:58 +00:00
goto error;
2010-05-28 12:33:18 +00:00
if (new_rate != (int)rate)
{
2012-04-21 21:25:32 +00:00
RARCH_WARN("Requested sample rate not supported. Adjusting output rate to %d Hz.\n", new_rate);
*new_out_rate = new_rate;
}
2020-09-20 21:30:19 +00:00
return ossaudio;
2016-09-08 09:41:58 +00:00
error:
2020-09-20 21:30:19 +00:00
close(ossaudio->fd);
if (ossaudio)
free(ossaudio);
2016-09-08 09:41:58 +00:00
perror("ioctl");
return NULL;
2010-05-28 12:33:18 +00:00
}
static ssize_t oss_write(void *data, const void *buf, size_t size)
2010-05-28 12:33:18 +00:00
{
2015-01-12 04:05:56 +00:00
ssize_t ret;
2020-09-20 21:30:19 +00:00
oss_audio_t *ossaudio = (oss_audio_t*)data;
2010-05-28 12:33:18 +00:00
2011-06-16 21:20:12 +00:00
if (size == 0)
2010-05-28 12:33:18 +00:00
return 0;
2020-09-20 21:30:19 +00:00
if ((ret = write(ossaudio->fd, buf, size)) < 0)
2010-08-16 17:16:03 +00:00
{
2020-09-20 21:30:19 +00:00
if (errno == EAGAIN && (fcntl(ossaudio->fd, F_GETFL) & O_NONBLOCK))
2010-08-16 17:16:03 +00:00
return 0;
2012-04-25 17:49:11 +00:00
2010-05-28 12:33:18 +00:00
return -1;
2010-08-16 17:16:03 +00:00
}
2010-05-28 12:33:18 +00:00
return ret;
}
static bool oss_stop(void *data)
2010-05-28 12:33:18 +00:00
{
2020-09-20 21:30:19 +00:00
oss_audio_t *ossaudio = (oss_audio_t*)data;
2015-01-12 04:05:56 +00:00
#if !defined(RETROFW)
2020-09-20 21:30:19 +00:00
if (ioctl(ossaudio->fd, SNDCTL_DSP_RESET, 0) < 0)
2017-06-28 04:58:01 +00:00
return false;
#endif
2017-06-28 04:58:01 +00:00
2020-09-20 21:33:13 +00:00
ossaudio->is_paused = true;
2010-05-28 12:33:18 +00:00
return true;
}
static bool oss_start(void *data, bool is_shutdown)
2010-05-28 12:33:18 +00:00
{
2020-09-20 21:33:13 +00:00
oss_audio_t *ossaudio = (oss_audio_t*)data;
if (!ossaudio)
return false;
ossaudio->is_paused = false;
2010-05-28 12:33:18 +00:00
return true;
}
static bool oss_alive(void *data)
{
2020-09-20 21:33:13 +00:00
oss_audio_t *ossaudio = (oss_audio_t*)data;
return !ossaudio->is_paused;
}
static void oss_set_nonblock_state(void *data, bool state)
2010-08-16 17:16:03 +00:00
{
2010-08-16 17:49:54 +00:00
int rc;
2020-09-20 21:30:19 +00:00
oss_audio_t *ossaudio = (oss_audio_t*)data;
2015-01-12 04:05:56 +00:00
2010-08-16 17:16:03 +00:00
if (state)
2020-09-20 21:30:19 +00:00
rc = fcntl(ossaudio->fd, F_SETFL,
fcntl(ossaudio->fd, F_GETFL) | O_NONBLOCK);
2010-08-16 17:16:03 +00:00
else
2020-09-20 21:30:19 +00:00
rc = fcntl(ossaudio->fd, F_SETFL,
fcntl(ossaudio->fd, F_GETFL) & (~O_NONBLOCK));
2010-08-16 17:49:54 +00:00
if (rc != 0)
2012-04-21 21:25:32 +00:00
RARCH_WARN("Could not set nonblocking on OSS file descriptor. Will not be able to fast-forward.\n");
2010-08-16 17:16:03 +00:00
}
static void oss_free(void *data)
2010-05-28 12:33:18 +00:00
{
2020-09-20 21:30:19 +00:00
oss_audio_t *ossaudio = (oss_audio_t*)data;
2010-05-28 12:33:18 +00:00
/*RETROFW IOCTL always returns EINVAL*/
#if !defined(RETROFW)
2020-09-20 21:30:19 +00:00
if (ioctl(ossaudio->fd, SNDCTL_DSP_RESET, 0) < 0)
2017-06-28 04:58:01 +00:00
return;
#endif
2017-06-28 04:58:01 +00:00
2020-09-20 21:30:19 +00:00
close(ossaudio->fd);
free(data);
2010-05-28 12:33:18 +00:00
}
static size_t oss_write_avail(void *data)
{
audio_buf_info info;
2020-09-20 21:30:19 +00:00
oss_audio_t *ossaudio = (oss_audio_t*)data;
2020-09-20 21:30:19 +00:00
if (ioctl(ossaudio->fd, SNDCTL_DSP_GETOSPACE, &info) < 0)
{
2017-03-24 00:46:28 +00:00
RARCH_ERR("[OSS]: SNDCTL_DSP_GETOSPACE failed ...\n");
return 0;
}
return info.bytes;
}
static size_t oss_buffer_size(void *data)
{
audio_buf_info info;
2020-09-20 21:30:19 +00:00
oss_audio_t *ossaudio = (oss_audio_t*)data;
2020-09-20 21:30:19 +00:00
if (ioctl(ossaudio->fd, SNDCTL_DSP_GETOSPACE, &info) < 0)
{
2017-03-24 00:46:28 +00:00
RARCH_ERR("[OSS]: SNDCTL_DSP_GETOSPACE failed ...\n");
return 1; /* Return something non-zero to avoid SIGFPE. */
}
return info.fragsize * info.fragstotal;
}
static bool oss_use_float(void *data)
{
(void)data;
return false;
}
2014-09-11 05:06:20 +00:00
audio_driver_t audio_oss = {
2011-12-24 12:46:12 +00:00
oss_init,
oss_write,
oss_stop,
oss_start,
oss_alive,
2011-12-24 12:46:12 +00:00
oss_set_nonblock_state,
oss_free,
oss_use_float,
"oss",
NULL,
NULL,
oss_write_avail,
oss_buffer_size,
2010-05-28 12:33:18 +00:00
};