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) {
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);
}
pa_tagstruct *t;
pa_datum key, data;
pa_bool_t r;
+ void *state;
+ struct port_info *p_info;
pa_assert(u);
pa_assert(name);
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);
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;
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);
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 (!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 (pa_streq(old->profile, entry->profile)) {
+ if (!dirty) {
entry_free(old);
entry_free(entry);
return;
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);
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;
}