Commit abf1b2bc authored by sletz's avatar sletz
Browse files

First version of D-Bus based audio device rerservation.

git-svn-id: http://subversion.jackaudio.org/jack/jack2/trunk/jackmp@3342 0c269be4-1314-0410-8aa9-9f06e86f4224
parent 3e1864c4
......@@ -22,16 +22,17 @@ Michael Voigt
---------------------------
Jackdmp changes log
---------------------------
2009-02-23 Stephane Letz <letz@grame.fr>
* Another fix in systemdeps.h and types.h: jack_time_t now uniquely defined in types.h.
* Another fix in systemdeps.h and types.h: jack_time_t now uniquely defined in types.h.
* Move generic code and data in JackNetInterface and JackNetMasterInterface classes.
* First version of D-Bus based audio device rerservation.
2009-02-20 Stephane Letz <letz@grame.fr>
* Add InitConnection and InitRendering methods in JackNetSlaveInterface, better packet type checking in JackNetSlaveInterface::SyncRecv.
* Change fMulticastIP handling in JackNetInterface.
* Change fMulticastIP handling in JackNetInterface.
* Cleanup systemdeps.h on Windows.
2009-02-17 Stephane Letz <letz@grame.fr>
......
......@@ -576,8 +576,8 @@ jackctl_wait_signals(sigset_t signals)
// driver exit
waiting = false;
break;
case SIGTTOU:
break;
case SIGTTOU:
break;
default:
waiting = false;
break;
......
......@@ -139,6 +139,9 @@ def build(bld):
'../posix/JackSocketServerNotifyChannel.cpp',
'../posix/JackNetUnixSocket.cpp',
]
if bld.env['IS_LINUX'] and bld.env['BUILD_JACKDBUS']:
serverlib.source += ['../dbus/reserve.c', '../dbus/audio_reserve.c']
if bld.env['IS_SUN']:
serverlib.source += [
......
/***
Copyright 2009 Grame
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 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 <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "reserve.h"
#include "audio_reserve.h"
#include "JackError.h"
static DBusConnection* connection = NULL;
SERVER_EXPORT int audio_reservation_init()
{
DBusError error;
dbus_error_init(&error);
if (!(connection = dbus_bus_get(DBUS_BUS_SESSION, &error))) {
jack_error("Failed to connect to session bus for device reservation %s\n", error.message);
return -1;
}
return 0;
}
SERVER_EXPORT int audio_reservation_finish()
{
if (connection)
dbus_connection_unref(connection);
}
SERVER_EXPORT void* audio_acquire(int num)
{
DBusError error;
rd_device* device;
char audio_name[32];
int e;
snprintf(audio_name, sizeof(audio_name) - 1, "Audio%d", num);
if ((e = rd_acquire(
&device,
connection,
audio_name,
"Jack audio server",
INT32_MAX,
NULL,
&error)) < 0) {
jack_error ("Failed to acquire device: %s\n", error.message ? error.message : strerror(-e));
return NULL;
}
jack_info("Acquire audio card %s", audio_name);
return (void*)device;
}
SERVER_EXPORT void audio_reserve_loop()
{
if (connection) {
while (dbus_connection_read_write_dispatch (connection, -1))
; // empty loop body
}
}
SERVER_EXPORT void audio_release(void* dev)
{
rd_device* device = (rd_device*)dev;
if (device) {
jack_info("Release audio card");
rd_release(device);
}
}
/***
Copyright 2009 GRAME
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 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 __audio_reserve__
#define __audio_reserve__
#include "JackCompilerDeps.h"
#ifdef __cplusplus
extern "C" {
#endif
SERVER_EXPORT int audio_reservation_init();
SERVER_EXPORT int audio_reservation_finish();
SERVER_EXPORT void* audio_acquire(int num);
SERVER_EXPORT void audio_release(void* dev);
SERVER_EXPORT void audio_reserve_loop();
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif
/***
Copyright 2009 Lennart Poettering
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 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 <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "reserve.h"
struct rd_device {
int ref;
char *device_name;
char *application_name;
char *application_device_name;
char *service_name;
char *object_path;
int32_t priority;
DBusConnection *connection;
int owning:1;
int registered:1;
int filtering:1;
int gave_up:1;
rd_request_cb_t request_cb;
void *userdata;
};
#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>";
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) {
rd_device *d;
DBusError error;
DBusMessage *reply = NULL;
dbus_error_init(&error);
d = userdata;
assert(d->ref >= 1);
if (dbus_message_is_method_call(
m,
"org.freedesktop.ReserveDevice1",
"RequestRelease")) {
int32_t priority;
dbus_bool_t ret;
if (!dbus_message_get_args(
m,
&error,
DBUS_TYPE_INT32, &priority,
DBUS_TYPE_INVALID))
goto invalid;
ret = FALSE;
if (priority > d->priority && d->request_cb) {
d->ref++;
if (d->request_cb(d, 0) > 0) {
ret = TRUE;
d->gave_up = 1;
}
rd_release(d);
}
if (!(reply = dbus_message_new_method_return(m)))
goto oom;
if (!dbus_message_append_args(
reply,
DBUS_TYPE_BOOLEAN, &ret,
DBUS_TYPE_INVALID))
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.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 DBusHandlerResult filter_handler(
DBusConnection *c,
DBusMessage *m,
void *userdata) {
DBusMessage *reply;
rd_device *d;
DBusError error;
dbus_error_init(&error);
d = userdata;
if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameLost")) {
const char *name;
if (!dbus_message_get_args(
m,
&error,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID))
goto invalid;
if (strcmp(name, d->service_name) == 0 && d->owning) {
d->owning = 0;
if (!d->gave_up) {
d->ref++;
if (d->request_cb)
d->request_cb(d, 1);
d->gave_up = 1;
rd_release(d);
}
return DBUS_HANDLER_RESULT_HANDLED;
}
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
invalid:
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
};
int rd_acquire(
rd_device **_d,
DBusConnection *connection,
const char *device_name,
const char *application_name,
int32_t priority,
rd_request_cb_t request_cb,
DBusError *error) {
rd_device *d = NULL;
int r, k;
DBusError _error;
DBusMessage *m = NULL, *reply = NULL;
dbus_bool_t good;
if (!error)
error = &_error;
dbus_error_init(error);
if (!_d)
return -EINVAL;
if (!connection)
return -EINVAL;
if (!device_name)
return -EINVAL;
if (!request_cb && priority != INT32_MAX)
return -EINVAL;
if (!(d = calloc(sizeof(rd_device), 1)))
return -ENOMEM;
d->ref = 1;
if (!(d->device_name = strdup(device_name))) {
r = -ENOMEM;
goto fail;
}
if (!(d->application_name = strdup(application_name))) {
r = -ENOMEM;
goto fail;
}
d->priority = priority;
d->connection = dbus_connection_ref(connection);
d->request_cb = request_cb;
if (!(d->service_name = malloc(sizeof(SERVICE_PREFIX) + strlen(device_name)))) {
r = -ENOMEM;
goto fail;
}
sprintf(d->service_name, SERVICE_PREFIX "%s", d->device_name);
if (!(d->object_path = malloc(sizeof(OBJECT_PREFIX) + strlen(device_name)))) {
r = -ENOMEM;
goto fail;
}
sprintf(d->object_path, OBJECT_PREFIX "%s", d->device_name);
if ((k = dbus_bus_request_name(
d->connection,
d->service_name,
DBUS_NAME_FLAG_DO_NOT_QUEUE|
(priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0),
error)) < 0) {
r = -EIO;
goto fail;
}
if (k == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
goto success;
if (k != DBUS_REQUEST_NAME_REPLY_EXISTS) {
r = -EIO;
goto fail;
}
if (priority <= INT32_MIN) {
r = -EBUSY;
goto fail;
}