From: Suyeon Hwang Date: Thu, 12 Nov 2020 09:11:42 +0000 (+0900) Subject: Make functions for set current state into client X-Git-Tag: submit/tizen/20210628.060348~49 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F96%2F248496%2F7;p=platform%2Fcore%2Fuifw%2Ftts.git Make functions for set current state into client Most of funtions in tts.c are directly access tts_client_s to set/get current state. This patch provides new functions to access state of client. This function reduces duplicates code and provides safer access to tts_client_s. Change-Id: Ia0eac2d4b43adf9badf49ff4b7bc822a19a4c9b5 Signed-off-by: Suyeon Hwang --- diff --git a/client/tts.c b/client/tts.c index 2629dbfe..ed7464cd 100644 --- a/client/tts.c +++ b/client/tts.c @@ -426,8 +426,7 @@ int tts_destroy(tts_h tts) } } - client->before_state = client->current_state; - client->current_state = TTS_STATE_CREATED; + tts_client_set_current_state(client, TTS_STATE_CREATED); case TTS_STATE_CREATED: /* Unset registered callbacks */ @@ -1161,7 +1160,7 @@ int tts_add_text(tts_h tts, const char* text, const char* language, int voice_ty if (0 != ret) { //LCOV_EXCL_START if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - client->current_state = TTS_STATE_CREATED; + 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"); @@ -1213,7 +1212,7 @@ static void __tts_play_async(void *data) ret = tts_ipc_request_play(client->uid, client->credential); if (0 != ret) { if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - client->current_state = TTS_STATE_CREATED; + 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"); @@ -1240,12 +1239,9 @@ static void __tts_play_async(void *data) return; } - client->before_state = client->current_state; - client->current_state = TTS_STATE_PLAYING; - tts_core_notify_state_changed(client, client->before_state, client->current_state); + tts_core_set_current_state(client, TTS_STATE_PLAYING); SLOG(LOG_DEBUG, TAG_TTSC, "@@@"); - return; } @@ -1360,7 +1356,7 @@ int tts_play(tts_h tts) if (0 != ret) { //LCOV_EXCL_START if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - client->current_state = TTS_STATE_CREATED; + 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"); @@ -1381,14 +1377,12 @@ int tts_play(tts_h tts) } } - client->before_state = client->current_state; - client->current_state = TTS_STATE_PLAYING; - tts_core_notify_state_changed(client, client->before_state, client->current_state); + tts_core_set_current_state(client, TTS_STATE_PLAYING); SLOG(LOG_DEBUG, TAG_TTSC, "@@@"); - return TTS_ERROR_NONE; } + //LCOV_EXCL_START static void __tts_stop_async(void *data) { @@ -1408,7 +1402,7 @@ static void __tts_stop_async(void *data) ret = tts_ipc_request_stop(client->uid); if (0 != ret) { if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - client->current_state = TTS_STATE_CREATED; + 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"); @@ -1435,12 +1429,9 @@ static void __tts_stop_async(void *data) return; } - client->before_state = client->current_state; - client->current_state = TTS_STATE_READY; - tts_core_notify_state_changed(client, client->before_state, client->current_state); + tts_core_set_current_state(client, TTS_STATE_READY); SLOG(LOG_DEBUG, TAG_TTSC, "@@@"); - return; } @@ -1543,7 +1534,7 @@ int tts_stop(tts_h tts) if (0 != ret) { //LCOV_EXCL_START if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - client->current_state = TTS_STATE_CREATED; + 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"); @@ -1564,12 +1555,9 @@ int tts_stop(tts_h tts) } } - client->before_state = client->current_state; - client->current_state = TTS_STATE_READY; - tts_core_notify_state_changed(client, client->before_state, client->current_state); + tts_core_set_current_state(client, TTS_STATE_READY); SLOG(LOG_DEBUG, TAG_TTSC, "@@@"); - return TTS_ERROR_NONE; } //LCOV_EXCL_START @@ -1591,7 +1579,7 @@ static void __tts_pause_async(void *data) ret = tts_ipc_request_pause(client->uid); if (0 != ret) { if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - client->current_state = TTS_STATE_CREATED; + 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"); @@ -1618,12 +1606,9 @@ static void __tts_pause_async(void *data) return; } - client->before_state = client->current_state; - client->current_state = TTS_STATE_PAUSED; - tts_core_notify_state_changed(client, client->before_state, client->current_state); + tts_core_set_current_state(client, TTS_STATE_PAUSED); SLOG(LOG_DEBUG, TAG_TTSC, "@@@"); - return; } @@ -1727,7 +1712,7 @@ int tts_pause(tts_h tts) if (0 != ret) { //LCOV_EXCL_START if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - client->current_state = TTS_STATE_CREATED; + 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"); @@ -1748,12 +1733,9 @@ int tts_pause(tts_h tts) } } - client->before_state = client->current_state; - client->current_state = TTS_STATE_PAUSED; - tts_core_notify_state_changed(client, client->before_state, client->current_state); + tts_core_set_current_state(client, TTS_STATE_PAUSED); SLOG(LOG_DEBUG, TAG_TTSC, "@@@"); - return TTS_ERROR_NONE; } @@ -1800,7 +1782,7 @@ int tts_set_private_data(tts_h tts, const char* key, const char* data) if (0 != ret) { //LCOV_EXCL_START if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - client->current_state = TTS_STATE_CREATED; + 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"); @@ -1864,7 +1846,7 @@ int tts_get_private_data(tts_h tts, const char* key, char** data) if (0 != ret) { //LCOV_EXCL_START if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - client->current_state = TTS_STATE_CREATED; + 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"); @@ -2275,7 +2257,7 @@ int tts_add_pcm(tts_h tts, int event, const void* data, unsigned int data_size, ret = tts_ipc_request_add_pcm(client->uid, event, data, data_size, audio_type, rate); if (0 != ret) { if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - client->current_state = TTS_STATE_CREATED; + 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"); @@ -2349,7 +2331,7 @@ int tts_play_pcm(tts_h tts) ret = tts_ipc_request_play_pcm(client->uid); if (0 != ret) { if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - client->current_state = TTS_STATE_CREATED; + 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"); @@ -2369,12 +2351,9 @@ int tts_play_pcm(tts_h tts) } } - client->before_state = client->current_state; - client->current_state = TTS_STATE_PLAYING; - tts_core_notify_state_changed(client, client->before_state, client->current_state); + tts_core_set_current_state(client, TTS_STATE_PLAYING); SLOG(LOG_DEBUG, TAG_TTSC, "@@@"); - return TTS_ERROR_NONE; } @@ -2427,7 +2406,7 @@ int tts_stop_pcm(tts_h tts) ret = tts_ipc_request_stop_pcm(client->uid); if (0 != ret) { if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - client->current_state = TTS_STATE_CREATED; + 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"); @@ -2447,12 +2426,9 @@ int tts_stop_pcm(tts_h tts) } } - client->before_state = client->current_state; - client->current_state = TTS_STATE_READY; - tts_core_notify_state_changed(client, client->before_state, client->current_state); + tts_core_set_current_state(client, TTS_STATE_READY); SLOG(LOG_DEBUG, TAG_TTSC, "@@@"); - return TTS_ERROR_NONE; } //LCOV_EXCL_STOP diff --git a/client/tts_client.c b/client/tts_client.c index 9900a79e..eb202c68 100644 --- a/client/tts_client.c +++ b/client/tts_client.c @@ -394,6 +394,27 @@ GList* tts_client_get_client_list() } //LCOV_EXCL_STOP +void tts_client_set_current_state(tts_client_s* client, tts_state_e state) +{ + if (NULL == client || false == tts_client_is_valid(client->uid)) { + return; + } + + client->before_state = client->current_state; + client->current_state = state; +} + +int tts_client_get_current_state(tts_client_s* client, tts_state_e* state) +{ + if (NULL == client || false == tts_client_is_valid(client->uid) || NULL == state) { + return TTS_ERROR_INVALID_PARAMETER; + } + + *state = client->current_state; + + return TTS_ERROR_NONE; +} + void tts_client_set_state_changed_cb(tts_client_s* client, tts_state_changed_cb callback, void* user_data) { if (NULL == client || false == tts_client_is_valid(client->uid)) { diff --git a/client/tts_client.h b/client/tts_client.h index f4920b32..8340e61d 100644 --- a/client/tts_client.h +++ b/client/tts_client.h @@ -101,6 +101,9 @@ int tts_client_get_mode_client_count(tts_mode_e mode); GList* tts_client_get_client_list(); +void tts_client_set_current_state(tts_client_s* client, tts_state_e state); +int tts_client_get_current_state(tts_client_s* client, tts_state_e* state); + void tts_client_set_state_changed_cb(tts_client_s* client, tts_state_changed_cb callback, void* user_data); void tts_client_set_utterance_started_cb(tts_client_s* client, tts_utterance_started_cb callback, void* user_data); void tts_client_set_utterance_completed_cb(tts_client_s* client, tts_utterance_completed_cb callback, void* user_data); diff --git a/client/tts_core.c b/client/tts_core.c index ef533f56..779058b2 100644 --- a/client/tts_core.c +++ b/client/tts_core.c @@ -62,6 +62,18 @@ static const char* __tts_get_error_code(tts_error_e err) return NULL; } +static const char* __convert_state(tts_state_e state) +{ + switch (state) { + case TTS_STATE_CREATED: return "Created"; + case TTS_STATE_READY: return "Ready"; + case TTS_STATE_PLAYING: return "Playing"; + case TTS_STATE_PAUSED: return "Paused"; + } + + return "Invalid state"; +} + static char* __get_engine_appid(int mode) { char* appid = NULL; appid = (char*)calloc(sizeof(char), 256); @@ -121,7 +133,7 @@ static inline void __client_state_changed_cb(tts_client_s* client, tts_state_e b void* data = tts_client_get_state_changed_user_data(client); if (NULL != callback) { - SLOG(LOG_DEBUG, TAG_TTSC, "Notify state changed"); + SLOG(LOG_DEBUG, TAG_TTSC, "State changed data : before_state(%s) curret_state(%s)", __convert_state(before_state), __convert_state(current_state)); tts_client_use_callback(client); callback(tts_client_get_handle(client), before_state, current_state, data); tts_client_not_use_callback(client); @@ -344,7 +356,7 @@ static void __start_reprepare_thread(void* data, Ecore_Thread* thread) cnt++; /* Checking thread is canceled or not */ - if (ecore_thread_check(thread)) { + if (ecore_thread_check(g_reprepare_thread)) { SLOG(LOG_WARN, TAG_TTSC, "[WARNING] client thread is canceled. Exit"); return; } @@ -356,7 +368,7 @@ static void __start_reprepare_thread(void* data, Ecore_Thread* thread) usleep(200000); /* Checking thread is canceled or not */ - if (ecore_thread_check(thread)) { + if (ecore_thread_check(g_reprepare_thread)) { SLOG(LOG_WARN, TAG_TTSC, "[WARNING] client thread is canceled. Exit"); return; } @@ -553,9 +565,7 @@ static Eina_Bool __prepare_sync_cb(tts_client_s* client) } // TODO: make function duplicated block - client->before_state = client->current_state; - client->current_state = TTS_STATE_READY; - __client_state_changed_cb(client, client->before_state, client->current_state); + tts_core_set_current_state(client, TTS_STATE_READY); SLOG(LOG_DEBUG, TAG_TTSC, "@@@"); return EINA_FALSE; @@ -645,7 +655,6 @@ int tts_core_notify_state_changed(tts_client_s* client, tts_state_e before_state return TTS_ERROR_INVALID_PARAMETER; } - SLOG(LOG_DEBUG, TAG_TTSC, "State changed data : before_state(%d) curret_state(%d)", before_state, current_state); __client_state_changed_cb(client, before_state, current_state); return TTS_ERROR_NONE; @@ -659,11 +668,8 @@ int tts_core_notify_state_changed_async(tts_client_s* client, tts_state_e before return TTS_ERROR_INVALID_PARAMETER; } - SLOG(LOG_DEBUG, TAG_TTSC, "State changed data : before_state(%d) curret_state(%d)", before_state, current_state); - client->before_state = before_state; - client->current_state = current_state; - SLOG(LOG_DEBUG, TAG_TTSC, "Notify state changed asynchronously"); + tts_client_set_current_state(client, current_state); if (NULL != client->notify_state_timer) { ecore_timer_del(client->notify_state_timer); } @@ -819,6 +825,27 @@ int tts_core_notify_engine_changed(tts_client_s* client, const char* engine_id, return TTS_ERROR_NONE; } +int tts_core_set_current_state(tts_client_s* client, tts_state_e state) +{ + /* check handle */ + if (NULL == client || false == tts_client_is_valid(client)) { + SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Handle is not valid."); + return TTS_ERROR_INVALID_PARAMETER; + } + + tts_state_e before_state = TTS_STATE_CREATED; + if (0 != tts_client_get_current_state(client, &before_state)) { + SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get before state."); + return TTS_ERROR_OPERATION_FAILED; + } + + SLOG(LOG_DEBUG, TAG_TTSC, "State changed to (%s).", __convert_state(state)); + tts_client_set_current_state(client, state); + __client_state_changed_cb(client, before_state, state); + + return TTS_ERROR_NONE; +} + int tts_core_receive_hello(int uid, int ret, int credential_needed) { tts_client_s* client = tts_client_get_by_uid(uid); @@ -947,7 +974,7 @@ int tts_core_unprepare(tts_client_s* client, bool is_screen_reader_on) if (0 != ret) { //LCOV_EXCL_START if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) { - client->current_state = TTS_STATE_CREATED; + 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; @@ -969,11 +996,7 @@ int tts_core_unprepare(tts_client_s* client, bool is_screen_reader_on) } while (0 != ret); } - // TODO: make function like tts_core_set_state() - client->before_state = client->current_state; - client->current_state = TTS_STATE_CREATED; - __client_state_changed_cb(client, client->before_state, client->current_state); - // TODO: make function like tts_core_set_state() + tts_core_set_current_state(client, TTS_STATE_CREATED); return TTS_ERROR_NONE; } @@ -994,7 +1017,7 @@ int tts_core_reprepare() while (NULL != iter) { tts_client_s* client = iter->data; if (NULL != client && tts_client_is_valid(client->uid)) { - client->current_state = TTS_STATE_CREATED; + tts_client_set_current_state(client, TTS_STATE_CREATED); client->reason = TTS_ERROR_NONE; } diff --git a/client/tts_core.h b/client/tts_core.h index a8fa4fd6..7e52df57 100644 --- a/client/tts_core.h +++ b/client/tts_core.h @@ -31,6 +31,8 @@ int tts_core_notify_error_async(tts_client_s* client, int utt_id, tts_error_e re int tts_core_notify_default_voice_changed(tts_client_s* client, const char* before_lang, int before_voice_type, const char* language, int voice_type); int tts_core_notify_engine_changed(tts_client_s* client, const char* engine_id, const char* language, int voice_type, bool need_credential); +int tts_core_set_current_state(tts_client_s* client, tts_state_e state); + // called by tts.c int tts_core_initialize(); int tts_core_deinitialize();