stream-manager: Revise init_stream_map() to use sub-functions 86/214286/3 accepted/tizen/unified/20190923.110254 submit/tizen/20190923.062727
authorSangchul Lee <sc11.lee@samsung.com>
Fri, 20 Sep 2019 01:05:10 +0000 (10:05 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Fri, 20 Sep 2019 03:06:28 +0000 (03:06 +0000)
It'll reduce cyclomatic complexity of SAM.

[Version] 11.1.83
[Issue Type] Refactoring

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

index af55cd8..5ad02e2 100644 (file)
@@ -1,6 +1,6 @@
 Name:             pulseaudio-modules-tizen
 Summary:          Pulseaudio modules for Tizen
-Version:          11.1.82
+Version:          11.1.83
 Release:          0
 Group:            Multimedia/Audio
 License:          LGPL-2.1+
index cb9e9ec..5468d09 100644 (file)
@@ -648,231 +648,273 @@ static void deinit_stream_map(pa_stream_manager *m) {
     }
 }
 
-static int init_stream_map(pa_stream_manager *m) {
-    volume_info *v = NULL;
-    stream_info *s = NULL;
-    latency_info *l = NULL;
-    json_object *o;
+static int get_latency_infos(json_object *o, pa_hashmap *latency_infos) {
     json_object *array_o;
     json_object *array_item_o;
     json_object *item_o;
-    json_object *sub_array_o;
     int array_length = 0;
-    int sub_array_length = 0;
     const char *type = NULL;
-    const char *role = NULL;
-    int i = 0, j = 0;
-    json_object *out_device_o;
-    json_object *in_device_o;
-    json_object *framework_o;
-
-    pa_assert(m);
+    latency_info *l = NULL;
+    int i;
 
-    o = json_object_from_file(STREAM_MAP_FILE);
-    if (o == NULL) {
-        pa_log_error("Read stream-map file(%s) failed", STREAM_MAP_FILE);
-        return -1;
-    }
+    pa_assert(latency_infos);
+    pa_assert(o);
 
-    /* Latencies */
-    m->latency_infos = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, pa_xfree);
     if (json_object_object_get_ex(o, STREAM_MAP_LATENCIES, &array_o) && json_object_is_type(array_o, json_type_array)) {
         array_length = json_object_array_length(array_o);
         for (i = 0; i < array_length; i++) {
             if ((array_item_o = json_object_array_get_idx(array_o, i)) && json_object_is_type(array_item_o, json_type_object)) {
                 l = pa_xmalloc0(sizeof(latency_info));
                 pa_log_debug("latency found [%d]", i);
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_LATENCY_TYPE, &item_o) && json_object_is_type(item_o, json_type_string)) {
-                    type = json_object_get_string(item_o);
-                    pa_log_debug(" - type : %s", type);
-                } else {
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_LATENCY_TYPE, &item_o) || !json_object_is_type(item_o, json_type_string)) {
                     pa_log_error("Get type failed");
                     goto fail;
                 }
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_LATENCY_FRAGSIZE_MS, &item_o) && json_object_is_type(item_o, json_type_int)) {
-                    l->fragsize_ms = json_object_get_int(item_o);
-                    pa_log_debug(" - fragsize-ms : %d", l->fragsize_ms);
-                } else {
+                type = json_object_get_string(item_o);
+                pa_log_debug(" - type : %s", type);
+
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_LATENCY_FRAGSIZE_MS, &item_o) || !json_object_is_type(item_o, json_type_int)) {
                     pa_log_error("Get fragsize-ms failed");
                     goto fail;
                 }
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_LATENCY_TLENGTH_MS, &item_o) && json_object_is_type(item_o, json_type_int)) {
-                    l->tlength_ms = json_object_get_int(item_o);
-                    pa_log_debug(" - tlength-ms : %d", l->tlength_ms);
-                } else {
+                l->fragsize_ms = json_object_get_int(item_o);
+                pa_log_debug(" - fragsize-ms : %d", l->fragsize_ms);
+
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_LATENCY_TLENGTH_MS, &item_o) || !json_object_is_type(item_o, json_type_int)) {
                     pa_log_error("Get tlength-ms failed");
                     goto fail;
                 }
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_LATENCY_MINREQ_MS, &item_o) && json_object_is_type(item_o, json_type_int)) {
-                    l->minreq_ms = json_object_get_int(item_o);
-                    pa_log_debug(" - minreq-ms : %d", l->minreq_ms);
-                } else {
+                l->tlength_ms = json_object_get_int(item_o);
+                pa_log_debug(" - tlength-ms : %d", l->tlength_ms);
+
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_LATENCY_MINREQ_MS, &item_o) || !json_object_is_type(item_o, json_type_int)) {
                     pa_log_error("Get minreq-ms failed");
                     goto fail;
                 }
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_LATENCY_PREBUF_MS, &item_o) && json_object_is_type(item_o, json_type_int)) {
-                    l->prebuf_ms = json_object_get_int(item_o);
-                    pa_log_debug(" - prebuf-ms : %d", l->prebuf_ms);
-                } else {
+                l->minreq_ms = json_object_get_int(item_o);
+                pa_log_debug(" - minreq-ms : %d", l->minreq_ms);
+
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_LATENCY_PREBUF_MS, &item_o) || !json_object_is_type(item_o, json_type_int)) {
                     pa_log_error("Get prebuf-ms failed");
                     goto fail;
                 }
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_LATENCY_MAXLENGTH, &item_o) && json_object_is_type(item_o, json_type_int)) {
-                    l->maxlength = json_object_get_int(item_o);
-                    pa_log_debug(" - maxlength : %d", l->maxlength);
-                } else {
+                l->prebuf_ms = json_object_get_int(item_o);
+                pa_log_debug(" - prebuf-ms : %d", l->prebuf_ms);
+
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_LATENCY_MAXLENGTH, &item_o) || !json_object_is_type(item_o, json_type_int)) {
                     pa_log_error("Get maxlength failed");
                     goto fail;
                 }
