media-session: store values as json

Save settings as jason and use a small json compatible tokenizer to
load settings instead of our own less flexible format.
Save settings with a prefix and filter out entries without prefix
Listen for changes in restore.stream metadata and update properties.
This commit is contained in:
Wim Taymans 2020-11-22 10:00:58 +01:00
parent b922f99293
commit 7ca8491be3
8 changed files with 507 additions and 128 deletions

View file

@ -40,9 +40,11 @@
#include "extensions/metadata.h"
#include "media-session.h"
#include "json.h"
#define NAME "default-nodes"
#define SESSION_KEY "default-nodes"
#define PREFIX "default."
#define SAVE_INTERVAL 1
@ -119,7 +121,8 @@ static void remove_idle_timeout(struct impl *impl)
int res;
if (impl->idle_timeout) {
if ((res = sm_media_session_save_state(impl->session, SESSION_KEY, impl->properties)) < 0)
if ((res = sm_media_session_save_state(impl->session,
SESSION_KEY, PREFIX, impl->properties)) < 0)
pw_log_error("can't save "SESSION_KEY" state: %s", spa_strerror(res));
pw_loop_destroy_source(main_loop, impl->idle_timeout);
impl->idle_timeout = NULL;
@ -173,7 +176,7 @@ static int metadata_property(void *object, uint32_t subject,
if (key == NULL)
pw_properties_clear(impl->properties);
else
pw_properties_set(impl->properties, key, name);
pw_properties_setf(impl->properties, key, "{ \"name\": \"%s\" }", name);
add_idle_timeout(impl);
}
return 0;
@ -193,12 +196,31 @@ static void session_create(void *data, struct sm_object *object)
return;
spa_dict_for_each(it, &impl->properties->dict) {
struct find_data d = { impl, it->value, SPA_ID_INVALID };
struct spa_json json[2];
int len;
const char *value;
char name [1024] = "\0";
struct find_data d;
spa_json_init(&json[0], it->value, strlen(it->value));
if (spa_json_enter_object(&json[0], &json[1]) <= 0)
continue;
while ((len = spa_json_next(&json[1], &value)) > 0) {
if (strncmp(value, "\"name\"", len) == 0) {
if (spa_json_get_string(&json[1], name, sizeof(name)) <= 0)
continue;
} else {
if (spa_json_next(&json[1], &value) <= 0)
break;
}
}
d = (struct find_data){ impl, name, SPA_ID_INVALID };
if (find_name(&d, object)) {
char val[16];
snprintf(val, sizeof(val)-1, "%u", d.id);
pw_log_info("found %s with id:%s restore as %s",
it->value, val, it->key);
name, val, it->key);
pw_metadata_set_property(impl->session->metadata,
PW_ID_CORE, it->key, SPA_TYPE_INFO_BASE"Id", val);
}
@ -260,7 +282,8 @@ int sm_default_nodes_start(struct sm_media_session *session)
return -ENOMEM;
}
if ((res = sm_media_session_load_state(impl->session, SESSION_KEY, impl->properties)) < 0)
if ((res = sm_media_session_load_state(impl->session,
SESSION_KEY, PREFIX, impl->properties)) < 0)
pw_log_info("can't load "SESSION_KEY" state: %s", spa_strerror(res));
sm_media_session_add_listener(impl->session, &impl->listener, &session_events, impl);