Add port order metadata to JACK sink / source ports
authorChristopher Arndt <chris@chrisarndt.de>
Sat, 1 May 2021 14:57:13 +0000 (16:57 +0200)
committerPulseAudio Marge Bot <pulseaudio-maintainers@lists.freedesktop.org>
Mon, 3 May 2021 12:19:17 +0000 (12:19 +0000)
Adds JACK metadata property to ports created by *module-jack-sink*
and *module-jack-source* with key `JACK_METADATA_ORDER`, the port index
(1-based, in order of creation) as value and type
`http://www.w3.org/2001/XMLSchema#int`.

This allows JACK applications, which use JACK metadata, to list or display
these ports in correct order.

See also: https://jackaudio.org/api/group__Metadata.html

Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/550>

src/modules/jack/module-jack-sink.c
src/modules/jack/module-jack-source.c

index a7c7230..5a690cb 100644 (file)
@@ -28,6 +28,8 @@
 #include <unistd.h>
 
 #include <jack/jack.h>
+#include <jack/metadata.h>
+#include <jack/uuid.h>
 
 #include <pulse/util.h>
 #include <pulse/xmalloc.h>
@@ -69,6 +71,8 @@ PA_MODULE_USAGE(
         "connect=<connect ports?>");
 
 #define DEFAULT_SINK_NAME "jack_out"
+#define METADATA_TYPE_INT "http://www.w3.org/2001/XMLSchema#int"
+#define METADATA_KEY_ORDER "http://jackaudio.org/metadata/order"
 
 struct userdata {
     pa_core *core;
@@ -301,6 +305,8 @@ int pa__init(pa_module*m) {
     const char **ports = NULL, **p;
     pa_sink_new_data data;
     jack_latency_range_t r;
+    jack_uuid_t port_uuid;
+    char port_order[4];
     size_t n;
 
     pa_assert(m);
@@ -389,6 +395,17 @@ int pa__init(pa_module*m) {
             pa_log("jack_port_register() failed.");
             goto fail;
         }
+
+        /* Set order of ports as JACK metadata, if possible. */
+        /* See: https://jackaudio.org/api/group__Metadata.html */
+        port_uuid = jack_port_uuid(u->port[i]);
+
+        if (!jack_uuid_empty(port_uuid)) {
+            if (snprintf(port_order, 4, "%d", i+1) >= 4)
+                pa_log("Port order metadata value > 999 truncated.");
+            if (jack_set_property(u->client, port_uuid, METADATA_KEY_ORDER, port_order, METADATA_TYPE_INT) != 0)
+                pa_log("jack_set_property() failed.");
+        }
     }
 
     pa_sink_new_data_init(&data);
index c454191..dbee5a6 100644 (file)
@@ -28,6 +28,8 @@
 #include <unistd.h>
 
 #include <jack/jack.h>
+#include <jack/metadata.h>
+#include <jack/uuid.h>
 
 #include <pulse/util.h>
 #include <pulse/xmalloc.h>
@@ -59,6 +61,8 @@ PA_MODULE_USAGE(
         "connect=<connect ports?>");
 
 #define DEFAULT_SOURCE_NAME "jack_in"
+#define METADATA_TYPE_INT "http://www.w3.org/2001/XMLSchema#int"
+#define METADATA_KEY_ORDER "http://jackaudio.org/metadata/order"
 
 struct userdata {
     pa_core *core;
@@ -249,6 +253,8 @@ int pa__init(pa_module*m) {
     const char **ports = NULL, **p;
     pa_source_new_data data;
     jack_latency_range_t r;
+    jack_uuid_t port_uuid;
+    char port_order[4];
     size_t n;
 
     pa_assert(m);
@@ -331,6 +337,17 @@ int pa__init(pa_module*m) {
             pa_log("jack_port_register() failed.");
             goto fail;
         }
+
+        /* Set order of ports as JACK metadata, if possible. */
+        /* See: https://jackaudio.org/api/group__Metadata.html */
+        port_uuid = jack_port_uuid(u->port[i]);
+
+        if (!jack_uuid_empty(port_uuid)) {
+            if (snprintf(port_order, 4, "%d", i+1) >= 4)
+                pa_log("Port order metadata value > 999 truncated.");
+            if (jack_set_property(u->client, port_uuid, METADATA_KEY_ORDER, port_order, METADATA_TYPE_INT) != 0)
+                pa_log("jack_set_property() failed.");
+        }
     }
 
     pa_source_new_data_init(&data);