Adapt session interrupt callback to focus callback 29/44329/17
authorinhyeok <i_bc.kim@samsung.com>
Tue, 21 Jul 2015 04:17:14 +0000 (13:17 +0900)
committerinhyeok <i_bc.kim@samsung.com>
Wed, 12 Aug 2015 07:12:03 +0000 (16:12 +0900)
[Version] Release 0.3.17
[Profile] Common
[Issue Type] Refactoring
[Dependency module] libmm-sound

Modify session interrupt callback to be invoked when focus callback is called

Change-Id: I0752f746e7cba3e54f544ec1560b5867575b4fd4
Signed-off-by: inhyeok <i_bc.kim@samsung.com>
include/sound_manager_private.h
packaging/capi-media-sound-manager.spec
src/sound_manager.c
src/sound_manager_private.c

index a09669be21996c4e0494fc395a94f0de3305219c..a0b3ecbca30822e72c04e7966830cce6ce134489 100644 (file)
@@ -172,6 +172,7 @@ typedef enum {
 
 typedef struct {
        int is_registered;
+       unsigned int subs_id; /* for internal device connected subscription */
        void *user_data;
        sound_session_interrupted_cb user_cb;
 }_session_interrupt_info_s;
@@ -200,6 +201,10 @@ typedef struct {
        sound_device_information_changed_cb user_cb;
 }_device_changed_info_s;
 
+void _focus_session_interrupt_cb (mm_sound_focus_state_e state, const char *reason_for_change, bool is_wcb, void *user_data);
+
+void _device_connected_cb(sound_device_h device, bool is_connected, void *user_data);
+
 void _focus_state_change_callback (int index, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state, const char *reason_for_change, const char *additional_info, void *user_data);
 
 void _focus_watch_callback (int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state, const char *reason_for_change, const char *additional_info, void *user_data);
index 60b590aa843cd6bafce95f7c36c9714743956ca3..a9f35021acc613ef327a7cc119825a9151b07910 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       capi-media-sound-manager
 Summary:    Sound Manager library
-Version:    0.3.16
+Version:    0.3.17
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index c5d603da301bfd71e9907b319187225793a6670f..8c8bcb7051f35101caa7bcf7504183bbd7398739 100644 (file)
@@ -19,7 +19,7 @@
 
 #define TMP_CODE
 
-_session_interrupt_info_s g_session_interrupt_cb_table = {0, NULL, NULL};
+_session_interrupt_info_s g_session_interrupt_cb_table = {0, 0, NULL, NULL};
 _volume_changed_info_s g_volume_changed_cb_table = {0, NULL, NULL};
 _focus_watch_info_s g_focus_watch_cb_table = {-1, NULL, NULL};
 _device_connected_info_s g_device_connected_cb_table = {0, NULL, NULL};
@@ -31,7 +31,7 @@ _session_mode_e g_cached_session_mode = -1;
 /* These variables will be removed when session features are deprecated. */
 extern int g_stream_info_count;
 extern pthread_mutex_t g_stream_info_count_mutex;
-pthread_mutex_t g_device_info_cb_mutex, g_device_conn_cb_mutex, g_volume_cb_mutex;
+pthread_mutex_t g_interrupt_cb_mutex, g_device_info_cb_mutex, g_device_conn_cb_mutex, g_volume_cb_mutex;
 
 #ifdef TMP_CODE
 /*temporary variable for set/get voip session mode. When 2.4  feature for routing is fully implemented, it will be removed.*/
@@ -974,34 +974,69 @@ int sound_manager_get_voip_session_mode (sound_session_voip_mode_e *mode)
 int sound_manager_set_session_interrupted_cb (sound_session_interrupted_cb callback, void *user_data)
 {
        int ret = MM_ERROR_NONE;
-       if (callback == NULL)
-               return __convert_sound_manager_error_code(__func__, MM_ERROR_INVALID_ARGUMENT);
+       unsigned int subs_id = 0;
+
+       SM_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_interrupt_cb_mutex, SOUND_MANAGER_ERROR_INTERNAL);
+
+       if (callback == NULL) {
+               ret = MM_ERROR_INVALID_ARGUMENT;
+               goto finish;
+       }
 
        /* it is not supported both session and stream feature at the same time */
-       if (g_stream_info_count)
-               return __convert_sound_manager_error_code(__func__, MM_ERROR_POLICY_INTERNAL);
+       if (g_stream_info_count) {
+               ret =  MM_ERROR_POLICY_INTERNAL;
+               goto finish;
+       }
 
        if (g_session_interrupt_cb_table.is_registered == 0) {
-               ret = mm_session_init_ex(SOUND_SESSION_TYPE_DEFAULT /*default*/ , __session_interrupt_cb, NULL);
-               if (ret != 0)
-                       return __convert_sound_manager_error_code(__func__, ret);
+               ret = mm_sound_add_device_connected_callback(SOUND_DEVICE_ALL_MASK, (mm_sound_device_connected_cb)_device_connected_cb, NULL, &subs_id);
+               if (ret)
+                       goto finish;
+               ret = mm_sound_focus_set_session_interrupt_callback((mm_sound_focus_session_interrupt_cb)_focus_session_interrupt_cb, NULL);
+               if (ret) {
+                       if(mm_sound_remove_device_connected_callback(subs_id) != MM_ERROR_NONE)
+                               LOGW("mm_sound_remove_device_connected_callback failed");
+                       goto finish;
+               }
                g_session_interrupt_cb_table.is_registered = 1;
+               g_session_interrupt_cb_table.subs_id = subs_id;
        }
 
        g_session_interrupt_cb_table.user_cb = (sound_session_interrupted_cb)callback;
        g_session_interrupt_cb_table.user_data = user_data;
-       return SOUND_MANAGER_ERROR_NONE;
+
+finish:
+       SM_LEAVE_CRITICAL_SECTION(&g_interrupt_cb_mutex);
+       return __convert_sound_manager_error_code(__func__, ret);
 }
 
 int sound_manager_unset_session_interrupted_cb (void)
 {
        int ret = MM_ERROR_NONE;
+
+       SM_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_interrupt_cb_mutex, SOUND_MANAGER_ERROR_INTERNAL);
+
        if (g_session_interrupt_cb_table.user_cb) {
+               ret = mm_sound_focus_unset_session_interrupt_callback();
+               if (ret) {
+                       if(mm_sound_remove_device_connected_callback(g_session_interrupt_cb_table.subs_id) != MM_ERROR_NONE)
+                               LOGW("mm_sound_remove_device_connected_callback failed");
+                       goto finish;
+               }
+               ret = mm_sound_remove_device_connected_callback(g_session_interrupt_cb_table.subs_id);
+               if (ret)
+                       goto finish;
+               g_session_interrupt_cb_table.subs_id = 0;
                g_session_interrupt_cb_table.user_cb = NULL;
                g_session_interrupt_cb_table.user_data = NULL;
+               g_session_interrupt_cb_table.is_registered = 0;
        } else {
                ret = MM_ERROR_SOUND_INTERNAL;
        }
