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