PipeWire 1.2.6

Loading...
Searching...
No Matches
graph.h
Go to the documentation of this file.
1/* Simple Plugin API */
2/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef SPA_GRAPH_H
6#define SPA_GRAPH_H
7
8#ifdef __cplusplus
9 extern "C" {
10#endif
11
21#include <spa/utils/atomic.h>
22#include <spa/utils/defs.h>
23#include <spa/utils/list.h>
24#include <spa/utils/hook.h>
25#include <spa/node/node.h>
26#include <spa/node/io.h>
27
28#ifndef spa_debug
29#define spa_debug(...)
30#endif
31
32 struct spa_graph;
33 struct spa_graph_node;
34 struct spa_graph_link;
35 struct spa_graph_port;
36
37 struct spa_graph_state {
38 int status;
39 int32_t required;
40 int32_t pending;
41};
42
43 static inline void spa_graph_state_reset(struct spa_graph_state *state)
44{
45 state->pending = state->required;
46}
47
48 struct spa_graph_link {
49 struct spa_list link;
50 struct spa_graph_state *state;
51 int (*signal) (void *data);
52 void *signal_data;
53};
54
55#define spa_graph_link_signal(l) ((l)->signal((l)->signal_data))
56
57#define spa_graph_state_dec(s) (SPA_ATOMIC_DEC(s->pending) == 0)
58
59 static inline int spa_graph_link_trigger(struct spa_graph_link *link)
60{
61 struct spa_graph_state *state = link->state;
62
63 spa_debug("link %p: state %p: pending %d/%d", link, state,
64 state->pending, state->required);
65
66 if (spa_graph_state_dec(state))
67 spa_graph_link_signal(link);
68
69 return state->status;
70}
71 struct spa_graph {
72 uint32_t flags; /* flags */
73 struct spa_graph_node *parent; /* parent node or NULL when driver */
74 struct spa_graph_state *state; /* state of graph */
75 struct spa_list nodes; /* list of nodes of this graph */
76};
77
78 struct spa_graph_node_callbacks {
79#define SPA_VERSION_GRAPH_NODE_CALLBACKS 0
80 uint32_t version;
81
82 int (*process) (void *data, struct spa_graph_node *node);
83 int (*reuse_buffer) (void *data, struct spa_graph_node *node,
84 uint32_t port_id, uint32_t buffer_id);
85};
86
87 struct spa_graph_node {
88 struct spa_list link;
89 struct spa_graph *graph;
90 struct spa_list ports[2];
91 struct spa_list links;
92 uint32_t flags;
93 struct spa_graph_state *state;
94 struct spa_graph_link graph_link;
95 struct spa_graph *subgraph;
96 struct spa_callbacks callbacks;
97 struct spa_list sched_link;
98};
99
100#define spa_graph_node_call(n,method,version,...) \
101({ \
102 int __res = 0; \
103 spa_callbacks_call_res(&(n)->callbacks, \
104 struct spa_graph_node_callbacks, __res, \
105 method, (version), ##__VA_ARGS__); \
106 __res; \
107})
108
109#define spa_graph_node_process(n) spa_graph_node_call((n), process, 0, (n))
110#define spa_graph_node_reuse_buffer(n,p,i) spa_graph_node_call((n), reuse_buffer, 0, (n), (p), (i))
111
112 struct spa_graph_port {
113 struct spa_list link;
114 struct spa_graph_node *node;
115 enum spa_direction direction;
116 uint32_t port_id;
117 uint32_t flags;
118 struct spa_graph_port *peer;
119};
120
121 static inline int spa_graph_node_trigger(struct spa_graph_node *node)
122{
123 struct spa_graph_link *l;
124 spa_debug("node %p trigger", node);
125 spa_list_for_each(l, &node->links, link)
126 spa_graph_link_trigger(l);
127 return 0;
128}
129
130static inline int spa_graph_run(struct spa_graph *graph)
131{
132 struct spa_graph_node *n, *t;
133 struct spa_list pending;
134
135 spa_graph_state_reset(graph->state);
136 spa_debug("graph %p run with state %p pending %d/%d", graph, graph->state,
137 graph->state->pending, graph->state->required);
138
139 spa_list_init(&pending);
140
141 spa_list_for_each(n, &graph->nodes, link) {
142 struct spa_graph_state *s = n->state;
143 spa_graph_state_reset(s);
144 spa_debug("graph %p node %p: state %p pending %d/%d status %d", graph, n,
145 s, s->pending, s->required, s->status);
146 if (--s->pending == 0)
147 spa_list_append(&pending, &n->sched_link);
148 }
149 spa_list_for_each_safe(n, t, &pending, sched_link)
150 spa_graph_node_process(n);
151
152 return 0;
153}
154
155 static inline int spa_graph_finish(struct spa_graph *graph)
156{
157 spa_debug("graph %p finish", graph);
158 if (graph->parent)
159 return spa_graph_node_trigger(graph->parent);
160 return 0;
161}
162 static inline int spa_graph_link_signal_node(void *data)
163{
164 struct spa_graph_node *node = (struct spa_graph_node *)data;
165 spa_debug("node %p call process", node);
166 return spa_graph_node_process(node);
167}
168
169 static inline int spa_graph_link_signal_graph(void *data)
170{
171 struct spa_graph_node *node = (struct spa_graph_node *)data;
172 return spa_graph_finish(node->graph);
173}
174
175 static inline void spa_graph_init(struct spa_graph *graph, struct spa_graph_state *state)
176{
177 spa_list_init(&graph->nodes);
178 graph->flags = 0;
179 graph->state = state;
180 spa_debug("graph %p init state %p", graph, state);
181}
182
183 static inline void
184 spa_graph_link_add(struct spa_graph_node *out,
185 struct spa_graph_state *state,
186 struct spa_graph_link *link)
187{
188 link->state = state;
189 state->required++;
190 spa_debug("node %p add link %p to state %p %d", out, link, state, state->required);
191 spa_list_append(&out->links, &link->link);
192}
193
194 static inline void spa_graph_link_remove(struct spa_graph_link *link)
195{
196 link->state->required--;
197 spa_debug("link %p state %p remove %d", link, link->state, link->state->required);
198 spa_list_remove(&link->link);
199}
200
201 static inline void
202 spa_graph_node_init(struct spa_graph_node *node, struct spa_graph_state *state)
203{
204 spa_list_init(&node->ports[SPA_DIRECTION_INPUT]);
205 spa_list_init(&node->ports[SPA_DIRECTION_OUTPUT]);
206 spa_list_init(&node->links);
207 node->flags = 0;
208 node->subgraph = NULL;
209 node->state = state;
210 node->state->required = node->state->pending = 0;
211 node->state->status = SPA_STATUS_OK;
212 node->graph_link.signal = spa_graph_link_signal_graph;
213 node->graph_link.signal_data = node;
214 spa_debug("node %p init state %p", node, state);
215}
216
217
218 static inline int spa_graph_node_impl_sub_process(void *data SPA_UNUSED, struct spa_graph_node *node)
219{
220 struct spa_graph *graph = node->subgraph;
221 spa_debug("node %p: sub process %p", node, graph);
222 return spa_graph_run(graph);
223}
224
225 static const struct spa_graph_node_callbacks spa_graph_node_sub_impl_default = {
226 .version = SPA_VERSION_GRAPH_NODE_CALLBACKS,
227 .process = spa_graph_node_impl_sub_process,
228};
229
230 static inline void spa_graph_node_set_subgraph(struct spa_graph_node *node,
231 struct spa_graph *subgraph)
232{
233 node->subgraph = subgraph;
234 subgraph->parent = node;
235 spa_debug("node %p set subgraph %p", node, subgraph);
236}
237
238 static inline void
239 spa_graph_node_set_callbacks(struct spa_graph_node *node,
240 const struct spa_graph_node_callbacks *callbacks,
241 void *data)
242{
243 node->callbacks = SPA_CALLBACKS_INIT(callbacks, data);
244}
245
246 static inline void
247 spa_graph_node_add(struct spa_graph *graph,
248 struct spa_graph_node *node)
249{
250 node->graph = graph;
251 spa_list_append(&graph->nodes, &node->link);
252 node->state->required++;
253 spa_debug("node %p add to graph %p, state %p required %d",
254 node, graph, node->state, node->state->required);
255 spa_graph_link_add(node, graph->state, &node->graph_link);
256}
257
258 static inline void spa_graph_node_remove(struct spa_graph_node *node)
259{
260 spa_debug("node %p remove from graph %p, state %p required %d",
261 node, node->graph, node->state, node->state->required);
262 spa_graph_link_remove(&node->graph_link);
263 node->state->required--;
264 spa_list_remove(&node->link);
265}
266
267
268 static inline void
269 spa_graph_port_init(struct spa_graph_port *port,
270 enum spa_direction direction,
271 uint32_t port_id,
272 uint32_t flags)
273{
274 spa_debug("port %p init type %d id %d", port, direction, port_id);
275 port->direction = direction;
276 port->port_id = port_id;
277 port->flags = flags;
278}
279
280 static inline void
281 spa_graph_port_add(struct spa_graph_node *node,
282 struct spa_graph_port *port)
283{
284 spa_debug("port %p add to node %p", port, node);
285 port->node = node;
286 spa_list_append(&node->ports[port->direction], &port->link);
287}
288
289 static inline void spa_graph_port_remove(struct spa_graph_port *port)
290{
291 spa_debug("port %p remove", port);
292 spa_list_remove(&port->link);
293}
294
295 static inline void
296 spa_graph_port_link(struct spa_graph_port *out, struct spa_graph_port *in)
297{
298 spa_debug("port %p link to %p %p %p", out, in, in->node, in->node->state);
299 out->peer = in;
300 in->peer = out;
301}
302
303 static inline void
304 spa_graph_port_unlink(struct spa_graph_port *port)
305{
306 spa_debug("port %p unlink from %p", port, port->peer);
307 if (port->peer) {
308 port->peer->peer = NULL;
309 port->peer = NULL;
310 }
311}
312
313 static inline int spa_graph_node_impl_process(void *data, struct spa_graph_node *node)
314{
315 struct spa_node *n = (struct spa_node *)data;
316 struct spa_graph_state *state = node->state;
317
318 spa_debug("node %p: process state %p: %d, node %p", node, state, state->status, n);
319 if ((state->status = spa_node_process(n)) != SPA_STATUS_OK)
320 spa_graph_node_trigger(node);
321
322 return state->status;
323}
324
325 static inline int spa_graph_node_impl_reuse_buffer(void *data, struct spa_graph_node *node SPA_UNUSED,
326 uint32_t port_id, uint32_t buffer_id)
327{
328 struct spa_node *n = (struct spa_node *)data;
329 return spa_node_port_reuse_buffer(n, port_id, buffer_id);
330}
331
332 static const struct spa_graph_node_callbacks spa_graph_node_impl_default = {
333 .version = SPA_VERSION_GRAPH_NODE_CALLBACKS,
334 .process = spa_graph_node_impl_process,
335 .reuse_buffer = spa_graph_node_impl_reuse_buffer,
336};
337
342#ifdef __cplusplus
343} /* extern "C" */
344#endif
345
346#endif /* SPA_GRAPH_H */
defs.h
spa/utils/defs.h
spa_graph_node_set_callbacks
static void spa_graph_node_set_callbacks(struct spa_graph_node *node, const struct spa_graph_node_callbacks *callbacks, void *data)
Definition graph.h:249
spa_graph_state_reset
static void spa_graph_state_reset(struct spa_graph_state *state)
Definition graph.h:52
spa_graph_link_add
static void spa_graph_link_add(struct spa_graph_node *out, struct spa_graph_state *state, struct spa_graph_link *link)
Definition graph.h:194
spa_graph_node_impl_default
static const struct spa_graph_node_callbacks spa_graph_node_impl_default
Definition graph.h:342
spa_graph_port_remove
static void spa_graph_port_remove(struct spa_graph_port *port)
Definition graph.h:299
spa_graph_init
static void spa_graph_init(struct spa_graph *graph, struct spa_graph_state *state)
Definition graph.h:185
spa_graph_node_add
static void spa_graph_node_add(struct spa_graph *graph, struct spa_graph_node *node)
Definition graph.h:257
spa_graph_node_impl_reuse_buffer
static int spa_graph_node_impl_reuse_buffer(void *data, struct spa_graph_node *node, uint32_t port_id, uint32_t buffer_id)
Definition graph.h:335
spa_graph_link_signal
#define spa_graph_link_signal(l)
Definition graph.h:64
spa_graph_run
static int spa_graph_run(struct spa_graph *graph)
Definition graph.h:140
spa_graph_node_impl_sub_process
static int spa_graph_node_impl_sub_process(void *data, struct spa_graph_node *node)
Definition graph.h:228
spa_graph_node_trigger
static int spa_graph_node_trigger(struct spa_graph_node *node)
Definition graph.h:131
spa_graph_node_impl_process
static int spa_graph_node_impl_process(void *data, struct spa_graph_node *node)
Definition graph.h:323
spa_graph_node_set_subgraph
static void spa_graph_node_set_subgraph(struct spa_graph_node *node, struct spa_graph *subgraph)
Definition graph.h:240
spa_graph_node_sub_impl_default
static const struct spa_graph_node_callbacks spa_graph_node_sub_impl_default
Definition graph.h:235
spa_graph_link_trigger
static int spa_graph_link_trigger(struct spa_graph_link *link)
Definition graph.h:68
spa_graph_node_init
static void spa_graph_node_init(struct spa_graph_node *node, struct spa_graph_state *state)
Definition graph.h:212
spa_graph_port_init
static void spa_graph_port_init(struct spa_graph_port *port, enum spa_direction direction, uint32_t port_id, uint32_t flags)
Definition graph.h:279
spa_graph_finish
static int spa_graph_finish(struct spa_graph *graph)
Definition graph.h:165
spa_debug
#define spa_debug(...)
Definition graph.h:34
spa_graph_node_remove
static void spa_graph_node_remove(struct spa_graph_node *node)
Definition graph.h:268
spa_graph_port_add
static void spa_graph_port_add(struct spa_graph_node *node, struct spa_graph_port *port)
Definition graph.h:291
spa_graph_link_signal_graph
static int spa_graph_link_signal_graph(void *data)
Definition graph.h:179
spa_graph_port_link
static void spa_graph_port_link(struct spa_graph_port *out, struct spa_graph_port *in)
Definition graph.h:306
spa_graph_node_process
#define spa_graph_node_process(n)
Definition graph.h:119
spa_graph_link_remove
static void spa_graph_link_remove(struct spa_graph_link *link)
Definition graph.h:204
spa_graph_link_signal_node
static int spa_graph_link_signal_node(void *data)
Definition graph.h:172
SPA_VERSION_GRAPH_NODE_CALLBACKS
#define SPA_VERSION_GRAPH_NODE_CALLBACKS
Definition graph.h:89
spa_graph_state_dec
#define spa_graph_state_dec(s)
Definition graph.h:66
spa_graph_port_unlink
static void spa_graph_port_unlink(struct spa_graph_port *port)
Definition graph.h:314
SPA_CALLBACKS_INIT
#define SPA_CALLBACKS_INIT(_funcs, _data)
Initialize the set of functions funcs as a spa_callbacks, together with _data.
Definition hook.h:134
spa_list_init
static void spa_list_init(struct spa_list *list)
Definition list.h:35
spa_list_remove
static void spa_list_remove(struct spa_list *elem)
Definition list.h:65
spa_list_for_each
#define spa_list_for_each(pos, head, member)
Definition list.h:107
spa_list_append
#define spa_list_append(list, item)
Definition list.h:77
spa_list_for_each_safe
#define spa_list_for_each_safe(pos, tmp, head, member)
Definition list.h:125
SPA_STATUS_OK
#define SPA_STATUS_OK
Definition io.h:82
spa_node_port_reuse_buffer
#define spa_node_port_reuse_buffer(n,...)
Tell an output port to reuse a buffer.
Definition node.h:762
spa_node_process
#define spa_node_process(n)
Process the node.
Definition node.h:766
SPA_UNUSED
#define SPA_UNUSED
Definition defs.h:307
spa_direction
spa_direction
Definition defs.h:106
SPA_DIRECTION_INPUT
@ SPA_DIRECTION_INPUT
Definition defs.h:107
SPA_DIRECTION_OUTPUT
@ SPA_DIRECTION_OUTPUT
Definition defs.h:108
hook.h
spa/utils/hook.h
io.h
spa/node/io.h
list.h
spa/utils/list.h
node.h
spa/node/node.h
spa_callbacks
Callbacks, contains the structure with functions and the data passed to the functions.
Definition hook.h:116
spa_graph_link
Definition graph.h:57
spa_graph_link::state
struct spa_graph_state * state
Definition graph.h:59
spa_graph_link::signal
int(* signal)(void *data)
Definition graph.h:60
spa_graph_link::signal_data
void * signal_data
Definition graph.h:61
spa_graph_node_callbacks
Definition graph.h:87
spa_graph_node_callbacks::process
int(* process)(void *data, struct spa_graph_node *node)
Definition graph.h:92
spa_graph_node_callbacks::reuse_buffer
int(* reuse_buffer)(void *data, struct spa_graph_node *node, uint32_t port_id, uint32_t buffer_id)
Definition graph.h:93
spa_graph_node_callbacks::version
uint32_t version
Definition graph.h:90
spa_graph_node
Definition graph.h:97
spa_graph_node::flags
uint32_t flags
node flags
Definition graph.h:102
spa_graph_node::callbacks
struct spa_callbacks callbacks
Definition graph.h:106
spa_graph_node::graph
struct spa_graph * graph
owner graph
Definition graph.h:99
spa_graph_node::subgraph
struct spa_graph * subgraph
subgraph or NULL
Definition graph.h:105
spa_graph_node::graph_link
struct spa_graph_link graph_link
link in graph
Definition graph.h:104
spa_graph_node::links
struct spa_list links
list of links to next nodes
Definition graph.h:101
spa_graph_node::state
struct spa_graph_state * state
state of the node
Definition graph.h:103
spa_graph_node::ports
struct spa_list ports[2]
list of input and output ports
Definition graph.h:100
spa_graph_node::sched_link
struct spa_list sched_link
link for scheduler
Definition graph.h:107
spa_graph_port
Definition graph.h:122
spa_graph_port::node
struct spa_graph_node * node
owner node
Definition graph.h:124
spa_graph_port::port_id
uint32_t port_id
port id
Definition graph.h:126
spa_graph_port::peer
struct spa_graph_port * peer
peer
Definition graph.h:128
spa_graph_port::flags
uint32_t flags
port flags
Definition graph.h:127
spa_graph_port::direction
enum spa_direction direction
port direction
Definition graph.h:125
spa_graph_state
Definition graph.h:46
spa_graph_state::pending
int32_t pending
number of pending signals
Definition graph.h:49
spa_graph_state::required
int32_t required
required number of signals
Definition graph.h:48
spa_graph_state::status
int status
current status
Definition graph.h:47
spa_graph
Definition graph.h:80
spa_graph::flags
uint32_t flags
Definition graph.h:81
spa_graph::nodes
struct spa_list nodes
Definition graph.h:84
spa_graph::parent
struct spa_graph_node * parent
Definition graph.h:82
spa_graph::state
struct spa_graph_state * state
Definition graph.h:83
spa_list
Definition list.h:27
spa_node
Definition node.h:41