+
+finish:
+       SM_LEAVE_CRITICAL_SECTION(&g_interrupt_cb_mutex);
        return __convert_sound_manager_error_code(__func__, ret);
 }
 
index 99449d7cb187ce8905a59f8f779f9edbc929125e..c931887fba26ce74b1ab91ef05efab045fec93f1 100644 (file)
@@ -223,6 +223,44 @@ int __convert_stream_type_to_change_reason (const char *stream_type, sound_strea
        return ret;
 }
 
+static int __convert_stream_type_to_interrupt_reason (const char *stream_type, sound_session_interrupted_code_e *change_reason)
+{
+       int ret = MM_ERROR_NONE;
+
+       SM_NULL_ARG_CHECK(stream_type);
+       SM_NULL_ARG_CHECK(change_reason);
+
+       if (!strncmp(stream_type, "media", SOUND_STREAM_TYPE_LEN) ||
+               !strncmp(stream_type, "radio", SOUND_STREAM_TYPE_LEN) ||
+               !strncmp(stream_type, "system", SOUND_STREAM_TYPE_LEN) ||
+               !strncmp(stream_type, "voice-information", SOUND_STREAM_TYPE_LEN) ||
+               !strncmp(stream_type, "voice-recognition", SOUND_STREAM_TYPE_LEN) ||
+               !strncmp(stream_type, "loopback", SOUND_STREAM_TYPE_LEN)) {
+               *change_reason = SOUND_SESSION_INTERRUPTED_BY_MEDIA;
+
+       } else if (!strncmp(stream_type, "alarm", SOUND_STREAM_TYPE_LEN)) {
+               *change_reason = SOUND_SESSION_INTERRUPTED_BY_ALARM;
+
+       } else if (!strncmp(stream_type, "notification", SOUND_STREAM_TYPE_LEN)) {
+               *change_reason = SOUND_SESSION_INTERRUPTED_BY_NOTIFICATION;
+
+       } else if (!strncmp(stream_type, "emergency", SOUND_STREAM_TYPE_LEN)) {
+               *change_reason = SOUND_SESSION_INTERRUPTED_BY_EMERGENCY;
+
+       } else if (!strncmp(stream_type, "ringtone-voip", SOUND_STREAM_TYPE_LEN) ||
+                       !strncmp(stream_type, "ringtone-call", SOUND_STREAM_TYPE_LEN) ||
+                       !strncmp(stream_type, "voip", SOUND_STREAM_TYPE_LEN) ||
+                       !strncmp(stream_type, "call-voice", SOUND_STREAM_TYPE_LEN) ||
+                       !strncmp(stream_type, "call-video", SOUND_STREAM_TYPE_LEN)) {
+               *change_reason = SOUND_SESSION_INTERRUPTED_BY_CALL;
+
+       } else {
+               ret = MM_ERROR_INVALID_ARGUMENT;
+               LOGE("not supported stream_type(%s), err(0x%08x)", stream_type, ret);
+       }
+       return ret;
+}
+
 int __convert_sound_type (sound_type_e sound_type, const char **volume_type)
 {
        int ret = MM_ERROR_NONE;
@@ -880,6 +918,51 @@ void __session_interrupt_cb (session_msg_t msg, session_event_t event, void *use
        }
 }
 
