From: Suyeon Hwang Date: Mon, 23 Nov 2020 06:26:59 +0000 (+0900) Subject: Refactor duplicated code for handling dbus request X-Git-Tag: submit/tizen/20210628.060348~23 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c0fe4e9b4f18d2fb0869ef3e5d77e7b832ce0b1f;p=platform%2Fcore%2Fuifw%2Ftts.git Refactor duplicated code for handling dbus request Change-Id: I7519e7144ed9c423304bcbc41f0e3beb752d96dd Signed-off-by: Suyeon Hwang --- diff --git a/client/tts.c b/client/tts.c index 81dfaaf3..b14dff4a 100644 --- a/client/tts.c +++ b/client/tts.c @@ -340,6 +340,7 @@ int tts_destroy(tts_h tts) if (0 != tts_core_unprepare(client)) { SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to unprepare"); } + tts_client_set_current_state(client, TTS_STATE_CREATED); case TTS_STATE_CREATED: /* Unset registered callbacks */ tts_client_unset_all_cb(client); @@ -651,6 +652,8 @@ int tts_unprepare(tts_h tts) return ret; } + tts_core_set_current_state(client, TTS_STATE_CREATED); + SLOG(LOG_DEBUG, TAG_TTSC, "@@@"); return TTS_ERROR_NONE; } diff --git a/client/tts_core.c b/client/tts_core.c index e1870223..ec2e6f2b 100644 --- a/client/tts_core.c +++ b/client/tts_core.c @@ -633,6 +633,28 @@ static void __screen_reader_state_changed_cb(bool value) } //LCOV_EXCL_STOP +static inline bool __handle_dbus_request_result(tts_client_s* client, tts_error_e ret, bool* is_prepared) +{ + if (TTS_ERROR_NONE == ret) { + SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success the dbus request"); + return false; + } else if (TTS_ERROR_INVALID_PARAMETER == ret && false == *is_prepared) { + tts_client_set_current_state(client, TTS_STATE_CREATED); + if (0 == tts_core_prepare_sync(client)) { + *is_prepared = true; + SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync"); + } + } else if (TTS_ERROR_TIMED_OUT != ret) { + SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret)); + return false; + } else { + SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", __tts_get_error_code(ret)); + usleep(10000); + } + + return true; +} + static inline int __request_add_text(tts_client_s* client, const char* text, const char* language, int voice_type, int speed, int* utt_id) { /* change default language value */ @@ -648,36 +670,20 @@ static inline int __request_add_text(tts_client_s* client, const char* text, con bool is_prepared = false; while (TTS_RETRY_COUNT > count) { ret = tts_ipc_request_add_text(uid, text, convert_language, voice_type, speed, new_utt_id, client->credential); - if (0 == ret) { - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_ipc_request_add_text"); - *utt_id = new_utt_id; + if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { break; } - //LCOV_EXCL_START - if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - tts_client_set_current_state(client, TTS_STATE_CREATED); - if (0 == tts_core_prepare_sync(client)) { - is_prepared = true; - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync"); - } - } else if (TTS_ERROR_TIMED_OUT != ret) { - SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret)); - return ret; - } else { - SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry add text : %s", __tts_get_error_code(ret)); - usleep(10000); - } - //LCOV_EXCL_STOP - count++; } - if (TTS_RETRY_COUNT == count) { + if (TTS_ERROR_NONE != ret) { SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request"); - return TTS_ERROR_TIMED_OUT; + return ret; } + SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_dbus_request_add_text"); + *utt_id = new_utt_id; return TTS_ERROR_NONE; } @@ -689,33 +695,19 @@ static inline int __request_play(tts_client_s* client) bool is_prepared = false; while (TTS_RETRY_COUNT > count) { ret = tts_ipc_request_play(uid, client->credential); - if (0 == ret) { - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_ipc_request_play"); + if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { break; } - if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - tts_client_set_current_state(client, TTS_STATE_CREATED); - if (0 == tts_core_prepare_sync(client)) { - is_prepared = true; - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync"); - } - } else if (TTS_ERROR_TIMED_OUT != ret) { - SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret)); - return ret; - } else { - SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", __tts_get_error_code(ret)); - usleep(10000); - } - count++; } - if (TTS_RETRY_COUNT == count) { + if (TTS_ERROR_NONE != ret) { SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request"); - return TTS_ERROR_TIMED_OUT; + return ret; } + SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_dbus_request_play"); return tts_core_set_current_state(client, TTS_STATE_PLAYING); } @@ -1151,34 +1143,19 @@ int tts_core_unprepare(tts_client_s* client) } else { bool is_prepared = false; - do { + while(TTS_RETRY_COUNT > count) { ret = tts_ipc_request_finalize(uid); - if (0 != ret) { - //LCOV_EXCL_START - if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - tts_client_set_current_state(client, TTS_STATE_CREATED); - if (0 == tts_core_prepare_sync(client)) { - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync"); - is_prepared = true; - } - } else if (TTS_ERROR_TIMED_OUT != ret) { - SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret)); - break; - } else { - SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry finalize : %s", __tts_get_error_code(ret)); - usleep(10000); - count++; - if (TTS_RETRY_COUNT == count) { - SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request"); - break; - } - } - //LCOV_EXCL_STOP + if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { + break; } - } while (0 != ret); - } - tts_core_set_current_state(client, TTS_STATE_CREATED); + count++; + } + + if (TTS_ERROR_NONE == ret) { + SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_dbus_request_finalize"); + } + } return TTS_ERROR_NONE; } @@ -1310,31 +1287,16 @@ int tts_core_stop(tts_client_s* client) bool is_prepared = false; while (TTS_RETRY_COUNT > count) { ret = tts_ipc_request_stop(uid); - if (0 == ret) { - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_ipc_request_play"); + if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { break; } - if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - tts_client_set_current_state(client, TTS_STATE_CREATED); - if (0 == tts_core_prepare_sync(client)) { - is_prepared = true; - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync"); - } - } else if (TTS_ERROR_TIMED_OUT != ret) { - SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret)); - return ret; - } else { - SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", __tts_get_error_code(ret)); - usleep(10000); - } - count++; } - if (TTS_RETRY_COUNT == count) { + if (TTS_ERROR_NONE != ret) { SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request"); - return TTS_ERROR_TIMED_OUT; + return ret; } return tts_core_set_current_state(client, TTS_STATE_READY); @@ -1353,33 +1315,19 @@ int tts_core_pause(tts_client_s* client) bool is_prepared = false; while (TTS_RETRY_COUNT > count) { ret = tts_ipc_request_pause(uid); - if (0 == ret) { - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_ipc_request_pause"); + if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { break; } - if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - tts_client_set_current_state(client, TTS_STATE_CREATED); - if (0 == tts_core_prepare_sync(client)) { - is_prepared = true; - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync"); - } - } else if (TTS_ERROR_TIMED_OUT != ret) { - SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret)); - return ret; - } else { - SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry pause : %s", __tts_get_error_code(ret)); - usleep(10000); - } - count++; } - if (TTS_RETRY_COUNT == count) { + if (TTS_ERROR_NONE != ret) { SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request"); - return TTS_ERROR_TIMED_OUT; + return ret; } + SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_dbus_request_pause"); return tts_core_set_current_state(client, TTS_STATE_PAUSED); } @@ -1435,33 +1383,19 @@ int tts_core_add_pcm(tts_client_s* client, int event, const void* data, unsigned bool is_prepared = false; while (TTS_RETRY_COUNT > count) { ret = tts_ipc_request_add_pcm(uid, event, data, data_size, audio_type, rate); - if (0 == ret) { - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_ipc_request_add_pcm"); + if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { break; } - if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - tts_client_set_current_state(client, TTS_STATE_CREATED); - if (0 == tts_core_prepare_sync(client)) { - is_prepared = true; - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync"); - } - } else if (TTS_ERROR_TIMED_OUT != ret) { - SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret)); - return ret; - } else { - SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry add pcm : %s", __tts_get_error_code(ret)); - usleep(10000); - } - count++; } - if (TTS_RETRY_COUNT == count) { + if (TTS_ERROR_NONE != ret) { SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request"); - return TTS_ERROR_TIMED_OUT; + return ret; } + SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_dbus_request_add_pcm"); return TTS_ERROR_NONE; } @@ -1478,33 +1412,19 @@ int tts_core_play_pcm(tts_client_s* client) bool is_prepared = false; while (TTS_RETRY_COUNT > count) { ret = tts_ipc_request_play_pcm(uid); - if (0 == ret) { - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_ipc_request_play_pcm"); + if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { break; } - if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - tts_client_set_current_state(client, TTS_STATE_CREATED); - if (0 == tts_core_prepare_sync(client)) { - is_prepared = true; - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync"); - } - } else if (TTS_ERROR_TIMED_OUT != ret) { - SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret)); - return ret; - } else { - SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play pcm : %s", __tts_get_error_code(ret)); - usleep(10000); - } - count++; } - if (TTS_RETRY_COUNT == count) { + if (TTS_ERROR_NONE != ret) { SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request"); - return TTS_ERROR_TIMED_OUT; + return ret; } + SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_dbus_request_play_pcm"); return tts_core_set_current_state(client, TTS_STATE_PLAYING); } @@ -1521,32 +1441,18 @@ int tts_core_stop_pcm(tts_client_s* client) bool is_prepared = false; while (TTS_RETRY_COUNT > count) { ret = tts_ipc_request_stop_pcm(uid); - if (0 == ret) { - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_ipc_request_stop_pcm"); + if (false == __handle_dbus_request_result(client, ret, &is_prepared)) { break; } - if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - tts_client_set_current_state(client, TTS_STATE_CREATED); - if (0 == tts_core_prepare_sync(client)) { - is_prepared = true; - SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync"); - } - } else if (TTS_ERROR_TIMED_OUT != ret) { - SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret)); - return ret; - } else { - SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry stop pcm : %s", __tts_get_error_code(ret)); - usleep(10000); - } - count++; } - if (TTS_RETRY_COUNT == count) { + if (TTS_ERROR_NONE != ret) { SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request"); - return TTS_ERROR_TIMED_OUT; + return ret; } + SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_dbus_request_stop_pcm"); return tts_core_set_current_state(client, TTS_STATE_READY); }