stream-manager-volume: Revise codes to reduce if-else statements 53/214053/2
authorSangchul Lee <sc11.lee@samsung.com>
Tue, 17 Sep 2019 10:30:45 +0000 (19:30 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Wed, 18 Sep 2019 01:43:04 +0000 (10:43 +0900)
It'll reduce cyclomatic complexity of SAM.

[Version] 11.1.76
[Issue Type] Refactoring

Change-Id: Ie44c99bc701dbb9741baec43723450040e5ebde5
Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
packaging/pulseaudio-modules-tizen.spec
src/stream-manager-volume.c

index 9296a04..1a8a1f8 100644 (file)
@@ -1,6 +1,6 @@
 Name:             pulseaudio-modules-tizen
 Summary:          Pulseaudio modules for Tizen
-Version:          11.1.75
+Version:          11.1.76
 Release:          0
 Group:            Multimedia/Audio
 License:          LGPL-2.1+
index 837aa7a..f524444 100644 (file)
@@ -383,38 +383,34 @@ int32_t set_volume_level_by_type(pa_stream_manager *m, stream_type_t stream_type
             return -1;
 
         PA_IDXSET_FOREACH(s, stream_type == STREAM_SINK_INPUT ? m->core->sink_inputs : m->core->source_outputs, idx) {
-            if (!(volume_type_str = pa_proplist_gets(GET_STREAM_PROPLIST(s, stream_type), PA_PROP_MEDIA_TIZEN_VOLUME_TYPE)))
-                continue;
-
             /* Update volume level of stream if it has requested the volume type */
-            if (!pa_safe_streq(volume_type_str, volume_type))
+            if (!(volume_type_str = pa_proplist_gets(GET_STREAM_PROPLIST(s, stream_type), PA_PROP_MEDIA_TIZEN_VOLUME_TYPE)) ||
+                !pa_safe_streq(volume_type_str, volume_type))
                 continue;
 
             /* Get modifier for gain */
             modifier_gain = pa_proplist_gets(GET_STREAM_PROPLIST(s, stream_type), PA_PROP_MEDIA_TIZEN_VOLUME_GAIN_TYPE);
-            if (modifier_gain && m->volume_modifiers) {
-                if ((modifier_gain_value = pa_hashmap_get(m->volume_modifiers, modifier_gain))) {
-                    volume_linear *= (*modifier_gain_value);
-                    pa_log_info("apply the modifier for the gain value[%s=>%f], result volume_linear[%f]",
-                                modifier_gain, *modifier_gain_value, volume_linear);
-                }
+            if (modifier_gain && m->volume_modifiers &&
+                (modifier_gain_value = pa_hashmap_get(m->volume_modifiers, modifier_gain))) {
+                volume_linear *= (*modifier_gain_value);
+                pa_log_info("apply the modifier for the gain value[%s=>%f], result volume_linear[%f]",
+                            modifier_gain, *modifier_gain_value, volume_linear);
             }
 
             apply_individual_ratio(m, s, volume_linear, &cv);
 
             if (stream_type == STREAM_SINK_INPUT)
                 pa_sink_input_set_volume((pa_sink_input*)s, &cv, true, true);
-            else if (stream_type == STREAM_SOURCE_OUTPUT)
+            else
                 pa_source_output_set_volume((pa_source_output*)s, &cv, true, true);
         }
     } else {
         PA_IDXSET_FOREACH(s, stream_type == STREAM_SINK_INPUT ? m->core->sink_inputs : m->core->source_outputs, idx) {
-            if (!(volume_type_str = pa_proplist_gets(GET_STREAM_PROPLIST(s, stream_type), PA_PROP_MEDIA_TIZEN_VOLUME_TYPE)))
-                continue;
-
             /* Get volume level of this type */
-            if (!(v = pa_hashmap_get(volumes, volume_type_str)))
+            if (!(volume_type_str = pa_proplist_gets(GET_STREAM_PROPLIST(s, stream_type), PA_PROP_MEDIA_TIZEN_VOLUME_TYPE)) ||
+                !(v = pa_hashmap_get(volumes, volume_type_str)))
                 continue;
+
             volume_level = v->values[stream_type].current_level;
 
             /* Get volume value */
@@ -423,19 +419,18 @@ int32_t set_volume_level_by_type(pa_stream_manager *m, stream_type_t stream_type
 
             /* Get modifier for gain */
             modifier_gain = pa_proplist_gets(GET_STREAM_PROPLIST(s, stream_type), PA_PROP_MEDIA_TIZEN_VOLUME_GAIN_TYPE);
-            if (modifier_gain && m->volume_modifiers) {
-                if ((modifier_gain_value = pa_hashmap_get(m->volume_modifiers, modifier_gain))) {
+            if (modifier_gain && m->volume_modifiers &&
+                (modifier_gain_value = pa_hashmap_get(m->volume_modifiers, modifier_gain))) {
                     volume_linear *= (*modifier_gain_value);
                     pa_log_info("apply the modifier for the gain value[%s=>%f], result volume_linear[%f]",
                                 modifier_gain, *modifier_gain_value, volume_linear);
-                }
             }
 
             apply_individual_ratio(m, s, volume_linear, &cv);
 
             if (stream_type == STREAM_SINK_INPUT)
                 pa_sink_input_set_volume((pa_sink_input*)s, &cv, true, true);
-            else if (stream_type == STREAM_SOURCE_OUTPUT)
+            else
                 pa_source_output_set_volume((pa_source_output*)s, &cv, true, true);
         }
     }
@@ -469,35 +464,35 @@ int32_t get_volume_level_by_type(pa_stream_manager *m, pa_volume_get_command_t c
             /* from HAL */
             if (pa_hal_interface_get_volume_level(m->hal, volume_type, CONVERT_TO_HAL_DIRECTION(stream_type), volume_level))
                 return -1;
-        } else {
-            /* from stream-manager */
-            if (!(v = pa_hashmap_get(volumes, volume_type))) {
-                pa_log_error("could not get volume_info, stream_type[%d], volume_type[%s]", stream_type, volume_type);
-                return -1;
-            }
-            *volume_level = v->values[stream_type].current_level;
-
+            return 0;
         }
+        /* from stream-manager */
+        if (!(v = pa_hashmap_get(volumes, volume_type))) {
+            pa_log_error("could not get volume_info, stream_type[%d], volume_type[%s]", stream_type, volume_type);
+            return -1;
+        }
+        *volume_level = v->values[stream_type].current_level;
+
     } else if (command == GET_VOLUME_MAX_LEVEL) {
         /* If it is the master volume type */
         if (pa_safe_streq(volume_type, MASTER_VOLUME_TYPE)) {
             *volume_level = MASTER_VOLUME_LEVEL_MAX;
+            return 0;
+        }
+        /* Get max level */
+        if (is_hal_volume) {
+            /* from HAL */
+            if (pa_hal_interface_get_volume_level_max(m->hal, volume_type, CONVERT_TO_HAL_DIRECTION(stream_type), volume_level))
+                return -1;
+            return 0;
+        }
+        /* from stream-manager */
+        v = pa_hashmap_get(volumes, volume_type);
+        if (v && v->values[stream_type].idx_volume_values) {
+            *volume_level = pa_idxset_size(v->values[stream_type].idx_volume_values);
         } else {
-            /* Get max level */
-            if (is_hal_volume) {
-                /* from HAL */
-                if (pa_hal_interface_get_volume_level_max(m->hal, volume_type, CONVERT_TO_HAL_DIRECTION(stream_type), volume_level))
-                    return -1;
-            } else {
-                /* from stream-manager */
-                v = pa_hashmap_get(volumes, volume_type);
-                if (v && v->values[stream_type].idx_volume_values)
-                    *volume_level = pa_idxset_size(v->values[stream_type].idx_volume_values);
-                else {
-                    pa_log_error("could not get volume_info, stream_type[%d], volume_type[%s]", stream_type, volume_type);
-                    return -1;
-                }
-            }
+            pa_log_error("could not get volume_info, stream_type[%d], volume_type[%s]", stream_type, volume_type);
+            return -1;
         }
     }
 
@@ -553,7 +548,7 @@ int32_t set_volume_level_by_idx(pa_stream_manager *m, stream_type_t stream_type,
 
     if (stream_type == STREAM_SINK_INPUT)
         pa_sink_input_set_volume((pa_sink_input*)s, &cv, true, true);
-    else if (stream_type == STREAM_SOURCE_OUTPUT)
+    else
         pa_source_output_set_volume((pa_source_output*)s, &cv, true, true);
 
     pa_log_debug("stream_type[%d], idx[%u]=>volume_type[%s], level[%u], value[%f]",
@@ -593,18 +588,17 @@ int32_t set_volume_level_with_new_data(pa_stream_manager *m, void *stream, strea
 
     /* Get modifier for gain */
     modifier_gain = pa_proplist_gets(GET_STREAM_NEW_PROPLIST(stream, stream_type), PA_PROP_MEDIA_TIZEN_VOLUME_GAIN_TYPE);
-    if (modifier_gain && m->volume_modifiers) {
-        if ((modifier_gain_value = pa_hashmap_get(m->volume_modifiers, modifier_gain))) {
-            volume_linear *= (*modifier_gain_value);
-            pa_log_info("apply the modifier for the gain value[%s=>%f], result volume_linear[%f]",
-                        modifier_gain, *modifier_gain_value, volume_linear);
-        }
+    if (modifier_gain && m->volume_modifiers &&
+        (modifier_gain_value = pa_hashmap_get(m->volume_modifiers, modifier_gain))) {
+        volume_linear *= (*modifier_gain_value);
+        pa_log_info("apply the modifier for the gain value[%s=>%f], result volume_linear[%f]",
+                    modifier_gain, *modifier_gain_value, volume_linear);
     }
 
     pa_cvolume_set(&cv, GET_STREAM_NEW_SAMPLE_SPEC(stream, stream_type).channels, pa_sw_volume_from_linear(volume_linear));
     if (stream_type == STREAM_SINK_INPUT)
         pa_sink_input_new_data_set_volume((pa_sink_input_new_data*)stream, &cv);
-    else if (stream_type == STREAM_SOURCE_OUTPUT)
+    else
         pa_source_output_new_data_set_volume((pa_source_output_new_data*)stream, &cv);
 
     pa_log_debug("stream_type[%d], volume_type[%s], level[%u], value[%f]",
@@ -657,25 +651,22 @@ int32_t set_volume_ratio_by_idx(pa_stream_manager *m, stream_type_t stream_type,
 
     /* Get modifier for gain */
     modifier_gain = pa_proplist_gets(GET_STREAM_PROPLIST(s, stream_type), PA_PROP_MEDIA_TIZEN_VOLUME_GAIN_TYPE);
-    if (modifier_gain && m->volume_modifiers) {
-        if ((modifier_gain_value = pa_hashmap_get(m->volume_modifiers, modifier_gain))) {
-            volume_linear *= (*modifier_gain_value);
-            pa_log_info("apply the modifier for the gain value[%s=>%f], result volume_linear[%f]",
-                        modifier_gain, *modifier_gain_value, volume_linear);
-        }
+    if (modifier_gain && m->volume_modifiers &&
+        (modifier_gain_value = pa_hashmap_get(m->volume_modifiers, modifier_gain))) {
+        volume_linear *= (*modifier_gain_value);
+        pa_log_info("apply the modifier for the gain value[%s=>%f], result volume_linear[%f]",
+                    modifier_gain, *modifier_gain_value, volume_linear);
     }
 
-    if (stream_type == STREAM_SINK_INPUT)
+    if (stream_type == STREAM_SINK_INPUT) {
         ((pa_sink_input*)s)->individual_volume_ratio = ratio;
-    else
-        ((pa_source_output*)s)->individual_volume_ratio = ratio;
-
-    apply_individual_ratio(m, s, volume_linear, &cv);
-
-    if (stream_type == STREAM_SINK_INPUT)
+        apply_individual_ratio(m, s, volume_linear, &cv);
         pa_sink_input_set_volume((pa_sink_input*)s, &cv, true, true);
-    else if (stream_type == STREAM_SOURCE_OUTPUT)
+    } else {
+        ((pa_source_output*)s)->individual_volume_ratio = ratio;
+        apply_individual_ratio(m, s, volume_linear, &cv);
         pa_source_output_set_volume((pa_source_output*)s, &cv, true, true);
+    }
 
     pa_log_debug("stream_type[%d], idx[%u]=>volume_type[%s], level[%u], ratio[%f], value[%f]",
                  stream_type, idx, volume_type_str, volume_level, ratio, volume_linear);
@@ -734,17 +725,15 @@ int32_t set_volume_mute_by_type(pa_stream_manager *m, stream_type_t stream_type,
     v->values[stream_type].is_muted = volume_mute;
 
     PA_IDXSET_FOREACH(s, (stream_type == STREAM_SINK_INPUT) ? m->core->sink_inputs : m->core->source_outputs, idx) {
-        if (!(volume_type_str = pa_proplist_gets(GET_STREAM_PROPLIST(s, stream_type), PA_PROP_MEDIA_TIZEN_VOLUME_TYPE))) {
-            continue;
-        }
-
         /* Update mute of stream if it has requested the volume type */
-        if (pa_safe_streq(volume_type_str, volume_type)) {
-            if (stream_type == STREAM_SINK_INPUT)
-                pa_sink_input_set_mute((pa_sink_input*)s, volume_mute, true);
-            else if (stream_type == STREAM_SOURCE_OUTPUT)
-                pa_source_output_set_mute((pa_source_output*)s, volume_mute, true);
+        if (!(volume_type_str = pa_proplist_gets(GET_STREAM_PROPLIST(s, stream_type), PA_PROP_MEDIA_TIZEN_VOLUME_TYPE)) ||
+            !pa_safe_streq(volume_type_str, volume_type)) {
+            continue;
         }
+        if (stream_type == STREAM_SINK_INPUT)
+            pa_sink_input_set_mute((pa_sink_input*)s, volume_mute, true);
+        else
+            pa_source_output_set_mute((pa_source_output*)s, volume_mute, true);
     }
 
     pa_log_info("stream_type[%d], volume_type[%s], mute[%d]", stream_type, volume_type, volume_mute);
@@ -802,7 +791,7 @@ int32_t set_volume_mute_by_idx(pa_stream_manager *m, uint32_t stream_idx, stream
             if (stream_idx == idx) {
                 if (stream_type == STREAM_SINK_INPUT)
                     pa_sink_input_set_mute((pa_sink_input*)s, volume_mute, true);
-                else if (stream_type == STREAM_SOURCE_OUTPUT)
+                else
                     pa_source_output_set_mute((pa_source_output*)s, volume_mute, true);
                 break;
             }
@@ -824,7 +813,7 @@ int32_t get_volume_mute_by_idx(pa_stream_manager *m, uint32_t stream_idx, stream
         if (stream_idx == idx) {
             if (stream_type == STREAM_SINK_INPUT)
                 *volume_mute = ((pa_sink_input*)s)->muted;
-            else if (stream_type == STREAM_SOURCE_OUTPUT)
+            else
                 *volume_mute = ((pa_source_output*)s)->muted;
             break;
         }
@@ -863,7 +852,7 @@ int32_t set_volume_mute_with_new_data(pa_stream_manager *m, void *stream, stream
 
     if (stream_type == STREAM_SINK_INPUT)
         pa_sink_input_new_data_set_muted((pa_sink_input_new_data*)stream, volume_mute);
-    else if (stream_type == STREAM_SOURCE_OUTPUT)
+    else
         pa_source_output_new_data_set_muted((pa_source_output_new_data*)stream, volume_mute);
 
     return 0;