McdeSys.h gains mcde_sys_pcm_open/wait/read/write/close/backend: signed 16-bit interleaved frames, one direction per handle, non-blocking (0 = would block). audio_linux.c drives ALSA (S16_LE, the wanted rate, 20 ms periods, four periods of buffer, EAGAIN -> 0, xrun recovery, snd_pcm_wait); it compiles to ENOTSUP without HAVE_ALSA so the desktop still builds. audio_netbsd.c sets audio(4) (/dev/audio or /dev/sound) to SLINEAR_LE 16 bit with AUDIO_SETINFO and verifies with AUDIO_GETINFO, then polls the descriptor; it compiles against the NetBSD sysroot (-D_NETBSD_SOURCE for u_int, play/record sub-structs) but has not run on NetBSD. audio_null.c is silence in/discard out. configure checks alsa/asoundlib.h+libasound on non-NetBSD (optional, with a warning) and passes ALSA_CPPFLAGS/ALSALIB. test_audio checks the null stream frame by frame (both directions, wrong-direction errors, bad arguments) and tries the real playback/capture device, skipping without a card: standalone and ASan/UBSan pass, and the container's ALSA device played and captured 5 periods each. test_audio is a noinst program and a harness entry. All four files compile with x86_64--netbsd-gcc against destdir.amd64.
207 lines
5.1 KiB
C
207 lines
5.1 KiB
C
/*
|
|
* audio_netbsd.c -- MCDE: PCM streams through audio(4) (McdeSys.h, "Sound")
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* The device is /dev/audio (or $AUDIODEV, or /dev/sound), set to signed
|
|
* 16-bit little-endian, the wanted rate and channels with AUDIO_SETINFO;
|
|
* the answer is checked, because a device may round the rate. The smaller
|
|
* of the wanted and the device's block size is one period. The descriptor is
|
|
* non-blocking: read/write answer EAGAIN when nothing fits, mcde_sys_pcm_wait
|
|
* polls it. Compiled for the NetBSD sysroot, not yet run on NetBSD.
|
|
*/
|
|
|
|
/* audio(4) uses the BSD types (u_int) that a strict POSIX feature set hides;
|
|
* NetBSD's own programs ask for _NETBSD_SOURCE, so this file does too. */
|
|
#define _NETBSD_SOURCE 1
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <poll.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include <sys/audioio.h>
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
#include "private.h"
|
|
|
|
typedef struct {
|
|
int fd;
|
|
size_t block; /* frames per period */
|
|
} BsdPcm;
|
|
|
|
static ssize_t
|
|
BsdRead(McdeSysPcm *pcm, int16_t *frames, size_t count)
|
|
{
|
|
BsdPcm *b = pcm->priv;
|
|
size_t bytes = count * sizeof *frames;
|
|
ssize_t n;
|
|
|
|
n = read(b->fd, frames, bytes);
|
|
if (n < 0) {
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
return 0;
|
|
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot read the audio "
|
|
"device: %s.", strerror(errno));
|
|
return -1;
|
|
}
|
|
return n / (ssize_t) sizeof *frames;
|
|
}
|
|
|
|
static ssize_t
|
|
BsdWrite(McdeSysPcm *pcm, const int16_t *frames, size_t count)
|
|
{
|
|
BsdPcm *b = pcm->priv;
|
|
ssize_t n;
|
|
|
|
n = write(b->fd, frames, count * sizeof *frames);
|
|
if (n < 0) {
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
return 0;
|
|
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write the audio "
|
|
"device: %s.", strerror(errno));
|
|
return -1;
|
|
}
|
|
return n / (ssize_t) sizeof *frames;
|
|
}
|
|
|
|
static int
|
|
BsdWait(McdeSysPcm *pcm, int timeout_ms)
|
|
{
|
|
BsdPcm *b = pcm->priv;
|
|
struct pollfd p;
|
|
int rc;
|
|
|
|
p.fd = b->fd;
|
|
p.events = pcm->direction == MCDE_SYS_PCM_CAPTURE ? POLLIN : POLLOUT;
|
|
p.revents = 0;
|
|
rc = poll(&p, 1, timeout_ms);
|
|
if (rc < 0) {
|
|
if (errno == EINTR)
|
|
return 0;
|
|
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot wait for the "
|
|
"audio device: %s.", strerror(errno));
|
|
return -1;
|
|
}
|
|
if (rc == 0)
|
|
return 0;
|
|
if (p.revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
|
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "The audio device went "
|
|
"away.");
|
|
return -1;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static void
|
|
BsdClose(McdeSysPcm *pcm)
|
|
{
|
|
BsdPcm *b = pcm->priv;
|
|
|
|
if (b != NULL) {
|
|
if (b->fd >= 0)
|
|
(void) close(b->fd);
|
|
free(b);
|
|
}
|
|
pcm->priv = NULL;
|
|
}
|
|
|
|
static const McdeSysPcmOps bsdOps = {
|
|
BsdRead,
|
|
BsdWrite,
|
|
BsdWait,
|
|
BsdClose
|
|
};
|
|
|
|
McdeSysPcm *
|
|
mcde_sys_pcm_device_open(int direction, unsigned rate, unsigned channels)
|
|
{
|
|
const char *dev = getenv("AUDIODEV");
|
|
audio_info_t info;
|
|
McdeSysPcm *pcm = NULL;
|
|
BsdPcm *b = NULL;
|
|
int fd = -1;
|
|
int flags;
|
|
|
|
if (dev == NULL || *dev == '\0')
|
|
dev = "/dev/audio";
|
|
flags = direction == MCDE_SYS_PCM_PLAYBACK ? O_WRONLY : O_RDONLY;
|
|
fd = open(dev, flags | O_NONBLOCK | O_CLOEXEC);
|
|
if (fd < 0 && errno == ENOENT && strcmp(dev, "/dev/audio") == 0) {
|
|
dev = "/dev/sound";
|
|
fd = open(dev, flags | O_NONBLOCK | O_CLOEXEC);
|
|
}
|
|
if (fd < 0) {
|
|
(void) mcde_sys_fail(MCDE_SYS_ENOTSUP, "No audio device: %s.",
|
|
strerror(errno));
|
|
return NULL;
|
|
}
|
|
|
|
AUDIO_INITINFO(&info);
|
|
info.mode = direction == MCDE_SYS_PCM_PLAYBACK ?
|
|
AUMODE_PLAY : AUMODE_RECORD;
|
|
{
|
|
audio_prinfo_t *pr = direction == MCDE_SYS_PCM_PLAYBACK ?
|
|
&info.play : &info.record;
|
|
|
|
pr->encoding = AUDIO_ENCODING_SLINEAR_LE;
|
|
pr->precision = 16;
|
|
pr->channels = (u_int) channels;
|
|
pr->sample_rate = rate;
|
|
}
|
|
/* one period, in bytes, for the device's own blocking writes */
|
|
info.blocksize = (u_int) (rate / 50 * channels * 2);
|
|
if (ioctl(fd, AUDIO_SETINFO, &info) < 0) {
|
|
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot set the audio "
|
|
"format on %s: %s.", dev, strerror(errno));
|
|
(void) close(fd);
|
|
return NULL;
|
|
}
|
|
AUDIO_INITINFO(&info);
|
|
if (ioctl(fd, AUDIO_GETINFO, &info) < 0) {
|
|
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot read the audio "
|
|
"format of %s: %s.", dev, strerror(errno));
|
|
(void) close(fd);
|
|
return NULL;
|
|
}
|
|
{
|
|
audio_prinfo_t *pr = direction == MCDE_SYS_PCM_PLAYBACK ?
|
|
&info.play : &info.record;
|
|
|
|
if (pr->encoding != AUDIO_ENCODING_SLINEAR_LE ||
|
|
pr->precision != 16 ||
|
|
pr->channels != (u_int) channels ||
|
|
pr->sample_rate != rate) {
|
|
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "The audio "
|
|
"device did not take %u Hz, %u channel(s), "
|
|
"16 bit.", rate, channels);
|
|
(void) close(fd);
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
pcm = calloc(1, sizeof *pcm);
|
|
b = calloc(1, sizeof *b);
|
|
if (pcm == NULL || b == NULL) {
|
|
free(pcm);
|
|
free(b);
|
|
(void) close(fd);
|
|
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "Out of memory.");
|
|
return NULL;
|
|
}
|
|
b->fd = fd;
|
|
b->block = info.blocksize / (channels * 2) != 0 ?
|
|
info.blocksize / (channels * 2) : rate / 50;
|
|
pcm->ops = &bsdOps;
|
|
pcm->name = "audio(4)";
|
|
pcm->direction = direction;
|
|
pcm->priv = b;
|
|
return pcm;
|
|
}
|