2017-06-30 19:32:11 +02:00
|
|
|
/* Simple Plugin API
|
|
|
|
|
* Copyright (C) 2017 Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* 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 library 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., 51 Franklin St, Fifth Floor,
|
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef __SPA_GRAPH_SCHEDULER_H__
|
|
|
|
|
#define __SPA_GRAPH_SCHEDULER_H__
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-11-10 13:36:14 +01:00
|
|
|
#include <spa/graph/graph.h>
|
2017-06-30 19:32:11 +02:00
|
|
|
|
2017-08-18 18:54:45 +02:00
|
|
|
#define SPA_GRAPH_STATE_IN 0
|
|
|
|
|
#define SPA_GRAPH_STATE_OUT 1
|
|
|
|
|
#define SPA_GRAPH_STATE_CHECK_IN 2
|
|
|
|
|
#define SPA_GRAPH_STATE_CHECK_OUT 3
|
|
|
|
|
|
2017-08-20 18:33:07 +02:00
|
|
|
struct spa_graph_data {
|
2017-06-30 19:32:11 +02:00
|
|
|
struct spa_graph *graph;
|
2017-07-03 17:34:30 +02:00
|
|
|
struct spa_list ready;
|
2017-06-30 19:32:11 +02:00
|
|
|
struct spa_graph_node *node;
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-20 18:33:07 +02:00
|
|
|
static inline void spa_graph_data_init(struct spa_graph_data *data,
|
|
|
|
|
struct spa_graph *graph)
|
2017-06-30 19:32:11 +02:00
|
|
|
{
|
2017-08-20 18:33:07 +02:00
|
|
|
data->graph = graph;
|
|
|
|
|
spa_list_init(&data->ready);
|
|
|
|
|
data->node = NULL;
|
2017-06-30 19:32:11 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-20 18:33:07 +02:00
|
|
|
static inline void spa_graph_data_port_check(struct spa_graph_data *data, struct spa_graph_port *port)
|
2017-07-03 17:34:30 +02:00
|
|
|
{
|
|
|
|
|
struct spa_graph_node *node = port->node;
|
2017-09-15 13:37:33 +02:00
|
|
|
uint32_t required = node->required[SPA_DIRECTION_INPUT];
|
2017-07-03 17:34:30 +02:00
|
|
|
|
2017-11-13 09:41:41 +01:00
|
|
|
if (port->io->status == SPA_STATUS_HAVE_BUFFER)
|
2017-09-15 13:37:33 +02:00
|
|
|
node->ready[SPA_DIRECTION_INPUT]++;
|
2017-07-03 17:34:30 +02:00
|
|
|
|
2017-09-15 17:26:01 +02:00
|
|
|
spa_debug("port %p node %p check %d %d %d", port, node,
|
2017-09-15 13:37:33 +02:00
|
|
|
port->io->status, node->ready[SPA_DIRECTION_INPUT], required);
|
2017-07-03 17:34:30 +02:00
|
|
|
|
2017-09-15 13:37:33 +02:00
|
|
|
if (required > 0 && node->ready[SPA_DIRECTION_INPUT] == required) {
|
2017-08-18 18:54:45 +02:00
|
|
|
node->state = SPA_GRAPH_STATE_IN;
|
2017-07-03 17:34:30 +02:00
|
|
|
if (node->ready_link.next == NULL)
|
2017-10-24 12:58:10 +02:00
|
|
|
spa_list_append(&data->ready, &node->ready_link);
|
2017-07-03 17:34:30 +02:00
|
|
|
} else if (node->ready_link.next) {
|
|
|
|
|
spa_list_remove(&node->ready_link);
|
|
|
|
|
node->ready_link.next = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-20 18:33:07 +02:00
|
|
|
static inline bool spa_graph_data_iterate(struct spa_graph_data *data)
|
2017-06-30 19:32:11 +02:00
|
|
|
{
|
|
|
|
|
bool res;
|
2017-08-18 18:54:45 +02:00
|
|
|
int state;
|
2017-06-30 19:32:11 +02:00
|
|
|
struct spa_graph_port *p;
|
|
|
|
|
struct spa_graph_node *n;
|
|
|
|
|
|
2017-08-20 18:33:07 +02:00
|
|
|
res = !spa_list_is_empty(&data->ready);
|
2017-06-30 19:32:11 +02:00
|
|
|
if (res) {
|
2017-08-20 18:33:07 +02:00
|
|
|
n = spa_list_first(&data->ready, struct spa_graph_node, ready_link);
|
2017-06-30 19:32:11 +02:00
|
|
|
|
|
|
|
|
spa_list_remove(&n->ready_link);
|
|
|
|
|
n->ready_link.next = NULL;
|
|
|
|
|
|
2017-09-15 17:26:01 +02:00
|
|
|
spa_debug("node %p state %d", n, n->state);
|
2017-06-30 19:32:11 +02:00
|
|
|
|
2017-08-18 18:54:45 +02:00
|
|
|
switch (n->state) {
|
|
|
|
|
case SPA_GRAPH_STATE_IN:
|
2017-08-27 12:12:14 +02:00
|
|
|
state = spa_node_process_input(n->implementation);
|
2017-11-13 09:41:41 +01:00
|
|
|
if (state == SPA_STATUS_NEED_BUFFER)
|
2017-08-18 18:54:45 +02:00
|
|
|
n->state = SPA_GRAPH_STATE_CHECK_IN;
|
2017-11-13 09:41:41 +01:00
|
|
|
else if (state == SPA_STATUS_HAVE_BUFFER)
|
2017-08-18 18:54:45 +02:00
|
|
|
n->state = SPA_GRAPH_STATE_CHECK_OUT;
|
2017-09-15 17:26:01 +02:00
|
|
|
spa_debug("node %p processed input state %d", n, n->state);
|
2017-08-20 18:33:07 +02:00
|
|
|
if (n == data->node)
|
2017-06-30 19:32:11 +02:00
|
|
|
break;
|
2017-08-20 18:33:07 +02:00
|
|
|
spa_list_append(&data->ready, &n->ready_link);
|
2017-06-30 19:32:11 +02:00
|
|
|
break;
|
|
|
|
|
|
2017-08-18 18:54:45 +02:00
|
|
|
case SPA_GRAPH_STATE_OUT:
|
2017-08-27 12:12:14 +02:00
|
|
|
state = spa_node_process_output(n->implementation);
|
2017-11-13 09:41:41 +01:00
|
|
|
if (state == SPA_STATUS_NEED_BUFFER)
|
2017-08-18 18:54:45 +02:00
|
|
|
n->state = SPA_GRAPH_STATE_CHECK_IN;
|
2017-11-13 09:41:41 +01:00
|
|
|
else if (state == SPA_STATUS_HAVE_BUFFER)
|
2017-08-18 18:54:45 +02:00
|
|
|
n->state = SPA_GRAPH_STATE_CHECK_OUT;
|
2017-09-15 17:26:01 +02:00
|
|
|
spa_debug("node %p processed output state %d", n, n->state);
|
2017-08-20 18:33:07 +02:00
|
|
|
spa_list_append(&data->ready, &n->ready_link);
|
2017-07-07 17:55:26 +02:00
|
|
|
break;
|
|
|
|
|
|
2017-08-18 18:54:45 +02:00
|
|
|
case SPA_GRAPH_STATE_CHECK_IN:
|
2017-09-15 13:37:33 +02:00
|
|
|
n->ready[SPA_DIRECTION_INPUT] = 0;
|
2017-08-18 18:54:45 +02:00
|
|
|
spa_list_for_each(p, &n->ports[SPA_DIRECTION_INPUT], link) {
|
|
|
|
|
struct spa_graph_node *pn = p->peer->node;
|
2017-11-13 09:41:41 +01:00
|
|
|
if (p->io->status == SPA_STATUS_NEED_BUFFER) {
|
2017-08-20 18:33:07 +02:00
|
|
|
if (pn != data->node
|
2017-08-18 18:54:45 +02:00
|
|
|
|| pn->flags & SPA_GRAPH_NODE_FLAG_ASYNC) {
|
|
|
|
|
pn->state = SPA_GRAPH_STATE_OUT;
|
2017-08-20 18:33:07 +02:00
|
|
|
spa_list_append(&data->ready,
|
2017-08-18 18:54:45 +02:00
|
|
|
&pn->ready_link);
|
|
|
|
|
}
|
2017-11-13 09:41:41 +01:00
|
|
|
} else if (p->io->status == SPA_STATUS_OK)
|
2017-09-15 13:37:33 +02:00
|
|
|
n->ready[SPA_DIRECTION_INPUT]++;
|
2017-06-30 19:32:11 +02:00
|
|
|
}
|
2017-08-18 18:54:45 +02:00
|
|
|
case SPA_GRAPH_STATE_CHECK_OUT:
|
|
|
|
|
spa_list_for_each(p, &n->ports[SPA_DIRECTION_OUTPUT], link)
|
2017-08-20 18:33:07 +02:00
|
|
|
spa_graph_data_port_check(data, p->peer);
|
2017-06-30 19:32:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-08-20 18:33:07 +02:00
|
|
|
res = !spa_list_is_empty(&data->ready);
|
2017-06-30 19:32:11 +02:00
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-20 18:33:07 +02:00
|
|
|
static inline int spa_graph_impl_need_input(void *data, struct spa_graph_node *node)
|
2017-06-30 19:32:11 +02:00
|
|
|
{
|
2018-02-08 11:24:20 +01:00
|
|
|
struct spa_graph_data *d = (struct spa_graph_data *) data;
|
2017-09-15 17:26:01 +02:00
|
|
|
spa_debug("node %p start pull", node);
|
2017-08-18 18:54:45 +02:00
|
|
|
node->state = SPA_GRAPH_STATE_CHECK_IN;
|
2017-08-20 18:33:07 +02:00
|
|
|
d->node = node;
|
2017-06-30 19:32:11 +02:00
|
|
|
if (node->ready_link.next == NULL)
|
2017-08-20 18:33:07 +02:00
|
|
|
spa_list_append(&d->ready, &node->ready_link);
|
|
|
|
|
|
2018-02-08 11:24:20 +01:00
|
|
|
while(spa_graph_data_iterate(d));
|
2017-08-20 18:33:07 +02:00
|
|
|
|
2017-11-13 09:41:41 +01:00
|
|
|
return 0;
|
2017-06-30 19:32:11 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-20 18:33:07 +02:00
|
|
|
static inline int spa_graph_impl_have_output(void *data, struct spa_graph_node *node)
|
2017-06-30 19:32:11 +02:00
|
|
|
{
|
2018-02-08 11:24:20 +01:00
|
|
|
struct spa_graph_data *d = (struct spa_graph_data *) data;
|
2017-09-15 17:26:01 +02:00
|
|
|
spa_debug("node %p start push", node);
|
2017-08-18 18:54:45 +02:00
|
|
|
node->state = SPA_GRAPH_STATE_OUT;
|
2017-08-20 18:33:07 +02:00
|
|
|
d->node = node;
|
2017-06-30 19:32:11 +02:00
|
|
|
if (node->ready_link.next == NULL)
|
2017-08-20 18:33:07 +02:00
|
|
|
spa_list_append(&d->ready, &node->ready_link);
|
|
|
|
|
|
2018-02-08 11:24:20 +01:00
|
|
|
while(spa_graph_data_iterate(d));
|
2017-08-20 18:33:07 +02:00
|
|
|
|
2017-11-13 09:41:41 +01:00
|
|
|
return 0;
|
2017-06-30 19:32:11 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-20 18:33:07 +02:00
|
|
|
static const struct spa_graph_callbacks spa_graph_impl_default = {
|
|
|
|
|
SPA_VERSION_GRAPH_CALLBACKS,
|
|
|
|
|
.need_input = spa_graph_impl_need_input,
|
|
|
|
|
.have_output = spa_graph_impl_have_output,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2017-06-30 19:32:11 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
} /* extern "C" */
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif /* __SPA_GRAPH_SCHEDULER_H__ */
|