[pulseaudio-discuss] [PATCH 2/5] device-port: Initialize profiles in pa_device_port_new().

Tanu Kaskinen tanu.kaskinen at digia.com
Tue Apr 17 01:08:55 PDT 2012


This way there's no need to always check whether the port
happens to have profiles or not.
---
 src/modules/alsa/alsa-mixer.c                 |    1 -
 src/modules/module-switch-on-port-available.c |   71 ++++++++++++-------------
 src/pulsecore/device-port.c                   |    2 +-
 src/pulsecore/device-port.h                   |    2 +-
 src/pulsecore/protocol-native.c               |   12 ++---
 5 files changed, 42 insertions(+), 46 deletions(-)

diff --git a/src/modules/alsa/alsa-mixer.c b/src/modules/alsa/alsa-mixer.c
index a60afe5..574f44b 100644
--- a/src/modules/alsa/alsa-mixer.c
+++ b/src/modules/alsa/alsa-mixer.c
@@ -4394,7 +4394,6 @@ static pa_device_port* device_port_alsa_init(pa_hashmap *ports,
         pa_assert(p);
         pa_hashmap_put(ports, p->name, p);
         pa_proplist_update(p->proplist, PA_UPDATE_REPLACE, path->proplist);
-        p->profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
 
         data = PA_DEVICE_PORT_DATA(p);
         data->path = path;
diff --git a/src/modules/module-switch-on-port-available.c b/src/modules/module-switch-on-port-available.c
index f198e44..bcb5a4c 100644
--- a/src/modules/module-switch-on-port-available.c
+++ b/src/modules/module-switch-on-port-available.c
@@ -55,51 +55,50 @@ static pa_bool_t try_to_switch_profile(pa_card *card, pa_device_port *port) {
 
     pa_log_debug("Finding best profile");
 
-    if (port->profiles)
-        PA_HASHMAP_FOREACH(profile, port->profiles, state) {
-            if (best_profile && best_profile->priority >= profile->priority)
-                continue;
+    PA_HASHMAP_FOREACH(profile, port->profiles, state) {
+        if (best_profile && best_profile->priority >= profile->priority)
+            continue;
 
-            if (!card->active_profile) {
-                best_profile = profile;
-                continue;
-            }
+        if (!card->active_profile) {
+            best_profile = profile;
+            continue;
+        }
 
-            /* We make a best effort to keep other direction unchanged */
-            if (!port->is_input) {
-                if (card->active_profile->n_sources != profile->n_sources)
-                    continue;
+        /* We make a best effort to keep other direction unchanged */
+        if (!port->is_input) {
+            if (card->active_profile->n_sources != profile->n_sources)
+                continue;
 
-                if (card->active_profile->max_source_channels != profile->max_source_channels)
-                    continue;
-            }
+            if (card->active_profile->max_source_channels != profile->max_source_channels)
+                continue;
+        }
 
-            if (!port->is_output) {
-                if (card->active_profile->n_sinks != profile->n_sinks)
-                    continue;
+        if (!port->is_output) {
+            if (card->active_profile->n_sinks != profile->n_sinks)
+                continue;
 
-                if (card->active_profile->max_sink_channels != profile->max_sink_channels)
-                    continue;
-            }
+            if (card->active_profile->max_sink_channels != profile->max_sink_channels)
+                continue;
+        }
 
-            if (port->is_output) {
-                /* Try not to switch to HDMI sinks from analog when HDMI is becoming available */
-                uint32_t state2;
-                pa_sink *sink;
-                pa_bool_t found_active_port = FALSE;
-                PA_IDXSET_FOREACH(sink, card->sinks, state2) {
-                    if (!sink->active_port)
-                        continue;
-                    if (sink->active_port->available != PA_PORT_AVAILABLE_NO)
-                        found_active_port = TRUE;
-                }
-                if (found_active_port)
+        if (port->is_output) {
+            /* Try not to switch to HDMI sinks from analog when HDMI is becoming available */
+            uint32_t state2;
+            pa_sink *sink;
+            pa_bool_t found_active_port = FALSE;
+            PA_IDXSET_FOREACH(sink, card->sinks, state2) {
+                if (!sink->active_port)
                     continue;
+                if (sink->active_port->available != PA_PORT_AVAILABLE_NO)
+                    found_active_port = TRUE;
             }
-
-            best_profile = profile;
+            if (found_active_port)
+                continue;
         }
 
+        best_profile = profile;
+    }
+
     if (!best_profile) {
         pa_log_debug("No suitable profile found");
         return FALSE;
@@ -156,7 +155,7 @@ static pa_hook_result_t port_available_hook_callback(pa_core *c, pa_device_port
 
     find_sink_and_source(card, port, &sink, &source);
 
-    is_active_profile = port->profiles && card->active_profile &&
+    is_active_profile = card->active_profile &&
         card->active_profile == pa_hashmap_get(port->profiles, card->active_profile->name);
     is_active_port = (sink && sink->active_port == port) || (source && source->active_port == port);
 
diff --git a/src/pulsecore/device-port.c b/src/pulsecore/device-port.c
index 50c99b8..c2d8fff 100644
--- a/src/pulsecore/device-port.c
+++ b/src/pulsecore/device-port.c
@@ -94,9 +94,9 @@ pa_device_port *pa_device_port_new(pa_core *c, const char *name, const char *des
     p->core = c;
     p->priority = 0;
     p->available = PA_PORT_AVAILABLE_UNKNOWN;
-    p->profiles = NULL;
     p->is_input = FALSE;
     p->is_output = FALSE;
+    p->profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
     p->proplist = pa_proplist_new();
 
     return p;
diff --git a/src/pulsecore/device-port.h b/src/pulsecore/device-port.h
index 4e90a62..63d5ccf 100644
--- a/src/pulsecore/device-port.h
+++ b/src/pulsecore/device-port.h
@@ -48,7 +48,7 @@ struct pa_device_port {
     pa_port_available_t available;         /* PA_PORT_AVAILABLE_UNKNOWN, PA_PORT_AVAILABLE_NO or PA_PORT_AVAILABLE_YES */
 
     pa_proplist *proplist;
-    pa_hashmap *profiles; /* Can be NULL. Does not own the profiles */
+    pa_hashmap *profiles; /* Does not own the profiles */
     pa_bool_t is_input:1;
     pa_bool_t is_output:1;
 
diff --git a/src/pulsecore/protocol-native.c b/src/pulsecore/protocol-native.c
index 487eb7a..5b2c433 100644
--- a/src/pulsecore/protocol-native.c
+++ b/src/pulsecore/protocol-native.c
@@ -3268,20 +3268,18 @@ static void card_fill_tagstruct(pa_native_connection *c, pa_tagstruct *t, pa_car
         pa_tagstruct_putu32(t, pa_hashmap_size(card->ports));
 
         PA_HASHMAP_FOREACH(port, card->ports, state) {
+            void *state2;
+
             pa_tagstruct_puts(t, port->name);
             pa_tagstruct_puts(t, port->description);
             pa_tagstruct_putu32(t, port->priority);
             pa_tagstruct_putu32(t, port->available);
             pa_tagstruct_putu8(t, /* FIXME: port->direction */ (port->is_input ? PA_DIRECTION_INPUT : 0) | (port->is_output ? PA_DIRECTION_OUTPUT : 0));
             pa_tagstruct_put_proplist(t, port->proplist);
+            pa_tagstruct_putu32(t, pa_hashmap_size(port->profiles));
 
-            if (port->profiles) {
-                void* state2;
-                pa_tagstruct_putu32(t, pa_hashmap_size(port->profiles));
-                PA_HASHMAP_FOREACH(p, port->profiles, state2)
-                    pa_tagstruct_puts(t, p->name);
-            } else
-                pa_tagstruct_putu32(t, 0);
+            PA_HASHMAP_FOREACH(p, port->profiles, state2)
+                pa_tagstruct_puts(t, p->name);
         }
 
     } else
-- 
1.7.10



More information about the pulseaudio-discuss mailing list