bluez-session.c

Using the SPA Device API, among other things.

/* 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.
*/
/*
[title]
Using the \ref spa_device "SPA Device API", among other things.
[title]
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <time.h>
#include "config.h"
#include <spa/monitor/device.h>
#include <spa/node/node.h>
#include <spa/utils/hook.h>
#include <spa/utils/names.h>
#include <spa/utils/result.h>
#include <spa/utils/string.h>
#include <spa/param/audio/format-utils.h>
#include <spa/param/props.h>
#include <spa/debug/dict.h>
#include "pipewire/pipewire.h"
#define NAME "bluez-session"
struct impl;
struct object;
struct node {
struct impl *impl;
struct object *object;
struct spa_list link;
uint32_t id;
struct spa_handle *handle;
struct pw_proxy *proxy;
struct spa_node *node;
};
struct object {
struct impl *impl;
struct spa_list link;
uint32_t id;
struct spa_handle *handle;
struct pw_proxy *proxy;
struct spa_device *device;
struct spa_hook listener;
struct spa_list node_list;
};
struct impl {
struct timespec now;
struct pw_main_loop *loop;
struct pw_context *context;
struct pw_core *core;
struct spa_hook core_listener;
struct spa_handle *handle;
struct spa_device *device;
struct spa_hook listener;
struct spa_list device_list;
};
static struct node *find_node(struct object *obj, uint32_t id)
{
struct node *node;
spa_list_for_each(node, &obj->node_list, link) {
if (node->id == id)
return node;
}
return NULL;
}
static void update_node(struct object *obj, struct node *node,
const struct spa_device_object_info *info)
{
pw_log_debug("update node %u", node->id);
spa_debug_dict(0, info->props);
}
static struct node *create_node(struct object *obj, uint32_t id,
const struct spa_device_object_info *info)
{
struct node *node;
struct impl *impl = obj->impl;
struct pw_context *context = impl->context;
struct spa_handle *handle;
int res;
void *iface;
pw_log_debug("new node %u", id);
if (!spa_streq(info->type, SPA_TYPE_INTERFACE_Node))
return NULL;
handle = pw_context_load_spa_handle(context,
info->factory_name,
info->props);
if (handle == NULL) {
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 %s interface: %s", info->type, spa_strerror(res));
goto unload_handle;
}
node = calloc(1, sizeof(*node));
if (node == NULL)
goto unload_handle;
node->impl = impl;
node->object = obj;
node->id = id;
node->handle = handle;
node->node = iface;
node->proxy = pw_core_export(impl->core,
info->type, info->props, node->node, 0);
if (node->proxy == NULL)
goto clean_node;
spa_list_append(&obj->node_list, &node->link);
update_node(obj, node, info);
return node;
clean_node:
free(node);
unload_handle:
pw_unload_spa_handle(handle);
exit:
return NULL;
}
static void remove_node(struct object *obj, struct node *node)
{
pw_log_debug("remove node %u", node->id);
spa_list_remove(&node->link);
pw_proxy_destroy(node->proxy);
free(node->handle);
free(node);
}
static void device_object_info(void *data, uint32_t id,
const struct spa_device_object_info *info)
{
struct object *obj = data;
struct node *node;
node = find_node(obj, id);
if (info == NULL) {
if (node == NULL) {
pw_log_warn("object %p: unknown node %u", obj, id);
return;
}
remove_node(obj, node);
} else if (node == NULL) {
create_node(obj, id, info);
} else {
update_node(obj, node, info);
}
}
static const struct spa_device_events device_events = {
SPA_VERSION_DEVICE_EVENTS,
.object_info = device_object_info
};
static struct object *find_object(struct impl *impl, uint32_t id)
{
struct object *obj;
spa_list_for_each(obj, &impl->device_list, link) {
if (obj->id == id)
return obj;
}
return NULL;
}
static void update_object(struct impl *impl, struct object *obj,
const struct spa_device_object_info *info)
{
pw_log_debug("update object %u", obj->id);
spa_debug_dict(0, info->props);
}
static struct object *create_object(struct impl *impl, uint32_t id,
const struct spa_device_object_info *info)
{
struct pw_context *context = impl->context;
struct object *obj;
struct spa_handle *handle;
int res;
void *iface;
pw_log_debug("new object %u", id);
if (!spa_streq(info->type, SPA_TYPE_INTERFACE_Device))
return NULL;
handle = pw_context_load_spa_handle(context,
info->factory_name,
info->props);
if (handle == NULL) {
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 %s interface: %s", info->type, spa_strerror(res));
goto unload_handle;
}
obj = calloc(1, sizeof(*obj));
if (obj == NULL)
goto unload_handle;
obj->impl = impl;
obj->id = id;
obj->handle = handle;
obj->device = iface;
obj->proxy = pw_core_export(impl->core,
info->type, info->props, obj->device, 0);
if (obj->proxy == NULL)
goto clean_object;
spa_list_init(&obj->node_list);
spa_device_add_listener(obj->device,
&obj->listener, &device_events, obj);
spa_list_append(&impl->device_list, &obj->link);
update_object(impl, obj, info);
return obj;
clean_object:
free(obj);
unload_handle:
pw_unload_spa_handle(handle);
exit:
return NULL;
}
static void remove_object(struct impl *impl, struct object *obj)
{
pw_log_debug("remove object %u", obj->id);
spa_list_remove(&obj->link);
spa_hook_remove(&obj->listener);
pw_proxy_destroy(obj->proxy);
free(obj->handle);
free(obj);
}
static void dbus_device_object_info(void *data, uint32_t id,
const struct spa_device_object_info *info)
{
struct impl *impl = data;
struct object *obj;
obj = find_object(impl, id);
if (info == NULL) {
if (obj == NULL)
return;
remove_object(impl, obj);
} else if (obj == NULL) {
if (create_object(impl, id, info) == NULL)
return;
} else {
update_object(impl, obj, info);
}
}
static const struct spa_device_events dbus_device_events =
{
SPA_VERSION_DEVICE_EVENTS,
.object_info = dbus_device_object_info,
};
static int start_monitor(struct impl *impl)
{
struct spa_handle *handle;
int res;
void *iface;
handle = pw_context_load_spa_handle(impl->context, 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 MONITOR interface: %d", res);
goto out_unload;
}
impl->handle = handle;
impl->device = iface;
spa_device_add_listener(impl->device, &impl->listener, &dbus_device_events, impl);
return 0;
out_unload:
pw_unload_spa_handle(handle);
out:
return res;
}
static void on_core_error(void *data, uint32_t id, int seq, int res, const char *message)
{
struct impl *impl = data;
pw_log_error("error id:%u seq:%d res:%d (%s): %s",
id, seq, res, spa_strerror(res), message);
if (id == PW_ID_CORE && res == -EPIPE)
pw_main_loop_quit(impl->loop);
}
static const struct pw_core_events core_events = {
PW_VERSION_CORE_EVENTS,
.error = on_core_error,
};
int main(int argc, char *argv[])
{
struct impl impl = { 0, };
int res;
pw_init(&argc, &argv);
impl.loop = pw_main_loop_new(NULL);
impl.context = pw_context_new(pw_main_loop_get_loop(impl.loop), NULL, 0);
clock_gettime(CLOCK_MONOTONIC, &impl.now);
spa_list_init(&impl.device_list);
impl.core = pw_context_connect(impl.context, NULL, 0);
if (impl.core == NULL) {
pw_log_error(NAME" %p: can't connect %m", &impl);
return -1;
}
pw_core_add_listener(impl.core,
&impl.core_listener,
&core_events, &impl);
if ((res = start_monitor(&impl)) < 0) {
pw_log_error(NAME" %p: error starting monitor: %s", &impl, spa_strerror(res));
return -1;
}
pw_main_loop_run(impl.loop);
pw_context_destroy(impl.context);
pw_main_loop_destroy(impl.loop);
return 0;
}
format-utils.h
dict.h
spa/debug/dict.h
pw_context_destroy
void pw_context_destroy(struct pw_context *context)
destroy a context object, all resources except the main_loop will be destroyed
Definition: context.c:434
pw_context_new
struct pw_context * pw_context_new(struct pw_loop *main_loop, struct pw_properties *props, size_t user_data_size)
Make a new context object for a given main_loop.
Definition: context.c:206
pw_context_load_spa_handle
struct spa_handle * pw_context_load_spa_handle(struct pw_context *context, const char *factory_name, const struct spa_dict *info)
Definition: context.c:1373
PW_ID_CORE
#define PW_ID_CORE
default ID for the core object after connect
Definition: core.h:79
pw_context_connect
struct pw_core * pw_context_connect(struct pw_context *context, struct pw_properties *properties, size_t user_data_size)
Connect to a PipeWire instance.
Definition: core.c:402
pw_core_add_listener
#define pw_core_add_listener(c,...)
Definition: core.h:363
PW_VERSION_CORE_EVENTS
#define PW_VERSION_CORE_EVENTS
Definition: core.h:143
pw_core_export
struct pw_proxy * pw_core_export(struct pw_core *core, const char *type, const struct spa_dict *props, void *object, size_t user_data_size)
Export an object into the PipeWire instance associated with core.
Definition: core.c:275
pw_log_warn
#define pw_log_warn(...)
Definition: log.h:163
pw_log_debug
#define pw_log_debug(...)
Definition: log.h:165
pw_log_error
#define pw_log_error(...)
Definition: log.h:162
pw_main_loop_new
struct pw_main_loop * pw_main_loop_new(const struct spa_dict *props)
Create a new main loop.
Definition: main-loop.c:80
pw_main_loop_quit
int pw_main_loop_quit(struct pw_main_loop *loop)
Quit a main loop.
Definition: main-loop.c:125
pw_main_loop_destroy
void pw_main_loop_destroy(struct pw_main_loop *loop)
Destroy a loop.
Definition: main-loop.c:90
pw_main_loop_run
int pw_main_loop_run(struct pw_main_loop *loop)
Run a main loop.
Definition: main-loop.c:139
pw_main_loop_get_loop
struct pw_loop * pw_main_loop_get_loop(struct pw_main_loop *loop)
Get the loop implementation.
Definition: main-loop.c:113
pw_init
void pw_init(int *argc, char **argv[])
Initialize PipeWire.
Definition: pipewire.c:586
pw_unload_spa_handle
int pw_unload_spa_handle(struct spa_handle *handle)
Definition: pipewire.c:337
pw_proxy_destroy
void pw_proxy_destroy(struct pw_proxy *proxy)
destroy a proxy
Definition: proxy.c:231
spa_debug_dict
static int spa_debug_dict(int indent, const struct spa_dict *dict)
Definition: dict.h:45
SPA_TYPE_INTERFACE_Device
#define SPA_TYPE_INTERFACE_Device
Definition: device.h:56
SPA_VERSION_DEVICE_EVENTS
#define SPA_VERSION_DEVICE_EVENTS
version of the structure
Definition: device.h:142
spa_device_add_listener
#define spa_device_add_listener(d,...)
Set events to receive asynchronous notifications from the device.
Definition: device.h:282
spa_handle_get_interface
#define spa_handle_get_interface(h,...)
Definition: plugin.h:80
spa_hook_remove
static void spa_hook_remove(struct spa_hook *hook)
Remove a hook.
Definition: hook.h:383
spa_list_init
static void spa_list_init(struct spa_list *list)
Definition: list.h:55
spa_list_remove
static void spa_list_remove(struct spa_list *elem)
Definition: list.h:80
spa_list_for_each
#define spa_list_for_each(pos, head, member)
Definition: list.h:122
spa_list_append
#define spa_list_append(list, item)
Definition: list.h:92
SPA_NAME_API_BLUEZ5_ENUM_DBUS
#define SPA_NAME_API_BLUEZ5_ENUM_DBUS
keys for bluez5 factory names
Definition: names.h:147
SPA_TYPE_INTERFACE_Node
#define SPA_TYPE_INTERFACE_Node
Definition: node.h:57
spa_strerror
#define spa_strerror(err)
Definition: result.h:69
spa_streq
static bool spa_streq(const char *s1, const char *s2)
Definition: string.h:59
hook.h
spa/utils/hook.h
names.h
spa/utils/names.h
pipewire.h
pipewire/pipewire.h
props.h
result.h
spa/utils/result.h
device.h
spa/monitor/device.h
node.h
spa/node/node.h
string.h
spa/utils/string.h
pw_context
pw_core_events
Core events.
Definition: core.h:141
pw_core
pw_main_loop
A main loop object.
pw_proxy
spa_device_events
spa_device_events:
Definition: device.h:139
spa_device_object_info
Information about a device object.
Definition: device.h:93
spa_device_object_info::type
const char * type
the object type managed by this device
Definition: device.h:98
spa_device_object_info::factory_name
const char * factory_name
a factory name that implements the object
Definition: device.h:99
spa_device
Definition: device.h:60
spa_handle
Definition: plugin.h:50
spa_hook
A hook, contains the structure with functions and the data passed to the functions.
Definition: hook.h:342
spa_list
Definition: list.h:47
spa_node
Definition: node.h:61