Over the past few days I've been making a new PulseAudio module that detects the presence of JACK server, and adds a sources and a sink when JACK is started.

Here are the notes:

* This version uses JACK's dbus interface to detect server being started or stopped, i e /usr/bin/jackdbus instead of /usr/bin/jackd. If you use qjackctl to control the server, make sure that you check the checkbox "Enable D-Bus interface" under Setup -> Misc.

* It is deliberately named module-jackdbus-detect instead of module-jack-detect, both to emphasize the above, and to avoid name clashes with an imaginary (?) future module detecting whether headphones are plugged in or not.

* The patch is against stable-queue.

* It is probably ready for merging into the next version of PA. I'm saying probably, since during testing yesterday, I sometimes got segfaults. However, I believe these are related to something happening inside libjack when the server shuts down; since I can trigger the same segfaults even without my new module involved. That said; it takes fewer strokes with the module enabled, as you can just press "start" and "stop" in qjackctl until it happens...

* A question is about the default; patch 2 here adds it to default.pa. Should "connect" default to true or false in such a scenario? I think it depends on whether we're aiming for just showing the user the possibility (connect=false), or if we're aiming for a just-works experience (connect=true), which might upset a few people who don't want it to work that way...I've gone with connect=true as the default to mimic the behaviour of module-jack-sink and module-jack-source.

--
David Henningsson, Canonical Ltd.
http://launchpad.net/~diwic
>From d111edff2667dcdaad69a0d32bee8358966e5f42 Mon Sep 17 00:00:00 2001
From: David Henningsson <[email protected]>
Date: Tue, 30 Nov 2010 04:59:27 +0100
Subject: [PATCH 1/2] JACK: Add module-jackdbus-detect

Signed-off-by: David Henningsson <[email protected]>
---
 src/Makefile.am                           |   12 ++
 src/modules/jack/module-jackdbus-detect.c |  304 +++++++++++++++++++++++++++++
 2 files changed, 316 insertions(+), 0 deletions(-)
 create mode 100644 src/modules/jack/module-jackdbus-detect.c

diff --git a/src/Makefile.am b/src/Makefile.am
index 164faa6..799ab8d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1124,6 +1124,12 @@ if HAVE_JACK
 modlibexec_LTLIBRARIES += \
 		module-jack-sink.la \
 		module-jack-source.la
+
+if HAVE_DBUS
+modlibexec_LTLIBRARIES += \
+		module-jackdbus-detect.la
+endif
+
 endif
 
 if HAVE_GCONF
@@ -1228,6 +1234,7 @@ SYMDEF_FILES = \
 		modules/module-detect-symdef.h \
 		modules/rtp/module-rtp-send-symdef.h \
 		modules/rtp/module-rtp-recv-symdef.h \
+		modules/jack/module-jackdbus-detect-symdef.h \
 		modules/jack/module-jack-sink-symdef.h \
 		modules/jack/module-jack-source-symdef.h \
 		modules/module-volume-restore-symdef.h \
@@ -1607,6 +1614,11 @@ module_rtp_recv_la_CFLAGS = $(AM_CFLAGS)
 
 # JACK
 
+module_jackdbus_detect_la_SOURCES = modules/jack/module-jackdbus-detect.c
+module_jackdbus_detect_la_LDFLAGS = $(MODULE_LDFLAGS)
+module_jackdbus_detect_la_LIBADD = $(AM_LIBADD) $(DBUS_LIBS) libpulseco...@[email protected] $(JACK_LIBS) libpulsecomm...@[email protected] libpulse.la
+module_jackdbus_detect_la_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS) $(JACK_CFLAGS)
+
 module_jack_sink_la_SOURCES = modules/jack/module-jack-sink.c
 module_jack_sink_la_LDFLAGS = $(MODULE_LDFLAGS)
 module_jack_sink_la_LIBADD = $(AM_LIBADD) libpulseco...@[email protected] $(JACK_LIBS) libpulsecomm...@[email protected] libpulse.la
