card-restore: Add the ability to save and restore the latency offset.

module-card-restore now saves the latency offsets.

This change includes a entry version bump.

The entry now consists of a port count and a port name and offset for
every port that belongs to the relevant card.
This commit is contained in:
poljar (Damir Jelić) 2012-06-27 22:55:35 +02:00 committed by Tanu Kaskinen
parent f6d5476e7a
commit 758e5bc28e

View file

@ -68,11 +68,17 @@ struct userdata {
pa_database *database;
};
#define ENTRY_VERSION 1
#define ENTRY_VERSION 2
struct port_info {
char *name;
int64_t offset;
};
struct entry {
uint8_t version;
char *profile;
pa_hashmap *ports; /* Port name -> struct port_info */
};
static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
@ -100,13 +106,23 @@ static void trigger_save(struct userdata *u) {
static struct entry* entry_new(void) {
struct entry *r = pa_xnew0(struct entry, 1);
r->version = ENTRY_VERSION;
r->ports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
return r;
}
static void port_info_free(struct port_info *p_info, void *userdata) {
pa_assert(p_info);
pa_xfree(p_info->name);
pa_xfree(p_info);
}
static void entry_free(struct entry* e) {
pa_assert(e);
pa_xfree(e->profile);
pa_hashmap_free(e->ports, (pa_free2_cb_t) port_info_free, NULL);
pa_xfree(e);
}
@ -114,6 +130,8 @@ static pa_bool_t entry_write(struct userdata *u, const char *name, const struct
pa_tagstruct *t;
pa_datum key, data;
pa_bool_t r;
void *state;
struct port_info *p_info;
pa_assert(u);
pa_assert(name);
@ -122,6 +140,12 @@ static pa_bool_t entry_write(struct userdata *u, const char *name, const struct
t = pa_tagstruct_new(NULL, 0);
pa_tagstruct_putu8(t, e->version);
pa_tagstruct_puts(t, e->profile);
pa_tagstruct_putu32(t, pa_hashmap_size(e->ports));
PA_HASHMAP_FOREACH(p_info, e->ports, state) {
pa_tagstruct_puts(t, p_info->name);
pa_tagstruct_puts64(t, p_info->offset);
}
key.data = (char *) name;
key.size = strlen(name);
@ -201,6 +225,31 @@ static struct entry* entry_read(struct userdata *u, const char *name) {
e->profile = pa_xstrdup(profile);
if (e->version >= 2) {
uint32_t port_count = 0;
const char *port_name = NULL;
int64_t port_offset = 0;
struct port_info *p_info;
unsigned i;
if (pa_tagstruct_getu32(t, &port_count) < 0)
goto fail;
for (i = 0; i < port_count; i++) {
if (pa_tagstruct_gets(t, &port_name) < 0 ||
!port_name ||
pa_hashmap_get(e->ports, port_name) ||
pa_tagstruct_gets64(t, &port_offset) < 0)
goto fail;
p_info = pa_xnew(struct port_info, 1);
p_info->name = pa_xstrdup(port_name);
p_info->offset = port_offset;
pa_assert_se(pa_hashmap_put(e->ports, p_info->name, p_info) >= 0);
}
}
if (!pa_tagstruct_eof(t))
goto fail;
@ -237,7 +286,10 @@ fail:
static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
struct userdata *u = userdata;
struct entry *entry, *old;
void *state;
pa_card *card;
pa_device_port *p;
struct port_info *p_info;
pa_assert(c);
pa_assert(u);
@ -249,15 +301,47 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
if (!(card = pa_idxset_get_by_index(c->cards, idx)))
return;
if (!card->save_profile)
return;
entry = entry_new();
entry->profile = pa_xstrdup(card->active_profile->name);
if (card->save_profile)
entry->profile = pa_xstrdup(card->active_profile->name);
PA_HASHMAP_FOREACH(p, card->ports, state) {
p_info = pa_xnew(struct port_info, 1);
p_info->name = pa_xstrdup(p->name);
p_info->offset = p->latency_offset;
pa_assert_se(pa_hashmap_put(entry->ports, p_info->name, p_info) >= 0);
}
if ((old = entry_read(u, card->name))) {
bool dirty = false;
if (pa_streq(old->profile, entry->profile)) {
if (!card->save_profile)
entry->profile = pa_xstrdup(old->profile);
if (!pa_streq(old->profile, entry->profile) ||
pa_hashmap_size(old->ports) != pa_hashmap_size(entry->ports))
dirty = true;
else {
struct port_info *old_p_info;
PA_HASHMAP_FOREACH(old_p_info, old->ports, state) {
if ((p_info = pa_hashmap_get(entry->ports, old_p_info->name))) {
if (p_info->offset != old_p_info->offset) {
dirty = true;
break;
}
} else {
dirty = true;
break;
}
}
}
if (!dirty) {
entry_free(old);
entry_free(entry);
return;
@ -266,7 +350,10 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
entry_free(old);
}
pa_log_info("Storing profile for card %s.", card->name);
if (card->save_profile)
pa_log_info("Storing profile and port latency offsets for card %s.", card->name);
else
pa_log_info("Storing port latency offsets for card %s.", card->name);
if (entry_write(u, card->name, entry))
trigger_save(u);
@ -276,21 +363,36 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
static pa_hook_result_t card_new_hook_callback(pa_core *c, pa_card_new_data *new_data, struct userdata *u) {
struct entry *e;
void *state;
pa_device_port *p;
struct port_info *p_info;
pa_assert(new_data);
if ((e = entry_read(u, new_data->name)) && e->profile[0]) {
if (!(e = entry_read(u, new_data->name)))
return PA_HOOK_OK;
if (e->profile[0]) {
if (!new_data->active_profile) {
pa_log_info("Restoring profile for card %s.", new_data->name);
pa_card_new_data_set_profile(new_data, e->profile);
new_data->save_profile = TRUE;
} else
pa_log_debug("Not restoring profile for card %s, because already set.", new_data->name);
entry_free(e);
}
/* Always restore the latency offsets because their
* initial value is always 0 */
pa_log_info("Restoring port latency offsets for card %s.", new_data->name);
PA_HASHMAP_FOREACH(p_info, e->ports, state)
if ((p = pa_hashmap_get(new_data->ports, p_info->name)))
p->latency_offset = p_info->offset;
entry_free(e);
return PA_HOOK_OK;
}