-                pa_hashmap_put(m->latency_infos, (void*)type, l);
+                l->maxlength = json_object_get_int(item_o);
+                pa_log_debug(" - maxlength : %d", l->maxlength);
+
+                pa_hashmap_put(latency_infos, (void*)type, l);
                 l = NULL;
             }
         }
     }
+    return 0;
+
+fail:
+    pa_xfree(l);
+    return -1;
+}
+
+static int get_volume_infos(json_object *o, pa_hashmap *volume_infos) {
+    json_object *array_o;
+    json_object *array_item_o;
+    json_object *item_o;
+    int array_length = 0;
+    const char *type = NULL;
+    volume_info *v = NULL;
+    int i;
+
+    pa_assert(volume_infos);
+    pa_assert(o);
 
-    /* Volumes */
-    m->volume_infos = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, pa_xfree);
     if (json_object_object_get_ex(o, STREAM_MAP_VOLUMES, &array_o) && json_object_is_type(array_o, json_type_array)) {
         array_length = json_object_array_length(array_o);
         for (i = 0; i < array_length; i++) {
             if ((array_item_o = json_object_array_get_idx(array_o, i)) && json_object_is_type(array_item_o, json_type_object)) {
                 v = pa_xmalloc0(sizeof(volume_info));
                 pa_log_debug("volume found [%d]", i);
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_VOLUME_TYPE, &item_o) && json_object_is_type(item_o, json_type_string)) {
-                    type = json_object_get_string(item_o);
-                    pa_log_debug(" - type : %s", type);
-                } else {
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_VOLUME_TYPE, &item_o) || !json_object_is_type(item_o, json_type_string)) {
                     pa_log_error("Get volume type failed");
                     goto fail;
                 }
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_VOLUME_IS_FOR_HAL, &item_o) && json_object_is_type(item_o, json_type_int)) {
-                    v->is_hal_volume_type = (bool)json_object_get_int(item_o);
-                    pa_log_debug(" - is-hal-volume : %d", v->is_hal_volume_type);
-                } else {
+                type = json_object_get_string(item_o);
+                pa_log_debug(" - type : %s", type);
+
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_VOLUME_IS_FOR_HAL, &item_o) || !json_object_is_type(item_o, json_type_int)) {
                     pa_log_error("Get is-hal-volume failed");
                     goto fail;
                 }
