PipeWire: spa/graph/graph.h Source File

PipeWire 1.6.8

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