Load engine when the first client is connected 31/279131/3
authorSuyeon Hwang <stom.hwang@samsung.com>
Mon, 1 Aug 2022 05:27:54 +0000 (14:27 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Wed, 3 Aug 2022 01:44:54 +0000 (10:44 +0900)
- Issue:
Engine loads model when the engine is initializing, so engine always has
to load model, even if engine application does not need to load model in
some case.

- Solution:
To solve this problem, first, we need to change the timing for loading
engine. This is because engine has no information about clients when
engine is initialzing. Without any clients information, engine can not
decide whether the engine needs to load model or not.
This patch changes the timing for loading engine to when the first client
information is received. Through this patch, engine already has client
information when engine has to decide model loading.

Change-Id: Idfb67c1209dd17da2a1298ff5b82e4fec2cde18e
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
server/ttsd_engine_agent.c
server/ttsd_engine_agent.h
server/ttsd_server.c

index 799a65b..37046ac 100644 (file)
@@ -72,9 +72,6 @@ static void __free_voice_list(GList* voice_list);
 
 static int ttsd_print_voicelist();
 
-/** Get engine info */
-static int __internal_get_engine_info(ttse_request_callback_s* callback);
-
 static const char* __ttsd_get_engine_error_code(ttse_error_e err)
 {
        switch (err) {
@@ -129,13 +126,94 @@ inline static int __check_engine_initialized_and_loaded()
        return TTSD_ERROR_NONE;
 }
 
-int ttsd_engine_agent_init()
+static bool __is_callback_valid(ttse_request_callback_s *callback)
+{
+       if (NULL == callback) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Invalid engine");
+               return false;
+       }
+
+       if (NULL == callback->get_info
+               || NULL == callback->initialize || NULL == callback->deinitialize
+               || NULL == callback->foreach_voices || NULL == callback->is_valid_voice
+               || NULL == callback->set_pitch
+               || NULL == callback->load_voice || NULL == callback->unload_voice
+               || NULL == callback->start_synth || NULL == callback->cancel_synth
+               || NULL == callback->check_app_agreed || NULL == callback->need_app_credential) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Invalid engine");
+               return false;
+       }
+
+       return true;
+}
+
+static int __set_engine_callback(ttse_request_callback_s* callback, ttsengine_info_s* engine_info)
+{
+       SLOG(LOG_DEBUG, tts_tag(), "[Engine Agent DEBUG] internal get engine info");
+
+       if (0 != callback->get_info(&(engine_info->engine_uuid), &(engine_info->engine_name), &(engine_info->engine_setting_path), &(engine_info->use_network))) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Fail to get engine info");
+               return TTSD_ERROR_ENGINE_NOT_FOUND;
+       }
+
+       if (NULL != engine_info->engine_path) {
+               free(engine_info->engine_path);
+               engine_info->engine_path = NULL;
+       }
+       engine_info->engine_path = strdup("empty");
+       engine_info->is_loaded = false;
+
+       if (NULL != engine_info->callbacks) {
+               free(engine_info->callbacks);
+               engine_info->callbacks = NULL;
+       }
+
+       engine_info->callbacks = (tts_engine_callback_s*)calloc(1, sizeof(tts_engine_callback_s));
+       if (NULL == engine_info->callbacks) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Fail to allocate memory");
+               return TTSD_ERROR_OUT_OF_MEMORY;
+       }
+
+       engine_info->callbacks->get_info = callback->get_info;
+       engine_info->callbacks->initialize = callback->initialize;
+       engine_info->callbacks->deinitialize = callback->deinitialize;
+       engine_info->callbacks->foreach_voices = callback->foreach_voices;
+       engine_info->callbacks->is_valid_voice = callback->is_valid_voice;
+       engine_info->callbacks->set_pitch = callback->set_pitch;
+       engine_info->callbacks->load_voice = callback->load_voice;
+       engine_info->callbacks->unload_voice = callback->unload_voice;
+       engine_info->callbacks->start_synth = callback->start_synth;
+       engine_info->callbacks->cancel_synth = callback->cancel_synth;
+       engine_info->callbacks->check_app_agreed = callback->check_app_agreed;
+       engine_info->callbacks->need_app_credential = callback->need_app_credential;
+
+       engine_info->callbacks->private_data_set = NULL;
+       engine_info->callbacks->private_data_requested = NULL;
+
+       SLOG(LOG_DEBUG, tts_tag(), "--- Valid Engine ---");
+       SLOG(LOG_DEBUG, tts_tag(), "Engine uuid : %s", engine_info->engine_uuid);
+       SLOG(LOG_DEBUG, tts_tag(), "Engine name : %s", engine_info->engine_name);
+       SLOG(LOG_DEBUG, tts_tag(), "Engine path : %s", engine_info->engine_path);
+       SLOG(LOG_DEBUG, tts_tag(), "Engine setting path : %s", engine_info->engine_setting_path);
+       SLOG(LOG_DEBUG, tts_tag(), "Use network : %s", engine_info->use_network ? "true" : "false");
+       SLOG(LOG_DEBUG, tts_tag(), "--------------------");
+       SLOG(LOG_DEBUG, tts_tag(), "  ");
+
+       return TTSD_ERROR_NONE;
+}
+
+int ttsd_engine_agent_init(ttse_request_callback_s *callback)
 {
        if (__is_agent_initialized()) {
                SLOG(LOG_WARN, tts_tag(), "[Engine Agent] Already initialized");
                return TTSD_ERROR_NONE;
        }
 
+       if (false == __is_callback_valid(callback)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Invalid engine");
+               return TTSD_ERROR_ENGINE_NOT_FOUND;
+       }
+
        ttsengine_info_s* temp;
        temp = (ttsengine_info_s*)calloc(1, sizeof(ttsengine_info_s));
        if (NULL == temp) {
@@ -162,9 +240,16 @@ int ttsd_engine_agent_init()
                temp->default_pitch = TTS_PITCH_NORMAL;
        }
 
-       temp->is_loaded = false;
-       g_engine_info = temp;
+       /* Get current engine info */
+       int ret = __set_engine_callback(callback, temp);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Fail to get engine info");
+               free(temp);
+               temp = NULL;
+               return ret;
+       }
 
