PipeWire  0.3.33
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 
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 
50 struct spa_graph;
51 struct spa_graph_node;
52 struct spa_graph_link;
53 struct 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);
70  void *signal_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 }
89 struct 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];
109  struct spa_list links;
110  uint32_t flags;
112  struct spa_graph_link graph_link;
113  struct spa_graph *subgraph;
114  struct spa_callbacks callbacks;
115  struct spa_list sched_link;
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;
133  enum spa_direction direction;
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 
243 /* static */ const struct spa_graph_node_callbacks spa_graph_node_sub_impl_default = {
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;
304  spa_list_append(&node->ports[port->direction], &port->link);
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 
350 /* static */ const struct spa_graph_node_callbacks spa_graph_node_impl_default = {
352  .process = spa_graph_node_impl_process,
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 */
spa_graph_state_reset
void spa_graph_state_reset(struct spa_graph_state *state)
Definition: graph.h:61
spa_graph_node_impl_default
const struct spa_graph_node_callbacks spa_graph_node_impl_default
Definition: graph.h:350
spa_direction
spa_direction
Definition: defs.h:78
spa_graph_port_link
void spa_graph_port_link(struct spa_graph_port *out, struct spa_graph_port *in)
Definition: graph.h:314
spa_graph_init
void spa_graph_init(struct spa_graph *graph, struct spa_graph_state *state)
Definition: graph.h:193
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:101
spa_graph::parent
struct spa_graph_node * parent
Definition: graph.h:91
node.h
spa_graph_finish
int spa_graph_finish(struct spa_graph *graph)
Definition: graph.h:173
spa_graph_node::sched_link
struct spa_list sched_link
link for scheduler
Definition: graph.h:115
spa_graph_state::status
int status
current status
Definition: graph.h:56
node::link
struct spa_list link
link in impl node_list
Definition: module-filter-chain.c:151
data
user data to add to an object
Definition: filter.c:75
spa_graph_port::flags
uint32_t flags
port flags
Definition: graph.h:135
spa_list_for_each
#define spa_list_for_each(pos, head, member)
Definition: list.h:111
SPA_DIRECTION_INPUT
@ SPA_DIRECTION_INPUT
Definition: defs.h:79
spa_graph_node_set_callbacks
void spa_graph_node_set_callbacks(struct spa_graph_node *node, const struct spa_graph_node_callbacks *callbacks, void *data)
Definition: graph.h:257
spa_graph_port::node
struct spa_graph_node * node
owner node
Definition: graph.h:132
node
Definition: module-filter-chain.c:150
spa_graph_link_add
void spa_graph_link_add(struct spa_graph_node *out, struct spa_graph_state *state, struct spa_graph_link *link)
Definition: graph.h:202
spa_graph_node_callbacks::version
uint32_t version
Definition: graph.h:98
spa_graph_node_init
void spa_graph_node_init(struct spa_graph_node *node, struct spa_graph_state *state)
Definition: graph.h:220
spa_graph_state::required
int32_t required
required number of signals
Definition: graph.h:57
spa_node_process
#define spa_node_process(n)
Definition: build-12683127/doc/spa/node/node.h:663
spa_list
Definition: list.h:37
spa_graph_node_sub_impl_default
const struct spa_graph_node_callbacks spa_graph_node_sub_impl_default
Definition: graph.h:243
port::node
struct spa_node * node
Definition: buffers.c:45
spa_graph_link_signal_node
int spa_graph_link_signal_node(void *data)
Definition: graph.h:180
graph
Definition: module-filter-chain.c:191
spa_graph
Definition: graph.h:89
spa_graph_state
Definition: graph.h:55
spa_graph_node_callbacks
Definition: graph.h:96
port::link
struct spa_list link
Definition: filter.c:89
spa_graph_link_signal
#define spa_graph_link_signal(l)
Definition: graph.h:73
spa_graph_node_process
#define spa_graph_node_process(n)
Definition: graph.h:127
spa_graph_port::direction
enum spa_direction direction
port direction
Definition: graph.h:133
spa_graph::state
struct spa_graph_state * state
Definition: graph.h:92
spa_list_remove
void spa_list_remove(struct spa_list *elem)
Definition: list.h:69
port
Definition: buffers.c:44
spa_graph::flags
uint32_t flags
Definition: graph.h:90
spa_graph_run
int spa_graph_run(struct spa_graph *graph)
Definition: graph.h:148
spa_graph_node::flags
uint32_t flags
node flags
Definition: graph.h:110
spa_graph_node::callbacks
struct spa_callbacks callbacks
Definition: graph.h:114
spa_node
Definition: build-12683127/doc/spa/node/node.h:53
spa_callbacks
Callbacks, contains the structure with functions and the data passed to the functions.
Definition: hook.h:130
spa_graph_state_dec
#define spa_graph_state_dec(s, c)
Definition: graph.h:75
spa_graph_port_remove
void spa_graph_port_remove(struct spa_graph_port *port)
Definition: graph.h:307
spa_graph_port_unlink
void spa_graph_port_unlink(struct spa_graph_port *port)
Definition: graph.h:322
spa_graph_port::port_id
uint32_t port_id
port id
Definition: graph.h:134
spa_node_port_reuse_buffer
#define spa_node_port_reuse_buffer(n,...)
Definition: build-12683127/doc/spa/node/node.h:662
spa_graph_link_signal_graph
int spa_graph_link_signal_graph(void *data)
Definition: graph.h:187
spa_list_init
void spa_list_init(struct spa_list *list)
Definition: list.h:44
spa_list_for_each_safe
#define spa_list_for_each_safe(pos, tmp, head, member)
Definition: list.h:129
spa_graph_node::ports
struct spa_list ports[2]
list of input and output ports
Definition: graph.h:108
spa_debug
#define spa_debug(...)
Definition: graph.h:47
SPA_DIRECTION_OUTPUT
@ SPA_DIRECTION_OUTPUT
Definition: defs.h:80
spa_graph_node::graph
struct spa_graph * graph
owner graph
Definition: graph.h:107
port::port_id
uint32_t port_id
Definition: buffers.c:47
node::graph
struct graph * graph
Definition: module-filter-chain.c:152
port::flags
uint32_t flags
Definition: filter.c:95
spa_graph_node_set_subgraph
void spa_graph_node_set_subgraph(struct spa_graph_node *node, struct spa_graph *subgraph)
Definition: graph.h:248
spa_graph_link_trigger
int spa_graph_link_trigger(struct spa_graph_link *link)
Definition: graph.h:77
spa_graph_node_impl_process
int spa_graph_node_impl_process(void *data, struct spa_graph_node *node)
Definition: graph.h:331
spa_graph_node
Definition: graph.h:105
spa_graph_node::graph_link
struct spa_graph_link graph_link
link in graph
Definition: graph.h:112
io.h
spa_graph_port::peer
struct spa_graph_port * peer
peer
Definition: graph.h:136
spa_graph_port
Definition: graph.h:130
spa_graph_port_add
void spa_graph_port_add(struct spa_graph_node *node, struct spa_graph_port *port)
Definition: graph.h:299
spa_graph_link_remove
void spa_graph_link_remove(struct spa_graph_link *link)
Definition: graph.h:212
spa_graph_node_callbacks::process
int(* process)(void *data, struct spa_graph_node *node)
Definition: graph.h:100
spa_graph_node_impl_sub_process
int spa_graph_node_impl_sub_process(void *data, struct spa_graph_node *node)
Definition: graph.h:236
defs.h
list.h
spa_graph_node_remove
void spa_graph_node_remove(struct spa_graph_node *node)
Definition: graph.h:276
spa_graph_node_impl_reuse_buffer
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
spa_graph_port_init
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
spa_graph_node_trigger
int spa_graph_node_trigger(struct spa_graph_node *node)
Definition: graph.h:139
SPA_STATUS_OK
#define SPA_STATUS_OK
Definition: io.h:94
spa_graph_node::state
struct spa_graph_state * state
state of the node
Definition: graph.h:111
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:142
hook.h
port::direction
enum spa_direction direction
Definition: buffers.c:46
spa_graph_node::links
struct spa_list links
list of links to next nodes
Definition: graph.h:109
spa_graph_node::subgraph
struct spa_graph * subgraph
subgraph or NULL
Definition: graph.h:113
SPA_VERSION_GRAPH_NODE_CALLBACKS
#define SPA_VERSION_GRAPH_NODE_CALLBACKS
Definition: graph.h:97
spa_graph_state::pending
int32_t pending
number of pending signals
Definition: graph.h:58
spa_graph::nodes
struct spa_list nodes
Definition: graph.h:93
spa_list_append
#define spa_list_append(list, item)
Definition: list.h:81
spa_graph_node_add
void spa_graph_node_add(struct spa_graph *graph, struct spa_graph_node *node)
Definition: graph.h:265