diff --git a/src/modules/jack/module-jackdbus-detect.c b/src/modules/jack/module-jackdbus-detect.c
new file mode 100644
index 0000000..f635b23
--- /dev/null
+++ b/src/modules/jack/module-jackdbus-detect.c
@@ -0,0 +1,304 @@
+/***
+  This file is part of PulseAudio.
+
+  Written by David Henningsson <[email protected]>
+  Copyright 2010 Canonical Ltd.
+
+  Some code taken from other parts of PulseAudio, these are
+  Copyright 2006-2009 Lennart Poettering
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio 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
+  General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulsecore/log.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-shared.h>
+
+#include "module-jackdbus-detect-symdef.h"
+
+PA_MODULE_AUTHOR("David Henningsson");
+PA_MODULE_DESCRIPTION("Adds JACK sink/source ports when JACK is started");
+PA_MODULE_LOAD_ONCE(TRUE);
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_USAGE("connect=<connect ports?>");
+
+#define JACK_SERVICE_NAME "org.jackaudio.service"
+#define JACK_INTERFACE_NAME "org.jackaudio.JackControl"
+#define JACK_INTERFACE_PATH "/org/jackaudio/Controller"
+
+#define SERVICE_FILTER				\
+	"type='signal',"			\
+	"sender='" DBUS_SERVICE_DBUS "',"	\
+	"interface='" DBUS_INTERFACE_DBUS "',"	\
+	"member='NameOwnerChanged',"		\
+	"arg0='" JACK_SERVICE_NAME "'"
+
+#define RUNNING_FILTER(_a)			\
+	"type='signal',"			\
+	"sender='" JACK_SERVICE_NAME "',"	\
+	"interface='" JACK_INTERFACE_NAME "',"	\
+	"member='" _a "'"
+
+static const char* const valid_modargs[] = {
+    "connect",
+    NULL
+};
+
+#define JACK_SS_SINK 0
+#define JACK_SS_SOURCE 1
+#define JACK_SS_COUNT 2
+
+static const char* const modnames[JACK_SS_COUNT] = {
+    "module-jack-sink",
+    "module-jack-source"
+};
+
+
+struct userdata {
+    pa_module *module;
+    pa_core *core;
+    pa_dbus_connection *connection;
+    pa_bool_t filter_added, match_added;
+    pa_bool_t is_service_started;
+    pa_bool_t autoconnect_ports;
+    /* Using index here protects us from module unloading without us knowing */
+    int jack_module_index[JACK_SS_COUNT];
+};
+
+
+static void ensure_ports_stopped(struct userdata* u)
+{
+    int i;
+    pa_assert(u);
+
+    for (i = 0; i < JACK_SS_COUNT; i++)
+        if (u->jack_module_index[i]) {
+            pa_module_unload_request_by_index(u->core, u->jack_module_index[i], TRUE);
+            u->jack_module_index[i] = 0;
+            pa_log_info("Stopped %s.", modnames[i]);
+        }
+}
+
+static void ensure_ports_started(struct userdata* u)
+{
+    int i;
+    pa_assert(u);
+
+    for (i = 0; i < JACK_SS_COUNT; i++)
+        if (!u->jack_module_index[i]) {
+            char* args;
+            pa_module* m;
+            args = pa_sprintf_malloc("connect=%s", pa_yes_no(u->autoconnect_ports));
+            m = pa_module_load(u->core, modnames[i], args);
+            pa_xfree(args);
+
+            if (m) {
+                pa_log_info("Successfully started %s.", modnames[i]);
+                u->jack_module_index[i] = m->index;
+            }
+            else
+                pa_log_info("Failed to start %s.", modnames[i]);
+        }
+}
+
+
+static pa_bool_t check_service_started(struct userdata* u)
+{
+    DBusError error;
+    DBusMessage *m = NULL, *reply = NULL;
+    pa_bool_t new_status = FALSE;
+    dbus_bool_t call_result;
+    pa_assert(u);
+
+    dbus_error_init(&error);
+
+    /* Just a safety check; it isn't such a big deal if the name disappears just after the call. */
+    if (!dbus_bus_name_has_owner(pa_dbus_connection_get(u->connection),
+            JACK_SERVICE_NAME, &error)) {
+        pa_log_debug("jackdbus isn't running.");
+        goto finish;
+    }
+
+    if (!(m = dbus_message_new_method_call(JACK_SERVICE_NAME, JACK_INTERFACE_PATH, JACK_INTERFACE_NAME, "IsStarted"))) {
+        pa_log("Failed to allocate IsStarted() method call.");
+        goto finish;
+    }
+
+    if (!(reply = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->connection), m, -1, &error))) {
+        pa_log("IsStarted() call failed: %s: %s", error.name, error.message);
+        goto finish;
+    }
+
+    if (!dbus_message_get_args(reply, &error, DBUS_TYPE_BOOLEAN, &call_result, DBUS_TYPE_INVALID)) {
+        pa_log("IsStarted() call return failed: %s: %s", error.name, error.message);
+        goto finish;
+    }
+
+    new_status = call_result;
+
+finish:
+    if (m)
+        dbus_message_unref(m);
+    if (reply)
+        dbus_message_unref(reply);
+
+    dbus_error_free(&error);
+    if (new_status)
+        ensure_ports_started(u);
+    else
+        ensure_ports_stopped(u);
+    u->is_service_started = new_status;
+    return new_status;
+}
+
+static DBusHandlerResult dbus_filter_handler(DBusConnection *c,	DBusMessage *s,	void *userdata)
+{
+    struct userdata *u = NULL;
+    DBusError error;
+
+    pa_assert(userdata);
+    u = ((pa_module*) userdata)->userdata;
+    pa_assert(u);
+
+    dbus_error_init(&error);
+
+    if (dbus_message_is_signal(s, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
+        const char *name, *old, *new;
+        if (!dbus_message_get_args(
+			    s,
+			    &error,
+			    DBUS_TYPE_STRING, &name,
+			    DBUS_TYPE_STRING, &old,
+			    DBUS_TYPE_STRING, &new,
+			    DBUS_TYPE_INVALID))
+            goto finish;
+        if (strcmp(name, JACK_SERVICE_NAME))
+            goto finish;
+
+        ensure_ports_stopped(u);
+        check_service_started(u);
+    }
+
+    else if (dbus_message_is_signal(s, JACK_INTERFACE_NAME, "ServerStarted")) {
+        ensure_ports_stopped(u);
+        check_service_started(u);
+    }
+
+    else if (dbus_message_is_signal(s, JACK_INTERFACE_NAME, "ServerStopped")) {
+        ensure_ports_stopped(u);
+    }
+
+finish:
+    dbus_error_free(&error);
+    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+int pa__init(pa_module *m)
+{
+    DBusError error;
+    pa_dbus_connection *connection = NULL;
+    struct userdata *u = NULL;
+    pa_modargs *ma;
+
+    pa_assert(m);
+
+    dbus_error_init(&error);
+
+    if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+        pa_log("Failed to parse module arguments");
+        goto fail;
+    }
+
+    m->userdata = u = pa_xnew0(struct userdata, 1);
+    u->core = m->core;
+    u->module = m;
+    u->autoconnect_ports = TRUE;
+
+    if (pa_modargs_get_value_boolean(ma, "connect", &u->autoconnect_ports) < 0) {
+        pa_log("Failed to parse connect= argument.");
+        goto fail;
+    }
+
+    if (!(connection = pa_dbus_bus_get(m->core, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) {
+
+        if (connection)
+            pa_dbus_connection_unref(connection);
+
+        pa_log_error("Unable to contact D-Bus session bus: %s: %s", error.name, error.message);
+        goto fail;
+    }
+    u->connection = connection;
+
+    if (!dbus_connection_add_filter(pa_dbus_connection_get(connection), dbus_filter_handler, m, NULL)) {
+        pa_log_error("Unable to add D-Bus filter");
+        goto fail;
+    }
+    u->filter_added = 1;
+
+    if (pa_dbus_add_matches(
+                pa_dbus_connection_get(connection), &error, SERVICE_FILTER,
+                RUNNING_FILTER("ServerStarted"), RUNNING_FILTER("ServerStopped"), NULL) < 0) {
+        pa_log_error("Unable to subscribe to signals: %s: %s", error.name, error.message);
+        goto fail;
+    }
+    u->match_added = 1;
+
+    check_service_started(u);
+
+    return 0;
+
+fail:
+    if (ma)
+        pa_modargs_free(ma);
+
+    dbus_error_free(&error);
+    pa__done(m);
+
+    return -1;
+}
+
+void pa__done(pa_module *m)
+{
+    struct userdata *u;
+
+    pa_assert(m);
+
+    if (!(u = m->userdata))
+        return;
+
+    ensure_ports_stopped(u);
+
+    if (u->match_added) {
+        pa_dbus_remove_matches(
+                pa_dbus_connection_get(u->connection), SERVICE_FILTER,
+                RUNNING_FILTER("ServerStarted"), RUNNING_FILTER("ServerStopped"), NULL);
+    }
+
+    if (u->filter_added) {
+        dbus_connection_remove_filter(pa_dbus_connection_get(u->connection), dbus_filter_handler, m);
+    }
+
+    if (u->connection) {
+        pa_dbus_connection_unref(u->connection);
+    }
+
+    pa_xfree(u);
+}
-- 
1.7.1

>From e5a6af86fa3463b186db528c259ad76d97d7110f Mon Sep 17 00:00:00 2001
From: David Henningsson <[email protected]>
Date: Tue, 30 Nov 2010 22:11:48 +0100
Subject: [PATCH 2/2] JACK: Load module-jackdbus-detect in default.pa

Signed-off-by: David Henningsson <[email protected]>
---
 src/daemon/default.pa.in |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)

diff --git a/src/daemon/default.pa.in b/src/daemon/default.pa.in
index 00c000e..e896385 100755
--- a/src/daemon/default.pa.in
+++ b/src/daemon/default.pa.in
@@ -62,6 +62,11 @@ load-module module-detect
 load-module module-bluetooth-discover
 .endif
 
+### Automatically connect sink and source if JACK server is present
+.ifexists module-jackdbus-det...@pa_soext@
+load-module module-jackdbus-detect
+.endif
+
 ### Load several protocols
 .ifexists module-esound-protocol-u...@pa_soext@
 load-module module-esound-protocol-unix
-- 
1.7.1

_______________________________________________
pulseaudio-discuss mailing list
[email protected]
https://tango.0pointer.de/mailman/listinfo/pulseaudio-discuss

Reply via email to