device-manager set/get state with direction 08/48208/8
authorMok Jeongho <jho.mok@samsung.com>
Wed, 16 Sep 2015 06:23:43 +0000 (15:23 +0900)
committerJeongho Mok <jho.mok@samsung.com>
Fri, 18 Sep 2015 05:41:50 +0000 (22:41 -0700)
Change-Id: I259d1ed6d3b36c90ea1e2d1900aa8b40c87e2b21

src/modules/tizen/device-manager.c
src/modules/tizen/device-manager.h
src/modules/tizen/module-policy.c

index bc9409a..361b2ea 100644 (file)
@@ -208,6 +208,7 @@ static const char* const valid_alsa_device_modargs[] = {
 #define MAKE_SINK(s) ((pa_sink*) (s))
 #define MAKE_SOURCE(s) ((pa_source*) (s))
 
+#define COMPOUND_STATE(d) (((dm_device_profile*)d)->playback_state | ((dm_device_profile*)d)->capture_state)
 
 #define BT_CVSD_CODEC_ID 1 // narrow-band
 #define BT_MSBC_CODEC_ID 2 // wide-band
@@ -320,7 +321,8 @@ struct dm_device {
 typedef struct dm_device_profile {
     char *profile;
     dm_device_direction_t direction;
-    dm_device_state_t state;
+    dm_device_state_t playback_state;
+    dm_device_state_t capture_state;
 
     /* Can get proper sink/source in hashmaps with key(=device_role) */
     pa_hashmap *playback_devices;
@@ -963,7 +965,7 @@ static void dump_device_profile_info(dm_device_profile *profile_item) {
 
     pa_log_debug("    profile   : %s", profile_item->profile);
     pa_log_debug("    direction : %s", device_direction_to_string(profile_item->direction));
-    pa_log_debug("    activated : %s", profile_item->state == DM_DEVICE_STATE_ACTIVATED ? "activated" : "not activated");
+    pa_log_debug("    activated : %s", COMPOUND_STATE(profile_item) == DM_DEVICE_STATE_ACTIVATED ? "activated" : "not activated");
     dump_playback_device_list(profile_item->playback_devices);
     dump_capture_device_list(profile_item->capture_devices);
 }
@@ -1508,7 +1510,8 @@ static dm_device_profile* create_device_profile(const char *device_profile, dm_d
     }
     profile_item->profile = device_profile ? strdup(device_profile) : NULL;
     profile_item->direction = direction;
-    profile_item->state = DM_DEVICE_STATE_DEACTIVATED;
+    profile_item->playback_state = DM_DEVICE_STATE_DEACTIVATED;
+    profile_item->capture_state = DM_DEVICE_STATE_DEACTIVATED;
     profile_item->playback_devices = playback;
     profile_item->capture_devices = capture;
 
@@ -1655,11 +1658,20 @@ static dm_device_profile* _device_profile_remove_source(dm_device_profile *profi
     return profile_item;
 }
 
-void _device_profile_set_state(dm_device_profile *profile_item, dm_device_state_t state) {
+void _device_profile_set_state(dm_device_profile *profile_item,  dm_device_direction_t direction, dm_device_state_t state) {
+    dm_device_state_t prev_state, new_state;
     pa_assert(profile_item);
 
-    if (profile_item->state != state) {
-        profile_item->state = state;
+    prev_state = COMPOUND_STATE(profile_item);
+    pa_log_debug("previous playback_state : %d, capture_state : %d => state %d", profile_item->playback_state, profile_item->capture_state, prev_state);
+    if (direction & DM_DEVICE_DIRECTION_IN)
+        profile_item->capture_state = state;
+    if (direction & DM_DEVICE_DIRECTION_OUT)
+        profile_item->playback_state = state;
+    new_state = COMPOUND_STATE(profile_item);
+    pa_log_debug("new playback_state : %d, capture_state : %d => state %d", profile_item->playback_state, profile_item->capture_state, new_state);
+
+    if (prev_state != new_state) {
         notify_device_info_changed(profile_item->device_item, DM_DEVICE_CHANGED_INFO_STATE, profile_item->device_item->dm);
     }
 }
@@ -3036,7 +3048,8 @@ static int handle_device_disconnected(pa_device_manager *dm, const char *device_
     PA_IDXSET_FOREACH(device_item, dm->device_list, device_idx) {
         if (pa_streq(device_item->type, device_type)) {
             if((profile_item = _device_item_get_profile(device_item, device_profile))) {
-                notify_device_connection_changed(profile_item, FALSE, dm);
+                if (_device_item_get_size(device_item) == 1)
+                    notify_device_connection_changed(device_item, FALSE, dm);
                 destroy_device_profile(profile_item, dm);
             } else {
                 pa_log_debug("no matching profile");
@@ -3335,6 +3348,7 @@ static void send_device_connected_signal(dm_device *device_item, pa_bool_t conne
     DBusMessageIter msg_iter, device_iter;
     dm_device_profile *profile_item;
     dbus_bool_t _connected = connected;
+    dm_device_state_t compound_state;
     dbus_int32_t device_id;
 
     pa_assert(device_item);
@@ -3353,10 +3367,11 @@ static void send_device_connected_signal(dm_device *device_item, pa_bool_t conne
     }
 
     device_id = (dbus_int32_t) device_item->id;
+    compound_state = COMPOUND_STATE(profile_item);
     dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_INT32, &device_id);
     dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_STRING, &device_item->type);
     dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_INT32, &profile_item->direction);
-    dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_INT32, &profile_item->state);
+    dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_INT32, &compound_state);
     dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_STRING, &device_item->name);
     pa_assert_se(dbus_message_iter_close_container(&msg_iter, &device_iter));
     dbus_message_iter_append_basic(&msg_iter, DBUS_TYPE_BOOLEAN, &_connected);
@@ -3370,6 +3385,7 @@ static void send_device_info_changed_signal(dm_device *device_item, int changed_
     DBusMessage *signal_msg;
     DBusMessageIter msg_iter, device_iter;
     dm_device_profile *profile_item;
+    dm_device_state_t compound_state;
     dbus_int32_t device_id;
 
     pa_assert(device_item);
@@ -3387,10 +3403,11 @@ static void send_device_info_changed_signal(dm_device *device_item, int changed_
         return;
     }
     device_id = (dbus_int32_t) device_item->id;
+    compound_state = COMPOUND_STATE(profile_item);
     dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_INT32, &device_id);
     dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_STRING, &device_item->type);
     dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_INT32, &profile_item->direction);
-    dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_INT32, &profile_item->state);
+    dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_INT32, &compound_state);
     dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_STRING, &device_item->name);
     pa_assert_se(dbus_message_iter_close_container(&msg_iter, &device_iter));
     dbus_message_iter_append_basic(&msg_iter, DBUS_TYPE_INT32, &changed_type);
