2000-12-11 11:16:07 +00:00
|
|
|
/*
|
|
|
|
|
* PCM - Copy conversion
|
|
|
|
|
* Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <byteswap.h>
|
|
|
|
|
#include "pcm_local.h"
|
|
|
|
|
#include "pcm_plugin.h"
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
/* This field need to be the first */
|
|
|
|
|
snd_pcm_plugin_t plug;
|
|
|
|
|
} snd_pcm_copy_t;
|
|
|
|
|
|
2000-12-21 20:44:10 +00:00
|
|
|
static int snd_pcm_copy_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
|
2000-12-11 11:16:07 +00:00
|
|
|
{
|
|
|
|
|
snd_pcm_copy_t *copy = pcm->private;
|
2000-12-21 20:44:10 +00:00
|
|
|
snd_pcm_t *slave = copy->plug.slave;
|
2000-12-11 11:16:07 +00:00
|
|
|
int err;
|
2001-01-15 11:06:53 +00:00
|
|
|
unsigned int cmask, lcmask;
|
2000-12-21 20:44:10 +00:00
|
|
|
snd_pcm_hw_params_t sparams;
|
|
|
|
|
mask_t *access_mask = alloca(mask_sizeof());
|
|
|
|
|
mask_t *saccess_mask = alloca(mask_sizeof());
|
|
|
|
|
mask_load(access_mask, SND_PCM_ACCBIT_PLUGIN);
|
|
|
|
|
mask_load(saccess_mask, SND_PCM_ACCBIT_MMAP);
|
2001-01-15 11:06:53 +00:00
|
|
|
cmask = params->cmask;
|
|
|
|
|
params->cmask = 0;
|
|
|
|
|
err = _snd_pcm_hw_param_mask(params, SND_PCM_HW_PARAM_ACCESS,
|
|
|
|
|
access_mask);
|
2000-12-11 11:16:07 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
2001-01-15 11:06:53 +00:00
|
|
|
lcmask = params->cmask;
|
|
|
|
|
params->cmask |= cmask;
|
2000-12-21 20:44:10 +00:00
|
|
|
_snd_pcm_hw_params_any(&sparams);
|
2001-01-15 11:06:53 +00:00
|
|
|
_snd_pcm_hw_param_mask(&sparams, SND_PCM_HW_PARAM_ACCESS,
|
2000-12-21 20:44:10 +00:00
|
|
|
saccess_mask);
|
|
|
|
|
err = snd_pcm_hw_refine2(params, &sparams,
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_generic_hw_link, slave,
|
2000-12-21 20:44:10 +00:00
|
|
|
~SND_PCM_HW_PARBIT_ACCESS);
|
2001-01-15 11:06:53 +00:00
|
|
|
params->cmask |= lcmask;
|
2000-12-21 20:44:10 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
|
|
|
|
params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
|
|
|
|
|
return err;
|
2000-12-11 11:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_copy_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_copy_t *copy = pcm->private;
|
|
|
|
|
snd_pcm_t *slave = copy->plug.slave;
|
|
|
|
|
int err;
|
2001-01-15 11:06:53 +00:00
|
|
|
unsigned int links;
|
2000-12-21 20:44:10 +00:00
|
|
|
snd_pcm_hw_params_t sparams;
|
|
|
|
|
mask_t *saccess_mask = alloca(mask_sizeof());
|
|
|
|
|
mask_load(saccess_mask, SND_PCM_ACCBIT_MMAP);
|
|
|
|
|
|
|
|
|
|
_snd_pcm_hw_params_any(&sparams);
|
2001-01-15 11:06:53 +00:00
|
|
|
_snd_pcm_hw_param_mask(&sparams, SND_PCM_HW_PARAM_ACCESS,
|
2000-12-21 20:44:10 +00:00
|
|
|
saccess_mask);
|
2001-01-15 11:06:53 +00:00
|
|
|
links = ~SND_PCM_HW_PARBIT_ACCESS;
|
|
|
|
|
err = snd_pcm_hw_params_refine(&sparams, links, params);
|
|
|
|
|
assert(err >= 0);
|
|
|
|
|
err = snd_pcm_hw_params(slave, &sparams);
|
|
|
|
|
params->cmask = 0;
|
|
|
|
|
sparams.cmask = ~0U;
|
|
|
|
|
snd_pcm_hw_params_refine(params, links, &sparams);
|
2000-12-21 20:44:10 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
|
|
|
|
params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
|
2000-12-11 11:16:07 +00:00
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2001-01-15 11:06:53 +00:00
|
|
|
static snd_pcm_sframes_t snd_pcm_copy_write_areas(snd_pcm_t *pcm,
|
2000-12-11 11:16:07 +00:00
|
|
|
const snd_pcm_channel_area_t *areas,
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_uframes_t offset,
|
|
|
|
|
snd_pcm_uframes_t size,
|
|
|
|
|
snd_pcm_uframes_t *slave_sizep)
|
2000-12-11 11:16:07 +00:00
|
|
|
{
|
|
|
|
|
snd_pcm_copy_t *copy = pcm->private;
|
|
|
|
|
snd_pcm_t *slave = copy->plug.slave;
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_uframes_t xfer = 0;
|
|
|
|
|
snd_pcm_sframes_t err = 0;
|
2000-12-11 11:16:07 +00:00
|
|
|
if (slave_sizep && *slave_sizep < size)
|
|
|
|
|
size = *slave_sizep;
|
|
|
|
|
assert(size > 0);
|
|
|
|
|
while (xfer < size) {
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_uframes_t frames = snd_pcm_mmap_playback_xfer(slave, size - xfer);
|
2000-12-11 11:16:07 +00:00
|
|
|
|
|
|
|
|
snd_pcm_areas_copy(areas, offset,
|
|
|
|
|
snd_pcm_mmap_areas(slave), snd_pcm_mmap_offset(slave),
|
|
|
|
|
pcm->channels, frames, pcm->format);
|
|
|
|
|
err = snd_pcm_mmap_forward(slave, frames);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
break;
|
2001-01-15 11:06:53 +00:00
|
|
|
assert((snd_pcm_uframes_t)err == frames);
|
2000-12-11 11:16:07 +00:00
|
|
|
offset += err;
|
|
|
|
|
xfer += err;
|
|
|
|
|
snd_pcm_mmap_hw_forward(pcm, err);
|
|
|
|
|
}
|
|
|
|
|
if (xfer > 0) {
|
|
|
|
|
if (slave_sizep)
|
|
|
|
|
*slave_sizep = xfer;
|
|
|
|
|
return xfer;
|
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2001-01-15 11:06:53 +00:00
|
|
|
static snd_pcm_sframes_t snd_pcm_copy_read_areas(snd_pcm_t *pcm,
|
2000-12-11 11:16:07 +00:00
|
|
|
const snd_pcm_channel_area_t *areas,
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_uframes_t offset,
|
|
|
|
|
snd_pcm_uframes_t size,
|
|
|
|
|
snd_pcm_uframes_t *slave_sizep)
|
2000-12-11 11:16:07 +00:00
|
|
|
{
|
|
|
|
|
snd_pcm_copy_t *copy = pcm->private;
|
|
|
|
|
snd_pcm_t *slave = copy->plug.slave;
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_uframes_t xfer = 0;
|
|
|
|
|
snd_pcm_sframes_t err = 0;
|
2000-12-11 11:16:07 +00:00
|
|
|
if (slave_sizep && *slave_sizep < size)
|
|
|
|
|
size = *slave_sizep;
|
|
|
|
|
assert(size > 0);
|
|
|
|
|
while (xfer < size) {
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_uframes_t frames = snd_pcm_mmap_capture_xfer(slave, size - xfer);
|
2000-12-11 11:16:07 +00:00
|
|
|
snd_pcm_areas_copy(snd_pcm_mmap_areas(slave), snd_pcm_mmap_offset(slave),
|
|
|
|
|
areas, offset,
|
|
|
|
|
pcm->channels, frames, pcm->format);
|
|
|
|
|
err = snd_pcm_mmap_forward(slave, frames);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
break;
|
2001-01-15 11:06:53 +00:00
|
|
|
assert((snd_pcm_uframes_t)err == frames);
|
2000-12-11 11:16:07 +00:00
|
|
|
offset += err;
|
|
|
|
|
xfer += err;
|
|
|
|
|
snd_pcm_mmap_hw_forward(pcm, err);
|
|
|
|
|
}
|
|
|
|
|
if (xfer > 0) {
|
|
|
|
|
if (slave_sizep)
|
|
|
|
|
*slave_sizep = xfer;
|
|
|
|
|
return xfer;
|
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void snd_pcm_copy_dump(snd_pcm_t *pcm, FILE *fp)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_copy_t *copy = pcm->private;
|
|
|
|
|
fprintf(fp, "Copy conversion PCM\n");
|
|
|
|
|
if (pcm->setup) {
|
|
|
|
|
fprintf(fp, "Its setup is:\n");
|
|
|
|
|
snd_pcm_dump_setup(pcm, fp);
|
|
|
|
|
}
|
|
|
|
|
fprintf(fp, "Slave: ");
|
|
|
|
|
snd_pcm_dump(copy->plug.slave, fp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snd_pcm_ops_t snd_pcm_copy_ops = {
|
|
|
|
|
close: snd_pcm_plugin_close,
|
2000-12-22 14:03:01 +00:00
|
|
|
card: snd_pcm_plugin_card,
|
2000-12-11 11:16:07 +00:00
|
|
|
info: snd_pcm_plugin_info,
|
2000-12-21 20:44:10 +00:00
|
|
|
hw_refine: snd_pcm_copy_hw_refine,
|
2000-12-11 11:16:07 +00:00
|
|
|
hw_params: snd_pcm_copy_hw_params,
|
|
|
|
|
sw_params: snd_pcm_plugin_sw_params,
|
|
|
|
|
channel_info: snd_pcm_plugin_channel_info,
|
|
|
|
|
dump: snd_pcm_copy_dump,
|
|
|
|
|
nonblock: snd_pcm_plugin_nonblock,
|
|
|
|
|
async: snd_pcm_plugin_async,
|
|
|
|
|
mmap: snd_pcm_plugin_mmap,
|
|
|
|
|
munmap: snd_pcm_plugin_munmap,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int snd_pcm_copy_open(snd_pcm_t **pcmp, char *name, snd_pcm_t *slave, int close_slave)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_t *pcm;
|
|
|
|
|
snd_pcm_copy_t *copy;
|
|
|
|
|
assert(pcmp && slave);
|
|
|
|
|
copy = calloc(1, sizeof(snd_pcm_copy_t));
|
|
|
|
|
if (!copy) {
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
copy->plug.read = snd_pcm_copy_read_areas;
|
|
|
|
|
copy->plug.write = snd_pcm_copy_write_areas;
|
|
|
|
|
copy->plug.slave = slave;
|
|
|
|
|
copy->plug.close_slave = close_slave;
|
|
|
|
|
|
|
|
|
|
pcm = calloc(1, sizeof(snd_pcm_t));
|
|
|
|
|
if (!pcm) {
|
|
|
|
|
free(copy);
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
if (name)
|
|
|
|
|
pcm->name = strdup(name);
|
|
|
|
|
pcm->type = SND_PCM_TYPE_COPY;
|
|
|
|
|
pcm->stream = slave->stream;
|
|
|
|
|
pcm->mode = slave->mode;
|
|
|
|
|
pcm->ops = &snd_pcm_copy_ops;
|
|
|
|
|
pcm->op_arg = pcm;
|
|
|
|
|
pcm->fast_ops = &snd_pcm_plugin_fast_ops;
|
|
|
|
|
pcm->fast_op_arg = pcm;
|
|
|
|
|
pcm->private = copy;
|
|
|
|
|
pcm->poll_fd = slave->poll_fd;
|
|
|
|
|
pcm->hw_ptr = ©->plug.hw_ptr;
|
|
|
|
|
pcm->appl_ptr = ©->plug.appl_ptr;
|
|
|
|
|
*pcmp = pcm;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int _snd_pcm_copy_open(snd_pcm_t **pcmp, char *name,
|
|
|
|
|
snd_config_t *conf,
|
|
|
|
|
int stream, int mode)
|
|
|
|
|
{
|
|
|
|
|
snd_config_iterator_t i;
|
|
|
|
|
char *sname = NULL;
|
|
|
|
|
int err;
|
|
|
|
|
snd_pcm_t *spcm;
|
|
|
|
|
snd_config_foreach(i, conf) {
|
|
|
|
|
snd_config_t *n = snd_config_entry(i);
|
|
|
|
|
if (strcmp(n->id, "comment") == 0)
|
|
|
|
|
continue;
|
|
|
|
|
if (strcmp(n->id, "type") == 0)
|
|
|
|
|
continue;
|
|
|
|
|
if (strcmp(n->id, "stream") == 0)
|
|
|
|
|
continue;
|
|
|
|
|
if (strcmp(n->id, "sname") == 0) {
|
|
|
|
|
err = snd_config_string_get(n, &sname);
|
|
|
|
|
if (err < 0) {
|
|
|
|
|
ERR("Invalid type for %s", n->id);
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ERR("Unknown field %s", n->id);
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
if (!sname) {
|
|
|
|
|
ERR("sname is not defined");
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
/* This is needed cause snd_config_update may destroy config */
|
|
|
|
|
sname = strdup(sname);
|
|
|
|
|
if (!sname)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
err = snd_pcm_open(&spcm, sname, stream, mode);
|
|
|
|
|
free(sname);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
|
|
|
|
err = snd_pcm_copy_open(pcmp, name, spcm, 1);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
snd_pcm_close(spcm);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|