stream: more work on converter

This commit is contained in:
Wim Taymans 2018-04-06 18:39:07 +02:00
parent 19067dde17
commit 142ef38df9
10 changed files with 2746 additions and 212 deletions

View file

@ -24,6 +24,7 @@
#include <time.h>
#include <spa/support/type-map.h>
#include <spa/buffer/alloc.h>
#include <spa/param/format-utils.h>
#include <spa/param/audio/format-utils.h>
#include <spa/param/props.h>
@ -119,9 +120,7 @@ struct stream {
struct data data;
bool use_converter;
struct spa_node *fmtconvert;
struct spa_node *resample;
struct spa_node *remix;
struct spa_node *convert;
struct spa_io_buffers conv_io;
};
@ -182,8 +181,11 @@ static int configure_converter(struct stream *impl)
}
}
if (impl->convert == NULL)
return -ENOTSUP;
/* configure the converter */
if ((res = spa_node_port_set_param(impl->fmtconvert,
if ((res = spa_node_port_set_param(impl->convert,
impl->direction, 0,
t->param.idFormat, 0,
impl->format)) < 0)
@ -195,7 +197,7 @@ static int configure_converter(struct stream *impl)
param = impl->init_params[i];
if (spa_pod_is_object_type(param, t->spa_format)) {
if ((res = spa_node_port_set_param(impl->fmtconvert,
if ((res = spa_node_port_set_param(impl->convert,
SPA_DIRECTION_REVERSE(impl->direction), 0,
t->param.idFormat,
SPA_NODE_PARAM_FLAG_FIXATE,
@ -211,12 +213,12 @@ static int configure_converter(struct stream *impl)
if (!impl->use_converter)
return -ENOTSUP;
res = spa_node_port_set_io(impl->fmtconvert,
res = spa_node_port_set_io(impl->convert,
impl->direction, 0,
t->io.Buffers,
impl->io, sizeof(struct spa_io_buffers));
impl->io = &impl->conv_io;
res = spa_node_port_set_io(impl->fmtconvert,
res = spa_node_port_set_io(impl->convert,
SPA_DIRECTION_REVERSE(impl->direction), 0,
t->io.Buffers,
impl->io, sizeof(struct spa_io_buffers));
@ -367,9 +369,9 @@ static int impl_port_set_io(struct spa_node *node, enum spa_direction direction,
if (impl->use_converter) {
impl->io = &impl->conv_io;
res = spa_node_port_set_io(impl->fmtconvert,
res = spa_node_port_set_io(impl->convert,
direction, 0, id, data, size);
res = spa_node_port_set_io(impl->fmtconvert,
res = spa_node_port_set_io(impl->convert,
SPA_DIRECTION_REVERSE(direction), 0,
id, impl->io, size);
}
@ -564,81 +566,6 @@ static void clear_buffers(struct pw_stream *stream)
spa_ringbuffer_init(&impl->queued.ring);
}
static struct spa_buffer ** alloc_buffers(struct stream *impl,
uint32_t n_buffers,
uint32_t n_metas,
uint32_t meta_sizes[n_metas],
uint32_t n_datas,
uint32_t data_sizes[n_datas])
{
struct spa_buffer **buffers;
size_t skel_size, data_size = 0;
struct spa_buffer *bp, *b;
void *dp, *d, *ddp;
struct spa_chunk *cdp;
int i, j;
struct pw_type *t = impl->t;
skel_size = sizeof(struct spa_buffer *);
skel_size += sizeof(struct spa_buffer);
skel_size += n_metas * sizeof(struct spa_meta);
for (i = 0; i < n_metas; i++)
data_size += meta_sizes[i];
skel_size += n_datas * sizeof(struct spa_data);
data_size += n_datas * sizeof(struct spa_chunk);
for (i = 0; i < n_datas; i++)
data_size += data_sizes[i];
buffers = malloc((skel_size + data_size) * n_buffers);
bp = SPA_MEMBER(buffers, n_buffers * sizeof(struct spa_buffer *), struct spa_buffer);
dp = SPA_MEMBER(bp, n_buffers * skel_size, void);
for (i = 0; i < n_buffers; i++) {
b = SPA_MEMBER(bp, skel_size * i, struct spa_buffer);
d = SPA_MEMBER(dp, data_size * i, void);
buffers[i] = b;
b->id = i;
b->n_metas = n_metas;
b->metas = SPA_MEMBER(b, sizeof(struct spa_buffer), struct spa_meta);
for (j = 0; j < n_metas; j++) {
struct spa_meta *m = &b->metas[j];
m->size = meta_sizes[j];
m->data = d;
d += m->size;
}
b->n_datas = n_datas;
b->datas = SPA_MEMBER(b->metas, n_metas * sizeof(struct spa_meta), struct spa_data);
cdp = d;
ddp = SPA_MEMBER(cdp, n_datas * sizeof(struct spa_chunk), void);
for (j = 0; j < n_datas; j++) {
struct spa_data *d = &b->datas[j];
d->chunk = &cdp[j];
if (data_sizes[j] > 0) {
d->type = t->data.MemPtr;
d->flags = 0;
d->fd = -1;
d->mapoffset = 0;
d->maxsize = data_sizes[j];
d->data = ddp;
d->chunk->offset = 0;
d->chunk->size = data_sizes[j];
d->chunk->stride = 0;
ddp += data_sizes[j];
} else {
/* needs to be allocated by a node */
d->type = SPA_ID_INVALID;
d->data = NULL;
}
}
}
return buffers;
}
static int impl_port_use_buffers(struct spa_node *node, enum spa_direction direction, uint32_t port_id,
struct spa_buffer **buffers, uint32_t n_buffers)
{
@ -687,21 +614,27 @@ static int impl_port_use_buffers(struct spa_node *node, enum spa_direction direc
impl->n_buffers = n_buffers;
if (impl->use_converter) {
uint32_t data_sizes[1];
uint32_t data_sizes[1], data_aligns[1];
spa_node_port_use_buffers(impl->fmtconvert,
if ((res = spa_node_port_use_buffers(impl->convert,
impl->direction, 0,
buffers,
n_buffers);
n_buffers)) < 0)
return res;
data_sizes[0] = size * 2;
buffers = alloc_buffers(impl, n_buffers, 0, NULL, 1, data_sizes);
data_aligns[0] = 16;
spa_node_port_use_buffers(impl->fmtconvert,
buffers = spa_buffer_alloc_array(n_buffers, t->data.MemPtr,
0, NULL,
1, data_sizes, data_aligns);
if (buffers == NULL)
return -ENOMEM;
if ((res = spa_node_port_use_buffers(impl->convert,
SPA_DIRECTION_REVERSE(impl->direction), 0,
buffers,
n_buffers);
buffers, n_buffers)) < 0)
return res;
}
for (i = 0; i < n_buffers; i++) {
@ -777,7 +710,7 @@ static int impl_node_process_output(struct spa_node *node)
io->status = SPA_STATUS_HAVE_BUFFER;
if (impl->use_converter)
res = spa_node_process(impl->fmtconvert);
res = spa_node_process(impl->convert);
pw_log_trace("stream %p: pop %d %s", stream, b->id, spa_strerror(res));
} else {
@ -825,9 +758,6 @@ struct pw_stream * pw_stream_new(struct pw_remote *remote, const char *name,
if (props == NULL)
goto no_mem;
impl->fmtconvert = pw_load_spa_interface("audioconvert/libspa-audioconvert",
"fmtconvert", SPA_TYPE__Node, NULL, 0);
this->properties = props;
this->remote = remote;
@ -993,16 +923,20 @@ set_init_params(struct pw_stream *stream,
}
impl->n_orig_params = n_init_params;
if (add_audio) {
if (add_audio && !SPA_FLAG_CHECK(impl->flags, PW_STREAM_FLAG_NO_CONVERT)) {
uint32_t state = 0;
int res;
if ((impl->convert = pw_load_spa_interface("audioconvert/libspa-audioconvert",
"audioconvert", SPA_TYPE__Node, NULL, 0)) == NULL)
goto done;
while (true) {
uint8_t buffer[4096];
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, 4096);
struct spa_pod *param;
if ((res = spa_node_port_enum_params(impl->fmtconvert,
if ((res = spa_node_port_enum_params(impl->convert,
impl->direction, 0,
t->param.idEnumFormat, &state,
NULL, &param, &b)) <= 0)
@ -1013,6 +947,7 @@ set_init_params(struct pw_stream *stream,
impl->init_params[n_init_params++] = pw_spa_pod_copy(param);
}
}
done:
impl->n_init_params = n_init_params;
}