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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
/* PipeWire
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
#include <sys/prctl.h>
#include <pwd.h>
#include <errno.h>
#include <dlfcn.h>
#include <spa/support/cpu.h>
#include "pipewire.h"
#include "private.h"
#define MAX_SUPPORT 32
#define SUPPORTLIB "support/libspa-support"
struct plugin {
struct spa_list link;
char *filename;
void *hnd;
spa_handle_factory_enum_func_t enum_func;
struct spa_list handles;
int ref;
};
struct handle {
struct spa_list link;
struct plugin *plugin;
char *factory_name;
int ref;
struct spa_handle handle;
};
struct registry {
struct spa_list plugins;
};
struct support {
char **categories;
const char *plugin_dir;
const char *support_lib;
struct registry *registry;
struct spa_support support[MAX_SUPPORT];
uint32_t n_support;
};
static struct registry global_registry;
static struct support global_support;
static struct plugin *
find_plugin(struct registry *registry, const char *filename)
{
struct plugin *p;
spa_list_for_each(p, ®istry->plugins, link) {
if (!strcmp(p->filename, filename))
return p;
}
return NULL;
}
static struct plugin *
open_plugin(struct registry *registry,
const char *path,
const char *lib)
{
struct plugin *plugin;
char *filename;
void *hnd;
spa_handle_factory_enum_func_t enum_func;
int res;
if (asprintf(&filename, "%s/%s.so", path, lib) < 0) {
res = -ENOMEM;
goto error_out;
}
if ((plugin = find_plugin(registry, filename)) != NULL) {
free(filename);
plugin->ref++;
return plugin;
}
if ((hnd = dlopen(filename, RTLD_NOW)) == NULL) {
res = -ENOENT;
pw_log_error("can't load %s: %s", filename, dlerror());
goto error_free_filename;
}
if ((enum_func = dlsym(hnd, SPA_HANDLE_FACTORY_ENUM_FUNC_NAME)) == NULL) {
res = -ESRCH;
pw_log_error("can't find enum function");
goto error_dlclose;
}
if ((plugin = calloc(1, sizeof(struct plugin))) == NULL) {
res = -errno;
goto error_dlclose;
}
plugin->ref = 1;
plugin->filename = filename;
plugin->hnd = hnd;
plugin->enum_func = enum_func;
spa_list_init(&plugin->handles);
spa_list_append(®istry->plugins, &plugin->link);
return plugin;
error_dlclose:
dlclose(hnd);
error_free_filename:
free(filename);
error_out:
errno = -res;
return NULL;
}
static void
unref_plugin(struct plugin *plugin)
{
if (--plugin->ref == 0) {
spa_list_remove(&plugin->link);
dlclose(plugin->hnd);
free(plugin->filename);
free(plugin);
}
}
static const struct spa_handle_factory *find_factory(struct plugin *plugin, const char *factory_name)
{
int res = -ENOENT;
uint32_t index;
const struct spa_handle_factory *factory;
for (index = 0;;) {
if ((res = plugin->enum_func(&factory, &index)) <= 0) {
if (res == 0)
break;
goto out;
}
if (strcmp(factory->name, factory_name) == 0)
return factory;
}
res = -ENOENT;
out:
pw_log_error("can't find factory %s: %s", factory_name, spa_strerror(res));
errno = -res;
return NULL;
}
static void unref_handle(struct handle *handle)
{
if (--handle->ref == 0) {
spa_list_remove(&handle->link);
spa_handle_clear(&handle->handle);
unref_plugin(handle->plugin);
free(handle->factory_name);
free(handle);
}
}
static void configure_debug(struct support *support, const char *str)
{
char **level;
int n_tokens;
level = pw_split_strv(str, ":", INT_MAX, &n_tokens);
if (n_tokens > 0)
pw_log_set_level(atoi(level[0]));
if (n_tokens > 1)
support->categories = pw_split_strv(level[1], ",", INT_MAX, &n_tokens);
if (level)
pw_free_strv(level);
}
SPA_EXPORT
uint32_t pw_get_support(struct spa_support *support, uint32_t max_support)
{
uint32_t i, n = SPA_MIN(global_support.n_support, max_support);
for (i = 0; i < n; i++)
support[i] = global_support.support[i];
return n;
}
SPA_EXPORT
struct spa_handle *pw_load_spa_handle(const char *lib,
const char *factory_name,
const struct spa_dict *info,
uint32_t n_support,
const struct spa_support support[])
{
struct support *sup = &global_support;
struct plugin *plugin;
struct handle *handle;
const struct spa_handle_factory *factory;
int res;
if (factory_name == NULL) {
res = -EINVAL;
goto error_out;
}
if (lib == NULL)
lib = sup->support_lib;
pw_log_debug("load \"%s\", \"%s\"", lib, factory_name);
if ((plugin = open_plugin(sup->registry, sup->plugin_dir, lib)) == NULL) {
res = -errno;
pw_log_error("can't load '%s': %m", lib);
goto error_out;
}
factory = find_factory(plugin, factory_name);
if (factory == NULL) {
res = -errno;
pw_log_error("can't find factory '%s': %m %s", factory_name, spa_strerror(res));
goto error_unref_plugin;
}
handle = calloc(1, sizeof(struct handle) + spa_handle_factory_get_size(factory, info));
if (handle == NULL) {
res = -errno;
goto error_unref_plugin;
}
if ((res = spa_handle_factory_init(factory,
&handle->handle, info,
support, n_support)) < 0) {
pw_log_error("can't make factory instance '%s': %d (%s)",
factory_name, res, spa_strerror(res));
goto error_free_handle;
}
handle->ref = 1;
handle->plugin = plugin;
handle->factory_name = strdup(factory_name);
spa_list_append(&plugin->handles, &handle->link);
return &handle->handle;
error_free_handle:
free(handle);
error_unref_plugin:
unref_plugin(plugin);
error_out:
errno = -res;
return NULL;
}
static struct handle *find_handle(struct spa_handle *handle)
{
struct registry *registry = global_support.registry;
struct plugin *p;
struct handle *h;
spa_list_for_each(p, ®istry->plugins, link) {
spa_list_for_each(h, &p->handles, link) {
if (&h->handle == handle)
return h;
}
}
return NULL;
}
SPA_EXPORT
int pw_unload_spa_handle(struct spa_handle *handle)
{
struct handle *h;
if ((h = find_handle(handle)) == NULL)
return -ENOENT;
unref_handle(h);
return 0;
}
static void *add_interface(struct support *support,
const char *factory_name,
uint32_t type,
const struct spa_dict *info)
{
struct spa_handle *handle;
void *iface = NULL;
int res = -ENOENT;
handle = pw_load_spa_handle(support->support_lib,
factory_name, info,
support->n_support, support->support);
if (handle == NULL ||
(res = spa_handle_get_interface(handle, type, &iface)) < 0) {
pw_log_error("can't get %d interface %d", type, res);
} else {
support->support[support->n_support++] =
SPA_SUPPORT_INIT(type, iface);
}
return iface;
}
/** Initialize PipeWire
*
* \param argc pointer to argc
* \param argv pointer to argv
*
* Initialize the PipeWire system, parse and modify any parameters given
* by \a argc and \a argv and set up debugging.
*
* The environment variable \a PIPEWIRE_DEBUG
*
* \memberof pw_pipewire
*/
SPA_EXPORT
void pw_init(int *argc, char **argv[])
{
const char *str;
struct spa_dict_item items[2];
uint32_t n_items;
struct spa_dict info;
struct support *support = &global_support;
struct spa_log *log;
char level[32];
if (support->registry != NULL)
return;
if ((str = getenv("PIPEWIRE_DEBUG")))
configure_debug(support, str);
if ((str = getenv("SPA_PLUGIN_DIR")) == NULL)
str = PLUGINDIR;
support->plugin_dir = str;
if ((str = getenv("SPA_SUPPORT_LIB")) == NULL)
str = SUPPORTLIB;
support->support_lib = str;
spa_list_init(&global_registry.plugins);
support->registry = &global_registry;
n_items = 0;
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_LOG_COLORS, "1");
snprintf(level, sizeof(level), "%d", pw_log_level);
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_LOG_LEVEL, level);
info = SPA_DICT_INIT(items, n_items);
log = add_interface(support, "logger", SPA_TYPE_INTERFACE_Log, &info);
if (log)
pw_log_set(log);
n_items = 0;
if ((str = getenv("PIPEWIRE_CPU")))
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_CPU_FORCE, str);
info = SPA_DICT_INIT(items, n_items);
add_interface(support, "cpu", SPA_TYPE_INTERFACE_CPU, &info);
pw_log_info("version %s", pw_get_library_version());
}
/** Check if a debug category is enabled
*
* \param name the name of the category to check
* \return true if enabled
*
* Debugging categories can be enabled by using the PIPEWIRE_DEBUG
* environment variable
*
* \memberof pw_pipewire
*/
SPA_EXPORT
bool pw_debug_is_category_enabled(const char *name)
{
int i;
if (global_support.categories == NULL)
return false;
for (i = 0; global_support.categories[i]; i++) {
if (strcmp(global_support.categories[i], name) == 0)
return true;
}
return false;
}
/** Get the application name \memberof pw_pipewire */
SPA_EXPORT
const char *pw_get_application_name(void)
{
errno = -ENOTSUP;
return NULL;
}
/** Get the program name \memberof pw_pipewire */
SPA_EXPORT
const char *pw_get_prgname(void)
{
static char tcomm[16 + 1];
spa_zero(tcomm);
if (prctl(PR_GET_NAME, (unsigned long) tcomm, 0, 0, 0) == 0)
return tcomm;
return NULL;
}
/** Get the user name \memberof pw_pipewire */
SPA_EXPORT
const char *pw_get_user_name(void)
{
struct passwd *pw;
if ((pw = getpwuid(getuid())))
return pw->pw_name;
return NULL;
}
/** Get the host name \memberof pw_pipewire */
SPA_EXPORT
const char *pw_get_host_name(void)
{
static char hname[256];
if (gethostname(hname, 256) < 0)
return NULL;
hname[255] = 0;
return hname;
}
/** Get the client name
*
* Make a new PipeWire client name that can be used to construct a remote.
*
* \memberof pw_pipewire
*/
SPA_EXPORT
const char *pw_get_client_name(void)
{
const char *cc;
static char cname[256];
if ((cc = pw_get_application_name()))
return cc;
else if ((cc = pw_get_prgname()))
return cc;
else {
if (snprintf(cname, sizeof(cname), "pipewire-pid-%zd", (size_t) getpid()) < 0)
return NULL;
cname[255] = 0;
return cname;
}
}
/** Fill remote properties
* \param properties a \ref pw_properties
*
* Fill \a properties with a set of default remote properties.
*
* \memberof pw_pipewire
*/
SPA_EXPORT
void pw_fill_remote_properties(struct pw_core *core, struct pw_properties *properties)
{
const char *val;
if (!pw_properties_get(properties, PW_KEY_APP_NAME))
pw_properties_set(properties, PW_KEY_APP_NAME, pw_get_client_name());
if (!pw_properties_get(properties, PW_KEY_APP_PROCESS_BINARY))
pw_properties_set(properties, PW_KEY_APP_PROCESS_BINARY, pw_get_prgname());
if (!pw_properties_get(properties, PW_KEY_APP_LANGUAGE)) {
pw_properties_set(properties, PW_KEY_APP_LANGUAGE, getenv("LANG"));
}
if (!pw_properties_get(properties, PW_KEY_APP_PROCESS_ID)) {
pw_properties_setf(properties, PW_KEY_APP_PROCESS_ID, "%zd", (size_t) getpid());
}
if (!pw_properties_get(properties, PW_KEY_APP_PROCESS_USER))
pw_properties_set(properties, PW_KEY_APP_PROCESS_USER, pw_get_user_name());
if (!pw_properties_get(properties, PW_KEY_APP_PROCESS_HOST))
pw_properties_set(properties, PW_KEY_APP_PROCESS_HOST, pw_get_host_name());
if (!pw_properties_get(properties, PW_KEY_APP_PROCESS_SESSION_ID)) {
pw_properties_set(properties, PW_KEY_APP_PROCESS_SESSION_ID,
getenv("XDG_SESSION_ID"));
}
if (!pw_properties_get(properties, PW_KEY_WINDOW_X11_DISPLAY)) {
pw_properties_set(properties, PW_KEY_WINDOW_X11_DISPLAY,
getenv("DISPLAY"));
}
pw_properties_set(properties, PW_KEY_CORE_VERSION, core->info.version);
pw_properties_set(properties, PW_KEY_CORE_NAME, core->info.name);
if ((val = pw_properties_get(core->properties, PW_KEY_CORE_DAEMON)))
pw_properties_set(properties, PW_KEY_CORE_DAEMON, val);
}
/** Fill stream properties
* \param properties a \ref pw_properties
*
* Fill \a properties with a set of default stream properties.
*
* \memberof pw_pipewire
*/
SPA_EXPORT
void pw_fill_stream_properties(struct pw_core *core, struct pw_properties *properties)
{
}
/** Reverse the direction \memberof pw_pipewire */
SPA_EXPORT
enum pw_direction pw_direction_reverse(enum pw_direction direction)
{
if (direction == PW_DIRECTION_INPUT)
return PW_DIRECTION_OUTPUT;
else if (direction == PW_DIRECTION_OUTPUT)
return PW_DIRECTION_INPUT;
return direction;
}
/** Get the currently running version */
SPA_EXPORT
const char* pw_get_library_version(void)
{
return pw_get_headers_version();
}
static const struct spa_type_info type_info[] = {
{ PW_TYPE_INTERFACE_Core, SPA_TYPE_Pointer, PW_TYPE_INFO_INTERFACE_BASE "Core", NULL },
{ PW_TYPE_INTERFACE_Registry, SPA_TYPE_Pointer, PW_TYPE_INFO_INTERFACE_BASE "Registry", NULL },
{ PW_TYPE_INTERFACE_Node, SPA_TYPE_Pointer, PW_TYPE_INFO_INTERFACE_BASE "Node", NULL },
{ PW_TYPE_INTERFACE_Port, SPA_TYPE_Pointer, PW_TYPE_INFO_INTERFACE_BASE "Port", NULL },
{ PW_TYPE_INTERFACE_Factory, SPA_TYPE_Pointer, PW_TYPE_INFO_INTERFACE_BASE "Factory", NULL },
{ PW_TYPE_INTERFACE_Link, SPA_TYPE_Pointer, PW_TYPE_INFO_INTERFACE_BASE "Link", NULL },
{ PW_TYPE_INTERFACE_Client, SPA_TYPE_Pointer, PW_TYPE_INFO_INTERFACE_BASE "Client", NULL },
{ PW_TYPE_INTERFACE_Module, SPA_TYPE_Pointer, PW_TYPE_INFO_INTERFACE_BASE "Module", NULL },
{ PW_TYPE_INTERFACE_ClientNode, SPA_TYPE_Pointer, PW_TYPE_INFO_INTERFACE_BASE "ClientNode", NULL },
{ PW_TYPE_INTERFACE_Device, SPA_TYPE_Pointer, PW_TYPE_INFO_INTERFACE_BASE "Device", NULL },
{ SPA_ID_INVALID, SPA_ID_INVALID, "spa_types", spa_types },
{ 0, 0, NULL, NULL },
};
SPA_EXPORT
const struct spa_type_info * pw_type_info(void)
{
return type_info;
}
|