Add dbus support interface

Add an interface that can manager a dbus connection.
Make a dbus interface in the core that can create connections running
in the core main loop. Keep this as support for spa plugins.
This commit is contained in:
Wim Taymans 2017-12-18 12:41:47 +01:00
parent 4d6ac37398
commit f7b6fea43d
7 changed files with 612 additions and 37 deletions

View file

@ -0,0 +1,87 @@
/* PipeWire
* Copyright (C) 2017 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.
*/
#ifndef __SPA_DBUS_H__
#define __SPA_DBUS_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <dbus/dbus.h>
#include <spa/support/loop.h>
#define SPA_TYPE__DBus SPA_TYPE_INTERFACE_BASE "DBus"
#define SPA_TYPE_DBUS_BASE SPA_TYPE__DBus ":"
#define SPA_TYPE_DBUS__Connection SPA_TYPE_DBUS_BASE "Connection"
struct spa_dbus_connection {
#define SPA_VERSION_DBUS_CONNECTION 0
uint32_t version;
/**
* Get the DBusConnection from a wraper
*
* \param conn the spa_dbus_connection wrapper
* \return a DBusConnection
*/
DBusConnection *(*get) (struct spa_dbus_connection *conn);
/**
* Destroy a dbus connection wrapper
*
* \param conn the wrapper to destroy
*/
void (*destroy) (struct spa_dbus_connection *conn);
};
#define spa_dbus_connection_get(c) (c)->get((c))
#define spa_dbus_connection_destroy(c) (c)->destroy((c))
struct spa_dbus {
/* the version of this structure. This can be used to expand this
* structure in the future */
#define SPA_VERSION_DBUS 0
uint32_t version;
/**
* Get a new connection wrapper for the given bus type.
*
* The connection wrapper is completely configured to operate
* in the main context of the handle that manages the spa_dbus
* interface.
*
* \param dbus the dbus manager
* \param type the bus type to wrap
* \param error location for the error
* \return a new dbus connection wrapper or NULL and \a error is
* set.
*/
struct spa_dbus_connection * (*get_connection) (struct spa_dbus *dbus,
DBusBusType type,
DBusError *error);
};
#define spa_dbus_get_connection(d,...) (d)->get_connection((d),__VA_ARGS__)
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* __SPA_DBUS_H__ */

445
spa/plugins/support/dbus.c Normal file
View file

