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
|
/* PipeWire
*
* Copyright © 2019 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.
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <time.h>
#include "config.h"
#include <spa/node/node.h>
#include <spa/utils/hook.h>
#include <spa/utils/names.h>
#include <spa/param/audio/format-utils.h>
#include <spa/param/props.h>
#include <spa/debug/dict.h>
#include "pipewire/pipewire.h"
#include "pipewire/private.h"
struct bluez5_object;
struct bluez5_node {
struct monitor *monitor;
struct bluez5_object *object;
struct spa_list link;
uint32_t id;
struct pw_properties *props;
struct pw_node *adapter;
struct pw_proxy *proxy;
};
struct bluez5_object {
struct monitor *monitor;
struct spa_list link;
uint32_t id;
struct pw_properties *props;
struct spa_handle *handle;
struct pw_proxy *proxy;
struct spa_device *device;
struct spa_hook device_listener;
struct spa_list node_list;
};
static struct bluez5_node *bluez5_find_node(struct bluez5_object *obj, uint32_t id)
{
struct bluez5_node *node;
spa_list_for_each(node, &obj->node_list, link) {
if (node->id == id)
return node;
}
return NULL;
}
static void bluez5_update_node(struct bluez5_object *obj, struct bluez5_node *node,
const struct spa_device_object_info *info)
{
pw_log_debug("update node %u", node->id);
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
spa_debug_dict(0, info->props);
}
static struct bluez5_node *bluez5_create_node(struct bluez5_object *obj, uint32_t id,
const struct spa_device_object_info *info)
{
struct bluez5_node *node;
struct monitor *monitor = obj->monitor;
struct impl *impl = monitor->impl;
struct pw_core *core = impl->session->core;
struct pw_factory *factory;
int res;
const char *str;
pw_log_debug("new node %u", id);
if (info->type != SPA_TYPE_INTERFACE_Node) {
errno = EINVAL;
return NULL;
}
node = calloc(1, sizeof(*node));
if (node == NULL) {
res = -errno;
goto exit;
}
node->props = pw_properties_new_dict(info->props);
str = pw_properties_get(obj->props, SPA_KEY_DEVICE_DESCRIPTION);
if (str == NULL)
str = pw_properties_get(obj->props, SPA_KEY_DEVICE_NAME);
if (str == NULL)
str = pw_properties_get(obj->props, SPA_KEY_DEVICE_NICK);
if (str == NULL)
str = pw_properties_get(obj->props, SPA_KEY_DEVICE_ALIAS);
if (str == NULL)
str = "bluetooth-device";
pw_properties_setf(node->props, PW_KEY_NODE_NAME, "%s.%s", info->factory_name, str);
pw_properties_set(node->props, PW_KEY_NODE_DESCRIPTION, str);
pw_properties_set(node->props, "factory.name", info->factory_name);
node->monitor = monitor;
node->object = obj;
node->id = id;
factory = pw_core_find_factory(core, "adapter");
if (factory == NULL) {
pw_log_error("no adapter factory found");
res = -EIO;
goto clean_node;
}
node->adapter = pw_factory_create_object(factory,
NULL,
PW_TYPE_INTERFACE_Node,
PW_VERSION_NODE_PROXY,
pw_properties_copy(node->props),
0);
if (node->adapter == NULL) {
res = -errno;
goto clean_node;
}
node->proxy = sm_media_session_export(impl->session,
PW_TYPE_INTERFACE_Node,
pw_properties_copy(node->props),
node->adapter, 0);
spa_list_append(&obj->node_list, &node->link);
bluez5_update_node(obj, node, info);
return node;
clean_node:
pw_properties_free(node->props);
free(node);
exit:
errno = -res;
return NULL;
}
static void bluez5_remove_node(struct bluez5_object *obj, struct bluez5_node *node)
{
pw_log_debug("remove node %u", node->id);
spa_list_remove(&node->link);
pw_node_destroy(node->adapter);
pw_properties_free(node->props);
free(node);
}
static void bluez5_device_object_info(void *data, uint32_t id,
const struct spa_device_object_info *info)
{
struct bluez5_object *obj = data;
struct bluez5_node *node;
node = bluez5_find_node(obj, id);
if (info == NULL) {
if (node == NULL) {
pw_log_warn("object %p: unknown node %u", obj, id);
return;
}
bluez5_remove_node(obj, node);
} else if (node == NULL) {
bluez5_create_node(obj, id, info);
} else {
bluez5_update_node(obj, node, info);
}
}
static const struct spa_device_events bluez5_device_events = {
SPA_VERSION_DEVICE_EVENTS,
.object_info = bluez5_device_object_info
};
static struct bluez5_object *bluez5_find_object(struct monitor *monitor, uint32_t id)
{
struct bluez5_object *obj;
spa_list_for_each(obj, &monitor->object_list, link) {
if (obj->id == id)
return obj;
}
return NULL;
}
static void bluez5_update_object(struct monitor *monitor, struct bluez5_object *obj,
const struct spa_device_object_info *info)
{
pw_log_debug("update object %u", obj->id);
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
spa_debug_dict(0, info->props);
}
static struct bluez5_object *bluez5_create_object(struct monitor *monitor, uint32_t id,
const struct spa_device_object_info *info)
{
struct impl *impl = monitor->impl;
struct pw_core *core = impl->session->core;
struct bluez5_object *obj;
struct spa_handle *handle;
int res;
void *iface;
pw_log_debug("new object %u", id);
if (info->type != SPA_TYPE_INTERFACE_Device) {
errno = EINVAL;
return NULL;
}
handle = pw_core_load_spa_handle(core,
info->factory_name,
info->props);
if (handle == NULL) {
res = -errno;
pw_log_error("can't make factory instance: %m");
goto exit;
}
if ((res = spa_handle_get_interface(handle, info->type, &iface)) < 0) {
pw_log_error("can't get %d interface: %d", info->type, res);
goto unload_handle;
}
obj = calloc(1, sizeof(*obj));
if (obj == NULL) {
res = -errno;
goto unload_handle;
}
obj->monitor = monitor;
obj->id = id;
obj->handle = handle;
obj->device = iface;
obj->props = pw_properties_new_dict(info->props);
obj->proxy = sm_media_session_export(impl->session,
info->type, pw_properties_copy(obj->props), obj->device, 0);
if (obj->proxy == NULL) {
res = -errno;
goto clean_object;
}
spa_list_init(&obj->node_list);
spa_device_add_listener(obj->device,
&obj->device_listener, &bluez5_device_events, obj);
spa_list_append(&monitor->object_list, &obj->link);
bluez5_update_object(monitor, obj, info);
return obj;
clean_object:
free(obj);
unload_handle:
pw_unload_spa_handle(handle);
exit:
errno = -res;
return NULL;
}
static void bluez5_remove_object(struct monitor *monitor, struct bluez5_object *obj)
{
struct bluez5_node *node;
pw_log_debug("remove object %u", obj->id);
spa_list_remove(&obj->link);
spa_hook_remove(&obj->device_listener);
spa_list_consume(node, &obj->node_list, link)
bluez5_remove_node(obj, node);
pw_proxy_destroy(obj->proxy);
pw_unload_spa_handle(obj->handle);
free(obj);
}
static void bluez5_enum_object_info(void *data, uint32_t id,
const struct spa_device_object_info *info)
{
struct monitor *monitor = data;
struct bluez5_object *obj;
obj = bluez5_find_object(monitor, id);
if (info == NULL) {
if (obj == NULL)
return;
bluez5_remove_object(monitor, obj);
} else if (obj == NULL) {
if ((obj = bluez5_create_object(monitor, id, info)) == NULL)
return;
} else {
bluez5_update_object(monitor, obj, info);
}
}
static const struct spa_device_events bluez5_enum_callbacks =
{
SPA_VERSION_DEVICE_EVENTS,
.object_info = bluez5_enum_object_info,
};
static int bluez5_start_monitor(struct impl *impl, struct monitor *monitor)
{
struct spa_handle *handle;
struct pw_core *core = impl->session->core;
int res;
void *iface;
handle = pw_core_load_spa_handle(core, SPA_NAME_API_BLUEZ5_ENUM_DBUS, NULL);
if (handle == NULL) {
res = -errno;
goto out;
}
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Device, &iface)) < 0) {
pw_log_error("can't get Device interface: %d", res);
goto out_unload;
}
monitor->impl = impl;
monitor->handle = handle;
monitor->monitor = iface;
spa_list_init(&monitor->object_list);
spa_device_add_listener(monitor->monitor, &monitor->listener,
&bluez5_enum_callbacks, monitor);
return 0;
out_unload:
pw_unload_spa_handle(handle);
out:
return res;
}
|