Files
RetroArch/audio/drivers/oss.c
libretroadmin 7e92aa944e oss: RetroFW writes whole 16-byte DMA units
RetroFW's jz4760 OSS driver moves a node to the codec in 16-byte DMA
units: it drops a write's remainder, and a write under one unit starts
a transfer of none. The threaded pipeline writes any number of frames,
so most writes lost one to three frames - static at its rate of writes -
and a short one could leave the device playing nothing it was given.
On RetroFW a write now goes out in whole units, its remainder held to
lead the next one.

samples/audio/oss_report models that DMA; built with RETROFW=1, which
CI now does, it checks every node is whole units and plays in order.

Fixes #20006
2026-11-07 19:06:01 +01:00

572 lines
17 KiB
C

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* 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.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <compat/strl.h>
#include <lists/string_list.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#ifdef HAVE_OSS_BSD
#include <soundcard.h>
#else
#include <sys/soundcard.h>
#endif
#include <retro_endianness.h>
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
#include <retro_miscellaneous.h>
#include "../audio_driver.h"
#include "../audio_device_label.h"
#include "../../verbosity.h"
#ifdef HAVE_OSS_BSD
#define DEFAULT_OSS_DEV "/dev/audio"
#else
#define DEFAULT_OSS_DEV "/dev/dsp"
#endif
typedef struct oss_audio
{
int fd;
/* The rate the device settled on; sizes the wait bound below. */
int rate;
/* Frames handed to the device since it opened, for the sink rate
* estimate; the device's own count is this less what is still
* queued. The format is fixed at S16 stereo below, so a frame is
* four bytes. */
uint64_t frames_written;
/* The layout the device opened with, and the frame: S16 at the
* layout's channel count. OSS's channel order is its own - FL FR
* BL BR FC LFE SL SR - not the frontend's mask order (FL FR FC LFE
* BL BR SL SR), so a wider frame is permuted on the way through
* order[] into stage, output channel k taking input slot order[k]. */
uint32_t layout;
unsigned channels;
unsigned order[8];
int16_t *stage;
size_t stage_bytes;
bool is_paused;
bool nonblock;
/* select() on this device does not wait for room: a driver with no
* poll method - RetroFW's jz4760 one - reports every descriptor
* ready at once. Latched by oss_wait_writable(); from then on the
* wait for room is the blocking write's, in the driver, on its own
* fragment-done interrupt. */
bool select_blind;
#ifdef RETROFW
/* jz4760's DMA moves 16-byte units: it drops a write's remainder,
* and a write under one unit starts a transfer of none. Writes go
* out in whole units; the remainder leads the next one. */
uint8_t *units;
size_t units_bytes;
uint8_t carry[16];
size_t carry_bytes;
#endif
} oss_audio_t;
#define OSS_FRAME_BYTES (ossaudio->channels * sizeof(int16_t))
/* Iteration cap for oss_wait_writable(): bounds wakes that deliver no
* space, so one call costs at most this many bounded waits. */
#define OSS_WAIT_WRITABLE_LAPS 8
#ifdef RETROFW
#define OSS_DMA_UNIT 16
/* @len bytes behind the carry, whole units to the device. Returns the
* bytes of @s taken, the remainder held among them. */
static ssize_t oss_write_units(oss_audio_t *ossaudio,
const uint8_t *s, size_t len)
{
ssize_t w;
size_t k = ossaudio->carry_bytes;
size_t total = k + len;
size_t whole = total & ~(size_t)(OSS_DMA_UNIT - 1);
const uint8_t *out = s;
if (!whole)
{
memcpy(ossaudio->carry + k, s, len);
ossaudio->carry_bytes = total;
return (ssize_t)len;
}
if (k)
{
if (ossaudio->units_bytes < whole)
{
uint8_t *n = (uint8_t*)realloc(ossaudio->units, whole);
if (!n)
return -1;
ossaudio->units = n;
ossaudio->units_bytes = whole;
}
memcpy(ossaudio->units, ossaudio->carry, k);
memcpy(ossaudio->units + k, s, whole - k);
out = ossaudio->units;
}
if ((w = write(ossaudio->fd, out, whole)) < 0)
{
if (errno == EAGAIN && (fcntl(ossaudio->fd, F_GETFL) & O_NONBLOCK))
return 0;
return -1;
}
if (!w)
return 0;
ossaudio->frames_written += (uint64_t)w / OSS_FRAME_BYTES;
ossaudio->carry_bytes = 0;
/* Short, the rest stays with the caller; whole units always. */
if ((size_t)w < whole)
return w - (ssize_t)k;
memcpy(ossaudio->carry, s + (whole - k), total - whole);
ossaudio->carry_bytes = total - whole;
return (ssize_t)len;
}
#endif
static void *oss_init(const char *device,
unsigned rate, unsigned latency,
unsigned *new_out_rate)
{
int frags, frag, channels, format, new_rate;
char oss_device[PATH_MAX_LENGTH];
oss_audio_t *ossaudio = (oss_audio_t*)calloc(1, sizeof(oss_audio_t));
if (!ossaudio)
return NULL;
/* The device may be a list entry, "/dev/dsp0 (card)": the path is
* what is opened. */
if (device)
audio_device_label_path(oss_device, sizeof(oss_device), device);
else
strlcpy(oss_device, DEFAULT_OSS_DEV, sizeof(oss_device));
if ((ossaudio->fd = open(oss_device, O_WRONLY)) < 0)
{
free(ossaudio);
perror("open");
return NULL;
}
frags = (latency * rate * 4) / (1000 * (1 << 10));
frag = (frags << 16) | 10;
if (ioctl(ossaudio->fd, SNDCTL_DSP_SETFRAGMENT, &frag) < 0)
RARCH_WARN("[OSS] Could not set fragment sizes. Latency might not be as expected.\n");
/* The layout the frontend asked for; the device says what count it
* takes, and anything but the count asked for is stereo. The back
* pair is what OSS's order has, so a layout with the pair at the
* sides is reported as the back pair. */
{
uint32_t want = audio_driver_requested_layout();
unsigned ch = audio_layout_channels(want);
ossaudio->layout = AUDIO_LAYOUT_STEREO;
ossaudio->channels = 2;
channels = (ch == 4 || ch == 6 || ch == 8) ? (int)ch : 2;
if (ioctl(ossaudio->fd, SNDCTL_DSP_CHANNELS, &channels) < 0)
goto error;
if (channels != 2 && channels != (int)ch)
{
RARCH_LOG("[OSS] The device answered %d channels to %u asked; opening stereo.\n", channels, ch);
channels = 2;
if (ioctl(ossaudio->fd, SNDCTL_DSP_CHANNELS, &channels) < 0 || channels != 2)
goto error;
}
if (channels == 8)
{
static const unsigned o[8] = { 0, 1, 4, 5, 2, 3, 6, 7 };
ossaudio->layout = AUDIO_LAYOUT_STEREO | AUDIO_SPEAKER_FRONT_CENTER | AUDIO_SPEAKER_LOW_FREQUENCY
| AUDIO_SPEAKER_BACK_LEFT | AUDIO_SPEAKER_BACK_RIGHT | AUDIO_SPEAKER_SIDE_LEFT | AUDIO_SPEAKER_SIDE_RIGHT;
memcpy(ossaudio->order, o, sizeof(o));
}
else if (channels == 6)
{
static const unsigned o[6] = { 0, 1, 4, 5, 2, 3 };
ossaudio->layout = AUDIO_LAYOUT_STEREO | AUDIO_SPEAKER_FRONT_CENTER | AUDIO_SPEAKER_LOW_FREQUENCY
| AUDIO_SPEAKER_BACK_LEFT | AUDIO_SPEAKER_BACK_RIGHT;
memcpy(ossaudio->order, o, sizeof(o));
}
else if (channels == 4)
{
static const unsigned o[4] = { 0, 1, 2, 3 };
ossaudio->layout = AUDIO_LAYOUT_STEREO | AUDIO_SPEAKER_BACK_LEFT | AUDIO_SPEAKER_BACK_RIGHT;
memcpy(ossaudio->order, o, sizeof(o));
}
ossaudio->channels = (unsigned)channels;
if (channels > 2)
RARCH_LOG("[OSS] Opened %d channels, layout 0x%03x, in OSS's channel order.\n", channels, ossaudio->layout);
}
format = is_little_endian() ? AFMT_S16_LE : AFMT_S16_BE;
if (ioctl(ossaudio->fd, SNDCTL_DSP_SETFMT, &format) < 0)
goto error;
new_rate = rate;
if (ioctl(ossaudio->fd, SNDCTL_DSP_SPEED, &new_rate) < 0)
goto error;
if (new_rate != (int)rate)
{
RARCH_WARN("[OSS] Requested sample rate not supported. Adjusting output rate to %d Hz.\n", new_rate);
*new_out_rate = new_rate;
}
ossaudio->rate = new_rate;
return ossaudio;
error:
close(ossaudio->fd);
if (ossaudio)
free(ossaudio);
perror("ioctl");
return NULL;
}
static ssize_t oss_write(void *data, const void *s, size_t len)
{
ssize_t _len;
oss_audio_t *ossaudio = (oss_audio_t*)data;
if (len == 0)
return 0;
if (ossaudio->channels > 2)
{
/* into OSS's channel order, a whole frame at a time */
size_t frames = len / OSS_FRAME_BYTES, f;
unsigned c;
const int16_t *in = (const int16_t*)s;
if (ossaudio->stage_bytes < frames * OSS_FRAME_BYTES)
{
int16_t *n = (int16_t*)realloc(ossaudio->stage, frames * OSS_FRAME_BYTES);
if (!n)
return -1;
ossaudio->stage = n;
ossaudio->stage_bytes = frames * OSS_FRAME_BYTES;
}
for (f = 0; f < frames; f++)
for (c = 0; c < ossaudio->channels; c++)
ossaudio->stage[f * ossaudio->channels + c] = in[f * ossaudio->channels + ossaudio->order[c]];
s = ossaudio->stage;
len = frames * OSS_FRAME_BYTES;
}
#ifdef RETROFW
return oss_write_units(ossaudio, (const uint8_t*)s, len);
#endif
if ((_len = write(ossaudio->fd, s, len)) < 0)
{
if (errno == EAGAIN && (fcntl(ossaudio->fd, F_GETFL) & O_NONBLOCK))
return 0;
return -1;
}
ossaudio->frames_written += (uint64_t)_len / OSS_FRAME_BYTES;
return _len;
}
/* Frames the device has played since it opened.
*
* ALSA's shape: everything written, less what has not been played yet.
* SNDCTL_DSP_GETODELAY reports that remainder in bytes - the whole of
* the device's queue, which is what should come off - and a driver that
* does not implement it fails the ioctl, which reports nothing rather
* than a count that would read as a stall. */
static size_t oss_frames_consumed(void *data)
{
oss_audio_t *ossaudio = (oss_audio_t*)data;
int delay = 0;
uint64_t queued;
if (!ossaudio || ossaudio->fd < 0)
return 0;
if (ioctl(ossaudio->fd, SNDCTL_DSP_GETODELAY, &delay) < 0 || delay < 0)
return 0;
queued = (uint64_t)delay / OSS_FRAME_BYTES;
if (queued > ossaudio->frames_written)
return 0;
return (size_t)(ossaudio->frames_written - queued);
}
/* Sleep in select() on the device until it is writable, which OSS
* reports at fragment granularity, then ask SNDCTL_DSP_GETOSPACE how
* much fits; repeat until at least len bytes do, len capped at one
* fragment. Every select() is bounded to two fragments' worth of time,
* so a device that stalls returns 0 - a pass to skip - rather than
* holding the audio thread. A device that wakes OSS_WAIT_WRITABLE_LAPS
* times without space has a select() that does not wait on room
* (select_blind): its room is claimed from then on, and the blocking
* write waits for it. Returns the free space otherwise. */
static size_t oss_wait_writable(void *data, size_t len)
{
oss_audio_t *ossaudio = (oss_audio_t*)data;
audio_buf_info info;
int laps = OSS_WAIT_WRITABLE_LAPS;
for (;;)
{
fd_set wfds;
struct timeval tv;
int rc;
size_t want = len;
long wait_us = 100000;
if (ioctl(ossaudio->fd, SNDCTL_DSP_GETOSPACE, &info) < 0)
return 0;
if (info.fragsize > 0 && want > (size_t)info.fragsize)
want = (size_t)info.fragsize;
if (info.bytes >= (int)want)
return (size_t)info.bytes;
/* Nothing here can wait on such a device: the room is claimed,
* and the write that follows sleeps in the driver until a
* fragment is played. Not without blocking writes - there the
* write would refuse, and the caller skips the pass instead. */
if (ossaudio->select_blind)
return ossaudio->nonblock ? 0 : want;
/* Two fragments, in microseconds: a fragment is fragsize bytes
* of 16-bit stereo at the device rate. Clamped so a strange
* fragment size cannot make the bound useless either way. */
if (ossaudio->rate > 0 && info.fragsize > 0)
wait_us = (long)((2000000LL * info.fragsize / 4) / ossaudio->rate);
if (wait_us < 20000)
wait_us = 20000;
if (wait_us > 200000)
wait_us = 200000;
tv.tv_sec = 0;
tv.tv_usec = wait_us;
FD_ZERO(&wfds);
FD_SET(ossaudio->fd, &wfds);
rc = select(ossaudio->fd + 1, NULL, &wfds, NULL, &tv);
if (rc < 0)
{
if (errno == EINTR)
continue;
return 0;
}
if (rc == 0)
return 0;
/* Every lap woken and none of them with room: select() is not
* waiting on room. Handed 0 for it, the audio thread comes
* straight back, and on a single core at real-time priority
* nothing else then runs. */
if (--laps < 0)
{
ossaudio->select_blind = true;
return ossaudio->nonblock ? 0 : want;
}
}
}
static bool oss_stop(void *data)
{
oss_audio_t *ossaudio = (oss_audio_t*)data;
#if !defined(RETROFW)
if (ioctl(ossaudio->fd, SNDCTL_DSP_RESET, 0) < 0)
return false;
#endif
ossaudio->is_paused = true;
#ifdef RETROFW
ossaudio->carry_bytes = 0;
#endif
return true;
}
static bool oss_start(void *data, bool is_shutdown)
{
oss_audio_t *ossaudio = (oss_audio_t*)data;
if (!ossaudio)
return false;
ossaudio->is_paused = false;
return true;
}
static bool oss_alive(void *data)
{
oss_audio_t *ossaudio = (oss_audio_t*)data;
return !ossaudio->is_paused;
}
static void oss_set_nonblock_state(void *data, bool state)
{
int rc;
oss_audio_t *ossaudio = (oss_audio_t*)data;
if (state)
rc = fcntl(ossaudio->fd, F_SETFL,
fcntl(ossaudio->fd, F_GETFL) | O_NONBLOCK);
else
rc = fcntl(ossaudio->fd, F_SETFL,
fcntl(ossaudio->fd, F_GETFL) & (~O_NONBLOCK));
if (rc != 0)
RARCH_WARN("[OSS] Could not set nonblocking on OSS file descriptor. Will not be able to fast-forward.\n");
else
ossaudio->nonblock = state;
}
static void oss_free(void *data)
{
oss_audio_t *ossaudio = (oss_audio_t*)data;
/*RETROFW IOCTL always returns EINVAL*/
#if !defined(RETROFW)
/* Fails on a device that has gone away; it is closed all the same. */
ioctl(ossaudio->fd, SNDCTL_DSP_RESET, 0);
#endif
if (ossaudio->select_blind)
RARCH_LOG("[OSS] The device's select() did not wait for room; "
"its blocking write did.\n");
close(ossaudio->fd);
free(ossaudio->stage);
#ifdef RETROFW
free(ossaudio->units);
#endif
free(data);
}
/* No room when the device cannot say: a device that has gone away
* fails this every frame, so it is not logged here. */
static size_t oss_write_avail(void *data)
{
audio_buf_info info;
oss_audio_t *ossaudio = (oss_audio_t*)data;
if (ioctl(ossaudio->fd, SNDCTL_DSP_GETOSPACE, &info) < 0)
return 0;
return info.bytes;
}
static size_t oss_buffer_size(void *data)
{
audio_buf_info info;
oss_audio_t *ossaudio = (oss_audio_t*)data;
/* Zero when the device cannot say, which turns rate control off for
* the session, as audio_driver.h has it; the frontend logs that. */
if (ioctl(ossaudio->fd, SNDCTL_DSP_GETOSPACE, &info) < 0)
return 0;
return info.fragsize * info.fragstotal;
}
static bool oss_use_float(void *data)
{
return false;
}
/* The device string is a path. List the /dev/dsp nodes that exist:
* /dev/dsp itself and /dev/dsp0..15, which covers Linux OSS, OSS4 and
* the BSDs. Where SNDCTL_AUDIOINFO exists the card name is appended so
* the entry is recognisable; the path stays the value. */
static void *oss_device_list_new(void *data)
{
int i;
union string_list_elem_attr attr;
struct string_list *sl = string_list_new();
(void)data;
attr.i = 0;
if (!sl)
return NULL;
for (i = -1; i < 16; i++)
{
char path[32];
char label[160];
int fd;
if (i < 0)
strlcpy(path, DEFAULT_OSS_DEV, sizeof(path));
else
snprintf(path, sizeof(path), "/dev/dsp%d", i);
/* A node that cannot be opened for output is not offered; a busy
* one is, since it exists and may be free later. */
fd = open(path, O_WRONLY | O_NONBLOCK);
if (fd < 0 && errno != EBUSY)
continue;
strlcpy(label, path, sizeof(label));
#ifdef SNDCTL_AUDIOINFO
if (fd >= 0)
{
oss_audioinfo ai;
memset(&ai, 0, sizeof(ai));
ai.dev = -1;
if (ioctl(fd, SNDCTL_AUDIOINFO, &ai) == 0 && ai.name[0])
snprintf(label, sizeof(label), "%s (%s)", path, ai.name);
}
#endif
if (fd >= 0)
close(fd);
attr.i = i;
string_list_append(sl, label, attr);
}
return sl;
}
static void oss_device_list_free(void *data, void *array_list_data)
{
struct string_list *sl = (struct string_list*)array_list_data;
(void)data;
if (sl)
string_list_free(sl);
}
static uint32_t oss_layout(void *data)
{
oss_audio_t *ossaudio = (oss_audio_t*)data;
return ossaudio ? ossaudio->layout : AUDIO_LAYOUT_STEREO;
}
audio_driver_t audio_oss = {
oss_init,
oss_write,
oss_stop,
oss_start,
oss_alive,
oss_set_nonblock_state,
oss_free,
oss_use_float,
"oss",
oss_device_list_new,
oss_device_list_free,
oss_write_avail,
oss_buffer_size,
NULL, /* write_raw */
oss_wait_writable,
oss_frames_consumed,
NULL, /* underruns */
oss_layout
};