-                pa_hashmap_put(m->volume_infos, (void*)type, v);
+                v->is_hal_volume_type = (bool)json_object_get_int(item_o);
+                pa_log_debug(" - is-hal-volume : %d", v->is_hal_volume_type);
+
+                pa_hashmap_put(volume_infos, (void*)type, v);
                 v = NULL;
             }
         }
     }
+    return 0;
+
+fail:
+    pa_xfree(v);
+    return -1;
+}
+
+static int get_stream_infos(json_object *o, pa_hashmap *stream_infos) {
+    json_object *array_o;
+    json_object *array_item_o;
+    json_object *item_o;
+    json_object *sub_array_o;
+    int array_length = 0;
+    int sub_array_length = 0;
+    const char *role = NULL;
+    stream_info *s = NULL;
+    json_object *out_device_o;
+    json_object *in_device_o;
+    json_object *framework_o;
+    int i, j;
+
+    pa_assert(stream_infos);
+    pa_assert(o);
 
-    /* Streams */
-    m->stream_infos = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t)stream_info_free);
     if (json_object_object_get_ex(o, STREAM_MAP_STREAMS, &array_o) && json_object_is_type(array_o, json_type_array)) {
         array_length = json_object_array_length(array_o);
         for (i = 0; i < array_length; i++) {
-
             if ((array_item_o = json_object_array_get_idx(array_o, i)) && json_object_is_type(array_item_o, json_type_object)) {
                 s = pa_xmalloc0(sizeof(stream_info));
                 pa_log_debug("stream found [%d]", i);
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_STREAM_ROLE, &item_o) && json_object_is_type(item_o, json_type_string)) {
-                    role = json_object_get_string(item_o);
-                    pa_log_debug(" - role : %s", role);
-                } else {
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_STREAM_ROLE, &item_o) || !json_object_is_type(item_o, json_type_string)) {
                     pa_log_error("Get stream role failed");
                     goto fail;
                 }
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_STREAM_PRIORITY, &item_o) && json_object_is_type(item_o, json_type_int)) {
-                    s->priority = json_object_get_int(item_o);
-                    pa_log_debug(" - priority : %d", s->priority);
-                } else {
+                role = json_object_get_string(item_o);
+                pa_log_debug(" - role : %s", role);
+
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_STREAM_PRIORITY, &item_o) || !json_object_is_type(item_o, json_type_int)) {
                     pa_log_error("Get stream priority failed");
                     goto fail;
                 }
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_STREAM_ROUTE_TYPE, &item_o) && json_object_is_type(item_o, json_type_string)) {
-                    if (convert_route_type(json_object_get_string(item_o), &(s->route_type))) {
-                        pa_log_error("convert stream route-type failed");
-                        goto fail;
-                    }
-                    pa_log_debug(" - route-type : %d", s->route_type);
-                } else {
+                s->priority = json_object_get_int(item_o);
+                pa_log_debug(" - priority : %d", s->priority);
+
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_STREAM_ROUTE_TYPE, &item_o) || !json_object_is_type(item_o, json_type_string)) {
                     pa_log_error("Get stream route-type failed");
                     goto fail;
                 }
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_STREAM_VOLUME_TYPES, &sub_array_o) && json_object_is_type(sub_array_o, json_type_object)) {
-                    if (json_object_object_get_ex(sub_array_o, STREAM_MAP_STREAM_VOLUME_TYPE_IN, &item_o) && json_object_is_type(item_o, json_type_string))
-                        s->volume_types[STREAM_DIRECTION_IN] = json_object_get_string(item_o);
-                    else {
-                        pa_log_error("Get stream volume-type-in failed");
-                        goto fail;
-                    }
-                    if (json_object_object_get_ex(sub_array_o, STREAM_MAP_STREAM_VOLUME_TYPE_OUT, &item_o) && json_object_is_type(item_o, json_type_string))
-                        s->volume_types[STREAM_DIRECTION_OUT] = json_object_get_string(item_o);
-                    else {
-                        pa_log_error("Get stream volume-type-out failed");
-                        goto fail;
-                    }
-                    pa_log_debug(" - volume-types : in[%s], out[%s]", s->volume_types[STREAM_DIRECTION_IN], s->volume_types[STREAM_DIRECTION_OUT]);
-                } else {
+                if (convert_route_type(json_object_get_string(item_o), &(s->route_type))) {
+                    pa_log_error("convert stream route-type failed");
+                    goto fail;
+                }
+                pa_log_debug(" - route-type : %d", s->route_type);
+
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_STREAM_VOLUME_TYPES, &sub_array_o) || !json_object_is_type(sub_array_o, json_type_object)) {
                     pa_log_error("Get stream volume-types failed");
                     goto fail;
                 }
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_STREAM_AVAIL_IN_DEVICES, &sub_array_o) && json_object_is_type(sub_array_o, json_type_array)) {
-                    j = 0;
-                    s->idx_avail_in_devices = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
-                    sub_array_length = json_object_array_length(sub_array_o);
-                    pa_log_debug(" - avail-in-devices");
-                    for (j = 0; j < sub_array_length; j++) {
-                        if ((in_device_o = json_object_array_get_idx(sub_array_o, j)) && json_object_is_type(in_device_o, json_type_string)) {
-                            pa_idxset_put(s->idx_avail_in_devices, (void*)json_object_get_string(in_device_o), NULL);
-                            pa_log_debug("      device[%d] : %s", j, json_object_get_string(in_device_o));
-                           }
-                       }
-                } else {
+                if (!json_object_object_get_ex(sub_array_o, STREAM_MAP_STREAM_VOLUME_TYPE_IN, &item_o) || !json_object_is_type(item_o, json_type_string)) {
+                    pa_log_error("Get stream volume-type-in failed");
+                    goto fail;
+                }
+                s->volume_types[STREAM_DIRECTION_IN] = json_object_get_string(item_o);
+
+                if (!json_object_object_get_ex(sub_array_o, STREAM_MAP_STREAM_VOLUME_TYPE_OUT, &item_o) || !json_object_is_type(item_o, json_type_string)) {
+                    pa_log_error("Get stream volume-type-out failed");
+                    goto fail;
+                }
+                s->volume_types[STREAM_DIRECTION_OUT] = json_object_get_string(item_o);
+
+                pa_log_debug(" - volume-types : in[%s], out[%s]", s->volume_types[STREAM_DIRECTION_IN], s->volume_types[STREAM_DIRECTION_OUT]);
+
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_STREAM_AVAIL_IN_DEVICES, &sub_array_o) || !json_object_is_type(sub_array_o, json_type_array)) {
                     pa_log_error("Get stream avail-in-devices failed");
                     goto fail;
                 }
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_STREAM_AVAIL_OUT_DEVICES, &sub_array_o) && json_object_is_type(sub_array_o, json_type_array)) {
-                    j = 0;
-                    s->idx_avail_out_devices = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
-                    sub_array_length = json_object_array_length(sub_array_o);
-                    pa_log_debug(" - avail-out-devices");
-                    for (j = 0; j < sub_array_length; j++) {
-                        if ((out_device_o = json_object_array_get_idx(sub_array_o, j)) && json_object_is_type(out_device_o, json_type_string)) {
-                            pa_idxset_put(s->idx_avail_out_devices, (void*)json_object_get_string(out_device_o), NULL);
-                            pa_log_debug("      device[%d] : %s", j, json_object_get_string(out_device_o));
-                           }
-                       }
-                } else {
+                s->idx_avail_in_devices = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+                sub_array_length = json_object_array_length(sub_array_o);
+                pa_log_debug(" - avail-in-devices");
+                for (j = 0; j < sub_array_length; j++) {
+                    if ((in_device_o = json_object_array_get_idx(sub_array_o, j)) && json_object_is_type(in_device_o, json_type_string)) {
+                        pa_idxset_put(s->idx_avail_in_devices, (void*)json_object_get_string(in_device_o), NULL);
+                        pa_log_debug("      device[%d] : %s", j, json_object_get_string(in_device_o));
+                    }
+                }
+
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_STREAM_AVAIL_OUT_DEVICES, &sub_array_o) || !json_object_is_type(sub_array_o, json_type_array)) {
                     pa_log_error("Get stream avail-out-devices failed");
                     goto fail;
                 }
