PipeWire  0.3.59
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
29 extern "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 
51 struct spa_graph;
52 struct spa_graph_node;
54 struct spa_graph_port;
55 
56 struct spa_graph_state {
57  int status;
58  int32_t required;
59  int32_t pending;
60 };
61 
62 static inline void spa_graph_state_reset(struct spa_graph_state *state)
63 {
64  state->pending = state->required;
65 }
66 
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))
87 
88  return state->status;
89 }
90 struct 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 
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 
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;
113  struct spa_graph_link graph_link;
114  struct spa_graph *subgraph;
115  struct spa_callbacks callbacks;
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 
131 struct 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 
140 static 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)
146  return 0;
147 }
148 
149 static 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;
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)
170 
171  return 0;
172 }
173 
174 static 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 }
181 static 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 
188 static 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 
194 static inline void spa_graph_init(struct spa_graph *graph, struct spa_graph_state *state)
195 {
197  graph->flags = 0;
199  spa_debug("graph %p init state %p", graph, state);
200 }
201 
202 static inline void
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 
220 static inline void
222 {
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;
232  node->graph_link.signal_data = node;
233  spa_debug("node %p init state %p", node, state);
234 }
235 
236 
237 static 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 
247 };
248 
249 static 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 
257 static inline void
259  const struct spa_graph_node_callbacks *callbacks,
260  void *data)
261 {
262  node->callbacks = SPA_CALLBACKS_INIT(callbacks, data);
263 }
264 
265 static 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 
277 static 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);
282  node->state->required--;
283  spa_list_remove(&node->link);
284 }
285 
286 
287 static inline void
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 
299 static inline void
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 
308 static 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 
314 static 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 
322 static inline void
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 
332 static 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)
340 
341  return state->status;
342 }
343 
344 static 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 
353  .process = spa_graph_node_impl_process,
355 };
356 
361 #ifdef __cplusplus
362 } /* extern "C" */
363 #endif
364 
365 #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:268
static void spa_graph_state_reset(struct spa_graph_state *state)
Definition: graph.h:71
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
static const struct spa_graph_node_callbacks spa_graph_node_impl_default
Definition: graph.h:361
static void spa_graph_port_remove(struct spa_graph_port *port)
Definition: graph.h:318
static void spa_graph_init(struct spa_graph *graph, struct spa_graph_state *state)
Definition: graph.h:204
static void spa_graph_node_add(struct spa_graph *graph, struct spa_graph_node *node)
Definition: graph.h:276
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
#define spa_graph_link_signal(l)
Definition: graph.h:83
static int spa_graph_run(struct spa_graph *graph)
Definition: graph.h:159
static int spa_graph_node_impl_sub_process(void *data, struct spa_graph_node *node)
Definition: graph.h:247
static int spa_graph_node_trigger(struct spa_graph_node *node)
Definition: graph.h:150
static int spa_graph_node_impl_process(void *data, struct spa_graph_node *node)
Definition: graph.h:342
static void spa_graph_node_set_subgraph(struct spa_graph_node *node, struct spa_graph *subgraph)
Definition: graph.h:259
static const struct spa_graph_node_callbacks spa_graph_node_sub_impl_default
Definition: graph.h:254
static int spa_graph_link_trigger(struct spa_graph_link *link)
Definition: graph.h:87
static void spa_graph_node_init(struct spa_graph_node *node, struct spa_graph_state *state)
Definition: graph.h:231
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
static int spa_graph_finish(struct spa_graph *graph)
Definition: graph.h:184
#define spa_debug(...)
Definition: graph.h:53
static void spa_graph_node_remove(struct spa_graph_node *node)
Definition: graph.h:287
static void spa_graph_port_add(struct spa_graph_node *node, struct spa_graph_port *port)
Definition: graph.h:310
#define spa_graph_state_dec(s, c)
Definition: graph.h:85
static int spa_graph_link_signal_graph(void *data)
Definition: graph.h:198
static void spa_graph_port_link(struct spa_graph_port *out, struct spa_graph_port *in)
Definition: graph.h:325
#define spa_graph_node_process(n)
Definition: graph.h:138
static void spa_graph_link_remove(struct spa_graph_link *link)
Definition: graph.h:223
static int spa_graph_link_signal_node(void *data)
Definition: graph.h:191
#define SPA_VERSION_GRAPH_NODE_CALLBACKS
Definition: graph.h:108
static void spa_graph_port_unlink(struct spa_graph_port *port)
Definition: graph.h:333
#define SPA_CALLBACKS_INIT(_funcs, _data)
Initialize the set of functions funcs as a spa_callbacks, together with _data.
Definition: hook.h:154
static void spa_list_init(struct spa_list *list)
Definition: list.h:55
static void spa_list_remove(struct spa_list *elem)
Definition: list.h:85
#define spa_list_for_each(pos, head, member)
Definition: list.h:127
#define spa_list_append(list, item)
Definition: list.h:97
#define spa_list_for_each_safe(pos, tmp, head, member)
Definition: list.h:145
#define SPA_STATUS_OK
Definition: io.h:100
#define spa_node_port_reuse_buffer(n,...)
Tell an output port to reuse a buffer.
Definition: node.h:761
#define spa_node_process(n)
Process the node.
Definition: node.h:764
spa_direction
Definition: defs.h:108
@ SPA_DIRECTION_INPUT
Definition: defs.h:109
@ SPA_DIRECTION_OUTPUT
Definition: defs.h:110
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:136
Definition: graph.h:106
int(* process)(void *data, struct spa_graph_node *node)
Definition: graph.h:111
int(* reuse_buffer)(void *data, struct spa_graph_node *node, uint32_t port_id, uint32_t buffer_id)
Definition: graph.h:112
uint32_t version
Definition: graph.h:109
Definition: graph.h:116
uint32_t flags
node flags
Definition: graph.h:121
struct spa_list link
link in graph nodes list
Definition: graph.h:117
struct spa_callbacks callbacks
Definition: graph.h:125
struct spa_graph * graph
owner graph
Definition: graph.h:118
struct spa_graph * subgraph
subgraph or NULL
Definition: graph.h:124
struct spa_graph_link graph_link
link in graph
Definition: graph.h:123
struct spa_list links
list of links to next nodes
Definition: graph.h:120
struct spa_graph_state * state
state of the node
Definition: graph.h:122
struct spa_list ports[2]
list of input and output ports
Definition: graph.h:119
struct spa_list sched_link
link for scheduler
Definition: graph.h:126
Definition: graph.h:141
struct spa_graph_node * node
owner node
Definition: graph.h:143
uint32_t port_id
port id
Definition: graph.h:145
struct spa_graph_port * peer
peer
Definition: graph.h:147
uint32_t flags
port flags
Definition: graph.h:146
enum spa_direction direction
port direction
Definition: graph.h:144
struct spa_list link
link in node port list
Definition: graph.h:142
Definition: graph.h:65
int32_t pending
number of pending signals
Definition: graph.h:68
int32_t required
required number of signals
Definition: graph.h:67
int status
current status
Definition: graph.h:66
Definition: graph.h:99
uint32_t flags
Definition: graph.h:100
struct spa_list nodes
Definition: graph.h:103
struct spa_graph_node * parent
Definition: graph.h:101
struct spa_graph_state * state
Definition: graph.h:102
Definition: list.h:47
Definition: node.h:61