+void _focus_session_interrupt_cb (mm_sound_focus_state_e state, const char *reason_for_change, bool is_wcb, void *user_data)
+{
+       sound_session_interrupted_code_e e;
+       LOGE("session interrupted by [%s]", reason_for_change);
+       if (g_session_interrupt_cb_table.user_cb) {
+               if (is_wcb) {
+                       if (state == FOCUS_IS_RELEASED) {
+                               e = SOUND_SESSION_INTERRUPTED_COMPLETED;
+                       } else {
+                               __convert_stream_type_to_interrupt_reason(reason_for_change, &e);
+                       }
+               } else {
+                       if (state == FOCUS_IS_ACQUIRED) {
+                               e = SOUND_SESSION_INTERRUPTED_COMPLETED;
+                       } else {
+                               __convert_stream_type_to_interrupt_reason(reason_for_change, &e);
+                       }
+               }
+               g_session_interrupt_cb_table.user_cb(e, g_session_interrupt_cb_table.user_data);
+       }
+}
+
+void _device_connected_cb(sound_device_h device, bool is_connected, void *user_data)
+{
+       sound_device_type_e type;
+       if (sound_manager_get_device_type (device, &type) != MM_ERROR_NONE) {
+               LOGE("getting device type failed");
+       } else {
+               switch (type) {
+                       case SOUND_DEVICE_AUDIO_JACK:
+                       case SOUND_DEVICE_BLUETOOTH:
+                       case SOUND_DEVICE_HDMI:
+                       case SOUND_DEVICE_MIRRORING:
+                       case SOUND_DEVICE_USB_AUDIO:
+                               if (!is_connected) {
+                                       LOGI("sound device unplugged");
+                                       g_session_interrupt_cb_table.user_cb(SOUND_SESSION_INTERRUPTED_BY_EARJACK_UNPLUG, g_session_interrupt_cb_table.user_data);
+                               }
+                               break;
+                       default:
+                               break;
+               }
+       }
+}
+
 int __set_session_mode (_session_mode_e mode)
 {
        int ret = MM_ERROR_NONE;