PipeWire: graph.h Source File

PipeWire 0.3.69

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