audiomixer: improve mixing

Remove PortIO flags, we can use the status
Move PortIO to ports
Move transport to client-node
Improve scheduling
This commit is contained in:
Wim Taymans 2017-04-12 10:40:17 +02:00
parent 9bd92b781c
commit 4c7b56020a
14 changed files with 398 additions and 300 deletions

View file

@ -58,6 +58,7 @@
typedef struct _SpaProxy SpaProxy;
typedef struct _ProxyBuffer ProxyBuffer;
typedef struct _PinosClientNodeImpl PinosClientNodeImpl;
struct _ProxyBuffer {
SpaBuffer *outbuf;
@ -88,7 +89,7 @@ struct _SpaProxy
{
SpaNode node;
PinosNode *pnode;
PinosClientNodeImpl *impl;
SpaTypeMap *map;
SpaLog *log;
@ -113,7 +114,7 @@ struct _SpaProxy
uint32_t seq;
};
typedef struct
struct _PinosClientNodeImpl
{
PinosClientNode this;
@ -121,13 +122,15 @@ typedef struct
SpaProxy proxy;
PinosTransport *transport;
PinosListener node_free;
PinosListener transport_changed;
PinosListener initialized;
PinosListener loop_changed;
PinosListener global_added;
int data_fd;
} PinosClientNodeImpl;
};
static SpaResult
clear_buffers (SpaProxy *this, SpaProxyPort *port)
@ -159,22 +162,22 @@ spa_proxy_node_set_props (SpaNode *node,
static void
send_need_input (SpaProxy *this)
{
PinosNode *pnode = this->pnode;
SpaEvent event = SPA_EVENT_INIT (pnode->core->type.event_node.NeedInput);
PinosClientNodeImpl *impl = SPA_CONTAINER_OF (this, PinosClientNodeImpl, proxy);
SpaEvent event = SPA_EVENT_INIT (impl->core->type.event_node.NeedInput);
uint64_t cmd = 1;
pinos_transport_add_event (pnode->transport, &event);
pinos_transport_add_event (impl->transport, &event);
write (this->data_source.fd, &cmd, 8);
}
static void
send_have_output (SpaProxy *this)
{
PinosNode *pnode = this->pnode;
SpaEvent event = SPA_EVENT_INIT (pnode->core->type.event_node.HaveOutput);
PinosClientNodeImpl *impl = SPA_CONTAINER_OF (this, PinosClientNodeImpl, proxy);
SpaEvent event = SPA_EVENT_INIT (impl->core->type.event_node.HaveOutput);
uint64_t cmd = 1;
pinos_transport_add_event (pnode->transport, &event);
pinos_transport_add_event (impl->transport, &event);
write (this->data_source.fd, &cmd, 8);
}
@ -194,7 +197,7 @@ spa_proxy_node_send_command (SpaNode *node,
if (this->resource == NULL)
return SPA_RESULT_OK;
core = this->pnode->core;
core = this->impl->core;
if (SPA_COMMAND_TYPE (command) == core->type.command_node.ClockUpdate) {
pinos_client_node_notify_node_command (this->resource,
@ -748,22 +751,22 @@ spa_proxy_node_port_reuse_buffer (SpaNode *node,
uint32_t buffer_id)
{
SpaProxy *this;
PinosNode *pnode;
PinosClientNodeImpl *impl;
if (node == NULL)
return SPA_RESULT_INVALID_ARGUMENTS;
this = SPA_CONTAINER_OF (node, SpaProxy, node);
pnode = this->pnode;
impl = this->impl;
if (!CHECK_OUT_PORT (this, SPA_DIRECTION_OUTPUT, port_id))
return SPA_RESULT_INVALID_PORT;
spa_log_trace (this->log, "reuse buffer %d", buffer_id);
{
SpaEventNodeReuseBuffer rb = SPA_EVENT_NODE_REUSE_BUFFER_INIT (pnode->core->type.event_node.ReuseBuffer,
SpaEventNodeReuseBuffer rb = SPA_EVENT_NODE_REUSE_BUFFER_INIT (impl->core->type.event_node.ReuseBuffer,
port_id, buffer_id);
pinos_transport_add_event (pnode->transport, (SpaEvent *)&rb);
pinos_transport_add_event (impl->transport, (SpaEvent *)&rb);
}
return SPA_RESULT_OK;
@ -789,13 +792,25 @@ spa_proxy_node_port_send_command (SpaNode *node,
static SpaResult
spa_proxy_node_process_input (SpaNode *node)
{
PinosClientNodeImpl *impl;
SpaProxy *this;
int i;
if (node == NULL)
return SPA_RESULT_INVALID_ARGUMENTS;
this = SPA_CONTAINER_OF (node, SpaProxy, node);
impl = this->impl;
for (i = 0; i < MAX_INPUTS; i++) {
SpaPortIO *io = this->in_ports[i].io;
if (!io)
continue;
impl->transport->inputs[i] = *io;
io->status = SPA_RESULT_OK;
}
send_have_output (this);
return SPA_RESULT_OK;
@ -805,21 +820,44 @@ static SpaResult
spa_proxy_node_process_output (SpaNode *node)
{
SpaProxy *this;
PinosClientNodeImpl *impl;
int i;
bool send_need = false;
if (node == NULL)
return SPA_RESULT_INVALID_ARGUMENTS;
this = SPA_CONTAINER_OF (node, SpaProxy, node);
impl = this->impl;
for (i = 0; i < MAX_OUTPUTS; i++) {
SpaPortIO *io = this->out_ports[i].io;
if (io && io->buffer_id != SPA_ID_INVALID) {
spa_proxy_node_port_reuse_buffer (node, i, io->buffer_id);
SpaPortIO *io = this->out_ports[i].io, tmp;
if (!io)
continue;
if (io->buffer_id != SPA_ID_INVALID) {
SpaEventNodeReuseBuffer rb =
SPA_EVENT_NODE_REUSE_BUFFER_INIT (impl->core->type.event_node.ReuseBuffer, i, io->buffer_id);
spa_log_trace (this->log, "reuse buffer %d", io->buffer_id);
pinos_transport_add_event (impl->transport, (SpaEvent *)&rb);
io->buffer_id = SPA_ID_INVALID;
}
tmp = impl->transport->outputs[i];
impl->transport->outputs[i] = *io;
pinos_log_trace ("%d %d %d %d", io->status, io->buffer_id, tmp.status, tmp.buffer_id);
if (io->status == SPA_RESULT_NEED_INPUT)
send_need = true;
*io = tmp;
}
send_need_input (this);
if (send_need)
send_need_input (this);
return SPA_RESULT_HAVE_OUTPUT;
}
@ -922,7 +960,7 @@ static void
proxy_on_data_fd_events (SpaSource *source)
{
SpaProxy *this = source->data;
PinosNode *pnode = this->pnode;
PinosClientNodeImpl *impl = this->impl;
if (source->rmask & (SPA_IO_ERR | SPA_IO_HUP)) {
spa_log_warn (this->log, "proxy %p: got error", this);
@ -935,9 +973,9 @@ proxy_on_data_fd_events (SpaSource *source)
read (this->data_source.fd, &cmd, 8);
while (pinos_transport_next_event (pnode->transport, &event) == SPA_RESULT_OK) {
while (pinos_transport_next_event (impl->transport, &event) == SPA_RESULT_OK) {
SpaEvent *ev = alloca (SPA_POD_SIZE (&event));
pinos_transport_parse_event (pnode->transport, ev);
pinos_transport_parse_event (impl->transport, ev);
this->event_cb (&this->node, ev, this->user_data);
}
}
@ -1004,17 +1042,22 @@ proxy_init (SpaProxy *this,
}
static void
on_transport_changed (PinosListener *listener,
PinosNode *node)
on_initialized (PinosListener *listener,
PinosNode *node)
{
PinosClientNodeImpl *impl = SPA_CONTAINER_OF (listener, PinosClientNodeImpl, transport_changed);
PinosClientNodeImpl *impl = SPA_CONTAINER_OF (listener, PinosClientNodeImpl, initialized);
PinosClientNode *this = &impl->this;
PinosTransportInfo info;
if (this->resource == NULL)
return;
pinos_transport_get_info (node->transport, &info);
impl->transport = pinos_transport_new (node->max_input_ports,
node->max_output_ports);
impl->transport->area->n_inputs = node->n_input_ports;
impl->transport->area->n_outputs = node->n_output_ports;
pinos_transport_get_info (impl->transport, &info);
pinos_client_node_notify_transport (this->resource,
info.memfd,
info.offset,
@ -1071,13 +1114,12 @@ client_node_resource_destroy (PinosResource *resource)
pinos_signal_remove (&impl->global_added);
pinos_signal_remove (&impl->loop_changed);
pinos_signal_remove (&impl->transport_changed);
pinos_signal_remove (&impl->initialized);
if (proxy->data_source.fd != -1) {
spa_loop_remove_source (proxy->data_loop, &proxy->data_source);
close (proxy->data_source.fd);
}
pinos_node_destroy (this->node);
}
@ -1092,6 +1134,9 @@ on_node_free (PinosListener *listener,
pinos_signal_remove (&impl->node_free);
if (impl->transport)
pinos_transport_destroy (impl->transport);
if (impl->data_fd != -1)
close (impl->data_fd);
free (impl);
@ -1140,7 +1185,7 @@ pinos_client_node_new (PinosClient *client,
if (this->node == NULL)
goto error_no_node;
impl->proxy.pnode = this->node;
impl->proxy.impl = impl;
this->resource = pinos_resource_new (client,
id,
@ -1156,9 +1201,9 @@ pinos_client_node_new (PinosClient *client,
&impl->node_free,
on_node_free);
pinos_signal_add (&this->node->transport_changed,
&impl->transport_changed,
on_transport_changed);
pinos_signal_add (&this->node->initialized,
&impl->initialized,
on_initialized);
pinos_signal_add (&this->node->loop_changed,
&impl->loop_changed,

View file

@ -62,15 +62,14 @@ update_port_ids (PinosNode *node)
&n_output_ports,
&max_output_ports);
node->transport = pinos_transport_new (max_input_ports,
max_output_ports);
node->n_input_ports = n_input_ports;
node->max_input_ports = max_input_ports;
node->n_output_ports = n_output_ports;
node->max_output_ports = max_output_ports;
node->input_port_map = calloc (max_input_ports, sizeof (PinosPort *));
node->output_port_map = calloc (max_output_ports, sizeof (PinosPort *));
node->transport->area->n_inputs = n_input_ports;
node->transport->area->n_outputs = n_output_ports;
input_port_ids = alloca (sizeof (uint32_t) * n_input_ports);
output_port_ids = alloca (sizeof (uint32_t) * n_output_ports);
@ -98,8 +97,7 @@ update_port_ids (PinosNode *node)
pinos_log_debug ("node %p: input port added %d", node, input_port_ids[i]);
np = pinos_port_new (node, PINOS_DIRECTION_INPUT, input_port_ids[i]);
np->io = &node->transport->inputs[np->port_id];
if ((res = spa_node_port_set_io (node->node, SPA_DIRECTION_INPUT, np->port_id, np->io)) < 0)
if ((res = spa_node_port_set_io (node->node, SPA_DIRECTION_INPUT, np->port_id, &np->io)) < 0)
pinos_log_warn ("node %p: can't set input IO %d", node, res);
spa_list_insert (ports, &np->link);
@ -137,8 +135,7 @@ update_port_ids (PinosNode *node)
pinos_log_debug ("node %p: output port added %d", node, output_port_ids[i]);
np = pinos_port_new (node, PINOS_DIRECTION_OUTPUT, output_port_ids[i]);
np->io = &node->transport->outputs[np->port_id];
if ((res = spa_node_port_set_io (node->node, SPA_DIRECTION_OUTPUT, np->port_id, np->io)) < 0)
if ((res = spa_node_port_set_io (node->node, SPA_DIRECTION_OUTPUT, np->port_id, &np->io)) < 0)
pinos_log_warn ("node %p: can't set output IO %d", node, res);
spa_list_insert (ports, &np->link);
@ -160,8 +157,7 @@ update_port_ids (PinosNode *node)
break;
}
}
pinos_signal_emit (&node->transport_changed, node);
pinos_signal_emit (&node->initialized, node);
}
static SpaResult
@ -257,6 +253,61 @@ send_clock_update (PinosNode *this)
pinos_log_debug ("got error %d", res);
}
static SpaResult
do_pull (PinosNode *this)
{
SpaResult res = SPA_RESULT_OK;
PinosPort *inport;
spa_list_for_each (inport, &this->input_ports, link) {
PinosLink *link;
PinosPort *outport;
SpaPortIO *pi;
SpaPortIO *po;
pi = &inport->io;
pinos_log_trace ("node %p: need input port %d, %d %d", this,
inport->port_id, pi->buffer_id, pi->status);
if (pi->status != SPA_RESULT_NEED_INPUT)
continue;
spa_list_for_each (link, &inport->rt.links, rt.input_link) {
if (link->rt.input == NULL || link->rt.output == NULL)
continue;
outport = link->rt.output;
po = &outport->io;
/* pull */
*po = *pi;
pinos_log_trace ("node %p: process output %p %d", outport->node, po, po->buffer_id);
res = spa_node_process_output (outport->node->node);
if (res == SPA_RESULT_NEED_INPUT) {
res = do_pull (outport->node);
*pi = *po;
pinos_log_trace ("node %p: pull return %d", outport->node, res);
}
else if (res == SPA_RESULT_HAVE_OUTPUT) {
*pi = *po;
}
else
pinos_log_warn ("node %p: got process output %d", outport->node, res);
}
if (pi->buffer_id != SPA_ID_INVALID) {
pinos_log_trace ("node %p: process input %d %d", this, pi->status, pi->buffer_id);
res = spa_node_process_input (this->node);
if (res == SPA_RESULT_HAVE_OUTPUT)
break;
}
}
return res;
}
static void
on_node_event (SpaNode *node, SpaEvent *event, void *user_data)
{
@ -272,99 +323,29 @@ on_node_event (SpaNode *node, SpaEvent *event, void *user_data)
}
}
else if (SPA_EVENT_TYPE (event) == this->core->type.event_node.NeedInput) {
SpaResult res;
int i;
this->sched_state = STATE_IN;
for (i = 0; i < this->transport->area->n_inputs; i++) {
PinosLink *link;
PinosPort *inport, *outport;
SpaPortIO *pi;
SpaPortIO *po;
pi = &this->transport->inputs[i];
pinos_log_trace ("node %p: need input port %d, %d", this, i, pi->buffer_id);
inport = this->input_port_map[i];
spa_list_for_each (link, &inport->rt.links, rt.input_link) {
if (link->rt.input == NULL || link->rt.output == NULL)
continue;
outport = link->rt.output;
po = &outport->node->transport->outputs[outport->port_id];
if (outport->node->sched_state == STATE_IN) {
/* pull */
*po = *pi;
pi->buffer_id = SPA_ID_INVALID;
pinos_log_trace ("node %p: process output %p %d", outport->node, po, po->buffer_id);
res = spa_node_process_output (outport->node->node);
if (res == SPA_RESULT_NEED_INPUT) {
on_node_event (outport->node->node, event, outport->node);
if (outport->node->sched_state == STATE_OUT)
goto push;
}
else if (res == SPA_RESULT_HAVE_OUTPUT) {
outport->node->sched_state = STATE_OUT;
}
else
pinos_log_warn ("node %p: got process output %d", outport->node, res);
} else {
/* push */
push:
*pi = *po;
pinos_log_trace ("node %p: process output %p %d", outport->node, po, po->buffer_id);
res = spa_node_process_output (outport->node->node);
if (res == SPA_RESULT_HAVE_OUTPUT)
outport->node->sched_state = STATE_OUT;
else if (res == SPA_RESULT_NEED_INPUT)
outport->node->sched_state = STATE_IN;
else if (res < 0)
pinos_log_warn ("node %p: got process output %d", this, res);
}
}
if (pi->buffer_id != SPA_ID_INVALID) {
pinos_log_trace ("node %p: process input", this);
res = spa_node_process_input (this->node);
if (res == SPA_RESULT_HAVE_OUTPUT)
this->sched_state = STATE_OUT;
else if (res == SPA_RESULT_NEED_INPUT)
this->sched_state = STATE_IN;
else if (res < 0)
pinos_log_warn ("node %p: got process input %d", this, res);
}
}
do_pull (this);
}
else if (SPA_EVENT_TYPE (event) == this->core->type.event_node.HaveOutput) {
SpaResult res;
int i;
PinosPort *outport;
for (i = 0; i < this->transport->area->n_outputs; i++) {
spa_list_for_each (outport, &this->output_ports, link) {
PinosLink *link;
PinosPort *inport, *outport;
PinosPort *inport;
SpaPortIO *po;
po = &this->transport->outputs[i];
po = &outport->io;
if (po->buffer_id == SPA_ID_INVALID)
continue;
pinos_log_trace ("node %p: have output %d", this, po->buffer_id);
outport = this->output_port_map[i];
spa_list_for_each (link, &outport->rt.links, rt.output_link) {
if (link->rt.input == NULL || link->rt.output == NULL)
continue;
inport = link->rt.input;
inport->node->transport->inputs[inport->port_id] = *po;
inport->io = *po;
if ((res = spa_node_process_input (inport->node->node)) < 0)
pinos_log_warn ("node %p: got process input %d", inport->node, res);
@ -374,31 +355,25 @@ push:
pinos_log_warn ("node %p: got process output %d", this, res);
}
#if 0
else if (SPA_EVENT_TYPE (event) == this->core->type.event_node.ReuseBuffer) {
SpaEventNodeReuseBuffer *rb = (SpaEventNodeReuseBuffer *) event;
int i;
PinosPort *inport;
pinos_log_trace ("node %p: reuse buffer %u", this, rb->body.buffer_id.value);
for (i = 0; i < this->transport->area->n_inputs; i++) {
spa_list_for_each (inport, &this->input_ports, link) {
PinosLink *link;
PinosPort *inport, *outport;
SpaPortIO *po;
PinosPort *outport;
inport = this->input_port_map[i];
spa_list_for_each (link, &inport->rt.links, rt.input_link) {
if (link->rt.input == NULL || link->rt.output == NULL)
continue;
outport = link->rt.output;
po = &outport->node->transport->outputs[outport->port_id];
po->buffer_id = rb->body.buffer_id.value;
outport->io.buffer_id = rb->body.buffer_id.value;
}
}
}
#endif
else if (SPA_EVENT_TYPE (event) == this->core->type.event_node.RequestClockUpdate) {
send_clock_update (this);
}
@ -437,8 +412,8 @@ node_bind_func (PinosGlobal *global,
info.id = global->id;
info.change_mask = ~0;
info.name = this->name;
info.max_inputs = this->transport->area->max_inputs;
info.n_inputs = this->transport->area->n_inputs;
info.max_inputs = this->max_input_ports;
info.n_inputs = this->n_input_ports;
info.input_formats = NULL;
for (info.n_input_formats = 0; ; info.n_input_formats++) {
SpaFormat *fmt;
@ -454,8 +429,8 @@ node_bind_func (PinosGlobal *global,
info.input_formats = realloc (info.input_formats, sizeof (SpaFormat*) * (info.n_input_formats + 1));
info.input_formats[info.n_input_formats] = spa_format_copy (fmt);
}
info.max_outputs = this->transport->area->max_outputs;
info.n_outputs = this->transport->area->n_outputs;
info.max_outputs = this->max_output_ports;
info.n_outputs = this->n_output_ports;
info.output_formats = NULL;
for (info.n_output_formats = 0; ; info.n_output_formats++) {
SpaFormat *fmt;
@ -568,7 +543,7 @@ pinos_node_new (PinosCore *core,
pinos_signal_init (&this->state_changed);
pinos_signal_init (&this->free_signal);
pinos_signal_init (&this->async_complete);
pinos_signal_init (&this->transport_changed);
pinos_signal_init (&this->initialized);
pinos_signal_init (&this->loop_changed);
this->state = PINOS_NODE_STATE_CREATING;
@ -634,8 +609,6 @@ do_node_remove_done (SpaLoop *loop,
pinos_work_queue_destroy (impl->work);
if (this->transport)
pinos_transport_destroy (this->transport);
if (this->input_port_map)
free (this->input_port_map);
if (this->output_port_map)
@ -737,22 +710,19 @@ pinos_node_get_free_port (PinosNode *node,
uint32_t *n_ports, max_ports;
SpaList *ports;
PinosPort *port = NULL, *p, **portmap;
SpaPortIO *io;
SpaResult res;
int i;
if (direction == PINOS_DIRECTION_INPUT) {
max_ports = node->transport->area->max_inputs;
n_ports = &node->transport->area->n_inputs;
max_ports = node->max_input_ports;
n_ports = &node->n_input_ports;
ports = &node->input_ports;
portmap = node->input_port_map;
io = node->transport->inputs;
} else {
max_ports = node->transport->area->max_outputs;
n_ports = &node->transport->area->n_outputs;
max_ports = node->max_output_ports;
n_ports = &node->n_output_ports;
ports = &node->output_ports;
portmap = node->output_port_map;
io = node->transport->outputs;
}
pinos_log_debug ("node %p: direction %d max %u, n %u", node, direction, max_ports, *n_ports);
@ -771,13 +741,13 @@ pinos_node_get_free_port (PinosNode *node,
if (portmap[i] == NULL) {
pinos_log_debug ("node %p: creating port direction %d %u", node, direction, i);
port = portmap[i] = pinos_port_new (node, direction, i);
port->io = &io[i];
spa_list_insert (ports, &port->link);
(*n_ports)++;
if ((res = spa_node_add_port (node->node, direction, i)) < 0) {
pinos_log_error ("node %p: could not add port %d", node, i);
}
else {
spa_node_port_set_io (node->node, direction, i, port->io);
spa_node_port_set_io (node->node, direction, i, &port->io);
}
}
}

View file

@ -67,12 +67,24 @@ struct _PinosNode {
SpaNode *node;
bool live;
SpaClock *clock;
int sched_state;
SpaList resource_list;
SpaList input_ports;
SpaList output_ports;
PINOS_SIGNAL (initialized, (PinosListener *listener,
PinosNode *object));
uint32_t max_input_ports;
uint32_t n_input_ports;
SpaList input_ports;
PinosPort **input_port_map;
uint32_t n_used_input_links;
uint32_t max_output_ports;
uint32_t n_output_ports;
SpaList output_ports;
PinosPort **output_port_map;
uint32_t n_used_output_links;
PINOS_SIGNAL (port_added, (PinosListener *listener,
PinosNode *node,
PinosPort *port));
@ -80,16 +92,6 @@ struct _PinosNode {
PinosNode *node,
PinosPort *port));
PinosPort **input_port_map;
PinosPort **output_port_map;
uint32_t n_used_output_links;
uint32_t n_used_input_links;
PinosTransport *transport;
PINOS_SIGNAL (transport_changed, (PinosListener *listener,
PinosNode *object));
PINOS_SIGNAL (destroy_signal, (PinosListener *listener,
PinosNode *object));
PINOS_SIGNAL (free_signal, (PinosListener *listener,

View file

@ -49,6 +49,8 @@ pinos_port_new (PinosNode *node,
this->direction = direction;
this->port_id = port_id;
this->state = SPA_PORT_STATE_CONFIGURE;
this->io.status = SPA_RESULT_OK;
this->io.buffer_id = SPA_ID_INVALID;
spa_list_init (&this->links);
spa_list_init (&this->rt.links);

View file

@ -46,7 +46,7 @@ struct _PinosPort {
PinosDirection direction;
uint32_t port_id;
uint32_t state;
SpaPortIO *io;
SpaPortIO io;
bool allocated;
PinosMemblock buffer_mem;