Remove sound_server in build packages
[platform/core/multimedia/libmm-sound.git] / mm_sound_client.c
index e66193a..9bcc86e 100644 (file)
@@ -38,8 +38,6 @@
 
 #define CLIENT_HANDLE_MAX 256
 #define VOLUME_TYPE_LEN 64
-#define VOLUME_MAX_MULTIMEDIA  16
-#define VOLUME_MAX_BASIC               8
 
 #define VCONF_KEY_VOLUME_PREFIX                                "file/private/sound/volume"
 #define VCONF_KEY_VOLUME_TYPE_SYSTEM           VCONF_KEY_VOLUME_PREFIX"/system"
@@ -62,6 +60,9 @@
 #define VCONF_KEY_MUTE_TYPE_VOIP                       VCONF_KEY_MUTE_PREFIX"/voip"
 #define VCONF_KEY_MUTE_TYPE_VOICE              VCONF_KEY_MUTE_PREFIX"/voice"
 
+/* For internal use */
+#define VCONF_KEY_VOLUME_TYPE_BIXBY            VCONF_KEY_VOLUME_PREFIX"/bixby"
+
 static char *g_volume_vconf[VOLUME_TYPE_MAX] = {
        VCONF_KEY_VOLUME_TYPE_SYSTEM,           /* VOLUME_TYPE_SYSTEM */
        VCONF_KEY_VOLUME_TYPE_NOTIFICATION,     /* VOLUME_TYPE_NOTIFICATION */
@@ -74,6 +75,10 @@ static char *g_volume_vconf[VOLUME_TYPE_MAX] = {
        VCONF_KEY_VOLUME_TYPE_ANDROID           /* VOLUME_TYPE_FIXED */
 };
 
+static char *g_volume_vconf_internal[] = {
+       VCONF_KEY_VOLUME_TYPE_BIXBY,            /* VOLUME_TYPE_BIXBY */
+};
+
 static char *g_mute_vconf[] = {
        VCONF_KEY_MUTE_TYPE_SYSTEM,             /* MUTE_TYPE_SYSTEM */
        VCONF_KEY_MUTE_TYPE_NOTIFICATION,       /* MUTE_TYPE_NOTIFICATION */
@@ -85,18 +90,6 @@ static char *g_mute_vconf[] = {
        VCONF_KEY_MUTE_TYPE_VOICE,              /* MUTE_TYPE_VOICE */
 };
 
-static char *g_volume_str[VOLUME_TYPE_MAX] = {
-       "SYSTEM",
-       "NOTIFICATION",
-       "ALARM",
-       "RINGTONE",
-       "MEDIA",
-       "CALL",
-       "VOIP",
-       "VOICE",
-       "FIXED",
-};
-
 struct callback_data {
        void *user_cb;
        void *user_data;
@@ -106,7 +99,11 @@ struct callback_data {
 
 #define GET_CB_DATA(_cb_data, _func, _userdata, _extradata) \
        do { \
-               _cb_data = (struct callback_data*) g_malloc0(sizeof(struct callback_data)); \
+               _cb_data = (struct callback_data*) g_try_malloc0(sizeof(struct callback_data)); \
+               if (!_cb_data) { \
+                       debug_error("failed to allocate callback_data"); \
+                       return MM_ERROR_OUT_OF_MEMORY; \
+               } \
                _cb_data->user_cb = _func; \
                _cb_data->user_data = _userdata; \
                _cb_data->extra_data = _extradata; \
@@ -114,7 +111,7 @@ struct callback_data {
 
 static pthread_mutex_t g_index_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t g_event_mutex = PTHREAD_MUTEX_INITIALIZER;
-guint g_idle_event_src;
+static guint g_idle_event_src;
 
 typedef struct {
        /* handle to watch end of playing */
@@ -128,35 +125,6 @@ typedef struct _focus_idle_event {
        int data;
 } focus_idle_event_t;
 
-static int _validate_volume(volume_type_t type, int level)
-{
-       if (level < 0)
-               return -1;
-
-       switch (type) {
-       case VOLUME_TYPE_CALL:
-       case VOLUME_TYPE_VOIP:
-               if (level >= VOLUME_MAX_BASIC) {
-                       return -1;
-               }
-               break;
-       case VOLUME_TYPE_SYSTEM:
-       case VOLUME_TYPE_MEDIA:
-       case VOLUME_TYPE_ALARM:
-       case VOLUME_TYPE_NOTIFICATION:
-       case VOLUME_TYPE_RINGTONE:
-       case VOLUME_TYPE_VOICE:
-               if (level >= VOLUME_MAX_MULTIMEDIA) {
-                       return -1;
-               }
-               break;
-       default:
-               return -1;
-               break;
-       }
-       return 0;
-}
-
 int mm_sound_client_initialize(void)
 {
        int ret = MM_ERROR_NONE;
@@ -227,99 +195,6 @@ void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_t
 /*****************************************************************************************
                            DBUS SUPPORTED FUNCTIONS
 ******************************************************************************************/
-int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id,
-                                                                                       double volume, int duration, int *handle)
-{
-       int ret = MM_ERROR_NONE;
-
-       debug_fenter();
-
-       ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
-
-       debug_fleave();
-       return ret;
-}
-
-static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
-{
-       struct callback_data *cb_data = (struct callback_data*) userdata;
-       play_sound_end_callback_data_t *end_cb_data;
-
-       debug_log("ended_handle : %d", ended_handle);
-
-       if (cb_data == NULL) {
-               debug_warning("stop callback data null");
-               return;
-       }
-
-       end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
-
-       if (ended_handle == end_cb_data->watching_handle) {
-               debug_log("Interested playing handle end : %d", ended_handle);
-               ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
-               if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
-                       debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
-       } else {
-               debug_log("Not interested playing handle : %d", ended_handle);
-       }
-}
-
-static void play_end_callback_data_free_func(void *data)
-{
-       struct callback_data *cb_data = (struct callback_data*) data;
-
-       if (cb_data) {
-               g_free(cb_data->extra_data);
-               g_free(cb_data);
-       }
-}
-
-int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
-{
-       int ret = MM_ERROR_NONE;
-       struct callback_data *cb_data = NULL;
-       play_sound_end_callback_data_t *end_cb_data;
-
-       ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
-                                                                                                       getpid(), handle, stream_type, stream_id);
-       if (ret != MM_ERROR_NONE) {
-               debug_error("Play Sound Failed");
-               goto failed;
-       }
-       if (param->callback) {
-               end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
-               end_cb_data->watching_handle = *handle;
-               GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
-
-               ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
-                                                                                                               play_end_callback_data_free_func, &end_cb_data->subs_id);
-               if (ret != MM_ERROR_NONE)
-                       debug_error("Add callback for play sound(%d) Failed", *handle);
-       }
-
-failed:
-
-       debug_fleave();
-       return ret;
-
-}
-
-int mm_sound_client_stop_sound(int handle)
-{
-       int ret = MM_ERROR_NONE;
-       debug_fenter();
-
-       if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
-               ret = MM_ERROR_INVALID_ARGUMENT;
-               return ret;
-       }
-
-       ret = mm_sound_proxy_stop_sound(handle);
-
-       debug_fleave();
-       return ret;
-}
-
 static int _mm_sound_client_device_list_dump(GList *device_list)
 {
        int ret = MM_ERROR_NONE;
@@ -736,7 +611,7 @@ failed:
        return ret;
 }
 
-int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
+int __convert_volume_type_to_str(volume_type_t volume_type, char **volume_type_str)
 {
        int ret = MM_ERROR_NONE;
 
@@ -772,38 +647,64 @@ int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
                debug_error("unexpected volume type [%d]", volume_type);
                return MM_ERROR_SOUND_INTERNAL;
        }
-       if (!strncmp(*volume_type_str, "", VOLUME_TYPE_LEN)) {
-               debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
-               ret = MM_ERROR_SOUND_INTERNAL;
-       } else {
-               debug_log("volume_type[%s]", *volume_type_str);
+       debug_log("volume_type[%s]", *volume_type_str);
+       return ret;
+}
+
+int __convert_volume_type_internal_to_str(volume_type_internal_t volume_type, char **volume_type_str)
+{
+       int ret = MM_ERROR_NONE;
+
+       if (!volume_type_str)
+               return MM_ERROR_COMMON_INVALID_ARGUMENT;
+
+       switch (volume_type) {
+       case VOLUME_TYPE_BIXBY:
+               *volume_type_str = "bixby";
+               break;
+       default:
+               debug_error("unexpected volume type [%d]", volume_type);
+               return MM_ERROR_SOUND_INTERNAL;
        }
+       debug_log("volume_type[%s]", *volume_type_str);
        return ret;
 }
 
-static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
+static int __convert_volume_type_to_int(const char *volume_type_str, int *volume_type, bool *is_for_internal)
 {
        int ret = MM_ERROR_NONE;
 
-       if (!volume_type || !volume_type_str)
+       if (!volume_type || !volume_type_str || !is_for_internal)
                return MM_ERROR_COMMON_INVALID_ARGUMENT;
 
        if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
                *volume_type = VOLUME_TYPE_SYSTEM;
+               *is_for_internal = false;
        } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
                *volume_type = VOLUME_TYPE_NOTIFICATION;
+               *is_for_internal = false;
        } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
                *volume_type = VOLUME_TYPE_ALARM;
+               *is_for_internal = false;
        } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
                *volume_type = VOLUME_TYPE_RINGTONE;
+               *is_for_internal = false;
        } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
                *volume_type = VOLUME_TYPE_MEDIA;
+               *is_for_internal = false;
        } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
                *volume_type = VOLUME_TYPE_CALL;
