PipeWire  0.3.66
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/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 
31 struct spa_graph;
32 struct spa_graph_node;
34 struct spa_graph_port;
35 
36 struct spa_graph_state {
37  int status;
38  int32_t required;
39  int32_t pending;
40 };
41 
42 static inline void spa_graph_state_reset(struct spa_graph_state *state)
43 {
44  state->pending = state->required;
45 }
46 
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))
67 
68  return state->status;
69 }
70 struct 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 
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 
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;
93  struct spa_graph_link graph_link;
94  struct spa_graph *subgraph;
95  struct spa_callbacks callbacks;
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 
111 struct 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 
120 static 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)
126  return 0;
127 }
128 
129 static 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;
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)
150 
151  return 0;
152 }
153 
154 static 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 }
161 static 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 
168 static 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 
174 static inline void spa_graph_init(struct spa_graph *graph, struct spa_graph_state *state)
175 {
177  graph->flags = 0;
179  spa_debug("graph %p init state %p", graph, state);
180 }
181 
182 static inline void
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 
200 static inline void
202 {
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;
212  node->graph_link.signal_data = node;
213  spa_debug("node %p init state %p", node, state);
214 }
215 
216 
217 static 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 
227 };
228 
229 static 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 
237 static inline void
239  const struct spa_graph_node_callbacks *callbacks,
240  void *data)
241 {
242  node->callbacks = SPA_CALLBACKS_INIT(callbacks, data);
243 }
244 
245 static 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 
257 static 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);
262  node->state->required--;
263  spa_list_remove(&node->link);
264 }
265 
266 
267 static inline void
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 
279 static inline void
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 
288 static 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 
294 static 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 
302 static inline void
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 
312 static 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)
320 
321  return state->status;
322 }
323 
324 static 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 
333  .process = spa_graph_node_impl_process,
335 };
336 
341 #ifdef __cplusplus
342 } /* extern "C" */
343 #endif
344 
345 #endif /* SPA_GRAPH_H */
spa/utils/defs.h
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
static void spa_graph_state_reset(struct spa_graph_state *state)
Definition: graph.h:51
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
static const struct spa_graph_node_callbacks spa_graph_node_impl_default
Definition: graph.h:341
static void spa_graph_port_remove(struct spa_graph_port *port)
Definition: graph.h:298
static void spa_graph_init(struct spa_graph *graph, struct spa_graph_state *state)
Definition: graph.h:184
static void spa_graph_node_add(struct spa_graph *graph, struct spa_graph_node *node)
Definition: graph.h:256
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
#define spa_graph_link_signal(l)
Definition: graph.h:63
static int spa_graph_run(struct spa_graph *graph)
Definition: graph.h:139
static int spa_graph_node_impl_sub_process(void *data, struct spa_graph_node *node)
Definition: graph.h:227
static int spa_graph_node_trigger(struct spa_graph_node *node)
Definition: graph.h:130
static int spa_graph_node_impl_process(void *data, struct spa_graph_node *node)
Definition: graph.h:322
static void spa_graph_node_set_subgraph(struct spa_graph_node *node, struct spa_graph *subgraph)
Definition: graph.h:239
static const struct spa_graph_node_callbacks spa_graph_node_sub_impl_default
Definition: graph.h:234
static int spa_graph_link_trigger(struct spa_graph_link *link)
Definition: graph.h:67
static void spa_graph_node_init(struct spa_graph_node *node, struct spa_graph_state *state)
Definition: graph.h:211
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
static int spa_graph_finish(struct spa_graph *graph)
Definition: graph.h:164
#define spa_debug(...)
Definition: graph.h:33
static void spa_graph_node_remove(struct spa_graph_node *node)
Definition: graph.h:267
static void spa_graph_port_add(struct spa_graph_node *node, struct spa_graph_port *port)
Definition: graph.h:290
#define spa_graph_state_dec(s, c)
Definition: graph.h:65
static int spa_graph_link_signal_graph(void *data)
Definition: graph.h:178
static void spa_graph_port_link(struct spa_graph_port *out, struct spa_graph_port *in)
Definition: graph.h:305
#define spa_graph_node_process(n)
Definition: graph.h:118
static void spa_graph_link_remove(struct spa_graph_link *link)
Definition: graph.h:203
static int spa_graph_link_signal_node(void *data)
Definition: graph.h:171
#define SPA_VERSION_GRAPH_NODE_CALLBACKS
Definition: graph.h:88
static void spa_graph_port_unlink(struct spa_graph_port *port)
Definition: graph.h:313
#define SPA_CALLBACKS_INIT(_funcs, _data)
Initialize the set of functions funcs as a spa_callbacks, together with _data.
Definition: hook.h:134
static void spa_list_init(struct spa_list *list)
Definition: list.h:35
static void spa_list_remove(struct spa_list *elem)
Definition: list.h:65
#define spa_list_for_each(pos, head, member)
Definition: list.h:107
#define spa_list_append(list, item)
Definition: list.h:77
#define spa_list_for_each_safe(pos, tmp, head, member)
Definition: list.h:125
#define SPA_STATUS_OK
Definition: io.h:80
#define spa_node_port_reuse_buffer(n,...)
Tell an output port to reuse a buffer.
Definition: spa/include/spa/node/node.h:741
#define spa_node_process(n)
Process the node.
Definition: spa/include/spa/node/node.h:744
spa_direction
Definition: defs.h:88
@ SPA_DIRECTION_INPUT
Definition: defs.h:89
@ SPA_DIRECTION_OUTPUT
Definition: defs.h:90
spa/utils/hook.h
spa/node/io.h
spa/utils/list.h
spa/node/node.h
Callbacks, contains the structure with functions and the data passed to the functions.
Definition: hook.h:116
Definition: graph.h:86
int(* process)(void *data, struct spa_graph_node *node)
Definition: graph.h:91
int(* reuse_buffer)(void *data, struct spa_graph_node *node, uint32_t port_id, uint32_t buffer_id)
Definition: graph.h:92
uint32_t version
Definition: graph.h:89
Definition: graph.h:96
uint32_t flags
node flags
Definition: graph.h:101
struct spa_list link
link in graph nodes list
Definition: graph.h:97
struct spa_callbacks callbacks
Definition: graph.h:105
struct spa_graph * graph
owner graph
Definition: graph.h:98
struct spa_graph * subgraph
subgraph or NULL
Definition: graph.h:104
struct spa_graph_link graph_link
link in graph
Definition: graph.h:103
struct spa_list links
list of links to next nodes
Definition: graph.h:100
struct spa_graph_state * state
state of the node
Definition: graph.h:102
struct spa_list ports[2]
list of input and output ports
Definition: graph.h:99
struct spa_list sched_link
link for scheduler
Definition: graph.h:106
Definition: graph.h:121
struct spa_graph_node * node
owner node
Definition: graph.h:123
uint32_t port_id
port id
Definition: graph.h:125
struct spa_graph_port * peer
peer
Definition: graph.h:127
uint32_t flags
port flags
Definition: graph.h:126
enum spa_direction direction
port direction
Definition: graph.h:124
struct spa_list link
link in node port list
Definition: graph.h:122
Definition: graph.h:45
int32_t pending
number of pending signals
Definition: graph.h:48
int32_t required
required number of signals
Definition: graph.h:47
int status
current status
Definition: graph.h:46
Definition: graph.h:79
uint32_t flags
Definition: graph.h:80
struct spa_list nodes
Definition: graph.h:83
struct spa_graph_node * parent
Definition: graph.h:81
struct spa_graph_state * state
Definition: graph.h:82
Definition: list.h:27
Definition: spa/include/spa/node/node.h:41