/* * 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 #include #include #include #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; }