stream: remove conversion

Improve audioconvert to also split the output.
Remove the format conversion from the stream and move into the
server client-stream.
This commit is contained in:
Wim Taymans 2018-06-15 11:31:42 +02:00
parent acfd07c504
commit 4ac21aea53
6 changed files with 478 additions and 514 deletions

View file

@ -38,6 +38,7 @@
#define NAME "audioconvert"
#define MAX_BUFFERS 32
#define MAX_PORTS 128
#define PROP_DEFAULT_TRUNCATE false
#define PROP_DEFAULT_DITHER 0
@ -99,17 +100,6 @@ struct buffer {
struct spa_meta_header *h;
};
struct port {
uint32_t id;
struct spa_io_buffers *io;
bool have_format;
struct spa_audio_info format;
struct spa_node *node;
};
struct link {
struct spa_node *out_node;
uint32_t out_port;
@ -137,30 +127,22 @@ struct impl {
const struct spa_node_callbacks *callbacks;
void *user_data;
struct port in_port;
struct port out_port;
int n_links;
struct link links[8];
int n_nodes;
struct spa_node *nodes[8];
bool started;
struct spa_handle *hnd_fmt_in;
struct spa_handle *hnd_fmt[2];
struct spa_handle *hnd_channelmix;
struct spa_handle *hnd_resample;
struct spa_handle *hnd_fmt_out;
struct spa_node *fmt_in;
struct spa_node *fmt[2];
struct spa_node *channelmix;
struct spa_node *resample;
struct spa_node *fmt_out;
};
#define CHECK_PORT(this,d,id) (id == 0)
#define GET_IN_PORT(this,id) (&this->in_port)
#define GET_OUT_PORT(this,id) (&this->out_port)
#define GET_PORT(this,d,id) (d == SPA_DIRECTION_INPUT ? GET_IN_PORT(this,id) : GET_OUT_PORT(this,id))
static int make_link(struct impl *this,
struct spa_node *out_node, uint32_t out_port,
struct spa_node *in_node, uint32_t in_port,
@ -219,6 +201,42 @@ static void clean_link(struct impl *this, struct link *link)
link->buffers = NULL;
}
static int debug_params(struct impl *this, struct spa_node *node,
enum spa_direction direction, uint32_t port_id, uint32_t id, struct spa_pod *filter)
{
struct type *t = &this->type;
struct spa_pod_builder b = { 0 };
uint8_t buffer[4096];
uint32_t state, flag;
struct spa_pod *format;
int res;
flag = 0;
if (id == t->param.idEnumFormat)
flag |= SPA_DEBUG_FLAG_FORMAT;
spa_log_error(this->log, "formats:");
state = 0;
while (true) {
spa_pod_builder_init(&b, buffer, sizeof(buffer));
res = spa_node_port_enum_params(node,
direction, port_id,
id, &state,
NULL, &format, &b);
if (res <= 0)
break;
spa_debug_pod(format, flag);
}
spa_log_error(this->log, "failed filter:");
if (filter)
spa_debug_pod(filter, flag);
return 0;
}
static int negotiate_link_format(struct impl *this, struct link *link)
{
struct type *t = &this->type;
@ -250,8 +268,11 @@ static int negotiate_link_format(struct impl *this, struct link *link)
if ((res = spa_node_port_enum_params(link->out_node,
SPA_DIRECTION_OUTPUT, link->out_port,
t->param.idEnumFormat, &state,
filter, &format, &b)) <= 0)
filter, &format, &b)) <= 0) {
debug_params(this, link->out_node, SPA_DIRECTION_OUTPUT, link->out_port,
t->param.idEnumFormat, filter);
return -ENOTSUP;
}
filter = format;
}
@ -260,13 +281,16 @@ static int negotiate_link_format(struct impl *this, struct link *link)
if ((res = spa_node_port_enum_params(link->in_node,
SPA_DIRECTION_INPUT, link->in_port,
t->param.idEnumFormat, &state,
filter, &format, &b)) <= 0)
filter, &format, &b)) <= 0) {
debug_params(this, link->in_node, SPA_DIRECTION_INPUT, link->in_port,
t->param.idEnumFormat, filter);
return -ENOTSUP;
}
filter = format;
}
spa_pod_fixate(filter);
spa_debug_pod(filter, SPA_DEBUG_FLAG_FORMAT);
if (link->out_node != NULL) {
if ((res = spa_node_port_set_param(link->out_node,
@ -289,53 +313,34 @@ static int negotiate_link_format(struct impl *this, struct link *link)
static int setup_convert(struct impl *this)
{
struct port *inport, *outport;
struct spa_node *prev = NULL;
int i, j, res;
struct type *t = &this->type;
inport = GET_PORT(this, SPA_DIRECTION_INPUT, 0);
outport = GET_PORT(this, SPA_DIRECTION_OUTPUT, 0);
struct spa_audio_info informat, outformat;
spa_log_info(this->log, NAME " %p: %d/%d@%d.%d->%d/%d@%d.%d", this,
inport->format.info.raw.format,
inport->format.info.raw.channels,
inport->format.info.raw.rate,
inport->format.info.raw.layout,
outport->format.info.raw.format,
outport->format.info.raw.channels,
outport->format.info.raw.rate,
outport->format.info.raw.layout);
informat.info.raw.format,
informat.info.raw.channels,
informat.info.raw.rate,
informat.info.raw.layout,
outformat.info.raw.format,
outformat.info.raw.channels,
outformat.info.raw.rate,
outformat.info.raw.layout);
if (this->n_links > 0)
return 0;
this->n_nodes = 0;
/* unpack */
make_link(this, NULL, 0, this->fmt_in, 0, &inport->format);
prev = this->fmt_in;
this->nodes[this->n_nodes++] = this->fmt[SPA_DIRECTION_INPUT];
/* down mix */
if (inport->format.info.raw.channels > outport->format.info.raw.channels) {
make_link(this, prev, 0, this->channelmix, 0, NULL);
prev = this->channelmix;
}
this->nodes[this->n_nodes++] = this->channelmix;
/* resample */
if (inport->format.info.raw.rate != outport->format.info.raw.rate) {
make_link(this, prev, 0, this->resample, 0, NULL);
prev = this->resample;
}
/* up mix */
if (inport->format.info.raw.channels < outport->format.info.raw.channels) {
make_link(this, prev, 0, this->channelmix, 0, NULL);
prev = this->channelmix;
}
make_link(this, prev, 0, this->fmt_out, 0, NULL);
this->nodes[this->n_nodes++] = this->resample;
/* pack */
make_link(this, this->fmt_out, 0, NULL, 0, &outport->format);
this->nodes[this->n_nodes++] = this->fmt[SPA_DIRECTION_OUTPUT];
for (i = 0; i < this->n_nodes - 1; i++)
make_link(this, this->nodes[i], 0, this->nodes[i+1], 0, NULL);
for (i = 0, j = this->n_links - 1; j >= i; i++, j--) {
if ((res = negotiate_link_format(this, &this->links[i])) < 0)
@ -343,13 +348,6 @@ static int setup_convert(struct impl *this)
if ((res = negotiate_link_format(this, &this->links[j])) < 0)
return res;
}
spa_node_port_set_io(inport->node, SPA_DIRECTION_INPUT, 0,
t->io.Buffers, inport->io, sizeof(struct spa_io_buffers));
spa_node_port_set_io(outport->node, SPA_DIRECTION_OUTPUT, 0,
t->io.Buffers, outport->io, sizeof(struct spa_io_buffers));
return 0;
}
@ -374,16 +372,22 @@ static int negotiate_link_buffers(struct impl *this, struct link *link)
if ((res = spa_node_port_enum_params(link->out_node,
SPA_DIRECTION_OUTPUT, link->out_port,
t->param.idBuffers, &state,
param, &param, &b)) <= 0)
param, &param, &b)) <= 0) {
debug_params(this, link->out_node, SPA_DIRECTION_OUTPUT, link->out_port,
t->param.idBuffers, param);
return -ENOTSUP;
}
}
if (link->in_node != NULL) {
state = 0;
if ((res = spa_node_port_enum_params(link->in_node,
SPA_DIRECTION_INPUT, link->in_port,
t->param.idBuffers, &state,
param, &param, &b)) <= 0)
param, &param, &b)) <= 0) {
debug_params(this, link->in_node, SPA_DIRECTION_INPUT, link->in_port,
t->param.idBuffers, param);
return -ENOTSUP;
}
}
spa_pod_fixate(param);
@ -415,6 +419,9 @@ static int negotiate_link_buffers(struct impl *this, struct link *link)
NULL) < 0)
return -EINVAL;
spa_log_debug(this->log, "%p: buffers %d, blocks %d, size %d, align %d",
this, buffers, blocks, size, align);
datas = alloca(sizeof(struct spa_data) * blocks);
memset(datas, 0, sizeof(struct spa_data) * blocks);
aligns = alloca(sizeof(uint32_t) * blocks);
@ -480,13 +487,13 @@ static int setup_buffers(struct impl *this, enum spa_direction direction)
spa_log_debug(this->log, NAME " %p: %d", this, direction);
if (direction == SPA_DIRECTION_INPUT) {
for (i = 1; i < this->n_links-1; i++) {
for (i = 0; i < this->n_links; i++) {
if ((res = negotiate_link_buffers(this, &this->links[i])) < 0)
spa_log_error(this->log, NAME " %p: buffers %d failed %s",
this, i, spa_strerror(res));
}
} else {
for (i = this->n_links-2; i > 0 ; i--) {
for (i = this->n_links-1; i >= 0 ; i--) {
if ((res = negotiate_link_buffers(this, &this->links[i])) < 0)
spa_log_error(this->log, NAME " %p: buffers %d failed %s",
this, i, spa_strerror(res));
@ -514,6 +521,7 @@ static int impl_node_set_param(struct spa_node *node, uint32_t id, uint32_t flag
static int impl_node_send_command(struct spa_node *node, const struct spa_command *command)
{
struct impl *this;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(command != NULL, -EINVAL);
@ -521,6 +529,9 @@ static int impl_node_send_command(struct spa_node *node, const struct spa_comman
this = SPA_CONTAINER_OF(node, struct impl, node);
if (SPA_COMMAND_TYPE(command) == this->type.command_node.Start) {
if ((res = setup_convert(this)) < 0)
goto error;
setup_buffers(this, SPA_DIRECTION_INPUT);
this->started = true;
} else if (SPA_COMMAND_TYPE(command) == this->type.command_node.Pause) {
this->started = false;
@ -528,6 +539,10 @@ static int impl_node_send_command(struct spa_node *node, const struct spa_comman
return -ENOTSUP;
return 0;
error:
spa_log_error(this->log, "error %s", spa_strerror(res));
return res;
}
static int
@ -554,16 +569,14 @@ impl_node_get_n_ports(struct spa_node *node,
uint32_t *n_output_ports,
uint32_t *max_output_ports)
{
struct impl *this;
spa_return_val_if_fail(node != NULL, -EINVAL);
if (n_input_ports)
*n_input_ports = 1;
if (max_input_ports)
*max_input_ports = 1;
if (n_output_ports)
*n_output_ports = 1;
if (max_output_ports)
*max_output_ports = 1;
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_node_get_n_ports(this->fmt[SPA_DIRECTION_INPUT], n_input_ports, max_input_ports, NULL, NULL);
spa_node_get_n_ports(this->fmt[SPA_DIRECTION_OUTPUT], NULL, NULL, n_output_ports, max_output_ports);
return 0;
}
@ -575,25 +588,39 @@ impl_node_get_port_ids(struct spa_node *node,
uint32_t *output_ids,
uint32_t n_output_ids)
{
struct impl *this;
spa_return_val_if_fail(node != NULL, -EINVAL);
if (n_input_ids && input_ids)
input_ids[0] = 0;
if (n_output_ids > 0 && output_ids)
output_ids[0] = 0;
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_node_get_port_ids(this->fmt[SPA_DIRECTION_INPUT], input_ids, n_input_ids, NULL, 0);
spa_node_get_port_ids(this->fmt[SPA_DIRECTION_OUTPUT], NULL, 0, output_ids, n_output_ids);
return 0;
}
static int impl_node_add_port(struct spa_node *node, enum spa_direction direction, uint32_t port_id)
{
return -ENOTSUP;
struct impl *this;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
return spa_node_add_port(this->fmt[direction], direction, port_id);
}
static int
impl_node_remove_port(struct spa_node *node, enum spa_direction direction, uint32_t port_id)
{
return -ENOTSUP;
struct impl *this;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
return spa_node_remove_port(this->fmt[direction], direction, port_id);
}
static int
@ -603,18 +630,13 @@ impl_node_port_get_info(struct spa_node *node,
const struct spa_port_info **info)
{
struct impl *this;
struct port *port;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(info != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
port = GET_PORT(this, direction, port_id);
return spa_node_port_get_info(port->node, direction, port_id, info);
return spa_node_port_get_info(this->fmt[direction], direction, port_id, info);
}
static int
@ -626,61 +648,13 @@ impl_node_port_enum_params(struct spa_node *node,
struct spa_pod_builder *builder)
{
struct impl *this;
struct port *port;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
port = GET_PORT(this, direction, port_id);
return spa_node_port_enum_params(port->node, direction, port_id, id, index,
filter, result, builder);
}
static int port_set_format(struct spa_node *node,
enum spa_direction direction,
uint32_t port_id,
uint32_t flags,
const struct spa_pod *format)
{
struct impl *this = SPA_CONTAINER_OF(node, struct impl, node);
struct port *port, *other;
struct type *t = &this->type;
int res = 0;
port = GET_PORT(this, direction, port_id);
other = GET_PORT(this, SPA_DIRECTION_REVERSE(direction), port_id);
if (format == NULL) {
clean_convert(this);
port->have_format = false;
} else {
struct spa_audio_info info = { 0 };
spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype);
if (info.media_type != t->media_type.audio ||
info.media_subtype != t->media_subtype.raw)
return -EINVAL;
if (spa_format_audio_raw_parse(format, &info.info.raw, &t->format_audio) < 0)
return -EINVAL;
clean_convert(this);
port->have_format = true;
port->format = info;
if (other->have_format)
res = setup_convert(this);
spa_log_debug(this->log, NAME " %p: set format on port %d %d", this, port_id, res);
}
return res;
return spa_node_port_enum_params(this->fmt[direction], direction, port_id,
id, index, filter, result, builder);
}
static int
@ -690,22 +664,12 @@ impl_node_port_set_param(struct spa_node *node,
const struct spa_pod *param)
{
struct impl *this;
struct port *port;
struct type *t;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
t = &this->type;
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
port = GET_PORT(this, direction, port_id);
if (id == t->param.idFormat)
return port_set_format(node, direction, port_id, flags, param);
else
return spa_node_port_set_param(port->node, direction, port_id, id, flags, param);
return spa_node_port_set_param(this->fmt[direction], direction, port_id, id, flags, param);
}
static int
@ -716,22 +680,12 @@ impl_node_port_use_buffers(struct spa_node *node,
uint32_t n_buffers)
{
struct impl *this;
struct port *port;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
port = GET_PORT(this, direction, port_id);
res = spa_node_port_use_buffers(port->node, direction, port_id, buffers, n_buffers);
if (res < 0)
return res;
return setup_buffers(this, direction);
return spa_node_port_use_buffers(this->fmt[direction], direction, port_id, buffers, n_buffers);
}
static int
@ -744,17 +698,12 @@ impl_node_port_alloc_buffers(struct spa_node *node,
uint32_t *n_buffers)
{
struct impl *this;
struct port *port;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
port = GET_PORT(this, direction, port_id);
return spa_node_port_alloc_buffers(port->node, direction, port_id,
return spa_node_port_alloc_buffers(this->fmt[direction], direction, port_id,
params, n_params, buffers, n_buffers);
}
@ -764,38 +713,23 @@ impl_node_port_set_io(struct spa_node *node,
uint32_t id, void *data, size_t size)
{
struct impl *this;
struct port *port;
struct type *t;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
t = &this->type;
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
port = GET_PORT(this, direction, port_id);
if (id == t->io.Buffers)
port->io = data;
return spa_node_port_set_io(port->node, direction, port_id, id, data, size);
return spa_node_port_set_io(this->fmt[direction], direction, port_id, id, data, size);
}
static int impl_node_port_reuse_buffer(struct spa_node *node, uint32_t port_id, uint32_t buffer_id)
{
struct impl *this;
struct port *port;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_return_val_if_fail(CHECK_PORT(this, SPA_DIRECTION_OUTPUT, port_id), -EINVAL);
port = GET_PORT(this, SPA_DIRECTION_OUTPUT, port_id);
return spa_node_port_reuse_buffer(port->node, port_id, buffer_id);
return spa_node_port_reuse_buffer(this->fmt[SPA_DIRECTION_OUTPUT], port_id, buffer_id);
}
static int
@ -805,23 +739,18 @@ impl_node_port_send_command(struct spa_node *node,
const struct spa_command *command)
{
struct impl *this;
struct port *port;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_return_val_if_fail(CHECK_PORT(this, SPA_DIRECTION_OUTPUT, port_id), -EINVAL);
port = GET_PORT(this, direction, port_id);
return spa_node_port_send_command(port->node, direction, port_id, command);
return spa_node_port_send_command(this->fmt[direction], direction, port_id, command);
}
static int impl_node_process(struct spa_node *node)
{
struct impl *this;
int i, res = SPA_STATUS_OK;
int r, i, res = SPA_STATUS_OK;
spa_return_val_if_fail(node != NULL, -EINVAL);
@ -829,17 +758,20 @@ static int impl_node_process(struct spa_node *node)
spa_log_trace(this->log, NAME " %p: process %d", this, this->n_links);
for (i = 1; i < this->n_links; i++) {
int r = spa_node_process(this->links[i].out_node);
if (i == 1)
for (i = 0; i < this->n_nodes; i++) {
r = spa_node_process(this->nodes[i]);
spa_log_trace(this->log, NAME " %p: process %d %d", this, i, r);
if (i == 0)
res |= r & SPA_STATUS_NEED_BUFFER;
if (i == this->n_links - 1)
if (i == this->n_nodes-1)
res |= r & SPA_STATUS_HAVE_BUFFER;
if (!SPA_FLAG_CHECK(r, SPA_STATUS_HAVE_BUFFER)) {
if (SPA_FLAG_CHECK(r, SPA_STATUS_NEED_BUFFER) && i == 1)
if (SPA_FLAG_CHECK(r, SPA_STATUS_NEED_BUFFER) && i == 0)
break;
i = res = SPA_STATUS_OK;
res = SPA_STATUS_OK;
i = -1;
continue;
}
}
@ -926,7 +858,6 @@ impl_init(const struct spa_handle_factory *factory,
uint32_t n_support)
{
struct impl *this;
struct port *port;
uint32_t i;
size_t size;
void *iface;
@ -953,47 +884,39 @@ impl_init(const struct spa_handle_factory *factory,
this->node = impl_node;
this->hnd_fmt_in = SPA_MEMBER(this, sizeof(struct impl), struct spa_handle);
this->hnd_fmt[SPA_DIRECTION_INPUT] = SPA_MEMBER(this, sizeof(struct impl), struct spa_handle);
spa_handle_factory_init(&spa_fmtconvert_factory,
this->hnd_fmt_in,
this->hnd_fmt[SPA_DIRECTION_INPUT],
info, support, n_support);
size = spa_handle_factory_get_size(&spa_fmtconvert_factory, info);
this->hnd_channelmix = SPA_MEMBER(this->hnd_fmt_in, size, struct spa_handle);
this->hnd_channelmix = SPA_MEMBER(this->hnd_fmt[SPA_DIRECTION_INPUT], size, struct spa_handle);
spa_handle_factory_init(&spa_channelmix_factory,
this->hnd_channelmix,
info, support, n_support);
size = spa_handle_factory_get_size(&spa_channelmix_factory, info);
this->hnd_fmt_out = SPA_MEMBER(this->hnd_channelmix, size, struct spa_handle);
this->hnd_fmt[SPA_DIRECTION_OUTPUT] = SPA_MEMBER(this->hnd_channelmix, size, struct spa_handle);
spa_handle_factory_init(&spa_fmtconvert_factory,
this->hnd_fmt_out,
this->hnd_fmt[SPA_DIRECTION_OUTPUT],
info, support, n_support);
size = spa_handle_factory_get_size(&spa_fmtconvert_factory, info);
this->hnd_resample = SPA_MEMBER(this->hnd_fmt_out, size, struct spa_handle);
this->hnd_resample = SPA_MEMBER(this->hnd_fmt[SPA_DIRECTION_OUTPUT], size, struct spa_handle);
spa_handle_factory_init(&spa_resample_factory,
this->hnd_resample,
info, support, n_support);
size = spa_handle_factory_get_size(&spa_resample_factory, info);
spa_handle_get_interface(this->hnd_fmt_in, this->type.node, &iface);
this->fmt_in = iface;
spa_handle_get_interface(this->hnd_fmt_out, this->type.node, &iface);
this->fmt_out = iface;
spa_handle_get_interface(this->hnd_fmt[SPA_DIRECTION_INPUT], this->type.node, &iface);
this->fmt[SPA_DIRECTION_INPUT] = iface;
spa_handle_get_interface(this->hnd_fmt[SPA_DIRECTION_OUTPUT], this->type.node, &iface);
this->fmt[SPA_DIRECTION_OUTPUT] = iface;
spa_handle_get_interface(this->hnd_channelmix, this->type.node, &iface);
this->channelmix = iface;
spa_handle_get_interface(this->hnd_resample, this->type.node, &iface);
this->resample = iface;
port = GET_OUT_PORT(this, 0);
port->id = 0;
port->node = this->fmt_out;
port = GET_IN_PORT(this, 0);
port->id = 0;
port->node = this->fmt_in;
props_reset(&this->props);
return 0;