tizenaudio-policy: Add function to unload combine sink with moving streams to another... 21/101421/4
authorSangchul Lee <sc11.lee@samsung.com>
Thu, 1 Dec 2016 06:44:00 +0000 (15:44 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Thu, 1 Dec 2016 08:21:06 +0000 (17:21 +0900)
[Version] 5.0.97
[Profile] Common
[Issue Type] SVACE, refactorying

Change-Id: I9ab63f177e6f08e87bdc057d61fb52801b0ce6ee
Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
packaging/pulseaudio-modules-tizen.spec
src/module-tizenaudio-policy.c

index 5afe532..6dcdb0a 100644 (file)
@@ -1,6 +1,6 @@
 Name:             pulseaudio-modules-tizen
 Summary:          Pulseaudio modules for Tizen
-Version:          5.0.96
+Version:          5.0.97
 Release:          0
 Group:            Multimedia/Audio
 License:          LGPL-2.1+
index 99907d3..bb2b940 100644 (file)
@@ -323,6 +323,45 @@ static void update_loopback_module(struct userdata *u, bool load) {
     }
 }
 
+static void unload_combine_sink_module(struct userdata *u, const char *combine_sink_name, pa_sink *dst_sink)
+{
+    pa_module **combine_sink_module = NULL;
+    pa_sink *combine_sink = NULL;
+    pa_sink_input *s = NULL;
+    uint32_t idx = 0;
+
+    pa_assert(u);
+    pa_assert(combine_sink_name);
+    pa_assert(dst_sink);
+
+    if (pa_streq(combine_sink_name, SINK_NAME_COMBINED)) {
+        combine_sink_module = &u->module_combine_sink;
+    } else if (pa_streq(combine_sink_name, SINK_NAME_COMBINED_EX)) {
+        combine_sink_module = &u->module_combine_sink_for_ex;
+    } else {
+        pa_log_error("unknown combine_sink_name(%s)", combine_sink_name);
+        return;
+    }
+
+    if (*combine_sink_module) {
+        combine_sink = (pa_sink*)pa_namereg_get(u->core, combine_sink_name, PA_NAMEREG_SINK);
+        if (!combine_sink)
+            pa_log_error("could not get combine_sink(%s)", combine_sink_name);
+        else {
+            PA_IDXSET_FOREACH(s, combine_sink->inputs, idx) {
+                pa_sink_input_move_to(s, dst_sink, false);
+                pa_log_info("[UNLOAD COMBINE SINK MODULE] *** sink-input(%p,%u) of (%s) moves to another sink(%p,%s)",
+                             s, ((pa_sink_input*)s)->index, combine_sink_name, dst_sink, dst_sink->name);
+            }
+            pa_sink_suspend(combine_sink, true, PA_SUSPEND_USER);
+        }
+        pa_log_info("unload combine sink module[%s]", combine_sink_name);
+        pa_module_unload(u->core, *combine_sink_module, true);
+        *combine_sink_module = NULL;
+    } else
+        pa_log_warn("module combine sink(%s) has been already unloaded", combine_sink_name);
+}
+
 static bool skip_device(const char *stream_role, const char *device_type)
 {
     int sound_on = 1;
@@ -427,7 +466,7 @@ static pa_hook_result_t select_proper_sink_or_source_hook_cb(pa_core *c, pa_stre
                         if (data->stream_type == STREAM_SINK_INPUT) {
                             if (data->route_type == STREAM_ROUTE_TYPE_AUTO_ALL && u->module_combine_sink) {
                                 *(data->proper_sink) = (pa_sink*)pa_namereg_get(u->core, SINK_NAME_COMBINED, PA_NAMEREG_SINK);
-                                pa_log_debug("  -- found the combine-sink, set it to the sink");
+                                pa_log_info("  -- found the combine-sink, set it to the sink");
                             } else
                                 *(data->proper_sink) = pa_tz_device_get_sink(device, data->device_role);
                         } else
@@ -519,13 +558,13 @@ static pa_hook_result_t select_proper_sink_or_source_hook_cb(pa_core *c, pa_stre
                         if (data->stream_type == STREAM_SINK_INPUT) {
                             if (!combine_sink_arg1) {
                                 if ((sink = combine_sink_arg1 = pa_tz_device_get_sink(device, DEVICE_ROLE_NORMAL)))
-                                    pa_log_debug("  -- combine_sink_arg1[%s], combine_sink_arg2[%p]", sink->name, combine_sink_arg2);
+                                    pa_log_info("  -- combine_sink_arg1[%s], combine_sink_arg2[%p]", sink->name, combine_sink_arg2);
                                 else
                                     pa_log_warn("  -- could not get combine_sink_arg1");
                             } else if (!combine_sink_arg2) {
                                 sink = combine_sink_arg2 = pa_tz_device_get_sink(device, DEVICE_ROLE_NORMAL);
                                 if (sink && !pa_streq(sink->name, combine_sink_arg1->name)) {
-                                    pa_log_debug("  -- combine_sink_arg2[%s]", sink->name);
+                                    pa_log_info("  -- combine_sink_arg2[%s]", sink->name);
                                     /* load combine sink */
                                     if (!u->module_combine_sink_for_ex) {
                                         args = pa_sprintf_malloc("sink_name=%s slaves=\"%s,%s\"", SINK_NAME_COMBINED_EX, combine_sink_arg1->name, combine_sink_arg2->name);
@@ -537,7 +576,7 @@ static pa_hook_result_t select_proper_sink_or_source_hook_cb(pa_core *c, pa_stre
                                     PA_IDXSET_FOREACH(s, combine_sink_arg1->inputs, s_idx) {
                                         if (s == data->stream) {
                                             pa_sink_input_move_to(s, sink, false);
-                                            pa_log_debug("  -- *** sink-input(%p,%u) moves to sink(%p,%s)", s, ((pa_sink_input*)s)->index, sink, sink->name);
+                                            pa_log_info("  -- *** sink-input(%p,%u) moves to sink(%p,%s)", s, ((pa_sink_input*)s)->index, sink, sink->name);
                                             break;
                                         }
                                     }
@@ -679,22 +718,9 @@ static pa_hook_result_t route_change_hook_cb(pa_core *c, pa_stream_manager_hook_
         route_info.device_infos[0].direction = CONVERT_TO_HAL_DIRECTION(data->stream_type);
 
         /* unload combine sink */
-        if (data->stream_type == STREAM_SINK_INPUT && u->module_combine_sink) {
-            pa_log_info("[ROUTE][RESET] unload module[%s]", SINK_NAME_COMBINED);
-            combine_sink = (pa_sink*)pa_namereg_get(u->core, SINK_NAME_COMBINED, PA_NAMEREG_SINK);
+        if (data->stream_type == STREAM_SINK_INPUT) {
             null_sink = (pa_sink*)pa_namereg_get(u->core, SINK_NAME_NULL, PA_NAMEREG_SINK);
-            if (!combine_sink || !null_sink)
-                pa_log_error("[ROUTE][RESET] could not get combine_sink(%p) or null_sink(%p)", combine_sink, null_sink);
-            else {
-                PA_IDXSET_FOREACH(s, combine_sink->inputs, s_idx) {
-                    pa_sink_input_move_to(s, null_sink, false);
-                    pa_log_debug("[ROUTE][RESET] *** sink-input(%p,%u) moves to sink(%p,%s)",
-                                 s, ((pa_sink_input*)s)->index, null_sink, null_sink->name);
-                }
-                pa_sink_suspend(combine_sink, true, PA_SUSPEND_USER);
-            }
-            pa_module_unload(u->core, u->module_combine_sink, true);
-            u->module_combine_sink = NULL;
+            unload_combine_sink_module(u, SINK_NAME_COMBINED, null_sink);
         }
 
     } else if ((data->route_type <= STREAM_ROUTE_TYPE_AUTO_ALL) && data->idx_avail_devices) {
@@ -767,24 +793,11 @@ static pa_hook_result_t route_change_hook_cb(pa_core *c, pa_stream_manager_hook_
                 /* move sink-inputs/source-outputs if needed */
                 if (data->stream_type == STREAM_SINK_INPUT)
                     /* unload combine sink */
-                    if (data->stream_type == STREAM_SINK_INPUT && u->module_combine_sink) {
-                        if ((combine_sink = (pa_sink*)pa_namereg_get(u->core, SINK_NAME_COMBINED, PA_NAMEREG_SINK))) {
-                            if ((sink = pa_tz_device_get_sink(device, data->device_role))) {
-                                PA_IDXSET_FOREACH(s, combine_sink->inputs, s_idx) {
-                                    pa_sink_input_move_to(s, sink, false);
-                                    pa_log_debug("[ROUTE][AUTO] *** sink-input(%p,%u) moves to sink(%p,%s)",
-                                                 s, ((pa_sink_input*)s)->index, sink, sink->name);
-                                }
-                            } else
-                                pa_log_error("[ROUTE][AUTO] could not get sink");
-
-                            pa_sink_suspend(combine_sink, true, PA_SUSPEND_USER);
-                        } else
-                            pa_log_error("[ROUTE][AUTO] could not get combine_sink");
-
-                        pa_log_debug("[ROUTE][AUTO] unload module[%s]", SINK_NAME_COMBINED);
-                        pa_module_unload(u->core, u->module_combine_sink, true);
-                        u->module_combine_sink = NULL;
+                    if (data->stream_type == STREAM_SINK_INPUT) {
+                        if ((sink = pa_tz_device_get_sink(device, data->device_role)))
+                            unload_combine_sink_module(u, SINK_NAME_COMBINED, sink);
+                        else
+                            pa_log_error("[ROUTE][AUTO] could not get sink");
                     }
                     break;
 
@@ -797,13 +810,13 @@ static pa_hook_result_t route_change_hook_cb(pa_core *c, pa_stream_manager_hook_
                     } else if (data->stream_type == STREAM_SINK_INPUT && !combine_sink_arg1) {
                         sink = combine_sink_arg1 = pa_tz_device_get_sink(device, data->device_role);
                         if (sink)
-                            pa_log_debug("[ROUTE][AUTO_ALL] combine_sink_arg1[%s], combine_sink_arg2[%p]", sink->name, combine_sink_arg2);
+                            pa_log_info("[ROUTE][AUTO_ALL] combine_sink_arg1[%s], combine_sink_arg2[%p]", sink->name, combine_sink_arg2);
                         else
                             pa_log_error("[ROUTE][AUTO_ALL] could not get sink from pa_device_manager_get_sink");
                     } else if (data->stream_type == STREAM_SINK_INPUT && !combine_sink_arg2) {
                         sink = combine_sink_arg2 = pa_tz_device_get_sink(device, data->device_role);
                         if (sink && !pa_streq(sink->name, combine_sink_arg1->name)) {
-                            pa_log_debug("[ROUTE][AUTO_ALL] combine_sink_arg2[%s]", sink->name);
+                            pa_log_info("[ROUTE][AUTO_ALL] combine_sink_arg2[%s]", sink->name);
                             /* load combine sink */
                             if (!u->module_combine_sink) {
                                 args = pa_sprintf_malloc("sink_name=%s slaves=\"%s,%s\"", SINK_NAME_COMBINED, combine_sink_arg1->name, combine_sink_arg2->name);
@@ -815,7 +828,7 @@ static pa_hook_result_t route_change_hook_cb(pa_core *c, pa_stream_manager_hook_
                                 PA_IDXSET_FOREACH(s, combine_sink_arg1->inputs, s_idx) {
                                     if (s == data->stream) {
                                         pa_sink_input_move_to(s, sink, false);
-                                        pa_log_debug("[ROUTE][AUTO_ALL] *** sink-nput(%p,%u) moves to sink(%p,%s)",
+                                        pa_log_info("[ROUTE][AUTO_ALL] *** sink-nput(%p,%u) moves to sink(%p,%s)",
                                                      s, ((pa_sink_input*)s)->index, sink, sink->name);
                                     }
                                 }
@@ -838,11 +851,11 @@ static pa_hook_result_t route_change_hook_cb(pa_core *c, pa_stream_manager_hook_
                                     (route_type == STREAM_ROUTE_TYPE_AUTO_ALL)) {
                                     if ((data->stream_type == STREAM_SINK_INPUT) && (sink && (sink != ((pa_sink_input*)s)->sink))) {
                                         pa_sink_input_move_to(s, sink, false);
-                                        pa_log_debug("[ROUTE][AUTO_ALL] *** sink-input(%p,%u) moves to sink(%p,%s)",
+                                        pa_log_info("[ROUTE][AUTO_ALL] *** sink-input(%p,%u) moves to sink(%p,%s)",
                                                      s, ((pa_sink_input*)s)->index, sink, sink->name);
                                     } else if ((data->stream_type == STREAM_SOURCE_OUTPUT) && (source && (source != ((pa_source_output*)s)->source))) {
                                         pa_source_output_move_to(s, source, false);
-                                        pa_log_debug("[ROUTE][AUTO_ALL] *** source-output(%p,%u) moves to source(%p,%s)",
+                                        pa_log_info("[ROUTE][AUTO_ALL] *** source-output(%p,%u) moves to source(%p,%s)",
                                                      s, ((pa_source_output*)s)->index, source, source->name);
                                     }
                                 }
@@ -907,20 +920,11 @@ static pa_hook_result_t route_change_hook_cb(pa_core *c, pa_stream_manager_hook_
                     pa_proplist_sets(GET_STREAM_PROPLIST(data->stream, data->stream_type), PA_PROP_MEDIA_ROUTE_AUTO_ACTIVE_DEV, dm_device_type);
 
                 /* unload combine sink */
-                if (data->stream_type == STREAM_SINK_INPUT && u->module_combine_sink) {
-                    if ((combine_sink = (pa_sink*)pa_namereg_get(u->core, SINK_NAME_COMBINED, PA_NAMEREG_SINK))) {
-                        PA_IDXSET_FOREACH(s, combine_sink->inputs, s_idx) {
-                            pa_sink_input_move_to(s, sink, false);
-                            pa_log_debug("[ROUTE][AUTO_LAST_CONN] *** sink-input(%p,%u) moves to sink(%p,%s)",
-                                         s, ((pa_sink_input*)s)->index, sink, sink->name);
-                        }
-                        pa_sink_suspend(combine_sink, true, PA_SUSPEND_USER);
-                    } else
-                        pa_log_error("[ROUTE][AUTO_LAST_CONN] could not get combine_sink");
-
-                    pa_log_info("[ROUTE][AUTO_LAST_CONN] unload module[%s]", SINK_NAME_COMBINED);
-                    pa_module_unload(u->core, u->module_combine_sink, true);
-                    u->module_combine_sink = NULL;
+                if (data->stream_type == STREAM_SINK_INPUT) {
+                    if ((sink = pa_tz_device_get_sink(latest_device, data->device_role)))
+                        unload_combine_sink_module(u, SINK_NAME_COMBINED, sink);
+                    else
+                        pa_log_error("[ROUTE][AUTO_LAST_CONN] could not get sink");
                 }
             }
         }
@@ -1157,11 +1161,9 @@ static void dump_connected_devices()
 /* Reorganize routing when a device has been connected or disconnected */
 static pa_hook_result_t device_connection_changed_hook_cb(pa_core *c, pa_tz_device_hook_data_for_conn_changed *conn, struct userdata *u) {
     uint32_t idx = 0;
-    pa_sink_input *s = NULL;
     dm_device_direction_t device_direction = DM_DEVICE_DIRECTION_OUT;
     pa_sink *sink = NULL;
     pa_sink *null_sink = NULL;
-    pa_sink *combine_sink = NULL;
     bool use_internal_codec = false;
     pa_idxset* conn_devices = NULL;
     pa_tz_device *device = NULL;
@@ -1178,7 +1180,7 @@ static pa_hook_result_t device_connection_changed_hook_cb(pa_core *c, pa_tz_devi
     update_connected_devices(pa_tz_device_get_type(conn->device), device_direction, conn->is_connected);
     dump_connected_devices();
 
-    null_sink = (pa_sink*)pa_namereg_get(u->core, SINK_NAME_NULL, PA_NAMEREG_SINK);
+    sink = null_sink = (pa_sink*)pa_namereg_get(u->core, SINK_NAME_NULL, PA_NAMEREG_SINK);
     if (!null_sink) {
         pa_log_error("[CONN] could not get null_sink(%p)", null_sink);
         return PA_HOOK_OK;
@@ -1188,49 +1190,22 @@ static pa_hook_result_t device_connection_changed_hook_cb(pa_core *c, pa_tz_devi
     /* update for unloading modules when external device is disconnected */
     if (!use_internal_codec && !conn->is_connected) {
         if (device_direction & DM_DEVICE_DIRECTION_OUT) {
-            if (u->module_combine_sink) {
-                /* unload combine sink */
-                if ((combine_sink = (pa_sink*)pa_namereg_get(u->core, SINK_NAME_COMBINED, PA_NAMEREG_SINK))) {
-                    conn_devices = pa_device_manager_get_device_list(u->device_manager);
-                    PA_IDXSET_FOREACH(device, conn_devices, idx) {
-                        device_direction = pa_tz_device_get_direction(device);
-                        if (device_direction == DM_DEVICE_DIRECTION_OUT) {
-                            if ((use_internal_codec = pa_tz_device_is_use_internal_codec(device))) {
-                                sink = pa_tz_device_get_sink(device, DEVICE_ROLE_NORMAL);
-                                break;
-                            }
-                        }
-                    }
-                    if (combine_sink->inputs) {
-                        if (!sink)
-                            sink = null_sink;
-                        PA_IDXSET_FOREACH(s, combine_sink->inputs, idx) {
-                            /* re-routing this stream to the remaining device using internal codec */
-                            pa_sink_input_move_to(s, sink, false);
-                            pa_log_debug("[CONN] *** sink-input(%p,%u) moves to sink(%p,%s)", s, ((pa_sink_input*)s)->index, sink, sink->name);
-                        }
-                    }
-                    pa_sink_suspend(combine_sink, true, PA_SUSPEND_USER);
-                    pa_module_unload(u->core, u->module_combine_sink, true);
-                    u->module_combine_sink = NULL;
-                } else
-                    pa_log_error("[CONN] could not get combine_sink");
-            }
-            if (u->module_combine_sink_for_ex) {
-                /* unload combine sink for external devices */
-                if ((combine_sink = (pa_sink*)pa_namereg_get(u->core, SINK_NAME_COMBINED_EX, PA_NAMEREG_SINK))) {
-                    if (combine_sink->inputs) {
-                        PA_IDXSET_FOREACH(s, combine_sink->inputs, idx) {
-                            pa_sink_input_move_to(s, null_sink, false);
-                            pa_log_debug("[CONN] *** sink-input(%p,%u) moves to sink(%p,%s)", s, ((pa_sink_input*)s)->index, null_sink, null_sink->name);
-                        }
+           /* unload combine sink */
+           conn_devices = pa_device_manager_get_device_list(u->device_manager);
+            PA_IDXSET_FOREACH(device, conn_devices, idx) {
+                device_direction = pa_tz_device_get_direction(device);
+                if (device_direction == DM_DEVICE_DIRECTION_OUT) {
+                    if ((use_internal_codec = pa_tz_device_is_use_internal_codec(device))) {
+                        sink = pa_tz_device_get_sink(device, DEVICE_ROLE_NORMAL);
+                        break;
                     }
-                    pa_sink_suspend(combine_sink, true, PA_SUSPEND_USER);
-                    pa_module_unload(u->core, u->module_combine_sink_for_ex, true);
-                    u->module_combine_sink_for_ex = NULL;
-                } else
-                    pa_log_error("[CONN] could not get combine_sink_ex");
+                }
             }
+            unload_combine_sink_module(u, SINK_NAME_COMBINED, sink);
+
+            /* unload combine sink for external devices */
+            unload_combine_sink_module(u, SINK_NAME_COMBINED_EX, null_sink);
+
             /* unload loopback module */
             if (u->module_loopback)
                 if (u->loopback_args.sink == pa_tz_device_get_sink(conn->device, DEVICE_ROLE_NORMAL))