[pulseaudio-discuss] [PATCH] card-restore: Add the ability to save and restore the latency offset.

poljar (Damir Jelić) poljarinho at gmail.com
Wed Jul 4 15:57:05 PDT 2012


Hey Tanu.

I've changed the patch to use a hash map. I hope the rest of the changes
also make sense.

Sorry about the increased sloppiness.

Thanks, poljar.
-------------- next part --------------
>From 35bfadedefd2bc1acad305d9c1616931035313bb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?"poljar=20(Damir=20Jeli=C4=87)"?= <poljarinho at gmail.com>
Date: Wed, 27 Jun 2012 22:55:35 +0200
Subject: [PATCH] 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.
---
 src/modules/module-card-restore.c | 120 ++++++++++++++++++++++++++++++++++----
 1 file changed, 110 insertions(+), 10 deletions(-)

diff --git a/src/modules/module-card-restore.c b/src/modules/module-card-restore.c
index 1079a72..c2c9a84 100644
--- a/src/modules/module-card-restore.c
+++ b/src/modules/module-card-restore.c
@@ -68,11 +68,18 @@ 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;
+    uint32_t port_count;
+    pa_hashmap *port_info;
 };
 
 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
@@ -100,13 +107,21 @@ 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->port_info = 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_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->port_info, (pa_free2_cb_t) port_info_free, NULL);
+
     pa_xfree(e);
 }
 
@@ -114,6 +129,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 +139,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, e->port_count);
+
+    PA_HASHMAP_FOREACH(p_info, e->port_info, 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 +224,28 @@ static struct entry* entry_read(struct userdata *u, const char *name) {
 
     e->profile = pa_xstrdup(profile);
 
+    if (e->version >= 2) {
+        const char *port_name = NULL;
+        int64_t port_offset = 0;
+        struct port_info *p_info;
+        unsigned i;
+
+        if (pa_tagstruct_getu32(t, &e->port_count) < 0)
+            goto fail;
+
+        for (i = 0; i < e->port_count; i++) {
+            if (pa_tagstruct_gets(t, &port_name) < 0 ||
+                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->port_info, p_info->name, p_info) >= 0);
+        }
+    }
+
     if (!pa_tagstruct_eof(t))
         goto fail;
 
@@ -237,7 +282,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 +297,49 @@ 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);
+
+    entry->port_count = pa_hashmap_size(card->ports);
+
+    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->port_info, 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) ||
+                old->port_count != entry->port_count)
+            dirty = true;
+
+        else {
+            struct port_info *old_p_info;
+
+            PA_HASHMAP_FOREACH(old_p_info, old->port_info, state) {
+                if ((p_info = pa_hashmap_get(entry->port_info, 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;
@@ -266,7 +348,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 +361,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->port_info, 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;
 }
 
-- 
1.7.11.1



More information about the pulseaudio-discuss mailing list