@ -0,0 +1,445 @@
/* PipeWire
* Copyright (C) 2017 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 <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <spa/support/type-map.h>
#include <spa/support/log.h>
#include <spa/support/plugin.h>
#include <spa/support/dbus.h>
#define NAME "dbus"
struct type {
uint32_t dbus;
};
static inline void init_type(struct type *type, struct spa_type_map *map)
{
type->dbus = spa_type_map_get_id(map, SPA_TYPE__DBus);
}
struct impl {
struct spa_handle handle;
struct spa_dbus dbus;
struct type type;
struct spa_type_map *map;
struct spa_log *log;
struct spa_loop_utils *utils;
struct spa_list connection_list;
};
struct connection {
struct spa_list link;
struct spa_dbus_connection this;
struct impl *impl;
DBusConnection *conn;
struct spa_source *dispatch_event;
};
static void dispatch_cb(void *userdata)
{
struct connection *conn = userdata;
struct impl *impl = conn->impl;
if (dbus_connection_dispatch(conn->conn) == DBUS_DISPATCH_COMPLETE)
spa_loop_utils_enable_idle(impl->utils, conn->dispatch_event, false);
}
static void dispatch_status(DBusConnection *conn, DBusDispatchStatus status, void *userdata)
{
struct connection *c = userdata;
struct impl *impl = c->impl;
spa_loop_utils_enable_idle(impl->utils, c->dispatch_event,
status == DBUS_DISPATCH_COMPLETE ? false : true);
}
static inline enum spa_io dbus_to_io(DBusWatch *watch)
{
enum spa_io mask;
unsigned int flags;
/* no watch flags for disabled watches */
if (!dbus_watch_get_enabled(watch))
return 0;
flags = dbus_watch_get_flags(watch);
mask = SPA_IO_HUP | SPA_IO_ERR;
if (flags & DBUS_WATCH_READABLE)
mask |= SPA_IO_IN;
if (flags & DBUS_WATCH_WRITABLE)
mask |= SPA_IO_OUT;
return mask;
}
static inline unsigned int io_to_dbus(enum spa_io mask)
{
unsigned int flags = 0;
if (mask & SPA_IO_IN)
flags |= DBUS_WATCH_READABLE;
if (mask & SPA_IO_OUT)
flags |= DBUS_WATCH_WRITABLE;
if (mask & SPA_IO_HUP)
flags |= DBUS_WATCH_HANGUP;
if (mask & SPA_IO_ERR)
flags |= DBUS_WATCH_ERROR;
return flags;
}
static void
handle_io_event(void *userdata, int fd, enum spa_io mask)
{
DBusWatch *watch = userdata;
if (!dbus_watch_get_enabled(watch)) {
fprintf(stderr, "Asked to handle disabled watch: %p %i", (void *) watch, fd);
return;
}
dbus_watch_handle(watch, io_to_dbus(mask));
}
static dbus_bool_t add_watch(DBusWatch *watch, void *userdata)
{
struct connection *conn = userdata;
struct impl *impl = conn->impl;
struct spa_source *source;
spa_log_debug(impl->log, "add watch %p %d", watch, dbus_watch_get_unix_fd(watch));
/* we dup because dbus tends to add the same fd multiple times and our epoll
* implementation does not like that */
source = spa_loop_utils_add_io(impl->utils,
dup(dbus_watch_get_unix_fd(watch)),
dbus_to_io(watch), true, handle_io_event, watch);
dbus_watch_set_data(watch, source, NULL);
return TRUE;
}
static void remove_watch(DBusWatch *watch, void *userdata)
{
struct connection *conn = userdata;
struct spa_source *source;
if ((source = dbus_watch_get_data(watch)))
spa_loop_utils_destroy_source(conn->impl->utils, source);
}
static void toggle_watch(DBusWatch *watch, void *userdata)
{
struct connection *conn = userdata;
struct impl *impl = conn->impl;
struct spa_source *source;
source = dbus_watch_get_data(watch);
spa_loop_utils_update_io(impl->utils, source, dbus_to_io(watch));
}
struct timeout_data {
struct spa_source *source;
struct connection *conn;
};
static void
handle_timer_event(void *userdata, uint64_t expirations)
{
DBusTimeout *timeout = userdata;
uint64_t t;
struct timespec ts;
struct timeout_data *data = dbus_timeout_get_data(timeout);
struct connection *conn = data->conn;
struct impl *impl = conn->impl;
if (dbus_timeout_get_enabled(timeout)) {
t = dbus_timeout_get_interval(timeout) * SPA_NSEC_PER_MSEC;
ts.tv_sec = t / SPA_NSEC_PER_SEC;
ts.tv_nsec = t % SPA_NSEC_PER_SEC;
spa_loop_utils_update_timer(impl->utils,
data->source, &ts, NULL, false);
dbus_timeout_handle(timeout);
}
}
static dbus_bool_t add_timeout(DBusTimeout *timeout, void *userdata)
{
struct connection *conn = userdata;
struct impl *impl = conn->impl;
struct timespec ts;
struct timeout_data *data;
uint64_t t;
if (!dbus_timeout_get_enabled(timeout))
return FALSE;
data = calloc(1, sizeof(struct timeout_data));
data->conn = conn;
data->source = spa_loop_utils_add_timer(impl->utils, handle_timer_event, timeout);
dbus_timeout_set_data(timeout, data, NULL);
t = dbus_timeout_get_interval(timeout) * SPA_NSEC_PER_MSEC;
ts.tv_sec = t / SPA_NSEC_PER_SEC;
ts.tv_nsec = t % SPA_NSEC_PER_SEC;
spa_loop_utils_update_timer(impl->utils, data->source, &ts, NULL, false);
return TRUE;
}
static void remove_timeout(DBusTimeout *timeout, void *userdata)
{
struct connection *conn = userdata;
struct impl *impl = conn->impl;
struct timeout_data *data;
if ((data = dbus_timeout_get_data(timeout))) {
spa_loop_utils_destroy_source(impl->utils, data->source);
free(data);
}
}
static void toggle_timeout(DBusTimeout *timeout, void *userdata)
{
struct connection *conn = userdata;
struct impl *impl = conn->impl;
struct timeout_data *data;
struct timespec ts, *tsp;
data = dbus_timeout_get_data(timeout);
if (dbus_timeout_get_enabled(timeout)) {
uint64_t t = dbus_timeout_get_interval(timeout) * SPA_NSEC_PER_MSEC;
ts.tv_sec = t / SPA_NSEC_PER_SEC;
ts.tv_nsec = t % SPA_NSEC_PER_SEC;
tsp = &ts;
} else {
tsp = NULL;
}
spa_loop_utils_update_timer(impl->utils, data->source, tsp, NULL, false);
}
static void wakeup_main(void *userdata)
{
struct connection *this = userdata;
struct impl *impl = this->impl;
spa_loop_utils_enable_idle(impl->utils, this->dispatch_event, true);
}
static DBusConnection *
impl_connection_get(struct spa_dbus_connection *conn)
{
struct connection *this = SPA_CONTAINER_OF(conn, struct connection, this);
return this->conn;
}
static void
impl_connection_destroy(struct spa_dbus_connection *conn)
{
struct connection *this = SPA_CONTAINER_OF(conn, struct connection, this);
struct impl *impl = this->impl;
dbus_connection_close(this->conn);
dbus_connection_unref(this->conn);
spa_loop_utils_destroy_source(impl->utils, this->dispatch_event);
spa_list_remove(&this->link);
free(this);
}
static const struct spa_dbus_connection impl_connection = {
SPA_VERSION_DBUS_CONNECTION,
impl_connection_get,
impl_connection_destroy,
};
static struct spa_dbus_connection *
impl_get_connection(struct spa_dbus *dbus,
DBusBusType type,
DBusError *error)
{
struct impl *impl = SPA_CONTAINER_OF(dbus, struct impl, dbus);
struct connection *conn;
dbus_error_init(error);
conn = calloc(1, sizeof(struct connection));
conn->this = impl_connection;
conn->impl = impl;
conn->conn = dbus_bus_get_private(type, error);
if (conn->conn == NULL)
goto error;
conn->dispatch_event = spa_loop_utils_add_idle(impl->utils,
false, dispatch_cb, conn);
dbus_connection_set_exit_on_disconnect(conn->conn, false);
dbus_connection_set_dispatch_status_function(conn->conn, dispatch_status, conn, NULL);
dbus_connection_set_watch_functions(conn->conn, add_watch, remove_watch, toggle_watch, conn,
NULL);
dbus_connection_set_timeout_functions(conn->conn, add_timeout, remove_timeout,
toggle_timeout, conn, NULL);
dbus_connection_set_wakeup_main_function(conn->conn, wakeup_main, conn, NULL);
spa_list_append(&impl->connection_list, &conn->link);
return &conn->this;
error:
free(conn);
return NULL;
}
static const struct spa_dbus impl_dbus = {
SPA_VERSION_DBUS,
impl_get_connection,
};
static int impl_get_interface(struct spa_handle *handle, uint32_t interface_id, void **interface)
{
struct impl *this;
spa_return_val_if_fail(handle != NULL, -EINVAL);
spa_return_val_if_fail(interface != NULL, -EINVAL);
this = (struct impl *) handle;
if (interface_id == this->type.dbus)
*interface = &this->dbus;
else
return -ENOENT;
return 0;
}
static int impl_clear(struct spa_handle *handle)
{
spa_return_val_if_fail(handle != NULL, -EINVAL);
return 0;
}
static int
impl_init(const struct spa_handle_factory *factory,
struct spa_handle *handle,
const struct spa_dict *info,
const struct spa_support *support,
uint32_t n_support)
{
struct impl *this;
uint32_t i;
spa_return_val_if_fail(factory != NULL, -EINVAL);
spa_return_val_if_fail(handle != NULL, -EINVAL);
handle->get_interface = impl_get_interface;
handle->clear = impl_clear;
this = (struct impl *) handle;
spa_list_init(&this->connection_list);
this->dbus = impl_dbus;
for (i = 0; i < n_support; i++) {
if (strcmp(support[i].type, SPA_TYPE__TypeMap) == 0)
this->map = support[i].data;
else if (strcmp(support[i].type, SPA_TYPE__Log) == 0)
this->log = support[i].data;
else if (strcmp(support[i].type, SPA_TYPE__LoopUtils) == 0)
this->utils = support[i].data;
}
if (this->map == NULL) {
spa_log_error(this->log, "a type-map is needed");
return -EINVAL;
}
if (this->utils == NULL) {
spa_log_error(this->log, "a LoopUtils is needed");
return -EINVAL;
}
init_type(&this->type, this->map);
spa_log_debug(this->log, NAME " %p: initialized", this);
return 0;
}
static const struct spa_interface_info impl_interfaces[] = {
{SPA_TYPE__DBus,},
};
static int
impl_enum_interface_info(const struct spa_handle_factory *factory,
const struct spa_interface_info **info,
uint32_t *index)
{
spa_return_val_if_fail(factory != NULL, -EINVAL);
spa_return_val_if_fail(info != NULL, -EINVAL);
spa_return_val_if_fail(index != NULL, -EINVAL);
switch (*index) {
case 0:
*info = &impl_interfaces[*index];
break;
default:
return 0;
}
(*index)++;
return 1;
}
static const struct spa_handle_factory dbus_factory = {
SPA_VERSION_HANDLE_FACTORY,
NAME,
NULL,
sizeof(struct impl),
impl_init,
impl_enum_interface_info,
};
int
spa_handle_factory_enum(const struct spa_handle_factory **factory, uint32_t *index)
{
spa_return_val_if_fail(factory != NULL, -EINVAL);
spa_return_val_if_fail(index != NULL, -EINVAL);
switch (*index) {
case 0:
*factory = &dbus_factory;
break;
default:
return 0;
}
(*index)++;
return 1;
}