+       g_engine_info = temp;
        g_agent_init = true;
 
        SLOG(LOG_DEBUG, tts_tag(), "[Engine Agent SUCCESS] Initialize Engine Agent");
@@ -299,26 +384,10 @@ static int __update_voice_list()
        return TTSD_ERROR_NONE;
 }
 
-int ttsd_engine_agent_load_current_engine(ttse_request_callback_s* callback)
+int ttsd_engine_agent_load_current_engine()
 {
        SLOG(LOG_DEBUG, tts_tag(), "[Engine Agent DEBUG] load current engine START");
 
-       if (NULL == callback) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Invalid engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
-       }
-
-       if (NULL == callback->get_info
-               || NULL == callback->initialize || NULL == callback->deinitialize
-               || NULL == callback->foreach_voices || NULL == callback->is_valid_voice
-               || NULL == callback->set_pitch
-               || NULL == callback->load_voice || NULL == callback->unload_voice
-               || NULL == callback->start_synth || NULL == callback->cancel_synth
-               || NULL == callback->check_app_agreed || NULL == callback->need_app_credential) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Invalid engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
-       }
-
        if (false == __is_agent_initialized()) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
                return TTSD_ERROR_INVALID_STATE;
@@ -330,15 +399,8 @@ int ttsd_engine_agent_load_current_engine(ttse_request_callback_s* callback)
                return TTSD_ERROR_NONE;
        }
 
-       /* Get current engine info */
-       int ret = __internal_get_engine_info(callback);
-       if (0 != ret) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Fail to get engine info");
-               return ret;
-       }
-
        /* Initialize engine */
-       ret = g_engine_info->callbacks->initialize();
+       int ret = g_engine_info->callbacks->initialize();
        if (0 != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Fail to initialize current engine : %s", __ttsd_get_engine_error_code(ret));
                return TTSD_ERROR_OPERATION_FAILED;
@@ -1162,78 +1224,13 @@ int ttsd_print_voicelist()
        return TTSD_ERROR_NONE;
 }
 
-static int __internal_get_engine_info(ttse_request_callback_s* callback)
-{
-       SLOG(LOG_DEBUG, tts_tag(), "[Engine Agent DEBUG] internal get engine info");
-
-       if (NULL == callback) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Invalid engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
-       }
-
-       if (NULL == callback->get_info) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Invalid engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
-       }
-
-       if (0 != callback->get_info(&(g_engine_info->engine_uuid), &(g_engine_info->engine_name), &(g_engine_info->engine_setting_path), &(g_engine_info->use_network))) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Fail to get engine info");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
-       }
-
-       if (NULL != g_engine_info->engine_path) {
-               free(g_engine_info->engine_path);
-               g_engine_info->engine_path = NULL;
-       }
-       g_engine_info->engine_path = strdup("empty");
-       g_engine_info->is_loaded = false;
-
-       if (NULL != g_engine_info->callbacks) {
-               free(g_engine_info->callbacks);
-               g_engine_info->callbacks = NULL;
-       }
-       g_engine_info->callbacks = (tts_engine_callback_s*)calloc(1, sizeof(tts_engine_callback_s));
-       if (NULL == g_engine_info->callbacks) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Fail to allocate memory");
-               return TTSD_ERROR_OUT_OF_MEMORY;
-       }
-
-       g_engine_info->callbacks->get_info = callback->get_info;
-       g_engine_info->callbacks->initialize = callback->initialize;
-       g_engine_info->callbacks->deinitialize = callback->deinitialize;
-       g_engine_info->callbacks->foreach_voices = callback->foreach_voices;
-       g_engine_info->callbacks->is_valid_voice = callback->is_valid_voice;
-       g_engine_info->callbacks->set_pitch = callback->set_pitch;
-       g_engine_info->callbacks->load_voice = callback->load_voice;
-       g_engine_info->callbacks->unload_voice = callback->unload_voice;
-       g_engine_info->callbacks->start_synth = callback->start_synth;
-       g_engine_info->callbacks->cancel_synth = callback->cancel_synth;
-       g_engine_info->callbacks->check_app_agreed = callback->check_app_agreed;
-       g_engine_info->callbacks->need_app_credential = callback->need_app_credential;
-
-       g_engine_info->callbacks->private_data_set = NULL;
-       g_engine_info->callbacks->private_data_requested = NULL;
-
-       SLOG(LOG_DEBUG, tts_tag(), "--- Valid Engine ---");
-       SLOG(LOG_DEBUG, tts_tag(), "Engine uuid : %s", g_engine_info->engine_uuid);
-       SLOG(LOG_DEBUG, tts_tag(), "Engine name : %s", g_engine_info->engine_name);
-       SLOG(LOG_DEBUG, tts_tag(), "Engine path : %s", g_engine_info->engine_path);
-       SLOG(LOG_DEBUG, tts_tag(), "Engine setting path : %s", g_engine_info->engine_setting_path);
-       SLOG(LOG_DEBUG, tts_tag(), "Use network : %s", g_engine_info->use_network ? "true" : "false");
-       SLOG(LOG_DEBUG, tts_tag(), "--------------------");
-       SLOG(LOG_DEBUG, tts_tag(), "  ");
-
-       return TTSD_ERROR_NONE;
-}
-
 
 /** Set callbacks of the current engine */
 int ttsd_engine_agent_set_private_data_set_cb(ttse_private_data_set_cb callback)
 {
-       int ret = __check_engine_initialized_and_loaded();
-       if (TTSD_ERROR_NONE != ret) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
-               return ret;
+       if (false == __is_agent_initialized()) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
+               return TTSD_ERROR_INVALID_STATE;
        }
 
        g_engine_info->callbacks->private_data_set = callback;
