summaryrefslogtreecommitdiffstats
path: root/third_party/pipewire/spa/graph/graph.h
blob: cf3f528cf1cac34b9942995d3ff75063fcdf7d1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* Simple Plugin API
 *
 * Copyright © 2018 Wim Taymans
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#ifndef SPA_GRAPH_H
#define SPA_GRAPH_H

#ifdef __cplusplus
extern "C" {
#endif

#include <spa/utils/defs.h>
#include <spa/utils/list.h>
#include <spa/utils/hook.h>
#include <spa/node/node.h>
#include <spa/node/io.h>

#ifndef spa_debug
#define spa_debug(...)
#endif

struct spa_graph;
struct spa_graph_node;
struct spa_graph_link;
struct spa_graph_port;

struct spa_graph_state {
	int status;			/**< current status */
	int32_t required;		/**< required number of signals */
	int32_t pending;		/**< number of pending signals */
};

static inline void spa_graph_state_reset(struct spa_graph_state *state)
{
	state->pending = state->required;
}

struct spa_graph_link {
	struct spa_list link;
	struct spa_graph_state *state;
	int (*signal) (void *data);
	void *signal_data;
};

#define spa_graph_link_signal(l)	((l)->signal((l)->signal_data))

#define spa_graph_state_dec(s,c) (__atomic_sub_fetch(&(s)->pending, c, __ATOMIC_SEQ_CST) == 0)

static inline int spa_graph_link_trigger(struct spa_graph_link *link)
{
	struct spa_graph_state *state = link->state;

	spa_debug("link %p: state %p: pending %d/%d", link, state,
                        state->pending, state->required);

	if (spa_graph_state_dec(state, 1))
		spa_graph_link_signal(link);

        return state->status;
}
struct spa_graph {
	uint32_t flags;			/* flags */
	struct spa_graph_node *parent;	/* parent node or NULL when driver */
	struct spa_graph_state *state;	/* state of graph */
	struct spa_list nodes;		/* list of nodes of this graph */
};

struct spa_graph_node_callbacks {
#define SPA_VERSION_GRAPH_NODE_CALLBACKS	0
	uint32_t version;

	int (*process) (void *data, struct spa_graph_node *node);
	int (*reuse_buffer) (void *data, struct spa_graph_node *node,
			uint32_t port_id, uint32_t buffer_id);
};

struct spa_graph_node {
	struct spa_list link;		/**< link in graph nodes list */
	struct spa_graph *graph;	/**< owner graph */
	struct spa_list ports[2];	/**< list of input and output ports */
	struct spa_list links;		/**< list of links to next nodes */
	uint32_t flags;			/**< node flags */
	struct spa_graph_state *state;	/**< state of the node */
	struct spa_graph_link graph_link;	/**< link in graph */
	struct spa_graph *subgraph;	/**< subgraph or NULL */
	struct spa_callbacks callbacks;
	struct spa_list sched_link;	/**< link for scheduler */
};

#define spa_graph_node_call(n,method,version,...)			\
({									\
	int __res = 0;							\
	spa_callbacks_call_res(&(n)->callbacks,				\
			struct spa_graph_node_callbacks, __res,		\
			method, version, ##__VA_ARGS__);		\
	__res;								\
})

#define spa_graph_node_process(n)		spa_graph_node_call(n, process, 0, n)
#define spa_graph_node_reuse_buffer(n,p,i)	spa_graph_node_call(n, reuse_buffer, 0, n, p, i)

struct spa_graph_port {
	struct spa_list link;		/**< link in node port list */
	struct spa_graph_node *node;	/**< owner node */
	enum spa_direction direction;	/**< port direction */
	uint32_t port_id;		/**< port id */
	uint32_t flags;			/**< port flags */
	struct spa_graph_port *peer;	/**< peer */
};

static inline int spa_graph_node_trigger(struct spa_graph_node *node)
{
	struct spa_graph_link *l;
	spa_debug("node %p trigger", node);
	spa_list_for_each(l, &node->links, link)
		spa_graph_link_trigger(l);
	return 0;
}

static inline int spa_graph_run(struct spa_graph *graph)
{
	struct spa_graph_node *n, *t;
	struct spa_list pending;

	spa_graph_state_reset(graph->state);
	spa_debug("graph %p run with state %p pending %d/%d", graph, graph->state,
			graph->state->pending, graph->state->required);

	spa_list_init(&pending);

	spa_list_for_each(n, &graph->nodes, link) {
		struct spa_graph_state *s = n->state;
		spa_graph_state_reset(s);
		spa_debug("graph %p node %p: state %p pending %d/%d status %d", graph, n,
				s, s->pending, s->required, s->status);
		if (--s->pending == 0)
			spa_list_append(&pending, &n->sched_link);
	}
	spa_list_for_each_safe(n, t, &pending, sched_link)
		spa_graph_node_process(n);

	return 0;
}

static inline int spa_graph_finish(struct spa_graph *graph)
{
	spa_debug("graph %p finish", graph);
	if (graph->parent)
		return spa_graph_node_trigger(graph->parent);
	return 0;
}
static inline int spa_graph_link_signal_node(void *data)
{
	struct spa_graph_node *node = (struct spa_graph_node *)data;
	spa_debug("node %p call process", node);
	return spa_graph_node_process(node);
}

static inline int spa_graph_link_signal_graph(void *data)
{
	struct spa_graph_node *node = (struct spa_graph_node *)data;
	return spa_graph_finish(node->graph);
}

static inline void spa_graph_init(struct spa_graph *graph, struct spa_graph_state *state)
{
	spa_list_init(&graph->nodes);
	graph->flags = 0;
	graph->state = state;
	spa_debug("graph %p init state %p", graph, state);
}

static inline void
spa_graph_link_add(struct spa_graph_node *out,
		   struct spa_graph_state *state,
		   struct spa_graph_link *link)
{
	link->state = state;
	state->required++;
	spa_debug("node %p add link %p to state %p %d", out, link, state, state->required);
	spa_list_append(&out->links, &link->link);
}

static inline void spa_graph_link_remove(struct spa_graph_link *link)
{
	link->state->required--;
	spa_debug("link %p state %p remove %d", link, link->state, link->state->required);
	spa_list_remove(&link->link);
}

static inline void
spa_graph_node_init(struct spa_graph_node *node, struct spa_graph_state *state)
{
	spa_list_init(&node->ports[SPA_DIRECTION_INPUT]);
	spa_list_init(&node->ports[SPA_DIRECTION_OUTPUT]);
	spa_list_init(&node->links);
	node->flags = 0;
	node->subgraph = NULL;
	node->state = state;
	node->state->required = node->state->pending = 0;
	node->state->status = SPA_STATUS_OK;
	node->graph_link.signal = spa_graph_link_signal_graph;
	node->graph_link.signal_data = node;
	spa_debug("node %p init state %p", node, state);
}


static inline int spa_graph_node_impl_sub_process(void *data, struct spa_graph_node *node)
{
	struct spa_graph *graph = node->subgraph;
	spa_debug("node %p: sub process %p", node, graph);
	return spa_graph_run(graph);
}

static const struct spa_graph_node_callbacks spa_graph_node_sub_impl_default = {
	SPA_VERSION_GRAPH_NODE_CALLBACKS,
	.process = spa_graph_node_impl_sub_process,
};

static inline void spa_graph_node_set_subgraph(struct spa_graph_node *node,
		struct spa_graph *subgraph)
{
	node->subgraph = subgraph;
	subgraph->parent = node;
	spa_debug("node %p set subgraph %p", node, subgraph);
}

static inline void
spa_graph_node_set_callbacks(struct spa_graph_node *node,
		const struct spa_graph_node_callbacks *callbacks,
		void *data)
{
	node->callbacks = SPA_CALLBACKS_INIT(callbacks, data);
}

static inline void
spa_graph_node_add(struct spa_graph *graph,
		   struct spa_graph_node *node)
{
	node->graph = graph;
	spa_list_append(&graph->nodes, &node->link);
	node->state->required++;
	spa_debug("node %p add to graph %p, state %p required %d",
			node, graph, node->state, node->state->required);
	spa_graph_link_add(node, graph->state, &node->graph_link);
}

static inline void spa_graph_node_remove(struct spa_graph_node *node)
{
	spa_debug("node %p remove from graph %p, state %p required %d",
			node, node->graph, node->state, node->state->required);
	spa_graph_link_remove(&node->graph_link);
	node->state->required--;
	spa_list_remove(&node->link);
}


static inline void
spa_graph_port_init(struct spa_graph_port *port,
		    enum spa_direction direction,
		    uint32_t port_id,
		    uint32_t flags)
{
	spa_debug("port %p init type %d id %d", port, direction, port_id);
	port->direction = direction;
	port->port_id = port_id;
	port->flags = flags;
}

static inline void
spa_graph_port_add(struct spa_graph_node *node,
		   struct spa_graph_port *port)
{
	spa_debug("port %p add to node %p", port, node);
	port->node = node;
	spa_list_append(&node->ports[port->direction], &port->link);
}

static inline void spa_graph_port_remove(struct spa_graph_port *port)
{
	spa_debug("port %p remove", port);
	spa_list_remove(&port->link);
}

static inline void
spa_graph_port_link(struct spa_graph_port *out, struct spa_graph_port *in)
{
	spa_debug("port %p link to %p %p %p", out, in, in->node, in->node->state);
	out->peer = in;
	in->peer = out;
}

static inline void
spa_graph_port_unlink(struct spa_graph_port *port)
{
	spa_debug("port %p unlink from %p", port, port->peer);
	if (port->peer) {
		port->peer->peer = NULL;
		port->peer = NULL;
	}
}

static inline int spa_graph_node_impl_process(void *data, struct spa_graph_node *node)
{
	struct spa_node *n = (struct spa_node *)data;
	struct spa_graph_state *state = node->state;

	spa_debug("node %p: process state %p: %d, node %p", node, state, state->status, n);
	if ((state->status = spa_node_process(n)) != SPA_STATUS_OK)
		spa_graph_node_trigger(node);

        return state->status;
}

static inline int spa_graph_node_impl_reuse_buffer(void *data, struct spa_graph_node *node,
		uint32_t port_id, uint32_t buffer_id)
{
	struct spa_node *n = (struct spa_node *)data;
	return spa_node_port_reuse_buffer(n, port_id, buffer_id);
}

static const struct spa_graph_node_callbacks spa_graph_node_impl_default = {
	SPA_VERSION_GRAPH_NODE_CALLBACKS,
	.process = spa_graph_node_impl_process,
	.reuse_buffer = spa_graph_node_impl_reuse_buffer,
};

#ifdef __cplusplus
}  /* extern "C" */
#endif

#endif /* SPA_GRAPH_H */