View file

@ -9,3 +9,12 @@ spa_support_lib = shared_library('spa-support',
dependencies : threads_dep,
install : true,
install_dir : '@0@/spa/support'.format(get_option('libdir')))
spa_dbus_sources = ['dbus.c']
spa_dbus_lib = shared_library('spa-dbus',
spa_dbus_sources,
include_directories : [ spa_inc, spa_libinc],
dependencies : dbus_dep,
install : true,
install_dir : '@0@/spa/support'.format(get_option('libdir')))

View file

@ -24,6 +24,7 @@
#define spa_debug pw_log_trace
#include <spa/lib/debug.h>
#include <spa/support/dbus.h>
#include <pipewire/pipewire.h>
#include <pipewire/private.h>
@ -407,6 +408,8 @@ struct pw_core *pw_core_new(struct pw_loop *main_loop, struct pw_properties *pro
if (this == NULL)
return NULL;
pw_log_debug("core %p: new", this);
if (properties == NULL)
properties = pw_properties_new(NULL, NULL);
if (properties == NULL)
@ -432,8 +435,12 @@ struct pw_core *pw_core_new(struct pw_loop *main_loop, struct pw_properties *pro
this->support[0] = SPA_SUPPORT_INIT(SPA_TYPE__TypeMap, this->type.map);
this->support[1] = SPA_SUPPORT_INIT(SPA_TYPE_LOOP__DataLoop, this->data_loop->loop);
this->support[2] = SPA_SUPPORT_INIT(SPA_TYPE_LOOP__MainLoop, this->main_loop->loop);
this->support[3] = SPA_SUPPORT_INIT(SPA_TYPE__Log, pw_log_get());
this->n_support = 4;
this->support[3] = SPA_SUPPORT_INIT(SPA_TYPE__LoopUtils, this->main_loop->utils);
this->support[4] = SPA_SUPPORT_INIT(SPA_TYPE__Log, pw_log_get());
this->support[5] = SPA_SUPPORT_INIT(SPA_TYPE__DBus, pw_get_spa_dbus(this->main_loop));
this->n_support = 6;
pw_log_debug("%p", this->support[5].data);
pw_data_loop_start(this->data_loop_impl);

View file

@ -29,6 +29,8 @@
#include <errno.h>
#include <dlfcn.h>
#include <spa/support/dbus.h>
#include "pipewire/pipewire.h"
#include "pipewire/private.h"
@ -37,7 +39,7 @@ static char **categories = NULL;
static struct support_info {
void *hnd;
spa_handle_factory_enum_func_t enum_func;
struct spa_support support[4];
struct spa_support support[16];
uint32_t n_support;
} support_info;
@ -71,6 +73,24 @@ open_support(const char *path,
return false;
}
static const struct spa_handle_factory *get_factory(struct support_info *info, const char *factory_name)
{
int res;
uint32_t index;
const struct spa_handle_factory *factory;
for (index = 0;;) {
if ((res = info->enum_func(&factory, &index)) <= 0) {
if (res != 0)
fprintf(stderr, "can't enumerate factories: %s\n", spa_strerror(res));
break;
}
if (strcmp(factory->name, factory_name) == 0)
return factory;
}
return NULL;
}
static void *
load_interface(struct support_info *info,
const char *factory_name,
@ -83,7 +103,7 @@ load_interface(struct support_info *info,
void *iface;
struct spa_type_map *map = NULL;
factory = pw_get_support_factory(factory_name);
factory = get_factory(info, factory_name);
if (factory == NULL)
goto not_found;
@ -101,6 +121,8 @@ load_interface(struct support_info *info,
fprintf(stderr, "can't get %s interface %d\n", type, res);
goto interface_failed;
}
fprintf(stderr, "loaded interface %s from %s\n", type, factory_name);
return iface;
interface_failed:
@ -124,22 +146,6 @@ static void configure_debug(const char *str)
categories = pw_split_strv(level[1], ",", INT_MAX, &n_tokens);
}
static void configure_support(struct support_info *info)
{
void *iface;
iface = load_interface(info, "mapper", SPA_TYPE__TypeMap);
if (iface != NULL) {
info->support[info->n_support++] = SPA_SUPPORT_INIT(SPA_TYPE__TypeMap, iface);
}
iface = load_interface(info, "logger", SPA_TYPE__Log);
if (iface != NULL) {
info->support[info->n_support++] = SPA_SUPPORT_INIT(SPA_TYPE__Log, iface);
pw_log_set(iface);
}
}
/** Get a support interface
* \param type the interface type
* \return the interface or NULL when not configured
@ -157,20 +163,7 @@ void *pw_get_support_interface(const char *type)
const struct spa_handle_factory *pw_get_support_factory(const char *factory_name)
{
int res;
uint32_t index;
const struct spa_handle_factory *factory;
for (index = 0;;) {
if ((res = support_info.enum_func(&factory, &index)) <= 0) {
if (res != 0)
fprintf(stderr, "can't enumerate factories: %s\n", spa_strerror(res));
break;
}
if (strcmp(factory->name, factory_name) == 0)
return factory;
}
return NULL;
return get_factory(&support_info, factory_name);
}
const struct spa_support *pw_get_support(uint32_t *n_support)
@ -179,6 +172,27 @@ const struct spa_support *pw_get_support(uint32_t *n_support)
return support_info.support;
}
void *pw_get_spa_dbus(struct pw_loop *loop)
{
struct support_info dbus_support_info;
const char *str;
dbus_support_info.n_support = support_info.n_support;
memcpy(dbus_support_info.support, support_info.support,
sizeof(struct spa_support) * dbus_support_info.n_support);
dbus_support_info.support[dbus_support_info.n_support++] =
SPA_SUPPORT_INIT(SPA_TYPE__LoopUtils, loop->utils);
if ((str = getenv("SPA_PLUGIN_DIR")) == NULL)
str = PLUGINDIR;
if (open_support(str, "support/libspa-dbus", &dbus_support_info))
return load_interface(&dbus_support_info, "dbus", SPA_TYPE__DBus);
return NULL;
}
/** Initialize PipeWire
*
* \param argc pointer to argc
@ -194,6 +208,8 @@ const struct spa_support *pw_get_support(uint32_t *n_support)
void pw_init(int *argc, char **argv[])
{
const char *str;
void *iface;
struct support_info *info = &support_info;
if ((str = getenv("PIPEWIRE_DEBUG")))
configure_debug(str);
@ -204,8 +220,17 @@ void pw_init(int *argc, char **argv[])
if (support_info.n_support > 0)
return;
if (open_support(str, "support/libspa-support", &support_info))
configure_support(&support_info);
if (open_support(str, "support/libspa-support", info)) {
iface = load_interface(info, "mapper", SPA_TYPE__TypeMap);
if (iface != NULL)
info->support[info->n_support++] = SPA_SUPPORT_INIT(SPA_TYPE__TypeMap, iface);
iface = load_interface(info, "logger", SPA_TYPE__Log);
if (iface != NULL) {
info->support[info->n_support++] = SPA_SUPPORT_INIT(SPA_TYPE__Log, iface);
pw_log_set(iface);
}
}
}
/** Check if a debug category is enabled

View file

@ -132,6 +132,8 @@ pw_direction_reverse(enum pw_direction direction);
void *
pw_get_support_interface(const char *type);
void *pw_get_spa_dbus(struct pw_loop *loop);
const struct spa_handle_factory *
pw_get_support_factory(const char *factory_name);

View file

@ -148,7 +148,7 @@ struct pw_core {
struct pw_loop *data_loop; /**< data loop for data passing */
struct pw_data_loop *data_loop_impl;
struct spa_support support[4]; /**< support for spa plugins */
struct spa_support support[16]; /**< support for spa plugins */
uint32_t n_support; /**< number of support items */
long sc_pagesize;