@@ -1243,10 +1240,9 @@ int ttsd_engine_agent_set_private_data_set_cb(ttse_private_data_set_cb callback)
 
 int ttsd_engine_agent_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
 {
-       int ret = __check_engine_initialized_and_loaded();
-       if (TTSD_ERROR_NONE != ret) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
-               return ret;
+       if (false == __is_agent_initialized()) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
+               return TTSD_ERROR_INVALID_STATE;
        }
 
        g_engine_info->callbacks->private_data_requested = callback;
index b8c93f6..e258448 100644 (file)
@@ -28,7 +28,7 @@ extern "C" {
 */
 
 /** Init engine agent */
-int ttsd_engine_agent_init();
+int ttsd_engine_agent_init(ttse_request_callback_s *callback);
 
 /** Release engine agent */
 int ttsd_engine_agent_release();
index d47a778..76c9659 100644 (file)
@@ -573,16 +573,11 @@ int ttsd_initialize(ttse_request_callback_s *callback)
        }
 
        /* Engine Agent initialize */
-       if (0 != ttsd_engine_agent_init()) {
+       if (TTSD_ERROR_NONE != ttsd_engine_agent_init(callback)) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to engine agent initialize.");
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       if (0 != ttsd_engine_agent_load_current_engine(callback)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
-               return TTSD_ERROR_OPERATION_FAILED;
-       }
-
        ttsd_data_set_synth_control(TTSD_SYNTHESIS_CONTROL_EXPIRED);
        ttsd_config_set_screen_reader_callback(__screen_reader_changed_cb);
 
@@ -686,8 +681,20 @@ int ttsd_server_initialize(int pid, unsigned int uid, ttsd_mode_e mode, tts_ipc_
                return TTSD_ERROR_NONE;
        }
 
+       if (0 != ttsd_data_new_client(pid, uid, mode, method)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
+               return TTSD_ERROR_OPERATION_FAILED;
+       }
+
+       if (TTSD_ERROR_NONE != ttsd_engine_agent_load_current_engine()) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to load current engine");
+               ttsd_data_delete_client(uid);
+               return TTSD_ERROR_OPERATION_FAILED;
+       }
+
        if (0 != ttsd_engine_agent_is_credential_needed(uid, credential_needed)) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get credential necessity");
+               ttsd_data_delete_client(uid);
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
@@ -701,6 +708,7 @@ int ttsd_server_initialize(int pid, unsigned int uid, ttsd_mode_e mode, tts_ipc_
                        SLOG(LOG_ERROR, tts_tag(), "Server ERROR] Fail to check app agreed");
                        if (!appid)
                                free(appid);
+                       ttsd_data_delete_client(uid);
                        return TTSD_ERROR_OPERATION_FAILED;
                }
                if (!appid)
@@ -708,15 +716,11 @@ int ttsd_server_initialize(int pid, unsigned int uid, ttsd_mode_e mode, tts_ipc_
 
                if (false == is_agreed) {
                        SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] App is not agreed");
+                       ttsd_data_delete_client(uid);
                        return TTSD_ERROR_PERMISSION_DENIED;
                }
        }
 
-       if (0 != ttsd_data_new_client(pid, uid, mode, method)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to add client info");
-               return TTSD_ERROR_OPERATION_FAILED;
-       }
-
        return TTSD_ERROR_NONE;
 }