A minor code refactoring 13/297613/6 accepted/tizen_8.0_unified tizen_8.0 accepted/tizen/8.0/unified/20231005.092908 accepted/tizen/unified/20230828.102411 tizen_8.0_m2_release
authorSeungbae Shin <seungbae.shin@samsung.com>
Mon, 21 Aug 2023 12:28:16 +0000 (21:28 +0900)
committerSeungbae Shin <seungbae.shin@samsung.com>
Fri, 25 Aug 2023 09:10:13 +0000 (18:10 +0900)
[Version] 0.2.9
[Issue Type] Refactoring

Change-Id: I3df5eba7e1eec154b6157ca7068e88f02f073941

packaging/capi-media-tone-player.spec
src/tone_player.c

index 6663ca0..6a20082 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       capi-media-tone-player
 Summary:    A tone player library in Tizen C API
-Version:    0.2.8
+Version:    0.2.9
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index f524845..8c3aa61 100755 (executable)
@@ -34,7 +34,7 @@
 #define PA_TONE_PLAYER_METHOD_NAME_TONE_PLAY "TonePlay"
 #define PA_TONE_PLAYER_METHOD_NAME_TONE_STOP "ToneStop"
 
-static int _convert_dbus_error(const char *error_msg)
+static int __convert_dbus_error(const char *error_msg)
 {
        if (error_msg && strstr(error_msg, "InvalidArgument"))
                return TONE_PLAYER_ERROR_INVALID_PARAMETER;
@@ -45,12 +45,12 @@ static int _convert_dbus_error(const char *error_msg)
 static GDBusConnection *__get_dbus_connection(void)
 {
        GDBusConnection *conn = NULL;
-       GError *err = NULL;
+       g_autoptr(GError) err = NULL;
 
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-       if (!conn) {
+       if (!(conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err))) {
+               g_assert(err);
                LOGE("g_bus_get_sync() error (%s)", err->message);
-               g_error_free(err);
+               return NULL;
        }
 
        return conn;
@@ -58,65 +58,42 @@ static GDBusConnection *__get_dbus_connection(void)
 
 static int __dbus_method_call(const char *method, GVariant *param, GVariant **reply)
 {
-       GDBusConnection *conn = NULL;
-       GVariant * _reply = NULL;
-       GError *err = NULL;
-       int ret;
+       g_autoptr(GDBusConnection) conn = NULL;
+       g_autoptr(GVariant) _reply = NULL;
+       g_autoptr(GError) err = NULL;
 
        if (!method || !param) {
-               LOGE("invalid parameter");
-               ret = TONE_PLAYER_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
-               goto error;
-       }
-
-       if (!(conn = __get_dbus_connection())) {
-               ret = TONE_PLAYER_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
-               goto error;
+               LOGE("invalid parameter"); //LCOV_EXCL_LINE
+               return TONE_PLAYER_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
        }
 
-       _reply = g_dbus_connection_call_sync(conn, PA_BUS_NAME,
-               PA_TONE_PLAYER_OBJECT_PATH,
-               PA_TONE_PLAYER_INTERFACE,
-               method, param, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
-
-       g_object_unref(conn);
+       if (!(conn = __get_dbus_connection()))
+               return TONE_PLAYER_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
 
-       if (!_reply) {
+       if (!(_reply = g_dbus_connection_call_sync(conn, PA_BUS_NAME,
+                                                                               PA_TONE_PLAYER_OBJECT_PATH,
+                                                                               PA_TONE_PLAYER_INTERFACE,
+                                                                               method, param, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL,
+                                                                               &err))) {
+               g_assert(err);
                LOGE("g_dbus_connection_call_sync() method(%s), err-msg(%s)", method, err->message);
-               ret = _convert_dbus_error(err->message);
-               g_error_free(err);
-               return ret;
+
+               return __convert_dbus_error(err->message);
        }
 
        if (reply)
-               *reply = _reply;
-       else
-               g_variant_unref(_reply);
+               *reply = g_steal_pointer(&_reply);
 
        return TONE_PLAYER_ERROR_NONE;
-
-error:
-       if (param)
-               g_variant_unref(param);
-
-       return ret;
 }
 
-
-int tone_player_start_new(tone_type_e tone, sound_stream_info_h stream_info, int duration_ms, int *id)
+static int __get_sound_manager_stream_properties(sound_stream_info_h stream_info, char **stream_type, int *stream_id)
 {
-       int ret;
-       char *stream_type = NULL;
-       int stream_id;
+       int ret = SOUND_MANAGER_ERROR_NONE;
        bool result = false;
 
-       GVariant *reply = NULL;
-       int handle;
-
-       LOGI("tone(%d), duration_ms(%d)", tone, duration_ms);
-
-       if (tone < TONE_TYPE_DEFAULT || tone > TONE_TYPE_USER_DEFINED_HIGH_FRE || !stream_info) {
-               LOGE("invalid parameter.");
+       if (!stream_info) {
+               LOGE("invalid stream_info!!");
                return TONE_PLAYER_ERROR_INVALID_PARAMETER;
        }
 
@@ -126,47 +103,66 @@ int tone_player_start_new(tone_type_e tone, sound_stream_info_h stream_info, int
                return TONE_PLAYER_ERROR_NOT_SUPPORTED_TYPE;
        }
 
-       ret = sound_manager_get_type_from_stream_information(stream_info, &stream_type);
-       if (ret) {
+       if ((ret = sound_manager_get_type_from_stream_information(stream_info, stream_type))) {
                LOGE("can't get stream type. ret(0x%x)", ret);
                return TONE_PLAYER_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
        }
 
-       ret = sound_manager_get_index_from_stream_information(stream_info, &stream_id);
-       if (ret) {
+       if ((ret = sound_manager_get_index_from_stream_information(stream_info, stream_id))) {
                LOGE("can't get stream index. ret(0x%x)", ret);
                return TONE_PLAYER_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
        }
 
-       ret =  __dbus_method_call(PA_TONE_PLAYER_METHOD_NAME_TONE_PLAY,
-                       g_variant_new("(uiisi)", tone, duration_ms, getpid(), stream_type, stream_id), &reply);
-       if (ret) {
+       return TONE_PLAYER_ERROR_NONE;
+}
+
+int tone_player_start_new(tone_type_e tone, sound_stream_info_h stream_info, int duration_ms, int *id)
+{
+       int ret = TONE_PLAYER_ERROR_NONE;
+       char *stream_type = NULL;
+       int stream_id = -1;
+       g_autoptr(GVariant) reply = NULL;
+       unsigned int handle_id = 0;
+
+       LOGI("tone(%d), duration_ms(%d)", tone, duration_ms);
+
+       if (tone < TONE_TYPE_DEFAULT || tone > TONE_TYPE_USER_DEFINED_HIGH_FRE) {
+               LOGE("invalid tone type (%d)", tone);
+               return TONE_PLAYER_ERROR_INVALID_PARAMETER;
+       }
+
+       if ((ret = __get_sound_manager_stream_properties(stream_info, &stream_type, &stream_id)))
+               return ret;
+
+       if ((ret = __dbus_method_call(PA_TONE_PLAYER_METHOD_NAME_TONE_PLAY,
+                                                               g_variant_new("(uiisi)", tone, duration_ms, getpid(), stream_type, stream_id),
+                                                               &reply))) {
                LOGE("__dbus_method_call ret(0x%x)", ret);
                return ret;
        }
 
-       g_variant_get(reply, "(u)", &handle);
-       g_variant_unref(reply);
-       if (id)
-               *id = handle;
+       g_assert(reply);
+       g_variant_get(reply, "(u)", &handle_id);
+       LOGI("handle id(%u)", handle_id);
 
-       LOGI("handle : %d", handle);
+       if (id)
+               *id = (int)handle_id;
 
        return TONE_PLAYER_ERROR_NONE;
 }
 
 int tone_player_stop(int id)
 {
-       int ret;
+       int ret = TONE_PLAYER_ERROR_NONE;
 
-       LOGI("handle(%d)", id);
+       LOGI("handle id(%d)", id);
 
-       ret =  __dbus_method_call(PA_TONE_PLAYER_METHOD_NAME_TONE_STOP, g_variant_new("(u)", id), NULL);
-       if (ret) {
+       if ((ret =  __dbus_method_call(PA_TONE_PLAYER_METHOD_NAME_TONE_STOP,
+                                                               g_variant_new("(u)", id),
+                                                               NULL))) {
                LOGE("__dbus_method_call ret(0x%x)", ret);
                return ret;
        }
 
        return TONE_PLAYER_ERROR_NONE;
 }
-