stream-manager-dbus: Revise handle_set_stream_preferred_device() to reduce condition... 69/213969/1 accepted/tizen/unified/20190918.010327 submit/tizen/20190917.095054
authorSangchul Lee <sc11.lee@samsung.com>
Tue, 17 Sep 2019 05:22:58 +0000 (14:22 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Tue, 17 Sep 2019 05:25:54 +0000 (14:25 +0900)
It'll reduce cyclomatic complexity of SAM.

[Version] 11.1.75
[Issue Type] Refactoring

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

index 549bdce..9296a04 100644 (file)
@@ -1,6 +1,6 @@
 Name:             pulseaudio-modules-tizen
 Summary:          Pulseaudio modules for Tizen
-Version:          11.1.74
+Version:          11.1.75
 Release:          0
 Group:            Multimedia/Audio
 License:          LGPL-2.1+
index a242f1e..1e2a44f 100644 (file)
@@ -646,6 +646,30 @@ static void handle_set_stream_route_option(DBusConnection *conn, DBusMessage *ms
     dbus_message_unref(reply);
 }
 
+static int get_device_for_preference(pa_stream_manager *m, stream_parent *sp, stream_direction_t direction, uint32_t device_id, pa_tz_device **device) {
+    pa_assert(m);
+    pa_assert(sp);
+    pa_assert(device);
+
+    if (device_id == 0) {
+        /* get a device of default role from previous device type */
+        if (!(*device = pa_device_manager_get_device(m->dm, sp->preferred_device.types[direction], NULL))) {
+            pa_log_error("could not get device[%s]", sp->preferred_device.types[direction]);
+            return -1;
+        }
+        pa_log_debug("unset preferred device, rollback to type(%s), id(%u)", pa_tz_device_get_type(*device), pa_tz_device_get_id(*device));
+    } else {
+        /* get the device of device id */
+        if (!(*device = pa_device_manager_get_device_by_id(m->dm, device_id))) {
+            pa_log_error("could not get device by id[%u]", device_id);
+            return -1;
+        }
+        pa_log_debug("requested preferred device type(%s), id(%u)", pa_tz_device_get_type(*device), pa_tz_device_get_id(*device));
+    }
+
+    return 0;
+}
+
 static void handle_set_stream_preferred_device(DBusConnection *conn, DBusMessage *msg, void *userdata) {
     const char *device_direction = NULL;
     uint32_t sp_id = 0;
@@ -666,7 +690,7 @@ static void handle_set_stream_preferred_device(DBusConnection *conn, DBusMessage
     uint32_t stream_index;
     pa_hashmap *devices;
     void *new_device = NULL;
-   ret_msg_t ret = RET_MSG_OK;
+    ret_msg_t ret = RET_MSG_OK;
     DBusMessage *reply = NULL;
 
     pa_stream_manager *m = (pa_stream_manager*)userdata;
@@ -699,40 +723,27 @@ static void handle_set_stream_preferred_device(DBusConnection *conn, DBusMessage
         goto finish;
     }
 
-    if (device_id == 0) {
-        /* in case of unset */
-        if (!sp->preferred_device.types[direction]) {
-            pa_log_debug("it is already unset");
-            goto finish;
-        }
-        /* get a device of default role from previous device type */
-        if (!(device = pa_device_manager_get_device(m->dm, sp->preferred_device.types[direction], NULL))) {
-            pa_log_error("could not get device[%s]", sp->preferred_device.types[direction]);
-            ret = RET_MSG_ERROR_DEVICE_NOT_FOUND;
-            goto finish;
-        }
-        pa_log_debug("unset preferred device, rollback to type(%s), id(%u)", pa_tz_device_get_type(device), pa_tz_device_get_id(device));
-    } else {
-        /* get device role from device id */
-        if (!(device = pa_device_manager_get_device_by_id(m->dm, device_id))) {
-            pa_log_error("could not get device by id[%u]", device_id);
-            ret = RET_MSG_ERROR_DEVICE_NOT_FOUND;
-            goto finish;
-        }
-        pa_log_debug("requested preferred device type(%s), id(%u)", pa_tz_device_get_type(device), pa_tz_device_get_id(device));
+    /* allow only auto routing type */
+    if (sp->route_type != STREAM_ROUTE_TYPE_AUTO &&
+        sp->route_type != STREAM_ROUTE_TYPE_AUTO_LAST_CONNECTED) {
+        pa_log_error("not allowed this route type[%d] of this parent_id[%u]", sp->route_type, sp_id);
+        ret = RET_MSG_ERROR_POLICY;
+        goto finish;
     }
 
-    /* only allow built-in device types */
-    if (!device_type_is_builtin(device->type)) {
-        pa_log_error("This device(id:%u, type:%s) is not built-in type", device_id, device->type);
-        ret = RET_MSG_ERROR_POLICY;
+    if (device_id == 0 && !sp->preferred_device.types[direction]) {
+        pa_log_debug("it is already unset");
         goto finish;
     }
 
-    /* allow only auto routing type */
-    if (sp->route_type != STREAM_ROUTE_TYPE_AUTO &&
-        sp->route_type != STREAM_ROUTE_TYPE_AUTO_LAST_CONNECTED) {
-        pa_log_error("not allowed this route type[%d] of this parent_id[%u]", sp->route_type, sp_id);
+    if (get_device_for_preference(m, sp, direction, device_id, &device) < 0) {
+        ret = RET_MSG_ERROR_DEVICE_NOT_FOUND;
+        goto finish;
+    }
+
+    /* only allow built-in device types */
+    if (!device_type_is_builtin(device->type)) {
+        pa_log_error("This device(id:%u, type:%s) is not built-in type", device_id, device->type);
         ret = RET_MSG_ERROR_POLICY;
         goto finish;
     }
@@ -740,24 +751,14 @@ static void handle_set_stream_preferred_device(DBusConnection *conn, DBusMessage
     /* get default role of the device */
     device_role = pa_tz_device_get_role(device, NULL);
 
-    if (device_id == 0) {
-        sp->preferred_device.types[direction] = NULL;
-        sp->preferred_device.roles[direction] = NULL;
-    } else {
-        sp->preferred_device.types[direction] = device->type;
-        sp->preferred_device.roles[direction] = device_role;
-    }
+    sp->preferred_device.types[direction] = (device_id == 0) ? NULL : device->type;
+    sp->preferred_device.roles[direction] = (device_id == 0) ? NULL : device_role;
 
     pa_log_info("preferred device role is set to [%s] of device type[%s], direction[%s]",
             sp->preferred_device.roles[direction], device->type, direction == STREAM_DIRECTION_OUT ? "out" :  "in");
 
-    if (direction == STREAM_DIRECTION_OUT) {
-        streams = sp->idx_sink_inputs;
-        devices = device->playback_devices;
-    } else {
-        streams = sp->idx_source_outputs;
-        devices = device->capture_devices;
-    }
+    streams = (direction == STREAM_DIRECTION_OUT) ? sp->idx_sink_inputs : sp->idx_source_outputs;
+    devices = (direction == STREAM_DIRECTION_OUT) ? device->playback_devices : device->capture_devices;
 
     count = pa_idxset_size(streams);
     PA_IDXSET_FOREACH(stream, streams, idx) {
@@ -797,12 +798,10 @@ static void handle_set_stream_preferred_device(DBusConnection *conn, DBusMessage
                         stream_index, prev_device_type, prev_device_role, pa_proplist_gets(device_props, PA_PROP_DEVICE_ROLE));
             }
         }
-        if (new_device) {
-            if (direction == STREAM_DIRECTION_OUT)
-                pa_sink_input_move_to(stream, PA_SINK(new_device), false);
-            else
-                pa_source_output_move_to(stream, PA_SOURCE(new_device), false);
-        }
+        if (new_device)
+            (direction == STREAM_DIRECTION_OUT) ? pa_sink_input_move_to(stream, PA_SINK(new_device), false) :
+                                                pa_source_output_move_to(stream, PA_SOURCE(new_device), false);
+
         if (count == 1)
         /* Use PROCESS_COMMAND_CHANGE_ROUTE_BY_STREAM_FOCUS_CHANGED here.
          * It's not about the focus change, but this command exactly does what is needed here