From e85c5c33aa5afd4a0b67a8543a435e23f7485a39 Mon Sep 17 00:00:00 2001 From: Suyeon Hwang Date: Fri, 13 Jan 2023 17:19:26 +0900 Subject: [PATCH] Try to prepare when the API is invoked on service out - Requirement: To reduce the resource for repreparing, clients need to reprepare when the API is invoked. - Contents: This patch changes the logic about service reset and repreparing. Through this patch, all clients just set service out flag when the service reset occurs. Plus, each clients will try to reprepare when the API is invoked by the app. In previous code, all clients tried to reprepare at the same time when the service reset occurs. However, this behavior needed to too much resource for repreparing in short time. This patch intends to decrease the resource requirements for repreparing by spreading the reprepare requests. Change-Id: I4fd7b8905975692efba973a7e6c2423d604b4624 Signed-off-by: Suyeon Hwang --- client/tts_client.c | 18 +++++++++++ client/tts_client.h | 4 +++ client/tts_core.c | 86 +++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 86 insertions(+), 22 deletions(-) diff --git a/client/tts_client.c b/client/tts_client.c index 2ff75a1..8cfc81b 100644 --- a/client/tts_client.c +++ b/client/tts_client.c @@ -480,6 +480,24 @@ bool tts_client_is_reprepared(tts_client_s* client) return client->reprepared; } +void tts_client_set_service_out(tts_client_s* client, bool is_service_out) +{ + if (false == tts_client_is_valid_client(client)) { + return; + } + + client->service_out = is_service_out; +} + +bool tts_client_is_service_out(tts_client_s* client) +{ + if (false == tts_client_is_valid_client(client)) { + return false; + } + + return client->service_out; +} + void tts_client_set_mode(tts_client_s* client, tts_mode_e mode) { if (false == tts_client_is_valid_client(client)) { diff --git a/client/tts_client.h b/client/tts_client.h index c9a150c..4426775 100644 --- a/client/tts_client.h +++ b/client/tts_client.h @@ -78,6 +78,7 @@ typedef struct { Ecore_Timer* hello_timer; bool start_listening; bool reprepared; + bool service_out; /* options */ char* credential; @@ -126,6 +127,9 @@ bool tts_client_is_listening_started(unsigned int uid); void tts_client_set_reprepared(tts_client_s* client, bool is_reprepared); bool tts_client_is_reprepared(tts_client_s* client); +void tts_client_set_service_out(tts_client_s* client, bool is_service_out); +bool tts_client_is_service_out(tts_client_s* client); + void tts_client_set_mode(tts_client_s* client, tts_mode_e mode); tts_mode_e tts_client_get_mode(tts_client_s* client); diff --git a/client/tts_core.c b/client/tts_core.c index cc6fc0b..24ac8a5 100644 --- a/client/tts_core.c +++ b/client/tts_core.c @@ -1029,6 +1029,7 @@ int tts_core_receive_hello(unsigned int uid, int ret, tts_service_state_e servic return TTS_ERROR_OPERATION_FAILED; } + tts_client_set_service_out(client, false); tts_core_notify_state_changed(client, TTS_STATE_READY); tts_service_state_e before_state = tts_client_get_current_service_state(client); @@ -1097,6 +1098,7 @@ int tts_core_prepare_sync(tts_client_s* client) return TTS_ERROR_OPERATION_FAILED; } + tts_client_set_service_out(client, false); tts_core_notify_state_changed(client, TTS_STATE_READY); tts_service_state_e before_state = tts_client_get_current_service_state(client); @@ -1117,14 +1119,19 @@ int tts_core_unprepare(tts_client_s* client) client->hello_timer = NULL; } - int ret = -1; + int ret = tts_ipc_stop_listening(uid); + if (0 != ret) { + SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to remove match : %s", tts_core_covert_error_code(ret)); + } + + if (tts_client_is_service_out(client)) { + SLOG(LOG_INFO, TAG_TTSC, "[INFO] Service is already out. Do not need to finalize"); + tts_client_set_service_out(client, false); + return TTS_ERROR_NONE; + } + if (false == tts_core_check_screen_reader(client)) { SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Do not request finalize. Handled by screen reader"); - - ret = tts_ipc_stop_listening(uid); - if (0 != ret) { - SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to remove match : %s", tts_core_covert_error_code(ret)); - } } else { SLOG(LOG_INFO, TAG_TTSC, "[INFO] Request finalize"); @@ -1186,37 +1193,62 @@ int tts_core_foreach_supported_voices(tts_client_s* client, const char* engine_i return TTS_ERROR_NONE; } +static void set_service_out_for_each_client(gpointer data, gpointer user_data) +{ + tts_client_s *client = (tts_client_s *)data; + if (false == tts_client_is_valid_client(client)) { + SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Client is not valid"); + return; + } + + const tts_state_e state = tts_client_get_current_state(client); + if (state == TTS_STATE_INVALID || state == TTS_STATE_CREATED) { + SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Client is not connected yet. state(%d)", state); + return; + } + + tts_client_set_service_out(client, true); + tts_client_set_current_state(client, TTS_STATE_READY); + tts_core_notify_error_async(client, TTS_ERROR_SERVICE_RESET, -1, "Daemon Reset"); + + const tts_service_state_e before_state = tts_client_get_current_service_state(client); + tts_core_notify_service_state_changed(client, before_state, TTS_SERVICE_STATE_BLOCKED); +} + int tts_core_handle_service_reset() { + SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset"); + GList* client_list = tts_client_get_client_list(); RETVM_IF(NULL == client_list, TTS_ERROR_OPERATION_FAILED, "[ERROR] Fail to get client list"); - if (g_list_length(client_list) > 0) { - GList *iter = g_list_first(client_list); - while (NULL != iter) { - tts_client_s *client = iter->data; - if (false == tts_client_is_valid_client(client)) { - SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Client is not valid"); - } else { - tts_core_notify_error_async(client, TTS_ERROR_SERVICE_RESET, -1, "Daemon Reset"); - } + SLOG(LOG_INFO, TAG_TTSC, "[INFO] Set service out to all clients"); + g_list_foreach(client_list, set_service_out_for_each_client, NULL); + g_list_free(client_list); - iter = g_list_next(iter); - } - } + return TTS_ERROR_NONE; +} - g_list_free(client_list); +static inline void reprepare_on_demand(tts_client_s* client) +{ + if (false == tts_client_is_service_out(client)) { + return; + } - SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset"); - tts_core_reprepare(); + const unsigned int uid = tts_client_get_uid(client); + SLOG(LOG_INFO, TAG_TTSC, "[INFO] Client is out of service. Try to reprepare. uid(%u)", uid); - return TTS_ERROR_NONE; + int ret = tts_core_prepare_sync(client); + if (TTS_ERROR_NONE != ret) { + SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Client fails to prepare. uid(%u)", uid); + } } int tts_core_add_text(tts_client_s* client, const char* text, const char* language, int voice_type, int speed, int* utt_id) { RETVM_IF(NULL == text || NULL == utt_id, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Parameter is invalid."); RETVM_IF(false == tts_client_is_valid_client(client), TTS_ERROR_INVALID_PARAMETER, "[ERROR] Client is invalid."); + reprepare_on_demand(client); tts_client_set_repeat_text(client, text); @@ -1235,6 +1267,7 @@ int tts_core_add_text(tts_client_s* client, const char* text, const char* langua int tts_core_play(tts_client_s* client) { RETVM_IF(false == tts_client_is_valid_client(client), TTS_ERROR_INVALID_PARAMETER, "[ERROR] Client is invalid."); + reprepare_on_demand(client); return __request_play(client); } @@ -1242,6 +1275,7 @@ int tts_core_play(tts_client_s* client) int tts_core_stop(tts_client_s* client) { RETVM_IF(false == tts_client_is_valid_client(client), TTS_ERROR_INVALID_PARAMETER, "[ERROR] Client is invalid."); + reprepare_on_demand(client); unsigned int uid = tts_client_get_uid(client); int ret = -1; @@ -1264,6 +1298,7 @@ int tts_core_stop(tts_client_s* client) int tts_core_pause(tts_client_s* client) { RETVM_IF(false == tts_client_is_valid_client(client), TTS_ERROR_INVALID_PARAMETER, "[ERROR] Client is invalid."); + reprepare_on_demand(client); unsigned int uid = tts_client_get_uid(client); int ret = -1; @@ -1288,6 +1323,7 @@ int tts_core_repeat(tts_client_s* client, char** text_repeat, int* utt_id) { RETVM_IF(NULL == text_repeat || NULL == utt_id, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Parameter is invalid."); RETVM_IF(false == tts_client_is_valid_client(client), TTS_ERROR_INVALID_PARAMETER, "[ERROR] Client is invalid."); + reprepare_on_demand(client); char* repeat_text = tts_client_get_repeat_text(client); if (NULL == repeat_text) { @@ -1321,6 +1357,7 @@ int tts_core_repeat(tts_client_s* client, char** text_repeat, int* utt_id) int tts_core_add_pcm(tts_client_s* client, int event, const void* data, unsigned int data_size, int audio_type, int rate) { RETVM_IF(false == tts_client_is_valid_client(client), TTS_ERROR_INVALID_PARAMETER, "[ERROR] Client is invalid."); + reprepare_on_demand(client); unsigned int uid = tts_client_get_uid(client); int ret = -1; @@ -1344,6 +1381,7 @@ int tts_core_add_pcm(tts_client_s* client, int event, const void* data, unsigned int tts_core_play_pcm(tts_client_s* client) { RETVM_IF(false == tts_client_is_valid_client(client), TTS_ERROR_INVALID_PARAMETER, "[ERROR] Client is invalid."); + reprepare_on_demand(client); unsigned int uid = tts_client_get_uid(client); int ret = -1; @@ -1367,6 +1405,7 @@ int tts_core_play_pcm(tts_client_s* client) int tts_core_stop_pcm(tts_client_s* client) { RETVM_IF(false == tts_client_is_valid_client(client), TTS_ERROR_INVALID_PARAMETER, "[ERROR] Client is invalid."); + reprepare_on_demand(client); unsigned int uid = tts_client_get_uid(client); int ret = -1; @@ -1390,6 +1429,7 @@ int tts_core_stop_pcm(tts_client_s* client) int tts_core_set_private_data(tts_client_s* client, const char* key, const char* data) { RETVM_IF(false == tts_client_is_valid_client(client), TTS_ERROR_INVALID_PARAMETER, "[ERROR] Client is invalid."); + reprepare_on_demand(client); unsigned int uid = tts_client_get_uid(client); int ret = -1; @@ -1413,6 +1453,7 @@ int tts_core_set_private_data(tts_client_s* client, const char* key, const char* int tts_core_get_private_data(tts_client_s* client, const char* key, char** data) { RETVM_IF(false == tts_client_is_valid_client(client), TTS_ERROR_INVALID_PARAMETER, "[ERROR] Client is invalid."); + reprepare_on_demand(client); unsigned int uid = tts_client_get_uid(client); int ret = -1; @@ -1441,6 +1482,7 @@ int tts_core_get_private_data(tts_client_s* client, const char* key, char** data int tts_core_get_service_state(tts_client_s* client, tts_service_state_e* service_state) { RETVM_IF(false == tts_client_is_valid_client(client), TTS_ERROR_INVALID_PARAMETER, "[ERROR] Client is invalid."); + reprepare_on_demand(client); unsigned int uid = tts_client_get_uid(client); int ret = -1; -- 2.7.4