PipeWire: spa/graph/graph.h Source File

PipeWire 0.3.64

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