-                if (json_object_object_get_ex(array_item_o, STREAM_MAP_STREAM_AVAIL_FRAMEWORKS, &sub_array_o) && json_object_is_type(sub_array_o, json_type_array)) {
-                    j = 0;
-                    s->idx_avail_frameworks = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
-                    sub_array_length = json_object_array_length(sub_array_o);
-                    pa_log_debug(" - avail-frameworks");
-                    for (j = 0; j < sub_array_length; j++) {
-                        if ((framework_o = json_object_array_get_idx(sub_array_o, j)) && json_object_is_type(framework_o, json_type_string)) {
-                            pa_idxset_put(s->idx_avail_frameworks, (void*)json_object_get_string(framework_o), NULL);
-                            pa_log_debug("      framework[%d] : %s", j, json_object_get_string(framework_o));
-                           }
-                       }
-                } else {
+                s->idx_avail_out_devices = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+                sub_array_length = json_object_array_length(sub_array_o);
+                pa_log_debug(" - avail-out-devices");
+                for (j = 0; j < sub_array_length; j++) {
+                    if ((out_device_o = json_object_array_get_idx(sub_array_o, j)) && json_object_is_type(out_device_o, json_type_string)) {
+                        pa_idxset_put(s->idx_avail_out_devices, (void*)json_object_get_string(out_device_o), NULL);
+                        pa_log_debug("      device[%d] : %s", j, json_object_get_string(out_device_o));
+                    }
+                }
+
+                if (!json_object_object_get_ex(array_item_o, STREAM_MAP_STREAM_AVAIL_FRAMEWORKS, &sub_array_o) || !json_object_is_type(sub_array_o, json_type_array)) {
                     pa_log_error("Get stream avail-frameworks failed");
                     goto fail;
                 }
-                pa_hashmap_put(m->stream_infos, (void*)role, s);
+                s->idx_avail_frameworks = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+                sub_array_length = json_object_array_length(sub_array_o);
+                pa_log_debug(" - avail-frameworks");
+                for (j = 0; j < sub_array_length; j++) {
+                    if ((framework_o = json_object_array_get_idx(sub_array_o, j)) && json_object_is_type(framework_o, json_type_string)) {
+                        pa_idxset_put(s->idx_avail_frameworks, (void*)json_object_get_string(framework_o), NULL);
+                        pa_log_debug("      framework[%d] : %s", j, json_object_get_string(framework_o));
+                    }
+                }
+
+                pa_hashmap_put(stream_infos, (void*)role, s);
                 s = NULL;
             }
         }
-    } else {
-        pa_log_error("Get streams object failed");
-        goto fail;
+    }
+    return 0;
+
+fail:
+    pa_xfree(s);
+    return -1;
+}
+
+static int init_stream_map(pa_stream_manager *m) {
+    json_object *o;
+
+    pa_assert(m);
+
+    o = json_object_from_file(STREAM_MAP_FILE);
+    if (o == NULL) {
+        pa_log_error("Read stream-map file(%s) failed", STREAM_MAP_FILE);
+        return -1;
     }
 
+    /* Latencies */
+    m->latency_infos = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, pa_xfree);
+    if (get_latency_infos(o, m->latency_infos))
+        goto fail;
+
+    /* Volumes */
+    m->volume_infos = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, pa_xfree);
+    if (get_volume_infos(o, m->volume_infos))
+        goto fail;
+
+    /* Streams */
+    m->stream_infos = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t)stream_info_free);
+    if (get_stream_infos(o, m->stream_infos))
+        goto fail;
+
     dump_stream_map(m);
 
     return 0;
+
 fail:
     pa_log_error("failed to initialize stream-map");
-
-    pa_xfree(v);
-    pa_xfree(s);
-    pa_xfree(l);
-
     deinit_stream_map(m);
 
     return -1;