From 4e087caa2b911dec87cd6677959871cc31fb279e Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Tue, 27 Jul 2021 15:50:05 +0200 Subject: [PATCH] jack: add per client match rules This makes it possible to configure per client settings such as latency and later also to lock the quantum for certain clients. See #1456 --- pipewire-jack/src/match-rules.c | 140 ++++++++++++++++++++++++++++++ pipewire-jack/src/meson.build | 1 + pipewire-jack/src/pipewire-jack.c | 23 +++++ src/daemon/jack.conf.in | 20 +++++ 4 files changed, 184 insertions(+) create mode 100644 pipewire-jack/src/match-rules.c diff --git a/pipewire-jack/src/match-rules.c b/pipewire-jack/src/match-rules.c new file mode 100644 index 000000000..8bb20bb3c --- /dev/null +++ b/pipewire-jack/src/match-rules.c @@ -0,0 +1,140 @@ +/* PipeWire + * + * Copyright © 2021 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. + */ + +#include +#include +#include +#include +#include +#include + +#include "config.h" + +#include +#include + +#include + +static bool find_match(struct spa_json *arr, const struct spa_dict *props) +{ + struct spa_json it[1]; + + while (spa_json_enter_object(arr, &it[0]) > 0) { + char key[256], val[1024]; + const char *str, *value; + int match = 0, fail = 0; + int len; + + while (spa_json_get_string(&it[0], key, sizeof(key)-1) > 0) { + bool success = false; + + if ((len = spa_json_next(&it[0], &value)) <= 0) + break; + + str = spa_dict_lookup(props, key); + + if (spa_json_is_null(value, len)) { + success = str == NULL; + } else { + spa_json_parse_string(value, SPA_MIN(len, 1023), val); + value = val; + len = strlen(val); + } + if (str != NULL) { + if (value[0] == '~') { + regex_t preg; + if (regcomp(&preg, value+1, REG_EXTENDED | REG_NOSUB) == 0) { + if (regexec(&preg, str, 0, NULL, 0) == 0) + success = true; + regfree(&preg); + } + } else if (strncmp(str, value, len) == 0 && + strlen(str) == (size_t)len) { + success = true; + } + } + if (success) { + match++; + pw_log_debug("'%s' match '%s' < > '%.*s'", key, str, len, value); + } + else + fail++; + } + if (match > 0 && fail == 0) + return true; + } + return false; +} + +int pw_jack_match_rules(const char *rules, size_t size, const struct spa_dict *props, + int (*matched) (void *data, const char *action, const char *val, int len), + void *data) +{ + const char *val; + struct spa_json it[4], actions; + int count = 0; + + spa_json_init(&it[0], rules, size); + if (spa_json_enter_array(&it[0], &it[1]) < 0) + return 0; + + while (spa_json_enter_object(&it[1], &it[2]) > 0) { + char key[64]; + bool have_match = false, have_actions = false; + + while (spa_json_get_string(&it[2], key, sizeof(key)-1) > 0) { + if (spa_streq(key, "matches")) { + if (spa_json_enter_array(&it[2], &it[3]) < 0) + break; + + have_match = find_match(&it[3], props); + } + else if (spa_streq(key, "actions")) { + if (spa_json_enter_object(&it[2], &actions) > 0) + have_actions = true; + } + else if (spa_json_next(&it[2], &val) <= 0) + break; + } + if (!have_match || !have_actions) + continue; + + while (spa_json_get_string(&actions, key, sizeof(key)-1) > 0) { + int res, len; + pw_log_debug("action %s", key); + + if ((len = spa_json_next(&actions, &val)) <= 0) + break; + + if (spa_json_is_container(val, len)) + len = spa_json_container_len(&actions, val, len); + + if ((res = matched(data, key, val, len)) < 0) + return res; + + count += res; + } + } + return count; +} diff --git a/pipewire-jack/src/meson.build b/pipewire-jack/src/meson.build index b0ab8dcba..b669e20f1 100644 --- a/pipewire-jack/src/meson.build +++ b/pipewire-jack/src/meson.build @@ -1,6 +1,7 @@ pipewire_jack_sources = [ 'export.c', 'pipewire-jack.c', + 'match-rules.c', 'ringbuffer.c', 'uuid.c', ] diff --git a/pipewire-jack/src/pipewire-jack.c b/pipewire-jack/src/pipewire-jack.c index a743a29b4..f2c17e5dc 100644 --- a/pipewire-jack/src/pipewire-jack.c +++ b/pipewire-jack/src/pipewire-jack.c @@ -395,6 +395,10 @@ static int do_sync(struct client *client); #include "metadata.c" +int pw_jack_match_rules(const char *rules, size_t size, const struct spa_dict *props, + int (*matched) (void *data, const char *action, const char *val, int len), + void *data); + static void init_port_pool(struct client *c, enum spa_direction direction) { spa_list_init(&c->ports[direction]); @@ -2824,6 +2828,15 @@ static void varargs_parse (struct client *c, jack_options_t options, va_list ap) } } + +static int execute_match(void *data, const char *action, const char *val, int len) +{ + struct client *client = data; + if (spa_streq(action, "update-props")) + pw_properties_update_string(client->props, val, len); + return 1; +} + SPA_EXPORT jack_client_t * jack_client_open (const char *client_name, jack_options_t options, @@ -2888,6 +2901,16 @@ jack_client_t * jack_client_open (const char *client_name, if ((str = getenv("PIPEWIRE_PROPS")) != NULL) pw_properties_update_string(client->props, str, strlen(str)); + + if ((str = pw_context_get_conf_section(client->context.context, + "jack.rules")) != NULL) { + const struct pw_properties *p = + pw_context_get_properties(client->context.context); + if (p != NULL) + pw_jack_match_rules(str, strlen(str), &p->dict, + execute_match, client); + } + if ((str = pw_properties_get(client->props, "jack.merge-monitor")) != NULL) client->merge_monitor = pw_properties_parse_bool(str); if ((str = pw_properties_get(client->props, "jack.short-name")) != NULL) diff --git a/src/daemon/jack.conf.in b/src/daemon/jack.conf.in index 1d283bca2..827fc31ff 100644 --- a/src/daemon/jack.conf.in +++ b/src/daemon/jack.conf.in @@ -56,6 +56,7 @@ context.modules = [ { name = libpipewire-module-metadata } ] +# global properties for all jack clients jack.properties = { #node.latency = 1024/48000 #jack.merge-monitor = false @@ -69,3 +70,22 @@ jack.properties = { # ignore-all: Ignore all self connect requests #jack.self-connect-mode = allow } + +# client specific properties +jack.rules = [ + { + matches = [ + { + # all keys must match the value. ~ starts regex. + #client.name = "Carla" + #application.process.binary = "jack_simple_client" + #application.name = "~jack_simple_client.*" + } + ] + actions = { + update-props = { + #node.latency = 512/48000 + } + } + } +]