2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
14 #include <app_manager.h>
19 #include <sys/types.h>
21 #include <system_info.h>
23 #include <package-manager.h>
27 #include "tts_client.h"
28 #include "tts_config_mgr.h"
32 #include "tts_internal.h"
34 static bool g_screen_reader;
36 static int g_feature_enabled = -1;
38 static bool g_err_callback_status = false;
40 static int g_max_text_size = -1;
42 static Ecore_Timer* g_check_state_timer = NULL;
44 static Ecore_Timer* g_hello_timer;
47 static char* g_language = NULL;
49 static int g_voice_type = -1;
51 static int g_speed = -1;
52 static int g_retry_cnt = 0;
54 /* for checking engine update */
55 static pkgmgr_client* g_pkgmgr = NULL;
56 static char* g_engine_name = NULL;
57 static int g_engine_update_status = 0;
58 static pthread_mutex_t g_pkgmgr_mutex = PTHREAD_MUTEX_INITIALIZER;
61 /* Function definition */
62 static Eina_Bool __tts_notify_state_changed(void *data);
63 static Eina_Bool __tts_notify_error(void *data);
64 int __tts_cb_error(int uid, tts_error_e reason, int utt_id, char* err_msg);
72 static int __tts_get_feature_enabled()
74 if (0 == g_feature_enabled) {
75 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] TTS feature NOT supported");
76 return TTS_ERROR_NOT_SUPPORTED;
77 } else if (-1 == g_feature_enabled) {
78 bool tts_supported = false;
79 if (0 == system_info_get_platform_bool(TTS_FEATURE_PATH, &tts_supported)) {
80 if (false == tts_supported) {
81 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] TTS feature NOT supported");
82 g_feature_enabled = 0;
83 return TTS_ERROR_NOT_SUPPORTED;
86 g_feature_enabled = 1;
88 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get feature value");
89 return TTS_ERROR_NOT_SUPPORTED;
96 static const char* __tts_get_error_code(tts_error_e err)
99 case TTS_ERROR_NONE: return "TTS_ERROR_NONE";
100 case TTS_ERROR_OUT_OF_MEMORY: return "TTS_ERROR_OUT_OF_MEMORY";
101 case TTS_ERROR_IO_ERROR: return "TTS_ERROR_IO_ERROR";
102 case TTS_ERROR_INVALID_PARAMETER: return "TTS_ERROR_INVALID_PARAMETER";
103 case TTS_ERROR_OUT_OF_NETWORK: return "TTS_ERROR_OUT_OF_NETWORK";
104 case TTS_ERROR_TIMED_OUT: return "TTS_ERROR_TIMED_OUT";
105 case TTS_ERROR_PERMISSION_DENIED: return "TTS_ERROR_PERMISSION_DENIED";
106 case TTS_ERROR_NOT_SUPPORTED: return "TTS_ERROR_NOT_SUPPORTED";
107 case TTS_ERROR_INVALID_STATE: return "TTS_ERROR_INVALID_STATE";
108 case TTS_ERROR_INVALID_VOICE: return "TTS_ERROR_INVALID_VOICE";
109 case TTS_ERROR_ENGINE_NOT_FOUND: return "TTS_ERROR_ENGINE_NOT_FOUND";
110 case TTS_ERROR_OPERATION_FAILED: return "TTS_ERROR_OPERATION_FAILED";
111 case TTS_ERROR_AUDIO_POLICY_BLOCKED: return "TTS_ERROR_AUDIO_POLICY_BLOCKED";
112 case TTS_ERROR_NOT_SUPPORTED_FEATURE: return "TTS_ERROR_NOT_SUPPORTED_FEATURE";
113 case TTS_ERROR_SERVICE_RESET: return "TTS_ERROR_SERVICE_RESET";
115 return "Invalid error code";
120 static int __tts_convert_config_error_code(tts_config_error_e code)
122 if (code == TTS_CONFIG_ERROR_NONE) return TTS_ERROR_NONE;
123 if (code == TTS_CONFIG_ERROR_OUT_OF_MEMORY) return TTS_ERROR_OUT_OF_MEMORY;
124 if (code == TTS_CONFIG_ERROR_IO_ERROR) return TTS_ERROR_IO_ERROR;
125 if (code == TTS_CONFIG_ERROR_INVALID_PARAMETER) return TTS_ERROR_INVALID_PARAMETER;
126 if (code == TTS_CONFIG_ERROR_INVALID_STATE) return TTS_ERROR_INVALID_STATE;
127 if (code == TTS_CONFIG_ERROR_INVALID_VOICE) return TTS_ERROR_INVALID_VOICE;
128 if (code == TTS_CONFIG_ERROR_ENGINE_NOT_FOUND) return TTS_ERROR_ENGINE_NOT_FOUND;
129 if (code == TTS_CONFIG_ERROR_OPERATION_FAILED) return TTS_ERROR_OPERATION_FAILED;
130 if (code == TTS_CONFIG_ERROR_NOT_SUPPORTED_FEATURE) return TTS_ERROR_NOT_SUPPORTED_FEATURE;
136 void __tts_config_voice_changed_cb(const char* before_lang, int before_voice_type, const char* language, int voice_type, bool auto_voice, void* user_data)
138 SLOG(LOG_DEBUG, TAG_TTSC, "Voice changed : Before lang(%s) type(%d) , Current lang(%s), type(%d)",
139 before_lang, before_voice_type, language, voice_type);
141 GList* client_list = NULL;
142 client_list = tts_client_get_client_list();
145 tts_client_s *data = NULL;
147 if (g_list_length(client_list) > 0) {
148 /* Get a first item */
149 iter = g_list_first(client_list);
151 while (NULL != iter) {
153 if (NULL != data->default_voice_changed_cb) {
154 SLOG(LOG_DEBUG, TAG_TTSC, "Call default voice changed callback : uid(%d)", data->uid);
155 data->default_voice_changed_cb(data->tts, before_lang, before_voice_type,
156 language, voice_type, data->default_voice_changed_user_data);
159 /* Check whether language is changed or not. If it is changed, make 'text_repeat' NULL */
160 if (0 != strncmp(before_lang, language, strlen(before_lang))) {
161 if (NULL != data->text_repeat) {
162 free(data->text_repeat);
163 data->text_repeat = NULL;
168 iter = g_list_next(iter);
175 static Eina_Bool __reconnect_by_engine_changed(void* data)
177 tts_h tts = (tts_h)data;
179 tts_client_s* client = tts_client_get(tts);
180 if (NULL == client) {
181 SLOG(LOG_ERROR, TAG_TTSC, "[WARNING] A handle is not valid");
185 if (TTS_STATE_READY != client->current_state) {
190 int ret = tts_unprepare(tts);
192 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
194 ret = tts_prepare(tts);
196 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
202 void _tts_config_engine_changed_cb(const char* engine_id, const char* setting, const char* language, int voice_type, bool auto_voice, bool need_credential, void* user_data)
204 tts_h tts = (tts_h)user_data;
206 tts_client_s* client = tts_client_get(tts);
207 if (NULL == client) {
208 SLOG(LOG_ERROR, TAG_TTSC, "[WARNING] A handle is not valid");
212 if (NULL != engine_id) SLOG(LOG_DEBUG, TAG_TTSC, "Engine id(%s)", engine_id);
213 if (NULL != setting) SLOG(LOG_DEBUG, TAG_TTSC, "Engine setting(%s)", setting);
214 if (NULL != language) SLOG(LOG_DEBUG, TAG_TTSC, "Language(%s)", language);
215 SLOG(LOG_DEBUG, TAG_TTSC, "Voice type(%d), Auto voice(%s), Credential(%s)", voice_type, auto_voice ? "on" : "off", need_credential ? "need" : "no need");
217 /* When the default engine is changed, please unload the old engine and load the new one. */
220 if (TTS_STATE_PLAYING == client->current_state || TTS_STATE_PAUSED == client->current_state) {
223 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] TTS client stopping...");
226 ecore_idler_add(__reconnect_by_engine_changed, (void*)tts);
227 } else if (TTS_STATE_READY == client->current_state) {
228 ret = tts_unprepare(tts);
230 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to unprepare for setting a new engine... (%d)", ret);
232 ret = tts_prepare(tts);
234 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
238 /* call callback function */
239 if (NULL != client->engine_changed_cb) {
240 client->engine_changed_cb(tts, engine_id, language, voice_type, need_credential, client->engine_changed_user_data);
242 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function for changed engine");
248 void __tts_unset_all_callbacks(tts_h tts)
250 SLOG(LOG_INFO, TAG_TTSC, "@@@ unset all callbacks");
252 tts_unset_state_changed_cb(tts);
253 tts_unset_utterance_started_cb(tts);
254 tts_unset_utterance_completed_cb(tts);
255 tts_unset_error_cb(tts);
256 tts_unset_default_voice_changed_cb(tts);
257 tts_unset_engine_changed_cb(tts);
259 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
262 static int __pkgmgr_status_cb(uid_t target_uid, int req_id, const char *type, const char *pkgname, const char *key, const char *val, const void *pmsg, void *data)
264 SLOG(LOG_INFO, TAG_TTSC, "[INFO] pkgmgr status cb is invoked. pkgname(%s), type(%s), key(%s), val(%s)", pkgname, type, key, val);
266 if (0 != strncmp(g_engine_name, pkgname, strlen(g_engine_name))) {
267 SLOG(LOG_DEBUG, TAG_TTSC, "[WARN] this is not tts engine");
270 if (key && 0 == strncmp(key, "start", strlen(key))) {
271 if (val && (0 == strncmp(val, "update", strlen(val) || 0 == strncmp(val, "uninstall", strlen(val))))) {
272 SLOG(LOG_INFO, TAG_TTSC, "[INFO] start to install.");
273 g_engine_update_status = 1;
275 } else if (key && 0 == strncmp(key, "end", strlen(key))) {
276 SLOG(LOG_INFO, TAG_TTSC, "[INFO] finish to install");
277 g_engine_update_status = 0;
284 static void __create_pkgmgr_thread(void* data, Ecore_Thread* thread)
286 SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] create pkgmgr thread");
288 pthread_mutex_lock(&g_pkgmgr_mutex);
291 g_pkgmgr = pkgmgr_client_new(PC_LISTENING);
292 if (NULL == g_pkgmgr) {
293 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to create pkgmgr handle");
295 if (pkgmgr_client_listen_status(g_pkgmgr, __pkgmgr_status_cb, NULL) < 0) {
296 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to listen pkgmgr status. remove and recreate client");
297 pkgmgr_client_free(g_pkgmgr);
301 SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] Succeed to register pkgmgr cb");
307 pthread_mutex_unlock(&g_pkgmgr_mutex);
312 static void __finish_pkgmgr_thread(void* data, Ecore_Thread* thread)
314 SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] Finish pkgmgr thread");
317 static void __pkgmgr_thread(void* data)
319 SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] call pkgmgr_thread");
321 ecore_thread_run(__create_pkgmgr_thread, __finish_pkgmgr_thread, NULL, NULL);
326 int tts_create(tts_h* tts)
328 if (0 != __tts_get_feature_enabled()) {
329 return TTS_ERROR_NOT_SUPPORTED;
332 SLOG(LOG_INFO, TAG_TTSC, "@@@ Create TTS");
336 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
337 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
338 return TTS_ERROR_INVALID_PARAMETER;
341 if (0 == tts_client_get_size()) {
342 if (0 != tts_dbus_open_connection()) {
343 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to open dbus connection");
344 return TTS_ERROR_OPERATION_FAILED;
348 if (0 != tts_client_new(tts)) {
349 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to create client!!!!!");
350 return TTS_ERROR_OUT_OF_MEMORY;
353 tts_client_s* client = tts_client_get(*tts);
354 if (NULL == client) {
355 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get client");
356 return TTS_ERROR_OPERATION_FAILED;
359 int ret = tts_config_mgr_initialize(client->uid);
361 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to init config manager : %d", ret);
362 tts_client_destroy(*tts);
363 return __tts_convert_config_error_code(ret);
366 ret = tts_config_mgr_set_callback(client->uid, _tts_config_engine_changed_cb, __tts_config_voice_changed_cb, NULL, NULL, NULL, client->tts);
368 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to set config changed : %d", ret);
369 tts_client_destroy(*tts);
370 return __tts_convert_config_error_code(ret);
373 ecore_main_loop_thread_safe_call_async(__pkgmgr_thread, NULL);
375 SLOG(LOG_INFO, TAG_TTSC, "[INFO] call ecore thread for creating pkgmgr thread");
377 g_engine_name = vconf_get_str(TTS_ENGINE_DB_DEFAULT);
378 if (NULL == g_engine_name) {
379 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get engine name");
381 SLOG(LOG_ERROR, TAG_TTSC, "[INFO] Engine name(%s)", g_engine_name);
384 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
385 SLOG(LOG_DEBUG, TAG_TTSC, " ");
387 return TTS_ERROR_NONE;
390 int tts_destroy(tts_h tts)
392 if (0 != __tts_get_feature_enabled()) {
393 return TTS_ERROR_NOT_SUPPORTED;
396 SLOG(LOG_INFO, TAG_TTSC, "@@@ Destroy TTS");
399 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
400 return TTS_ERROR_INVALID_PARAMETER;
403 tts_client_s* client = tts_client_get(tts);
406 if (NULL == client) {
407 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
408 return TTS_ERROR_INVALID_PARAMETER;
411 /* check used callback */
412 if (0 != tts_client_get_use_callback(client)) {
413 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Cannot destroy in Callback function");
414 return TTS_ERROR_OPERATION_FAILED;
417 pthread_mutex_lock(&g_pkgmgr_mutex);
419 pkgmgr_client_remove_listen_status(g_pkgmgr);
420 pkgmgr_client_free(g_pkgmgr);
423 pthread_mutex_unlock(&g_pkgmgr_mutex);
425 tts_config_mgr_finalize(client->uid);
429 int screen_reader = -1;
432 switch (client->current_state) {
433 case TTS_STATE_PAUSED:
434 case TTS_STATE_PLAYING:
435 case TTS_STATE_READY:
436 ret = vconf_get_bool(TTS_ACCESSIBILITY_KEY, &screen_reader);
438 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get screen reader");
440 SLOG(LOG_INFO, tts_tag(), "[INFO] Success to get screen reader(%d), g_screen_reader(%s), client->mode(%d)", screen_reader, (true == g_screen_reader) ? "True" : "False", client->mode);
441 g_screen_reader = (bool)screen_reader;
443 if (!(false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode)) {
445 ret = tts_dbus_request_finalize(client->uid);
448 if (TTS_ERROR_TIMED_OUT != ret) {
449 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
452 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry finalize");
455 if (TTS_RETRY_COUNT == count) {
456 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
464 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Do not request finalize : g_sr(%d) mode(%d)", g_screen_reader, client->mode);
467 client->before_state = client->current_state;
468 client->current_state = TTS_STATE_CREATED;
470 case TTS_STATE_CREATED:
471 if (NULL != client->conn_timer) {
472 SLOG(LOG_DEBUG, TAG_TTSC, "Connect Timer is deleted");
473 ecore_timer_del(client->conn_timer);
474 client->conn_timer = NULL;
476 /* Unset registered callbacks */
477 __tts_unset_all_callbacks(tts);
480 tts_client_destroy(tts);
487 if (0 == tts_client_get_size()) {
488 if (0 != tts_dbus_close_connection()) {
489 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to close connection");
493 if (NULL != g_language) {
498 /* Delete state timer before destroying handle */
499 if (NULL != g_check_state_timer) {
500 ecore_timer_del(g_check_state_timer);
501 g_check_state_timer = NULL;
506 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
508 return TTS_ERROR_NONE;
512 void __tts_screen_reader_changed_cb(bool value)
514 g_screen_reader = value;
518 int tts_set_mode(tts_h tts, tts_mode_e mode)
520 if (0 != __tts_get_feature_enabled()) {
521 return TTS_ERROR_NOT_SUPPORTED;
524 SLOG(LOG_INFO, TAG_TTSC, "@@@ Set TTS mode(%d)", mode);
526 tts_client_s* client = tts_client_get(tts);
529 if (NULL == client) {
530 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
531 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
532 return TTS_ERROR_INVALID_PARAMETER;
536 if (client->current_state != TTS_STATE_CREATED) {
537 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'CREATED'");
538 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
539 SLOG(LOG_DEBUG, TAG_TTSC, " ");
540 return TTS_ERROR_INVALID_STATE;
543 if (TTS_MODE_DEFAULT <= mode && mode <= TTS_MODE_INTERRUPT) {
546 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] mode is not valid : %d", mode);
547 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
548 SLOG(LOG_DEBUG, TAG_TTSC, " ");
549 return TTS_ERROR_INVALID_PARAMETER;
552 if (TTS_MODE_SCREEN_READER == mode) {
555 ret = vconf_get_bool(TTS_ACCESSIBILITY_KEY, &screen_reader);
557 SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to get screen reader");
558 return TTS_ERROR_OPERATION_FAILED;
560 g_screen_reader = (bool)screen_reader;
561 tts_config_set_screen_reader_callback(client->uid, __tts_screen_reader_changed_cb);
563 tts_config_unset_screen_reader_callback(client->uid);
566 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
568 return TTS_ERROR_NONE;
571 int tts_get_mode(tts_h tts, tts_mode_e* mode)
573 if (0 != __tts_get_feature_enabled()) {
574 return TTS_ERROR_NOT_SUPPORTED;
577 SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Get TTS mode");
579 tts_client_s* client = tts_client_get(tts);
582 if (NULL == client) {
583 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
584 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
585 SLOG(LOG_DEBUG, TAG_TTSC, " ");
586 return TTS_ERROR_INVALID_PARAMETER;
590 if (client->current_state != TTS_STATE_CREATED) {
591 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'CREATED'");
592 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
593 SLOG(LOG_DEBUG, TAG_TTSC, " ");
594 return TTS_ERROR_INVALID_STATE;
598 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter(mode) is NULL");
599 return TTS_ERROR_INVALID_PARAMETER;
602 *mode = client->mode;
604 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
605 SLOG(LOG_DEBUG, TAG_TTSC, " ");
607 return TTS_ERROR_NONE;
610 int tts_set_credential(tts_h tts, const char* credential)
612 if (0 != __tts_get_feature_enabled()) {
613 return TTS_ERROR_NOT_SUPPORTED;
616 if (NULL == tts || NULL == credential) {
617 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
618 return TTS_ERROR_INVALID_PARAMETER;
621 tts_client_s* client = tts_client_get(tts);
623 if (NULL == client) {
624 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
625 return TTS_ERROR_INVALID_PARAMETER;
628 if (TTS_STATE_CREATED != client->current_state && TTS_STATE_READY != client->current_state) {
629 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid (%d).", client->current_state);
630 return TTS_ERROR_INVALID_STATE;
633 if (NULL != client->credential) {
634 free(client->credential);
635 client->credential = NULL;
637 client->credential = strdup(credential);
639 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
641 return TTS_ERROR_NONE;
645 int tts_set_server_tts(tts_h tts, const char* credential)
647 if (0 != __tts_get_feature_enabled()) {
648 return TTS_ERROR_NOT_SUPPORTED;
652 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
653 return TTS_ERROR_INVALID_PARAMETER;
656 tts_client_s* client = tts_client_get(tts);
658 if (NULL == client) {
659 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
660 return TTS_ERROR_INVALID_PARAMETER;
663 if (TTS_STATE_CREATED != client->current_state && TTS_STATE_READY != client->current_state) {
664 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid (%d).", client->current_state);
665 return TTS_ERROR_INVALID_STATE;
668 if (NULL != client->credential) {
669 free(client->credential);
670 client->credential = NULL;
673 client->internal = true;
676 if (NULL != credential) {
677 key = strdup("EnableServerTTS");
678 client->credential = strdup(credential);
679 if (NULL == client->credential) {
680 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
685 return TTS_ERROR_OUT_OF_MEMORY;
688 key = strdup("DisableServerTTS");
692 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
693 return TTS_ERROR_OUT_OF_MEMORY;
698 int ret = app_manager_get_app_id(pid, &appid);
699 if (0 != ret || NULL == appid) {
700 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get appid, ret(%d), pid(%d), appid(%s)", ret, pid, appid);
707 return TTS_ERROR_OPERATION_FAILED;
710 ret = tts_set_private_data(tts, key, appid);
712 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to set private data, ret(%d), pid(%d), appid(%s)", ret, pid, appid);
725 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
727 return TTS_ERROR_NONE;
731 static Eina_Bool __tts_connect_daemon(void *data)
733 tts_h tts = (tts_h)data;
734 tts_client_s* client = tts_client_get(tts);
737 if (NULL == client) {
738 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
742 /* check whether engine is updating or not */
743 if (g_engine_update_status) {
744 SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] cannot prepare due to engine update");
745 __tts_cb_error(-1, TTS_ERROR_SERVICE_RESET, -1, "Daemon Reset");
751 if (0 != tts_dbus_request_hello_sync(client->uid)) {
755 SLOG(LOG_INFO, TAG_TTSC, "@@@ Connect daemon");
757 /* do request initialize */
759 bool credential_needed = false;
761 ret = tts_dbus_request_initialize(client->uid, &credential_needed);
763 if (TTS_ERROR_ENGINE_NOT_FOUND == ret) {
764 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize : %s", __tts_get_error_code(ret));
766 client->reason = TTS_ERROR_ENGINE_NOT_FOUND;
769 ecore_timer_add(0, __tts_notify_error, (void*)client->tts);
770 if (client->conn_timer) {
771 ecore_timer_del(client->conn_timer);
772 client->conn_timer = NULL;
776 } else if (TTS_ERROR_PERMISSION_DENIED == ret) {
777 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize : %s", __tts_get_error_code(ret));
779 client->reason = TTS_ERROR_PERMISSION_DENIED;
782 ecore_timer_add(0, __tts_notify_error, (void*)client->tts);
783 if (client->conn_timer) {
784 ecore_timer_del(client->conn_timer);
785 client->conn_timer = NULL;
789 } else if (TTS_ERROR_NONE != ret) {
790 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Fail to connection. Retry to connect : %s", __tts_get_error_code(ret));
794 /* success to connect tts-daemon */
795 client->credential_needed = credential_needed;
796 SLOG(LOG_ERROR, TAG_TTSC, "Supported options : credential(%s)", credential_needed ? "need" : "no need");
799 if (client->conn_timer) {
800 ecore_timer_del(client->conn_timer);
801 client->conn_timer = NULL;
804 client = tts_client_get(tts);
806 if (NULL == client) {
807 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
811 client->before_state = client->current_state;
812 client->current_state = TTS_STATE_READY;
814 if (NULL != client->state_changed_cb) {
815 tts_client_use_callback(client);
816 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
817 tts_client_not_use_callback(client);
819 SLOG(LOG_WARN, TAG_TTSC, "State changed callback is NULL");
822 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
827 int __tts_cb_hello(int uid, int ret, int credential_needed)
829 tts_client_s* client = tts_client_get_by_uid(uid);
830 if (NULL == client) {
831 SLOG(LOG_ERROR, TAG_TTSC, "Fail to get TTS client or ignore this uid(%d)", uid);
832 return TTS_ERROR_OPERATION_FAILED;
836 ecore_timer_del(g_hello_timer);
837 g_hello_timer = NULL;
840 if (TTS_STATE_READY == client->current_state) {
841 SLOG(LOG_INFO, TAG_TTSC, "[INFO] tts client is already READY");
842 return TTS_ERROR_NONE;
845 if (TTS_ERROR_ENGINE_NOT_FOUND == ret) {
846 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize : %s", __tts_get_error_code(ret));
848 client->reason = TTS_ERROR_ENGINE_NOT_FOUND;
851 ecore_timer_add(0, __tts_notify_error, (void*)client->tts);
852 return TTS_ERROR_OPERATION_FAILED;
854 } else if (TTS_ERROR_PERMISSION_DENIED == ret) {
855 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize : %s", __tts_get_error_code(ret));
857 client->reason = TTS_ERROR_PERMISSION_DENIED;
860 ecore_timer_add(0, __tts_notify_error, (void*)client->tts);
861 return TTS_ERROR_PERMISSION_DENIED;
863 } else if (TTS_ERROR_NONE != ret) {
864 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Fail to connection. Retry to connect : %s", __tts_get_error_code(ret));
865 return TTS_ERROR_OPERATION_FAILED;
868 /* success to connect tts-daemon */
869 client->credential_needed = credential_needed;
870 SLOG(LOG_ERROR, TAG_TTSC, "Supported options : credential(%s)", credential_needed ? "need" : "no need");
873 client->before_state = client->current_state;
874 client->current_state = TTS_STATE_READY;
876 ecore_timer_add(0.0, __tts_notify_state_changed, client->tts);
878 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
879 return TTS_ERROR_NONE;
882 static Eina_Bool __send_hello(void *data)
884 tts_h tts = (tts_h)data;
885 tts_client_s* client = tts_client_get(tts);
888 if (NULL == client) {
889 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
890 g_hello_timer = NULL;
895 if (client->current_state == TTS_STATE_READY) {
896 SLOG(LOG_ERROR, TAG_TTSC, "[INFO] TTS client has been already connected to tts service"); //LCOV_EXCL_LINE
897 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
898 g_hello_timer = NULL;
903 int ret = tts_dbus_request_hello(client->uid);
905 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request hello !!"); //LCOV_EXCL_LINE
907 SLOG(LOG_ERROR, TAG_TTSC, "@@@ Send Hello");
911 if (TTS_HELLO_RETRY_COUNT == g_retry_cnt) {
912 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Stop to send hello, retry count reaches the limit");
917 if (!g_hello_timer) {
918 SLOG(LOG_ERROR, TAG_TTSC, "@@@ Call checking Hello timer callback");
919 g_hello_timer = ecore_timer_add(0.5, __send_hello, tts);
925 int tts_prepare(tts_h tts)
927 if (0 != __tts_get_feature_enabled()) {
928 return TTS_ERROR_NOT_SUPPORTED;
931 SLOG(LOG_INFO, TAG_TTSC, "@@@ Prepare TTS");
933 tts_client_s* client = tts_client_get(tts);
936 if (NULL == client) {
937 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
938 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
939 return TTS_ERROR_INVALID_PARAMETER;
943 if (client->current_state != TTS_STATE_CREATED) {
944 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'CREATED'");
945 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
946 return TTS_ERROR_INVALID_STATE;
949 if (NULL == g_hello_timer) {
950 SLOG(LOG_ERROR, TAG_TTSC, "@@@ Call checking Hello timer callback");
952 ecore_thread_main_loop_begin();
953 ecore_timer_add(0.0, __send_hello, (void*)tts);
954 ecore_thread_main_loop_end();
957 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
959 return TTS_ERROR_NONE;
963 int tts_prepare_sync(tts_h tts)
965 if (0 != __tts_get_feature_enabled()) {
966 return TTS_ERROR_NOT_SUPPORTED;
969 SLOG(LOG_INFO, TAG_TTSC, "@@@ Prepare TTS");
971 tts_client_s* client = tts_client_get(tts);
974 if (NULL == client) {
975 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
976 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
977 return TTS_ERROR_INVALID_PARAMETER;
981 if (client->current_state != TTS_STATE_CREATED) {
982 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'CREATED'");
983 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
984 return TTS_ERROR_INVALID_STATE;
988 while (EINA_TRUE == __tts_connect_daemon((void*)tts) && TTS_CONNECTION_RETRY_COUNT > cnt) {
992 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
994 if (TTS_CONNECTION_RETRY_COUNT == cnt) {
995 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to connect daemon");
996 return TTS_ERROR_OPERATION_FAILED;
999 return TTS_ERROR_NONE;
1003 int tts_unprepare(tts_h tts)
1005 if (0 != __tts_get_feature_enabled()) {
1006 return TTS_ERROR_NOT_SUPPORTED;
1009 SLOG(LOG_INFO, TAG_TTSC, "@@@ Unprepare TTS");
1011 tts_client_s* client = tts_client_get(tts);
1014 if (NULL == client) {
1015 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
1016 return TTS_ERROR_INVALID_PARAMETER;
1020 if (client->current_state != TTS_STATE_READY) {
1021 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'READY'");
1022 return TTS_ERROR_INVALID_STATE;
1027 int screen_reader = -1;
1029 ret = vconf_get_bool(TTS_ACCESSIBILITY_KEY, &screen_reader);
1031 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get screen reader");
1033 SLOG(LOG_INFO, tts_tag(), "[INFO] Success to get screen reader(%d), g_screen_reader(%s), client->mode(%d)", screen_reader, (true == g_screen_reader) ? "True" : "False", client->mode);
1034 g_screen_reader = (bool)screen_reader;
1037 bool is_prepared = false;
1038 if (!(false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode)) {
1040 ret = tts_dbus_request_finalize(client->uid);
1043 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1044 client->current_state = TTS_STATE_CREATED;
1045 if (0 == tts_prepare_sync(tts)) {
1047 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1049 } else if (TTS_ERROR_TIMED_OUT != ret) {
1050 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1053 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry finalize : %s", __tts_get_error_code(ret));
1056 if (TTS_RETRY_COUNT == count) {
1057 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1065 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Do not request finalize : g_sr(%d) mode(%d)", g_screen_reader, client->mode);
1068 client->before_state = client->current_state;
1069 client->current_state = TTS_STATE_CREATED;
1071 if (NULL != client->state_changed_cb) {
1072 tts_client_use_callback(client);
1073 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1074 tts_client_not_use_callback(client);
1075 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1078 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1080 return TTS_ERROR_NONE;
1083 bool __tts_supported_voice_cb(const char* engine_id, const char* language, int type, void* user_data)
1085 tts_h tts = (tts_h)user_data;
1087 tts_client_s* client = tts_client_get(tts);
1088 if (NULL == client) {
1089 SLOG(LOG_ERROR, TAG_TTSC, "[WARNING] A handle is not valid");
1093 /* call callback function */
1094 if (NULL != client->supported_voice_cb) {
1095 return client->supported_voice_cb(tts, language, type, client->supported_voice_user_data);
1097 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of supported voice");
1103 int tts_foreach_supported_voices(tts_h tts, tts_supported_voice_cb callback, void* user_data)
1105 if (0 != __tts_get_feature_enabled()) {
1106 return TTS_ERROR_NOT_SUPPORTED;
1109 SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Foreach supported voices");
1111 if (NULL == tts || NULL == callback) {
1112 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
1113 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1114 return TTS_ERROR_INVALID_PARAMETER;
1117 tts_client_s* client = tts_client_get(tts);
1120 if (NULL == client) {
1121 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1122 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1123 return TTS_ERROR_INVALID_PARAMETER;
1127 char* current_engine = NULL;
1128 ret = tts_config_mgr_get_engine(¤t_engine);
1130 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get current engine : %d", ret);
1131 return __tts_convert_config_error_code(ret);
1134 client->supported_voice_cb = callback;
1135 client->supported_voice_user_data = user_data;
1137 ret = tts_config_mgr_get_voice_list(current_engine, __tts_supported_voice_cb, client->tts);
1139 if (NULL != current_engine) {
1140 free(current_engine);
1141 current_engine = NULL;
1144 client->supported_voice_cb = NULL;
1145 client->supported_voice_user_data = NULL;
1148 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Result : %d", ret);
1149 ret = TTS_ERROR_OPERATION_FAILED;
1152 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1157 int tts_get_default_voice(tts_h tts, char** lang, int* vctype)
1159 if (0 != __tts_get_feature_enabled()) {
1160 return TTS_ERROR_NOT_SUPPORTED;
1163 SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Get default voice");
1166 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1167 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1168 return TTS_ERROR_INVALID_PARAMETER;
1171 tts_client_s* client = tts_client_get(tts);
1173 if (NULL == client) {
1174 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1175 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1176 return TTS_ERROR_INVALID_PARAMETER;
1179 /* Request call remote method */
1181 ret = tts_config_mgr_get_voice(lang, vctype);
1183 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %d", ret);
1184 return __tts_convert_config_error_code(ret);
1186 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] Default language(%s), type(%d)", *lang, *vctype);
1189 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1194 int tts_get_max_text_size(tts_h tts, unsigned int* size)
1196 if (0 != __tts_get_feature_enabled()) {
1197 return TTS_ERROR_NOT_SUPPORTED;
1200 if (NULL == tts || NULL == size) {
1201 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get max text count : Input parameter is null");
1202 return TTS_ERROR_INVALID_PARAMETER;
1205 tts_client_s* client = tts_client_get(tts);
1207 if (NULL == client) {
1208 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get max text count : A handle is not valid");
1209 return TTS_ERROR_INVALID_PARAMETER;
1212 if (TTS_STATE_READY != client->current_state) {
1213 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get max text count : Current state is NOT 'READY'.");
1214 return TTS_ERROR_INVALID_STATE;
1217 if (0 != tts_config_mgr_get_max_text_size(size)) {
1218 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get max text size");
1219 return TTS_ERROR_INVALID_PARAMETER;
1222 g_max_text_size = (int)*size;
1224 SLOG(LOG_DEBUG, TAG_TTSC, "Get max text count : %d", *size);
1225 return TTS_ERROR_NONE;
1228 int tts_get_state(tts_h tts, tts_state_e* state)
1230 if (0 != __tts_get_feature_enabled()) {
1231 return TTS_ERROR_NOT_SUPPORTED;
1234 if (NULL == tts || NULL == state) {
1235 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : Input parameter is null");
1236 return TTS_ERROR_INVALID_PARAMETER;
1239 tts_client_s* client = tts_client_get(tts);
1241 if (NULL == client) {
1242 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
1243 return TTS_ERROR_INVALID_PARAMETER;
1246 *state = client->current_state;
1249 case TTS_STATE_CREATED: SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Created'"); break;
1250 case TTS_STATE_READY: SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Ready'"); break;
1251 case TTS_STATE_PLAYING: SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Playing'"); break;
1252 case TTS_STATE_PAUSED: SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Paused'"); break;
1253 default: SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid value"); break;
1256 return TTS_ERROR_NONE;
1259 int tts_get_speed_range(tts_h tts, int* min, int* normal, int* max)
1261 if (0 != __tts_get_feature_enabled()) {
1262 return TTS_ERROR_NOT_SUPPORTED;
1265 if (NULL == tts || NULL == min || NULL == normal || NULL == max) {
1266 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
1267 return TTS_ERROR_INVALID_PARAMETER;
1270 tts_client_s* client = tts_client_get(tts);
1272 if (NULL == client) {
1273 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
1274 return TTS_ERROR_INVALID_PARAMETER;
1277 *min = TTS_SPEED_MIN;
1278 *normal = TTS_SPEED_NORMAL;
1279 *max = TTS_SPEED_MAX;
1281 return TTS_ERROR_NONE;
1284 int tts_get_error_message(tts_h tts, char** err_msg)
1286 if (0 != __tts_get_feature_enabled()) {
1287 return TTS_ERROR_NOT_SUPPORTED;
1290 if (NULL == tts || NULL == err_msg) {
1291 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
1292 return TTS_ERROR_INVALID_PARAMETER;
1295 tts_client_s* client = tts_client_get(tts);
1297 if (NULL == client) {
1298 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
1299 return TTS_ERROR_INVALID_PARAMETER;
1302 if (NULL != client->err_msg) {
1303 *err_msg = strdup(client->err_msg);
1304 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Error msg (%s)", *err_msg);
1307 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Error msg (NULL)");
1310 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1312 return TTS_ERROR_NONE;
1315 int tts_add_text(tts_h tts, const char* text, const char* language, int voice_type, int speed, int* utt_id)
1317 SLOG(LOG_INFO, TAG_TTSC, "[DEBUG] Add text: text(%s), language(%s), type(%d)", (NULL == text) ? "NULL" : text, (NULL == language) ? "NULL" : language, voice_type);
1319 if (0 != __tts_get_feature_enabled()) {
1320 return TTS_ERROR_NOT_SUPPORTED;
1324 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Speed should not be negative(%d)", speed);
1325 return TTS_ERROR_INVALID_PARAMETER;
1328 if (voice_type < 0) {
1329 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Voice type should not be negative(%d)", voice_type);
1330 return TTS_ERROR_INVALID_PARAMETER;
1333 SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Add text");
1335 if (NULL == tts || NULL == utt_id) {
1336 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
1337 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1338 return TTS_ERROR_INVALID_PARAMETER;
1341 tts_client_s* client = tts_client_get(tts);
1343 if (NULL == client) {
1344 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1345 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1346 return TTS_ERROR_INVALID_PARAMETER;
1349 if (TTS_STATE_CREATED == client->current_state) {
1350 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Current state is 'CREATED'.");
1351 return TTS_ERROR_INVALID_STATE;
1354 if (-1 == g_max_text_size) {
1355 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] g_max_text_size is %d", g_max_text_size);
1356 if (0 != tts_config_mgr_get_max_text_size((unsigned int*)&g_max_text_size)) {
1357 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get max text size");
1358 return TTS_ERROR_INVALID_PARAMETER;
1362 if (0 == g_max_text_size) {
1363 if (strlen(text) <= 0) {
1364 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] Max Text Size is %d", g_max_text_size);
1365 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input text size is invalid. (max text size is unlimited.)");
1366 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1367 return TTS_ERROR_INVALID_PARAMETER;
1370 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] g_max_text_size is %d", g_max_text_size);
1371 if (g_max_text_size < strlen(text) || strlen(text) <= 0) {
1372 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input text size is invalid.");
1373 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1374 return TTS_ERROR_INVALID_PARAMETER;
1378 if (TTS_SPEED_AUTO > speed || TTS_SPEED_MAX < speed) {
1379 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] speed value(%d) is invalid.", speed);
1380 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1381 return TTS_ERROR_INVALID_PARAMETER;
1384 if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1385 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1386 return TTS_ERROR_INVALID_STATE;
1389 if (true == client->credential_needed && NULL == client->credential) {
1390 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
1391 return TTS_ERROR_PERMISSION_DENIED;
1394 /* check valid utf8 */
1398 dbus_error_init(&err);
1400 valid = dbus_validate_utf8(text, &err);
1401 if (dbus_error_is_set(&err)) {
1402 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Dbus Error(%s), text(%s)", err.message, text);
1403 dbus_error_free(&err);
1404 return TTS_ERROR_INVALID_PARAMETER;
1407 if (valid != true) {
1408 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Text is invalid - '%s'", text);
1409 return TTS_ERROR_INVALID_PARAMETER;
1411 SLOG(LOG_DEBUG, TAG_TTSC, "Text is valid - text is '%s'", text);
1413 /* save texts for repetition */
1414 if (NULL != client->text_repeat) {
1415 free(client->text_repeat);
1416 client->text_repeat = NULL;
1419 client->text_repeat = strdup(text);
1421 if (NULL != g_language) {
1425 if (NULL == language)
1428 g_language = strdup(language);
1430 g_voice_type = voice_type;
1433 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] repeat: text(%s), language(%s), voice type(%d), speed(%d)", client->text_repeat, (g_language) ? g_language : "NULL", g_voice_type, g_speed);
1435 /* change default language value */
1438 if (NULL == language)
1439 temp = strdup("default");
1441 temp = strdup(language);
1443 client->current_utt_id++;
1444 if (client->current_utt_id == 10000) {
1445 client->current_utt_id = 1;
1451 bool is_prepared = false;
1453 ret = tts_dbus_request_add_text(client->uid, text, temp, voice_type, speed, client->current_utt_id, client->credential);
1456 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1457 client->current_state = TTS_STATE_CREATED;
1458 if (0 == tts_prepare_sync(tts)) {
1460 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1462 } else if (TTS_ERROR_TIMED_OUT != ret) {
1463 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1466 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry add text : %s", __tts_get_error_code(ret));
1469 if (TTS_RETRY_MIN_COUNT == count) {
1470 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1476 *utt_id = client->current_utt_id;
1485 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1491 static void __tts_play_async(void *data)
1493 tts_h tts = (tts_h)data;
1494 tts_client_s* client = tts_client_get(tts);
1497 if (NULL == client) {
1498 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1504 bool is_prepared = false;
1506 ret = tts_dbus_request_play(client->uid, client->credential);
1508 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1509 client->current_state = TTS_STATE_CREATED;
1510 if (0 == tts_prepare_sync(tts)) {
1512 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1514 } else if (TTS_ERROR_TIMED_OUT != ret) {
1515 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1518 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", __tts_get_error_code(ret));
1521 if (TTS_RETRY_COUNT == count) {
1522 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1530 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to play tts : %s", __tts_get_error_code(ret));
1532 client->reason = ret;
1533 client->utt_id = -1;
1535 ecore_timer_add(0, __tts_notify_error, client->tts);
1539 client->before_state = client->current_state;
1540 client->current_state = TTS_STATE_PLAYING;
1542 if (NULL != client->state_changed_cb) {
1543 tts_client_use_callback(client);
1544 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1545 tts_client_not_use_callback(client);
1546 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1549 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1554 int tts_play_async(tts_h tts)
1556 if (0 != __tts_get_feature_enabled()) {
1557 return TTS_ERROR_NOT_SUPPORTED;
1560 SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Play tts");
1563 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
1564 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1565 return TTS_ERROR_INVALID_PARAMETER;
1568 tts_client_s* client = tts_client_get(tts);
1570 if (NULL == client) {
1571 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
1572 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1573 return TTS_ERROR_INVALID_PARAMETER;
1576 if (TTS_STATE_PLAYING == client->current_state || TTS_STATE_CREATED == client->current_state) {
1577 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
1578 return TTS_ERROR_INVALID_STATE;
1581 if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1582 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1583 return TTS_ERROR_INVALID_STATE;
1586 if (true == client->credential_needed && NULL == client->credential) {
1587 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
1588 return TTS_ERROR_PERMISSION_DENIED;
1591 ecore_main_loop_thread_safe_call_async(__tts_play_async, (void*)tts);
1593 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1595 return TTS_ERROR_NONE;
1598 int tts_play(tts_h tts)
1600 if (0 != __tts_get_feature_enabled()) {
1601 return TTS_ERROR_NOT_SUPPORTED;
1604 SLOG(LOG_INFO, TAG_TTSC, "@@@ Play tts");
1607 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
1608 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1609 return TTS_ERROR_INVALID_PARAMETER;
1612 tts_client_s* client = tts_client_get(tts);
1614 if (NULL == client) {
1615 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
1616 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1617 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1618 return TTS_ERROR_INVALID_PARAMETER;
1621 if (TTS_STATE_PLAYING == client->current_state || TTS_STATE_CREATED == client->current_state) {
1622 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
1623 return TTS_ERROR_INVALID_STATE;
1626 if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1627 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1628 return TTS_ERROR_INVALID_STATE;
1631 if (true == client->credential_needed && NULL == client->credential) {
1632 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
1633 return TTS_ERROR_PERMISSION_DENIED;
1638 bool is_prepared = false;
1640 ret = tts_dbus_request_play(client->uid, client->credential);
1643 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1644 client->current_state = TTS_STATE_CREATED;
1645 if (0 == tts_prepare_sync(tts)) {
1647 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1649 } else if (TTS_ERROR_TIMED_OUT != ret) {
1650 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1653 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", __tts_get_error_code(ret));
1656 if (TTS_RETRY_COUNT == count) {
1657 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1665 client->before_state = client->current_state;
1666 client->current_state = TTS_STATE_PLAYING;
1668 if (NULL != client->state_changed_cb) {
1669 tts_client_use_callback(client);
1670 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1671 tts_client_not_use_callback(client);
1672 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1675 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1677 return TTS_ERROR_NONE;
1680 static void __tts_stop_async(void *data)
1682 tts_h tts = (tts_h)data;
1683 tts_client_s* client = tts_client_get(tts);
1686 if (NULL == client) {
1687 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1693 bool is_prepared = false;
1695 ret = tts_dbus_request_stop(client->uid);
1697 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1698 client->current_state = TTS_STATE_CREATED;
1699 if (0 == tts_prepare_sync(tts)) {
1701 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1703 } else if (TTS_ERROR_TIMED_OUT != ret) {
1704 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1707 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry stop : %s", __tts_get_error_code(ret));
1710 if (TTS_RETRY_COUNT == count) {
1711 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1719 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to stop tts : %s", __tts_get_error_code(ret));
1721 client->reason = ret;
1722 client->utt_id = -1;
1724 ecore_timer_add(0, __tts_notify_error, client->tts);
1728 client->before_state = client->current_state;
1729 client->current_state = TTS_STATE_READY;
1731 if (NULL != client->state_changed_cb) {
1732 tts_client_use_callback(client);
1733 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1734 tts_client_not_use_callback(client);
1735 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1738 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1743 int tts_stop_aync(tts_h tts)
1745 if (0 != __tts_get_feature_enabled()) {
1746 return TTS_ERROR_NOT_SUPPORTED;
1749 SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Stop tts");
1752 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1753 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1754 return TTS_ERROR_INVALID_PARAMETER;
1757 tts_client_s* client = tts_client_get(tts);
1759 if (NULL == client) {
1760 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1761 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1762 return TTS_ERROR_INVALID_PARAMETER;
1765 if (TTS_STATE_CREATED == client->current_state) {
1766 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Current state is 'CREATED'.");
1767 return TTS_ERROR_INVALID_STATE;
1770 if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1771 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1772 return TTS_ERROR_INVALID_STATE;
1775 ecore_main_loop_thread_safe_call_async(__tts_stop_async, (void*)tts);
1777 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1779 return TTS_ERROR_NONE;
1782 int tts_stop(tts_h tts)
1784 if (0 != __tts_get_feature_enabled()) {
1785 return TTS_ERROR_NOT_SUPPORTED;
1788 SLOG(LOG_INFO, TAG_TTSC, "@@@ Stop tts");
1791 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1792 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1793 return TTS_ERROR_INVALID_PARAMETER;
1796 tts_client_s* client = tts_client_get(tts);
1798 if (NULL == client) {
1799 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1800 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1801 return TTS_ERROR_INVALID_PARAMETER;
1804 if (TTS_STATE_CREATED == client->current_state) {
1805 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Current state is 'CREATED'.");
1806 return TTS_ERROR_INVALID_STATE;
1809 if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1810 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1811 return TTS_ERROR_INVALID_STATE;
1816 bool is_prepared = false;
1818 ret = tts_dbus_request_stop(client->uid);
1821 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1822 client->current_state = TTS_STATE_CREATED;
1823 if (0 == tts_prepare_sync(tts)) {
1825 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1827 } else if (TTS_ERROR_TIMED_OUT != ret) {
1828 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1831 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry stop : %s", __tts_get_error_code(ret));
1834 if (TTS_RETRY_COUNT == count) {
1835 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1843 client->before_state = client->current_state;
1844 client->current_state = TTS_STATE_READY;
1846 if (NULL != client->state_changed_cb) {
1847 tts_client_use_callback(client);
1848 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1849 tts_client_not_use_callback(client);
1850 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1853 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1855 return TTS_ERROR_NONE;
1858 static void __tts_pause_async(void *data)
1860 tts_h tts = (tts_h)data;
1861 tts_client_s* client = tts_client_get(tts);
1864 if (NULL == client) {
1865 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1871 bool is_prepared = false;
1873 ret = tts_dbus_request_pause(client->uid);
1875 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1876 client->current_state = TTS_STATE_CREATED;
1877 if (0 == tts_prepare_sync(tts)) {
1879 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1881 } else if (TTS_ERROR_TIMED_OUT != ret) {
1882 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1885 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry pause : %s", __tts_get_error_code(ret));
1888 if (TTS_RETRY_COUNT == count) {
1889 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1897 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to pause tts : %s", __tts_get_error_code(ret));
1899 client->reason = ret;
1900 client->utt_id = -1;
1902 ecore_timer_add(0, __tts_notify_error, client->tts);
1906 client->before_state = client->current_state;
1907 client->current_state = TTS_STATE_PAUSED;
1909 if (NULL != client->state_changed_cb) {
1910 tts_client_use_callback(client);
1911 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1912 tts_client_not_use_callback(client);
1913 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1916 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1921 int tts_pause_async(tts_h tts)
1923 if (0 != __tts_get_feature_enabled()) {
1924 return TTS_ERROR_NOT_SUPPORTED;
1927 SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Pause tts");
1930 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1931 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1932 return TTS_ERROR_INVALID_PARAMETER;
1935 tts_client_s* client = tts_client_get(tts);
1937 if (NULL == client) {
1938 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1939 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1940 return TTS_ERROR_INVALID_PARAMETER;
1943 if (TTS_STATE_PLAYING != client->current_state) {
1944 SLOG(LOG_ERROR, TAG_TTSC, "[Error] The Current state is NOT 'playing'. So this request should be not running.");
1945 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1946 return TTS_ERROR_INVALID_STATE;
1949 if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1950 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1951 return TTS_ERROR_INVALID_STATE;
1954 ecore_main_loop_thread_safe_call_async(__tts_pause_async, (void*)tts);
1956 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1958 return TTS_ERROR_NONE;
1961 int tts_pause(tts_h tts)
1963 if (0 != __tts_get_feature_enabled()) {
1964 return TTS_ERROR_NOT_SUPPORTED;
1967 SLOG(LOG_INFO, TAG_TTSC, "@@@ Pause tts");
1970 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1971 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1972 return TTS_ERROR_INVALID_PARAMETER;
1975 tts_client_s* client = tts_client_get(tts);
1977 if (NULL == client) {
1978 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1979 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1980 return TTS_ERROR_INVALID_PARAMETER;
1983 if (TTS_STATE_PLAYING != client->current_state) {
1984 SLOG(LOG_ERROR, TAG_TTSC, "[Error] The Current state is NOT 'playing'. So this request should be not running.");
1985 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1986 return TTS_ERROR_INVALID_STATE;
1989 if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1990 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1991 return TTS_ERROR_INVALID_STATE;
1996 bool is_prepared = false;
1998 ret = tts_dbus_request_pause(client->uid);
2001 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2002 client->current_state = TTS_STATE_CREATED;
2003 if (0 == tts_prepare_sync(tts)) {
2005 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2007 } else if (TTS_ERROR_TIMED_OUT != ret) {
2008 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2011 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry pause : %s", __tts_get_error_code(ret));
2014 if (TTS_RETRY_COUNT == count) {
2015 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2023 client->before_state = client->current_state;
2024 client->current_state = TTS_STATE_PAUSED;
2026 if (NULL != client->state_changed_cb) {
2027 tts_client_use_callback(client);
2028 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
2029 tts_client_not_use_callback(client);
2030 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
2033 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2035 return TTS_ERROR_NONE;
2038 int tts_set_private_data(tts_h tts, const char* key, const char* data)
2040 if (0 != __tts_get_feature_enabled()) {
2041 return TTS_ERROR_NOT_SUPPORTED;
2044 SLOG(LOG_INFO, TAG_TTSC, "@@@ Set private data, key(%s), data(%s)", key, data);
2047 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
2048 return TTS_ERROR_INVALID_PARAMETER;
2051 if (NULL == key || NULL == data) {
2052 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid parameter");
2053 return TTS_ERROR_INVALID_PARAMETER;
2056 tts_client_s* client = tts_client_get(tts);
2058 if (NULL == client) {
2059 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
2060 return TTS_ERROR_INVALID_PARAMETER;
2063 if (TTS_STATE_READY != client->current_state) {
2064 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid state : Current state is NOT 'READY'");
2065 return TTS_ERROR_INVALID_STATE;
2068 if (true != client->internal && (0 == strcmp(key, "EnableServerTTS") || 0 == strcmp(key, "DisableServerTTS"))) {
2069 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] This is not an internal app");
2070 return TTS_ERROR_INVALID_PARAMETER;
2075 bool is_prepared = false;
2077 ret = tts_dbus_request_set_private_data(client->uid, key, data);
2080 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2081 client->current_state = TTS_STATE_CREATED;
2082 if (0 == tts_prepare_sync(tts)) {
2084 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2086 } else if (TTS_ERROR_TIMED_OUT != ret) {
2087 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2090 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry : %s", __tts_get_error_code(ret));
2093 if (TTS_RETRY_COUNT == count) {
2094 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2102 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2107 int tts_get_private_data(tts_h tts, const char* key, char** data)
2109 if (0 != __tts_get_feature_enabled()) {
2110 return TTS_ERROR_NOT_SUPPORTED;
2113 SLOG(LOG_INFO, TAG_TTSC, "@@@ Get private data, key(%s)", key);
2116 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
2117 return TTS_ERROR_INVALID_PARAMETER;
2120 if (NULL == key || NULL == data) {
2121 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid parameter");
2122 return TTS_ERROR_INVALID_PARAMETER;
2125 tts_client_s* client = tts_client_get(tts);
2127 if (NULL == client) {
2128 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
2129 return TTS_ERROR_INVALID_PARAMETER;
2132 if (TTS_STATE_READY != client->current_state) {
2133 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid state : Current state is NOT 'READY'");
2134 return TTS_ERROR_INVALID_STATE;
2139 bool is_prepared = false;
2141 ret = tts_dbus_request_get_private_data(client->uid, key, data);
2144 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2145 client->current_state = TTS_STATE_CREATED;
2146 if (0 == tts_prepare_sync(tts)) {
2148 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2150 } else if (TTS_ERROR_TIMED_OUT != ret) {
2151 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2154 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry : %s", __tts_get_error_code(ret));
2157 if (TTS_RETRY_COUNT == count) {
2158 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2166 if (0 == strncmp(*data, "NULL", strlen(*data))) {
2171 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2177 static Eina_Bool __tts_notify_error(void *data)
2179 tts_h tts = (tts_h)data;
2181 tts_client_s* client = tts_client_get(tts);
2184 if (NULL == client) {
2185 SLOG(LOG_WARN, TAG_TTSC, "Fail to notify error msg : A handle is not valid");
2189 SLOG(LOG_DEBUG, TAG_TTSC, "Error data : uttid(%d) reason(%s)", client->utt_id, __tts_get_error_code(client->reason));
2191 if (NULL != client->error_cb) {
2192 SLOG(LOG_DEBUG, TAG_TTSC, "Call callback function of error");
2193 tts_client_use_callback(client);
2194 g_err_callback_status = true;
2195 client->error_cb(client->tts, client->utt_id, client->reason, client->error_user_data);
2196 g_err_callback_status = false;
2197 tts_client_not_use_callback(client);
2199 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of error ");
2205 static void __start_reprepare_thread(void* data, Ecore_Thread* thread)
2207 SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] start reprepare thread. engine update status(%d)", g_engine_update_status);
2209 tts_client_s* temp = (tts_client_s*)data;
2211 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] data is null");
2216 while (!g_engine_update_status && (cnt < 10)) {
2217 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] wait for starting update");
2222 SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] update status(%d)", g_engine_update_status);
2224 while (g_engine_update_status && (NULL != g_pkgmgr)) {
2225 // SLOG(LOG_WARN, TAG_TTSC, "[WARNING] wait for finishing update");
2229 SLOG(LOG_INFO, TAG_TTSC, "[INFO] finish updating. request to prepare");
2231 if (0 != tts_prepare(temp->tts)) {
2232 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare");
2238 static void __end_reprepare_thread(void* data, Ecore_Thread* thread)
2240 SLOG(LOG_INFO, TAG_TTSC, "[INFO] end reprepare thread");
2243 int __tts_cb_error(int uid, tts_error_e reason, int utt_id, char* err_msg)
2246 GList* client_list = NULL;
2247 client_list = tts_client_get_client_list();
2250 tts_client_s *data = NULL;
2252 if (g_list_length(client_list) > 0) {
2253 /* Get a first item */
2254 iter = g_list_first(client_list);
2256 while (NULL != iter) {
2259 data->utt_id = utt_id;
2260 data->reason = reason;
2261 if (NULL != data->err_msg) {
2262 free(data->err_msg);
2263 data->err_msg = NULL;
2265 if (NULL != err_msg)
2266 data->err_msg = strdup(err_msg);
2268 /* call callback function */
2269 if (NULL != data->error_cb) {
2270 ecore_timer_add(0, __tts_notify_error, data->tts);
2272 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of error ");
2275 if (TTS_ERROR_SERVICE_RESET == reason) {
2276 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset");
2278 data->current_state = TTS_STATE_CREATED;
2280 ecore_thread_run(__start_reprepare_thread, __end_reprepare_thread, NULL, data);
2284 iter = g_list_next(iter);
2288 tts_client_s* client = tts_client_get_by_uid(uid);
2290 if (NULL == client) {
2291 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
2292 return TTS_ERROR_INVALID_PARAMETER;
2295 client->utt_id = utt_id;
2296 client->reason = reason;
2297 if (NULL != client->err_msg) {
2298 free(client->err_msg);
2299 client->err_msg = NULL;
2301 if (NULL != err_msg)
2302 client->err_msg = strdup(err_msg);
2304 /* call callback function */
2305 if (NULL != client->error_cb) {
2306 ecore_timer_add(0, __tts_notify_error, client->tts);
2308 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of error ");
2311 if (TTS_ERROR_SERVICE_RESET == reason) {
2312 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset");
2314 client->current_state = TTS_STATE_CREATED;
2316 ecore_thread_run(__start_reprepare_thread, __end_reprepare_thread, NULL, client);
2323 static Eina_Bool __tts_notify_state_changed(void *data)
2325 tts_h tts = (tts_h)data;
2327 tts_client_s* client = tts_client_get(tts);
2330 if (NULL == client) {
2331 SLOG(LOG_WARN, TAG_TTSC, "Fail to notify state changed : A handle is not valid");
2335 if (NULL != client->state_changed_cb) {
2336 tts_client_use_callback(client);
2337 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
2338 tts_client_not_use_callback(client);
2339 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called : pre(%d) cur(%d)", client->before_state, client->current_state);
2341 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] State changed callback is null");
2347 int __tts_cb_set_state(int uid, int state)
2349 tts_client_s* client = tts_client_get_by_uid(uid);
2350 if (NULL == client) {
2351 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] The handle is not valid");
2355 tts_state_e state_from_daemon = (tts_state_e)state;
2357 if (client->current_state == state_from_daemon) {
2358 SLOG(LOG_DEBUG, TAG_TTSC, "Current state has already been %d", client->current_state);
2362 if (NULL != client->state_changed_cb) {
2363 if (NULL != g_check_state_timer) {
2364 ecore_timer_del(g_check_state_timer);
2365 g_check_state_timer = NULL;
2367 g_check_state_timer = ecore_timer_add(0, __tts_notify_state_changed, client->tts);
2369 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] State changed callback is null");
2372 client->before_state = client->current_state;
2373 client->current_state = state_from_daemon;
2379 int __tts_cb_utt_started(int uid, int utt_id)
2381 tts_client_s* client = tts_client_get_by_uid(uid);
2383 if (NULL == client) {
2384 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
2385 return TTS_ERROR_INVALID_PARAMETER;
2388 SLOG(LOG_INFO, TAG_TTSC, "utterance started : utt id(%d) ", utt_id);
2390 client->utt_id = utt_id;
2392 /* call callback function */
2393 if (NULL != client->utt_started_cb) {
2394 SLOG(LOG_DEBUG, TAG_TTSC, "Call callback function of utterance started ");
2395 tts_client_use_callback(client);
2396 client->utt_started_cb(client->tts, client->utt_id, client->utt_started_user_data);
2397 tts_client_not_use_callback(client);
2399 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of utterance started ");
2405 int __tts_cb_utt_completed(int uid, int utt_id)
2407 tts_client_s* client = tts_client_get_by_uid(uid);
2409 if (NULL == client) {
2410 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
2411 return TTS_ERROR_INVALID_PARAMETER;
2414 SLOG(LOG_INFO, TAG_TTSC, "utterance completed : uttid(%d) ", utt_id);
2416 client->utt_id = utt_id;
2418 /* call callback function */
2419 if (NULL != client->utt_completeted_cb) {
2420 SLOG(LOG_DEBUG, TAG_TTSC, "Call callback function of utterance completed ");
2421 tts_client_use_callback(client);
2422 client->utt_completeted_cb(client->tts, client->utt_id, client->utt_completed_user_data);
2423 tts_client_not_use_callback(client);
2425 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of utterance completed ");
2431 int tts_set_state_changed_cb(tts_h tts, tts_state_changed_cb callback, void* user_data)
2433 if (0 != __tts_get_feature_enabled()) {
2434 return TTS_ERROR_NOT_SUPPORTED;
2437 if (NULL == tts || NULL == callback) {
2438 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set state changed cb : Input parameter is null");
2439 return TTS_ERROR_INVALID_PARAMETER;
2442 tts_client_s* client = tts_client_get(tts);
2444 if (NULL == client) {
2445 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set state changed cb : A handle is not valid");
2446 return TTS_ERROR_INVALID_PARAMETER;
2449 if (TTS_STATE_CREATED != client->current_state) {
2450 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set state changed cb : Current state is not 'Created'.");
2451 return TTS_ERROR_INVALID_STATE;
2454 client->state_changed_cb = callback;
2455 client->state_changed_user_data = user_data;
2457 SLOG(LOG_INFO, TAG_TTSC, "[SUCCESS] Set state changed cb");
2462 int tts_unset_state_changed_cb(tts_h tts)
2464 if (0 != __tts_get_feature_enabled()) {
2465 return TTS_ERROR_NOT_SUPPORTED;
2469 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset state changed cb : Input parameter is null");
2470 return TTS_ERROR_INVALID_PARAMETER;
2473 tts_client_s* client = tts_client_get(tts);
2475 if (NULL == client) {
2476 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset state changed cb : A handle is not valid");
2477 return TTS_ERROR_INVALID_PARAMETER;
2480 if (TTS_STATE_CREATED != client->current_state) {
2481 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset state changed cb : Current state is not 'Created'.");
2482 return TTS_ERROR_INVALID_STATE;
2485 client->state_changed_cb = NULL;
2486 client->state_changed_user_data = NULL;
2488 if (NULL != g_check_state_timer) {
2489 ecore_timer_del(g_check_state_timer);
2490 g_check_state_timer = NULL;
2493 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset state changed cb");
2498 int tts_set_utterance_started_cb(tts_h tts, tts_utterance_started_cb callback, void* user_data)
2500 if (0 != __tts_get_feature_enabled()) {
2501 return TTS_ERROR_NOT_SUPPORTED;
2504 if (NULL == tts || NULL == callback) {
2505 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt started cb : Input parameter is null");
2506 return TTS_ERROR_INVALID_PARAMETER;
2509 tts_client_s* client = tts_client_get(tts);
2511 if (NULL == client) {
2512 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt started cb : A handle is not valid");
2513 return TTS_ERROR_INVALID_PARAMETER;
2516 if (TTS_STATE_CREATED != client->current_state) {
2517 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt started cb : Current state is not 'Created'.");
2518 return TTS_ERROR_INVALID_STATE;
2521 client->utt_started_cb = callback;
2522 client->utt_started_user_data = user_data;
2524 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set utt started cb");
2529 int tts_unset_utterance_started_cb(tts_h tts)
2531 if (0 != __tts_get_feature_enabled()) {
2532 return TTS_ERROR_NOT_SUPPORTED;
2536 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt started cb : Input parameter is null");
2537 return TTS_ERROR_INVALID_PARAMETER;
2540 tts_client_s* client = tts_client_get(tts);
2542 if (NULL == client) {
2543 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt started cb : A handle is not valid");
2544 return TTS_ERROR_INVALID_PARAMETER;
2547 if (TTS_STATE_CREATED != client->current_state) {
2548 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt started cb : Current state is not 'Created'.");
2549 return TTS_ERROR_INVALID_STATE;
2552 client->utt_started_cb = NULL;
2553 client->utt_started_user_data = NULL;
2555 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset utt started cb");
2560 int tts_set_utterance_completed_cb(tts_h tts, tts_utterance_completed_cb callback, void* user_data)
2562 if (0 != __tts_get_feature_enabled()) {
2563 return TTS_ERROR_NOT_SUPPORTED;
2566 if (NULL == tts || NULL == callback) {
2567 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt completed cb : Input parameter is null");
2568 return TTS_ERROR_INVALID_PARAMETER;
2571 tts_client_s* client = tts_client_get(tts);
2573 if (NULL == client) {
2574 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt completed cb : A handle is not valid");
2575 return TTS_ERROR_INVALID_PARAMETER;
2578 if (TTS_STATE_CREATED != client->current_state) {
2579 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt completed cb : Current state is not 'Created'.");
2580 return TTS_ERROR_INVALID_STATE;
2583 client->utt_completeted_cb = callback;
2584 client->utt_completed_user_data = user_data;
2586 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set utt completed cb");
2591 int tts_unset_utterance_completed_cb(tts_h tts)
2593 if (0 != __tts_get_feature_enabled()) {
2594 return TTS_ERROR_NOT_SUPPORTED;
2598 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt completed cb : Input parameter is null");
2599 return TTS_ERROR_INVALID_PARAMETER;
2602 tts_client_s* client = tts_client_get(tts);
2604 if (NULL == client) {
2605 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt completed cb : A handle is not valid");
2606 return TTS_ERROR_INVALID_PARAMETER;
2609 if (TTS_STATE_CREATED != client->current_state) {
2610 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt completed cb : Current state is not 'Created'.");
2611 return TTS_ERROR_INVALID_STATE;
2614 client->utt_completeted_cb = NULL;
2615 client->utt_completed_user_data = NULL;
2617 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset utt completed cb");
2621 int tts_set_error_cb(tts_h tts, tts_error_cb callback, void* user_data)
2623 if (0 != __tts_get_feature_enabled()) {
2624 return TTS_ERROR_NOT_SUPPORTED;
2627 if (NULL == tts || NULL == callback) {
2628 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set error cb : Input parameter is null");
2629 return TTS_ERROR_INVALID_PARAMETER;
2632 tts_client_s* client = tts_client_get(tts);
2634 if (NULL == client) {
2635 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set error cb : A handle is not valid");
2636 return TTS_ERROR_INVALID_PARAMETER;
2639 if (TTS_STATE_CREATED != client->current_state) {
2640 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set error cb : Current state is not 'Created'.");
2641 return TTS_ERROR_INVALID_STATE;
2644 client->error_cb = callback;
2645 client->error_user_data = user_data;
2647 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set error cb");
2652 int tts_unset_error_cb(tts_h tts)
2654 if (0 != __tts_get_feature_enabled()) {
2655 return TTS_ERROR_NOT_SUPPORTED;
2659 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset error cb : Input parameter is null");
2660 return TTS_ERROR_INVALID_PARAMETER;
2663 tts_client_s* client = tts_client_get(tts);
2665 if (NULL == client) {
2666 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset error cb : A handle is not valid");
2667 return TTS_ERROR_INVALID_PARAMETER;
2670 if (TTS_STATE_CREATED != client->current_state) {
2671 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset error cb : Current state is not 'Created'.");
2672 return TTS_ERROR_INVALID_STATE;
2675 client->error_cb = NULL;
2676 client->error_user_data = NULL;
2678 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset error cb");
2683 int tts_set_default_voice_changed_cb(tts_h tts, tts_default_voice_changed_cb callback, void* user_data)
2685 if (0 != __tts_get_feature_enabled()) {
2686 return TTS_ERROR_NOT_SUPPORTED;
2689 if (NULL == tts || NULL == callback) {
2690 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set default voice changed cb : Input parameter is null");
2691 return TTS_ERROR_INVALID_PARAMETER;
2694 tts_client_s* client = tts_client_get(tts);
2696 if (NULL == client) {
2697 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set default voice changed cb : A handle is not valid");
2698 return TTS_ERROR_INVALID_PARAMETER;
2701 if (TTS_STATE_CREATED != client->current_state) {
2702 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set default voice changed cb : Current state is not 'Created'.");
2703 return TTS_ERROR_INVALID_STATE;
2706 client->default_voice_changed_cb = callback;
2707 client->default_voice_changed_user_data = user_data;
2709 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set default voice changed cb");
2714 int tts_unset_default_voice_changed_cb(tts_h tts)
2716 if (0 != __tts_get_feature_enabled()) {
2717 return TTS_ERROR_NOT_SUPPORTED;
2721 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset default voice changed cb : Input parameter is null");
2722 return TTS_ERROR_INVALID_PARAMETER;
2725 tts_client_s* client = tts_client_get(tts);
2727 if (NULL == client) {
2728 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset default voice changed cb : A handle is not valid");
2729 return TTS_ERROR_INVALID_PARAMETER;
2732 if (TTS_STATE_CREATED != client->current_state) {
2733 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset default voice changed cb : Current state is not 'Created'.");
2734 return TTS_ERROR_INVALID_STATE;
2737 client->default_voice_changed_cb = NULL;
2738 client->default_voice_changed_user_data = NULL;
2740 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset default voice changed cb");
2745 int tts_set_engine_changed_cb(tts_h tts, tts_engine_changed_cb callback, void* user_data)
2747 if (0 != __tts_get_feature_enabled()) {
2748 return TTS_ERROR_NOT_SUPPORTED;
2751 if (NULL == tts || NULL == callback) {
2752 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : Input parameter is null");
2753 return TTS_ERROR_INVALID_PARAMETER;
2756 tts_client_s* client = tts_client_get(tts);
2758 if (NULL == client) {
2759 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : A handle is not valid");
2760 return TTS_ERROR_INVALID_PARAMETER;
2763 if (TTS_STATE_CREATED != client->current_state) {
2764 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : Current state is not 'Created'.");
2765 return TTS_ERROR_INVALID_STATE;
2768 client->engine_changed_cb = callback;
2769 client->engine_changed_user_data = user_data;
2771 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set engine changed cb");
2776 int tts_unset_engine_changed_cb(tts_h tts)
2778 if (0 != __tts_get_feature_enabled()) {
2779 return TTS_ERROR_NOT_SUPPORTED;
2783 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : Input parameter is null");
2784 return TTS_ERROR_INVALID_PARAMETER;
2787 tts_client_s* client = tts_client_get(tts);
2789 if (NULL == client) {
2790 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : A handle is not valid");
2791 return TTS_ERROR_INVALID_PARAMETER;
2794 if (TTS_STATE_CREATED != client->current_state) {
2795 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : Current state is not 'Created'.");
2796 return TTS_ERROR_INVALID_STATE;
2799 client->engine_changed_cb = NULL;
2800 client->engine_changed_user_data = NULL;
2802 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset engine changed cb");
2808 int tts_add_pcm(tts_h tts, int event, const void* data, unsigned int data_size, int audio_type, int rate)
2810 if (0 != __tts_get_feature_enabled()) {
2811 return TTS_ERROR_NOT_SUPPORTED;
2814 SLOG(LOG_INFO, TAG_TTSC, "@@@ Add pcm tts");
2817 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
2818 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2819 return TTS_ERROR_INVALID_PARAMETER;
2822 tts_client_s* client = tts_client_get(tts);
2824 if (NULL == client) {
2825 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
2826 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2827 return TTS_ERROR_INVALID_PARAMETER;
2830 if (TTS_STATE_CREATED == client->current_state) {
2831 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
2832 return TTS_ERROR_INVALID_STATE;
2835 if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
2836 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
2837 return TTS_ERROR_INVALID_STATE;
2842 bool is_prepared = false;
2844 ret = tts_dbus_request_add_pcm(client->uid, event, data, data_size, audio_type, rate);
2846 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2847 client->current_state = TTS_STATE_CREATED;
2848 if (0 == tts_prepare_sync(tts)) {
2850 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2852 } else if (TTS_ERROR_TIMED_OUT != ret) {
2853 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2856 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry add pcm : %s", __tts_get_error_code(ret));
2859 if (TTS_RETRY_COUNT == count) {
2860 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2867 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2869 return TTS_ERROR_NONE;
2872 int tts_play_pcm(tts_h tts)
2874 if (0 != __tts_get_feature_enabled()) {
2875 return TTS_ERROR_NOT_SUPPORTED;
2878 SLOG(LOG_INFO, TAG_TTSC, "@@@ Play pcm tts");
2881 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
2882 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2883 return TTS_ERROR_INVALID_PARAMETER;
2886 tts_client_s* client = tts_client_get(tts);
2888 if (NULL == client) {
2889 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
2890 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2891 return TTS_ERROR_INVALID_PARAMETER;
2894 if (TTS_STATE_PLAYING == client->current_state || TTS_STATE_CREATED == client->current_state) {
2895 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
2896 return TTS_ERROR_INVALID_STATE;
2899 if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
2900 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
2901 return TTS_ERROR_INVALID_STATE;
2906 bool is_prepared = false;
2908 ret = tts_dbus_request_play_pcm(client->uid);
2910 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2911 client->current_state = TTS_STATE_CREATED;
2912 if (0 == tts_prepare_sync(tts)) {
2914 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2916 } else if (TTS_ERROR_TIMED_OUT != ret) {
2917 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2920 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play pcm : %s", __tts_get_error_code(ret));
2923 if (TTS_RETRY_COUNT == count) {
2924 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2931 client->before_state = client->current_state;
2932 client->current_state = TTS_STATE_PLAYING;
2934 if (NULL != client->state_changed_cb) {
2935 tts_client_use_callback(client);
2936 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
2937 tts_client_not_use_callback(client);
2938 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
2941 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2943 return TTS_ERROR_NONE;
2946 int tts_stop_pcm(tts_h tts)
2948 if (0 != __tts_get_feature_enabled()) {
2949 return TTS_ERROR_NOT_SUPPORTED;
2952 SLOG(LOG_INFO, TAG_TTSC, "@@@ Stop pcm tts");
2955 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
2956 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2957 return TTS_ERROR_INVALID_PARAMETER;
2960 tts_client_s* client = tts_client_get(tts);
2962 if (NULL == client) {
2963 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
2964 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2965 return TTS_ERROR_INVALID_PARAMETER;
2968 if (TTS_STATE_CREATED == client->current_state) {
2969 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
2970 return TTS_ERROR_INVALID_STATE;
2973 if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
2974 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
2975 return TTS_ERROR_INVALID_STATE;
2980 bool is_prepared = false;
2982 ret = tts_dbus_request_stop_pcm(client->uid);
2984 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2985 client->current_state = TTS_STATE_CREATED;
2986 if (0 == tts_prepare_sync(tts)) {
2988 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2990 } else if (TTS_ERROR_TIMED_OUT != ret) {
2991 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2994 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry stop pcm : %s", __tts_get_error_code(ret));
2997 if (TTS_RETRY_COUNT == count) {
2998 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
3005 client->before_state = client->current_state;
3006 client->current_state = TTS_STATE_READY;
3008 if (NULL != client->state_changed_cb) {
3009 tts_client_use_callback(client);
3010 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
3011 tts_client_not_use_callback(client);
3012 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
3015 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
3017 return TTS_ERROR_NONE;
3021 int tts_repeat(tts_h tts, char** text_repeat, int* utt_id)
3023 if (0 != __tts_get_feature_enabled()) {
3024 return TTS_ERROR_NOT_SUPPORTED;
3027 SLOG(LOG_INFO, TAG_TTSC, "@@@ Repeat TTS");
3030 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
3031 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
3032 return TTS_ERROR_INVALID_PARAMETER;
3035 if (NULL == text_repeat || NULL == utt_id) {
3036 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null.");
3037 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
3038 return TTS_ERROR_INVALID_PARAMETER;
3041 *text_repeat = NULL;
3044 tts_client_s* client = tts_client_get(tts);
3046 if (NULL == client) {
3047 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
3048 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
3049 return TTS_ERROR_INVALID_PARAMETER;
3052 if (TTS_STATE_READY != client->current_state) {
3053 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid. (%d)", client->current_state);
3054 return TTS_ERROR_INVALID_STATE;
3057 /* Clear the legacy and Add texts to be played repeatedly */
3059 ret = tts_stop(tts);
3060 if (TTS_ERROR_NONE != ret) {
3061 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to clear the legacy");
3065 if (NULL != client->text_repeat) {
3066 char* tmp_text = strdup(client->text_repeat);
3067 char* tmp_lang = NULL;
3068 if (NULL != g_language) {
3069 tmp_lang = strdup(g_language);
3071 ret = tts_add_text(tts, tmp_text, tmp_lang, g_voice_type, g_speed, utt_id);
3072 if (TTS_ERROR_NONE != ret) {
3073 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to add texts for repetition.");
3074 if (NULL != tmp_text) {
3078 if (NULL != tmp_lang) {
3084 *text_repeat = strdup(client->text_repeat);
3085 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] text to repeat(%s), utt_id(%d)", (*text_repeat) ? *text_repeat : "NULL", *utt_id);
3086 if (NULL != tmp_text) {
3090 if (NULL != tmp_lang) {
3095 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] There is no previous added texts. Please add texts");
3096 return TTS_ERROR_OPERATION_FAILED;
3099 /* Play added texts */
3100 ret = tts_play(tts);
3101 if (TTS_ERROR_NONE != ret) {
3102 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to play texts for repetition.");
3103 if (NULL != *text_repeat) {
3105 *text_repeat = NULL;
3111 return TTS_ERROR_NONE;