mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-02 09:01:50 -05:00
graph: add graph datastructure and scheduler
Improve event and command init so that it can be used more easily as compound literals. Improve volume Make it possible to use graph scheduler in test-mixer
This commit is contained in:
parent
53dd63eb3a
commit
6691eb7845
18 changed files with 970 additions and 108 deletions
|
|
@ -95,7 +95,8 @@ typedef struct {
|
|||
} SpaCommandNodeClockUpdate;
|
||||
|
||||
#define SPA_COMMAND_NODE_CLOCK_UPDATE_INIT(type,change_mask,rate,ticks,monotonic_time,offset,scale,state,flags,latency) \
|
||||
SPA_COMMAND_INIT_COMPLEX (sizeof (SpaCommandNodeClockUpdateBody), type, \
|
||||
SPA_COMMAND_INIT_COMPLEX (SpaCommandNodeClockUpdate, \
|
||||
sizeof (SpaCommandNodeClockUpdateBody), type, \
|
||||
SPA_POD_INT_INIT (change_mask), \
|
||||
SPA_POD_INT_INIT (rate), \
|
||||
SPA_POD_LONG_INIT (ticks), \
|
||||
|
|
|
|||
|
|
@ -43,11 +43,11 @@ struct _SpaCommand {
|
|||
|
||||
#define SPA_COMMAND_TYPE(cmd) ((cmd)->body.body.type)
|
||||
|
||||
#define SPA_COMMAND_INIT(type) \
|
||||
#define SPA_COMMAND_INIT(type) (SpaCommand) \
|
||||
{ { sizeof (SpaCommandBody), SPA_POD_TYPE_OBJECT }, \
|
||||
{ { 0, type } } } \
|
||||
|
||||
#define SPA_COMMAND_INIT_COMPLEX(size,type,...) \
|
||||
#define SPA_COMMAND_INIT_COMPLEX(t,size,type,...) (t) \
|
||||
{ { size, SPA_POD_TYPE_OBJECT }, \
|
||||
{ { 0, type }, __VA_ARGS__ } } \
|
||||
|
||||
|
|
|
|||
|
|
@ -70,7 +70,8 @@ typedef struct {
|
|||
} SpaEventNodeAsyncComplete;
|
||||
|
||||
#define SPA_EVENT_NODE_ASYNC_COMPLETE_INIT(type,seq,res) \
|
||||
SPA_EVENT_INIT_COMPLEX (sizeof (SpaEventNodeAsyncCompleteBody), type, \
|
||||
SPA_EVENT_INIT_COMPLEX (SpaEventNodeAsyncComplete, \
|
||||
sizeof (SpaEventNodeAsyncCompleteBody), type, \
|
||||
SPA_POD_INT_INIT (seq), \
|
||||
SPA_POD_INT_INIT (res))
|
||||
|
||||
|
|
@ -90,7 +91,8 @@ typedef struct {
|
|||
} SpaEventNodeRequestClockUpdate;
|
||||
|
||||
#define SPA_EVENT_NODE_REQUEST_CLOCK_UPDATE_INIT(type,update_mask,timestamp,offset) \
|
||||
SPA_EVENT_INIT_COMPLEX (sizeof (SpaEventNodeRequestClockUpdateBody), type, \
|
||||
SPA_EVENT_INIT_COMPLEX (SpaEventNodeRequestClockUpdate, \
|
||||
sizeof (SpaEventNodeRequestClockUpdateBody), type, \
|
||||
SPA_POD_INT_INIT (update_mask), \
|
||||
SPA_POD_LONG_INIT (timestamp), \
|
||||
SPA_POD_LONG_INIT (offset))
|
||||
|
|
|
|||
|
|
@ -43,11 +43,11 @@ struct _SpaEvent {
|
|||
|
||||
#define SPA_EVENT_TYPE(ev) ((ev)->body.body.type)
|
||||
|
||||
#define SPA_EVENT_INIT(type) \
|
||||
#define SPA_EVENT_INIT(type) (SpaEvent) \
|
||||
{ { sizeof (SpaEventBody), SPA_POD_TYPE_OBJECT }, \
|
||||
{ { 0, type } } } \
|
||||
|
||||
#define SPA_EVENT_INIT_COMPLEX(size,type,...) \
|
||||
#define SPA_EVENT_INIT_COMPLEX(t,size,type,...) (t) \
|
||||
{ { size, SPA_POD_TYPE_OBJECT }, \
|
||||
{ { 0, type }, __VA_ARGS__ } } \
|
||||
|
||||
|
|
|
|||
221
spa/include/spa/graph.h
Normal file
221
spa/include/spa/graph.h
Normal file
|
|
@ -0,0 +1,221 @@
|
|||
/* 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_H__
|
||||
#define __SPA_GRAPH_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <spa/defs.h>
|
||||
#include <spa/list.h>
|
||||
|
||||
typedef struct SpaGraph SpaGraph;
|
||||
typedef struct SpaGraphNode SpaGraphNode;
|
||||
typedef struct SpaGraphPort SpaGraphPort;
|
||||
|
||||
struct SpaGraph {
|
||||
SpaList nodes;
|
||||
SpaList ready;
|
||||
};
|
||||
|
||||
#define PROCESS_CHECK 0
|
||||
#define PROCESS_IN 1
|
||||
#define PROCESS_OUT 2
|
||||
|
||||
typedef SpaResult (*SpaGraphNodeFunc) (SpaGraphNode *node);
|
||||
|
||||
struct SpaGraphNode {
|
||||
SpaList link;
|
||||
SpaList ready_link;
|
||||
SpaList ports[2];
|
||||
#define SPA_GRAPH_NODE_FLAG_ASYNC (1 << 0)
|
||||
uint32_t flags;
|
||||
SpaResult state;
|
||||
uint32_t action;
|
||||
SpaGraphNodeFunc schedule;
|
||||
void *user_data;
|
||||
uint32_t max_in;
|
||||
uint32_t required_in;
|
||||
uint32_t ready_in;
|
||||
};
|
||||
|
||||
struct SpaGraphPort {
|
||||
SpaList link;
|
||||
SpaGraphNode *node;
|
||||
SpaDirection direction;
|
||||
uint32_t port_id;
|
||||
uint32_t flags;
|
||||
SpaPortIO *io;
|
||||
SpaGraphPort *peer;
|
||||
};
|
||||
|
||||
static inline void
|
||||
spa_graph_init (SpaGraph *graph)
|
||||
{
|
||||
spa_list_init (&graph->nodes);
|
||||
spa_list_init (&graph->ready);
|
||||
}
|
||||
|
||||
static inline SpaResult
|
||||
spa_graph_node_schedule_default (SpaGraphNode *node)
|
||||
{
|
||||
SpaNode *n = node->user_data;
|
||||
if (node->action == PROCESS_IN)
|
||||
return spa_node_process_input (n);
|
||||
else if (node->action == PROCESS_OUT)
|
||||
return spa_node_process_output (n);
|
||||
else
|
||||
return SPA_RESULT_ERROR;
|
||||
}
|
||||
|
||||
static inline void
|
||||
spa_graph_node_add (SpaGraph *graph, SpaGraphNode *node, SpaGraphNodeFunc schedule, void *user_data)
|
||||
{
|
||||
spa_list_init (&node->ports[SPA_DIRECTION_INPUT]);
|
||||
spa_list_init (&node->ports[SPA_DIRECTION_OUTPUT]);
|
||||
node->flags = 0;
|
||||
node->state = SPA_RESULT_OK;
|
||||
node->action = PROCESS_OUT;
|
||||
node->schedule = schedule;
|
||||
node->user_data = user_data;
|
||||
spa_list_insert (graph->nodes.prev, &node->link);
|
||||
node->max_in = node->required_in = node->ready_in = 0;
|
||||
}
|
||||
|
||||
static inline void
|
||||
spa_graph_port_check (SpaGraph *graph,
|
||||
SpaGraphPort *port)
|
||||
{
|
||||
SpaGraphNode *node = port->node;
|
||||
|
||||
if (port->io->status == SPA_RESULT_HAVE_BUFFER)
|
||||
node->ready_in++;
|
||||
|
||||
if (node->required_in > 0 && node->ready_in == node->required_in) {
|
||||
node->action = PROCESS_IN;
|
||||
if (node->ready_link.next == NULL)
|
||||
spa_list_insert (graph->ready.prev, &node->ready_link);
|
||||
} else if (node->ready_link.next) {
|
||||
spa_list_remove (&node->ready_link);
|
||||
node->ready_link.next = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void
|
||||
spa_graph_port_add (SpaGraph *graph,
|
||||
SpaGraphNode *node,
|
||||
SpaGraphPort *port,
|
||||
SpaDirection direction,
|
||||
uint32_t port_id,
|
||||
uint32_t flags,
|
||||
SpaPortIO *io)
|
||||
{
|
||||
port->node = node;
|
||||
port->direction = direction;
|
||||
port->port_id = port_id;
|
||||
port->flags = flags;
|
||||
port->io = io;
|
||||
port->peer = NULL;
|
||||
spa_list_insert (node->ports[port->direction].prev, &port->link);
|
||||
node->max_in++;
|
||||
if (!(port->flags & SPA_PORT_INFO_FLAG_OPTIONAL) && direction == SPA_DIRECTION_INPUT)
|
||||
node->required_in++;
|
||||
spa_graph_port_check (graph, port);
|
||||
}
|
||||
|
||||
static inline void
|
||||
spa_graph_node_remove (SpaGraph *graph, SpaGraphNode *node)
|
||||
{
|
||||
spa_list_remove (&node->link);
|
||||
}
|
||||
|
||||
static inline void
|
||||
spa_graph_port_remove (SpaGraph *graph, SpaGraphPort *port)
|
||||
{
|
||||
spa_list_remove (&port->link);
|
||||
}
|
||||
|
||||
static inline void
|
||||
spa_graph_port_link (SpaGraph *graph, SpaGraphPort *out, SpaGraphPort *in)
|
||||
{
|
||||
out->peer = in;
|
||||
in->peer = out;
|
||||
}
|
||||
|
||||
static inline void
|
||||
spa_graph_port_unlink (SpaGraph *graph, SpaGraphPort *out, SpaGraphPort *in)
|
||||
{
|
||||
out->peer = NULL;
|
||||
in->peer = NULL;
|
||||
}
|
||||
|
||||
static inline void
|
||||
spa_graph_node_schedule (SpaGraph *graph, SpaGraphNode *node)
|
||||
{
|
||||
SpaGraphPort *p;
|
||||
|
||||
if (node->ready_link.next == NULL)
|
||||
spa_list_insert (graph->ready.prev, &node->ready_link);
|
||||
|
||||
while (!spa_list_is_empty (&graph->ready)) {
|
||||
SpaGraphNode *n = spa_list_first (&graph->ready, SpaGraphNode, ready_link);
|
||||
|
||||
spa_list_remove (&n->ready_link);
|
||||
n->ready_link.next = NULL;
|
||||
|
||||
switch (n->action) {
|
||||
case PROCESS_IN:
|
||||
case PROCESS_OUT:
|
||||
n->state = n->schedule (n);
|
||||
n->action = PROCESS_CHECK;
|
||||
spa_list_insert (graph->ready.prev, &n->ready_link);
|
||||
break;
|
||||
|
||||
case PROCESS_CHECK:
|
||||
if (n->state == SPA_RESULT_NEED_BUFFER) {
|
||||
n->ready_in = 0;
|
||||
spa_list_for_each (p, &n->ports[SPA_DIRECTION_INPUT], link) {
|
||||
SpaGraphNode *pn = p->peer->node;
|
||||
if (p->io->status == SPA_RESULT_NEED_BUFFER) {
|
||||
pn->action = PROCESS_OUT;
|
||||
spa_list_insert (graph->ready.prev, &pn->ready_link);
|
||||
}
|
||||
else if (p->io->status == SPA_RESULT_OK)
|
||||
n->ready_in++;
|
||||
}
|
||||
}
|
||||
else if (n->state == SPA_RESULT_HAVE_BUFFER) {
|
||||
spa_list_for_each (p, &n->ports[SPA_DIRECTION_OUTPUT], link)
|
||||
spa_graph_port_check (graph, p->peer);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* __SPA_GRAPH_H__ */
|
||||
Loading…
Add table
Add a link
Reference in a new issue