+               *is_for_internal = false;
        } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
                *volume_type = VOLUME_TYPE_VOIP;
+               *is_for_internal = false;
        } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
                *volume_type = VOLUME_TYPE_VOICE;
+               *is_for_internal = false;
+       /* else-if statements below are for internal use */
+       } else if (!strncmp(volume_type_str, "bixby", VOLUME_TYPE_LEN)) {
+               *volume_type = VOLUME_TYPE_BIXBY;
+               *is_for_internal = true;
        } else {
                debug_log("Invalid volume type : [%s]", volume_type_str);
                ret = MM_ERROR_SOUND_INTERNAL;
@@ -823,12 +724,6 @@ int mm_sound_client_set_volume_by_type(volume_type_t type, const unsigned int le
                return MM_ERROR_INVALID_ARGUMENT;
        }
 
-       /* Check input param */
-       if (0 > _validate_volume(type, (int)level)) {
-               debug_error("invalid level %u of %s", level, g_volume_str[type]);
-               return MM_ERROR_INVALID_ARGUMENT;
-       }
-
        if ((ret = __convert_volume_type_to_str(type, &type_str)) != MM_ERROR_NONE) {
                debug_error("volume type convert failed");
                goto failed;
@@ -867,10 +762,51 @@ int mm_sound_client_get_volume_by_type(volume_type_t type, unsigned int *level)
        return ret;
 }
 
+int mm_sound_client_set_volume_by_internal_type(volume_type_internal_t type, const unsigned int level)
+{
+       int ret = MM_ERROR_NONE;
+       char *type_str = NULL;
+
+       debug_fenter();
+
+       if ((ret = __convert_volume_type_internal_to_str(type, &type_str)) != MM_ERROR_NONE) {
+               debug_error("volume type convert failed");
+               goto failed;
+       }
+
+       ret = mm_sound_proxy_set_volume_by_type(type_str, level);
+
+failed:
+       debug_fleave();
+       return ret;
+}
+
+int mm_sound_client_get_volume_by_internal_type(volume_type_internal_t type, unsigned int *level)
+{
+       int ret = MM_ERROR_NONE;
+       int vconf_value = 0;
+
+       if (level == NULL) {
+               debug_error("invalid argument, level is null");
+               return MM_ERROR_INVALID_ARGUMENT;
+       }
+
+       /* Get volume value from VCONF */
+       if (vconf_get_int(g_volume_vconf_internal[type], &vconf_value)) {
+               debug_error("vconf_get_int(%s) failed..\n", g_volume_vconf_internal[type]);
+               return MM_ERROR_SOUND_INTERNAL;
+       }
+
+       *level = vconf_value;
+
+       return ret;
+}
+
 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str,
                                                                                                                int volume_level, void *userdata)
 {
-       volume_type_t volume_type = 0;
+       int volume_type = 0;
+       bool is_for_internal =  false;
        struct callback_data *cb_data = (struct callback_data *) userdata;
 
        debug_log("direction : %s, volume_type : %s, volume_level : %d",
@@ -881,13 +817,20 @@ static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction
                return;
        }
 
-       if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
+       if (__convert_volume_type_to_int(volume_type_str, &volume_type, &is_for_internal) != MM_ERROR_NONE) {
                debug_error("volume type convert failed");
                return;
        }
-       debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u",
+
+       if (!is_for_internal && !cb_data->extra_data) {
+               debug_log("Invoke volume changed cb, direction : %s, vol_type : %s(%d), level : %u",
+                       direction, volume_type_str, volume_type, volume_level);
+               ((mm_sound_volume_changed_cb)(cb_data->user_cb))((volume_type_t)volume_type, volume_level, cb_data->user_data);
+       } else if (is_for_internal && (cb_data->extra_data && (bool)(cb_data->extra_data))) {
+               debug_log("Invoke internal volume changed cb, direction : %s, vol_type : %s(%d), level : %u",
                        direction, volume_type_str, volume_type, volume_level);
-       ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
+               ((mm_sound_volume_changed_cb_internal)(cb_data->user_cb))((volume_type_internal_t)volume_type, volume_level, cb_data->user_data);
+       }
 }
 
 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
