From c9bff879a6fe51d06131cc327892b3ec3abd04e1 Mon Sep 17 00:00:00 2001 From: Seungbae Shin Date: Mon, 21 Aug 2023 21:28:16 +0900 Subject: [PATCH] A minor code refactoring [Version] 0.2.9 [Issue Type] Refactoring Change-Id: I3df5eba7e1eec154b6157ca7068e88f02f073941 --- packaging/capi-media-tone-player.spec | 2 +- src/tone_player.c | 126 ++++++++++++++++------------------ 2 files changed, 62 insertions(+), 66 deletions(-) diff --git a/packaging/capi-media-tone-player.spec b/packaging/capi-media-tone-player.spec index 6663ca0..6a20082 100755 --- a/packaging/capi-media-tone-player.spec +++ b/packaging/capi-media-tone-player.spec @@ -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 diff --git a/src/tone_player.c b/src/tone_player.c index f524845..8c3aa61 100755 --- a/src/tone_player.c +++ b/src/tone_player.c @@ -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; } - -- 2.7.4