pipewire: module-rt: use dbus helpers

This commit is contained in:
Barnabás Pőcze 2023-12-30 01:26:01 +01:00 committed by Wim Taymans
parent 91a9f816a7
commit 3a20bcadba

View file

@ -54,6 +54,7 @@
#ifdef HAVE_DBUS #ifdef HAVE_DBUS
#include <spa/support/dbus.h> #include <spa/support/dbus.h>
#include <spa-private/dbus-helpers.h>
#include <dbus/dbus.h> #include <dbus/dbus.h>
#endif #endif
@ -337,42 +338,33 @@ static int translate_error(const char *name)
static long long rtkit_get_int_property(struct impl *impl, const char *propname, static long long rtkit_get_int_property(struct impl *impl, const char *propname,
long long *propval) long long *propval)
{ {
DBusMessage *m = NULL, *r = NULL; spa_autoptr(DBusMessage) m = NULL, r = NULL;
DBusMessageIter iter, subiter; DBusMessageIter iter, subiter;
dbus_int64_t i64; dbus_int64_t i64;
dbus_int32_t i32; dbus_int32_t i32;
DBusError error;
int current_type; int current_type;
long long ret;
struct pw_rtkit_bus *connection = impl->rtkit_bus; struct pw_rtkit_bus *connection = impl->rtkit_bus;
dbus_error_init(&error);
if (!(m = dbus_message_new_method_call(impl->service_name, if (!(m = dbus_message_new_method_call(impl->service_name,
impl->object_path, impl->object_path,
"org.freedesktop.DBus.Properties", "Get"))) { "org.freedesktop.DBus.Properties", "Get"))) {
ret = -ENOMEM; return -ENOMEM;
goto finish;
} }
if (!dbus_message_append_args(m, if (!dbus_message_append_args(m,
DBUS_TYPE_STRING, &impl->interface, DBUS_TYPE_STRING, &impl->interface,
DBUS_TYPE_STRING, &propname, DBUS_TYPE_INVALID)) { DBUS_TYPE_STRING, &propname, DBUS_TYPE_INVALID)) {
ret = -ENOMEM; return -ENOMEM;
goto finish;
} }
if (!(r = dbus_connection_send_with_reply_and_block(connection->bus, m, -1, &error))) { spa_auto(DBusError) error = DBUS_ERROR_INIT;
ret = translate_error(error.name);
goto finish;
}
if (dbus_set_error_from_message(&error, r)) { if (!(r = dbus_connection_send_with_reply_and_block(connection->bus, m, -1, &error)))
ret = translate_error(error.name); return translate_error(error.name);
goto finish;
} if (dbus_set_error_from_message(&error, r))
return translate_error(error.name);
ret = -EBADMSG;
dbus_message_iter_init(r, &iter); dbus_message_iter_init(r, &iter);
while ((current_type = dbus_message_iter_get_arg_type(&iter)) != DBUS_TYPE_INVALID) { while ((current_type = dbus_message_iter_get_arg_type(&iter)) != DBUS_TYPE_INVALID) {
@ -385,13 +377,13 @@ static long long rtkit_get_int_property(struct impl *impl, const char *propname,
if (current_type == DBUS_TYPE_INT32) { if (current_type == DBUS_TYPE_INT32) {
dbus_message_iter_get_basic(&subiter, &i32); dbus_message_iter_get_basic(&subiter, &i32);
*propval = i32; *propval = i32;
ret = 0; return 0;
} }
if (current_type == DBUS_TYPE_INT64) { if (current_type == DBUS_TYPE_INT64) {
dbus_message_iter_get_basic(&subiter, &i64); dbus_message_iter_get_basic(&subiter, &i64);
*propval = i64; *propval = i64;
ret = 0; return 0;
} }
dbus_message_iter_next(&subiter); dbus_message_iter_next(&subiter);
@ -400,26 +392,15 @@ static long long rtkit_get_int_property(struct impl *impl, const char *propname,
dbus_message_iter_next(&iter); dbus_message_iter_next(&iter);
} }
finish: return -EBADMSG;
if (m)
dbus_message_unref(m);
if (r)
dbus_message_unref(r);
dbus_error_free(&error);
return ret;
} }
static int pw_rtkit_make_realtime(struct impl *impl, pid_t thread, int priority) static int pw_rtkit_make_realtime(struct impl *impl, pid_t thread, int priority)
{ {
DBusMessage *m = NULL; spa_autoptr(DBusMessage) m = NULL;
dbus_uint64_t pid; dbus_uint64_t pid;
dbus_uint64_t u64; dbus_uint64_t u64;
dbus_uint32_t u32, serial; dbus_uint32_t u32, serial;
int ret;
struct pw_rtkit_bus *connection = impl->rtkit_bus; struct pw_rtkit_bus *connection = impl->rtkit_bus;
if (thread == 0) if (thread == 0)
@ -428,8 +409,7 @@ static int pw_rtkit_make_realtime(struct impl *impl, pid_t thread, int priority)
if (!(m = dbus_message_new_method_call(impl->service_name, if (!(m = dbus_message_new_method_call(impl->service_name,
impl->object_path, impl->interface, impl->object_path, impl->interface,
"MakeThreadRealtimeWithPID"))) { "MakeThreadRealtimeWithPID"))) {
ret = -ENOMEM; return -ENOMEM;
goto finish;
} }
pid = (dbus_uint64_t) getpid(); pid = (dbus_uint64_t) getpid();
@ -440,33 +420,22 @@ static int pw_rtkit_make_realtime(struct impl *impl, pid_t thread, int priority)
DBUS_TYPE_UINT64, &pid, DBUS_TYPE_UINT64, &pid,
DBUS_TYPE_UINT64, &u64, DBUS_TYPE_UINT64, &u64,
DBUS_TYPE_UINT32, &u32, DBUS_TYPE_INVALID)) { DBUS_TYPE_UINT32, &u32, DBUS_TYPE_INVALID)) {
ret = -ENOMEM; return -ENOMEM;
goto finish;
} }
if (!dbus_connection_send(connection->bus, m, &serial)) { if (!dbus_connection_send(connection->bus, m, &serial))
ret = -EIO; return -EIO;
goto finish;
return 0;
} }
ret = 0;
finish:
if (m)
dbus_message_unref(m);
return ret;
}
static int pw_rtkit_make_high_priority(struct impl *impl, pid_t thread, int nice_level) static int pw_rtkit_make_high_priority(struct impl *impl, pid_t thread, int nice_level)
{ {
DBusMessage *m = NULL; spa_autoptr(DBusMessage) m = NULL;
dbus_uint64_t pid; dbus_uint64_t pid;
dbus_uint64_t u64; dbus_uint64_t u64;
dbus_int32_t s32; dbus_int32_t s32;
dbus_uint32_t serial; dbus_uint32_t serial;
int ret;
struct pw_rtkit_bus *connection = impl->rtkit_bus; struct pw_rtkit_bus *connection = impl->rtkit_bus;
if (thread == 0) if (thread == 0)
@ -475,8 +444,7 @@ static int pw_rtkit_make_high_priority(struct impl *impl, pid_t thread, int nice
if (!(m = dbus_message_new_method_call(impl->service_name, if (!(m = dbus_message_new_method_call(impl->service_name,
impl->object_path, impl->interface, impl->object_path, impl->interface,
"MakeThreadHighPriorityWithPID"))) { "MakeThreadHighPriorityWithPID"))) {
ret = -ENOMEM; return -ENOMEM;
goto finish;
} }
pid = (dbus_uint64_t) getpid(); pid = (dbus_uint64_t) getpid();
@ -487,21 +455,13 @@ static int pw_rtkit_make_high_priority(struct impl *impl, pid_t thread, int nice
DBUS_TYPE_UINT64, &pid, DBUS_TYPE_UINT64, &pid,
DBUS_TYPE_UINT64, &u64, DBUS_TYPE_UINT64, &u64,
DBUS_TYPE_INT32, &s32, DBUS_TYPE_INVALID)) { DBUS_TYPE_INT32, &s32, DBUS_TYPE_INVALID)) {
ret = -ENOMEM; return -ENOMEM;
goto finish;
} }
if (!dbus_connection_send(connection->bus, m, &serial)) {
ret = -EIO;
goto finish;
}
ret = 0;
finish: if (!dbus_connection_send(connection->bus, m, &serial))
return -EIO;
if (m) return 0;
dbus_message_unref(m);
return ret;
} }
#endif /* HAVE_DBUS */ #endif /* HAVE_DBUS */