alsa-lib/src/pcm/pcm.c

373 lines
7.6 KiB
C
Raw Normal View History

1998-08-13 15:42:56 +00:00
/*
* PCM Interface - main file
* Copyright (c) 1998 by Jaroslav Kysela <perex@suse.cz>
1998-08-27 20:47:51 +00:00
*
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
1998-08-13 15:42:56 +00:00
*/
1998-08-13 15:42:56 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
1998-08-27 20:47:51 +00:00
#include <string.h>
1998-08-13 15:42:56 +00:00
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
1998-08-30 21:08:44 +00:00
#include "asoundlib.h"
1998-08-13 15:42:56 +00:00
#define SND_FILE_PCM "/dev/snd/pcmC%iD%i"
1998-11-01 23:22:41 +00:00
#define SND_PCM_VERSION_MAX SND_PROTOCOL_VERSION( 1, 0, 1 )
1998-11-27 14:57:39 +00:00
struct snd_pcm {
1998-11-27 14:57:39 +00:00
int card;
int device;
int fd;
1999-08-22 13:34:31 +00:00
int mode;
};
1998-11-27 14:57:39 +00:00
int snd_pcm_open(snd_pcm_t **handle, int card, int device, int mode)
1998-08-13 15:42:56 +00:00
{
1999-08-22 13:34:31 +00:00
return snd_pcm_open_subdevice(handle, card, device, -1, mode);
}
int snd_pcm_open_subdevice(snd_pcm_t **handle, int card, int device, int subdevice, int mode)
{
int fd, ver, err, attempt = 0;
1998-11-27 14:57:39 +00:00
char filename[32];
snd_pcm_t *pcm;
1999-08-22 13:34:31 +00:00
snd_ctl_t *ctl;
snd_pcm_playback_info_t pinfo;
snd_pcm_capture_info_t cinfo;
1998-11-27 14:57:39 +00:00
*handle = NULL;
1999-01-30 18:35:52 +00:00
1998-11-27 14:57:39 +00:00
if (card < 0 || card >= SND_CARDS)
return -EINVAL;
1999-08-22 13:34:31 +00:00
if ((err = snd_ctl_open(&ctl, card)) < 0)
return err;
__again:
if (attempt++ > 3) {
snd_ctl_close(ctl);
return -EBUSY;
}
if ((err = snd_ctl_pcm_prefer_subdevice(ctl, device, subdevice)) < 0) {
snd_ctl_close(ctl);
return err;
}
1998-11-27 14:57:39 +00:00
sprintf(filename, SND_FILE_PCM, card, device);
1999-01-30 18:35:52 +00:00
if ((fd = open(filename, mode)) < 0) {
1999-08-22 13:34:31 +00:00
err = -errno;
snd_ctl_close(ctl);
return err;
1999-01-30 18:35:52 +00:00
}
1998-11-27 14:57:39 +00:00
if (ioctl(fd, SND_PCM_IOCTL_PVERSION, &ver) < 0) {
1999-08-22 13:34:31 +00:00
err = -errno;
1998-11-27 14:57:39 +00:00
close(fd);
1999-08-22 13:34:31 +00:00
snd_ctl_close(ctl);
return err;
1998-11-27 14:57:39 +00:00
}
1999-07-22 10:49:39 +00:00
if (SND_PROTOCOL_INCOMPATIBLE(ver, SND_PCM_VERSION_MAX)) {
1998-11-27 14:57:39 +00:00
close(fd);
1999-08-22 13:34:31 +00:00
snd_ctl_close(ctl);
1999-07-22 10:49:39 +00:00
return -SND_ERROR_INCOMPATIBLE_VERSION;
1998-11-27 14:57:39 +00:00
}
1999-08-22 13:34:31 +00:00
if (subdevice >= 0 && (mode == SND_PCM_OPEN_PLAYBACK || mode == SND_PCM_OPEN_DUPLEX)) {
if (ioctl(fd, SND_PCM_IOCTL_PLAYBACK_INFO, &pinfo) < 0) {
err = -errno;
close(fd);
snd_ctl_close(ctl);
return err;
}
if (pinfo.subdevice != subdevice) {
close(fd);
goto __again;
}
}
if (subdevice >= 0 && (mode == SND_PCM_OPEN_CAPTURE || mode == SND_PCM_OPEN_DUPLEX)) {
if (ioctl(fd, SND_PCM_IOCTL_CAPTURE_INFO, &cinfo) < 0) {
err = -errno;
close(fd);
snd_ctl_close(ctl);
return err;
}
if (cinfo.subdevice != subdevice) {
close(fd);
goto __again;
}
}
1998-11-27 14:57:39 +00:00
pcm = (snd_pcm_t *) calloc(1, sizeof(snd_pcm_t));
if (pcm == NULL) {
close(fd);
return -ENOMEM;
}
pcm->card = card;
pcm->device = device;
pcm->fd = fd;
1999-08-22 13:34:31 +00:00
pcm->mode = mode;
1998-11-27 14:57:39 +00:00
*handle = pcm;
return 0;
1998-08-13 15:42:56 +00:00
}
int snd_pcm_close(snd_pcm_t *handle)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
int res;
pcm = handle;
1998-11-27 14:57:39 +00:00
if (!pcm)
return -EINVAL;
res = close(pcm->fd) < 0 ? -errno : 0;
free(pcm);
return res;
1998-08-13 15:42:56 +00:00
}
int snd_pcm_file_descriptor(snd_pcm_t *handle)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-11-27 14:57:39 +00:00
if (!pcm)
return -EINVAL;
return pcm->fd;
1998-08-13 15:42:56 +00:00
}
int snd_pcm_block_mode(snd_pcm_t *handle, int enable)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
long flags;
pcm = handle;
1998-11-27 14:57:39 +00:00
if (!pcm)
return -EINVAL;
1999-01-06 15:25:15 +00:00
if ((flags = fcntl(pcm->fd, F_GETFL)) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
if (enable)
flags &= ~O_NONBLOCK;
1999-01-13 14:51:21 +00:00
else
flags |= O_NONBLOCK;
1999-01-06 15:25:15 +00:00
if (fcntl(pcm->fd, F_SETFL, flags) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
int snd_pcm_info(snd_pcm_t *handle, snd_pcm_info_t * info)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-12-27 01:01:47 +00:00
if (!pcm || !info)
1998-11-27 14:57:39 +00:00
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_INFO, info) < 0)
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
int snd_pcm_playback_info(snd_pcm_t *handle, snd_pcm_playback_info_t * info)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-12-27 01:01:47 +00:00
if (!pcm || !info)
1998-11-27 14:57:39 +00:00
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_INFO, info) < 0)
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
1999-06-12 19:23:10 +00:00
int snd_pcm_capture_info(snd_pcm_t *handle, snd_pcm_capture_info_t * info)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-12-27 01:01:47 +00:00
if (!pcm || !info)
1998-11-27 14:57:39 +00:00
return -EINVAL;
1999-06-12 19:23:10 +00:00
if (ioctl(pcm->fd, SND_PCM_IOCTL_CAPTURE_INFO, info) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
int snd_pcm_playback_format(snd_pcm_t *handle, snd_pcm_format_t * format)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-12-27 01:01:47 +00:00
if (!pcm || !format)
1998-11-27 14:57:39 +00:00
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_FORMAT, format) < 0)
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
1999-06-12 19:23:10 +00:00
int snd_pcm_capture_format(snd_pcm_t *handle, snd_pcm_format_t * format)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-12-27 01:01:47 +00:00
if (!pcm || !format)
1998-11-27 14:57:39 +00:00
return -EINVAL;
1999-06-12 19:23:10 +00:00
if (ioctl(pcm->fd, SND_PCM_IOCTL_CAPTURE_FORMAT, format) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
int snd_pcm_playback_params(snd_pcm_t *handle, snd_pcm_playback_params_t * params)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-12-27 01:01:47 +00:00
if (!pcm || !params)
1998-11-27 14:57:39 +00:00
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_PARAMS, params) < 0)
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
1999-06-12 19:23:10 +00:00
int snd_pcm_capture_params(snd_pcm_t *handle, snd_pcm_capture_params_t * params)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-12-27 01:01:47 +00:00
if (!pcm || !params)
1998-11-27 14:57:39 +00:00
return -EINVAL;
1999-06-12 19:23:10 +00:00
if (ioctl(pcm->fd, SND_PCM_IOCTL_CAPTURE_PARAMS, params) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
int snd_pcm_playback_status(snd_pcm_t *handle, snd_pcm_playback_status_t * status)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-12-27 01:01:47 +00:00
if (!pcm || !status)
1998-11-27 14:57:39 +00:00
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_STATUS, status) < 0)
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
1999-06-12 19:23:10 +00:00
int snd_pcm_capture_status(snd_pcm_t *handle, snd_pcm_capture_status_t * status)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-12-27 01:01:47 +00:00
if (!pcm || !status)
1998-11-27 14:57:39 +00:00
return -EINVAL;
1999-06-12 19:23:10 +00:00
if (ioctl(pcm->fd, SND_PCM_IOCTL_CAPTURE_STATUS, status) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
int snd_pcm_drain_playback(snd_pcm_t *handle)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-11-27 14:57:39 +00:00
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_DRAIN_PLAYBACK) < 0)
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
int snd_pcm_flush_playback(snd_pcm_t *handle)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-11-27 14:57:39 +00:00
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_FLUSH_PLAYBACK) < 0)
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
1999-06-12 19:23:10 +00:00
int snd_pcm_flush_capture(snd_pcm_t *handle)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-11-27 14:57:39 +00:00
if (!pcm)
return -EINVAL;
1999-06-12 19:23:10 +00:00
if (ioctl(pcm->fd, SND_PCM_IOCTL_FLUSH_CAPTURE) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-25 15:38:50 +00:00
}
int snd_pcm_playback_pause(snd_pcm_t *handle, int enable)
1998-08-25 15:38:50 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-11-27 14:57:39 +00:00
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_PAUSE, &enable) < 0)
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
int snd_pcm_playback_time(snd_pcm_t *handle, int enable)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-11-27 14:57:39 +00:00
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_TIME, &enable) < 0)
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
1999-06-12 19:23:10 +00:00
int snd_pcm_capture_time(snd_pcm_t *handle, int enable)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
pcm = handle;
1998-11-27 14:57:39 +00:00
if (!pcm)
return -EINVAL;
1999-06-12 19:23:10 +00:00
if (ioctl(pcm->fd, SND_PCM_IOCTL_CAPTURE_TIME, &enable) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-13 15:42:56 +00:00
}
ssize_t snd_pcm_write(snd_pcm_t *handle, const void *buffer, size_t size)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
ssize_t result;
pcm = handle;
1998-12-27 01:01:47 +00:00
if (!pcm || (!buffer && size > 0) || size < 0)
1998-11-27 14:57:39 +00:00
return -EINVAL;
result = write(pcm->fd, buffer, size);
if (result < 0)
return -errno;
return result;
1998-08-13 15:42:56 +00:00
}
ssize_t snd_pcm_read(snd_pcm_t *handle, void *buffer, size_t size)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
snd_pcm_t *pcm;
ssize_t result;
pcm = handle;
1998-12-27 01:01:47 +00:00
if (!pcm || (!buffer && size > 0) || size < 0)
1998-11-27 14:57:39 +00:00
return -EINVAL;
result = read(pcm->fd, buffer, size);
if (result < 0)
return -errno;
return result;
1998-08-13 15:42:56 +00:00
}