Try to prepare when dbus call is requested earlier while a tts engine is reset
[platform/core/uifw/tts.git] / client / tts.c
index e6385b4..f08165b 100644 (file)
@@ -687,6 +687,45 @@ int tts_prepare(tts_h tts)
        return TTS_ERROR_NONE;
 }
 
+int tts_prepare_sync(tts_h tts)
+{
+       if (0 != __tts_get_feature_enabled()) {
+               return TTS_ERROR_NOT_SUPPORTED;
+       }
+
+       SLOG(LOG_INFO, TAG_TTSC, "@@@ Prepare TTS");
+
+       tts_client_s* client = tts_client_get(tts);
+
+       /* check handle */
+       if (NULL == client) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
+               SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
+       /* check state */
+       if (client->current_state != TTS_STATE_CREATED) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'CREATED'");
+               SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
+               return TTS_ERROR_INVALID_STATE;
+       }
+
+       int cnt = 0;
+       while (EINA_TRUE == __tts_connect_daemon(NULL) && TTS_CONNECTION_RETRY_COUNT > cnt) {
+               cnt++;
+       }
+
+       SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
+
+       if (TTS_CONNECTION_RETRY_COUNT == cnt) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to connect daemon");
+               return TTS_ERROR_OPERATION_FAILED;
+       }
+
+       return TTS_ERROR_NONE;
+}
+
 int tts_unprepare(tts_h tts)
 {
        if (0 != __tts_get_feature_enabled()) {
@@ -721,11 +760,17 @@ int tts_unprepare(tts_h tts)
                g_screen_reader = (bool)screen_reader;
        }
 
+       bool is_prepared = false;
        if (!(false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode)) {
                do {
                        ret = tts_dbus_request_finalize(client->uid);
                        if (0 != ret) {
-                               if (TTS_ERROR_TIMED_OUT != ret) {
+                               if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
+                                       if (0 == tts_prepare_sync(tts)) {
+                                               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));
                                        break;
                                } else {
@@ -1104,10 +1149,16 @@ int tts_add_text(tts_h tts, const char* text, const char* language, int voice_ty
        /* do request */
        int ret = -1;
        int count = 0;
+       bool is_prepared = false;
        while (0 != ret) {
                ret = tts_dbus_request_add_text(client->uid, text, temp, voice_type, speed, client->current_utt_id, client->credential);
                if (0 != ret) {
-                       if (TTS_ERROR_TIMED_OUT != ret) {
+                       if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
+                               if (0 == tts_prepare_sync(tts)) {
+                                       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));
                                break;
                        } else {
@@ -1147,10 +1198,16 @@ static void __tts_play_async(void *data)
 
        int ret = -1;
        int count = 0;
+       bool is_prepared = false;
        while (0 != ret) {
                ret = tts_dbus_request_play(client->uid, client->credential);
                if (0 != ret) {
-                       if (TTS_ERROR_TIMED_OUT != ret) {
+                       if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
+                               if (0 == tts_prepare_sync(tts)) {
+                                       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));
                                break;
                        } else {
@@ -1274,10 +1331,16 @@ int tts_play(tts_h tts)
 
        int ret = -1;
        int count = 0;
+       bool is_prepared = false;
        while (0 != ret) {
                ret = tts_dbus_request_play(client->uid, client->credential);
                if (0 != ret) {
-                       if (TTS_ERROR_TIMED_OUT != ret) {
+                       if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
+                               if (0 == tts_prepare_sync(tts)) {
+                                       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 {
@@ -1320,10 +1383,16 @@ static void __tts_stop_async(void *data)
 
        int ret = -1;
        int count = 0;
+       bool is_prepared = false;
        while (0 != ret) {
                ret = tts_dbus_request_stop(client->uid);
                if (0 != ret) {
-                       if (TTS_ERROR_TIMED_OUT != ret) {
+                       if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
+                               if (0 == tts_prepare_sync(tts)) {
+                                       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));
                                break;
                        } else {
@@ -1436,10 +1505,16 @@ int tts_stop(tts_h tts)
 
        int ret = -1;
        int count = 0;
+       bool is_prepared = false;
        while (0 != ret) {
                ret = tts_dbus_request_stop(client->uid);
                if (0 != ret) {
-                       if (TTS_ERROR_TIMED_OUT != ret) {
+                       if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
+                               if (0 == tts_prepare_sync(tts)) {
+                                       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 {
@@ -1482,10 +1557,16 @@ static void __tts_pause_async(void *data)
 
        int ret = -1;
        int count = 0;
+       bool is_prepared = false;
        while (0 != ret) {
                ret = tts_dbus_request_pause(client->uid);
                if (0 != ret) {
-                       if (TTS_ERROR_TIMED_OUT != ret) {
+                       if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
+                               if (0 == tts_prepare_sync(tts)) {
+                                       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));
                                break;
                        } else {
@@ -1600,10 +1681,16 @@ int tts_pause(tts_h tts)
 
        int ret = -1;
        int count = 0;
+       bool is_prepared = false;
        while (0 != ret) {
                ret = tts_dbus_request_pause(client->uid);
                if (0 != ret) {
-                       if (TTS_ERROR_TIMED_OUT != ret) {
+                       if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
+                               if (0 == tts_prepare_sync(tts)) {
+                                       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 {
@@ -1670,10 +1757,16 @@ int tts_set_private_data(tts_h tts, const char* key, const char* data)
 
        int ret = -1;
        int count = 0;
+       bool is_prepared = false;
        while (0 != ret) {
                ret = tts_dbus_request_set_private_data(client->uid, key, data);
                if (0 != ret) {
-                       if (TTS_ERROR_TIMED_OUT != ret) {
+                       if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
+                               if (0 == tts_prepare_sync(tts)) {
+                                       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 {
@@ -1725,10 +1818,16 @@ int tts_get_private_data(tts_h tts, const char* key, char** data)
 
        int ret = -1;
        int count = 0;
+       bool is_prepared = false;
        while (0 != ret) {
                ret = tts_dbus_request_get_private_data(client->uid, key, data);
                if (0 != ret) {
-                       if (TTS_ERROR_TIMED_OUT != ret) {
+                       if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
+                               if (0 == tts_prepare_sync(tts)) {
+                                       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 {
@@ -2371,10 +2470,16 @@ int tts_add_pcm(tts_h tts, int event, const void* data, unsigned int data_size,
 
        int ret = -1;
        int count = 0;
+       bool is_prepared = false;
        while (0 != ret) {
                ret = tts_dbus_request_add_pcm(client->uid, event, data, data_size, audio_type, rate);
                if (0 != ret) {
-                       if (TTS_ERROR_TIMED_OUT != ret) {
+                       if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
+                               if (0 == tts_prepare_sync(tts)) {
+                                       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 {
@@ -2428,10 +2533,16 @@ int tts_play_pcm(tts_h tts)
 
        int ret = -1;
        int count = 0;
+       bool is_prepared = false;
        while (0 != ret) {
                ret = tts_dbus_request_play_pcm(client->uid);
                if (0 != ret) {
-                       if (TTS_ERROR_TIMED_OUT != ret) {
+                       if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
+                               if (0 == tts_prepare_sync(tts)) {
+                                       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 {
@@ -2495,10 +2606,16 @@ int tts_stop_pcm(tts_h tts)
 
        int ret = -1;
        int count = 0;
+       bool is_prepared = false;
        while (0 != ret) {
                ret = tts_dbus_request_stop_pcm(client->uid);
                if (0 != ret) {
-                       if (TTS_ERROR_TIMED_OUT != ret) {
+                       if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
+                               if (0 == tts_prepare_sync(tts)) {
+                                       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 {