94432c853a5c21d9463f119b1d74a48e1edf58ed
[platform/core/uifw/tts.git] / client / tts.c
1 /*
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.
12 */
13
14 #include <app_manager.h>
15 #include <dirent.h>
16 #include <Ecore.h>
17 #include <iconv.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <system_info.h>
22 #include <vconf.h>
23 #include <package-manager.h>
24 #include <pthread.h>
25
26 #include "tts.h"
27 #include "tts_client.h"
28 #include "tts_config_mgr.h"
29 #include "tts_main.h"
30 #include "tts_core.h"
31 #include "tts_ipc.h"
32 #include "tts_dlog.h"
33
34 #include "tts_internal.h"
35
36 static int g_feature_enabled = -1;
37
38
39 static int __tts_get_feature_enabled()
40 {
41         RETV_IF(1 == g_feature_enabled, TTS_ERROR_NONE);
42         RETVM_IF(0 == g_feature_enabled, TTS_ERROR_NOT_SUPPORTED, "[ERROR] TTS feature NOT supported");
43
44         bool tts_supported = false;
45         if (SYSTEM_INFO_ERROR_NONE != system_info_get_platform_bool(TTS_FEATURE_PATH, &tts_supported)) {
46                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get feature value");
47                 return TTS_ERROR_NOT_SUPPORTED;
48         }
49
50         if (false == tts_supported) {
51                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] TTS feature NOT supported");
52                 g_feature_enabled = 0;
53                 return TTS_ERROR_NOT_SUPPORTED;
54         }
55
56         g_feature_enabled = 1;
57         return TTS_ERROR_NONE;
58 }
59
60 // TODO: move into tts_core to make common function
61 //LCOV_EXCL_START
62 static int __tts_convert_config_error_code(tts_config_error_e code)
63 {
64         if (code == TTS_CONFIG_ERROR_NONE)                      return TTS_ERROR_NONE;
65         if (code == TTS_CONFIG_ERROR_OUT_OF_MEMORY)             return TTS_ERROR_OUT_OF_MEMORY;
66         if (code == TTS_CONFIG_ERROR_IO_ERROR)                  return TTS_ERROR_IO_ERROR;
67         if (code == TTS_CONFIG_ERROR_INVALID_PARAMETER)         return TTS_ERROR_INVALID_PARAMETER;
68         if (code == TTS_CONFIG_ERROR_INVALID_STATE)             return TTS_ERROR_INVALID_STATE;
69         if (code == TTS_CONFIG_ERROR_INVALID_VOICE)             return TTS_ERROR_INVALID_VOICE;
70         if (code == TTS_CONFIG_ERROR_ENGINE_NOT_FOUND)          return TTS_ERROR_ENGINE_NOT_FOUND;
71         if (code == TTS_CONFIG_ERROR_OPERATION_FAILED)          return TTS_ERROR_OPERATION_FAILED;
72         if (code == TTS_CONFIG_ERROR_NOT_SUPPORTED_FEATURE)     return TTS_ERROR_NOT_SUPPORTED_FEATURE;
73
74         return code;
75 }
76
77 static 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)
78 {
79         SLOG(LOG_DEBUG, TAG_TTSC, "Voice changed : Before lang(%s) type(%d) , Current lang(%s), type(%d)",
80                 before_lang, before_voice_type, language, voice_type);
81
82         GList* client_list = tts_client_get_client_list();
83         if (NULL == client_list) {
84                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get client list");
85                 return;
86         }
87
88         GList *iter = NULL;
89         if (g_list_length(client_list) > 0) {
90                 /* Get a first item */
91                 iter = g_list_first(client_list);
92
93                 while (NULL != iter) {
94                         tts_client_s *client = iter->data;
95                         if (tts_client_is_valid_client(client)) {
96                                 tts_core_notify_default_voice_changed(client, before_lang, before_voice_type, language, voice_type);
97
98                                 /* Check whether language is changed or not. If it is changed, make 'text_repeat' NULL */
99                                 if (0 != strncmp(before_lang, language, strlen(before_lang))) {
100                                         tts_client_set_repeat_text(client, NULL);
101                                 }
102                         } else {
103                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Client is not valid (%p)", client);
104                         }
105
106                         /* Next item */
107                         iter = g_list_next(iter);
108                 }
109         }
110
111         g_list_free(client_list);
112
113         return;
114 }
115 /*
116 static Eina_Bool __reconnect_by_engine_changed(void* data)
117 {
118         tts_h tts = (tts_h)data;
119
120         tts_client_s* client = tts_client_get(tts);
121         if (NULL == client) {
122                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid. tts(%p)", tts);
123                 return EINA_FALSE;
124         }
125
126         tts_state_e current_state = tts_client_get_current_state(client);
127         if (TTS_STATE_READY != current_state) {
128                 usleep(10000);
129                 return EINA_TRUE;
130         }
131
132         // TODO: change to tts_core function
133         int ret = tts_unprepare(tts);
134         if (0 != ret) {
135                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
136         }
137         ret = tts_prepare(tts);
138         if (0 != ret) {
139                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
140         }
141
142         return EINA_FALSE;
143 }
144 */
145 static 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)
146 {
147         tts_h tts = (tts_h)user_data;
148
149         tts_client_s* client = tts_client_get(tts);
150         RETM_IF(NULL == client, "[ERROR] A handle is not valid. tts(%p)", tts);
151
152         if (NULL != engine_id)  SLOG(LOG_DEBUG, TAG_TTSC, "Engine id(%s)", engine_id);
153         if (NULL != setting)    SLOG(LOG_DEBUG, TAG_TTSC, "Engine setting(%s)", setting);
154         if (NULL != language)   SLOG(LOG_DEBUG, TAG_TTSC, "Language(%s)", language);
155         SLOG(LOG_DEBUG, TAG_TTSC, "Voice type(%d), Auto voice(%s), Credential(%s)", voice_type, auto_voice ? "on" : "off", need_credential ? "need" : "no need");
156
157         /* When the default engine is changed, please unload the old engine and load the new one. */
158 /*      int ret = -1;
159
160         tts_state_e current_state = tts_client_get_current_state(client);
161         if (TTS_STATE_PLAYING == current_state || TTS_STATE_PAUSED == current_state) {
162                 ret = tts_stop(tts);
163                 if (0 != ret) {
164                         SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] TTS client stopping...");
165                 }
166
167                 ecore_idler_add(__reconnect_by_engine_changed, (void*)tts);
168         } else if (TTS_STATE_READY == current_state) {
169                 // TODO: change to tts_core function
170                 ret = tts_unprepare(tts);
171                 if (0 != ret) {
172                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to unprepare for setting a new engine... (%d)", ret);
173                 }
174                 ret = tts_prepare(tts);
175                 if (0 != ret) {
176                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
177                 }
178         }
179 */
180         /* call callback function */
181         tts_core_notify_engine_changed(client, engine_id, language, voice_type, need_credential);
182         return;
183 }
184 //LCOV_EXCL_STOP
185
186 static void __tts_config_screen_reader_changed_cb(bool value, void* user_data)
187 {
188         tts_h tts = (tts_h)user_data;
189
190         tts_client_s* client = tts_client_get(tts);
191         RETM_IF(NULL == client, "[ERROR] A handle is not valid. tts(%p)", tts);
192
193         SLOG(LOG_DEBUG, TAG_TTSC, "Screen reader is changed. current status (%d)", value);
194
195         /* call callback function */
196         tts_core_notify_screen_reader_changed(client, value);
197 }
198
199 static int __initialize_tts_config(unsigned int uid, tts_h new_tts)
200 {
201         int ret = tts_config_mgr_initialize(uid, TTS_CONFIG_CLIENT_TYPE_DEFAULT);
202         if (TTS_CONFIG_ERROR_NONE != ret) {
203                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to init config manager : %d", ret);
204                 return __tts_convert_config_error_code(ret);
205         }
206
207         ret = tts_config_mgr_set_callback(uid, __tts_config_engine_changed_cb, __tts_config_voice_changed_cb, NULL, NULL, NULL, new_tts);
208         if (TTS_CONFIG_ERROR_NONE != ret) {
209                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to set config changed : %d", ret);
210                 tts_config_mgr_finalize(uid, TTS_CONFIG_CLIENT_TYPE_DEFAULT);
211                 return __tts_convert_config_error_code(ret);
212         }
213
214         ret = tts_config_mgr_set_screen_reader_callback(uid, __tts_config_screen_reader_changed_cb, new_tts);
215         if (TTS_CONFIG_ERROR_NONE != ret) {
216                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to set config changed : %d", ret);
217                 tts_config_mgr_finalize(uid, TTS_CONFIG_CLIENT_TYPE_DEFAULT);
218                 return __tts_convert_config_error_code(ret);
219         }
220
221         return TTS_ERROR_NONE;
222 }
223
224 int tts_create(tts_h* tts)
225 {
226         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
227
228         SLOG(LOG_INFO, TAG_TTSC, "@@@ Create TTS");
229
230         RETVM_IF(NULL == tts, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Input handle is null");
231
232         bool is_first_client = false;
233         if (0 == tts_client_get_size()) {
234                 is_first_client = true;
235         }
236
237         tts_h new_tts = NULL;
238         if (0 != tts_client_new(&new_tts)) {
239                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to create client!!!!!");
240                 return TTS_ERROR_OUT_OF_MEMORY;
241         }
242
243         tts_client_s* client = tts_client_get(new_tts);
244         if (NULL == client) {
245                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get client");
246                 tts_client_destroy(new_tts);
247                 return TTS_ERROR_OPERATION_FAILED;
248         }
249
250         unsigned int uid = tts_client_get_uid(client);
251         SLOG(LOG_ERROR, TAG_TTSC, "[INFO] tts_h(%p), tts_client(%p), uid(%u)", tts, client, uid);
252
253         if (false == tts_ipc_is_method_set()) {
254                 int pid = getpid();
255                 char* appid = NULL;
256                 int ret = app_manager_get_app_id(pid, &appid);
257                 if (0 != ret || NULL == appid) {
258                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get appid, ret(%d), pid(%d)", ret, pid);
259                         tts_ipc_set_method(TTS_IPC_METHOD_DBUS);
260                 } else {
261                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] tts_create : client appid(%s), pid(%d)", appid, pid);
262                         tts_ipc_set_method(TTS_IPC_METHOD_TIDL);
263                 }
264
265                 if (NULL != appid) {
266                         free(appid);
267                         appid = NULL;
268                 }
269         }
270
271         if (0 != tts_ipc_open_connection(uid)) {
272                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to open ipc connection");
273                 tts_client_destroy(new_tts);
274                 return TTS_ERROR_OPERATION_FAILED;
275         }
276
277         int ret = __initialize_tts_config(uid, new_tts);
278         if (TTS_ERROR_NONE != ret) {
279                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to init config manager : %d", ret);
280                 tts_client_destroy(new_tts);
281                 return ret;
282         }
283
284         if (is_first_client) {
285                 // These function would be called only when first client is created.
286                 if (0 != tts_core_initialize()) {
287                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize core");
288                         tts_config_mgr_finalize(uid, TTS_CONFIG_CLIENT_TYPE_DEFAULT);
289                         tts_client_destroy(new_tts);
290                         return TTS_ERROR_OPERATION_FAILED;
291                 }
292         }
293
294         *tts = new_tts;
295         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
296         return TTS_ERROR_NONE;
297 }
298
299 int tts_destroy(tts_h tts)
300 {
301         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
302
303         SLOG(LOG_INFO, TAG_TTSC, "@@@ Destroy TTS");
304
305         tts_client_s* client = tts_client_get(tts);
306         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
307
308         unsigned int uid = tts_client_get_uid(client);
309         SLOG(LOG_ERROR, TAG_TTSC, "[INFO] tts_h(%p), tts_client(%p), uid(%u)", tts, client, uid);
310
311         /* check used callback */
312         if (0 != tts_client_get_use_callback(client)) {
313                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Cannot destroy in Callback function");
314                 return TTS_ERROR_OPERATION_FAILED;
315         }
316
317         tts_config_mgr_finalize(uid, TTS_CONFIG_CLIENT_TYPE_DEFAULT);
318
319         // TODO: move into tts_client
320         if (client->hello_timer) {
321                 ecore_timer_del(client->hello_timer);
322                 client->hello_timer = NULL;
323         }
324
325         switch (tts_client_get_current_state(client)) {
326         case TTS_STATE_PAUSED:
327         case TTS_STATE_PLAYING:
328         case TTS_STATE_READY:
329                 if (0 != tts_core_unprepare(client)) {
330                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to unprepare");
331                 }
332                 tts_client_set_current_state(client, TTS_STATE_CREATED);
333         case TTS_STATE_CREATED:
334                 /* Unset registered callbacks */
335                 tts_client_unset_all_cb(client);
336
337                 int thread_count = ecore_thread_active_get();
338                 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Active thread count: %d", thread_count);
339                 int cnt = 0;
340                 while (0 < thread_count) {
341                         usleep(50000);
342                         cnt++;
343                         if (30 == cnt) {
344                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNNING] Thread is blocked, %d", thread_count);
345                                 break;
346                         }
347                         thread_count = ecore_thread_active_get();
348                 }
349
350                 if (0 != tts_ipc_close_connection(uid)) {
351                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to close connection");
352                         return TTS_ERROR_OPERATION_FAILED;
353                 }
354
355                 /* Free resources */
356                 tts_client_destroy(tts);
357
358                 break;
359         default:
360                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Client is not valid");
361                 return TTS_ERROR_INVALID_PARAMETER;
362         }
363
364         unsigned int num_of_client = tts_client_get_size();
365         SLOG(LOG_ERROR, TAG_TTSC, "[INFO] num_of_client(%u)", num_of_client);
366
367         if (0 == num_of_client) {
368                 if (0 != tts_core_deinitialize()) {
369                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to deinitialize core");
370                 }
371         }
372
373         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
374         return TTS_ERROR_NONE;
375 }
376
377 int tts_set_mode(tts_h tts, tts_mode_e mode)
378 {
379         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
380
381         SLOG(LOG_INFO, TAG_TTSC, "@@@ Set TTS mode(%d)", mode);
382
383         RETVM_IF(TTS_MODE_DEFAULT > mode || TTS_MODE_INTERRUPT < mode, TTS_ERROR_INVALID_PARAMETER, "[ERROR] mode is not valid : %d", mode);
384
385         tts_client_s* client = tts_client_get(tts);
386         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
387
388         tts_state_e current_state = tts_client_get_current_state(client);
389         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
390
391         tts_client_set_mode(client, mode);
392
393         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
394         return TTS_ERROR_NONE;
395 }
396
397 int tts_get_mode(tts_h tts, tts_mode_e* mode)
398 {
399         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
400
401         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Get TTS mode");
402
403         RETVM_IF(NULL == mode, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter(mode) is NULL");
404
405         tts_client_s* client = tts_client_get(tts);
406         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
407
408         tts_state_e current_state = tts_client_get_current_state(client);
409         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
410
411         *mode = tts_client_get_mode(client);
412
413         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
414         return TTS_ERROR_NONE;
415 }
416
417 int tts_set_credential(tts_h tts, const char* credential)
418 {
419         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
420
421         RETVM_IF(NULL == credential, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is null");
422
423         tts_client_s* client = tts_client_get(tts);
424         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
425
426         tts_state_e current_state = tts_client_get_current_state(client);
427         RETVM_IF(TTS_STATE_CREATED != current_state && TTS_STATE_READY != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
428
429         tts_client_set_credential_key(client, credential);
430
431         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
432         return TTS_ERROR_NONE;
433 }
434
435 int tts_set_server_tts(tts_h tts, const char* credential)
436 {
437         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
438
439         tts_client_s* client = tts_client_get(tts);
440         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
441
442         // TODO: fix state check, because set_private_data runs only ready state
443         tts_state_e current_state = tts_client_get_current_state(client);
444         RETVM_IF(TTS_STATE_CREATED != current_state && TTS_STATE_READY != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
445
446         // TODO: fix name more clear and move into tts_client
447         client->internal = true;
448
449         char* key = NULL;
450         if (NULL != credential) {
451                 key = "EnableServerTTS";
452                 tts_client_set_credential_key(client, credential);
453                 if (NULL == tts_client_get_credential_key(client)) {
454                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
455                         return TTS_ERROR_OUT_OF_MEMORY;
456                 }
457         } else {
458                 key = "DisableServerTTS";
459         }
460
461         int pid = getpid();
462         char* appid = NULL;
463         int ret = app_manager_get_app_id(pid, &appid);
464         if (0 != ret || NULL == appid) {
465                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get appid, ret(%d), pid(%d), appid(%s)", ret, pid, appid);;
466                 if (NULL != appid) {
467                         free(appid);
468                         appid = NULL;
469                 }
470                 return TTS_ERROR_OPERATION_FAILED;
471         }
472
473         ret = tts_core_set_private_data(client, key, appid);
474         if (0 != ret) {
475                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to set private data, ret(%d), pid(%d), appid(%s)", ret, pid, appid);
476                 free(appid);
477                 appid = NULL;
478                 return ret;
479         }
480
481         free(appid);
482         appid = NULL;
483
484         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
485         return TTS_ERROR_NONE;
486 }
487
488 int tts_prepare(tts_h tts)
489 {
490         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
491
492         tts_client_s* client = tts_client_get(tts);
493         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
494
495         tts_state_e current_state = tts_client_get_current_state(client);
496         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
497
498         SLOG(LOG_INFO, TAG_TTSC, "@@@ Prepare TTS");
499         int ret = tts_core_prepare(client);
500         if (0 != ret) {
501                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] tts_core_prepare failed. (%s)", tts_core_covert_error_code(ret));
502                 return ret;
503         }
504
505         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
506         return TTS_ERROR_NONE;
507 }
508
509 int tts_prepare_sync(tts_h tts)
510 {
511         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
512
513         tts_client_s* client = tts_client_get(tts);
514         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
515
516         tts_state_e current_state = tts_client_get_current_state(client);
517         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
518
519         SLOG(LOG_INFO, TAG_TTSC, "@@@ Prepare TTS synchronously");
520         int ret = tts_core_prepare_sync(client);
521         if (TTS_ERROR_NONE != ret) {
522                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] tts_core_prepare_sync failed. (%s)", tts_core_covert_error_code(ret));
523                 return ret;
524         }
525
526         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
527         return TTS_ERROR_NONE;
528 }
529
530 int tts_unprepare(tts_h tts)
531 {
532         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
533
534         tts_client_s* client = tts_client_get(tts);
535         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
536
537         tts_state_e current_state = tts_client_get_current_state(client);
538         RETVM_IF(TTS_STATE_READY != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
539
540         SLOG(LOG_INFO, TAG_TTSC, "@@@ Unprepare TTS");
541         int ret = tts_core_unprepare(client);
542         if (0 != ret) {
543                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] tts_core_unprepare is failed(%s)", tts_core_covert_error_code(ret));
544                 return ret;
545         }
546
547         tts_core_notify_state_changed(client, TTS_STATE_CREATED);
548
549         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
550         return TTS_ERROR_NONE;
551 }
552
553 int tts_foreach_supported_voices(tts_h tts, tts_supported_voice_cb callback, void* user_data)
554 {
555         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
556
557         SLOG(LOG_INFO, TAG_TTSC, "@@@ Foreach supported voices");
558
559         RETVM_IF(NULL == callback, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is null");
560
561         tts_client_s* client = tts_client_get(tts);
562         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
563
564         int ret = 0;
565         char* current_engine = NULL;
566         ret = tts_config_mgr_get_engine(&current_engine);
567         if (0 != ret || NULL == current_engine) {
568                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get current engine : %d", ret);
569                 return __tts_convert_config_error_code(ret);
570         }
571
572         ret = tts_core_foreach_supported_voices(client, current_engine, callback, user_data);
573         free(current_engine);
574
575         if (0 != ret) {
576                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Result : %d", ret);
577                 return ret;
578         }
579
580         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
581         return TTS_ERROR_NONE;
582 }
583
584 int tts_get_default_voice(tts_h tts, char** lang, int* vctype)
585 {
586         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
587
588         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Get default voice");
589
590         RETVM_IF(NULL == lang || NULL == vctype, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid");
591
592         tts_client_s* client = tts_client_get(tts);
593         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
594
595         /* Request call remote method */
596         int ret = 0;
597         ret = tts_config_mgr_get_voice(lang, vctype);
598         if (0 != ret) {
599                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %d", ret);
600                 return __tts_convert_config_error_code(ret);
601         }
602
603         SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] Default language(%s), type(%d)", *lang, *vctype);
604         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
605         return TTS_ERROR_NONE;
606 }
607
608 int tts_get_max_text_size(tts_h tts, unsigned int* size)
609 {
610         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
611
612         RETVM_IF(NULL == size, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Get max text count : Input parameter is null");
613
614         tts_client_s* client = tts_client_get(tts);
615         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
616
617         tts_state_e current_state = tts_client_get_current_state(client);
618         RETVM_IF(TTS_STATE_READY != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
619
620         if (0 != tts_config_mgr_get_max_text_size(size)) {
621                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get max text size");
622                 return TTS_ERROR_OPERATION_FAILED;
623         }
624
625         SLOG(LOG_INFO, TAG_TTSC, "Get max text size : %d byte", *size);
626         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
627         return TTS_ERROR_NONE;
628 }
629
630 int tts_get_state(tts_h tts, tts_state_e* state)
631 {
632         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
633
634         RETVM_IF(NULL == state, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Get state : Input parameter is null");
635
636         tts_client_s* client = tts_client_get(tts);
637         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
638
639         tts_state_e current_state = tts_client_get_current_state(client);
640         switch (current_state) {
641         case TTS_STATE_CREATED: SLOG(LOG_INFO, TAG_TTSC, "Current state is 'Created'"); break;
642         case TTS_STATE_READY:   SLOG(LOG_INFO, TAG_TTSC, "Current state is 'Ready'");   break;
643         case TTS_STATE_PLAYING: SLOG(LOG_INFO, TAG_TTSC, "Current state is 'Playing'"); break;
644         case TTS_STATE_PAUSED:  SLOG(LOG_INFO, TAG_TTSC, "Current state is 'Paused'");  break;
645         default:
646                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Client is not valid");
647                 return TTS_ERROR_INVALID_PARAMETER;
648         }
649
650         *state = current_state;
651         return TTS_ERROR_NONE;
652 }
653
654 int tts_get_speed_range(tts_h tts, int* min, int* normal, int* max)
655 {
656         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
657
658         RETVM_IF(NULL == min || NULL == normal || NULL == max, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is null");
659
660         tts_client_s* client = tts_client_get(tts);
661         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
662
663         *min = TTS_SPEED_MIN;
664         *normal = TTS_SPEED_NORMAL;
665         *max = TTS_SPEED_MAX;
666
667         return TTS_ERROR_NONE;
668 }
669
670 int tts_get_error_message(tts_h tts, char** err_msg)
671 {
672         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
673
674         RETVM_IF(NULL == err_msg, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is null");
675
676         tts_client_s* client = tts_client_get(tts);
677         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
678
679         const char* error_message = tts_client_get_error_message(client);
680         if (NULL != error_message) {
681                 *err_msg = strdup(error_message);
682                 SLOG(LOG_DEBUG, TAG_TTSC, "Error msg (%s)", error_message);
683         } else {
684                 *err_msg = NULL;
685                 SLOG(LOG_DEBUG, TAG_TTSC, "Error msg (NULL)");
686         }
687
688         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
689         return TTS_ERROR_NONE;
690 }
691
692 int tts_check_screen_reader_on(tts_h tts, bool* is_on)
693 {
694         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
695
696         RETVM_IF(NULL == is_on, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is null");
697
698         tts_client_s* client = tts_client_get(tts);
699         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
700
701         if (false == tts_core_check_screen_reader(client)) {
702                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is not available");
703                 *is_on = false;
704         } else {
705                 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Screen reader option is available");
706                 *is_on = true;
707         }
708
709         return TTS_ERROR_NONE;
710 }
711
712 int tts_get_service_state(tts_h tts, tts_service_state_e* service_state)
713 {
714         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
715
716         RETVM_IF(NULL == service_state, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Get service state : Input parameter is null");
717
718         tts_client_s* client = tts_client_get(tts);
719         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
720
721         tts_state_e current_state = tts_client_get_current_state(client);
722         RETVM_IF(TTS_STATE_CREATED == current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
723
724         // TODO: Implement business logic
725
726         return TTS_ERROR_NONE;
727 }
728
729 int tts_add_text(tts_h tts, const char* text, const char* language, int voice_type, int speed, int* utt_id)
730 {
731         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
732
733         SLOG(LOG_ERROR, TAG_TTSC, "[INFO] Add text: text(%s), language(%s), type(%d)", (NULL == text) ? "NULL" : text, (NULL == language) ? "NULL" : language, voice_type);
734
735         RETVM_IF(TTS_SPEED_AUTO > speed || TTS_SPEED_MAX < speed, TTS_ERROR_INVALID_PARAMETER, "[ERROR] speed value(%d) is invalid.", speed);
736         RETVM_IF(voice_type < 0, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Voice type should not be negative(%d)", voice_type);
737         RETVM_IF(NULL == utt_id, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is null");
738         RETVM_IF(false == tts_core_is_valid_text(text), TTS_ERROR_INVALID_PARAMETER, "[ERROR] Input text is invalid");
739
740         tts_client_s* client = tts_client_get(tts);
741         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
742
743         tts_state_e current_state = tts_client_get_current_state(client);
744         RETVM_IF(TTS_STATE_CREATED == current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
745
746         RETVM_IF(false == tts_core_check_screen_reader(client), TTS_ERROR_SCREEN_READER_OFF, "[ERROR] Screen reader option is not available");
747         RETVM_IF(false == tts_core_check_credential(client), TTS_ERROR_PERMISSION_DENIED, "[ERROR] Do not have app credential for this engine");
748
749         int ret = tts_core_add_text(client, text, language, voice_type, speed, utt_id);
750         if (TTS_ERROR_NONE != ret) {
751                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request add text. ret(%s)", tts_core_covert_error_code(ret));
752                 return ret;
753         }
754
755         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
756         return TTS_ERROR_NONE;
757 }
758
759 //LCOV_EXCL_START
760 static void __tts_play_async(void *data)
761 {
762         tts_h tts = (tts_h)data;
763
764         tts_client_s* client = tts_client_get(tts);
765         RETM_IF(NULL == client, "[ERROR] A handle is not valid. tts(%p)", tts);
766
767         int ret = tts_core_play(client);
768         if (0 != ret) {
769                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to play tts : %s", tts_core_covert_error_code(ret));
770                 tts_core_notify_error_async(client, ret, -1, NULL);
771                 return;
772         }
773
774         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
775         return;
776 }
777
778 int tts_play_async(tts_h tts)
779 {
780         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
781
782         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Play asynchronously tts");
783
784         tts_client_s* client = tts_client_get(tts);
785         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
786
787         tts_state_e current_state = tts_client_get_current_state(client);
788         RETVM_IF(TTS_STATE_PLAYING == current_state || TTS_STATE_CREATED == current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
789
790         RETVM_IF(false == tts_core_check_screen_reader(client), TTS_ERROR_SCREEN_READER_OFF, "[ERROR] Screen reader option is not available");
791         RETVM_IF(false == tts_core_check_credential(client), TTS_ERROR_PERMISSION_DENIED, "[ERROR] Do not have app credential for this engine");
792
793         ecore_main_loop_thread_safe_call_async(__tts_play_async, (void*)tts);
794
795         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
796         return TTS_ERROR_NONE;
797 }
798 //LCOV_EXCL_STOP
799
800 int tts_play(tts_h tts)
801 {
802         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
803
804         SLOG(LOG_INFO, TAG_TTSC, "@@@ Play tts");
805
806         tts_client_s* client = tts_client_get(tts);
807         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
808
809         tts_state_e current_state = tts_client_get_current_state(client);
810         RETVM_IF(TTS_STATE_PLAYING == current_state || TTS_STATE_CREATED == current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
811
812         RETVM_IF(false == tts_core_check_screen_reader(client), TTS_ERROR_SCREEN_READER_OFF, "[ERROR] Screen reader option is not available");
813         RETVM_IF(false == tts_core_check_credential(client), TTS_ERROR_PERMISSION_DENIED, "[ERROR] Do not have app credential for this engine");
814
815         int ret = tts_core_play(client);
816         if (TTS_ERROR_NONE != ret) {
817                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request play. ret(%s)", tts_core_covert_error_code(ret));
818                 return ret;
819         }
820
821         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
822         return TTS_ERROR_NONE;
823 }
824
825 //LCOV_EXCL_START
826 static void __tts_stop_async(void *data)
827 {
828         tts_h tts = (tts_h)data;
829
830         tts_client_s* client = tts_client_get(tts);
831         RETM_IF(NULL == client, "[ERROR] A handle is not valid. tts(%p)", tts);
832
833         int ret = tts_core_stop(client);
834         if (0 != ret) {
835                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to stop tts : %s", tts_core_covert_error_code(ret));
836                 tts_core_notify_error_async(client, ret, -1, NULL);
837                 return;
838         }
839
840         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
841         return;
842 }
843
844 int tts_stop_aync(tts_h tts)
845 {
846         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
847
848         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Stop asynchronously tts");
849
850         tts_client_s* client = tts_client_get(tts);
851         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
852
853         tts_state_e current_state = tts_client_get_current_state(client);
854         RETVM_IF(TTS_STATE_CREATED == current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
855
856         RETVM_IF(false == tts_core_check_screen_reader(client), TTS_ERROR_SCREEN_READER_OFF, "[ERROR] Screen reader option is not available");
857
858         ecore_main_loop_thread_safe_call_async(__tts_stop_async, (void*)tts);
859
860         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
861         return TTS_ERROR_NONE;
862 }
863 //LCOV_EXCL_STOP
864
865 int tts_stop(tts_h tts)
866 {
867         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
868
869         SLOG(LOG_INFO, TAG_TTSC, "@@@ Stop tts");
870
871         tts_client_s* client = tts_client_get(tts);
872         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
873
874         tts_state_e current_state = tts_client_get_current_state(client);
875         RETVM_IF(TTS_STATE_CREATED == current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
876
877         RETVM_IF(false == tts_core_check_screen_reader(client), TTS_ERROR_SCREEN_READER_OFF, "[ERROR] Screen reader option is not available");
878
879         int ret = tts_core_stop(client);
880         if (TTS_ERROR_NONE != ret) {
881                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request stop. ret(%s)", tts_core_covert_error_code(ret));
882                 return ret;
883         }
884
885         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
886         return TTS_ERROR_NONE;
887 }
888
889 //LCOV_EXCL_START
890 static void __tts_pause_async(void *data)
891 {
892         tts_h tts = (tts_h)data;
893
894         tts_client_s* client = tts_client_get(tts);
895         RETM_IF(NULL == client, "[ERROR] A handle is not valid. tts(%p)", tts);
896
897         int ret = tts_core_pause(client);
898         if (0 != ret) {
899                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to pause tts : %s", tts_core_covert_error_code(ret));
900                 tts_core_notify_error_async(client, ret, -1, NULL);
901                 return;
902         }
903
904         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
905         return;
906 }
907
908 int tts_pause_async(tts_h tts)
909 {
910         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
911
912         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Pause asynchronously tts");
913
914         tts_client_s* client = tts_client_get(tts);
915         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
916
917         tts_state_e current_state = tts_client_get_current_state(client);
918         RETVM_IF(TTS_STATE_PLAYING != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
919
920         RETVM_IF(false == tts_core_check_screen_reader(client), TTS_ERROR_SCREEN_READER_OFF, "[ERROR] Screen reader option is not available");
921
922         ecore_main_loop_thread_safe_call_async(__tts_pause_async, (void*)tts);
923
924         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
925         return TTS_ERROR_NONE;
926 }
927 //LCOV_EXCL_STOP
928 int tts_pause(tts_h tts)
929 {
930         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
931
932         SLOG(LOG_INFO, TAG_TTSC, "@@@ Pause tts");
933
934         tts_client_s* client = tts_client_get(tts);
935         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
936
937         tts_state_e current_state = tts_client_get_current_state(client);
938         RETVM_IF(TTS_STATE_PLAYING != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
939
940         RETVM_IF(false == tts_core_check_screen_reader(client), TTS_ERROR_SCREEN_READER_OFF, "[ERROR] Screen reader option is not available");
941
942         int ret = tts_core_pause(client);
943         if (TTS_ERROR_NONE != ret) {
944                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request pause. ret(%s)", tts_core_covert_error_code(ret));
945                 return ret;
946         }
947
948         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
949         return TTS_ERROR_NONE;
950 }
951
952 int tts_set_private_data(tts_h tts, const char* key, const char* data)
953 {
954         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
955
956         SLOG(LOG_INFO, TAG_TTSC, "@@@ Set private data, key(%s), data(%s)", key, data);
957
958         RETVM_IF(NULL == key || NULL == data, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Invalid parameter");
959
960         tts_client_s* client = tts_client_get(tts);
961         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
962
963         tts_state_e current_state = tts_client_get_current_state(client);
964         RETVM_IF(TTS_STATE_READY != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
965
966         if (true != client->internal && (0 == strcmp(key, "EnableServerTTS") || 0 == strcmp(key, "DisableServerTTS"))) {
967                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] This is not an internal app");
968                 return TTS_ERROR_INVALID_PARAMETER;
969         }
970
971         int ret = tts_core_set_private_data(client, key, data);
972         if (TTS_ERROR_NONE != ret) {
973                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request set private data. ret(%s)", tts_core_covert_error_code(ret));
974                 return ret;
975         }
976
977         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
978         return 0;
979 }
980
981 int tts_get_private_data(tts_h tts, const char* key, char** data)
982 {
983         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
984
985         SLOG(LOG_INFO, TAG_TTSC, "@@@ Get private data, key(%s)", key);
986
987         RETVM_IF(NULL == key || NULL == data, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Invalid parameter");
988
989         tts_client_s* client = tts_client_get(tts);
990         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
991
992         tts_state_e current_state = tts_client_get_current_state(client);
993         RETVM_IF(TTS_STATE_READY != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
994
995         int ret = tts_core_get_private_data(client, key, data);
996         if (TTS_ERROR_NONE != ret) {
997                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request get private data. ret(%s)", tts_core_covert_error_code(ret));
998                 return ret;
999         }
1000
1001         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1002         return 0;
1003 }
1004
1005 int tts_set_state_changed_cb(tts_h tts, tts_state_changed_cb callback, void* user_data)
1006 {
1007         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1008
1009         tts_client_s* client = tts_client_get(tts);
1010         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1011
1012         RETVM_IF(NULL == callback, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Set state changed cb : Input parameter is null");
1013
1014         tts_state_e current_state = tts_client_get_current_state(client);
1015         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1016
1017         tts_client_set_state_changed_cb(client, callback, user_data);
1018
1019         SLOG(LOG_INFO, TAG_TTSC, "[SUCCESS] Set state changed cb");
1020         return 0;
1021 }
1022
1023 int tts_unset_state_changed_cb(tts_h tts)
1024 {
1025         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1026
1027         tts_client_s* client = tts_client_get(tts);
1028         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1029
1030         tts_state_e current_state = tts_client_get_current_state(client);
1031         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1032
1033         tts_client_set_state_changed_cb(client, NULL, NULL);
1034
1035         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset state changed cb");
1036         return 0;
1037 }
1038
1039 int tts_set_utterance_started_cb(tts_h tts, tts_utterance_started_cb callback, void* user_data)
1040 {
1041         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1042
1043         tts_client_s* client = tts_client_get(tts);
1044         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1045
1046         RETVM_IF(NULL == callback, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Set utt started cb : Input parameter is null");
1047
1048         tts_state_e current_state = tts_client_get_current_state(client);
1049         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1050
1051         tts_client_set_utterance_started_cb(client, callback, user_data);
1052
1053         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set utt started cb");
1054
1055         return 0;
1056 }
1057
1058 int tts_unset_utterance_started_cb(tts_h tts)
1059 {
1060         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1061
1062         tts_client_s* client = tts_client_get(tts);
1063         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1064
1065         tts_state_e current_state = tts_client_get_current_state(client);
1066         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1067
1068         tts_client_set_utterance_started_cb(client, NULL, NULL);
1069
1070         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset utt started cb");
1071
1072         return 0;
1073 }
1074
1075 int tts_set_utterance_completed_cb(tts_h tts, tts_utterance_completed_cb callback, void* user_data)
1076 {
1077         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1078
1079         tts_client_s* client = tts_client_get(tts);
1080         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1081
1082         RETVM_IF(NULL == callback, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Set utt completed cb : Input parameter is null");
1083
1084         tts_state_e current_state = tts_client_get_current_state(client);
1085         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1086
1087         tts_client_set_utterance_completed_cb(client, callback, user_data);
1088
1089         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set utt completed cb");
1090
1091         return 0;
1092 }
1093
1094 int tts_unset_utterance_completed_cb(tts_h tts)
1095 {
1096         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1097
1098         tts_client_s* client = tts_client_get(tts);
1099         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1100
1101         tts_state_e current_state = tts_client_get_current_state(client);
1102         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1103
1104         tts_client_set_utterance_completed_cb(client, NULL, NULL);
1105
1106         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset utt completed cb");
1107         return 0;
1108 }
1109
1110 int tts_set_error_cb(tts_h tts, tts_error_cb callback, void* user_data)
1111 {
1112         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1113
1114         tts_client_s* client = tts_client_get(tts);
1115         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1116
1117         RETVM_IF(NULL == callback, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Set error cb : Input parameter is null");
1118
1119         tts_state_e current_state = tts_client_get_current_state(client);
1120         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1121
1122         tts_client_set_error_cb(client, callback, user_data);
1123
1124         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set error cb");
1125
1126         return 0;
1127 }
1128
1129 int tts_unset_error_cb(tts_h tts)
1130 {
1131         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1132
1133         tts_client_s* client = tts_client_get(tts);
1134         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1135
1136         tts_state_e current_state = tts_client_get_current_state(client);
1137         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1138
1139         tts_client_set_error_cb(client, NULL, NULL);
1140
1141         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset error cb");
1142
1143         return 0;
1144 }
1145
1146 int tts_set_default_voice_changed_cb(tts_h tts, tts_default_voice_changed_cb callback, void* user_data)
1147 {
1148         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1149
1150         tts_client_s* client = tts_client_get(tts);
1151         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1152
1153         RETVM_IF(NULL == callback, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Set default voice changed cb : Input parameter is null");
1154
1155         tts_state_e current_state = tts_client_get_current_state(client);
1156         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1157
1158         tts_client_set_default_voice_changed_cb(client, callback, user_data);
1159
1160         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set default voice changed cb");
1161
1162         return 0;
1163 }
1164
1165 int tts_unset_default_voice_changed_cb(tts_h tts)
1166 {
1167         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1168
1169         tts_client_s* client = tts_client_get(tts);
1170         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1171
1172         tts_state_e current_state = tts_client_get_current_state(client);
1173         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1174
1175         tts_client_set_default_voice_changed_cb(client, NULL, NULL);
1176
1177         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset default voice changed cb");
1178
1179         return 0;
1180 }
1181
1182 int tts_set_engine_changed_cb(tts_h tts, tts_engine_changed_cb callback, void* user_data)
1183 {
1184         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1185
1186         tts_client_s* client = tts_client_get(tts);
1187         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1188
1189         RETVM_IF(NULL == callback, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Set engine changed cb : Input parameter is null");
1190
1191         tts_state_e current_state = tts_client_get_current_state(client);
1192         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1193
1194         tts_client_set_engine_changed_cb(client, callback, user_data);
1195
1196         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set engine changed cb");
1197
1198         return 0;
1199 }
1200
1201 int tts_unset_engine_changed_cb(tts_h tts)
1202 {
1203         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1204
1205         tts_client_s* client = tts_client_get(tts);
1206         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1207
1208         tts_state_e current_state = tts_client_get_current_state(client);
1209         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1210
1211         tts_client_set_engine_changed_cb(client, NULL, NULL);
1212
1213         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset engine changed cb");
1214
1215         return 0;
1216 }
1217
1218 int tts_set_screen_reader_changed_cb(tts_h tts, tts_screen_reader_changed_cb callback, void* user_data)
1219 {
1220         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1221
1222         tts_client_s* client = tts_client_get(tts);
1223         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1224
1225         RETVM_IF(NULL == callback, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Set screen reader changed cb : Input parameter is null");
1226
1227         tts_state_e current_state = tts_client_get_current_state(client);
1228         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1229
1230         tts_client_set_screen_reader_changed_cb(client, callback, user_data);
1231
1232         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set screen reader changed cb");
1233
1234         return 0;
1235 }
1236
1237 int tts_unset_screen_reader_changed_cb(tts_h tts)
1238 {
1239         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1240
1241         tts_client_s* client = tts_client_get(tts);
1242         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1243
1244         tts_state_e current_state = tts_client_get_current_state(client);
1245         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1246
1247         tts_client_set_screen_reader_changed_cb(client, NULL, NULL);
1248
1249         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset screen reader changed cb");
1250
1251         return 0;
1252 }
1253
1254 int tts_add_pcm(tts_h tts, int event, const void* data, unsigned int data_size, int audio_type, int rate)
1255 {
1256         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1257
1258         SLOG(LOG_INFO, TAG_TTSC, "@@@ Add pcm tts");
1259
1260         tts_client_s* client = tts_client_get(tts);
1261         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1262
1263         tts_state_e current_state = tts_client_get_current_state(client);
1264         RETVM_IF(TTS_STATE_CREATED == current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1265
1266         RETVM_IF(false == tts_core_check_screen_reader(client), TTS_ERROR_SCREEN_READER_OFF, "[ERROR] Screen reader option is not available");
1267
1268         int ret = tts_core_add_pcm(client, event, data, data_size, audio_type, rate);
1269         if (TTS_ERROR_NONE != ret) {
1270                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request add_pcm. ret(%s)", tts_core_covert_error_code(ret));
1271                 return ret;
1272         }
1273
1274         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1275         return TTS_ERROR_NONE;
1276 }
1277
1278 int tts_play_pcm(tts_h tts)
1279 {
1280         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1281
1282         SLOG(LOG_INFO, TAG_TTSC, "@@@ Play pcm tts");
1283
1284         tts_client_s* client = tts_client_get(tts);
1285         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1286
1287         tts_state_e current_state = tts_client_get_current_state(client);
1288         RETVM_IF(TTS_STATE_PLAYING == current_state || TTS_STATE_CREATED == current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1289
1290         RETVM_IF(false == tts_core_check_screen_reader(client), TTS_ERROR_SCREEN_READER_OFF, "[ERROR] Screen reader option is not available");
1291
1292         int ret = tts_core_play_pcm(client);
1293         if (TTS_ERROR_NONE != ret) {
1294                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request play_pcm. ret(%s)", tts_core_covert_error_code(ret));
1295                 return ret;
1296         }
1297
1298         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1299         return TTS_ERROR_NONE;
1300 }
1301
1302 int tts_stop_pcm(tts_h tts)
1303 {
1304         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1305
1306         SLOG(LOG_INFO, TAG_TTSC, "@@@ Stop pcm tts");
1307
1308         tts_client_s* client = tts_client_get(tts);
1309         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1310
1311         tts_state_e current_state = tts_client_get_current_state(client);
1312         RETVM_IF(TTS_STATE_CREATED == current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1313
1314         RETVM_IF(false == tts_core_check_screen_reader(client), TTS_ERROR_SCREEN_READER_OFF, "[ERROR] Screen reader option is not available");
1315
1316         int ret = tts_core_stop_pcm(client);
1317         if (TTS_ERROR_NONE != ret) {
1318                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request stop_pcm. ret(%s)", tts_core_covert_error_code(ret));
1319                 return ret;
1320         }
1321
1322         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1323         return TTS_ERROR_NONE;
1324 }
1325
1326 int tts_repeat(tts_h tts, char** text_repeat, int* utt_id)
1327 {
1328         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1329
1330         SLOG(LOG_INFO, TAG_TTSC, "@@@ Repeat TTS");
1331
1332         RETVM_IF(NULL == text_repeat || NULL == utt_id, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is null.");
1333
1334         tts_client_s* client = tts_client_get(tts);
1335         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1336
1337         tts_state_e current_state = tts_client_get_current_state(client);
1338         RETVM_IF(TTS_STATE_READY != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1339
1340         RETVM_IF(false == tts_core_check_screen_reader(client), TTS_ERROR_SCREEN_READER_OFF, "[ERROR] Screen reader option is not available");
1341
1342         if (true == client->credential_needed && NULL == client->credential) {
1343                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
1344                 return TTS_ERROR_PERMISSION_DENIED;
1345         }
1346
1347         *text_repeat = NULL;
1348         *utt_id = -1;
1349
1350         int ret = tts_core_repeat(client, text_repeat, utt_id);
1351         if (TTS_ERROR_NONE != ret) {
1352                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request stop. ret(%s)", tts_core_covert_error_code(ret));
1353                 return ret;
1354         }
1355
1356         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1357         return TTS_ERROR_NONE;
1358 }
1359
1360 int tts_set_service_state_changed_cb(tts_h tts, tts_service_state_changed_cb callback, void* user_data)
1361 {
1362         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1363
1364         tts_client_s* client = tts_client_get(tts);
1365         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1366         RETVM_IF(NULL == callback, TTS_ERROR_INVALID_PARAMETER, "[ERROR] Set service state changed cb : Input parameter is null");
1367
1368         tts_state_e current_state = tts_client_get_current_state(client);
1369         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1370
1371         // TODO: Implement business logic
1372
1373         SLOG(LOG_INFO, TAG_TTSC, "[SUCCESS] Set service state changed cb");
1374         return TTS_ERROR_NONE;
1375 }
1376
1377 int tts_unset_service_state_changed_cb(tts_h tts)
1378 {
1379         RETV_IF(TTS_ERROR_NONE != __tts_get_feature_enabled(), TTS_ERROR_NOT_SUPPORTED);
1380
1381         tts_client_s* client = tts_client_get(tts);
1382         RETVM_IF(NULL == client, TTS_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not valid. tts(%p)", tts);
1383
1384         tts_state_e current_state = tts_client_get_current_state(client);
1385         RETVM_IF(TTS_STATE_CREATED != current_state, TTS_ERROR_INVALID_STATE, "[ERROR] The current state(%d) is invalid", current_state);
1386
1387         // TODO: Implement business logic
1388
1389         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset service state changed cb");
1390         return TTS_ERROR_NONE;
1391 }