PipeWire 0.3.34
graph.h
Go to the documentation of this file.
1/* Simple Plugin API
2 *
3 * Copyright © 2018 Wim Taymans
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef SPA_GRAPH_H
26#define SPA_GRAPH_H
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
40#include <spa/utils/defs.h>
41#include <spa/utils/list.h>
42#include <spa/utils/hook.h>
43#include <spa/node/node.h>
44#include <spa/node/io.h>
45
46#ifndef spa_debug
47#define spa_debug(...)
48#endif
49
50struct spa_graph;
51struct spa_graph_node;
52struct spa_graph_link;
53struct spa_graph_port;
54
56 int status;
57 int32_t required;
58 int32_t pending;
59};
60
61/* static */ inline void spa_graph_state_reset(struct spa_graph_state *state)
62{
63 state->pending = state->required;
64}
65
67 struct spa_list link;
69 int (*signal) (void *data);
71};
72
73#define spa_graph_link_signal(l) ((l)->signal((l)->signal_data))
74
75#define spa_graph_state_dec(s,c) (__atomic_sub_fetch(&(s)->pending, c, __ATOMIC_SEQ_CST) == 0)
76
77/* static */ inline int spa_graph_link_trigger(struct spa_graph_link *link)
78{
79 struct spa_graph_state *state = link->state;
80
81 spa_debug("link %p: state %p: pending %d/%d", link, state,
82 state->pending, state->required);
83
84 if (spa_graph_state_dec(state, 1))
86
87 return state->status;
88}
89struct spa_graph {
90 uint32_t flags; /* flags */
91 struct spa_graph_node *parent; /* parent node or NULL when driver */
92 struct spa_graph_state *state; /* state of graph */
93 struct spa_list nodes; /* list of nodes of this graph */
94};
95
97#define SPA_VERSION_GRAPH_NODE_CALLBACKS 0
98 uint32_t version;
99
100 int (*process) (void *data, struct spa_graph_node *node);
101 int (*reuse_buffer) (void *data, struct spa_graph_node *node,
102 uint32_t port_id, uint32_t buffer_id);
103};
104
106 struct spa_list link;
107 struct spa_graph *graph;
108 struct spa_list ports[2];
110 uint32_t flags;
116};
117
118#define spa_graph_node_call(n,method,version,...) \
119({ \
120 int __res = 0; \
121 spa_callbacks_call_res(&(n)->callbacks, \
122 struct spa_graph_node_callbacks, __res, \
123 method, version, ##__VA_ARGS__); \
124 __res; \
125})
126
127#define spa_graph_node_process(n) spa_graph_node_call(n, process, 0, n)
128#define spa_graph_node_reuse_buffer(n,p,i) spa_graph_node_call(n, reuse_buffer, 0, n, p, i)
129
131 struct spa_list link;
134 uint32_t port_id;
135 uint32_t flags;
137};
138
139/* static */ inline int spa_graph_node_trigger(struct spa_graph_node *node)
140{
141 struct spa_graph_link *l;
142 spa_debug("node %p trigger", node);
143 spa_list_for_each(l, &node->links, link)
145 return 0;
146}
147
148/* static */ inline int spa_graph_run(struct spa_graph *graph)
149{
150 struct spa_graph_node *n, *t;
151 struct spa_list pending;
152
154 spa_debug("graph %p run with state %p pending %d/%d", graph, graph->state,
155 graph->state->pending, graph->state->required);
156
157 spa_list_init(&pending);
158
159 spa_list_for_each(n, &graph->nodes, link) {
160 struct spa_graph_state *s = n->state;
162 spa_debug("graph %p node %p: state %p pending %d/%d status %d", graph, n,
163 s, s->pending, s->required, s->status);
164 if (--s->pending == 0)
166 }
167 spa_list_for_each_safe(n, t, &pending, sched_link)
169
170 return 0;
171}
172
173/* static */ inline int spa_graph_finish(struct spa_graph *graph)
174{
175 spa_debug("graph %p finish", graph);
176 if (graph->parent)
177 return spa_graph_node_trigger(graph->parent);
178 return 0;
179}
180/* static */ inline int spa_graph_link_signal_node(void *data)
181{
182 struct spa_graph_node *node = (struct spa_graph_node *)data;
183 spa_debug("node %p call process", node);
185}
186
187/* static */ inline int spa_graph_link_signal_graph(void *data)
188{
189 struct spa_graph_node *node = (struct spa_graph_node *)data;
190 return spa_graph_finish(node->graph);
191}
192
193/* static */ inline void spa_graph_init(struct spa_graph *graph, struct spa_graph_state *state)
194{
195 spa_list_init(&graph->nodes);
196 graph->flags = 0;
197 graph->state = state;
198 spa_debug("graph %p init state %p", graph, state);
199}
200
201/* static */ inline void
203 struct spa_graph_state *state,
204 struct spa_graph_link *link)
205{
206 link->state = state;
207 state->required++;
208 spa_debug("node %p add link %p to state %p %d", out, link, state, state->required);
209 spa_list_append(&out->links, &link->link);
210}
211
212/* static */ inline void spa_graph_link_remove(struct spa_graph_link *link)
213{
214 link->state->required--;
215 spa_debug("link %p state %p remove %d", link, link->state, link->state->required);
217}
218
219/* static */ inline void
221{
224 spa_list_init(&node->links);
225 node->flags = 0;
226 node->subgraph = NULL;
227 node->state = state;
228 node->state->required = node->state->pending = 0;
229 node->state->status = SPA_STATUS_OK;
230 node->graph_link.signal = spa_graph_link_signal_graph;
231 node->graph_link.signal_data = node;
232 spa_debug("node %p init state %p", node, state);
233}
234
235
236/* static */ inline int spa_graph_node_impl_sub_process(void *data, struct spa_graph_node *node)
237{
238 struct spa_graph *graph = node->subgraph;
239 spa_debug("node %p: sub process %p", node, graph);
240 return spa_graph_run(graph);
241}
242
246};
247
248/* static */ inline void spa_graph_node_set_subgraph(struct spa_graph_node *node,
249 struct spa_graph *subgraph)
250{
251 node->subgraph = subgraph;
252 subgraph->parent = node;
253 spa_debug("node %p set subgraph %p", node, subgraph);
254}
255
256/* static */ inline void
258 const struct spa_graph_node_callbacks *callbacks,
259 void *data)
260{
261 node->callbacks = SPA_CALLBACKS_INIT(callbacks, data);
262}
263
264/* static */ inline void
266 struct spa_graph_node *node)
267{
268 node->graph = graph;
269 spa_list_append(&graph->nodes, &node->link);
270 node->state->required++;
271 spa_debug("node %p add to graph %p, state %p required %d",
272 node, graph, node->state, node->state->required);
273 spa_graph_link_add(node, graph->state, &node->graph_link);
274}
275
276/* static */ inline void spa_graph_node_remove(struct spa_graph_node *node)
277{
278 spa_debug("node %p remove from graph %p, state %p required %d",
279 node, node->graph, node->state, node->state->required);
280 spa_graph_link_remove(&node->graph_link);
281 node->state->required--;
283}
284
285
286/* static */ inline void
288 enum spa_direction direction,
289 uint32_t port_id,
290 uint32_t flags)
291{
292 spa_debug("port %p init type %d id %d", port, direction, port_id);
293 port->direction = direction;
294 port->port_id = port_id;
295 port->flags = flags;
296}
297
298/* static */ inline void
300 struct spa_graph_port *port)
301{
302 spa_debug("port %p add to node %p", port, node);
303 port->node = node;
305}
306
307/* static */ inline void spa_graph_port_remove(struct spa_graph_port *port)
308{
309 spa_debug("port %p remove", port);
311}
312
313/* static */ inline void
315{
316 spa_debug("port %p link to %p %p %p", out, in, in->node, in->node->state);
317 out->peer = in;
318 in->peer = out;
319}
320
321/* static */ inline void
323{
324 spa_debug("port %p unlink from %p", port, port->peer);
325 if (port->peer) {
326 port->peer->peer = NULL;
327 port->peer = NULL;
328 }
329}
330
331/* static */ inline int spa_graph_node_impl_process(void *data, struct spa_graph_node *node)
332{
333 struct spa_node *n = (struct spa_node *)data;
334 struct spa_graph_state *state = node->state;
335
336 spa_debug("node %p: process state %p: %d, node %p", node, state, state->status, n);
337 if ((state->status = spa_node_process(n)) != SPA_STATUS_OK)
339
340 return state->status;
341}
342
343/* static */ inline int spa_graph_node_impl_reuse_buffer(void *data, struct spa_graph_node *node,
344 uint32_t port_id, uint32_t buffer_id)
345{
346 struct spa_node *n = (struct spa_node *)data;
347 return spa_node_port_reuse_buffer(n, port_id, buffer_id);
348}
349
353 .reuse_buffer = spa_graph_node_impl_reuse_buffer,
354};
355
360#ifdef __cplusplus
361} /* extern "C" */
362#endif
363
364#endif /* SPA_GRAPH_H */
#define SPA_VERSION_GRAPH_NODE_CALLBACKS
Definition: graph.h:97
void spa_graph_port_remove(struct spa_graph_port *port)
Definition: graph.h:307
void spa_graph_port_init(struct spa_graph_port *port, enum spa_direction direction, uint32_t port_id, uint32_t flags)
Definition: graph.h:287
const struct spa_graph_node_callbacks spa_graph_node_impl_default
Definition: graph.h:350
void spa_graph_node_set_callbacks(struct spa_graph_node *node, const struct spa_graph_node_callbacks *callbacks, void *data)
Definition: graph.h:257
void spa_graph_port_link(struct spa_graph_port *out, struct spa_graph_port *in)
Definition: graph.h:314
#define spa_graph_link_signal(l)
Definition: graph.h:73
void spa_graph_port_add(struct spa_graph_node *node, struct spa_graph_port *port)
Definition: graph.h:299
int spa_graph_link_trigger(struct spa_graph_link *link)
Definition: graph.h:77
void spa_graph_init(struct spa_graph *graph, struct spa_graph_state *state)
Definition: graph.h:193
void spa_graph_node_remove(struct spa_graph_node *node)
Definition: graph.h:276
void spa_graph_node_init(struct spa_graph_node *node, struct spa_graph_state *state)
Definition: graph.h:220
const struct spa_graph_node_callbacks spa_graph_node_sub_impl_default
Definition: graph.h:243
void spa_graph_link_add(struct spa_graph_node *out, struct spa_graph_state *state, struct spa_graph_link *link)
Definition: graph.h:202
#define spa_debug(...)
Definition: graph.h:47
int spa_graph_link_signal_node(void *data)
Definition: graph.h:180
#define spa_graph_state_dec(s, c)
Definition: graph.h:75
int spa_graph_node_trigger(struct spa_graph_node *node)
Definition: graph.h:139
int spa_graph_run(struct spa_graph *graph)
Definition: graph.h:148
void spa_graph_port_unlink(struct spa_graph_port *port)
Definition: graph.h:322
int spa_graph_node_impl_process(void *data, struct spa_graph_node *node)
Definition: graph.h:331
void spa_graph_link_remove(struct spa_graph_link *link)
Definition: graph.h:212
void spa_graph_node_set_subgraph(struct spa_graph_node *node, struct spa_graph *subgraph)
Definition: graph.h:248
int spa_graph_finish(struct spa_graph *graph)
Definition: graph.h:173
#define spa_graph_node_process(n)
Definition: graph.h:127
int spa_graph_link_signal_graph(void *data)
Definition: graph.h:187
void spa_graph_state_reset(struct spa_graph_state *state)
Definition: graph.h:61
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:343
void spa_graph_node_add(struct spa_graph *graph, struct spa_graph_node *node)
Definition: graph.h:265
int spa_graph_node_impl_sub_process(void *data, struct spa_graph_node *node)
Definition: graph.h:236
#define SPA_CALLBACKS_INIT(_funcs, _data)
Initialize the set of functions funcs as a spa_callbacks, together with _data.
Definition: hook.h:142
#define spa_node_port_reuse_buffer(n,...)
Definition: redhat-linux-build/doc/spa/node/node.h:662
#define spa_node_process(n)
Definition: redhat-linux-build/doc/spa/node/node.h:663
void spa_list_init(struct spa_list *list)
Definition: list.h:44
void spa_list_remove(struct spa_list *elem)
Definition: list.h:69
spa_direction
Definition: defs.h:78
#define spa_list_for_each(pos, head, member)
Definition: list.h:111
#define spa_list_append(list, item)
Definition: list.h:81
#define spa_list_for_each_safe(pos, tmp, head, member)
Definition: list.h:129
@ SPA_DIRECTION_INPUT
Definition: defs.h:79
@ SPA_DIRECTION_OUTPUT
Definition: defs.h:80
#define SPA_STATUS_OK
Definition: io.h:94
user data to add to an object
Definition: media-session.c:109
Definition: module-filter-chain.c:201
Definition: alsa-endpoint.c:103
struct graph * graph
Definition: module-filter-chain.c:161
struct spa_list link
link in impl node_list
Definition: alsa-monitor.c:71
Definition: module-filter-chain.c:144
struct spa_list link
Definition: module-filter-chain.c:145
uint32_t flags
Definition: filter.c:95
struct node * node
Definition: module-filter-chain.c:146
enum spa_direction direction
Definition: buffers.c:46
uint32_t port_id
Definition: buffers.c:47
Callbacks, contains the structure with functions and the data passed to the functions.
Definition: hook.h:130
Definition: graph.h:96
int(* process)(void *data, struct spa_graph_node *node)
Definition: graph.h:100
int(* reuse_buffer)(void *data, struct spa_graph_node *node, uint32_t port_id, uint32_t buffer_id)
Definition: graph.h:101
uint32_t version
Definition: graph.h:98
Definition: graph.h:105
uint32_t flags
node flags
Definition: graph.h:110
struct spa_callbacks callbacks
Definition: graph.h:114
struct spa_graph * graph
owner graph
Definition: graph.h:107
struct spa_graph * subgraph
subgraph or NULL
Definition: graph.h:113
struct spa_graph_link graph_link
link in graph
Definition: graph.h:112
struct spa_list links
list of links to next nodes
Definition: graph.h:109
struct spa_graph_state * state
state of the node
Definition: graph.h:111
struct spa_list ports[2]
list of input and output ports
Definition: graph.h:108
struct spa_list sched_link
link for scheduler
Definition: graph.h:115
Definition: graph.h:130
struct spa_graph_node * node
owner node
Definition: graph.h:132
uint32_t port_id
port id
Definition: graph.h:134
struct spa_graph_port * peer
peer
Definition: graph.h:136
uint32_t flags
port flags
Definition: graph.h:135
enum spa_direction direction
port direction
Definition: graph.h:133
Definition: graph.h:55
int32_t pending
number of pending signals
Definition: graph.h:58
int32_t required
required number of signals
Definition: graph.h:57
int status
current status
Definition: graph.h:56
Definition: graph.h:89
uint32_t flags
Definition: graph.h:90
struct spa_list nodes
Definition: graph.h:93
struct spa_graph_node * parent
Definition: graph.h:91
struct spa_graph_state * state
Definition: graph.h:92
Definition: list.h:37
Definition: redhat-linux-build/doc/spa/node/node.h:53