metadata: store default-nodes as JSON

Don't just store the id in the metadata but a JSON object with
the node name. This makes it possible to easily introspect the
metadata and also extend the metadata with more fields later.

Instead of matching the metadata id to the global ids we now
have to match it against the name.
This commit is contained in:
Wim Taymans 2021-03-05 17:37:12 +01:00
parent 8d5cc7013d
commit c8fd34a41d
6 changed files with 462 additions and 202 deletions

View file

@ -71,8 +71,20 @@ struct impl {
struct default_node defaults[4];
struct pw_properties *properties;
unsigned int sync:1;
};
static struct default_node *find_default(struct impl *impl, const char *key)
{
struct default_node *def;
/* Check that the item key is a valid default key */
for (def = impl->defaults; def->key != NULL; ++def)
if (strcmp(key, def->key) == 0)
return def;
return NULL;
}
struct find_data {
struct impl *impl;
const char *name;
@ -94,32 +106,34 @@ static int find_name(void *data, struct sm_object *object)
return 0;
}
#if 0
static uint32_t find_id_for_name(struct impl *impl, const char *name)
{
struct find_data d = { impl, name, SPA_ID_INVALID };
sm_media_session_for_each_object(impl->session, find_name, &d);
return d.id;
}
#endif
static const char *find_name_for_id(struct impl *impl, uint32_t id)
static int json_object_find(const char *obj, const char *key, char *value, size_t len)
{
struct sm_object *obj;
const char *str;
struct spa_json it[2];
const char *v;
char k[128];
if (id == SPA_ID_INVALID)
return NULL;
spa_json_init(&it[0], obj, strlen(obj));
if (spa_json_enter_object(&it[0], &it[1]) <= 0)
return -EINVAL;
obj = sm_media_session_find_object(impl->session, id);
if (obj == NULL)
return NULL;
if (strcmp(obj->type, PW_TYPE_INTERFACE_Node) == 0 &&
obj->props &&
(str = pw_properties_get(obj->props, PW_KEY_NODE_NAME)) != NULL)
return str;
return NULL;
while (spa_json_get_string(&it[1], k, sizeof(k)-1) > 0) {
if (strcmp(k, key) == 0) {
if (spa_json_get_string(&it[1], value, len) <= 0)
continue;
return 0;
} else {
if (spa_json_next(&it[1], &v) <= 0)
break;
}
}
return -ENOENT;
}
static void remove_idle_timeout(struct impl *impl)
@ -160,27 +174,41 @@ static int metadata_property(void *object, uint32_t subject,
const char *key, const char *type, const char *value)
{
struct impl *impl = object;
uint32_t val;
bool changed = false;
int changed = 0;
if (impl->sync)
return 0;
if (subject == PW_ID_CORE) {
struct default_node *def;
val = (key && value) ? (uint32_t)atoi(value) : SPA_ID_INVALID;
for (def = impl->defaults; def->key != NULL; ++def) {
if (key == NULL || strcmp(key, def->key) == 0) {
changed = (def->value != val);
def->value = val;
if (key == NULL) {
pw_properties_clear(impl->properties);
changed++;
} else {
uint32_t id;
struct default_node *def;
char name[1024];
if ((def = find_default(impl, key)) == NULL)
return 0;
if (value == NULL) {
def->value = SPA_ID_INVALID;
} else {
if (json_object_find(value, "name", name, sizeof(name)) < 0)
return 0;
if ((id = find_id_for_name(impl, name)) == SPA_ID_INVALID)
return 0;
def->value = id;
changed += pw_properties_set(impl->properties,
key, value);
}
}
}
if (changed) {
const char *name = find_name_for_id(impl, val);
if (key == NULL)
pw_properties_clear(impl->properties);
else
pw_properties_setf(impl->properties, key, "{ \"name\": \"%s\" }", name);
if (changed)
add_idle_timeout(impl);
}
return 0;
}
@ -198,42 +226,22 @@ static void session_create(void *data, struct sm_object *object)
return;
spa_dict_for_each(item, &impl->properties->dict) {
struct spa_json it[2];
const char *value;
char name [1024] = "\0", key[128];
char name [1024] = "\0";
struct find_data d;
spa_json_init(&it[0], item->value, strlen(item->value));
if (spa_json_enter_object(&it[0], &it[1]) <= 0)
if (find_default(impl, item->key) == NULL)
continue;
if (json_object_find(item->value, "name", name, sizeof(name)) < 0)
continue;
while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
if (strcmp(key, "name") == 0) {
if (spa_json_get_string(&it[1], name, sizeof(name)) <= 0)
continue;
} else {
if (spa_json_next(&it[1], &value) <= 0)
break;
}
}
d = (struct find_data){ impl, name, SPA_ID_INVALID };
if (find_name(&d, object)) {
const struct default_node *def;
/* Check that the item key is a valid default key */
for (def = impl->defaults; def->key != NULL; ++def)
if (item->key != NULL && strcmp(item->key, def->key) == 0)
break;
if (def->key == NULL)
continue;
if (impl->session->metadata != NULL) {
char val[16];
snprintf(val, sizeof(val), "%u", d.id);
pw_log_info("found %s with id:%s restore as %s",
name, val, item->key);
pw_log_info("found %s with id:%u restore as %s",
name, d.id, item->key);
pw_metadata_set_property(impl->session->metadata,
PW_ID_CORE, item->key, SPA_TYPE_INFO_BASE"Id", val);
PW_ID_CORE, item->key, "Spa:String:JSON", item->value);
}
}
}
@ -288,10 +296,10 @@ int sm_default_nodes_start(struct sm_media_session *session)
impl->session = session;
impl->context = session->context;
impl->defaults[0] = (struct default_node){ DEFAULT_CONFIG_AUDIO_SINK_KEY, SPA_ID_INVALID };
impl->defaults[1] = (struct default_node){ DEFAULT_CONFIG_AUDIO_SOURCE_KEY, SPA_ID_INVALID };
impl->defaults[2] = (struct default_node){ DEFAULT_CONFIG_VIDEO_SOURCE_KEY, SPA_ID_INVALID };
impl->defaults[3] = (struct default_node){ NULL, SPA_ID_INVALID };
impl->defaults[0] = (struct default_node){ DEFAULT_CONFIG_AUDIO_SINK_KEY, };
impl->defaults[1] = (struct default_node){ DEFAULT_CONFIG_AUDIO_SOURCE_KEY, };
impl->defaults[2] = (struct default_node){ DEFAULT_CONFIG_VIDEO_SOURCE_KEY, };
impl->defaults[3] = (struct default_node){ NULL, };
impl->properties = pw_properties_new(NULL, NULL);
if (impl->properties == NULL) {

View file

@ -35,6 +35,7 @@
#include <spa/param/audio/format-utils.h>
#include <spa/param/props.h>
#include <spa/debug/pod.h>
#include <spa/utils/json.h>
#include "pipewire/pipewire.h"
#include "extensions/metadata.h"
@ -60,8 +61,8 @@
struct default_node {
char *key;
char *key_config;
uint32_t value;
uint32_t config;
char *value;
char *config;
};
struct impl {
@ -362,11 +363,56 @@ static void destroy_node(struct impl *impl, struct node *node)
sm_object_remove_data((struct sm_object*)node->obj, SESSION_KEY);
}
static struct node *find_node_by_id(struct impl *impl, uint32_t id)
static inline int strzcmp(const char *s1, const char *s2)
{
if (s1 == s2)
return 0;
if (s1 == NULL || s2 == NULL)
return 1;
return strcmp(s1, s2);
}
static int json_object_find(const char *obj, const char *key, char *value, size_t len)
{
struct spa_json it[2];
const char *v;
char k[128];
spa_json_init(&it[0], obj, strlen(obj));
if (spa_json_enter_object(&it[0], &it[1]) <= 0)
return -EINVAL;
while (spa_json_get_string(&it[1], k, sizeof(k)-1) > 0) {
if (strcmp(k, key) == 0) {
if (spa_json_get_string(&it[1], value, len) <= 0)
continue;
return 0;
} else {
if (spa_json_next(&it[1], &v) <= 0)
break;
}
}
return -ENOENT;
}
static bool check_node_name(struct node *node, const char *name)
{
const char *str;
if ((str = pw_properties_get(node->obj->obj.props, PW_KEY_NODE_NAME)) != NULL &&
name != NULL && strcmp(str, name) == 0)
return true;
return false;
}
static struct node *find_node_by_id_name(struct impl *impl, uint32_t id, const char *name)
{
struct node *node;
uint32_t name_id = name ? atoi(name) : SPA_ID_INVALID;
spa_list_for_each(node, &impl->node_list, link) {
if (node->id == id)
if (node->id == id || node->id == name_id)
return node;
if (check_node_name(node, name))
return node;
}
return NULL;
@ -417,7 +463,6 @@ static void session_create(void *data, struct sm_object *object)
static void session_remove(void *data, struct sm_object *object)
{
struct default_node *def;
struct impl *impl = data;
pw_log_debug(NAME " %p: remove global '%d'", impl, object->id);
@ -431,12 +476,7 @@ static void session_remove(void *data, struct sm_object *object)
if (n->peer == node)
n->peer = NULL;
}
for (def = impl->defaults; def->key != NULL; ++def)
if (def->config == object->id)
def->config = SPA_ID_INVALID;
}
sm_media_session_schedule_rescan(impl->session);
}
@ -491,14 +531,18 @@ static int find_node(void *data, struct node *node)
if (node->media) {
bool is_default = false;
if (strcmp(node->media, "Audio") == 0) {
if (node->direction == PW_DIRECTION_INPUT)
is_default = impl->defaults[DEFAULT_AUDIO_SINK].config == node->id;
is_default = check_node_name(node,
impl->defaults[DEFAULT_AUDIO_SINK].config);
else if (node->direction == PW_DIRECTION_OUTPUT)
is_default = impl->defaults[DEFAULT_AUDIO_SOURCE].config == node->id;
is_default = check_node_name(node,
impl->defaults[DEFAULT_AUDIO_SOURCE].config);
} else if (strcmp(node->media, "Video") == 0) {
if (node->direction == PW_DIRECTION_OUTPUT)
is_default = impl->defaults[DEFAULT_VIDEO_SOURCE].config == node->id;
is_default = check_node_name(node,
impl->defaults[DEFAULT_VIDEO_SOURCE].config);
}
if (is_default)
priority += 10000;
@ -813,19 +857,30 @@ static void refresh_auto_default_nodes(struct impl *impl)
if (impl->session->metadata == NULL)
return;
pw_log_debug(NAME" %p: refresh", impl);
/* Auto set default nodes */
for (def = impl->defaults; def->key != NULL; ++def) {
struct node *node;
node = find_auto_default_node(impl, def);
if (node == NULL && def->value != SPA_ID_INVALID) {
def->value = SPA_ID_INVALID;
pw_metadata_set_property(impl->session->metadata, PW_ID_CORE, def->key, NULL, NULL);
} else if (node != NULL && def->value != node->id) {
char buf[64];
def->value = node->id;
snprintf(buf, sizeof(buf), "%d", node->id);
pw_metadata_set_property(impl->session->metadata, PW_ID_CORE, def->key,
SPA_TYPE_INFO_BASE"Id", buf);
if (node == NULL && def->value != NULL) {
def->value = NULL;
pw_metadata_set_property(impl->session->metadata,
PW_ID_CORE, def->key, NULL, NULL);
} else if (node != NULL) {
const char *name = pw_properties_get(node->obj->obj.props, PW_KEY_NODE_NAME);
char buf[1024];
if (strzcmp(name, def->value) == 0)
continue;
free(def->value);
def->value = strdup(name);
snprintf(buf, sizeof(buf), "{ \"name\": \"%s\" }", name);
pw_metadata_set_property(impl->session->metadata,
PW_ID_CORE, def->key,
"Spa:String:JSON", buf);
}
}
}
@ -905,21 +960,32 @@ static int metadata_property(void *object, uint32_t subject,
const char *key, const char *type, const char *value)
{
struct impl *impl = object;
uint32_t val = (key && value) ? (uint32_t)atoi(value) : SPA_ID_INVALID;
if (subject == PW_ID_CORE) {
struct default_node *def;
bool changed = false;
char *val = NULL;
if (key != NULL && value != NULL) {
char name[1024];
pw_log_info("meta %s: %s", key, value);
if (json_object_find(value, "name", name, sizeof(name)) < 0)
return 0;
pw_log_info("meta name: %s", name);
val = name;
}
for (def = impl->defaults; def->key != NULL; ++def) {
if (key == NULL || strcmp(key, def->key_config) == 0) {
if (def->config != val)
if (strzcmp(def->config, val) != 0)
changed = true;
def->config = val;
free(def->config);
def->config = val ? strdup(val) : NULL;
}
if (key == NULL || strcmp(key, def->key) == 0) {
bool eff_changed = (def->value != val);
def->value = val;
bool eff_changed = strzcmp(def->value, val) != 0;
free(def->value);
def->value = val ? strdup(val) : NULL;
/* The effective value was changed. In case it was changed by
* someone else than us, reset the value to avoid confusion. */
@ -927,23 +993,21 @@ static int metadata_property(void *object, uint32_t subject,
refresh_auto_default_nodes(impl);
}
}
if (changed)
sm_media_session_schedule_rescan(impl->session);
} else {
if (val != SPA_ID_INVALID && strcmp(key, "target.node") == 0) {
} else if (key != NULL && strcmp(key, "target.node") == 0) {
if (value != NULL) {
struct node *src_node, *dst_node;
dst_node = find_node_by_id(impl, val);
src_node = dst_node ? find_node_by_id(impl, subject) : NULL;
dst_node = find_node_by_id_name(impl, SPA_ID_INVALID, value);
src_node = dst_node ? find_node_by_id_name(impl, subject, NULL) : NULL;
if (dst_node && src_node)
handle_move(impl, src_node, dst_node);
} else if (val == SPA_ID_INVALID && key != NULL &&
strcmp(key, "target.node") == 0) {
} else {
/* Unset target node. Schedule rescan to re-link, if needed. */
struct node *src_node;
src_node = find_node_by_id(impl, subject);
src_node = find_node_by_id_name(impl, subject, NULL);
if (src_node) {
free(src_node->obj->target_node);
src_node->obj->target_node = NULL;
@ -974,15 +1038,15 @@ int sm_policy_node_start(struct sm_media_session *session)
impl->sample_rate = 48000;
impl->defaults[DEFAULT_AUDIO_SINK] = (struct default_node){
DEFAULT_AUDIO_SINK_KEY, DEFAULT_CONFIG_AUDIO_SINK_KEY, SPA_ID_INVALID, SPA_ID_INVALID
DEFAULT_AUDIO_SINK_KEY, DEFAULT_CONFIG_AUDIO_SINK_KEY, NULL, NULL
};
impl->defaults[DEFAULT_AUDIO_SOURCE] = (struct default_node){
DEFAULT_AUDIO_SOURCE_KEY, DEFAULT_CONFIG_AUDIO_SOURCE_KEY, SPA_ID_INVALID, SPA_ID_INVALID
DEFAULT_AUDIO_SOURCE_KEY, DEFAULT_CONFIG_AUDIO_SOURCE_KEY, NULL, NULL
};
impl->defaults[DEFAULT_VIDEO_SOURCE] = (struct default_node){
DEFAULT_VIDEO_SOURCE_KEY, DEFAULT_CONFIG_VIDEO_SOURCE_KEY, SPA_ID_INVALID, SPA_ID_INVALID
DEFAULT_VIDEO_SOURCE_KEY, DEFAULT_CONFIG_VIDEO_SOURCE_KEY, NULL, NULL
};
impl->defaults[3] = (struct default_node){ NULL, NULL, SPA_ID_INVALID, SPA_ID_INVALID };
impl->defaults[3] = (struct default_node){ NULL, NULL, NULL, NULL };
flag = pw_properties_get(session->props, NAME ".streams-follow-default");
impl->streams_follow_default = (flag != NULL && pw_properties_parse_bool(flag));

View file

@ -139,8 +139,8 @@ struct client {
uint32_t subscribed;
struct pw_manager_object *metadata_default;
uint32_t default_sink;
uint32_t default_source;
char *default_sink;
char *default_source;
struct pw_manager_object *metadata_routes;
struct pw_properties *routes;
@ -899,25 +899,75 @@ static void manager_removed(void *data, struct pw_manager_object *o)
send_default_change_subscribe_event(client, object_is_sink(o), object_is_source_or_monitor(o));
}
static int json_object_find(const char *obj, const char *key, char *value, size_t len)
{
struct spa_json it[2];
const char *v;
char k[128];
spa_json_init(&it[0], obj, strlen(obj));
if (spa_json_enter_object(&it[0], &it[1]) <= 0)
return -EINVAL;
while (spa_json_get_string(&it[1], k, sizeof(k)-1) > 0) {
if (strcmp(k, key) == 0) {
if (spa_json_get_string(&it[1], value, len) <= 0)
continue;
return 0;
} else {
if (spa_json_next(&it[1], &v) <= 0)
break;
}
}
return -ENOENT;
}
static inline int strzcmp(const char *s1, const char *s2)
{
if (s1 == s2)
return 0;
if (s1 == NULL || s2 == NULL)
return 1;
return strcmp(s1, s2);
}
static void manager_metadata(void *data, struct pw_manager_object *o,
uint32_t subject, const char *key, const char *type, const char *value)
{
struct client *client = data;
uint32_t val;
bool changed = false;
pw_log_debug("meta id:%d subject:%d key:%s type:%s value:%s",
o->id, subject, key, type, value);
if (subject == PW_ID_CORE && o == client->metadata_default) {
val = (key && value) ? (uint32_t)atoi(value) : SPA_ID_INVALID;
char name[1024];
if (key == NULL || strcmp(key, "default.audio.sink") == 0) {
changed = client->default_sink != val;
client->default_sink = val;
if (value != NULL) {
if (json_object_find(value,
"name", name, sizeof(name)) < 0)
value = NULL;
else
value = name;
}
if ((changed = strzcmp(client->default_sink, value))) {
free(client->default_sink);
client->default_sink = strdup(value);
}
}
if (key == NULL || strcmp(key, "default.audio.source") == 0) {
changed = client->default_source != val;
client->default_source = val;
if (value != NULL) {
if (json_object_find(value,
"name", name, sizeof(name)) < 0)
value = NULL;
else
value = name;
}
if ((changed = strzcmp(client->default_source, value))) {
free(client->default_source);
client->default_source = strdup(value);
}
}
if (changed)
send_default_change_subscribe_event(client, true, true);
@ -2594,11 +2644,13 @@ static const char *get_default(struct client *client, bool sink)
spa_zero(sel);
if (sink) {
sel.type = object_is_sink;
sel.id = client->default_sink;
sel.key = PW_KEY_NODE_NAME;
sel.value = client->default_sink;
def = DEFAULT_SINK;
} else {
sel.type = object_is_source_or_monitor;
sel.id = client->default_source;
sel.key = PW_KEY_NODE_NAME;
sel.value = client->default_source;
def = DEFAULT_SOURCE;
}
sel.accumulate = select_best;
@ -4801,7 +4853,7 @@ static int do_set_default(struct client *client, uint32_t command, uint32_t tag,
if ((res = pw_manager_set_metadata(manager, client->metadata_default,
PW_ID_CORE,
sink ? METADATA_CONFIG_DEFAULT_SINK : METADATA_CONFIG_DEFAULT_SOURCE,
SPA_TYPE_INFO_BASE"Id", "%d", o->id)) < 0)
"Spa:String:JSON", "{ \"name\": \"%s\" }", name)) < 0)
return res;
return reply_simple_ack(client, tag);
@ -5280,6 +5332,8 @@ static void client_free(struct client *client)
client->disconnecting = true;
pw_core_disconnect(client->core);
}
free(client->default_sink);
free(client->default_source);
if (client->props)
pw_properties_free(client->props);
if (client->routes)

View file

@ -42,6 +42,7 @@
#include <spa/param/audio/type-info.h>
#include <spa/param/props.h>
#include <spa/utils/result.h>
#include <spa/utils/json.h>
#include <spa/debug/types.h>
#include <pipewire/pipewire.h>
@ -105,8 +106,8 @@ struct data {
struct spa_hook registry_listener;
struct pw_metadata *metadata;
struct spa_hook metadata_listener;
uint32_t default_sink;
uint32_t default_source;
char default_sink[1024];
char default_source[1024];
struct pw_stream *stream;
struct spa_hook stream_listener;
@ -647,17 +648,47 @@ static const struct pw_core_events core_events = {
.error = on_core_error,
};
static int json_object_find(const char *obj, const char *key, char *value, size_t len)
{
struct spa_json it[2];
const char *v;
char k[128];
spa_json_init(&it[0], obj, strlen(obj));
if (spa_json_enter_object(&it[0], &it[1]) <= 0)
return -EINVAL;
while (spa_json_get_string(&it[1], k, sizeof(k)-1) > 0) {
if (strcmp(k, key) == 0) {
if (spa_json_get_string(&it[1], value, len) <= 0)
continue;
return 0;
} else {
if (spa_json_next(&it[1], &v) <= 0)
break;
}
}
return -ENOENT;
}
static int metadata_property(void *object,
uint32_t subject, const char *key, const char *type, const char *value)
{
struct data *data = object;
if (subject == PW_ID_CORE) {
uint32_t val = (key && value) ? (uint32_t)atoi(value) : SPA_ID_INVALID;
if (key == NULL || strcmp(key, "default.audio.sink") == 0)
data->default_sink = val;
if (key == NULL || strcmp(key, "default.audio.source") == 0)
data->default_source = val;
if (key == NULL || strcmp(key, "default.audio.sink") == 0) {
if (value == NULL ||
json_object_find(value, "name",
data->default_sink, sizeof(data->default_sink)) < 0)
data->default_sink[0] = '\0';
}
if (key == NULL || strcmp(key, "default.audio.source") == 0) {
if (value == NULL ||
json_object_find(value, "name",
data->default_source, sizeof(data->default_source)) < 0)
data->default_source[0] = '\0';
}
}
return 0;
}
@ -1326,7 +1357,6 @@ int main(int argc, char *argv[])
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
const char *prog;
int exit_code = EXIT_FAILURE, c, ret;
struct target *target, *target_default;
enum pw_stream_flags flags = 0;
pw_init(&argc, &argv);
@ -1339,9 +1369,6 @@ int main(int argc, char *argv[])
else
prog = argv[0];
data.default_source = SPA_ID_INVALID;
data.default_sink = SPA_ID_INVALID;
/* prime the mode from the program name */
if (!strcmp(prog, "pw-play"))
data.mode = mode_playback;
@ -1676,9 +1703,10 @@ int main(int argc, char *argv[])
exit_code = EXIT_SUCCESS;
} else {
if (data.targets_listed) {
uint32_t default_id;
struct target *target, *target_default;
char *default_name;
default_id = (data.mode == mode_record) ?
default_name = (data.mode == mode_record) ?
data.default_source : data.default_sink;
exit_code = EXIT_SUCCESS;
@ -1687,12 +1715,12 @@ int main(int argc, char *argv[])
target_default = NULL;
spa_list_for_each(target, &data.targets, link) {
if (target_default == NULL ||
default_id == target->id ||
(default_id == SPA_ID_INVALID &&
strcmp(default_name, target->name) == 0 ||
(default_name[0] == '\0' &&
target->prio > target_default->prio))
target_default = target;
}
printf("Available targets (\"*\" denotes default): %d\n", default_id);
printf("Available targets (\"*\" denotes default): %s\n", default_name);
spa_list_for_each(target, &data.targets, link) {
printf("%s\t%"PRIu32": description=\"%s\" prio=%d\n",
target == target_default ? "*" : "",
@ -1703,6 +1731,7 @@ int main(int argc, char *argv[])
/* destroy targets */
while (!spa_list_is_empty(&data.targets)) {
struct target *target;
target = spa_list_last(&data.targets, struct target, link);
spa_list_remove(&target->link);
target_destroy(target);