@@ -3444,9 +3461,9 @@ static pa_bool_t device_item_match_for_mask(dm_device *device_item, int device_f
     }
     if (need_to_check_for_state) {
         match = FALSE;
-        if ((profile_item->state == DM_DEVICE_STATE_DEACTIVATED) && (device_flags & DEVICE_STATE_DEACTIVATED_FLAG))
+        if ((COMPOUND_STATE(profile_item)== DM_DEVICE_STATE_DEACTIVATED) && (device_flags & DEVICE_STATE_DEACTIVATED_FLAG))
             match = TRUE;
-        else if ((profile_item->state == DM_DEVICE_STATE_ACTIVATED) && (device_flags & DEVICE_STATE_ACTIVATED_FLAG))
+        else if ((COMPOUND_STATE(profile_item) == DM_DEVICE_STATE_ACTIVATED) && (device_flags & DEVICE_STATE_ACTIVATED_FLAG))
             match = TRUE;
         if (match) {
             if (!need_to_check_for_type)
@@ -3610,6 +3627,7 @@ static void handle_get_connected_device_list(DBusConnection *conn, DBusMessage *
     DBusMessageIter msg_iter, array_iter, device_iter;
     dm_device *device_item;
     dm_device_profile *profile_item;
+    dm_device_state_t compound_state;
     uint32_t device_idx;
     dbus_int32_t device_id;
     int mask_flags;
@@ -3636,13 +3654,14 @@ static void handle_get_connected_device_list(DBusConnection *conn, DBusMessage *
             pa_log_error("no active profile");
             continue;
         }
+        compound_state = COMPOUND_STATE(profile_item);
         if (device_item_match_for_mask(device_item,  mask_flags, dm)) {
             device_id = (dbus_int32_t)device_item->id;
             pa_assert_se(dbus_message_iter_open_container(&array_iter, DBUS_TYPE_STRUCT, NULL, &device_iter));
             dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_INT32, &device_id);
             dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_STRING, &profile_item->device_item->type);
             dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_INT32, &profile_item->direction);
-            dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_INT32, &profile_item->state);
+            dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_INT32, &compound_state);
             dbus_message_iter_append_basic(&device_iter, DBUS_TYPE_STRING, &device_item->name);
             pa_assert_se(dbus_message_iter_close_container(&array_iter, &device_iter));
         }
