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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/* PipeWire
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "config.h"
#include "pipewire/core.h"
#include "pipewire/interfaces.h"
#include "pipewire/link.h"
#include "pipewire/log.h"
#include "pipewire/module.h"
#include "pipewire/control.h"
#include "pipewire/private.h"
struct impl {
struct pw_core *core;
struct pw_type *t;
struct pw_module *module;
struct pw_properties *properties;
struct spa_hook core_listener;
struct spa_hook module_listener;
struct spa_list node_list;
};
struct node_info {
struct spa_list l;
struct impl *impl;
struct pw_node *node;
struct spa_hook node_listener;
struct spa_list links;
};
struct link_data {
struct spa_list l;
struct node_info *node_info;
struct pw_link *link;
struct spa_hook link_listener;
};
static struct node_info *find_node_info(struct impl *impl, struct pw_node *node)
{
struct node_info *info;
spa_list_for_each(info, &impl->node_list, l) {
if (info->node == node)
return info;
}
return NULL;
}
static void link_data_remove(struct link_data *data)
{
if (data->node_info) {
spa_list_remove(&data->l);
spa_hook_remove(&data->link_listener);
data->node_info = NULL;
}
}
static void node_info_free(struct node_info *info)
{
struct link_data *ld, *t;
spa_list_remove(&info->l);
spa_hook_remove(&info->node_listener);
spa_list_for_each_safe(ld, t, &info->links, l)
link_data_remove(ld);
free(info);
}
static void try_link_port(struct pw_node *node, struct pw_port *port, struct node_info *info);
static void
link_port_unlinked(void *data, struct pw_port *port)
{
struct link_data *ld = data;
struct node_info *info = ld->node_info;
struct pw_link *link = ld->link;
struct impl *impl = info->impl;
struct pw_port *input = pw_link_get_input(link);
pw_log_debug("module %p: link %p: port %p unlinked", impl, link, port);
if (pw_port_get_direction(port) == PW_DIRECTION_OUTPUT && input)
try_link_port(pw_port_get_node(input), input, info);
}
static void
link_state_changed(void *data, enum pw_link_state old, enum pw_link_state state, const char *error)
{
struct link_data *ld = data;
struct node_info *info = ld->node_info;
struct pw_link *link = ld->link;
struct impl *impl = info->impl;
switch (state) {
case PW_LINK_STATE_ERROR:
{
struct pw_global *global = pw_node_get_global(info->node);
struct pw_client *owner = pw_global_get_owner(global);
pw_log_debug("module %p: link %p: state error: %s", impl, link, error);
if (owner)
pw_resource_error(pw_client_get_core_resource(owner), -ENODEV, error);
break;
}
case PW_LINK_STATE_UNLINKED:
pw_log_debug("module %p: link %p: unlinked", impl, link);
break;
case PW_LINK_STATE_INIT:
case PW_LINK_STATE_NEGOTIATING:
case PW_LINK_STATE_ALLOCATING:
case PW_LINK_STATE_PAUSED:
case PW_LINK_STATE_RUNNING:
break;
}
}
static void try_link_controls(struct impl *impl, struct pw_port *port, struct pw_port *target)
{
struct pw_control *cin, *cout;
int res;
pw_log_debug("module %p: trying controls", impl);
spa_list_for_each(cout, &port->control_list[SPA_DIRECTION_OUTPUT], port_link) {
spa_list_for_each(cin, &target->control_list[SPA_DIRECTION_INPUT], port_link) {
if (cin->prop_id == cout->prop_id) {
if ((res = pw_control_link(cout, cin)) < 0)
pw_log_error("failed to link controls: %s", spa_strerror(res));
}
}
}
spa_list_for_each(cin, &port->control_list[SPA_DIRECTION_INPUT], port_link) {
spa_list_for_each(cout, &target->control_list[SPA_DIRECTION_OUTPUT], port_link) {
if (cin->prop_id == cout->prop_id) {
if ((res = pw_control_link(cout, cin)) < 0)
pw_log_error("failed to link controls: %s", spa_strerror(res));
}
}
}
}
static void
link_destroy(void *data)
{
struct link_data *ld = data;
pw_log_debug("module %p: link %p destroyed", ld->node_info->impl, ld->link);
link_data_remove(ld);
}
static const struct pw_link_events link_events = {
PW_VERSION_LINK_EVENTS,
.destroy = link_destroy,
.port_unlinked = link_port_unlinked,
.state_changed = link_state_changed,
};
static void try_link_port(struct pw_node *node, struct pw_port *port, struct node_info *info)
{
struct impl *impl = info->impl;
const struct pw_properties *props;
const char *str;
uint32_t path_id;
char *error = NULL;
struct pw_link *link;
struct pw_port *target;
struct link_data *ld;
struct pw_global *global = pw_node_get_global(info->node);
struct pw_client *owner = pw_global_get_owner(global);
props = pw_node_get_properties(node);
str = pw_properties_get(props, PW_NODE_PROP_TARGET_NODE);
if (str != NULL)
path_id = atoi(str);
else {
str = pw_properties_get(props, PW_NODE_PROP_AUTOCONNECT);
if (str == NULL || !pw_properties_parse_bool(str)) {
pw_log_debug("module %p: node does not need autoconnect", impl);
return;
}
path_id = SPA_ID_INVALID;
}
pw_log_debug("module %p: try to find and link to node '%d'", impl, path_id);
target = pw_core_find_port(impl->core, port, path_id, NULL, 0, NULL, &error);
if (target == NULL)
goto error;
if (pw_port_get_direction(port) == PW_DIRECTION_INPUT) {
struct pw_port *tmp = target;
target = port;
port = tmp;
}
link = pw_link_new(impl->core,
port, target,
NULL, NULL,
&error,
sizeof(struct link_data));
if (link == NULL)
goto error;
ld = pw_link_get_user_data(link);
ld->link = link;
ld->node_info = info;
pw_link_add_listener(link, &ld->link_listener, &link_events, ld);
spa_list_append(&info->links, &ld->l);
pw_link_register(link, NULL, pw_module_get_global(impl->module), NULL);
try_link_controls(impl, port, target);
return;
error:
pw_log_error("module %p: can't link node '%s'", impl, error);
if (owner)
pw_resource_error(pw_client_get_core_resource(owner), -EINVAL, error);
free(error);
return;
}
static void node_port_added(void *data, struct pw_port *port)
{
struct node_info *info = data;
try_link_port(info->node, port, info);
}
static void node_port_removed(void *data, struct pw_port *port)
{
}
static int on_node_port_added(void *data, struct pw_port *port)
{
node_port_added(data, port);
return 0;
}
static void on_node_created(struct pw_node *node, struct node_info *info)
{
pw_node_for_each_port(node, PW_DIRECTION_INPUT, on_node_port_added, info);
pw_node_for_each_port(node, PW_DIRECTION_OUTPUT, on_node_port_added, info);
}
static void
node_state_changed(void *data, enum pw_node_state old, enum pw_node_state state, const char *error)
{
struct node_info *info = data;
if (old == PW_NODE_STATE_CREATING && state == PW_NODE_STATE_SUSPENDED)
on_node_created(info->node, info);
}
static const struct pw_node_events node_events = {
PW_VERSION_NODE_EVENTS,
.port_added = node_port_added,
.port_removed = node_port_removed,
.state_changed = node_state_changed,
};
static void
core_global_added(void *data, struct pw_global *global)
{
struct impl *impl = data;
if (pw_global_get_type(global) == impl->t->node) {
struct pw_node *node = pw_global_get_object(global);
struct node_info *ninfo;
ninfo = calloc(1, sizeof(struct node_info));
ninfo->impl = impl;
ninfo->node = node;
spa_list_init(&ninfo->links);
spa_list_append(&impl->node_list, &ninfo->l);
pw_node_add_listener(node, &ninfo->node_listener, &node_events, ninfo);
pw_log_debug("module %p: node %p added", impl, node);
if (pw_node_get_info(node)->state > PW_NODE_STATE_CREATING)
on_node_created(node, ninfo);
}
}
static void
core_global_removed(void *data, struct pw_global *global)
{
struct impl *impl = data;
if (pw_global_get_type(global) == impl->t->node) {
struct pw_node *node = pw_global_get_object(global);
struct node_info *ninfo;
if ((ninfo = find_node_info(impl, node)))
node_info_free(ninfo);
pw_log_debug("module %p: node %p removed", impl, node);
}
}
static void module_destroy(void *data)
{
struct impl *impl = data;
struct node_info *info, *t;
spa_list_for_each_safe(info, t, &impl->node_list, l)
node_info_free(info);
spa_hook_remove(&impl->core_listener);
spa_hook_remove(&impl->module_listener);
if (impl->properties)
pw_properties_free(impl->properties);
free(impl);
}
static const struct pw_module_events module_events = {
PW_VERSION_MODULE_EVENTS,
.destroy = module_destroy,
};
static const struct pw_core_events core_events = {
PW_VERSION_CORE_EVENTS,
.global_added = core_global_added,
.global_removed = core_global_removed,
};
/**
* module_new:
* @core: #struct pw_core
* @properties: #struct pw_properties
*
* Make a new #struct impl object with given @properties
*
* Returns: a new #struct impl
*/
static int module_init(struct pw_module *module, struct pw_properties *properties)
{
struct pw_core *core = pw_module_get_core(module);
struct impl *impl;
impl = calloc(1, sizeof(struct impl));
if (impl == NULL)
return -ENOMEM;
pw_log_debug("module %p: new", impl);
impl->core = core;
impl->t = pw_core_get_type(core);
impl->module = module;
impl->properties = properties;
spa_list_init(&impl->node_list);
pw_core_add_listener(core, &impl->core_listener, &core_events, impl);
pw_module_add_listener(module, &impl->module_listener, &module_events, impl);
return 0;
}
SPA_EXPORT
int pipewire__module_init(struct pw_module *module, const char *args)
{
return module_init(module, NULL);
}
|