2019-07-10 20:24:11 +02:00
|
|
|
/* 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"
|
|
|
|
|
|
2019-12-11 10:57:10 +01:00
|
|
|
#include <spa/monitor/device.h>
|
2021-01-07 17:30:10 +01:00
|
|
|
#include <spa/monitor/event.h>
|
2019-07-10 20:24:11 +02:00
|
|
|
#include <spa/node/node.h>
|
|
|
|
|
#include <spa/utils/hook.h>
|
2019-12-18 12:15:03 +01:00
|
|
|
#include <spa/utils/result.h>
|
2019-07-10 20:24:11 +02:00
|
|
|
#include <spa/utils/names.h>
|
2019-12-19 13:15:10 +01:00
|
|
|
#include <spa/utils/result.h>
|
2021-01-10 20:53:59 +01:00
|
|
|
#include <spa/utils/keys.h>
|
2020-01-07 16:07:51 +01:00
|
|
|
#include <spa/pod/builder.h>
|
2021-01-07 17:30:10 +01:00
|
|
|
#include <spa/pod/parser.h>
|
2019-07-10 20:24:11 +02:00
|
|
|
#include <spa/param/props.h>
|
|
|
|
|
#include <spa/debug/dict.h>
|
2020-01-07 16:07:51 +01:00
|
|
|
#include <spa/debug/pod.h>
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-12-11 10:57:10 +01:00
|
|
|
#include "pipewire/impl.h"
|
2019-11-20 16:18:46 +01:00
|
|
|
#include "media-session.h"
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2021-01-12 20:38:47 +01:00
|
|
|
#define NAME "bluez5-monitor"
|
|
|
|
|
#define SESSION_CONF "bluez-monitor.conf"
|
2020-01-07 16:07:51 +01:00
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
struct device;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
struct node {
|
2019-11-20 16:18:46 +01:00
|
|
|
struct impl *impl;
|
2020-01-07 16:07:51 +01:00
|
|
|
enum pw_direction direction;
|
2020-01-03 13:02:59 +01:00
|
|
|
struct device *device;
|
2019-07-10 20:24:11 +02:00
|
|
|
struct spa_list link;
|
|
|
|
|
uint32_t id;
|
|
|
|
|
|
|
|
|
|
struct pw_properties *props;
|
|
|
|
|
|
2019-12-11 12:41:45 +01:00
|
|
|
struct pw_impl_node *adapter;
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
struct sm_node *snode;
|
2019-07-10 20:24:11 +02:00
|
|
|
};
|
|
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
struct device {
|
2019-11-20 16:18:46 +01:00
|
|
|
struct impl *impl;
|
2019-07-10 20:24:11 +02:00
|
|
|
struct spa_list link;
|
|
|
|
|
uint32_t id;
|
2020-01-07 16:07:51 +01:00
|
|
|
uint32_t device_id;
|
|
|
|
|
|
|
|
|
|
int priority;
|
|
|
|
|
int profile;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
struct pw_properties *props;
|
|
|
|
|
|
|
|
|
|
struct spa_handle *handle;
|
|
|
|
|
struct spa_device *device;
|
2020-01-07 16:07:51 +01:00
|
|
|
struct spa_hook device_listener;
|
2020-01-03 13:02:59 +01:00
|
|
|
|
|
|
|
|
struct sm_device *sdevice;
|
2020-01-07 16:07:51 +01:00
|
|
|
struct spa_hook listener;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
unsigned int appeared:1;
|
2019-07-10 20:24:11 +02:00
|
|
|
struct spa_list node_list;
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
struct impl {
|
|
|
|
|
struct sm_media_session *session;
|
2019-12-18 12:15:03 +01:00
|
|
|
struct spa_hook session_listener;
|
2019-11-20 16:18:46 +01:00
|
|
|
|
2021-01-12 20:38:47 +01:00
|
|
|
struct pw_properties *conf;
|
2021-01-13 11:42:08 +01:00
|
|
|
struct pw_properties *props;
|
2021-01-12 20:38:47 +01:00
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
struct spa_handle *handle;
|
|
|
|
|
|
|
|
|
|
struct spa_device *monitor;
|
|
|
|
|
struct spa_hook listener;
|
|
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
struct spa_list device_list;
|
2019-11-20 16:18:46 +01:00
|
|
|
};
|
|
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
static struct node *bluez5_find_node(struct device *device, uint32_t id)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2020-01-03 13:02:59 +01:00
|
|
|
struct node *node;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
spa_list_for_each(node, &device->node_list, link) {
|
2019-07-10 20:24:11 +02:00
|
|
|
if (node->id == id)
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
static void bluez5_update_node(struct device *device, struct node *node,
|
2019-07-10 20:24:11 +02:00
|
|
|
const struct spa_device_object_info *info)
|
|
|
|
|
{
|
|
|
|
|
pw_log_debug("update node %u", node->id);
|
2019-10-22 13:06:20 +02:00
|
|
|
|
|
|
|
|
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
|
|
|
|
|
spa_debug_dict(0, info->props);
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
static struct node *bluez5_create_node(struct device *device, uint32_t id,
|
2019-07-10 20:24:11 +02:00
|
|
|
const struct spa_device_object_info *info)
|
|
|
|
|
{
|
2020-01-03 13:02:59 +01:00
|
|
|
struct node *node;
|
|
|
|
|
struct impl *impl = device->impl;
|
2019-12-10 18:19:56 +01:00
|
|
|
struct pw_context *context = impl->session->context;
|
2019-12-11 11:45:27 +01:00
|
|
|
struct pw_impl_factory *factory;
|
2019-07-10 20:24:11 +02:00
|
|
|
int res;
|
2021-01-12 20:38:47 +01:00
|
|
|
const char *prefix, *str, *profile, *rules;
|
2021-01-29 21:52:06 +02:00
|
|
|
int priority;
|
2021-02-17 13:12:22 +01:00
|
|
|
char tmp[1024];
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
pw_log_debug("new node %u", id);
|
|
|
|
|
|
2019-12-19 13:15:10 +01:00
|
|
|
if (strcmp(info->type, SPA_TYPE_INTERFACE_Node) != 0) {
|
2019-07-10 20:24:11 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
node = calloc(1, sizeof(*node));
|
|
|
|
|
if (node == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 14:47:16 +02:00
|
|
|
node->props = pw_properties_new_dict(info->props);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
str = pw_properties_get(device->props, SPA_KEY_DEVICE_DESCRIPTION);
|
2019-07-10 20:24:11 +02:00
|
|
|
if (str == NULL)
|
2020-01-03 13:02:59 +01:00
|
|
|
str = pw_properties_get(device->props, SPA_KEY_DEVICE_NAME);
|
2019-08-12 14:47:16 +02:00
|
|
|
if (str == NULL)
|
2020-01-03 13:02:59 +01:00
|
|
|
str = pw_properties_get(device->props, SPA_KEY_DEVICE_NICK);
|
2019-07-10 20:24:11 +02:00
|
|
|
if (str == NULL)
|
2020-01-03 13:02:59 +01:00
|
|
|
str = pw_properties_get(device->props, SPA_KEY_DEVICE_ALIAS);
|
2019-07-10 20:24:11 +02:00
|
|
|
if (str == NULL)
|
|
|
|
|
str = "bluetooth-device";
|
2019-08-12 14:47:16 +02:00
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
pw_properties_setf(node->props, PW_KEY_DEVICE_ID, "%d", device->device_id);
|
2021-02-17 13:12:22 +01:00
|
|
|
|
|
|
|
|
pw_properties_set(node->props, PW_KEY_NODE_DESCRIPTION,
|
|
|
|
|
sm_media_session_sanitize_description(tmp, sizeof(tmp),
|
|
|
|
|
' ', "%s", str));
|
2021-01-10 20:53:59 +01:00
|
|
|
|
|
|
|
|
profile = pw_properties_get(node->props, SPA_KEY_API_BLUEZ5_PROFILE);
|
|
|
|
|
if (profile == NULL)
|
|
|
|
|
profile = "unknown";
|
|
|
|
|
str = pw_properties_get(node->props, SPA_KEY_API_BLUEZ5_ADDRESS);
|
|
|
|
|
if (str == NULL)
|
|
|
|
|
str = pw_properties_get(device->props, SPA_KEY_DEVICE_NAME);
|
|
|
|
|
|
|
|
|
|
if (strstr(info->factory_name, "sink") != NULL)
|
2021-01-12 20:38:47 +01:00
|
|
|
prefix = "bluez_output";
|
2021-02-12 16:10:58 +01:00
|
|
|
else if (strstr(info->factory_name, "source") != NULL)
|
|
|
|
|
prefix = "bluez_input";
|
2021-01-10 20:53:59 +01:00
|
|
|
else
|
|
|
|
|
prefix = info->factory_name;
|
|
|
|
|
|
2021-02-12 20:47:41 +01:00
|
|
|
pw_properties_set(node->props, PW_KEY_NODE_NAME,
|
2021-02-17 13:12:22 +01:00
|
|
|
sm_media_session_sanitize_name(tmp, sizeof(tmp),
|
2021-02-12 20:47:41 +01:00
|
|
|
'_', "%s.%s.%s", prefix, str, profile));
|
|
|
|
|
|
2020-01-09 09:45:28 +01:00
|
|
|
pw_properties_set(node->props, PW_KEY_FACTORY_NAME, info->factory_name);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2021-01-29 21:52:06 +02:00
|
|
|
if (pw_properties_get(node->props, PW_KEY_PRIORITY_DRIVER) == NULL) {
|
|
|
|
|
priority = device->priority + 10;
|
|
|
|
|
|
2021-02-12 17:23:39 +02:00
|
|
|
if (strstr(info->factory_name, "source") != NULL)
|
2021-01-29 21:52:06 +02:00
|
|
|
priority += 1000;
|
|
|
|
|
|
|
|
|
|
pw_properties_setf(node->props, PW_KEY_PRIORITY_DRIVER, "%d", priority);
|
|
|
|
|
pw_properties_setf(node->props, PW_KEY_PRIORITY_SESSION, "%d", priority);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
node->impl = impl;
|
2020-01-03 13:02:59 +01:00
|
|
|
node->device = device;
|
2019-07-10 20:24:11 +02:00
|
|
|
node->id = id;
|
2019-07-12 10:04:26 +02:00
|
|
|
|
2021-01-12 20:38:47 +01:00
|
|
|
if ((rules = pw_properties_get(impl->conf, "rules")) != NULL)
|
|
|
|
|
sm_media_session_match_rules(rules, strlen(rules), node->props);
|
|
|
|
|
|
2019-12-10 18:19:56 +01:00
|
|
|
factory = pw_context_find_factory(context, "adapter");
|
2019-07-12 10:04:26 +02:00
|
|
|
if (factory == NULL) {
|
|
|
|
|
pw_log_error("no adapter factory found");
|
|
|
|
|
res = -EIO;
|
|
|
|
|
goto clean_node;
|
|
|
|
|
}
|
2019-12-11 11:45:27 +01:00
|
|
|
node->adapter = pw_impl_factory_create_object(factory,
|
2019-07-12 10:04:26 +02:00
|
|
|
NULL,
|
|
|
|
|
PW_TYPE_INTERFACE_Node,
|
2019-12-11 15:26:11 +01:00
|
|
|
PW_VERSION_NODE,
|
2019-07-31 18:30:19 +02:00
|
|
|
pw_properties_copy(node->props),
|
2019-07-12 10:04:26 +02:00
|
|
|
0);
|
|
|
|
|
if (node->adapter == NULL) {
|
2019-07-10 20:24:11 +02:00
|
|
|
res = -errno;
|
|
|
|
|
goto clean_node;
|
|
|
|
|
}
|
2020-01-07 16:07:51 +01:00
|
|
|
node->snode = sm_media_session_export_node(impl->session,
|
2019-12-13 11:26:05 +01:00
|
|
|
&node->props->dict,
|
2020-01-07 16:07:51 +01:00
|
|
|
node->adapter);
|
|
|
|
|
if (node->snode == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto clean_node;
|
|
|
|
|
}
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
spa_list_append(&device->node_list, &node->link);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
bluez5_update_node(device, node, info);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
|
|
|
|
|
clean_node:
|
|
|
|
|
pw_properties_free(node->props);
|
|
|
|
|
free(node);
|
|
|
|
|
exit:
|
|
|
|
|
errno = -res;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
static void bluez5_remove_node(struct device *device, struct node *node)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
|
|
|
|
pw_log_debug("remove node %u", node->id);
|
|
|
|
|
spa_list_remove(&node->link);
|
2020-07-03 16:26:52 +02:00
|
|
|
sm_object_destroy(&node->snode->obj);
|
2019-12-11 12:41:45 +01:00
|
|
|
pw_impl_node_destroy(node->adapter);
|
2019-07-31 18:30:19 +02:00
|
|
|
pw_properties_free(node->props);
|
2019-07-10 20:24:11 +02:00
|
|
|
free(node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void bluez5_device_object_info(void *data, uint32_t id,
|
|
|
|
|
const struct spa_device_object_info *info)
|
|
|
|
|
{
|
2020-01-03 13:02:59 +01:00
|
|
|
struct device *device = data;
|
|
|
|
|
struct node *node;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
node = bluez5_find_node(device, id);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
if (info == NULL) {
|
|
|
|
|
if (node == NULL) {
|
2020-01-03 13:02:59 +01:00
|
|
|
pw_log_warn("device %p: unknown node %u", device, id);
|
2019-07-10 20:24:11 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2020-01-03 13:02:59 +01:00
|
|
|
bluez5_remove_node(device, node);
|
2019-07-10 20:24:11 +02:00
|
|
|
} else if (node == NULL) {
|
2020-01-03 13:02:59 +01:00
|
|
|
bluez5_create_node(device, id, info);
|
2019-07-10 20:24:11 +02:00
|
|
|
} else {
|
2020-01-03 13:02:59 +01:00
|
|
|
bluez5_update_node(device, node, info);
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-07 17:30:10 +01:00
|
|
|
static void bluez_device_event(void *data, const struct spa_event *event)
|
|
|
|
|
{
|
|
|
|
|
struct device *device = data;
|
|
|
|
|
struct node *node;
|
|
|
|
|
uint32_t id, type;
|
|
|
|
|
struct spa_pod *props = NULL;
|
|
|
|
|
|
|
|
|
|
if (spa_pod_parse_object(&event->pod,
|
|
|
|
|
SPA_TYPE_EVENT_Device, &type,
|
|
|
|
|
SPA_EVENT_DEVICE_Object, SPA_POD_Int(&id),
|
|
|
|
|
SPA_EVENT_DEVICE_Props, SPA_POD_OPT_Pod(&props)) < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-02-09 10:57:16 +01:00
|
|
|
if ((node = bluez5_find_node(device, id)) == NULL) {
|
|
|
|
|
pw_log_warn("device %p: unknown node %d", device, id);
|
2021-01-07 17:30:10 +01:00
|
|
|
return;
|
2021-02-09 10:57:16 +01:00
|
|
|
}
|
2021-01-07 17:30:10 +01:00
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
case SPA_DEVICE_EVENT_ObjectConfig:
|
2021-01-09 20:07:32 +01:00
|
|
|
/* FIXME, proxy might be NULL at this point until the
|
|
|
|
|
* node proxy is created. We should probably do
|
|
|
|
|
* pw_client_node_get_node() and perform the set_param on
|
|
|
|
|
* that node proxy instead of waiting for the session manager
|
|
|
|
|
* proxy. */
|
2021-02-09 10:57:16 +01:00
|
|
|
if (props != NULL) {
|
|
|
|
|
if (node->snode->obj.proxy != NULL) {
|
|
|
|
|
pw_node_set_param((struct pw_node*)node->snode->obj.proxy,
|
|
|
|
|
SPA_PARAM_Props, 0, props);
|
|
|
|
|
} else {
|
media-session: deal with global id race conditions
To resolve monitor and policy core global ids racing with each other,
use separate registry event handlers for both cores. Each handles only
their own objects, determined by where the object handle was created.
Postpone handling of policy core new global events after monitor sync,
which orders them after the corresponding monitor proxy and registry
events. Monitor core is then more up-to-date, so we resolve id clashes
in favor of monitor globals, which avoids duplicate objects.
Fix use-after-free by tracking whether a monitor holds references to
sm_object. Keep also objects pending for id in a list, so that they can
be cleaned up on session_shutdown (monitors may leak objects at
shutdown, because spa objectinfo events won't be handled then).
Caveats:
Zombie objects may still created if policy core is late by several
events, but in those cases the corresponding remove events are already
in the queue.
Also, there's a (theoretical) possibility that pw_registry_bind will
bind the wrong object, if the registry event is handled too late and an
id is reused by the server.
For details, see reverted 77e4fdb1e485681635b282579d1c8d26f828a0a6
for which this is a another approach.
2021-02-27 14:55:13 +02:00
|
|
|
pw_log_warn("device %p: node %d object:%p not ready for volume yet",
|
|
|
|
|
device, id, node->snode);
|
2021-02-09 10:57:16 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-01-07 17:30:10 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-10 20:24:11 +02:00
|
|
|
static const struct spa_device_events bluez5_device_events = {
|
|
|
|
|
SPA_VERSION_DEVICE_EVENTS,
|
2021-01-07 17:30:10 +01:00
|
|
|
.object_info = bluez5_device_object_info,
|
|
|
|
|
.event = bluez_device_event,
|
2019-07-10 20:24:11 +02:00
|
|
|
};
|
|
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
static struct device *bluez5_find_device(struct impl *impl, uint32_t id)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2020-01-03 13:02:59 +01:00
|
|
|
struct device *device;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
spa_list_for_each(device, &impl->device_list, link) {
|
|
|
|
|
if (device->id == id)
|
|
|
|
|
return device;
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 12:45:35 +01:00
|
|
|
static int update_device_props(struct device *device)
|
|
|
|
|
{
|
|
|
|
|
struct pw_properties *p = device->props;
|
|
|
|
|
const char *s;
|
|
|
|
|
char temp[32], tmp[1024];
|
|
|
|
|
|
|
|
|
|
s = pw_properties_get(p, SPA_KEY_DEVICE_NAME);
|
|
|
|
|
if (s == NULL)
|
|
|
|
|
s = pw_properties_get(p, SPA_KEY_API_BLUEZ5_ADDRESS);
|
|
|
|
|
if (s == NULL)
|
|
|
|
|
s = pw_properties_get(p, SPA_KEY_DEVICE_DESCRIPTION);
|
|
|
|
|
if (s == NULL) {
|
|
|
|
|
snprintf(temp, sizeof(temp), "%d", device->id);
|
|
|
|
|
s = temp;
|
|
|
|
|
}
|
|
|
|
|
if (strstr(s, "bluez_card.") == s)
|
|
|
|
|
s += strlen("bluez_card.");
|
|
|
|
|
|
|
|
|
|
pw_properties_set(p, PW_KEY_DEVICE_NAME,
|
|
|
|
|
sm_media_session_sanitize_name(tmp, sizeof(tmp),
|
|
|
|
|
'_', "bluez_card.%s", s));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
static void bluez5_update_device(struct impl *impl, struct device *dev,
|
2019-09-20 13:04:14 +02:00
|
|
|
const struct spa_device_object_info *info)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2020-01-07 16:07:51 +01:00
|
|
|
pw_log_debug("update device %u", dev->id);
|
2019-10-22 13:06:20 +02:00
|
|
|
|
|
|
|
|
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
|
|
|
|
|
spa_debug_dict(0, info->props);
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
pw_properties_update(dev->props, info->props);
|
2021-03-04 12:45:35 +01:00
|
|
|
update_device_props(dev);
|
2020-01-07 16:07:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void device_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct device *device = data;
|
|
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
|
|
pw_log_debug("device %p destroy", device);
|
|
|
|
|
|
|
|
|
|
spa_list_consume(node, &device->node_list, link)
|
|
|
|
|
bluez5_remove_node(device, node);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 23:57:29 +02:00
|
|
|
static void device_free(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct device *device = data;
|
|
|
|
|
|
|
|
|
|
pw_log_debug("remove device %u", device->id);
|
|
|
|
|
spa_list_remove(&device->link);
|
|
|
|
|
spa_hook_remove(&device->listener);
|
|
|
|
|
if (device->appeared)
|
|
|
|
|
spa_hook_remove(&device->device_listener);
|
|
|
|
|
|
|
|
|
|
sm_object_discard(&device->sdevice->obj);
|
|
|
|
|
|
|
|
|
|
pw_unload_spa_handle(device->handle);
|
|
|
|
|
pw_properties_free(device->props);
|
|
|
|
|
free(device);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
static void device_update(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct device *device = data;
|
|
|
|
|
|
|
|
|
|
pw_log_debug("device %p appeared %d %d", device, device->appeared, device->profile);
|
|
|
|
|
|
|
|
|
|
if (device->appeared)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
device->device_id = device->sdevice->obj.id;
|
|
|
|
|
device->appeared = true;
|
|
|
|
|
|
|
|
|
|
spa_device_add_listener(device->device,
|
|
|
|
|
&device->device_listener,
|
|
|
|
|
&bluez5_device_events, device);
|
|
|
|
|
|
|
|
|
|
sm_object_sync_update(&device->sdevice->obj);
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
static const struct sm_object_events device_events = {
|
|
|
|
|
SM_VERSION_OBJECT_EVENTS,
|
|
|
|
|
.destroy = device_destroy,
|
2021-03-04 23:57:29 +02:00
|
|
|
.free = device_free,
|
2020-01-07 16:07:51 +01:00
|
|
|
.update = device_update,
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
static struct device *bluez5_create_device(struct impl *impl, uint32_t id,
|
2019-09-20 13:04:14 +02:00
|
|
|
const struct spa_device_object_info *info)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2019-12-10 18:19:56 +01:00
|
|
|
struct pw_context *context = impl->session->context;
|
2020-01-03 13:02:59 +01:00
|
|
|
struct device *device;
|
2019-07-10 20:24:11 +02:00
|
|
|
struct spa_handle *handle;
|
|
|
|
|
int res;
|
|
|
|
|
void *iface;
|
2021-01-12 20:38:47 +01:00
|
|
|
const char *rules;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
pw_log_debug("new device %u", id);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-12-19 13:15:10 +01:00
|
|
|
if (strcmp(info->type, SPA_TYPE_INTERFACE_Device) != 0) {
|
2019-07-10 20:24:11 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-10 18:19:56 +01:00
|
|
|
handle = pw_context_load_spa_handle(context,
|
2019-07-10 20:24:11 +02:00
|
|
|
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) {
|
2019-12-19 13:15:10 +01:00
|
|
|
pw_log_error("can't get %s interface: %s", info->type, spa_strerror(res));
|
2019-07-10 20:24:11 +02:00
|
|
|
goto unload_handle;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
device = calloc(1, sizeof(*device));
|
|
|
|
|
if (device == NULL) {
|
2019-07-10 20:24:11 +02:00
|
|
|
res = -errno;
|
|
|
|
|
goto unload_handle;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
device->impl = impl;
|
|
|
|
|
device->id = id;
|
2021-01-29 21:52:06 +02:00
|
|
|
device->priority = 1000;
|
2020-01-03 13:02:59 +01:00
|
|
|
device->handle = handle;
|
|
|
|
|
device->device = iface;
|
|
|
|
|
device->props = pw_properties_new_dict(info->props);
|
2021-03-04 12:45:35 +01:00
|
|
|
update_device_props(device);
|
2020-01-03 13:02:59 +01:00
|
|
|
device->sdevice = sm_media_session_export_device(impl->session,
|
|
|
|
|
&device->props->dict, device->device);
|
|
|
|
|
if (device->sdevice == NULL) {
|
2019-07-10 20:24:11 +02:00
|
|
|
res = -errno;
|
2020-01-03 13:02:59 +01:00
|
|
|
goto clean_device;
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
spa_list_init(&device->node_list);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2021-01-12 20:38:47 +01:00
|
|
|
if ((rules = pw_properties_get(impl->conf, "rules")) != NULL)
|
|
|
|
|
sm_media_session_match_rules(rules, strlen(rules), device->props);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
sm_object_add_listener(&device->sdevice->obj,
|
|
|
|
|
&device->listener,
|
|
|
|
|
&device_events, device);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
spa_list_append(&impl->device_list, &device->link);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
return device;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
clean_device:
|
|
|
|
|
pw_properties_free(device->props);
|
|
|
|
|
free(device);
|
2019-07-10 20:24:11 +02:00
|
|
|
unload_handle:
|
|
|
|
|
pw_unload_spa_handle(handle);
|
|
|
|
|
exit:
|
|
|
|
|
errno = -res;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
static void bluez5_remove_device(struct impl *impl, struct device *device)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2021-03-04 23:57:29 +02:00
|
|
|
sm_object_destroy(&device->sdevice->obj);
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-20 13:04:14 +02:00
|
|
|
static void bluez5_enum_object_info(void *data, uint32_t id,
|
|
|
|
|
const struct spa_device_object_info *info)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2019-11-20 16:18:46 +01:00
|
|
|
struct impl *impl = data;
|
2020-01-03 13:02:59 +01:00
|
|
|
struct device *device;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2020-01-03 13:02:59 +01:00
|
|
|
device = bluez5_find_device(impl, id);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
if (info == NULL) {
|
2020-01-03 13:02:59 +01:00
|
|
|
if (device == NULL)
|
2019-09-20 13:04:14 +02:00
|
|
|
return;
|
2020-01-03 13:02:59 +01:00
|
|
|
bluez5_remove_device(impl, device);
|
|
|
|
|
} else if (device == NULL) {
|
2020-05-20 13:52:10 +02:00
|
|
|
if (bluez5_create_device(impl, id, info) == NULL)
|
2019-09-20 13:04:14 +02:00
|
|
|
return;
|
2019-07-10 20:24:11 +02:00
|
|
|
} else {
|
2020-01-03 13:02:59 +01:00
|
|
|
bluez5_update_device(impl, device, info);
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-20 13:04:14 +02:00
|
|
|
static const struct spa_device_events bluez5_enum_callbacks =
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2019-09-20 13:04:14 +02:00
|
|
|
SPA_VERSION_DEVICE_EVENTS,
|
|
|
|
|
.object_info = bluez5_enum_object_info,
|
2019-07-10 20:24:11 +02:00
|
|
|
};
|
|
|
|
|
|
2019-12-18 12:15:03 +01:00
|
|
|
static void session_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
spa_hook_remove(&impl->session_listener);
|
|
|
|
|
spa_hook_remove(&impl->listener);
|
|
|
|
|
pw_unload_spa_handle(impl->handle);
|
2021-01-13 11:42:08 +01:00
|
|
|
pw_properties_free(impl->props);
|
2021-01-12 20:38:47 +01:00
|
|
|
pw_properties_free(impl->conf);
|
2019-12-18 12:15:03 +01:00
|
|
|
free(impl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct sm_media_session_events session_events = {
|
|
|
|
|
SM_VERSION_MEDIA_SESSION_EVENTS,
|
|
|
|
|
.destroy = session_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int sm_bluez5_monitor_start(struct sm_media_session *session)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2019-12-10 18:19:56 +01:00
|
|
|
struct pw_context *context = session->context;
|
2019-07-10 20:24:11 +02:00
|
|
|
int res;
|
|
|
|
|
void *iface;
|
2019-11-20 16:18:46 +01:00
|
|
|
struct impl *impl;
|
2021-01-13 11:42:08 +01:00
|
|
|
const char *str;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2021-01-13 11:42:08 +01:00
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL) {
|
2019-07-10 20:24:11 +02:00
|
|
|
res = -errno;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2021-01-13 11:42:08 +01:00
|
|
|
impl->session = session;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2021-01-13 11:42:08 +01:00
|
|
|
if ((impl->conf = pw_properties_new(NULL, NULL)) == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto out_free;
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
2021-01-13 11:42:08 +01:00
|
|
|
if ((res = sm_media_session_load_conf(impl->session,
|
|
|
|
|
SESSION_CONF, impl->conf)) < 0)
|
|
|
|
|
pw_log_info("can't load "SESSION_CONF" config: %s", spa_strerror(res));
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2021-01-13 11:42:08 +01:00
|
|
|
if ((impl->props = pw_properties_new(NULL, NULL)) == NULL) {
|
2019-11-20 16:18:46 +01:00
|
|
|
res = -errno;
|
2021-01-13 11:42:08 +01:00
|
|
|
goto out_free;
|
2019-11-20 16:18:46 +01:00
|
|
|
}
|
2021-01-13 11:42:08 +01:00
|
|
|
if ((str = pw_properties_get(impl->conf, "properties")) != NULL)
|
|
|
|
|
pw_properties_update_string(impl->props, str, strlen(str));
|
|
|
|
|
|
|
|
|
|
impl->handle = pw_context_load_spa_handle(context, SPA_NAME_API_BLUEZ5_ENUM_DBUS, &impl->props->dict);
|
|
|
|
|
if (impl->handle == NULL) {
|
2021-01-12 20:38:47 +01:00
|
|
|
res = -errno;
|
2021-01-13 11:42:08 +01:00
|
|
|
pw_log_error("can't load %s: %m", SPA_NAME_API_BLUEZ5_ENUM_DBUS);
|
|
|
|
|
goto out_free;
|
|
|
|
|
}
|
|
|
|
|
if ((res = spa_handle_get_interface(impl->handle, SPA_TYPE_INTERFACE_Device, &iface)) < 0) {
|
|
|
|
|
pw_log_error("can't get Device interface: %s", spa_strerror(res));
|
2021-01-12 20:38:47 +01:00
|
|
|
goto out_free;
|
|
|
|
|
}
|
2019-11-20 16:18:46 +01:00
|
|
|
impl->monitor = iface;
|
2020-01-03 13:02:59 +01:00
|
|
|
spa_list_init(&impl->device_list);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
spa_device_add_listener(impl->monitor, &impl->listener,
|
|
|
|
|
&bluez5_enum_callbacks, impl);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-12-18 12:15:03 +01:00
|
|
|
sm_media_session_add_listener(session, &impl->session_listener,
|
|
|
|
|
&session_events, impl);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
return 0;
|
2019-12-18 12:15:03 +01:00
|
|
|
|
2021-01-12 20:38:47 +01:00
|
|
|
out_free:
|
2021-01-13 11:42:08 +01:00
|
|
|
if (impl->handle)
|
|
|
|
|
pw_unload_spa_handle(impl->handle);
|
|
|
|
|
if (impl->conf)
|
|
|
|
|
pw_properties_free(impl->conf);
|
|
|
|
|
if (impl->props)
|
|
|
|
|
pw_properties_free(impl->props);
|
2021-01-12 20:38:47 +01:00
|
|
|
free(impl);
|
2019-12-18 12:15:03 +01:00
|
|
|
out:
|
|
|
|
|
return res;
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|