media-session: add device reservation for alsa

Implement the device reservation DBus API.

When we acquire the device name, set our device profile to 'On'. This
adds our sources and sinks to the graph.

When we lose the name, switch back to 'Off' and remove our nodes
again.

Move the session mamager stuff in a directory.

Fixes #191
This commit is contained in:
Wim Taymans 2019-09-30 21:23:29 +02:00
parent 96ac81958b
commit dc83c10c9a
8 changed files with 672 additions and 8 deletions

View file

@ -41,6 +41,8 @@
#include "pipewire/pipewire.h"
#include "pipewire/private.h"
#include "reserve.c"
struct alsa_object;
struct alsa_node {
@ -61,6 +63,10 @@ struct alsa_object {
uint32_t id;
uint32_t device_id;
struct rd_device *reserve;
struct spa_hook sync_listener;
int seq;
struct pw_properties *props;
struct spa_handle *handle;
@ -326,6 +332,63 @@ static int update_device_props(struct alsa_object *obj)
return 1;
}
static void set_profile(struct alsa_object *obj, int index)
{
char buf[1024];
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buf, sizeof(buf));
spa_device_set_param(obj->device,
SPA_PARAM_Profile, 0,
spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamProfile, 0,
SPA_PARAM_PROFILE_index, SPA_POD_Int(index)));
}
static void reserve_acquired(void *data, struct rd_device *d)
{
struct alsa_object *obj = data;
pw_log_debug("%p: reserve acquired", obj);
set_profile(obj, 1);
}
static void sync_complete_done(void *data, int seq)
{
struct alsa_object *obj = data;
if (seq != obj->seq)
return;
spa_hook_remove(&obj->sync_listener);
obj->seq = 0;
rd_device_complete_release(obj->reserve, true);
}
static const struct pw_proxy_events sync_complete_release = {
PW_VERSION_PROXY_EVENTS,
.done = sync_complete_done
};
static void reserve_release(void *data, struct rd_device *d, int forced)
{
struct alsa_object *obj = data;
pw_log_debug("%p: reserve release", obj);
set_profile(obj, 0);
if (obj->seq == 0)
pw_proxy_add_listener(obj->proxy,
&obj->sync_listener,
&sync_complete_release, obj);
obj->seq = pw_proxy_sync(obj->proxy, 0);
}
static const struct rd_device_callbacks reserve_callbacks = {
.acquired = reserve_acquired,
.release = reserve_release,
};
static struct alsa_object *alsa_create_object(struct monitor *monitor, uint32_t id,
const struct spa_device_object_info *info)
@ -336,6 +399,7 @@ static struct alsa_object *alsa_create_object(struct monitor *monitor, uint32_t
struct spa_handle *handle;
int res;
void *iface;
const char *card;
pw_log_debug("new object %u", id);
@ -378,6 +442,27 @@ static struct alsa_object *alsa_create_object(struct monitor *monitor, uint32_t
goto clean_object;
}
if ((card = spa_dict_lookup(info->props, SPA_KEY_API_ALSA_CARD)) != NULL) {
const char *reserve;
pw_properties_setf(obj->props, "api.dbus.ReserveDevice1", "Audio%s", card);
reserve = pw_properties_get(obj->props, "api.dbus.ReserveDevice1");
obj->reserve = rd_device_new(impl->conn, reserve,
"PipeWire", 0,
&reserve_callbacks, obj);
if (obj->reserve == NULL) {
pw_log_warn("can't create device reserve for %s: %m", reserve);
} else {
rd_device_set_application_device_name(obj->reserve,
spa_dict_lookup(info->props, SPA_KEY_API_ALSA_PATH));
}
}
if (obj->reserve == NULL)
set_profile(obj, 1);
spa_list_init(&obj->node_list);
spa_device_add_listener(obj->device,
@ -401,6 +486,10 @@ static void alsa_remove_object(struct monitor *monitor, struct alsa_object *obj)
pw_log_debug("remove object %u", obj->id);
spa_list_remove(&obj->link);
spa_hook_remove(&obj->device_listener);
if (obj->seq != 0)
spa_hook_remove(&obj->sync_listener);
if (obj->reserve)
rd_device_destroy(obj->reserve);
pw_proxy_destroy(obj->proxy);
pw_unload_spa_handle(obj->handle);
free(obj);

View file

@ -35,10 +35,13 @@
#include <spa/param/audio/format-utils.h>
#include <spa/param/props.h>
#include <spa/debug/pod.h>
#include <spa/support/dbus.h>
#include "pipewire/pipewire.h"
#include "pipewire/private.h"
#include <dbus/dbus.h>
#define NAME "media-session"
#define DEFAULT_CHANNELS 2
@ -86,6 +89,10 @@ struct impl {
struct monitor bluez5_monitor;
struct monitor alsa_monitor;
struct monitor v4l2_monitor;
struct spa_dbus *dbus;
struct spa_dbus_connection *dbus_connection;
DBusConnection *conn;
};
struct object {
@ -1201,6 +1208,28 @@ static const struct pw_core_proxy_events core_events = {
.done = core_done
};
static void start_services(struct impl *impl)
{
const struct spa_support *support;
uint32_t n_support;
support = pw_core_get_support(impl->core, &n_support);
impl->dbus = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_DBus);
if (impl->dbus)
impl->dbus_connection = spa_dbus_get_connection(impl->dbus, DBUS_BUS_SESSION);
if (impl->dbus_connection)
impl->conn = spa_dbus_connection_get(impl->dbus_connection);
if (impl->conn == NULL)
pw_log_warn("no dbus connection, device reservation disabled");
else
pw_log_debug("got dbus connection %p", impl->conn);
bluez5_start_monitor(impl, &impl->bluez5_monitor);
alsa_start_monitor(impl, &impl->alsa_monitor);
v4l2_start_monitor(impl, &impl->v4l2_monitor);
}
static void on_state_changed(void *_data, enum pw_remote_state old, enum pw_remote_state state, const char *error)
{
struct impl *impl = _data;
@ -1222,10 +1251,7 @@ static void on_state_changed(void *_data, enum pw_remote_state old, enum pw_remo
pw_registry_proxy_add_listener(impl->registry_proxy,
&impl->registry_listener,
&registry_events, impl);
bluez5_start_monitor(impl, &impl->bluez5_monitor);
alsa_start_monitor(impl, &impl->alsa_monitor);
v4l2_start_monitor(impl, &impl->v4l2_monitor);
start_services(impl);
schedule_rescan(impl);
break;

View file

@ -0,0 +1,473 @@
/* DBus device reservation API
*
* 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.
*/
#ifndef NAME
#define NAME "reserve"
#endif
#include "reserve.h"
#define SERVICE_PREFIX "org.freedesktop.ReserveDevice1."
#define OBJECT_PREFIX "/org/freedesktop/ReserveDevice1/"
static const char introspection[] =
DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
"<node>"
" <!-- If you are looking for documentation make sure to check out\n"
" http://git.0pointer.de/?p=reserve.git;a=blob;f=reserve.txt -->\n"
" <interface name=\"org.freedesktop.ReserveDevice1\">"
" <method name=\"RequestRelease\">"
" <arg name=\"priority\" type=\"i\" direction=\"in\"/>"
" <arg name=\"result\" type=\"b\" direction=\"out\"/>"
" </method>"
" <property name=\"Priority\" type=\"i\" access=\"read\"/>"
" <property name=\"ApplicationName\" type=\"s\" access=\"read\"/>"
" <property name=\"ApplicationDeviceName\" type=\"s\" access=\"read\"/>"
" </interface>"
" <interface name=\"org.freedesktop.DBus.Properties\">"
" <method name=\"Get\">"
" <arg name=\"interface\" direction=\"in\" type=\"s\"/>"
" <arg name=\"property\" direction=\"in\" type=\"s\"/>"
" <arg name=\"value\" direction=\"out\" type=\"v\"/>"
" </method>"
" </interface>"
" <interface name=\"org.freedesktop.DBus.Introspectable\">"
" <method name=\"Introspect\">"
" <arg name=\"data\" type=\"s\" direction=\"out\"/>"
" </method>"
" </interface>"
"</node>";
struct rd_device {
DBusConnection *connection;
int32_t priority;
char *service_name;
char *object_path;
char *application_name;
char *application_device_name;
const struct rd_device_callbacks *callbacks;
void *data;
DBusMessage *reply;
unsigned int filtering:1;
unsigned int registered:1;
unsigned int owning:1;
};
static dbus_bool_t add_variant(DBusMessage *m, int type, const void *data)
{
DBusMessageIter iter, sub;
char t[2];
t[0] = (char) type;
t[1] = 0;
dbus_message_iter_init_append(m, &iter);
if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, t, &sub))
return false;
if (!dbus_message_iter_append_basic(&sub, type, data))
return false;
if (!dbus_message_iter_close_container(&iter, &sub))
return false;
return true;
}
static DBusHandlerResult object_handler(DBusConnection *c, DBusMessage *m, void *userdata)
{
struct rd_device *d = userdata;
DBusError error;
DBusMessage *reply = NULL;
dbus_error_init(&error);
if (dbus_message_is_method_call(m, "org.freedesktop.ReserveDevice1",
"RequestRelease")) {
int32_t priority;
if (!dbus_message_get_args(m, &error,
DBUS_TYPE_INT32, &priority,
DBUS_TYPE_INVALID))
goto invalid;
pw_log_debug(NAME" %p: request release priority:%d", d, priority);
if (!(reply = dbus_message_new_method_return(m)))
goto oom;
if (d->reply)
rd_device_complete_release(d, false);
d->reply = reply;
if (priority > d->priority && d->callbacks->release)
d->callbacks->release(d->data, d, 0);
else
rd_device_complete_release(d, false);
return DBUS_HANDLER_RESULT_HANDLED;
} else if (dbus_message_is_method_call(
m,
"org.freedesktop.DBus.Properties",
"Get")) {
const char *interface, *property;
if (!dbus_message_get_args( m, &error,
DBUS_TYPE_STRING, &interface,
DBUS_TYPE_STRING, &property,
DBUS_TYPE_INVALID))
goto invalid;
if (strcmp(interface, "org.freedesktop.ReserveDevice1") == 0) {
const char *empty = "";
if (strcmp(property, "ApplicationName") == 0 && d->application_name) {
if (!(reply = dbus_message_new_method_return(m)))
goto oom;
if (!add_variant(reply,
DBUS_TYPE_STRING,
d->application_name ? (const char**) &d->application_name : &empty))
goto oom;
} else if (strcmp(property, "ApplicationDeviceName") == 0) {
if (!(reply = dbus_message_new_method_return(m)))
goto oom;
if (!add_variant(reply,
DBUS_TYPE_STRING,
d->application_device_name ? (const char**) &d->application_device_name : &empty))
goto oom;
} else if (strcmp(property, "Priority") == 0) {
if (!(reply = dbus_message_new_method_return(m)))
goto oom;
if (!add_variant(reply,
DBUS_TYPE_INT32, &d->priority))
goto oom;
} else {
if (!(reply = dbus_message_new_error_printf(m,
DBUS_ERROR_UNKNOWN_METHOD,
"Unknown property %s", property)))
goto oom;
}
if (!dbus_connection_send(c, reply, NULL))
goto oom;
dbus_message_unref(reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
} else if (dbus_message_is_method_call(
m,
"org.freedesktop.DBus.Introspectable",
"Introspect")) {
const char *i = introspection;
if (!(reply = dbus_message_new_method_return(m)))
goto oom;
if (!dbus_message_append_args(reply,
DBUS_TYPE_STRING, &i,
DBUS_TYPE_INVALID))
goto oom;
if (!dbus_connection_send(c, reply, NULL))
goto oom;
dbus_message_unref(reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
invalid:
if (reply)
dbus_message_unref(reply);
if (!(reply = dbus_message_new_error(m,
DBUS_ERROR_INVALID_ARGS,
"Invalid arguments")))
goto oom;
if (!dbus_connection_send(c, reply, NULL))
goto oom;
dbus_message_unref(reply);
dbus_error_free(&error);
return DBUS_HANDLER_RESULT_HANDLED;
oom:
if (reply)
dbus_message_unref(reply);
dbus_error_free(&error);
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
static const struct DBusObjectPathVTable vtable ={
.message_function = object_handler
};
static DBusHandlerResult filter_handler(DBusConnection *c, DBusMessage *m, void *userdata)
{
struct rd_device *d = userdata;
DBusError error;
const char *name;
dbus_error_init(&error);
if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameAcquired")) {
if (!dbus_message_get_args( m, &error,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID))
goto invalid;
if (strcmp(name, d->service_name) != 0)
goto invalid;
pw_log_debug(NAME" %p: acquired %s", d, name);
d->owning = true;
if (!d->registered) {
if (!(dbus_connection_register_object_path(d->connection,
d->object_path,
&vtable,
d)))
goto invalid;
if (d->callbacks->acquired)
d->callbacks->acquired(d->data, d);
d->registered = true;
}
} else if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameLost")) {
if (!dbus_message_get_args( m, &error,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID))
goto invalid;
if (strcmp(name, d->service_name) != 0)
goto invalid;
pw_log_debug(NAME" %p: lost %s", d, name);
d->owning = false;
if (d->registered) {
dbus_connection_unregister_object_path(d->connection,
d->object_path);
d->registered = false;
}
}
invalid:
dbus_error_free(&error);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
struct rd_device *
rd_device_new(DBusConnection *connection, const char *device_name, const char *application_name,
int32_t priority, const struct rd_device_callbacks *callbacks, void *data)
{
struct rd_device *d;
DBusError error;
int res;
d = calloc(1, sizeof(struct rd_device));
if (d == NULL)
return NULL;
d->connection = connection;
d->priority = priority;
d->callbacks = callbacks;
d->data = data;
d->application_name = strdup(application_name);
asprintf(&d->object_path, OBJECT_PREFIX "%s", device_name);
if (d->object_path == NULL) {
res = -errno;
goto error_free;
}
asprintf(&d->service_name, SERVICE_PREFIX "%s", device_name);
if (d->service_name == NULL) {
res = -errno;
goto error_free;
}
dbus_error_init(&error);
if (!dbus_connection_add_filter(d->connection,
filter_handler,
d,
NULL)) {
res = -ENOMEM;
goto error_free;
}
dbus_bus_add_match(d->connection,
"type='signal',sender='org.freedesktop.DBus',"
"interface='org.freedesktop.DBus',member='NameLost'", NULL);
dbus_bus_add_match(d->connection,
"type='signal',sender='org.freedesktop.DBus',"
"interface='org.freedesktop.DBus',member='NameAcquired'", NULL);
if ((res = dbus_bus_request_name(d->connection,
d->service_name,
(d->priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0),
&error)) < 0) {
dbus_error_free(&error);
res = -EIO;
goto error_free;
}
dbus_connection_ref(d->connection);
pw_log_debug(NAME"%p: new device %s", d, device_name);
return d;
error_free:
free(d->service_name);
free(d->object_path);
free(d);
errno = -res;
return NULL;
}
int rd_device_request_release(struct rd_device *d)
{
DBusMessage *m = NULL;
if (d->priority <= INT32_MIN)
return -EBUSY;
if ((m = dbus_message_new_method_call(d->service_name,
d->object_path,
"org.freedesktop.ReserveDevice1",
"RequestRelease"))) {
return -ENOMEM;
}
if (!dbus_message_append_args(m,
DBUS_TYPE_INT32, &d->priority,
DBUS_TYPE_INVALID)) {
dbus_message_unref(m);
return -ENOMEM;
}
return 0;
}
int rd_device_complete_release(struct rd_device *d, int res)
{
dbus_bool_t ret = res != 0;
if (d->reply == NULL)
return -EINVAL;
pw_log_debug(NAME" %p: complete release %d", d, res);
if (!dbus_message_append_args(d->reply,
DBUS_TYPE_BOOLEAN, &ret,
DBUS_TYPE_INVALID)) {
res = -ENOMEM;
goto exit;
}
if (!dbus_connection_send(d->connection, d->reply, NULL)) {
res = -ENOMEM;
goto exit;
}
res = 0;
exit:
dbus_message_unref(d->reply);
d->reply = NULL;
return res;
}
void rd_device_release(struct rd_device *d)
{
if (d->owning) {
DBusError error;
dbus_error_init(&error);
dbus_bus_release_name(d->connection,
d->service_name, &error);
dbus_error_free(&error);
}
}
void rd_device_destroy(struct rd_device *d)
{
dbus_connection_remove_filter(d->connection,
filter_handler, d);
if (d->registered)
dbus_connection_unregister_object_path(d->connection,
d->object_path);
rd_device_release(d);
free(d->service_name);
free(d->object_path);
free(d->application_name);
free(d->application_device_name);
if (d->reply)
dbus_message_unref(d->reply);
dbus_connection_unref(d->connection);
free(d);
}
int rd_device_set_application_device_name(struct rd_device *d, const char *name)
{
char *t;
if (!d)
return -EINVAL;
if (!(t = strdup(name)))
return -ENOMEM;
free(d->application_device_name);
d->application_device_name = t;
return 0;
}

View file

@ -0,0 +1,78 @@
/* DBus device reservation API
*
* 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.
*/
#ifndef DEVICE_RESERVE_H
#define DEVICE_RESERVE_H
#include <dbus/dbus.h>
#include <inttypes.h>
#ifdef __cplusplus
extern "C" {
#endif
struct rd_device;
struct rd_device_callbacks {
/** the device is acquired */
void (*acquired) (void *data, struct rd_device *d);
/** request a release of the device */
void (*release) (void *data, struct rd_device *d, int forced);
};
/* create a new device and start watching */
struct rd_device *
rd_device_new(DBusConnection *connection, /**< Bus to watch */
const char *device_name, /**< The device to lock, e.g. "Audio0" */
const char *application_name, /**< A human readable name of the application,
* e.g. "PipeWire Server" */
int32_t priority, /**< The priority for this application.
* If unsure use 0 */
const struct rd_device_callbacks *callbacks, /**< Called when device name is acquired/released */
void *data);
/** try to acquire the device */
int rd_device_request_acquire(struct rd_device *d);
/** request the owner to release the device */
int rd_device_request_release(struct rd_device *d);
/** complete the release of the device */
int rd_device_complete_release(struct rd_device *d, int res);
/** release a device */
void rd_device_release(struct rd_device *d);
/** destroy a device */
void rd_device_destroy(struct rd_device *d);
/* Set the application device name for an rd_device object. Returns 0
* on success, a negative errno style return value on error. */
int rd_device_set_application_device_name(struct rd_device *d, const char *name);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -38,10 +38,10 @@ executable('export-spa-device',
)
executable('media-session',
'media-session.c',
'media-session/media-session.c',
c_args : [ '-D_GNU_SOURCE' ],
install: false,
dependencies : [pipewire_dep, mathlib],
dependencies : [dbus_dep, pipewire_dep, mathlib],
)
executable('bluez-session',