Files
mcde/lib/McdeSys/audio_null.c
T
lotherk 233da2bc3f mcde: add McdeSys PCM streams for dtchat's calls (T6.1)
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.
2026-11-17 07:23:23 +00:00

87 lines
1.7 KiB
C

/*
* audio_null.c -- MCDE: the silent PCM stream (McdeSys.h, "Sound")
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* Capture reads silence, playback discards, a wait is over at once. It is
* what a test uses (no sound card in a test VM) and what a session without a
* device falls back to when the caller asks for it; nothing here needs ALSA
* or /dev/audio, so it builds on every system.
*/
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <Mcde/McdeSys.h>
#include "private.h"
typedef struct {
unsigned rate;
unsigned channels;
} NullPcm;
static ssize_t
NullRead(McdeSysPcm *pcm, int16_t *frames, size_t count)
{
(void) pcm;
memset(frames, 0, count * sizeof *frames);
return (ssize_t) count;
}
static ssize_t
NullWrite(McdeSysPcm *pcm, const int16_t *frames, size_t count)
{
(void) pcm;
(void) frames;
return (ssize_t) count;
}
static int
NullWait(McdeSysPcm *pcm, int timeout_ms)
{
(void) pcm;
(void) timeout_ms;
return 1;
}
static void
NullClose(McdeSysPcm *pcm)
{
free(pcm->priv);
pcm->priv = NULL;
}
static const McdeSysPcmOps nullOps = {
NullRead,
NullWrite,
NullWait,
NullClose
};
McdeSysPcm *
mcde_sys_pcm_null_open(int direction, unsigned rate, unsigned channels)
{
McdeSysPcm *pcm;
NullPcm *priv;
pcm = calloc(1, sizeof *pcm);
priv = calloc(1, sizeof *priv);
if (pcm == NULL || priv == NULL) {
free(pcm);
free(priv);
(void) mcde_sys_fail(MCDE_SYS_EFAIL, "Out of memory.");
return NULL;
}
priv->rate = rate;
priv->channels = channels;
pcm->ops = &nullOps;
pcm->name = "null";
pcm->direction = direction;
pcm->priv = priv;
return pcm;
}