@@ -3927,23 +3946,30 @@ pa_source* pa_device_manager_get_source(dm_device *device_item, const char *role
     return pa_hashmap_get(profile_item->capture_devices, role);
 }
 
-void pa_device_manager_set_device_state(dm_device *device_item, dm_device_state_t state) {
+void pa_device_manager_set_device_state(dm_device *device_item, dm_device_direction_t direction, dm_device_state_t state) {
     dm_device_profile *profile_item;
 
     pa_assert(device_item);
     pa_assert(profile_item = _device_item_get_active_profile(device_item));
 
-    pa_log_debug("pa_device_manager_set_device_state : %s.%s -> %d", device_item->type, profile_item->profile, state);
-    _device_profile_set_state(profile_item,  state);
+    pa_log_debug("pa_device_manager_set_device_state : %s.%s  direction %s -> %d", device_item->type, profile_item->profile, device_direction_to_string(direction), state);
+    _device_profile_set_state(profile_item, direction, state);
 }
 
-dm_device_state_t pa_device_manager_get_device_state(dm_device *device_item) {
+dm_device_state_t pa_device_manager_get_device_state(dm_device *device_item, dm_device_direction_t direction) {
     dm_device_profile *profile_item;
 
     pa_assert(device_item);
     pa_assert(profile_item = _device_item_get_active_profile(device_item));
 
-    return profile_item->state;
+    if (direction == DM_DEVICE_DIRECTION_BOTH)
+        return COMPOUND_STATE(profile_item);
+    else if (direction == DM_DEVICE_DIRECTION_OUT)
+        return profile_item->playback_state;
+    else if (direction == DM_DEVICE_DIRECTION_IN)
+        return profile_item->capture_state;
+    else
+        return DM_DEVICE_STATE_DEACTIVATED;
 }
 
 uint32_t pa_device_manager_get_device_id(dm_device *device_item) {
index 5d363ed..a62d701 100644 (file)
@@ -34,7 +34,7 @@ typedef enum dm_device_changed_into_type {
 } dm_device_changed_info_t;
 
 typedef enum dm_device_state_type {
-    DM_DEVICE_STATE_DEACTIVATED,
+    DM_DEVICE_STATE_DEACTIVATED = 0,
     DM_DEVICE_STATE_ACTIVATED
 } dm_device_state_t;
 
@@ -60,8 +60,8 @@ dm_device* pa_device_manager_get_device_by_id(pa_device_manager *dm, uint32_t id
 
 pa_sink* pa_device_manager_get_sink(dm_device *device, const char *role);
 pa_source* pa_device_manager_get_source(dm_device *device, const char *role);
-void pa_device_manager_set_device_state(dm_device *device, dm_device_state_t state);
-dm_device_state_t pa_device_manager_get_device_state(dm_device *device);
+void pa_device_manager_set_device_state(dm_device *device, dm_device_direction_t direction, dm_device_state_t state);
+dm_device_state_t pa_device_manager_get_device_state(dm_device *device_item, dm_device_direction_t direction);
 uint32_t pa_device_manager_get_device_id(dm_device *device);
 const char* pa_device_manager_get_device_type(dm_device *device);
 const char* pa_device_manager_get_device_subtype(dm_device *device);
index 2a371bc..af44db5 100644 (file)
@@ -214,8 +214,6 @@ enum signal_index {
 #endif
 
 /* Sink & Source names */
-#define SINK_HIGH_LATENCY        "alsa_output.4.analog-stereo"
-#define SINK_HIGH_LATENCY_UHQA   "alsa_output.4.analog-stereo-uhqa"
 #define SINK_COMBINED            "sink_combined"
 #define SINK_NULL                "sink_null"
 #define SOURCE_NULL              "source_null"
@@ -224,8 +222,9 @@ enum signal_index {
 #define POLICY_HIGH_LATENCY "high-latency"
 #define POLICY_HIGH_LATENCY_UHQA "high-latency-uhqa"
 
-/* Sink Identify Macros */
-#define sink_is_highlatency(sink) !strncmp(sink->name, SINK_HIGH_LATENCY, strlen(SINK_HIGH_LATENCY))
+/* Macros */
+#define CONVERT_TO_DEVICE_DIRECTION(stream_type) \
+    ((stream_type==STREAM_SINK_INPUT)?DM_DEVICE_DIRECTION_OUT:DM_DEVICE_DIRECTION_IN)
 
 /* PCM Dump */
 #define PA_DUMP_INI_DEFAULT_PATH                "/usr/etc/mmfw_audio_pcm_dump.ini"
@@ -621,14 +620,14 @@ static pa_hook_result_t route_change_hook_cb(pa_core *c, pa_stream_manager_hook_
         /* Set device state to deactivate */
         PA_IDXSET_FOREACH(device, conn_devices, conn_idx) {
             dm_device_type = pa_device_manager_get_device_type(device);
-            device_state = pa_device_manager_get_device_state(device);
+            device_state = pa_device_manager_get_device_state(device, CONVERT_TO_DEVICE_DIRECTION(data->stream_type));
             device_direction = pa_device_manager_get_device_direction(device);
             if (device_state == DM_DEVICE_STATE_ACTIVATED &&
                 (((data->stream_type==STREAM_SINK_INPUT) && (device_direction & DM_DEVICE_DIRECTION_OUT)) ||
                 ((data->stream_type==STREAM_SOURCE_OUTPUT) && (device_direction & DM_DEVICE_DIRECTION_IN)))) {
                 pa_log_debug("[RESET] found a matched device and set state to DE-ACTIVATED: type[%s], direction[0x%x]", dm_device_type, device_direction);
                 /* set device state to deactivated */
-                pa_device_manager_set_device_state(device, DM_DEVICE_STATE_DEACTIVATED);
+                pa_device_manager_set_device_state(device, CONVERT_TO_DEVICE_DIRECTION(data->stream_type), DM_DEVICE_STATE_DEACTIVATED);
               }
         }
         route_info.num_of_devices = 1;
@@ -668,7 +667,7 @@ static pa_hook_result_t route_change_hook_cb(pa_core *c, pa_stream_manager_hook_
                     pa_log_debug("[AUTO(_ALL)] found a matched device and set state to ACTIVATED: type[%s], direction[0x%x], id[%u]",
                         route_info.device_infos[route_info.num_of_devices-1].type, device_direction, device_idx);
                     /* Set device state to activated */
-                    pa_device_manager_set_device_state(device, DM_DEVICE_STATE_ACTIVATED);
+                    pa_device_manager_set_device_state(device, CONVERT_TO_DEVICE_DIRECTION(data->stream_type), DM_DEVICE_STATE_ACTIVATED);
                     break;
                     }
                 }
@@ -677,7 +676,7 @@ static pa_hook_result_t route_change_hook_cb(pa_core *c, pa_stream_manager_hook_
                 PA_IDXSET_FOREACH(_device, conn_devices, conn_idx) {
                     if (device == _device)
                         continue;
-                    pa_device_manager_set_device_state(_device, DM_DEVICE_STATE_DEACTIVATED);
+                    pa_device_manager_set_device_state(_device, CONVERT_TO_DEVICE_DIRECTION(data->stream_type), DM_DEVICE_STATE_DEACTIVATED);
                 }
 
                 /* Move sink-inputs/source-outputs if needed */
@@ -786,7 +785,7 @@ static pa_hook_result_t route_change_hook_cb(pa_core *c, pa_stream_manager_hook_
                     }
                 }
                 if (need_to_deactive)
-                    pa_device_manager_set_device_state(_device, DM_DEVICE_STATE_DEACTIVATED);
+                    pa_device_manager_set_device_state(_device, CONVERT_TO_DEVICE_DIRECTION(data->stream_type), DM_DEVICE_STATE_DEACTIVATED);
             }
         }
 
@@ -812,7 +811,7 @@ static pa_hook_result_t route_change_hook_cb(pa_core *c, pa_stream_manager_hook_
                         pa_log_debug("[MANUAL] found a matched device and set state to ACTIVATED: type[%s], direction[0x%x]",
                             route_info.device_infos[route_info.num_of_devices-1].type, device_direction);
                         /* Set device state to activated */
-                        pa_device_manager_set_device_state(device, DM_DEVICE_STATE_ACTIVATED);
+                        pa_device_manager_set_device_state(device, CONVERT_TO_DEVICE_DIRECTION(data->stream_type), DM_DEVICE_STATE_ACTIVATED);
                         }
                 }
             }