@@ -906,6 +849,23 @@ int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func,
        return ret;
 }
 
+int mm_sound_client_add_volume_changed_callback_internal(mm_sound_volume_changed_cb_internal func, void* userdata, unsigned int *subs_id)
+{
+       int ret = MM_ERROR_NONE;
+       struct callback_data *cb_data = NULL;
+       gboolean is_for_internal = true;
+
+       debug_fenter();
+
+       GET_CB_DATA(cb_data, func, userdata, (void*)is_for_internal);
+
+       ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
+
+       debug_fleave();
+
+       return ret;
+}
+
 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
 {
        int ret = MM_ERROR_NONE;
@@ -1062,7 +1022,7 @@ int mm_sound_client_register_focus(int pid, const char *stream_type,
        focus_init_callback(index, false);
 
 cleanup:
-       if (ret)
+       if (ret != MM_ERROR_NONE && index >= 0)
                g_focus_sound_handle[index].is_used = false;
 
        MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
@@ -1349,7 +1309,7 @@ int mm_sound_client_set_focus_watch_callback(int pid, mm_sound_focus_type_e focu
        focus_init_callback(index, true);
 
 cleanup:
-       if (ret)
+       if (ret != MM_ERROR_NONE && index >= 0)
                g_focus_sound_handle[index].is_used = false;
 
        MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
@@ -1477,6 +1437,34 @@ int mm_sound_client_execute_focus_func_in_main_context(focus_idle_event_type_e t
        return MM_ERROR_NONE;
 }
 
+int mm_sound_client_add_ducking_state_changed_callback(mm_sound_ducking_state_changed_cb func, void* userdata, unsigned int *subs_id)
+{
+       int ret = MM_ERROR_NONE;
+       struct callback_data *cb_data = NULL;
+
+       debug_fenter();
+
+       GET_CB_DATA(cb_data, func, userdata, NULL);
+
+       ret = mm_sound_proxy_add_ducking_state_changed_callback((mm_sound_ducking_state_changed_wrapper_cb)func,
+               cb_data, g_free, subs_id);
+
+       debug_fleave();
+
+       return ret;
+}
+
+int mm_sound_client_remove_ducking_state_changed_callback(unsigned int subs_id)
+{
+       int ret = MM_ERROR_NONE;
+       debug_fenter();
+
+       ret = mm_sound_proxy_remove_ducking_state_changed_callback(subs_id);
+
+       debug_fleave();
+       return ret;
+}
+
 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
 {
        int ret = MM_ERROR_NONE;